msg_parser.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * History
  28. * -------
  29. * 2003-01-28 removed scratchpad (jiri)
  30. * 2003-02-28 scratchpad compatibility abandoned (jiri)
  31. * 2003-03-06 enum_request_method changed to begin with 1;
  32. * 0 reserved for invalid values; (jiri)
  33. * 2003-03-31 removed sip_msg->repl_add_rm (andrei)
  34. * 2003-04-01 2 macros added: GET_NEXT_HOP and GET_RURI (janakj)
  35. * 2003-04-04 structure for parsed inbound uri added (jiri)
  36. * 2003-04-11 updated the sip_uri structure (lots of fields added) (andrei)
  37. * 2003-04-12 added msg_flags to sip_msg (andrei)
  38. * 2003-11-02 added diversion header field to sip_msg (jh)
  39. * 2004-11-08 added force_send_socket (andrei)
  40. * 2005-02-25 uri types added (sip, sips & tel) (andrei)
  41. * 2006-04-20 uri comp member (only if USE_COMP is defined) (andrei)
  42. * 2006-11-10 check_transaction_quadruple inlined (andrei)
  43. * 2007-01-26 added date, identity, identity_info header fields
  44. * to sip_msg (gergo)
  45. * 2007-03-14 added SIP_MSG_START(msg) macro
  46. */
  47. /*! \file
  48. * \brief Parser :: ???
  49. *
  50. * \ingroup parser
  51. */
  52. #ifndef msg_parser_h
  53. #define msg_parser_h
  54. #include "../comp_defs.h"
  55. #include "../str.h"
  56. #include "../lump_struct.h"
  57. #include "../flags.h"
  58. #include "../ip_addr.h"
  59. #include "../md5utils.h"
  60. #include "../config.h"
  61. #include "parse_def.h"
  62. #include "parse_cseq.h"
  63. #include "parse_via.h"
  64. #include "parse_fline.h"
  65. #include "parse_retry_after.h"
  66. #include "hf.h"
  67. #include "../error.h"
  68. /*! \name convenience short-cut macros */
  69. /*@{ */
  70. #define REQ_LINE(_msg) ((_msg)->first_line.u.request)
  71. #define REQ_METHOD first_line.u.request.method_value
  72. #define REPLY_STATUS first_line.u.reply.statuscode
  73. #define REPLY_CLASS(_reply) ((_reply)->REPLY_STATUS/100)
  74. /*@} */
  75. /*! \brief start of "actual" sip msg (start of first line) */
  76. #define SIP_MSG_START(m) ((m)->first_line.u.request.method.s)
  77. /*! \brief number methods as power of two to allow bitmap matching */
  78. enum request_method {
  79. METHOD_UNDEF=0, /*!< 0 - --- */
  80. METHOD_INVITE=1, /*!< 1 - 2^0 */
  81. METHOD_CANCEL=2, /*!< 2 - 2^1 */
  82. METHOD_ACK=4, /*!< 3 - 2^2 */
  83. METHOD_BYE=8, /*!< 4 - 2^3 */
  84. METHOD_INFO=16, /*!< 5 - 2^4 */
  85. METHOD_REGISTER=32, /*!< 6 - 2^5 */
  86. METHOD_SUBSCRIBE=64, /*!< 7 - 2^6 */
  87. METHOD_NOTIFY=128, /*!< 8 - 2^7 */
  88. METHOD_MESSAGE=256, /*!< 9 - 2^8 */
  89. METHOD_OPTIONS=512, /*!< 10 - 2^9 */
  90. METHOD_PRACK=1024, /*!< 11 - 2^10 */
  91. METHOD_UPDATE=2048, /*!< 12 - 2^11 */
  92. METHOD_REFER=4096, /*!< 13 - 2^12 */
  93. METHOD_PUBLISH=8192, /*!< 14 - 2^13 */
  94. METHOD_OTHER=16384 /*!< 15 - 2^14 */
  95. };
  96. #define FL_FORCE_RPORT (1 << 0) /*!< force rport */
  97. #define FL_FORCE_ACTIVE (1 << 1) /*!< force active SDP */
  98. #define FL_SDP_IP_AFS (1 << 2) /*!< SDP IP rewritten */
  99. #define FL_SDP_PORT_AFS (1 << 3) /*!< SDP port rewritten */
  100. #define FL_SHM_CLONE (1 << 4) /*!< msg cloned in SHM as a single chunk */
  101. #define FL_TIMEOUT (1 << 5) /*!< message belongs to an "expired" branch
  102. (for failure route use) */
  103. #define FL_REPLIED (1 << 6) /*!< message branch received at least one reply
  104. (for failure route use) */
  105. #define FL_HASH_INDEX (1 << 7) /*!< msg->hash_index contains a valid value (tm use)*/
  106. #define FL_MTU_TCP_FB (1 << 8)
  107. #define FL_MTU_TLS_FB (1 << 9)
  108. #define FL_MTU_SCTP_FB (1 << 10)
  109. #define FL_ADD_LOCAL_RPORT (1 << 11) /*!< add 'rport' to local via hdr */
  110. /* WARNING: Value (1 << 29) is temporarily reserved for use in kamailio acc
  111. * module (flag FL_REQ_UPSTREAM)! */
  112. /* WARNING: Value (1 << 30) is temporarily reserved for use in kamailio
  113. * media proxy module (flag FL_USE_MEDIA_PROXY)! */
  114. /* WARNING: Value (1 << 31) is temporarily reserved for use in kamailio
  115. * nat_traversal module (flag FL_DO_KEEPALIVE)! */
  116. #define FL_MTU_FB_MASK (FL_MTU_TCP_FB|FL_MTU_TLS_FB|FL_MTU_SCTP_FB)
  117. #define IFISMETHOD(methodname,firstchar) \
  118. if ( (*tmp==(firstchar) || *tmp==((firstchar) | 32)) && \
  119. strncasecmp( tmp+1, #methodname +1, methodname##_LEN-1)==0 && \
  120. *(tmp+methodname##_LEN)==' ') { \
  121. fl->type=SIP_REQUEST; \
  122. fl->u.request.method.len=methodname##_LEN; \
  123. fl->u.request.method_value=METHOD_##methodname; \
  124. tmp=buffer+methodname##_LEN; \
  125. }
  126. #define IS_HTTP(req) \
  127. ((req)->first_line.u.request.version.len >= HTTP_VERSION_LEN && \
  128. !strncasecmp((req)->first_line.u.request.version.s, \
  129. HTTP_VERSION, HTTP_VERSION_LEN))
  130. #define IS_SIP(req) \
  131. ((req)->first_line.u.request.version.len >= SIP_VERSION_LEN && \
  132. !strncasecmp((req)->first_line.u.request.version.s, \
  133. SIP_VERSION, SIP_VERSION_LEN))
  134. /*! \brief
  135. * Return a URI to which the message should be really sent (not what should
  136. * be in the Request URI. The following fields are tried in this order:
  137. * 1) dst_uri
  138. * 2) new_uri
  139. * 3) first_line.u.request.uri
  140. */
  141. #define GET_NEXT_HOP(m) \
  142. (((m)->dst_uri.s && (m)->dst_uri.len) ? (&(m)->dst_uri) : \
  143. (((m)->new_uri.s && (m)->new_uri.len) ? (&(m)->new_uri) : (&(m)->first_line.u.request.uri)))
  144. /*! \brief
  145. * Return the Reqeust URI of a message.
  146. * The following fields are tried in this order:
  147. * 1) new_uri
  148. * 2) first_line.u.request.uri
  149. */
  150. #define GET_RURI(m) \
  151. (((m)->new_uri.s && (m)->new_uri.len) ? (&(m)->new_uri) : (&(m)->first_line.u.request.uri))
  152. #if 0
  153. /* old version */
  154. struct sip_uri {
  155. str user; /* Username */
  156. str passwd; /* Password */
  157. str host; /* Host name */
  158. str port; /* Port number */
  159. str params; /* Parameters */
  160. str headers;
  161. unsigned short port_no;
  162. };
  163. #endif
  164. enum _uri_type{ERROR_URI_T=0, SIP_URI_T, SIPS_URI_T, TEL_URI_T, TELS_URI_T};
  165. typedef enum _uri_type uri_type;
  166. enum _uri_flags{
  167. URI_USER_NORMALIZE=1,
  168. URI_SIP_USER_PHONE=2
  169. }; /* bit fields */
  170. typedef enum _uri_flags uri_flags;
  171. /*! \brief The SIP uri object */
  172. struct sip_uri {
  173. str user; /*!< Username */
  174. str passwd; /*!< Password */
  175. str host; /*!< Host name */
  176. str port; /*!< Port number */
  177. str params; /*!< Parameters */
  178. str sip_params; /*!< Parameters of the sip: URI.
  179. * (If a tel: URI is embedded in a sip: URI, then
  180. * params points to the parameters of the tel: URI,
  181. * and sip_params to the parameters of the sip: URI.
  182. */
  183. str headers;
  184. unsigned short port_no;
  185. unsigned short proto; /*!< from transport */
  186. uri_type type; /*!< uri scheme */
  187. uri_flags flags;
  188. /*!< parameters */
  189. str transport;
  190. str ttl;
  191. str user_param;
  192. str maddr;
  193. str method;
  194. str lr;
  195. str r2; /*!< ser specific rr parameter */
  196. str transport_val; /*!< transport value */
  197. str ttl_val; /*!< TTL value */
  198. str user_param_val; /*!< User= param value */
  199. str maddr_val; /*!< Maddr= param value */
  200. str method_val; /*!< Method value */
  201. str lr_val; /*!< lr value placeholder for lr=on a.s.o*/
  202. str r2_val;
  203. #ifdef USE_COMP
  204. unsigned short comp;
  205. #endif
  206. };
  207. struct msg_body;
  208. typedef void (*free_msg_body_f)(struct msg_body** ptr);
  209. typedef enum msg_body_type {
  210. MSG_BODY_UNKNOWN = 0,
  211. MSG_BODY_SDP
  212. } msg_body_type_t;
  213. /*! \brief This structure represents a generic SIP message body, regardless of the
  214. * body type.
  215. *
  216. * Body parsers are supposed to cast this structure to some other
  217. * body-type specific structure, but the body type specific structure must
  218. * retain msg_body_type variable and a pointer to the free function as the
  219. * first two variables within the structure.
  220. */
  221. typedef struct msg_body {
  222. msg_body_type_t type;
  223. free_msg_body_f free;
  224. } msg_body_t;
  225. /*! \brief The SIP message */
  226. typedef struct sip_msg {
  227. unsigned int id; /*!< message id, unique/process*/
  228. snd_flags_t fwd_send_flags; /*!< send flags for forwarding */
  229. snd_flags_t rpl_send_flags; /*!< send flags for replies */
  230. struct msg_start first_line; /*!< Message first line */
  231. struct via_body* via1; /*!< The first via */
  232. struct via_body* via2; /*!< The second via */
  233. struct hdr_field* headers; /*!< All the parsed headers*/
  234. struct hdr_field* last_header; /*!< Pointer to the last parsed header*/
  235. hdr_flags_t parsed_flag; /*!< Already parsed header field types */
  236. /* Via, To, CSeq, Call-Id, From, end of header*/
  237. /* pointers to the first occurrences of these headers;
  238. * everything is also saved in 'headers'
  239. * (WARNING: do not deallocate them twice!)*/
  240. struct hdr_field* h_via1;
  241. struct hdr_field* h_via2;
  242. struct hdr_field* callid;
  243. struct hdr_field* to;
  244. struct hdr_field* cseq;
  245. struct hdr_field* from;
  246. struct hdr_field* contact;
  247. struct hdr_field* maxforwards;
  248. struct hdr_field* route;
  249. struct hdr_field* record_route;
  250. struct hdr_field* content_type;
  251. struct hdr_field* content_length;
  252. struct hdr_field* authorization;
  253. struct hdr_field* expires;
  254. struct hdr_field* proxy_auth;
  255. struct hdr_field* supported;
  256. struct hdr_field* require;
  257. struct hdr_field* proxy_require;
  258. struct hdr_field* unsupported;
  259. struct hdr_field* allow;
  260. struct hdr_field* event;
  261. struct hdr_field* accept;
  262. struct hdr_field* accept_language;
  263. struct hdr_field* organization;
  264. struct hdr_field* priority;
  265. struct hdr_field* subject;
  266. struct hdr_field* user_agent;
  267. struct hdr_field* server;
  268. struct hdr_field* content_disposition;
  269. struct hdr_field* accept_disposition;
  270. struct hdr_field* diversion;
  271. struct hdr_field* rpid;
  272. struct hdr_field* refer_to;
  273. struct hdr_field* session_expires;
  274. struct hdr_field* min_se;
  275. struct hdr_field* sipifmatch;
  276. struct hdr_field* subscription_state;
  277. struct hdr_field* date;
  278. struct hdr_field* identity;
  279. struct hdr_field* identity_info;
  280. struct hdr_field* pai;
  281. struct hdr_field* ppi;
  282. struct hdr_field* path;
  283. struct hdr_field* privacy;
  284. struct msg_body* body;
  285. char* eoh; /*!< pointer to the end of header (if found) or null */
  286. char* unparsed; /*!< here we stopped parsing*/
  287. struct receive_info rcv; /*!< source & dest ip, ports, proto a.s.o*/
  288. char* buf; /*!< scratch pad, holds a modified message,
  289. * via, etc. point into it */
  290. unsigned int len; /*!< message len (orig) */
  291. /* modifications */
  292. str new_uri; /*!< changed first line uri, when you change this
  293. don't forget to set parsed_uri_ok to 0*/
  294. str dst_uri; /*!< Destination URI, must be forwarded to this URI if len != 0 */
  295. /* current uri */
  296. int parsed_uri_ok; /*!< 1 if parsed_uri is valid, 0 if not, set if to 0
  297. if you modify the uri (e.g change new_uri)*/
  298. struct sip_uri parsed_uri; /*!< speed-up > keep here the parsed uri*/
  299. int parsed_orig_ruri_ok; /*!< 1 if parsed_orig_uri is valid, 0 if not, set if to 0
  300. if you modify the uri (e.g change new_uri)*/
  301. struct sip_uri parsed_orig_ruri; /*!< speed-up > keep here the parsed orig uri*/
  302. struct lump* add_rm; /*!< used for all the forwarded requests/replies */
  303. struct lump* body_lumps; /*!< Lumps that update Content-Length */
  304. struct lump_rpl *reply_lump; /*!< only for localy generated replies !!!*/
  305. /*! \brief str add_to_branch;
  306. whatever whoever want to append to branch comes here
  307. */
  308. char add_to_branch_s[MAX_BRANCH_PARAM_LEN];
  309. int add_to_branch_len;
  310. unsigned int hash_index; /*!< index to TM hash table; stored in core to avoid unnecessary calculations */
  311. unsigned int msg_flags; /*!< flags used by core */
  312. /* allows to set various flags on the message; may be used for
  313. * simple inter-module communication or remembering processing state
  314. * reached
  315. */
  316. flag_t flags;
  317. str set_global_address;
  318. str set_global_port;
  319. struct socket_info* force_send_socket; /* force sending on this socket,
  320. if ser */
  321. str path_vec;
  322. } sip_msg_t;
  323. /*! \brief pointer to a fakes message which was never received ;
  324. (when this message is "relayed", it is generated out
  325. of the original request)
  326. */
  327. #define FAKED_REPLY ((struct sip_msg *) -1)
  328. extern int via_cnt;
  329. int parse_msg(char* buf, unsigned int len, struct sip_msg* msg);
  330. int parse_headers(struct sip_msg* msg, hdr_flags_t flags, int next);
  331. char* get_hdr_field(char* buf, char* end, struct hdr_field* hdr);
  332. void free_sip_msg(struct sip_msg* msg);
  333. /*! \brief make sure all HFs needed for transaction identification have been
  334. parsed; return 0 if those HFs can't be found
  335. */
  336. inline static int check_transaction_quadruple( struct sip_msg* msg )
  337. {
  338. if ( parse_headers(msg, HDR_FROM_F|HDR_TO_F|HDR_CALLID_F|HDR_CSEQ_F,0)!=-1
  339. && msg->from && msg->to && msg->callid && msg->cseq ) {
  340. return 1;
  341. } else {
  342. ser_error=E_BAD_TUPEL;
  343. return 0;
  344. }
  345. }
  346. /*! \brief calculate characteristic value of a message -- this value
  347. is used to identify a transaction during the process of
  348. reply matching
  349. */
  350. inline static int char_msg_val( struct sip_msg *msg, char *cv )
  351. {
  352. str src[8];
  353. if (!check_transaction_quadruple(msg)) {
  354. LOG(L_ERR, "ERROR: can't calculate char_value due "
  355. "to a parsing error\n");
  356. memset( cv, '0', MD5_LEN );
  357. return 0;
  358. }
  359. src[0]= msg->from->body;
  360. src[1]= msg->to->body;
  361. src[2]= msg->callid->body;
  362. src[3]= msg->first_line.u.request.uri;
  363. src[4]= get_cseq( msg )->number;
  364. /* topmost Via is part of transaction key as well ! */
  365. src[5]= msg->via1->host;
  366. src[6]= msg->via1->port_str;
  367. if (msg->via1->branch) {
  368. src[7]= msg->via1->branch->value;
  369. MD5StringArray ( cv, src, 8 );
  370. } else {
  371. MD5StringArray( cv, src, 7 );
  372. }
  373. return 1;
  374. }
  375. /*! \brief returns a pointer to the begining of the msg's body
  376. */
  377. inline static char* get_body(struct sip_msg *msg)
  378. {
  379. int offset;
  380. unsigned int len;
  381. if ( parse_headers(msg, HDR_EOH_F, 0)==-1 )
  382. return 0;
  383. if (msg->unparsed){
  384. len=(unsigned int)(msg->unparsed-msg->buf);
  385. }else return 0;
  386. if ((len+2<=msg->len) && (strncmp(CRLF,msg->unparsed,CRLF_LEN)==0) )
  387. offset = CRLF_LEN;
  388. else if ( (len+1<=msg->len) &&
  389. (*(msg->unparsed)=='\n' || *(msg->unparsed)=='\r' ) )
  390. offset = 1;
  391. else
  392. return 0;
  393. return msg->unparsed + offset;
  394. }
  395. /*! \brief
  396. * Make a private copy of the string and assign it to dst_uri
  397. */
  398. int set_dst_uri(struct sip_msg* msg, str* uri);
  399. /*! \brief If the dst_uri is set to an URI then reset it */
  400. void reset_dst_uri(struct sip_msg* msg);
  401. struct hdr_field* get_hdr(struct sip_msg *msg, enum _hdr_types_t ht);
  402. struct hdr_field* next_sibling_hdr(struct hdr_field *hf);
  403. int set_path_vector(struct sip_msg* msg, str* path);
  404. void reset_path_vector(struct sip_msg* msg);
  405. /** force a specific send socket for forwarding a request.
  406. * @param msg - sip msg.
  407. * @param fsocket - forced socket, pointer to struct socket_info, can be 0 (in
  408. * which case it's equivalent to reset_force_socket()).
  409. */
  410. #define set_force_socket(msg, fsocket) \
  411. do { \
  412. (msg)->force_send_socket=(fsocket); \
  413. if ((msg)->force_send_socket) \
  414. (msg)->fwd_send_flags |= SND_F_FORCE_SOCKET; \
  415. else \
  416. (msg)->fwd_send_flags &= ~SND_F_FORCE_SOCKET; \
  417. } while (0)
  418. /** reset a previously forced send socket. */
  419. #define reset_force_socket(msg) set_force_socket(msg, 0)
  420. #endif