- added support for maximum 2 parameters per cmd call
[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 #ifdef DEBUG_DMALLOC
16 #include <dmalloc.h>
17 #endif
18
19 int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
20 {
21         struct sip_msg msg;
22
23         memset(&msg,0, sizeof(struct sip_msg)); /* init everything to 0 */
24         /* fill in msg */
25         msg.buf=buf;
26         msg.len=len;
27         msg.src_ip=src_ip;
28         /* make a copy of the message */
29         msg.orig=(char*) malloc(len+1);
30         if (msg.orig==0){
31                 LOG(L_ERR, "ERROR:receive_msg: memory allocation failure\n");
32                 goto error1;
33         }
34         memcpy(msg.orig, buf, len);
35         msg.orig[len]=0; /* null terminate it,good for using str* functions on it*/
36         
37         if (parse_msg(buf,len, &msg)!=0){
38                 goto error;
39         }
40         
41         if (msg.first_line.type==SIP_REQUEST){
42                 /* sanity checks */
43                 if (msg.via1.error!=VIA_PARSE_OK){
44                         /* no via, send back error ? */
45                         goto skip;
46                 }
47                 /* check if neccesarry to add receive? */
48                 
49                 /* exec routing script */
50                 if (run_actions(rlist[0], &msg)<0){
51                         LOG(L_WARN, "WARNING: receive_msg: "
52                                         "error while trying script\n");
53                         goto error;
54                 }
55         }else if (msg.first_line.type==SIP_REPLY){
56                 /* sanity checks */
57                 if (msg.via1.error!=VIA_PARSE_OK){
58                         /* no via, send back error ? */
59                         goto skip;
60                 }
61                 if (msg.via2.error!=VIA_PARSE_OK){
62                         /* no second via => error? */
63                         goto skip;
64                 }
65                 /* check if via1 == us */
66                 
67                 /* send the msg */
68                 if (forward_reply(&msg)==0){
69                         DBG(" reply forwarded to %s:%d\n", 
70                                                 msg.via2.host,
71                                                 (unsigned short) msg.via2.port);
72                 }
73         }
74 skip:
75         if (msg.new_uri) free(msg.new_uri);
76         if (msg.add_rm) free_lump_list(msg.add_rm);
77         if (msg.repl_add_rm) free_lump_list(msg.repl_add_rm);
78         free(msg.orig);
79         return 0;
80 error:
81         if (msg.new_uri) free(msg.new_uri);
82         if (msg.add_rm) free_lump_list(msg.add_rm);
83         if (msg.repl_add_rm) free_lump_list(msg.repl_add_rm);
84         free(msg.orig);
85 error1:
86         return -1;
87 }
88