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