receive.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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-02-28 scratchpad compatibility abandoned (jiri)
  30. * 2003-01-29 transport-independent message zero-termination in
  31. * receive_msg (jiri)
  32. * 2003-02-07 undoed jiri's zero term. changes (they break tcp) (andrei)
  33. * 2003-02-10 moved zero-term in the calling functions (udp_receive &
  34. * tcp_read_req)
  35. * 2003-08-13 fixed exec_pre_cb returning 0 (backported from stable) (andrei)
  36. * 2004-02-06 added user preferences support - destroy_avps() (bogdan)
  37. * 2004-04-30 exec_pre_cb is called after basic sanity checks (at least one
  38. * via present & parsed ok) (andrei)
  39. * 2004-08-23 avp core changed - destroy_avp-> reset_avps (bogdan)
  40. * 2006-11-29 nonsip_msg hooks called for non-sip msg (e.g HTTP) (andrei)
  41. */
  42. #include <string.h>
  43. #include <stdlib.h>
  44. #include <sys/time.h>
  45. #include "receive.h"
  46. #include "globals.h"
  47. #include "dprint.h"
  48. #include "route.h"
  49. #include "parser/msg_parser.h"
  50. #include "forward.h"
  51. #include "action.h"
  52. #include "mem/mem.h"
  53. #include "stats.h"
  54. #include "ip_addr.h"
  55. #include "script_cb.h"
  56. #include "nonsip_hooks.h"
  57. #include "dset.h"
  58. #include "usr_avp.h"
  59. #include "select_buf.h"
  60. #include "tcp_server.h" /* for tcpconn_add_alias */
  61. #include "tcp_options.h" /* for access to tcp_accept_aliases*/
  62. #include "cfg/cfg.h"
  63. #ifdef DEBUG_DMALLOC
  64. #include <mem/dmalloc.h>
  65. #endif
  66. unsigned int msg_no=0;
  67. /* address preset vars */
  68. str default_global_address={0,0};
  69. str default_global_port={0,0};
  70. str default_via_address={0,0};
  71. str default_via_port={0,0};
  72. /* WARNING: buf must be 0 terminated (buf[len]=0) or some things might
  73. * break (e.g.: modules/textops)
  74. */
  75. int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
  76. {
  77. struct sip_msg* msg;
  78. int ret;
  79. struct run_act_ctx ra_ctx;
  80. #ifdef STATS
  81. int skipped = 1;
  82. struct timeval tvb, tve;
  83. struct timezone tz;
  84. unsigned int diff;
  85. #endif
  86. msg=pkg_malloc(sizeof(struct sip_msg));
  87. if (msg==0) {
  88. LOG(L_ERR, "ERROR: receive_msg: no mem for sip_msg\n");
  89. goto error00;
  90. }
  91. msg_no++;
  92. /* number of vias parsed -- good for diagnostic info in replies */
  93. via_cnt=0;
  94. memset(msg,0, sizeof(struct sip_msg)); /* init everything to 0 */
  95. /* fill in msg */
  96. msg->buf=buf;
  97. msg->len=len;
  98. /* zero termination (termination of orig message bellow not that
  99. useful as most of the work is done with scratch-pad; -jiri */
  100. /* buf[len]=0; */ /* WARNING: zero term removed! */
  101. msg->rcv=*rcv_info;
  102. msg->id=msg_no;
  103. msg->set_global_address=default_global_address;
  104. msg->set_global_port=default_global_port;
  105. if (parse_msg(buf,len, msg)!=0){
  106. LOG(L_ERR, "ERROR: receive_msg: parse_msg failed\n");
  107. goto error02;
  108. }
  109. DBG("After parse_msg...\n");
  110. /* ... clear branches from previous message */
  111. clear_branches();
  112. reset_static_buffer();
  113. if (msg->first_line.type==SIP_REQUEST){
  114. if (!IS_SIP(msg)){
  115. if (nonsip_msg_run_hooks(msg)!=NONSIP_MSG_ACCEPT)
  116. goto end; /* drop the message */
  117. }
  118. /* sanity checks */
  119. if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
  120. /* no via, send back error ? */
  121. LOG(L_ERR, "ERROR: receive_msg: no via found in request\n");
  122. goto error02;
  123. }
  124. /* check if necessary to add receive?->moved to forward_req */
  125. /* check for the alias stuff */
  126. #ifdef USE_TCP
  127. if (msg->via1->alias && cfg_get(tcp, tcp_cfg, accept_aliases) &&
  128. (((rcv_info->proto==PROTO_TCP) && !tcp_disable)
  129. #ifdef USE_TLS
  130. || ((rcv_info->proto==PROTO_TLS) && !tls_disable)
  131. #endif
  132. )
  133. ){
  134. if (tcpconn_add_alias(rcv_info->proto_reserved1, msg->via1->port,
  135. rcv_info->proto)!=0){
  136. LOG(L_ERR, " ERROR: receive_msg: tcp alias failed\n");
  137. /* continue */
  138. }
  139. }
  140. #endif
  141. /* skip: */
  142. DBG("preparing to run routing scripts...\n");
  143. #ifdef STATS
  144. gettimeofday( & tvb, &tz );
  145. #endif
  146. /* execute pre-script callbacks, if any; -jiri */
  147. /* if some of the callbacks said not to continue with
  148. script processing, don't do so
  149. if we are here basic sanity checks are already done
  150. (like presence of at least one via), so you can count
  151. on via1 being parsed in a pre-script callback --andrei
  152. */
  153. if (exec_pre_req_cb(msg)==0 )
  154. goto end; /* drop the request */
  155. /* exec the routing script */
  156. init_run_actions_ctx(&ra_ctx);
  157. if (run_actions(&ra_ctx, main_rt.rlist[DEFAULT_RT], msg)<0){
  158. LOG(L_WARN, "WARNING: receive_msg: "
  159. "error while trying script\n");
  160. goto error_req;
  161. }
  162. #ifdef STATS
  163. gettimeofday( & tve, &tz );
  164. diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
  165. stats->processed_requests++;
  166. stats->acc_req_time += diff;
  167. DBG("successfully ran routing scripts...(%d usec)\n", diff);
  168. STATS_RX_REQUEST( msg->first_line.u.request.method_value );
  169. #endif
  170. /* execute post request-script callbacks */
  171. exec_post_req_cb(msg);
  172. }else if (msg->first_line.type==SIP_REPLY){
  173. /* sanity checks */
  174. if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
  175. /* no via, send back error ? */
  176. LOG(L_ERR, "ERROR: receive_msg: no via found in reply\n");
  177. goto error02;
  178. }
  179. #ifdef STATS
  180. gettimeofday( & tvb, &tz );
  181. STATS_RX_RESPONSE ( msg->first_line.u.reply.statuscode / 100 );
  182. #endif
  183. /* execute pre-script callbacks, if any; -jiri */
  184. /* if some of the callbacks said not to continue with
  185. script processing, don't do so
  186. if we are here basic sanity checks are already done
  187. (like presence of at least one via), so you can count
  188. on via1 being parsed in a pre-script callback --andrei
  189. */
  190. if (exec_pre_rpl_cb(msg)==0 )
  191. goto end; /* drop the request */
  192. /* exec the onreply routing script */
  193. if (onreply_rt.rlist[DEFAULT_RT]){
  194. init_run_actions_ctx(&ra_ctx);
  195. ret=run_actions(&ra_ctx, onreply_rt.rlist[DEFAULT_RT], msg);
  196. if (ret<0){
  197. LOG(L_WARN, "WARNING: receive_msg: "
  198. "error while trying onreply script\n");
  199. goto error_rpl;
  200. }else if (ret==0) goto skip_send_reply; /* drop the message,
  201. no error */
  202. }
  203. /* send the msg */
  204. forward_reply(msg);
  205. skip_send_reply:
  206. #ifdef STATS
  207. gettimeofday( & tve, &tz );
  208. diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
  209. stats->processed_responses++;
  210. stats->acc_res_time+=diff;
  211. DBG("successfully ran reply processing...(%d usec)\n", diff);
  212. #endif
  213. /* execute post reply-script callbacks */
  214. exec_post_rpl_cb(msg);
  215. }
  216. end:
  217. #ifdef STATS
  218. skipped = 0;
  219. #endif
  220. /* free possible loaded avps -bogdan */
  221. reset_avps();
  222. DBG("receive_msg: cleaning up\n");
  223. free_sip_msg(msg);
  224. pkg_free(msg);
  225. #ifdef STATS
  226. if (skipped) STATS_RX_DROPS;
  227. #endif
  228. return 0;
  229. error_rpl:
  230. /* execute post reply-script callbacks */
  231. exec_post_rpl_cb(msg);
  232. reset_avps();
  233. goto error02;
  234. error_req:
  235. DBG("receive_msg: error:...\n");
  236. /* execute post request-script callbacks */
  237. exec_post_req_cb(msg);
  238. /* free possible loaded avps -bogdan */
  239. reset_avps();
  240. error02:
  241. free_sip_msg(msg);
  242. pkg_free(msg);
  243. error00:
  244. STATS_RX_DROPS;
  245. return -1;
  246. }