msg_parser.h 10 KB

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