receive.c 8.1 KB

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