msg_parser.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. #ifndef msg_parser_h
  48. #define msg_parser_h
  49. #include "../comp_defs.h"
  50. #include "../str.h"
  51. #include "../lump_struct.h"
  52. #include "../flags.h"
  53. #include "../ip_addr.h"
  54. #include "../md5utils.h"
  55. #include "../config.h"
  56. #include "parse_def.h"
  57. #include "parse_cseq.h"
  58. #include "parse_to.h"
  59. #include "parse_via.h"
  60. #include "parse_fline.h"
  61. #include "parse_retry_after.h"
  62. #include "hf.h"
  63. #include "../error.h"
  64. /* convenience short-cut macros */
  65. #define REQ_LINE(_msg) ((_msg)->first_line.u.request)
  66. #define REQ_METHOD first_line.u.request.method_value
  67. #define REPLY_STATUS first_line.u.reply.statuscode
  68. #define REPLY_CLASS(_reply) ((_reply)->REPLY_STATUS/100)
  69. /* start of "actual" sip msg (start of first line) */
  70. #define SIP_MSG_START(m) ((m)->first_line.u.request.method.s)
  71. /* number methods as power of two to allow bitmap matching */
  72. enum request_method { METHOD_UNDEF=0, METHOD_INVITE=1, METHOD_CANCEL=2, METHOD_ACK=4,
  73. METHOD_BYE=8, METHOD_INFO=16, METHOD_REGISTER=32, METHOD_SUBSCRIBE=64,
  74. METHOD_NOTIFY=128, METHOD_OTHER=256 };
  75. #define FL_FORCE_RPORT 1 /* force rport */
  76. #define FL_FORCE_ACTIVE 2 /* force active SDP */
  77. #define FL_SDP_IP_AFS 4 /* SDP IP rewritten */
  78. #define FL_SDP_PORT_AFS 8 /* SDP port rewritten */
  79. #define FL_SHM_CLONE 16 /* msg cloned in SHM as a single chunk */
  80. #define FL_TIMEOUT 32 /* message belongs to an "expired" branch
  81. (for failure route use) */
  82. #define FL_REPLIED 64 /* message branch received at least one reply
  83. (for failure route use) */
  84. #define FL_HASH_INDEX 128 /* msg->hash_index contains a valid value (tm use)*/
  85. #define FL_MTU_TCP_FB 256
  86. #define FL_MTU_TLS_FB 512
  87. #define FL_MTU_SCTP_FB 1024
  88. #define FL_MTU_FB_MASK (FL_MTU_TCP_FB|FL_MTU_TLS_FB|FL_MTU_SCTP_FB)
  89. #define IFISMETHOD(methodname,firstchar) \
  90. if ( (*tmp==(firstchar) || *tmp==((firstchar) | 32)) && \
  91. strncasecmp( tmp+1, #methodname +1, methodname##_LEN-1)==0 && \
  92. *(tmp+methodname##_LEN)==' ') { \
  93. fl->type=SIP_REQUEST; \
  94. fl->u.request.method.len=methodname##_LEN; \
  95. fl->u.request.method_value=METHOD_##methodname; \
  96. tmp=buffer+methodname##_LEN; \
  97. }
  98. #define IS_HTTP(req) \
  99. ((req)->first_line.u.request.version.len >= HTTP_VERSION_LEN && \
  100. !strncasecmp((req)->first_line.u.request.version.s, \
  101. HTTP_VERSION, HTTP_VERSION_LEN))
  102. #define IS_SIP(req) \
  103. ((req)->first_line.u.request.version.len >= SIP_VERSION_LEN && \
  104. !strncasecmp((req)->first_line.u.request.version.s, \
  105. SIP_VERSION, SIP_VERSION_LEN))
  106. /*
  107. * Return a URI to which the message should be really sent (not what should
  108. * be in the Request URI. The following fields are tried in this order:
  109. * 1) dst_uri
  110. * 2) new_uri
  111. * 3) first_line.u.request.uri
  112. */
  113. #define GET_NEXT_HOP(m) \
  114. (((m)->dst_uri.s && (m)->dst_uri.len) ? (&(m)->dst_uri) : \
  115. (((m)->new_uri.s && (m)->new_uri.len) ? (&(m)->new_uri) : (&(m)->first_line.u.request.uri)))
  116. /*
  117. * Return the Reqeust URI of a message.
  118. * The following fields are tried in this order:
  119. * 1) new_uri
  120. * 2) first_line.u.request.uri
  121. */
  122. #define GET_RURI(m) \
  123. (((m)->new_uri.s && (m)->new_uri.len) ? (&(m)->new_uri) : (&(m)->first_line.u.request.uri))
  124. #if 0
  125. /* old version */
  126. struct sip_uri {
  127. str user; /* Username */
  128. str passwd; /* Password */
  129. str host; /* Host name */
  130. str port; /* Port number */
  131. str params; /* Parameters */
  132. str headers;
  133. unsigned short port_no;
  134. };
  135. #endif
  136. enum _uri_type{ERROR_URI_T=0, SIP_URI_T, SIPS_URI_T, TEL_URI_T, TELS_URI_T};
  137. typedef enum _uri_type uri_type;
  138. enum _uri_flags{
  139. URI_USER_NORMALIZE=1,
  140. URI_SIP_USER_PHONE=2
  141. }; /* bit fields */
  142. typedef enum _uri_flags uri_flags;
  143. struct sip_uri {
  144. str user; /* Username */
  145. str passwd; /* Password */
  146. str host; /* Host name */
  147. str port; /* Port number */
  148. str params; /* Parameters */
  149. str headers;
  150. unsigned short port_no;
  151. unsigned short proto; /* from transport */
  152. uri_type type; /* uri scheme */
  153. uri_flags flags;
  154. /* parameters */
  155. str transport;
  156. str ttl;
  157. str user_param;
  158. str maddr;
  159. str method;
  160. str lr;
  161. str r2; /* ser specific rr parameter */
  162. /* values */
  163. str transport_val;
  164. str ttl_val;
  165. str user_param_val;
  166. str maddr_val;
  167. str method_val;
  168. str lr_val; /* lr value placeholder for lr=on a.s.o*/
  169. str r2_val;
  170. #ifdef USE_COMP
  171. unsigned short comp;
  172. #endif
  173. };
  174. typedef struct sip_msg {
  175. unsigned int id; /* message id, unique/process*/
  176. struct msg_start first_line; /* Message first line */
  177. struct via_body* via1; /* The first via */
  178. struct via_body* via2; /* The second via */
  179. struct hdr_field* headers; /* All the parsed headers*/
  180. struct hdr_field* last_header; /* Pointer to the last parsed header*/
  181. hdr_flags_t parsed_flag; /* Already parsed header field types */
  182. /* Via, To, CSeq, Call-Id, From, end of header*/
  183. /* pointers to the first occurrences of these headers;
  184. * everything is also saved in 'headers'
  185. * (WARNING: do not deallocate them twice!)*/
  186. struct hdr_field* h_via1;
  187. struct hdr_field* h_via2;
  188. struct hdr_field* callid;
  189. struct hdr_field* to;
  190. struct hdr_field* cseq;
  191. struct hdr_field* from;
  192. struct hdr_field* contact;
  193. struct hdr_field* maxforwards;
  194. struct hdr_field* route;
  195. struct hdr_field* record_route;
  196. struct hdr_field* content_type;
  197. struct hdr_field* content_length;
  198. struct hdr_field* authorization;
  199. struct hdr_field* expires;
  200. struct hdr_field* proxy_auth;
  201. struct hdr_field* supported;
  202. struct hdr_field* require;
  203. struct hdr_field* proxy_require;
  204. struct hdr_field* unsupported;
  205. struct hdr_field* allow;
  206. struct hdr_field* event;
  207. struct hdr_field* accept;
  208. struct hdr_field* accept_language;
  209. struct hdr_field* organization;
  210. struct hdr_field* priority;
  211. struct hdr_field* subject;
  212. struct hdr_field* user_agent;
  213. struct hdr_field* server;
  214. struct hdr_field* content_disposition;
  215. struct hdr_field* accept_disposition;
  216. struct hdr_field* diversion;
  217. struct hdr_field* rpid;
  218. struct hdr_field* refer_to;
  219. struct hdr_field* session_expires;
  220. struct hdr_field* min_se;
  221. struct hdr_field* sipifmatch;
  222. struct hdr_field* subscription_state;
  223. struct hdr_field* date;
  224. struct hdr_field* identity;
  225. struct hdr_field* identity_info;
  226. char* eoh; /* pointer to the end of header (if found) or null */
  227. char* unparsed; /* here we stopped parsing*/
  228. struct receive_info rcv; /* source & dest ip, ports, proto a.s.o*/
  229. char* buf; /* scratch pad, holds a modified message,
  230. * via, etc. point into it */
  231. unsigned int len; /* message len (orig) */
  232. /* modifications */
  233. str new_uri; /* changed first line uri, when you change this
  234. don't forget to set parsed_uri_ok to 0*/
  235. str dst_uri; /* Destination URI, must be forwarded to this URI if len != 0 */
  236. /* current uri */
  237. int parsed_uri_ok; /* 1 if parsed_uri is valid, 0 if not, set if to 0
  238. if you modify the uri (e.g change new_uri)*/
  239. struct sip_uri parsed_uri; /* speed-up > keep here the parsed uri*/
  240. /* the same for original uri */
  241. int parsed_orig_ruri_ok;
  242. struct sip_uri parsed_orig_ruri;
  243. struct lump* add_rm; /* used for all the forwarded requests/replies */
  244. struct lump* body_lumps; /* Lumps that update Content-Length */
  245. struct lump_rpl *reply_lump; /* only for localy generated replies !!!*/
  246. /* str add_to_branch;
  247. whatever whoever want to append to branch comes here
  248. */
  249. char add_to_branch_s[MAX_BRANCH_PARAM_LEN];
  250. int add_to_branch_len;
  251. /* index to TM hash table; stored in core to avoid unnecessary calculations */
  252. unsigned int hash_index;
  253. unsigned int msg_flags; /* flags used by core */
  254. /* allows to set various flags on the message; may be used for
  255. * simple inter-module communication or remembering processing state
  256. * reached
  257. */
  258. flag_t flags;
  259. str set_global_address;
  260. str set_global_port;
  261. struct socket_info* force_send_socket; /* force sending on this socket,
  262. if ser */
  263. } sip_msg_t;
  264. /* pointer to a fakes message which was never received ;
  265. (when this message is "relayed", it is generated out
  266. of the original request)
  267. */
  268. #define FAKED_REPLY ((struct sip_msg *) -1)
  269. extern int via_cnt;
  270. int parse_msg(char* buf, unsigned int len, struct sip_msg* msg);
  271. int parse_headers(struct sip_msg* msg, hdr_flags_t flags, int next);
  272. char* get_hdr_field(char* buf, char* end, struct hdr_field* hdr);
  273. void free_sip_msg(struct sip_msg* msg);
  274. /* make sure all HFs needed for transaction identification have been
  275. parsed; return 0 if those HFs can't be found
  276. */
  277. /* make sure all HFs needed for transaction identification have been
  278. parsed; return 0 if those HFs can't be found
  279. */
  280. inline static int check_transaction_quadruple( struct sip_msg* msg )
  281. {
  282. if ( parse_headers(msg, HDR_FROM_F|HDR_TO_F|HDR_CALLID_F|HDR_CSEQ_F,0)!=-1
  283. && msg->from && msg->to && msg->callid && msg->cseq ) {
  284. return 1;
  285. } else {
  286. ser_error=E_BAD_TUPEL;
  287. return 0;
  288. }
  289. }
  290. /* calculate characteristic value of a message -- this value
  291. is used to identify a transaction during the process of
  292. reply matching
  293. */
  294. inline static int char_msg_val( struct sip_msg *msg, char *cv )
  295. {
  296. str src[8];
  297. if (!check_transaction_quadruple(msg)) {
  298. LOG(L_ERR, "ERROR: can't calculate char_value due "
  299. "to a parsing error\n");
  300. memset( cv, '0', MD5_LEN );
  301. return 0;
  302. }
  303. src[0]= msg->from->body;
  304. src[1]= msg->to->body;
  305. src[2]= msg->callid->body;
  306. src[3]= msg->first_line.u.request.uri;
  307. src[4]= get_cseq( msg )->number;
  308. /* topmost Via is part of transaction key as well ! */
  309. src[5]= msg->via1->host;
  310. src[6]= msg->via1->port_str;
  311. if (msg->via1->branch) {
  312. src[7]= msg->via1->branch->value;
  313. MDStringArray ( cv, src, 8 );
  314. } else {
  315. MDStringArray( cv, src, 7 );
  316. }
  317. return 1;
  318. }
  319. /* returns a pointer to the begining of the msg's body
  320. */
  321. inline static char* get_body(struct sip_msg *msg)
  322. {
  323. int offset;
  324. unsigned int len;
  325. if ( parse_headers(msg, HDR_EOH_F, 0)==-1 )
  326. return 0;
  327. if (msg->unparsed){
  328. len=(unsigned int)(msg->unparsed-msg->buf);
  329. }else return 0;
  330. if ((len+2<=msg->len) && (strncmp(CRLF,msg->unparsed,CRLF_LEN)==0) )
  331. offset = CRLF_LEN;
  332. else if ( (len+1<=msg->len) &&
  333. (*(msg->unparsed)=='\n' || *(msg->unparsed)=='\r' ) )
  334. offset = 1;
  335. else
  336. return 0;
  337. return msg->unparsed + offset;
  338. }
  339. /*
  340. * Make a private copy of the string and assign it to dst_uri
  341. */
  342. int set_dst_uri(struct sip_msg* msg, str* uri);
  343. #endif