core: build_req_buf_from_sip_req() has a new parameter
[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         char proto;
346 #ifdef USE_DNS_FAILOVER
347         struct socket_info* prev_send_sock;
348         int err;
349         struct dns_srv_handle dns_srv_h;
350         
351         prev_send_sock=0;
352         err=0;
353 #endif
354         
355         
356         buf=0;
357         orig_send_sock=send_info->send_sock;
358         proto=send_info->proto;
359         ret=0;
360
361         if(dst){
362 #ifdef USE_DNS_FAILOVER
363                 if (cfg_get(core, core_cfg, use_dns_failover)){
364                         dns_srv_handle_init(&dns_srv_h);
365                         err=dns_sip_resolve2su(&dns_srv_h, &send_info->to, dst, port,
366                                                                         &proto, dns_flags);
367                         if (err!=0){
368                                 LOG(L_ERR, "ERROR: forward_request: resolving \"%.*s\""
369                                                 " failed: %s [%d]\n", dst->len, ZSW(dst->s),
370                                                 dns_strerror(err), err);
371                                 ret=E_BAD_ADDRESS;
372                                 goto error;
373                         }
374                 }else
375 #endif
376                 if (sip_hostport2su(&send_info->to, dst, port, &proto)<0){
377                         LOG(L_ERR, "ERROR: forward_request: bad host name %.*s,"
378                                                 " dropping packet\n", dst->len, ZSW(dst->s));
379                         ret=E_BAD_ADDRESS;
380                         goto error;
381                 }
382         }/* dst */
383         /* calculate branch for outbound request;  if syn_branch is turned off,
384            calculate is from transaction key, i.e., as an md5 of From/To/CallID/
385            CSeq exactly the same way as TM does; good for reboot -- than messages
386            belonging to transaction lost due to reboot will still be forwarded
387            with the same branch parameter and will be match-able downstream
388
389        if it is turned on, we don't care about reboot; we simply put a simple
390            value in there; better for performance
391         */
392         if (syn_branch ) {
393                 *msg->add_to_branch_s='0';
394                 msg->add_to_branch_len=1;
395         } else {
396                 if (!char_msg_val( msg, md5 ))  { /* parses transaction key */
397                         LOG(L_ERR, "ERROR: forward_request: char_msg_val failed\n");
398                         ret=E_UNSPEC;
399                         goto error;
400                 }
401                 msg->hash_index=hash( msg->callid->body, get_cseq(msg)->number);
402                 if (!branch_builder( msg->hash_index, 0, md5, 0 /* 0-th branch */,
403                                         msg->add_to_branch_s, &msg->add_to_branch_len )) {
404                         LOG(L_ERR, "ERROR: forward_request: branch_builder failed\n");
405                         ret=E_UNSPEC;
406                         goto error;
407                 }
408         }
409         /* try to send the message until success or all the ips are exhausted
410          *  (if dns lookup is performed && the dns cache used ) */
411 #ifdef USE_DNS_FAILOVER
412         do{
413 #endif
414                 if (orig_send_sock==0) /* no forced send_sock => find it **/
415                         send_info->send_sock=get_send_socket(msg, &send_info->to, proto);
416                 if (send_info->send_sock==0){
417                         LOG(L_ERR, "forward_req: ERROR: cannot forward to af %d, proto %d "
418                                                 "no corresponding listening socket\n",
419                                                 send_info->to.s.sa_family, proto);
420                         ret=ser_error=E_NO_SOCKET;
421 #ifdef USE_DNS_FAILOVER
422                         /* continue, maybe we find a socket for some other ip */
423                         continue;
424 #else
425                         goto error;
426 #endif
427                 }
428         
429 #ifdef USE_DNS_FAILOVER
430                 if (prev_send_sock!=send_info->send_sock){
431                         /* rebuild the message only if the send_sock changed */
432                         prev_send_sock=send_info->send_sock;
433 #endif
434                         if (buf) pkg_free(buf);
435                         send_info->proto=proto;
436                         buf = build_req_buf_from_sip_req(msg, &len, send_info, 0);
437                         if (!buf){
438                                 LOG(L_ERR, "ERROR: forward_request: building failed\n");
439                                 ret=E_OUT_OF_MEM; /* most probable */
440                                 goto error;
441                         }
442 #ifdef USE_DNS_FAILOVER
443                 }
444 #endif
445                  /* send it! */
446                 DBG("Sending:\n%.*s.\n", (int)len, buf);
447                 DBG("orig. len=%d, new_len=%d, proto=%d\n",
448                                 msg->len, len, send_info->proto );
449         
450                 if (run_onsend(msg, send_info, buf, len)==0){
451                         su2ip_addr(&ip, &send_info->to);
452                         LOG(L_INFO, "forward_request: request to %s:%d(%d) dropped"
453                                         " (onsend_route)\n", ip_addr2a(&ip),
454                                                 su_getport(&send_info->to), send_info->proto);
455                         ser_error=E_OK; /* no error */
456                         ret=E_ADM_PROHIBITED;
457 #ifdef USE_DNS_FAILOVER
458                         continue; /* try another ip */
459 #else
460                         goto error; /* error ? */
461 #endif
462                 }
463 #ifdef USE_DST_BLACKLIST
464                 if (cfg_get(core, core_cfg, use_dst_blacklist)){
465                         if (dst_is_blacklisted(send_info, msg)){
466                                 su2ip_addr(&ip, &send_info->to);
467                                 LOG(L_DBG, "DEBUG: blacklisted destination:%s:%d (%d)\n",
468                                                         ip_addr2a(&ip), su_getport(&send_info->to),
469                                                         send_info->proto);
470                                 ret=ser_error=E_SEND;
471 #ifdef USE_DNS_FAILOVER
472                                 continue; /* try another ip */
473 #else
474                                 goto error;
475 #endif
476                         }
477                 }
478 #endif
479                 if (msg_send(send_info, buf, len)<0){
480                         ret=ser_error=E_SEND;
481 #ifdef USE_DST_BLACKLIST
482                         if (cfg_get(core, core_cfg, use_dst_blacklist))
483                                 dst_blacklist_add(BLST_ERR_SEND, send_info, msg);
484 #endif
485 #ifdef USE_DNS_FAILOVER
486                         continue; /* try another ip */
487 #else
488                         goto error;
489 #endif
490                 }else{
491                         ret=ser_error=E_OK;
492                         /* sent requests stats */
493                         STATS_TX_REQUEST(  msg->first_line.u.request.method_value );
494                         /* exit succcesfully */
495                         goto end;
496                 }
497 #ifdef USE_DNS_FAILOVER
498         }while(dst && cfg_get(core, core_cfg, use_dns_failover) &&
499                         dns_srv_handle_next(&dns_srv_h, err) && 
500                         ((err=dns_sip_resolve2su(&dns_srv_h, &send_info->to, dst, port,
501                                                                                 &proto, dns_flags))==0));
502         if ((err!=0) && (err!=-E_DNS_EOR)){
503                 LOG(L_ERR, "ERROR:  resolving %.*s host name in uri"
504                                                         " failed: %s [%d] (dropping packet)\n",
505                                                                         dst->len, ZSW(dst->s),
506                                                                         dns_strerror(err), err);
507                 ret=ser_error=E_BAD_ADDRESS;
508                 goto error;
509         }
510 #endif
511         
512 error:
513         STATS_TX_DROPS;
514 end:
515 #ifdef USE_DNS_FAILOVER
516         if (dst && cfg_get(core, core_cfg, use_dns_failover)){
517                                 dns_srv_handle_put(&dns_srv_h);
518         }
519 #endif
520         if (buf) pkg_free(buf);
521         /* received_buf & line_buf will be freed in receive_msg by free_lump_list*/
522         return ret;
523 }
524
525
526
527 int update_sock_struct_from_via( union sockaddr_union* to,
528                                                                  struct sip_msg* msg,
529                                                                  struct via_body* via )
530 {
531         struct hostent* he;
532         str* name;
533         int err;
534         unsigned short port;
535         char proto;
536
537         port=0;
538         if(via==msg->via1){ 
539                 /* _local_ reply, we ignore any rport or received value
540                  * (but we will send back to the original port if rport is
541                  *  present) */
542                 if ((msg->msg_flags&FL_FORCE_RPORT)||(via->rport))
543                         port=msg->rcv.src_port;
544                 else port=via->port;
545                 name=&(via->host); /* received=ip in 1st via is ignored (it's
546                                                           not added by us so it's bad) */
547         }else{
548                 /* "normal" reply, we use rport's & received value if present */
549                 if (via->rport && via->rport->value.s){
550                         DBG("update_sock_struct_from_via: using 'rport'\n");
551                         port=str2s(via->rport->value.s, via->rport->value.len, &err);
552                         if (err){
553                                 LOG(L_NOTICE, "ERROR: update_sock_struct_from_via: bad rport value(%.*s)\n",
554                                                 via->rport->value.len, via->rport->value.s);
555                                 port=0;
556                         }
557                 }
558                 if (via->received){
559                         DBG("update_sock_struct_from_via: using 'received'\n");
560                         name=&(via->received->value);
561                         /* making sure that we won't do SRV lookup on "received"
562                          * (possible if no DNS_IP_HACK is used)*/
563                         if (port==0) port=via->port?via->port:SIP_PORT; 
564                 }else{
565                         DBG("update_sock_struct_from_via: using via host\n");
566                         name=&(via->host);
567                         if (port==0) port=via->port;
568                 }
569         }
570         /* we do now a malloc/memcpy because gethostbyname loves \0-terminated 
571            strings; -jiri 
572            but only if host is not null terminated
573            (host.s[len] will always be ok for a via)
574             BTW: when is via->host.s non null terminated? tm copy? - andrei 
575             Yes -- it happened on generating a 408 by TM; -jiri
576             sip_resolvehost now accepts str -janakj
577         */
578         DBG("update_sock_struct_from_via: trying SRV lookup\n");
579         proto=via->proto;
580         he=sip_resolvehost(name, &port, &proto);
581         
582         if (he==0){
583                 LOG(L_NOTICE, "ERROR:forward_reply:resolve_host(%.*s) failure\n",
584                                 name->len, name->s);
585                 return -1;
586         }
587                 
588         hostent2su(to, he, 0, port);
589         return 1;
590 }
591
592
593
594 /* removes first via & sends msg to the second */
595 int forward_reply(struct sip_msg* msg)
596 {
597         char* new_buf;
598         struct dest_info dst;
599         unsigned int new_len;
600         int r;
601 #ifdef USE_TCP
602         char* s;
603         int len;
604 #endif
605         init_dest_info(&dst);
606         new_buf=0;
607         /*check if first via host = us */
608         if (check_via){
609                 if (check_self(&msg->via1->host,
610                                         msg->via1->port?msg->via1->port:SIP_PORT,
611                                         msg->via1->proto)!=1){
612                         LOG(L_NOTICE, "ERROR: forward_reply: host in first via!=me :"
613                                         " %.*s:%d\n", msg->via1->host.len, msg->via1->host.s,
614                                                                         msg->via1->port);
615                         /* send error msg back? */
616                         goto error;
617                 }
618         }
619         
620         /* check modules response_f functions */
621         for (r=0; r<mod_response_cbk_no; r++)
622                 if (mod_response_cbks[r](msg)==0) goto skip;
623         /* we have to forward the reply stateless, so we need second via -bogdan*/
624         if (parse_headers( msg, HDR_VIA2_F, 0 )==-1 
625                 || (msg->via2==0) || (msg->via2->error!=PARSE_OK))
626         {
627                 /* no second via => error */
628                 LOG(L_ERR, "ERROR: forward_reply: no 2nd via found in reply\n");
629                 goto error;
630         }
631
632         new_buf = build_res_buf_from_sip_res( msg, &new_len);
633         if (!new_buf){
634                 LOG(L_ERR, "ERROR: forward_reply: building failed\n");
635                 goto error;
636         }
637
638         dst.proto=msg->via2->proto;
639         if (update_sock_struct_from_via( &dst.to, msg, msg->via2 )==-1) goto error;
640 #ifdef USE_COMP
641         dst.comp=msg->via2->comp_no;
642 #endif
643
644 #if defined USE_TCP || defined USE_SCTP
645         if (
646 #ifdef USE_TCP
647                         dst.proto==PROTO_TCP
648 #ifdef USE_TLS
649                         || dst.proto==PROTO_TLS
650 #endif
651 #ifdef USE_SCTP
652                         ||
653 #endif /* USE_SCTP */
654 #endif /* USE_TCP */
655 #ifdef USE_SCTP
656                         dst.proto==PROTO_SCTP
657 #endif /* USE_SCTP */
658                         ){
659                 /* find id in i param if it exists */
660                 if (msg->via1->i && msg->via1->i->value.s){
661                         s=msg->via1->i->value.s;
662                         len=msg->via1->i->value.len;
663                         DBG("forward_reply: i=%.*s\n",len, ZSW(s));
664                         if (reverse_hex2int(s, len, (unsigned int*)&dst.id)<0){
665                                 LOG(L_ERR, "ERROR: forward_reply: bad via i param \"%.*s\"\n",
666                                                 len, ZSW(s));
667                                         dst.id=0;
668                         }
669                 }               
670                                 
671         } 
672 #endif
673         if (msg_send(&dst, new_buf, new_len)<0) goto error;
674 #ifdef STATS
675         STATS_TX_RESPONSE(  (msg->first_line.u.reply.statuscode/100) );
676 #endif
677
678         DBG(" reply forwarded to %.*s:%d\n", 
679                         msg->via2->host.len, msg->via2->host.s,
680                         (unsigned short) msg->via2->port);
681
682         pkg_free(new_buf);
683 skip:
684         return 0;
685 error:
686         if (new_buf) pkg_free(new_buf);
687         return -1;
688 }