PcapPlusPlus  23.09
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 
51  HeaderField& operator=(const HeaderField& other);
52 
57  size_t getFieldSize() const { return m_FieldSize; }
58 
62  std::string getFieldName() const;
63 
67  std::string getFieldValue() const;
68 
74  bool setFieldValue(const std::string& newValue);
75 
80  bool isEndOfHeader() const { return m_IsEndOfHeaderField; }
81 
82 private:
83  HeaderField(const std::string& name, const std::string& value, char nameValueSeparator, bool spacesAllowedBetweenNameAndValue);
84  HeaderField(TextBasedProtocolMessage* TextBasedProtocolMessage, int offsetInMessage, char nameValueSeparator, bool spacesAllowedBetweenNameAndValue);
85 
86  char* getData() const;
87  void setNextField(HeaderField* nextField);
88  HeaderField *getNextField() const;
89  void initNewField(const std::string& name, const std::string& value);
90  void attachToTextBasedProtocolMessage(TextBasedProtocolMessage* message, int fieldOffsetInMessage);
91 
92  uint8_t* m_NewFieldData;
93  TextBasedProtocolMessage* m_TextBasedProtocolMessage;
94  int m_NameOffsetInMessage;
95  size_t m_FieldNameSize;
96  int m_ValueOffsetInMessage;
97  size_t m_FieldValueSize;
98  size_t m_FieldSize;
99  HeaderField* m_NextField;
100  bool m_IsEndOfHeaderField;
101  char m_NameValueSeparator;
102  bool m_SpacesAllowedBetweenNameAndValue;
103 };
104 
105 
106 
107 
108 // -------- Class TextBasedProtocolMessage -----------------
109 
116 {
117  friend class HeaderField;
118 public:
120 
129  HeaderField* getFieldByName(std::string fieldName, int index = 0) const;
130 
134  HeaderField* getFirstField() const { return m_FieldList; }
135 
141  HeaderField* getNextField(HeaderField* prevField) const { if (prevField != NULL) return prevField->getNextField(); else return NULL; }
142 
146  int getFieldCount() const;
147 
154  virtual HeaderField* addField(const std::string& fieldName, const std::string& fieldValue);
155 
161  virtual HeaderField* addField(const HeaderField& newField);
162 
167  HeaderField* addEndOfHeader();
168 
176  virtual HeaderField* insertField(HeaderField* prevField, const std::string& fieldName, const std::string& fieldValue);
177 
186  virtual HeaderField* insertField(std::string prevFieldName, const std::string& fieldName, const std::string& fieldValue);
187 
194  virtual HeaderField* insertField(HeaderField* prevField, const HeaderField& newField);
195 
202  bool removeField(HeaderField* fieldToRemove);
203 
211  bool removeField(std::string fieldName, int index = 0);
212 
217  bool isHeaderComplete() const;
218 
219  // implement Layer's abstract methods
220 
224  virtual void parseNextLayer();
225 
229  size_t getHeaderLen() const;
230 
234  virtual void computeCalculateFields();
235 
236 protected:
237  TextBasedProtocolMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
238  TextBasedProtocolMessage() : m_FieldList(NULL), m_LastField(NULL), m_FieldsOffset(0) {}
239 
240  // copy c'tor
243 
244  void copyDataFrom(const TextBasedProtocolMessage& other);
245 
246  void parseFields();
247  void shiftFieldsOffset(HeaderField* fromField, int numOfBytesToShift);
248 
249  // abstract methods
250  virtual char getHeaderFieldNameValueSeparator() const = 0;
251  virtual bool spacesAllowedBetweenHeaderFieldNameAndValue() const = 0;
252 
253  HeaderField* m_FieldList;
254  HeaderField* m_LastField;
255  int m_FieldsOffset;
256  std::multimap<std::string, HeaderField*> m_FieldNameToFieldMap;
257 };
258 
259 
260 }
261 
262 
263 #endif // PACKETPP_TEXT_BASED_PROTOCOL_LAYER
The main namespace for the PcapPlusPlus lib.
HeaderField * getFirstField() const
Definition: TextBasedProtocol.h:134
Definition: Layer.h:70
Definition: Packet.h:26
HeaderField(const HeaderField &other)
Definition: TextBasedProtocol.h:115
std::string getFieldValue() const
bool setFieldValue(const std::string &newValue)
bool isEndOfHeader() const
Definition: TextBasedProtocol.h:80
HeaderField * getNextField(HeaderField *prevField) const
Definition: TextBasedProtocol.h:141
size_t getFieldSize() const
Definition: TextBasedProtocol.h:57
HeaderField & operator=(const HeaderField &other)
Definition: TextBasedProtocol.h:31
std::string getFieldName() const