receive.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. LM_ERR("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. if(sr_event_exec(SREV_RCV_NOSIP, (void*)msg)!=0) {
  131. LOG(cfg_get(core, core_cfg, corelog),
  132. "core parsing of SIP message failed (%s:%d/%d)\n",
  133. ip_addr2a(&msg->rcv.src_ip), (int)msg->rcv.src_port,
  134. (int)msg->rcv.proto);
  135. sr_core_ert_run(msg, SR_CORE_ERT_RECEIVE_PARSE_ERROR);
  136. }
  137. goto error02;
  138. }
  139. DBG("After parse_msg...\n");
  140. /* set log prefix */
  141. log_prefix_set(msg);
  142. /* ... clear branches from previous message */
  143. clear_branches();
  144. if (msg->first_line.type==SIP_REQUEST){
  145. ruri_mark_new(); /* ruri is usable for forking (not consumed yet) */
  146. if (!IS_SIP(msg)){
  147. if ((ret=nonsip_msg_run_hooks(msg))!=NONSIP_MSG_ACCEPT){
  148. if (unlikely(ret==NONSIP_MSG_ERROR))
  149. goto error03;
  150. goto end; /* drop the message */
  151. }
  152. }
  153. /* sanity checks */
  154. if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
  155. /* no via, send back error ? */
  156. LM_ERR("no via found in request\n");
  157. STATS_BAD_MSG();
  158. goto error02;
  159. }
  160. /* check if necessary to add receive?->moved to forward_req */
  161. /* check for the alias stuff */
  162. #ifdef USE_TCP
  163. if (msg->via1->alias && cfg_get(tcp, tcp_cfg, accept_aliases) &&
  164. (((rcv_info->proto==PROTO_TCP) && !tcp_disable)
  165. #ifdef USE_TLS
  166. || ((rcv_info->proto==PROTO_TLS) && !tls_disable)
  167. #endif
  168. )
  169. ){
  170. if (tcpconn_add_alias(rcv_info->proto_reserved1, msg->via1->port,
  171. rcv_info->proto)!=0){
  172. LM_ERR("tcp alias failed\n");
  173. /* continue */
  174. }
  175. }
  176. #endif
  177. /* skip: */
  178. DBG("preparing to run routing scripts...\n");
  179. #ifdef STATS
  180. gettimeofday( & tvb, &tz );
  181. #endif
  182. /* execute pre-script callbacks, if any; -jiri */
  183. /* if some of the callbacks said not to continue with
  184. script processing, don't do so
  185. if we are here basic sanity checks are already done
  186. (like presence of at least one via), so you can count
  187. on via1 being parsed in a pre-script callback --andrei
  188. */
  189. if (exec_pre_script_cb(msg, REQUEST_CB_TYPE)==0 )
  190. {
  191. STATS_REQ_FWD_DROP();
  192. goto end; /* drop the request */
  193. }
  194. set_route_type(REQUEST_ROUTE);
  195. /* exec the routing script */
  196. if (run_top_route(main_rt.rlist[DEFAULT_RT], msg, 0)<0){
  197. LM_WARN("error while trying script\n");
  198. goto error_req;
  199. }
  200. #ifdef STATS
  201. gettimeofday( & tve, &tz );
  202. diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
  203. stats->processed_requests++;
  204. stats->acc_req_time += diff;
  205. DBG("successfully ran routing scripts...(%d usec)\n", diff);
  206. STATS_RX_REQUEST( msg->first_line.u.request.method_value );
  207. #endif
  208. /* execute post request-script callbacks */
  209. exec_post_script_cb(msg, REQUEST_CB_TYPE);
  210. }else if (msg->first_line.type==SIP_REPLY){
  211. /* sanity checks */
  212. if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
  213. /* no via, send back error ? */
  214. LM_ERR("no via found in reply\n");
  215. STATS_BAD_RPL();
  216. goto error02;
  217. }
  218. #ifdef STATS
  219. gettimeofday( & tvb, &tz );
  220. STATS_RX_RESPONSE ( msg->first_line.u.reply.statuscode / 100 );
  221. #endif
  222. /* execute pre-script callbacks, if any; -jiri */
  223. /* if some of the callbacks said not to continue with
  224. script processing, don't do so
  225. if we are here basic sanity checks are already done
  226. (like presence of at least one via), so you can count
  227. on via1 being parsed in a pre-script callback --andrei
  228. */
  229. if (exec_pre_script_cb(msg, ONREPLY_CB_TYPE)==0 )
  230. {
  231. STATS_RPL_FWD_DROP();
  232. goto end; /* drop the reply */
  233. }
  234. /* exec the onreply routing script */
  235. if (onreply_rt.rlist[DEFAULT_RT]){
  236. set_route_type(CORE_ONREPLY_ROUTE);
  237. ret=run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, &ctx);
  238. #ifndef NO_ONREPLY_ROUTE_ERROR
  239. if (unlikely(ret<0)){
  240. LM_WARN("error while trying onreply script\n");
  241. goto error_rpl;
  242. }else
  243. #endif /* NO_ONREPLY_ROUTE_ERROR */
  244. if (unlikely(ret==0 || (ctx.run_flags&DROP_R_F))){
  245. STATS_RPL_FWD_DROP();
  246. goto skip_send_reply; /* drop the message, no error */
  247. }
  248. }
  249. /* send the msg */
  250. forward_reply(msg);
  251. skip_send_reply:
  252. #ifdef STATS
  253. gettimeofday( & tve, &tz );
  254. diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
  255. stats->processed_responses++;
  256. stats->acc_res_time+=diff;
  257. DBG("successfully ran reply processing...(%d usec)\n", diff);
  258. #endif
  259. /* execute post reply-script callbacks */
  260. exec_post_script_cb(msg, ONREPLY_CB_TYPE);
  261. }
  262. end:
  263. #ifdef STATS
  264. skipped = 0;
  265. #endif
  266. /* free possible loaded avps -bogdan */
  267. reset_avps();
  268. #ifdef WITH_XAVP
  269. xavp_reset_list();
  270. #endif
  271. DBG("receive_msg: cleaning up\n");
  272. free_sip_msg(msg);
  273. pkg_free(msg);
  274. #ifdef STATS
  275. if (skipped) STATS_RX_DROPS;
  276. #endif
  277. /* reset log prefix */
  278. log_prefix_set(NULL);
  279. return 0;
  280. #ifndef NO_ONREPLY_ROUTE_ERROR
  281. error_rpl:
  282. /* execute post reply-script callbacks */
  283. exec_post_script_cb(msg, ONREPLY_CB_TYPE);
  284. reset_avps();
  285. #ifdef WITH_XAVP
  286. xavp_reset_list();
  287. #endif
  288. goto error02;
  289. #endif /* NO_ONREPLY_ROUTE_ERROR */
  290. error_req:
  291. DBG("receive_msg: error:...\n");
  292. /* execute post request-script callbacks */
  293. exec_post_script_cb(msg, REQUEST_CB_TYPE);
  294. error03:
  295. /* free possible loaded avps -bogdan */
  296. reset_avps();
  297. #ifdef WITH_XAVP
  298. xavp_reset_list();
  299. #endif
  300. error02:
  301. free_sip_msg(msg);
  302. pkg_free(msg);
  303. error00:
  304. STATS_RX_DROPS;
  305. /* reset log prefix */
  306. log_prefix_set(NULL);
  307. return -1;
  308. }