PcapPlusPlus  23.09
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, const std::string& requestUri, const 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  {
413  SipStatusCodeUnknown
414  };
415 
422  SipResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
423 
434  explicit SipResponseLayer(SipResponseLayer::SipResponseStatusCode statusCode, std::string statusCodeString = "", const std::string& sipVersion = "SIP/2.0");
435 
436  virtual ~SipResponseLayer();
437 
443  SipResponseLayer(const SipResponseLayer& other);
444 
450  SipResponseLayer& operator=(const SipResponseLayer& other);
451 
455  SipResponseFirstLine* getFirstLine() const { return m_FirstLine; }
456 
457  // implement Layer's abstract methods
458 
459  std::string toString() const;
460 
461  private:
462  SipResponseFirstLine* m_FirstLine;
463  };
464 
465 
466 
478  {
479  friend class SipRequestLayer;
480  public:
481 
485  SipRequestLayer::SipMethod getMethod() const { return m_Method; }
486 
492  bool setMethod(SipRequestLayer::SipMethod newMethod);
493 
497  std::string getUri() const;
498 
504  bool setUri(const std::string& newUri);
505 
509  std::string getVersion() const { return m_Version; }
510 
517  static SipRequestLayer::SipMethod parseMethod(const char* data, size_t dataLen);
518 
522  int getSize() const { return m_FirstLineEndOffset; }
523 
530  bool isComplete() const { return m_IsComplete; }
531 
538  class SipRequestFirstLineException : public std::exception
539  {
540  public:
541  ~SipRequestFirstLineException() throw() {}
542  void setMessage(const std::string &message) { m_Message = message; }
543  virtual const char* what() const throw()
544  {
545  return m_Message.c_str();
546  }
547  private:
548  std::string m_Message;
549  };
550 
551  private:
553  SipRequestFirstLine(SipRequestLayer* sipRequest, SipRequestLayer::SipMethod method, const std::string& version, const std::string& uri);
554 
555  void parseVersion();
556 
557  SipRequestLayer* m_SipRequest;
559  std::string m_Version;
560  int m_VersionOffset;
561  int m_UriOffset;
562  int m_FirstLineEndOffset;
563  bool m_IsComplete;
564  SipRequestFirstLineException m_Exception;
565  };
566 
567 
568 
569 
580  {
581  friend class SipResponseLayer;
582  public:
587 
591  int getStatusCodeAsInt() const;
592 
596  std::string getStatusCodeString() const;
597 
604  bool setStatusCode(SipResponseLayer::SipResponseStatusCode newStatusCode, std::string statusCodeString = "");
605 
609  std::string getVersion() const { return m_Version; }
610 
615  void setVersion(const std::string& newVersion);
616 
623  static SipResponseLayer::SipResponseStatusCode parseStatusCode(const char* data, size_t dataLen);
624 
631  static std::string parseVersion(const char* data, size_t dataLen);
632 
636  int getSize() const { return m_FirstLineEndOffset; }
637 
644  bool isComplete() const { return m_IsComplete; }
645 
652  class SipResponseFirstLineException : public std::exception
653  {
654  public:
655  ~SipResponseFirstLineException() throw() {}
656  void setMessage(const std::string &message) { m_Message = message; }
657  virtual const char* what() const throw()
658  {
659  return m_Message.c_str();
660  }
661  private:
662  std::string m_Message;
663  };
664 
665  private:
667  SipResponseFirstLine(SipResponseLayer* sipResponse, const std::string& version, SipResponseLayer::SipResponseStatusCode statusCode, std::string statusCodeString = "");
668 
669  SipResponseLayer* m_SipResponse;
670  std::string m_Version;
672  int m_FirstLineEndOffset;
673  bool m_IsComplete;
674  SipResponseFirstLineException m_Exception;
675  };
676 
677 }
678 
679 #endif // PACKETPP_SIP_LAYER
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:333
Definition: SipLayer.h:411
Definition: SipLayer.h:295
bool isComplete() const
Definition: SipLayer.h:530
Definition: SipLayer.h:375
Definition: SipLayer.h:293
std::string getVersion() const
Definition: SipLayer.h:509
Definition: SipLayer.h:369
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:522
Definition: SipLayer.h:168
Definition: Packet.h:26
Definition: SipLayer.h:367
SipResponseLayer::SipResponseStatusCode getStatusCode() const
Definition: SipLayer.h:586
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:609
Definition: SipLayer.h:172
Definition: SipLayer.h:166
Definition: SipLayer.h:176
Definition: SipLayer.h:158
Definition: SipLayer.h:409
Definition: SipLayer.h:248
Definition: SipLayer.h:162
Definition: SipLayer.h:403
void computeCalculateFields()
SipRequestLayer::SipMethod getMethod() const
Definition: SipLayer.h:485
Definition: SipLayer.h:174
Definition: SipLayer.h:283
int getSize() const
Definition: SipLayer.h:636
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:344
virtual std::string toString() const =0
Definition: SipLayer.h:170
Definition: SipLayer.h:265
Definition: SipLayer.h:579
SipResponseFirstLine * getFirstLine() const
Definition: SipLayer.h:455
HeaderField * setContentLength(int contentLength, const std::string &prevFieldName="")
Definition: SipLayer.h:271
bool isComplete() const
Definition: SipLayer.h:644
Definition: TextBasedProtocol.h:31
Definition: SipLayer.h:145
Definition: SipLayer.h:182
SipRequestFirstLine * getFirstLine() const
Definition: SipLayer.h:223
Definition: SipLayer.h:477