pcpp::DhcpLayer Class Reference

#include <DhcpLayer.h>

Inheritance diagram for pcpp::DhcpLayer:
pcpp::Layer pcpp::IDataContainer

Public Member Functions

 DhcpLayer (uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
 DhcpLayer (DhcpMessageType msgType, const MacAddress &clientMacAddr)
 DhcpLayer ()
virtual ~DhcpLayer ()
dhcp_headergetDhcpHeader ()
BootpOpCodes getOpCode ()
IPv4Address getClientIpAddress ()
void setClientIpAddress (const IPv4Address &addr)
IPv4Address getServerIpAddress ()
void setServerIpAddress (const IPv4Address &addr)
IPv4Address getYourIpAddress ()
void setYourIpAddress (const IPv4Address &addr)
IPv4Address getGatewayIpAddress ()
void setGatewayIpAddress (const IPv4Address &addr)
MacAddress getClientHardwareAddress ()
void setClientHardwareAddress (const MacAddress &addr)
DhcpMessageType getMesageType ()
bool setMesageType (DhcpMessageType msgType)
DhcpOption getFirstOptionData ()
DhcpOption getNextOptionData (DhcpOption dhcpOption)
DhcpOption getOptionData (DhcpOptionTypes option)
size_t getOptionsCount ()
DhcpOption addOption (const DhcpOptionBuilder &optionBuilder)
DhcpOption addOptionAfter (const DhcpOptionBuilder &optionBuilder, DhcpOptionTypes prevOption)
bool removeOption (DhcpOptionTypes optionType)
bool removeAllOptions ()
void parseNextLayer ()
size_t getHeaderLen ()
void computeCalculateFields ()
std::string toString ()
OsiModelLayer getOsiModelLayer ()
- Public Member Functions inherited from pcpp::Layer
virtual ~Layer ()
LayergetNextLayer ()
LayergetPrevLayer ()
ProtocolType getProtocol ()
uint8_t * getData ()
size_t getDataLen ()
uint8_t * getLayerPayload ()
size_t getLayerPayloadSize ()
bool isAllocatedToPacket ()
void copyData (uint8_t *toArr)
uint8_t * getDataPtr (size_t offset=0)

Detailed Description

Represents a DHCP (Dynamic Host Configuration Protocol) protocol layer

Constructor & Destructor Documentation

◆ DhcpLayer() [1/3]

pcpp::DhcpLayer::DhcpLayer ( uint8_t *  data,
size_t  dataLen,
Layer prevLayer,
Packet packet 

A constructor that creates the layer from an existing packet raw data

[in]dataA pointer to the raw data
[in]dataLenSize of the data in bytes
[in]prevLayerA pointer to the previous layer
[in]packetA pointer to the Packet instance where layer will be stored in

◆ DhcpLayer() [2/3]

pcpp::DhcpLayer::DhcpLayer ( DhcpMessageType  msgType,
const MacAddress clientMacAddr 

A constructor that creates the layer from scratch. Adds a DHCPOPT_DHCP_MESSAGE_TYPE and a DHCPOPT_END options

[in]msgTypeA DHCP message type to be set
[in]clientMacAddrA client MAC address to set in dhcp_header::clientHardwareAddress field

◆ DhcpLayer() [3/3]

pcpp::DhcpLayer::DhcpLayer ( )

A constructor that creates the layer from scratch with clean data

◆ ~DhcpLayer()

virtual pcpp::DhcpLayer::~DhcpLayer ( )

A destructor for this layer

Member Function Documentation

◆ addOption()

DhcpOption pcpp::DhcpLayer::addOption ( const DhcpOptionBuilder optionBuilder)

Add a new DHCP option at the end of the layer

[in]optionBuilderA DhcpOptionBuilder object that contains the requested DHCP option data to add
A DhcpOption object containing the newly added DHCP option data or logical NULL (DhcpOption::isNull() == true) if addition failed

◆ addOptionAfter()

DhcpOption pcpp::DhcpLayer::addOptionAfter ( const DhcpOptionBuilder optionBuilder,
DhcpOptionTypes  prevOption 

Add a new DHCP option after an existing one

[in]optionBuilderA DhcpOptionBuilder object that contains the requested DHCP option data to add
[in]prevOptionThe DHCP option type which the newly added option will come after
A DhcpOption object containing the newly added DHCP option data or logical NULL (DhcpOption::isNull() == true) if addition failed

◆ computeCalculateFields()

void pcpp::DhcpLayer::computeCalculateFields ( )

Calculate the following fields:

Implements pcpp::Layer.

◆ getClientHardwareAddress()

MacAddress pcpp::DhcpLayer::getClientHardwareAddress ( )
The client MAC address as extracted from dhcp_header::clientHardwareAddress, assuming dhcp_header::hardwareType is 1 (Ethernet) and dhcp_header::hardwareAddressLength is 6 (MAC address length). Otherwise returns MacAddress::Zero

◆ getClientIpAddress()

IPv4Address pcpp::DhcpLayer::getClientIpAddress ( )
The client IPv4 address (as extracted from dhcp_header::clientIpAddress converted to IPv4Address object)

◆ getDhcpHeader()

dhcp_header* pcpp::DhcpLayer::getDhcpHeader ( )

Get a pointer to the DHCP header. Notice this points directly to the data, so every change will change the actual packet data

A pointer to the dhcp_header

◆ getFirstOptionData()

DhcpOption pcpp::DhcpLayer::getFirstOptionData ( )
The first DHCP option in the packet. If there are no DHCP options the returned value will contain a logical NULL (DhcpOption::isNull() == true)

◆ getGatewayIpAddress()

IPv4Address pcpp::DhcpLayer::getGatewayIpAddress ( )
Gateway IPv4 address (as extracted from dhcp_header::gatewayIpAddress converted to IPv4Address object)

◆ getHeaderLen()

size_t pcpp::DhcpLayer::getHeaderLen ( )
The size of dhcp_header + size of options

Implements pcpp::Layer.

◆ getMesageType()

DhcpMessageType pcpp::DhcpLayer::getMesageType ( )
DHCP message type as extracted from DHCPOPT_DHCP_MESSAGE_TYPE option. If this option doesn't exist the value of DHCP_UNKNOWN_MSG_TYPE is returned

◆ getNextOptionData()

DhcpOption pcpp::DhcpLayer::getNextOptionData ( DhcpOption  dhcpOption)

Get the DHCP option that comes after a given option. If the given option was the last one, the returned value will contain a logical NULL (DhcpOption::isNull() == true)

[in]dhcpOptionA given DHCP option
A DhcpOption object containing the option data that comes next, or logical NULL if the given DHCP option: (1) was the last one; (2) contains a logical NULL or (3) doesn't belong to this packet

◆ getOpCode()

BootpOpCodes pcpp::DhcpLayer::getOpCode ( )
The BootP opcode of this message

◆ getOptionData()

DhcpOption pcpp::DhcpLayer::getOptionData ( DhcpOptionTypes  option)

Get a DHCP option by type

[in]optionDHCP option type
A DhcpOption object containing the first DHCP option data that matches this type, or logical NULL (DhcpOption::isNull() == true) if no such option found

◆ getOptionsCount()

size_t pcpp::DhcpLayer::getOptionsCount ( )
The number of DHCP options in this layer

◆ getOsiModelLayer()

OsiModelLayer pcpp::DhcpLayer::getOsiModelLayer ( )
The OSI Model layer this protocol belongs to

Implements pcpp::Layer.

◆ getServerIpAddress()

IPv4Address pcpp::DhcpLayer::getServerIpAddress ( )
The server IPv4 address (as extracted from dhcp_header::serverIpAddress converted to IPv4Address object)

◆ getYourIpAddress()

IPv4Address pcpp::DhcpLayer::getYourIpAddress ( )
Your IPv4 address (as extracted from dhcp_header::yourIpAddress converted to IPv4Address object)

◆ parseNextLayer()

void pcpp::DhcpLayer::parseNextLayer ( )

Does nothing for this layer (DhcpLayer is always last)

Implements pcpp::Layer.

◆ removeAllOptions()

bool pcpp::DhcpLayer::removeAllOptions ( )

Remove all DHCP options in this layer

True if all DHCP options were successfully removed or false if removal failed for some reason

◆ removeOption()

bool pcpp::DhcpLayer::removeOption ( DhcpOptionTypes  optionType)

Remove an existing DHCP option from the layer

[in]optionTypeThe DHCP option type to remove
True if DHCP option was successfully removed or false if type wasn't found or if removal failed

◆ setClientHardwareAddress()

void pcpp::DhcpLayer::setClientHardwareAddress ( const MacAddress addr)

Set a MAC address into the first 6 bytes of dhcp_header::clientHardwareAddress. This method also sets dhcp_header::hardwareType to 1 (Ethernet) and dhcp_header::hardwareAddressLength to 6 (MAC address length)

[in]addrThe MAC address to set

◆ setClientIpAddress()

void pcpp::DhcpLayer::setClientIpAddress ( const IPv4Address addr)

Set the client IPv4 address in dhcp_header::clientIpAddress

[in]addrThe IPv4 address to set

◆ setGatewayIpAddress()

void pcpp::DhcpLayer::setGatewayIpAddress ( const IPv4Address addr)

Set the gateway IPv4 address in dhcp_header::gatewayIpAddress

[in]addrThe IPv4 address to set

◆ setMesageType()

bool pcpp::DhcpLayer::setMesageType ( DhcpMessageType  msgType)

Set DHCP message type. This method searches for existing DHCPOPT_DHCP_MESSAGE_TYPE option. If found, it sets the requested message type as its value. If not, it creates a DHCPOPT_DHCP_MESSAGE_TYPE option and sets the requested message type as its value

[in]msgTypeMessage type to set
True if message type was set successfully or false if msgType is DHCP_UNKNOWN_MSG_TYPE or if failed to add DHCPOPT_DHCP_MESSAGE_TYPE option

◆ setServerIpAddress()

void pcpp::DhcpLayer::setServerIpAddress ( const IPv4Address addr)

Set the server IPv4 address in dhcp_header::serverIpAddress

[in]addrThe IPv4 address to set

◆ setYourIpAddress()

void pcpp::DhcpLayer::setYourIpAddress ( const IPv4Address addr)

Set your IPv4 address in dhcp_header::yourIpAddress

[in]addrThe IPv4 address to set

◆ toString()

std::string pcpp::DhcpLayer::toString ( )
A string representation of the layer most important data (should look like the layer description in Wireshark)

Implements pcpp::Layer.