PcapPlusPlus  21.05
pcpp::TcpLayer Class Reference

#include <TcpLayer.h>

Inheritance diagram for pcpp::TcpLayer:
pcpp::Layer pcpp::IDataContainer

Public Member Functions

 TcpLayer (uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
 
 TcpLayer ()
 
 TcpLayer (uint16_t portSrc, uint16_t portDst)
 
 TcpLayer (const TcpLayer &other)
 
TcpLayeroperator= (const TcpLayer &other)
 
tcphdrgetTcpHeader () const
 
uint16_t getSrcPort () const
 
uint16_t getDstPort () const
 
TcpOption getTcpOption (TcpOptionType option) const
 
TcpOption getFirstTcpOption () const
 
TcpOption getNextTcpOption (TcpOption &tcpOption) const
 
size_t getTcpOptionCount () const
 
TcpOption addTcpOption (const TcpOptionBuilder &optionBuilder)
 
TcpOption addTcpOptionAfter (const TcpOptionBuilder &optionBuilder, TcpOptionType prevOptionType=TCPOPT_Unknown)
 
bool removeTcpOption (TcpOptionType optionType)
 
bool removeAllTcpOptions ()
 
uint16_t calculateChecksum (bool writeResultToPacket)
 
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
 

Static Public Member Functions

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

Detailed Description

Represents a TCP (Transmission Control Protocol) protocol layer

Constructor & Destructor Documentation

◆ TcpLayer() [1/4]

pcpp::TcpLayer::TcpLayer ( 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 tcphdr)
[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

◆ TcpLayer() [2/4]

pcpp::TcpLayer::TcpLayer ( )

A constructor that allocates a new TCP header with zero TCP options

◆ TcpLayer() [3/4]

pcpp::TcpLayer::TcpLayer ( uint16_t  portSrc,
uint16_t  portDst 
)

A constructor that allocates a new TCP header with source port and destination port and zero TCP options

Parameters
[in]portSrcSource port
[in]portDstDestination port

◆ TcpLayer() [4/4]

pcpp::TcpLayer::TcpLayer ( const TcpLayer other)

A copy constructor that copy the entire header from the other TcpLayer (including TCP options)

Member Function Documentation

◆ addTcpOption()

TcpOption pcpp::TcpLayer::addTcpOption ( const TcpOptionBuilder optionBuilder)

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

Parameters
[in]optionBuilderA TcpOptionBuilder object that contains the TCP option data to be added
Returns
A TcpOption object that contains the newly added TCP option data or logical NULL (TcpOption::isNull() == true) if addition failed. In case of a failure a corresponding error message will be printed to log

◆ addTcpOptionAfter()

TcpOption pcpp::TcpLayer::addTcpOptionAfter ( const TcpOptionBuilder optionBuilder,
TcpOptionType  prevOptionType = TCPOPT_Unknown 
)

Add a new TCP option after an existing one

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

◆ calculateChecksum()

uint16_t pcpp::TcpLayer::calculateChecksum ( bool  writeResultToPacket)

Calculate the checksum from header and data and possibly write the result to tcphdr::headerChecksum

Parameters
[in]writeResultToPacketIf set to true then checksum result will be written to tcphdr::headerChecksum
Returns
The checksum result

◆ computeCalculateFields()

void pcpp::TcpLayer::computeCalculateFields ( )
virtual

Calculate tcphdr::headerChecksum field

Implements pcpp::Layer.

◆ getDstPort()

uint16_t pcpp::TcpLayer::getDstPort ( ) const
Returns
TCP destination port

◆ getFirstTcpOption()

TcpOption pcpp::TcpLayer::getFirstTcpOption ( ) const
Returns
The first TCP option in the packet. If the current layer contains no options the returned value will contain a logical NULL (TcpOption::isNull() == true)

◆ getHeaderLen()

size_t pcpp::TcpLayer::getHeaderLen ( ) const
inlinevirtual
Returns
Size of tcphdr + all TCP options

Implements pcpp::Layer.

◆ getNextTcpOption()

TcpOption pcpp::TcpLayer::getNextTcpOption ( TcpOption tcpOption) const

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

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

◆ getOsiModelLayer()

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

Implements pcpp::Layer.

◆ getSrcPort()

uint16_t pcpp::TcpLayer::getSrcPort ( ) const
Returns
TCP source port

◆ getTcpHeader()

tcphdr* pcpp::TcpLayer::getTcpHeader ( ) const
inline

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

Returns
A pointer to the tcphdr

◆ getTcpOption()

TcpOption pcpp::TcpLayer::getTcpOption ( TcpOptionType  option) const

Get a TCP option by type

Parameters
[in]optionTCP option type to retrieve
Returns
An TcpOption object that contains the first option that matches this type, or logical NULL (TcpOption::isNull() == true) if no such option found

◆ getTcpOptionCount()

size_t pcpp::TcpLayer::getTcpOptionCount ( ) const
Returns
The number of TCP options in this layer

◆ isDataValid()

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

The static method makes validation of input data

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

◆ operator=()

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

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

◆ parseNextLayer()

void pcpp::TcpLayer::parseNextLayer ( )
virtual

Currently identifies the following next layers: HttpRequestLayer, HttpResponseLayer. Otherwise sets PayloadLayer

Implements pcpp::Layer.

◆ removeAllTcpOptions()

bool pcpp::TcpLayer::removeAllTcpOptions ( )

Remove all TCP options in this layer

Returns
True if all TCP options were successfully removed or false if removal failed for some reason (a proper error will be written to log)

◆ removeTcpOption()

bool pcpp::TcpLayer::removeTcpOption ( TcpOptionType  optionType)

Remove an existing TCP option from the layer. TCP option is found by type

Parameters
[in]optionTypeThe TCP option type to remove
Returns
True if TCP option was removed or false if type wasn't found or if removal failed (in each case a proper error will be written to log)

◆ toString()

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

Implements pcpp::Layer.