PcapPlusPlus
pcpp::IPv4Layer Class Reference

#include <IPv4Layer.h>

Inheritance diagram for pcpp::IPv4Layer:
pcpp::Layer

Public Member Functions

 IPv4Layer (uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
 
 IPv4Layer ()
 
 IPv4Layer (const IPv4Address &srcIP, const IPv4Address &dstIP)
 
 IPv4Layer (const IPv4Layer &other)
 
IPv4Layeroperator= (const IPv4Layer &other)
 
iphdrgetIPv4Header ()
 
IPv4Address getSrcIpAddress ()
 
void setSrcIpAddress (const IPv4Address &ipAddr)
 
IPv4Address getDstIpAddress ()
 
void setDstIpAddress (const IPv4Address &ipAddr)
 
bool isFragment ()
 
bool isFirstFragment ()
 
bool isLastFragment ()
 
uint8_t getFragmentFlags ()
 
uint16_t getFragmentOffset ()
 
IPv4OptionDatagetOptionData (IPv4OptionTypes option)
 
IPv4OptionDatagetFirstOptionData ()
 
IPv4OptionDatagetNextOptionData (IPv4OptionData *option)
 
size_t getOptionsCount ()
 
IPv4OptionDataaddOption (IPv4OptionTypes optionType, uint8_t optionDataLength, const uint8_t *optionData)
 
IPv4OptionDataaddOption (IPv4OptionTypes optionType, const std::vector< IPv4Address > &ipList)
 
IPv4OptionDataaddTimestampOption (const IPv4TimestampOptionValue &timestampValue)
 
IPv4OptionDataaddOptionAfter (IPv4OptionTypes optionType, uint8_t optionDataLength, const uint8_t *optionData, IPv4OptionTypes prevOption=IPV4OPT_Unknown)
 
IPv4OptionDataaddOptionAfter (IPv4OptionTypes optionType, const std::vector< IPv4Address > &ipList, IPv4OptionTypes prevOption=IPV4OPT_Unknown)
 
IPv4OptionDataaddTimestampOptionAfter (const IPv4TimestampOptionValue &timestampValue, IPv4OptionTypes prevOption=IPV4OPT_Unknown)
 
bool removeOption (IPv4OptionTypes option)
 
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)
 

Detailed Description

Represents an IPv4 protocol layer

Constructor & Destructor Documentation

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

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
pcpp::IPv4Layer::IPv4Layer ( )

A constructor that allocates a new IPv4 header with empty fields

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
pcpp::IPv4Layer::IPv4Layer ( const IPv4Layer other)

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

Member Function Documentation

IPv4OptionData* pcpp::IPv4Layer::addOption ( IPv4OptionTypes  optionType,
uint8_t  optionDataLength,
const uint8_t *  optionData 
)

Add an IPv4 option

Parameters
[in]optionTypeThe IPv4 option type to add
[in]optionDataLengthThe length of the option data [in bytes]. For IPV4OPT_NOP and IPV4OPT_EndOfOtionsList this parameter must be 0 otherwise the method will fail
[in]optionDataA byte array containing the IPv4 option data (should be in size indicated in optionDataLength). For IPV4OPT_NOP and IPV4OPT_EndOfOtionsList this parameter is ignored (expected to be NULL)
Returns
If option was added successfully a pointer to the new option will be returned. If adding failed NULL will be returned. Failure can happen if layer couldn't be extended for some reason or if added option exceeds maximum options length (in IPv4 total options length must not exceed 40 bytes). In any case of failure an appropriate error message will be printed to log
IPv4OptionData* pcpp::IPv4Layer::addOption ( IPv4OptionTypes  optionType,
const std::vector< IPv4Address > &  ipList 
)

Add an IPv4 option with value of IP address list. This method is relevant only for option types which value is IP address list such as IPV4OPT_StrictSourceRoute, IPV4OPT_RecordRoute and IPV4OPT_LooseSourceRoute. Please note this method calculates and sets the pointer field in the option value automatically

Parameters
[in]optionTypeThe IPv4 option type to add (should be option type which value is IP address list)
[in]ipListThe list of IP addresses to set. Please note this method doesn't automatically adds zero IP addresses (with value of 0.0.0.0), so if you want the option value to include zero values please add them to the list
Returns
If option was added successfully a pointer to the new option will be returned. If adding failed NULL will be returned. Failure can happen if layer couldn't be extended for some reason or if added option exceeds maximum options length (in IPv4 total options length must not exceed 40 bytes). In any case of failure an appropriate error message will be printed to log
IPv4OptionData* pcpp::IPv4Layer::addOptionAfter ( IPv4OptionTypes  optionType,
uint8_t  optionDataLength,
const uint8_t *  optionData,
IPv4OptionTypes  prevOption = IPV4OPT_Unknown 
)

Add an IPv4 option after an existing option

Parameters
[in]optionTypeThe IPv4 option type to add
[in]optionDataLengthThe length of the option data [in bytes]. For IPV4OPT_NOP and IPV4OPT_EndOfOtionsList this parameter must be 0 otherwise the method will fail
[in]optionDataA byte array containing the IPv4 option data (should be in size indicated in optionDataLength). For IPV4OPT_NOP and IPV4OPT_EndOfOtionsList this parameter is ignored (expected to be NULL)
[in]prevOptionThe option type which the new option should be added after. It's an optional parameter, if it's not set or set to an option that doesn't exist the new option will be added as the first option in the layer
Returns
If option was added successfully a pointer to the new option will be returned. If adding failed NULL will be returned. Failure can happen if layer couldn't be extended for some reason or if added option exceeds maximum options length (in IPv4 total options length must not exceed 40 bytes). In any case of failure an appropriate error message will be printed to log
IPv4OptionData* pcpp::IPv4Layer::addOptionAfter ( IPv4OptionTypes  optionType,
const std::vector< IPv4Address > &  ipList,
IPv4OptionTypes  prevOption = IPV4OPT_Unknown 
)

Add an IPv4 option with value of IP address list after an existing option. This method is relevant only for option types which value is IP address list such as IPV4OPT_StrictSourceRoute, IPV4OPT_RecordRoute and IPV4OPT_LooseSourceRoute. Please note this method calculates and sets the pointer field in the option value automatically

Parameters
[in]optionTypeThe IPv4 option type to add (should be option type which value is IP address list)
[in]ipListThe list of IP addresses to set. Please note this method doesn't automatically adds zero IP addresses (with value of 0.0.0.0), so if you want the option value to include zero values please add them to the list
[in]prevOptionThe option type which the new option should be added after. It's an optional parameter, if it's not set or set to an option that doesn't exist the new option will be added as the first option in the layer
Returns
If option was added successfully a pointer to the new option will be returned. If adding failed NULL will be returned. Failure can happen if layer couldn't be extended for some reason or if added option exceeds maximum options length (in IPv4 total options length must not exceed 40 bytes). In any case of failure an appropriate error message will be printed to log
IPv4OptionData* pcpp::IPv4Layer::addTimestampOption ( const IPv4TimestampOptionValue timestampValue)

Add an IPv4 timestamp option. Please note that timestamp type of IPv4TimestampOptionValue::TimestampsForPrespecifiedIPs is currently not supported. Please also note this method calculates and sets the pointer field in the timestamp option value automatically

Parameters
[in]timestampValueTimestamp option value to set, including timestamp type (timestamp only or timestamp and IP) and a list of timestamps and/or IP addresses
Returns
If option was added successfully a pointer to the new option will be returned. If adding failed NULL will be returned. Failure can happen if layer couldn't be extended for some reason, if added option exceeds maximum options length (in IPv4 total options length must not exceed 40 bytes), or if the timestamp value is wrong (for example: type is IPv4TimestampOptionValue::TimestampsForPrespecifiedIPs or IPv4TimestampOptionValue::Unknown, type is IPv4TimestampOptionValue::TimestampAndIP and number of timestamps is not equal to the number of IP addresses, etc.). In any case of failure an appropriate error message will be printed to log
IPv4OptionData* pcpp::IPv4Layer::addTimestampOptionAfter ( const IPv4TimestampOptionValue timestampValue,
IPv4OptionTypes  prevOption = IPV4OPT_Unknown 
)

Add an IPv4 timestamp option after some existing option. Please note that timestamp type of IPv4TimestampOptionValue::TimestampsForPrespecifiedIPs is currently not supported. Please also note this method calculates and sets the pointer field in the timestamp option value automatically

Parameters
[in]timestampValueTimestamp option value to set, including timestamp type (timestamp only or timestamp and IP) and a list of timestamps and/or IP addresses
[in]prevOptionThe option type which the new option should be added after. It's an optional parameter, if it's not set or set to an option that doesn't exist the new option will be added as the first option in the layer
Returns
If option was added successfully a pointer to the new option will be returned. If adding failed NULL will be returned. Failure can happen if layer couldn't be extended for some reason, if added option exceeds maximum options length (in IPv4 total options length must not exceed 40 bytes), or if the timestamp value is wrong (for example: type is IPv4TimestampOptionValue::TimestampsForPrespecifiedIPs or IPv4TimestampOptionValue::Unknown, type is IPv4TimestampOptionValue::TimestampAndIP and number of timestamps is not equal to the number of IP addresses, etc.). In any case of failure an appropriate error message will be printed to log
void pcpp::IPv4Layer::computeCalculateFields ( )
virtual

Calculate the following fields:

Implements pcpp::Layer.

IPv4Address pcpp::IPv4Layer::getDstIpAddress ( )
inline

Get the destination IP address in the form of IPv4Address

Returns
An IPv4Address containing the destination address
IPv4OptionData* pcpp::IPv4Layer::getFirstOptionData ( )
Returns
The first IPv4 option, or NULL if no IPv4 options exist. Notice the return value is a pointer to the real data casted to IPv4OptionData type (as opposed to a copy of the option data). So changes in the return value will affect the packet data
uint8_t pcpp::IPv4Layer::getFragmentFlags ( )
Returns
A bitmask containing the fragmentation flags (e.g IP_DONT_FRAGMENT or IP_MORE_FRAGMENTS)
uint16_t pcpp::IPv4Layer::getFragmentOffset ( )
Returns
The fragment offset in case this packet is a fragment, 0 otherwise
size_t pcpp::IPv4Layer::getHeaderLen ( )
inlinevirtual
Returns
Size of IPv4 header (including IPv4 options if exist)

Implements pcpp::Layer.

iphdr* pcpp::IPv4Layer::getIPv4Header ( )
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
IPv4OptionData* pcpp::IPv4Layer::getNextOptionData ( IPv4OptionData option)

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

Parameters
[in]optionThe IPv4 option to start searching from
Returns
The next IPv4 option or NULL if "option" is NULL or "option" is the last IPv4 option
IPv4OptionData* pcpp::IPv4Layer::getOptionData ( IPv4OptionTypes  option)

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

Parameters
[in]optionThe IPv4 option to get
Returns
A pointer to the IPv4 option location in the packet
size_t pcpp::IPv4Layer::getOptionsCount ( )
Returns
The number of IPv4 options in this layer
OsiModelLayer pcpp::IPv4Layer::getOsiModelLayer ( )
inlinevirtual
Returns
The OSI Model layer this protocol belongs to

Implements pcpp::Layer.

IPv4Address pcpp::IPv4Layer::getSrcIpAddress ( )
inline

Get the source IP address in the form of IPv4Address

Returns
An IPv4Address containing the source address
bool pcpp::IPv4Layer::isFirstFragment ( )
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)
bool pcpp::IPv4Layer::isFragment ( )
Returns
True if this packet is a fragment (in sense of IP fragmentation), false otherwise
bool pcpp::IPv4Layer::isLastFragment ( )
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)
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)

void pcpp::IPv4Layer::parseNextLayer ( )
virtual

Currently identifies the following next layers: UdpLayer, TcpLayer. Otherwise sets PayloadLayer

Implements pcpp::Layer.

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)
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
void pcpp::IPv4Layer::setDstIpAddress ( const IPv4Address ipAddr)
inline

Set the dest IP address

Parameters
[in]ipAddrThe IP address to set
void pcpp::IPv4Layer::setSrcIpAddress ( const IPv4Address ipAddr)
inline

Set the source IP address

Parameters
[in]ipAddrThe IP address to set
std::string pcpp::IPv4Layer::toString ( )
virtual
Returns
A string representation of the layer most important data (should look like the layer description in Wireshark)

Implements pcpp::Layer.