msg_parser.h 12 KB

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