PcapPlusPlus  24.09
TextBasedProtocol.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include "Layer.h"
5 
7 
8 namespace pcpp
9 {
10 
12 #define PCPP_END_OF_TEXT_BASED_PROTOCOL_HEADER ""
13 
14  class TextBasedProtocolMessage;
15 
16  // -------- Class HeaderField -----------------
17 
30  {
31  friend class TextBasedProtocolMessage;
32 
33  public:
34  ~HeaderField();
35 
41  HeaderField(const HeaderField& other);
42 
50 
56  size_t getFieldSize() const
57  {
58  return m_FieldSize;
59  }
60 
65  std::string getFieldName() const;
66 
71  std::string getFieldValue() const;
72 
78  bool setFieldValue(const std::string& newValue);
79 
85  bool isEndOfHeader() const
86  {
87  return m_IsEndOfHeaderField;
88  }
89 
90  private:
91  HeaderField(const std::string& name, const std::string& value, char nameValueSeparator,
92  bool spacesAllowedBetweenNameAndValue);
93  HeaderField(TextBasedProtocolMessage* TextBasedProtocolMessage, int offsetInMessage, char nameValueSeparator,
94  bool spacesAllowedBetweenNameAndValue);
95 
96  char* getData() const;
97  void setNextField(HeaderField* nextField);
98  HeaderField* getNextField() const;
99  void initNewField(const std::string& name, const std::string& value);
100  void attachToTextBasedProtocolMessage(TextBasedProtocolMessage* message, int fieldOffsetInMessage);
101 
102  uint8_t* m_NewFieldData;
103  TextBasedProtocolMessage* m_TextBasedProtocolMessage;
104  int m_NameOffsetInMessage;
105  size_t m_FieldNameSize;
106  int m_ValueOffsetInMessage;
107  size_t m_FieldValueSize;
108  size_t m_FieldSize;
109  HeaderField* m_NextField;
110  bool m_IsEndOfHeaderField;
111  char m_NameValueSeparator;
112  bool m_SpacesAllowedBetweenNameAndValue;
113  };
114 
115  // -------- Class TextBasedProtocolMessage -----------------
116 
123  {
124  friend class HeaderField;
125 
126  public:
128 
139  HeaderField* getFieldByName(std::string fieldName, int index = 0) const;
140 
145  {
146  return m_FieldList;
147  }
148 
156  {
157  if (prevField != nullptr)
158  return prevField->getNextField();
159  else
160  return nullptr;
161  }
162 
166  int getFieldCount() const;
167 
174  virtual HeaderField* addField(const std::string& fieldName, const std::string& fieldValue);
175 
181  virtual HeaderField* addField(const HeaderField& newField);
182 
188 
197  virtual HeaderField* insertField(HeaderField* prevField, const std::string& fieldName,
198  const std::string& fieldValue);
199 
208  virtual HeaderField* insertField(std::string prevFieldName, const std::string& fieldName,
209  const std::string& fieldValue);
210 
217  virtual HeaderField* insertField(HeaderField* prevField, const HeaderField& newField);
218 
225  bool removeField(HeaderField* fieldToRemove);
226 
236  bool removeField(std::string fieldName, int index = 0);
237 
243  bool isHeaderComplete() const;
244 
245  // implement Layer's abstract methods
246 
250  virtual void parseNextLayer();
251 
255  size_t getHeaderLen() const;
256 
260  virtual void computeCalculateFields();
261 
262  protected:
263  TextBasedProtocolMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet,
264  ProtocolType protocol);
265  TextBasedProtocolMessage() : m_FieldList(nullptr), m_LastField(nullptr), m_FieldsOffset(0)
266  {}
267 
268  // copy c'tor
270  TextBasedProtocolMessage& operator=(const TextBasedProtocolMessage& other);
271 
272  void copyDataFrom(const TextBasedProtocolMessage& other);
273 
274  void parseFields();
275  void shiftFieldsOffset(HeaderField* fromField, int numOfBytesToShift);
276 
277  // abstract methods
278  virtual char getHeaderFieldNameValueSeparator() const = 0;
279  virtual bool spacesAllowedBetweenHeaderFieldNameAndValue() const = 0;
280 
281  HeaderField* m_FieldList;
282  HeaderField* m_LastField;
283  int m_FieldsOffset;
284  std::multimap<std::string, HeaderField*> m_FieldNameToFieldMap;
285  };
286 
287 } // namespace pcpp
Definition: TextBasedProtocol.h:30
size_t getFieldSize() const
Definition: TextBasedProtocol.h:56
std::string getFieldValue() const
bool setFieldValue(const std::string &newValue)
HeaderField & operator=(const HeaderField &other)
HeaderField(const HeaderField &other)
std::string getFieldName() const
bool isEndOfHeader() const
Definition: TextBasedProtocol.h:85
Definition: Layer.h:70
Definition: Packet.h:27
Definition: TextBasedProtocol.h:123
HeaderField * getFieldByName(std::string fieldName, int index=0) const
bool removeField(HeaderField *fieldToRemove)
HeaderField * getFirstField() const
Definition: TextBasedProtocol.h:144
virtual HeaderField * insertField(std::string prevFieldName, const std::string &fieldName, const std::string &fieldValue)
HeaderField * getNextField(HeaderField *prevField) const
Definition: TextBasedProtocol.h:155
bool removeField(std::string fieldName, int index=0)
virtual void computeCalculateFields()
virtual HeaderField * insertField(HeaderField *prevField, const std::string &fieldName, const std::string &fieldValue)
virtual HeaderField * insertField(HeaderField *prevField, const HeaderField &newField)
virtual HeaderField * addField(const std::string &fieldName, const std::string &fieldValue)
virtual HeaderField * addField(const HeaderField &newField)
The main namespace for the PcapPlusPlus lib.
uint8_t ProtocolType
Definition: ProtocolType.h:17