PcapPlusPlus  Next
SipLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "TextBasedProtocol.h"
4 
6 
9 namespace pcpp
10 {
11 // some popular SIP header fields
12 
14 #define PCPP_SIP_FROM_FIELD "From"
16 #define PCPP_SIP_TO_FIELD "To"
18 #define PCPP_SIP_VIA_FIELD "Via"
20 #define PCPP_SIP_CALL_ID_FIELD "Call-ID"
22 #define PCPP_SIP_CONTENT_TYPE_FIELD "Content-Type"
24 #define PCPP_SIP_CONTENT_LENGTH_FIELD "Content-Length"
26 #define PCPP_SIP_CONTENT_DISPOSITION_FIELD "Content-Disposition"
28 #define PCPP_SIP_CONTENT_ENCODING_FIELD "Content-Encoding"
30 #define PCPP_SIP_CONTENT_LANGUAGE_FIELD "Content-Language"
32 #define PCPP_SIP_CSEQ_FIELD "CSeq"
34 #define PCPP_SIP_CONTACT_FIELD "Contact"
36 #define PCPP_SIP_MAX_FORWARDS_FIELD "Max-Forwards"
38 #define PCPP_SIP_USER_AGENT_FIELD "User-Agent"
40 #define PCPP_SIP_ACCEPT_FIELD "Accept"
42 #define PCPP_SIP_ACCEPT_ENCODING_FIELD "Accept-Encoding"
44 #define PCPP_SIP_ACCEPT_LANGUAGE_FIELD "Accept-Language"
46 #define PCPP_SIP_ALLOW_FIELD "Allow"
48 #define PCPP_SIP_AUTHORIZATION_FIELD "Authorization"
50 #define PCPP_SIP_DATE_FIELD "Date"
52 #define PCPP_SIP_MIME_VERSION_FIELD "MIME-Version"
54 #define PCPP_SIP_REASON_FIELD "Reason"
56 #define PCPP_SIP_SUPPORTED_FIELD "Supported"
58 #define PCPP_SIP_SERVER_FIELD "Server"
60 #define PCPP_SIP_WWW_AUTHENTICATE_FIELD "WWW-Authenticate"
62 #define PCPP_SIP_RETRY_AFTER_FIELD "Retry-After"
64 #define PCPP_SIP_RECORD_ROUTE_FIELD "Record-Route"
65 
70  {
71  public:
76  int getContentLength() const;
77 
92  HeaderField* setContentLength(int contentLength, const std::string& prevFieldName = "");
93 
94  // Overridden methods
95 
97  {
98  return OsiModelSesionLayer;
99  }
100 
104  void parseNextLayer() override;
105 
108  void computeCalculateFields() override;
109 
112  static bool isSipPort(uint16_t port)
113  {
114  return port == 5060 || port == 5061;
115  }
116 
117  protected:
118  SipLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ProtocolType protocol)
119  : TextBasedProtocolMessage(data, dataLen, prevLayer, packet, protocol)
120  {}
121  SipLayer() : TextBasedProtocolMessage()
122  {}
123  SipLayer(const SipLayer& other) : TextBasedProtocolMessage(other)
124  {}
125  SipLayer& operator=(const SipLayer& other)
126  {
127  TextBasedProtocolMessage::operator=(other);
128  return *this;
129  }
130 
131  // implementation of abstract methods
132  char getHeaderFieldNameValueSeparator() const override
133  {
134  return ':';
135  }
136  bool spacesAllowedBetweenHeaderFieldNameAndValue() const override
137  {
138  return true;
139  }
140  };
141 
142  class SipRequestFirstLine;
143 
151  class SipRequestLayer : public SipLayer
152  {
153  friend class SipRequestFirstLine;
154 
155  public:
158  {
189  };
190 
196  SipRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
197 
203  SipRequestLayer(SipMethod method, const std::string& requestUri, const std::string& version = "SIP/2.0");
204 
205  ~SipRequestLayer() override;
206 
211 
216 
219  {
220  return m_FirstLine;
221  }
222 
223  // implement Layer's abstract methods
224 
225  std::string toString() const override;
226 
227  private:
228  SipRequestFirstLine* m_FirstLine;
229  };
230 
231  class SipResponseFirstLine;
232 
240  class SipResponseLayer : public SipLayer
241  {
242  friend class SipResponseFirstLine;
243 
244  public:
248  {
441  };
442 
448  SipResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
449 
458  explicit SipResponseLayer(SipResponseLayer::SipResponseStatusCode statusCode, std::string statusCodeString = "",
459  const std::string& sipVersion = "SIP/2.0");
460 
461  ~SipResponseLayer() override;
462 
467 
472 
475  {
476  return m_FirstLine;
477  }
478 
479  // implement Layer's abstract methods
480 
481  std::string toString() const override;
482 
483  private:
484  SipResponseFirstLine* m_FirstLine;
485  };
486 
497  {
498  friend class SipRequestLayer;
499 
500  public:
503  {
504  return m_Method;
505  }
506 
512 
515  std::string getUri() const;
516 
520  bool setUri(const std::string& newUri);
521 
523  std::string getVersion() const
524  {
525  return m_Version;
526  }
527 
532  static SipRequestLayer::SipMethod parseMethod(const char* data, size_t dataLen);
533 
535  int getSize() const
536  {
537  return m_FirstLineEndOffset;
538  }
539 
544  bool isComplete() const
545  {
546  return m_IsComplete;
547  }
548 
553  class SipRequestFirstLineException : public std::exception
554  {
555  public:
556  ~SipRequestFirstLineException() noexcept
557  {}
558  void setMessage(const std::string& message)
559  {
560  m_Message = message;
561  }
562  virtual const char* what() const noexcept
563  {
564  return m_Message.c_str();
565  }
566 
567  private:
568  std::string m_Message;
569  };
570 
571  private:
573  SipRequestFirstLine(SipRequestLayer* sipRequest, SipRequestLayer::SipMethod method, const std::string& version,
574  const std::string& uri);
575 
576  void parseVersion();
577 
578  SipRequestLayer* m_SipRequest;
580  std::string m_Version;
581  int m_VersionOffset;
582  int m_UriOffset;
583  int m_FirstLineEndOffset;
584  bool m_IsComplete;
585  SipRequestFirstLineException m_Exception;
586  };
587 
598  {
599  friend class SipResponseLayer;
600 
601  public:
604  {
605  return m_StatusCode;
606  }
607 
609  int getStatusCodeAsInt() const;
610 
612  std::string getStatusCodeString() const;
613 
619  bool setStatusCode(SipResponseLayer::SipResponseStatusCode newStatusCode, std::string statusCodeString = "");
620 
622  std::string getVersion() const
623  {
624  return m_Version;
625  }
626 
630  void setVersion(const std::string& newVersion);
631 
636  static SipResponseLayer::SipResponseStatusCode parseStatusCode(const char* data, size_t dataLen);
637 
642  static std::string parseVersion(const char* data, size_t dataLen);
643 
645  int getSize() const
646  {
647  return m_FirstLineEndOffset;
648  }
649 
654  bool isComplete() const
655  {
656  return m_IsComplete;
657  }
658 
663  class SipResponseFirstLineException : public std::exception
664  {
665  public:
667  {}
668  void setMessage(const std::string& message)
669  {
670  m_Message = message;
671  }
672  virtual const char* what() const noexcept
673  {
674  return m_Message.c_str();
675  }
676 
677  private:
678  std::string m_Message;
679  };
680 
681  private:
683  SipResponseFirstLine(SipResponseLayer* sipResponse, const std::string& version,
684  SipResponseLayer::SipResponseStatusCode statusCode, std::string statusCodeString = "");
685 
686  SipResponseLayer* m_SipResponse;
687  std::string m_Version;
689  int m_FirstLineEndOffset;
690  bool m_IsComplete;
691  SipResponseFirstLineException m_Exception;
692  };
693 
694 } // namespace pcpp
Definition: TextBasedProtocol.h:28
Definition: Layer.h:60
Definition: Packet.h:22
Definition: SipLayer.h:70
void computeCalculateFields() override
OsiModelLayer getOsiModelLayer() const override
Definition: SipLayer.h:96
HeaderField * setContentLength(int contentLength, const std::string &prevFieldName="")
static bool isSipPort(uint16_t port)
Definition: SipLayer.h:112
int getContentLength() const
void parseNextLayer() override
Definition: SipLayer.h:497
std::string getVersion() const
Definition: SipLayer.h:523
bool setUri(const std::string &newUri)
bool isComplete() const
Definition: SipLayer.h:544
std::string getUri() const
int getSize() const
Definition: SipLayer.h:535
bool setMethod(SipRequestLayer::SipMethod newMethod)
static SipRequestLayer::SipMethod parseMethod(const char *data, size_t dataLen)
SipRequestLayer::SipMethod getMethod() const
Definition: SipLayer.h:502
Definition: SipLayer.h:152
SipRequestLayer(const SipRequestLayer &other)
SipRequestLayer & operator=(const SipRequestLayer &other)
SipRequestFirstLine * getFirstLine() const
Definition: SipLayer.h:218
SipRequestLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
std::string toString() const override
SipRequestLayer(SipMethod method, const std::string &requestUri, const std::string &version="SIP/2.0")
SipMethod
SIP request methods.
Definition: SipLayer.h:158
@ SipMESSAGE
MESSAGE.
Definition: SipLayer.h:184
@ SipSUBSCRIBE
SUBSCRIBE.
Definition: SipLayer.h:174
@ SipINFO
INFO.
Definition: SipLayer.h:180
@ SipUPDATE
UPDATE.
Definition: SipLayer.h:186
@ SipPUBLISH
PUBLISH.
Definition: SipLayer.h:178
@ SipCANCEL
CANCEL.
Definition: SipLayer.h:166
@ SipINVITE
INVITE.
Definition: SipLayer.h:160
@ SipNOTIFY
NOTIFY.
Definition: SipLayer.h:176
@ SipPRACK
PRACK.
Definition: SipLayer.h:170
@ SipOPTIONS
OPTIONS.
Definition: SipLayer.h:172
@ SipREGISTER
REFISTER.
Definition: SipLayer.h:168
@ SipMethodUnknown
Unknown SIP method.
Definition: SipLayer.h:188
@ SipACK
ACK.
Definition: SipLayer.h:162
@ SipREFER
REFER.
Definition: SipLayer.h:182
@ SipBYE
BYE.
Definition: SipLayer.h:164
Definition: SipLayer.h:598
static SipResponseLayer::SipResponseStatusCode parseStatusCode(const char *data, size_t dataLen)
static std::string parseVersion(const char *data, size_t dataLen)
bool setStatusCode(SipResponseLayer::SipResponseStatusCode newStatusCode, std::string statusCodeString="")
SipResponseLayer::SipResponseStatusCode getStatusCode() const
Definition: SipLayer.h:603
void setVersion(const std::string &newVersion)
int getSize() const
Definition: SipLayer.h:645
bool isComplete() const
Definition: SipLayer.h:654
std::string getVersion() const
Definition: SipLayer.h:622
std::string getStatusCodeString() const
Definition: SipLayer.h:241
SipResponseStatusCode
Definition: SipLayer.h:248
@ Sip416UnsupportedURIScheme
Request-URI is unknown to the server.
Definition: SipLayer.h:323
@ Sip608Rejected
An intermediary machine or process rejected the call attempt.
Definition: SipLayer.h:438
@ Sip415UnsupportedMediaType
Request body in a format not supported.
Definition: SipLayer.h:321
@ Sip420BadExtension
Bad SIP Protocol Extension used, not understood by the server.
Definition: SipLayer.h:327
@ Sip428UseIdentityHeader
The server policy requires an Identity header, and one has not been provided.
Definition: SipLayer.h:340
@ Sip502BadGateway
Definition: SipLayer.h:406
@ Sip181CallisBeingForwarded
Servers can optionally send this response to indicate a call is being forwarded.
Definition: SipLayer.h:255
@ Sip503ServiceUnavailable
Definition: SipLayer.h:409
@ Sip183SessioninProgress
This response may be used to send extra information for a call which is still being set up.
Definition: SipLayer.h:260
@ Sip301MovedPermanently
Definition: SipLayer.h:275
@ Sip414RequestURITooLong
Definition: SipLayer.h:319
@ Sip555PushNotificationServiceNotSupported
Definition: SipLayer.h:419
@ Sip504ServerTimeout
Definition: SipLayer.h:412
@ Sip380AlternativeService
The call failed, but alternatives are detailed in the message body.
Definition: SipLayer.h:282
@ Sip600BusyEverywhere
Definition: SipLayer.h:424
@ Sip202Accepted
Indicates that the request has been accepted for processing, but the processing has not been complete...
Definition: SipLayer.h:267
@ Sip500ServerInternalError
The server could not fulfill the request due to some unexpected condition.
Definition: SipLayer.h:399
@ Sip493Undecipherable
Request contains an encrypted MIME body, which recipient can not decrypt.
Definition: SipLayer.h:393
@ Sip412ConditionalRequestFailed
The given precondition has not been met.
Definition: SipLayer.h:314
@ Sip402PaymentRequired
Reserved for future use.
Definition: SipLayer.h:288
@ Sip436BadIdentityInfo
The request has an Identity-Info header, and the URI scheme in that header cannot be dereferenced.
Definition: SipLayer.h:350
@ Sip422SessionIntervalTooSmall
The received request contains a Session-Expires header field with a duration below the minimum timer.
Definition: SipLayer.h:331
@ Sip406NotAcceptable
Definition: SipLayer.h:300
@ Sip180Ringing
Destination user agent received INVITE, and is alerting user of call.
Definition: SipLayer.h:253
@ Sip470ConsentNeeded
The source of the request did not have the permission of the recipient to make such a request.
Definition: SipLayer.h:369
@ Sip199EarlyDialogTerminated
Definition: SipLayer.h:263
@ Sip305UseProxy
The Contact field details a proxy that must be used to access the requested destination.
Definition: SipLayer.h:280
@ Sip604DoesNotExistAnywhere
The server has authoritative information that the requested user does not exist anywhere.
Definition: SipLayer.h:430
@ Sip424BadLocationInformation
The request's location content was malformed or otherwise unsatisfactory.
Definition: SipLayer.h:335
@ Sip400BadRequest
The request could not be understood due to malformed syntax.
Definition: SipLayer.h:284
@ Sip486BusyHere
Callee is busy.
Definition: SipLayer.h:383
@ Sip484AddressIncomplete
Request-URI incomplete.
Definition: SipLayer.h:379
@ Sip413RequestEntityTooLarge
Request body too large.
Definition: SipLayer.h:316
@ Sip433AnonymityDisallowed
The request has been rejected because it was anonymous.
Definition: SipLayer.h:348
@ Sip501NotImplemented
Definition: SipLayer.h:403
@ Sip491RequestPending
Server has some pending request from the same dialog.
Definition: SipLayer.h:391
@ Sip429ProvideReferrerIdentity
The server did not receive a valid Referred-By token on the request.
Definition: SipLayer.h:342
@ Sip408RequestTimeout
Definition: SipLayer.h:306
@ Sip603Decline
Definition: SipLayer.h:428
@ Sip440MaxBreadthExceeded
Definition: SipLayer.h:363
@ Sip439FirstHopLacksOutboundSupport
Definition: SipLayer.h:358
@ Sip480TemporarilyUnavailable
Callee currently unavailable.
Definition: SipLayer.h:371
@ Sip481Call_TransactionDoesNotExist
Server received a request that does not match any dialog or transaction.
Definition: SipLayer.h:373
@ Sip409Conflict
User already registered.
Definition: SipLayer.h:308
@ Sip606NotAcceptable
Definition: SipLayer.h:433
@ Sip403Forbidden
The server understood the request, but is refusing to fulfill it.
Definition: SipLayer.h:290
@ Sip421ExtensionRequired
The server needs a specific extension not listed in the Supported header.
Definition: SipLayer.h:329
@ Sip487RequestTerminated
Request has terminated by bye or cancel.
Definition: SipLayer.h:385
@ Sip404NotFound
Definition: SipLayer.h:294
@ Sip100Trying
Definition: SipLayer.h:251
@ Sip411LengthRequired
The server will not accept the request without a valid Content-Length.
Definition: SipLayer.h:312
@ Sip407ProxyAuthenticationRequired
The request requires user authentication. This response is issued by proxies.
Definition: SipLayer.h:302
@ Sip204NoNotification
Indicates the request was successful, but the corresponding response will not be received.
Definition: SipLayer.h:269
@ Sip494SecurityAgreementRequired
Definition: SipLayer.h:397
@ Sip437UnsupportedCertificate
The server was unable to validate a certificate for the domain that signed the request.
Definition: SipLayer.h:352
@ Sip488NotAcceptableHere
Some aspect of the session description or the Request-URI is not acceptable.
Definition: SipLayer.h:387
@ Sip417UnknownResourcePriority
There was a resource-priority option tag, but no Resource-Priority header.
Definition: SipLayer.h:325
@ Sip405MethodNotAllowed
Definition: SipLayer.h:297
@ SipStatusCodeUnknown
Unknown SIP status code.
Definition: SipLayer.h:440
@ Sip430FlowFailed
Definition: SipLayer.h:346
@ Sip469BadInfoPackage
Definition: SipLayer.h:367
@ Sip302MovedTemporarily
Definition: SipLayer.h:278
@ Sip489BadEvent
The server did not understand an event package specified in an Event header field.
Definition: SipLayer.h:389
@ Sip410Gone
The user existed once, but is not available here any more.
Definition: SipLayer.h:310
@ Sip300MultipleChoices
Definition: SipLayer.h:272
@ Sip482LoopDetected
Server has detected a loop.
Definition: SipLayer.h:375
@ Sip483TooManyHops
Max-Forwards header has reached the value '0'.
Definition: SipLayer.h:377
@ Sip513MessageTooLarge
The request message length is longer than the server can process.
Definition: SipLayer.h:416
@ Sip580PreconditionFailure
The server is unable or unwilling to meet some constraints specified in the offer.
Definition: SipLayer.h:421
@ Sip401Unauthorized
The request requires user authentication. This response is issued by UASs and registrars.
Definition: SipLayer.h:286
@ Sip438InvalidIdentityHeader
Definition: SipLayer.h:355
@ Sip485Ambiguous
Request-URI is ambiguous.
Definition: SipLayer.h:381
@ Sip607Unwanted
Definition: SipLayer.h:436
@ Sip425BadAlertMessage
Definition: SipLayer.h:338
@ Sip200OK
Indicates the request was successful.
Definition: SipLayer.h:265
@ Sip182Queued
Definition: SipLayer.h:258
@ Sip423IntervalTooBrief
Expiration time of the resource is too short.
Definition: SipLayer.h:333
@ Sip505VersionNotSupported
The SIP protocol version in the request is not supported by the server.
Definition: SipLayer.h:414
std::string toString() const override
SipResponseLayer(SipResponseLayer::SipResponseStatusCode statusCode, std::string statusCodeString="", const std::string &sipVersion="SIP/2.0")
SipResponseLayer & operator=(const SipResponseLayer &other)
SipResponseFirstLine * getFirstLine() const
Definition: SipLayer.h:474
SipResponseLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
SipResponseLayer(const SipResponseLayer &other)
Definition: TextBasedProtocol.h:105
The main namespace for the PcapPlusPlus lib.
uint8_t ProtocolType
Definition: ProtocolType.h:13
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:225
@ OsiModelSesionLayer
Session layer (layer 5)
Definition: ProtocolType.h:235