enum.c 3.92 KB
Newer Older
aymeric's avatar
aymeric committed
1 2
/*
linphone
3
Copyright (C) 2000-2009  Simon MORLAT (simon.morlat@linphone.org)
aymeric's avatar
aymeric committed
4 5 6 7 8 9 10 11 12 13 14 15 16

This program 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.

This program 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
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
aymeric's avatar
aymeric committed
18 19 20 21
*/

/* enum lookup code */

Simon Morlat's avatar
Simon Morlat committed
22
#ifndef _WIN32_WCE
aymeric's avatar
aymeric committed
23
#include <errno.h>
Simon Morlat's avatar
Simon Morlat committed
24 25
#endif

aymeric's avatar
aymeric committed
26 27
#include <string.h>

28
#include "enum.h"
aymeric's avatar
aymeric committed
29 30 31 32

#define DNS_ANSWER_MAX_SIZE 2048


33
static char *create_enum_domain(const char *number){
34
	long len=(long)strlen(number);
Benjamin REIS's avatar
Benjamin REIS committed
35
	char *domain=reinterpret_cast<char *>(ms_malloc((size_t)(len*2)+10));
36 37
	long i,j;

aymeric's avatar
aymeric committed
38 39 40 41 42 43 44 45 46 47 48 49
	for (i=0,j=len-1;j>=0;j--){
		domain[i]=number[j];
		i++;
		domain[i]='.';
		i++;
	}
	strcpy(&domain[i],"e164.arpa");
	ms_message("enum domain for %s is %s",number,domain);
	return domain;
}


50
static bool_t is_a_number(const char *str){
aymeric's avatar
aymeric committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	char *p=(char *)str;
	bool_t res=FALSE;
	bool_t space_found=FALSE;
	for(;;p++){
		switch(p[0]){
			case '9':
			case '8':
			case '7':
			case '6':
			case '5':
			case '4':
			case '3':
			case '2':
			case '1':
			case '0':
				res=TRUE;
				if (space_found) return FALSE; /* avoid splited numbers */
				break;
			case '\0':
				return res;
				break;
			case ' ':
				space_found=TRUE;
				break;
			default:
				return FALSE;
		}
	}
	return FALSE;
}
//4970072278724
bool_t is_enum(const char *sipaddress, char **enum_domain){
Wescoeur's avatar
Wescoeur committed
83
	const char *p;
aymeric's avatar
aymeric committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	p=strstr(sipaddress,"sip:");
	if (p==NULL) return FALSE; /* enum should look like sip:4369959250*/
	else p+=4;
	if (is_a_number(p)){
		if (enum_domain!=NULL){
			*enum_domain=create_enum_domain(p);
		}
		return TRUE;
	}
	return FALSE;
}



int enum_lookup(const char *enum_domain, enum_lookup_res_t **res){
	int err;
	//char dns_answer[DNS_ANSWER_MAX_SIZE];
	char *begin,*end;
	char *host_result, *command;
	int i;
	bool_t forkok;
	/*
	ns_msg handle;
	int count;
108

aymeric's avatar
aymeric committed
109 110 111
	memset(&handle,0,sizeof(handle));
	*res=NULL;
	ms_message("Resolving %s...",enum_domain);
112

aymeric's avatar
aymeric committed
113 114 115 116 117 118 119
	err=res_search(enum_domain,ns_c_in,ns_t_naptr,dns_answer,DNS_ANSWER_MAX_SIZE);
	if (err<0){
		ms_warning("Error resolving enum:",herror(h_errno));
		return -1;
	}
	ns_initparse(dns_answer,DNS_ANSWER_MAX_SIZE,&handle);
	count=ns_msg_count(handle,ns_s_an);
120

aymeric's avatar
aymeric committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	for(i=0;i<count;i++){
		ns_rr rr;
		memset(&rr,0,sizeof(rr));
		ns_parserr(&handle,ns_s_an,i,&rr);
		ms_message("data=%s",ns_rr_rdata(rr));
	}
	*/
	command=ms_strdup_printf("host -t naptr %s",enum_domain);
	forkok=lp_spawn_command_line_sync(command,&host_result,&err);
	ms_free(command);
	if (forkok){
		if (err!=0){
			ms_warning("Host exited with %i error status.",err);
			return -1;
		}
	}else{
		ms_warning("Could not spawn the \'host\' command.");
		return -1;
139
	}
aymeric's avatar
aymeric committed
140
	ms_message("Answer received from dns (err=%i): %s",err,host_result);
141

aymeric's avatar
aymeric committed
142 143 144 145 146
	begin=strstr(host_result,"sip:");
	if (begin==0) {
		ms_warning("No sip address found in dns naptr answer.");
		return -1;
	}
147
	*res=reinterpret_cast<enum_lookup_res_t *>(ms_malloc0(sizeof(enum_lookup_res_t)));
aymeric's avatar
aymeric committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	err=0;
	for(i=0;i<MAX_ENUM_LOOKUP_RESULTS;i++){
		end=strstr(begin,"!");
		if (end==NULL) goto parse_error;
		end[0]='\0';
		(*res)->sip_address[i]=ms_strdup(begin);
		err++;
		begin=strstr(end+1,"sip:");
		if (begin==NULL) break;
	}
	ms_free(host_result);
	return err;

	parse_error:
		ms_free(*res);
		ms_free(host_result);
		*res=NULL;
		ms_warning("Parse error in enum_lookup().");
		return -1;
}

void enum_lookup_res_free(enum_lookup_res_t *res){
	int i;
	for (i=0;i<MAX_ENUM_LOOKUP_RESULTS;i++){
		if (res->sip_address[i]!=NULL) ms_free(res->sip_address[i]);
	}
	ms_free(res);
}