PcapPlusPlus
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();
84 
98  HeaderField* setContentLength(int contentLength, const std::string prevFieldName = "");
99 
100  // Overridden methods
101 
103 
108  void parseNextLayer();
109 
113  void computeCalculateFields();
114 
115  protected:
116  SipLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : TextBasedProtocolMessage(data, dataLen, prevLayer, packet) {}
117  SipLayer() : TextBasedProtocolMessage() {}
118  SipLayer(const SipLayer& other) : TextBasedProtocolMessage(other) {}
119  SipLayer& operator=(const SipLayer& other) { TextBasedProtocolMessage::operator=(other); return *this; }
120 
121  // implementation of abstract methods
122  char getHeaderFieldNameValueSeparator() { return ':'; }
123  bool spacesAllowedBetweenHeaderFieldNameAndValue() { return true; }
124  };
125 
126 
127 
128  class SipRequestFirstLine;
129 
130 
140  class SipRequestLayer : public SipLayer
141  {
142  friend class SipRequestFirstLine;
143 
144  public:
149  {
180  };
181 
188  SipRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
189 
197  SipRequestLayer(SipMethod method, std::string requestUri, std::string version = "SIP/2.0");
198 
199  ~SipRequestLayer();
200 
206  SipRequestLayer(const SipRequestLayer& other);
207 
214 
218  inline SipRequestFirstLine* getFirstLine() { return m_FirstLine; }
219 
220  // implement Layer's abstract methods
221 
222  std::string toString();
223 
224  private:
225  SipRequestFirstLine* m_FirstLine;
226  };
227 
228 
229 
230 
231  class SipResponseFirstLine;
232 
233 
243  class SipResponseLayer : public SipLayer
244  {
245  friend class SipResponseFirstLine;
246  public:
247 
252  {
403  };
404 
411  SipResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
412 
423  SipResponseLayer(SipResponseLayer::SipResponseStatusCode statusCode, std::string statusCodeString = "", std::string sipVersion = "SIP/2.0");
424 
425  virtual ~SipResponseLayer();
426 
432  SipResponseLayer(const SipResponseLayer& other);
433 
440 
444  inline SipResponseFirstLine* getFirstLine() { return m_FirstLine; }
445 
446  // implement Layer's abstract methods
447 
448  std::string toString();
449 
450  private:
451  SipResponseFirstLine* m_FirstLine;
452  };
453 
454 
455 
467  {
468  friend class SipRequestLayer;
469  public:
470 
474  inline SipRequestLayer::SipMethod getMethod() { return m_Method; }
475 
481  bool setMethod(SipRequestLayer::SipMethod newMethod);
482 
486  std::string getUri();
487 
493  bool setUri(std::string newUri);
494 
498  inline std::string getVersion() { return m_Version; }
499 
506  static SipRequestLayer::SipMethod parseMethod(char* data, size_t dataLen);
507 
511  inline int getSize() { return m_FirstLineEndOffset; }
512 
519  inline bool isComplete() { return m_IsComplete; }
520 
527  class SipRequestFirstLineException : public std::exception
528  {
529  public:
530  ~SipRequestFirstLineException() throw() {}
531  void setMessage(std::string message) { m_Message = message; }
532  virtual const char* what() const throw()
533  {
534  return m_Message.c_str();
535  }
536  private:
537  std::string m_Message;
538  };
539 
540  private:
542  SipRequestFirstLine(SipRequestLayer* sipRequest, SipRequestLayer::SipMethod method, std::string version, std::string uri);
543  //throw(SipRequestFirstLineException); // Deprecated in C++17
544 
545  void parseVersion();
546 
547  SipRequestLayer* m_SipRequest;
549  std::string m_Version;
550  int m_VersionOffset;
551  int m_UriOffset;
552  int m_FirstLineEndOffset;
553  bool m_IsComplete;
554  SipRequestFirstLineException m_Exception;
555  };
556 
557 
558 
559 
570  {
571  friend class SipResponseLayer;
572  public:
576  inline SipResponseLayer::SipResponseStatusCode getStatusCode() { return m_StatusCode; }
577 
581  int getStatusCodeAsInt();
582 
586  std::string getStatusCodeString();
587 
594  bool setStatusCode(SipResponseLayer::SipResponseStatusCode newStatusCode, std::string statusCodeString = "");
595 
599  inline std::string getVersion() { return m_Version; }
600 
605  void setVersion(std::string newVersion);
606 
613  static SipResponseLayer::SipResponseStatusCode parseStatusCode(char* data, size_t dataLen);
614 
618  inline int getSize() { return m_FirstLineEndOffset; }
619 
626  inline bool isComplete() { return m_IsComplete; }
627 
634  class SipResponseFirstLineException : public std::exception
635  {
636  public:
637  ~SipResponseFirstLineException() throw() {}
638  void setMessage(std::string message) { m_Message = message; }
639  virtual const char* what() const throw()
640  {
641  return m_Message.c_str();
642  }
643  private:
644  std::string m_Message;
645  };
646 
647  private:
649  SipResponseFirstLine(SipResponseLayer* sipResponse, std::string version, SipResponseLayer::SipResponseStatusCode statusCode, std::string statusCodeString = "");
650 
651  static std::string parseVersion(char* data, size_t dataLen);
652  static SipResponseLayer::SipResponseStatusCode validateStatusCode(char* data, size_t dataLen, SipResponseLayer::SipResponseStatusCode potentialCode);
653 
654 
655  SipResponseLayer* m_SipResponse;
656  std::string m_Version;
658  int m_FirstLineEndOffset;
659  bool m_IsComplete;
660  SipResponseFirstLineException m_Exception;
661  };
662 
663 }
664 
665 #endif // PACKETPP_SIP_LAYER
Definition: SipLayer.h:243
Definition: TextBasedProtocol.h:31
Definition: SipLayer.h:300
int getContentLength()
Definition: SipLayer.h:163
Definition: SipLayer.h:171
std::string toString()
bool isComplete()
Definition: SipLayer.h:519
HeaderField * setContentLength(int contentLength, const std::string prevFieldName="")
Definition: SipLayer.h:155
bool setMethod(SipRequestLayer::SipMethod newMethod)
bool isComplete()
Definition: SipLayer.h:626
void setVersion(std::string newVersion)
Definition: ProtocolType.h:224
Definition: SipLayer.h:278
Definition: SipLayer.h:157
SipRequestLayer & operator=(const SipRequestLayer &other)
Definition: SipLayer.h:165
Definition: SipLayer.h:179
Definition: SipLayer.h:256
Definition: SipLayer.h:290
Definition: SipLayer.h:175
Definition: SipLayer.h:161
SipRequestLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
static SipResponseLayer::SipResponseStatusCode parseStatusCode(char *data, size_t dataLen)
SipResponseStatusCode
Definition: SipLayer.h:251
void parseNextLayer()
SipRequestLayer::SipMethod getMethod()
Definition: SipLayer.h:474
OsiModelLayer getOsiModelLayer()
Definition: SipLayer.h:102
bool setStatusCode(SipResponseLayer::SipResponseStatusCode newStatusCode, std::string statusCodeString="")
Definition: SipLayer.h:394
Definition: SipLayer.h:177
SipResponseLayer & operator=(const SipResponseLayer &other)
Definition: Layer.h:70
int getSize()
Definition: SipLayer.h:618
Definition: SipLayer.h:268
Definition: Packet.h:26
Definition: SipLayer.h:153
std::string toString()
Definition: SipLayer.h:466
Definition: TextBasedProtocol.h:104
Definition: SipLayer.h:159
Definition: SipLayer.h:266
Definition: SipLayer.h:254
OsiModelLayer
Definition: ProtocolType.h:213
int getSize()
Definition: SipLayer.h:511
Definition: SipLayer.h:569
Definition: SipLayer.h:288
Definition: SipLayer.h:400
std::string getStatusCodeString()
Definition: SipLayer.h:302
bool setUri(std::string newUri)
Definition: SipLayer.h:362
Definition: SipLayer.h:151
SipResponseLayer::SipResponseStatusCode getStatusCode()
Definition: SipLayer.h:576
Definition: SipLayer.h:167
Definition: SipLayer.h:360
std::string getVersion()
Definition: SipLayer.h:599
Definition: SipLayer.h:260
Definition: SipLayer.h:140
SipMethod
Definition: SipLayer.h:148
Definition: SipLayer.h:169
std::string getVersion()
Definition: SipLayer.h:498
SipResponseLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SipLayer.h:173
static SipRequestLayer::SipMethod parseMethod(char *data, size_t dataLen)
The main namespace for the PcapPlusPlus lib.
Definition: SipLayer.h:368
SipRequestFirstLine * getFirstLine()
Definition: SipLayer.h:218
Definition: SipLayer.h:74
SipResponseFirstLine * getFirstLine()
Definition: SipLayer.h:444
void computeCalculateFields()