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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies, and that
36 * the name of Digital Equipment Corporation not be used in advertising or
37 * publicity pertaining to distribution of the document or software without
38 * specific, written prior permission.
40 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
43 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
52 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
54 * Permission to use, copy, modify, and distribute this software for any
55 * purpose with or without fee is hereby granted, provided that the above
56 * copyright notice and this permission notice appear in all copies.
58 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
59 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
60 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
61 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
62 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
63 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
64 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
67 #if defined(LIBC_SCCS) && !defined(lint)
68 static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
69 static const char rcsid[] = "$Id: res_init.c,v 1.23 2007/07/09 01:43:23 marka Exp $";
70 #endif /* LIBC_SCCS and not lint */
72 #include "port_before.h"
75 #include "namespace.h"
77 #include <sys/types.h>
78 #include <sys/param.h>
79 #include <sys/socket.h>
82 #include <netinet/in.h>
83 #include <arpa/inet.h>
84 #include <arpa/nameser.h>
93 #include "un-namespace.h"
96 #include "port_after.h"
98 /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
101 #include "res_private.h"
103 /*% Options. Should all be left alone. */
108 #include <sys/systeminfo.h>
111 static void res_setoptions __P((res_state, const char *, const char *));
114 static const char sort_mask[] = "/&";
115 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
116 static u_int32_t net_mask __P((struct in_addr));
119 #if !defined(isascii) /*%< XXX - could be a function */
120 # define isascii(c) (!(c & 0200))
124 * Resolver state default settings.
128 * Set up default settings. If the configuration file exist, the values
129 * there will have precedence. Otherwise, the server address is set to
130 * INADDR_ANY and the default domain name comes from the gethostname().
132 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
133 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
134 * since it was noted that INADDR_ANY actually meant ``the first interface
135 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
136 * it had to be "up" in order for you to reach your own name server. It
137 * was later decided that since the recommended practice is to always
138 * install local static routes through 127.0.0.1 for all your network
139 * interfaces, that we could solve this problem without a code change.
141 * The configuration file should always be used, since it is the only way
142 * to specify a default domain. If you are running a server on your local
143 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
144 * in the configuration file.
146 * Return 0 if completes successfully, -1 on error
149 res_ninit(res_state statp) {
150 extern int __res_vinit(res_state, int);
152 return (__res_vinit(statp, 0));
155 /*% This function has to be reachable by res_data.c but not publically. */
157 __res_vinit(res_state statp, int preinit) {
162 int nserv = 0; /*%< number of nameserver records read from file */
170 union res_sockaddr_union u[2];
173 RES_SET_H_ERRNO(statp, 0);
174 if (statp->_u._ext.ext != NULL)
178 statp->retrans = RES_TIMEOUT;
179 statp->retry = RES_DFLRETRY;
180 statp->options = RES_DEFAULT;
181 statp->id = res_randomid();
184 memset(u, 0, sizeof(u));
186 u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
188 u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
190 u[nserv].sin.sin_family = AF_INET;
191 u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
193 u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
196 #ifdef HAS_INET6_STRUCTS
198 u[nserv].sin6.sin6_addr = in6addr_loopback;
200 u[nserv].sin6.sin6_addr = in6addr_any;
202 u[nserv].sin6.sin6_family = AF_INET6;
203 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
205 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
216 statp->_u._ext.nscount = 0;
217 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
218 if (statp->_u._ext.ext != NULL) {
219 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
220 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
221 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
222 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
225 * Historically res_init() rarely, if at all, failed.
226 * Examples and applications exist which do not check
227 * our return code. Furthermore several applications
228 * simply call us to get the systems domainname. So
229 * rather then immediately fail here we store the
230 * failure, which is returned later, in h_errno. And
231 * prevent the collection of 'nameserver' information
232 * by setting maxns to 0. Thus applications that fail
233 * to check our return code wont be able to make
236 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
242 res_setservers(statp, u, nserv);
246 * The old libresolv derived the defaultdomain from NIS/NIS+.
247 * We want to keep this behaviour
250 char buf[sizeof(statp->defdname)], *cp;
253 if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 &&
254 (unsigned int)ret <= sizeof(buf)) {
257 cp = strchr(buf, '.');
258 cp = (cp == NULL) ? buf : (cp + 1);
259 strncpy(statp->defdname, cp,
260 sizeof(statp->defdname) - 1);
261 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
264 #endif /* SOLARIS2 */
266 /* Allow user to override the local domain definition */
267 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
268 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
269 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
273 * Set search list to be blank-separated strings
274 * from rest of env value. Permits users of LOCALDOMAIN
275 * to still have a search list, and anyone to set the
276 * one that they want to use as an individual (even more
277 * important now that the rfc1535 stuff restricts searches)
279 cp = statp->defdname;
282 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
283 if (*cp == '\n') /*%< silly backwards compat */
285 else if (*cp == ' ' || *cp == '\t') {
294 /* null terminate last domain if there are excess */
295 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
301 #define MATCH(line, name) \
302 (!strncmp(line, name, sizeof(name) - 1) && \
303 (line[sizeof(name) - 1] == ' ' || \
304 line[sizeof(name) - 1] == '\t'))
307 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
308 /* read the config file */
309 while (fgets(buf, sizeof(buf), fp) != NULL) {
311 if (*buf == ';' || *buf == '#')
313 /* read default domain name */
314 if (MATCH(buf, "domain")) {
315 if (haveenv) /*%< skip if have from environ */
317 cp = buf + sizeof("domain") - 1;
318 while (*cp == ' ' || *cp == '\t')
320 if ((*cp == '\0') || (*cp == '\n'))
322 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
323 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
324 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
329 /* set search list */
330 if (MATCH(buf, "search")) {
331 if (haveenv) /*%< skip if have from environ */
333 cp = buf + sizeof("search") - 1;
334 while (*cp == ' ' || *cp == '\t')
336 if ((*cp == '\0') || (*cp == '\n'))
338 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
339 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
340 if ((cp = strchr(statp->defdname, '\n')) != NULL)
343 * Set search list to be blank-separated strings
346 cp = statp->defdname;
349 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
350 if (*cp == ' ' || *cp == '\t') {
358 /* null terminate last domain if there are excess */
359 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
366 /* read nameservers to query */
367 if (MATCH(buf, "nameserver") && nserv < maxns) {
368 struct addrinfo hints, *ai;
369 char sbuf[NI_MAXSERV];
370 const size_t minsiz =
371 sizeof(statp->_u._ext.ext->nsaddrs[0]);
373 cp = buf + sizeof("nameserver") - 1;
374 while (*cp == ' ' || *cp == '\t')
376 cp[strcspn(cp, ";# \t\n")] = '\0';
377 if ((*cp != '\0') && (*cp != '\n')) {
378 memset(&hints, 0, sizeof(hints));
379 hints.ai_family = PF_UNSPEC;
380 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
381 hints.ai_flags = AI_NUMERICHOST;
382 sprintf(sbuf, "%u", NAMESERVER_PORT);
383 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
384 ai->ai_addrlen <= minsiz) {
385 if (statp->_u._ext.ext != NULL) {
386 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
387 ai->ai_addr, ai->ai_addrlen);
389 if (ai->ai_addrlen <=
390 sizeof(statp->nsaddr_list[nserv])) {
391 memcpy(&statp->nsaddr_list[nserv],
392 ai->ai_addr, ai->ai_addrlen);
394 statp->nsaddr_list[nserv].sin_family = 0;
402 if (MATCH(buf, "sortlist")) {
405 cp = buf + sizeof("sortlist") - 1;
406 while (nsort < MAXRESOLVSORT) {
407 while (*cp == ' ' || *cp == '\t')
409 if (*cp == '\0' || *cp == '\n' || *cp == ';')
412 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
413 isascii(*cp) && !isspace((unsigned char)*cp))
417 if (inet_aton(net, &a)) {
418 statp->sort_list[nsort].addr = a;
422 while (*cp && *cp != ';' &&
424 !isspace((unsigned char)*cp))
428 if (inet_aton(net, &a)) {
429 statp->sort_list[nsort].mask = a.s_addr;
431 statp->sort_list[nsort].mask =
432 net_mask(statp->sort_list[nsort].addr);
435 statp->sort_list[nsort].mask =
436 net_mask(statp->sort_list[nsort].addr);
445 if (MATCH(buf, "options"))
446 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
449 statp->nscount = nserv;
451 statp->nsort = nsort;
456 * Last chance to get a nameserver. This should not normally
459 #ifdef NO_RESOLV_CONF
461 nserv = get_nameservers(statp);
464 if (statp->defdname[0] == 0 &&
465 gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
466 (cp = strchr(buf, '.')) != NULL)
467 strcpy(statp->defdname, cp + 1);
469 /* find components of local domain that might be searched */
470 if (havesearch == 0) {
472 *pp++ = statp->defdname;
476 for (cp = statp->defdname; *cp; cp++)
477 dots += (*cp == '.');
479 cp = statp->defdname;
480 while (pp < statp->dnsrch + MAXDFLSRCH) {
481 if (dots < LOCALDOMAINPARTS)
483 cp = strchr(cp, '.') + 1; /*%< we know there is one */
489 if (statp->options & RES_DEBUG) {
490 printf(";; res_init()... default dnsrch list:\n");
491 for (pp = statp->dnsrch; *pp; pp++)
492 printf(";;\t%s\n", *pp);
493 printf(";;\t..END..\n");
498 if ((cp = getenv("RES_OPTIONS")) != NULL)
499 res_setoptions(statp, cp, "env");
500 statp->options |= RES_INIT;
501 return (statp->res_h_errno);
505 res_setoptions(res_state statp, const char *options, const char *source)
507 const char *cp = options;
510 struct __res_state_ext *ext = statp->_u._ext.ext;
514 if (statp->options & RES_DEBUG)
515 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
519 /* skip leading and inner runs of spaces */
520 while (*cp == ' ' || *cp == '\t')
522 /* search for and process individual options */
523 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
524 i = atoi(cp + sizeof("ndots:") - 1);
525 if (i <= RES_MAXNDOTS)
528 statp->ndots = RES_MAXNDOTS;
530 if (statp->options & RES_DEBUG)
531 printf(";;\tndots=%d\n", statp->ndots);
533 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
534 i = atoi(cp + sizeof("timeout:") - 1);
535 if (i <= RES_MAXRETRANS)
538 statp->retrans = RES_MAXRETRANS;
540 if (statp->options & RES_DEBUG)
541 printf(";;\ttimeout=%d\n", statp->retrans);
544 } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) {
546 * For backward compatibility, 'retrans' is
547 * supported as an alias for 'timeout', though
548 * without an imposed maximum.
550 statp->retrans = atoi(cp + sizeof("retrans:") - 1);
551 } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){
553 * For backward compatibility, 'retry' is
554 * supported as an alias for 'attempts', though
555 * without an imposed maximum.
557 statp->retry = atoi(cp + sizeof("retry:") - 1);
558 #endif /* SOLARIS2 */
559 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
560 i = atoi(cp + sizeof("attempts:") - 1);
561 if (i <= RES_MAXRETRY)
564 statp->retry = RES_MAXRETRY;
566 if (statp->options & RES_DEBUG)
567 printf(";;\tattempts=%d\n", statp->retry);
569 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
571 if (!(statp->options & RES_DEBUG)) {
572 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
574 statp->options |= RES_DEBUG;
576 printf(";;\tdebug\n");
578 } else if (!strncmp(cp, "no_tld_query",
579 sizeof("no_tld_query") - 1) ||
580 !strncmp(cp, "no-tld-query",
581 sizeof("no-tld-query") - 1)) {
582 statp->options |= RES_NOTLDQUERY;
583 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
584 statp->options |= RES_USE_INET6;
585 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
586 statp->options |= RES_ROTATE;
587 } else if (!strncmp(cp, "no-check-names",
588 sizeof("no-check-names") - 1)) {
589 statp->options |= RES_NOCHECKNAME;
592 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
593 statp->options |= RES_USE_EDNS0;
597 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
598 statp->options |= RES_USE_DNAME;
600 else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
603 cp += sizeof("nibble:") - 1;
604 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
605 strncpy(ext->nsuffix, cp, i);
606 ext->nsuffix[i] = '\0';
608 else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
611 cp += sizeof("nibble2:") - 1;
612 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
613 strncpy(ext->nsuffix2, cp, i);
614 ext->nsuffix2[i] = '\0';
616 else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
617 cp += sizeof("v6revmode:") - 1;
618 /* "nibble" and "bitstring" used to be valid */
619 if (!strncmp(cp, "single", sizeof("single") - 1)) {
620 statp->options |= RES_NO_NIBBLE2;
621 } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
628 /* XXX - print a warning here? */
633 /* skip to next run of spaces */
634 while (*cp && *cp != ' ' && *cp != '\t')
640 /* XXX - should really support CIDR which means explicit masks always. */
642 net_mask(struct in_addr in) /*!< XXX - should really use system's version of this */
644 u_int32_t i = ntohl(in.s_addr);
647 return (htonl(IN_CLASSA_NET));
648 else if (IN_CLASSB(i))
649 return (htonl(IN_CLASSB_NET));
650 return (htonl(IN_CLASSC_NET));
658 gettimeofday(&now, NULL);
659 return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
663 * This routine is for closing the socket if a virtual circuit is used and
664 * the program wants to close it. This provides support for endhostent()
665 * which expects to close the socket.
667 * This routine is not expected to be user visible.
670 res_nclose(res_state statp) {
673 if (statp->_vcsock >= 0) {
675 (void) close(statp->_vcsock);
677 _close(statp->_vcsock);
680 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
682 for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
683 if (statp->_u._ext.nssocks[ns] != -1) {
685 (void) close(statp->_u._ext.nssocks[ns]);
687 _close(statp->_u._ext.nssocks[ns]);
689 statp->_u._ext.nssocks[ns] = -1;
695 res_ndestroy(res_state statp) {
697 if (statp->_u._ext.ext != NULL)
698 free(statp->_u._ext.ext);
699 statp->options &= ~RES_INIT;
700 statp->_u._ext.ext = NULL;
705 res_get_nibblesuffix(res_state statp) {
706 if (statp->_u._ext.ext)
707 return (statp->_u._ext.ext->nsuffix);
712 res_get_nibblesuffix2(res_state statp) {
713 if (statp->_u._ext.ext)
714 return (statp->_u._ext.ext->nsuffix2);
720 res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) {
724 /* close open servers */
727 /* cause rtt times to be forgotten */
728 statp->_u._ext.nscount = 0;
731 for (i = 0; i < cnt && nserv < MAXNS; i++) {
732 switch (set->sin.sin_family) {
734 size = sizeof(set->sin);
735 if (statp->_u._ext.ext)
736 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
738 if (size <= sizeof(statp->nsaddr_list[nserv]))
739 memcpy(&statp->nsaddr_list[nserv],
742 statp->nsaddr_list[nserv].sin_family = 0;
746 #ifdef HAS_INET6_STRUCTS
748 size = sizeof(set->sin6);
749 if (statp->_u._ext.ext)
750 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
752 if (size <= sizeof(statp->nsaddr_list[nserv]))
753 memcpy(&statp->nsaddr_list[nserv],
756 statp->nsaddr_list[nserv].sin_family = 0;
766 statp->nscount = nserv;
771 res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) {
776 for (i = 0; i < statp->nscount && i < cnt; i++) {
777 if (statp->_u._ext.ext)
778 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
780 family = statp->nsaddr_list[i].sin_family;
784 size = sizeof(set->sin);
785 if (statp->_u._ext.ext)
787 &statp->_u._ext.ext->nsaddrs[i],
790 memcpy(&set->sin, &statp->nsaddr_list[i],
794 #ifdef HAS_INET6_STRUCTS
796 size = sizeof(set->sin6);
797 if (statp->_u._ext.ext)
799 &statp->_u._ext.ext->nsaddrs[i],
802 memcpy(&set->sin6, &statp->nsaddr_list[i],
808 set->sin.sin_family = 0;
813 return (statp->nscount);