PcapPlusPlus  Next
pcpp::TextBasedProtocolMessage Class Referenceabstract

#include <TextBasedProtocol.h>

Inheritance diagram for pcpp::TextBasedProtocolMessage:
pcpp::Layer pcpp::IDataContainer pcpp::HttpMessage pcpp::SdpLayer pcpp::SipLayer pcpp::HttpRequestLayer pcpp::HttpResponseLayer pcpp::SipRequestLayer pcpp::SipResponseLayer

Public Member Functions

HeaderFieldgetFieldByName (std::string fieldName, int index=0) const
 
HeaderFieldgetFirstField () const
 
HeaderFieldgetNextField (HeaderField *prevField) const
 
int getFieldCount () const
 
virtual HeaderFieldaddField (const std::string &fieldName, const std::string &fieldValue)
 
virtual HeaderFieldaddField (const HeaderField &newField)
 
HeaderFieldaddEndOfHeader ()
 
virtual HeaderFieldinsertField (HeaderField *prevField, const std::string &fieldName, const std::string &fieldValue)
 
virtual HeaderFieldinsertField (std::string prevFieldName, const std::string &fieldName, const std::string &fieldValue)
 
virtual HeaderFieldinsertField (HeaderField *prevField, const HeaderField &newField)
 
bool removeField (HeaderField *fieldToRemove)
 
bool removeField (std::string fieldName, int index=0)
 
bool isHeaderComplete () const
 
void parseNextLayer () override
 Currently set only PayloadLayer for the rest of the data.
 
size_t getHeaderLen () const override
 
void computeCalculateFields () override
 Does nothing for this class.
 
- Public Member Functions inherited from pcpp::Layer
 ~Layer () override
 
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
 Copy the raw data of this layer to another array. More...
 
size_t copyData (uint8_t *dest, size_t destSize) const
 Copy the raw data of this layer to another array, with a specified maximum size. More...
 
uint8_t * getDataPtr (size_t offset=0) const override
 
virtual std::string toString () const =0
 
virtual OsiModelLayer getOsiModelLayer () const =0
 

Protected Member Functions

void setFieldsOffset (int fieldsOffset)
 Sets the base offset of the fields in the message. More...
 
void shiftFieldsOffset (int numOfBytesToShift)
 Shifts the fields offset by a specified number of bytes. More...
 
void shiftFieldsOffset (HeaderField *fromField, int numOfBytesToShift)
 Shifts the fields offset of all headers after a specified header field by a specified number of bytes. More...
 
- Protected Member Functions inherited from pcpp::Layer
PacketgetAttachedPacket ()
 Get a pointer to the Packet this layer is attached to (if any). More...
 
Packet const * getAttachedPacket () const
 Get a pointer to the Packet this layer is attached to (if any). More...
 
void allocData (size_t dataLen, bool zeroInit=true)
 Requests the layer to allocate a new data buffer of the specified length. More...
 
template<typename T , typename... Args>
LayerconstructNextLayer (uint8_t *data, size_t dataLen, Args &&... extraArgs)
 Construct the next layer in the protocol stack. No validation is performed on the data. More...
 
template<typename T , typename... Args>
LayerconstructNextLayer (uint8_t *data, size_t dataLen, Packet *packet, Args &&... extraArgs)
 
template<typename TFactory , typename... Args>
LayerconstructNextLayerFromFactory (TFactory factoryFn, uint8_t *data, size_t dataLen, Args &&... extraArgs)
 Construct the next layer in the protocol stack using a factory functor. More...
 
template<typename TFactory , typename... Args>
LayerconstructNextLayerFromFactory (TFactory factoryFn, uint8_t *data, size_t dataLen, Packet *packet, Args &&... extraArgs)
 Construct the next layer in the protocol stack using a factory functor. More...
 
template<typename T , typename... Args>
LayertryConstructNextLayer (uint8_t *data, size_t dataLen, Args &&... extraArgs)
 
template<typename T , typename... Args>
LayertryConstructNextLayer (uint8_t *data, size_t dataLen, Packet *packet, Args &&... extraArgs)
 
template<typename T , typename TFallback , typename... Args>
LayertryConstructNextLayerWithFallback (uint8_t *data, size_t dataLen, Args &&... extraArgs)
 Try to construct the next layer in the protocol stack with a fallback option. More...
 
template<typename T , typename TFallback , typename... Args>
LayertryConstructNextLayerWithFallback (uint8_t *data, size_t dataLen, Packet *packet, Args &&... extraArgs)
 
template<typename TFallback , typename TFactory , typename... Args>
LayertryConstructNextLayerFromFactoryWithFallback (TFactory factoryFn, uint8_t *data, size_t dataLen, Args &&... extraArgs)
 Try to construct the next layer in the protocol stack using a factory functor with a fallback option. More...
 
template<typename TFallback , typename TFactory , typename... Args>
LayertryConstructNextLayerFromFactoryWithFallback (TFactory factoryFn, uint8_t *data, size_t dataLen, Packet *packet, Args &&... extraArgs)
 Try to construct the next layer in the protocol stack using a factory functor with a fallback option. More...
 

Additional Inherited Members

- Static Protected Member Functions inherited from pcpp::Layer
template<typename T >
static bool canReinterpretAs (const uint8_t *data, size_t dataLen)
 Check if the data is large enough to reinterpret as a type. More...
 

Detailed Description

An abstract base class that wraps text-based-protocol header layers (both requests and responses). It is the base class for all those layers. This class is not meant to be instantiated, hence the protected c'tor

Member Function Documentation

◆ addEndOfHeader()

HeaderField* pcpp::TextBasedProtocolMessage::addEndOfHeader ( )

Add the special end-of-header field (see the explanation in HeaderField)

Returns
A pointer to the newly created header field, or nullptr if the field could not be created

◆ addField() [1/2]

virtual HeaderField* pcpp::TextBasedProtocolMessage::addField ( const HeaderField newField)
virtual

Add a new header field to this message. This field will be added last (before the end-of-header field)

Parameters
[in]newFieldThe header field to add
Returns
A pointer to the newly created header field, or nullptr if the field could not be created

Reimplemented in pcpp::HttpMessage.

◆ addField() [2/2]

virtual HeaderField* pcpp::TextBasedProtocolMessage::addField ( const std::string &  fieldName,
const std::string &  fieldValue 
)
virtual

Add a new header field to this message. This field will be added last (before the end-of-header field)

Parameters
[in]fieldNameThe field name
[in]fieldValueThe field value
Returns
A pointer to the newly created header field, or nullptr if the field could not be created

Reimplemented in pcpp::HttpMessage.

◆ getFieldByName()

HeaderField* pcpp::TextBasedProtocolMessage::getFieldByName ( std::string  fieldName,
int  index = 0 
) const

Get a pointer to a header field by name. The search is case insensitive, meaning if a field with name "Host" exists and the fieldName parameter is "host" (all letter are lower case), this method will return a pointer to "Host" field

Parameters
[in]fieldNameThe field name
[in]indexOptional parameter. If the field name appears more than once, this parameter will indicate which field to get. The default value is 0 (get the first appearance of the field name as appears on the packet)
Returns
A pointer to an HeaderField instance, or nullptr if field doesn't exist

◆ getFieldCount()

int pcpp::TextBasedProtocolMessage::getFieldCount ( ) const
Returns
The number of header fields currently in the layer (not including CRLF at the end of the header)

◆ getFirstField()

HeaderField* pcpp::TextBasedProtocolMessage::getFirstField ( ) const
inline
Returns
A pointer to the first header field exists in this message, or nullptr if no such field exists

◆ getHeaderLen()

size_t pcpp::TextBasedProtocolMessage::getHeaderLen ( ) const
overridevirtual
Returns
The message length

Implements pcpp::Layer.

◆ getNextField()

HeaderField* pcpp::TextBasedProtocolMessage::getNextField ( HeaderField prevField) const
inline

Get the field which appears after a certain field

Parameters
[in]prevFieldA pointer to the field
Returns
The field after prevField or nullptr if prevField is the last field. If prevField is nullptr, this method will return nullptr

◆ insertField() [1/3]

virtual HeaderField* pcpp::TextBasedProtocolMessage::insertField ( HeaderField prevField,
const HeaderField newField 
)
virtual

Insert a new field after an existing field

Parameters
[in]prevFieldA pointer to the existing field
[in]newFieldThe header field to add
Returns
A pointer to the newly created header field, or nullptr if the field could not be created

Reimplemented in pcpp::HttpMessage.

◆ insertField() [2/3]

virtual HeaderField* pcpp::TextBasedProtocolMessage::insertField ( HeaderField prevField,
const std::string &  fieldName,
const std::string &  fieldValue 
)
virtual

Insert a new field after an existing field

Parameters
[in]prevFieldA pointer to the existing field. If it's nullptr the new field will be added as first field
[in]fieldNameThe field name
[in]fieldValueThe field value
Returns
A pointer to the newly created header field, or nullptr if the field could not be created

Reimplemented in pcpp::HttpMessage.

◆ insertField() [3/3]

virtual HeaderField* pcpp::TextBasedProtocolMessage::insertField ( std::string  prevFieldName,
const std::string &  fieldName,
const std::string &  fieldValue 
)
virtual

Insert a new field after an existing field

Parameters
[in]prevFieldNameA name of an existing field. If the field doesn't exist nullptr will be returned. If field name is empty ('') the new field will be added as first field
[in]fieldNameThe field name
[in]fieldValueThe field value
Returns
A pointer to the newly created header field, or nullptr if the field could not be created

◆ isHeaderComplete()

bool pcpp::TextBasedProtocolMessage::isHeaderComplete ( ) const

Indicate whether the header is complete (ending with end-of-header "\r\n\r\n" or "\n\n") or spread over more packets

Returns
True if the header is complete or false if not

◆ removeField() [1/2]

bool pcpp::TextBasedProtocolMessage::removeField ( HeaderField fieldToRemove)

Remove a field from the message

Parameters
[in]fieldToRemoveA pointer to the field that should be removed
Returns
True if the field was removed successfully, or false otherwise (for example: if fieldToRemove is nullptr, if it doesn't exist in the message, or if the removal failed)

◆ removeField() [2/2]

bool pcpp::TextBasedProtocolMessage::removeField ( std::string  fieldName,
int  index = 0 
)

Remove a field from the message

Parameters
[in]fieldNameThe name of the field that should be removed
[in]indexOptional parameter. If the field name appears more than once, this parameter will indicate which field to remove. The default value is 0 (remove the first appearance of the field name as appears on the packet)
Returns
True if the field was removed successfully, or false otherwise (for example: if fieldName doesn't exist in the message, or if the removal failed)

◆ setFieldsOffset()

void pcpp::TextBasedProtocolMessage::setFieldsOffset ( int  fieldsOffset)
protected

Sets the base offset of the fields in the message.

This offset is used to determine where in the message to begin parsing the header fields.

Parameters
[in]fieldsOffsetA non-negative integer representing the offset in bytes from the start of the message to the first header field.

◆ shiftFieldsOffset() [1/2]

void pcpp::TextBasedProtocolMessage::shiftFieldsOffset ( HeaderField fromField,
int  numOfBytesToShift 
)
protected

Shifts the fields offset of all headers after a specified header field by a specified number of bytes.

Warning
This method does not adjust the main fields offset.
Parameters
[in]fromFieldThe header field from which to start shifting the offsets.
[in]numOfBytesToShiftThe number of bytes to shift the offsets.

◆ shiftFieldsOffset() [2/2]

void pcpp::TextBasedProtocolMessage::shiftFieldsOffset ( int  numOfBytesToShift)
protected

Shifts the fields offset by a specified number of bytes.

The operation adjusts the main offset and all header fields' offsets accordingly.

This can be used when adding or removing data that is located before the header fields, ensuring that the offsets remain accurate.

Parameters
[in]numOfBytesToShiftThe number of bytes to shift the offsets.