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