S390: Ifunc resolver macro for vector instructions.
[glibc.git] / resolv / res_init.c
blob66561ffac2ffc039707676ed8b4bf36ee50ee889
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_ANY and the default domain name comes from the gethostname().
123 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
124 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
125 * since it was noted that INADDR_ANY actually meant ``the first interface
126 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
127 * it had to be "up" in order for you to reach your own name server. It
128 * was later decided that since the recommended practice is to always
129 * install local static routes through 127.0.0.1 for all your network
130 * interfaces, that we could solve this problem without a code change.
132 * The configuration file should always be used, since it is the only way
133 * to specify a default domain. If you are running a server on your local
134 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
135 * in the configuration file.
137 * Return 0 if completes successfully, -1 on error
140 res_ninit(res_state statp) {
141 extern int __res_vinit(res_state, int);
143 return (__res_vinit(statp, 0));
145 #ifdef _LIBC
146 libc_hidden_def (__res_ninit)
147 #endif
149 /* This function has to be reachable by res_data.c but not publically. */
151 __res_vinit(res_state statp, int preinit) {
152 FILE *fp;
153 char *cp, **pp;
154 int n;
155 char buf[BUFSIZ];
156 int nserv = 0; /* number of nameservers read from file */
157 int have_serv6 = 0;
158 int haveenv = 0;
159 int havesearch = 0;
160 #ifdef RESOLVSORT
161 int nsort = 0;
162 char *net;
163 #endif
164 #ifndef RFC1535
165 int dots;
166 #endif
167 #ifdef _LIBC
168 statp->_u._ext.initstamp = __res_initstamp;
169 #endif
171 if (!preinit) {
172 statp->retrans = RES_TIMEOUT;
173 statp->retry = RES_DFLRETRY;
174 statp->options = RES_DEFAULT;
175 statp->id = res_randomid();
178 statp->nscount = 0;
179 statp->ndots = 1;
180 statp->pfcode = 0;
181 statp->_vcsock = -1;
182 statp->_flags = 0;
183 statp->qhook = NULL;
184 statp->rhook = NULL;
185 statp->_u._ext.nscount = 0;
186 for (n = 0; n < MAXNS; n++)
187 statp->_u._ext.nsaddrs[n] = NULL;
189 /* Allow user to override the local domain definition */
190 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
191 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
192 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
193 haveenv++;
196 * Set search list to be blank-separated strings
197 * from rest of env value. Permits users of LOCALDOMAIN
198 * to still have a search list, and anyone to set the
199 * one that they want to use as an individual (even more
200 * important now that the rfc1535 stuff restricts searches)
202 cp = statp->defdname;
203 pp = statp->dnsrch;
204 *pp++ = cp;
205 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
206 if (*cp == '\n') /* silly backwards compat */
207 break;
208 else if (*cp == ' ' || *cp == '\t') {
209 *cp = 0;
210 n = 1;
211 } else if (n) {
212 *pp++ = cp;
213 n = 0;
214 havesearch = 1;
217 /* null terminate last domain if there are excess */
218 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
219 cp++;
220 *cp = '\0';
221 *pp++ = 0;
224 #define MATCH(line, name) \
225 (!strncmp(line, name, sizeof(name) - 1) && \
226 (line[sizeof(name) - 1] == ' ' || \
227 line[sizeof(name) - 1] == '\t'))
229 if ((fp = fopen(_PATH_RESCONF, "rce")) != NULL) {
230 /* No threads use this stream. */
231 __fsetlocking (fp, FSETLOCKING_BYCALLER);
232 /* read the config file */
233 while (__fgets_unlocked(buf, sizeof(buf), fp) != NULL) {
234 /* skip comments */
235 if (*buf == ';' || *buf == '#')
236 continue;
237 /* read default domain name */
238 if (MATCH(buf, "domain")) {
239 if (haveenv) /* skip if have from environ */
240 continue;
241 cp = buf + sizeof("domain") - 1;
242 while (*cp == ' ' || *cp == '\t')
243 cp++;
244 if ((*cp == '\0') || (*cp == '\n'))
245 continue;
246 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
247 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
248 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
249 *cp = '\0';
250 havesearch = 0;
251 continue;
253 /* set search list */
254 if (MATCH(buf, "search")) {
255 if (haveenv) /* skip if have from environ */
256 continue;
257 cp = buf + sizeof("search") - 1;
258 while (*cp == ' ' || *cp == '\t')
259 cp++;
260 if ((*cp == '\0') || (*cp == '\n'))
261 continue;
262 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
263 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
264 if ((cp = strchr(statp->defdname, '\n')) != NULL)
265 *cp = '\0';
267 * Set search list to be blank-separated strings
268 * on rest of line.
270 cp = statp->defdname;
271 pp = statp->dnsrch;
272 *pp++ = cp;
273 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
274 if (*cp == ' ' || *cp == '\t') {
275 *cp = 0;
276 n = 1;
277 } else if (n) {
278 *pp++ = cp;
279 n = 0;
282 /* null terminate last domain if there are excess */
283 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
284 cp++;
285 *cp = '\0';
286 *pp++ = 0;
287 havesearch = 1;
288 continue;
290 /* read nameservers to query */
291 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
292 struct in_addr a;
294 cp = buf + sizeof("nameserver") - 1;
295 while (*cp == ' ' || *cp == '\t')
296 cp++;
297 if ((*cp != '\0') && (*cp != '\n')
298 && __inet_aton(cp, &a)) {
299 statp->nsaddr_list[nserv].sin_addr = a;
300 statp->nsaddr_list[nserv].sin_family = AF_INET;
301 statp->nsaddr_list[nserv].sin_port =
302 htons(NAMESERVER_PORT);
303 nserv++;
304 #ifdef _LIBC
305 } else {
306 struct in6_addr a6;
307 char *el;
309 if ((el = strpbrk(cp, " \t\n")) != NULL)
310 *el = '\0';
311 if ((el = strchr(cp, SCOPE_DELIMITER)) != NULL)
312 *el = '\0';
313 if ((*cp != '\0') &&
314 (__inet_pton(AF_INET6, cp, &a6) > 0)) {
315 struct sockaddr_in6 *sa6;
317 sa6 = malloc(sizeof(*sa6));
318 if (sa6 != NULL) {
319 sa6->sin6_family = AF_INET6;
320 sa6->sin6_port = htons(NAMESERVER_PORT);
321 sa6->sin6_flowinfo = 0;
322 sa6->sin6_addr = a6;
324 if (__glibc_likely (el == NULL))
325 sa6->sin6_scope_id = 0;
326 else {
327 int try_numericscope = 1;
328 if (IN6_IS_ADDR_LINKLOCAL (&a6)
329 || IN6_IS_ADDR_MC_LINKLOCAL (&a6)) {
330 sa6->sin6_scope_id
331 = __if_nametoindex (el + 1);
332 if (sa6->sin6_scope_id != 0)
333 try_numericscope = 0;
336 if (try_numericscope) {
337 char *end;
338 sa6->sin6_scope_id
339 = (uint32_t) strtoul (el + 1, &end,
340 10);
341 if (*end != '\0')
342 sa6->sin6_scope_id = 0;
346 statp->nsaddr_list[nserv].sin_family = 0;
347 statp->_u._ext.nsaddrs[nserv] = sa6;
348 statp->_u._ext.nssocks[nserv] = -1;
349 have_serv6 = 1;
350 nserv++;
353 #endif
355 continue;
357 #ifdef RESOLVSORT
358 if (MATCH(buf, "sortlist")) {
359 struct in_addr a;
361 cp = buf + sizeof("sortlist") - 1;
362 while (nsort < MAXRESOLVSORT) {
363 while (*cp == ' ' || *cp == '\t')
364 cp++;
365 if (*cp == '\0' || *cp == '\n' || *cp == ';')
366 break;
367 net = cp;
368 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
369 isascii(*cp) && !isspace(*cp))
370 cp++;
371 n = *cp;
372 *cp = 0;
373 if (__inet_aton(net, &a)) {
374 statp->sort_list[nsort].addr = a;
375 if (ISSORTMASK(n)) {
376 *cp++ = n;
377 net = cp;
378 while (*cp && *cp != ';' &&
379 isascii(*cp) && !isspace(*cp))
380 cp++;
381 n = *cp;
382 *cp = 0;
383 if (__inet_aton(net, &a)) {
384 statp->sort_list[nsort].mask = a.s_addr;
385 } else {
386 statp->sort_list[nsort].mask =
387 net_mask(statp->sort_list[nsort].addr);
389 } else {
390 statp->sort_list[nsort].mask =
391 net_mask(statp->sort_list[nsort].addr);
393 nsort++;
395 *cp = n;
397 continue;
399 #endif
400 if (MATCH(buf, "options")) {
401 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
402 continue;
405 statp->nscount = nserv;
406 #ifdef _LIBC
407 if (have_serv6) {
408 /* We try IPv6 servers again. */
409 statp->ipv6_unavail = false;
411 #endif
412 #ifdef RESOLVSORT
413 statp->nsort = nsort;
414 #endif
415 (void) fclose(fp);
417 if (__builtin_expect(statp->nscount == 0, 0)) {
418 statp->nsaddr.sin_addr = __inet_makeaddr(IN_LOOPBACKNET, 1);
419 statp->nsaddr.sin_family = AF_INET;
420 statp->nsaddr.sin_port = htons(NAMESERVER_PORT);
421 statp->nscount = 1;
423 if (statp->defdname[0] == 0 &&
424 __gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
425 (cp = strchr(buf, '.')) != NULL)
426 strcpy(statp->defdname, cp + 1);
428 /* find components of local domain that might be searched */
429 if (havesearch == 0) {
430 pp = statp->dnsrch;
431 *pp++ = statp->defdname;
432 *pp = NULL;
434 #ifndef RFC1535
435 dots = 0;
436 for (cp = statp->defdname; *cp; cp++)
437 dots += (*cp == '.');
439 cp = statp->defdname;
440 while (pp < statp->dnsrch + MAXDFLSRCH) {
441 if (dots < LOCALDOMAINPARTS)
442 break;
443 cp = __rawmemchr(cp, '.') + 1; /* we know there is one */
444 *pp++ = cp;
445 dots--;
447 *pp = NULL;
448 #ifdef DEBUG
449 if (statp->options & RES_DEBUG) {
450 printf(";; res_init()... default dnsrch list:\n");
451 for (pp = statp->dnsrch; *pp; pp++)
452 printf(";;\t%s\n", *pp);
453 printf(";;\t..END..\n");
455 #endif
456 #endif /* !RFC1535 */
459 if ((cp = getenv("RES_OPTIONS")) != NULL)
460 res_setoptions(statp, cp, "env");
461 statp->options |= RES_INIT;
462 return (0);
465 static void
466 internal_function
467 res_setoptions(res_state statp, const char *options, const char *source) {
468 const char *cp = options;
469 int i;
471 #ifdef DEBUG
472 if (statp->options & RES_DEBUG)
473 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
474 options, source);
475 #endif
476 while (*cp) {
477 /* skip leading and inner runs of spaces */
478 while (*cp == ' ' || *cp == '\t')
479 cp++;
480 /* search for and process individual options */
481 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
482 i = atoi(cp + sizeof("ndots:") - 1);
483 if (i <= RES_MAXNDOTS)
484 statp->ndots = i;
485 else
486 statp->ndots = RES_MAXNDOTS;
487 #ifdef DEBUG
488 if (statp->options & RES_DEBUG)
489 printf(";;\tndots=%d\n", statp->ndots);
490 #endif
491 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
492 i = atoi(cp + sizeof("timeout:") - 1);
493 if (i <= RES_MAXRETRANS)
494 statp->retrans = i;
495 else
496 statp->retrans = RES_MAXRETRANS;
497 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
498 i = atoi(cp + sizeof("attempts:") - 1);
499 if (i <= RES_MAXRETRY)
500 statp->retry = i;
501 else
502 statp->retry = RES_MAXRETRY;
503 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
504 #ifdef DEBUG
505 if (!(statp->options & RES_DEBUG)) {
506 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
507 options, source);
508 statp->options |= RES_DEBUG;
510 printf(";;\tdebug\n");
511 #endif
512 } else {
513 static const struct
515 char str[22];
516 uint8_t len;
517 uint8_t clear;
518 unsigned long int flag;
519 } options[] = {
520 #define STRnLEN(str) str, sizeof (str) - 1
521 { STRnLEN ("inet6"), 0, RES_USE_INET6 },
522 { STRnLEN ("ip6-bytestring"), 0, RES_USEBSTRING },
523 { STRnLEN ("no-ip6-dotint"), 0, RES_NOIP6DOTINT },
524 { STRnLEN ("ip6-dotint"), 1, ~RES_NOIP6DOTINT },
525 { STRnLEN ("rotate"), 0, RES_ROTATE },
526 { STRnLEN ("no-check-names"), 0, RES_NOCHECKNAME },
527 { STRnLEN ("edns0"), 0, RES_USE_EDNS0 },
528 { STRnLEN ("single-request-reopen"), 0, RES_SNGLKUPREOP },
529 { STRnLEN ("single-request"), 0, RES_SNGLKUP },
530 { STRnLEN ("no_tld_query"), 0, RES_NOTLDQUERY },
531 { STRnLEN ("no-tld-query"), 0, RES_NOTLDQUERY },
532 { STRnLEN ("use-vc"), 0, RES_USEVC }
534 #define noptions (sizeof (options) / sizeof (options[0]))
535 int i;
536 for (i = 0; i < noptions; ++i)
537 if (strncmp (cp, options[i].str, options[i].len) == 0)
539 if (options[i].clear)
540 statp->options &= options[i].flag;
541 else
542 statp->options |= options[i].flag;
543 break;
545 if (i == noptions) {
546 /* XXX - print a warning here? */
549 /* skip to next run of spaces */
550 while (*cp && *cp != ' ' && *cp != '\t')
551 cp++;
555 #ifdef RESOLVSORT
556 /* XXX - should really support CIDR which means explicit masks always. */
557 static u_int32_t
558 net_mask(in) /* XXX - should really use system's version of this */
559 struct in_addr in;
561 u_int32_t i = ntohl(in.s_addr);
563 if (IN_CLASSA(i))
564 return (htonl(IN_CLASSA_NET));
565 else if (IN_CLASSB(i))
566 return (htonl(IN_CLASSB_NET));
567 return (htonl(IN_CLASSC_NET));
569 #endif
571 u_int
572 res_randomid(void) {
573 return 0xffff & __getpid();
575 #ifdef _LIBC
576 libc_hidden_def (__res_randomid)
577 #endif
581 * This routine is for closing the socket if a virtual circuit is used and
582 * the program wants to close it. This provides support for endhostent()
583 * which expects to close the socket.
585 * This routine is not expected to be user visible.
587 void
588 __res_iclose(res_state statp, bool free_addr) {
589 int ns;
591 if (statp->_vcsock >= 0) {
592 close_not_cancel_no_status(statp->_vcsock);
593 statp->_vcsock = -1;
594 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
596 for (ns = 0; ns < statp->_u._ext.nscount; ns++)
597 if (statp->_u._ext.nsaddrs[ns]) {
598 if (statp->_u._ext.nssocks[ns] != -1) {
599 close_not_cancel_no_status(statp->_u._ext.nssocks[ns]);
600 statp->_u._ext.nssocks[ns] = -1;
602 if (free_addr) {
603 free (statp->_u._ext.nsaddrs[ns]);
604 statp->_u._ext.nsaddrs[ns] = NULL;
608 libc_hidden_def (__res_iclose)
610 void
611 res_nclose(res_state statp)
613 __res_iclose (statp, true);
615 #ifdef _LIBC
616 libc_hidden_def (__res_nclose)
617 #endif
619 #ifdef _LIBC
620 # ifdef _LIBC_REENTRANT
621 /* This is called when a thread is exiting to free resources held in _res. */
622 static void __attribute__ ((section ("__libc_thread_freeres_fn")))
623 res_thread_freeres (void)
625 if (_res.nscount == 0)
626 /* Never called res_ninit. */
627 return;
629 __res_iclose (&_res, true); /* Close any VC sockets. */
631 /* Make sure we do a full re-initialization the next time. */
632 _res.options = 0;
634 text_set_element (__libc_thread_subfreeres, res_thread_freeres);
635 text_set_element (__libc_subfreeres, res_thread_freeres);
636 # endif
637 #endif