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