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 static int sortlist_alloc(struct apattern **sortlist, int *nsort,
97 struct apattern *pat);
98 static int ip_addr(const char *s, ssize_t len, struct in_addr *addr);
99 static void natural_mask(struct apattern *pat);
100 #if !defined(ANDROID) && !defined(__ANDROID__)
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);
110 #define ARES_CONFIG_CHECK(x) (x->lookups && x->nsort > -1 && \
111 x->nservers > -1 && \
112 x->ndomains > -1 && \
113 x->ndots > -1 && x->timeout > -1 && \
116 int ares_init(ares_channel *channelptr)
118 return ares_init_options(channelptr, NULL, 0);
121 int ares_init_options(ares_channel *channelptr, struct ares_options *options,
124 ares_channel channel;
126 int status = ARES_SUCCESS;
130 const char *env = getenv("CARES_MEMDEBUG");
134 env = getenv("CARES_MEMLIMIT");
137 long num = strtol(env, &endptr, 10);
138 if((endptr != env) && (endptr == env + strlen(env)) && (num > 0))
143 if (ares_library_initialized() != ARES_SUCCESS)
144 return ARES_ENOTINITIALIZED;
146 channel = malloc(sizeof(struct ares_channeldata));
154 /* Set everything to distinguished values so we know they haven't
158 channel->timeout = -1;
161 channel->rotate = -1;
162 channel->udp_port = -1;
163 channel->tcp_port = -1;
164 channel->socket_send_buffer_size = -1;
165 channel->socket_receive_buffer_size = -1;
166 channel->nservers = -1;
167 channel->ndomains = -1;
169 channel->tcp_connection_generation = 0;
170 channel->lookups = NULL;
171 channel->domains = NULL;
172 channel->sortlist = NULL;
173 channel->servers = NULL;
174 channel->sock_state_cb = NULL;
175 channel->sock_state_cb_data = NULL;
176 channel->sock_create_cb = NULL;
177 channel->sock_create_cb_data = NULL;
179 channel->last_server = 0;
180 channel->last_timeout_processed = (time_t)now.tv_sec;
182 memset(&channel->local_dev_name, 0, sizeof(channel->local_dev_name));
183 channel->local_ip4 = 0;
184 memset(&channel->local_ip6, 0, sizeof(channel->local_ip6));
186 /* Initialize our lists of queries */
187 ares__init_list_head(&(channel->all_queries));
188 for (i = 0; i < ARES_QID_TABLE_SIZE; i++)
190 ares__init_list_head(&(channel->queries_by_qid[i]));
192 for (i = 0; i < ARES_TIMEOUT_TABLE_SIZE; i++)
194 ares__init_list_head(&(channel->queries_by_timeout[i]));
197 /* Initialize configuration by each of the four sources, from highest
198 * precedence to lowest.
201 if (status == ARES_SUCCESS) {
202 status = init_by_options(channel, options, optmask);
203 if (status != ARES_SUCCESS)
204 DEBUGF(fprintf(stderr, "Error: init_by_options failed: %s\n",
205 ares_strerror(status)));
207 if (status == ARES_SUCCESS) {
208 status = init_by_environment(channel);
209 if (status != ARES_SUCCESS)
210 DEBUGF(fprintf(stderr, "Error: init_by_environment failed: %s\n",
211 ares_strerror(status)));
213 if (status == ARES_SUCCESS) {
214 status = init_by_resolv_conf(channel);
215 if (status != ARES_SUCCESS)
216 DEBUGF(fprintf(stderr, "Error: init_by_resolv_conf failed: %s\n",
217 ares_strerror(status)));
221 * No matter what failed or succeeded, seed defaults to provide
222 * useful behavior for things that we missed.
224 status = init_by_defaults(channel);
225 if (status != ARES_SUCCESS)
226 DEBUGF(fprintf(stderr, "Error: init_by_defaults failed: %s\n",
227 ares_strerror(status)));
229 /* Generate random key */
231 if (status == ARES_SUCCESS) {
232 status = init_id_key(&channel->id_key, ARES_ID_KEY_LEN);
233 if (status == ARES_SUCCESS)
234 channel->next_id = ares__generate_new_id(&channel->id_key);
236 DEBUGF(fprintf(stderr, "Error: init_id_key failed: %s\n",
237 ares_strerror(status)));
240 if (status != ARES_SUCCESS)
242 /* Something failed; clean up memory we may have allocated. */
243 if (channel->servers)
244 free(channel->servers);
245 if (channel->domains)
247 for (i = 0; i < channel->ndomains; i++)
248 free(channel->domains[i]);
249 free(channel->domains);
251 if (channel->sortlist)
252 free(channel->sortlist);
254 free(channel->lookups);
259 /* Trim to one server if ARES_FLAG_PRIMARY is set. */
260 if ((channel->flags & ARES_FLAG_PRIMARY) && channel->nservers > 1)
261 channel->nservers = 1;
263 ares__init_servers_state(channel);
265 *channelptr = channel;
269 /* ares_dup() duplicates a channel handle with all its options and returns a
270 new channel handle */
271 int ares_dup(ares_channel *dest, ares_channel src)
273 struct ares_options opts;
274 struct ares_addr_node *servers;
275 int ipv6_nservers = 0;
279 *dest = NULL; /* in case of failure return NULL explicitly */
281 /* First get the options supported by the old ares_save_options() function,
282 which is most of them */
283 rc = ares_save_options(src, &opts, &optmask);
287 /* Then create the new channel with those options */
288 rc = ares_init_options(dest, &opts, optmask);
290 /* destroy the options copy to not leak any memory */
291 ares_destroy_options(&opts);
296 /* Now clone the options that ares_save_options() doesn't support. */
297 (*dest)->sock_create_cb = src->sock_create_cb;
298 (*dest)->sock_create_cb_data = src->sock_create_cb_data;
300 strncpy((*dest)->local_dev_name, src->local_dev_name,
301 sizeof(src->local_dev_name));
302 (*dest)->local_ip4 = src->local_ip4;
303 memcpy((*dest)->local_ip6, src->local_ip6, sizeof(src->local_ip6));
305 /* Full name server cloning required when not all are IPv4 */
306 for (i = 0; i < src->nservers; i++)
308 if (src->servers[i].addr.family != AF_INET) {
314 rc = ares_get_servers(src, &servers);
315 if (rc != ARES_SUCCESS)
317 rc = ares_set_servers(*dest, servers);
318 ares_free_data(servers);
319 if (rc != ARES_SUCCESS)
323 return ARES_SUCCESS; /* everything went fine */
326 /* Save options from initialized channel */
327 int ares_save_options(ares_channel channel, struct ares_options *options,
331 int ipv4_nservers = 0;
333 /* Zero everything out */
334 memset(options, 0, sizeof(struct ares_options));
336 if (!ARES_CONFIG_CHECK(channel))
339 /* Traditionally the optmask wasn't saved in the channel struct so it was
340 recreated here. ROTATE is the first option that has no struct field of
341 its own in the public config struct */
342 (*optmask) = (ARES_OPT_FLAGS|ARES_OPT_TRIES|ARES_OPT_NDOTS|
343 ARES_OPT_UDP_PORT|ARES_OPT_TCP_PORT|ARES_OPT_SOCK_STATE_CB|
344 ARES_OPT_SERVERS|ARES_OPT_DOMAINS|ARES_OPT_LOOKUPS|
345 ARES_OPT_SORTLIST|ARES_OPT_TIMEOUTMS) |
346 (channel->optmask & ARES_OPT_ROTATE);
348 /* Copy easy stuff */
349 options->flags = channel->flags;
351 /* We return full millisecond resolution but that's only because we don't
352 set the ARES_OPT_TIMEOUT anymore, only the new ARES_OPT_TIMEOUTMS */
353 options->timeout = channel->timeout;
354 options->tries = channel->tries;
355 options->ndots = channel->ndots;
356 options->udp_port = (unsigned short)channel->udp_port;
357 options->tcp_port = (unsigned short)channel->tcp_port;
358 options->sock_state_cb = channel->sock_state_cb;
359 options->sock_state_cb_data = channel->sock_state_cb_data;
361 /* Copy IPv4 servers */
362 if (channel->nservers) {
363 for (i = 0; i < channel->nservers; i++)
365 if (channel->servers[i].addr.family == AF_INET)
369 options->servers = malloc(ipv4_nservers * sizeof(struct in_addr));
370 if (!options->servers)
372 for (i = j = 0; i < channel->nservers; i++)
374 if (channel->servers[i].addr.family == AF_INET)
375 memcpy(&options->servers[j++],
376 &channel->servers[i].addr.addrV4,
377 sizeof(channel->servers[i].addr.addrV4));
381 options->nservers = ipv4_nservers;
384 if (channel->ndomains) {
385 options->domains = malloc(channel->ndomains * sizeof(char *));
386 if (!options->domains)
389 for (i = 0; i < channel->ndomains; i++)
391 options->ndomains = i;
392 options->domains[i] = strdup(channel->domains[i]);
393 if (!options->domains[i])
397 options->ndomains = channel->ndomains;
400 if (channel->lookups) {
401 options->lookups = strdup(channel->lookups);
402 if (!options->lookups && channel->lookups)
407 if (channel->nsort) {
408 options->sortlist = malloc(channel->nsort * sizeof(struct apattern));
409 if (!options->sortlist)
411 for (i = 0; i < channel->nsort; i++)
412 options->sortlist[i] = channel->sortlist[i];
414 options->nsort = channel->nsort;
419 static int init_by_options(ares_channel channel,
420 const struct ares_options *options,
426 if ((optmask & ARES_OPT_FLAGS) && channel->flags == -1)
427 channel->flags = options->flags;
428 if ((optmask & ARES_OPT_TIMEOUTMS) && channel->timeout == -1)
429 channel->timeout = options->timeout;
430 else if ((optmask & ARES_OPT_TIMEOUT) && channel->timeout == -1)
431 channel->timeout = options->timeout * 1000;
432 if ((optmask & ARES_OPT_TRIES) && channel->tries == -1)
433 channel->tries = options->tries;
434 if ((optmask & ARES_OPT_NDOTS) && channel->ndots == -1)
435 channel->ndots = options->ndots;
436 if ((optmask & ARES_OPT_ROTATE) && channel->rotate == -1)
438 if ((optmask & ARES_OPT_UDP_PORT) && channel->udp_port == -1)
439 channel->udp_port = htons(options->udp_port);
440 if ((optmask & ARES_OPT_TCP_PORT) && channel->tcp_port == -1)
441 channel->tcp_port = htons(options->tcp_port);
442 if ((optmask & ARES_OPT_SOCK_STATE_CB) && channel->sock_state_cb == NULL)
444 channel->sock_state_cb = options->sock_state_cb;
445 channel->sock_state_cb_data = options->sock_state_cb_data;
447 if ((optmask & ARES_OPT_SOCK_SNDBUF)
448 && channel->socket_send_buffer_size == -1)
449 channel->socket_send_buffer_size = options->socket_send_buffer_size;
450 if ((optmask & ARES_OPT_SOCK_RCVBUF)
451 && channel->socket_receive_buffer_size == -1)
452 channel->socket_receive_buffer_size = options->socket_receive_buffer_size;
454 /* Copy the IPv4 servers, if given. */
455 if ((optmask & ARES_OPT_SERVERS) && channel->nservers == -1)
457 /* Avoid zero size allocations at any cost */
458 if (options->nservers > 0)
461 malloc(options->nservers * sizeof(struct server_state));
462 if (!channel->servers)
464 for (i = 0; i < options->nservers; i++)
466 channel->servers[i].addr.family = AF_INET;
467 memcpy(&channel->servers[i].addr.addrV4,
468 &options->servers[i],
469 sizeof(channel->servers[i].addr.addrV4));
472 channel->nservers = options->nservers;
475 /* Copy the domains, if given. Keep channel->ndomains consistent so
476 * we can clean up in case of error.
478 if ((optmask & ARES_OPT_DOMAINS) && channel->ndomains == -1)
480 /* Avoid zero size allocations at any cost */
481 if (options->ndomains > 0)
483 channel->domains = malloc(options->ndomains * sizeof(char *));
484 if (!channel->domains)
486 for (i = 0; i < options->ndomains; i++)
488 channel->ndomains = i;
489 channel->domains[i] = strdup(options->domains[i]);
490 if (!channel->domains[i])
494 channel->ndomains = options->ndomains;
497 /* Set lookups, if given. */
498 if ((optmask & ARES_OPT_LOOKUPS) && !channel->lookups)
500 channel->lookups = strdup(options->lookups);
501 if (!channel->lookups)
506 if ((optmask & ARES_OPT_SORTLIST) && (channel->nsort == -1) &&
507 (options->nsort>0)) {
508 channel->sortlist = malloc(options->nsort * sizeof(struct apattern));
509 if (!channel->sortlist)
511 for (i = 0; i < options->nsort; i++)
512 channel->sortlist[i] = options->sortlist[i];
513 channel->nsort = options->nsort;
516 channel->optmask = optmask;
521 static int init_by_environment(ares_channel channel)
523 const char *localdomain, *res_options;
526 localdomain = getenv("LOCALDOMAIN");
527 if (localdomain && channel->ndomains == -1)
529 status = set_search(channel, localdomain);
530 if (status != ARES_SUCCESS)
534 res_options = getenv("RES_OPTIONS");
537 status = set_options(channel, res_options);
538 if (status != ARES_SUCCESS)
547 * Warning: returns a dynamically allocated buffer, the user MUST
548 * use free() if the function returns 1
550 static int get_res_nt(HKEY hKey, const char *subkey, char **obuf)
552 /* Test for the size we need */
556 result = RegQueryValueEx(hKey, subkey, 0, NULL, NULL, &size);
557 if ((result != ERROR_SUCCESS && result != ERROR_MORE_DATA) || !size)
559 *obuf = malloc(size+1);
563 if (RegQueryValueEx(hKey, subkey, 0, NULL,
564 (LPBYTE)*obuf, &size) != ERROR_SUCCESS)
577 static int get_res_interfaces_nt(HKEY hKey, const char *subkey, char **obuf)
579 char enumbuf[39]; /* GUIDs are 38 chars + 1 for NULL */
580 DWORD enum_size = 39;
584 while (RegEnumKeyEx(hKey, idx++, enumbuf, &enum_size, 0,
585 NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS)
590 if (RegOpenKeyEx(hKey, enumbuf, 0, KEY_QUERY_VALUE, &hVal) !=
593 rc = get_res_nt(hVal, subkey, obuf);
601 /* get_iphlpapi_dns_classic() is supported on W98 and newer */
602 static int get_iphlpapi_dns_classic(char *ret_buf, size_t ret_size)
604 FIXED_INFO *fi, *newfi;
605 struct ares_addr namesrvr;
607 IP_ADDR_STRING *ipAddr;
610 DWORD size = sizeof (*fi);
611 char *endptr = ret_buf;
620 res = (*ares_fpGetNetworkParams) (fi, &size);
621 if ((res != ERROR_BUFFER_OVERFLOW) && (res != ERROR_SUCCESS))
624 newfi = realloc(fi, size);
629 res = (*ares_fpGetNetworkParams) (fi, &size);
630 if (res != ERROR_SUCCESS)
633 for (ipAddr = &fi->DnsServerList; ipAddr; ipAddr = ipAddr->Next)
635 txtaddr = &ipAddr->IpAddress.String[0];
637 /* Validate converting textual address to binary format. */
638 if (ares_inet_pton(AF_INET, txtaddr, &namesrvr.addrV4) == 1)
640 if ((namesrvr.addrV4.S_un.S_addr == INADDR_ANY) ||
641 (namesrvr.addrV4.S_un.S_addr == INADDR_NONE))
644 else if (ares_inet_pton(AF_INET6, txtaddr, &namesrvr.addrV6) == 1)
646 if (memcmp(&namesrvr.addrV6, &ares_in6addr_any,
647 sizeof(namesrvr.addrV6)) == 0)
653 txtlen = strlen(txtaddr);
654 if (ret_size >= strlen(ret_buf) + txtlen + 2)
656 sprintf(endptr, "%s,", txtaddr);
657 endptr += txtlen + 1;
666 if (endptr != ret_buf)
667 *(endptr - 1) = '\0';
672 #define IPAA_INITIAL_BUF_SZ 15 * 1024
673 #define IPAA_MAX_TRIES 3
675 static int get_iphlpapi_dns_info(char *ret_buf, size_t ret_size)
677 IP_ADAPTER_DNS_SERVER_ADDRESS *ipaDNSAddr;
678 IP_ADAPTER_ADDRESSES *ipaa, *newipaa, *ipaaEntry;
680 ULONG ReqBufsz = IPAA_INITIAL_BUF_SZ;
681 ULONG Bufsz = IPAA_INITIAL_BUF_SZ;
683 char *endptr = ret_buf;
684 int trying = IPAA_MAX_TRIES;
689 struct sockaddr_in *sa4;
690 struct sockaddr_in6 *sa6;
693 char txtaddr[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
696 /* Unless GetAdaptersAddresses is available, use GetNetworkParams */
697 if (ares_fpGetAdaptersAddresses == ZERO_NULL)
698 return get_iphlpapi_dns_classic(ret_buf, ret_size);
702 ipaa = malloc(Bufsz);
706 /* Usually this call suceeds with initial buffer size */
707 res = (*ares_fpGetAdaptersAddresses) (AF_UNSPEC, AddrFlags, NULL,
709 if ((res != ERROR_BUFFER_OVERFLOW) && (res != ERROR_SUCCESS))
712 while ((res == ERROR_BUFFER_OVERFLOW) && (--trying))
714 if (Bufsz < ReqBufsz)
716 newipaa = realloc(ipaa, ReqBufsz);
722 res = (*ares_fpGetAdaptersAddresses) (AF_UNSPEC, AddrFlags, NULL,
724 if (res == ERROR_SUCCESS)
727 if (res != ERROR_SUCCESS)
730 for (ipaaEntry = ipaa; ipaaEntry; ipaaEntry = ipaaEntry->Next)
732 for (ipaDNSAddr = ipaaEntry->FirstDnsServerAddress;
734 ipaDNSAddr = ipaDNSAddr->Next)
736 namesrvr.sa = ipaDNSAddr->Address.lpSockaddr;
738 if (namesrvr.sa->sa_family == AF_INET)
740 if ((namesrvr.sa4->sin_addr.S_un.S_addr == INADDR_ANY) ||
741 (namesrvr.sa4->sin_addr.S_un.S_addr == INADDR_NONE))
743 if (! ares_inet_ntop(AF_INET, &namesrvr.sa4->sin_addr,
744 txtaddr, sizeof(txtaddr)))
747 else if (namesrvr.sa->sa_family == AF_INET6)
749 if (memcmp(&namesrvr.sa6->sin6_addr, &ares_in6addr_any,
750 sizeof(namesrvr.sa6->sin6_addr)) == 0)
752 if (! ares_inet_ntop(AF_INET, &namesrvr.sa6->sin6_addr,
753 txtaddr, sizeof(txtaddr)))
759 txtlen = strlen(txtaddr);
760 if (ret_size >= strlen(ret_buf) + txtlen + 2)
762 sprintf(endptr, "%s,", txtaddr);
763 endptr += txtlen + 1;
773 if (endptr != ret_buf)
774 *(endptr - 1) = '\0';
780 static int init_by_resolv_conf(ares_channel channel)
782 #if !defined(ANDROID) && !defined(__ANDROID__) && !defined(WATT32)
785 int status = -1, nservers = 0, nsort = 0;
786 struct server_state *servers = NULL;
787 struct apattern *sortlist = NULL;
792 NameServer info via IPHLPAPI (IP helper API):
793 GetNetworkParams() should be the trusted source for this.
794 Available in Win-98/2000 and later. If that fail, fall-back to
795 registry information.
799 On Windows 9X, the DNS server can be found in:
800 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\MSTCP\NameServer
802 On Windows NT/2000/XP/2003:
803 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NameServer
805 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DhcpNameServer
807 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\{AdapterID}\
810 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\{AdapterID}\
820 win_platform platform;
822 if (channel->nservers > -1) /* don't override ARES_OPT_SERVER */
825 if (get_iphlpapi_dns_info(buf,sizeof(buf)) > 0)
827 status = config_nameserver(&servers, &nservers, buf);
828 if (status == ARES_SUCCESS)
832 platform = ares__getplatform();
834 if (platform == WIN_NT)
837 HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
841 RegOpenKeyEx(mykey, "Interfaces", 0,
842 KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS, &subkey);
843 if (get_res_nt(mykey, NAMESERVER, &line))
845 status = config_nameserver(&servers, &nservers, line);
848 else if (get_res_nt(mykey, DHCPNAMESERVER, &line))
850 status = config_nameserver(&servers, &nservers, line);
853 /* Try the interfaces */
854 else if (get_res_interfaces_nt(subkey, NAMESERVER, &line))
856 status = config_nameserver(&servers, &nservers, line);
859 else if (get_res_interfaces_nt(subkey, DHCPNAMESERVER, &line))
861 status = config_nameserver(&servers, &nservers, line);
868 else if (platform == WIN_9X)
871 HKEY_LOCAL_MACHINE, WIN_NS_9X, 0,
875 if ((result = RegQueryValueEx(
876 mykey, NAMESERVER, NULL, &data_type,
879 ) == ERROR_SUCCESS ||
880 result == ERROR_MORE_DATA)
884 line = malloc(bytes+1);
885 if (RegQueryValueEx(mykey, NAMESERVER, NULL, &data_type,
886 (unsigned char *)line, &bytes) ==
889 status = config_nameserver(&servers, &nservers, line);
898 if (status == ARES_SUCCESS)
901 /* Catch the case when all the above checks fail (which happens when there
902 is no network card or the cable is unplugged) */
905 #elif defined(__riscos__)
907 /* Under RISC OS, name servers are listed in the
908 system variable Inet$Resolvers, space separated. */
910 line = getenv("Inet$Resolvers");
913 char *resolvers = strdup(line), *pos, *space;
920 space = strchr(pos, ' ');
923 status = config_nameserver(&servers, &nservers, pos);
924 if (status != ARES_SUCCESS)
929 if (status == ARES_SUCCESS)
935 #elif defined(WATT32)
939 for (i = 0; def_nameservers[i]; i++)
942 return ARES_SUCCESS; /* use localhost DNS server */
945 servers = calloc(i, sizeof(struct server_state));
949 for (i = 0; def_nameservers[i]; i++)
951 servers[i].addr.addrV4.s_addr = htonl(def_nameservers[i]);
952 servers[i].addr.family = AF_INET;
956 #elif defined(ANDROID) || defined(__ANDROID__)
957 char value[PROP_VALUE_MAX]="";
958 __system_property_get("net.dns1", value);
959 status = config_nameserver(&servers, &nservers, value);
960 if (status == ARES_SUCCESS)
969 /* Don't read resolv.conf and friends if we don't have to */
970 if (ARES_CONFIG_CHECK(channel))
973 fp = fopen(PATH_RESOLV_CONF, "r");
975 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
977 if ((p = try_config(line, "domain", ';')))
978 status = config_domain(channel, p);
979 else if ((p = try_config(line, "lookup", ';')) && !channel->lookups)
980 status = config_lookup(channel, p, "bind", "file");
981 else if ((p = try_config(line, "search", ';')))
982 status = set_search(channel, p);
983 else if ((p = try_config(line, "nameserver", ';')) &&
984 channel->nservers == -1)
985 status = config_nameserver(&servers, &nservers, p);
986 else if ((p = try_config(line, "sortlist", ';')) &&
987 channel->nsort == -1)
988 status = config_sortlist(&sortlist, &nsort, p);
989 else if ((p = try_config(line, "options", ';')))
990 status = set_options(channel, p);
992 status = ARES_SUCCESS;
993 if (status != ARES_SUCCESS)
1006 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
1007 error, strerror(error)));
1008 DEBUGF(fprintf(stderr, "Error opening file: %s\n", PATH_RESOLV_CONF));
1009 status = ARES_EFILE;
1013 if ((status == ARES_EOF) && (!channel->lookups)) {
1014 /* Many systems (Solaris, Linux, BSD's) use nsswitch.conf */
1015 fp = fopen("/etc/nsswitch.conf", "r");
1017 while ((status = ares__read_line(fp, &line, &linesize)) ==
1020 if ((p = try_config(line, "hosts:", '\0')) && !channel->lookups)
1022 (void)config_lookup(channel, p, "dns", "files");
1034 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
1035 error, strerror(error)));
1036 DEBUGF(fprintf(stderr, "Error opening file: %s\n",
1037 "/etc/nsswitch.conf"));
1038 status = ARES_EFILE;
1043 if ((status == ARES_EOF) && (!channel->lookups)) {
1044 /* Linux / GNU libc 2.x and possibly others have host.conf */
1045 fp = fopen("/etc/host.conf", "r");
1047 while ((status = ares__read_line(fp, &line, &linesize)) ==
1050 if ((p = try_config(line, "order", '\0')) && !channel->lookups)
1052 (void)config_lookup(channel, p, "bind", "hosts");
1064 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
1065 error, strerror(error)));
1066 DEBUGF(fprintf(stderr, "Error opening file: %s\n",
1068 status = ARES_EFILE;
1073 if ((status == ARES_EOF) && (!channel->lookups)) {
1074 /* Tru64 uses /etc/svc.conf */
1075 fp = fopen("/etc/svc.conf", "r");
1077 while ((status = ares__read_line(fp, &line, &linesize)) ==
1080 if ((p = try_config(line, "hosts=", '\0')) && !channel->lookups)
1082 (void)config_lookup(channel, p, "bind", "local");
1094 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
1095 error, strerror(error)));
1096 DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/svc.conf"));
1097 status = ARES_EFILE;
1108 /* Handle errors. */
1109 if (status != ARES_EOF)
1111 if (servers != NULL)
1113 if (sortlist != NULL)
1118 /* If we got any name server entries, fill them in. */
1124 channel->servers = servers;
1125 channel->nservers = nservers;
1128 /* If we got any sortlist entries, fill them in. */
1131 channel->sortlist = sortlist;
1132 channel->nsort = nsort;
1135 return ARES_SUCCESS;
1138 static int init_by_defaults(ares_channel channel)
1140 char *hostname = NULL;
1141 int rc = ARES_SUCCESS;
1142 #ifdef HAVE_GETHOSTNAME
1146 if (channel->flags == -1)
1148 if (channel->timeout == -1)
1149 channel->timeout = DEFAULT_TIMEOUT;
1150 if (channel->tries == -1)
1151 channel->tries = DEFAULT_TRIES;
1152 if (channel->ndots == -1)
1154 if (channel->rotate == -1)
1155 channel->rotate = 0;
1156 if (channel->udp_port == -1)
1157 channel->udp_port = htons(NAMESERVER_PORT);
1158 if (channel->tcp_port == -1)
1159 channel->tcp_port = htons(NAMESERVER_PORT);
1161 if (channel->nservers == -1) {
1162 /* If nobody specified servers, try a local named. */
1163 channel->servers = malloc(sizeof(struct server_state));
1164 if (!channel->servers) {
1168 channel->servers[0].addr.family = AF_INET;
1169 channel->servers[0].addr.addrV4.s_addr = htonl(INADDR_LOOPBACK);
1170 channel->nservers = 1;
1173 #if defined(USE_WINSOCK)
1174 #define toolong(x) (x == -1) && (SOCKERRNO == WSAEFAULT)
1175 #elif defined(ENAMETOOLONG)
1176 #define toolong(x) (x == -1) && ((SOCKERRNO == ENAMETOOLONG) || \
1177 (SOCKERRNO == EINVAL))
1179 #define toolong(x) (x == -1) && (SOCKERRNO == EINVAL)
1182 if (channel->ndomains == -1) {
1183 /* Derive a default domain search list from the kernel hostname,
1184 * or set it to empty if the hostname isn't helpful.
1186 #ifndef HAVE_GETHOSTNAME
1187 channel->ndomains = 0; /* default to none */
1189 GETHOSTNAME_TYPE_ARG2 lenv = 64;
1192 channel->ndomains = 0; /* default to none */
1194 hostname = malloc(len);
1201 res = gethostname(hostname, lenv);
1207 p = realloc(hostname, len);
1222 dot = strchr(hostname, '.');
1224 /* a dot was found */
1225 channel->domains = malloc(sizeof(char *));
1226 if (!channel->domains) {
1230 channel->domains[0] = strdup(dot + 1);
1231 if (!channel->domains[0]) {
1235 channel->ndomains = 1;
1240 if (channel->nsort == -1) {
1241 channel->sortlist = NULL;
1245 if (!channel->lookups) {
1246 channel->lookups = strdup("fb");
1247 if (!channel->lookups)
1253 if(channel->servers) {
1254 free(channel->servers);
1255 channel->servers = NULL;
1258 if(channel->domains && channel->domains[0])
1259 free(channel->domains[0]);
1260 if(channel->domains) {
1261 free(channel->domains);
1262 channel->domains = NULL;
1265 if(channel->lookups) {
1266 free(channel->lookups);
1267 channel->lookups = NULL;
1277 #if !defined(WIN32) && !defined(WATT32)
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 */
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;
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;
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 static char *try_config(char *s, const char *opt, char scc)
1578 /* no line or no option */
1581 /* Hash '#' character is always used as primary comment char, additionally
1582 a not-NUL secondary comment char will be considered when specified. */
1584 /* trim line comment */
1587 while (*p && (*p != '#') && (*p != scc))
1590 while (*p && (*p != '#'))
1594 /* trim trailing whitespace */
1596 while ((q >= s) && ISSPACE(*q))
1600 /* skip leading whitespace */
1602 while (*p && ISSPACE(*p))
1609 if ((len = strlen(opt)) == 0)
1613 if (strncmp(p, opt, len) != 0)
1614 /* line and option do not match */
1617 /* skip over given option name */
1621 /* no option value */
1624 if ((opt[len-1] != ':') && (opt[len-1] != '=') && !ISSPACE(*p))
1625 /* whitespace between option name and value is mandatory
1626 for given option names which do not end with ':' or '=' */
1629 /* skip over whitespace */
1630 while (*p && ISSPACE(*p))
1634 /* no option value */
1637 /* return pointer to option value */
1641 static int sortlist_alloc(struct apattern **sortlist, int *nsort,
1642 struct apattern *pat)
1644 struct apattern *newsort;
1645 newsort = realloc(*sortlist, (*nsort + 1) * sizeof(struct apattern));
1648 newsort[*nsort] = *pat;
1649 *sortlist = newsort;
1654 static int ip_addr(const char *ipbuf, ssize_t len, struct in_addr *addr)
1657 /* Four octets and three periods yields at most 15 characters. */
1661 addr->s_addr = inet_addr(ipbuf);
1662 if (addr->s_addr == INADDR_NONE && strcmp(ipbuf, "255.255.255.255") != 0)
1667 static void natural_mask(struct apattern *pat)
1669 struct in_addr addr;
1671 /* Store a host-byte-order copy of pat in a struct in_addr. Icky,
1674 addr.s_addr = ntohl(pat->addrV4.s_addr);
1676 /* This is out of date in the CIDR world, but some people might
1679 if (IN_CLASSA(addr.s_addr))
1680 pat->mask.addr4.s_addr = htonl(IN_CLASSA_NET);
1681 else if (IN_CLASSB(addr.s_addr))
1682 pat->mask.addr4.s_addr = htonl(IN_CLASSB_NET);
1684 pat->mask.addr4.s_addr = htonl(IN_CLASSC_NET);
1686 #endif /* !WIN32 && !WATT32 */
1688 /* initialize an rc4 key. If possible a cryptographically secure random key
1689 is generated using a suitable function (for example win32's RtlGenRandom as
1691 http://blogs.msdn.com/michael_howard/archive/2005/01/14/353379.aspx
1692 otherwise the code defaults to cross-platform albeit less secure mechanism
1695 static void randomize_key(unsigned char* key,int key_data_len)
1701 if (ares_fpSystemFunction036)
1703 res = (*ares_fpSystemFunction036) (key, key_data_len);
1709 FILE *f = fopen(RANDOM_FILE, "rb");
1711 counter = aresx_uztosi(fread(key, 1, key_data_len, f));
1718 for (;counter<key_data_len;counter++)
1719 key[counter]=(unsigned char)(rand() % 256);
1723 static int init_id_key(rc4_key* key,int key_data_len)
1725 unsigned char index1;
1726 unsigned char index2;
1727 unsigned char* state;
1729 unsigned char *key_data_ptr = 0;
1731 key_data_ptr = calloc(1,key_data_len);
1735 state = &key->state[0];
1736 for(counter = 0; counter < 256; counter++)
1737 /* unnecessary AND but it keeps some compilers happier */
1738 state[counter] = (unsigned char)(counter & 0xff);
1739 randomize_key(key->state,key_data_len);
1744 for(counter = 0; counter < 256; counter++)
1746 index2 = (unsigned char)((key_data_ptr[index1] + state[counter] +
1748 ARES_SWAP_BYTE(&state[counter], &state[index2]);
1750 index1 = (unsigned char)((index1 + 1) % key_data_len);
1753 return ARES_SUCCESS;
1756 unsigned short ares__generate_new_id(rc4_key* key)
1759 ares__rc4(key, (unsigned char *)&r, sizeof(r));
1763 void ares_set_local_ip4(ares_channel channel, unsigned int local_ip)
1765 channel->local_ip4 = local_ip;
1768 /* local_ip6 should be 16 bytes in length */
1769 void ares_set_local_ip6(ares_channel channel,
1770 const unsigned char* local_ip6)
1772 memcpy(&channel->local_ip6, local_ip6, sizeof(channel->local_ip6));
1775 /* local_dev_name should be null terminated. */
1776 void ares_set_local_dev(ares_channel channel,
1777 const char* local_dev_name)
1779 strncpy(channel->local_dev_name, local_dev_name,
1780 sizeof(channel->local_dev_name));
1781 channel->local_dev_name[sizeof(channel->local_dev_name) - 1] = 0;
1785 void ares_set_socket_callback(ares_channel channel,
1786 ares_sock_create_callback cb,
1789 channel->sock_create_cb = cb;
1790 channel->sock_create_cb_data = data;
1793 void ares__init_servers_state(ares_channel channel)
1795 struct server_state *server;
1798 for (i = 0; i < channel->nservers; i++)
1800 server = &channel->servers[i];
1801 server->udp_socket = ARES_SOCKET_BAD;
1802 server->tcp_socket = ARES_SOCKET_BAD;
1803 server->tcp_connection_generation = ++channel->tcp_connection_generation;
1804 server->tcp_lenbuf_pos = 0;
1805 server->tcp_buffer_pos = 0;
1806 server->tcp_buffer = NULL;
1807 server->tcp_length = 0;
1808 server->qhead = NULL;
1809 server->qtail = NULL;
1810 ares__init_list_head(&server->queries_to_server);
1811 server->channel = channel;
1812 server->is_broken = 0;