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>
55 #include "ares_private.h"
56 #include "inet_net_pton.h"
59 #undef WIN32 /* Redefined in MingW/MSVC headers */
62 static int init_by_options(ares_channel channel, struct ares_options *options,
64 static int init_by_environment(ares_channel channel);
65 static int init_by_resolv_conf(ares_channel channel);
66 static int init_by_defaults(ares_channel channel);
68 static int config_nameserver(struct server_state **servers, int *nservers,
70 static int set_search(ares_channel channel, const char *str);
71 static int set_options(ares_channel channel, const char *str);
72 static const char *try_option(const char *p, const char *q, const char *opt);
74 static int sortlist_alloc(struct apattern **sortlist, int *nsort, struct apattern *pat);
75 static int ip_addr(const char *s, int len, struct in_addr *addr);
76 static void natural_mask(struct apattern *pat);
77 static int config_domain(ares_channel channel, char *str);
78 static int config_lookup(ares_channel channel, const char *str,
79 const char *bindch, const char *filech);
80 static int config_sortlist(struct apattern **sortlist, int *nsort,
82 static char *try_config(char *s, const char *opt);
85 int ares_init(ares_channel *channelptr)
87 return ares_init_options(channelptr, NULL, 0);
90 int ares_init_options(ares_channel *channelptr, struct ares_options *options,
95 int status = ARES_SUCCESS;
96 struct server_state *server;
100 const char *env = getenv("CARES_MEMDEBUG");
104 env = getenv("CARES_MEMLIMIT");
106 curl_memlimit(atoi(env));
109 channel = malloc(sizeof(struct ares_channeldata));
115 /* Set everything to distinguished values so we know they haven't
119 channel->timeout = -1;
122 channel->udp_port = -1;
123 channel->tcp_port = -1;
124 channel->nservers = -1;
125 channel->ndomains = -1;
127 channel->lookups = NULL;
128 channel->queries = NULL;
129 channel->domains = NULL;
130 channel->sortlist = NULL;
131 channel->servers = NULL;
132 channel->sock_state_cb = NULL;
133 channel->sock_state_cb_data = NULL;
135 /* Initialize configuration by each of the four sources, from highest
136 * precedence to lowest.
139 if (status == ARES_SUCCESS) {
140 status = init_by_options(channel, options, optmask);
141 if (status != ARES_SUCCESS)
142 DEBUGF(fprintf(stderr, "Error: init_by_options failed: %s\n",
143 ares_strerror(status)));
145 if (status == ARES_SUCCESS) {
146 status = init_by_environment(channel);
147 if (status != ARES_SUCCESS)
148 DEBUGF(fprintf(stderr, "Error: init_by_environment failed: %s\n",
149 ares_strerror(status)));
151 if (status == ARES_SUCCESS) {
152 status = init_by_resolv_conf(channel);
153 if (status != ARES_SUCCESS)
154 DEBUGF(fprintf(stderr, "Error: init_by_resolv_conf failed: %s\n",
155 ares_strerror(status)));
157 if (status == ARES_SUCCESS) {
158 status = init_by_defaults(channel);
159 if (status != ARES_SUCCESS)
160 DEBUGF(fprintf(stderr, "Error: init_by_defaults failed: %s\n",
161 ares_strerror(status)));
163 if (status != ARES_SUCCESS)
165 /* Something failed; clean up memory we may have allocated. */
166 if (channel->servers)
167 free(channel->servers);
168 if (channel->domains)
170 for (i = 0; i < channel->ndomains; i++)
171 free(channel->domains[i]);
172 free(channel->domains);
174 if (channel->sortlist)
175 free(channel->sortlist);
177 free(channel->lookups);
182 /* Trim to one server if ARES_FLAG_PRIMARY is set. */
183 if ((channel->flags & ARES_FLAG_PRIMARY) && channel->nservers > 1)
184 channel->nservers = 1;
186 /* Initialize server states. */
187 for (i = 0; i < channel->nservers; i++)
189 server = &channel->servers[i];
190 server->udp_socket = ARES_SOCKET_BAD;
191 server->tcp_socket = ARES_SOCKET_BAD;
192 server->tcp_lenbuf_pos = 0;
193 server->tcp_buffer = NULL;
194 server->qhead = NULL;
195 server->qtail = NULL;
198 /* Choose a somewhat random query ID. The main point is to avoid
199 * collisions with stale queries. An attacker trying to spoof a DNS
200 * answer also has to guess the query ID, but it's only a 16-bit
201 * field, so there's not much to be done about that.
203 gettimeofday(&tv, NULL);
204 channel->next_id = (unsigned short)
205 ((tv.tv_sec ^ tv.tv_usec ^ getpid()) & 0xffff);
207 channel->queries = NULL;
209 *channelptr = channel;
213 static int init_by_options(ares_channel channel, struct ares_options *options,
219 if ((optmask & ARES_OPT_FLAGS) && channel->flags == -1)
220 channel->flags = options->flags;
221 if ((optmask & ARES_OPT_TIMEOUT) && channel->timeout == -1)
222 channel->timeout = options->timeout;
223 if ((optmask & ARES_OPT_TRIES) && channel->tries == -1)
224 channel->tries = options->tries;
225 if ((optmask & ARES_OPT_NDOTS) && channel->ndots == -1)
226 channel->ndots = options->ndots;
227 if ((optmask & ARES_OPT_UDP_PORT) && channel->udp_port == -1)
228 channel->udp_port = options->udp_port;
229 if ((optmask & ARES_OPT_TCP_PORT) && channel->tcp_port == -1)
230 channel->tcp_port = options->tcp_port;
231 if ((optmask & ARES_OPT_SOCK_STATE_CB) && channel->sock_state_cb == NULL)
233 channel->sock_state_cb = options->sock_state_cb;
234 channel->sock_state_cb_data = options->sock_state_cb_data;
237 /* Copy the servers, if given. */
238 if ((optmask & ARES_OPT_SERVERS) && channel->nservers == -1)
240 /* Avoid zero size allocations at any cost */
241 if (options->nservers > 0)
244 malloc(options->nservers * sizeof(struct server_state));
245 if (!channel->servers)
247 for (i = 0; i < options->nservers; i++)
248 channel->servers[i].addr = options->servers[i];
250 channel->nservers = options->nservers;
253 /* Copy the domains, if given. Keep channel->ndomains consistent so
254 * we can clean up in case of error.
256 if ((optmask & ARES_OPT_DOMAINS) && channel->ndomains == -1)
258 /* Avoid zero size allocations at any cost */
259 if (options->ndomains > 0)
261 channel->domains = malloc(options->ndomains * sizeof(char *));
262 if (!channel->domains)
264 for (i = 0; i < options->ndomains; i++)
266 channel->ndomains = i;
267 channel->domains[i] = strdup(options->domains[i]);
268 if (!channel->domains[i])
272 channel->ndomains = options->ndomains;
275 /* Set lookups, if given. */
276 if ((optmask & ARES_OPT_LOOKUPS) && !channel->lookups)
278 channel->lookups = strdup(options->lookups);
279 if (!channel->lookups)
286 static int init_by_environment(ares_channel channel)
288 const char *localdomain, *res_options;
291 localdomain = getenv("LOCALDOMAIN");
292 if (localdomain && channel->ndomains == -1)
294 status = set_search(channel, localdomain);
295 if (status != ARES_SUCCESS)
299 res_options = getenv("RES_OPTIONS");
302 status = set_options(channel, res_options);
303 if (status != ARES_SUCCESS)
312 * Warning: returns a dynamically allocated buffer, the user MUST
313 * use free() if the function returns 1
315 static int get_res_nt(HKEY hKey, const char *subkey, char **obuf)
317 /* Test for the size we need */
321 result = RegQueryValueEx(hKey, subkey, 0, NULL, NULL, &size);
322 if ((result != ERROR_SUCCESS && result != ERROR_MORE_DATA) || !size)
324 *obuf = malloc(size+1);
328 if (RegQueryValueEx(hKey, subkey, 0, NULL,
329 (LPBYTE)*obuf, &size) != ERROR_SUCCESS)
342 static int get_res_interfaces_nt(HKEY hKey, const char *subkey, char **obuf)
344 char enumbuf[39]; /* GUIDs are 38 chars + 1 for NULL */
345 DWORD enum_size = 39;
349 while (RegEnumKeyEx(hKey, idx++, enumbuf, &enum_size, 0,
350 NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS)
355 if (RegOpenKeyEx(hKey, enumbuf, 0, KEY_QUERY_VALUE, &hVal) !=
358 rc = get_res_nt(hVal, subkey, obuf);
366 static int get_iphlpapi_dns_info (char *ret_buf, size_t ret_size)
368 FIXED_INFO *fi = alloca (sizeof(*fi));
369 DWORD size = sizeof (*fi);
370 typedef DWORD (WINAPI* get_net_param_func) (FIXED_INFO*, DWORD*);
371 get_net_param_func GetNetworkParams; /* available only on Win-98/2000+ */
373 IP_ADDR_STRING *ipAddr;
376 size_t ip_size = sizeof("255.255.255.255,")-1;
377 size_t left = ret_size;
384 handle = LoadLibrary ("iphlpapi.dll");
388 GetNetworkParams = (get_net_param_func) GetProcAddress (handle, "GetNetworkParams");
389 if (!GetNetworkParams)
392 res = (*GetNetworkParams) (fi, &size);
393 if ((res != ERROR_BUFFER_OVERFLOW) && (res != ERROR_SUCCESS))
397 if (!fi || (*GetNetworkParams) (fi, &size) != ERROR_SUCCESS)
402 printf ("Host Name: %s\n", fi->HostName);
403 printf ("Domain Name: %s\n", fi->DomainName);
404 printf ("DNS Servers:\n"
405 " %s (primary)\n", fi->DnsServerList.IpAddress.String);
407 if (strlen(fi->DnsServerList.IpAddress.String) > 0 &&
408 inet_addr(fi->DnsServerList.IpAddress.String) != INADDR_NONE &&
411 ret += sprintf (ret, "%s,", fi->DnsServerList.IpAddress.String);
412 left -= ret - ret_buf;
416 for (i = 0, ipAddr = fi->DnsServerList.Next; ipAddr && left > ip_size;
417 ipAddr = ipAddr->Next, i++)
419 if (inet_addr(ipAddr->IpAddress.String) != INADDR_NONE)
421 ret += sprintf (ret, "%s,", ipAddr->IpAddress.String);
422 left -= ret - ret_buf;
426 printf (" %s (secondary %d)\n", ipAddr->IpAddress.String, i+1);
431 FreeLibrary (handle);
433 if (debug && left <= ip_size)
434 printf ("Too many nameservers. Truncating to %d addressess", count);
441 static int init_by_resolv_conf(ares_channel channel)
444 int status = -1, nservers = 0, nsort = 0;
445 struct server_state *servers = NULL;
446 struct apattern *sortlist = NULL;
451 NameServer info via IPHLPAPI (IP helper API):
452 GetNetworkParams() should be the trusted source for this.
453 Available in Win-98/2000 and later. If that fail, fall-back to
454 registry information.
458 On Windows 9X, the DNS server can be found in:
459 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\MSTCP\NameServer
461 On Windows NT/2000/XP/2003:
462 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NameServer
464 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DhcpNameServer
466 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\{AdapterID}\
469 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\{AdapterID}\
480 if (channel->nservers > -1) /* don't override ARES_OPT_SERVER */
483 if (get_iphlpapi_dns_info(buf,sizeof(buf)) > 0)
485 status = config_nameserver(&servers, &nservers, buf);
486 if (status == ARES_SUCCESS)
493 HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
497 RegOpenKeyEx(mykey, "Interfaces", 0,
498 KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS, &subkey);
499 if (get_res_nt(mykey, NAMESERVER, &line))
501 status = config_nameserver(&servers, &nservers, line);
504 else if (get_res_nt(mykey, DHCPNAMESERVER, &line))
506 status = config_nameserver(&servers, &nservers, line);
509 /* Try the interfaces */
510 else if (get_res_interfaces_nt(subkey, NAMESERVER, &line))
512 status = config_nameserver(&servers, &nservers, line);
515 else if (get_res_interfaces_nt(subkey, DHCPNAMESERVER, &line))
517 status = config_nameserver(&servers, &nservers, line);
527 HKEY_LOCAL_MACHINE, WIN_NS_9X, 0,
531 if ((result = RegQueryValueEx(
532 mykey, NAMESERVER, NULL, &data_type,
535 ) == ERROR_SUCCESS ||
536 result == ERROR_MORE_DATA)
540 line = (char *)malloc(bytes+1);
541 if (RegQueryValueEx(mykey, NAMESERVER, NULL, &data_type,
542 (unsigned char *)line, &bytes) ==
545 status = config_nameserver(&servers, &nservers, line);
554 if (status == ARES_SUCCESS)
557 #elif defined(__riscos__)
559 /* Under RISC OS, name servers are listed in the
560 system variable Inet$Resolvers, space separated. */
562 line = getenv("Inet$Resolvers");
565 char *resolvers = strdup(line), *pos, *space;
572 space = strchr(pos, ' ');
575 status = config_nameserver(&servers, &nservers, pos);
576 if (status != ARES_SUCCESS)
581 if (status == ARES_SUCCESS)
587 #elif defined(WATT32)
591 for (i = 0; def_nameservers[i]; i++)
594 return ARES_SUCCESS; /* use localhost DNS server */
597 servers = calloc(sizeof(*servers), i);
601 for (i = 0; def_nameservers[i]; i++)
602 servers[i].addr.s_addr = htonl(def_nameservers[i]);
611 fp = fopen(PATH_RESOLV_CONF, "r");
613 return (errno == ENOENT) ? ARES_SUCCESS : ARES_EFILE;
614 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
616 if ((p = try_config(line, "domain")) && channel->ndomains == -1)
617 status = config_domain(channel, p);
618 else if ((p = try_config(line, "lookup")) && !channel->lookups)
619 status = config_lookup(channel, p, "bind", "file");
620 else if ((p = try_config(line, "search")) && channel->ndomains == -1)
621 status = set_search(channel, p);
622 else if ((p = try_config(line, "nameserver")) && channel->nservers == -1)
623 status = config_nameserver(&servers, &nservers, p);
624 else if ((p = try_config(line, "sortlist")) && channel->nsort == -1)
625 status = config_sortlist(&sortlist, &nsort, p);
626 else if ((p = try_config(line, "options")))
627 status = set_options(channel, p);
629 status = ARES_SUCCESS;
630 if (status != ARES_SUCCESS)
635 if (!channel->lookups) {
636 /* Many systems (Solaris, Linux, BSD's) use nsswitch.conf */
637 fp = fopen("/etc/nsswitch.conf", "r");
639 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
641 if ((p = try_config(line, "hosts:")) && !channel->lookups)
642 status = config_lookup(channel, p, "dns", "files");
648 if (!channel->lookups) {
649 /* Linux / GNU libc 2.x and possibly others have host.conf */
650 fp = fopen("/etc/host.conf", "r");
652 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
654 if ((p = try_config(line, "order")) && !channel->lookups)
655 status = config_lookup(channel, p, "bind", "hosts");
661 if (!channel->lookups) {
662 /* Tru64 uses /etc/svc.conf */
663 fp = fopen("/etc/svc.conf", "r");
665 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
667 if ((p = try_config(line, "hosts=")) && !channel->lookups)
668 status = config_lookup(channel, p, "bind", "local");
681 if (status != ARES_EOF)
685 if (sortlist != NULL)
690 /* If we got any name server entries, fill them in. */
696 channel->servers = servers;
697 channel->nservers = nservers;
700 /* If we got any sortlist entries, fill them in. */
703 channel->sortlist = sortlist;
704 channel->nsort = nsort;
710 static int init_by_defaults(ares_channel channel)
712 char hostname[MAXHOSTNAMELEN + 1];
714 if (channel->flags == -1)
716 if (channel->timeout == -1)
717 channel->timeout = DEFAULT_TIMEOUT;
718 if (channel->tries == -1)
719 channel->tries = DEFAULT_TRIES;
720 if (channel->ndots == -1)
722 if (channel->udp_port == -1)
723 channel->udp_port = htons(NAMESERVER_PORT);
724 if (channel->tcp_port == -1)
725 channel->tcp_port = htons(NAMESERVER_PORT);
727 if (channel->nservers == -1)
729 /* If nobody specified servers, try a local named. */
730 channel->servers = malloc(sizeof(struct server_state));
731 if (!channel->servers)
733 channel->servers[0].addr.s_addr = htonl(INADDR_LOOPBACK);
734 channel->nservers = 1;
737 if (channel->ndomains == -1)
739 /* Derive a default domain search list from the kernel hostname,
740 * or set it to empty if the hostname isn't helpful.
742 if (gethostname(hostname, sizeof(hostname)) == -1
743 || !strchr(hostname, '.'))
745 channel->ndomains = 0;
749 channel->domains = malloc(sizeof(char *));
750 if (!channel->domains)
752 channel->ndomains = 0;
753 channel->domains[0] = strdup(strchr(hostname, '.') + 1);
754 if (!channel->domains[0])
756 channel->ndomains = 1;
760 if (channel->nsort == -1)
762 channel->sortlist = NULL;
766 if (!channel->lookups)
768 channel->lookups = strdup("fb");
769 if (!channel->lookups)
777 static int config_domain(ares_channel channel, char *str)
781 /* Set a single search domain. */
783 while (*q && !ISSPACE(*q))
786 return set_search(channel, str);
789 static int config_lookup(ares_channel channel, const char *str,
790 const char *bindch, const char *filech)
795 /* Set the lookup order. Only the first letter of each work
796 * is relevant, and it has to be "b" for DNS or "f" for the
797 * host file. Ignore everything else.
803 if ((*p == *bindch || *p == *filech) && l < lookups + 2) {
804 if (*p == *bindch) *l++ = 'b';
807 while (*p && !ISSPACE(*p) && (*p != ','))
809 while (*p && (ISSPACE(*p) || (*p == ',')))
813 channel->lookups = strdup(lookups);
814 return (channel->lookups) ? ARES_SUCCESS : ARES_ENOMEM;
819 static int config_nameserver(struct server_state **servers, int *nservers,
823 struct server_state *newserv;
824 /* On Windows, there may be more than one nameserver specified in the same
825 * registry key, so we parse it as a space or comma seperated list.
834 while (*p && !ISSPACE(*p) && *p != ',')
843 /* Skip multiple spaces or trailing spaces */
850 /* This is the part that actually sets the nameserver */
851 addr.s_addr = inet_addr(begin);
852 if (addr.s_addr == INADDR_NONE)
854 newserv = realloc(*servers, (*nservers + 1) * sizeof(struct server_state));
857 newserv[*nservers].addr = addr;
866 /* Add a nameserver entry, if this is a valid address. */
867 addr.s_addr = inet_addr(str);
868 if (addr.s_addr == INADDR_NONE)
870 newserv = realloc(*servers, (*nservers + 1) * sizeof(struct server_state));
873 newserv[*nservers].addr = addr;
881 static int config_sortlist(struct apattern **sortlist, int *nsort,
887 /* Add sortlist entries. */
888 while (*str && *str != ';')
891 char ipbuf[16], ipbufpfx[32];
892 /* Find just the IP */
894 while (*q && *q != '/' && *q != ';' && !ISSPACE(*q))
896 memcpy(ipbuf, str, (int)(q-str));
897 ipbuf[(int)(q-str)] = 0;
898 /* Find the prefix */
901 const char *str2 = q+1;
902 while (*q && *q != ';' && !ISSPACE(*q))
904 memcpy(ipbufpfx, str, (int)(q-str));
905 ipbufpfx[(int)(q-str)] = 0;
910 /* Lets see if it is CIDR */
911 /* First we'll try IPv6 */
912 if ((bits = ares_inet_net_pton(AF_INET6, ipbufpfx ? ipbufpfx : ipbuf,
914 sizeof(pat.addr.addr6))) > 0)
916 pat.type = PATTERN_CIDR;
917 pat.mask.bits = (unsigned short)bits;
918 pat.family = AF_INET6;
919 if (!sortlist_alloc(sortlist, nsort, &pat))
923 (bits = ares_inet_net_pton(AF_INET, ipbufpfx, &pat.addr.addr4,
924 sizeof(pat.addr.addr4))) > 0)
926 pat.type = PATTERN_CIDR;
927 pat.mask.bits = (unsigned short)bits;
928 pat.family = AF_INET;
929 if (!sortlist_alloc(sortlist, nsort, &pat))
932 /* See if it is just a regular IP */
933 else if (ip_addr(ipbuf, (int)(q-str), &pat.addr.addr4) == 0)
937 memcpy(ipbuf, str, (int)(q-str));
938 ipbuf[(int)(q-str)] = 0;
939 if (ip_addr(ipbuf, (int)(q - str), &pat.mask.addr.addr4) != 0)
944 pat.family = AF_INET;
945 pat.type = PATTERN_MASK;
946 if (!sortlist_alloc(sortlist, nsort, &pat))
951 while (*q && *q != ';' && !ISSPACE(*q))
955 while (ISSPACE(*str))
963 static int set_search(ares_channel channel, const char *str)
968 if(channel->ndomains != -1) {
969 /* if we already have some domains present, free them first */
970 for(n=0; n < channel->ndomains; n++)
971 free(channel->domains[n]);
972 free(channel->domains);
973 channel->domains = NULL;
974 channel->ndomains = -1;
977 /* Count the domains given. */
982 while (*p && !ISSPACE(*p))
991 channel->ndomains = 0;
995 channel->domains = malloc(n * sizeof(char *));
996 if (!channel->domains)
999 /* Now copy the domains. */
1004 channel->ndomains = n;
1006 while (*q && !ISSPACE(*q))
1008 channel->domains[n] = malloc(q - p + 1);
1009 if (!channel->domains[n])
1011 memcpy(channel->domains[n], p, q - p);
1012 channel->domains[n][q - p] = 0;
1018 channel->ndomains = n;
1020 return ARES_SUCCESS;
1023 static int set_options(ares_channel channel, const char *str)
1025 const char *p, *q, *val;
1031 while (*q && !ISSPACE(*q))
1033 val = try_option(p, q, "ndots:");
1034 if (val && channel->ndots == -1)
1035 channel->ndots = atoi(val);
1036 val = try_option(p, q, "retrans:");
1037 if (val && channel->timeout == -1)
1038 channel->timeout = atoi(val);
1039 val = try_option(p, q, "retry:");
1040 if (val && channel->tries == -1)
1041 channel->tries = atoi(val);
1047 return ARES_SUCCESS;
1051 static char *try_config(char *s, const char *opt)
1056 if (strncmp(s, opt, len) != 0 || !ISSPACE(s[len]))
1066 static const char *try_option(const char *p, const char *q, const char *opt)
1068 size_t len = strlen(opt);
1069 return ((size_t)(q - p) > len && !strncmp(p, opt, len)) ? &p[len] : NULL;
1073 static int sortlist_alloc(struct apattern **sortlist, int *nsort,
1074 struct apattern *pat)
1076 struct apattern *newsort;
1077 newsort = realloc(*sortlist, (*nsort + 1) * sizeof(struct apattern));
1080 newsort[*nsort] = *pat;
1081 *sortlist = newsort;
1086 static int ip_addr(const char *ipbuf, int len, struct in_addr *addr)
1089 /* Four octets and three periods yields at most 15 characters. */
1093 addr->s_addr = inet_addr(ipbuf);
1094 if (addr->s_addr == INADDR_NONE && strcmp(ipbuf, "255.255.255.255") != 0)
1099 static void natural_mask(struct apattern *pat)
1101 struct in_addr addr;
1103 /* Store a host-byte-order copy of pat in a struct in_addr. Icky,
1106 addr.s_addr = ntohl(pat->addr.addr4.s_addr);
1108 /* This is out of date in the CIDR world, but some people might
1111 if (IN_CLASSA(addr.s_addr))
1112 pat->mask.addr.addr4.s_addr = htonl(IN_CLASSA_NET);
1113 else if (IN_CLASSB(addr.s_addr))
1114 pat->mask.addr.addr4.s_addr = htonl(IN_CLASSB_NET);
1116 pat->mask.addr.addr4.s_addr = htonl(IN_CLASSC_NET);