PcapPlusPlus  20.08
TextBasedProtocol.h
Go to the documentation of this file.
1 #ifndef PACKETPP_TEXT_BASED_PROTOCOL_LAYER
2 #define PACKETPP_TEXT_BASED_PROTOCOL_LAYER
3 
4 #include <map>
5 #include "Layer.h"
6 
8 
9 namespace pcpp
10 {
11 
13 #define PCPP_END_OF_TEXT_BASED_PROTOCOL_HEADER ""
14 
15 class TextBasedProtocolMessage;
16 
17 
18 // -------- Class HeaderField -----------------
19 
20 
32 {
33  friend class TextBasedProtocolMessage;
34 public:
35 
36  ~HeaderField();
37 
43  HeaderField(const HeaderField& other);
44 
50  HeaderField& operator=(const HeaderField& other);
51 
56  size_t getFieldSize() const { return m_FieldSize; }
57 
61  std::string getFieldName() const;
62 
66  std::string getFieldValue() const;
67 
72  bool setFieldValue(std::string newValue);
73 
78  bool isEndOfHeader() const { return m_IsEndOfHeaderField; }
79 
80 private:
81  HeaderField(std::string name, std::string value, char nameValueSeperator, bool spacesAllowedBetweenNameAndValue);
82  HeaderField(TextBasedProtocolMessage* TextBasedProtocolMessage, int offsetInMessage, char nameValueSeperator, bool spacesAllowedBetweenNameAndValue);
83 
84  char* getData() const;
85  void setNextField(HeaderField* nextField);
86  HeaderField *getNextField() const;
87  void initNewField(std::string name, std::string value);
88  void attachToTextBasedProtocolMessage(TextBasedProtocolMessage* message, int fieldOffsetInMessage);
89 
90  uint8_t* m_NewFieldData;
91  TextBasedProtocolMessage* m_TextBasedProtocolMessage;
92  int m_NameOffsetInMessage;
93  size_t m_FieldNameSize;
94  int m_ValueOffsetInMessage;
95  size_t m_FieldValueSize;
96  size_t m_FieldSize;
97  HeaderField* m_NextField;
98  bool m_IsEndOfHeaderField;
99  char m_NameValueSeperator;
100  bool m_SpacesAllowedBetweenNameAndValue;
101 };
102 
103 
104 
105 
106 // -------- Class TextBasedProtocolMessage -----------------
107 
114 {
115  friend class HeaderField;
116 public:
118 
127  HeaderField* getFieldByName(std::string fieldName, int index = 0) const;
128 
132  HeaderField* getFirstField() const { return m_FieldList; }
133 
139  HeaderField* getNextField(HeaderField* prevField) const { if (prevField != NULL) return prevField->getNextField(); else return NULL; }
140 
144  int getFieldCount() const;
145 
152  virtual HeaderField* addField(const std::string& fieldName, const std::string& fieldValue);
153 
159  virtual HeaderField* addField(const HeaderField& newField);
160 
166 
174  virtual HeaderField* insertField(HeaderField* prevField, const std::string& fieldName, const std::string& fieldValue);
175 
184  virtual HeaderField* insertField(std::string prevFieldName, const std::string& fieldName, const std::string& fieldValue);
185 
192  virtual HeaderField* insertField(HeaderField* prevField, const HeaderField& newField);
193 
200  bool removeField(HeaderField* fieldToRemove);
201 
209  bool removeField(std::string fieldName, int index = 0);
210 
215  bool isHeaderComplete() const;
216 
217  // implement Layer's abstract methods
218 
222  virtual void parseNextLayer();
223 
227  size_t getHeaderLen() const;
228 
232  virtual void computeCalculateFields();
233 
234 protected:
235  TextBasedProtocolMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
236  TextBasedProtocolMessage() : m_FieldList(NULL), m_LastField(NULL), m_FieldsOffset(0) {}
237 
238  // copy c'tor
239  TextBasedProtocolMessage(const TextBasedProtocolMessage& other);
240  TextBasedProtocolMessage& operator=(const TextBasedProtocolMessage& other);
241 
242  void copyDataFrom(const TextBasedProtocolMessage& other);
243 
244  void parseFields();
245  void shiftFieldsOffset(HeaderField* fromField, int numOfBytesToShift);
246 
247  // abstract methods
248  virtual char getHeaderFieldNameValueSeparator() const = 0;
249  virtual bool spacesAllowedBetweenHeaderFieldNameAndValue() const = 0;
250 
251  HeaderField* m_FieldList;
252  HeaderField* m_LastField;
253  int m_FieldsOffset;
254  std::multimap<std::string, HeaderField*> m_FieldNameToFieldMap;
255 };
256 
257 
258 }
259 
260 
261 #endif // PACKETPP_TEXT_BASED_PROTOCOL_LAYER
pcpp::TextBasedProtocolMessage::getNextField
HeaderField * getNextField(HeaderField *prevField) const
Definition: TextBasedProtocol.h:139
pcpp::Packet
Definition: Packet.h:26
pcpp::TextBasedProtocolMessage::insertField
virtual HeaderField * insertField(HeaderField *prevField, const std::string &fieldName, const std::string &fieldValue)
Layer.h
pcpp::HeaderField::setFieldValue
bool setFieldValue(std::string newValue)
pcpp::TextBasedProtocolMessage::addEndOfHeader
HeaderField * addEndOfHeader()
pcpp::TextBasedProtocolMessage::parseNextLayer
virtual void parseNextLayer()
pcpp::HeaderField::getFieldSize
size_t getFieldSize() const
Definition: TextBasedProtocol.h:56
pcpp::TextBasedProtocolMessage::removeField
bool removeField(HeaderField *fieldToRemove)
pcpp::TextBasedProtocolMessage::computeCalculateFields
virtual void computeCalculateFields()
pcpp::TextBasedProtocolMessage::getFieldByName
HeaderField * getFieldByName(std::string fieldName, int index=0) const
pcpp::TextBasedProtocolMessage
Definition: TextBasedProtocol.h:113
pcpp::HeaderField::getFieldName
std::string getFieldName() const
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::HeaderField::HeaderField
HeaderField(const HeaderField &other)
pcpp::HeaderField::isEndOfHeader
bool isEndOfHeader() const
Definition: TextBasedProtocol.h:78
pcpp::TextBasedProtocolMessage::getFirstField
HeaderField * getFirstField() const
Definition: TextBasedProtocol.h:132
pcpp::Layer
Definition: Layer.h:70
pcpp::TextBasedProtocolMessage::addField
virtual HeaderField * addField(const std::string &fieldName, const std::string &fieldValue)
pcpp::HeaderField
Definition: TextBasedProtocol.h:31
pcpp::HeaderField::operator=
HeaderField & operator=(const HeaderField &other)
pcpp::HeaderField::getFieldValue
std::string getFieldValue() const
pcpp::TextBasedProtocolMessage::isHeaderComplete
bool isHeaderComplete() const
pcpp::TextBasedProtocolMessage::getHeaderLen
size_t getHeaderLen() const
pcpp::TextBasedProtocolMessage::getFieldCount
int getFieldCount() const