PcapPlusPlus  22.11
SipLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_SIP_LAYER
2 #define PACKETPP_SIP_LAYER
3 
4 #include "TextBasedProtocol.h"
5 
7 
12 namespace pcpp
13 {
14 // some popular SIP header fields
15 
17 #define PCPP_SIP_FROM_FIELD "From"
18 
19 #define PCPP_SIP_TO_FIELD "To"
20 
21 #define PCPP_SIP_VIA_FIELD "Via"
22 
23 #define PCPP_SIP_CALL_ID_FIELD "Call-ID"
24 
25 #define PCPP_SIP_CONTENT_TYPE_FIELD "Content-Type"
26 
27 #define PCPP_SIP_CONTENT_LENGTH_FIELD "Content-Length"
28 
29 #define PCPP_SIP_CONTENT_DISPOSITION_FIELD "Content-Disposition"
30 
31 #define PCPP_SIP_CONTENT_ENCODING_FIELD "Content-Encoding"
32 
33 #define PCPP_SIP_CONTENT_LANGUAGE_FIELD "Content-Language"
34 
35 #define PCPP_SIP_CSEQ_FIELD "CSeq"
36 
37 #define PCPP_SIP_CONTACT_FIELD "Contact"
38 
39 #define PCPP_SIP_MAX_FORWARDS_FIELD "Max-Forwards"
40 
41 #define PCPP_SIP_USER_AGENT_FIELD "User-Agent"
42 
43 #define PCPP_SIP_ACCEPT_FIELD "Accept"
44 
45 #define PCPP_SIP_ACCEPT_ENCODING_FIELD "Accept-Encoding"
46 
47 #define PCPP_SIP_ACCEPT_LANGUAGE_FIELD "Accept-Language"
48 
49 #define PCPP_SIP_ALLOW_FIELD "Allow"
50 
51 #define PCPP_SIP_AUTHORIZATION_FIELD "Authorization"
52 
53 #define PCPP_SIP_DATE_FIELD "Date"
54 
55 #define PCPP_SIP_MIME_VERSION_FIELD "MIME-Version"
56 
57 #define PCPP_SIP_REASON_FIELD "Reason"
58 
59 #define PCPP_SIP_SUPPORTED_FIELD "Supported"
60 
61 #define PCPP_SIP_SERVER_FIELD "Server"
62 
63 #define PCPP_SIP_WWW_AUTHENTICATE_FIELD "WWW-Authenticate"
64 
65 #define PCPP_SIP_RETRY_AFTER_FIELD "Retry-After"
66 
67 #define PCPP_SIP_RECORD_ROUTE_FIELD "Record-Route"
68 
69 
75  {
76  public:
77 
83  int getContentLength() const;
84 
98  HeaderField* setContentLength(int contentLength, const std::string prevFieldName = "");
99 
100  // Overridden methods
101 
103 
108  void parseNextLayer();
109 
113  void computeCalculateFields();
114 
119  static bool isSipPort(uint16_t port) { return port == 5060 || port == 5061; }
120 
121  protected:
122  SipLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : TextBasedProtocolMessage(data, dataLen, prevLayer, packet) {}
124  SipLayer(const SipLayer& other) : TextBasedProtocolMessage(other) {}
125  SipLayer& operator=(const SipLayer& other) { TextBasedProtocolMessage::operator=(other); return *this; }
126 
127  // implementation of abstract methods
128  char getHeaderFieldNameValueSeparator() const { return ':'; }
129  bool spacesAllowedBetweenHeaderFieldNameAndValue() const { return true; }
130  };
131 
132 
133  class SipRequestFirstLine;
134 
135 
145  class SipRequestLayer : public SipLayer
146  {
147  friend class SipRequestFirstLine;
148 
149  public:
154  {
184  SipMethodUnknown
185  };
186 
193  SipRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
194 
202  SipRequestLayer(SipMethod method, std::string requestUri, std::string version = "SIP/2.0");
203 
204  ~SipRequestLayer();
205 
211  SipRequestLayer(const SipRequestLayer& other);
212 
218  SipRequestLayer& operator=(const SipRequestLayer& other);
219 
223  SipRequestFirstLine* getFirstLine() const { return m_FirstLine; }
224 
225  // implement Layer's abstract methods
226 
227  std::string toString() const;
228 
229  private:
230  SipRequestFirstLine* m_FirstLine;
231  };
232 
233 
234 
235 
236  class SipResponseFirstLine;
237 
238 
248  class SipResponseLayer : public SipLayer
249  {
250  friend class SipResponseFirstLine;
251  public:
252 
257  {
407  SipStatusCodeUnknown
408  };
409 
416  SipResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
417 
428  SipResponseLayer(SipResponseLayer::SipResponseStatusCode statusCode, std::string statusCodeString = "", std::string sipVersion = "SIP/2.0");
429 
430  virtual ~SipResponseLayer();
431 
437  SipResponseLayer(const SipResponseLayer& other);
438 
444  SipResponseLayer& operator=(const SipResponseLayer& other);
445 
449  SipResponseFirstLine* getFirstLine() const { return m_FirstLine; }
450 
451  // implement Layer's abstract methods
452 
453  std::string toString() const;
454 
455  private:
456  SipResponseFirstLine* m_FirstLine;
457  };
458 
459 
460 
472  {
473  friend class SipRequestLayer;
474  public:
475 
479  SipRequestLayer::SipMethod getMethod() const { return m_Method; }
480 
486  bool setMethod(SipRequestLayer::SipMethod newMethod);
487 
491  std::string getUri() const;
492 
498  bool setUri(std::string newUri);
499 
503  std::string getVersion() const { return m_Version; }
504 
511  static SipRequestLayer::SipMethod parseMethod(char* data, size_t dataLen);
512 
516  int getSize() const { return m_FirstLineEndOffset; }
517 
524  bool isComplete() const { return m_IsComplete; }
525 
532  class SipRequestFirstLineException : public std::exception
533  {
534  public:
535  ~SipRequestFirstLineException() throw() {}
536  void setMessage(std::string message) { m_Message = message; }
537  virtual const char* what() const throw()
538  {
539  return m_Message.c_str();
540  }
541  private:
542  std::string m_Message;
543  };
544 
545  private:
547  SipRequestFirstLine(SipRequestLayer* sipRequest, SipRequestLayer::SipMethod method, std::string version, std::string uri);
548  //throw(SipRequestFirstLineException); // Deprecated in C++17
549 
550  void parseVersion();
551 
552  SipRequestLayer* m_SipRequest;
554  std::string m_Version;
555  int m_VersionOffset;
556  int m_UriOffset;
557  int m_FirstLineEndOffset;
558  bool m_IsComplete;
559  SipRequestFirstLineException m_Exception;
560  };
561 
562 
563 
564 
575  {
576  friend class SipResponseLayer;
577  public:
582 
586  int getStatusCodeAsInt() const;
587 
591  std::string getStatusCodeString() const;
592 
599  bool setStatusCode(SipResponseLayer::SipResponseStatusCode newStatusCode, std::string statusCodeString = "");
600 
604  std::string getVersion() const { return m_Version; }
605 
610  void setVersion(std::string newVersion);
611 
618  static SipResponseLayer::SipResponseStatusCode parseStatusCode(char* data, size_t dataLen);
619 
626  static std::string parseVersion(char* data, size_t dataLen);
627 
631  int getSize() const { return m_FirstLineEndOffset; }
632 
639  bool isComplete() const { return m_IsComplete; }
640 
647  class SipResponseFirstLineException : public std::exception
648  {
649  public:
650  ~SipResponseFirstLineException() throw() {}
651  void setMessage(std::string message) { m_Message = message; }
652  virtual const char* what() const throw()
653  {
654  return m_Message.c_str();
655  }
656  private:
657  std::string m_Message;
658  };
659 
660  private:
662  SipResponseFirstLine(SipResponseLayer* sipResponse, std::string version, SipResponseLayer::SipResponseStatusCode statusCode, std::string statusCodeString = "");
663 
664  static SipResponseLayer::SipResponseStatusCode validateStatusCode(char* data, size_t dataLen, SipResponseLayer::SipResponseStatusCode potentialCode);
665 
666 
667  SipResponseLayer* m_SipResponse;
668  std::string m_Version;
670  int m_FirstLineEndOffset;
671  bool m_IsComplete;
672  SipResponseFirstLineException m_Exception;
673  };
674 
675 }
676 
677 #endif // PACKETPP_SIP_LAYER
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:298
Definition: SipLayer.h:295
HeaderField * setContentLength(int contentLength, const std::string prevFieldName="")
bool isComplete() const
Definition: SipLayer.h:524
Definition: SipLayer.h:373
Definition: SipLayer.h:293
std::string getVersion() const
Definition: SipLayer.h:503
Definition: SipLayer.h:367
Definition: SipLayer.h:160
Definition: SipLayer.h:180
Definition: SipLayer.h:156
Definition: SipLayer.h:305
Definition: Layer.h:70
int getSize() const
Definition: SipLayer.h:516
Definition: SipLayer.h:168
Definition: Packet.h:26
Definition: SipLayer.h:365
SipResponseLayer::SipResponseStatusCode getStatusCode() const
Definition: SipLayer.h:581
Definition: SipLayer.h:74
Definition: SipLayer.h:273
static bool isSipPort(uint16_t port)
Definition: SipLayer.h:119
Definition: SipLayer.h:307
Definition: TextBasedProtocol.h:115
std::string getVersion() const
Definition: SipLayer.h:604
Definition: SipLayer.h:172
Definition: SipLayer.h:166
Definition: SipLayer.h:176
Definition: SipLayer.h:158
Definition: SipLayer.h:405
Definition: SipLayer.h:248
Definition: SipLayer.h:162
Definition: SipLayer.h:399
void computeCalculateFields()
SipRequestLayer::SipMethod getMethod() const
Definition: SipLayer.h:479
Definition: SipLayer.h:174
Definition: SipLayer.h:283
int getSize() const
Definition: SipLayer.h:631
void parseNextLayer()
SipMethod
Definition: SipLayer.h:153
SipResponseStatusCode
Definition: SipLayer.h:256
Definition: SipLayer.h:261
Definition: SipLayer.h:178
OsiModelLayer getOsiModelLayer() const
Definition: SipLayer.h:102
Definition: SipLayer.h:164
int getContentLength() const
Definition: SipLayer.h:259
Definition: ProtocolType.h:309
virtual std::string toString() const =0
Definition: SipLayer.h:170
Definition: SipLayer.h:265
Definition: SipLayer.h:574
SipResponseFirstLine * getFirstLine() const
Definition: SipLayer.h:449
Definition: SipLayer.h:271
bool isComplete() const
Definition: SipLayer.h:639
Definition: TextBasedProtocol.h:31
Definition: SipLayer.h:145
Definition: SipLayer.h:182
SipRequestFirstLine * getFirstLine() const
Definition: SipLayer.h:223
Definition: SipLayer.h:471