PcapPlusPlus
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 
49  inline size_t getFieldSize() { return m_FieldSize; }
50 
54  std::string getFieldName() const;
55 
59  std::string getFieldValue() const;
60 
65  bool setFieldValue(std::string newValue);
66 
71  inline bool isEndOfHeader() { return m_IsEndOfHeaderField; }
72 
73 private:
74  HeaderField(std::string name, std::string value, char nameValueSeperator, bool spacesAllowedBetweenNameAndValue);
75  HeaderField(TextBasedProtocolMessage* TextBasedProtocolMessage, int offsetInMessage, char nameValueSeperator, bool spacesAllowedBetweenNameAndValue);
76  char* getData();
77  inline void setNextField(HeaderField* nextField) { m_NextField = nextField; }
78  inline HeaderField* getNextField() { return m_NextField; }
79  void initNewField(std::string name, std::string value);
80  void attachToTextBasedProtocolMessage(TextBasedProtocolMessage* message, int fieldOffsetInMessage);
81  uint8_t* m_NewFieldData;
82  TextBasedProtocolMessage* m_TextBasedProtocolMessage;
83  int m_NameOffsetInMessage;
84  size_t m_FieldNameSize;
85  int m_ValueOffsetInMessage;
86  size_t m_FieldValueSize;
87  size_t m_FieldSize;
88  HeaderField* m_NextField;
89  bool m_IsEndOfHeaderField;
90  char m_NameValueSeperator;
91  bool m_SpacesAllowedBetweenNameAndValue;
92 };
93 
94 
95 
96 
97 // -------- Class TextBasedProtocolMessage -----------------
98 
105 {
106  friend class HeaderField;
107 public:
109 
118  HeaderField* getFieldByName(std::string fieldName, int index = 0);
119 
123  inline HeaderField* getFirstField() { return m_FieldList; }
124 
130  inline HeaderField* getNextField(HeaderField* prevField) { if (prevField != NULL) return prevField->getNextField(); else return NULL; }
131 
135  int getFieldCount();
136 
143  virtual HeaderField* addField(const std::string& fieldName, const std::string& fieldValue);
144 
150  virtual HeaderField* addField(const HeaderField& newField);
151 
157 
165  virtual HeaderField* insertField(HeaderField* prevField, const std::string& fieldName, const std::string& fieldValue);
166 
175  virtual HeaderField* insertField(std::string prevFieldName, const std::string& fieldName, const std::string& fieldValue);
176 
183  virtual HeaderField* insertField(HeaderField* prevField, const HeaderField& newField);
184 
191  bool removeField(HeaderField* fieldToRemove);
192 
200  bool removeField(std::string fieldName, int index = 0);
201 
206  bool isHeaderComplete();
207 
208  // implement Layer's abstract methods
209 
213  virtual void parseNextLayer();
214 
218  size_t getHeaderLen();
219 
223  virtual void computeCalculateFields();
224 
225 protected:
226  TextBasedProtocolMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
227  TextBasedProtocolMessage() : m_FieldList(NULL), m_LastField(NULL), m_FieldsOffset(0) {}
228 
229  // copy c'tor
230  TextBasedProtocolMessage(const TextBasedProtocolMessage& other);
231  TextBasedProtocolMessage& operator=(const TextBasedProtocolMessage& other);
232 
233  void copyDataFrom(const TextBasedProtocolMessage& other);
234 
235  void parseFields();
236  void shiftFieldsOffset(HeaderField* fromField, int numOfBytesToShift);
237 
238  // abstract methods
239  virtual char getHeaderFieldNameValueSeparator() = 0;
240  virtual bool spacesAllowedBetweenHeaderFieldNameAndValue() = 0;
241 
242  HeaderField* m_FieldList;
243  HeaderField* m_LastField;
244  int m_FieldsOffset;
245  std::multimap<std::string, HeaderField*> m_FieldNameToFieldMap;
246 };
247 
248 
249 }
250 
251 
252 #endif // PACKETPP_TEXT_BASED_PROTOCOL_LAYER
Definition: TextBasedProtocol.h:31
bool removeField(HeaderField *fieldToRemove)
size_t getFieldSize()
Definition: TextBasedProtocol.h:49
bool setFieldValue(std::string newValue)
Definition: Layer.h:70
bool isEndOfHeader()
Definition: TextBasedProtocol.h:71
virtual HeaderField * addField(const std::string &fieldName, const std::string &fieldValue)
Definition: Packet.h:26
Definition: TextBasedProtocol.h:104
HeaderField * getFieldByName(std::string fieldName, int index=0)
std::string getFieldName() const
std::string getFieldValue() const
HeaderField(const HeaderField &other)
HeaderField * getNextField(HeaderField *prevField)
Definition: TextBasedProtocol.h:130
virtual void computeCalculateFields()
The main namespace for the PcapPlusPlus lib.
HeaderField * getFirstField()
Definition: TextBasedProtocol.h:123
virtual HeaderField * insertField(HeaderField *prevField, const std::string &fieldName, const std::string &fieldValue)