PcapPlusPlus  Next
pcpp::IPv4Layer Class Reference

#include <IPv4Layer.h>

Inheritance diagram for pcpp::IPv4Layer:
pcpp::Layer pcpp::IPLayer pcpp::IDataContainer

Public Member Functions

 IPv4Layer (uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
 
 IPv4Layer (uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet, bool setTotalLenAsDataLen)
 
 IPv4Layer ()
 
 IPv4Layer (const IPv4Address &srcIP, const IPv4Address &dstIP)
 
 IPv4Layer (const IPv4Layer &other)
 
IPv4Layeroperator= (const IPv4Layer &other)
 
iphdrgetIPv4Header () const
 
IPAddress getSrcIPAddress () const override
 
IPv4Address getSrcIPv4Address () const
 
void setSrcIPv4Address (const IPv4Address &ipAddr)
 
IPAddress getDstIPAddress () const override
 
IPv4Address getDstIPv4Address () const
 
void setDstIPv4Address (const IPv4Address &ipAddr)
 
bool isFragment () const
 
bool isFirstFragment () const
 
bool isLastFragment () const
 
uint8_t getFragmentFlags () const
 
uint16_t getFragmentOffset () const
 
IPv4Option getOption (IPv4OptionTypes option) const
 
IPv4Option getFirstOption () const
 
IPv4Option getNextOption (IPv4Option &option) const
 
size_t getOptionCount () const
 
IPv4Option addOption (const IPv4OptionBuilder &optionBuilder)
 
IPv4Option addOptionAfter (const IPv4OptionBuilder &optionBuilder, IPv4OptionTypes prevOptionType=IPV4OPT_Unknown)
 
bool removeOption (IPv4OptionTypes option)
 
bool removeAllOptions ()
 
void parseNextLayer () override
 
size_t getHeaderLen () const override
 
void computeCalculateFields () override
 
std::string toString () const override
 
OsiModelLayer getOsiModelLayer () const override
 
- Public Member Functions inherited from pcpp::Layer
 ~Layer () override
 
LayergetNextLayer () const
 
LayergetPrevLayer () const
 
ProtocolType getProtocol () const
 
bool isMemberOfProtocolFamily (ProtocolTypeFamily protocolTypeFamily) 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 override
 
- Public Member Functions inherited from pcpp::IPLayer
virtual ~IPLayer ()=default
 

Static Public Member Functions

static bool isDataValid (const uint8_t *data, size_t dataLen)
 

Detailed Description

Represents an IPv4 protocol layer

Constructor & Destructor Documentation

◆ IPv4Layer() [1/5]

pcpp::IPv4Layer::IPv4Layer ( 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 (will be casted to iphdr)
[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

◆ IPv4Layer() [2/5]

pcpp::IPv4Layer::IPv4Layer ( uint8_t *  data,
size_t  dataLen,
Layer prevLayer,
Packet packet,
bool  setTotalLenAsDataLen 
)

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

Parameters
[in]dataA pointer to the raw data (will be casted to iphdr)
[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
[in]setTotalLenAsDataLenWhen setting this value to "true" or when using the other c'tor, the layer data length is calculated from iphdr::totalLength field. When setting to "false" the data length is set as the value of dataLen parameter. Please notice that if iphdr::totalLength is equal to zero (which can happen in TCP Segmentation Offloading), this flag is ignored and the layer data length is calculated by the actual data captured on the wire

◆ IPv4Layer() [3/5]

pcpp::IPv4Layer::IPv4Layer ( )

A constructor that allocates a new IPv4 header with empty fields

◆ IPv4Layer() [4/5]

pcpp::IPv4Layer::IPv4Layer ( const IPv4Address srcIP,
const IPv4Address dstIP 
)

A constructor that allocates a new IPv4 header with source and destination IPv4 addresses

Parameters
[in]srcIPSource IPv4 address
[in]dstIPDestination IPv4 address

◆ IPv4Layer() [5/5]

pcpp::IPv4Layer::IPv4Layer ( const IPv4Layer other)

A copy constructor that copy the entire header from the other IPv4Layer (including IPv4 options)

Member Function Documentation

◆ addOption()

IPv4Option pcpp::IPv4Layer::addOption ( const IPv4OptionBuilder optionBuilder)

Add a new IPv4 option at the end of the layer (after the last IPv4 option)

Parameters
[in]optionBuilderAn IPv4OptionBuilder object that contains the IPv4 option data to be added
Returns
A IPv4Option object that contains the newly added IPv4 option data or logical null (IPv4Option::isNull() == true) if addition failed. In case of a failure a corresponding error message will be printed to log

◆ addOptionAfter()

IPv4Option pcpp::IPv4Layer::addOptionAfter ( const IPv4OptionBuilder optionBuilder,
IPv4OptionTypes  prevOptionType = IPV4OPT_Unknown 
)

Add a new IPv4 option after an existing one

Parameters
[in]optionBuilderAn IPv4OptionBuilder object that contains the requested IPv4 option data to be added
[in]prevOptionTypeThe IPv4 option which the newly added option should come after. This is an optional parameter which gets a default value of IPV4OPT_Unknown if omitted, which means the new option will be added as the first option in the layer
Returns
A IPv4Option object containing the newly added IPv4 option data or logical null (IPv4Option::isNull() == true) if addition failed. In case of a failure a corresponding error message will be printed to log

◆ computeCalculateFields()

void pcpp::IPv4Layer::computeCalculateFields ( )
overridevirtual

Calculate the following fields:

Implements pcpp::Layer.

◆ getDstIPAddress()

IPAddress pcpp::IPv4Layer::getDstIPAddress ( ) const
inlineoverridevirtual

Get the destination IP address in the form of IPAddress. This method is very similar to getDstIPv4Address(), but adds a level of abstraction because IPAddress can be used for both IPv4 and IPv6 addresses

Returns
An IPAddress containing the destination address

Implements pcpp::IPLayer.

◆ getDstIPv4Address()

IPv4Address pcpp::IPv4Layer::getDstIPv4Address ( ) const
inline

Get the destination IP address in the form of IPv4Address

Returns
An IPv4Address containing the destination address

◆ getFirstOption()

IPv4Option pcpp::IPv4Layer::getFirstOption ( ) const
Returns
The first IPv4 option in the packet. If the current layer contains no options the returned value will contain a logical null (IPv4Option::isNull() == true)

◆ getFragmentFlags()

uint8_t pcpp::IPv4Layer::getFragmentFlags ( ) const
Returns
A bitmask containing the fragmentation flags (e.g IP_DONT_FRAGMENT or IP_MORE_FRAGMENTS)

◆ getFragmentOffset()

uint16_t pcpp::IPv4Layer::getFragmentOffset ( ) const
Returns
The fragment offset in case this packet is a fragment, 0 otherwise

◆ getHeaderLen()

size_t pcpp::IPv4Layer::getHeaderLen ( ) const
inlineoverridevirtual
Returns
Size of IPv4 header (including IPv4 options if exist)

Implements pcpp::Layer.

◆ getIPv4Header()

iphdr* pcpp::IPv4Layer::getIPv4Header ( ) const
inline

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

Returns
A pointer to the iphdr

◆ getNextOption()

IPv4Option pcpp::IPv4Layer::getNextOption ( IPv4Option option) const

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

Parameters
[in]optionAn IPv4 option object that exists in the current layer
Returns
A IPv4Option object that contains the IPv4 option data that comes next, or logical null if the given IPv4 option: (1) was the last one; or (2) contains a logical null; or (3) doesn't belong to this packet

◆ getOption()

IPv4Option pcpp::IPv4Layer::getOption ( IPv4OptionTypes  option) const

Get an IPv4 option by type.

Parameters
[in]optionIPv4 option type
Returns
An IPv4Option object that contains the first option that matches this type, or logical null (IPv4Option::isNull() == true) if no such option found

◆ getOptionCount()

size_t pcpp::IPv4Layer::getOptionCount ( ) const
Returns
The number of IPv4 options in this layer

◆ getOsiModelLayer()

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

Implements pcpp::Layer.

◆ getSrcIPAddress()

IPAddress pcpp::IPv4Layer::getSrcIPAddress ( ) const
inlineoverridevirtual

Get the source IP address in the form of IPAddress. This method is very similar to getSrcIPv4Address(), but adds a level of abstraction because IPAddress can be used for both IPv4 and IPv6 addresses

Returns
An IPAddress containing the source address

Implements pcpp::IPLayer.

◆ getSrcIPv4Address()

IPv4Address pcpp::IPv4Layer::getSrcIPv4Address ( ) const
inline

Get the source IP address in the form of IPv4Address

Returns
An IPv4Address containing the source address

◆ isDataValid()

bool pcpp::IPv4Layer::isDataValid ( const uint8_t *  data,
size_t  dataLen 
)
inlinestatic

A static method that validates the input data

Parameters
[in]dataThe pointer to the beginning of a byte stream of IP packet
[in]dataLenThe length of the byte stream
Returns
True if the data is valid and can represent an IPv4 packet

◆ isFirstFragment()

bool pcpp::IPv4Layer::isFirstFragment ( ) const
Returns
True if this packet is a fragment (in sense of IP fragmentation) and is the first fragment (which usually contains the L4 header). Return false otherwise (not a fragment or not the first fragment)

◆ isFragment()

bool pcpp::IPv4Layer::isFragment ( ) const
Returns
True if this packet is a fragment (in sense of IP fragmentation), false otherwise

◆ isLastFragment()

bool pcpp::IPv4Layer::isLastFragment ( ) const
Returns
True if this packet is a fragment (in sense of IP fragmentation) and is the last fragment. Return false otherwise (not a fragment or not the last fragment)

◆ operator=()

IPv4Layer& pcpp::IPv4Layer::operator= ( const IPv4Layer other)

An assignment operator that first delete all data from current layer and then copy the entire header from the other IPv4Layer (including IPv4 options)

◆ parseNextLayer()

void pcpp::IPv4Layer::parseNextLayer ( )
overridevirtual

Currently identifies the following next layers:

Otherwise sets PayloadLayer

Implements pcpp::Layer.

◆ removeAllOptions()

bool pcpp::IPv4Layer::removeAllOptions ( )

Remove all IPv4 options from the layer

Returns
True if options removed successfully or false if some error occurred (an appropriate error message will be printed to log)

◆ removeOption()

bool pcpp::IPv4Layer::removeOption ( IPv4OptionTypes  option)

Remove an IPv4 option

Parameters
[in]optionThe option type to remove
Returns
True if option was removed successfully or false if option type wasn't found or failed to shorten the layer. If an option appears twice in the layer, its first instance will be removed

◆ setDstIPv4Address()

void pcpp::IPv4Layer::setDstIPv4Address ( const IPv4Address ipAddr)
inline

Set the dest IP address

Parameters
[in]ipAddrThe IP address to set

◆ setSrcIPv4Address()

void pcpp::IPv4Layer::setSrcIPv4Address ( const IPv4Address ipAddr)
inline

Set the source IP address

Parameters
[in]ipAddrThe IP address to set

◆ toString()

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

Implements pcpp::Layer.