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 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
56 * Portions Copyright (c) 1996-1999 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.
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.9.2.5.4.6 2006/08/30 23:23:01 marka Exp $";
74 #endif /* LIBC_SCCS and not lint */
76 #include "port_before.h"
78 #include <sys/types.h>
79 #include <sys/param.h>
80 #include <sys/socket.h>
83 #include <netinet/in.h>
84 #include <arpa/inet.h>
85 #include <arpa/nameser.h>
94 #include "port_after.h"
96 /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
99 #include "res_private.h"
101 /* Options. Should all be left alone. */
106 #include <sys/systeminfo.h>
109 static void res_setoptions __P((res_state, const char *, const char *));
112 static const char sort_mask[] = "/&";
113 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
114 static u_int32_t net_mask __P((struct in_addr));
117 #if !defined(isascii) /* XXX - could be a function */
118 # define isascii(c) (!(c & 0200))
122 * Resolver state default settings.
126 * Set up default settings. If the configuration file exist, the values
127 * there will have precedence. Otherwise, the server address is set to
128 * INADDR_ANY and the default domain name comes from the gethostname().
130 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
131 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
132 * since it was noted that INADDR_ANY actually meant ``the first interface
133 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
134 * it had to be "up" in order for you to reach your own name server. It
135 * was later decided that since the recommended practice is to always
136 * install local static routes through 127.0.0.1 for all your network
137 * interfaces, that we could solve this problem without a code change.
139 * The configuration file should always be used, since it is the only way
140 * to specify a default domain. If you are running a server on your local
141 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
142 * in the configuration file.
144 * Return 0 if completes successfully, -1 on error
147 res_ninit(res_state statp) {
148 extern int __res_vinit(res_state, int);
150 return (__res_vinit(statp, 0));
153 /* This function has to be reachable by res_data.c but not publically. */
155 __res_vinit(res_state statp, int preinit) {
157 register char *cp, **pp;
160 int nserv = 0; /* number of nameserver records read from file */
168 union res_sockaddr_union u[2];
170 if (statp->_u._ext.ext != NULL)
174 statp->retrans = RES_TIMEOUT;
175 statp->retry = RES_DFLRETRY;
176 statp->options = RES_DEFAULT;
177 statp->id = res_randomid();
180 memset(u, 0, sizeof(u));
182 u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
184 u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
186 u[nserv].sin.sin_family = AF_INET;
187 u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
189 u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
192 #ifdef HAS_INET6_STRUCTS
194 u[nserv].sin6.sin6_addr = in6addr_loopback;
196 u[nserv].sin6.sin6_addr = in6addr_any;
198 u[nserv].sin6.sin6_family = AF_INET6;
199 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
201 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
212 statp->_u._ext.nscount = 0;
213 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
214 if (statp->_u._ext.ext != NULL) {
215 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
216 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
217 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
218 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
224 res_setservers(statp, u, nserv);
228 * The old libresolv derived the defaultdomain from NIS/NIS+.
229 * We want to keep this behaviour
232 char buf[sizeof(statp->defdname)], *cp;
235 if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 &&
236 (unsigned int)ret <= sizeof(buf)) {
239 cp = strchr(buf, '.');
240 cp = (cp == NULL) ? buf : (cp + 1);
241 if (strlen(cp) >= sizeof(statp->defdname))
243 strcpy(statp->defdname, cp);
246 #endif /* SOLARIS2 */
248 /* Allow user to override the local domain definition */
249 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
250 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
251 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
255 * Set search list to be blank-separated strings
256 * from rest of env value. Permits users of LOCALDOMAIN
257 * to still have a search list, and anyone to set the
258 * one that they want to use as an individual (even more
259 * important now that the rfc1535 stuff restricts searches)
261 cp = statp->defdname;
264 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
265 if (*cp == '\n') /* silly backwards compat */
267 else if (*cp == ' ' || *cp == '\t') {
276 /* null terminate last domain if there are excess */
277 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
283 #define MATCH(line, name) \
284 (!strncmp(line, name, sizeof(name) - 1) && \
285 (line[sizeof(name) - 1] == ' ' || \
286 line[sizeof(name) - 1] == '\t'))
289 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
290 /* read the config file */
291 while (fgets(buf, sizeof(buf), fp) != NULL) {
293 if (*buf == ';' || *buf == '#')
295 /* read default domain name */
296 if (MATCH(buf, "domain")) {
297 if (haveenv) /* skip if have from environ */
299 cp = buf + sizeof("domain") - 1;
300 while (*cp == ' ' || *cp == '\t')
302 if ((*cp == '\0') || (*cp == '\n'))
304 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
305 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
306 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
311 /* set search list */
312 if (MATCH(buf, "search")) {
313 if (haveenv) /* skip if have from environ */
315 cp = buf + sizeof("search") - 1;
316 while (*cp == ' ' || *cp == '\t')
318 if ((*cp == '\0') || (*cp == '\n'))
320 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
321 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
322 if ((cp = strchr(statp->defdname, '\n')) != NULL)
325 * Set search list to be blank-separated strings
328 cp = statp->defdname;
331 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
332 if (*cp == ' ' || *cp == '\t') {
340 /* null terminate last domain if there are excess */
341 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
348 /* read nameservers to query */
349 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
350 struct addrinfo hints, *ai;
351 char sbuf[NI_MAXSERV];
352 const size_t minsiz =
353 sizeof(statp->_u._ext.ext->nsaddrs[0]);
355 cp = buf + sizeof("nameserver") - 1;
356 while (*cp == ' ' || *cp == '\t')
358 cp[strcspn(cp, ";# \t\n")] = '\0';
359 if ((*cp != '\0') && (*cp != '\n')) {
360 memset(&hints, 0, sizeof(hints));
361 hints.ai_family = PF_UNSPEC;
362 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
363 hints.ai_flags = AI_NUMERICHOST;
364 sprintf(sbuf, "%u", NAMESERVER_PORT);
365 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
366 ai->ai_addrlen <= minsiz) {
367 if (statp->_u._ext.ext != NULL) {
368 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
369 ai->ai_addr, ai->ai_addrlen);
371 if (ai->ai_addrlen <=
372 sizeof(statp->nsaddr_list[nserv])) {
373 memcpy(&statp->nsaddr_list[nserv],
374 ai->ai_addr, ai->ai_addrlen);
376 statp->nsaddr_list[nserv].sin_family = 0;
384 if (MATCH(buf, "sortlist")) {
387 cp = buf + sizeof("sortlist") - 1;
388 while (nsort < MAXRESOLVSORT) {
389 while (*cp == ' ' || *cp == '\t')
391 if (*cp == '\0' || *cp == '\n' || *cp == ';')
394 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
395 isascii(*cp) && !isspace((unsigned char)*cp))
399 if (inet_aton(net, &a)) {
400 statp->sort_list[nsort].addr = a;
404 while (*cp && *cp != ';' &&
406 !isspace((unsigned char)*cp))
410 if (inet_aton(net, &a)) {
411 statp->sort_list[nsort].mask = a.s_addr;
413 statp->sort_list[nsort].mask =
414 net_mask(statp->sort_list[nsort].addr);
417 statp->sort_list[nsort].mask =
418 net_mask(statp->sort_list[nsort].addr);
427 if (MATCH(buf, "options")) {
428 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
433 statp->nscount = nserv;
435 statp->nsort = nsort;
440 * Last chance to get a nameserver. This should not normally
443 #ifdef NO_RESOLV_CONF
445 nserv = get_nameservers(statp);
448 if (statp->defdname[0] == 0 &&
449 gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
450 (cp = strchr(buf, '.')) != NULL)
451 strcpy(statp->defdname, cp + 1);
453 /* find components of local domain that might be searched */
454 if (havesearch == 0) {
456 *pp++ = statp->defdname;
460 for (cp = statp->defdname; *cp; cp++)
461 dots += (*cp == '.');
463 cp = statp->defdname;
464 while (pp < statp->dnsrch + MAXDFLSRCH) {
465 if (dots < LOCALDOMAINPARTS)
467 cp = strchr(cp, '.') + 1; /* we know there is one */
473 if (statp->options & RES_DEBUG) {
474 printf(";; res_init()... default dnsrch list:\n");
475 for (pp = statp->dnsrch; *pp; pp++)
476 printf(";;\t%s\n", *pp);
477 printf(";;\t..END..\n");
482 if ((cp = getenv("RES_OPTIONS")) != NULL)
483 res_setoptions(statp, cp, "env");
484 statp->options |= RES_INIT;
489 if (statp->_u._ext.ext != NULL) {
490 free(statp->_u._ext.ext);
491 statp->_u._ext.ext = NULL;
498 res_setoptions(res_state statp, const char *options, const char *source)
500 const char *cp = options;
502 struct __res_state_ext *ext = statp->_u._ext.ext;
505 if (statp->options & RES_DEBUG)
506 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
510 /* skip leading and inner runs of spaces */
511 while (*cp == ' ' || *cp == '\t')
513 /* search for and process individual options */
514 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
515 i = atoi(cp + sizeof("ndots:") - 1);
516 if (i <= RES_MAXNDOTS)
519 statp->ndots = RES_MAXNDOTS;
521 if (statp->options & RES_DEBUG)
522 printf(";;\tndots=%d\n", statp->ndots);
524 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
525 i = atoi(cp + sizeof("timeout:") - 1);
526 if (i <= RES_MAXRETRANS)
529 statp->retrans = RES_MAXRETRANS;
531 if (statp->options & RES_DEBUG)
532 printf(";;\ttimeout=%d\n", statp->retrans);
535 } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) {
537 * For backward compatibility, 'retrans' is
538 * supported as an alias for 'timeout', though
539 * without an imposed maximum.
541 statp->retrans = atoi(cp + sizeof("retrans:") - 1);
542 } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){
544 * For backward compatibility, 'retry' is
545 * supported as an alias for 'attempts', though
546 * without an imposed maximum.
548 statp->retry = atoi(cp + sizeof("retry:") - 1);
549 #endif /* SOLARIS2 */
550 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
551 i = atoi(cp + sizeof("attempts:") - 1);
552 if (i <= RES_MAXRETRY)
555 statp->retry = RES_MAXRETRY;
557 if (statp->options & RES_DEBUG)
558 printf(";;\tattempts=%d\n", statp->retry);
560 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
562 if (!(statp->options & RES_DEBUG)) {
563 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
565 statp->options |= RES_DEBUG;
567 printf(";;\tdebug\n");
569 } else if (!strncmp(cp, "no_tld_query",
570 sizeof("no_tld_query") - 1) ||
571 !strncmp(cp, "no-tld-query",
572 sizeof("no-tld-query") - 1)) {
573 statp->options |= RES_NOTLDQUERY;
574 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
575 statp->options |= RES_USE_INET6;
576 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
577 statp->options |= RES_ROTATE;
578 } else if (!strncmp(cp, "no-check-names",
579 sizeof("no-check-names") - 1)) {
580 statp->options |= RES_NOCHECKNAME;
583 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
584 statp->options |= RES_USE_EDNS0;
587 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
588 statp->options |= RES_USE_DNAME;
590 else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
593 cp += sizeof("nibble:") - 1;
594 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
595 strncpy(ext->nsuffix, cp, i);
596 ext->nsuffix[i] = '\0';
598 else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
601 cp += sizeof("nibble2:") - 1;
602 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
603 strncpy(ext->nsuffix2, cp, i);
604 ext->nsuffix2[i] = '\0';
606 else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
607 cp += sizeof("v6revmode:") - 1;
608 /* "nibble" and "bitstring" used to be valid */
609 if (!strncmp(cp, "single", sizeof("single") - 1)) {
610 statp->options |= RES_NO_NIBBLE2;
611 } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
617 /* XXX - print a warning here? */
620 /* skip to next run of spaces */
621 while (*cp && *cp != ' ' && *cp != '\t')
627 /* XXX - should really support CIDR which means explicit masks always. */
629 net_mask(in) /* XXX - should really use system's version of this */
632 register u_int32_t i = ntohl(in.s_addr);
635 return (htonl(IN_CLASSA_NET));
636 else if (IN_CLASSB(i))
637 return (htonl(IN_CLASSB_NET));
638 return (htonl(IN_CLASSC_NET));
646 gettimeofday(&now, NULL);
647 return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
651 * This routine is for closing the socket if a virtual circuit is used and
652 * the program wants to close it. This provides support for endhostent()
653 * which expects to close the socket.
655 * This routine is not expected to be user visible.
658 res_nclose(res_state statp) {
661 if (statp->_vcsock >= 0) {
662 (void) close(statp->_vcsock);
664 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
666 for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
667 if (statp->_u._ext.nssocks[ns] != -1) {
668 (void) close(statp->_u._ext.nssocks[ns]);
669 statp->_u._ext.nssocks[ns] = -1;
675 res_ndestroy(res_state statp) {
677 if (statp->_u._ext.ext != NULL)
678 free(statp->_u._ext.ext);
679 statp->options &= ~RES_INIT;
680 statp->_u._ext.ext = NULL;
684 res_get_nibblesuffix(res_state statp) {
685 if (statp->_u._ext.ext)
686 return (statp->_u._ext.ext->nsuffix);
691 res_get_nibblesuffix2(res_state statp) {
692 if (statp->_u._ext.ext)
693 return (statp->_u._ext.ext->nsuffix2);
698 res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) {
702 /* close open servers */
705 /* cause rtt times to be forgotten */
706 statp->_u._ext.nscount = 0;
709 for (i = 0; i < cnt && nserv < MAXNS; i++) {
710 switch (set->sin.sin_family) {
712 size = sizeof(set->sin);
713 if (statp->_u._ext.ext)
714 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
716 if (size <= sizeof(statp->nsaddr_list[nserv]))
717 memcpy(&statp->nsaddr_list[nserv],
720 statp->nsaddr_list[nserv].sin_family = 0;
724 #ifdef HAS_INET6_STRUCTS
726 size = sizeof(set->sin6);
727 if (statp->_u._ext.ext)
728 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
730 if (size <= sizeof(statp->nsaddr_list[nserv]))
731 memcpy(&statp->nsaddr_list[nserv],
734 statp->nsaddr_list[nserv].sin_family = 0;
744 statp->nscount = nserv;
749 res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) {
754 for (i = 0; i < statp->nscount && i < cnt; i++) {
755 if (statp->_u._ext.ext)
756 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
758 family = statp->nsaddr_list[i].sin_family;
762 size = sizeof(set->sin);
763 if (statp->_u._ext.ext)
765 &statp->_u._ext.ext->nsaddrs[i],
768 memcpy(&set->sin, &statp->nsaddr_list[i],
772 #ifdef HAS_INET6_STRUCTS
774 size = sizeof(set->sin6);
775 if (statp->_u._ext.ext)
777 &statp->_u._ext.ext->nsaddrs[i],
780 memcpy(&set->sin6, &statp->nsaddr_list[i],
786 set->sin.sin_family = 0;
791 return (statp->nscount);