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

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()

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

Add a new DHCP option at the end of the layer

Parameters
[in]optionBuilderA DhcpOptionBuilder object that contains the requested DHCP option data to add
Returns
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

Parameters
[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
Returns
A DhcpOption object containing the newly added DHCP option data or logical NULL (DhcpOption::isNull() == true) if addition failed

◆ computeCalculateFields()

void pcpp::DhcpLayer::computeCalculateFields ( )
virtual

Calculate the following fields:

Implements pcpp::Layer.

◆ getClientHardwareAddress()

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

◆ getDhcpHeader()

dhcp_header* pcpp::DhcpLayer::getDhcpHeader ( ) const
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()

DhcpOption pcpp::DhcpLayer::getFirstOptionData ( ) const
Returns
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 ( ) const
inline
Returns
Gateway IPv4 address (as extracted from dhcp_header::gatewayIpAddress converted to IPv4Address object)

◆ getHeaderLen()

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

Implements pcpp::Layer.

◆ getMesageType()

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

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

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)

Parameters
[in]dhcpOptionA given DHCP option
Returns
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 ( ) const
inline
Returns
The BootP opcode of this message

◆ getOptionData()

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

Get a DHCP option by type

Parameters
[in]optionDHCP option type
Returns
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 ( ) const
Returns
The number of DHCP options in this layer

◆ getOsiModelLayer()

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

Implements pcpp::Layer.

◆ getServerIpAddress()

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

◆ getYourIpAddress()

IPv4Address pcpp::DhcpLayer::getYourIpAddress ( ) const
inline
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)
inline

Set the client IPv4 address in dhcp_header::clientIpAddress

Parameters
[in]addrThe IPv4 address to set

◆ setGatewayIpAddress()

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

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)
inline

Set the server IPv4 address in dhcp_header::serverIpAddress

Parameters
[in]addrThe IPv4 address to set

◆ setYourIpAddress()

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

Set your IPv4 address in dhcp_header::yourIpAddress

Parameters
[in]addrThe IPv4 address to set

◆ toString()

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

Implements pcpp::Layer.