forward.c
888ca09d
 /*
  * $Id$
  */
 
 
 #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"
888ca09d
 #include "route.h"
 #include "dprint.h"
e60a9728
 #include "udp_server.h"
1b1b19d8
 #include "globals.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"
dda9dab1
 #include "stats.h"
4e2fdd79
 #include "ip_addr.h"
 #include "resolve.h"
888ca09d
 
03150098
 #ifdef DEBUG_DMALLOC
 #include <dmalloc.h>
 #endif
 
b2e71d5b
 
1b1b19d8
 
36ef0329
 /* returns a socket_info pointer to the sending socket or 0 on error
  * params: destination socke_union pointer
  */
 struct socket_info* get_send_socket(union sockaddr_union* to)
 {
 	struct socket_info* send_sock;
 	
 	send_sock=0;
 	/* check if we need to change the socket (different address families -
 	 * eg: ipv4 -> ipv6 or ipv6 -> ipv4) */
 	if (to->s.sa_family!=bind_address->address.af){
 		switch(to->s.sa_family){
 			case AF_INET:	send_sock=sendipv4;
 							break;
 #ifdef USE_IPV6
 			case AF_INET6:	send_sock=sendipv6;
 							break;
 #endif
 			default:		LOG(L_ERR, "get_send_socket: BUG: don't know how"
 									" to forward to af %d\n", to->s.sa_family);
 		}
 	}else send_sock=bind_address;
 	return send_sock;
 }
 
 
 
3e429f5c
 int forward_request( struct sip_msg* msg, struct proxy_l * p)
888ca09d
 {
c2c6a6bf
 	unsigned int len;
ac79e819
 	char* buf;
4e2fdd79
 	union sockaddr_union* to;
36ef0329
 	struct socket_info* send_sock;
caf80ae6
 	char md5[MD5_LEN];
36ef0329
 	
ac79e819
 	to=0;
36ef0329
 	buf=0;
 	
4e2fdd79
 	to=(union sockaddr_union*)malloc(sizeof(union sockaddr_union));
dc862225
 	if (to==0){
1400b772
 		ser_error=E_OUT_OF_MEM;
68a3fc65
 		LOG(L_ERR, "ERROR: forward_request: out of memory\n");
dc862225
 		goto error;
 	}
36ef0329
 	
 	
e60a9728
 	/* if error try next ip address if possible */
4ac74c03
 	if (p->ok==0){
 		if (p->host.h_addr_list[p->addr_idx+1])
 			p->addr_idx++;
4e2fdd79
 		else p->addr_idx=0;
4ac74c03
 		p->ok=1;
e60a9728
 	}
36ef0329
 	
4e2fdd79
 	hostent2su(to, &p->host, p->addr_idx, 
 				(p->port)?htons(p->port):htons(SIP_PORT));
4ac74c03
 	p->tx++;
c2c6a6bf
 	p->tx_bytes+=len;
36ef0329
 	
5b253cc6
 
36ef0329
 	send_sock=get_send_socket(to);
 	if (send_sock==0){
 		LOG(L_ERR, "forward_req: ERROR: cannot forward to af %d "
 				"no coresponding listening socket\n", to->s.sa_family);
1400b772
 		ser_error=E_NO_SOCKET;
caf80ae6
 		goto error1;
36ef0329
 	}
caf80ae6
 
 	/* calculate branch for outbound request;  if syn_branch is turned off,
 	   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
 
        if it is turned on, we don't care about reboot; we simply put a simple
 	   value in there; better for performance
 	*/
 
 	if (syn_branch ) {
 		*msg->add_to_branch_s='0';
 		msg->add_to_branch_len=1;
 	} else {
 		if (!char_msg_val( msg, md5 )) 	{ /* parses transaction key */
 			LOG(L_ERR, "ERROR: forward_request: char_msg_val failed\n");
 			goto error1;
 		}
 		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 )) {
 			LOG(L_ERR, "ERROR: forward_request: branch_builder failed\n");
 			goto error1;
 		}
 	}
 
36ef0329
 	buf = build_req_buf_from_sip_req( msg, &len, send_sock);
 	if (!buf){
caf80ae6
 		LOG(L_ERR, "ERROR: forward_request: building failed\n");
 		goto error1;
36ef0329
 	}
 	 /* send it! */
 	DBG("Sending:\n%s.\n", buf);
 	DBG("orig. len=%d, new_len=%d\n", msg->len, len );
 	
6a8606d9
 	if (udp_send(send_sock, buf, len,  to,
36ef0329
 							sizeof(union sockaddr_union))==-1){
1400b772
 			ser_error=E_SEND;
4ac74c03
 			p->errors++;
 			p->ok=0;
dda9dab1
 			STATS_TX_DROPS;
caf80ae6
 			goto error1;
c2c6a6bf
 	}
f571aa35
 	/* sent requests stats */
dda9dab1
 	else STATS_TX_REQUEST(  msg->first_line.u.request.method_value );
e22bbdb8
 	pkg_free(buf);
dc862225
 	free(to);
831faabf
 	/* received_buf & line_buf will be freed in receiv_msg by free_lump_list*/
888ca09d
 	return 0;
caf80ae6
 
 error1:
 	free(to);
888ca09d
 error:
e22bbdb8
 	if (buf) pkg_free(buf);
888ca09d
 	return -1;
 }
 
 
caf80ae6
 int update_sock_struct_from_ip( union sockaddr_union* to,
 	struct sip_msg *msg )
 {
 	to->sin.sin_port=(msg->via1->port)
 		?htons(msg->via1->port): htons(SIP_PORT);
 	to->sin.sin_family=msg->src_ip.af;
 	memcpy(&to->sin.sin_addr, &msg->src_ip.u, msg->src_ip.len);
 
 	return 1;
 }
 
92b6f40f
 int update_sock_struct_from_via( union sockaddr_union* to,
4e2fdd79
 								 struct via_body* via )
68a3fc65
 {
ac79e819
 	struct hostent* he;
bea9a486
 	char *host_copy;
5bc22c81
 	str* name;
 	unsigned short port;
68a3fc65
 
 
 #ifdef DNS_IP_HACK
c64e586d
 	int err;
 	unsigned int ip;
5bc22c81
 #endif
 
c64e586d
 
5bc22c81
 	if (via->received){
 		DBG("update_sock_struct_from_via: using 'received'\n");
 		name=&(via->received->value);
 		/* making sure that we won't do SRV lookup on "received"
 		 * (possible if no DNS_IP_HACK is used)*/
 		port=via->port?via->port:SIP_PORT; 
 	}else{
 		DBG("update_sock_struct_from_via: using via host\n");
 		name=&(via->host);
 		port=via->port;
 	}
 #ifdef DNS_IP_HACK
 	ip=str2ip((unsigned char*)name->s, name->len,&err);
4e2fdd79
 	if (err==0){
 		to->sin.sin_family=AF_INET;
 		to->sin.sin_port=(via->port)?htons(via->port): htons(SIP_PORT);
 		memcpy(&to->sin.sin_addr, (char*)&ip, 4);
 	}else
68a3fc65
 #endif
 	{
1f377e97
 		/* we do now a malloc/memcpy because gethostbyname loves \0-terminated 
4e2fdd79
 		   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 
7fab4681
 			Yes -- it happened on generating a 408 by TM; -jiri
4e2fdd79
 		*/
5bc22c81
 		if (name->s[name->len]){
 			host_copy=pkg_malloc( name->len+1 );
12b21cda
 			if (!host_copy) {
1b71270a
 				LOG(L_NOTICE, "ERROR: update_sock_struct_from_via:"
 								" not enough memory\n");
4e2fdd79
 				return -1;
 			}
5bc22c81
 			memcpy(host_copy, name->s, name->len );
 			host_copy[name->len]=0;
 			DBG("update_sock_struct_from_via: trying SRV lookup\n");
 			he=sip_resolvehost(host_copy, &port);
 			
bea9a486
 			pkg_free( host_copy );
4e2fdd79
 		}else{
5bc22c81
 			DBG("update_sock_struct_from_via: trying SRV lookup\n");
 			he=sip_resolvehost(name->s, &port);
1f377e97
 		}
5bc22c81
 		
68a3fc65
 		if (he==0){
5bc22c81
 			LOG(L_NOTICE, "ERROR:forward_reply:resolve_host(%s) failure\n",
 					name->s);
68a3fc65
 			return -1;
 		}
5bc22c81
 		hostent2su(to, he, 0, htons(port));
68a3fc65
 	}
 	return 1;
 }
 
888ca09d
 
 /* removes first via & sends msg to the second */
4ac74c03
 int forward_reply(struct sip_msg* msg)
888ca09d
 {
8cc98445
 	int  r;
ac79e819
 	char* new_buf;
4e2fdd79
 	union sockaddr_union* to;
36ef0329
 	struct socket_info* send_sock;
8cc98445
 	unsigned int new_len;
031e278e
 	struct sr_module *mod;
e22bbdb8
 	
ac79e819
 	to=0;
 	new_buf=0;
1b1b19d8
 	/*check if first via host = us */
 	if (check_via){
36ef0329
 		for (r=0; r<sock_no; r++)
5d71ce1e
 		{
 			DBG("forward_reply - checking if via==us: %d==%d && "
 					" [%.*s] == [%.*s]\n", 
 					msg->via1->host.len,
 					sock_info[r].name.len,
 					msg->via1->host.len, msg->via1->host.s,
 					sock_info[r].name.len, sock_info[r].name.s
 				);
36ef0329
 			if ( (msg->via1->host.len==sock_info[r].name.len) && 
cf5f65d6
 	#ifdef USE_IPV6
 					(strncasecmp(msg->via1->host.s, sock_info[r].name.s,
 								 sock_info[r].name.len)==0) /*slower*/
 	#else
36ef0329
 					(memcmp(msg->via1->host.s, sock_info[r].name.s, 
cf5f65d6
 										sock_info[r].name.len)==0)
 	#endif
 					)
36ef0329
 				break;
5d71ce1e
 		}
36ef0329
 		if (r==sock_no){
f8d46776
 			LOG(L_NOTICE, "ERROR: forward_reply: host in first via!=me :"
36ef0329
 					" %.*s\n", msg->via1->host.len, msg->via1->host.s);
1b1b19d8
 			/* send error msg back? */
 			goto error;
 		}
 	}
031e278e
 	/* quick hack, slower for mutliple modules*/
 	for (mod=modules;mod;mod=mod->next){
 		if ((mod->exports) && (mod->exports->response_f)){
 			DBG("forward_reply: found module %s, passing reply to it\n",
 					mod->exports->name);
 			if (mod->exports->response_f(msg)==0) goto skip;
 		}
 	}
caf80ae6
 
b908cdeb
 	/* we have to forward the reply stateless, so we need second via -bogdan*/
caf80ae6
 	if (parse_headers( msg, HDR_VIA2, 0 )==-1 
 		|| (msg->via2==0) || (msg->via2->error!=PARSE_OK))
b908cdeb
 	{
 		/* no second via => error */
 		LOG(L_ERR, "ERROR: forward_msg: no 2nd via found in reply\n");
 		goto error;
 	}
 
4e2fdd79
 	to=(union sockaddr_union*)malloc(sizeof(union sockaddr_union));
031e278e
 	if (to==0){
 		LOG(L_ERR, "ERROR: forward_reply: out of memory\n");
 		goto error;
 	}
8cc98445
 
5843cd1b
 	new_buf = build_res_buf_from_sip_res( msg, &new_len);
8cc98445
 	if (!new_buf){
 		LOG(L_ERR, "ERROR: forward_reply: building failed\n");
888ca09d
 		goto error;
 	}
8cc98445
 
68a3fc65
 	if (update_sock_struct_from_via( to, msg->via2 )==-1) goto error;
36ef0329
 	send_sock=get_send_socket(to);
 	if (send_sock==0){
 		LOG(L_ERR, "forward_reply: ERROR: no sending socket found\n");
 		goto error;
 	}
5b253cc6
 
36ef0329
 	if (udp_send(send_sock, new_buf,new_len,  to,
4e2fdd79
 				sizeof(union sockaddr_union))==-1)
f571aa35
 	{
dda9dab1
 		STATS_TX_DROPS;
e60a9728
 		goto error;
b908cdeb
 	} else {
9598488f
 #ifdef STATS
 		int j = msg->first_line.u.reply.statuscode/100;
 		STATS_TX_RESPONSE(  j );
 #endif
 	}
b908cdeb
 
 	DBG(" reply forwarded to %s:%d\n",msg->via2->host.s,
 		(unsigned short) msg->via2->port);
 
e22bbdb8
 	pkg_free(new_buf);
dc862225
 	free(to);
031e278e
 skip:
888ca09d
 	return 0;
 error:
e22bbdb8
 	if (new_buf) pkg_free(new_buf);
dc862225
 	if (to) free(to);
888ca09d
 	return -1;
 }