2
0

msg_parser.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #ifndef msg_parser_h
  28. #define msg_parser_h
  29. #include "../str.h"
  30. #include "../data_lump.h"
  31. #include "../flags.h"
  32. #include "../ip_addr.h"
  33. #include "../md5utils.h"
  34. #include "../config.h"
  35. #include "parse_def.h"
  36. #include "parse_cseq.h"
  37. #include "parse_to.h"
  38. #include "parse_via.h"
  39. #include "parse_fline.h"
  40. #include "hf.h"
  41. /* convenience short-cut macros */
  42. #define REQ_LINE(_msg) ((_msg)->first_line.u.request)
  43. #define REQ_METHOD first_line.u.request.method_value
  44. #define REPLY_STATUS first_line.u.reply.statuscode
  45. #define REPLY_CLASS(_reply) ((_reply)->REPLY_STATUS/100)
  46. /* number methods as power of two to allow bitmap matching */
  47. enum request_method { METHOD_INVITE=1, METHOD_CANCEL=2, METHOD_ACK=4,
  48. METHOD_BYE=8, METHOD_OTHER=16 };
  49. #define IFISMETHOD(methodname,firstchar) \
  50. if ( (*tmp==(firstchar) || *tmp==((firstchar) | 32)) && \
  51. strncasecmp( tmp+1, #methodname +1, methodname##_LEN-1)==0 && \
  52. *(tmp+methodname##_LEN)==' ') { \
  53. fl->type=SIP_REQUEST; \
  54. fl->u.request.method.len=methodname##_LEN; \
  55. fl->u.request.method_value=METHOD_##methodname; \
  56. tmp=buffer+methodname##_LEN; \
  57. }
  58. struct sip_uri {
  59. str user; /* Username */
  60. str passwd; /* Password */
  61. str host; /* Host name */
  62. str port; /* Port number */
  63. str params; /* Parameters */
  64. str headers;
  65. unsigned short port_no;
  66. };
  67. struct sip_msg {
  68. unsigned int id; /* message id, unique/process*/
  69. struct msg_start first_line; /* Message first line */
  70. struct via_body* via1; /* The first via */
  71. struct via_body* via2; /* The second via */
  72. struct hdr_field* headers; /* All the parsed headers*/
  73. struct hdr_field* last_header; /* Pointer to the last parsed header*/
  74. int parsed_flag; /* Already parsed header field types */
  75. /* Via, To, CSeq, Call-Id, From, end of header*/
  76. /* pointers to the first occurances of these headers;
  77. * everything is also saved in 'headers'
  78. * (WARNING: do not deallocate them twice!)*/
  79. struct hdr_field* h_via1;
  80. struct hdr_field* h_via2;
  81. struct hdr_field* callid;
  82. struct hdr_field* to;
  83. struct hdr_field* cseq;
  84. struct hdr_field* from;
  85. struct hdr_field* contact;
  86. struct hdr_field* maxforwards;
  87. struct hdr_field* route;
  88. struct hdr_field* record_route;
  89. struct hdr_field* content_type;
  90. struct hdr_field* content_length;
  91. struct hdr_field* authorization;
  92. struct hdr_field* expires;
  93. struct hdr_field* proxy_auth;
  94. struct hdr_field* www_auth;
  95. struct hdr_field* supported;
  96. struct hdr_field* require;
  97. struct hdr_field* proxy_require;
  98. struct hdr_field* unsupported;
  99. struct hdr_field* allow;
  100. struct hdr_field* event;
  101. char* eoh; /* pointer to the end of header (if found) or null */
  102. char* unparsed; /* here we stopped parsing*/
  103. struct receive_info rcv; /* source & dest ip, ports, proto a.s.o*/
  104. char* orig; /* original message copy */
  105. char* buf; /* scratch pad, holds a modfied message,
  106. * via, etc. point into it */
  107. unsigned int len; /* message len (orig) */
  108. /* modifications */
  109. str new_uri; /* changed first line uri*/
  110. str dst_uri; /* Destination URI, must be forwarded to this URI if len != 0 */
  111. int parsed_uri_ok; /* 1 if parsed_uri is valid, 0 if not */
  112. struct sip_uri parsed_uri; /* speed-up > keep here the parsed uri*/
  113. struct lump* add_rm; /* used for all the forwarded requests */
  114. struct lump* repl_add_rm; /* used for all the forwarded replies */
  115. struct lump_rpl *reply_lump; /* only for localy generated replies !!!*/
  116. /* str add_to_branch;
  117. whatever whoever want to append to branch comes here
  118. */
  119. char add_to_branch_s[MAX_BRANCH_PARAM_LEN];
  120. int add_to_branch_len;
  121. /* index to TM hash table; stored in core to avoid unnecessary calcs */
  122. unsigned int hash_index;
  123. /* allows to set various flags on the message; may be used for
  124. * simple inter-module communication or remembering processing state
  125. * reached
  126. */
  127. flag_t flags;
  128. };
  129. /* pointer to a fakes message which was never received ;
  130. (when this message is "relayed", it is generated out
  131. of the original request)
  132. */
  133. #define FAKED_REPLY ((struct sip_msg *) -1)
  134. extern int via_cnt;
  135. int parse_msg(char* buf, unsigned int len, struct sip_msg* msg);
  136. int parse_headers(struct sip_msg* msg, int flags, int next);
  137. void free_sip_msg(struct sip_msg* msg);
  138. /* make sure all HFs needed for transaction identification have been
  139. parsed; return 0 if those HFs can't be found
  140. */
  141. int check_transaction_quadruple( struct sip_msg* msg );
  142. /* calculate characteristic value of a message -- this value
  143. is used to identify a transaction during the process of
  144. reply matching
  145. */
  146. inline static int char_msg_val( struct sip_msg *msg, char *cv )
  147. {
  148. str src[8];
  149. if (!check_transaction_quadruple(msg)) {
  150. LOG(L_ERR, "ERROR: can't calculate char_value due "
  151. "to a parsing error\n");
  152. memset( cv, '0', MD5_LEN );
  153. return 0;
  154. }
  155. src[0]= msg->from->body;
  156. src[1]= msg->to->body;
  157. src[2]= msg->callid->body;
  158. src[3]= msg->first_line.u.request.uri;
  159. src[4]= get_cseq( msg )->number;
  160. /* topmost Via is part of transaction key as well ! */
  161. src[5]= msg->via1->host;
  162. src[6]= msg->via1->port_str;
  163. if (msg->via1->branch) {
  164. src[7]= msg->via1->branch->value;
  165. MDStringArray ( cv, src, 8 );
  166. } else {
  167. MDStringArray( cv, src, 7 );
  168. }
  169. return 1;
  170. }
  171. /* returns a pointer to the begining of the msg's body
  172. */
  173. inline static char* get_body(struct sip_msg *msg)
  174. {
  175. int offset;
  176. int len;
  177. if ( parse_headers(msg,HDR_EOH, 0)==-1 )
  178. return 0;
  179. if (msg->unparsed){
  180. len=(int)(msg->unparsed-msg->buf);
  181. }else return 0;
  182. if ((len+2<=msg->len) && (strncmp(CRLF,msg->unparsed,CRLF_LEN)==0) )
  183. offset = CRLF_LEN;
  184. else if ( (len+1<=msg->len) &&
  185. (*(msg->unparsed)=='\n' || *(msg->unparsed)=='\r' ) )
  186. offset = 1;
  187. else
  188. return 0;
  189. return msg->unparsed + offset;
  190. }
  191. #endif