receive.c 7.6 KB

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