forward.c
888ca09d
 /*
53c7e0f1
  * Copyright (C) 2001-2003 FhG Fokus
7dd0b342
  *
6a0f4382
  * This file is part of Kamailio, a free SIP server.
7dd0b342
  *
6a0f4382
  * Kamailio is free software; you can redistribute it and/or modify
7dd0b342
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
6a0f4382
  * Kamailio is distributed in the hope that it will be useful,
7dd0b342
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
2d4b798e
  *
888ca09d
  */
 
1d0661db
 /*!
  * \file
6a0f4382
  * \brief Kamailio core :: Message forwarding
1d0661db
  * \ingroup core
  * Module: \ref core
  */
 
888ca09d
 
7dd0b342
 
888ca09d
 #include <string.h>
3e429f5c
 #include <stdio.h>
 #include <stdlib.h>
9a3dc64b
 #include <sys/types.h>
 #include <sys/socket.h>
e60a9728
 #include <netdb.h>
 #include <netinet/in.h>
1b1b19d8
 #include <arpa/inet.h>
888ca09d
 
 #include "forward.h"
caf80ae6
 #include "hash_func.h"
e60a9728
 #include "config.h"
3881f12c
 #include "parser/msg_parser.h"
817b9c5c
 #include "char_msg_val.h"
888ca09d
 #include "route.h"
878d2c6b
 #include "events.h"
888ca09d
 #include "dprint.h"
1b1b19d8
 #include "globals.h"
a4d17f82
 #include "cfg_core.h"
831faabf
 #include "data_lump.h"
104316b6
 #include "ut.h"
dda9dab1
 #include "mem/mem.h"
c2c6a6bf
 #include "msg_translator.h"
031e278e
 #include "sr_module.h"
4e2fdd79
 #include "ip_addr.h"
 #include "resolve.h"
e278821b
 #include "name_alias.h"
9f4c52ce
 #include "socket_info.h"
0d88ce78
 #include "onsend.h"
dcb59e67
 #include "resolve.h"
 #ifdef USE_DNS_FAILOVER
 #include "dns_cache.h"
 #endif
 #ifdef USE_DST_BLACKLIST
 #include "dst_blacklist.h"
 #endif
f479903f
 #include "compiler_opt.h"
6b109fa2
 #include "core_stats.h"
888ca09d
 
03150098
 #ifdef DEBUG_DMALLOC
 #include <dmalloc.h>
 #endif
 
a35ad52c
 
 
2d4b798e
 /* return a socket_info_pointer to the sending socket; as opposed to
  * get_send_socket, which returns process's default socket, get_out_socket
  * attempts to determine the outbound interface which will be used;
ead9f3f9
  * it uses a temporary connected socket to determine it; it will
2d4b798e
  * be very likely noticeably slower, but it can deal better with
  * multihomed hosts
  */
ead9f3f9
 
fe6207c8
 static int mhomed_sock_cache_disabled = 0;
 static int sock_inet = -1;
 static int sock_inet6 = -1;
9d9e7399
 static int _forward_set_send_info = 0;
 
 void forward_set_send_info(int v)
 {
 	_forward_set_send_info = v;
 }
ead9f3f9
 
bf48d463
 static void apply_force_send_socket(struct dest_info* dst, struct sip_msg* msg);
 
2d4b798e
 struct socket_info* get_out_socket(union sockaddr_union* to, int proto)
 {
fe6207c8
 	int* temp_sock;
2d4b798e
 	socklen_t len;
 	union sockaddr_union from; 
9f4c52ce
 	struct socket_info* si;
7a6d1ee1
 	struct ip_addr ip;
ef0dd4e3
 	union sockaddr_union uncon;
 
 	memset(&uncon, 0, sizeof(union sockaddr_union));
 	uncon.sin.sin_family = AF_UNSPEC;
2d4b798e
 
ced07a1f
 	if (unlikely(proto!=PROTO_UDP)) {
c68b4078
 		LM_CRIT("can only be called for UDP\n");
2d4b798e
 		return 0;
 	}
fe6207c8
 retry:
ead9f3f9
 	switch(to->s.sa_family){
 	case AF_INET : {
ced07a1f
 		if(unlikely(sock_inet < 0)){
ead9f3f9
 			sock_inet = socket(AF_INET, SOCK_DGRAM, 0);
 			if (sock_inet==-1) {
 				LM_ERR("socket() failed: %s\n", strerror(errno));
 				return 0;
 			}
 		}
fe6207c8
 		temp_sock = &sock_inet;
ead9f3f9
 		break;
 	}
 	case AF_INET6 : {
ced07a1f
 		if(unlikely(sock_inet6 < 0)){
ead9f3f9
 			sock_inet6 = socket(AF_INET6, SOCK_DGRAM, 0);
 			if (sock_inet6==-1) {
 				LM_ERR("socket() failed: %s\n", strerror(errno));
 				return 0;
 			}
 		}
fe6207c8
 		temp_sock = &sock_inet6;
ead9f3f9
 		break;
fe6207c8
 	}
ead9f3f9
 	default: {
fe6207c8
 		LM_ERR("Unknown protocol family \n");
2d4b798e
 		return 0;
 	}
ead9f3f9
 	}
ef0dd4e3
 
 	if( !mhomed_sock_cache_disabled ){
 		/* some Linux kernel versions (all?) along with other UNIXes don't re-bound the sock if already bound */
 		/* to un-bound a socket set sin_family to AF_UNSPEC and zero out the rest*/
6cfee826
 		if (unlikely(connect(*temp_sock, &uncon.s, sockaddru_len(uncon)) < 0))
ef0dd4e3
 				mhomed_sock_cache_disabled = 1;
 	}
 
ced07a1f
 	if (unlikely(connect(*temp_sock, &to->s, sockaddru_len(*to))==-1)) {
 		if (unlikely(errno==EISCONN && !mhomed_sock_cache_disabled)){
fe6207c8
 			/*  no multiple connects support on the same socket */
 			mhomed_sock_cache_disabled=1;
ced07a1f
 			if (sock_inet>=0){
 				close(sock_inet);
 				sock_inet=-1;
 			}
cd94c13e
 			if (sock_inet6>=0){
ced07a1f
 				close(sock_inet6);
 				sock_inet6=-1;
 			}
fe6207c8
 			goto retry;
 		}
c68b4078
 		LM_ERR("connect failed: %s\n", strerror(errno));
2d4b798e
 		goto error;
 	}
a9cd95b5
 	len=sizeof(from);
ced07a1f
 	if (unlikely(getsockname(*temp_sock, &from.s, &len)==-1)) {
c68b4078
 		LM_ERR("getsockname failed: %s\n", strerror(errno));
2d4b798e
 		goto error;
 	}
7a6d1ee1
 	su2ip_addr(&ip, &from);
 	si=find_si(&ip, 0, proto);
 	if (si==0) goto error;
bccfb2e3
 	LM_DBG("socket determined: %p\n", si );
fe6207c8
 	if (unlikely(mhomed_sock_cache_disabled)){
 		close(*temp_sock);
 		*temp_sock=-1;
 	}
7a6d1ee1
 	return si;
2d4b798e
 error:
c68b4078
 	LM_ERR("no socket found\n");
f50c1e9f
 	ERR("no corresponding socket found for(%s:%s)\n",
 			proto2a(proto), su2a(to, sizeof(*to)));
ced07a1f
 	if (unlikely(mhomed_sock_cache_disabled && *temp_sock >=0)){
fe6207c8
 		close(*temp_sock);
 		*temp_sock=-1;
 	}
9f4c52ce
 	return 0;
2d4b798e
 }
 
b2e71d5b
 
1b1b19d8
 
9981e48a
 /** get the sending socket for a corresponding destination.
  * @param force_send_socket - if !=0 and the protocol and af correspond
  *                            with the destination, it will be returned.
  *                            If the protocol or af check fail, a look-alike
  *                            socket will be searched for and mismatch will be
  *                            set. If no look-alike socket is found it will
  *                            fallback to normal resolution.
  * @param to - destination
  * @param proto - protocol
  * @param mismatch - result parameter, set if a force_send_socket was used, but
  *                   there was an error matching it exactly to the destination.
  *                   Possible values: 0 ok, SS_MISMATCH_PROTO,
  *                   SS_MISMATCH_ADDR, SS_MISMATCH_AF, SS_MISMATCH_MCAST.
  * @return a socket_info pointer to the sending socket on success (and possibly
  *         sets mismatch) or 0 on error.
36ef0329
  */
9981e48a
 struct socket_info* get_send_socket2(struct socket_info* force_send_socket,
 										union sockaddr_union* to, int proto,
 										enum ss_mismatch* mismatch)
36ef0329
 {
 	struct socket_info* send_sock;
20a8b62b
 	struct socket_info* orig;
a35ad52c
 	
9981e48a
 	if (likely(mismatch)) *mismatch=0;
a35ad52c
 	/* check if send interface is not forced */
9981e48a
 	if (unlikely(force_send_socket)){
1ccd1f68
 		orig=force_send_socket;
 		/* Special case here as there is no ;transport=wss - so wss connections will
 		   appear as ws ones and be sorted out in the WebSocket module */
 		if (unlikely(orig->proto!=proto && !(orig->proto==PROTO_TLS && proto==PROTO_WS))){
9981e48a
 			force_send_socket=find_si(&(force_send_socket->address),
 											force_send_socket->port_no,
a35ad52c
 											proto);
9981e48a
 			if (unlikely(force_send_socket == 0)){
 				if (likely(mismatch)) *mismatch=SS_MISMATCH_ADDR;
c68b4078
 				LM_WARN("protocol/port mismatch (forced %s:%s:%d, to %s:%s)\n",
20a8b62b
 						proto2a(orig->proto), ip_addr2a(&orig->address),
 						orig->port_no,
 						proto2a(proto), su2a(to, sizeof(*to)));
b2385677
 				goto not_forced;
 			}
9981e48a
 			if (likely(mismatch)) *mismatch=SS_MISMATCH_PROTO;
7a6d1ee1
 		}
9981e48a
 		if (unlikely(force_send_socket->address.af!=to->s.sa_family)){
bccfb2e3
 			LM_DBG("force_send_socket of different af"
20a8b62b
 					" (dst %d - %s:%s forced %d -%s:%s:%d)\n",
 					to->s.sa_family, proto2a(proto), su2a(to, sizeof(*to)),
 					force_send_socket->address.af,
 					proto2a(force_send_socket->proto),
 					ip_addr2a(&force_send_socket->address),
 					force_send_socket->port_no);
9981e48a
 			if (likely(mismatch)) *mismatch=SS_MISMATCH_AF;
878f9ca6
 			goto not_forced;
 		}
bc71de00
 		/* check if listening on the socket (the check does not work
 		   for TCP and TLS, for them socket==-1 on all the processes
 		   except tcp_main(), see close_extra_socks() */
 		if (likely((force_send_socket->socket!=-1 ||
 						force_send_socket->proto==PROTO_TCP ||
1e2f18da
 						force_send_socket->proto==PROTO_TLS ||
 						force_send_socket->proto==PROTO_WS  ||
 						force_send_socket->proto==PROTO_WSS) &&
9981e48a
 					!(force_send_socket->flags & SI_IS_MCAST)))
 				return force_send_socket;
f479903f
 		else{
9981e48a
 			if (!(force_send_socket->flags & SI_IS_MCAST))
c68b4078
 				LM_WARN("not listening on the requested socket (%s:%s:%d),"
20a8b62b
 							 " no fork mode?\n",
 							proto2a(force_send_socket->proto),
 							ip_addr2a(&force_send_socket->address),
 							force_send_socket->port_no);
9981e48a
 			else if (likely(mismatch)) *mismatch=SS_MISMATCH_MCAST;
f479903f
 		}
a35ad52c
 	};
b2385677
 not_forced:
7a6d1ee1
 	if (mhomed && proto==PROTO_UDP){
 		send_sock=get_out_socket(to, proto);
 		if ((send_sock==0) || (send_sock->socket!=-1))
 			return send_sock; /* found or error*/
 		else if (send_sock->socket==-1){
c68b4078
 			LM_WARN("not listening on the"
20a8b62b
 					" requested socket (%s:%s:%d), no fork mode?\n",
 					proto2a(send_sock->proto), ip_addr2a(&send_sock->address),
 					send_sock->port_no);
7a6d1ee1
 			/* continue: try to use some socket */
 		}
 	}
2d4b798e
 
36ef0329
 	send_sock=0;
 	/* check if we need to change the socket (different address families -
 	 * eg: ipv4 -> ipv6 or ipv6 -> ipv4) */
21e0d5c1
 	switch(proto){
f2f969dd
 #ifdef USE_TCP
8393efff
 		case PROTO_WS:
21e0d5c1
 		case PROTO_TCP:
f2f969dd
 		/* on tcp just use the "main address", we don't really now the
e1e6c914
 		 * sending address (we can find it out, but we'll need also to see
f2f969dd
 		 * if we listen on it, and if yes on which port -> too complicated*/
21e0d5c1
 			switch(to->s.sa_family){
 				/* FIXME */
 				case AF_INET:	send_sock=sendipv4_tcp;
 								break;
 				case AF_INET6:	send_sock=sendipv6_tcp;
 								break;
c68b4078
 				default:	LM_ERR("don't know how to forward to af %d\n",
 									to->s.sa_family);
21e0d5c1
 			}
 			break;
f2f969dd
 #endif
e9b02e8e
 #ifdef USE_TLS
1e2f18da
 		case PROTO_WSS:
e9b02e8e
 		case PROTO_TLS:
 			switch(to->s.sa_family){
 				/* FIXME */
 				case AF_INET:	send_sock=sendipv4_tls;
 								break;
 				case AF_INET6:	send_sock=sendipv6_tls;
 								break;
c68b4078
 				default:	LM_ERR("don't know how to forward to af %d\n",
 									to->s.sa_family);
e9b02e8e
 			}
 			break;
 #endif /* USE_TLS */
ed990c31
 #ifdef USE_SCTP
 		case PROTO_SCTP:
 			if ((bind_address==0) ||
 					(to->s.sa_family!=bind_address->address.af) ||
 					(bind_address->proto!=PROTO_SCTP)){
 				switch(to->s.sa_family){
 					case AF_INET:	send_sock=sendipv4_sctp;
 									break;
 					case AF_INET6:	send_sock=sendipv6_sctp;
 									break;
c68b4078
 					default:	LM_ERR("don't know how to forward to af %d\n",
ed990c31
 										to->s.sa_family);
 				}
 			}else send_sock=bind_address;
 			break;
 #endif /* USE_SCTP */
21e0d5c1
 		case PROTO_UDP:
ed990c31
 			if ((bind_address==0) ||
 					(to->s.sa_family!=bind_address->address.af) ||
 					(bind_address->proto!=PROTO_UDP)){
21e0d5c1
 				switch(to->s.sa_family){
 					case AF_INET:	send_sock=sendipv4;
 									break;
 					case AF_INET6:	send_sock=sendipv6;
 									break;
c68b4078
 					default:	LM_ERR("don't know how to forward to af %d\n",
21e0d5c1
 										to->s.sa_family);
 				}
 			}else send_sock=bind_address;
 			break;
 		default:
c68b4078
 			LM_CRIT("unsupported proto %d (%s)\n", proto, proto2a(proto));
21e0d5c1
 	}
36ef0329
 	return send_sock;
 }
 
1dfc197d
 static struct _check_self_func {
 	check_self_f fself;
 	struct _check_self_func *next;
 } *_check_self_func_list = NULL;
36ef0329
 
6a0f4382
 /** check if _check_self_func_list is set
a077f5e1
  * - return 1 if yes, 0 if no
  */
 int is_check_self_func_list_set(void)
 {
 	return (_check_self_func_list)?1:0;
 }
 
6a0f4382
 /** register a function to be called when matching for myself
1dfc197d
  * - return 0 on success, -1 on error
  * - f must have same prototype as check_self() and return same kind of values
  */
 int register_check_self_func(check_self_f f)
 {
 	struct _check_self_func *nf = 0;
 	nf=(struct _check_self_func*)pkg_malloc(sizeof(struct _check_self_func));
 	if(nf==0)
 	{
 		LM_ERR("no more pkg\n");
 		return -1;
 	}
 	nf->fself = f;
 	nf->next = _check_self_func_list;
 	_check_self_func_list = nf;
 	return 0;
 }
 
6a0f4382
 /** run registered check self functions
1dfc197d
  * returns 1 if true, 0 if false
  */
 int run_check_self_func(str* host, unsigned short port, unsigned short proto)
 {
 	struct _check_self_func *sf = 0;
 
 	if(_check_self_func_list==NULL)
 		return 0;
 	for(sf=_check_self_func_list; sf; sf=sf->next)
 		if(sf->fself(host, port, proto)==1)
 			return 1;
 	return 0;
 }
36ef0329
 
6a0f4382
 /** checks if the proto: host:port is one of the address we listen on;
  *
1baa06b5
  * if port==0, the  port number is ignored
50d5fa87
  * if proto==0 (PROTO_NONE) the protocol is ignored
1baa06b5
  * returns 1 if true, 0 if false, -1 on error
1f5206fd
  * WARNING: uses str2ip6 so it will overwrite any previous
  *  unsaved result of this function (static buffer)
  */
50d5fa87
 int check_self(str* host, unsigned short port, unsigned short proto)
855c2e68
 {
463fdeec
 	if (grep_sock_info(host, port, proto)) goto found;
9f4c52ce
 	/* try to look into the aliases*/
463fdeec
 	if (grep_aliases(host->s, host->len, port, proto)==0){
bccfb2e3
 		LM_DBG("host != me\n");
1dfc197d
 		return (_check_self_func_list==NULL)?0:run_check_self_func(host,
 														port, proto);
855c2e68
 	}
9f4c52ce
 found:
855c2e68
 	return 1;
 }
 
6a0f4382
 /** checks if the proto:port is one of the ports we listen on;
2bf9371d
  * if proto==0 (PROTO_NONE) the protocol is ignored
  * returns 1 if true, 0 if false, -1 on error
  */
 int check_self_port(unsigned short port, unsigned short proto)
 {
 	if (grep_sock_info_by_port(port, proto))
 		/* as aliases do not contain different ports we can skip them */
 		return 1;
 	else
 		return 0;
 }
 
855c2e68
 
e278821b
 
6a0f4382
 /** forwards a request to dst
2f0f1a30
  * parameters:
  *   msg       - sip msg
dcb59e67
  *   dst       - destination name, if non-null it will be resolved and
  *               send_info updated with the ip/port. Even if dst is non
  *               null send_info must contain the protocol and if a non
  *               default port or non srv. lookup is desired, the port must
  *               be !=0 
  *   port      - used only if dst!=0 (else the port in send_info->to is used)
67c9277a
  *   send_info - value/result partially filled dest_info structure:
  *                 - send_info->proto and comp are used
  *                 - send_info->to will be filled (dns)
  *                 - send_info->send_flags is filled from the message
  *                 - if the send_socket member is null, a send_socket will be 
  *                   chosen automatically
dcb59e67
  * WARNING: don't forget to zero-fill all the  unused members (a non-zero 
2f0f1a30
  * random id along with proto==PROTO_TCP can have bad consequences, same for
dcb59e67
  *   a bogus send_socket value)
e6a2b12e
  */
dcb59e67
 int forward_request(struct sip_msg* msg, str* dst, unsigned short port,
 							struct dest_info* send_info)
888ca09d
 {
c2c6a6bf
 	unsigned int len;
ac79e819
 	char* buf;
caf80ae6
 	char md5[MD5_LEN];
dcb59e67
 	struct socket_info* orig_send_sock; /* initial send_sock */
 	int ret;
 	struct ip_addr ip; /* debugging only */
f62c96d8
 	char proto;
9d9e7399
 	struct onsend_info onsnd_info = {0};
dcb59e67
 #ifdef USE_DNS_FAILOVER
 	struct socket_info* prev_send_sock;
 	int err;
 	struct dns_srv_handle dns_srv_h;
36ef0329
 	
dcb59e67
 	prev_send_sock=0;
 	err=0;
 #endif
36ef0329
 	
dcb59e67
 	
 	buf=0;
 	orig_send_sock=send_info->send_sock;
f62c96d8
 	proto=send_info->proto;
dcb59e67
 	ret=0;
caf80ae6
 
dcb59e67
 	if(dst){
 #ifdef USE_DNS_FAILOVER
2cfcc6bb
 		if (cfg_get(core, core_cfg, use_dns_failover)){
dcb59e67
 			dns_srv_handle_init(&dns_srv_h);
 			err=dns_sip_resolve2su(&dns_srv_h, &send_info->to, dst, port,
f62c96d8
 									&proto, dns_flags);
dcb59e67
 			if (err!=0){
c68b4078
 				LM_ERR("resolving \"%.*s\" failed: %s [%d]\n",
 						dst->len, ZSW(dst->s), dns_strerror(err), err);
dcb59e67
 				ret=E_BAD_ADDRESS;
 				goto error;
 			}
 		}else
 #endif
f62c96d8
 		if (sip_hostport2su(&send_info->to, dst, port, &proto)<0){
c68b4078
 			LM_ERR("bad host name %.*s, dropping packet\n", dst->len, ZSW(dst->s));
dcb59e67
 			ret=E_BAD_ADDRESS;
 			goto error;
 		}
 	}/* dst */
67c9277a
 	send_info->send_flags=msg->fwd_send_flags;
6287caec
 	/* calculate branch for outbound request;
caf80ae6
 	   calculate is from transaction key, i.e., as an md5 of From/To/CallID/
 	   CSeq exactly the same way as TM does; good for reboot -- than messages
 	   belonging to transaction lost due to reboot will still be forwarded
 	   with the same branch parameter and will be match-able downstream
 	*/
6287caec
 	if (!char_msg_val( msg, md5 )) 	{ /* parses transaction key */
c68b4078
 		LM_ERR("char_msg_val failed\n");
6287caec
 		ret=E_UNSPEC;
 		goto error;
 	}
 	msg->hash_index=hash( msg->callid->body, get_cseq(msg)->number);
 	if (!branch_builder( msg->hash_index, 0, md5, 0 /* 0-th branch */,
 				msg->add_to_branch_s, &msg->add_to_branch_len )) {
c68b4078
 		LM_ERR("branch_builder failed\n");
6287caec
 		ret=E_UNSPEC;
 		goto error;
caf80ae6
 	}
dcb59e67
 	/* try to send the message until success or all the ips are exhausted
45974eba
 	 *  (if dns lookup is performed && the dns cache used ) */
dcb59e67
 #ifdef USE_DNS_FAILOVER
 	do{
 #endif
 		if (orig_send_sock==0) /* no forced send_sock => find it **/
f62c96d8
 			send_info->send_sock=get_send_socket(msg, &send_info->to, proto);
dcb59e67
 		if (send_info->send_sock==0){
c68b4078
 			LM_ERR("cannot forward to af %d, proto %d "
dcb59e67
 						"no corresponding listening socket\n",
f62c96d8
 						send_info->to.s.sa_family, proto);
dcb59e67
 			ret=ser_error=E_NO_SOCKET;
 #ifdef USE_DNS_FAILOVER
 			/* continue, maybe we find a socket for some other ip */
 			continue;
 #else
 			goto error;
 #endif
 		}
36ef0329
 	
dcb59e67
 #ifdef USE_DNS_FAILOVER
 		if (prev_send_sock!=send_info->send_sock){
 			/* rebuild the message only if the send_sock changed */
 			prev_send_sock=send_info->send_sock;
 #endif
 			if (buf) pkg_free(buf);
f62c96d8
 			send_info->proto=proto;
61f86410
 			buf = build_req_buf_from_sip_req(msg, &len, send_info, 0);
dcb59e67
 			if (!buf){
c68b4078
 				LM_ERR("building failed\n");
dcb59e67
 				ret=E_OUT_OF_MEM; /* most probable */
 				goto error;
 			}
 #ifdef USE_DNS_FAILOVER
 		}
 #endif
 		 /* send it! */
bccfb2e3
 		LM_DBG("Sending:\n%.*s.\n", (int)len, buf);
 		LM_DBG("orig. len=%d, new_len=%d, proto=%d\n",
dcb59e67
 				msg->len, len, send_info->proto );
 	
 		if (run_onsend(msg, send_info, buf, len)==0){
 			su2ip_addr(&ip, &send_info->to);
c68b4078
 			LM_INFO("request to %s:%d(%d) dropped (onsend_route)\n",
 				ip_addr2a(&ip), su_getport(&send_info->to), send_info->proto);
dcb59e67
 			ser_error=E_OK; /* no error */
 			ret=E_ADM_PROHIBITED;
 #ifdef USE_DNS_FAILOVER
 			continue; /* try another ip */
 #else
 			goto error; /* error ? */
 #endif
 		}
 #ifdef USE_DST_BLACKLIST
a4d17f82
 		if (cfg_get(core, core_cfg, use_dst_blacklist)){
7a64325c
 			if (dst_is_blacklisted(send_info, msg)){
dcb59e67
 				su2ip_addr(&ip, &send_info->to);
c68b4078
 				LM_DBG("blacklisted destination:%s:%d (%d)\n",
 					ip_addr2a(&ip), su_getport(&send_info->to), send_info->proto);
dcb59e67
 				ret=ser_error=E_SEND;
 #ifdef USE_DNS_FAILOVER
 				continue; /* try another ip */
 #else
 				goto error;
 #endif
 			}
 		}
 #endif
9d9e7399
 
 		if(unlikely(_forward_set_send_info==1)) {
 			onsnd_info.to=&send_info->to;
 			onsnd_info.send_sock=send_info->send_sock;
 			onsnd_info.buf=buf;
 			onsnd_info.len=len;
 			onsnd_info.msg=msg;
 			p_onsend=&onsnd_info;
 		}
 
dcb59e67
 		if (msg_send(send_info, buf, len)<0){
9d9e7399
 			p_onsend=0;
dcb59e67
 			ret=ser_error=E_SEND;
 #ifdef USE_DST_BLACKLIST
a6c250c0
 			(void)dst_blacklist_add(BLST_ERR_SEND, send_info, msg);
dcb59e67
 #endif
 #ifdef USE_DNS_FAILOVER
 			continue; /* try another ip */
 #else
 			goto error;
 #endif
 		}else{
9d9e7399
 			p_onsend=0;
dcb59e67
 			ret=ser_error=E_OK;
 			/* sent requests stats */
 			STATS_TX_REQUEST(  msg->first_line.u.request.method_value );
 			/* exit succcesfully */
 			goto end;
 		}
 #ifdef USE_DNS_FAILOVER
2cfcc6bb
 	}while(dst && cfg_get(core, core_cfg, use_dns_failover) &&
 			dns_srv_handle_next(&dns_srv_h, err) && 
dcb59e67
 			((err=dns_sip_resolve2su(&dns_srv_h, &send_info->to, dst, port,
f62c96d8
 										&proto, dns_flags))==0));
dcb59e67
 	if ((err!=0) && (err!=-E_DNS_EOR)){
c68b4078
 		LM_ERR("resolving %.*s host name in uri failed: %s [%d] (dropping packet)\n",
dcb59e67
 									dst->len, ZSW(dst->s),
 									dns_strerror(err), err);
 		ret=ser_error=E_BAD_ADDRESS;
0d88ce78
 		goto error;
c2c6a6bf
 	}
dcb59e67
 #endif
c030910c
 	
888ca09d
 error:
dcb59e67
 	STATS_TX_DROPS;
 end:
 #ifdef USE_DNS_FAILOVER
2cfcc6bb
 	if (dst && cfg_get(core, core_cfg, use_dns_failover)){
dcb59e67
 				dns_srv_handle_put(&dns_srv_h);
 	}
 #endif
e22bbdb8
 	if (buf) pkg_free(buf);
dcb59e67
 	/* received_buf & line_buf will be freed in receive_msg by free_lump_list*/
6b109fa2
 #if defined STATS_REQ_FWD_OK || defined STATS_REQ_FWD_DROP
878d2c6b
 	if(ret==0)
6b109fa2
 		STATS_REQ_FWD_OK();
45a368cb
 	else
6b109fa2
 		STATS_REQ_FWD_DROP();
 #endif /* STATS_REQ_FWD_* */
dcb59e67
 	return ret;
888ca09d
 }
 
 
caf80ae6
 
92b6f40f
 int update_sock_struct_from_via( union sockaddr_union* to,
181e7cc0
 								 struct sip_msg* msg,
4e2fdd79
 								 struct via_body* via )
68a3fc65
 {
ac79e819
 	struct hostent* he;
5bc22c81
 	str* name;
e1e6c914
 	int err;
5bc22c81
 	unsigned short port;
dd4ffbb7
 	char proto;
68a3fc65
 
e1e6c914
 	port=0;
181e7cc0
 	if(via==msg->via1){ 
 		/* _local_ reply, we ignore any rport or received value
 		 * (but we will send back to the original port if rport is
 		 *  present) */
3e8c3475
 		if ((msg->msg_flags&FL_FORCE_RPORT)||(via->rport))
 			port=msg->rcv.src_port;
181e7cc0
 		else port=via->port;
 		name=&(via->host); /* received=ip in 1st via is ignored (it's
 							  not added by us so it's bad) */
5bc22c81
 	}else{
181e7cc0
 		/* "normal" reply, we use rport's & received value if present */
 		if (via->rport && via->rport->value.s){
bccfb2e3
 			LM_DBG("using 'rport'\n");
181e7cc0
 			port=str2s(via->rport->value.s, via->rport->value.len, &err);
 			if (err){
c68b4078
 				LM_ERR("bad rport value(%.*s)\n",
181e7cc0
 						via->rport->value.len, via->rport->value.s);
 				port=0;
 			}
 		}
 		if (via->received){
bccfb2e3
 			LM_DBG("using 'received'\n");
181e7cc0
 			name=&(via->received->value);
 			/* making sure that we won't do SRV lookup on "received"
 			 * (possible if no DNS_IP_HACK is used)*/
 			if (port==0) port=via->port?via->port:SIP_PORT; 
 		}else{
bccfb2e3
 			LM_DBG("using via host\n");
181e7cc0
 			name=&(via->host);
 			if (port==0) port=via->port;
 		}
5bc22c81
 	}
4c9f3fde
 	/* we do now a malloc/memcpy because gethostbyname loves \0-terminated 
 	   strings; -jiri 
 	   but only if host is not null terminated
 	   (host.s[len] will always be ok for a via)
 	    BTW: when is via->host.s non null terminated? tm copy? - andrei 
 	    Yes -- it happened on generating a 408 by TM; -jiri
a6982b85
 	    sip_resolvehost now accepts str -janakj
4c9f3fde
 	*/
bccfb2e3
 	LM_DBG("trying SRV lookup\n");
dd4ffbb7
 	proto=via->proto;
 	he=sip_resolvehost(name, &port, &proto);
4c9f3fde
 	
 	if (he==0){
c68b4078
 		LM_NOTICE("resolve_host(%.*s) failure\n", name->len, name->s);
4c9f3fde
 		return -1;
68a3fc65
 	}
e1e6c914
 		
6eacb2bc
 	hostent2su(to, he, 0, port);
68a3fc65
 	return 1;
 }
 
888ca09d
 
c030910c
 
6a0f4382
 /** removes first via & sends msg to the second
9cb17369
  * - mode param controls if modules sip response callbacks are executed */
 static int do_forward_reply(struct sip_msg* msg, int mode)
888ca09d
 {
ac79e819
 	char* new_buf;
e6a2b12e
 	struct dest_info dst;
8cc98445
 	unsigned int new_len;
55faef7d
 	int r;
214b161e
 	struct ip_addr ip;
f2f969dd
 #ifdef USE_TCP
 	char* s;
 	int len;
 #endif
2f0f1a30
 	init_dest_info(&dst);
ac79e819
 	new_buf=0;
1b1b19d8
 	/*check if first via host = us */
 	if (check_via){
1baa06b5
 		if (check_self(&msg->via1->host,
50d5fa87
 					msg->via1->port?msg->via1->port:SIP_PORT,
 					msg->via1->proto)!=1){
c68b4078
 			LM_ERR("host in first via!=me : %.*s:%d\n",
 				msg->via1->host.len, msg->via1->host.s, msg->via1->port);
1b1b19d8
 			/* send error msg back? */
 			goto error;
 		}
 	}
55faef7d
 	
 	/* check modules response_f functions */
9cb17369
 	if(likely(mode==0)) {
 		for (r=0; r<mod_response_cbk_no; r++)
 			if (mod_response_cbks[r](msg)==0) goto skip;
 	}
b908cdeb
 	/* we have to forward the reply stateless, so we need second via -bogdan*/
5c28a534
 	if (parse_headers( msg, HDR_VIA2_F, 0 )==-1 
caf80ae6
 		|| (msg->via2==0) || (msg->via2->error!=PARSE_OK))
b908cdeb
 	{
 		/* no second via => error */
c68b4078
 		LM_DBG("reply cannot be forwarded - no 2nd via\n");
b908cdeb
 		goto error;
 	}
 
5843cd1b
 	new_buf = build_res_buf_from_sip_res( msg, &new_len);
8cc98445
 	if (!new_buf){
c68b4078
 		LM_ERR("building failed\n");
888ca09d
 		goto error;
 	}
8cc98445
 
e6a2b12e
 	dst.proto=msg->via2->proto;
5d6752dc
 	SND_FLAGS_OR(&dst.send_flags, &msg->fwd_send_flags, &msg->rpl_send_flags);
e6a2b12e
 	if (update_sock_struct_from_via( &dst.to, msg, msg->via2 )==-1) goto error;
7a068ff3
 #ifdef USE_COMP
 	dst.comp=msg->via2->comp_no;
 #endif
c030910c
 
31fd952b
 #if defined USE_TCP || defined USE_SCTP
 	if (
f2f969dd
 #ifdef USE_TCP
31fd952b
 			dst.proto==PROTO_TCP
1e2f18da
 			|| dst.proto==PROTO_WS
e9b02e8e
 #ifdef USE_TLS
e6a2b12e
 			|| dst.proto==PROTO_TLS
1e2f18da
 			|| dst.proto==PROTO_WSS
e9b02e8e
 #endif
31fd952b
 #ifdef USE_SCTP
 			||
 #endif /* USE_SCTP */
 #endif /* USE_TCP */
 #ifdef USE_SCTP
 			dst.proto==PROTO_SCTP
 #endif /* USE_SCTP */
e9b02e8e
 			){
e1e6c914
 		/* find id in i param if it exists */
e6a2b12e
 		if (msg->via1->i && msg->via1->i->value.s){
e1e6c914
 			s=msg->via1->i->value.s;
 			len=msg->via1->i->value.len;
bccfb2e3
 			LM_DBG("i=%.*s\n",len, ZSW(s));
e6a2b12e
 			if (reverse_hex2int(s, len, (unsigned int*)&dst.id)<0){
c68b4078
 				LM_ERR("bad via i param \"%.*s\"\n", len, ZSW(s));
 				dst.id=0;
33d814e0
 			}
f2f969dd
 		}		
 				
 	} 
 #endif
bf48d463
 
 	apply_force_send_socket(&dst, msg);
 
cd335add
 	/* call onsend_route */
 	if(dst.send_sock == NULL) {
 		dst.send_sock=get_send_socket(msg, &dst.to, dst.proto);
 		if (dst.send_sock==0){
c68b4078
 			LM_ERR("cannot forward reply\n");
cd335add
 			goto done;
 		}
 	}
214b161e
 	if (onsend_route_enabled(SIP_REPLY)){
 		if (run_onsend(msg, &dst, new_buf, new_len)==0){
 			su2ip_addr(&ip, &(dst.to));
 			LOG(L_ERR, "forward_reply: reply to %s:%d(%d) dropped"
 					" (onsend_route)\n", ip_addr2a(&ip),
 						su_getport(&(dst.to)), dst.proto);
 			goto error; /* error ? */
 		}
 	}
 
 	if (msg_send(&dst, new_buf, new_len)<0)
 	{
 		STATS_RPL_FWD_DROP();
 		goto error;
 	}
cd335add
 
 	done:
9598488f
 #ifdef STATS
c030910c
 	STATS_TX_RESPONSE(  (msg->first_line.u.reply.statuscode/100) );
9598488f
 #endif
b908cdeb
 
bccfb2e3
 	LM_DBG("reply forwarded to %.*s:%d\n", 
ab130758
 			msg->via2->host.len, msg->via2->host.s,
 			(unsigned short) msg->via2->port);
b908cdeb
 
6b109fa2
 	STATS_RPL_FWD_OK();
e22bbdb8
 	pkg_free(new_buf);
031e278e
 skip:
888ca09d
 	return 0;
 error:
e22bbdb8
 	if (new_buf) pkg_free(new_buf);
888ca09d
 	return -1;
 }
bf48d463
 
6a0f4382
 /** removes first via & sends msg to the second */
9cb17369
 int forward_reply(struct sip_msg* msg)
 {
 	return do_forward_reply(msg, 0);
 }
 
6a0f4382
 /** removes first via & sends msg to the second - no module callbacks */
9cb17369
 int forward_reply_nocb(struct sip_msg* msg)
 {
 	return do_forward_reply(msg, 1);
 }
 
bf48d463
 static void apply_force_send_socket(struct dest_info* dst, struct sip_msg* msg)
 {
 	if (msg->force_send_socket != 0) {
 		dst->send_sock = get_send_socket(msg, &dst->to, dst->proto);
 	}
 }