fixed merging bug for STATS
[sip-router] / receive.c
1 /* 
2  *$Id$
3  */
4
5 #include <string.h>
6 #include <stdlib.h>
7
8 #include "receive.h"
9 #include "dprint.h"
10 #include "route.h"
11 #include "msg_parser.h"
12 #include "forward.h"
13 #include "action.h"
14 #include "mem.h"
15
16
17 #ifdef DEBUG_DMALLOC
18 #include <dmalloc.h>
19 #endif
20
21 #ifdef STATS
22 #include "stats.h"
23 #endif
24
25 unsigned int msg_no=0;
26
27 int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
28 {
29         struct sip_msg* msg;
30 #ifdef STATS
31         int skipped = 1;
32 #endif
33
34         msg=pkg_malloc(sizeof(struct sip_msg));
35         if (msg==0) goto error1;
36         msg_no++;
37
38         memset(msg,0, sizeof(struct sip_msg)); /* init everything to 0 */
39         /* fill in msg */
40         msg->buf=buf;
41         msg->len=len;
42         msg->src_ip=src_ip;
43         msg->id=msg_no;
44         /* make a copy of the message */
45         msg->orig=(char*) pkg_malloc(len+1);
46         if (msg->orig==0){
47                 LOG(L_ERR, "ERROR:receive_msg: memory allocation failure\n");
48                 goto error1;
49         }
50         memcpy(msg->orig, buf, len);
51         msg->orig[len]=0; /* null terminate it,good for using str* functions
52                                                  on it*/
53         
54         if (parse_msg(buf,len, msg)!=0){
55                 goto error;
56         }
57         DBG("After parse_msg...\n");
58         if (msg->first_line.type==SIP_REQUEST){
59                 DBG("msg= request\n");
60                 /* sanity checks */
61                 if ((msg->via1==0) || (msg->via1->error!=VIA_PARSE_OK)){
62                         /* no via, send back error ? */
63                         LOG(L_ERR, "ERROR: receive_msg: no via found in request\n");
64                         goto error;
65                 }
66                 /* check if neccesarry to add receive?->moved to forward_req */
67                 
68                 /* exec routing script */
69                 DBG("preparing to run routing scripts...\n");
70                 if (run_actions(rlist[0], msg)<0){
71                         LOG(L_WARN, "WARNING: receive_msg: "
72                                         "error while trying script\n");
73                         goto error;
74                 }
75 #ifdef STATS
76                 /* jku -- update request statistics  */
77                 else update_received_request(msg->first_line.u.request.method_value );
78 #endif
79         }else if (msg->first_line.type==SIP_REPLY){
80                 DBG("msg= reply\n");
81                 /* sanity checks */
82                 if ((msg->via1==0) || (msg->via1->error!=VIA_PARSE_OK)){
83                         /* no via, send back error ? */
84                         LOG(L_ERR, "ERROR: receive_msg: no via found in reply\n");
85                         goto error;
86                 }
87                 if ((msg->via2==0) || (msg->via2->error!=VIA_PARSE_OK)){
88                         /* no second via => error? */
89                         LOG(L_ERR, "ERROR: receive_msg: no 2nd via found in reply\n");
90                         goto error;
91                 }
92                 /* check if via1 == us */
93
94 #ifdef STATS
95                 /* jku -- update statistics  */
96                 update_received_response( msg->first_line.u.reply.statusclass );
97 #endif
98                 
99                 /* send the msg */
100                 if (forward_reply(msg)==0){
101                         DBG(" reply forwarded to %s:%d\n", 
102                                                 msg->via2->host.s,
103                                                 (unsigned short) msg->via2->port);
104                 }
105         }
106 #ifdef STATS
107         skipped = 0;
108 #endif
109 skip:
110         DBG("skip:...\n");
111         free_sip_msg(msg);
112         pkg_free(msg);
113 #ifdef STATS
114         if (skipped) update_received_drops;
115 #endif
116         return 0;
117 error:
118         DBG("error:...\n");
119         free_sip_msg(msg);
120         pkg_free(msg);
121 #ifdef STATS
122         update_received_drops;
123 #endif
124         return -1;
125 error1:
126         if (msg) pkg_free(msg);
127         pkg_free(buf);
128 #ifdef STATS
129         update_received_drops;
130 #endif
131         return -1;
132 }
133