pcpp::Layer Class Referenceabstract

#include <Layer.h>

Inheritance diagram for pcpp::Layer:
pcpp::IDataContainer pcpp::ArpLayer pcpp::DhcpLayer pcpp::DnsLayer pcpp::EthLayer pcpp::GreLayer pcpp::IcmpLayer pcpp::IgmpLayer pcpp::IPv4Layer pcpp::IPv6Layer pcpp::MplsLayer pcpp::NullLoopbackLayer pcpp::PayloadLayer pcpp::PPP_PPTPLayer pcpp::PPPoELayer pcpp::SllLayer pcpp::SSLLayer pcpp::TcpLayer pcpp::TextBasedProtocolMessage pcpp::UdpLayer pcpp::VlanLayer pcpp::VxlanLayer

Public Member Functions

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)
virtual void parseNextLayer ()=0
virtual size_t getHeaderLen ()=0
virtual void computeCalculateFields ()=0
virtual std::string toString ()=0
virtual OsiModelLayer getOsiModelLayer ()=0

Detailed Description

Layer is the base class for all protocol layers. Each protocol supported in PcapPlusPlus has a class that inherits Layer. The protocol layer class expose all properties and methods relevant for viewing and editing protocol fields. For example: a pointer to a structured header (e.g tcphdr, iphdr, etc.), protocol header size, payload size, compute fields that can be automatically computed, print protocol data to string, etc. Each protocol instance is obviously part of a protocol stack (which construct a packet). This protocol stack is represented in PcapPlusPlus in a linked list, and each layer is an element in this list. That's why each layer has proprties to the next and previous layer in the protocol stack The Layer class, as a base class, is abstract and the user can't create an instance of it (it has a private constructor) Each layer holds a pointer to the relevant place in the packet. The layer sees all the data from this pointer forward until the end of the packet. Here is an example packet showing this concept:

|Eth       |IPv4       |TCP       |Packet          |
|Header    |Header     |Header    |Payload         |

EthLayer data
         IPv4Layer data
                     TcpLayer data
                                PayloadLayer data

Constructor & Destructor Documentation

virtual pcpp::Layer::~Layer ( )

A destructor for this class. Frees the data if it was allocated by the layer constructor (see isAllocatedToPacket() for more info)

Member Function Documentation

void pcpp::Layer::copyData ( uint8_t *  toArr)

Copy the raw data of this layer to another array

[out]toArrThe destination byte array
uint8_t* pcpp::Layer::getData ( )
A pointer to the layer raw data. In most cases it'll be a pointer to the first byte of the header
size_t pcpp::Layer::getDataLen ( )
The length in bytes of the data from the first byte of the header until the end of the packet
uint8_t* pcpp::Layer::getDataPtr ( size_t  offset = 0)

Get a pointer to the data

[in]offsetGet a pointer in a certain offset. Default is 0 - get a pointer to start of data
A pointer to the data

Implements pcpp::IDataContainer.

uint8_t* pcpp::Layer::getLayerPayload ( )
A pointer for the layer payload, meaning the first byte after the header
size_t pcpp::Layer::getLayerPayloadSize ( )
The size in bytes of the payload
Layer* pcpp::Layer::getNextLayer ( )
A pointer to the next layer in the protocol stack or NULL if the layer is the last one
Layer* pcpp::Layer::getPrevLayer ( )
A pointer to the previous layer in the protocol stack or NULL if the layer is the first one
ProtocolType pcpp::Layer::getProtocol ( )
The protocol enum
bool pcpp::Layer::isAllocatedToPacket ( )

Raw data in layers can come from one of sources:

  1. from an existing packet - this is the case when parsing packets received from files or the network. In this case the data was already allocated by someone else, and layer only holds the pointer to the relevant place inside this data
  2. when creating packets, data is allocated when layer is created. In this case the layer is responsible for freeing it as well
Returns true if the data was allocated by an external source (a packet) or false if it was allocated by the layer itself