tested and ifdef-ed the Stats hack; to be done:L put it in shmem
[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 int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
25 {
26         struct sip_msg msg;
27
28 #ifdef STATS
29         stats.total_rx++;       
30 #endif
31
32         memset(&msg,0, sizeof(struct sip_msg)); /* init everything to 0 */
33         /* fill in msg */
34         msg.buf=buf;
35         msg.len=len;
36         msg.src_ip=src_ip;
37         /* make a copy of the message */
38         msg.orig=(char*) malloc(len+1);
39         if (msg.orig==0){
40                 LOG(L_ERR, "ERROR:receive_msg: memory allocation failure\n");
41                 goto error1;
42         }
43         memcpy(msg.orig, buf, len);
44         msg.orig[len]=0; /* null terminate it,good for using str* functions on it*/
45         
46         if (parse_msg(buf,len, &msg)!=0){
47                 goto error;
48         }
49         
50         if (msg.first_line.type==SIP_REQUEST){
51                 /* sanity checks */
52                 if (msg.via1.error!=VIA_PARSE_OK){
53                         /* no via, send back error ? */
54                         goto skip;
55                 }
56                 /* check if neccesarry to add receive? */
57                 
58                 /* exec routing script */
59                 if (run_actions(rlist[0], &msg)<0){
60                         LOG(L_WARN, "WARNING: receive_msg: "
61                                         "error while trying script\n");
62                         goto error;
63                 }
64 #ifdef STATS
65                 /* jku -- update statistics  */
66                 else stats.ok_rx_rq++;  
67 #endif
68         }else if (msg.first_line.type==SIP_REPLY){
69                 /* sanity checks */
70                 if (msg.via1.error!=VIA_PARSE_OK){
71                         /* no via, send back error ? */
72                         goto skip;
73                 }
74                 if (msg.via2.error!=VIA_PARSE_OK){
75                         /* no second via => error? */
76                         goto skip;
77                 }
78                 /* check if via1 == us */
79
80 #ifdef STATS
81                 /* jku -- update statistics  */
82                 stats.ok_rx_rs++;       
83 #endif
84                 
85                 /* send the msg */
86                 if (forward_reply(&msg)==0){
87                         DBG(" reply forwarded to %s:%d\n", 
88                                                 msg.via2.host,
89                                                 (unsigned short) msg.via2.port);
90                 }
91         }
92 skip:
93         if (msg.new_uri) free(msg.new_uri);
94         if (msg.add_rm) free_lump_list(msg.add_rm);
95         if (msg.repl_add_rm) free_lump_list(msg.repl_add_rm);
96         free(msg.orig);
97         return 0;
98 error:
99         if (msg.new_uri) free(msg.new_uri);
100         if (msg.add_rm) free_lump_list(msg.add_rm);
101         if (msg.repl_add_rm) free_lump_list(msg.repl_add_rm);
102         free(msg.orig);
103 error1:
104         return -1;
105 }
106