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