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.
20 #if defined(WIN32) && !defined(WATT32)
26 #include <sys/param.h>
27 #ifdef HAVE_SYS_TIME_H
31 #ifdef HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
38 #include <arpa/nameser.h>
39 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
40 #include <arpa/nameser_compat.h>
46 #include <process.h> /* Some have getpid() here */
57 #include "ares_private.h"
58 #include "inet_net_pton.h"
61 #undef WIN32 /* Redefined in MingW/MSVC headers */
64 static int init_by_options(ares_channel channel, struct ares_options *options,
66 static int init_by_environment(ares_channel channel);
67 static int init_by_resolv_conf(ares_channel channel);
68 static int init_by_defaults(ares_channel channel);
70 static int config_nameserver(struct server_state **servers, int *nservers,
72 static int set_search(ares_channel channel, const char *str);
73 static int set_options(ares_channel channel, const char *str);
74 static const char *try_option(const char *p, const char *q, const char *opt);
76 static int sortlist_alloc(struct apattern **sortlist, int *nsort, struct apattern *pat);
77 static int ip_addr(const char *s, int len, struct in_addr *addr);
78 static void natural_mask(struct apattern *pat);
79 static int config_domain(ares_channel channel, char *str);
80 static int config_lookup(ares_channel channel, const char *str,
81 const char *bindch, const char *filech);
82 static int config_sortlist(struct apattern **sortlist, int *nsort,
84 static char *try_config(char *s, const char *opt);
87 int ares_init(ares_channel *channelptr)
89 return ares_init_options(channelptr, NULL, 0);
92 int ares_init_options(ares_channel *channelptr, struct ares_options *options,
97 int status = ARES_SUCCESS;
98 struct server_state *server;
102 const char *env = getenv("CARES_MEMDEBUG");
106 env = getenv("CARES_MEMLIMIT");
108 curl_memlimit(atoi(env));
111 channel = malloc(sizeof(struct ares_channeldata));
117 /* Set everything to distinguished values so we know they haven't
121 channel->timeout = -1;
124 channel->udp_port = -1;
125 channel->tcp_port = -1;
126 channel->nservers = -1;
127 channel->ndomains = -1;
129 channel->lookups = NULL;
130 channel->queries = NULL;
131 channel->domains = NULL;
132 channel->sortlist = NULL;
133 channel->servers = NULL;
134 channel->sock_state_cb = NULL;
135 channel->sock_state_cb_data = NULL;
137 /* Initialize configuration by each of the four sources, from highest
138 * precedence to lowest.
141 if (status == ARES_SUCCESS) {
142 status = init_by_options(channel, options, optmask);
143 if (status != ARES_SUCCESS)
144 DEBUGF(fprintf(stderr, "Error: init_by_options failed: %s\n",
145 ares_strerror(status)));
147 if (status == ARES_SUCCESS) {
148 status = init_by_environment(channel);
149 if (status != ARES_SUCCESS)
150 DEBUGF(fprintf(stderr, "Error: init_by_environment failed: %s\n",
151 ares_strerror(status)));
153 if (status == ARES_SUCCESS) {
154 status = init_by_resolv_conf(channel);
155 if (status != ARES_SUCCESS)
156 DEBUGF(fprintf(stderr, "Error: init_by_resolv_conf failed: %s\n",
157 ares_strerror(status)));
159 if (status == ARES_SUCCESS) {
160 status = init_by_defaults(channel);
161 if (status != ARES_SUCCESS)
162 DEBUGF(fprintf(stderr, "Error: init_by_defaults failed: %s\n",
163 ares_strerror(status)));
165 if (status != ARES_SUCCESS)
167 /* Something failed; clean up memory we may have allocated. */
168 if (channel->servers)
169 free(channel->servers);
170 if (channel->domains)
172 for (i = 0; i < channel->ndomains; i++)
173 free(channel->domains[i]);
174 free(channel->domains);
176 if (channel->sortlist)
177 free(channel->sortlist);
179 free(channel->lookups);
184 /* Trim to one server if ARES_FLAG_PRIMARY is set. */
185 if ((channel->flags & ARES_FLAG_PRIMARY) && channel->nservers > 1)
186 channel->nservers = 1;
188 /* Initialize server states. */
189 for (i = 0; i < channel->nservers; i++)
191 server = &channel->servers[i];
192 server->udp_socket = ARES_SOCKET_BAD;
193 server->tcp_socket = ARES_SOCKET_BAD;
194 server->tcp_lenbuf_pos = 0;
195 server->tcp_buffer = NULL;
196 server->qhead = NULL;
197 server->qtail = NULL;
200 /* Choose a somewhat random query ID. The main point is to avoid
201 * collisions with stale queries. An attacker trying to spoof a DNS
202 * answer also has to guess the query ID, but it's only a 16-bit
203 * field, so there's not much to be done about that.
205 gettimeofday(&tv, NULL);
206 channel->next_id = (unsigned short)
207 ((tv.tv_sec ^ tv.tv_usec ^ getpid()) & 0xffff);
209 channel->queries = NULL;
211 *channelptr = channel;
215 static int init_by_options(ares_channel channel, struct ares_options *options,
221 if ((optmask & ARES_OPT_FLAGS) && channel->flags == -1)
222 channel->flags = options->flags;
223 if ((optmask & ARES_OPT_TIMEOUT) && channel->timeout == -1)
224 channel->timeout = options->timeout;
225 if ((optmask & ARES_OPT_TRIES) && channel->tries == -1)
226 channel->tries = options->tries;
227 if ((optmask & ARES_OPT_NDOTS) && channel->ndots == -1)
228 channel->ndots = options->ndots;
229 if ((optmask & ARES_OPT_UDP_PORT) && channel->udp_port == -1)
230 channel->udp_port = options->udp_port;
231 if ((optmask & ARES_OPT_TCP_PORT) && channel->tcp_port == -1)
232 channel->tcp_port = options->tcp_port;
233 if ((optmask & ARES_OPT_SOCK_STATE_CB) && channel->sock_state_cb == NULL)
235 channel->sock_state_cb = options->sock_state_cb;
236 channel->sock_state_cb_data = options->sock_state_cb_data;
239 /* Copy the servers, if given. */
240 if ((optmask & ARES_OPT_SERVERS) && channel->nservers == -1)
242 /* Avoid zero size allocations at any cost */
243 if (options->nservers > 0)
246 malloc(options->nservers * sizeof(struct server_state));
247 if (!channel->servers)
249 for (i = 0; i < options->nservers; i++)
250 channel->servers[i].addr = options->servers[i];
252 channel->nservers = options->nservers;
255 /* Copy the domains, if given. Keep channel->ndomains consistent so
256 * we can clean up in case of error.
258 if ((optmask & ARES_OPT_DOMAINS) && channel->ndomains == -1)
260 /* Avoid zero size allocations at any cost */
261 if (options->ndomains > 0)
263 channel->domains = malloc(options->ndomains * sizeof(char *));
264 if (!channel->domains)
266 for (i = 0; i < options->ndomains; i++)
268 channel->ndomains = i;
269 channel->domains[i] = strdup(options->domains[i]);
270 if (!channel->domains[i])
274 channel->ndomains = options->ndomains;
277 /* Set lookups, if given. */
278 if ((optmask & ARES_OPT_LOOKUPS) && !channel->lookups)
280 channel->lookups = strdup(options->lookups);
281 if (!channel->lookups)
288 static int init_by_environment(ares_channel channel)
290 const char *localdomain, *res_options;
293 localdomain = getenv("LOCALDOMAIN");
294 if (localdomain && channel->ndomains == -1)
296 status = set_search(channel, localdomain);
297 if (status != ARES_SUCCESS)
301 res_options = getenv("RES_OPTIONS");
304 status = set_options(channel, res_options);
305 if (status != ARES_SUCCESS)
314 * Warning: returns a dynamically allocated buffer, the user MUST
315 * use free() if the function returns 1
317 static int get_res_nt(HKEY hKey, const char *subkey, char **obuf)
319 /* Test for the size we need */
323 result = RegQueryValueEx(hKey, subkey, 0, NULL, NULL, &size);
324 if ((result != ERROR_SUCCESS && result != ERROR_MORE_DATA) || !size)
326 *obuf = malloc(size+1);
330 if (RegQueryValueEx(hKey, subkey, 0, NULL,
331 (LPBYTE)*obuf, &size) != ERROR_SUCCESS)
344 static int get_res_interfaces_nt(HKEY hKey, const char *subkey, char **obuf)
346 char enumbuf[39]; /* GUIDs are 38 chars + 1 for NULL */
347 DWORD enum_size = 39;
351 while (RegEnumKeyEx(hKey, idx++, enumbuf, &enum_size, 0,
352 NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS)
357 if (RegOpenKeyEx(hKey, enumbuf, 0, KEY_QUERY_VALUE, &hVal) !=
360 rc = get_res_nt(hVal, subkey, obuf);
368 static int get_iphlpapi_dns_info (char *ret_buf, size_t ret_size)
370 FIXED_INFO *fi = alloca (sizeof(*fi));
371 DWORD size = sizeof (*fi);
372 typedef DWORD (WINAPI* get_net_param_func) (FIXED_INFO*, DWORD*);
373 get_net_param_func GetNetworkParams; /* available only on Win-98/2000+ */
375 IP_ADDR_STRING *ipAddr;
378 size_t ip_size = sizeof("255.255.255.255,")-1;
379 size_t left = ret_size;
386 handle = LoadLibrary ("iphlpapi.dll");
390 GetNetworkParams = (get_net_param_func) GetProcAddress (handle, "GetNetworkParams");
391 if (!GetNetworkParams)
394 res = (*GetNetworkParams) (fi, &size);
395 if ((res != ERROR_BUFFER_OVERFLOW) && (res != ERROR_SUCCESS))
399 if (!fi || (*GetNetworkParams) (fi, &size) != ERROR_SUCCESS)
404 printf ("Host Name: %s\n", fi->HostName);
405 printf ("Domain Name: %s\n", fi->DomainName);
406 printf ("DNS Servers:\n"
407 " %s (primary)\n", fi->DnsServerList.IpAddress.String);
409 if (strlen(fi->DnsServerList.IpAddress.String) > 0 &&
410 inet_addr(fi->DnsServerList.IpAddress.String) != INADDR_NONE &&
413 ret += sprintf (ret, "%s,", fi->DnsServerList.IpAddress.String);
414 left -= ret - ret_buf;
418 for (i = 0, ipAddr = fi->DnsServerList.Next; ipAddr && left > ip_size;
419 ipAddr = ipAddr->Next, i++)
421 if (inet_addr(ipAddr->IpAddress.String) != INADDR_NONE)
423 ret += sprintf (ret, "%s,", ipAddr->IpAddress.String);
424 left -= ret - ret_buf;
428 printf (" %s (secondary %d)\n", ipAddr->IpAddress.String, i+1);
433 FreeLibrary (handle);
435 if (debug && left <= ip_size)
436 printf ("Too many nameservers. Truncating to %d addressess", count);
443 static int init_by_resolv_conf(ares_channel channel)
446 int status = -1, nservers = 0, nsort = 0;
447 struct server_state *servers = NULL;
448 struct apattern *sortlist = NULL;
453 NameServer info via IPHLPAPI (IP helper API):
454 GetNetworkParams() should be the trusted source for this.
455 Available in Win-98/2000 and later. If that fail, fall-back to
456 registry information.
460 On Windows 9X, the DNS server can be found in:
461 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\MSTCP\NameServer
463 On Windows NT/2000/XP/2003:
464 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NameServer
466 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DhcpNameServer
468 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\{AdapterID}\
471 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\{AdapterID}\
482 if (channel->nservers > -1) /* don't override ARES_OPT_SERVER */
485 if (get_iphlpapi_dns_info(buf,sizeof(buf)) > 0)
487 status = config_nameserver(&servers, &nservers, buf);
488 if (status == ARES_SUCCESS)
495 HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
499 RegOpenKeyEx(mykey, "Interfaces", 0,
500 KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS, &subkey);
501 if (get_res_nt(mykey, NAMESERVER, &line))
503 status = config_nameserver(&servers, &nservers, line);
506 else if (get_res_nt(mykey, DHCPNAMESERVER, &line))
508 status = config_nameserver(&servers, &nservers, line);
511 /* Try the interfaces */
512 else if (get_res_interfaces_nt(subkey, NAMESERVER, &line))
514 status = config_nameserver(&servers, &nservers, line);
517 else if (get_res_interfaces_nt(subkey, DHCPNAMESERVER, &line))
519 status = config_nameserver(&servers, &nservers, line);
529 HKEY_LOCAL_MACHINE, WIN_NS_9X, 0,
533 if ((result = RegQueryValueEx(
534 mykey, NAMESERVER, NULL, &data_type,
537 ) == ERROR_SUCCESS ||
538 result == ERROR_MORE_DATA)
542 line = (char *)malloc(bytes+1);
543 if (RegQueryValueEx(mykey, NAMESERVER, NULL, &data_type,
544 (unsigned char *)line, &bytes) ==
547 status = config_nameserver(&servers, &nservers, line);
556 if (status == ARES_SUCCESS)
559 /* Catch the case when all the above checks fail (which happens when there
560 is no network card or the cable is unplugged) */
563 #elif defined(__riscos__)
565 /* Under RISC OS, name servers are listed in the
566 system variable Inet$Resolvers, space separated. */
568 line = getenv("Inet$Resolvers");
571 char *resolvers = strdup(line), *pos, *space;
578 space = strchr(pos, ' ');
581 status = config_nameserver(&servers, &nservers, pos);
582 if (status != ARES_SUCCESS)
587 if (status == ARES_SUCCESS)
593 #elif defined(WATT32)
597 for (i = 0; def_nameservers[i]; i++)
600 return ARES_SUCCESS; /* use localhost DNS server */
603 servers = calloc(sizeof(*servers), i);
607 for (i = 0; def_nameservers[i]; i++)
608 servers[i].addr.s_addr = htonl(def_nameservers[i]);
618 fp = fopen(PATH_RESOLV_CONF, "r");
620 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
622 if ((p = try_config(line, "domain")) && channel->ndomains == -1)
623 status = config_domain(channel, p);
624 else if ((p = try_config(line, "lookup")) && !channel->lookups)
625 status = config_lookup(channel, p, "bind", "file");
626 else if ((p = try_config(line, "search")) && channel->ndomains == -1)
627 status = set_search(channel, p);
628 else if ((p = try_config(line, "nameserver")) && channel->nservers == -1)
629 status = config_nameserver(&servers, &nservers, p);
630 else if ((p = try_config(line, "sortlist")) && channel->nsort == -1)
631 status = config_sortlist(&sortlist, &nsort, p);
632 else if ((p = try_config(line, "options")))
633 status = set_options(channel, p);
635 status = ARES_SUCCESS;
636 if (status != ARES_SUCCESS)
649 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
650 error, strerror(error)));
651 DEBUGF(fprintf(stderr, "Error opening file: %s\n", PATH_RESOLV_CONF));
656 if ((status == ARES_EOF) && (!channel->lookups)) {
657 /* Many systems (Solaris, Linux, BSD's) use nsswitch.conf */
658 fp = fopen("/etc/nsswitch.conf", "r");
660 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
662 if ((p = try_config(line, "hosts:")) && !channel->lookups)
663 status = config_lookup(channel, p, "dns", "files");
675 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
676 error, strerror(error)));
677 DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/nsswitch.conf"));
683 if ((status == ARES_EOF) && (!channel->lookups)) {
684 /* Linux / GNU libc 2.x and possibly others have host.conf */
685 fp = fopen("/etc/host.conf", "r");
687 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
689 if ((p = try_config(line, "order")) && !channel->lookups)
690 status = config_lookup(channel, p, "bind", "hosts");
702 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
703 error, strerror(error)));
704 DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/host.conf"));
710 if ((status == ARES_EOF) && (!channel->lookups)) {
711 /* Tru64 uses /etc/svc.conf */
712 fp = fopen("/etc/svc.conf", "r");
714 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
716 if ((p = try_config(line, "hosts=")) && !channel->lookups)
717 status = config_lookup(channel, p, "bind", "local");
729 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
730 error, strerror(error)));
731 DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/svc.conf"));
744 if (status != ARES_EOF)
748 if (sortlist != NULL)
753 /* If we got any name server entries, fill them in. */
759 channel->servers = servers;
760 channel->nservers = nservers;
763 /* If we got any sortlist entries, fill them in. */
766 channel->sortlist = sortlist;
767 channel->nsort = nsort;
773 static int init_by_defaults(ares_channel channel)
775 char hostname[MAXHOSTNAMELEN + 1];
777 if (channel->flags == -1)
779 if (channel->timeout == -1)
780 channel->timeout = DEFAULT_TIMEOUT;
781 if (channel->tries == -1)
782 channel->tries = DEFAULT_TRIES;
783 if (channel->ndots == -1)
785 if (channel->udp_port == -1)
786 channel->udp_port = htons(NAMESERVER_PORT);
787 if (channel->tcp_port == -1)
788 channel->tcp_port = htons(NAMESERVER_PORT);
790 if (channel->nservers == -1)
792 /* If nobody specified servers, try a local named. */
793 channel->servers = malloc(sizeof(struct server_state));
794 if (!channel->servers)
796 channel->servers[0].addr.s_addr = htonl(INADDR_LOOPBACK);
797 channel->nservers = 1;
800 if (channel->ndomains == -1)
802 /* Derive a default domain search list from the kernel hostname,
803 * or set it to empty if the hostname isn't helpful.
805 if (gethostname(hostname, sizeof(hostname)) == -1
806 || !strchr(hostname, '.'))
808 channel->ndomains = 0;
812 channel->domains = malloc(sizeof(char *));
813 if (!channel->domains)
815 channel->ndomains = 0;
816 channel->domains[0] = strdup(strchr(hostname, '.') + 1);
817 if (!channel->domains[0])
819 channel->ndomains = 1;
823 if (channel->nsort == -1)
825 channel->sortlist = NULL;
829 if (!channel->lookups)
831 channel->lookups = strdup("fb");
832 if (!channel->lookups)
840 static int config_domain(ares_channel channel, char *str)
844 /* Set a single search domain. */
846 while (*q && !ISSPACE(*q))
849 return set_search(channel, str);
852 static int config_lookup(ares_channel channel, const char *str,
853 const char *bindch, const char *filech)
858 /* Set the lookup order. Only the first letter of each work
859 * is relevant, and it has to be "b" for DNS or "f" for the
860 * host file. Ignore everything else.
866 if ((*p == *bindch || *p == *filech) && l < lookups + 2) {
867 if (*p == *bindch) *l++ = 'b';
870 while (*p && !ISSPACE(*p) && (*p != ','))
872 while (*p && (ISSPACE(*p) || (*p == ',')))
876 channel->lookups = strdup(lookups);
877 return (channel->lookups) ? ARES_SUCCESS : ARES_ENOMEM;
882 static int config_nameserver(struct server_state **servers, int *nservers,
886 struct server_state *newserv;
887 /* On Windows, there may be more than one nameserver specified in the same
888 * registry key, so we parse it as a space or comma seperated list.
897 while (*p && !ISSPACE(*p) && *p != ',')
906 /* Skip multiple spaces or trailing spaces */
913 /* This is the part that actually sets the nameserver */
914 addr.s_addr = inet_addr(begin);
915 if (addr.s_addr == INADDR_NONE)
917 newserv = realloc(*servers, (*nservers + 1) * sizeof(struct server_state));
920 newserv[*nservers].addr = addr;
929 /* Add a nameserver entry, if this is a valid address. */
930 addr.s_addr = inet_addr(str);
931 if (addr.s_addr == INADDR_NONE)
933 newserv = realloc(*servers, (*nservers + 1) * sizeof(struct server_state));
936 newserv[*nservers].addr = addr;
944 static int config_sortlist(struct apattern **sortlist, int *nsort,
950 /* Add sortlist entries. */
951 while (*str && *str != ';')
954 char ipbuf[16], ipbufpfx[32];
955 /* Find just the IP */
957 while (*q && *q != '/' && *q != ';' && !ISSPACE(*q))
959 memcpy(ipbuf, str, (int)(q-str));
960 ipbuf[(int)(q-str)] = '\0';
961 /* Find the prefix */
964 const char *str2 = q+1;
965 while (*q && *q != ';' && !ISSPACE(*q))
967 memcpy(ipbufpfx, str, (int)(q-str));
968 ipbufpfx[(int)(q-str)] = '\0';
973 /* Lets see if it is CIDR */
974 /* First we'll try IPv6 */
975 if ((bits = ares_inet_net_pton(AF_INET6, ipbufpfx ? ipbufpfx : ipbuf,
977 sizeof(pat.addr.addr6))) > 0)
979 pat.type = PATTERN_CIDR;
980 pat.mask.bits = (unsigned short)bits;
981 pat.family = AF_INET6;
982 if (!sortlist_alloc(sortlist, nsort, &pat))
986 (bits = ares_inet_net_pton(AF_INET, ipbufpfx, &pat.addr.addr4,
987 sizeof(pat.addr.addr4))) > 0)
989 pat.type = PATTERN_CIDR;
990 pat.mask.bits = (unsigned short)bits;
991 pat.family = AF_INET;
992 if (!sortlist_alloc(sortlist, nsort, &pat))
995 /* See if it is just a regular IP */
996 else if (ip_addr(ipbuf, (int)(q-str), &pat.addr.addr4) == 0)
1000 memcpy(ipbuf, str, (int)(q-str));
1001 ipbuf[(int)(q-str)] = '\0';
1002 if (ip_addr(ipbuf, (int)(q - str), &pat.mask.addr.addr4) != 0)
1007 pat.family = AF_INET;
1008 pat.type = PATTERN_MASK;
1009 if (!sortlist_alloc(sortlist, nsort, &pat))
1014 while (*q && *q != ';' && !ISSPACE(*q))
1018 while (ISSPACE(*str))
1022 return ARES_SUCCESS;
1026 static int set_search(ares_channel channel, const char *str)
1031 if(channel->ndomains != -1) {
1032 /* if we already have some domains present, free them first */
1033 for(n=0; n < channel->ndomains; n++)
1034 free(channel->domains[n]);
1035 free(channel->domains);
1036 channel->domains = NULL;
1037 channel->ndomains = -1;
1040 /* Count the domains given. */
1045 while (*p && !ISSPACE(*p))
1054 channel->ndomains = 0;
1055 return ARES_SUCCESS;
1058 channel->domains = malloc(n * sizeof(char *));
1059 if (!channel->domains)
1062 /* Now copy the domains. */
1067 channel->ndomains = n;
1069 while (*q && !ISSPACE(*q))
1071 channel->domains[n] = malloc(q - p + 1);
1072 if (!channel->domains[n])
1074 memcpy(channel->domains[n], p, q - p);
1075 channel->domains[n][q - p] = 0;
1081 channel->ndomains = n;
1083 return ARES_SUCCESS;
1086 static int set_options(ares_channel channel, const char *str)
1088 const char *p, *q, *val;
1094 while (*q && !ISSPACE(*q))
1096 val = try_option(p, q, "ndots:");
1097 if (val && channel->ndots == -1)
1098 channel->ndots = atoi(val);
1099 val = try_option(p, q, "retrans:");
1100 if (val && channel->timeout == -1)
1101 channel->timeout = atoi(val);
1102 val = try_option(p, q, "retry:");
1103 if (val && channel->tries == -1)
1104 channel->tries = atoi(val);
1110 return ARES_SUCCESS;
1114 static char *try_config(char *s, const char *opt)
1119 if (strncmp(s, opt, len) != 0 || !ISSPACE(s[len]))
1129 static const char *try_option(const char *p, const char *q, const char *opt)
1131 size_t len = strlen(opt);
1132 return ((size_t)(q - p) > len && !strncmp(p, opt, len)) ? &p[len] : NULL;
1136 static int sortlist_alloc(struct apattern **sortlist, int *nsort,
1137 struct apattern *pat)
1139 struct apattern *newsort;
1140 newsort = realloc(*sortlist, (*nsort + 1) * sizeof(struct apattern));
1143 newsort[*nsort] = *pat;
1144 *sortlist = newsort;
1149 static int ip_addr(const char *ipbuf, int len, struct in_addr *addr)
1152 /* Four octets and three periods yields at most 15 characters. */
1156 addr->s_addr = inet_addr(ipbuf);
1157 if (addr->s_addr == INADDR_NONE && strcmp(ipbuf, "255.255.255.255") != 0)
1162 static void natural_mask(struct apattern *pat)
1164 struct in_addr addr;
1166 /* Store a host-byte-order copy of pat in a struct in_addr. Icky,
1169 addr.s_addr = ntohl(pat->addr.addr4.s_addr);
1171 /* This is out of date in the CIDR world, but some people might
1174 if (IN_CLASSA(addr.s_addr))
1175 pat->mask.addr.addr4.s_addr = htonl(IN_CLASSA_NET);
1176 else if (IN_CLASSB(addr.s_addr))
1177 pat->mask.addr.addr4.s_addr = htonl(IN_CLASSB_NET);
1179 pat->mask.addr.addr4.s_addr = htonl(IN_CLASSC_NET);