proxy.c
a15c363f
 /*
  * $Id$
  *
  * proxy list & assoc. functions
  *
7dd0b342
  *
  * Copyright (C) 2001-2003 Fhg Fokus
  *
  * 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
  */
 
 
7dd0b342
 
1b71270a
 #include "config.h"
a15c363f
 #include "proxy.h"
 #include "error.h"
4ac74c03
 #include "dprint.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
 
03150098
 #ifdef DEBUG_DMALLOC
 #include <dmalloc.h>
 #endif
a15c363f
 
 struct proxy_l* proxies=0;
 
 
 
 /* searches for the proxy named 'name', on port 'port'
    returns: pointer to proxy_l on success or 0 if not found */ 
3e429f5c
 static struct proxy_l* find_proxy(char *name, unsigned short port)
a15c363f
 {
 	struct proxy_l* t;
 	for(t=proxies; t; t=t->next)
 		if ((strcasecmp(t->name, name)==0) && (t->port==port))
 			break;
 	return t;
 }
 
 
 
 /* copies a hostent structure*, returns 0 on success, <0 on error*/
3e429f5c
 static int hostent_cpy(struct hostent *dst, struct hostent* src)
a15c363f
 {
3e429f5c
 	unsigned len,len2;
 	int r,ret,i;
a15c363f
 
 	/* start copying the host entry.. */
 	/* copy h_name */
 	len=strlen(src->h_name)+1;
 	dst->h_name=(char*)malloc(sizeof(char) * len);
 	if (dst->h_name) strncpy(dst->h_name,src->h_name, len);
 	else{
1400b772
 		ser_error=ret=E_OUT_OF_MEM;
a15c363f
 		goto error;
 	}
 
 	/* copy h_aliases */
ba506d03
 	len=0;
 	if (src->h_aliases)
 		for (;src->h_aliases[len];len++);
a15c363f
 	dst->h_aliases=(char**)malloc(sizeof(char*)*(len+1));
 	if (dst->h_aliases==0){
1400b772
 		ser_error=ret=E_OUT_OF_MEM;
a15c363f
 		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;
4ac74c03
 		dst->h_aliases[i]=(char*)malloc(sizeof(char)*len2);
a15c363f
 		if (dst->h_aliases==0){
1400b772
 			ser_error=ret=E_OUT_OF_MEM;
a15c363f
 			free(dst->h_name);
 			for(r=0; r<i; r++)	free(dst->h_aliases[r]);
 			free(dst->h_aliases);
 			goto error;
 		}
 		strncpy(dst->h_aliases[i], src->h_aliases[i], len2);
 	}
 	/* copy h_addr_list */
ba506d03
 	len=0;
 	if (src->h_addr_list)
 		for (;src->h_addr_list[len];len++);
a15c363f
 	dst->h_addr_list=(char**)malloc(sizeof(char*)*(len+1));
 	if (dst->h_addr_list==0){
1400b772
 		ser_error=ret=E_OUT_OF_MEM;
a15c363f
 		free(dst->h_name);
4ac74c03
 		for(r=0; dst->h_aliases[r]; r++)	free(dst->h_aliases[r]);
 		free(dst->h_aliases[r]);
a15c363f
 		free(dst->h_aliases);
 		goto error;
 	}
4ac74c03
 	memset((void*)dst->h_addr_list, 0, sizeof(char*) * (len+1) );
a15c363f
 	for (i=0;i<len;i++){
 		dst->h_addr_list[i]=(char*)malloc(sizeof(char)*src->h_length);
 		if (dst->h_addr_list[i]==0){
1400b772
 			ser_error=ret=E_OUT_OF_MEM;
a15c363f
 			free(dst->h_name);
4ac74c03
 			for(r=0; dst->h_aliases[r]; r++)	free(dst->h_aliases[r]);
 			free(dst->h_aliases[r]);
a15c363f
 			free(dst->h_aliases);
 			for (r=0; r<i;r++) free(dst->h_addr_list[r]);
 			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;
4ac74c03
 	dst->h_length=src->h_length;
a15c363f
 	/*finished hostent copy */
 	
 	return 0;
 
 error:
 	LOG(L_CRIT, "ERROR: hostent_cpy: memory allocation failure\n");
 	return ret;
 }
 
 
 
5ada8f8a
 void free_hostent(struct hostent *dst)
 {
 	int r;
 	if (dst->h_name) free(dst->h_name);
 	if (dst->h_aliases){
 		for(r=0; dst->h_aliases[r]; r++)	free(dst->h_aliases[r]);
 		free(dst->h_aliases[r]);
 		free(dst->h_aliases);
 	}
 	if (dst->h_addr_list){
 		for (r=0; dst->h_addr_list[r];r++) free(dst->h_addr_list[r]);
 		free(dst->h_addr_list[r]);
 		free(dst->h_addr_list);
 	}
 }
 
 
 
 
a15c363f
 struct proxy_l* add_proxy(char* name, unsigned short port)
 {
4ac74c03
 	struct proxy_l* p;
a15c363f
 	
 	if ((p=find_proxy(name, port))!=0) return p;
5ada8f8a
 	if ((p=mk_proxy(name, port))==0) goto error;
 	/* add p to the proxy list */
 	p->next=proxies;
 	proxies=p;
 	return p;
 
 error:
 	return 0;
 }
 
 
 
baa4fa73
 
1b71270a
 /* same as add_proxy, but it doesn't add the proxy to the list
  * uses also SRV if possible (quick hack) */
 
5ada8f8a
 struct proxy_l* mk_proxy(char* name, unsigned short port)
 {
 	struct proxy_l* p;
 	struct hostent* he;
baa4fa73
 
a15c363f
 	p=(struct proxy_l*) malloc(sizeof(struct proxy_l));
 	if (p==0){
1400b772
 		ser_error=E_OUT_OF_MEM;
5ada8f8a
 		LOG(L_CRIT, "ERROR: mk_proxy: memory allocation failure\n");
a15c363f
 		goto error;
 	}
4ac74c03
 	memset(p,0,sizeof(struct proxy_l));
a15c363f
 	p->name=name;
5bc22c81
 	p->port=port;
baa4fa73
 
caf80ae6
 	DBG("DEBUG: mk_proxy: doing DNS lookup...\n");
5bc22c81
 	he=sip_resolvehost(name, &(p->port));
a15c363f
 	if (he==0){
1400b772
 		ser_error=E_BAD_ADDRESS;
5ada8f8a
 		LOG(L_CRIT, "ERROR: mk_proxy: could not resolve hostname:"
a15c363f
 					" \"%s\"\n", name);
 		free(p);
 		goto error;
 	}
 	if (hostent_cpy(&(p->host), he)!=0){
 		free(p);
 		goto error;
 	}
 	p->ok=1;
 	return p;
 error:
 	return 0;
 }
 
5ada8f8a
 
 
baa4fa73
 /* same as mk_proxy, but get the host as an ip*/
4e2fdd79
 struct proxy_l* mk_proxy_from_ip(struct ip_addr* ip, unsigned short port)
baa4fa73
 {
 	struct proxy_l* p;
 
 	p=(struct proxy_l*) malloc(sizeof(struct proxy_l));
 	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;
4e2fdd79
 	p->host.h_addrtype=ip->af;
 	p->host.h_length=ip->len;
baa4fa73
 	p->host.h_addr_list=malloc(2*sizeof(char*));
 	if (p->host.h_addr_list==0) goto error;
 	p->host.h_addr_list[1]=0;
4e2fdd79
 	p->host.h_addr_list[0]=malloc(ip->len+1);
baa4fa73
 	if (p->host.h_addr_list[0]==0){
 		free(p->host.h_addr_list);
 		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);
 }