2 * Copyright (c) 1985, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
55 * Portions Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
56 * Portions Copyright (c) 1995-2003 by Internet Software Consortium
58 * Permission to use, copy, modify, and distribute this software for any
59 * purpose with or without fee is hereby granted, provided that the above
60 * copyright notice and this permission notice appear in all copies.
62 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
63 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
64 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
65 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
66 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
67 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
68 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
70 * Internet Systems Consortium, Inc.
72 * Redwood City, CA 94063
77 #if defined(LIBC_SCCS) && !defined(lint)
78 static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
79 static const char rcsid[] = "$Id: res_init.c,v 1.4.2.2 2004/06/10 17:59:43 dhankins Exp $";
80 #endif /* LIBC_SCCS and not lint */
82 #include <sys/types.h>
83 #include <sys/param.h>
84 #include <sys/socket.h>
87 #include <netinet/in.h>
88 #include <arpa/inet.h>
96 #include "minires/minires.h"
97 #include "arpa/nameser.h"
99 /* Options. Should all be left alone. */
104 static void res_setoptions (res_state, const char *, const char *);
107 static const char sort_mask[] = "/&";
108 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
109 static u_int32_t net_mask (struct in_addr);
112 #if !defined(isascii) /* XXX - could be a function */
113 # define isascii(c) (!(c & 0200))
117 * Resolver state default settings.
121 * Set up default settings. If the configuration file exist, the values
122 * there will have precedence. Otherwise, the server address is set to
123 * INADDR_ANY and the default domain name comes from the gethostname().
125 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
126 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
127 * since it was noted that INADDR_ANY actually meant ``the first interface
128 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
129 * it had to be "up" in order for you to reach your own name server. It
130 * was later decided that since the recommended practice is to always
131 * install local static routes through 127.0.0.1 for all your network
132 * interfaces, that we could solve this problem without a code change.
134 * The configuration file should always be used, since it is the only way
135 * to specify a default domain. If you are running a server on your local
136 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
137 * in the configuration file.
139 * Return 0 if completes successfully, -1 on error
141 extern int minires_vinit(res_state, int);
143 #if defined (TRACING)
144 u_int trace_mr_res_randomid(u_int);
148 res_ninit(res_state statp) {
150 return (minires_vinit(statp, 0));
153 /* This function has to be reachable by res_data.c but not publically. */
155 minires_vinit(res_state statp, int preinit) {
157 register char *cp, **pp;
160 int nserv = 0; /* number of nameserver records read from file */
172 statp->retrans = RES_TIMEOUT;
173 statp->retry = RES_DFLRETRY;
174 statp->options = RES_DEFAULT;
175 statp->id = res_randomid();
176 #if defined (TRACING)
177 statp->id = trace_mr_res_randomid (statp -> id);
182 statp->nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
184 statp->nsaddr.sin_addr.s_addr = INADDR_ANY;
186 statp->nsaddr.sin_family = AF_INET;
187 statp->nsaddr.sin_port = htons(NAMESERVER_PORT);
196 /* Allow user to override the local domain definition */
197 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
198 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
199 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
203 * Set search list to be blank-separated strings
204 * from rest of env value. Permits users of LOCALDOMAIN
205 * to still have a search list, and anyone to set the
206 * one that they want to use as an individual (even more
207 * important now that the rfc1535 stuff restricts searches)
209 cp = statp->defdname;
212 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
213 if (*cp == '\n') /* silly backwards compat */
215 else if (*cp == ' ' || *cp == '\t') {
224 /* null terminate last domain if there are excess */
225 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
231 #define MATCH(line, name) \
232 (!strncmp(line, name, sizeof(name) - 1) && \
233 (line[sizeof(name) - 1] == ' ' || \
234 line[sizeof(name) - 1] == '\t'))
236 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
237 /* read the config file */
238 while (fgets(buf, sizeof(buf), fp) != NULL) {
240 if (*buf == ';' || *buf == '#')
242 /* read default domain name */
243 if (MATCH(buf, "domain")) {
244 if (haveenv) /* skip if have from environ */
246 cp = buf + sizeof("domain") - 1;
247 while (*cp == ' ' || *cp == '\t')
249 if ((*cp == '\0') || (*cp == '\n'))
251 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
252 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
253 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
258 /* set search list */
259 if (MATCH(buf, "search")) {
260 if (haveenv) /* skip if have from environ */
262 cp = buf + sizeof("search") - 1;
263 while (*cp == ' ' || *cp == '\t')
265 if ((*cp == '\0') || (*cp == '\n'))
267 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
268 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
269 if ((cp = strchr(statp->defdname, '\n')) != NULL)
272 * Set search list to be blank-separated strings
275 cp = statp->defdname;
278 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
279 if (*cp == ' ' || *cp == '\t') {
287 /* null terminate last domain if there are excess */
288 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
295 /* read nameservers to query */
296 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
299 cp = buf + sizeof("nameserver") - 1;
300 while (*cp == ' ' || *cp == '\t')
302 if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) {
303 statp->nsaddr_list[nserv].sin_addr = a;
304 statp->nsaddr_list[nserv].sin_family = AF_INET;
305 statp->nsaddr_list[nserv].sin_port =
306 htons(NAMESERVER_PORT);
312 if (MATCH(buf, "sortlist")) {
315 cp = buf + sizeof("sortlist") - 1;
316 while (nsort < MAXRESOLVSORT) {
317 while (*cp == ' ' || *cp == '\t')
319 if (*cp == '\0' || *cp == '\n' || *cp == ';')
322 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
323 isascii(*cp) && !isspace(*cp))
327 if (inet_aton(net, &a)) {
328 statp->sort_list[nsort].addr = a;
332 while (*cp && *cp != ';' &&
333 isascii(*cp) && !isspace(*cp))
337 if (inet_aton(net, &a)) {
338 statp->sort_list[nsort].mask = a.s_addr;
340 statp->sort_list[nsort].mask =
341 net_mask(statp->sort_list[nsort].addr);
344 statp->sort_list[nsort].mask =
345 net_mask(statp->sort_list[nsort].addr);
354 if (MATCH(buf, "options")) {
355 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
360 statp->nscount = nserv;
362 statp->nsort = nsort;
366 if (statp->defdname[0] == 0 &&
367 gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
368 (cp = strchr(buf, '.')) != NULL)
369 strcpy(statp->defdname, cp + 1);
371 /* find components of local domain that might be searched */
372 if (havesearch == 0) {
374 *pp++ = statp->defdname;
379 for (cp = statp->defdname; *cp; cp++)
380 dots += (*cp == '.');
382 cp = statp->defdname;
383 while (pp < statp->dnsrch + MAXDFLSRCH) {
384 if (dots < LOCALDOMAINPARTS)
386 cp = strchr(cp, '.') + 1; /* we know there is one */
392 if (statp->options & RES_DEBUG) {
393 printf(";; res_init()... default dnsrch list:\n");
394 for (pp = statp->dnsrch; *pp; pp++)
395 printf(";;\t%s\n", *pp);
396 printf(";;\t..END..\n");
399 #endif /* !RFC1535 */
402 if ((cp = getenv("RES_OPTIONS")) != NULL)
403 res_setoptions(statp, cp, "env");
404 statp->options |= RES_INIT;
409 res_setoptions(res_state statp, const char *options, const char *source) {
410 const char *cp = options;
414 if (statp->options & RES_DEBUG)
415 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
419 /* skip leading and inner runs of spaces */
420 while (*cp == ' ' || *cp == '\t')
422 /* search for and process individual options */
423 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
424 i = atoi(cp + sizeof("ndots:") - 1);
425 if (i <= RES_MAXNDOTS)
428 statp->ndots = RES_MAXNDOTS;
430 if (statp->options & RES_DEBUG)
431 printf(";;\tndots=%d\n", statp->ndots);
433 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
434 i = atoi(cp + sizeof("timeout:") - 1);
435 if (i <= RES_MAXRETRANS)
438 statp->retrans = RES_MAXRETRANS;
439 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
440 i = atoi(cp + sizeof("attempts:") - 1);
441 if (i <= RES_MAXRETRY)
444 statp->retry = RES_MAXRETRY;
445 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
447 if (!(statp->options & RES_DEBUG)) {
448 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
450 statp->options |= RES_DEBUG;
452 printf(";;\tdebug\n");
454 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
455 statp->options |= RES_USE_INET6;
456 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
457 statp->options |= RES_ROTATE;
458 } else if (!strncmp(cp, "no-check-names",
459 sizeof("no-check-names") - 1)) {
460 statp->options |= RES_NOCHECKNAME;
462 /* XXX - print a warning here? */
464 /* skip to next run of spaces */
465 while (*cp && *cp != ' ' && *cp != '\t')
471 /* XXX - should really support CIDR which means explicit masks always. */
473 net_mask(in) /* XXX - should really use system's version of this */
476 register u_int32_t i = ntohl(in.s_addr);
479 return (htonl(IN_CLASSA_NET));
480 else if (IN_CLASSB(i))
481 return (htonl(IN_CLASSB_NET));
482 return (htonl(IN_CLASSC_NET));
490 gettimeofday(&now, NULL);
491 return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));