receive.c 6.8 KB

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