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