msg_parser.h 9.5 KB

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