GPLization banner introduced to *.[hc] files
[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
28
29 #include <string.h>
30 #include <stdlib.h>
31 #include <sys/time.h>
32
33 #include "receive.h"
34 #include "globals.h"
35 #include "dprint.h"
36 #include "route.h"
37 #include "parser/msg_parser.h"
38 #include "forward.h"
39 #include "action.h"
40 #include "mem/mem.h"
41 #include "stats.h"
42 #include "ip_addr.h"
43 #include "script_cb.h"
44 #include "dset.h"
45
46
47 #ifdef DEBUG_DMALLOC
48 #include <mem/dmalloc.h>
49 #endif
50
51 unsigned int msg_no=0;
52
53 int receive_msg(char* buf, unsigned int len, union sockaddr_union* src_su)
54 {
55         struct sip_msg* msg;
56 #ifdef STATS
57         int skipped = 1;
58         struct timeval tvb, tve;        
59         struct timezone tz;
60         unsigned int diff;
61 #endif
62
63         msg=pkg_malloc(sizeof(struct sip_msg));
64         if (msg==0) {
65                 LOG(L_ERR, "ERROR: receive_msg: no mem for sip_msg\n");
66                 goto error00;
67         }
68         msg_no++;
69         /* number of vias parsed -- good for diagnostic info in replies */
70         via_cnt=0;
71
72         memset(msg,0, sizeof(struct sip_msg)); /* init everything to 0 */
73         /* fill in msg */
74         msg->buf=buf;
75         msg->len=len;
76         /* zero termination (termination of orig message bellow not that
77            useful as most of the work is done with scrath-pad; -jiri  */
78         buf[len]=0;
79         su2ip_addr(&msg->src_ip, src_su);
80         msg->dst_ip=bind_address->address; /* won't work if listening on 0.0.0.0 */
81         msg->id=msg_no;
82         /* make a copy of the message */
83         msg->orig=(char*) pkg_malloc(len+1);
84         if (msg->orig==0){
85                 LOG(L_ERR, "ERROR:receive_msg: memory allocation failure\n");
86                 goto error01;
87         }
88         memcpy(msg->orig, buf, len);
89         msg->orig[len]=0; /* null terminate it,good for using str* functions
90                                                  on it*/
91         
92         if (parse_msg(buf,len, msg)!=0){
93                 LOG(L_ERR, "ERROR: receive_msg: parse_msg failed\n");
94                 goto error02;
95         }
96         DBG("After parse_msg...\n");
97
98         /* execute pre-script callbacks, if any; -jiri */
99         exec_pre_cb(msg);
100         /* ... and clear branches from previous message */
101         clear_branches();
102
103         if (msg->first_line.type==SIP_REQUEST){
104                 /* sanity checks */
105                 if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
106                         /* no via, send back error ? */
107                         LOG(L_ERR, "ERROR: receive_msg: no via found in request\n");
108                         goto error;
109                 }
110                 /* check if neccesarry to add receive?->moved to forward_req */
111
112                 /* exec routing script */
113                 DBG("preparing to run routing scripts...\n");
114 #ifdef  STATS
115                 gettimeofday( & tvb, &tz );
116 #endif
117
118                 if (run_actions(rlist[0], msg)<0){
119
120                         LOG(L_WARN, "WARNING: receive_msg: "
121                                         "error while trying script\n");
122                         goto error;
123                 }
124
125
126 #ifdef STATS
127                 gettimeofday( & tve, &tz );
128                 diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
129                 stats->processed_requests++;
130                 stats->acc_req_time += diff;
131                 DBG("succesfully ran routing scripts...(%d usec)\n", diff);
132                 STATS_RX_REQUEST( msg->first_line.u.request.method_value );
133 #endif
134         }else if (msg->first_line.type==SIP_REPLY){
135                 /* sanity checks */
136                 if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
137                         /* no via, send back error ? */
138                         LOG(L_ERR, "ERROR: receive_msg: no via found in reply\n");
139                         goto error;
140                 }
141 #if 0
142                 if ((msg->via2==0) || (msg->via2->error!=PARSE_OK)){
143                         /* no second via => error? */
144                         LOG(L_ERR, "ERROR: receive_msg: no 2nd via found in reply\n");
145                         goto error;
146                 }
147                 /* check if via1 == us */
148 #endif
149
150 #ifdef STATS
151                 gettimeofday( & tvb, &tz );
152                 STATS_RX_RESPONSE ( msg->first_line.u.reply.statuscode / 100 );
153 #endif
154                 
155                 /* send the msg */
156                 forward_reply(msg);
157
158 #ifdef STATS
159                 gettimeofday( & tve, &tz );
160                 diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
161                 stats->processed_responses++;
162                 stats->acc_res_time+=diff;
163                 DBG("succesfully ran reply processing...(%d usec)\n", diff);
164 #endif
165         }
166 #ifdef STATS
167         skipped = 0;
168 #endif
169         /* execute post-script callbacks, if any; -jiri */
170         exec_post_cb(msg);
171         DBG("receive_msg: cleaning up\n");
172         free_sip_msg(msg);
173         pkg_free(msg);
174 #ifdef STATS
175         if (skipped) STATS_RX_DROPS;
176 #endif
177         return 0;
178 error:
179         DBG("error:...\n");
180         /* execute post-script callbacks, if any; -jiri */
181         exec_post_cb(msg);
182 error02:
183         free_sip_msg(msg);
184 error01:
185         pkg_free(msg);
186 error00:
187         STATS_RX_DROPS;
188         return -1;
189 }
190