PcapPlusPlus  19.12
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  size_t getFieldSize() const { return m_FieldSize; }
50 
54  std::string getFieldName() const;
55 
59  std::string getFieldValue() const;
60 
65  bool setFieldValue(std::string newValue);
66 
71  bool isEndOfHeader() const { 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 
77  char* getData() const;
78  void setNextField(HeaderField* nextField);
79  HeaderField *getNextField() const;
80  void initNewField(std::string name, std::string value);
81  void attachToTextBasedProtocolMessage(TextBasedProtocolMessage* message, int fieldOffsetInMessage);
82 
83  uint8_t* m_NewFieldData;
84  TextBasedProtocolMessage* m_TextBasedProtocolMessage;
85  int m_NameOffsetInMessage;
86  size_t m_FieldNameSize;
87  int m_ValueOffsetInMessage;
88  size_t m_FieldValueSize;
89  size_t m_FieldSize;
90  HeaderField* m_NextField;
91  bool m_IsEndOfHeaderField;
92  char m_NameValueSeperator;
93  bool m_SpacesAllowedBetweenNameAndValue;
94 };
95 
96 
97 
98 
99 // -------- Class TextBasedProtocolMessage -----------------
100 
107 {
108  friend class HeaderField;
109 public:
111 
120  HeaderField* getFieldByName(std::string fieldName, int index = 0) const;
121 
125  HeaderField* getFirstField() const { return m_FieldList; }
126 
132  HeaderField* getNextField(HeaderField* prevField) const { if (prevField != NULL) return prevField->getNextField(); else return NULL; }
133 
137  int getFieldCount() const;
138 
145  virtual HeaderField* addField(const std::string& fieldName, const std::string& fieldValue);
146 
152  virtual HeaderField* addField(const HeaderField& newField);
153 
159 
167  virtual HeaderField* insertField(HeaderField* prevField, const std::string& fieldName, const std::string& fieldValue);
168 
177  virtual HeaderField* insertField(std::string prevFieldName, const std::string& fieldName, const std::string& fieldValue);
178 
185  virtual HeaderField* insertField(HeaderField* prevField, const HeaderField& newField);
186 
193  bool removeField(HeaderField* fieldToRemove);
194 
202  bool removeField(std::string fieldName, int index = 0);
203 
208  bool isHeaderComplete() const;
209 
210  // implement Layer's abstract methods
211 
215  virtual void parseNextLayer();
216 
220  size_t getHeaderLen() const;
221 
225  virtual void computeCalculateFields();
226 
227 protected:
228  TextBasedProtocolMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
229  TextBasedProtocolMessage() : m_FieldList(NULL), m_LastField(NULL), m_FieldsOffset(0) {}
230 
231  // copy c'tor
232  TextBasedProtocolMessage(const TextBasedProtocolMessage& other);
233  TextBasedProtocolMessage& operator=(const TextBasedProtocolMessage& other);
234 
235  void copyDataFrom(const TextBasedProtocolMessage& other);
236 
237  void parseFields();
238  void shiftFieldsOffset(HeaderField* fromField, int numOfBytesToShift);
239 
240  // abstract methods
241  virtual char getHeaderFieldNameValueSeparator() const = 0;
242  virtual bool spacesAllowedBetweenHeaderFieldNameAndValue() const = 0;
243 
244  HeaderField* m_FieldList;
245  HeaderField* m_LastField;
246  int m_FieldsOffset;
247  std::multimap<std::string, HeaderField*> m_FieldNameToFieldMap;
248 };
249 
250 
251 }
252 
253 
254 #endif // PACKETPP_TEXT_BASED_PROTOCOL_LAYER
pcpp::TextBasedProtocolMessage::getNextField
HeaderField * getNextField(HeaderField *prevField) const
Definition: TextBasedProtocol.h:132
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:49
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:106
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:71
pcpp::TextBasedProtocolMessage::getFirstField
HeaderField * getFirstField() const
Definition: TextBasedProtocol.h:125
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::getFieldValue
std::string getFieldValue() const
pcpp::TextBasedProtocolMessage::isHeaderComplete
bool isHeaderComplete() const
pcpp::TextBasedProtocolMessage::getHeaderLen
size_t getHeaderLen() const
pcpp::TextBasedProtocolMessage::getFieldCount
int getFieldCount() const