update fwd_rpls statistics
[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  *  2003-04-12  update_sock_struct_from via uses also FL_FORCE_RPORT for
43  *               local replies (andrei)
44  *  2003-08-21  check_self properly handles ipv6 addresses & refs   (andrei)
45  *  2003-10-21  check_self updated to handle proto (andrei)
46  *  2003-10-24  converted to the new socket_info lists (andrei)
47  *  2004-10-10  modified check_self to use grep_sock_info (andrei)
48  *  2004-11-08  added force_send_socket support in get_send_socket (andrei)
49  *  2005-12-11  onsend_router support; forward_request to no longer
50  *              pkg_malloc'ed (andrei)
51  *  2006-04-12  forward_{request,reply} use now struct dest_info (andrei)
52  *  2006-04-21  basic comp via param support (andrei)
53  *  2006-07-31  forward_request can resolve destination on its own, uses the 
54  *              dns cache and falls back on send error to other ips (andrei)
55  *  2007-10-08  get_send_socket() will ignore force_send_socket if the forced
56  *               socket is multicast (andrei)
57  */
58
59
60
61 #include <string.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <sys/types.h>
65 #include <sys/socket.h>
66 #include <netdb.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69
70 #include "forward.h"
71 #include "hash_func.h"
72 #include "config.h"
73 #include "parser/msg_parser.h"
74 #include "route.h"
75 #include "events.h"
76 #include "dprint.h"
77 #include "globals.h"
78 #include "cfg_core.h"
79 #include "data_lump.h"
80 #include "ut.h"
81 #include "mem/mem.h"
82 #include "msg_translator.h"
83 #include "sr_module.h"
84 #include "ip_addr.h"
85 #include "resolve.h"
86 #include "name_alias.h"
87 #include "socket_info.h"
88 #include "onsend.h"
89 #include "resolve.h"
90 #ifdef USE_DNS_FAILOVER
91 #include "dns_cache.h"
92 #endif
93 #ifdef USE_DST_BLACKLIST
94 #include "dst_blacklist.h"
95 #endif
96 #include "compiler_opt.h"
97
98 #ifdef DEBUG_DMALLOC
99 #include <dmalloc.h>
100 #endif
101
102
103
104 /* return a socket_info_pointer to the sending socket; as opposed to
105  * get_send_socket, which returns process's default socket, get_out_socket
106  * attempts to determine the outbound interface which will be used;
107  * it creates a temporary connected socket to determine it; it will
108  * be very likely noticeably slower, but it can deal better with
109  * multihomed hosts
110  */
111 struct socket_info* get_out_socket(union sockaddr_union* to, int proto)
112 {
113         int temp_sock;
114         socklen_t len;
115         union sockaddr_union from; 
116         struct socket_info* si;
117         struct ip_addr ip;
118
119         if (proto!=PROTO_UDP) {
120                 LOG(L_CRIT, "BUG: get_out_socket can only be called for UDP\n");
121                 return 0;
122         }
123         
124         temp_sock=socket(to->s.sa_family, SOCK_DGRAM, 0 );
125         if (temp_sock==-1) {
126                 LOG(L_ERR, "ERROR: get_out_socket: socket() failed: %s\n",
127                                 strerror(errno));
128                 return 0;
129         }
130         if (connect(temp_sock, &to->s, sockaddru_len(*to))==-1) {
131                 LOG(L_ERR, "ERROR: get_out_socket: connect failed: %s\n",
132                                 strerror(errno));
133                 goto error;
134         }
135         len=sizeof(from);
136         if (getsockname(temp_sock, &from.s, &len)==-1) {
137                 LOG(L_ERR, "ERROR: get_out_socket: getsockname failed: %s\n",
138                                 strerror(errno));
139                 goto error;
140         }
141         su2ip_addr(&ip, &from);
142         si=find_si(&ip, 0, proto);
143         if (si==0) goto error;
144         close(temp_sock);
145         DBG("DEBUG: get_out_socket: socket determined: %p\n", si );
146         return si;
147 error:
148         LOG(L_ERR, "ERROR: get_out_socket: no socket found\n");
149         close(temp_sock);
150         return 0;
151 }
152
153
154
155 /** get the sending socket for a corresponding destination.
156  * @param force_send_socket - if !=0 and the protocol and af correspond
157  *                            with the destination, it will be returned.
158  *                            If the protocol or af check fail, a look-alike
159  *                            socket will be searched for and mismatch will be
160  *                            set. If no look-alike socket is found it will
161  *                            fallback to normal resolution.
162  * @param to - destination
163  * @param proto - protocol
164  * @param mismatch - result parameter, set if a force_send_socket was used, but
165  *                   there was an error matching it exactly to the destination.
166  *                   Possible values: 0 ok, SS_MISMATCH_PROTO,
167  *                   SS_MISMATCH_ADDR, SS_MISMATCH_AF, SS_MISMATCH_MCAST.
168  * @return a socket_info pointer to the sending socket on success (and possibly
169  *         sets mismatch) or 0 on error.
170  */
171 struct socket_info* get_send_socket2(struct socket_info* force_send_socket,
172                                                                                 union sockaddr_union* to, int proto,
173                                                                                 enum ss_mismatch* mismatch)
174 {
175         struct socket_info* send_sock;
176         
177         if (likely(mismatch)) *mismatch=0;
178         /* check if send interface is not forced */
179         if (unlikely(force_send_socket)){
180                 if (unlikely(force_send_socket->proto!=proto)){
181                         force_send_socket=find_si(&(force_send_socket->address),
182                                                                                         force_send_socket->port_no,
183                                                                                         proto);
184                         if (unlikely(force_send_socket == 0)){
185                                 if (likely(mismatch)) *mismatch=SS_MISMATCH_ADDR;
186                                 LOG(L_WARN, "WARNING: get_send_socket: "
187                                                 "protocol/port mismatch\n");
188                                 goto not_forced;
189                         }
190                         if (likely(mismatch)) *mismatch=SS_MISMATCH_PROTO;
191                 }
192                 if (unlikely(force_send_socket->address.af!=to->s.sa_family)){
193                         DBG("get_send_socket: force_send_socket of different af (dst %d,"
194                                         " forced %d)\n",
195                                         to->s.sa_family, force_send_socket->address.af);
196                         if (likely(mismatch)) *mismatch=SS_MISMATCH_AF;
197                         goto not_forced;
198                 }
199                 /* check if listening on the socket (the check does not work
200                    for TCP and TLS, for them socket==-1 on all the processes
201                    except tcp_main(), see close_extra_socks() */
202                 if (likely((force_send_socket->socket!=-1 ||
203                                                 force_send_socket->proto==PROTO_TCP ||
204                                                 force_send_socket->proto==PROTO_TLS) &&
205                                         !(force_send_socket->flags & SI_IS_MCAST)))
206                                 return force_send_socket;
207                 else{
208                         if (!(force_send_socket->flags & SI_IS_MCAST))
209                                 LOG(L_WARN, "WARNING: get_send_socket: not listening"
210                                                          " on the requested socket, no fork mode?\n");
211                         else if (likely(mismatch)) *mismatch=SS_MISMATCH_MCAST;
212                 }
213         };
214 not_forced:
215         if (mhomed && proto==PROTO_UDP){
216                 send_sock=get_out_socket(to, proto);
217                 if ((send_sock==0) || (send_sock->socket!=-1))
218                         return send_sock; /* found or error*/
219                 else if (send_sock->socket==-1){
220                         LOG(L_WARN, "WARNING: get_send_socket: not listening on the"
221                                         " requested socket, no fork mode?\n");
222                         /* continue: try to use some socket */
223                 }
224         }
225
226         send_sock=0;
227         /* check if we need to change the socket (different address families -
228          * eg: ipv4 -> ipv6 or ipv6 -> ipv4) */
229         switch(proto){
230 #ifdef USE_TCP
231                 case PROTO_TCP:
232                 /* on tcp just use the "main address", we don't really now the
233                  * sending address (we can find it out, but we'll need also to see
234                  * if we listen on it, and if yes on which port -> too complicated*/
235                         switch(to->s.sa_family){
236                                 /* FIXME */
237                                 case AF_INET:   send_sock=sendipv4_tcp;
238                                                                 break;
239 #ifdef USE_IPV6
240                                 case AF_INET6:  send_sock=sendipv6_tcp;
241                                                                 break;
242 #endif
243                                 default:        LOG(L_ERR, "get_send_socket: BUG: don't know how"
244                                                                         " to forward to af %d\n", to->s.sa_family);
245                         }
246                         break;
247 #endif
248 #ifdef USE_TLS
249                 case PROTO_TLS:
250                         switch(to->s.sa_family){
251                                 /* FIXME */
252                                 case AF_INET:   send_sock=sendipv4_tls;
253                                                                 break;
254 #ifdef USE_IPV6
255                                 case AF_INET6:  send_sock=sendipv6_tls;
256                                                                 break;
257 #endif
258                                 default:        LOG(L_ERR, "get_send_socket: BUG: don't know how"
259                                                                         " to forward to af %d\n", to->s.sa_family);
260                         }
261                         break;
262 #endif /* USE_TLS */
263 #ifdef USE_SCTP
264                 case PROTO_SCTP:
265                         if ((bind_address==0) ||
266                                         (to->s.sa_family!=bind_address->address.af) ||
267                                         (bind_address->proto!=PROTO_SCTP)){
268                                 switch(to->s.sa_family){
269                                         case AF_INET:   send_sock=sendipv4_sctp;
270                                                                         break;
271 #ifdef USE_IPV6
272                                         case AF_INET6:  send_sock=sendipv6_sctp;
273                                                                         break;
274 #endif
275                                         default:        LOG(L_ERR, "get_send_socket: BUG: don't know"
276                                                                                 " how to forward to af %d\n",
277                                                                                 to->s.sa_family);
278                                 }
279                         }else send_sock=bind_address;
280                         break;
281 #endif /* USE_SCTP */
282                 case PROTO_UDP:
283                         if ((bind_address==0) ||
284                                         (to->s.sa_family!=bind_address->address.af) ||
285                                         (bind_address->proto!=PROTO_UDP)){
286                                 switch(to->s.sa_family){
287                                         case AF_INET:   send_sock=sendipv4;
288                                                                         break;
289 #ifdef USE_IPV6
290                                         case AF_INET6:  send_sock=sendipv6;
291                                                                         break;
292 #endif
293                                         default:        LOG(L_ERR, "get_send_socket: BUG: don't know"
294                                                                                 " how to forward to af %d\n",
295                                                                                 to->s.sa_family);
296                                 }
297                         }else send_sock=bind_address;
298                         break;
299                 default:
300                         LOG(L_CRIT, "BUG: get_send_socket: unknown proto %d\n", proto);
301         }
302         return send_sock;
303 }
304
305 static struct _check_self_func {
306         check_self_f fself;
307         struct _check_self_func *next;
308 } *_check_self_func_list = NULL;
309
310 /* register a function to be called when matching for myself
311  * - return 0 on success, -1 on error
312  * - f must have same prototype as check_self() and return same kind of values
313  */
314 int register_check_self_func(check_self_f f)
315 {
316         struct _check_self_func *nf = 0;
317         nf=(struct _check_self_func*)pkg_malloc(sizeof(struct _check_self_func));
318         if(nf==0)
319         {
320                 LM_ERR("no more pkg\n");
321                 return -1;
322         }
323         nf->fself = f;
324         nf->next = _check_self_func_list;
325         _check_self_func_list = nf;
326         return 0;
327 }
328
329 /* run registered check self functions
330  * returns 1 if true, 0 if false
331  */
332 int run_check_self_func(str* host, unsigned short port, unsigned short proto)
333 {
334         struct _check_self_func *sf = 0;
335
336         if(_check_self_func_list==NULL)
337                 return 0;
338         for(sf=_check_self_func_list; sf; sf=sf->next)
339                 if(sf->fself(host, port, proto)==1)
340                         return 1;
341         return 0;
342 }
343
344 /* checks if the proto: host:port is one of the address we listen on;
345  * if port==0, the  port number is ignored
346  * if proto==0 (PROTO_NONE) the protocol is ignored
347  * returns 1 if true, 0 if false, -1 on error
348  * WARNING: uses str2ip6 so it will overwrite any previous
349  *  unsaved result of this function (static buffer)
350  */
351 int check_self(str* host, unsigned short port, unsigned short proto)
352 {
353         if (grep_sock_info(host, port, proto)) goto found;
354         /* try to look into the aliases*/
355         if (grep_aliases(host->s, host->len, port, proto)==0){
356                 DBG("check_self: host != me\n");
357                 return (_check_self_func_list==NULL)?0:run_check_self_func(host,
358                                                                                                                 port, proto);
359         }
360 found:
361         return 1;
362 }
363
364 /* checks if the proto:port is one of the ports we listen on;
365  * if proto==0 (PROTO_NONE) the protocol is ignored
366  * returns 1 if true, 0 if false, -1 on error
367  */
368 int check_self_port(unsigned short port, unsigned short proto)
369 {
370         if (grep_sock_info_by_port(port, proto))
371                 /* as aliases do not contain different ports we can skip them */
372                 return 1;
373         else
374                 return 0;
375 }
376
377
378
379 /* forwards a request to dst
380  * parameters:
381  *   msg       - sip msg
382  *   dst       - destination name, if non-null it will be resolved and
383  *               send_info updated with the ip/port. Even if dst is non
384  *               null send_info must contain the protocol and if a non
385  *               default port or non srv. lookup is desired, the port must
386  *               be !=0 
387  *   port      - used only if dst!=0 (else the port in send_info->to is used)
388  *   send_info - value/result partially filled dest_info structure:
389  *                 - send_info->proto and comp are used
390  *                 - send_info->to will be filled (dns)
391  *                 - send_info->send_flags is filled from the message
392  *                 - if the send_socket member is null, a send_socket will be 
393  *                   chosen automatically
394  * WARNING: don't forget to zero-fill all the  unused members (a non-zero 
395  * random id along with proto==PROTO_TCP can have bad consequences, same for
396  *   a bogus send_socket value)
397  */
398 int forward_request(struct sip_msg* msg, str* dst, unsigned short port,
399                                                         struct dest_info* send_info)
400 {
401         unsigned int len;
402         char* buf;
403         char md5[MD5_LEN];
404         struct socket_info* orig_send_sock; /* initial send_sock */
405         int ret;
406         struct ip_addr ip; /* debugging only */
407         char proto;
408 #ifdef USE_DNS_FAILOVER
409         struct socket_info* prev_send_sock;
410         int err;
411         struct dns_srv_handle dns_srv_h;
412         
413         prev_send_sock=0;
414         err=0;
415 #endif
416         
417         
418         buf=0;
419         orig_send_sock=send_info->send_sock;
420         proto=send_info->proto;
421         ret=0;
422
423         if(dst){
424 #ifdef USE_DNS_FAILOVER
425                 if (cfg_get(core, core_cfg, use_dns_failover)){
426                         dns_srv_handle_init(&dns_srv_h);
427                         err=dns_sip_resolve2su(&dns_srv_h, &send_info->to, dst, port,
428                                                                         &proto, dns_flags);
429                         if (err!=0){
430                                 LOG(L_ERR, "ERROR: forward_request: resolving \"%.*s\""
431                                                 " failed: %s [%d]\n", dst->len, ZSW(dst->s),
432                                                 dns_strerror(err), err);
433                                 ret=E_BAD_ADDRESS;
434                                 goto error;
435                         }
436                 }else
437 #endif
438                 if (sip_hostport2su(&send_info->to, dst, port, &proto)<0){
439                         LOG(L_ERR, "ERROR: forward_request: bad host name %.*s,"
440                                                 " dropping packet\n", dst->len, ZSW(dst->s));
441                         ret=E_BAD_ADDRESS;
442                         goto error;
443                 }
444         }/* dst */
445         send_info->send_flags=msg->fwd_send_flags;
446         /* calculate branch for outbound request;  if syn_branch is turned off,
447            calculate is from transaction key, i.e., as an md5 of From/To/CallID/
448            CSeq exactly the same way as TM does; good for reboot -- than messages
449            belonging to transaction lost due to reboot will still be forwarded
450            with the same branch parameter and will be match-able downstream
451         
452            if it is turned on, we don't care about reboot; we simply put a simple
453            value in there; better for performance
454         */
455         if (syn_branch ) {
456                 *msg->add_to_branch_s='0';
457                 msg->add_to_branch_len=1;
458         } else {
459                 if (!char_msg_val( msg, md5 ))  { /* parses transaction key */
460                         LOG(L_ERR, "ERROR: forward_request: char_msg_val failed\n");
461                         ret=E_UNSPEC;
462                         goto error;
463                 }
464                 msg->hash_index=hash( msg->callid->body, get_cseq(msg)->number);
465                 if (!branch_builder( msg->hash_index, 0, md5, 0 /* 0-th branch */,
466                                         msg->add_to_branch_s, &msg->add_to_branch_len )) {
467                         LOG(L_ERR, "ERROR: forward_request: branch_builder failed\n");
468                         ret=E_UNSPEC;
469                         goto error;
470                 }
471         }
472         /* try to send the message until success or all the ips are exhausted
473          *  (if dns lookup is performed && the dns cache used ) */
474 #ifdef USE_DNS_FAILOVER
475         do{
476 #endif
477                 if (orig_send_sock==0) /* no forced send_sock => find it **/
478                         send_info->send_sock=get_send_socket(msg, &send_info->to, proto);
479                 if (send_info->send_sock==0){
480                         LOG(L_ERR, "forward_req: ERROR: cannot forward to af %d, proto %d "
481                                                 "no corresponding listening socket\n",
482                                                 send_info->to.s.sa_family, proto);
483                         ret=ser_error=E_NO_SOCKET;
484 #ifdef USE_DNS_FAILOVER
485                         /* continue, maybe we find a socket for some other ip */
486                         continue;
487 #else
488                         goto error;
489 #endif
490                 }
491         
492 #ifdef USE_DNS_FAILOVER
493                 if (prev_send_sock!=send_info->send_sock){
494                         /* rebuild the message only if the send_sock changed */
495                         prev_send_sock=send_info->send_sock;
496 #endif
497                         if (buf) pkg_free(buf);
498                         send_info->proto=proto;
499                         buf = build_req_buf_from_sip_req(msg, &len, send_info, 0);
500                         if (!buf){
501                                 LOG(L_ERR, "ERROR: forward_request: building failed\n");
502                                 ret=E_OUT_OF_MEM; /* most probable */
503                                 goto error;
504                         }
505 #ifdef USE_DNS_FAILOVER
506                 }
507 #endif
508                  /* send it! */
509                 DBG("Sending:\n%.*s.\n", (int)len, buf);
510                 DBG("orig. len=%d, new_len=%d, proto=%d\n",
511                                 msg->len, len, send_info->proto );
512         
513                 if (run_onsend(msg, send_info, buf, len)==0){
514                         su2ip_addr(&ip, &send_info->to);
515                         LOG(L_INFO, "forward_request: request to %s:%d(%d) dropped"
516                                         " (onsend_route)\n", ip_addr2a(&ip),
517                                                 su_getport(&send_info->to), send_info->proto);
518                         ser_error=E_OK; /* no error */
519                         ret=E_ADM_PROHIBITED;
520 #ifdef USE_DNS_FAILOVER
521                         continue; /* try another ip */
522 #else
523                         goto error; /* error ? */
524 #endif
525                 }
526 #ifdef USE_DST_BLACKLIST
527                 if (cfg_get(core, core_cfg, use_dst_blacklist)){
528                         if (dst_is_blacklisted(send_info, msg)){
529                                 su2ip_addr(&ip, &send_info->to);
530                                 LOG(L_DBG, "DEBUG: blacklisted destination:%s:%d (%d)\n",
531                                                         ip_addr2a(&ip), su_getport(&send_info->to),
532                                                         send_info->proto);
533                                 ret=ser_error=E_SEND;
534 #ifdef USE_DNS_FAILOVER
535                                 continue; /* try another ip */
536 #else
537                                 goto error;
538 #endif
539                         }
540                 }
541 #endif
542                 if (msg_send(send_info, buf, len)<0){
543                         ret=ser_error=E_SEND;
544 #ifdef USE_DST_BLACKLIST
545                         if (cfg_get(core, core_cfg, use_dst_blacklist))
546                                 dst_blacklist_add(BLST_ERR_SEND, send_info, msg);
547 #endif
548 #ifdef USE_DNS_FAILOVER
549                         continue; /* try another ip */
550 #else
551                         goto error;
552 #endif
553                 }else{
554                         ret=ser_error=E_OK;
555                         /* sent requests stats */
556                         STATS_TX_REQUEST(  msg->first_line.u.request.method_value );
557                         /* exit succcesfully */
558                         goto end;
559                 }
560 #ifdef USE_DNS_FAILOVER
561         }while(dst && cfg_get(core, core_cfg, use_dns_failover) &&
562                         dns_srv_handle_next(&dns_srv_h, err) && 
563                         ((err=dns_sip_resolve2su(&dns_srv_h, &send_info->to, dst, port,
564                                                                                 &proto, dns_flags))==0));
565         if ((err!=0) && (err!=-E_DNS_EOR)){
566                 LOG(L_ERR, "ERROR:  resolving %.*s host name in uri"
567                                                         " failed: %s [%d] (dropping packet)\n",
568                                                                         dst->len, ZSW(dst->s),
569                                                                         dns_strerror(err), err);
570                 ret=ser_error=E_BAD_ADDRESS;
571                 goto error;
572         }
573 #endif
574         
575 error:
576         STATS_TX_DROPS;
577 end:
578 #ifdef USE_DNS_FAILOVER
579         if (dst && cfg_get(core, core_cfg, use_dns_failover)){
580                                 dns_srv_handle_put(&dns_srv_h);
581         }
582 #endif
583         if (buf) pkg_free(buf);
584         /* received_buf & line_buf will be freed in receive_msg by free_lump_list*/
585         if(ret==0)
586                 sr_event_exec(SREV_CORE_STATS, (void*)1);
587         return ret;
588 }
589
590
591
592 int update_sock_struct_from_via( union sockaddr_union* to,
593                                                                  struct sip_msg* msg,
594                                                                  struct via_body* via )
595 {
596         struct hostent* he;
597         str* name;
598         int err;
599         unsigned short port;
600         char proto;
601
602         port=0;
603         if(via==msg->via1){ 
604                 /* _local_ reply, we ignore any rport or received value
605                  * (but we will send back to the original port if rport is
606                  *  present) */
607                 if ((msg->msg_flags&FL_FORCE_RPORT)||(via->rport))
608                         port=msg->rcv.src_port;
609                 else port=via->port;
610                 name=&(via->host); /* received=ip in 1st via is ignored (it's
611                                                           not added by us so it's bad) */
612         }else{
613                 /* "normal" reply, we use rport's & received value if present */
614                 if (via->rport && via->rport->value.s){
615                         DBG("update_sock_struct_from_via: using 'rport'\n");
616                         port=str2s(via->rport->value.s, via->rport->value.len, &err);
617                         if (err){
618                                 LOG(L_NOTICE, "ERROR: update_sock_struct_from_via: bad rport value(%.*s)\n",
619                                                 via->rport->value.len, via->rport->value.s);
620                                 port=0;
621                         }
622                 }
623                 if (via->received){
624                         DBG("update_sock_struct_from_via: using 'received'\n");
625                         name=&(via->received->value);
626                         /* making sure that we won't do SRV lookup on "received"
627                          * (possible if no DNS_IP_HACK is used)*/
628                         if (port==0) port=via->port?via->port:SIP_PORT; 
629                 }else{
630                         DBG("update_sock_struct_from_via: using via host\n");
631                         name=&(via->host);
632                         if (port==0) port=via->port;
633                 }
634         }
635         /* we do now a malloc/memcpy because gethostbyname loves \0-terminated 
636            strings; -jiri 
637            but only if host is not null terminated
638            (host.s[len] will always be ok for a via)
639             BTW: when is via->host.s non null terminated? tm copy? - andrei 
640             Yes -- it happened on generating a 408 by TM; -jiri
641             sip_resolvehost now accepts str -janakj
642         */
643         DBG("update_sock_struct_from_via: trying SRV lookup\n");
644         proto=via->proto;
645         he=sip_resolvehost(name, &port, &proto);
646         
647         if (he==0){
648                 LOG(L_NOTICE, "ERROR:forward_reply:resolve_host(%.*s) failure\n",
649                                 name->len, name->s);
650                 return -1;
651         }
652                 
653         hostent2su(to, he, 0, port);
654         return 1;
655 }
656
657
658
659 /* removes first via & sends msg to the second */
660 int forward_reply(struct sip_msg* msg)
661 {
662         char* new_buf;
663         struct dest_info dst;
664         unsigned int new_len;
665         int r;
666 #ifdef USE_TCP
667         char* s;
668         int len;
669 #endif
670         init_dest_info(&dst);
671         new_buf=0;
672         /*check if first via host = us */
673         if (check_via){
674                 if (check_self(&msg->via1->host,
675                                         msg->via1->port?msg->via1->port:SIP_PORT,
676                                         msg->via1->proto)!=1){
677                         LOG(L_NOTICE, "ERROR: forward_reply: host in first via!=me :"
678                                         " %.*s:%d\n", msg->via1->host.len, msg->via1->host.s,
679                                                                         msg->via1->port);
680                         /* send error msg back? */
681                         goto error;
682                 }
683         }
684         
685         /* check modules response_f functions */
686         for (r=0; r<mod_response_cbk_no; r++)
687                 if (mod_response_cbks[r](msg)==0) goto skip;
688         /* we have to forward the reply stateless, so we need second via -bogdan*/
689         if (parse_headers( msg, HDR_VIA2_F, 0 )==-1 
690                 || (msg->via2==0) || (msg->via2->error!=PARSE_OK))
691         {
692                 /* no second via => error */
693                 LOG(L_ERR, "ERROR: forward_reply: no 2nd via found in reply\n");
694                 goto error;
695         }
696
697         new_buf = build_res_buf_from_sip_res( msg, &new_len);
698         if (!new_buf){
699                 LOG(L_ERR, "ERROR: forward_reply: building failed\n");
700                 goto error;
701         }
702
703         dst.proto=msg->via2->proto;
704         dst.send_flags=msg->fwd_send_flags | msg->rpl_send_flags;
705         if (update_sock_struct_from_via( &dst.to, msg, msg->via2 )==-1) goto error;
706 #ifdef USE_COMP
707         dst.comp=msg->via2->comp_no;
708 #endif
709
710 #if defined USE_TCP || defined USE_SCTP
711         if (
712 #ifdef USE_TCP
713                         dst.proto==PROTO_TCP
714 #ifdef USE_TLS
715                         || dst.proto==PROTO_TLS
716 #endif
717 #ifdef USE_SCTP
718                         ||
719 #endif /* USE_SCTP */
720 #endif /* USE_TCP */
721 #ifdef USE_SCTP
722                         dst.proto==PROTO_SCTP
723 #endif /* USE_SCTP */
724                         ){
725                 /* find id in i param if it exists */
726                 if (msg->via1->i && msg->via1->i->value.s){
727                         s=msg->via1->i->value.s;
728                         len=msg->via1->i->value.len;
729                         DBG("forward_reply: i=%.*s\n",len, ZSW(s));
730                         if (reverse_hex2int(s, len, (unsigned int*)&dst.id)<0){
731                                 LOG(L_ERR, "ERROR: forward_reply: bad via i param \"%.*s\"\n",
732                                                 len, ZSW(s));
733                                         dst.id=0;
734                         }
735                 }               
736                                 
737         } 
738 #endif
739         if (msg_send(&dst, new_buf, new_len)<0) goto error;
740 #ifdef STATS
741         STATS_TX_RESPONSE(  (msg->first_line.u.reply.statuscode/100) );
742 #endif
743
744         DBG(" reply forwarded to %.*s:%d\n", 
745                         msg->via2->host.len, msg->via2->host.s,
746                         (unsigned short) msg->via2->port);
747
748         sr_event_exec(SREV_CORE_STATS, (void*)2);
749         pkg_free(new_buf);
750 skip:
751         return 0;
752 error:
753         if (new_buf) pkg_free(new_buf);
754         return -1;
755 }