8 #include <netinet/in.h>
12 #include "msg_parser.h"
15 #include "udp_server.h"
17 #define MAX_VIA_LINE_SIZE 240
18 #define MAX_RECEIVED_SIZE 57
22 int forward_request(char * orig, char* buf,
25 struct route_elem* re)
27 unsigned int new_len, via_len, received_len;
28 char line_buf[MAX_VIA_LINE_SIZE];
29 char received_buf[MAX_RECEIVED_SIZE];
31 int offset, s_offset, size;
32 struct sockaddr_in to;
36 via_len=snprintf(line_buf, MAX_VIA_LINE_SIZE, "Via: SIP/2.0/UDP %s:%d\r\n",
38 /* check if received needs to be added */
39 /* if check_address(source_ip, msg->via1.host) */
40 received_len=snprintf(received_buf, MAX_RECEIVED_SIZE, ";received=%s",
43 new_len=len+via_len+received_len;
44 new_buf=(char*)malloc(new_len+1);
46 DPrint("ERROR: forward_request: out of memory\n");
49 /* copy msg till first via */
51 size=msg->via1.hdr-buf;
52 memcpy(new_buf, orig, size);
56 memcpy(new_buf+offset, line_buf, via_len);
58 /* modify original via if neccesarry (received=...)*/
60 if (msg->via1.params){
61 size= msg->via1.params-msg->via1.hdr-1; /*compensate for ';' */
63 size= msg->via1.host-msg->via1.hdr+strlen(msg->via1.host);
64 if (msg->via1.port!=0){
65 size+=strlen(msg->via1.hdr+size+1)+1; /* +1 for ':'*/
68 memcpy(new_buf+offset, orig+s_offset,
72 memcpy(new_buf+offset, received_buf, received_len);
75 /* copy the rest of the msg */
76 memcpy(new_buf+offset, orig+s_offset, len-s_offset);
80 printf("Sending:\n%s.\n", new_buf);
81 printf("orig. len=%d, new_len=%d, via_len=%d, received_len=%d\n",
82 len, new_len, via_len, received_len);
84 to.sin_family = AF_INET;
85 to.sin_port = (re->port)?htons(re->port):htons(SIP_PORT);
86 /* if error try next ip address if possible */
88 if (re->host.h_addr_list[re->current_addr_idx+1])
89 re->current_addr_idx++;
92 /* ? not 64bit clean?*/
93 to.sin_addr.s_addr=*((long*)re->host.h_addr_list[re->current_addr_idx]);
96 re->tx_bytes+=new_len;
97 if (udp_send(new_buf, new_len, &to, sizeof(to))==-1){
114 /* removes first via & sends msg to the second */
115 int forward_reply(char * orig, char* buf,
121 unsigned int new_len, via_len;
123 int offset, s_offset, size;
125 struct sockaddr_in to;
128 /* we must remove the first via */
129 via_len=msg->via1.size;
130 size=msg->via1.hdr-buf;
132 /* keep hdr =substract hdr size +1 (hdr':') and add
134 via_len-=strlen(msg->via1.hdr)+1;
135 size+=strlen(msg->via1.hdr)+1;
138 new_buf=(char*)malloc(new_len);
140 DPrint("ERROR: forward_reply: out of memory\n");
143 memcpy(new_buf, orig, size);
145 s_offset=size+via_len;
146 memcpy(new_buf+offset,orig+s_offset, len-s_offset);
148 printf("Sending: to %s:%d, \n%s.\n",
150 (unsigned short)msg->via2.port,
152 /* fork? gethostbyname will probably block... */
153 he=gethostbyname(msg->via2.host);
155 DPrint("ERROR:forward_reply:gethostbyname failure\n");
158 to.sin_family = AF_INET;
159 to.sin_port = (msg->via2.port)?htons(msg->via2.port):htons(SIP_PORT);
160 to.sin_addr.s_addr=*((long*)he->h_addr_list[0]);
162 if (udp_send(new_buf,new_len, &to, sizeof(to))==-1)
169 if (new_buf) free(new_buf);