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 * 4. 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 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
53 * Permission to use, copy, modify, and distribute this software for any
54 * purpose with or without fee is hereby granted, provided that the above
55 * copyright notice and this permission notice appear in all copies.
57 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
58 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
60 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
61 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
62 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
63 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
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
[] = "$BINDId: res_init.c,v 8.16 2000/05/09 07:10:12 vixie Exp $";
70 #endif /* LIBC_SCCS and not lint */
76 #include <stdio_ext.h>
80 #include <arpa/inet.h>
81 #include <arpa/nameser.h>
83 #include <netinet/in.h>
84 #include <sys/param.h>
85 #include <sys/socket.h>
87 #include <sys/types.h>
89 #include <not-cancel.h>
91 /* Options. Should all be left alone. */
96 static void res_setoptions (res_state
, const char *, const char *)
100 static const char sort_mask_chars
[] = "/&";
101 #define ISSORTMASK(ch) (strchr(sort_mask_chars, ch) != NULL)
102 static u_int32_t
net_mask (struct in_addr
) __THROW
;
105 #if !defined(isascii) /* XXX - could be a function */
106 # define isascii(c) (!(c & 0200))
110 unsigned long long int __res_initstamp attribute_hidden
;
114 * Resolver state default settings.
118 * Set up default settings. If the configuration file exist, the values
119 * there will have precedence. Otherwise, the server address is set to
120 * INADDR_ANY and the default domain name comes from the gethostname().
122 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
123 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
124 * since it was noted that INADDR_ANY actually meant ``the first interface
125 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
126 * it had to be "up" in order for you to reach your own name server. It
127 * was later decided that since the recommended practice is to always
128 * install local static routes through 127.0.0.1 for all your network
129 * interfaces, that we could solve this problem without a code change.
131 * The configuration file should always be used, since it is the only way
132 * to specify a default domain. If you are running a server on your local
133 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
134 * in the configuration file.
136 * Return 0 if completes successfully, -1 on error
139 res_ninit(res_state statp
) {
140 extern int __res_vinit(res_state
, int);
142 return (__res_vinit(statp
, 0));
145 libc_hidden_def (__res_ninit
)
148 /* This function has to be reachable by res_data.c but not publically. */
150 __res_vinit(res_state statp
, int preinit
) {
152 register char *cp
, **pp
;
155 int nserv
= 0; /* number of nameserver records read from file */
157 int nservall
= 0; /* number of NS records read, nserv IPv4 only */
169 statp
->_u
._ext
.initstamp
= __res_initstamp
;
173 statp
->retrans
= RES_TIMEOUT
;
174 statp
->retry
= RES_DFLRETRY
;
175 statp
->options
= RES_DEFAULT
;
176 statp
->id
= res_randomid();
180 statp
->nsaddr
.sin_addr
= inet_makeaddr(IN_LOOPBACKNET
, 1);
182 statp
->nsaddr
.sin_addr
.s_addr
= INADDR_ANY
;
184 statp
->nsaddr
.sin_family
= AF_INET
;
185 statp
->nsaddr
.sin_port
= htons(NAMESERVER_PORT
);
193 statp
->_u
._ext
.nsinit
= 0;
194 statp
->_u
._ext
.nscount
= 0;
196 statp
->_u
._ext
.nscount6
= 0;
197 for (n
= 0; n
< MAXNS
; n
++) {
198 statp
->_u
._ext
.nsaddrs
[n
] = NULL
;
199 statp
->_u
._ext
.nsmap
[n
] = MAXNS
;
203 /* Allow user to override the local domain definition */
204 if ((cp
= getenv("LOCALDOMAIN")) != NULL
) {
205 (void)strncpy(statp
->defdname
, cp
, sizeof(statp
->defdname
) - 1);
206 statp
->defdname
[sizeof(statp
->defdname
) - 1] = '\0';
210 * Set search list to be blank-separated strings
211 * from rest of env value. Permits users of LOCALDOMAIN
212 * to still have a search list, and anyone to set the
213 * one that they want to use as an individual (even more
214 * important now that the rfc1535 stuff restricts searches)
216 cp
= statp
->defdname
;
219 for (n
= 0; *cp
&& pp
< statp
->dnsrch
+ MAXDNSRCH
; cp
++) {
220 if (*cp
== '\n') /* silly backwards compat */
222 else if (*cp
== ' ' || *cp
== '\t') {
231 /* null terminate last domain if there are excess */
232 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t' && *cp
!= '\n')
238 #define MATCH(line, name) \
239 (!strncmp(line, name, sizeof(name) - 1) && \
240 (line[sizeof(name) - 1] == ' ' || \
241 line[sizeof(name) - 1] == '\t'))
243 if ((fp
= fopen(_PATH_RESCONF
, "rc")) != NULL
) {
244 /* No threads use this stream. */
245 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
246 /* read the config file */
247 while (fgets_unlocked(buf
, sizeof(buf
), fp
) != NULL
) {
249 if (*buf
== ';' || *buf
== '#')
251 /* read default domain name */
252 if (MATCH(buf
, "domain")) {
253 if (haveenv
) /* skip if have from environ */
255 cp
= buf
+ sizeof("domain") - 1;
256 while (*cp
== ' ' || *cp
== '\t')
258 if ((*cp
== '\0') || (*cp
== '\n'))
260 strncpy(statp
->defdname
, cp
, sizeof(statp
->defdname
) - 1);
261 statp
->defdname
[sizeof(statp
->defdname
) - 1] = '\0';
262 if ((cp
= strpbrk(statp
->defdname
, " \t\n")) != NULL
)
267 /* set search list */
268 if (MATCH(buf
, "search")) {
269 if (haveenv
) /* skip if have from environ */
271 cp
= buf
+ sizeof("search") - 1;
272 while (*cp
== ' ' || *cp
== '\t')
274 if ((*cp
== '\0') || (*cp
== '\n'))
276 strncpy(statp
->defdname
, cp
, sizeof(statp
->defdname
) - 1);
277 statp
->defdname
[sizeof(statp
->defdname
) - 1] = '\0';
278 if ((cp
= strchr(statp
->defdname
, '\n')) != NULL
)
281 * Set search list to be blank-separated strings
284 cp
= statp
->defdname
;
287 for (n
= 0; *cp
&& pp
< statp
->dnsrch
+ MAXDNSRCH
; cp
++) {
288 if (*cp
== ' ' || *cp
== '\t') {
296 /* null terminate last domain if there are excess */
297 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t')
304 /* read nameservers to query */
306 if (MATCH(buf
, "nameserver") && nservall
< MAXNS
) {
308 if (MATCH(buf
, "nameserver") && nserv
< MAXNS
) {
312 cp
= buf
+ sizeof("nameserver") - 1;
313 while (*cp
== ' ' || *cp
== '\t')
315 if ((*cp
!= '\0') && (*cp
!= '\n')
316 && __inet_aton(cp
, &a
)) {
317 statp
->nsaddr_list
[nserv
].sin_addr
= a
;
318 statp
->nsaddr_list
[nserv
].sin_family
= AF_INET
;
319 statp
->nsaddr_list
[nserv
].sin_port
=
320 htons(NAMESERVER_PORT
);
328 if ((el
= strchr(cp
, '\n')) != NULL
)
330 if ((el
= strchr(cp
, SCOPE_DELIMITER
)) != NULL
)
333 (inet_pton(AF_INET6
, cp
, &a6
) > 0)) {
334 struct sockaddr_in6
*sa6
;
336 sa6
= malloc(sizeof(*sa6
));
338 sa6
->sin6_family
= AF_INET6
;
339 sa6
->sin6_port
= htons(NAMESERVER_PORT
);
340 sa6
->sin6_flowinfo
= 0;
343 if (__builtin_expect (el
== NULL
, 1))
344 sa6
->sin6_scope_id
= 0;
346 int try_numericscope
= 1;
347 if (IN6_IS_ADDR_LINKLOCAL (&a6
)
348 || IN6_IS_ADDR_MC_LINKLOCAL (&a6
)) {
350 = if_nametoindex (el
+ 1);
351 if (sa6
->sin6_scope_id
!= 0)
352 try_numericscope
= 0;
355 if (try_numericscope
) {
358 = (uint32_t) strtoul (el
+ 1, &end
,
361 sa6
->sin6_scope_id
= 0;
365 statp
->_u
._ext
.nsaddrs
[nservall
] = sa6
;
366 statp
->_u
._ext
.nssocks
[nservall
] = -1;
367 statp
->_u
._ext
.nsmap
[nservall
] = MAXNS
+ 1;
376 if (MATCH(buf
, "sortlist")) {
379 cp
= buf
+ sizeof("sortlist") - 1;
380 while (nsort
< MAXRESOLVSORT
) {
381 while (*cp
== ' ' || *cp
== '\t')
383 if (*cp
== '\0' || *cp
== '\n' || *cp
== ';')
386 while (*cp
&& !ISSORTMASK(*cp
) && *cp
!= ';' &&
387 isascii(*cp
) && !isspace(*cp
))
391 if (__inet_aton(net
, &a
)) {
392 statp
->sort_list
[nsort
].addr
= a
;
396 while (*cp
&& *cp
!= ';' &&
397 isascii(*cp
) && !isspace(*cp
))
401 if (__inet_aton(net
, &a
)) {
402 statp
->sort_list
[nsort
].mask
= a
.s_addr
;
404 statp
->sort_list
[nsort
].mask
=
405 net_mask(statp
->sort_list
[nsort
].addr
);
408 statp
->sort_list
[nsort
].mask
=
409 net_mask(statp
->sort_list
[nsort
].addr
);
418 if (MATCH(buf
, "options")) {
419 res_setoptions(statp
, buf
+ sizeof("options") - 1, "conf");
424 statp
->nscount
= nserv
;
426 if (nservall
- nserv
> 0) {
427 statp
->_u
._ext
.nscount6
= nservall
- nserv
;
428 /* We try IPv6 servers again. */
429 statp
->ipv6_unavail
= false;
433 statp
->nsort
= nsort
;
437 if (statp
->defdname
[0] == 0 &&
438 __gethostname(buf
, sizeof(statp
->defdname
) - 1) == 0 &&
439 (cp
= strchr(buf
, '.')) != NULL
)
440 strcpy(statp
->defdname
, cp
+ 1);
442 /* find components of local domain that might be searched */
443 if (havesearch
== 0) {
445 *pp
++ = statp
->defdname
;
450 for (cp
= statp
->defdname
; *cp
; cp
++)
451 dots
+= (*cp
== '.');
453 cp
= statp
->defdname
;
454 while (pp
< statp
->dnsrch
+ MAXDFLSRCH
) {
455 if (dots
< LOCALDOMAINPARTS
)
457 cp
= __rawmemchr(cp
, '.') + 1; /* we know there is one */
463 if (statp
->options
& RES_DEBUG
) {
464 printf(";; res_init()... default dnsrch list:\n");
465 for (pp
= statp
->dnsrch
; *pp
; pp
++)
466 printf(";;\t%s\n", *pp
);
467 printf(";;\t..END..\n");
470 #endif /* !RFC1535 */
473 if ((cp
= getenv("RES_OPTIONS")) != NULL
)
474 res_setoptions(statp
, cp
, "env");
475 statp
->options
|= RES_INIT
;
481 res_setoptions(res_state statp
, const char *options
, const char *source
) {
482 const char *cp
= options
;
486 if (statp
->options
& RES_DEBUG
)
487 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
491 /* skip leading and inner runs of spaces */
492 while (*cp
== ' ' || *cp
== '\t')
494 /* search for and process individual options */
495 if (!strncmp(cp
, "ndots:", sizeof("ndots:") - 1)) {
496 i
= atoi(cp
+ sizeof("ndots:") - 1);
497 if (i
<= RES_MAXNDOTS
)
500 statp
->ndots
= RES_MAXNDOTS
;
502 if (statp
->options
& RES_DEBUG
)
503 printf(";;\tndots=%d\n", statp
->ndots
);
505 } else if (!strncmp(cp
, "timeout:", sizeof("timeout:") - 1)) {
506 i
= atoi(cp
+ sizeof("timeout:") - 1);
507 if (i
<= RES_MAXRETRANS
)
510 statp
->retrans
= RES_MAXRETRANS
;
511 } else if (!strncmp(cp
, "attempts:", sizeof("attempts:") - 1)){
512 i
= atoi(cp
+ sizeof("attempts:") - 1);
513 if (i
<= RES_MAXRETRY
)
516 statp
->retry
= RES_MAXRETRY
;
517 } else if (!strncmp(cp
, "debug", sizeof("debug") - 1)) {
519 if (!(statp
->options
& RES_DEBUG
)) {
520 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
522 statp
->options
|= RES_DEBUG
;
524 printf(";;\tdebug\n");
526 } else if (!strncmp(cp
, "inet6", sizeof("inet6") - 1)) {
527 statp
->options
|= RES_USE_INET6
;
528 } else if (!strncmp(cp
, "ip6-bytestring",
529 sizeof("ip6-bytestring") - 1)) {
530 statp
->options
|= RES_USEBSTRING
;
531 } else if (!strncmp(cp
, "no-ip6-dotint",
532 sizeof("no-ip6-dotint") - 1)) {
533 statp
->options
|= RES_NOIP6DOTINT
;
534 } else if (!strncmp(cp
, "ip6-dotint",
535 sizeof("ip6-dotint") - 1)) {
536 statp
->options
&= ~RES_NOIP6DOTINT
;
537 } else if (!strncmp(cp
, "rotate", sizeof("rotate") - 1)) {
538 statp
->options
|= RES_ROTATE
;
539 } else if (!strncmp(cp
, "no-check-names",
540 sizeof("no-check-names") - 1)) {
541 statp
->options
|= RES_NOCHECKNAME
;
542 } else if (!strncmp(cp
, "edns0", sizeof("edns0") - 1)) {
543 statp
->options
|= RES_USE_EDNS0
;
545 /* XXX - print a warning here? */
547 /* skip to next run of spaces */
548 while (*cp
&& *cp
!= ' ' && *cp
!= '\t')
554 /* XXX - should really support CIDR which means explicit masks always. */
556 net_mask(in
) /* XXX - should really use system's version of this */
559 register u_int32_t i
= ntohl(in
.s_addr
);
562 return (htonl(IN_CLASSA_NET
));
563 else if (IN_CLASSB(i
))
564 return (htonl(IN_CLASSB_NET
));
565 return (htonl(IN_CLASSC_NET
));
571 return 0xffff & __getpid();
574 libc_hidden_def (__res_randomid
)
579 * This routine is for closing the socket if a virtual circuit is used and
580 * the program wants to close it. This provides support for endhostent()
581 * which expects to close the socket.
583 * This routine is not expected to be user visible.
586 __res_iclose(res_state statp
, bool free_addr
) {
589 if (statp
->_vcsock
>= 0) {
590 close_not_cancel_no_status(statp
->_vcsock
);
592 statp
->_flags
&= ~(RES_F_VC
| RES_F_CONN
);
595 for (ns
= 0; ns
< MAXNS
; ns
++)
597 for (ns
= 0; ns
< statp
->_u
._ext
.nscount
; ns
++)
599 if (statp
->_u
._ext
.nsaddrs
[ns
]) {
600 if (statp
->_u
._ext
.nssocks
[ns
] != -1) {
601 close_not_cancel_no_status(statp
->_u
._ext
.nssocks
[ns
]);
602 statp
->_u
._ext
.nssocks
[ns
] = -1;
605 free (statp
->_u
._ext
.nsaddrs
[ns
]);
606 statp
->_u
._ext
.nsaddrs
[ns
] = NULL
;
609 statp
->_u
._ext
.nsinit
= 0;
611 libc_hidden_def (__res_iclose
)
614 res_nclose(res_state statp
)
616 __res_iclose (statp
, true);
619 libc_hidden_def (__res_nclose
)
623 # ifdef _LIBC_REENTRANT
624 /* This is called when a thread is exiting to free resources held in _res. */
625 static void __attribute__ ((section ("__libc_thread_freeres_fn")))
626 res_thread_freeres (void)
628 if (_res
.nscount
== 0)
629 /* Never called res_ninit. */
632 __res_iclose (&_res
, true); /* Close any VC sockets. */
634 /* Make sure we do a full re-initialization the next time. */
637 text_set_element (__libc_thread_subfreeres
, res_thread_freeres
);
638 text_set_element (__libc_subfreeres
, res_thread_freeres
);