9c477b7e4c656cfe09388976acb3aeb445e35dc3
[sip-router] / forward.c
1 /*
2  * $Id$
3  */
4
5
6 #include <string.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <netdb.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
14
15 #include "forward.h"
16 #include "hash_func.h"
17 #include "config.h"
18 #include "parser/msg_parser.h"
19 #include "route.h"
20 #include "dprint.h"
21 #include "udp_server.h"
22 #include "globals.h"
23 #include "data_lump.h"
24 #include "ut.h"
25 #include "mem/mem.h"
26 #include "msg_translator.h"
27 #include "sr_module.h"
28 #include "stats.h"
29 #include "ip_addr.h"
30 #include "resolve.h"
31 #include "name_alias.h"
32
33 #ifdef DEBUG_DMALLOC
34 #include <dmalloc.h>
35 #endif
36
37
38
39 /* returns a socket_info pointer to the sending socket or 0 on error
40  * params: destination socket_union pointer
41  */
42 struct socket_info* get_send_socket(union sockaddr_union* to)
43 {
44         struct socket_info* send_sock;
45         
46         send_sock=0;
47         /* check if we need to change the socket (different address families -
48          * eg: ipv4 -> ipv6 or ipv6 -> ipv4) */
49         if (to->s.sa_family!=bind_address->address.af){
50                 switch(to->s.sa_family){
51                         case AF_INET:   send_sock=sendipv4;
52                                                         break;
53 #ifdef USE_IPV6
54                         case AF_INET6:  send_sock=sendipv6;
55                                                         break;
56 #endif
57                         default:                LOG(L_ERR, "get_send_socket: BUG: don't know how"
58                                                                         " to forward to af %d\n", to->s.sa_family);
59                 }
60         }else send_sock=bind_address;
61         return send_sock;
62 }
63
64
65
66 /* checks if the host is one of the address we listen on
67 * returns 1 if true, 0 if false, -1 on error
68 */
69 int check_self(str* host)
70 {
71         int r;
72         
73         for (r=0; r<sock_no; r++){
74                 DBG("check_self - checking if host==us: %d==%d && "
75                                 " [%.*s] == [%.*s]\n", 
76                                         host->len,
77                                         sock_info[r].name.len,
78                                         host->len, host->s,
79                                         sock_info[r].name.len, sock_info[r].name.s
80                         );
81                 if ( (host->len==sock_info[r].name.len) && 
82         #ifdef USE_IPV6
83                         (strncasecmp(host->s, sock_info[r].name.s,
84                                                                  sock_info[r].name.len)==0) /*slower*/
85         #else
86                         (memcmp(host->s, sock_info[r].name.s, 
87                                                                 sock_info[r].name.len)==0)
88         #endif
89                         )
90                         break;
91         /* check if host == ip address */
92                 if (    (!sock_info[r].is_ip) &&
93                                 (host->len==sock_info[r].address_str.len) && 
94         #ifdef USE_IPV6
95                         (strncasecmp(host->s, sock_info[r].address_str.s,
96                                                                  sock_info[r].address_str.len)==0) /*slower*/
97         #else
98                         (memcmp(host->s, sock_info[r].address_str.s, 
99                                                                 sock_info[r].address_str.len)==0)
100         #endif
101                         )
102                         break;
103         }
104         if (r==sock_no){
105                 /* try to look into the aliases*/
106                 if (grep_aliases(host->s, host->len)==0){
107                         DBG("check_self: host != me\n");
108                         return 0;
109                 }
110         }
111         return 1;
112 }
113
114
115
116 int forward_request( struct sip_msg* msg, struct proxy_l * p)
117 {
118         unsigned int len;
119         char* buf;
120         union sockaddr_union* to;
121         struct socket_info* send_sock;
122         char md5[MD5_LEN];
123         
124         to=0;
125         buf=0;
126         
127         to=(union sockaddr_union*)malloc(sizeof(union sockaddr_union));
128         if (to==0){
129                 ser_error=E_OUT_OF_MEM;
130                 LOG(L_ERR, "ERROR: forward_request: out of memory\n");
131                 goto error;
132         }
133         
134         
135         /* if error try next ip address if possible */
136         if (p->ok==0){
137                 if (p->host.h_addr_list[p->addr_idx+1])
138                         p->addr_idx++;
139                 else p->addr_idx=0;
140                 p->ok=1;
141         }
142         
143         hostent2su(to, &p->host, p->addr_idx, 
144                                 (p->port)?htons(p->port):htons(SIP_PORT));
145         p->tx++;
146         p->tx_bytes+=len;
147         
148
149         send_sock=get_send_socket(to);
150         if (send_sock==0){
151                 LOG(L_ERR, "forward_req: ERROR: cannot forward to af %d "
152                                 "no coresponding listening socket\n", to->s.sa_family);
153                 ser_error=E_NO_SOCKET;
154                 goto error1;
155         }
156
157         /* calculate branch for outbound request;  if syn_branch is turned off,
158            calculate is from transaction key, i.e., as an md5 of From/To/CallID/
159            CSeq exactly the same way as TM does; good for reboot -- than messages
160            belonging to transaction lost due to reboot will still be forwarded
161            with the same branch parameter and will be match-able downstream
162
163        if it is turned on, we don't care about reboot; we simply put a simple
164            value in there; better for performance
165         */
166
167         if (syn_branch ) {
168                 *msg->add_to_branch_s='0';
169                 msg->add_to_branch_len=1;
170         } else {
171                 if (!char_msg_val( msg, md5 ))  { /* parses transaction key */
172                         LOG(L_ERR, "ERROR: forward_request: char_msg_val failed\n");
173                         goto error1;
174                 }
175                 msg->hash_index=hash( msg->callid->body, get_cseq(msg)->number);
176                 if (!branch_builder( msg->hash_index, 0, md5, 0 /* 0-th branch */,
177                                         msg->add_to_branch_s, &msg->add_to_branch_len )) {
178                         LOG(L_ERR, "ERROR: forward_request: branch_builder failed\n");
179                         goto error1;
180                 }
181         }
182
183         buf = build_req_buf_from_sip_req( msg, &len, send_sock);
184         if (!buf){
185                 LOG(L_ERR, "ERROR: forward_request: building failed\n");
186                 goto error1;
187         }
188          /* send it! */
189         DBG("Sending:\n%s.\n", buf);
190         DBG("orig. len=%d, new_len=%d\n", msg->len, len );
191         
192         if (udp_send(send_sock, buf, len,  to)==-1){
193                         ser_error=E_SEND;
194                         p->errors++;
195                         p->ok=0;
196                         STATS_TX_DROPS;
197                         goto error1;
198         }
199         /* sent requests stats */
200         else STATS_TX_REQUEST(  msg->first_line.u.request.method_value );
201         pkg_free(buf);
202         free(to);
203         /* received_buf & line_buf will be freed in receiv_msg by free_lump_list*/
204         return 0;
205
206 error1:
207         free(to);
208 error:
209         if (buf) pkg_free(buf);
210         return -1;
211 }
212
213
214 int update_sock_struct_from_ip( union sockaddr_union* to,
215         struct sip_msg *msg )
216 {
217         to->sin.sin_port=(msg->via1->port)
218                 ?htons(msg->via1->port): htons(SIP_PORT);
219         to->sin.sin_family=msg->src_ip.af;
220         memcpy(&to->sin.sin_addr, &msg->src_ip.u, msg->src_ip.len);
221
222         return 1;
223 }
224
225 int update_sock_struct_from_via( union sockaddr_union* to,
226                                                                  struct via_body* via )
227 {
228         struct hostent* he;
229         char *host_copy;
230         str* name;
231         unsigned short port;
232
233
234         if (via->received){
235                 DBG("update_sock_struct_from_via: using 'received'\n");
236                 name=&(via->received->value);
237                 /* making sure that we won't do SRV lookup on "received"
238                  * (possible if no DNS_IP_HACK is used)*/
239                 port=via->port?via->port:SIP_PORT; 
240         }else{
241                 DBG("update_sock_struct_from_via: using via host\n");
242                 name=&(via->host);
243                 port=via->port;
244         }
245         /* we do now a malloc/memcpy because gethostbyname loves \0-terminated 
246            strings; -jiri 
247            but only if host is not null terminated
248            (host.s[len] will always be ok for a via)
249             BTW: when is via->host.s non null terminated? tm copy? - andrei 
250             Yes -- it happened on generating a 408 by TM; -jiri
251         */
252         if (name->s[name->len]){
253                 host_copy=pkg_malloc( name->len+1 );
254                 if (!host_copy) {
255                         LOG(L_NOTICE, "ERROR: update_sock_struct_from_via:"
256                                                         " not enough memory\n");
257                         return -1;
258                 }
259                 memcpy(host_copy, name->s, name->len );
260                 host_copy[name->len]=0;
261                 DBG("update_sock_struct_from_via: trying SRV lookup\n");
262                 he=sip_resolvehost(host_copy, &port);
263                 
264                 pkg_free( host_copy );
265         }else{
266                 DBG("update_sock_struct_from_via: trying SRV lookup\n");
267                 he=sip_resolvehost(name->s, &port);
268         }
269         
270         if (he==0){
271                 LOG(L_NOTICE, "ERROR:forward_reply:resolve_host(%s) failure\n",
272                                 name->s);
273                 return -1;
274         }
275         hostent2su(to, he, 0, htons(port));
276         return 1;
277 }
278
279
280 /* removes first via & sends msg to the second */
281 int forward_reply(struct sip_msg* msg)
282 {
283         char* new_buf;
284         union sockaddr_union* to;
285         struct socket_info* send_sock;
286         unsigned int new_len;
287         struct sr_module *mod;
288         
289         to=0;
290         new_buf=0;
291         /*check if first via host = us */
292         if (check_via){
293                 if (check_self(&(msg->via1->host))!=1){
294                         LOG(L_NOTICE, "ERROR: forward_reply: host in first via!=me :"
295                                         " %.*s\n", msg->via1->host.len, msg->via1->host.s);
296                         /* send error msg back? */
297                         goto error;
298                 }
299         }
300         /* quick hack, slower for mutliple modules*/
301         for (mod=modules;mod;mod=mod->next){
302                 if ((mod->exports) && (mod->exports->response_f)){
303                         DBG("forward_reply: found module %s, passing reply to it\n",
304                                         mod->exports->name);
305                         if (mod->exports->response_f(msg)==0) goto skip;
306                 }
307         }
308
309         /* we have to forward the reply stateless, so we need second via -bogdan*/
310         if (parse_headers( msg, HDR_VIA2, 0 )==-1 
311                 || (msg->via2==0) || (msg->via2->error!=PARSE_OK))
312         {
313                 /* no second via => error */
314                 LOG(L_ERR, "ERROR: forward_msg: no 2nd via found in reply\n");
315                 goto error;
316         }
317
318         to=(union sockaddr_union*)malloc(sizeof(union sockaddr_union));
319         if (to==0){
320                 LOG(L_ERR, "ERROR: forward_reply: out of memory\n");
321                 goto error;
322         }
323
324         new_buf = build_res_buf_from_sip_res( msg, &new_len);
325         if (!new_buf){
326                 LOG(L_ERR, "ERROR: forward_reply: building failed\n");
327                 goto error;
328         }
329
330         if (update_sock_struct_from_via( to, msg->via2 )==-1) goto error;
331         send_sock=get_send_socket(to);
332         if (send_sock==0){
333                 LOG(L_ERR, "forward_reply: ERROR: no sending socket found\n");
334                 goto error;
335         }
336
337         if (udp_send(send_sock, new_buf,new_len,  to)==-1)
338         {
339                 STATS_TX_DROPS;
340                 goto error;
341         } else {
342 #ifdef STATS
343                 int j = msg->first_line.u.reply.statuscode/100;
344                 STATS_TX_RESPONSE(  j );
345 #endif
346         }
347
348         DBG(" reply forwarded to %s:%d\n",msg->via2->host.s,
349                 (unsigned short) msg->via2->port);
350
351         pkg_free(new_buf);
352         free(to);
353 skip:
354         return 0;
355 error:
356         if (new_buf) pkg_free(new_buf);
357         if (to) free(to);
358         return -1;
359 }