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) 1996-1999 by Internet Software Consortium.
57 * Permission to use, copy, modify, and distribute this software for any
58 * purpose with or without fee is hereby granted, provided that the above
59 * copyright notice and this permission notice appear in all copies.
61 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
62 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
63 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
64 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
65 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
66 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
67 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
71 #if defined(LIBC_SCCS) && !defined(lint)
72 static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
73 static const char rcsid[] = "$Id: res_init.c,v 1.4.2.1 2001/06/22 02:57:54 mellon Exp $";
74 #endif /* LIBC_SCCS and not lint */
76 #include <sys/types.h>
77 #include <sys/param.h>
78 #include <sys/socket.h>
81 #include <netinet/in.h>
82 #include <arpa/inet.h>
90 #include "minires/minires.h"
91 #include "arpa/nameser.h"
93 /* Options. Should all be left alone. */
98 static void res_setoptions (res_state, const char *, const char *);
101 static const char sort_mask[] = "/&";
102 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
103 static u_int32_t net_mask (struct in_addr);
106 #if !defined(isascii) /* XXX - could be a function */
107 # define isascii(c) (!(c & 0200))
111 * Resolver state default settings.
115 * Set up default settings. If the configuration file exist, the values
116 * there will have precedence. Otherwise, the server address is set to
117 * INADDR_ANY and the default domain name comes from the gethostname().
119 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
120 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
121 * since it was noted that INADDR_ANY actually meant ``the first interface
122 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
123 * it had to be "up" in order for you to reach your own name server. It
124 * was later decided that since the recommended practice is to always
125 * install local static routes through 127.0.0.1 for all your network
126 * interfaces, that we could solve this problem without a code change.
128 * The configuration file should always be used, since it is the only way
129 * to specify a default domain. If you are running a server on your local
130 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
131 * in the configuration file.
133 * Return 0 if completes successfully, -1 on error
135 extern int minires_vinit(res_state, int);
137 #if defined (TRACING)
138 u_int trace_mr_res_randomid(u_int);
142 res_ninit(res_state statp) {
144 return (minires_vinit(statp, 0));
147 /* This function has to be reachable by res_data.c but not publically. */
149 minires_vinit(res_state statp, int preinit) {
151 register char *cp, **pp;
154 int nserv = 0; /* number of nameserver records read from file */
166 statp->retrans = RES_TIMEOUT;
167 statp->retry = RES_DFLRETRY;
168 statp->options = RES_DEFAULT;
169 statp->id = res_randomid();
170 #if defined (TRACING)
171 statp->id = trace_mr_res_randomid (statp -> id);
176 statp->nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
178 statp->nsaddr.sin_addr.s_addr = INADDR_ANY;
180 statp->nsaddr.sin_family = AF_INET;
181 statp->nsaddr.sin_port = htons(NAMESERVER_PORT);
190 /* Allow user to override the local domain definition */
191 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
192 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
193 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
197 * Set search list to be blank-separated strings
198 * from rest of env value. Permits users of LOCALDOMAIN
199 * to still have a search list, and anyone to set the
200 * one that they want to use as an individual (even more
201 * important now that the rfc1535 stuff restricts searches)
203 cp = statp->defdname;
206 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
207 if (*cp == '\n') /* silly backwards compat */
209 else if (*cp == ' ' || *cp == '\t') {
218 /* null terminate last domain if there are excess */
219 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
225 #define MATCH(line, name) \
226 (!strncmp(line, name, sizeof(name) - 1) && \
227 (line[sizeof(name) - 1] == ' ' || \
228 line[sizeof(name) - 1] == '\t'))
230 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
231 /* read the config file */
232 while (fgets(buf, sizeof(buf), fp) != NULL) {
234 if (*buf == ';' || *buf == '#')
236 /* read default domain name */
237 if (MATCH(buf, "domain")) {
238 if (haveenv) /* skip if have from environ */
240 cp = buf + sizeof("domain") - 1;
241 while (*cp == ' ' || *cp == '\t')
243 if ((*cp == '\0') || (*cp == '\n'))
245 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
246 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
247 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
252 /* set search list */
253 if (MATCH(buf, "search")) {
254 if (haveenv) /* skip if have from environ */
256 cp = buf + sizeof("search") - 1;
257 while (*cp == ' ' || *cp == '\t')
259 if ((*cp == '\0') || (*cp == '\n'))
261 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
262 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
263 if ((cp = strchr(statp->defdname, '\n')) != NULL)
266 * Set search list to be blank-separated strings
269 cp = statp->defdname;
272 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
273 if (*cp == ' ' || *cp == '\t') {
281 /* null terminate last domain if there are excess */
282 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
289 /* read nameservers to query */
290 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
293 cp = buf + sizeof("nameserver") - 1;
294 while (*cp == ' ' || *cp == '\t')
296 if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) {
297 statp->nsaddr_list[nserv].sin_addr = a;
298 statp->nsaddr_list[nserv].sin_family = AF_INET;
299 statp->nsaddr_list[nserv].sin_port =
300 htons(NAMESERVER_PORT);
306 if (MATCH(buf, "sortlist")) {
309 cp = buf + sizeof("sortlist") - 1;
310 while (nsort < MAXRESOLVSORT) {
311 while (*cp == ' ' || *cp == '\t')
313 if (*cp == '\0' || *cp == '\n' || *cp == ';')
316 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
317 isascii(*cp) && !isspace(*cp))
321 if (inet_aton(net, &a)) {
322 statp->sort_list[nsort].addr = a;
326 while (*cp && *cp != ';' &&
327 isascii(*cp) && !isspace(*cp))
331 if (inet_aton(net, &a)) {
332 statp->sort_list[nsort].mask = a.s_addr;
334 statp->sort_list[nsort].mask =
335 net_mask(statp->sort_list[nsort].addr);
338 statp->sort_list[nsort].mask =
339 net_mask(statp->sort_list[nsort].addr);
348 if (MATCH(buf, "options")) {
349 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
354 statp->nscount = nserv;
356 statp->nsort = nsort;
360 if (statp->defdname[0] == 0 &&
361 gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
362 (cp = strchr(buf, '.')) != NULL)
363 strcpy(statp->defdname, cp + 1);
365 /* find components of local domain that might be searched */
366 if (havesearch == 0) {
368 *pp++ = statp->defdname;
373 for (cp = statp->defdname; *cp; cp++)
374 dots += (*cp == '.');
376 cp = statp->defdname;
377 while (pp < statp->dnsrch + MAXDFLSRCH) {
378 if (dots < LOCALDOMAINPARTS)
380 cp = strchr(cp, '.') + 1; /* we know there is one */
386 if (statp->options & RES_DEBUG) {
387 printf(";; res_init()... default dnsrch list:\n");
388 for (pp = statp->dnsrch; *pp; pp++)
389 printf(";;\t%s\n", *pp);
390 printf(";;\t..END..\n");
393 #endif /* !RFC1535 */
396 if ((cp = getenv("RES_OPTIONS")) != NULL)
397 res_setoptions(statp, cp, "env");
398 statp->options |= RES_INIT;
403 res_setoptions(res_state statp, const char *options, const char *source) {
404 const char *cp = options;
408 if (statp->options & RES_DEBUG)
409 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
413 /* skip leading and inner runs of spaces */
414 while (*cp == ' ' || *cp == '\t')
416 /* search for and process individual options */
417 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
418 i = atoi(cp + sizeof("ndots:") - 1);
419 if (i <= RES_MAXNDOTS)
422 statp->ndots = RES_MAXNDOTS;
424 if (statp->options & RES_DEBUG)
425 printf(";;\tndots=%d\n", statp->ndots);
427 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
428 i = atoi(cp + sizeof("timeout:") - 1);
429 if (i <= RES_MAXRETRANS)
432 statp->retrans = RES_MAXRETRANS;
433 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
434 i = atoi(cp + sizeof("attempts:") - 1);
435 if (i <= RES_MAXRETRY)
438 statp->retry = RES_MAXRETRY;
439 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
441 if (!(statp->options & RES_DEBUG)) {
442 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
444 statp->options |= RES_DEBUG;
446 printf(";;\tdebug\n");
448 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
449 statp->options |= RES_USE_INET6;
450 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
451 statp->options |= RES_ROTATE;
452 } else if (!strncmp(cp, "no-check-names",
453 sizeof("no-check-names") - 1)) {
454 statp->options |= RES_NOCHECKNAME;
456 /* XXX - print a warning here? */
458 /* skip to next run of spaces */
459 while (*cp && *cp != ' ' && *cp != '\t')
465 /* XXX - should really support CIDR which means explicit masks always. */
467 net_mask(in) /* XXX - should really use system's version of this */
470 register u_int32_t i = ntohl(in.s_addr);
473 return (htonl(IN_CLASSA_NET));
474 else if (IN_CLASSB(i))
475 return (htonl(IN_CLASSB_NET));
476 return (htonl(IN_CLASSC_NET));
484 gettimeofday(&now, NULL);
485 return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));