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