PcapPlusPlus
pcpp::TcpLayer Class Reference

#include <TcpLayer.h>

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

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 ()
 
TcpOptionDatagetTcpOptionData (TcpOption option)
 
TcpOptionDatagetFirstTcpOptionData ()
 
TcpOptionDatagetNextTcpOptionData (TcpOptionData *tcpOption)
 
size_t getTcpOptionsCount ()
 
TcpOptionDataaddTcpOption (TcpOption optionType, uint8_t optionLength, const uint8_t *optionData)
 
TcpOptionDataaddTcpOptionAfter (TcpOption optionType, uint8_t optionLength, const uint8_t *optionData, TcpOptionData *prevOption)
 
bool removeTcpOption (TcpOption optionType)
 
bool removeAllTcpOptions ()
 
uint16_t calculateChecksum (bool writeResultToPacket)
 
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 a TCP (Transmission Control Protocol) protocol layer

Constructor & Destructor Documentation

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

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

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

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

Member Function Documentation

TcpOptionData* pcpp::TcpLayer::addTcpOption ( TcpOption  optionType,
uint8_t  optionLength,
const uint8_t *  optionData 
)

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

Parameters
[in]optionTypeThe type of the newly added option
[in]optionLengthThe length of the option data
[in]optionDataA pointer to the option data. This data will be copied to added option data. Notice the length of optionData must be optionLength
Returns
A pointer to the new added TCP option data or NULL if addition failed. Notice this is a pointer to the real data casted to TcpOptionData type (as opposed to a copy of the option data). So changes in this return value will affect the packet data
TcpOptionData* pcpp::TcpLayer::addTcpOptionAfter ( TcpOption  optionType,
uint8_t  optionLength,
const uint8_t *  optionData,
TcpOptionData prevOption 
)

Add a new TCP option after an existing TCP option

Parameters
[in]optionTypeThe type of the newly added option
[in]optionLengthThe length of the option data
[in]optionDataA pointer to the option data. This data will be copied to added option data. Notice the length of optionData must be optionLength
[in]prevOptionThe TCP option which the newly added tag will come after. If set to NULL TCP option will be added as the first TCP option
Returns
A pointer to the new added TCP option or NULL if addition failed. Notice this is a pointer to the real data casted to TcpOptionData type (as opposed to a copy of the option data). So changes in this return value will affect the packet data
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
void pcpp::TcpLayer::computeCalculateFields ( )
virtual

Calculate tcphdr::headerChecksum field

Implements pcpp::Layer.

TcpOptionData* pcpp::TcpLayer::getFirstTcpOptionData ( )
Returns
The first TCP option, or NULL if no TCP options exist. Notice the return value is a pointer to the real data casted to TcpOptionData type (as opposed to a copy of the option data). So changes in the return value will affect the packet data
size_t pcpp::TcpLayer::getHeaderLen ( )
inlinevirtual
Returns
Size of tcphdr + all TCP options

Implements pcpp::Layer.

TcpOptionData* pcpp::TcpLayer::getNextTcpOptionData ( TcpOptionData tcpOption)

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

Parameters
[in]tcpOptionThe TCP option to start searching from
Returns
The next TCP option or NULL if "tcpOption" is NULL or "tcpOption" is the last TCP option
OsiModelLayer pcpp::TcpLayer::getOsiModelLayer ( )
inlinevirtual
Returns
The OSI Model layer this protocol belongs to

Implements pcpp::Layer.

tcphdr* pcpp::TcpLayer::getTcpHeader ( )
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
TcpOptionData* pcpp::TcpLayer::getTcpOptionData ( TcpOption  option)

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

Parameters
[in]optionThe TCP option to get
Returns
A pointer to the TCP option location in the packet
size_t pcpp::TcpLayer::getTcpOptionsCount ( )
Returns
The number of TCP options in this layer
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)

void pcpp::TcpLayer::parseNextLayer ( )
virtual

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

Implements pcpp::Layer.

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)
bool pcpp::TcpLayer::removeTcpOption ( TcpOption  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)
std::string pcpp::TcpLayer::toString ( )
virtual
Returns
A string representation of the layer most important data (should look like the layer description in Wireshark)

Implements pcpp::Layer.