3 /* Copyright 1998 by the Massachusetts Institute of Technology.
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.
19 #include <sys/types.h>
21 #if defined(WIN32) && !defined(WATT32)
27 #include <sys/param.h>
28 #ifdef HAVE_SYS_TIME_H
32 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
39 #include <arpa/nameser.h>
40 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
41 #include <arpa/nameser_compat.h>
47 #include <process.h> /* Some have getpid() here */
58 #include "ares_private.h"
59 #include "inet_net_pton.h"
62 #undef WIN32 /* Redefined in MingW/MSVC headers */
65 static int init_by_options(ares_channel channel, struct ares_options *options,
67 static int init_by_environment(ares_channel channel);
68 static int init_by_resolv_conf(ares_channel channel);
69 static int init_by_defaults(ares_channel channel);
71 static int config_nameserver(struct server_state **servers, int *nservers,
73 static int set_search(ares_channel channel, const char *str);
74 static int set_options(ares_channel channel, const char *str);
75 static const char *try_option(const char *p, const char *q, const char *opt);
77 static int sortlist_alloc(struct apattern **sortlist, int *nsort, struct apattern *pat);
78 static int ip_addr(const char *s, int len, struct in_addr *addr);
79 static void natural_mask(struct apattern *pat);
80 static int config_domain(ares_channel channel, char *str);
81 static int config_lookup(ares_channel channel, const char *str,
82 const char *bindch, const char *filech);
83 static int config_sortlist(struct apattern **sortlist, int *nsort,
85 static char *try_config(char *s, const char *opt);
88 int ares_init(ares_channel *channelptr)
90 return ares_init_options(channelptr, NULL, 0);
93 int ares_init_options(ares_channel *channelptr, struct ares_options *options,
98 int status = ARES_SUCCESS;
99 struct server_state *server;
103 const char *env = getenv("CARES_MEMDEBUG");
107 env = getenv("CARES_MEMLIMIT");
109 curl_memlimit(atoi(env));
112 channel = malloc(sizeof(struct ares_channeldata));
118 /* Set everything to distinguished values so we know they haven't
122 channel->timeout = -1;
125 channel->udp_port = -1;
126 channel->tcp_port = -1;
127 channel->nservers = -1;
128 channel->ndomains = -1;
130 channel->lookups = NULL;
131 channel->queries = NULL;
132 channel->domains = NULL;
133 channel->sortlist = NULL;
134 channel->servers = NULL;
135 channel->sock_state_cb = NULL;
136 channel->sock_state_cb_data = NULL;
138 /* Initialize configuration by each of the four sources, from highest
139 * precedence to lowest.
142 if (status == ARES_SUCCESS) {
143 status = init_by_options(channel, options, optmask);
144 if (status != ARES_SUCCESS)
145 DEBUGF(fprintf(stderr, "Error: init_by_options failed: %s\n",
146 ares_strerror(status)));
148 if (status == ARES_SUCCESS) {
149 status = init_by_environment(channel);
150 if (status != ARES_SUCCESS)
151 DEBUGF(fprintf(stderr, "Error: init_by_environment failed: %s\n",
152 ares_strerror(status)));
154 if (status == ARES_SUCCESS) {
155 status = init_by_resolv_conf(channel);
156 if (status != ARES_SUCCESS)
157 DEBUGF(fprintf(stderr, "Error: init_by_resolv_conf failed: %s\n",
158 ares_strerror(status)));
160 if (status == ARES_SUCCESS) {
161 status = init_by_defaults(channel);
162 if (status != ARES_SUCCESS)
163 DEBUGF(fprintf(stderr, "Error: init_by_defaults failed: %s\n",
164 ares_strerror(status)));
166 if (status != ARES_SUCCESS)
168 /* Something failed; clean up memory we may have allocated. */
169 if (channel->servers)
170 free(channel->servers);
171 if (channel->domains)
173 for (i = 0; i < channel->ndomains; i++)
174 free(channel->domains[i]);
175 free(channel->domains);
177 if (channel->sortlist)
178 free(channel->sortlist);
180 free(channel->lookups);
185 /* Trim to one server if ARES_FLAG_PRIMARY is set. */
186 if ((channel->flags & ARES_FLAG_PRIMARY) && channel->nservers > 1)
187 channel->nservers = 1;
189 /* Initialize server states. */
190 for (i = 0; i < channel->nservers; i++)
192 server = &channel->servers[i];
193 server->udp_socket = ARES_SOCKET_BAD;
194 server->tcp_socket = ARES_SOCKET_BAD;
195 server->tcp_lenbuf_pos = 0;
196 server->tcp_buffer = NULL;
197 server->qhead = NULL;
198 server->qtail = NULL;
201 /* Choose a somewhat random query ID. The main point is to avoid
202 * collisions with stale queries. An attacker trying to spoof a DNS
203 * answer also has to guess the query ID, but it's only a 16-bit
204 * field, so there's not much to be done about that.
206 gettimeofday(&tv, NULL);
207 channel->next_id = (unsigned short)
208 ((tv.tv_sec ^ tv.tv_usec ^ getpid()) & 0xffff);
210 channel->queries = NULL;
212 *channelptr = channel;
216 static int init_by_options(ares_channel channel, struct ares_options *options,
222 if ((optmask & ARES_OPT_FLAGS) && channel->flags == -1)
223 channel->flags = options->flags;
224 if ((optmask & ARES_OPT_TIMEOUT) && channel->timeout == -1)
225 channel->timeout = options->timeout;
226 if ((optmask & ARES_OPT_TRIES) && channel->tries == -1)
227 channel->tries = options->tries;
228 if ((optmask & ARES_OPT_NDOTS) && channel->ndots == -1)
229 channel->ndots = options->ndots;
230 if ((optmask & ARES_OPT_UDP_PORT) && channel->udp_port == -1)
231 channel->udp_port = options->udp_port;
232 if ((optmask & ARES_OPT_TCP_PORT) && channel->tcp_port == -1)
233 channel->tcp_port = options->tcp_port;
234 if ((optmask & ARES_OPT_SOCK_STATE_CB) && channel->sock_state_cb == NULL)
236 channel->sock_state_cb = options->sock_state_cb;
237 channel->sock_state_cb_data = options->sock_state_cb_data;
240 /* Copy the servers, if given. */
241 if ((optmask & ARES_OPT_SERVERS) && channel->nservers == -1)
243 /* Avoid zero size allocations at any cost */
244 if (options->nservers > 0)
247 malloc(options->nservers * sizeof(struct server_state));
248 if (!channel->servers)
250 for (i = 0; i < options->nservers; i++)
251 channel->servers[i].addr = options->servers[i];
253 channel->nservers = options->nservers;
256 /* Copy the domains, if given. Keep channel->ndomains consistent so
257 * we can clean up in case of error.
259 if ((optmask & ARES_OPT_DOMAINS) && channel->ndomains == -1)
261 /* Avoid zero size allocations at any cost */
262 if (options->ndomains > 0)
264 channel->domains = malloc(options->ndomains * sizeof(char *));
265 if (!channel->domains)
267 for (i = 0; i < options->ndomains; i++)
269 channel->ndomains = i;
270 channel->domains[i] = strdup(options->domains[i]);
271 if (!channel->domains[i])
275 channel->ndomains = options->ndomains;
278 /* Set lookups, if given. */
279 if ((optmask & ARES_OPT_LOOKUPS) && !channel->lookups)
281 channel->lookups = strdup(options->lookups);
282 if (!channel->lookups)
289 static int init_by_environment(ares_channel channel)
291 const char *localdomain, *res_options;
294 localdomain = getenv("LOCALDOMAIN");
295 if (localdomain && channel->ndomains == -1)
297 status = set_search(channel, localdomain);
298 if (status != ARES_SUCCESS)
302 res_options = getenv("RES_OPTIONS");
305 status = set_options(channel, res_options);
306 if (status != ARES_SUCCESS)
315 * Warning: returns a dynamically allocated buffer, the user MUST
316 * use free() if the function returns 1
318 static int get_res_nt(HKEY hKey, const char *subkey, char **obuf)
320 /* Test for the size we need */
324 result = RegQueryValueEx(hKey, subkey, 0, NULL, NULL, &size);
325 if ((result != ERROR_SUCCESS && result != ERROR_MORE_DATA) || !size)
327 *obuf = malloc(size+1);
331 if (RegQueryValueEx(hKey, subkey, 0, NULL,
332 (LPBYTE)*obuf, &size) != ERROR_SUCCESS)
345 static int get_res_interfaces_nt(HKEY hKey, const char *subkey, char **obuf)
347 char enumbuf[39]; /* GUIDs are 38 chars + 1 for NULL */
348 DWORD enum_size = 39;
352 while (RegEnumKeyEx(hKey, idx++, enumbuf, &enum_size, 0,
353 NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS)
358 if (RegOpenKeyEx(hKey, enumbuf, 0, KEY_QUERY_VALUE, &hVal) !=
361 rc = get_res_nt(hVal, subkey, obuf);
369 static int get_iphlpapi_dns_info (char *ret_buf, size_t ret_size)
371 FIXED_INFO *fi = alloca (sizeof(*fi));
372 DWORD size = sizeof (*fi);
373 typedef DWORD (WINAPI* get_net_param_func) (FIXED_INFO*, DWORD*);
374 get_net_param_func GetNetworkParams; /* available only on Win-98/2000+ */
376 IP_ADDR_STRING *ipAddr;
379 size_t ip_size = sizeof("255.255.255.255,")-1;
380 size_t left = ret_size;
387 handle = LoadLibrary ("iphlpapi.dll");
391 GetNetworkParams = (get_net_param_func) GetProcAddress (handle, "GetNetworkParams");
392 if (!GetNetworkParams)
395 res = (*GetNetworkParams) (fi, &size);
396 if ((res != ERROR_BUFFER_OVERFLOW) && (res != ERROR_SUCCESS))
400 if (!fi || (*GetNetworkParams) (fi, &size) != ERROR_SUCCESS)
405 printf ("Host Name: %s\n", fi->HostName);
406 printf ("Domain Name: %s\n", fi->DomainName);
407 printf ("DNS Servers:\n"
408 " %s (primary)\n", fi->DnsServerList.IpAddress.String);
410 if (strlen(fi->DnsServerList.IpAddress.String) > 0 &&
411 inet_addr(fi->DnsServerList.IpAddress.String) != INADDR_NONE &&
414 ret += sprintf (ret, "%s,", fi->DnsServerList.IpAddress.String);
415 left -= ret - ret_buf;
419 for (i = 0, ipAddr = fi->DnsServerList.Next; ipAddr && left > ip_size;
420 ipAddr = ipAddr->Next, i++)
422 if (inet_addr(ipAddr->IpAddress.String) != INADDR_NONE)
424 ret += sprintf (ret, "%s,", ipAddr->IpAddress.String);
425 left -= ret - ret_buf;
429 printf (" %s (secondary %d)\n", ipAddr->IpAddress.String, i+1);
434 FreeLibrary (handle);
436 if (debug && left <= ip_size)
437 printf ("Too many nameservers. Truncating to %d addressess", count);
444 static int init_by_resolv_conf(ares_channel channel)
447 int status = -1, nservers = 0, nsort = 0;
448 struct server_state *servers = NULL;
449 struct apattern *sortlist = NULL;
454 NameServer info via IPHLPAPI (IP helper API):
455 GetNetworkParams() should be the trusted source for this.
456 Available in Win-98/2000 and later. If that fail, fall-back to
457 registry information.
461 On Windows 9X, the DNS server can be found in:
462 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\MSTCP\NameServer
464 On Windows NT/2000/XP/2003:
465 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NameServer
467 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DhcpNameServer
469 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\{AdapterID}\
472 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\{AdapterID}\
483 if (channel->nservers > -1) /* don't override ARES_OPT_SERVER */
486 if (get_iphlpapi_dns_info(buf,sizeof(buf)) > 0)
488 status = config_nameserver(&servers, &nservers, buf);
489 if (status == ARES_SUCCESS)
496 HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
500 RegOpenKeyEx(mykey, "Interfaces", 0,
501 KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS, &subkey);
502 if (get_res_nt(mykey, NAMESERVER, &line))
504 status = config_nameserver(&servers, &nservers, line);
507 else if (get_res_nt(mykey, DHCPNAMESERVER, &line))
509 status = config_nameserver(&servers, &nservers, line);
512 /* Try the interfaces */
513 else if (get_res_interfaces_nt(subkey, NAMESERVER, &line))
515 status = config_nameserver(&servers, &nservers, line);
518 else if (get_res_interfaces_nt(subkey, DHCPNAMESERVER, &line))
520 status = config_nameserver(&servers, &nservers, line);
530 HKEY_LOCAL_MACHINE, WIN_NS_9X, 0,
534 if ((result = RegQueryValueEx(
535 mykey, NAMESERVER, NULL, &data_type,
538 ) == ERROR_SUCCESS ||
539 result == ERROR_MORE_DATA)
543 line = (char *)malloc(bytes+1);
544 if (RegQueryValueEx(mykey, NAMESERVER, NULL, &data_type,
545 (unsigned char *)line, &bytes) ==
548 status = config_nameserver(&servers, &nservers, line);
557 if (status == ARES_SUCCESS)
560 #elif defined(__riscos__)
562 /* Under RISC OS, name servers are listed in the
563 system variable Inet$Resolvers, space separated. */
565 line = getenv("Inet$Resolvers");
568 char *resolvers = strdup(line), *pos, *space;
575 space = strchr(pos, ' ');
578 status = config_nameserver(&servers, &nservers, pos);
579 if (status != ARES_SUCCESS)
584 if (status == ARES_SUCCESS)
590 #elif defined(WATT32)
594 for (i = 0; def_nameservers[i]; i++)
597 return ARES_SUCCESS; /* use localhost DNS server */
600 servers = calloc(sizeof(*servers), i);
604 for (i = 0; def_nameservers[i]; i++)
605 servers[i].addr.s_addr = htonl(def_nameservers[i]);
614 fp = fopen(PATH_RESOLV_CONF, "r");
616 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
618 if ((p = try_config(line, "domain")) && channel->ndomains == -1)
619 status = config_domain(channel, p);
620 else if ((p = try_config(line, "lookup")) && !channel->lookups)
621 status = config_lookup(channel, p, "bind", "file");
622 else if ((p = try_config(line, "search")) && channel->ndomains == -1)
623 status = set_search(channel, p);
624 else if ((p = try_config(line, "nameserver")) && channel->nservers == -1)
625 status = config_nameserver(&servers, &nservers, p);
626 else if ((p = try_config(line, "sortlist")) && channel->nsort == -1)
627 status = config_sortlist(&sortlist, &nsort, p);
628 else if ((p = try_config(line, "options")))
629 status = set_options(channel, p);
631 status = ARES_SUCCESS;
632 if (status != ARES_SUCCESS)
643 DEBUGF(fprintf(stderr, "Error opening file: %s\n", PATH_RESOLV_CONF));
644 DEBUGF(fprintf(stderr, "fopen() failed with error: %d\n", errno));
649 if ((status == ARES_EOF) && (!channel->lookups)) {
650 /* Many systems (Solaris, Linux, BSD's) use nsswitch.conf */
651 fp = fopen("/etc/nsswitch.conf", "r");
653 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
655 if ((p = try_config(line, "hosts:")) && !channel->lookups)
656 status = config_lookup(channel, p, "dns", "files");
666 DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/nsswitch.conf"));
667 DEBUGF(fprintf(stderr, "fopen() failed with error: %d\n", errno));
673 if ((status == ARES_EOF) && (!channel->lookups)) {
674 /* Linux / GNU libc 2.x and possibly others have host.conf */
675 fp = fopen("/etc/host.conf", "r");
677 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
679 if ((p = try_config(line, "order")) && !channel->lookups)
680 status = config_lookup(channel, p, "bind", "hosts");
690 DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/host.conf"));
691 DEBUGF(fprintf(stderr, "fopen() failed with error: %d\n", errno));
697 if ((status == ARES_EOF) && (!channel->lookups)) {
698 /* Tru64 uses /etc/svc.conf */
699 fp = fopen("/etc/svc.conf", "r");
701 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
703 if ((p = try_config(line, "hosts=")) && !channel->lookups)
704 status = config_lookup(channel, p, "bind", "local");
714 DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/svc.conf"));
715 DEBUGF(fprintf(stderr, "fopen() failed with error: %d\n", errno));
728 if (status != ARES_EOF)
732 if (sortlist != NULL)
737 /* If we got any name server entries, fill them in. */
743 channel->servers = servers;
744 channel->nservers = nservers;
747 /* If we got any sortlist entries, fill them in. */
750 channel->sortlist = sortlist;
751 channel->nsort = nsort;
757 static int init_by_defaults(ares_channel channel)
759 char hostname[MAXHOSTNAMELEN + 1];
761 if (channel->flags == -1)
763 if (channel->timeout == -1)
764 channel->timeout = DEFAULT_TIMEOUT;
765 if (channel->tries == -1)
766 channel->tries = DEFAULT_TRIES;
767 if (channel->ndots == -1)
769 if (channel->udp_port == -1)
770 channel->udp_port = htons(NAMESERVER_PORT);
771 if (channel->tcp_port == -1)
772 channel->tcp_port = htons(NAMESERVER_PORT);
774 if (channel->nservers == -1)
776 /* If nobody specified servers, try a local named. */
777 channel->servers = malloc(sizeof(struct server_state));
778 if (!channel->servers)
780 channel->servers[0].addr.s_addr = htonl(INADDR_LOOPBACK);
781 channel->nservers = 1;
784 if (channel->ndomains == -1)
786 /* Derive a default domain search list from the kernel hostname,
787 * or set it to empty if the hostname isn't helpful.
789 if (gethostname(hostname, sizeof(hostname)) == -1
790 || !strchr(hostname, '.'))
792 channel->ndomains = 0;
796 channel->domains = malloc(sizeof(char *));
797 if (!channel->domains)
799 channel->ndomains = 0;
800 channel->domains[0] = strdup(strchr(hostname, '.') + 1);
801 if (!channel->domains[0])
803 channel->ndomains = 1;
807 if (channel->nsort == -1)
809 channel->sortlist = NULL;
813 if (!channel->lookups)
815 channel->lookups = strdup("fb");
816 if (!channel->lookups)
824 static int config_domain(ares_channel channel, char *str)
828 /* Set a single search domain. */
830 while (*q && !ISSPACE(*q))
833 return set_search(channel, str);
836 static int config_lookup(ares_channel channel, const char *str,
837 const char *bindch, const char *filech)
842 /* Set the lookup order. Only the first letter of each work
843 * is relevant, and it has to be "b" for DNS or "f" for the
844 * host file. Ignore everything else.
850 if ((*p == *bindch || *p == *filech) && l < lookups + 2) {
851 if (*p == *bindch) *l++ = 'b';
854 while (*p && !ISSPACE(*p) && (*p != ','))
856 while (*p && (ISSPACE(*p) || (*p == ',')))
860 channel->lookups = strdup(lookups);
861 return (channel->lookups) ? ARES_SUCCESS : ARES_ENOMEM;
866 static int config_nameserver(struct server_state **servers, int *nservers,
870 struct server_state *newserv;
871 /* On Windows, there may be more than one nameserver specified in the same
872 * registry key, so we parse it as a space or comma seperated list.
881 while (*p && !ISSPACE(*p) && *p != ',')
890 /* Skip multiple spaces or trailing spaces */
897 /* This is the part that actually sets the nameserver */
898 addr.s_addr = inet_addr(begin);
899 if (addr.s_addr == INADDR_NONE)
901 newserv = realloc(*servers, (*nservers + 1) * sizeof(struct server_state));
904 newserv[*nservers].addr = addr;
913 /* Add a nameserver entry, if this is a valid address. */
914 addr.s_addr = inet_addr(str);
915 if (addr.s_addr == INADDR_NONE)
917 newserv = realloc(*servers, (*nservers + 1) * sizeof(struct server_state));
920 newserv[*nservers].addr = addr;
928 static int config_sortlist(struct apattern **sortlist, int *nsort,
934 /* Add sortlist entries. */
935 while (*str && *str != ';')
938 char ipbuf[16], ipbufpfx[32];
939 /* Find just the IP */
941 while (*q && *q != '/' && *q != ';' && !ISSPACE(*q))
943 memcpy(ipbuf, str, (int)(q-str));
944 ipbuf[(int)(q-str)] = '\0';
945 /* Find the prefix */
948 const char *str2 = q+1;
949 while (*q && *q != ';' && !ISSPACE(*q))
951 memcpy(ipbufpfx, str, (int)(q-str));
952 ipbufpfx[(int)(q-str)] = '\0';
957 /* Lets see if it is CIDR */
958 /* First we'll try IPv6 */
959 if ((bits = ares_inet_net_pton(AF_INET6, ipbufpfx ? ipbufpfx : ipbuf,
961 sizeof(pat.addr.addr6))) > 0)
963 pat.type = PATTERN_CIDR;
964 pat.mask.bits = (unsigned short)bits;
965 pat.family = AF_INET6;
966 if (!sortlist_alloc(sortlist, nsort, &pat))
970 (bits = ares_inet_net_pton(AF_INET, ipbufpfx, &pat.addr.addr4,
971 sizeof(pat.addr.addr4))) > 0)
973 pat.type = PATTERN_CIDR;
974 pat.mask.bits = (unsigned short)bits;
975 pat.family = AF_INET;
976 if (!sortlist_alloc(sortlist, nsort, &pat))
979 /* See if it is just a regular IP */
980 else if (ip_addr(ipbuf, (int)(q-str), &pat.addr.addr4) == 0)
984 memcpy(ipbuf, str, (int)(q-str));
985 ipbuf[(int)(q-str)] = '\0';
986 if (ip_addr(ipbuf, (int)(q - str), &pat.mask.addr.addr4) != 0)
991 pat.family = AF_INET;
992 pat.type = PATTERN_MASK;
993 if (!sortlist_alloc(sortlist, nsort, &pat))
998 while (*q && *q != ';' && !ISSPACE(*q))
1002 while (ISSPACE(*str))
1006 return ARES_SUCCESS;
1010 static int set_search(ares_channel channel, const char *str)
1015 if(channel->ndomains != -1) {
1016 /* if we already have some domains present, free them first */
1017 for(n=0; n < channel->ndomains; n++)
1018 free(channel->domains[n]);
1019 free(channel->domains);
1020 channel->domains = NULL;
1021 channel->ndomains = -1;
1024 /* Count the domains given. */
1029 while (*p && !ISSPACE(*p))
1038 channel->ndomains = 0;
1039 return ARES_SUCCESS;
1042 channel->domains = malloc(n * sizeof(char *));
1043 if (!channel->domains)
1046 /* Now copy the domains. */
1051 channel->ndomains = n;
1053 while (*q && !ISSPACE(*q))
1055 channel->domains[n] = malloc(q - p + 1);
1056 if (!channel->domains[n])
1058 memcpy(channel->domains[n], p, q - p);
1059 channel->domains[n][q - p] = 0;
1065 channel->ndomains = n;
1067 return ARES_SUCCESS;
1070 static int set_options(ares_channel channel, const char *str)
1072 const char *p, *q, *val;
1078 while (*q && !ISSPACE(*q))
1080 val = try_option(p, q, "ndots:");
1081 if (val && channel->ndots == -1)
1082 channel->ndots = atoi(val);
1083 val = try_option(p, q, "retrans:");
1084 if (val && channel->timeout == -1)
1085 channel->timeout = atoi(val);
1086 val = try_option(p, q, "retry:");
1087 if (val && channel->tries == -1)
1088 channel->tries = atoi(val);
1094 return ARES_SUCCESS;
1098 static char *try_config(char *s, const char *opt)
1103 if (strncmp(s, opt, len) != 0 || !ISSPACE(s[len]))
1113 static const char *try_option(const char *p, const char *q, const char *opt)
1115 size_t len = strlen(opt);
1116 return ((size_t)(q - p) > len && !strncmp(p, opt, len)) ? &p[len] : NULL;
1120 static int sortlist_alloc(struct apattern **sortlist, int *nsort,
1121 struct apattern *pat)
1123 struct apattern *newsort;
1124 newsort = realloc(*sortlist, (*nsort + 1) * sizeof(struct apattern));
1127 newsort[*nsort] = *pat;
1128 *sortlist = newsort;
1133 static int ip_addr(const char *ipbuf, int len, struct in_addr *addr)
1136 /* Four octets and three periods yields at most 15 characters. */
1140 addr->s_addr = inet_addr(ipbuf);
1141 if (addr->s_addr == INADDR_NONE && strcmp(ipbuf, "255.255.255.255") != 0)
1146 static void natural_mask(struct apattern *pat)
1148 struct in_addr addr;
1150 /* Store a host-byte-order copy of pat in a struct in_addr. Icky,
1153 addr.s_addr = ntohl(pat->addr.addr4.s_addr);
1155 /* This is out of date in the CIDR world, but some people might
1158 if (IN_CLASSA(addr.s_addr))
1159 pat->mask.addr.addr4.s_addr = htonl(IN_CLASSA_NET);
1160 else if (IN_CLASSB(addr.s_addr))
1161 pat->mask.addr.addr4.s_addr = htonl(IN_CLASSB_NET);
1163 pat->mask.addr.addr4.s_addr = htonl(IN_CLASSC_NET);