PcapPlusPlus
TextBasedProtocol.h
1 #ifndef PACKETPP_TEXT_BASED_PROTOCOL_LAYER
2 #define PACKETPP_TEXT_BASED_PROTOCOL_LAYER
3 
4 #include <map>
5 #include "Layer.h"
6 
7 
8 namespace pcpp
9 {
10 
12 #define PCPP_END_OF_TEXT_BASED_PROTOCOL_HEADER ""
13 
14 class TextBasedProtocolMessage;
15 
16 
17 // -------- Class HeaderField -----------------
18 
19 
31 {
32  friend class TextBasedProtocolMessage;
33 public:
34 
35  ~HeaderField();
36 
42  HeaderField(const HeaderField& other);
43 
48  inline size_t getFieldSize() { return m_FieldSize; }
49 
53  std::string getFieldName() const;
54 
58  std::string getFieldValue() const;
59 
64  bool setFieldValue(std::string newValue);
65 
70  inline bool isEndOfHeader() { return m_IsEndOfHeaderField; }
71 
72 private:
73  HeaderField(std::string name, std::string value, char nameValueSeperator, bool spacesAllowedBetweenNameAndValue);
74  HeaderField(TextBasedProtocolMessage* TextBasedProtocolMessage, int offsetInMessage, char nameValueSeperator, bool spacesAllowedBetweenNameAndValue);
75  char* getData();
76  inline void setNextField(HeaderField* nextField) { m_NextField = nextField; }
77  inline HeaderField* getNextField() { return m_NextField; }
78  void initNewField(std::string name, std::string value);
79  void attachToTextBasedProtocolMessage(TextBasedProtocolMessage* message, int fieldOffsetInMessage);
80  uint8_t* m_NewFieldData;
81  TextBasedProtocolMessage* m_TextBasedProtocolMessage;
82  int m_NameOffsetInMessage;
83  size_t m_FieldNameSize;
84  int m_ValueOffsetInMessage;
85  size_t m_FieldValueSize;
86  size_t m_FieldSize;
87  HeaderField* m_NextField;
88  bool m_IsEndOfHeaderField;
89  char m_NameValueSeperator;
90  bool m_SpacesAllowedBetweenNameAndValue;
91 };
92 
93 
94 
95 
96 // -------- Class TextBasedProtocolMessage -----------------
97 
104 {
105  friend class HeaderField;
106 public:
108 
117  HeaderField* getFieldByName(std::string fieldName, int index = 0);
118 
122  inline HeaderField* getFirstField() { return m_FieldList; }
123 
129  inline HeaderField* getNextField(HeaderField* prevField) { if (prevField != NULL) return prevField->getNextField(); else return NULL; }
130 
134  int getFieldCount();
135 
142  virtual HeaderField* addField(const std::string& fieldName, const std::string& fieldValue);
143 
149  virtual HeaderField* addField(const HeaderField& newField);
150 
156 
164  virtual HeaderField* insertField(HeaderField* prevField, const std::string& fieldName, const std::string& fieldValue);
165 
174  virtual HeaderField* insertField(std::string prevFieldName, const std::string& fieldName, const std::string& fieldValue);
175 
182  virtual HeaderField* insertField(HeaderField* prevField, const HeaderField& newField);
183 
190  bool removeField(HeaderField* fieldToRemove);
191 
199  bool removeField(std::string fieldName, int index = 0);
200 
205  bool isHeaderComplete();
206 
207  // implement Layer's abstract methods
208 
212  virtual void parseNextLayer();
213 
217  size_t getHeaderLen();
218 
222  virtual void computeCalculateFields();
223 
224 protected:
225  TextBasedProtocolMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
226  TextBasedProtocolMessage() : m_FieldList(NULL), m_LastField(NULL), m_FieldsOffset(0) {}
227 
228  // copy c'tor
229  TextBasedProtocolMessage(const TextBasedProtocolMessage& other);
230  TextBasedProtocolMessage& operator=(const TextBasedProtocolMessage& other);
231 
232  void copyDataFrom(const TextBasedProtocolMessage& other);
233 
234  void parseFields();
235  void shiftFieldsOffset(HeaderField* fromField, int numOfBytesToShift);
236 
237  // abstract methods
238  virtual char getHeaderFieldNameValueSeparator() = 0;
239  virtual bool spacesAllowedBetweenHeaderFieldNameAndValue() = 0;
240 
241  HeaderField* m_FieldList;
242  HeaderField* m_LastField;
243  int m_FieldsOffset;
244  std::multimap<std::string, HeaderField*> m_FieldNameToFieldMap;
245 };
246 
247 
248 }
249 
250 
251 #endif // PACKETPP_TEXT_BASED_PROTOCOL_LAYER
bool isEndOfHeader()
Definition: TextBasedProtocol.h:70
bool removeField(HeaderField *fieldToRemove)
Definition: TextBasedProtocol.h:103
std::string getFieldName() const
HeaderField(const HeaderField &other)
Definition: Packet.h:26
virtual HeaderField * insertField(HeaderField *prevField, const std::string &fieldName, const std::string &fieldValue)
size_t getFieldSize()
Definition: TextBasedProtocol.h:48
bool setFieldValue(std::string newValue)
Definition: Layer.h:52
HeaderField * getFieldByName(std::string fieldName, int index=0)
std::string getFieldValue() const
HeaderField * getFirstField()
Definition: TextBasedProtocol.h:122
virtual HeaderField * addField(const std::string &fieldName, const std::string &fieldValue)
Definition: TextBasedProtocol.h:30
The main namespace for the PcapPlusPlus lib.
virtual void computeCalculateFields()
HeaderField * getNextField(HeaderField *prevField)
Definition: TextBasedProtocol.h:129