/src/PcapPlusPlus/Packet++/header/SipLayer.h
Line | Count | Source |
1 | | #pragma once |
2 | | |
3 | | #include "TextBasedProtocol.h" |
4 | | |
5 | | /// @file |
6 | | |
7 | | /// @namespace pcpp |
8 | | /// @brief The main namespace for the PcapPlusPlus lib |
9 | | namespace pcpp |
10 | | { |
11 | | // some popular SIP header fields |
12 | | |
13 | | /// From field |
14 | | #define PCPP_SIP_FROM_FIELD "From" |
15 | | /// To field |
16 | | #define PCPP_SIP_TO_FIELD "To" |
17 | | /// Via field |
18 | | #define PCPP_SIP_VIA_FIELD "Via" |
19 | | /// Call-ID field |
20 | | #define PCPP_SIP_CALL_ID_FIELD "Call-ID" |
21 | | /// Content-Type field |
22 | 0 | #define PCPP_SIP_CONTENT_TYPE_FIELD "Content-Type" |
23 | | /// Content-Length field |
24 | 0 | #define PCPP_SIP_CONTENT_LENGTH_FIELD "Content-Length" |
25 | | /// Content-Disposition field |
26 | | #define PCPP_SIP_CONTENT_DISPOSITION_FIELD "Content-Disposition" |
27 | | /// Content-Encoding field |
28 | | #define PCPP_SIP_CONTENT_ENCODING_FIELD "Content-Encoding" |
29 | | /// Content-Language field |
30 | | #define PCPP_SIP_CONTENT_LANGUAGE_FIELD "Content-Language" |
31 | | /// CSeq field |
32 | | #define PCPP_SIP_CSEQ_FIELD "CSeq" |
33 | | /// Contact field |
34 | | #define PCPP_SIP_CONTACT_FIELD "Contact" |
35 | | /// Max-Forwards field |
36 | | #define PCPP_SIP_MAX_FORWARDS_FIELD "Max-Forwards" |
37 | | /// User-Agent field |
38 | | #define PCPP_SIP_USER_AGENT_FIELD "User-Agent" |
39 | | /// Accept field |
40 | | #define PCPP_SIP_ACCEPT_FIELD "Accept" |
41 | | /// Accept-Encoding field |
42 | | #define PCPP_SIP_ACCEPT_ENCODING_FIELD "Accept-Encoding" |
43 | | /// Accept-Language field |
44 | | #define PCPP_SIP_ACCEPT_LANGUAGE_FIELD "Accept-Language" |
45 | | /// Allow field |
46 | | #define PCPP_SIP_ALLOW_FIELD "Allow" |
47 | | /// Authorization field |
48 | | #define PCPP_SIP_AUTHORIZATION_FIELD "Authorization" |
49 | | /// Date field |
50 | | #define PCPP_SIP_DATE_FIELD "Date" |
51 | | /// MIME-Version field |
52 | | #define PCPP_SIP_MIME_VERSION_FIELD "MIME-Version" |
53 | | /// Reason field |
54 | | #define PCPP_SIP_REASON_FIELD "Reason" |
55 | | /// Supported field |
56 | | #define PCPP_SIP_SUPPORTED_FIELD "Supported" |
57 | | /// Server field |
58 | | #define PCPP_SIP_SERVER_FIELD "Server" |
59 | | /// WWW-Authenticate fild |
60 | | #define PCPP_SIP_WWW_AUTHENTICATE_FIELD "WWW-Authenticate" |
61 | | /// Retry-After field |
62 | | #define PCPP_SIP_RETRY_AFTER_FIELD "Retry-After" |
63 | | /// Record-Route field |
64 | | #define PCPP_SIP_RECORD_ROUTE_FIELD "Record-Route" |
65 | | |
66 | | /// @class SipLayer |
67 | | /// Represents a general SIP message. It's an abstract class and cannot be instantiated. It's inherited by |
68 | | /// SipRequestLayer and SipResponseLayer |
69 | | class SipLayer : public TextBasedProtocolMessage |
70 | | { |
71 | | public: |
72 | | /// The length of the body of many SIP response messages is determined by a SIP header field called |
73 | | /// "Content-Length". This method parses this field, extracts its value and return it. If this field doesn't |
74 | | /// exist 0 is returned |
75 | | /// @return SIP response body length determined by "Content-Length" field |
76 | | int getContentLength() const; |
77 | | |
78 | | /// The length of the body of many SIP messages is determined by a header field called "Content-Length". This |
79 | | /// method sets The content-length field value. The method supports several cases: |
80 | | /// - If the "Content-Length" field exists - the method will only replace the existing value with the new value |
81 | | /// - If the "Content-Length" field doesn't exist - the method will create this field and put the value in it. |
82 | | /// Here are also 2 cases: |
83 | | /// - If prevFieldName is specified - the new "Content-Length" field will be created after it |
84 | | /// - If prevFieldName isn't specified or doesn't exist - the new "Content-Length" field will be created as |
85 | | /// the |
86 | | /// last field before end-of-header field |
87 | | /// |
88 | | /// @param[in] contentLength The content length value to set |
89 | | /// @param[in] prevFieldName Optional parameter, if specified and "Content-Length" field doesn't exist, it will |
90 | | /// be created after this field |
91 | | /// @return A pointer to the "Content-Length" field, or nullptr if creation failed |
92 | | HeaderField* setContentLength(int contentLength, const std::string& prevFieldName = ""); |
93 | | |
94 | | // Overridden methods |
95 | | |
96 | | OsiModelLayer getOsiModelLayer() const override |
97 | 0 | { |
98 | 0 | return OsiModelSessionLayer; |
99 | 0 | } |
100 | | |
101 | | /// Currently identifies only SDP if content-length field exists and set to a value greater than zero. |
102 | | /// If content-length field doesn't exist or set to zero and still there is data after this layer, a |
103 | | /// PayloadLayer will be created |
104 | | void parseNextLayer() override; |
105 | | |
106 | | /// Set the content-length only if a content-length field already exists and if its current value is different |
107 | | /// than the total length of the next layer(s) |
108 | | void computeCalculateFields() override; |
109 | | |
110 | | /// A static method that checks whether the port is considered as SIP |
111 | | /// @param[in] port The port number to be checked |
112 | | static bool isSipPort(uint16_t port) |
113 | 0 | { |
114 | 0 | return port == 5060 || port == 5061; |
115 | 0 | } |
116 | | |
117 | | /// A static factory method that attempts to create a SIP layer from existing packet raw data |
118 | | /// The method first checks whether the source or destination port matches the SIP protocol. |
119 | | /// @param[in] data A pointer to the raw data |
120 | | /// @param[in] dataLen Size of the data in bytes |
121 | | /// @param[in] prevLayer A pointer to the previous layer |
122 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
123 | | /// @param[in] srcPort Source port number to check |
124 | | /// @param[in] dstPort Dest port number to check |
125 | | /// @return A newly allocated SIP layer of type request or response, or nullptr if parsing fails |
126 | | static SipLayer* parseSipLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, |
127 | | uint16_t srcPort, uint16_t dstPort); |
128 | | |
129 | | /// A static factory method that attempts to create a SIP layer from existing packet raw data |
130 | | /// This method does not check source or destination ports. Instead, it uses heuristics |
131 | | /// to determine whether the data represents a SIP request or response. |
132 | | /// @param[in] data A pointer to the raw data |
133 | | /// @param[in] dataLen Size of the data in bytes |
134 | | /// @param[in] prevLayer A pointer to the previous layer |
135 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
136 | | /// @return A newly allocated SIP layer of type request or response, or nullptr if parsing fails |
137 | | static SipLayer* parseSipLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); |
138 | | |
139 | | /// @struct SerializedFields |
140 | | /// @brief Fields shared by SipRequestLayer and SipResponseLayer. They're written after the first line fields, |
141 | | /// which use IDs Layer::SerializedFields::MaxID + 1 to Layer::SerializedFields::MaxID + 3. |
142 | | struct SerializedFields : Layer::SerializedFields |
143 | | { |
144 | | |
145 | | /// @struct HeaderFieldObject |
146 | | /// @brief Fields describing one element of the HeaderFields array of SipRequestLayer and SipResponseLayer |
147 | | struct HeaderFieldObject : ObjectFieldDescriptor<HeaderFieldObject> |
148 | | { |
149 | | using ObjectFieldDescriptor::ObjectFieldDescriptor; |
150 | | |
151 | | /// @return All field descriptors for one header field |
152 | | static std::vector<FieldDescriptor> all() |
153 | 0 | { |
154 | 0 | return { Name, Value }; |
155 | 0 | } |
156 | | |
157 | | /// @brief Header field name, as a string |
158 | | static const FieldDescriptor Name; |
159 | | |
160 | | /// @brief Header field value, as a string |
161 | | static const FieldDescriptor Value; |
162 | | }; |
163 | | |
164 | | /// @brief Field descriptor for whether the SIP header is complete (i.e. ends with an empty line) or is |
165 | | /// partial and continues in the next packet, written as a boolean. |
166 | | static const FieldDescriptor HeaderComplete; |
167 | | |
168 | | /// @brief Field descriptor for the array of SIP header fields, in the order they appear in the message. |
169 | | /// Elements are described by HeaderFieldObject. |
170 | | static const FieldDescriptor HeaderFields; |
171 | | }; |
172 | | |
173 | | protected: |
174 | | SipLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ProtocolType protocol) |
175 | 0 | : TextBasedProtocolMessage(data, dataLen, prevLayer, packet, protocol) |
176 | 0 | {} |
177 | 0 | SipLayer() : TextBasedProtocolMessage() |
178 | 0 | {} |
179 | 0 | SipLayer(const SipLayer& other) : TextBasedProtocolMessage(other) |
180 | 0 | {} |
181 | | SipLayer& operator=(const SipLayer& other) |
182 | 0 | { |
183 | 0 | TextBasedProtocolMessage::operator=(other); |
184 | 0 | return *this; |
185 | 0 | } |
186 | | |
187 | | // implementation of abstract methods |
188 | | char getHeaderFieldNameValueSeparator() const override |
189 | 0 | { |
190 | 0 | return ':'; |
191 | 0 | } |
192 | | bool spacesAllowedBetweenHeaderFieldNameAndValue() const override |
193 | 0 | { |
194 | 0 | return true; |
195 | 0 | } |
196 | | |
197 | | void serializeHeader(ObjectScope& serializer) const; |
198 | | |
199 | | private: |
200 | | enum class SipParseResult |
201 | | { |
202 | | Unknown = 0, |
203 | | Request = 1, |
204 | | Response = 2, |
205 | | }; |
206 | | |
207 | | static SipParseResult detectSipMessageType(const uint8_t* data, size_t dataLen); |
208 | | }; |
209 | | |
210 | | class SipRequestFirstLine; |
211 | | |
212 | | /// @class SipRequestLayer |
213 | | /// Represents a SIP request header and inherits all basic functionality of SipLayer and TextBasedProtocolMessage. |
214 | | /// The functionality that is added for this class is the SIP first line concept. A SIP request has the following |
215 | | /// first line: <i>INVITE sip:bla@bla.com:12345 SIP/2.0</i> Since it's not an "ordinary" header field, it requires a |
216 | | /// special treatment and gets a class of it's own: SipRequestFirstLine. In most cases a SIP request will be |
217 | | /// contained in a single packet but for cases it is not, only the first packet will be identified as SIP request |
218 | | /// layer. You can find out whether the header is complete by using SipLayer#isHeaderComplete() |
219 | | class SipRequestLayer : public SipLayer |
220 | | { |
221 | | friend class SipRequestFirstLine; |
222 | | |
223 | | public: |
224 | | /// SIP request methods |
225 | | enum SipMethod |
226 | | { |
227 | | /// INVITE |
228 | | SipINVITE, |
229 | | /// ACK |
230 | | SipACK, |
231 | | /// BYE |
232 | | SipBYE, |
233 | | /// CANCEL |
234 | | SipCANCEL, |
235 | | /// REFISTER |
236 | | SipREGISTER, |
237 | | /// PRACK |
238 | | SipPRACK, |
239 | | /// OPTIONS |
240 | | SipOPTIONS, |
241 | | /// SUBSCRIBE |
242 | | SipSUBSCRIBE, |
243 | | /// NOTIFY |
244 | | SipNOTIFY, |
245 | | /// PUBLISH |
246 | | SipPUBLISH, |
247 | | /// INFO |
248 | | SipINFO, |
249 | | /// REFER |
250 | | SipREFER, |
251 | | /// MESSAGE |
252 | | SipMESSAGE, |
253 | | /// UPDATE |
254 | | SipUPDATE, |
255 | | /// Unknown SIP method |
256 | | SipMethodUnknown |
257 | | }; |
258 | | |
259 | | /// A constructor that creates the layer from an existing packet raw data |
260 | | /// @param[in] data A pointer to the raw data |
261 | | /// @param[in] dataLen Size of the data in bytes |
262 | | /// @param[in] prevLayer A pointer to the previous layer |
263 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
264 | | SipRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); |
265 | | |
266 | | /// A constructor that allocates a new SIP request with only the first line filled. The request will be created |
267 | | /// without further fields. The user can then add fields using addField() or insertField() methods |
268 | | /// @param[in] method The SIP method to be used in this SIP request |
269 | | /// @param[in] requestUri The URI of the request |
270 | | /// @param[in] version SIP version to be used in this request. Default is "SIP/2.0" |
271 | | SipRequestLayer(SipMethod method, const std::string& requestUri, const std::string& version = "SIP/2.0"); |
272 | | |
273 | | ~SipRequestLayer() override; |
274 | | |
275 | | /// A copy constructor for this layer. Inherits base copy constructor SipLayer and adds the functionality |
276 | | /// of copying the first line |
277 | | /// @param[in] other The instance to copy from |
278 | | SipRequestLayer(const SipRequestLayer& other); |
279 | | |
280 | | /// An assignment operator overload for this layer. This method inherits base assignment operator |
281 | | /// SipLayer#operator=() and adds the functionality of copying the first line |
282 | | /// @param[in] other The instance to copy from |
283 | | SipRequestLayer& operator=(const SipRequestLayer& other); |
284 | | |
285 | | /// @return A pointer to the first line instance for this message |
286 | | SipRequestFirstLine* getFirstLine() const |
287 | 0 | { |
288 | 0 | return m_FirstLine; |
289 | 0 | } |
290 | | |
291 | | // implement Layer's abstract methods |
292 | | |
293 | | std::string toString() const override; |
294 | | |
295 | | /// @struct SerializedFields |
296 | | /// @brief Fields written by SipRequestLayer's serializeLayer(), in addition to |
297 | | /// SipLayer::SerializedFields. |
298 | | struct SerializedFields : SipLayer::SerializedFields |
299 | | { |
300 | | /// @return All field descriptors for SipRequestLayer |
301 | | static std::vector<FieldDescriptor> all() |
302 | 0 | { |
303 | 0 | auto result = SipLayer::SerializedFields::all(); |
304 | 0 | std::initializer_list<FieldDescriptor> extra{ Method, Uri, Version, HeaderComplete, HeaderFields }; |
305 | 0 | std::copy(extra.begin(), extra.end(), std::back_inserter(result)); |
306 | 0 | return result; |
307 | 0 | } |
308 | | |
309 | | /// @brief Field descriptor for the SIP method, written as a string (for example "INVITE"), |
310 | | /// or "Unknown" if the method isn't recognized. |
311 | | static const FieldDescriptor Method; |
312 | | |
313 | | /// @brief Field descriptor for the request URI, written as a string. |
314 | | static const FieldDescriptor Uri; |
315 | | |
316 | | /// @brief Field descriptor for the SIP version, written as a string (for example "SIP/2.0"), or |
317 | | /// "" if the version isn't recognized. |
318 | | static const FieldDescriptor Version; |
319 | | }; |
320 | | |
321 | | protected: |
322 | | void serializeLayer(ObjectScope& serializer) const override; |
323 | | |
324 | | private: |
325 | | SipRequestFirstLine* m_FirstLine; |
326 | | }; |
327 | | |
328 | | class SipResponseFirstLine; |
329 | | |
330 | | /// @class SipResponseLayer |
331 | | /// Represents an SIP response message and inherits all basic functionality of SipLayer and |
332 | | /// TextBasedProtocolMessage. The functionality that is added for this class is the SIP first line concept. A SIP |
333 | | /// response has the following first line: <i>200 OK SIP/2.0</i> Since it's not an "ordinary" header field, it |
334 | | /// requires a special treatment and gets a class of it's own: SipResponseFirstLine. In most cases a SIP response |
335 | | /// will be contained in a single packet but for cases it is not, only the first packet will be identified as SIP |
336 | | /// response layer. You can find out whether the header is complete by using SipLayer#isHeaderComplete() |
337 | | class SipResponseLayer : public SipLayer |
338 | | { |
339 | | friend class SipResponseFirstLine; |
340 | | |
341 | | public: |
342 | | /// Enum for SIP response status codes. List is taken from Wikipedia: |
343 | | /// https://en.wikipedia.org/wiki/List_of_SIP_response_codes |
344 | | enum SipResponseStatusCode |
345 | | { |
346 | | /// Extended search being performed may take a significant time so a forking proxy must send a 100 Trying |
347 | | /// response |
348 | | Sip100Trying, |
349 | | /// Destination user agent received INVITE, and is alerting user of call |
350 | | Sip180Ringing, |
351 | | /// Servers can optionally send this response to indicate a call is being forwarded |
352 | | Sip181CallisBeingForwarded, |
353 | | /// Indicates that the destination was temporarily unavailable, so the server has queued the call until the |
354 | | /// destination is available. A server may send multiple 182 responses to update progress of the queue |
355 | | Sip182Queued, |
356 | | /// This response may be used to send extra information for a call which is still being set up |
357 | | Sip183SessioninProgress, |
358 | | /// Can be used by User Agent Server to indicate to upstream SIP entities (including the User Agent Client |
359 | | /// (UAC)) that an early dialog has been terminated |
360 | | Sip199EarlyDialogTerminated, |
361 | | /// Indicates the request was successful |
362 | | Sip200OK, |
363 | | /// Indicates that the request has been accepted for processing, but the processing has not been completed |
364 | | Sip202Accepted, |
365 | | /// Indicates the request was successful, but the corresponding response will not be received |
366 | | Sip204NoNotification, |
367 | | /// The address resolved to one of several options for the user or client to choose between, which are |
368 | | /// listed in the message body or the message's Contact fields |
369 | | Sip300MultipleChoices, |
370 | | /// The original Request-URI is no longer valid, the new address is given in the Contact header field, and |
371 | | /// the client should update any records of the original Request-URI with the new value |
372 | | Sip301MovedPermanently, |
373 | | /// The client should try at the address in the Contact field. If an Expires field is present, the client |
374 | | /// may cache the result for that period of time |
375 | | Sip302MovedTemporarily, |
376 | | /// The Contact field details a proxy that must be used to access the requested destination |
377 | | Sip305UseProxy, |
378 | | /// The call failed, but alternatives are detailed in the message body |
379 | | Sip380AlternativeService, |
380 | | /// The request could not be understood due to malformed syntax |
381 | | Sip400BadRequest, |
382 | | /// The request requires user authentication. This response is issued by UASs and registrars |
383 | | Sip401Unauthorized, |
384 | | /// Reserved for future use |
385 | | Sip402PaymentRequired, |
386 | | /// The server understood the request, but is refusing to fulfill it |
387 | | Sip403Forbidden, |
388 | | /// The server has definitive information that the user does not exist at the domain specified in the |
389 | | /// Request-URI. This status is also returned if the domain in the Request-URI does not match any of the |
390 | | /// domains handled by the recipient of the request |
391 | | Sip404NotFound, |
392 | | /// The method specified in the Request-Line is understood, but not allowed for the address identified by |
393 | | /// the Request-URI |
394 | | Sip405MethodNotAllowed, |
395 | | /// The resource identified by the request is only capable of generating response entities that have content |
396 | | /// characteristics but not acceptable according to the Accept header field sent in the request |
397 | | Sip406NotAcceptable, |
398 | | /// The request requires user authentication. This response is issued by proxies |
399 | | Sip407ProxyAuthenticationRequired, |
400 | | /// Couldn't find the user in time. The server could not produce a response within a suitable amount of |
401 | | /// time, for example, if it could not determine the location of the user in time. The client MAY repeat the |
402 | | /// request without modifications at any later time |
403 | | Sip408RequestTimeout, |
404 | | /// User already registered |
405 | | Sip409Conflict, |
406 | | /// The user existed once, but is not available here any more |
407 | | Sip410Gone, |
408 | | /// The server will not accept the request without a valid Content-Length |
409 | | Sip411LengthRequired, |
410 | | /// The given precondition has not been met |
411 | | Sip412ConditionalRequestFailed, |
412 | | /// Request body too large |
413 | | Sip413RequestEntityTooLarge, |
414 | | /// The server is refusing to service the request because the Request-URI is longer than the server is |
415 | | /// willing to interpret |
416 | | Sip414RequestURITooLong, |
417 | | /// Request body in a format not supported |
418 | | Sip415UnsupportedMediaType, |
419 | | /// Request-URI is unknown to the server |
420 | | Sip416UnsupportedURIScheme, |
421 | | /// There was a resource-priority option tag, but no Resource-Priority header |
422 | | Sip417UnknownResourcePriority, |
423 | | /// Bad SIP Protocol Extension used, not understood by the server |
424 | | Sip420BadExtension, |
425 | | /// The server needs a specific extension not listed in the Supported header |
426 | | Sip421ExtensionRequired, |
427 | | /// The received request contains a Session-Expires header field with a duration below the minimum timer |
428 | | Sip422SessionIntervalTooSmall, |
429 | | /// Expiration time of the resource is too short |
430 | | Sip423IntervalTooBrief, |
431 | | /// The request's location content was malformed or otherwise unsatisfactory |
432 | | Sip424BadLocationInformation, |
433 | | /// The server rejected a non-interactive emergency call, indicating that the request was malformed enough |
434 | | /// that no reasonable emergency response to the alert can be determined |
435 | | Sip425BadAlertMessage, |
436 | | /// The server policy requires an Identity header, and one has not been provided |
437 | | Sip428UseIdentityHeader, |
438 | | /// The server did not receive a valid Referred-By token on the request |
439 | | Sip429ProvideReferrerIdentity, |
440 | | /// A specific flow to a user agent has failed, although other flows may succeed. This response is intended |
441 | | /// for use between proxy devices, and should not be seen by an endpoint (and if it is seen by one, should |
442 | | /// be treated as a 400 Bad Request response) |
443 | | Sip430FlowFailed, |
444 | | /// The request has been rejected because it was anonymous |
445 | | Sip433AnonymityDisallowed, |
446 | | /// The request has an Identity-Info header, and the URI scheme in that header cannot be dereferenced |
447 | | Sip436BadIdentityInfo, |
448 | | /// The server was unable to validate a certificate for the domain that signed the request |
449 | | Sip437UnsupportedCertificate, |
450 | | /// The server obtained a valid certificate that the request claimed was used to sign the request, but was |
451 | | /// unable to verify that signature |
452 | | Sip438InvalidIdentityHeader, |
453 | | /// The first outbound proxy the user is attempting to register through does not support the "outbound" |
454 | | /// feature of RFC 5626, although the registrar does |
455 | | Sip439FirstHopLacksOutboundSupport, |
456 | | /// If a SIP proxy determines a response context has insufficient Incoming Max-Breadth to carry out a |
457 | | /// desired parallel fork, and the proxy is unwilling/unable to compensate by forking serially or sending a |
458 | | /// redirect, that proxy MUST return a 440 response. A client receiving a 440 response can infer that its |
459 | | /// request did not reach all possible destinations |
460 | | Sip440MaxBreadthExceeded, |
461 | | /// If a SIP UA receives an INFO request associated with an Info Package that the UA has not indicated |
462 | | /// willingness to receive, the UA MUST send a 469 response, which contains a Recv-Info header field with |
463 | | /// Info Packages for which the UA is willing to receive INFO requests |
464 | | Sip469BadInfoPackage, |
465 | | /// The source of the request did not have the permission of the recipient to make such a request |
466 | | Sip470ConsentNeeded, |
467 | | /// Callee currently unavailable |
468 | | Sip480TemporarilyUnavailable, |
469 | | /// Server received a request that does not match any dialog or transaction |
470 | | Sip481Call_TransactionDoesNotExist, |
471 | | /// Server has detected a loop |
472 | | Sip482LoopDetected, |
473 | | /// Max-Forwards header has reached the value '0' |
474 | | Sip483TooManyHops, |
475 | | /// Request-URI incomplete |
476 | | Sip484AddressIncomplete, |
477 | | /// Request-URI is ambiguous |
478 | | Sip485Ambiguous, |
479 | | /// Callee is busy |
480 | | Sip486BusyHere, |
481 | | /// Request has terminated by bye or cancel |
482 | | Sip487RequestTerminated, |
483 | | /// Some aspect of the session description or the Request-URI is not acceptable |
484 | | Sip488NotAcceptableHere, |
485 | | /// The server did not understand an event package specified in an Event header field |
486 | | Sip489BadEvent, |
487 | | /// Server has some pending request from the same dialog |
488 | | Sip491RequestPending, |
489 | | /// Request contains an encrypted MIME body, which recipient can not decrypt |
490 | | Sip493Undecipherable, |
491 | | /// The server has received a request that requires a negotiated security mechanism, and the response |
492 | | /// contains a list of suitable security mechanisms for the requester to choose between, or a digest |
493 | | /// authentication challenge |
494 | | Sip494SecurityAgreementRequired, |
495 | | /// The server could not fulfill the request due to some unexpected condition |
496 | | Sip500ServerInternalError, |
497 | | /// The server does not have the ability to fulfill the request, such as because it does not recognize the |
498 | | /// request method. (Compare with 405 Method Not Allowed, where the server recognizes the method but does |
499 | | /// not allow or support it.) |
500 | | Sip501NotImplemented, |
501 | | /// The server is acting as a gateway or proxy, and received an invalid response from a downstream server |
502 | | /// while attempting to fulfill the request |
503 | | Sip502BadGateway, |
504 | | /// The server is undergoing maintenance or is temporarily overloaded and so cannot process the request. A |
505 | | /// "Retry-After" header field may specify when the client may reattempt its request |
506 | | Sip503ServiceUnavailable, |
507 | | /// The server attempted to access another server in attempting to process the request, and did not receive |
508 | | /// a prompt response |
509 | | Sip504ServerTimeout, |
510 | | /// The SIP protocol version in the request is not supported by the server |
511 | | Sip505VersionNotSupported, |
512 | | /// The request message length is longer than the server can process |
513 | | Sip513MessageTooLarge, |
514 | | /// The server does not support the push notification service identified in a 'pn-provider' SIP URI |
515 | | /// parameter |
516 | | Sip555PushNotificationServiceNotSupported, |
517 | | /// The server is unable or unwilling to meet some constraints specified in the offer |
518 | | Sip580PreconditionFailure, |
519 | | /// All possible destinations are busy. Unlike the 486 response, this response indicates the destination |
520 | | /// knows there are no alternative destinations (such as a voicemail server) able to accept the call |
521 | | Sip600BusyEverywhere, |
522 | | /// The destination does not wish to participate in the call, or cannot do so, and additionally the |
523 | | /// destination knows there are no alternative destinations (such as a voicemail server) willing to accept |
524 | | /// the call |
525 | | Sip603Decline, |
526 | | /// The server has authoritative information that the requested user does not exist anywhere |
527 | | Sip604DoesNotExistAnywhere, |
528 | | /// The user's agent was contacted successfully but some aspects of the session description such as the |
529 | | /// requested media, bandwidth, or addressing style were not acceptable |
530 | | Sip606NotAcceptable, |
531 | | /// The called party did not want this call from the calling party. Future attempts from the calling party |
532 | | /// are likely to be similarly rejected |
533 | | Sip607Unwanted, |
534 | | /// An intermediary machine or process rejected the call attempt |
535 | | Sip608Rejected, |
536 | | /// Unknown SIP status code |
537 | | SipStatusCodeUnknown |
538 | | }; |
539 | | |
540 | | /// A constructor that creates the layer from an existing packet raw data |
541 | | /// @param[in] data A pointer to the raw data |
542 | | /// @param[in] dataLen Size of the data in bytes |
543 | | /// @param[in] prevLayer A pointer to the previous layer |
544 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
545 | | SipResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); |
546 | | |
547 | | /// A constructor that allocates a new SIP response with only the first line filled. The request will be created |
548 | | /// without further fields. The user can then add fields using addField() or insertField() methods |
549 | | /// @param[in] statusCode SIP status code to set |
550 | | /// @param[in] statusCodeString Most status codes have their default string, e.g 200 is usually "OK" etc. |
551 | | /// But the user can set a non-default status code string and it will be written in the header first line. Empty |
552 | | /// string ("") means using the default status code string. Also, the default is using the default status code |
553 | | /// string |
554 | | /// @param[in] sipVersion SIP version to set, default is SIP/2.0 |
555 | | explicit SipResponseLayer(SipResponseLayer::SipResponseStatusCode statusCode, std::string statusCodeString = "", |
556 | | const std::string& sipVersion = "SIP/2.0"); |
557 | | |
558 | | ~SipResponseLayer() override; |
559 | | |
560 | | /// A copy constructor for this layer. This copy constructor inherits base copy constructor SipLayer and adds |
561 | | /// the functionality of copying the first line as well |
562 | | /// @param[in] other The instance to copy from |
563 | | SipResponseLayer(const SipResponseLayer& other); |
564 | | |
565 | | /// An assignment operator overload for this layer. This method inherits base assignment operator |
566 | | /// SipLayer#operator=() and adds the functionality of copying the first line as well |
567 | | /// @param[in] other The instance to copy from |
568 | | SipResponseLayer& operator=(const SipResponseLayer& other); |
569 | | |
570 | | /// @return A pointer to the first line instance for this message |
571 | | SipResponseFirstLine* getFirstLine() const |
572 | 0 | { |
573 | 0 | return m_FirstLine; |
574 | 0 | } |
575 | | |
576 | | // implement Layer's abstract methods |
577 | | |
578 | | std::string toString() const override; |
579 | | |
580 | | /// @struct SerializedFields |
581 | | /// @brief Fields written by SipResponseLayer's serializeLayer(), in addition to |
582 | | /// SipLayer::SerializedFields. |
583 | | struct SerializedFields : SipLayer::SerializedFields |
584 | | { |
585 | | /// @return All field descriptors for SipResponseLayer |
586 | | static std::vector<FieldDescriptor> all() |
587 | 0 | { |
588 | 0 | auto result = SipLayer::SerializedFields::all(); |
589 | 0 | std::initializer_list<FieldDescriptor> extra{ Version, StatusCode, StatusCodeDescription, |
590 | 0 | HeaderComplete, HeaderFields }; |
591 | 0 | std::copy(extra.begin(), extra.end(), std::back_inserter(result)); |
592 | 0 | return result; |
593 | 0 | } |
594 | | |
595 | | /// @brief SIP version as a string (for example "SIP/2.0"), |
596 | | /// or an empty string if parsing fails. |
597 | | static const FieldDescriptor Version; |
598 | | |
599 | | /// @brief Field descriptor for the SIP status code, written as a number (for example 200) |
600 | | /// or -1 if the status code is SipStatusCodeUnknown |
601 | | static const FieldDescriptor StatusCode; |
602 | | |
603 | | /// @brief Field descriptor for the SIP reason phrase (eg. "OK"), written as a string. |
604 | | static const FieldDescriptor StatusCodeDescription; |
605 | | }; |
606 | | |
607 | | protected: |
608 | | void serializeLayer(ObjectScope& serializer) const override; |
609 | | |
610 | | private: |
611 | | SipResponseFirstLine* m_FirstLine; |
612 | | }; |
613 | | |
614 | | /// @class SipRequestFirstLine |
615 | | /// Represents an SIP request first line. The first line includes 3 parameters: SIP method (e.g INVITE, ACK, BYE, |
616 | | /// etc.), URI (e.g sip:bla@bla.com:12345) and SIP version (usually SIP/2.0). All these parameters are included in |
617 | | /// this class, and the user can retrieve or set them. This class cannot be instantiated by users, it's created |
618 | | /// inside SipRequestLayer and user can get a pointer to an instance of it. All "getters" of this class retrieve the |
619 | | /// actual data of the SIP request and the "setters" actually change the packet data. Since SIP is a textual |
620 | | /// protocol, most fields aren't of fixed size and this also applies to the first line parameters. So many "setter" |
621 | | /// methods of this class may need to shorten or extend the data in SipRequestLayer. These methods will return a |
622 | | /// false value if this action failed |
623 | | class SipRequestFirstLine |
624 | | { |
625 | | friend class SipRequestLayer; |
626 | | |
627 | | public: |
628 | | /// A structure containing parsed components from a SIP request first line. |
629 | | /// All string fields are empty if parsing fails |
630 | | struct SipFirstLineData |
631 | | { |
632 | | std::string method; ///< The SIP method (e.g., INVITE, REGISTER, BYE) |
633 | | std::string uri; ///< The Request-URI destination |
634 | | std::string version; ///< The SIP protocol version (e.g., SIP/2.0) |
635 | | }; |
636 | | |
637 | | /// @return The SIP request method |
638 | | SipRequestLayer::SipMethod getMethod() const |
639 | 0 | { |
640 | 0 | return m_Method; |
641 | 0 | } |
642 | | |
643 | | /// Set the SIP request method |
644 | | /// @param[in] newMethod The method to set |
645 | | /// @return False if newMethod is SipRequestLayer#SipMethodUnknown or if shortening/extending the |
646 | | /// SipRequestLayer data failed. True otherwise |
647 | | bool setMethod(SipRequestLayer::SipMethod newMethod); |
648 | | |
649 | | /// @return A copied version of the URI (notice changing the return value won't change the actual data of the |
650 | | /// packet) |
651 | | std::string getUri() const; |
652 | | |
653 | | /// Set the URI |
654 | | /// @param[in] newUri The URI to set |
655 | | /// @return False if shortening/extending the SipRequestLayer data failed. True otherwise |
656 | | bool setUri(const std::string& newUri); |
657 | | |
658 | | /// @return The SIP version |
659 | | std::string getVersion() const |
660 | 0 | { |
661 | 0 | return m_Version; |
662 | 0 | } |
663 | | |
664 | | /// A static method for parsing the SIP method out of raw data |
665 | | /// @param[in] data The raw data |
666 | | /// @param[in] dataLen The raw data length |
667 | | /// @return The parsed SIP method |
668 | | static SipRequestLayer::SipMethod parseMethod(const char* data, size_t dataLen); |
669 | | |
670 | | /// A static method for parsing the complete SIP request first line from raw data |
671 | | /// @param[in] data The raw data containing the SIP request line |
672 | | /// @param[in] dataLen The raw data length |
673 | | /// @return A pair where first indicates success/failure, and second contains the parsed data. |
674 | | /// If parsing fails, first is false and second contains empty strings |
675 | | static std::pair<bool, SipFirstLineData> parseFirstLine(const char* data, size_t dataLen); |
676 | | |
677 | | /// @return The size in bytes of the SIP request first line |
678 | | int getSize() const |
679 | 0 | { |
680 | 0 | return m_FirstLineEndOffset; |
681 | 0 | } |
682 | | |
683 | | /// As explained in SipRequestLayer, a SIP message can sometimes spread over more than 1 packet, so when looking |
684 | | /// at a single packet the header can be partial. Same goes for the first line - it can spread over more than 1 |
685 | | /// packet. This method returns an indication whether the first line is partial |
686 | | /// @return False if the first line is partial, true if it's complete |
687 | | bool isComplete() const |
688 | 0 | { |
689 | 0 | return m_IsComplete; |
690 | 0 | } |
691 | | |
692 | | /// @class SipRequestFirstLineException |
693 | | /// This exception can be thrown while constructing SipRequestFirstLine (the constructor is private, so the |
694 | | /// construction happens only in SipRequestLayer). This kind of exception is thrown if trying to construct with |
695 | | /// SIP method of SipRequestLayer#SipMethodUnknown or with empty SIP version |
696 | | class SipRequestFirstLineException : public std::exception |
697 | | { |
698 | | public: |
699 | | ~SipRequestFirstLineException() noexcept |
700 | 0 | {} |
701 | | void setMessage(const std::string& message) |
702 | 0 | { |
703 | 0 | m_Message = message; |
704 | 0 | } |
705 | | virtual const char* what() const noexcept |
706 | 0 | { |
707 | 0 | return m_Message.c_str(); |
708 | 0 | } |
709 | | |
710 | | private: |
711 | | std::string m_Message; |
712 | | }; |
713 | | |
714 | | private: |
715 | | explicit SipRequestFirstLine(SipRequestLayer* sipRequest); |
716 | | SipRequestFirstLine(SipRequestLayer* sipRequest, SipRequestLayer::SipMethod method, const std::string& version, |
717 | | const std::string& uri); |
718 | | |
719 | | void parseVersion(); |
720 | | |
721 | | SipRequestLayer* m_SipRequest; |
722 | | SipRequestLayer::SipMethod m_Method; |
723 | | std::string m_Version; |
724 | | int m_VersionOffset; |
725 | | int m_UriOffset; |
726 | | int m_FirstLineEndOffset; |
727 | | bool m_IsComplete; |
728 | | SipRequestFirstLineException m_Exception; |
729 | | }; |
730 | | |
731 | | /// @class SipResponseFirstLine |
732 | | /// Represents an SIP response message first line. The first line includes 2 parameters: status code (e.g 100 Trying |
733 | | /// ,200 OK, etc.), and SIP version (usually SIP/2.0). These 2 parameters are included in this class, and the user |
734 | | /// can retrieve or set them. This class cannot be instantiated by users, it's created inside SipResponseLayer and |
735 | | /// user can get a pointer to an instance of it. The "getter" methods of this class will retrieve the actual data of |
736 | | /// the SIP response and the "setter" methods will change the packet data. Since SIP is a textual protocol, most |
737 | | /// fields aren't of fixed size and this also applies to the first line parameters. So most "setter" methods of this |
738 | | /// class may need to shorten or extend the data in SipResponseLayer. These methods will return a false value if |
739 | | /// this action failed |
740 | | class SipResponseFirstLine |
741 | | { |
742 | | friend class SipResponseLayer; |
743 | | |
744 | | public: |
745 | | /// @brief A structure containing parsed components from a SIP response first line. |
746 | | struct FirstLineData |
747 | | { |
748 | | /// @brief The SIP protocol version (e.g., SIP/2.0) |
749 | | std::string version; |
750 | | /// @brief The response status code number (e.g., 200, 100) |
751 | | SipResponseLayer::SipResponseStatusCode statusCode = SipResponseLayer::SipStatusCodeUnknown; |
752 | | }; |
753 | | |
754 | | /// @return The status code as SipResponseLayer#SipResponseStatusCode enum |
755 | | SipResponseLayer::SipResponseStatusCode getStatusCode() const |
756 | 0 | { |
757 | 0 | return m_StatusCode; |
758 | 0 | } |
759 | | |
760 | | /// @return The status code number as integer (e.g 200, 100, etc.), |
761 | | /// or -1 if the status code is SipStatusCodeUnknown |
762 | | int getStatusCodeAsInt() const; |
763 | | |
764 | | /// @return The status code message (e.g "OK", "Trying", etc.) |
765 | | std::string getStatusCodeString() const; |
766 | | |
767 | | /// Set the status code |
768 | | /// @param[in] newStatusCode The new status code to set |
769 | | /// @param[in] statusCodeString An optional parameter: set a non-default status code message (e.g "Bla Bla" |
770 | | /// instead of "Not Found"). If this parameter isn't supplied or supplied as empty string (""), the default |
771 | | /// message for the status code will be set |
772 | | bool setStatusCode(SipResponseLayer::SipResponseStatusCode newStatusCode, std::string statusCodeString = ""); |
773 | | |
774 | | /// @return The SIP version |
775 | | std::string getVersion() const |
776 | 0 | { |
777 | 0 | return m_Version; |
778 | 0 | } |
779 | | |
780 | | /// Set the SIP version. The version to set is expected to be in the format of SIP/x.y otherwise an error will |
781 | | /// be written to log |
782 | | /// @param[in] newVersion The SIP version to set |
783 | | void setVersion(const std::string& newVersion); |
784 | | |
785 | | /// A static method for parsing the SIP status code out of raw data |
786 | | /// @param[in] data The raw data |
787 | | /// @param[in] dataLen The raw data length |
788 | | /// @return The parsed SIP status code as enum |
789 | | static SipResponseLayer::SipResponseStatusCode parseStatusCode(const char* data, size_t dataLen); |
790 | | |
791 | | /// A static method for parsing the SIP version out of raw data |
792 | | /// @param[in] data The raw data |
793 | | /// @param[in] dataLen The raw data length |
794 | | /// @return The parsed SIP version string or an empty string if version cannot be extracted |
795 | | static std::string parseVersion(const char* data, size_t dataLen); |
796 | | |
797 | | /// @brief A static method for parsing the complete SIP response first line from raw data |
798 | | /// @param data The raw data containing the SIP response line |
799 | | /// @param dataLen The raw data length |
800 | | /// @return A pair where first indicates success/failure, and second contains the parsed data. |
801 | | static std::pair<bool, FirstLineData> parseFirstLine(const char* data, size_t dataLen); |
802 | | |
803 | | /// @return The size in bytes of the SIP response first line |
804 | | int getSize() const |
805 | 0 | { |
806 | 0 | return m_FirstLineEndOffset; |
807 | 0 | } |
808 | | |
809 | | /// As explained in SipResponseLayer, A SIP message can sometimes spread over more than 1 packet, so when |
810 | | /// looking at a single packet the header can be partial. Same goes for the first line - it can spread over more |
811 | | /// than 1 packet. This method returns an indication whether the first line is partial |
812 | | /// @return False if the first line is partial, true if it's complete |
813 | | bool isComplete() const |
814 | 0 | { |
815 | 0 | return m_IsComplete; |
816 | 0 | } |
817 | | |
818 | | /// @class SipResponseFirstLineException |
819 | | /// This exception can be thrown while constructing SipResponseFirstLine (the constructor is private, so the |
820 | | /// construction happens only in SipResponseLayer). This kind of exception will be thrown if trying to construct |
821 | | /// with SIP status code of SipResponseLayer#SipStatusCodeUnknown or with an empty SIP version |
822 | | class SipResponseFirstLineException : public std::exception |
823 | | { |
824 | | public: |
825 | | ~SipResponseFirstLineException() noexcept |
826 | 0 | {} |
827 | | void setMessage(const std::string& message) |
828 | 0 | { |
829 | 0 | m_Message = message; |
830 | 0 | } |
831 | | virtual const char* what() const noexcept |
832 | 0 | { |
833 | 0 | return m_Message.c_str(); |
834 | 0 | } |
835 | | |
836 | | private: |
837 | | std::string m_Message; |
838 | | }; |
839 | | |
840 | | private: |
841 | | explicit SipResponseFirstLine(SipResponseLayer* sipResponse); |
842 | | SipResponseFirstLine(SipResponseLayer* sipResponse, const std::string& version, |
843 | | SipResponseLayer::SipResponseStatusCode statusCode, std::string statusCodeString = ""); |
844 | | |
845 | | SipResponseLayer* m_SipResponse; |
846 | | std::string m_Version; |
847 | | SipResponseLayer::SipResponseStatusCode m_StatusCode; |
848 | | int m_FirstLineEndOffset; |
849 | | bool m_IsComplete; |
850 | | SipResponseFirstLineException m_Exception; |
851 | | }; |
852 | | |
853 | | } // namespace pcpp |