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