3 Copyright (C) 2000-2009 Simon MORLAT (simon.morlat@linphone.org)
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 /* enum lookup code */
30 #define DNS_ANSWER_MAX_SIZE 2048
33 static char *create_enum_domain(const char *number){
34 int len=strlen(number);
35 char *domain=ms_malloc((len*2)+10);
38 for (i=0,j=len-1;j>=0;j--){
44 strcpy(&domain[i],"e164.arpa");
45 ms_message("enum domain for %s is %s",number,domain);
50 static bool_t is_a_number(const char *str){
53 bool_t space_found=FALSE;
67 if (space_found) return FALSE; /* avoid splited numbers */
82 bool_t is_enum(const char *sipaddress, char **enum_domain){
84 p=strstr(sipaddress,"sip:");
85 if (p==NULL) return FALSE; /* enum should look like sip:4369959250*/
88 if (enum_domain!=NULL){
89 *enum_domain=create_enum_domain(p);
98 int enum_lookup(const char *enum_domain, enum_lookup_res_t **res){
100 //char dns_answer[DNS_ANSWER_MAX_SIZE];
102 char *host_result, *command;
109 memset(&handle,0,sizeof(handle));
111 ms_message("Resolving %s...",enum_domain);
113 err=res_search(enum_domain,ns_c_in,ns_t_naptr,dns_answer,DNS_ANSWER_MAX_SIZE);
115 ms_warning("Error resolving enum:",herror(h_errno));
118 ns_initparse(dns_answer,DNS_ANSWER_MAX_SIZE,&handle);
119 count=ns_msg_count(handle,ns_s_an);
121 for(i=0;i<count;i++){
123 memset(&rr,0,sizeof(rr));
124 ns_parserr(&handle,ns_s_an,i,&rr);
125 ms_message("data=%s",ns_rr_rdata(rr));
128 command=ms_strdup_printf("host -t naptr %s",enum_domain);
129 forkok=lp_spawn_command_line_sync(command,&host_result,&err);
133 ms_warning("Host exited with %i error status.",err);
137 ms_warning("Could not spawn the \'host\' command.");
140 ms_message("Answer received from dns (err=%i): %s",err,host_result);
142 begin=strstr(host_result,"sip:");
144 ms_warning("No sip address found in dns naptr answer.");
147 *res=ms_malloc0(sizeof(enum_lookup_res_t));
149 for(i=0;i<MAX_ENUM_LOOKUP_RESULTS;i++){
150 end=strstr(begin,"!");
151 if (end==NULL) goto parse_error;
153 (*res)->sip_address[i]=ms_strdup(begin);
155 begin=strstr(end+1,"sip:");
156 if (begin==NULL) break;
158 ms_free(host_result);
163 ms_free(host_result);
165 ms_warning("Parse error in enum_lookup().");
169 void enum_lookup_res_free(enum_lookup_res_t *res){
171 for (i=0;i<MAX_ENUM_LOOKUP_RESULTS;i++){
172 if (res->sip_address[i]!=NULL) ms_free(res->sip_address[i]);