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 return (errno == ENOENT) ? ARES_SUCCESS : ARES_EFILE;
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)
638 if (!channel->lookups) {
639 /* Many systems (Solaris, Linux, BSD's) use nsswitch.conf */
640 fp = fopen("/etc/nsswitch.conf", "r");
642 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
644 if ((p = try_config(line, "hosts:")) && !channel->lookups)
645 status = config_lookup(channel, p, "dns", "files");
651 if (!channel->lookups) {
652 /* Linux / GNU libc 2.x and possibly others have host.conf */
653 fp = fopen("/etc/host.conf", "r");
655 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
657 if ((p = try_config(line, "order")) && !channel->lookups)
658 status = config_lookup(channel, p, "bind", "hosts");
664 if (!channel->lookups) {
665 /* Tru64 uses /etc/svc.conf */
666 fp = fopen("/etc/svc.conf", "r");
668 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
670 if ((p = try_config(line, "hosts=")) && !channel->lookups)
671 status = config_lookup(channel, p, "bind", "local");
684 if (status != ARES_EOF)
688 if (sortlist != NULL)
693 /* If we got any name server entries, fill them in. */
699 channel->servers = servers;
700 channel->nservers = nservers;
703 /* If we got any sortlist entries, fill them in. */
706 channel->sortlist = sortlist;
707 channel->nsort = nsort;
713 static int init_by_defaults(ares_channel channel)
715 char hostname[MAXHOSTNAMELEN + 1];
717 if (channel->flags == -1)
719 if (channel->timeout == -1)
720 channel->timeout = DEFAULT_TIMEOUT;
721 if (channel->tries == -1)
722 channel->tries = DEFAULT_TRIES;
723 if (channel->ndots == -1)
725 if (channel->udp_port == -1)
726 channel->udp_port = htons(NAMESERVER_PORT);
727 if (channel->tcp_port == -1)
728 channel->tcp_port = htons(NAMESERVER_PORT);
730 if (channel->nservers == -1)
732 /* If nobody specified servers, try a local named. */
733 channel->servers = malloc(sizeof(struct server_state));
734 if (!channel->servers)
736 channel->servers[0].addr.s_addr = htonl(INADDR_LOOPBACK);
737 channel->nservers = 1;
740 if (channel->ndomains == -1)
742 /* Derive a default domain search list from the kernel hostname,
743 * or set it to empty if the hostname isn't helpful.
745 if (gethostname(hostname, sizeof(hostname)) == -1
746 || !strchr(hostname, '.'))
748 channel->ndomains = 0;
752 channel->domains = malloc(sizeof(char *));
753 if (!channel->domains)
755 channel->ndomains = 0;
756 channel->domains[0] = strdup(strchr(hostname, '.') + 1);
757 if (!channel->domains[0])
759 channel->ndomains = 1;
763 if (channel->nsort == -1)
765 channel->sortlist = NULL;
769 if (!channel->lookups)
771 channel->lookups = strdup("fb");
772 if (!channel->lookups)
780 static int config_domain(ares_channel channel, char *str)
784 /* Set a single search domain. */
786 while (*q && !ISSPACE(*q))
789 return set_search(channel, str);
792 static int config_lookup(ares_channel channel, const char *str,
793 const char *bindch, const char *filech)
798 /* Set the lookup order. Only the first letter of each work
799 * is relevant, and it has to be "b" for DNS or "f" for the
800 * host file. Ignore everything else.
806 if ((*p == *bindch || *p == *filech) && l < lookups + 2) {
807 if (*p == *bindch) *l++ = 'b';
810 while (*p && !ISSPACE(*p) && (*p != ','))
812 while (*p && (ISSPACE(*p) || (*p == ',')))
816 channel->lookups = strdup(lookups);
817 return (channel->lookups) ? ARES_SUCCESS : ARES_ENOMEM;
822 static int config_nameserver(struct server_state **servers, int *nservers,
826 struct server_state *newserv;
827 /* On Windows, there may be more than one nameserver specified in the same
828 * registry key, so we parse it as a space or comma seperated list.
837 while (*p && !ISSPACE(*p) && *p != ',')
846 /* Skip multiple spaces or trailing spaces */
853 /* This is the part that actually sets the nameserver */
854 addr.s_addr = inet_addr(begin);
855 if (addr.s_addr == INADDR_NONE)
857 newserv = realloc(*servers, (*nservers + 1) * sizeof(struct server_state));
860 newserv[*nservers].addr = addr;
869 /* Add a nameserver entry, if this is a valid address. */
870 addr.s_addr = inet_addr(str);
871 if (addr.s_addr == INADDR_NONE)
873 newserv = realloc(*servers, (*nservers + 1) * sizeof(struct server_state));
876 newserv[*nservers].addr = addr;
884 static int config_sortlist(struct apattern **sortlist, int *nsort,
890 /* Add sortlist entries. */
891 while (*str && *str != ';')
894 char ipbuf[16], ipbufpfx[32];
895 /* Find just the IP */
897 while (*q && *q != '/' && *q != ';' && !ISSPACE(*q))
899 memcpy(ipbuf, str, (int)(q-str));
900 ipbuf[(int)(q-str)] = 0;
901 /* Find the prefix */
904 const char *str2 = q+1;
905 while (*q && *q != ';' && !ISSPACE(*q))
907 memcpy(ipbufpfx, str, (int)(q-str));
908 ipbufpfx[(int)(q-str)] = 0;
913 /* Lets see if it is CIDR */
914 /* First we'll try IPv6 */
915 if ((bits = ares_inet_net_pton(AF_INET6, ipbufpfx ? ipbufpfx : ipbuf,
917 sizeof(pat.addr.addr6))) > 0)
919 pat.type = PATTERN_CIDR;
920 pat.mask.bits = (unsigned short)bits;
921 pat.family = AF_INET6;
922 if (!sortlist_alloc(sortlist, nsort, &pat))
926 (bits = ares_inet_net_pton(AF_INET, ipbufpfx, &pat.addr.addr4,
927 sizeof(pat.addr.addr4))) > 0)
929 pat.type = PATTERN_CIDR;
930 pat.mask.bits = (unsigned short)bits;
931 pat.family = AF_INET;
932 if (!sortlist_alloc(sortlist, nsort, &pat))
935 /* See if it is just a regular IP */
936 else if (ip_addr(ipbuf, (int)(q-str), &pat.addr.addr4) == 0)
940 memcpy(ipbuf, str, (int)(q-str));
941 ipbuf[(int)(q-str)] = 0;
942 if (ip_addr(ipbuf, (int)(q - str), &pat.mask.addr.addr4) != 0)
947 pat.family = AF_INET;
948 pat.type = PATTERN_MASK;
949 if (!sortlist_alloc(sortlist, nsort, &pat))
954 while (*q && *q != ';' && !ISSPACE(*q))
958 while (ISSPACE(*str))
966 static int set_search(ares_channel channel, const char *str)
971 if(channel->ndomains != -1) {
972 /* if we already have some domains present, free them first */
973 for(n=0; n < channel->ndomains; n++)
974 free(channel->domains[n]);
975 free(channel->domains);
976 channel->domains = NULL;
977 channel->ndomains = -1;
980 /* Count the domains given. */
985 while (*p && !ISSPACE(*p))
994 channel->ndomains = 0;
998 channel->domains = malloc(n * sizeof(char *));
999 if (!channel->domains)
1002 /* Now copy the domains. */
1007 channel->ndomains = n;
1009 while (*q && !ISSPACE(*q))
1011 channel->domains[n] = malloc(q - p + 1);
1012 if (!channel->domains[n])
1014 memcpy(channel->domains[n], p, q - p);
1015 channel->domains[n][q - p] = 0;
1021 channel->ndomains = n;
1023 return ARES_SUCCESS;
1026 static int set_options(ares_channel channel, const char *str)
1028 const char *p, *q, *val;
1034 while (*q && !ISSPACE(*q))
1036 val = try_option(p, q, "ndots:");
1037 if (val && channel->ndots == -1)
1038 channel->ndots = atoi(val);
1039 val = try_option(p, q, "retrans:");
1040 if (val && channel->timeout == -1)
1041 channel->timeout = atoi(val);
1042 val = try_option(p, q, "retry:");
1043 if (val && channel->tries == -1)
1044 channel->tries = atoi(val);
1050 return ARES_SUCCESS;
1054 static char *try_config(char *s, const char *opt)
1059 if (strncmp(s, opt, len) != 0 || !ISSPACE(s[len]))
1069 static const char *try_option(const char *p, const char *q, const char *opt)
1071 size_t len = strlen(opt);
1072 return ((size_t)(q - p) > len && !strncmp(p, opt, len)) ? &p[len] : NULL;
1076 static int sortlist_alloc(struct apattern **sortlist, int *nsort,
1077 struct apattern *pat)
1079 struct apattern *newsort;
1080 newsort = realloc(*sortlist, (*nsort + 1) * sizeof(struct apattern));
1083 newsort[*nsort] = *pat;
1084 *sortlist = newsort;
1089 static int ip_addr(const char *ipbuf, int len, struct in_addr *addr)
1092 /* Four octets and three periods yields at most 15 characters. */
1096 addr->s_addr = inet_addr(ipbuf);
1097 if (addr->s_addr == INADDR_NONE && strcmp(ipbuf, "255.255.255.255") != 0)
1102 static void natural_mask(struct apattern *pat)
1104 struct in_addr addr;
1106 /* Store a host-byte-order copy of pat in a struct in_addr. Icky,
1109 addr.s_addr = ntohl(pat->addr.addr4.s_addr);
1111 /* This is out of date in the CIDR world, but some people might
1114 if (IN_CLASSA(addr.s_addr))
1115 pat->mask.addr.addr4.s_addr = htonl(IN_CLASSA_NET);
1116 else if (IN_CLASSB(addr.s_addr))
1117 pat->mask.addr.addr4.s_addr = htonl(IN_CLASSB_NET);
1119 pat->mask.addr.addr4.s_addr = htonl(IN_CLASSC_NET);