/src/PcapPlusPlus/Packet++/header/TextBasedProtocol.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <map> |
4 | | #include "Layer.h" |
5 | | |
6 | | /// @file |
7 | | |
8 | | namespace pcpp |
9 | | { |
10 | | |
11 | | /** End of header */ |
12 | 55.5k | #define PCPP_END_OF_TEXT_BASED_PROTOCOL_HEADER "" |
13 | | |
14 | | class TextBasedProtocolMessage; |
15 | | |
16 | | |
17 | | // -------- Class HeaderField ----------------- |
18 | | |
19 | | |
20 | | /** |
21 | | * @class HeaderField |
22 | | * A wrapper class for each text-based-protocol header field, e.g "Host", "Cookie", "Content-Length", "Via", "Call-ID", etc. |
23 | | * Each field contains a name (e.g "Host") and a value (e.g "www.wikipedia.org"). The user can get and set both of them through dedicated methods. |
24 | | * The separator between header fields is either CRLF ("\r\n\") or LF ("\n") in more rare cases, which means every HeaderField instance is |
25 | | * responsible for wrapping and parsing a header field from the previous CRLF (not inclusive) until the next CRLF/LF (inclusive) |
26 | | * A special case is with the end of a header, meaning 2 consecutive CRLFs ("\r\n\r\n") or consecutive LFs ("\n\n"). PcapPlusPlus treats the first |
27 | | * CRLF/LF as part of the last field in the header, and the second CRLF is an HeaderField instance of its own which name and values are an empty string ("") |
28 | | * or pcpp::PCPP_END_OF_TEXT_BASED_PROTOCOL_HEADER |
29 | | */ |
30 | | class HeaderField |
31 | | { |
32 | | friend class TextBasedProtocolMessage; |
33 | | public: |
34 | | |
35 | | ~HeaderField(); |
36 | | |
37 | | /** |
38 | | * A copy constructor that creates a new instance out of an existing HeaderField instance. The copied instance will not have shared |
39 | | * resources with the original instance, meaning all members and properties are copied |
40 | | * @param[in] other The original instance to copy from |
41 | | */ |
42 | | HeaderField(const HeaderField& other); |
43 | | |
44 | | /** |
45 | | * Assignment operator for this class. This method copies the data from the other instance and will not share any resources with it. |
46 | | * Also, if the instance already contains data it will be deleted or zeroed |
47 | | * @param[in] other The instance to assign from |
48 | | * @return A reference to the assignee |
49 | | */ |
50 | | HeaderField& operator=(const HeaderField& other); |
51 | | |
52 | | /** |
53 | | * @return The field length in bytes, meaning count of all characters from the previous CRLF (not inclusive) until the next CRLF (inclusive) |
54 | | * For example: the field "Host: www.wikipedia.org\r\n" will have the length of 25 |
55 | | */ |
56 | 1.89M | size_t getFieldSize() const { return m_FieldSize; } |
57 | | |
58 | | /** |
59 | | * @return The field name as string. Notice the return data is copied data, so changing it won't change the packet data |
60 | | */ |
61 | | std::string getFieldName() const; |
62 | | |
63 | | /** |
64 | | * @return The field value as string. Notice the return data is copied data, so changing it won't change the packet data |
65 | | */ |
66 | | std::string getFieldValue() const; |
67 | | |
68 | | /** |
69 | | * A setter for field value |
70 | | * @param[in] newValue The new value to set to the field. Old value will be deleted |
71 | | * @return True if setting the value was completed successfully, false otherwise |
72 | | */ |
73 | | bool setFieldValue(const std::string& newValue); |
74 | | |
75 | | /** |
76 | | * Get an indication whether the field is a field that ends the header (meaning contain only CRLF - see class explanation) |
77 | | * @return True if this is a end-of-header field, false otherwise |
78 | | */ |
79 | 694k | bool isEndOfHeader() const { return m_IsEndOfHeaderField; } |
80 | | |
81 | | private: |
82 | | HeaderField(const std::string& name, const std::string& value, char nameValueSeparator, bool spacesAllowedBetweenNameAndValue); |
83 | | HeaderField(TextBasedProtocolMessage* TextBasedProtocolMessage, int offsetInMessage, char nameValueSeparator, bool spacesAllowedBetweenNameAndValue); |
84 | | |
85 | | char* getData() const; |
86 | | void setNextField(HeaderField* nextField); |
87 | | HeaderField *getNextField() const; |
88 | | void initNewField(const std::string& name, const std::string& value); |
89 | | void attachToTextBasedProtocolMessage(TextBasedProtocolMessage* message, int fieldOffsetInMessage); |
90 | | |
91 | | uint8_t* m_NewFieldData; |
92 | | TextBasedProtocolMessage* m_TextBasedProtocolMessage; |
93 | | int m_NameOffsetInMessage; |
94 | | size_t m_FieldNameSize; |
95 | | int m_ValueOffsetInMessage; |
96 | | size_t m_FieldValueSize; |
97 | | size_t m_FieldSize; |
98 | | HeaderField* m_NextField; |
99 | | bool m_IsEndOfHeaderField; |
100 | | char m_NameValueSeparator; |
101 | | bool m_SpacesAllowedBetweenNameAndValue; |
102 | | }; |
103 | | |
104 | | |
105 | | |
106 | | |
107 | | // -------- Class TextBasedProtocolMessage ----------------- |
108 | | |
109 | | /** |
110 | | * @class TextBasedProtocolMessage |
111 | | * An abstract base class that wraps text-based-protocol header layers (both requests and responses). It is the base class for all those layers. |
112 | | * This class is not meant to be instantiated, hence the protected c'tor |
113 | | */ |
114 | | class TextBasedProtocolMessage : public Layer |
115 | | { |
116 | | friend class HeaderField; |
117 | | public: |
118 | | ~TextBasedProtocolMessage(); |
119 | | |
120 | | /** |
121 | | * Get a pointer to a header field by name. The search is case insensitive, meaning if a field with name "Host" exists and the |
122 | | * fieldName parameter is "host" (all letter are lower case), this method will return a pointer to "Host" field |
123 | | * @param[in] fieldName The field name |
124 | | * @param[in] index Optional parameter. If the field name appears more than once, this parameter will indicate which field to get. |
125 | | * The default value is 0 (get the first appearance of the field name as appears on the packet) |
126 | | * @return A pointer to an HeaderField instance, or NULL if field doesn't exist |
127 | | */ |
128 | | HeaderField* getFieldByName(std::string fieldName, int index = 0) const; |
129 | | |
130 | | /** |
131 | | * @return A pointer to the first header field exists in this message, or NULL if no such field exists |
132 | | */ |
133 | 75 | HeaderField* getFirstField() const { return m_FieldList; } |
134 | | |
135 | | /** |
136 | | * Get the field which appears after a certain field |
137 | | * @param[in] prevField A pointer to the field |
138 | | * @return The field after prevField or NULL if prevField is the last field. If prevField is NULL, this method will return NULL |
139 | | */ |
140 | 0 | HeaderField* getNextField(HeaderField* prevField) const { if (prevField != NULL) return prevField->getNextField(); else return NULL; } |
141 | | |
142 | | /** |
143 | | * @return The number of header fields currently in the layer (not including CRLF at the end of the header) |
144 | | */ |
145 | | int getFieldCount() const; |
146 | | |
147 | | /** |
148 | | * Add a new header field to this message. This field will be added last (before the end-of-header field) |
149 | | * @param[in] fieldName The field name |
150 | | * @param[in] fieldValue The field value |
151 | | * @return A pointer to the newly created header field, or NULL if the field could not be created |
152 | | */ |
153 | | virtual HeaderField* addField(const std::string& fieldName, const std::string& fieldValue); |
154 | | |
155 | | /** |
156 | | * Add a new header field to this message. This field will be added last (before the end-of-header field) |
157 | | * @param[in] newField The header field to add |
158 | | * @return A pointer to the newly created header field, or NULL if the field could not be created |
159 | | */ |
160 | | virtual HeaderField* addField(const HeaderField& newField); |
161 | | |
162 | | /** |
163 | | * Add the special end-of-header field (see the explanation in HeaderField) |
164 | | * @return A pointer to the newly created header field, or NULL if the field could not be created |
165 | | */ |
166 | | HeaderField* addEndOfHeader(); |
167 | | |
168 | | /** |
169 | | * Insert a new field after an existing field |
170 | | * @param[in] prevField A pointer to the existing field. If it's NULL the new field will be added as first field |
171 | | * @param[in] fieldName The field name |
172 | | * @param[in] fieldValue The field value |
173 | | * @return A pointer to the newly created header field, or NULL if the field could not be created |
174 | | */ |
175 | | virtual HeaderField* insertField(HeaderField* prevField, const std::string& fieldName, const std::string& fieldValue); |
176 | | |
177 | | /** |
178 | | * Insert a new field after an existing field |
179 | | * @param[in] prevFieldName A name of an existing field. If the field doesn't exist NULL will be returned. |
180 | | * If field name is empty ('') the new field will be added as first field |
181 | | * @param[in] fieldName The field name |
182 | | * @param[in] fieldValue The field value |
183 | | * @return A pointer to the newly created header field, or NULL if the field could not be created |
184 | | */ |
185 | | virtual HeaderField* insertField(std::string prevFieldName, const std::string& fieldName, const std::string& fieldValue); |
186 | | |
187 | | /** |
188 | | * Insert a new field after an existing field |
189 | | * @param[in] prevField A pointer to the existing field |
190 | | * @param[in] newField The header field to add |
191 | | * @return A pointer to the newly created header field, or NULL if the field could not be created |
192 | | */ |
193 | | virtual HeaderField* insertField(HeaderField* prevField, const HeaderField& newField); |
194 | | |
195 | | /** |
196 | | * Remove a field from the message |
197 | | * @param[in] fieldToRemove A pointer to the field that should be removed |
198 | | * @return True if the field was removed successfully, or false otherwise (for example: if fieldToRemove is NULL, if it doesn't exist |
199 | | * in the message, or if the removal failed) |
200 | | */ |
201 | | bool removeField(HeaderField* fieldToRemove); |
202 | | |
203 | | /** |
204 | | * Remove a field from the message |
205 | | * @param[in] fieldName The name of the field that should be removed |
206 | | * @param[in] index Optional parameter. If the field name appears more than once, this parameter will indicate which field to remove. |
207 | | * The default value is 0 (remove the first appearance of the field name as appears on the packet) |
208 | | * @return True if the field was removed successfully, or false otherwise (for example: if fieldName doesn't exist in the message, or if the removal failed) |
209 | | */ |
210 | | bool removeField(std::string fieldName, int index = 0); |
211 | | |
212 | | /** |
213 | | * Indicate whether the header is complete (ending with end-of-header "\r\n\r\n" or "\n\n") or spread over more packets |
214 | | * @return True if the header is complete or false if not |
215 | | */ |
216 | | bool isHeaderComplete() const; |
217 | | |
218 | | // implement Layer's abstract methods |
219 | | |
220 | | /** |
221 | | * Currently set only PayloadLayer for the rest of the data |
222 | | */ |
223 | | virtual void parseNextLayer(); |
224 | | |
225 | | /** |
226 | | * @return The message length |
227 | | */ |
228 | | size_t getHeaderLen() const; |
229 | | |
230 | | /** |
231 | | * Does nothing for this class |
232 | | */ |
233 | | virtual void computeCalculateFields(); |
234 | | |
235 | | protected: |
236 | | TextBasedProtocolMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); |
237 | 0 | TextBasedProtocolMessage() : m_FieldList(NULL), m_LastField(NULL), m_FieldsOffset(0) {} |
238 | | |
239 | | // copy c'tor |
240 | | TextBasedProtocolMessage(const TextBasedProtocolMessage& other); |
241 | | TextBasedProtocolMessage& operator=(const TextBasedProtocolMessage& other); |
242 | | |
243 | | void copyDataFrom(const TextBasedProtocolMessage& other); |
244 | | |
245 | | void parseFields(); |
246 | | void shiftFieldsOffset(HeaderField* fromField, int numOfBytesToShift); |
247 | | |
248 | | // abstract methods |
249 | | virtual char getHeaderFieldNameValueSeparator() const = 0; |
250 | | virtual bool spacesAllowedBetweenHeaderFieldNameAndValue() const = 0; |
251 | | |
252 | | HeaderField* m_FieldList; |
253 | | HeaderField* m_LastField; |
254 | | int m_FieldsOffset; |
255 | | std::multimap<std::string, HeaderField*> m_FieldNameToFieldMap; |
256 | | }; |
257 | | |
258 | | |
259 | | } |