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