2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Copyright (C) 2007-2012 by Daniel Stenberg
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for any purpose and without
7 * fee is hereby granted, provided that the above copyright
8 * notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting
10 * documentation, and that the name of M.I.T. not be used in
11 * advertising or publicity pertaining to distribution of the
12 * software without specific, written prior permission.
13 * M.I.T. makes no representations about the suitability of
14 * this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
18 #include "ares_setup.h"
20 #ifdef HAVE_SYS_PARAM_H
21 #include <sys/param.h>
24 #ifdef HAVE_SYS_TIME_H
28 #ifdef HAVE_SYS_SOCKET_H
29 #include <sys/socket.h>
32 #ifdef HAVE_NETINET_IN_H
33 #include <netinet/in.h>
40 #ifdef HAVE_ARPA_INET_H
41 #include <arpa/inet.h>
44 #ifdef HAVE_ARPA_NAMESER_H
45 # include <arpa/nameser.h>
49 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
50 # include <arpa/nameser_compat.h>
63 #if defined(ANDROID) || defined(__ANDROID__)
64 #include <sys/system_properties.h>
68 #include "inet_ntop.h"
69 #include "inet_net_pton.h"
70 #include "ares_library_init.h"
71 #include "ares_nowarn.h"
72 #include "ares_platform.h"
73 #include "ares_private.h"
76 #undef WIN32 /* Redefined in MingW/MSVC headers */
79 static int init_by_options(ares_channel channel,
80 const struct ares_options *options,
82 static int init_by_environment(ares_channel channel);
83 static int init_by_resolv_conf(ares_channel channel);
84 static int init_by_defaults(ares_channel channel);
87 static int config_nameserver(struct server_state **servers, int *nservers,
90 static int set_search(ares_channel channel, const char *str);
91 static int set_options(ares_channel channel, const char *str);
92 static const char *try_option(const char *p, const char *q, const char *opt);
93 static int init_id_key(rc4_key* key,int key_data_len);
95 #if !defined(WIN32) && !defined(WATT32) && \
96 !defined(ANDROID) && !defined(__ANDROID__)
97 static int sortlist_alloc(struct apattern **sortlist, int *nsort,
98 struct apattern *pat);
99 static int ip_addr(const char *s, ssize_t len, struct in_addr *addr);
100 static void natural_mask(struct apattern *pat);
101 static int config_domain(ares_channel channel, char *str);
102 static int config_lookup(ares_channel channel, const char *str,
103 const char *bindch, const char *filech);
104 static int config_sortlist(struct apattern **sortlist, int *nsort,
106 static char *try_config(char *s, const char *opt, char scc);
109 #define ARES_CONFIG_CHECK(x) (x->lookups && x->nsort > -1 && \
110 x->nservers > -1 && \
111 x->ndomains > -1 && \
112 x->ndots > -1 && x->timeout > -1 && \
115 int ares_init(ares_channel *channelptr)
117 return ares_init_options(channelptr, NULL, 0);
120 int ares_init_options(ares_channel *channelptr, struct ares_options *options,
123 ares_channel channel;
125 int status = ARES_SUCCESS;
129 const char *env = getenv("CARES_MEMDEBUG");
133 env = getenv("CARES_MEMLIMIT");
136 long num = strtol(env, &endptr, 10);
137 if((endptr != env) && (endptr == env + strlen(env)) && (num > 0))
142 if (ares_library_initialized() != ARES_SUCCESS)
143 return ARES_ENOTINITIALIZED;
145 channel = malloc(sizeof(struct ares_channeldata));
153 /* Set everything to distinguished values so we know they haven't
157 channel->timeout = -1;
160 channel->rotate = -1;
161 channel->udp_port = -1;
162 channel->tcp_port = -1;
163 channel->socket_send_buffer_size = -1;
164 channel->socket_receive_buffer_size = -1;
165 channel->nservers = -1;
166 channel->ndomains = -1;
168 channel->tcp_connection_generation = 0;
169 channel->lookups = NULL;
170 channel->domains = NULL;
171 channel->sortlist = NULL;
172 channel->servers = NULL;
173 channel->sock_state_cb = NULL;
174 channel->sock_state_cb_data = NULL;
175 channel->sock_create_cb = NULL;
176 channel->sock_create_cb_data = NULL;
178 channel->last_server = 0;
179 channel->last_timeout_processed = (time_t)now.tv_sec;
181 memset(&channel->local_dev_name, 0, sizeof(channel->local_dev_name));
182 channel->local_ip4 = 0;
183 memset(&channel->local_ip6, 0, sizeof(channel->local_ip6));
185 /* Initialize our lists of queries */
186 ares__init_list_head(&(channel->all_queries));
187 for (i = 0; i < ARES_QID_TABLE_SIZE; i++)
189 ares__init_list_head(&(channel->queries_by_qid[i]));
191 for (i = 0; i < ARES_TIMEOUT_TABLE_SIZE; i++)
193 ares__init_list_head(&(channel->queries_by_timeout[i]));
196 /* Initialize configuration by each of the four sources, from highest
197 * precedence to lowest.
200 if (status == ARES_SUCCESS) {
201 status = init_by_options(channel, options, optmask);
202 if (status != ARES_SUCCESS)
203 DEBUGF(fprintf(stderr, "Error: init_by_options failed: %s\n",
204 ares_strerror(status)));
206 if (status == ARES_SUCCESS) {
207 status = init_by_environment(channel);
208 if (status != ARES_SUCCESS)
209 DEBUGF(fprintf(stderr, "Error: init_by_environment failed: %s\n",
210 ares_strerror(status)));
212 if (status == ARES_SUCCESS) {
213 status = init_by_resolv_conf(channel);
214 if (status != ARES_SUCCESS)
215 DEBUGF(fprintf(stderr, "Error: init_by_resolv_conf failed: %s\n",
216 ares_strerror(status)));
220 * No matter what failed or succeeded, seed defaults to provide
221 * useful behavior for things that we missed.
223 status = init_by_defaults(channel);
224 if (status != ARES_SUCCESS)
225 DEBUGF(fprintf(stderr, "Error: init_by_defaults failed: %s\n",
226 ares_strerror(status)));
228 /* Generate random key */
230 if (status == ARES_SUCCESS) {
231 status = init_id_key(&channel->id_key, ARES_ID_KEY_LEN);
232 if (status == ARES_SUCCESS)
233 channel->next_id = ares__generate_new_id(&channel->id_key);
235 DEBUGF(fprintf(stderr, "Error: init_id_key failed: %s\n",
236 ares_strerror(status)));
239 if (status != ARES_SUCCESS)
241 /* Something failed; clean up memory we may have allocated. */
242 if (channel->servers)
243 free(channel->servers);
244 if (channel->domains)
246 for (i = 0; i < channel->ndomains; i++)
247 free(channel->domains[i]);
248 free(channel->domains);
250 if (channel->sortlist)
251 free(channel->sortlist);
253 free(channel->lookups);
258 /* Trim to one server if ARES_FLAG_PRIMARY is set. */
259 if ((channel->flags & ARES_FLAG_PRIMARY) && channel->nservers > 1)
260 channel->nservers = 1;
262 ares__init_servers_state(channel);
264 *channelptr = channel;
268 /* ares_dup() duplicates a channel handle with all its options and returns a
269 new channel handle */
270 int ares_dup(ares_channel *dest, ares_channel src)
272 struct ares_options opts;
273 struct ares_addr_node *servers;
274 int ipv6_nservers = 0;
278 *dest = NULL; /* in case of failure return NULL explicitly */
280 /* First get the options supported by the old ares_save_options() function,
281 which is most of them */
282 rc = ares_save_options(src, &opts, &optmask);
286 /* Then create the new channel with those options */
287 rc = ares_init_options(dest, &opts, optmask);
289 /* destroy the options copy to not leak any memory */
290 ares_destroy_options(&opts);
295 /* Now clone the options that ares_save_options() doesn't support. */
296 (*dest)->sock_create_cb = src->sock_create_cb;
297 (*dest)->sock_create_cb_data = src->sock_create_cb_data;
299 strncpy((*dest)->local_dev_name, src->local_dev_name,
300 sizeof(src->local_dev_name));
301 (*dest)->local_ip4 = src->local_ip4;
302 memcpy((*dest)->local_ip6, src->local_ip6, sizeof(src->local_ip6));
304 /* Full name server cloning required when not all are IPv4 */
305 for (i = 0; i < src->nservers; i++)
307 if (src->servers[i].addr.family != AF_INET) {
313 rc = ares_get_servers(src, &servers);
314 if (rc != ARES_SUCCESS)
316 rc = ares_set_servers(*dest, servers);
317 ares_free_data(servers);
318 if (rc != ARES_SUCCESS)
322 return ARES_SUCCESS; /* everything went fine */
325 /* Save options from initialized channel */
326 int ares_save_options(ares_channel channel, struct ares_options *options,
330 int ipv4_nservers = 0;
332 /* Zero everything out */
333 memset(options, 0, sizeof(struct ares_options));
335 if (!ARES_CONFIG_CHECK(channel))
338 /* Traditionally the optmask wasn't saved in the channel struct so it was
339 recreated here. ROTATE is the first option that has no struct field of
340 its own in the public config struct */
341 (*optmask) = (ARES_OPT_FLAGS|ARES_OPT_TRIES|ARES_OPT_NDOTS|
342 ARES_OPT_UDP_PORT|ARES_OPT_TCP_PORT|ARES_OPT_SOCK_STATE_CB|
343 ARES_OPT_SERVERS|ARES_OPT_DOMAINS|ARES_OPT_LOOKUPS|
344 ARES_OPT_SORTLIST|ARES_OPT_TIMEOUTMS) |
345 (channel->optmask & ARES_OPT_ROTATE);
347 /* Copy easy stuff */
348 options->flags = channel->flags;
350 /* We return full millisecond resolution but that's only because we don't
351 set the ARES_OPT_TIMEOUT anymore, only the new ARES_OPT_TIMEOUTMS */
352 options->timeout = channel->timeout;
353 options->tries = channel->tries;
354 options->ndots = channel->ndots;
355 options->udp_port = (unsigned short)channel->udp_port;
356 options->tcp_port = (unsigned short)channel->tcp_port;
357 options->sock_state_cb = channel->sock_state_cb;
358 options->sock_state_cb_data = channel->sock_state_cb_data;
360 /* Copy IPv4 servers */
361 if (channel->nservers) {
362 for (i = 0; i < channel->nservers; i++)
364 if (channel->servers[i].addr.family == AF_INET)
368 options->servers = malloc(ipv4_nservers * sizeof(struct in_addr));
369 if (!options->servers)
371 for (i = j = 0; i < channel->nservers; i++)
373 if (channel->servers[i].addr.family == AF_INET)
374 memcpy(&options->servers[j++],
375 &channel->servers[i].addr.addrV4,
376 sizeof(channel->servers[i].addr.addrV4));
380 options->nservers = ipv4_nservers;
383 if (channel->ndomains) {
384 options->domains = malloc(channel->ndomains * sizeof(char *));
385 if (!options->domains)
388 for (i = 0; i < channel->ndomains; i++)
390 options->ndomains = i;
391 options->domains[i] = strdup(channel->domains[i]);
392 if (!options->domains[i])
396 options->ndomains = channel->ndomains;
399 if (channel->lookups) {
400 options->lookups = strdup(channel->lookups);
401 if (!options->lookups && channel->lookups)
406 if (channel->nsort) {
407 options->sortlist = malloc(channel->nsort * sizeof(struct apattern));
408 if (!options->sortlist)
410 for (i = 0; i < channel->nsort; i++)
411 options->sortlist[i] = channel->sortlist[i];
413 options->nsort = channel->nsort;
418 static int init_by_options(ares_channel channel,
419 const struct ares_options *options,
425 if ((optmask & ARES_OPT_FLAGS) && channel->flags == -1)
426 channel->flags = options->flags;
427 if ((optmask & ARES_OPT_TIMEOUTMS) && channel->timeout == -1)
428 channel->timeout = options->timeout;
429 else if ((optmask & ARES_OPT_TIMEOUT) && channel->timeout == -1)
430 channel->timeout = options->timeout * 1000;
431 if ((optmask & ARES_OPT_TRIES) && channel->tries == -1)
432 channel->tries = options->tries;
433 if ((optmask & ARES_OPT_NDOTS) && channel->ndots == -1)
434 channel->ndots = options->ndots;
435 if ((optmask & ARES_OPT_ROTATE) && channel->rotate == -1)
437 if ((optmask & ARES_OPT_UDP_PORT) && channel->udp_port == -1)
438 channel->udp_port = htons(options->udp_port);
439 if ((optmask & ARES_OPT_TCP_PORT) && channel->tcp_port == -1)
440 channel->tcp_port = htons(options->tcp_port);
441 if ((optmask & ARES_OPT_SOCK_STATE_CB) && channel->sock_state_cb == NULL)
443 channel->sock_state_cb = options->sock_state_cb;
444 channel->sock_state_cb_data = options->sock_state_cb_data;
446 if ((optmask & ARES_OPT_SOCK_SNDBUF)
447 && channel->socket_send_buffer_size == -1)
448 channel->socket_send_buffer_size = options->socket_send_buffer_size;
449 if ((optmask & ARES_OPT_SOCK_RCVBUF)
450 && channel->socket_receive_buffer_size == -1)
451 channel->socket_receive_buffer_size = options->socket_receive_buffer_size;
453 /* Copy the IPv4 servers, if given. */
454 if ((optmask & ARES_OPT_SERVERS) && channel->nservers == -1)
456 /* Avoid zero size allocations at any cost */
457 if (options->nservers > 0)
460 malloc(options->nservers * sizeof(struct server_state));
461 if (!channel->servers)
463 for (i = 0; i < options->nservers; i++)
465 channel->servers[i].addr.family = AF_INET;
466 memcpy(&channel->servers[i].addr.addrV4,
467 &options->servers[i],
468 sizeof(channel->servers[i].addr.addrV4));
471 channel->nservers = options->nservers;
474 /* Copy the domains, if given. Keep channel->ndomains consistent so
475 * we can clean up in case of error.
477 if ((optmask & ARES_OPT_DOMAINS) && channel->ndomains == -1)
479 /* Avoid zero size allocations at any cost */
480 if (options->ndomains > 0)
482 channel->domains = malloc(options->ndomains * sizeof(char *));
483 if (!channel->domains)
485 for (i = 0; i < options->ndomains; i++)
487 channel->ndomains = i;
488 channel->domains[i] = strdup(options->domains[i]);
489 if (!channel->domains[i])
493 channel->ndomains = options->ndomains;
496 /* Set lookups, if given. */
497 if ((optmask & ARES_OPT_LOOKUPS) && !channel->lookups)
499 channel->lookups = strdup(options->lookups);
500 if (!channel->lookups)
505 if ((optmask & ARES_OPT_SORTLIST) && (channel->nsort == -1) &&
506 (options->nsort>0)) {
507 channel->sortlist = malloc(options->nsort * sizeof(struct apattern));
508 if (!channel->sortlist)
510 for (i = 0; i < options->nsort; i++)
511 channel->sortlist[i] = options->sortlist[i];
512 channel->nsort = options->nsort;
515 channel->optmask = optmask;
520 static int init_by_environment(ares_channel channel)
522 const char *localdomain, *res_options;
525 localdomain = getenv("LOCALDOMAIN");
526 if (localdomain && channel->ndomains == -1)
528 status = set_search(channel, localdomain);
529 if (status != ARES_SUCCESS)
533 res_options = getenv("RES_OPTIONS");
536 status = set_options(channel, res_options);
537 if (status != ARES_SUCCESS)
546 * Warning: returns a dynamically allocated buffer, the user MUST
547 * use free() if the function returns 1
549 static int get_res_nt(HKEY hKey, const char *subkey, char **obuf)
551 /* Test for the size we need */
555 result = RegQueryValueEx(hKey, subkey, 0, NULL, NULL, &size);
556 if ((result != ERROR_SUCCESS && result != ERROR_MORE_DATA) || !size)
558 *obuf = malloc(size+1);
562 if (RegQueryValueEx(hKey, subkey, 0, NULL,
563 (LPBYTE)*obuf, &size) != ERROR_SUCCESS)
576 static int get_res_interfaces_nt(HKEY hKey, const char *subkey, char **obuf)
578 char enumbuf[39]; /* GUIDs are 38 chars + 1 for NULL */
579 DWORD enum_size = 39;
583 while (RegEnumKeyEx(hKey, idx++, enumbuf, &enum_size, 0,
584 NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS)
589 if (RegOpenKeyEx(hKey, enumbuf, 0, KEY_QUERY_VALUE, &hVal) !=
592 rc = get_res_nt(hVal, subkey, obuf);
600 /* get_iphlpapi_dns_classic() is supported on W98 and newer */
601 static int get_iphlpapi_dns_classic(char *ret_buf, size_t ret_size)
603 FIXED_INFO *fi, *newfi;
604 struct ares_addr namesrvr;
606 IP_ADDR_STRING *ipAddr;
609 DWORD size = sizeof (*fi);
610 char *endptr = ret_buf;
619 res = (*ares_fpGetNetworkParams) (fi, &size);
620 if ((res != ERROR_BUFFER_OVERFLOW) && (res != ERROR_SUCCESS))
623 newfi = realloc(fi, size);
628 res = (*ares_fpGetNetworkParams) (fi, &size);
629 if (res != ERROR_SUCCESS)
632 for (ipAddr = &fi->DnsServerList; ipAddr; ipAddr = ipAddr->Next)
634 txtaddr = &ipAddr->IpAddress.String[0];
636 /* Validate converting textual address to binary format. */
637 if (ares_inet_pton(AF_INET, txtaddr, &namesrvr.addrV4) == 1)
639 if ((namesrvr.addrV4.S_un.S_addr == INADDR_ANY) ||
640 (namesrvr.addrV4.S_un.S_addr == INADDR_NONE))
643 else if (ares_inet_pton(AF_INET6, txtaddr, &namesrvr.addrV6) == 1)
645 if (memcmp(&namesrvr.addrV6, &ares_in6addr_any,
646 sizeof(namesrvr.addrV6)) == 0)
652 txtlen = strlen(txtaddr);
653 if (ret_size >= strlen(ret_buf) + txtlen + 2)
655 sprintf(endptr, "%s,", txtaddr);
656 endptr += txtlen + 1;
665 if (endptr != ret_buf)
666 *(endptr - 1) = '\0';
671 #define IPAA_INITIAL_BUF_SZ 15 * 1024
672 #define IPAA_MAX_TRIES 3
674 static int get_iphlpapi_dns_info(char *ret_buf, size_t ret_size)
676 IP_ADAPTER_DNS_SERVER_ADDRESS *ipaDNSAddr;
677 IP_ADAPTER_ADDRESSES *ipaa, *newipaa, *ipaaEntry;
679 ULONG ReqBufsz = IPAA_INITIAL_BUF_SZ;
680 ULONG Bufsz = IPAA_INITIAL_BUF_SZ;
682 char *endptr = ret_buf;
683 int trying = IPAA_MAX_TRIES;
688 struct sockaddr_in *sa4;
689 struct sockaddr_in6 *sa6;
692 char txtaddr[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
695 /* Unless GetAdaptersAddresses is available, use GetNetworkParams */
696 if (ares_fpGetAdaptersAddresses == ZERO_NULL)
697 return get_iphlpapi_dns_classic(ret_buf, ret_size);
701 ipaa = malloc(Bufsz);
705 /* Usually this call suceeds with initial buffer size */
706 res = (*ares_fpGetAdaptersAddresses) (AF_UNSPEC, AddrFlags, NULL,
708 if ((res != ERROR_BUFFER_OVERFLOW) && (res != ERROR_SUCCESS))
711 while ((res == ERROR_BUFFER_OVERFLOW) && (--trying))
713 if (Bufsz < ReqBufsz)
715 newipaa = realloc(ipaa, ReqBufsz);
721 res = (*ares_fpGetAdaptersAddresses) (AF_UNSPEC, AddrFlags, NULL,
723 if (res == ERROR_SUCCESS)
726 if (res != ERROR_SUCCESS)
729 for (ipaaEntry = ipaa; ipaaEntry; ipaaEntry = ipaaEntry->Next)
731 for (ipaDNSAddr = ipaaEntry->FirstDnsServerAddress;
733 ipaDNSAddr = ipaDNSAddr->Next)
735 namesrvr.sa = ipaDNSAddr->Address.lpSockaddr;
737 if (namesrvr.sa->sa_family == AF_INET)
739 if ((namesrvr.sa4->sin_addr.S_un.S_addr == INADDR_ANY) ||
740 (namesrvr.sa4->sin_addr.S_un.S_addr == INADDR_NONE))
742 if (! ares_inet_ntop(AF_INET, &namesrvr.sa4->sin_addr,
743 txtaddr, sizeof(txtaddr)))
746 else if (namesrvr.sa->sa_family == AF_INET6)
748 if (memcmp(&namesrvr.sa6->sin6_addr, &ares_in6addr_any,
749 sizeof(namesrvr.sa6->sin6_addr)) == 0)
751 if (! ares_inet_ntop(AF_INET, &namesrvr.sa6->sin6_addr,
752 txtaddr, sizeof(txtaddr)))
758 txtlen = strlen(txtaddr);
759 if (ret_size >= strlen(ret_buf) + txtlen + 2)
761 sprintf(endptr, "%s,", txtaddr);
762 endptr += txtlen + 1;
772 if (endptr != ret_buf)
773 *(endptr - 1) = '\0';
779 static int init_by_resolv_conf(ares_channel channel)
781 #if !defined(ANDROID) && !defined(__ANDROID__) && !defined(WATT32)
784 int status = -1, nservers = 0, nsort = 0;
785 struct server_state *servers = NULL;
786 struct apattern *sortlist = NULL;
791 NameServer info via IPHLPAPI (IP helper API):
792 GetNetworkParams() should be the trusted source for this.
793 Available in Win-98/2000 and later. If that fail, fall-back to
794 registry information.
798 On Windows 9X, the DNS server can be found in:
799 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\MSTCP\NameServer
801 On Windows NT/2000/XP/2003:
802 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NameServer
804 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DhcpNameServer
806 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\{AdapterID}\
809 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\{AdapterID}\
819 win_platform platform;
821 if (channel->nservers > -1) /* don't override ARES_OPT_SERVER */
824 if (get_iphlpapi_dns_info(buf,sizeof(buf)) > 0)
826 status = config_nameserver(&servers, &nservers, buf);
827 if (status == ARES_SUCCESS)
831 platform = ares__getplatform();
833 if (platform == WIN_NT)
836 HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
840 RegOpenKeyEx(mykey, "Interfaces", 0,
841 KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS, &subkey);
842 if (get_res_nt(mykey, NAMESERVER, &line))
844 status = config_nameserver(&servers, &nservers, line);
847 else if (get_res_nt(mykey, DHCPNAMESERVER, &line))
849 status = config_nameserver(&servers, &nservers, line);
852 /* Try the interfaces */
853 else if (get_res_interfaces_nt(subkey, NAMESERVER, &line))
855 status = config_nameserver(&servers, &nservers, line);
858 else if (get_res_interfaces_nt(subkey, DHCPNAMESERVER, &line))
860 status = config_nameserver(&servers, &nservers, line);
867 else if (platform == WIN_9X)
870 HKEY_LOCAL_MACHINE, WIN_NS_9X, 0,
874 if ((result = RegQueryValueEx(
875 mykey, NAMESERVER, NULL, &data_type,
878 ) == ERROR_SUCCESS ||
879 result == ERROR_MORE_DATA)
883 line = malloc(bytes+1);
884 if (RegQueryValueEx(mykey, NAMESERVER, NULL, &data_type,
885 (unsigned char *)line, &bytes) ==
888 status = config_nameserver(&servers, &nservers, line);
897 if (status == ARES_SUCCESS)
900 /* Catch the case when all the above checks fail (which happens when there
901 is no network card or the cable is unplugged) */
904 #elif defined(__riscos__)
906 /* Under RISC OS, name servers are listed in the
907 system variable Inet$Resolvers, space separated. */
909 line = getenv("Inet$Resolvers");
912 char *resolvers = strdup(line), *pos, *space;
919 space = strchr(pos, ' ');
922 status = config_nameserver(&servers, &nservers, pos);
923 if (status != ARES_SUCCESS)
928 if (status == ARES_SUCCESS)
934 #elif defined(WATT32)
938 for (i = 0; def_nameservers[i]; i++)
941 return ARES_SUCCESS; /* use localhost DNS server */
944 servers = calloc(i, sizeof(struct server_state));
948 for (i = 0; def_nameservers[i]; i++)
950 servers[i].addr.addrV4.s_addr = htonl(def_nameservers[i]);
951 servers[i].addr.family = AF_INET;
955 #elif defined(ANDROID) || defined(__ANDROID__)
956 char value[PROP_VALUE_MAX]="";
957 __system_property_get("net.dns1", value);
958 status = config_nameserver(&servers, &nservers, value);
959 if (status == ARES_SUCCESS)
968 /* Don't read resolv.conf and friends if we don't have to */
969 if (ARES_CONFIG_CHECK(channel))
972 fp = fopen(PATH_RESOLV_CONF, "r");
974 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
976 if ((p = try_config(line, "domain", ';')))
977 status = config_domain(channel, p);
978 else if ((p = try_config(line, "lookup", ';')) && !channel->lookups)
979 status = config_lookup(channel, p, "bind", "file");
980 else if ((p = try_config(line, "search", ';')))
981 status = set_search(channel, p);
982 else if ((p = try_config(line, "nameserver", ';')) &&
983 channel->nservers == -1)
984 status = config_nameserver(&servers, &nservers, p);
985 else if ((p = try_config(line, "sortlist", ';')) &&
986 channel->nsort == -1)
987 status = config_sortlist(&sortlist, &nsort, p);
988 else if ((p = try_config(line, "options", ';')))
989 status = set_options(channel, p);
991 status = ARES_SUCCESS;
992 if (status != ARES_SUCCESS)
1005 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
1006 error, strerror(error)));
1007 DEBUGF(fprintf(stderr, "Error opening file: %s\n", PATH_RESOLV_CONF));
1008 status = ARES_EFILE;
1012 if ((status == ARES_EOF) && (!channel->lookups)) {
1013 /* Many systems (Solaris, Linux, BSD's) use nsswitch.conf */
1014 fp = fopen("/etc/nsswitch.conf", "r");
1016 while ((status = ares__read_line(fp, &line, &linesize)) ==
1019 if ((p = try_config(line, "hosts:", '\0')) && !channel->lookups)
1021 (void)config_lookup(channel, p, "dns", "files");
1033 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
1034 error, strerror(error)));
1035 DEBUGF(fprintf(stderr, "Error opening file: %s\n",
1036 "/etc/nsswitch.conf"));
1037 status = ARES_EFILE;
1042 if ((status == ARES_EOF) && (!channel->lookups)) {
1043 /* Linux / GNU libc 2.x and possibly others have host.conf */
1044 fp = fopen("/etc/host.conf", "r");
1046 while ((status = ares__read_line(fp, &line, &linesize)) ==
1049 if ((p = try_config(line, "order", '\0')) && !channel->lookups)
1051 (void)config_lookup(channel, p, "bind", "hosts");
1063 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
1064 error, strerror(error)));
1065 DEBUGF(fprintf(stderr, "Error opening file: %s\n",
1067 status = ARES_EFILE;
1072 if ((status == ARES_EOF) && (!channel->lookups)) {
1073 /* Tru64 uses /etc/svc.conf */
1074 fp = fopen("/etc/svc.conf", "r");
1076 while ((status = ares__read_line(fp, &line, &linesize)) ==
1079 if ((p = try_config(line, "hosts=", '\0')) && !channel->lookups)
1081 (void)config_lookup(channel, p, "bind", "local");
1093 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
1094 error, strerror(error)));
1095 DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/svc.conf"));
1096 status = ARES_EFILE;
1107 /* Handle errors. */
1108 if (status != ARES_EOF)
1110 if (servers != NULL)
1112 if (sortlist != NULL)
1117 /* If we got any name server entries, fill them in. */
1123 channel->servers = servers;
1124 channel->nservers = nservers;
1127 /* If we got any sortlist entries, fill them in. */
1130 channel->sortlist = sortlist;
1131 channel->nsort = nsort;
1134 return ARES_SUCCESS;
1137 static int init_by_defaults(ares_channel channel)
1139 char *hostname = NULL;
1140 int rc = ARES_SUCCESS;
1141 #ifdef HAVE_GETHOSTNAME
1145 if (channel->flags == -1)
1147 if (channel->timeout == -1)
1148 channel->timeout = DEFAULT_TIMEOUT;
1149 if (channel->tries == -1)
1150 channel->tries = DEFAULT_TRIES;
1151 if (channel->ndots == -1)
1153 if (channel->rotate == -1)
1154 channel->rotate = 0;
1155 if (channel->udp_port == -1)
1156 channel->udp_port = htons(NAMESERVER_PORT);
1157 if (channel->tcp_port == -1)
1158 channel->tcp_port = htons(NAMESERVER_PORT);
1160 if (channel->nservers == -1) {
1161 /* If nobody specified servers, try a local named. */
1162 channel->servers = malloc(sizeof(struct server_state));
1163 if (!channel->servers) {
1167 channel->servers[0].addr.family = AF_INET;
1168 channel->servers[0].addr.addrV4.s_addr = htonl(INADDR_LOOPBACK);
1169 channel->nservers = 1;
1172 #if defined(USE_WINSOCK)
1173 #define toolong(x) (x == -1) && (SOCKERRNO == WSAEFAULT)
1174 #elif defined(ENAMETOOLONG)
1175 #define toolong(x) (x == -1) && ((SOCKERRNO == ENAMETOOLONG) || \
1176 (SOCKERRNO == EINVAL))
1178 #define toolong(x) (x == -1) && (SOCKERRNO == EINVAL)
1181 if (channel->ndomains == -1) {
1182 /* Derive a default domain search list from the kernel hostname,
1183 * or set it to empty if the hostname isn't helpful.
1185 #ifndef HAVE_GETHOSTNAME
1186 channel->ndomains = 0; /* default to none */
1188 GETHOSTNAME_TYPE_ARG2 lenv = 64;
1191 channel->ndomains = 0; /* default to none */
1193 hostname = malloc(len);
1200 res = gethostname(hostname, lenv);
1206 p = realloc(hostname, len);
1221 dot = strchr(hostname, '.');
1223 /* a dot was found */
1224 channel->domains = malloc(sizeof(char *));
1225 if (!channel->domains) {
1229 channel->domains[0] = strdup(dot + 1);
1230 if (!channel->domains[0]) {
1234 channel->ndomains = 1;
1239 if (channel->nsort == -1) {
1240 channel->sortlist = NULL;
1244 if (!channel->lookups) {
1245 channel->lookups = strdup("fb");
1246 if (!channel->lookups)
1252 if(channel->servers) {
1253 free(channel->servers);
1254 channel->servers = NULL;
1257 if(channel->domains && channel->domains[0])
1258 free(channel->domains[0]);
1259 if(channel->domains) {
1260 free(channel->domains);
1261 channel->domains = NULL;
1264 if(channel->lookups) {
1265 free(channel->lookups);
1266 channel->lookups = NULL;
1276 #if !defined(WIN32) && !defined(WATT32) && \
1277 !defined(ANDROID) && !defined(__ANDROID__)
1278 static int config_domain(ares_channel channel, char *str)
1282 /* Set a single search domain. */
1284 while (*q && !ISSPACE(*q))
1287 return set_search(channel, str);
1290 #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER == 910) && \
1291 defined(__OPTIMIZE__) && defined(__unix__) && defined(__i386__)
1292 /* workaround icc 9.1 optimizer issue */
1293 # define vqualifier volatile
1298 static int config_lookup(ares_channel channel, const char *str,
1299 const char *bindch, const char *filech)
1301 char lookups[3], *l;
1302 const char *vqualifier p;
1304 /* Set the lookup order. Only the first letter of each work
1305 * is relevant, and it has to be "b" for DNS or "f" for the
1306 * host file. Ignore everything else.
1312 if ((*p == *bindch || *p == *filech) && l < lookups + 2) {
1313 if (*p == *bindch) *l++ = 'b';
1316 while (*p && !ISSPACE(*p) && (*p != ','))
1318 while (*p && (ISSPACE(*p) || (*p == ',')))
1322 channel->lookups = strdup(lookups);
1323 return (channel->lookups) ? ARES_SUCCESS : ARES_ENOMEM;
1325 #endif /* !WIN32 & !WATT32 & !ANDROID & !__ANDROID__ */
1328 static int config_nameserver(struct server_state **servers, int *nservers,
1331 struct ares_addr host;
1332 struct server_state *newserv;
1334 /* On Windows, there may be more than one nameserver specified in the same
1335 * registry key, so we parse input as a space or comma seperated list.
1339 /* Skip whitespace and commas. */
1340 while (*p && (ISSPACE(*p) || (*p == ',')))
1343 /* No more input, done. */
1346 /* Pointer to start of IPv4 or IPv6 address part. */
1349 /* Advance past this address. */
1350 while (*p && !ISSPACE(*p) && (*p != ','))
1353 /* Null terminate this address. */
1356 /* Reached end of input, done when this address is processed. */
1359 /* Convert textual address to binary format. */
1360 if (ares_inet_pton(AF_INET, txtaddr, &host.addrV4) == 1)
1361 host.family = AF_INET;
1362 else if (ares_inet_pton(AF_INET6, txtaddr, &host.addrV6) == 1)
1363 host.family = AF_INET6;
1367 /* Resize servers state array. */
1368 newserv = realloc(*servers, (*nservers + 1) *
1369 sizeof(struct server_state));
1373 /* Store address data. */
1374 newserv[*nservers].addr.family = host.family;
1375 if (host.family == AF_INET)
1376 memcpy(&newserv[*nservers].addr.addrV4, &host.addrV4,
1377 sizeof(host.addrV4));
1379 memcpy(&newserv[*nservers].addr.addrV6, &host.addrV6,
1380 sizeof(host.addrV6));
1382 /* Update arguments. */
1387 return ARES_SUCCESS;
1390 #if !defined(WIN32) && !defined(ANDROID) && !defined(__ANDROID__)
1391 static int config_sortlist(struct apattern **sortlist, int *nsort,
1394 struct apattern pat;
1397 /* Add sortlist entries. */
1398 while (*str && *str != ';')
1401 char ipbuf[16], ipbufpfx[32];
1402 /* Find just the IP */
1404 while (*q && *q != '/' && *q != ';' && !ISSPACE(*q))
1406 memcpy(ipbuf, str, q-str);
1407 ipbuf[q-str] = '\0';
1408 /* Find the prefix */
1411 const char *str2 = q+1;
1412 while (*q && *q != ';' && !ISSPACE(*q))
1414 memcpy(ipbufpfx, str, q-str);
1415 ipbufpfx[q-str] = '\0';
1420 /* Lets see if it is CIDR */
1421 /* First we'll try IPv6 */
1422 if ((bits = ares_inet_net_pton(AF_INET6, ipbufpfx[0] ? ipbufpfx : ipbuf,
1424 sizeof(pat.addrV6))) > 0)
1426 pat.type = PATTERN_CIDR;
1427 pat.mask.bits = (unsigned short)bits;
1428 pat.family = AF_INET6;
1429 if (!sortlist_alloc(sortlist, nsort, &pat))
1432 else if (ipbufpfx[0] &&
1433 (bits = ares_inet_net_pton(AF_INET, ipbufpfx, &pat.addrV4,
1434 sizeof(pat.addrV4))) > 0)
1436 pat.type = PATTERN_CIDR;
1437 pat.mask.bits = (unsigned short)bits;
1438 pat.family = AF_INET;
1439 if (!sortlist_alloc(sortlist, nsort, &pat))
1442 /* See if it is just a regular IP */
1443 else if (ip_addr(ipbuf, q-str, &pat.addrV4) == 0)
1447 memcpy(ipbuf, str, q-str);
1448 ipbuf[q-str] = '\0';
1449 if (ip_addr(ipbuf, q-str, &pat.mask.addr4) != 0)
1454 pat.family = AF_INET;
1455 pat.type = PATTERN_MASK;
1456 if (!sortlist_alloc(sortlist, nsort, &pat))
1461 while (*q && *q != ';' && !ISSPACE(*q))
1465 while (ISSPACE(*str))
1469 return ARES_SUCCESS;
1471 #endif /* !WIN32 & !ANDROID & !__ANDROID__ */
1472 #endif /* !WATT32 */
1474 static int set_search(ares_channel channel, const char *str)
1479 if(channel->ndomains != -1) {
1480 /* if we already have some domains present, free them first */
1481 for(n=0; n < channel->ndomains; n++)
1482 free(channel->domains[n]);
1483 free(channel->domains);
1484 channel->domains = NULL;
1485 channel->ndomains = -1;
1488 /* Count the domains given. */
1493 while (*p && !ISSPACE(*p))
1502 channel->ndomains = 0;
1503 return ARES_SUCCESS;
1506 channel->domains = malloc(n * sizeof(char *));
1507 if (!channel->domains)
1510 /* Now copy the domains. */
1515 channel->ndomains = n;
1517 while (*q && !ISSPACE(*q))
1519 channel->domains[n] = malloc(q - p + 1);
1520 if (!channel->domains[n])
1522 memcpy(channel->domains[n], p, q - p);
1523 channel->domains[n][q - p] = 0;
1529 channel->ndomains = n;
1531 return ARES_SUCCESS;
1534 static int set_options(ares_channel channel, const char *str)
1536 const char *p, *q, *val;
1542 while (*q && !ISSPACE(*q))
1544 val = try_option(p, q, "ndots:");
1545 if (val && channel->ndots == -1)
1546 channel->ndots = aresx_sltosi(strtol(val, NULL, 10));
1547 val = try_option(p, q, "retrans:");
1548 if (val && channel->timeout == -1)
1549 channel->timeout = aresx_sltosi(strtol(val, NULL, 10));
1550 val = try_option(p, q, "retry:");
1551 if (val && channel->tries == -1)
1552 channel->tries = aresx_sltosi(strtol(val, NULL, 10));
1553 val = try_option(p, q, "rotate");
1554 if (val && channel->rotate == -1)
1555 channel->rotate = 1;
1561 return ARES_SUCCESS;
1564 static const char *try_option(const char *p, const char *q, const char *opt)
1566 size_t len = strlen(opt);
1567 return ((size_t)(q - p) >= len && !strncmp(p, opt, len)) ? &p[len] : NULL;
1570 #if !defined(WIN32) && !defined(WATT32) && \
1571 !defined(ANDROID) && !defined(__ANDROID__)
1572 static char *try_config(char *s, const char *opt, char scc)
1579 /* no line or no option */
1582 /* Hash '#' character is always used as primary comment char, additionally
1583 a not-NUL secondary comment char will be considered when specified. */
1585 /* trim line comment */
1588 while (*p && (*p != '#') && (*p != scc))
1591 while (*p && (*p != '#'))
1595 /* trim trailing whitespace */
1597 while ((q >= s) && ISSPACE(*q))
1601 /* skip leading whitespace */
1603 while (*p && ISSPACE(*p))
1610 if ((len = strlen(opt)) == 0)
1614 if (strncmp(p, opt, len) != 0)
1615 /* line and option do not match */
1618 /* skip over given option name */
1622 /* no option value */
1625 if ((opt[len-1] != ':') && (opt[len-1] != '=') && !ISSPACE(*p))
1626 /* whitespace between option name and value is mandatory
1627 for given option names which do not end with ':' or '=' */
1630 /* skip over whitespace */
1631 while (*p && ISSPACE(*p))
1635 /* no option value */
1638 /* return pointer to option value */
1642 static int sortlist_alloc(struct apattern **sortlist, int *nsort,
1643 struct apattern *pat)
1645 struct apattern *newsort;
1646 newsort = realloc(*sortlist, (*nsort + 1) * sizeof(struct apattern));
1649 newsort[*nsort] = *pat;
1650 *sortlist = newsort;
1655 static int ip_addr(const char *ipbuf, ssize_t len, struct in_addr *addr)
1658 /* Four octets and three periods yields at most 15 characters. */
1662 addr->s_addr = inet_addr(ipbuf);
1663 if (addr->s_addr == INADDR_NONE && strcmp(ipbuf, "255.255.255.255") != 0)
1668 static void natural_mask(struct apattern *pat)
1670 struct in_addr addr;
1672 /* Store a host-byte-order copy of pat in a struct in_addr. Icky,
1675 addr.s_addr = ntohl(pat->addrV4.s_addr);
1677 /* This is out of date in the CIDR world, but some people might
1680 if (IN_CLASSA(addr.s_addr))
1681 pat->mask.addr4.s_addr = htonl(IN_CLASSA_NET);
1682 else if (IN_CLASSB(addr.s_addr))
1683 pat->mask.addr4.s_addr = htonl(IN_CLASSB_NET);
1685 pat->mask.addr4.s_addr = htonl(IN_CLASSC_NET);
1687 #endif /* !WIN32 & !WATT32 & !ANDROID & !__ANDROID__ */
1689 /* initialize an rc4 key. If possible a cryptographically secure random key
1690 is generated using a suitable function (for example win32's RtlGenRandom as
1692 http://blogs.msdn.com/michael_howard/archive/2005/01/14/353379.aspx
1693 otherwise the code defaults to cross-platform albeit less secure mechanism
1696 static void randomize_key(unsigned char* key,int key_data_len)
1702 if (ares_fpSystemFunction036)
1704 res = (*ares_fpSystemFunction036) (key, key_data_len);
1710 FILE *f = fopen(RANDOM_FILE, "rb");
1712 counter = aresx_uztosi(fread(key, 1, key_data_len, f));
1719 for (;counter<key_data_len;counter++)
1720 key[counter]=(unsigned char)(rand() % 256);
1724 static int init_id_key(rc4_key* key,int key_data_len)
1726 unsigned char index1;
1727 unsigned char index2;
1728 unsigned char* state;
1730 unsigned char *key_data_ptr = 0;
1732 key_data_ptr = calloc(1,key_data_len);
1736 state = &key->state[0];
1737 for(counter = 0; counter < 256; counter++)
1738 /* unnecessary AND but it keeps some compilers happier */
1739 state[counter] = (unsigned char)(counter & 0xff);
1740 randomize_key(key->state,key_data_len);
1745 for(counter = 0; counter < 256; counter++)
1747 index2 = (unsigned char)((key_data_ptr[index1] + state[counter] +
1749 ARES_SWAP_BYTE(&state[counter], &state[index2]);
1751 index1 = (unsigned char)((index1 + 1) % key_data_len);
1754 return ARES_SUCCESS;
1757 unsigned short ares__generate_new_id(rc4_key* key)
1760 ares__rc4(key, (unsigned char *)&r, sizeof(r));
1764 void ares_set_local_ip4(ares_channel channel, unsigned int local_ip)
1766 channel->local_ip4 = local_ip;
1769 /* local_ip6 should be 16 bytes in length */
1770 void ares_set_local_ip6(ares_channel channel,
1771 const unsigned char* local_ip6)
1773 memcpy(&channel->local_ip6, local_ip6, sizeof(channel->local_ip6));
1776 /* local_dev_name should be null terminated. */
1777 void ares_set_local_dev(ares_channel channel,
1778 const char* local_dev_name)
1780 strncpy(channel->local_dev_name, local_dev_name,
1781 sizeof(channel->local_dev_name));
1782 channel->local_dev_name[sizeof(channel->local_dev_name) - 1] = 0;
1786 void ares_set_socket_callback(ares_channel channel,
1787 ares_sock_create_callback cb,
1790 channel->sock_create_cb = cb;
1791 channel->sock_create_cb_data = data;
1794 void ares__init_servers_state(ares_channel channel)
1796 struct server_state *server;
1799 for (i = 0; i < channel->nservers; i++)
1801 server = &channel->servers[i];
1802 server->udp_socket = ARES_SOCKET_BAD;
1803 server->tcp_socket = ARES_SOCKET_BAD;
1804 server->tcp_connection_generation = ++channel->tcp_connection_generation;
1805 server->tcp_lenbuf_pos = 0;
1806 server->tcp_buffer_pos = 0;
1807 server->tcp_buffer = NULL;
1808 server->tcp_length = 0;
1809 server->qhead = NULL;
1810 server->qtail = NULL;
1811 ares__init_list_head(&server->queries_to_server);
1812 server->channel = channel;
1813 server->is_broken = 0;