parser seems to work
[sip-router] / forward.c
1 /*
2  * $Id$
3  */
4
5
6 #include <string.h>
7
8 #include "forward.h"
9 #include "msg_parser.h"
10 #include "route.h"
11 #include "dprint.h"
12
13 #define MAX_VIA_LINE_SIZE      240
14 #define MAX_RECEIVED_SIZE  57
15
16 #define our_address "dorian.fokus.gmd.de"
17 #define our_port 1234
18
19
20 int forward_request(char * orig, char* buf, 
21                                          unsigned int len,
22                                          struct sip_msg* msg,
23                                          struct route_elem* re)
24 {
25         unsigned int new_len, via_len, received_len;
26         char line_buf[MAX_VIA_LINE_SIZE];
27         char received_buf[MAX_RECEIVED_SIZE];
28         char* new_buf;
29         int offset, s_offset, size;
30
31         received_len=0;
32         printf("0\n");
33
34         via_len=snprintf(line_buf, MAX_VIA_LINE_SIZE, "Via: SIP/2.0/UDP %s:%d\r\n",
35                                                 our_address, our_port);
36         /* check if received needs to be added */
37         /* if check_address(source_ip, msg->via1.host) */
38         received_len=snprintf(received_buf, MAX_RECEIVED_SIZE, ";received=%s",
39                                                         "10.11.12.13");
40         
41         new_len=len+via_len+received_len;
42         new_buf=(char*)malloc(new_len+1);
43         if (new_buf==0){
44                 DPrint("ERROR: forward_request: out of memory\n");
45                 goto error;
46         }
47         printf("1\n");
48  /* copy msg till first via */
49         offset=s_offset=0;
50         size=msg->via1.hdr-buf;
51         memcpy(new_buf, orig, size);
52         offset+=size;
53         s_offset+=size;
54         printf("2\n");
55  /* add our via */
56         memcpy(new_buf+offset, line_buf, via_len);
57         offset+=via_len;
58         printf("3\n");
59  /* modify original via if neccesarry (received=...)*/
60         if (received_len){
61                 if (msg->via1.params){
62                                 size= msg->via1.params-msg->via1.hdr-1; /*compensate for ';' */
63                 }else{
64                                 size= msg->via1.host-msg->via1.hdr+strlen(msg->via1.host);
65                                 if (msg->via1.port!=0){
66                                         size+=strlen(msg->via1.hdr+size+1);
67                                 }
68                 }
69                 memcpy(new_buf+offset, orig+s_offset, 
70                                                                 size);
71                 offset+=size;
72                 s_offset+=size;
73                 printf("4\n");
74                 memcpy(new_buf+offset, received_buf, received_len);
75                 printf("5\n");
76                 offset+=received_len;
77         }
78         /* copy the rest of the msg */
79         memcpy(new_buf+offset, orig+s_offset, len-s_offset);
80         printf("6\n");
81         new_buf[new_len]=0;
82
83          /* send it! */
84          printf("Sending:\n%s.\n", new_buf);
85          
86         return 0;
87 error:
88         return -1;
89
90 }
91
92
93
94 /* removes first via & sends msg to the second */
95 int forward_reply(char * orig, char* buf, 
96                                          unsigned int len,
97                                          struct sip_msg* msg)
98 {
99
100
101         unsigned int new_len, via_len;
102         char* new_buf;
103         int offset, s_offset, size;
104
105
106         /* we must remove first via */
107         via_len=msg->via1.size;
108         size=msg->via1.hdr-buf;
109         if (msg->via1.next){
110                 via_len-=strlen(msg->via1.hdr)+1; /* +1 from ':' */
111                 size+=strlen(msg->via1.hdr)+1;
112         }
113         new_len=len-size;
114         printf("r1\n");
115         new_buf=(char*)malloc(new_len);
116         if (new_buf==0){
117                 DPrint("ERROR: forward_reply: out of memory\n");
118                 goto error;
119         }
120         printf("r2\n");
121         memcpy(new_buf, orig, size);
122         offset=size;
123         s_offset=size+via_len;
124         printf("r3\n");
125         memcpy(new_buf+offset,orig+s_offset, len-s_offset);
126         printf("r4\n");
127          /* send it! */
128         printf("Sending: to %s:%d, \n%s.\n",
129                         msg->via2.host, 
130                         (unsigned short)msg->via2.port,
131                         new_buf);
132         
133         return 0;
134
135 error:
136         return -1;
137 }