proxy.c
a15c363f
 /*
  * $Id$
  *
  * proxy list & assoc. functions
  *
7dd0b342
  *
53c7e0f1
  * Copyright (C) 2001-2003 FhG Fokus
7dd0b342
  *
  * This file is part of ser, a free SIP server.
  *
  * ser is free software; you can redistribute it and/or modify
  * 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
  *
  * For a license to use the ser software under conditions
  * other than those described here, or to purchase support for this
  * software, please contact iptel.org by e-mail at the following addresses:
  *    info@iptel.org
  *
  * ser is distributed in the hope that it will be useful,
  * 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 
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
a15c363f
  */
d531a5d5
  /*
   * History:
   * -------
a7e13a89
   *  2003-02-13  all *proxy functions are now proto aware (andrei)
e3dccdc9
   *  2003-03-19  replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
d531a5d5
   */
a15c363f
 
1d0661db
 /*!
  * \file
  * \brief SIP-router core :: 
  * \ingroup core
  * Module: \ref core
  */
 
a15c363f
 
7dd0b342
 
1b71270a
 #include "config.h"
a15c363f
 #include "proxy.h"
 #include "error.h"
4ac74c03
 #include "dprint.h"
e3dccdc9
 #include "mem/mem.h"
346a9a05
 #include "mem/shm_mem.h"
a15c363f
 
 #include <string.h>
3e429f5c
 #include <stdlib.h>
f65e6f3d
 #include <sys/socket.h>
a15c363f
 
104316b6
 #ifdef DNS_IP_HACK
 #include "ut.h"
 #endif
 
4e2fdd79
 #include "resolve.h"
 #include "ip_addr.h"
1400b772
 #include "globals.h"
4e2fdd79
 
a15c363f
 
 struct proxy_l* proxies=0;
 
 
 
d531a5d5
 /* searches for the proxy named 'name', on port 'port' with 
    proto 'proto'; if proto==0 => proto wildcard (will match any proto)
a15c363f
    returns: pointer to proxy_l on success or 0 if not found */ 
d531a5d5
 static struct proxy_l* find_proxy(str *name, unsigned short port, int proto)
a15c363f
 {
 	struct proxy_l* t;
 	for(t=proxies; t; t=t->next)
d531a5d5
 		if (((t->name.len == name->len) &&
 			 ((proto==PROTO_NONE)||(t->proto==proto))&&
 			(strncasecmp(t->name.s, name->s, name->len)==0)) &&
 				(t->port==port))
a15c363f
 			break;
 	return t;
 }
 
 
346a9a05
 #define HOSTENT_CPY(dst, src, he_malloc, he_free)						\
 	do {																\
 		unsigned len,len2;												\
 		int r,i;														\
 																		\
 		/* start copying the host entry.. */							\
 		/* copy h_name */												\
 		len=strlen(src->h_name)+1;										\
 		dst->h_name=(char*)he_malloc(sizeof(char) * len);				\
 		if (dst->h_name) strncpy(dst->h_name,src->h_name, len);			\
 		else{															\
 			ser_error=ret=E_OUT_OF_MEM;									\
 			goto error;													\
 		}																\
 																		\
 		/* copy h_aliases */											\
 		len=0;															\
 		if (src->h_aliases)												\
 			for (;src->h_aliases[len];len++);							\
 		dst->h_aliases=(char**)he_malloc(sizeof(char*)*(len+1));		\
 		if (dst->h_aliases==0){											\
 			ser_error=ret=E_OUT_OF_MEM;									\
 			he_free(dst->h_name);										\
 			goto error;													\
 		}																\
 		memset((void*)dst->h_aliases, 0, sizeof(char*) * (len+1) );		\
 		for (i=0;i<len;i++){											\
 			len2=strlen(src->h_aliases[i])+1;							\
 			dst->h_aliases[i]=(char*)he_malloc(sizeof(char)*len2);		\
 			if (dst->h_aliases==0){										\
 				ser_error=ret=E_OUT_OF_MEM;								\
 				he_free(dst->h_name);									\
 				for(r=0; r<i; r++)	he_free(dst->h_aliases[r]);			\
 				he_free(dst->h_aliases);								\
 				goto error;												\
 			}															\
 			strncpy(dst->h_aliases[i], src->h_aliases[i], len2);		\
 		}																\
 		/* copy h_addr_list */											\
 		len=0;															\
 		if (src->h_addr_list)											\
 			for (;src->h_addr_list[len];len++);							\
 		dst->h_addr_list=(char**)he_malloc(sizeof(char*)*(len+1));		\
 		if (dst->h_addr_list==0){										\
 			ser_error=ret=E_OUT_OF_MEM;									\
 			he_free(dst->h_name);										\
 			for(r=0; dst->h_aliases[r]; r++)							\
 				he_free(dst->h_aliases[r]);								\
 			he_free(dst->h_aliases[r]);									\
 			he_free(dst->h_aliases);									\
 			goto error;													\
 		}																\
 		memset((void*)dst->h_addr_list, 0, sizeof(char*) * (len+1) );	\
 		for (i=0;i<len;i++){											\
 			dst->h_addr_list[i]=										\
 				(char*)he_malloc(sizeof(char)*src->h_length);			\
 			if (dst->h_addr_list[i]==0){								\
 				ser_error=ret=E_OUT_OF_MEM;								\
 				he_free(dst->h_name);									\
 				for(r=0; dst->h_aliases[r]; r++)						\
 					he_free(dst->h_aliases[r]);							\
 				he_free(dst->h_aliases[r]);								\
 				he_free(dst->h_aliases);								\
 				for (r=0; r<i;r++) he_free(dst->h_addr_list[r]);		\
 				he_free(dst->h_addr_list);								\
 				goto error;												\
 			}															\
 			memcpy(dst->h_addr_list[i], src->h_addr_list[i],			\
 				   src->h_length);										\
 		}																\
 																		\
 		/* copy h_addr_type & length */									\
 		dst->h_addrtype=src->h_addrtype;								\
 		dst->h_length=src->h_length;									\
 		/*finished hostent copy */										\
 																		\
 		return 0;														\
 	} while(0)
 
 
 #define FREE_HOSTENT(dst, he_free)						\
 	do {												\
 		int r;											\
 		if (dst->h_name) he_free(dst->h_name);			\
 		if (dst->h_aliases){							\
 			for(r=0; dst->h_aliases[r]; r++) {			\
 				he_free(dst->h_aliases[r]);				\
 			}											\
 			he_free(dst->h_aliases);					\
 		}												\
 		if (dst->h_addr_list){							\
 			for (r=0; dst->h_addr_list[r];r++) {		\
 				he_free(dst->h_addr_list[r]);			\
 			}											\
 			he_free(dst->h_addr_list);					\
 		}												\
 	} while(0)
 
 
a15c363f
 
 /* copies a hostent structure*, returns 0 on success, <0 on error*/
3e429f5c
 static int hostent_cpy(struct hostent *dst, struct hostent* src)
a15c363f
 {
346a9a05
 	int ret;
 	HOSTENT_CPY(dst, src, pkg_malloc, pkg_free);
a15c363f
 error:
 	LOG(L_CRIT, "ERROR: hostent_cpy: memory allocation failure\n");
 	return ret;
 }
 
 
346a9a05
 static int hostent_shm_cpy(struct hostent *dst, struct hostent* src)
 {
 	int ret;
 	HOSTENT_CPY(dst, src, shm_malloc, shm_free);
 error:
 	LOG(L_CRIT, "ERROR: hostent_shm_cpy: memory allocation failure\n");
 	return ret;
 }
 
a15c363f
 
346a9a05
 void free_hostent(struct hostent* dst)
5ada8f8a
 {
346a9a05
 	FREE_HOSTENT(dst, pkg_free);
5ada8f8a
 }
 
346a9a05
 void free_shm_hostent(struct hostent* dst)
 {
 	FREE_HOSTENT(dst, shm_free);
 }
5ada8f8a
 
 
 
d531a5d5
 struct proxy_l* add_proxy(str* name, unsigned short port, int proto)
a15c363f
 {
4ac74c03
 	struct proxy_l* p;
a15c363f
 	
d531a5d5
 	if ((p=find_proxy(name, port, proto))!=0) return p;
 	if ((p=mk_proxy(name, port, proto))==0) goto error;
5ada8f8a
 	/* add p to the proxy list */
 	p->next=proxies;
 	proxies=p;
 	return p;
 
 error:
 	return 0;
 }
 
 
346a9a05
 #define MK_PROXY(name, port, protocol, p_malloc, p_free, he_cpy)		\
 	do {																\
 		struct proxy_l* p;												\
 		struct hostent* he;												\
 		char proto;														\
 																		\
 		p=(struct proxy_l*) p_malloc(sizeof(struct proxy_l));			\
 		if (p==0){														\
 			ser_error=E_OUT_OF_MEM;										\
 			ERR("ERROR: mk_proxy: memory allocation failure\n");		\
 			goto error;													\
 		}																\
 		memset(p,0,sizeof(struct proxy_l));								\
 		p->name=*name;													\
 		p->port=port;													\
 																		\
 		DBG("DEBUG: mk_proxy: doing DNS lookup...\n");					\
 		proto=protocol;													\
 		he=sip_resolvehost(name, &(p->port), &proto);					\
 		if (he==0){														\
 			ser_error=E_BAD_ADDRESS;									\
 			LOG(L_CRIT, "ERROR: mk_proxy: could not resolve hostname:"	\
 				" \"%.*s\"\n", name->len, name->s);						\
 			p_free(p);													\
 			goto error;													\
 		}																\
 		if (he_cpy(&(p->host), he)!=0){									\
 			p_free(p);													\
 			goto error;													\
 		}																\
 		p->proto=proto;													\
 		p->ok=1;														\
 		return p;														\
 	error:																\
 		return 0;														\
 																		\
 	} while(0)
baa4fa73
 
1b71270a
 /* same as add_proxy, but it doesn't add the proxy to the list
d531a5d5
  * uses also SRV if possible & port==0 (quick hack) */
1b71270a
 
dd4ffbb7
 struct proxy_l* mk_proxy(str* name, unsigned short port, int protocol)
5ada8f8a
 {
346a9a05
 	MK_PROXY(name, port, protocol, pkg_malloc, pkg_free, hostent_cpy);
 }
baa4fa73
 
 
346a9a05
 struct proxy_l* mk_shm_proxy(str* name, unsigned short port, int protocol)
 {
 	MK_PROXY(name, port, protocol, shm_malloc, shm_free, hostent_shm_cpy);
a15c363f
 }
 
5ada8f8a
 
 
baa4fa73
 /* same as mk_proxy, but get the host as an ip*/
d531a5d5
 struct proxy_l* mk_proxy_from_ip(struct ip_addr* ip, unsigned short port,
 									int proto)
baa4fa73
 {
 	struct proxy_l* p;
 
e3dccdc9
 	p=(struct proxy_l*) pkg_malloc(sizeof(struct proxy_l));
baa4fa73
 	if (p==0){
 		LOG(L_CRIT, "ERROR: mk_proxy_from_ip: memory allocation failure\n");
 		goto error;
 	}
 	memset(p,0,sizeof(struct proxy_l));
 
 	p->port=port;
d531a5d5
 	p->proto=proto;
4e2fdd79
 	p->host.h_addrtype=ip->af;
 	p->host.h_length=ip->len;
e3dccdc9
 	p->host.h_addr_list=pkg_malloc(2*sizeof(char*));
baa4fa73
 	if (p->host.h_addr_list==0) goto error;
 	p->host.h_addr_list[1]=0;
e3dccdc9
 	p->host.h_addr_list[0]=pkg_malloc(ip->len+1);
baa4fa73
 	if (p->host.h_addr_list[0]==0){
e3dccdc9
 		pkg_free(p->host.h_addr_list);
baa4fa73
 		goto error;
 	}
 
4e2fdd79
 	memcpy(p->host.h_addr_list[0], ip->u.addr, ip->len);
 	p->host.h_addr_list[0][ip->len]=0;
baa4fa73
 
 	return p;
 
 error:
 	return 0;
 }
 
 
 
 
5ada8f8a
 void free_proxy(struct proxy_l* p)
 {
 	if (p) free_hostent(&p->host);
 }
346a9a05
 
 
 void free_shm_proxy(struct proxy_l* p)
 {
 	if (p) free_shm_hostent(&p->host);
 }