PcapPlusPlus
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)
 
DhcpOptionDatagetFirstOptionData ()
 
DhcpOptionDatagetNextOptionData (DhcpOptionData *dhcpOption)
 
DhcpOptionDatagetOptionData (DhcpOptionTypes option)
 
size_t getOptionsCount ()
 
DhcpOptionDataaddOption (DhcpOptionTypes optionType, uint16_t optionLen, const uint8_t *optionData)
 
DhcpOptionDataaddOptionAfter (DhcpOptionTypes optionType, uint16_t optionLen, const uint8_t *optionData, 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

Parameters
[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

Parameters
[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 ( )
inlinevirtual

A destructor for this layer

Member Function Documentation

◆ addOption()

DhcpOptionData* pcpp::DhcpLayer::addOption ( DhcpOptionTypes  optionType,
uint16_t  optionLen,
const uint8_t *  optionData 
)

Add a new DHCP option at the end of the layer (but before the DHCPOPT_END option if exists)

Parameters
[in]optionTypeThe type of the newly added option
[in]optionLenThe length of the option data
[in]optionDataA pointer to the option data. This data will be copied to newly added option data. Notice the length of optionData must be optionLen
Returns
A pointer to the newly added DHCP option data or NULL if addition failed. Notice this is a pointer to the real data casted to DhcpOptionData type (as opposed to a copy of the option data). So changes in this return value will affect the packet data

◆ addOptionAfter()

DhcpOptionData* pcpp::DhcpLayer::addOptionAfter ( DhcpOptionTypes  optionType,
uint16_t  optionLen,
const uint8_t *  optionData,
DhcpOptionTypes  prevOption 
)

Add a new DHCP option after an existing option

Parameters
[in]optionTypeThe type of the newly added option
[in]optionLenThe length of the option data
[in]optionDataA pointer to the option data. This data will be copied to added option data. Notice the length of optionData must be optionLength
[in]prevOptionThe DHCP option which the newly added tag will come after. If set to DHCPOPT_UNKNOWN DHCP option will be added as the first DHCP option
Returns
A pointer to the newly added option or NULL if addition failed. Notice this is a pointer to the real data casted to DhcpOptionData type (as opposed to a copy of the option data). So changes in this return value will affect the packet data

◆ computeCalculateFields()

void pcpp::DhcpLayer::computeCalculateFields ( )
virtual

Calculate the following fields:

Implements pcpp::Layer.

◆ getClientHardwareAddress()

MacAddress pcpp::DhcpLayer::getClientHardwareAddress ( )
Returns
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 ( )
Returns
The client IPv4 address (as extracted from dhcp_header::clientIpAddress converted to IPv4Address object)

◆ getDhcpHeader()

dhcp_header* pcpp::DhcpLayer::getDhcpHeader ( )
inline

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

Returns
A pointer to the dhcp_header

◆ getFirstOptionData()

DhcpOptionData* pcpp::DhcpLayer::getFirstOptionData ( )
Returns
The first DHCP option, or NULL if no options exist. Notice the return value is a pointer to the real data casted to DhcpOptionData type (as opposed to a copy of the option data). So changes in the return value will affect the packet data

◆ getGatewayIpAddress()

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

◆ getHeaderLen()

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

Implements pcpp::Layer.

◆ getMesageType()

DhcpMessageType pcpp::DhcpLayer::getMesageType ( )
Returns
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()

DhcpOptionData* pcpp::DhcpLayer::getNextOptionData ( DhcpOptionData dhcpOption)

Get the DHCP option that comes next to "dhcpOption" option. If "dhcpOption" is NULL then NULL will be returned. If "dhcpOption" is the last DHCP option NULL will be returned. Notice the return value is a pointer to the real data casted to DhcpOptionData type (as opposed to a copy of the option data). So changes in the return value will affect the packet data

Parameters
[in]dhcpOptionThe DHCP option to start searching from
Returns
The next DHCP option or NULL if "dhcpOption" is NULL or "dhcpOption" is the last DHCP option

◆ getOpCode()

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

◆ getOptionData()

DhcpOptionData* pcpp::DhcpLayer::getOptionData ( DhcpOptionTypes  option)

Search for a DHCP option by type. Notice the return value points directly to the data, so every change will change the actual packet data

Parameters
[in]optionThe DHCP option type to search
Returns
A pointer to the DHCP option in this layer

◆ getOptionsCount()

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

◆ getOsiModelLayer()

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

Implements pcpp::Layer.

◆ getServerIpAddress()

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

◆ getYourIpAddress()

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

◆ parseNextLayer()

void pcpp::DhcpLayer::parseNextLayer ( )
inlinevirtual

Does nothing for this layer (DhcpLayer is always last)

Implements pcpp::Layer.

◆ removeAllOptions()

bool pcpp::DhcpLayer::removeAllOptions ( )

Remove all DHCP options in this layer

Returns
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

Parameters
[in]optionTypeThe DHCP option type to remove
Returns
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)

Parameters
[in]addrThe MAC address to set

◆ setClientIpAddress()

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

Set the client IPv4 address in dhcp_header::clientIpAddress

Parameters
[in]addrThe IPv4 address to set

◆ setGatewayIpAddress()

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

Set the gateway IPv4 address in dhcp_header::gatewayIpAddress

Parameters
[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

Parameters
[in]msgTypeMessage type to set
Returns
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

Parameters
[in]addrThe IPv4 address to set

◆ setYourIpAddress()

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

Set your IPv4 address in dhcp_header::yourIpAddress

Parameters
[in]addrThe IPv4 address to set

◆ toString()

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

Implements pcpp::Layer.