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