PcapPlusPlus  24.09
SipLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "TextBasedProtocol.h"
4 
6 
11 namespace pcpp
12 {
13 // some popular SIP header fields
14 
16 #define PCPP_SIP_FROM_FIELD "From"
18 #define PCPP_SIP_TO_FIELD "To"
20 #define PCPP_SIP_VIA_FIELD "Via"
22 #define PCPP_SIP_CALL_ID_FIELD "Call-ID"
24 #define PCPP_SIP_CONTENT_TYPE_FIELD "Content-Type"
26 #define PCPP_SIP_CONTENT_LENGTH_FIELD "Content-Length"
28 #define PCPP_SIP_CONTENT_DISPOSITION_FIELD "Content-Disposition"
30 #define PCPP_SIP_CONTENT_ENCODING_FIELD "Content-Encoding"
32 #define PCPP_SIP_CONTENT_LANGUAGE_FIELD "Content-Language"
34 #define PCPP_SIP_CSEQ_FIELD "CSeq"
36 #define PCPP_SIP_CONTACT_FIELD "Contact"
38 #define PCPP_SIP_MAX_FORWARDS_FIELD "Max-Forwards"
40 #define PCPP_SIP_USER_AGENT_FIELD "User-Agent"
42 #define PCPP_SIP_ACCEPT_FIELD "Accept"
44 #define PCPP_SIP_ACCEPT_ENCODING_FIELD "Accept-Encoding"
46 #define PCPP_SIP_ACCEPT_LANGUAGE_FIELD "Accept-Language"
48 #define PCPP_SIP_ALLOW_FIELD "Allow"
50 #define PCPP_SIP_AUTHORIZATION_FIELD "Authorization"
52 #define PCPP_SIP_DATE_FIELD "Date"
54 #define PCPP_SIP_MIME_VERSION_FIELD "MIME-Version"
56 #define PCPP_SIP_REASON_FIELD "Reason"
58 #define PCPP_SIP_SUPPORTED_FIELD "Supported"
60 #define PCPP_SIP_SERVER_FIELD "Server"
62 #define PCPP_SIP_WWW_AUTHENTICATE_FIELD "WWW-Authenticate"
64 #define PCPP_SIP_RETRY_AFTER_FIELD "Retry-After"
66 #define PCPP_SIP_RECORD_ROUTE_FIELD "Record-Route"
67 
74  {
75  public:
82  int getContentLength() const;
83 
99  HeaderField* setContentLength(int contentLength, const std::string& prevFieldName = "");
100 
101  // Overridden methods
102 
104  {
105  return OsiModelSesionLayer;
106  }
107 
114 
120 
125  static bool isSipPort(uint16_t port)
126  {
127  return port == 5060 || port == 5061;
128  }
129 
130  protected:
131  SipLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ProtocolType protocol)
132  : TextBasedProtocolMessage(data, dataLen, prevLayer, packet, protocol)
133  {}
134  SipLayer() : TextBasedProtocolMessage()
135  {}
136  SipLayer(const SipLayer& other) : TextBasedProtocolMessage(other)
137  {}
138  SipLayer& operator=(const SipLayer& other)
139  {
140  TextBasedProtocolMessage::operator=(other);
141  return *this;
142  }
143 
144  // implementation of abstract methods
145  char getHeaderFieldNameValueSeparator() const
146  {
147  return ':';
148  }
149  bool spacesAllowedBetweenHeaderFieldNameAndValue() const
150  {
151  return true;
152  }
153  };
154 
155  class SipRequestFirstLine;
156 
166  class SipRequestLayer : public SipLayer
167  {
168  friend class SipRequestFirstLine;
169 
170  public:
175  {
206  };
207 
214  SipRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
215 
223  SipRequestLayer(SipMethod method, const std::string& requestUri, const std::string& version = "SIP/2.0");
224 
225  ~SipRequestLayer();
226 
233 
240 
245  {
246  return m_FirstLine;
247  }
248 
249  // implement Layer's abstract methods
250 
251  std::string toString() const;
252 
253  private:
254  SipRequestFirstLine* m_FirstLine;
255  };
256 
257  class SipResponseFirstLine;
258 
268  class SipResponseLayer : public SipLayer
269  {
270  friend class SipResponseFirstLine;
271 
272  public:
278  {
472  };
473 
480  SipResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
481 
493  explicit SipResponseLayer(SipResponseLayer::SipResponseStatusCode statusCode, std::string statusCodeString = "",
494  const std::string& sipVersion = "SIP/2.0");
495 
496  virtual ~SipResponseLayer();
497 
504 
511 
516  {
517  return m_FirstLine;
518  }
519 
520  // implement Layer's abstract methods
521 
522  std::string toString() const;
523 
524  private:
525  SipResponseFirstLine* m_FirstLine;
526  };
527 
540  {
541  friend class SipRequestLayer;
542 
543  public:
548  {
549  return m_Method;
550  }
551 
559 
564  std::string getUri() const;
565 
571  bool setUri(const std::string& newUri);
572 
576  std::string getVersion() const
577  {
578  return m_Version;
579  }
580 
587  static SipRequestLayer::SipMethod parseMethod(const char* data, size_t dataLen);
588 
592  int getSize() const
593  {
594  return m_FirstLineEndOffset;
595  }
596 
603  bool isComplete() const
604  {
605  return m_IsComplete;
606  }
607 
614  class SipRequestFirstLineException : public std::exception
615  {
616  public:
617  ~SipRequestFirstLineException() noexcept
618  {}
619  void setMessage(const std::string& message)
620  {
621  m_Message = message;
622  }
623  virtual const char* what() const noexcept
624  {
625  return m_Message.c_str();
626  }
627 
628  private:
629  std::string m_Message;
630  };
631 
632  private:
634  SipRequestFirstLine(SipRequestLayer* sipRequest, SipRequestLayer::SipMethod method, const std::string& version,
635  const std::string& uri);
636 
637  void parseVersion();
638 
639  SipRequestLayer* m_SipRequest;
641  std::string m_Version;
642  int m_VersionOffset;
643  int m_UriOffset;
644  int m_FirstLineEndOffset;
645  bool m_IsComplete;
646  SipRequestFirstLineException m_Exception;
647  };
648 
661  {
662  friend class SipResponseLayer;
663 
664  public:
669  {
670  return m_StatusCode;
671  }
672 
676  int getStatusCodeAsInt() const;
677 
681  std::string getStatusCodeString() const;
682 
690  bool setStatusCode(SipResponseLayer::SipResponseStatusCode newStatusCode, std::string statusCodeString = "");
691 
695  std::string getVersion() const
696  {
697  return m_Version;
698  }
699 
705  void setVersion(const std::string& newVersion);
706 
713  static SipResponseLayer::SipResponseStatusCode parseStatusCode(const char* data, size_t dataLen);
714 
721  static std::string parseVersion(const char* data, size_t dataLen);
722 
726  int getSize() const
727  {
728  return m_FirstLineEndOffset;
729  }
730 
737  bool isComplete() const
738  {
739  return m_IsComplete;
740  }
741 
748  class SipResponseFirstLineException : public std::exception
749  {
750  public:
752  {}
753  void setMessage(const std::string& message)
754  {
755  m_Message = message;
756  }
757  virtual const char* what() const noexcept
758  {
759  return m_Message.c_str();
760  }
761 
762  private:
763  std::string m_Message;
764  };
765 
766  private:
768  SipResponseFirstLine(SipResponseLayer* sipResponse, const std::string& version,
769  SipResponseLayer::SipResponseStatusCode statusCode, std::string statusCodeString = "");
770 
771  SipResponseLayer* m_SipResponse;
772  std::string m_Version;
774  int m_FirstLineEndOffset;
775  bool m_IsComplete;
776  SipResponseFirstLineException m_Exception;
777  };
778 
779 } // namespace pcpp
Definition: TextBasedProtocol.h:30
Definition: Layer.h:70
Definition: Packet.h:27
Definition: SipLayer.h:74
OsiModelLayer getOsiModelLayer() const
Definition: SipLayer.h:103
HeaderField * setContentLength(int contentLength, const std::string &prevFieldName="")
static bool isSipPort(uint16_t port)
Definition: SipLayer.h:125
void computeCalculateFields()
void parseNextLayer()
int getContentLength() const
Definition: SipLayer.h:540
std::string getVersion() const
Definition: SipLayer.h:576
bool setUri(const std::string &newUri)
bool isComplete() const
Definition: SipLayer.h:603
std::string getUri() const
int getSize() const
Definition: SipLayer.h:592
bool setMethod(SipRequestLayer::SipMethod newMethod)
static SipRequestLayer::SipMethod parseMethod(const char *data, size_t dataLen)
SipRequestLayer::SipMethod getMethod() const
Definition: SipLayer.h:547
Definition: SipLayer.h:167
SipRequestLayer(const SipRequestLayer &other)
SipRequestLayer & operator=(const SipRequestLayer &other)
SipRequestFirstLine * getFirstLine() const
Definition: SipLayer.h:244
SipRequestLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
SipRequestLayer(SipMethod method, const std::string &requestUri, const std::string &version="SIP/2.0")
SipMethod
Definition: SipLayer.h:175
@ SipMESSAGE
Definition: SipLayer.h:201
@ SipSUBSCRIBE
Definition: SipLayer.h:191
@ SipINFO
Definition: SipLayer.h:197
@ SipUPDATE
Definition: SipLayer.h:203
@ SipPUBLISH
Definition: SipLayer.h:195
@ SipCANCEL
Definition: SipLayer.h:183
@ SipINVITE
Definition: SipLayer.h:177
@ SipNOTIFY
Definition: SipLayer.h:193
@ SipPRACK
Definition: SipLayer.h:187
@ SipOPTIONS
Definition: SipLayer.h:189
@ SipREGISTER
Definition: SipLayer.h:185
@ SipMethodUnknown
Definition: SipLayer.h:205
@ SipACK
Definition: SipLayer.h:179
@ SipREFER
Definition: SipLayer.h:199
@ SipBYE
Definition: SipLayer.h:181
std::string toString() const
Definition: SipLayer.h:661
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:668
void setVersion(const std::string &newVersion)
int getSize() const
Definition: SipLayer.h:726
bool isComplete() const
Definition: SipLayer.h:737
std::string getVersion() const
Definition: SipLayer.h:695
std::string getStatusCodeString() const
Definition: SipLayer.h:269
SipResponseStatusCode
Definition: SipLayer.h:278
@ Sip416UnsupportedURIScheme
Definition: SipLayer.h:354
@ Sip608Rejected
Definition: SipLayer.h:469
@ Sip415UnsupportedMediaType
Definition: SipLayer.h:352
@ Sip420BadExtension
Definition: SipLayer.h:358
@ Sip428UseIdentityHeader
Definition: SipLayer.h:371
@ Sip502BadGateway
Definition: SipLayer.h:437
@ Sip181CallisBeingForwarded
Definition: SipLayer.h:285
@ Sip503ServiceUnavailable
Definition: SipLayer.h:440
@ Sip183SessioninProgress
Definition: SipLayer.h:290
@ Sip301MovedPermanently
Definition: SipLayer.h:306
@ Sip414RequestURITooLong
Definition: SipLayer.h:350
@ Sip555PushNotificationServiceNotSupported
Definition: SipLayer.h:450
@ Sip504ServerTimeout
Definition: SipLayer.h:443
@ Sip380AlternativeService
Definition: SipLayer.h:313
@ Sip600BusyEverywhere
Definition: SipLayer.h:455
@ Sip202Accepted
Definition: SipLayer.h:298
@ Sip500ServerInternalError
Definition: SipLayer.h:430
@ Sip493Undecipherable
Definition: SipLayer.h:424
@ Sip412ConditionalRequestFailed
Definition: SipLayer.h:345
@ Sip402PaymentRequired
Definition: SipLayer.h:319
@ Sip436BadIdentityInfo
Definition: SipLayer.h:381
@ Sip422SessionIntervalTooSmall
Definition: SipLayer.h:362
@ Sip406NotAcceptable
Definition: SipLayer.h:331
@ Sip180Ringing
Definition: SipLayer.h:283
@ Sip470ConsentNeeded
Definition: SipLayer.h:400
@ Sip199EarlyDialogTerminated
Definition: SipLayer.h:293
@ Sip305UseProxy
Definition: SipLayer.h:311
@ Sip604DoesNotExistAnywhere
Definition: SipLayer.h:461
@ Sip424BadLocationInformation
Definition: SipLayer.h:366
@ Sip400BadRequest
Definition: SipLayer.h:315
@ Sip486BusyHere
Definition: SipLayer.h:414
@ Sip484AddressIncomplete
Definition: SipLayer.h:410
@ Sip413RequestEntityTooLarge
Definition: SipLayer.h:347
@ Sip433AnonymityDisallowed
Definition: SipLayer.h:379
@ Sip501NotImplemented
Definition: SipLayer.h:434
@ Sip491RequestPending
Definition: SipLayer.h:422
@ Sip429ProvideReferrerIdentity
Definition: SipLayer.h:373
@ Sip408RequestTimeout
Definition: SipLayer.h:337
@ Sip603Decline
Definition: SipLayer.h:459
@ Sip440MaxBreadthExceeded
Definition: SipLayer.h:394
@ Sip439FirstHopLacksOutboundSupport
Definition: SipLayer.h:389
@ Sip480TemporarilyUnavailable
Definition: SipLayer.h:402
@ Sip481Call_TransactionDoesNotExist
Definition: SipLayer.h:404
@ Sip409Conflict
Definition: SipLayer.h:339
@ Sip606NotAcceptable
Definition: SipLayer.h:464
@ Sip403Forbidden
Definition: SipLayer.h:321
@ Sip421ExtensionRequired
Definition: SipLayer.h:360
@ Sip487RequestTerminated
Definition: SipLayer.h:416
@ Sip404NotFound
Definition: SipLayer.h:325
@ Sip100Trying
Definition: SipLayer.h:281
@ Sip411LengthRequired
Definition: SipLayer.h:343
@ Sip407ProxyAuthenticationRequired
Definition: SipLayer.h:333
@ Sip204NoNotification
Definition: SipLayer.h:300
@ Sip494SecurityAgreementRequired
Definition: SipLayer.h:428
@ Sip437UnsupportedCertificate
Definition: SipLayer.h:383
@ Sip488NotAcceptableHere
Definition: SipLayer.h:418
@ Sip417UnknownResourcePriority
Definition: SipLayer.h:356
@ Sip405MethodNotAllowed
Definition: SipLayer.h:328
@ SipStatusCodeUnknown
Definition: SipLayer.h:471
@ Sip430FlowFailed
Definition: SipLayer.h:377
@ Sip469BadInfoPackage
Definition: SipLayer.h:398
@ Sip302MovedTemporarily
Definition: SipLayer.h:309
@ Sip489BadEvent
Definition: SipLayer.h:420
@ Sip410Gone
Definition: SipLayer.h:341
@ Sip300MultipleChoices
Definition: SipLayer.h:303
@ Sip482LoopDetected
Definition: SipLayer.h:406
@ Sip483TooManyHops
Definition: SipLayer.h:408
@ Sip513MessageTooLarge
Definition: SipLayer.h:447
@ Sip580PreconditionFailure
Definition: SipLayer.h:452
@ Sip401Unauthorized
Definition: SipLayer.h:317
@ Sip438InvalidIdentityHeader
Definition: SipLayer.h:386
@ Sip485Ambiguous
Definition: SipLayer.h:412
@ Sip607Unwanted
Definition: SipLayer.h:467
@ Sip425BadAlertMessage
Definition: SipLayer.h:369
@ Sip200OK
Definition: SipLayer.h:295
@ Sip182Queued
Definition: SipLayer.h:288
@ Sip423IntervalTooBrief
Definition: SipLayer.h:364
@ Sip505VersionNotSupported
Definition: SipLayer.h:445
SipResponseLayer(SipResponseLayer::SipResponseStatusCode statusCode, std::string statusCodeString="", const std::string &sipVersion="SIP/2.0")
SipResponseLayer & operator=(const SipResponseLayer &other)
std::string toString() const
SipResponseFirstLine * getFirstLine() const
Definition: SipLayer.h:515
SipResponseLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
SipResponseLayer(const SipResponseLayer &other)
Definition: TextBasedProtocol.h:123
The main namespace for the PcapPlusPlus lib.
uint8_t ProtocolType
Definition: ProtocolType.h:17
OsiModelLayer
Definition: ProtocolType.h:354
@ OsiModelSesionLayer
Definition: ProtocolType.h:364