msg_parser.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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, /*!< 4 - 2^2 */
  83. METHOD_BYE=8, /*!< 8 - 2^3 */
  84. METHOD_INFO=16, /*!< 16 - 2^4 */
  85. METHOD_REGISTER=32, /*!< 32 - 2^5 */
  86. METHOD_SUBSCRIBE=64, /*!< 64 - 2^6 */
  87. METHOD_NOTIFY=128, /*!< 128 - 2^7 */
  88. METHOD_MESSAGE=256, /*!< 256 - 2^8 */
  89. METHOD_OPTIONS=512, /*!< 512 - 2^9 */
  90. METHOD_PRACK=1024, /*!< 1024 - 2^10 */
  91. METHOD_UPDATE=2048, /*!< 2048 - 2^11 */
  92. METHOD_REFER=4096, /*!< 4096 - 2^12 */
  93. METHOD_PUBLISH=8192, /*!< 8192 - 2^13 */
  94. METHOD_OTHER=16384 /*!< 16384 - 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. #define FL_SDP_BODY (1 << 12) /*!< msg has SDP in body */
  111. #define FL_USE_UAC_FROM (1<<13) /* take FROM hdr from UAC instead of UAS*/
  112. #define FL_USE_UAC_TO (1<<14) /* take TO hdr from UAC instead of UAS */
  113. /* WARNING: Value (1 << 28) is temporarily reserved for use in kamailio call_control
  114. * module (flag FL_USE_CALL_CONTROL )! */
  115. /* WARNING: Value (1 << 29) is temporarily reserved for use in kamailio acc
  116. * module (flag FL_REQ_UPSTREAM)! */
  117. /* WARNING: Value (1 << 30) is temporarily reserved for use in kamailio
  118. * media proxy module (flag FL_USE_MEDIA_PROXY)! */
  119. /* WARNING: Value (1 << 31) is temporarily reserved for use in kamailio
  120. * nat_traversal module (flag FL_DO_KEEPALIVE)! */
  121. #define FL_MTU_FB_MASK (FL_MTU_TCP_FB|FL_MTU_TLS_FB|FL_MTU_SCTP_FB)
  122. #define IFISMETHOD(methodname,firstchar) \
  123. if ( (*tmp==(firstchar) || *tmp==((firstchar) | 32)) && \
  124. strncasecmp( tmp+1, #methodname +1, methodname##_LEN-1)==0 && \
  125. *(tmp+methodname##_LEN)==' ') { \
  126. fl->type=SIP_REQUEST; \
  127. fl->u.request.method.len=methodname##_LEN; \
  128. fl->u.request.method_value=METHOD_##methodname; \
  129. tmp=buffer+methodname##_LEN; \
  130. }
  131. #define IS_HTTP(req) \
  132. ((req)->first_line.u.request.version.len >= HTTP_VERSION_LEN && \
  133. !strncasecmp((req)->first_line.u.request.version.s, \
  134. HTTP_VERSION, HTTP_VERSION_LEN))
  135. #define IS_SIP(req) \
  136. ((req)->first_line.u.request.version.len >= SIP_VERSION_LEN && \
  137. !strncasecmp((req)->first_line.u.request.version.s, \
  138. SIP_VERSION, SIP_VERSION_LEN))
  139. /*! \brief
  140. * Return a URI to which the message should be really sent (not what should
  141. * be in the Request URI. The following fields are tried in this order:
  142. * 1) dst_uri
  143. * 2) new_uri
  144. * 3) first_line.u.request.uri
  145. */
  146. #define GET_NEXT_HOP(m) \
  147. (((m)->dst_uri.s && (m)->dst_uri.len) ? (&(m)->dst_uri) : \
  148. (((m)->new_uri.s && (m)->new_uri.len) ? (&(m)->new_uri) : (&(m)->first_line.u.request.uri)))
  149. /*! \brief
  150. * Return the Reqeust URI of a message.
  151. * The following fields are tried in this order:
  152. * 1) new_uri
  153. * 2) first_line.u.request.uri
  154. */
  155. #define GET_RURI(m) \
  156. (((m)->new_uri.s && (m)->new_uri.len) ? (&(m)->new_uri) : (&(m)->first_line.u.request.uri))
  157. enum _uri_type{ERROR_URI_T=0, SIP_URI_T, SIPS_URI_T, TEL_URI_T, TELS_URI_T, URN_URI_T};
  158. typedef enum _uri_type uri_type;
  159. enum _uri_flags{
  160. URI_USER_NORMALIZE=1,
  161. URI_SIP_USER_PHONE=2
  162. }; /* bit fields */
  163. typedef enum _uri_flags uri_flags;
  164. /*! \brief The SIP uri object */
  165. struct sip_uri {
  166. str user; /*!< Username */
  167. str passwd; /*!< Password */
  168. str host; /*!< Host name */
  169. str port; /*!< Port number */
  170. str params; /*!< Parameters */
  171. str sip_params; /*!< Parameters of the sip: URI.
  172. * (If a tel: URI is embedded in a sip: URI, then
  173. * params points to the parameters of the tel: URI,
  174. * and sip_params to the parameters of the sip: URI.
  175. */
  176. str headers;
  177. unsigned short port_no;
  178. unsigned short proto; /*!< from transport */
  179. uri_type type; /*!< uri scheme */
  180. uri_flags flags;
  181. /*!< parameters */
  182. str transport;
  183. str ttl;
  184. str user_param;
  185. str maddr;
  186. str method;
  187. str lr;
  188. str r2; /*!< ser specific rr parameter */
  189. str gr;
  190. str transport_val; /*!< transport value */
  191. str ttl_val; /*!< TTL value */
  192. str user_param_val; /*!< User= param value */
  193. str maddr_val; /*!< Maddr= param value */
  194. str method_val; /*!< Method value */
  195. str lr_val; /*!< lr value placeholder for lr=on a.s.o*/
  196. str r2_val;
  197. str gr_val;
  198. #ifdef USE_COMP
  199. unsigned short comp;
  200. #endif
  201. };
  202. typedef struct sip_uri sip_uri_t;
  203. struct msg_body;
  204. typedef void (*free_msg_body_f)(struct msg_body** ptr);
  205. typedef enum msg_body_type {
  206. MSG_BODY_UNKNOWN = 0,
  207. MSG_BODY_SDP
  208. } msg_body_type_t;
  209. /*! \brief This structure represents a generic SIP message body, regardless of the
  210. * body type.
  211. *
  212. * Body parsers are supposed to cast this structure to some other
  213. * body-type specific structure, but the body type specific structure must
  214. * retain msg_body_type variable and a pointer to the free function as the
  215. * first two variables within the structure.
  216. */
  217. typedef struct msg_body {
  218. msg_body_type_t type;
  219. free_msg_body_f free;
  220. } msg_body_t;
  221. /* pre-declaration, to include sys/time.h in .c */
  222. struct timeval;
  223. /*! \brief The SIP message */
  224. typedef struct sip_msg {
  225. unsigned int id; /*!< message id, unique/process*/
  226. int pid; /*!< process id */
  227. struct timeval tval; /*!< time value associated to message */
  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* diversion;
  270. struct hdr_field* rpid;
  271. struct hdr_field* refer_to;
  272. struct hdr_field* session_expires;
  273. struct hdr_field* min_se;
  274. struct hdr_field* sipifmatch;
  275. struct hdr_field* subscription_state;
  276. struct hdr_field* date;
  277. struct hdr_field* identity;
  278. struct hdr_field* identity_info;
  279. struct hdr_field* pai;
  280. struct hdr_field* ppi;
  281. struct hdr_field* path;
  282. struct hdr_field* privacy;
  283. struct msg_body* body;
  284. char* eoh; /*!< pointer to the end of header (if found) or null */
  285. char* unparsed; /*!< here we stopped parsing*/
  286. struct receive_info rcv; /*!< source & dest ip, ports, proto a.s.o*/
  287. char* buf; /*!< scratch pad, holds a modified message,
  288. * via, etc. point into it */
  289. unsigned int len; /*!< message len (orig) */
  290. /* modifications */
  291. str new_uri; /*!< changed first line uri, when you change this
  292. don't forget to set parsed_uri_ok to 0*/
  293. str dst_uri; /*!< Destination URI, must be forwarded to this URI if len != 0 */
  294. /* current uri */
  295. int parsed_uri_ok; /*!< 1 if parsed_uri is valid, 0 if not, set if to 0
  296. if you modify the uri (e.g change new_uri)*/
  297. struct sip_uri parsed_uri; /*!< speed-up > keep here the parsed uri*/
  298. int parsed_orig_ruri_ok; /*!< 1 if parsed_orig_uri is valid, 0 if not, set if to 0
  299. if you modify the uri (e.g change new_uri)*/
  300. struct sip_uri parsed_orig_ruri; /*!< speed-up > keep here the parsed orig uri*/
  301. struct lump* add_rm; /*!< used for all the forwarded requests/replies */
  302. struct lump* body_lumps; /*!< Lumps that update Content-Length */
  303. struct lump_rpl *reply_lump; /*!< only for localy generated replies !!!*/
  304. /*! \brief str add_to_branch;
  305. whatever whoever want to append to branch comes here
  306. */
  307. char add_to_branch_s[MAX_BRANCH_PARAM_LEN];
  308. int add_to_branch_len;
  309. unsigned int hash_index; /*!< index to TM hash table; stored in core to avoid unnecessary calculations */
  310. unsigned int msg_flags; /*!< flags used by core */
  311. /* allows to set various flags on the message; may be used for
  312. * simple inter-module communication or remembering processing state
  313. * reached
  314. */
  315. flag_t flags;
  316. str set_global_address;
  317. str set_global_port;
  318. struct socket_info* force_send_socket; /* force sending on this socket,
  319. if ser */
  320. str path_vec;
  321. } sip_msg_t;
  322. /*! \brief pointer to a fakes message which was never received ;
  323. (when this message is "relayed", it is generated out
  324. of the original request)
  325. */
  326. #define FAKED_REPLY ((struct sip_msg *) -1)
  327. extern int via_cnt;
  328. /** global request flags.
  329. * msg->msg_flags should be OR'ed with it before
  330. * a flag value is checked, e.g.:
  331. * if ((msg->msg_flags|global_req_flags) & FL_XXX) ...
  332. */
  333. extern unsigned int global_req_flags;
  334. int parse_msg(char* buf, unsigned int len, struct sip_msg* msg);
  335. int parse_headers(struct sip_msg* msg, hdr_flags_t flags, int next);
  336. char* get_hdr_field(char* buf, char* end, struct hdr_field* hdr);
  337. void free_sip_msg(struct sip_msg* msg);
  338. /*! \brief make sure all HFs needed for transaction identification have been
  339. parsed; return 0 if those HFs can't be found
  340. */
  341. inline static int check_transaction_quadruple( struct sip_msg* msg )
  342. {
  343. if ( parse_headers(msg, HDR_FROM_F|HDR_TO_F|HDR_CALLID_F|HDR_CSEQ_F,0)!=-1
  344. && msg->from && msg->to && msg->callid && msg->cseq ) {
  345. return 1;
  346. } else {
  347. ser_error=E_BAD_TUPEL;
  348. return 0;
  349. }
  350. }
  351. /*! \brief returns a pointer to the begining of the msg's body
  352. */
  353. inline static char* get_body(struct sip_msg *msg)
  354. {
  355. int offset;
  356. unsigned int len;
  357. if ( parse_headers(msg, HDR_EOH_F, 0)==-1 )
  358. return 0;
  359. if (msg->unparsed){
  360. len=(unsigned int)(msg->unparsed-msg->buf);
  361. }else return 0;
  362. if ((len+2<=msg->len) && (strncmp(CRLF,msg->unparsed,CRLF_LEN)==0) )
  363. offset = CRLF_LEN;
  364. else if ( (len+1<=msg->len) &&
  365. (*(msg->unparsed)=='\n' || *(msg->unparsed)=='\r' ) )
  366. offset = 1;
  367. else
  368. return 0;
  369. return msg->unparsed + offset;
  370. }
  371. /*! \brief
  372. * Make a private copy of the string and assign it to dst_uri
  373. */
  374. int set_dst_uri(struct sip_msg* msg, str* uri);
  375. /*! \brief If the dst_uri is set to an URI then reset it */
  376. void reset_dst_uri(struct sip_msg* msg);
  377. hdr_field_t* get_hdr(sip_msg_t *msg, enum _hdr_types_t ht);
  378. hdr_field_t* next_sibling_hdr(hdr_field_t *hf);
  379. hdr_field_t* get_hdr_by_name(sip_msg_t *msg, char *name, int name_len);
  380. hdr_field_t* next_sibling_hdr_by_name(hdr_field_t *hf);
  381. int set_path_vector(struct sip_msg* msg, str* path);
  382. void reset_path_vector(struct sip_msg* msg);
  383. /** force a specific send socket for forwarding a request.
  384. * @param msg - sip msg.
  385. * @param fsocket - forced socket, pointer to struct socket_info, can be 0 (in
  386. * which case it's equivalent to reset_force_socket()).
  387. */
  388. #define set_force_socket(msg, fsocket) \
  389. do { \
  390. (msg)->force_send_socket=(fsocket); \
  391. if ((msg)->force_send_socket) \
  392. (msg)->fwd_send_flags.f |= SND_F_FORCE_SOCKET; \
  393. else \
  394. (msg)->fwd_send_flags.f &= ~SND_F_FORCE_SOCKET; \
  395. } while (0)
  396. /** reset a previously forced send socket. */
  397. #define reset_force_socket(msg) set_force_socket(msg, 0)
  398. /**
  399. * struct to identify a msg context
  400. * - the pair of pid and message-id
  401. */
  402. typedef struct msg_ctx_id {
  403. int pid;
  404. int msgid;
  405. } msg_ctx_id_t;
  406. /**
  407. * set msg context id
  408. * - return: -1 on error; 0 - on set
  409. */
  410. int msg_ctx_id_set(sip_msg_t *msg, msg_ctx_id_t *mid);
  411. /**
  412. * check msg context id
  413. * - return: -1 on error; 0 - on no match; 1 - on match
  414. */
  415. int msg_ctx_id_match(sip_msg_t *msg, msg_ctx_id_t *mid);
  416. /**
  417. * set msg time value
  418. */
  419. int msg_set_time(sip_msg_t *msg);
  420. #endif