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]);
615 fp = fopen(PATH_RESOLV_CONF, "r");
617 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
619 if ((p = try_config(line, "domain")) && channel->ndomains == -1)
620 status = config_domain(channel, p);
621 else if ((p = try_config(line, "lookup")) && !channel->lookups)
622 status = config_lookup(channel, p, "bind", "file");
623 else if ((p = try_config(line, "search")) && channel->ndomains == -1)
624 status = set_search(channel, p);
625 else if ((p = try_config(line, "nameserver")) && channel->nservers == -1)
626 status = config_nameserver(&servers, &nservers, p);
627 else if ((p = try_config(line, "sortlist")) && channel->nsort == -1)
628 status = config_sortlist(&sortlist, &nsort, p);
629 else if ((p = try_config(line, "options")))
630 status = set_options(channel, p);
632 status = ARES_SUCCESS;
633 if (status != ARES_SUCCESS)
646 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
647 error, strerror(error)));
648 DEBUGF(fprintf(stderr, "Error opening file: %s\n", PATH_RESOLV_CONF));
653 if ((status == ARES_EOF) && (!channel->lookups)) {
654 /* Many systems (Solaris, Linux, BSD's) use nsswitch.conf */
655 fp = fopen("/etc/nsswitch.conf", "r");
657 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
659 if ((p = try_config(line, "hosts:")) && !channel->lookups)
660 status = config_lookup(channel, p, "dns", "files");
672 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
673 error, strerror(error)));
674 DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/nsswitch.conf"));
680 if ((status == ARES_EOF) && (!channel->lookups)) {
681 /* Linux / GNU libc 2.x and possibly others have host.conf */
682 fp = fopen("/etc/host.conf", "r");
684 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
686 if ((p = try_config(line, "order")) && !channel->lookups)
687 status = config_lookup(channel, p, "bind", "hosts");
699 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
700 error, strerror(error)));
701 DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/host.conf"));
707 if ((status == ARES_EOF) && (!channel->lookups)) {
708 /* Tru64 uses /etc/svc.conf */
709 fp = fopen("/etc/svc.conf", "r");
711 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
713 if ((p = try_config(line, "hosts=")) && !channel->lookups)
714 status = config_lookup(channel, p, "bind", "local");
726 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
727 error, strerror(error)));
728 DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/svc.conf"));
741 if (status != ARES_EOF)
745 if (sortlist != NULL)
750 /* If we got any name server entries, fill them in. */
756 channel->servers = servers;
757 channel->nservers = nservers;
760 /* If we got any sortlist entries, fill them in. */
763 channel->sortlist = sortlist;
764 channel->nsort = nsort;
770 static int init_by_defaults(ares_channel channel)
772 char hostname[MAXHOSTNAMELEN + 1];
774 if (channel->flags == -1)
776 if (channel->timeout == -1)
777 channel->timeout = DEFAULT_TIMEOUT;
778 if (channel->tries == -1)
779 channel->tries = DEFAULT_TRIES;
780 if (channel->ndots == -1)
782 if (channel->udp_port == -1)
783 channel->udp_port = htons(NAMESERVER_PORT);
784 if (channel->tcp_port == -1)
785 channel->tcp_port = htons(NAMESERVER_PORT);
787 if (channel->nservers == -1)
789 /* If nobody specified servers, try a local named. */
790 channel->servers = malloc(sizeof(struct server_state));
791 if (!channel->servers)
793 channel->servers[0].addr.s_addr = htonl(INADDR_LOOPBACK);
794 channel->nservers = 1;
797 if (channel->ndomains == -1)
799 /* Derive a default domain search list from the kernel hostname,
800 * or set it to empty if the hostname isn't helpful.
802 if (gethostname(hostname, sizeof(hostname)) == -1
803 || !strchr(hostname, '.'))
805 channel->ndomains = 0;
809 channel->domains = malloc(sizeof(char *));
810 if (!channel->domains)
812 channel->ndomains = 0;
813 channel->domains[0] = strdup(strchr(hostname, '.') + 1);
814 if (!channel->domains[0])
816 channel->ndomains = 1;
820 if (channel->nsort == -1)
822 channel->sortlist = NULL;
826 if (!channel->lookups)
828 channel->lookups = strdup("fb");
829 if (!channel->lookups)
837 static int config_domain(ares_channel channel, char *str)
841 /* Set a single search domain. */
843 while (*q && !ISSPACE(*q))
846 return set_search(channel, str);
849 static int config_lookup(ares_channel channel, const char *str,
850 const char *bindch, const char *filech)
855 /* Set the lookup order. Only the first letter of each work
856 * is relevant, and it has to be "b" for DNS or "f" for the
857 * host file. Ignore everything else.
863 if ((*p == *bindch || *p == *filech) && l < lookups + 2) {
864 if (*p == *bindch) *l++ = 'b';
867 while (*p && !ISSPACE(*p) && (*p != ','))
869 while (*p && (ISSPACE(*p) || (*p == ',')))
873 channel->lookups = strdup(lookups);
874 return (channel->lookups) ? ARES_SUCCESS : ARES_ENOMEM;
879 static int config_nameserver(struct server_state **servers, int *nservers,
883 struct server_state *newserv;
884 /* On Windows, there may be more than one nameserver specified in the same
885 * registry key, so we parse it as a space or comma seperated list.
894 while (*p && !ISSPACE(*p) && *p != ',')
903 /* Skip multiple spaces or trailing spaces */
910 /* This is the part that actually sets the nameserver */
911 addr.s_addr = inet_addr(begin);
912 if (addr.s_addr == INADDR_NONE)
914 newserv = realloc(*servers, (*nservers + 1) * sizeof(struct server_state));
917 newserv[*nservers].addr = addr;
926 /* Add a nameserver entry, if this is a valid address. */
927 addr.s_addr = inet_addr(str);
928 if (addr.s_addr == INADDR_NONE)
930 newserv = realloc(*servers, (*nservers + 1) * sizeof(struct server_state));
933 newserv[*nservers].addr = addr;
941 static int config_sortlist(struct apattern **sortlist, int *nsort,
947 /* Add sortlist entries. */
948 while (*str && *str != ';')
951 char ipbuf[16], ipbufpfx[32];
952 /* Find just the IP */
954 while (*q && *q != '/' && *q != ';' && !ISSPACE(*q))
956 memcpy(ipbuf, str, (int)(q-str));
957 ipbuf[(int)(q-str)] = '\0';
958 /* Find the prefix */
961 const char *str2 = q+1;
962 while (*q && *q != ';' && !ISSPACE(*q))
964 memcpy(ipbufpfx, str, (int)(q-str));
965 ipbufpfx[(int)(q-str)] = '\0';
970 /* Lets see if it is CIDR */
971 /* First we'll try IPv6 */
972 if ((bits = ares_inet_net_pton(AF_INET6, ipbufpfx ? ipbufpfx : ipbuf,
974 sizeof(pat.addr.addr6))) > 0)
976 pat.type = PATTERN_CIDR;
977 pat.mask.bits = (unsigned short)bits;
978 pat.family = AF_INET6;
979 if (!sortlist_alloc(sortlist, nsort, &pat))
983 (bits = ares_inet_net_pton(AF_INET, ipbufpfx, &pat.addr.addr4,
984 sizeof(pat.addr.addr4))) > 0)
986 pat.type = PATTERN_CIDR;
987 pat.mask.bits = (unsigned short)bits;
988 pat.family = AF_INET;
989 if (!sortlist_alloc(sortlist, nsort, &pat))
992 /* See if it is just a regular IP */
993 else if (ip_addr(ipbuf, (int)(q-str), &pat.addr.addr4) == 0)
997 memcpy(ipbuf, str, (int)(q-str));
998 ipbuf[(int)(q-str)] = '\0';
999 if (ip_addr(ipbuf, (int)(q - str), &pat.mask.addr.addr4) != 0)
1004 pat.family = AF_INET;
1005 pat.type = PATTERN_MASK;
1006 if (!sortlist_alloc(sortlist, nsort, &pat))
1011 while (*q && *q != ';' && !ISSPACE(*q))
1015 while (ISSPACE(*str))
1019 return ARES_SUCCESS;
1023 static int set_search(ares_channel channel, const char *str)
1028 if(channel->ndomains != -1) {
1029 /* if we already have some domains present, free them first */
1030 for(n=0; n < channel->ndomains; n++)
1031 free(channel->domains[n]);
1032 free(channel->domains);
1033 channel->domains = NULL;
1034 channel->ndomains = -1;
1037 /* Count the domains given. */
1042 while (*p && !ISSPACE(*p))
1051 channel->ndomains = 0;
1052 return ARES_SUCCESS;
1055 channel->domains = malloc(n * sizeof(char *));
1056 if (!channel->domains)
1059 /* Now copy the domains. */
1064 channel->ndomains = n;
1066 while (*q && !ISSPACE(*q))
1068 channel->domains[n] = malloc(q - p + 1);
1069 if (!channel->domains[n])
1071 memcpy(channel->domains[n], p, q - p);
1072 channel->domains[n][q - p] = 0;
1078 channel->ndomains = n;
1080 return ARES_SUCCESS;
1083 static int set_options(ares_channel channel, const char *str)
1085 const char *p, *q, *val;
1091 while (*q && !ISSPACE(*q))
1093 val = try_option(p, q, "ndots:");
1094 if (val && channel->ndots == -1)
1095 channel->ndots = atoi(val);
1096 val = try_option(p, q, "retrans:");
1097 if (val && channel->timeout == -1)
1098 channel->timeout = atoi(val);
1099 val = try_option(p, q, "retry:");
1100 if (val && channel->tries == -1)
1101 channel->tries = atoi(val);
1107 return ARES_SUCCESS;
1111 static char *try_config(char *s, const char *opt)
1116 if (strncmp(s, opt, len) != 0 || !ISSPACE(s[len]))
1126 static const char *try_option(const char *p, const char *q, const char *opt)
1128 size_t len = strlen(opt);
1129 return ((size_t)(q - p) > len && !strncmp(p, opt, len)) ? &p[len] : NULL;
1133 static int sortlist_alloc(struct apattern **sortlist, int *nsort,
1134 struct apattern *pat)
1136 struct apattern *newsort;
1137 newsort = realloc(*sortlist, (*nsort + 1) * sizeof(struct apattern));
1140 newsort[*nsort] = *pat;
1141 *sortlist = newsort;
1146 static int ip_addr(const char *ipbuf, int len, struct in_addr *addr)
1149 /* Four octets and three periods yields at most 15 characters. */
1153 addr->s_addr = inet_addr(ipbuf);
1154 if (addr->s_addr == INADDR_NONE && strcmp(ipbuf, "255.255.255.255") != 0)
1159 static void natural_mask(struct apattern *pat)
1161 struct in_addr addr;
1163 /* Store a host-byte-order copy of pat in a struct in_addr. Icky,
1166 addr.s_addr = ntohl(pat->addr.addr4.s_addr);
1168 /* This is out of date in the CIDR world, but some people might
1171 if (IN_CLASSA(addr.s_addr))
1172 pat->mask.addr.addr4.s_addr = htonl(IN_CLASSA_NET);
1173 else if (IN_CLASSB(addr.s_addr))
1174 pat->mask.addr.addr4.s_addr = htonl(IN_CLASSB_NET);
1176 pat->mask.addr.addr4.s_addr = htonl(IN_CLASSC_NET);