e1483c90abc9ddfc5bd19f973b3a9a8b7ec362fb
[sip-router] / forward.c
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2001-2003 Fhg Fokus
5  *
6  * This file is part of ser, a free SIP server.
7  *
8  * ser is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * For a license to use the ser software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * ser is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License 
24  * along with this program; if not, write to the Free Software 
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  *
27  * History:
28  * -------
29  *  2001-??-??  created by andrei
30  *  ????-??-??  lots of changes by a lot of people
31  *  2003-01-23  support for determination of outbound interface added :
32  *               get_out_socket (jiri)
33  *  2003-01-24  reply to rport support added, contributed by
34  *               Maxim Sobolev <sobomax@FreeBSD.org> and modified by andrei
35  *  2003-02-11  removed calls to upd_send & tcp_send & replaced them with
36  *               calls to msg_send (andrei)
37  *  2003-03-19  replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
38  *  2003-04-02  fixed get_send_socket for tcp fwd to udp (andrei)
39  *  2003-04-03  added su_setport (andrei)
40  *  2003-04-04  update_sock_struct_from_via now differentiates between
41  *               local replies  & "normal" replies (andrei)
42  */
43
44
45
46 #include <string.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netdb.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54
55 #include "forward.h"
56 #include "hash_func.h"
57 #include "config.h"
58 #include "parser/msg_parser.h"
59 #include "route.h"
60 #include "dprint.h"
61 #include "globals.h"
62 #include "data_lump.h"
63 #include "ut.h"
64 #include "mem/mem.h"
65 #include "msg_translator.h"
66 #include "sr_module.h"
67 #include "ip_addr.h"
68 #include "resolve.h"
69 #include "name_alias.h"
70
71 #ifdef DEBUG_DMALLOC
72 #include <dmalloc.h>
73 #endif
74
75 /* return a socket_info_pointer to the sending socket; as opposed to
76  * get_send_socket, which returns process's default socket, get_out_socket
77  * attempts to determine the outbound interface which will be used;
78  * it creates a temporary connected socket to determine it; it will
79  * be very likely noticeably slower, but it can deal better with
80  * multihomed hosts
81  */
82 struct socket_info* get_out_socket(union sockaddr_union* to, int proto)
83 {
84         int temp_sock;
85         socklen_t len;
86         int r;
87         union sockaddr_union from; 
88
89         if (proto!=PROTO_UDP) {
90                 LOG(L_CRIT, "BUG: get_out_socket can only be called for UDP\n");
91                 return 0;
92         }
93
94         r=-1;
95         temp_sock=socket(to->s.sa_family, SOCK_DGRAM, 0 );
96         if (temp_sock==-1) {
97                 LOG(L_ERR, "ERROR: get_out_socket: socket() failed: %s\n",
98                                 strerror(errno));
99                 return 0;
100         }
101         if (connect(temp_sock, &to->s, sockaddru_len(*to))==-1) {
102                 LOG(L_ERR, "ERROR: get_out_socket: connect failed: %s\n",
103                                 strerror(errno));
104                 goto error;
105         }
106         len=sockaddru_len(from);
107         if (getsockname(temp_sock, &from.s, &len)==-1) {
108                 LOG(L_ERR, "ERROR: get_out_socket: getsockname failed: %s\n",
109                                 strerror(errno));
110                 goto error;
111         }
112         for (r=0; r<sock_no; r++) {
113                 switch(from.s.sa_family) {
114                         case AF_INET:   
115                                                 if (sock_info[r].address.af!=AF_INET)
116                                                                 continue;
117                                                 if (memcmp(&sock_info[r].address.u,
118                                                                 &from.sin.sin_addr, 
119                                                                 sock_info[r].address.len)==0)
120                                                         goto error; /* it is actually success */
121                                                 break;
122                         case AF_INET6:  
123                                                 if (sock_info[r].address.af!=AF_INET6)
124                                                                 continue;
125                                                 if (memcmp(&sock_info[r].address.u,
126                                                                 &from.sin6.sin6_addr, len)==0)
127                                                         goto error;
128                                                 continue;
129                         default:        LOG(L_ERR, "ERROR: get_out_socket: "
130                                                                         "unknown family: %d\n",
131                                                                         from.s.sa_family);
132                                                 r=-1;
133                                                 goto error;
134                 }
135         }
136         LOG(L_ERR, "ERROR: get_out_socket: no socket found\n");
137         r=-1;
138 error:
139         close(temp_sock);
140         DBG("DEBUG: get_out_socket: socket determined: %d\n", r );
141         return r==-1? 0: &sock_info[r];
142 }
143
144
145
146 /* returns a socket_info pointer to the sending socket or 0 on error
147  * params: destination socket_union pointer
148  */
149 struct socket_info* get_send_socket(union sockaddr_union* to, int proto)
150 {
151         struct socket_info* send_sock;
152
153         if (mhomed && proto==PROTO_UDP) return get_out_socket(to, proto);
154
155         send_sock=0;
156         /* check if we need to change the socket (different address families -
157          * eg: ipv4 -> ipv6 or ipv6 -> ipv4) */
158         switch(proto){
159 #ifdef USE_TCP
160                 case PROTO_TCP:
161                 /* on tcp just use the "main address", we don't really now the
162                  * sending address (we can find it out, but we'll need also to see
163                  * if we listen on it, and if yes on which port -> too complicated*/
164                         switch(to->s.sa_family){
165                                 /* FIXME */
166                                 case AF_INET:   send_sock=sendipv4_tcp;
167                                                                 break;
168 #ifdef USE_IPV6
169                                 case AF_INET6:  send_sock=sendipv6_tcp;
170                                                                 break;
171 #endif
172                                 default:        LOG(L_ERR, "get_send_socket: BUG: don't know how"
173                                                                         " to forward to af %d\n", to->s.sa_family);
174                         }
175                         break;
176 #endif
177                 case PROTO_UDP:
178                         if ((bind_address==0)||(to->s.sa_family!=bind_address->address.af)||
179                                   (bind_address->proto!=PROTO_UDP)){
180                                 switch(to->s.sa_family){
181                                         case AF_INET:   send_sock=sendipv4;
182                                                                         break;
183 #ifdef USE_IPV6
184                                         case AF_INET6:  send_sock=sendipv6;
185                                                                         break;
186 #endif
187                                         default:        LOG(L_ERR, "get_send_socket: BUG: don't know"
188                                                                                 " how to forward to af %d\n",
189                                                                                 to->s.sa_family);
190                                 }
191                         }else send_sock=bind_address;
192                         break;
193                 default:
194                         LOG(L_CRIT, "BUG: get_send_socket: unkown proto %d\n", proto);
195         }
196         return send_sock;
197 }
198
199
200
201 /* checks if the host:port is one of the address we listen on;
202  * if port==0, the  port number is ignored
203  * returns 1 if true, 0 if false, -1 on error
204 */
205 int check_self(str* host, unsigned short port)
206 {
207         int r;
208         
209         for (r=0; r<sock_no; r++){
210                 DBG("check_self - checking if host==us: %d==%d && "
211                                 " [%.*s] == [%.*s]\n", 
212                                         host->len,
213                                         sock_info[r].name.len,
214                                         host->len, host->s,
215                                         sock_info[r].name.len, sock_info[r].name.s
216                         );
217                 if  ((port)&&(sock_info[r].port_no!=port)) continue;
218                 if ( (host->len==sock_info[r].name.len) && 
219         #ifdef USE_IPV6
220                         (strncasecmp(host->s, sock_info[r].name.s,
221                                                                  sock_info[r].name.len)==0) /*slower*/
222         #else
223                         (memcmp(host->s, sock_info[r].name.s, 
224                                                                 sock_info[r].name.len)==0)
225         #endif
226                         )
227                         break;
228         /* check if host == ip address */
229                 if (    (!sock_info[r].is_ip) &&
230                                 (host->len==sock_info[r].address_str.len) && 
231         #ifdef USE_IPV6
232                         (strncasecmp(host->s, sock_info[r].address_str.s,
233                                                                  sock_info[r].address_str.len)==0) /*slower*/
234         #else
235                         (memcmp(host->s, sock_info[r].address_str.s, 
236                                                                 sock_info[r].address_str.len)==0)
237         #endif
238                         )
239                         break;
240         }
241         if (r==sock_no){
242                 /* try to look into the aliases*/
243                 if (grep_aliases(host->s, host->len, port)==0){
244                         DBG("check_self: host != me\n");
245                         return 0;
246                 }
247         }
248         return 1;
249 }
250
251
252
253 int forward_request( struct sip_msg* msg, struct proxy_l * p, int proto)
254 {
255         unsigned int len;
256         char* buf;
257         union sockaddr_union* to;
258         struct socket_info* send_sock;
259         char md5[MD5_LEN];
260         int id; /* used as branch for tcp! */
261         
262         to=0;
263         buf=0;
264         id=0;
265         
266         to=(union sockaddr_union*)pkg_malloc(sizeof(union sockaddr_union));
267         if (to==0){
268                 ser_error=E_OUT_OF_MEM;
269                 LOG(L_ERR, "ERROR: forward_request: out of memory\n");
270                 goto error;
271         }
272         
273         
274         /* if error try next ip address if possible */
275         if (p->ok==0){
276                 if (p->host.h_addr_list[p->addr_idx+1])
277                         p->addr_idx++;
278                 else p->addr_idx=0;
279                 p->ok=1;
280         }
281         
282         hostent2su(to, &p->host, p->addr_idx, 
283                                 (p->port)?htons(p->port):htons(SIP_PORT));
284         p->tx++;
285         p->tx_bytes+=len;
286         
287
288         send_sock=get_send_socket(to, proto);
289         if (send_sock==0){
290                 LOG(L_ERR, "forward_req: ERROR: cannot forward to af %d "
291                                 "no coresponding listening socket\n", to->s.sa_family);
292                 ser_error=E_NO_SOCKET;
293                 goto error1;
294         }
295
296         /* calculate branch for outbound request;  if syn_branch is turned off,
297            calculate is from transaction key, i.e., as an md5 of From/To/CallID/
298            CSeq exactly the same way as TM does; good for reboot -- than messages
299            belonging to transaction lost due to reboot will still be forwarded
300            with the same branch parameter and will be match-able downstream
301
302        if it is turned on, we don't care about reboot; we simply put a simple
303            value in there; better for performance
304         */
305         if (syn_branch ) {
306                 *msg->add_to_branch_s='0';
307                 msg->add_to_branch_len=1;
308         } else {
309                 if (!char_msg_val( msg, md5 ))  { /* parses transaction key */
310                         LOG(L_ERR, "ERROR: forward_request: char_msg_val failed\n");
311                         goto error1;
312                 }
313                 msg->hash_index=hash( msg->callid->body, get_cseq(msg)->number);
314                 if (!branch_builder( msg->hash_index, 0, md5, id /* 0-th branch */,
315                                         msg->add_to_branch_s, &msg->add_to_branch_len )) {
316                         LOG(L_ERR, "ERROR: forward_request: branch_builder failed\n");
317                         goto error1;
318                 }
319         }
320
321         buf = build_req_buf_from_sip_req( msg, &len, send_sock,  proto);
322         if (!buf){
323                 LOG(L_ERR, "ERROR: forward_request: building failed\n");
324                 goto error1;
325         }
326          /* send it! */
327         DBG("Sending:\n%.*s.\n", (int)len, buf);
328         DBG("orig. len=%d, new_len=%d, proto=%d\n", msg->len, len, proto );
329         
330         if (msg_send(send_sock, proto, to, 0, buf, len)<0){
331                 ser_error=E_SEND;
332                 p->errors++;
333                 p->ok=0;
334                 STATS_TX_DROPS;
335                 goto error1;
336         }
337         
338         /* sent requests stats */
339         STATS_TX_REQUEST(  msg->first_line.u.request.method_value );
340         
341         pkg_free(buf);
342         pkg_free(to);
343         /* received_buf & line_buf will be freed in receive_msg by free_lump_list*/
344         return 0;
345
346 error1:
347         pkg_free(to);
348 error:
349         if (buf) pkg_free(buf);
350         return -1;
351 }
352
353
354
355 int update_sock_struct_from_via( union sockaddr_union* to,
356                                                                  struct sip_msg* msg,
357                                                                  struct via_body* via )
358 {
359         struct hostent* he;
360         str* name;
361         int err;
362         unsigned short port;
363
364         port=0;
365         if(via==msg->via1){ 
366                 /* _local_ reply, we ignore any rport or received value
367                  * (but we will send back to the original port if rport is
368                  *  present) */
369                 if (via->rport) port=msg->rcv.src_port;
370                 else port=via->port;
371                 name=&(via->host); /* received=ip in 1st via is ignored (it's
372                                                           not added by us so it's bad) */
373         }else{
374                 /* "normal" reply, we use rport's & received value if present */
375                 if (via->rport && via->rport->value.s){
376                         DBG("update_sock_struct_from_via: using 'rport'\n");
377                         port=str2s(via->rport->value.s, via->rport->value.len, &err);
378                         if (err){
379                                 LOG(L_NOTICE, "ERROR: forward_reply: bad rport value(%.*s)\n",
380                                                 via->rport->value.len, via->rport->value.s);
381                                 port=0;
382                         }
383                 }
384                 if (via->received){
385                         DBG("update_sock_struct_from_via: using 'received'\n");
386                         name=&(via->received->value);
387                         /* making sure that we won't do SRV lookup on "received"
388                          * (possible if no DNS_IP_HACK is used)*/
389                         if (port==0) port=via->port?via->port:SIP_PORT; 
390                 }else{
391                         DBG("update_sock_struct_from_via: using via host\n");
392                         name=&(via->host);
393                         if (port==0) port=via->port;
394                 }
395         }
396         /* we do now a malloc/memcpy because gethostbyname loves \0-terminated 
397            strings; -jiri 
398            but only if host is not null terminated
399            (host.s[len] will always be ok for a via)
400             BTW: when is via->host.s non null terminated? tm copy? - andrei 
401             Yes -- it happened on generating a 408 by TM; -jiri
402             sip_resolvehost now accepts str -janakj
403         */
404         DBG("update_sock_struct_from_via: trying SRV lookup\n");
405         he=sip_resolvehost(name, &port, via->proto);
406         
407         if (he==0){
408                 LOG(L_NOTICE, "ERROR:forward_reply:resolve_host(%.*s) failure\n",
409                                 name->len, name->s);
410                 return -1;
411         }
412                 
413         hostent2su(to, he, 0, htons(port));
414         return 1;
415 }
416
417
418
419 /* removes first via & sends msg to the second */
420 int forward_reply(struct sip_msg* msg)
421 {
422         char* new_buf;
423         union sockaddr_union* to;
424         unsigned int new_len;
425         struct sr_module *mod;
426         int proto;
427         int id; /* used only by tcp*/
428 #ifdef USE_TCP
429         char* s;
430         int len;
431 #endif
432         
433         to=0;
434         id=0;
435         new_buf=0;
436         /*check if first via host = us */
437         if (check_via){
438                 if (check_self(&msg->via1->host,
439                                         msg->via1->port?msg->via1->port:SIP_PORT)!=1){
440                         LOG(L_NOTICE, "ERROR: forward_reply: host in first via!=me :"
441                                         " %.*s:%d\n", msg->via1->host.len, msg->via1->host.s,
442                                                                         msg->via1->port);
443                         /* send error msg back? */
444                         goto error;
445                 }
446         }
447         /* quick hack, slower for mutliple modules*/
448         for (mod=modules;mod;mod=mod->next){
449                 if ((mod->exports) && (mod->exports->response_f)){
450                         DBG("forward_reply: found module %s, passing reply to it\n",
451                                         mod->exports->name);
452                         if (mod->exports->response_f(msg)==0) goto skip;
453                 }
454         }
455
456         /* we have to forward the reply stateless, so we need second via -bogdan*/
457         if (parse_headers( msg, HDR_VIA2, 0 )==-1 
458                 || (msg->via2==0) || (msg->via2->error!=PARSE_OK))
459         {
460                 /* no second via => error */
461                 LOG(L_ERR, "ERROR: forward_msg: no 2nd via found in reply\n");
462                 goto error;
463         }
464
465         to=(union sockaddr_union*)pkg_malloc(sizeof(union sockaddr_union));
466         if (to==0){
467                 LOG(L_ERR, "ERROR: forward_reply: out of memory\n");
468                 goto error;
469         }
470
471         new_buf = build_res_buf_from_sip_res( msg, &new_len);
472         if (!new_buf){
473                 LOG(L_ERR, "ERROR: forward_reply: building failed\n");
474                 goto error;
475         }
476
477         proto=msg->via2->proto;
478         if (update_sock_struct_from_via( to, msg, msg->via2 )==-1) goto error;
479
480
481 #ifdef USE_TCP
482         if (proto==PROTO_TCP){
483                 /* find id in i param if it exists */
484                 if (msg->via1->i&&msg->via1->i->value.s){
485                         s=msg->via1->i->value.s;
486                         len=msg->via1->i->value.len;
487                         DBG("forward_reply: i=%.*s\n",len, s);
488                         id=reverse_hex2int(s, len);
489                         DBG("forward_reply: id= %x\n", id);
490                 }               
491                                 
492         } 
493 #endif
494         if (msg_send(0, proto, to, id, new_buf, new_len)<0) goto error;
495 #ifdef STATS
496         STATS_TX_RESPONSE(  (msg->first_line.u.reply.statuscode/100) );
497 #endif
498
499         DBG(" reply forwarded to %.*s:%d\n", 
500                         msg->via2->host.len, msg->via2->host.s,
501                         (unsigned short) msg->via2->port);
502
503         pkg_free(new_buf);
504         pkg_free(to);
505 skip:
506         return 0;
507 error:
508         if (new_buf) pkg_free(new_buf);
509         if (to) pkg_free(to);
510         return -1;
511 }