* sysdeps/unix/sysv/linux/alpha/timer_create.c: New file.
[glibc/pb-stable.git] / resolv / res_init.c
blob7b9cf433131573dbe1a5e64b9c0bb52061fbea64
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 <sys/types.h>
73 #include <sys/param.h>
74 #include <sys/socket.h>
75 #include <sys/time.h>
77 #include <netinet/in.h>
78 #include <arpa/inet.h>
79 #include <arpa/nameser.h>
81 #include <ctype.h>
82 #include <resolv.h>
83 #include <stdio.h>
84 #include <stdio_ext.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <unistd.h>
89 /* Options. Should all be left alone. */
90 #define RESOLVSORT
91 #define RFC1535
92 /* #undef DEBUG */
94 static void res_setoptions (res_state, const char *, const char *)
95 internal_function;
97 #ifdef RESOLVSORT
98 static const char sort_mask_chars[] = "/&";
99 #define ISSORTMASK(ch) (strchr(sort_mask_chars, ch) != NULL)
100 static u_int32_t net_mask __P((struct in_addr));
101 #endif
103 #if !defined(isascii) /* XXX - could be a function */
104 # define isascii(c) (!(c & 0200))
105 #endif
108 * Resolver state default settings.
112 * Set up default settings. If the configuration file exist, the values
113 * there will have precedence. Otherwise, the server address is set to
114 * INADDR_ANY and the default domain name comes from the gethostname().
116 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
117 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
118 * since it was noted that INADDR_ANY actually meant ``the first interface
119 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
120 * it had to be "up" in order for you to reach your own name server. It
121 * was later decided that since the recommended practice is to always
122 * install local static routes through 127.0.0.1 for all your network
123 * interfaces, that we could solve this problem without a code change.
125 * The configuration file should always be used, since it is the only way
126 * to specify a default domain. If you are running a server on your local
127 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
128 * in the configuration file.
130 * Return 0 if completes successfully, -1 on error
133 res_ninit(res_state statp) {
134 extern int __res_vinit(res_state, int);
136 return (__res_vinit(statp, 0));
138 #ifdef _LIBC
139 libc_hidden_def (__res_ninit)
140 #endif
142 /* This function has to be reachable by res_data.c but not publically. */
144 __res_vinit(res_state statp, int preinit) {
145 register FILE *fp;
146 register char *cp, **pp;
147 register int n;
148 char buf[BUFSIZ];
149 int nserv = 0; /* number of nameserver records read from file */
150 #ifdef _LIBC
151 int nservall = 0; /* number of NS records read, nserv IPv4 only */
152 #endif
153 int haveenv = 0;
154 int havesearch = 0;
155 #ifdef RESOLVSORT
156 int nsort = 0;
157 char *net;
158 #endif
159 #ifndef RFC1535
160 int dots;
161 #endif
163 if (!preinit) {
164 statp->retrans = RES_TIMEOUT;
165 statp->retry = RES_DFLRETRY;
166 statp->options = RES_DEFAULT;
167 statp->id = res_randomid();
170 #ifdef USELOOPBACK
171 statp->nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
172 #else
173 statp->nsaddr.sin_addr.s_addr = INADDR_ANY;
174 #endif
175 statp->nsaddr.sin_family = AF_INET;
176 statp->nsaddr.sin_port = htons(NAMESERVER_PORT);
177 statp->nscount = 1;
178 statp->ndots = 1;
179 statp->pfcode = 0;
180 statp->_vcsock = -1;
181 statp->_flags = 0;
182 statp->qhook = NULL;
183 statp->rhook = NULL;
184 statp->_u._ext.nsinit = 0;
185 statp->_u._ext.nscount = 0;
186 #ifdef _LIBC
187 statp->_u._ext.nscount6 = 0;
188 for (n = 0; n < MAXNS; n++) {
189 statp->_u._ext.nsaddrs[n] = NULL;
190 statp->_u._ext.nsmap[n] = MAXNS;
192 #endif
194 /* Allow user to override the local domain definition */
195 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
196 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
197 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
198 haveenv++;
201 * Set search list to be blank-separated strings
202 * from rest of env value. Permits users of LOCALDOMAIN
203 * to still have a search list, and anyone to set the
204 * one that they want to use as an individual (even more
205 * important now that the rfc1535 stuff restricts searches)
207 cp = statp->defdname;
208 pp = statp->dnsrch;
209 *pp++ = cp;
210 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
211 if (*cp == '\n') /* silly backwards compat */
212 break;
213 else if (*cp == ' ' || *cp == '\t') {
214 *cp = 0;
215 n = 1;
216 } else if (n) {
217 *pp++ = cp;
218 n = 0;
219 havesearch = 1;
222 /* null terminate last domain if there are excess */
223 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
224 cp++;
225 *cp = '\0';
226 *pp++ = 0;
229 #define MATCH(line, name) \
230 (!strncmp(line, name, sizeof(name) - 1) && \
231 (line[sizeof(name) - 1] == ' ' || \
232 line[sizeof(name) - 1] == '\t'))
234 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
235 /* No threads use this stream. */
236 __fsetlocking (fp, FSETLOCKING_BYCALLER);
237 /* read the config file */
238 while (fgets_unlocked(buf, sizeof(buf), fp) != NULL) {
239 /* skip comments */
240 if (*buf == ';' || *buf == '#')
241 continue;
242 /* read default domain name */
243 if (MATCH(buf, "domain")) {
244 if (haveenv) /* skip if have from environ */
245 continue;
246 cp = buf + sizeof("domain") - 1;
247 while (*cp == ' ' || *cp == '\t')
248 cp++;
249 if ((*cp == '\0') || (*cp == '\n'))
250 continue;
251 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
252 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
253 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
254 *cp = '\0';
255 havesearch = 0;
256 continue;
258 /* set search list */
259 if (MATCH(buf, "search")) {
260 if (haveenv) /* skip if have from environ */
261 continue;
262 cp = buf + sizeof("search") - 1;
263 while (*cp == ' ' || *cp == '\t')
264 cp++;
265 if ((*cp == '\0') || (*cp == '\n'))
266 continue;
267 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
268 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
269 if ((cp = strchr(statp->defdname, '\n')) != NULL)
270 *cp = '\0';
272 * Set search list to be blank-separated strings
273 * on rest of line.
275 cp = statp->defdname;
276 pp = statp->dnsrch;
277 *pp++ = cp;
278 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
279 if (*cp == ' ' || *cp == '\t') {
280 *cp = 0;
281 n = 1;
282 } else if (n) {
283 *pp++ = cp;
284 n = 0;
287 /* null terminate last domain if there are excess */
288 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
289 cp++;
290 *cp = '\0';
291 *pp++ = 0;
292 havesearch = 1;
293 continue;
295 /* read nameservers to query */
296 #ifdef _LIBC
297 if (MATCH(buf, "nameserver") && nservall < MAXNS) {
298 #else
299 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
300 #endif
301 struct in_addr a;
303 cp = buf + sizeof("nameserver") - 1;
304 while (*cp == ' ' || *cp == '\t')
305 cp++;
306 if ((*cp != '\0') && (*cp != '\n')
307 && __inet_aton(cp, &a)) {
308 statp->nsaddr_list[nserv].sin_addr = a;
309 statp->nsaddr_list[nserv].sin_family = AF_INET;
310 statp->nsaddr_list[nserv].sin_port =
311 htons(NAMESERVER_PORT);
312 nserv++;
313 #ifdef _LIBC
314 nservall++;
315 } else {
316 struct in6_addr a6;
317 char *el;
319 if ((el = strchr(cp, '\n')) != NULL)
320 *el = '\0';
321 if ((*cp != '\0') &&
322 (inet_pton(AF_INET6, cp, &a6) > 0)) {
323 struct sockaddr_in6 *sa6;
325 sa6 = malloc(sizeof(*sa6));
326 if (sa6 != NULL) {
327 sa6->sin6_addr = a6;
328 sa6->sin6_family = AF_INET6;
329 sa6->sin6_port = htons(NAMESERVER_PORT);
330 statp->_u._ext.nsaddrs[nservall] = sa6;
331 statp->_u._ext.nssocks[nservall] = -1;
332 statp->_u._ext.nsmap[nservall] = MAXNS + 1;
333 nservall++;
336 #endif
338 continue;
340 #ifdef RESOLVSORT
341 if (MATCH(buf, "sortlist")) {
342 struct in_addr a;
344 cp = buf + sizeof("sortlist") - 1;
345 while (nsort < MAXRESOLVSORT) {
346 while (*cp == ' ' || *cp == '\t')
347 cp++;
348 if (*cp == '\0' || *cp == '\n' || *cp == ';')
349 break;
350 net = cp;
351 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
352 isascii(*cp) && !isspace(*cp))
353 cp++;
354 n = *cp;
355 *cp = 0;
356 if (__inet_aton(net, &a)) {
357 statp->sort_list[nsort].addr = a;
358 if (ISSORTMASK(n)) {
359 *cp++ = n;
360 net = cp;
361 while (*cp && *cp != ';' &&
362 isascii(*cp) && !isspace(*cp))
363 cp++;
364 n = *cp;
365 *cp = 0;
366 if (__inet_aton(net, &a)) {
367 statp->sort_list[nsort].mask = a.s_addr;
368 } else {
369 statp->sort_list[nsort].mask =
370 net_mask(statp->sort_list[nsort].addr);
372 } else {
373 statp->sort_list[nsort].mask =
374 net_mask(statp->sort_list[nsort].addr);
376 nsort++;
378 *cp = n;
380 continue;
382 #endif
383 if (MATCH(buf, "options")) {
384 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
385 continue;
388 if (nserv > 1)
389 statp->nscount = nserv;
390 #ifdef _LIBC
391 if (nservall - nserv > 0)
392 statp->_u._ext.nscount6 = nservall - nserv;
393 #endif
394 #ifdef RESOLVSORT
395 statp->nsort = nsort;
396 #endif
397 (void) fclose(fp);
399 if (statp->defdname[0] == 0 &&
400 __gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
401 (cp = strchr(buf, '.')) != NULL)
402 strcpy(statp->defdname, cp + 1);
404 /* find components of local domain that might be searched */
405 if (havesearch == 0) {
406 pp = statp->dnsrch;
407 *pp++ = statp->defdname;
408 *pp = NULL;
410 #ifndef RFC1535
411 dots = 0;
412 for (cp = statp->defdname; *cp; cp++)
413 dots += (*cp == '.');
415 cp = statp->defdname;
416 while (pp < statp->dnsrch + MAXDFLSRCH) {
417 if (dots < LOCALDOMAINPARTS)
418 break;
419 cp = __rawmemchr(cp, '.') + 1; /* we know there is one */
420 *pp++ = cp;
421 dots--;
423 *pp = NULL;
424 #ifdef DEBUG
425 if (statp->options & RES_DEBUG) {
426 printf(";; res_init()... default dnsrch list:\n");
427 for (pp = statp->dnsrch; *pp; pp++)
428 printf(";;\t%s\n", *pp);
429 printf(";;\t..END..\n");
431 #endif
432 #endif /* !RFC1535 */
435 if ((cp = getenv("RES_OPTIONS")) != NULL)
436 res_setoptions(statp, cp, "env");
437 statp->options |= RES_INIT;
438 return (0);
441 static void
442 internal_function
443 res_setoptions(res_state statp, const char *options, const char *source) {
444 const char *cp = options;
445 int i;
447 #ifdef DEBUG
448 if (statp->options & RES_DEBUG)
449 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
450 options, source);
451 #endif
452 while (*cp) {
453 /* skip leading and inner runs of spaces */
454 while (*cp == ' ' || *cp == '\t')
455 cp++;
456 /* search for and process individual options */
457 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
458 i = atoi(cp + sizeof("ndots:") - 1);
459 if (i <= RES_MAXNDOTS)
460 statp->ndots = i;
461 else
462 statp->ndots = RES_MAXNDOTS;
463 #ifdef DEBUG
464 if (statp->options & RES_DEBUG)
465 printf(";;\tndots=%d\n", statp->ndots);
466 #endif
467 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
468 i = atoi(cp + sizeof("timeout:") - 1);
469 if (i <= RES_MAXRETRANS)
470 statp->retrans = i;
471 else
472 statp->retrans = RES_MAXRETRANS;
473 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
474 i = atoi(cp + sizeof("attempts:") - 1);
475 if (i <= RES_MAXRETRY)
476 statp->retry = i;
477 else
478 statp->retry = RES_MAXRETRY;
479 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
480 #ifdef DEBUG
481 if (!(statp->options & RES_DEBUG)) {
482 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
483 options, source);
484 statp->options |= RES_DEBUG;
486 printf(";;\tdebug\n");
487 #endif
488 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
489 statp->options |= RES_USE_INET6;
490 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
491 statp->options |= RES_ROTATE;
492 } else if (!strncmp(cp, "no-check-names",
493 sizeof("no-check-names") - 1)) {
494 statp->options |= RES_NOCHECKNAME;
495 } else {
496 /* XXX - print a warning here? */
498 /* skip to next run of spaces */
499 while (*cp && *cp != ' ' && *cp != '\t')
500 cp++;
504 #ifdef RESOLVSORT
505 /* XXX - should really support CIDR which means explicit masks always. */
506 static u_int32_t
507 net_mask(in) /* XXX - should really use system's version of this */
508 struct in_addr in;
510 register u_int32_t i = ntohl(in.s_addr);
512 if (IN_CLASSA(i))
513 return (htonl(IN_CLASSA_NET));
514 else if (IN_CLASSB(i))
515 return (htonl(IN_CLASSB_NET));
516 return (htonl(IN_CLASSC_NET));
518 #endif
520 u_int
521 res_randomid(void) {
522 struct timeval now;
524 __gettimeofday(&now, NULL);
525 return (0xffff & (now.tv_sec ^ now.tv_usec ^ __getpid()));
527 #ifdef _LIBC
528 libc_hidden_def (__res_randomid)
529 #endif
533 * This routine is for closing the socket if a virtual circuit is used and
534 * the program wants to close it. This provides support for endhostent()
535 * which expects to close the socket.
537 * This routine is not expected to be user visible.
539 void
540 res_nclose(res_state statp) {
541 int ns;
543 if (statp->_vcsock >= 0) {
544 (void) __close(statp->_vcsock);
545 statp->_vcsock = -1;
546 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
548 #ifdef _LIBC
549 for (ns = 0; ns < MAXNS; ns++)
550 #else
551 for (ns = 0; ns < statp->_u._ext.nscount; ns++)
552 #endif
553 if (statp->_u._ext.nsaddrs[ns]
554 && statp->_u._ext.nssocks[ns] != -1) {
555 (void) __close(statp->_u._ext.nssocks[ns]);
556 statp->_u._ext.nssocks[ns] = -1;
558 statp->_u._ext.nsinit = 0;
560 #ifdef _LIBC
561 libc_hidden_def (__res_nclose)
562 #endif
564 #ifdef _LIBC
565 # ifdef _LIBC_REENTRANT
566 /* This is called when a thread is exiting to free resources held in _res. */
567 static void __attribute__ ((section ("__libc_thread_freeres_fn")))
568 res_thread_freeres (void)
570 if (_res.nscount == 0)
571 /* Never called res_ninit. */
572 return;
574 __res_nclose (&_res); /* Close any VC sockets. */
576 for (int ns = 0; ns < MAXNS; ns++)
577 if (_res._u._ext.nsaddrs[ns] != NULL)
579 free (_res._u._ext.nsaddrs[ns]);
580 _res._u._ext.nsaddrs[ns] = NULL;
583 text_set_element (__libc_thread_subfreeres, res_thread_freeres);
584 text_set_element (__libc_subfreeres, res_thread_freeres);
585 # endif
586 #endif