Increase internal precision of ldbl-128ibm decimal printf [BZ #19853]
[glibc.git] / resolv / res_init.c
blob02590fb1fdbb15c9ff20295d7a96d2d426752e04
1 /*
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
7 * are met:
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
27 * SUCH DAMAGE.
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
47 * SOFTWARE.
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
64 * 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[] = "$BINDId: res_init.c,v 8.16 2000/05/09 07:10:12 vixie Exp $";
70 #endif /* LIBC_SCCS and not lint */
72 #include <ctype.h>
73 #include <netdb.h>
74 #include <resolv.h>
75 #include <stdio.h>
76 #include <stdio_ext.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include <unistd.h>
80 #include <stdint.h>
81 #include <arpa/inet.h>
82 #include <arpa/nameser.h>
83 #include <net/if.h>
84 #include <netinet/in.h>
85 #include <sys/param.h>
86 #include <sys/socket.h>
87 #include <sys/time.h>
88 #include <sys/types.h>
90 #include <not-cancel.h>
92 /* Options. Should all be left alone. */
93 #define RESOLVSORT
94 #define RFC1535
95 /* #undef DEBUG */
97 static void res_setoptions (res_state, const char *, const char *)
98 internal_function;
100 #ifdef RESOLVSORT
101 static const char sort_mask_chars[] = "/&";
102 #define ISSORTMASK(ch) (strchr(sort_mask_chars, ch) != NULL)
103 static u_int32_t net_mask (struct in_addr) __THROW;
104 #endif
106 #if !defined(isascii) /* XXX - could be a function */
107 # define isascii(c) (!(c & 0200))
108 #endif
110 #ifdef _LIBC
111 unsigned long long int __res_initstamp attribute_hidden;
112 #endif
115 * Resolver state default settings.
119 * Set up default settings. If the configuration file exist, the values
120 * there will have precedence. Otherwise, the server address is set to
121 * INADDR_LOOPBACK and the default domain name comes from gethostname.
123 * Return 0 if completes successfully, -1 on error
126 res_ninit(res_state statp) {
127 extern int __res_vinit(res_state, int);
129 return (__res_vinit(statp, 0));
131 #ifdef _LIBC
132 libc_hidden_def (__res_ninit)
133 #endif
135 /* This function has to be reachable by res_data.c but not publically. */
137 __res_vinit(res_state statp, int preinit) {
138 FILE *fp;
139 char *cp, **pp;
140 int n;
141 char buf[BUFSIZ];
142 int nserv = 0; /* number of nameservers read from file */
143 int have_serv6 = 0;
144 int haveenv = 0;
145 int havesearch = 0;
146 #ifdef RESOLVSORT
147 int nsort = 0;
148 char *net;
149 #endif
150 #ifndef RFC1535
151 int dots;
152 #endif
153 #ifdef _LIBC
154 statp->_u._ext.initstamp = __res_initstamp;
155 #endif
157 if (!preinit) {
158 statp->retrans = RES_TIMEOUT;
159 statp->retry = RES_DFLRETRY;
160 statp->options = RES_DEFAULT;
161 statp->id = res_randomid();
164 statp->nscount = 0;
165 statp->defdname[0] = '\0';
166 statp->ndots = 1;
167 statp->pfcode = 0;
168 statp->_vcsock = -1;
169 statp->_flags = 0;
170 statp->qhook = NULL;
171 statp->rhook = NULL;
172 statp->_u._ext.nscount = 0;
173 for (n = 0; n < MAXNS; n++)
174 statp->_u._ext.nsaddrs[n] = NULL;
176 /* Allow user to override the local domain definition */
177 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
178 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
179 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
180 haveenv++;
183 * Set search list to be blank-separated strings
184 * from rest of env value. Permits users of LOCALDOMAIN
185 * to still have a search list, and anyone to set the
186 * one that they want to use as an individual (even more
187 * important now that the rfc1535 stuff restricts searches)
189 cp = statp->defdname;
190 pp = statp->dnsrch;
191 *pp++ = cp;
192 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
193 if (*cp == '\n') /* silly backwards compat */
194 break;
195 else if (*cp == ' ' || *cp == '\t') {
196 *cp = 0;
197 n = 1;
198 } else if (n) {
199 *pp++ = cp;
200 n = 0;
201 havesearch = 1;
204 /* null terminate last domain if there are excess */
205 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
206 cp++;
207 *cp = '\0';
208 *pp++ = 0;
211 #define MATCH(line, name) \
212 (!strncmp(line, name, sizeof(name) - 1) && \
213 (line[sizeof(name) - 1] == ' ' || \
214 line[sizeof(name) - 1] == '\t'))
216 if ((fp = fopen(_PATH_RESCONF, "rce")) != NULL) {
217 /* No threads use this stream. */
218 __fsetlocking (fp, FSETLOCKING_BYCALLER);
219 /* read the config file */
220 while (__fgets_unlocked(buf, sizeof(buf), fp) != NULL) {
221 /* skip comments */
222 if (*buf == ';' || *buf == '#')
223 continue;
224 /* read default domain name */
225 if (MATCH(buf, "domain")) {
226 if (haveenv) /* skip if have from environ */
227 continue;
228 cp = buf + sizeof("domain") - 1;
229 while (*cp == ' ' || *cp == '\t')
230 cp++;
231 if ((*cp == '\0') || (*cp == '\n'))
232 continue;
233 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
234 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
235 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
236 *cp = '\0';
237 havesearch = 0;
238 continue;
240 /* set search list */
241 if (MATCH(buf, "search")) {
242 if (haveenv) /* skip if have from environ */
243 continue;
244 cp = buf + sizeof("search") - 1;
245 while (*cp == ' ' || *cp == '\t')
246 cp++;
247 if ((*cp == '\0') || (*cp == '\n'))
248 continue;
249 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
250 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
251 if ((cp = strchr(statp->defdname, '\n')) != NULL)
252 *cp = '\0';
254 * Set search list to be blank-separated strings
255 * on rest of line.
257 cp = statp->defdname;
258 pp = statp->dnsrch;
259 *pp++ = cp;
260 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
261 if (*cp == ' ' || *cp == '\t') {
262 *cp = 0;
263 n = 1;
264 } else if (n) {
265 *pp++ = cp;
266 n = 0;
269 /* null terminate last domain if there are excess */
270 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
271 cp++;
272 *cp = '\0';
273 *pp++ = 0;
274 havesearch = 1;
275 continue;
277 /* read nameservers to query */
278 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
279 struct in_addr a;
281 cp = buf + sizeof("nameserver") - 1;
282 while (*cp == ' ' || *cp == '\t')
283 cp++;
284 if ((*cp != '\0') && (*cp != '\n')
285 && __inet_aton(cp, &a)) {
286 statp->nsaddr_list[nserv].sin_addr = a;
287 statp->nsaddr_list[nserv].sin_family = AF_INET;
288 statp->nsaddr_list[nserv].sin_port =
289 htons(NAMESERVER_PORT);
290 nserv++;
291 #ifdef _LIBC
292 } else {
293 struct in6_addr a6;
294 char *el;
296 if ((el = strpbrk(cp, " \t\n")) != NULL)
297 *el = '\0';
298 if ((el = strchr(cp, SCOPE_DELIMITER)) != NULL)
299 *el = '\0';
300 if ((*cp != '\0') &&
301 (__inet_pton(AF_INET6, cp, &a6) > 0)) {
302 struct sockaddr_in6 *sa6;
304 sa6 = malloc(sizeof(*sa6));
305 if (sa6 != NULL) {
306 sa6->sin6_family = AF_INET6;
307 sa6->sin6_port = htons(NAMESERVER_PORT);
308 sa6->sin6_flowinfo = 0;
309 sa6->sin6_addr = a6;
311 if (__glibc_likely (el == NULL))
312 sa6->sin6_scope_id = 0;
313 else {
314 int try_numericscope = 1;
315 if (IN6_IS_ADDR_LINKLOCAL (&a6)
316 || IN6_IS_ADDR_MC_LINKLOCAL (&a6)) {
317 sa6->sin6_scope_id
318 = __if_nametoindex (el + 1);
319 if (sa6->sin6_scope_id != 0)
320 try_numericscope = 0;
323 if (try_numericscope) {
324 char *end;
325 sa6->sin6_scope_id
326 = (uint32_t) strtoul (el + 1, &end,
327 10);
328 if (*end != '\0')
329 sa6->sin6_scope_id = 0;
333 statp->nsaddr_list[nserv].sin_family = 0;
334 statp->_u._ext.nsaddrs[nserv] = sa6;
335 statp->_u._ext.nssocks[nserv] = -1;
336 have_serv6 = 1;
337 nserv++;
340 #endif
342 continue;
344 #ifdef RESOLVSORT
345 if (MATCH(buf, "sortlist")) {
346 struct in_addr a;
348 cp = buf + sizeof("sortlist") - 1;
349 while (nsort < MAXRESOLVSORT) {
350 while (*cp == ' ' || *cp == '\t')
351 cp++;
352 if (*cp == '\0' || *cp == '\n' || *cp == ';')
353 break;
354 net = cp;
355 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
356 isascii(*cp) && !isspace(*cp))
357 cp++;
358 n = *cp;
359 *cp = 0;
360 if (__inet_aton(net, &a)) {
361 statp->sort_list[nsort].addr = a;
362 if (ISSORTMASK(n)) {
363 *cp++ = n;
364 net = cp;
365 while (*cp && *cp != ';' &&
366 isascii(*cp) && !isspace(*cp))
367 cp++;
368 n = *cp;
369 *cp = 0;
370 if (__inet_aton(net, &a)) {
371 statp->sort_list[nsort].mask = a.s_addr;
372 } else {
373 statp->sort_list[nsort].mask =
374 net_mask(statp->sort_list[nsort].addr);
376 } else {
377 statp->sort_list[nsort].mask =
378 net_mask(statp->sort_list[nsort].addr);
380 nsort++;
382 *cp = n;
384 continue;
386 #endif
387 if (MATCH(buf, "options")) {
388 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
389 continue;
392 statp->nscount = nserv;
393 #ifdef _LIBC
394 if (have_serv6) {
395 /* We try IPv6 servers again. */
396 statp->ipv6_unavail = false;
398 #endif
399 #ifdef RESOLVSORT
400 statp->nsort = nsort;
401 #endif
402 (void) fclose(fp);
404 if (__builtin_expect(statp->nscount == 0, 0)) {
405 statp->nsaddr.sin_addr = __inet_makeaddr(IN_LOOPBACKNET, 1);
406 statp->nsaddr.sin_family = AF_INET;
407 statp->nsaddr.sin_port = htons(NAMESERVER_PORT);
408 statp->nscount = 1;
410 if (statp->defdname[0] == 0 &&
411 __gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
412 (cp = strchr(buf, '.')) != NULL)
413 strcpy(statp->defdname, cp + 1);
415 /* find components of local domain that might be searched */
416 if (havesearch == 0) {
417 pp = statp->dnsrch;
418 *pp++ = statp->defdname;
419 *pp = NULL;
421 #ifndef RFC1535
422 dots = 0;
423 for (cp = statp->defdname; *cp; cp++)
424 dots += (*cp == '.');
426 cp = statp->defdname;
427 while (pp < statp->dnsrch + MAXDFLSRCH) {
428 if (dots < LOCALDOMAINPARTS)
429 break;
430 cp = __rawmemchr(cp, '.') + 1; /* we know there is one */
431 *pp++ = cp;
432 dots--;
434 *pp = NULL;
435 #ifdef DEBUG
436 if (statp->options & RES_DEBUG) {
437 printf(";; res_init()... default dnsrch list:\n");
438 for (pp = statp->dnsrch; *pp; pp++)
439 printf(";;\t%s\n", *pp);
440 printf(";;\t..END..\n");
442 #endif
443 #endif /* !RFC1535 */
446 if ((cp = getenv("RES_OPTIONS")) != NULL)
447 res_setoptions(statp, cp, "env");
448 statp->options |= RES_INIT;
449 return (0);
452 static void
453 internal_function
454 res_setoptions(res_state statp, const char *options, const char *source) {
455 const char *cp = options;
456 int i;
458 #ifdef DEBUG
459 if (statp->options & RES_DEBUG)
460 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
461 options, source);
462 #endif
463 while (*cp) {
464 /* skip leading and inner runs of spaces */
465 while (*cp == ' ' || *cp == '\t')
466 cp++;
467 /* search for and process individual options */
468 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
469 i = atoi(cp + sizeof("ndots:") - 1);
470 if (i <= RES_MAXNDOTS)
471 statp->ndots = i;
472 else
473 statp->ndots = RES_MAXNDOTS;
474 #ifdef DEBUG
475 if (statp->options & RES_DEBUG)
476 printf(";;\tndots=%d\n", statp->ndots);
477 #endif
478 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
479 i = atoi(cp + sizeof("timeout:") - 1);
480 if (i <= RES_MAXRETRANS)
481 statp->retrans = i;
482 else
483 statp->retrans = RES_MAXRETRANS;
484 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
485 i = atoi(cp + sizeof("attempts:") - 1);
486 if (i <= RES_MAXRETRY)
487 statp->retry = i;
488 else
489 statp->retry = RES_MAXRETRY;
490 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
491 #ifdef DEBUG
492 if (!(statp->options & RES_DEBUG)) {
493 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
494 options, source);
495 statp->options |= RES_DEBUG;
497 printf(";;\tdebug\n");
498 #endif
499 } else {
500 static const struct
502 char str[22];
503 uint8_t len;
504 uint8_t clear;
505 unsigned long int flag;
506 } options[] = {
507 #define STRnLEN(str) str, sizeof (str) - 1
508 { STRnLEN ("inet6"), 0, RES_USE_INET6 },
509 { STRnLEN ("ip6-bytestring"), 0, RES_USEBSTRING },
510 { STRnLEN ("no-ip6-dotint"), 0, RES_NOIP6DOTINT },
511 { STRnLEN ("ip6-dotint"), 1, ~RES_NOIP6DOTINT },
512 { STRnLEN ("rotate"), 0, RES_ROTATE },
513 { STRnLEN ("no-check-names"), 0, RES_NOCHECKNAME },
514 { STRnLEN ("edns0"), 0, RES_USE_EDNS0 },
515 { STRnLEN ("single-request-reopen"), 0, RES_SNGLKUPREOP },
516 { STRnLEN ("single-request"), 0, RES_SNGLKUP },
517 { STRnLEN ("no_tld_query"), 0, RES_NOTLDQUERY },
518 { STRnLEN ("no-tld-query"), 0, RES_NOTLDQUERY },
519 { STRnLEN ("use-vc"), 0, RES_USEVC }
521 #define noptions (sizeof (options) / sizeof (options[0]))
522 int i;
523 for (i = 0; i < noptions; ++i)
524 if (strncmp (cp, options[i].str, options[i].len) == 0)
526 if (options[i].clear)
527 statp->options &= options[i].flag;
528 else
529 statp->options |= options[i].flag;
530 break;
532 if (i == noptions) {
533 /* XXX - print a warning here? */
536 /* skip to next run of spaces */
537 while (*cp && *cp != ' ' && *cp != '\t')
538 cp++;
542 #ifdef RESOLVSORT
543 /* XXX - should really support CIDR which means explicit masks always. */
544 /* XXX - should really use system's version of this */
545 static u_int32_t
546 net_mask (struct in_addr in)
548 u_int32_t i = ntohl(in.s_addr);
550 if (IN_CLASSA(i))
551 return (htonl(IN_CLASSA_NET));
552 else if (IN_CLASSB(i))
553 return (htonl(IN_CLASSB_NET));
554 return (htonl(IN_CLASSC_NET));
556 #endif
558 u_int
559 res_randomid(void) {
560 return 0xffff & __getpid();
562 #ifdef _LIBC
563 libc_hidden_def (__res_randomid)
564 #endif
568 * This routine is for closing the socket if a virtual circuit is used and
569 * the program wants to close it. This provides support for endhostent()
570 * which expects to close the socket.
572 * This routine is not expected to be user visible.
574 void
575 __res_iclose(res_state statp, bool free_addr) {
576 int ns;
578 if (statp->_vcsock >= 0) {
579 close_not_cancel_no_status(statp->_vcsock);
580 statp->_vcsock = -1;
581 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
583 for (ns = 0; ns < statp->nscount; ns++)
584 if (statp->_u._ext.nsaddrs[ns]) {
585 if (statp->_u._ext.nssocks[ns] != -1) {
586 close_not_cancel_no_status(statp->_u._ext.nssocks[ns]);
587 statp->_u._ext.nssocks[ns] = -1;
589 if (free_addr) {
590 free (statp->_u._ext.nsaddrs[ns]);
591 statp->_u._ext.nsaddrs[ns] = NULL;
595 libc_hidden_def (__res_iclose)
597 void
598 res_nclose(res_state statp)
600 __res_iclose (statp, true);
602 #ifdef _LIBC
603 libc_hidden_def (__res_nclose)
604 #endif
606 #ifdef _LIBC
607 # ifdef _LIBC_REENTRANT
608 /* This is called when a thread is exiting to free resources held in _res. */
609 static void __attribute__ ((section ("__libc_thread_freeres_fn")))
610 res_thread_freeres (void)
612 if (_res.nscount == 0)
613 /* Never called res_ninit. */
614 return;
616 __res_iclose (&_res, true); /* Close any VC sockets. */
618 /* Make sure we do a full re-initialization the next time. */
619 _res.options = 0;
621 text_set_element (__libc_thread_subfreeres, res_thread_freeres);
622 text_set_element (__libc_subfreeres, res_thread_freeres);
623 # endif
624 #endif