PcapPlusPlus  24.09
pcpp::Layer Class Referenceabstract

#include <Layer.h>

Inheritance diagram for pcpp::Layer:
pcpp::IDataContainer pcpp::ArpLayer pcpp::AuthenticationHeaderLayer pcpp::BgpLayer pcpp::CotpLayer pcpp::DhcpLayer pcpp::DhcpV6Layer pcpp::DnsLayer pcpp::ESPLayer pcpp::EthDot3Layer pcpp::EthLayer pcpp::GreLayer pcpp::GtpV1Layer pcpp::IPv4Layer pcpp::IPv6Layer pcpp::IcmpLayer pcpp::IcmpV6Layer pcpp::IgmpLayer pcpp::LLCLayer pcpp::LdapLayer pcpp::MplsLayer pcpp::NflogLayer pcpp::NtpLayer pcpp::NullLoopbackLayer pcpp::PPP_PPTPLayer pcpp::PPPoELayer pcpp::PacketTrailerLayer pcpp::PayloadLayer pcpp::RadiusLayer pcpp::S7CommLayer pcpp::SSHLayer pcpp::SSLLayer pcpp::SingleCommandTextProtocol pcpp::Sll2Layer pcpp::SllLayer pcpp::SomeIpLayer pcpp::StpLayer pcpp::TcpLayer pcpp::TelnetLayer pcpp::TextBasedProtocolMessage pcpp::TpktLayer pcpp::UdpLayer pcpp::VlanLayer pcpp::VrrpLayer pcpp::VxlanLayer pcpp::WakeOnLanLayer

Public Member Functions

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

◆ ~Layer()

virtual pcpp::Layer::~Layer ( )
virtual

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

Member Function Documentation

◆ computeCalculateFields()

◆ copyData()

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

Copy the raw data of this layer to another array

Parameters
[out]toArrThe destination byte array

◆ getData()

uint8_t* pcpp::Layer::getData ( ) const
inline
Returns
A pointer to the layer raw data. In most cases it'll be a pointer to the first byte of the header

◆ getDataLen()

size_t pcpp::Layer::getDataLen ( ) const
inline
Returns
The length in bytes of the data from the first byte of the header until the end of the packet

◆ getDataPtr()

uint8_t* pcpp::Layer::getDataPtr ( size_t  offset = 0) const
inlinevirtual

Get a pointer to the data

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

Implements pcpp::IDataContainer.

◆ getHeaderLen()

◆ getLayerPayload()

uint8_t* pcpp::Layer::getLayerPayload ( ) const
inline
Returns
A pointer for the layer payload, meaning the first byte after the header

◆ getLayerPayloadSize()

size_t pcpp::Layer::getLayerPayloadSize ( ) const
inline
Returns
The size in bytes of the payload

◆ getNextLayer()

Layer* pcpp::Layer::getNextLayer ( ) const
inline
Returns
A pointer to the next layer in the protocol stack or nullptr if the layer is the last one

◆ getOsiModelLayer()

◆ getPrevLayer()

Layer* pcpp::Layer::getPrevLayer ( ) const
inline
Returns
A pointer to the previous layer in the protocol stack or nullptr if the layer is the first one

◆ getProtocol()

ProtocolType pcpp::Layer::getProtocol ( ) const
inline
Returns
The protocol enum

◆ isAllocatedToPacket()

bool pcpp::Layer::isAllocatedToPacket ( ) const
inline

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
Returns true if the data was allocated by an external source (a packet) or false if it was allocated by the layer itself

◆ isMemberOfProtocolFamily()

bool pcpp::Layer::isMemberOfProtocolFamily ( ProtocolTypeFamily  protocolTypeFamily) const

Check if the layer's protocol matches a protocol family

Parameters
protocolTypeFamilyThe protocol family to check
Returns
True if the layer's protocol matches the protocol family, false otherwise

◆ parseNextLayer()

◆ toString()

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

Implemented in pcpp::VrrpLayer, pcpp::TpktLayer, pcpp::S7CommLayer, pcpp::LdapLayer, pcpp::CotpLayer, pcpp::WakeOnLanLayer, pcpp::VxlanLayer, pcpp::VlanLayer, pcpp::UdpLayer, pcpp::TelnetLayer, pcpp::TcpLayer, pcpp::MultipleStpLayer, pcpp::RapidStpLayer, pcpp::StpConfigurationBPDULayer, pcpp::StpTopologyChangeBPDULayer, pcpp::SSLApplicationDataLayer, pcpp::SSLAlertLayer, pcpp::SSLChangeCipherSpecLayer, pcpp::SSLHandshakeLayer, pcpp::SSHEncryptedMessage, pcpp::SSHHandshakeMessage, pcpp::SSHIdentificationMessage, pcpp::SomeIpSdLayer, pcpp::SomeIpTpLayer, pcpp::SomeIpLayer, pcpp::SmtpResponseLayer, pcpp::SmtpRequestLayer, pcpp::SllLayer, pcpp::Sll2Layer, pcpp::SipResponseLayer, pcpp::SipRequestLayer, pcpp::SdpLayer, pcpp::RadiusLayer, pcpp::PPPoEDiscoveryLayer, pcpp::PPPoESessionLayer, pcpp::PayloadLayer, pcpp::PacketTrailerLayer, pcpp::NullLoopbackLayer, pcpp::NtpLayer, pcpp::NflogLayer, pcpp::NDPNeighborAdvertisementLayer, pcpp::NDPNeighborSolicitationLayer, pcpp::MplsLayer, pcpp::LLCLayer, pcpp::IPv6Layer, pcpp::IPv4Layer, pcpp::ESPLayer, pcpp::AuthenticationHeaderLayer, pcpp::IgmpLayer, pcpp::ICMPv6EchoLayer, pcpp::IcmpV6Layer, pcpp::IcmpLayer, pcpp::HttpResponseLayer, pcpp::HttpRequestLayer, pcpp::GtpV1Layer, pcpp::PPP_PPTPLayer, pcpp::GREv1Layer, pcpp::GREv0Layer, pcpp::FtpDataLayer, pcpp::FtpResponseLayer, pcpp::FtpRequestLayer, pcpp::EthLayer, pcpp::EthDot3Layer, pcpp::DnsLayer, pcpp::DhcpV6Layer, pcpp::DhcpLayer, pcpp::BgpLayer, and pcpp::ArpLayer.