receive.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. /*!
  43. * \file
  44. * \brief SIP-router core ::
  45. * \ingroup core
  46. * Module: \ref core
  47. */
  48. #include <string.h>
  49. #include <stdlib.h>
  50. #include <sys/time.h>
  51. #include "receive.h"
  52. #include "globals.h"
  53. #include "dprint.h"
  54. #include "route.h"
  55. #include "parser/msg_parser.h"
  56. #include "forward.h"
  57. #include "action.h"
  58. #include "mem/mem.h"
  59. #include "stats.h"
  60. #include "ip_addr.h"
  61. #include "script_cb.h"
  62. #include "nonsip_hooks.h"
  63. #include "dset.h"
  64. #include "usr_avp.h"
  65. #ifdef WITH_XAVP
  66. #include "xavp.h"
  67. #endif
  68. #include "select_buf.h"
  69. #include "tcp_server.h" /* for tcpconn_add_alias */
  70. #include "tcp_options.h" /* for access to tcp_accept_aliases*/
  71. #include "cfg/cfg.h"
  72. #include "core_stats.h"
  73. #ifdef DEBUG_DMALLOC
  74. #include <mem/dmalloc.h>
  75. #endif
  76. unsigned int msg_no=0;
  77. /* address preset vars */
  78. str default_global_address={0,0};
  79. str default_global_port={0,0};
  80. str default_via_address={0,0};
  81. str default_via_port={0,0};
  82. /**
  83. * increment msg_no and return the new value
  84. */
  85. unsigned int inc_msg_no(void)
  86. {
  87. return ++msg_no;
  88. }
  89. /* WARNING: buf must be 0 terminated (buf[len]=0) or some things might
  90. * break (e.g.: modules/textops)
  91. */
  92. int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
  93. {
  94. struct sip_msg* msg;
  95. struct run_act_ctx ctx;
  96. int ret;
  97. #ifdef STATS
  98. int skipped = 1;
  99. struct timeval tvb, tve;
  100. struct timezone tz;
  101. unsigned int diff;
  102. #endif
  103. str inb;
  104. inb.s = buf;
  105. inb.len = len;
  106. sr_event_exec(SREV_NET_DATA_IN, (void*)&inb);
  107. len = inb.len;
  108. msg=pkg_malloc(sizeof(struct sip_msg));
  109. if (msg==0) {
  110. LOG(L_ERR, "ERROR: receive_msg: no mem for sip_msg\n");
  111. goto error00;
  112. }
  113. msg_no++;
  114. /* number of vias parsed -- good for diagnostic info in replies */
  115. via_cnt=0;
  116. memset(msg,0, sizeof(struct sip_msg)); /* init everything to 0 */
  117. /* fill in msg */
  118. msg->buf=buf;
  119. msg->len=len;
  120. /* zero termination (termination of orig message bellow not that
  121. useful as most of the work is done with scratch-pad; -jiri */
  122. /* buf[len]=0; */ /* WARNING: zero term removed! */
  123. msg->rcv=*rcv_info;
  124. msg->id=msg_no;
  125. msg->pid=my_pid();
  126. msg->set_global_address=default_global_address;
  127. msg->set_global_port=default_global_port;
  128. if(likely(sr_msg_time==1)) msg_set_time(msg);
  129. if (parse_msg(buf,len, msg)!=0){
  130. LOG(cfg_get(core, core_cfg, corelog),
  131. "core parsing of SIP message failed (%s:%d/%d)\n",
  132. ip_addr2a(&msg->rcv.src_ip), (int)msg->rcv.src_port,
  133. (int)msg->rcv.proto);
  134. goto error02;
  135. }
  136. DBG("After parse_msg...\n");
  137. /* ... clear branches from previous message */
  138. clear_branches();
  139. if (msg->first_line.type==SIP_REQUEST){
  140. ruri_mark_new(); /* ruri is usable for forking (not consumed yet) */
  141. if (!IS_SIP(msg)){
  142. if ((ret=nonsip_msg_run_hooks(msg))!=NONSIP_MSG_ACCEPT){
  143. if (unlikely(ret==NONSIP_MSG_ERROR))
  144. goto error03;
  145. goto end; /* drop the message */
  146. }
  147. }
  148. /* sanity checks */
  149. if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
  150. /* no via, send back error ? */
  151. LOG(L_ERR, "ERROR: receive_msg: no via found in request\n");
  152. STATS_BAD_MSG();
  153. goto error02;
  154. }
  155. /* check if necessary to add receive?->moved to forward_req */
  156. /* check for the alias stuff */
  157. #ifdef USE_TCP
  158. if (msg->via1->alias && cfg_get(tcp, tcp_cfg, accept_aliases) &&
  159. (((rcv_info->proto==PROTO_TCP) && !tcp_disable)
  160. #ifdef USE_TLS
  161. || ((rcv_info->proto==PROTO_TLS) && !tls_disable)
  162. #endif
  163. )
  164. ){
  165. if (tcpconn_add_alias(rcv_info->proto_reserved1, msg->via1->port,
  166. rcv_info->proto)!=0){
  167. LOG(L_ERR, " ERROR: receive_msg: tcp alias failed\n");
  168. /* continue */
  169. }
  170. }
  171. #endif
  172. /* skip: */
  173. DBG("preparing to run routing scripts...\n");
  174. #ifdef STATS
  175. gettimeofday( & tvb, &tz );
  176. #endif
  177. /* execute pre-script callbacks, if any; -jiri */
  178. /* if some of the callbacks said not to continue with
  179. script processing, don't do so
  180. if we are here basic sanity checks are already done
  181. (like presence of at least one via), so you can count
  182. on via1 being parsed in a pre-script callback --andrei
  183. */
  184. if (exec_pre_script_cb(msg, REQUEST_CB_TYPE)==0 )
  185. {
  186. STATS_REQ_FWD_DROP();
  187. goto end; /* drop the request */
  188. }
  189. set_route_type(REQUEST_ROUTE);
  190. /* exec the routing script */
  191. if (run_top_route(main_rt.rlist[DEFAULT_RT], msg, 0)<0){
  192. LOG(L_WARN, "WARNING: receive_msg: "
  193. "error while trying script\n");
  194. goto error_req;
  195. }
  196. #ifdef STATS
  197. gettimeofday( & tve, &tz );
  198. diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
  199. stats->processed_requests++;
  200. stats->acc_req_time += diff;
  201. DBG("successfully ran routing scripts...(%d usec)\n", diff);
  202. STATS_RX_REQUEST( msg->first_line.u.request.method_value );
  203. #endif
  204. /* execute post request-script callbacks */
  205. exec_post_script_cb(msg, REQUEST_CB_TYPE);
  206. }else if (msg->first_line.type==SIP_REPLY){
  207. /* sanity checks */
  208. if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
  209. /* no via, send back error ? */
  210. LOG(L_ERR, "ERROR: receive_msg: no via found in reply\n");
  211. STATS_BAD_RPL();
  212. goto error02;
  213. }
  214. #ifdef STATS
  215. gettimeofday( & tvb, &tz );
  216. STATS_RX_RESPONSE ( msg->first_line.u.reply.statuscode / 100 );
  217. #endif
  218. /* execute pre-script callbacks, if any; -jiri */
  219. /* if some of the callbacks said not to continue with
  220. script processing, don't do so
  221. if we are here basic sanity checks are already done
  222. (like presence of at least one via), so you can count
  223. on via1 being parsed in a pre-script callback --andrei
  224. */
  225. if (exec_pre_script_cb(msg, ONREPLY_CB_TYPE)==0 )
  226. {
  227. STATS_RPL_FWD_DROP();
  228. goto end; /* drop the reply */
  229. }
  230. /* exec the onreply routing script */
  231. if (onreply_rt.rlist[DEFAULT_RT]){
  232. set_route_type(CORE_ONREPLY_ROUTE);
  233. ret=run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, &ctx);
  234. #ifndef NO_ONREPLY_ROUTE_ERROR
  235. if (unlikely(ret<0)){
  236. LOG(L_WARN, "WARNING: receive_msg: "
  237. "error while trying onreply script\n");
  238. goto error_rpl;
  239. }else
  240. #endif /* NO_ONREPLY_ROUTE_ERROR */
  241. if (unlikely(ret==0 || (ctx.run_flags&DROP_R_F))){
  242. STATS_RPL_FWD_DROP();
  243. goto skip_send_reply; /* drop the message, no error */
  244. }
  245. }
  246. /* send the msg */
  247. forward_reply(msg);
  248. skip_send_reply:
  249. #ifdef STATS
  250. gettimeofday( & tve, &tz );
  251. diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
  252. stats->processed_responses++;
  253. stats->acc_res_time+=diff;
  254. DBG("successfully ran reply processing...(%d usec)\n", diff);
  255. #endif
  256. /* execute post reply-script callbacks */
  257. exec_post_script_cb(msg, ONREPLY_CB_TYPE);
  258. }
  259. end:
  260. #ifdef STATS
  261. skipped = 0;
  262. #endif
  263. /* free possible loaded avps -bogdan */
  264. reset_avps();
  265. #ifdef WITH_XAVP
  266. xavp_reset_list();
  267. #endif
  268. DBG("receive_msg: cleaning up\n");
  269. free_sip_msg(msg);
  270. pkg_free(msg);
  271. #ifdef STATS
  272. if (skipped) STATS_RX_DROPS;
  273. #endif
  274. return 0;
  275. #ifndef NO_ONREPLY_ROUTE_ERROR
  276. error_rpl:
  277. /* execute post reply-script callbacks */
  278. exec_post_script_cb(msg, ONREPLY_CB_TYPE);
  279. reset_avps();
  280. #ifdef WITH_XAVP
  281. xavp_reset_list();
  282. #endif
  283. goto error02;
  284. #endif /* NO_ONREPLY_ROUTE_ERROR */
  285. error_req:
  286. DBG("receive_msg: error:...\n");
  287. /* execute post request-script callbacks */
  288. exec_post_script_cb(msg, REQUEST_CB_TYPE);
  289. error03:
  290. /* free possible loaded avps -bogdan */
  291. reset_avps();
  292. #ifdef WITH_XAVP
  293. xavp_reset_list();
  294. #endif
  295. error02:
  296. free_sip_msg(msg);
  297. pkg_free(msg);
  298. error00:
  299. STATS_RX_DROPS;
  300. return -1;
  301. }