libutil: add O_NOCTTY back to old pty open code
[minix.git] / lib / libc / resolv / res_init.c
bloba3e65b92b39830a3b0fd756753a7f36faf2e89d2
1 /* $NetBSD: res_init.c,v 1.22 2009/10/24 17:24:01 christos Exp $ */
3 /*
4 * Copyright (c) 1985, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
37 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
39 * Permission to use, copy, modify, and distribute this software for any
40 * purpose with or without fee is hereby granted, provided that the above
41 * copyright notice and this permission notice appear in all copies, and that
42 * the name of Digital Equipment Corporation not be used in advertising or
43 * publicity pertaining to distribution of the document or software without
44 * specific, written prior permission.
46 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
47 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
49 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53 * SOFTWARE.
57 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
58 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
60 * Permission to use, copy, modify, and distribute this software for any
61 * purpose with or without fee is hereby granted, provided that the above
62 * copyright notice and this permission notice appear in all copies.
64 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
65 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
66 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
67 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
68 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
69 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
70 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
73 #include <sys/cdefs.h>
74 #if defined(LIBC_SCCS) && !defined(lint)
75 #ifdef notdef
76 static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
77 static const char rcsid[] = "Id: res_init.c,v 1.26 2008/12/11 09:59:00 marka Exp";
78 #else
79 __RCSID("$NetBSD: res_init.c,v 1.22 2009/10/24 17:24:01 christos Exp $");
80 #endif
81 #endif /* LIBC_SCCS and not lint */
83 #include "port_before.h"
85 #include "namespace.h"
86 #include <sys/types.h>
87 #include <sys/param.h>
88 #include <sys/socket.h>
89 #include <sys/stat.h>
90 #include <sys/time.h>
91 #ifndef __minix
92 #include <sys/event.h>
93 #endif /* !__minix */
95 #include <netinet/in.h>
96 #include <arpa/inet.h>
97 #include <arpa/nameser.h>
99 #include <ctype.h>
100 #include <stdio.h>
101 #include <stdlib.h>
102 #include <string.h>
103 #include <unistd.h>
104 #include <fcntl.h>
105 #include <netdb.h>
107 #define HAVE_MD5
108 #include <md5.h>
110 #ifndef HAVE_MD5
111 # include "../dst/md5.h"
112 #else
113 # ifdef SOLARIS2
114 # include <sys/md5.h>
115 # endif
116 #endif
117 #ifndef _MD5_H_
118 # define _MD5_H_ 1 /*%< make sure we do not include rsaref md5.h file */
119 #endif
121 #include "port_after.h"
123 #if 0
124 #ifdef __weak_alias
125 __weak_alias(res_ninit,_res_ninit)
126 __weak_alias(res_randomid,__res_randomid)
127 __weak_alias(res_nclose,_res_nclose)
128 __weak_alias(res_ndestroy,_res_ndestroy)
129 __weak_alias(res_get_nibblesuffix,__res_get_nibblesuffix)
130 __weak_alias(res_get_nibblesuffix2,__res_get_nibblesuffix2)
131 __weak_alias(res_getservers,__res_getservers)
132 __weak_alias(res_setservers,__res_setservers)
133 #endif
134 #endif
137 /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
138 #include <resolv.h>
140 #include "res_private.h"
142 #define RESOLVSORT
143 /*% Options. Should all be left alone. */
144 #ifndef DEBUG
145 #define DEBUG
146 #endif
148 #ifdef SOLARIS2
149 #include <sys/systeminfo.h>
150 #endif
152 static void res_setoptions __P((res_state, const char *, const char *));
154 #ifdef RESOLVSORT
155 static const char sort_mask[] = "/&";
156 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
157 static u_int32_t net_mask __P((struct in_addr));
158 #endif
160 #if !defined(isascii) /*%< XXX - could be a function */
161 # define isascii(c) (!(c & 0200))
162 #endif
164 static struct timespec __res_conf_time;
165 static const struct timespec ts = { 0, 0 };
168 * Resolver state default settings.
172 * Set up default settings. If the configuration file exist, the values
173 * there will have precedence. Otherwise, the server address is set to
174 * INADDR_ANY and the default domain name comes from the gethostname().
176 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
177 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
178 * since it was noted that INADDR_ANY actually meant ``the first interface
179 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
180 * it had to be "up" in order for you to reach your own name server. It
181 * was later decided that since the recommended practice is to always
182 * install local static routes through 127.0.0.1 for all your network
183 * interfaces, that we could solve this problem without a code change.
185 * The configuration file should always be used, since it is the only way
186 * to specify a default domain. If you are running a server on your local
187 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
188 * in the configuration file.
190 * Return 0 if completes successfully, -1 on error
193 res_ninit(res_state statp) {
194 return (__res_vinit(statp, 0));
197 /*% This function has to be reachable by res_data.c but not publically. */
199 __res_vinit(res_state statp, int preinit) {
200 register FILE *fp;
201 register char *cp, **pp;
202 register int n;
203 char buf[BUFSIZ];
204 int nserv = 0; /*%< number of nameserver records read from file */
205 int haveenv = 0;
206 int havesearch = 0;
207 #ifdef RESOLVSORT
208 int nsort = 0;
209 char *net;
210 #endif
211 int dots;
212 union res_sockaddr_union u[2];
213 int maxns = MAXNS;
215 RES_SET_H_ERRNO(statp, 0);
217 if ((statp->options & RES_INIT) != 0U)
218 res_ndestroy(statp);
220 if (!preinit) {
221 statp->retrans = RES_TIMEOUT;
222 statp->retry = RES_DFLRETRY;
223 statp->options = RES_DEFAULT;
225 statp->_rnd = malloc(16);
226 res_rndinit(statp);
227 statp->id = res_nrandomid(statp);
229 memset(u, 0, sizeof(u));
230 #ifdef USELOOPBACK
231 u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
232 #else
233 u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
234 #endif
235 u[nserv].sin.sin_family = AF_INET;
236 u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
237 #ifdef HAVE_SA_LEN
238 u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
239 #endif
240 nserv++;
241 #ifdef HAS_INET6_STRUCTS
242 #ifdef USELOOPBACK
243 u[nserv].sin6.sin6_addr = in6addr_loopback;
244 #else
245 u[nserv].sin6.sin6_addr = in6addr_any;
246 #endif
247 u[nserv].sin6.sin6_family = AF_INET6;
248 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
249 #ifdef HAVE_SA_LEN
250 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
251 #endif
252 nserv++;
253 #endif
254 statp->nscount = 0;
255 statp->ndots = 1;
256 statp->pfcode = 0;
257 statp->_vcsock = -1;
258 statp->_flags = 0;
259 statp->qhook = NULL;
260 statp->rhook = NULL;
261 statp->_u._ext.nscount = 0;
262 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
263 if (statp->_u._ext.ext != NULL) {
264 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
265 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
266 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
267 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
268 } else {
270 * Historically res_init() rarely, if at all, failed.
271 * Examples and applications exist which do not check
272 * our return code. Furthermore several applications
273 * simply call us to get the systems domainname. So
274 * rather then immediately fail here we store the
275 * failure, which is returned later, in h_errno. And
276 * prevent the collection of 'nameserver' information
277 * by setting maxns to 0. Thus applications that fail
278 * to check our return code wont be able to make
279 * queries anyhow.
281 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
282 maxns = 0;
284 #ifdef RESOLVSORT
285 statp->nsort = 0;
286 #endif
287 res_setservers(statp, u, nserv);
289 #ifdef SOLARIS2
291 * The old libresolv derived the defaultdomain from NIS/NIS+.
292 * We want to keep this behaviour
295 char buf[sizeof(statp->defdname)], *cp;
296 int ret;
298 if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 &&
299 (unsigned int)ret <= sizeof(buf)) {
300 if (buf[0] == '+')
301 buf[0] = '.';
302 cp = strchr(buf, '.');
303 cp = (cp == NULL) ? buf : (cp + 1);
304 (void)strlcpy(statp->defdname, cp,
305 sizeof(statp->defdname));
308 #endif /* SOLARIS2 */
310 /* Allow user to override the local domain definition */
311 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
312 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
313 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
314 haveenv++;
317 * Set search list to be blank-separated strings
318 * from rest of env value. Permits users of LOCALDOMAIN
319 * to still have a search list, and anyone to set the
320 * one that they want to use as an individual (even more
321 * important now that the rfc1535 stuff restricts searches)
323 cp = statp->defdname;
324 pp = statp->dnsrch;
325 *pp++ = cp;
326 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
327 if (*cp == '\n') /*%< silly backwards compat */
328 break;
329 else if (*cp == ' ' || *cp == '\t') {
330 *cp = 0;
331 n = 1;
332 } else if (n) {
333 *pp++ = cp;
334 n = 0;
335 havesearch = 1;
338 /* null terminate last domain if there are excess */
339 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
340 cp++;
341 *cp = '\0';
342 *pp++ = 0;
345 #define MATCH(line, name) \
346 (!strncmp(line, name, sizeof(name) - 1) && \
347 (line[sizeof(name) - 1] == ' ' || \
348 line[sizeof(name) - 1] == '\t'))
350 nserv = 0;
351 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
352 struct stat st;
353 #ifndef __minix
354 struct kevent kc;
355 #endif /* !__minix */
357 /* read the config file */
358 while (fgets(buf, sizeof(buf), fp) != NULL) {
359 /* skip comments */
360 if (*buf == ';' || *buf == '#')
361 continue;
362 /* read default domain name */
363 if (MATCH(buf, "domain")) {
364 if (haveenv) /*%< skip if have from environ */
365 continue;
366 cp = buf + sizeof("domain") - 1;
367 while (*cp == ' ' || *cp == '\t')
368 cp++;
369 if ((*cp == '\0') || (*cp == '\n'))
370 continue;
371 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
372 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
373 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
374 *cp = '\0';
375 havesearch = 0;
376 continue;
378 /* set search list */
379 if (MATCH(buf, "search")) {
380 if (haveenv) /*%< skip if have from environ */
381 continue;
382 cp = buf + sizeof("search") - 1;
383 while (*cp == ' ' || *cp == '\t')
384 cp++;
385 if ((*cp == '\0') || (*cp == '\n'))
386 continue;
387 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
388 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
389 if ((cp = strchr(statp->defdname, '\n')) != NULL)
390 *cp = '\0';
392 * Set search list to be blank-separated strings
393 * on rest of line.
395 cp = statp->defdname;
396 pp = statp->dnsrch;
397 *pp++ = cp;
398 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
399 if (*cp == ' ' || *cp == '\t') {
400 *cp = 0;
401 n = 1;
402 } else if (n) {
403 *pp++ = cp;
404 n = 0;
407 /* null terminate last domain if there are excess */
408 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
409 cp++;
410 *cp = '\0';
411 *pp++ = 0;
412 havesearch = 1;
413 continue;
415 /* read nameservers to query */
416 if (MATCH(buf, "nameserver") && nserv < maxns) {
417 struct addrinfo hints, *ai;
418 char sbuf[NI_MAXSERV];
419 const size_t minsiz =
420 sizeof(statp->_u._ext.ext->nsaddrs[0]);
422 cp = buf + sizeof("nameserver") - 1;
423 while (*cp == ' ' || *cp == '\t')
424 cp++;
425 cp[strcspn(cp, ";# \t\n")] = '\0';
426 if ((*cp != '\0') && (*cp != '\n')) {
427 memset(&hints, 0, sizeof(hints));
428 hints.ai_family = PF_UNSPEC;
429 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
430 hints.ai_flags = AI_NUMERICHOST;
431 sprintf(sbuf, "%u", NAMESERVER_PORT);
432 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
433 ai->ai_addrlen <= minsiz) {
434 if (statp->_u._ext.ext != NULL) {
435 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
436 ai->ai_addr, ai->ai_addrlen);
438 if (ai->ai_addrlen <=
439 sizeof(statp->nsaddr_list[nserv])) {
440 memcpy(&statp->nsaddr_list[nserv],
441 ai->ai_addr, ai->ai_addrlen);
442 } else
443 statp->nsaddr_list[nserv].sin_family = 0;
444 freeaddrinfo(ai);
445 nserv++;
448 continue;
450 #ifdef RESOLVSORT
451 if (MATCH(buf, "sortlist")) {
452 struct in_addr a;
454 cp = buf + sizeof("sortlist") - 1;
455 while (nsort < MAXRESOLVSORT) {
456 while (*cp == ' ' || *cp == '\t')
457 cp++;
458 if (*cp == '\0' || *cp == '\n' || *cp == ';')
459 break;
460 net = cp;
461 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
462 isascii(*cp) && !isspace((unsigned char)*cp))
463 cp++;
464 n = *cp;
465 *cp = 0;
466 if (inet_aton(net, &a)) {
467 statp->sort_list[nsort].addr = a;
468 if (ISSORTMASK(n)) {
469 *cp++ = n;
470 net = cp;
471 while (*cp && *cp != ';' &&
472 isascii(*cp) &&
473 !isspace((unsigned char)*cp))
474 cp++;
475 n = *cp;
476 *cp = 0;
477 if (inet_aton(net, &a)) {
478 statp->sort_list[nsort].mask = a.s_addr;
479 } else {
480 statp->sort_list[nsort].mask =
481 net_mask(statp->sort_list[nsort].addr);
483 } else {
484 statp->sort_list[nsort].mask =
485 net_mask(statp->sort_list[nsort].addr);
487 nsort++;
489 *cp = n;
491 continue;
493 #endif
494 if (MATCH(buf, "options")) {
495 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
496 continue;
499 if (nserv > 0)
500 statp->nscount = nserv;
501 #ifdef RESOLVSORT
502 statp->nsort = nsort;
503 #endif
504 statp->_u._ext.ext->resfd = dup(fileno(fp));
505 (void) fclose(fp);
506 if (fstat(statp->_u._ext.ext->resfd, &st) != -1)
507 __res_conf_time = statp->_u._ext.ext->res_conf_time =
508 st.st_mtimespec;
509 #ifndef __minix
510 statp->_u._ext.ext->kq = kqueue();
511 (void)fcntl(statp->_u._ext.ext->kq, F_SETFD, FD_CLOEXEC);
512 (void)fcntl(statp->_u._ext.ext->resfd, F_SETFD, FD_CLOEXEC);
513 EV_SET(&kc, statp->_u._ext.ext->resfd, EVFILT_VNODE,
514 EV_ADD|EV_ENABLE|EV_CLEAR, NOTE_DELETE|NOTE_WRITE| NOTE_EXTEND|
515 NOTE_ATTRIB|NOTE_LINK|NOTE_RENAME|NOTE_REVOKE, 0, 0);
516 (void)kevent(statp->_u._ext.ext->kq, &kc, 1, NULL, 0, &ts);
517 #else /* __minix */
518 statp->_u._ext.ext->kq = -1;
519 #endif /* !__minix */
520 } else {
521 statp->_u._ext.ext->kq = -1;
522 statp->_u._ext.ext->resfd = -1;
525 * Last chance to get a nameserver. This should not normally
526 * be necessary
528 #ifdef NO_RESOLV_CONF
529 if(nserv == 0)
530 nserv = get_nameservers(statp);
531 #endif
533 if (statp->defdname[0] == 0 &&
534 gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
535 (cp = strchr(buf, '.')) != NULL)
536 strcpy(statp->defdname, cp + 1);
538 /* find components of local domain that might be searched */
539 if (havesearch == 0) {
540 pp = statp->dnsrch;
541 *pp++ = statp->defdname;
542 *pp = NULL;
544 dots = 0;
545 for (cp = statp->defdname; *cp; cp++)
546 dots += (*cp == '.');
548 cp = statp->defdname;
549 while (pp < statp->dnsrch + MAXDFLSRCH) {
550 if (dots < LOCALDOMAINPARTS)
551 break;
552 cp = strchr(cp, '.') + 1; /*%< we know there is one */
553 *pp++ = cp;
554 dots--;
556 *pp = NULL;
557 #ifdef DEBUG
558 if (statp->options & RES_DEBUG) {
559 printf(";; res_init()... default dnsrch list:\n");
560 for (pp = statp->dnsrch; *pp; pp++)
561 printf(";;\t%s\n", *pp);
562 printf(";;\t..END..\n");
564 #endif
567 if ((cp = getenv("RES_OPTIONS")) != NULL)
568 res_setoptions(statp, cp, "env");
569 statp->options |= RES_INIT;
570 return (statp->res_h_errno);
574 res_check(res_state statp, struct timespec *mtime)
576 #ifdef __minix
578 * XXX: No update on change.
580 return 0;
581 #else /* !__minix */
583 * If the times are equal, then we check if there
584 * was a kevent related to resolv.conf and reload.
585 * If the times are not equal, then we don't bother
586 * to check the kevent, because another thread already
587 * did, loaded and changed the time.
589 if (timespeccmp(&statp->_u._ext.ext->res_conf_time,
590 &__res_conf_time, ==)) {
591 struct kevent ke;
592 if (statp->_u._ext.ext->kq == -1)
593 goto out;
595 switch (kevent(statp->_u._ext.ext->kq, NULL, 0, &ke, 1, &ts)) {
596 case 0:
597 case -1:
598 out:
599 if (mtime)
600 *mtime = __res_conf_time;
601 return 0;
602 default:
603 break;
606 (void)__res_vinit(statp, 0);
607 if (mtime)
608 *mtime = __res_conf_time;
609 return 1;
610 #endif /* !__minix */
613 static void
614 res_setoptions(res_state statp, const char *options, const char *source)
616 const char *cp = options;
617 int i;
618 struct __res_state_ext *ext = statp->_u._ext.ext;
620 #ifdef DEBUG
621 if (statp->options & RES_DEBUG)
622 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
623 options, source);
624 #endif
625 while (*cp) {
626 /* skip leading and inner runs of spaces */
627 while (*cp == ' ' || *cp == '\t')
628 cp++;
629 /* search for and process individual options */
630 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
631 i = atoi(cp + sizeof("ndots:") - 1);
632 if (i <= RES_MAXNDOTS)
633 statp->ndots = i;
634 else
635 statp->ndots = RES_MAXNDOTS;
636 #ifdef DEBUG
637 if (statp->options & RES_DEBUG)
638 printf(";;\tndots=%d\n", statp->ndots);
639 #endif
640 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
641 i = atoi(cp + sizeof("timeout:") - 1);
642 if (i <= RES_MAXRETRANS)
643 statp->retrans = i;
644 else
645 statp->retrans = RES_MAXRETRANS;
646 #ifdef DEBUG
647 if (statp->options & RES_DEBUG)
648 printf(";;\ttimeout=%d\n", statp->retrans);
649 #endif
650 #ifdef SOLARIS2
651 } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) {
653 * For backward compatibility, 'retrans' is
654 * supported as an alias for 'timeout', though
655 * without an imposed maximum.
657 statp->retrans = atoi(cp + sizeof("retrans:") - 1);
658 } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){
660 * For backward compatibility, 'retry' is
661 * supported as an alias for 'attempts', though
662 * without an imposed maximum.
664 statp->retry = atoi(cp + sizeof("retry:") - 1);
665 #endif /* SOLARIS2 */
666 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
667 i = atoi(cp + sizeof("attempts:") - 1);
668 if (i <= RES_MAXRETRY)
669 statp->retry = i;
670 else
671 statp->retry = RES_MAXRETRY;
672 #ifdef DEBUG
673 if (statp->options & RES_DEBUG)
674 printf(";;\tattempts=%d\n", statp->retry);
675 #endif
676 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
677 #ifdef DEBUG
678 if (!(statp->options & RES_DEBUG)) {
679 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
680 options, source);
681 statp->options |= RES_DEBUG;
683 printf(";;\tdebug\n");
684 #endif
685 } else if (!strncmp(cp, "no_tld_query",
686 sizeof("no_tld_query") - 1) ||
687 !strncmp(cp, "no-tld-query",
688 sizeof("no-tld-query") - 1)) {
689 statp->options |= RES_NOTLDQUERY;
690 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
691 statp->options |= RES_USE_INET6;
692 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
693 statp->options |= RES_ROTATE;
694 } else if (!strncmp(cp, "no-check-names",
695 sizeof("no-check-names") - 1)) {
696 statp->options |= RES_NOCHECKNAME;
698 #ifdef RES_USE_EDNS0
699 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
700 statp->options |= RES_USE_EDNS0;
702 #endif
703 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
704 statp->options |= RES_USE_DNAME;
706 else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
707 if (ext == NULL)
708 goto skip;
709 cp += sizeof("nibble:") - 1;
710 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
711 strncpy(ext->nsuffix, cp, (size_t)i);
712 ext->nsuffix[i] = '\0';
714 else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
715 if (ext == NULL)
716 goto skip;
717 cp += sizeof("nibble2:") - 1;
718 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
719 strncpy(ext->nsuffix2, cp, (size_t)i);
720 ext->nsuffix2[i] = '\0';
722 else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
723 cp += sizeof("v6revmode:") - 1;
724 /* "nibble" and "bitstring" used to be valid */
725 if (!strncmp(cp, "single", sizeof("single") - 1)) {
726 statp->options |= RES_NO_NIBBLE2;
727 } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
728 statp->options &=
729 ~RES_NO_NIBBLE2;
732 else {
733 /* XXX - print a warning here? */
735 skip:
736 /* skip to next run of spaces */
737 while (*cp && *cp != ' ' && *cp != '\t')
738 cp++;
742 #ifdef RESOLVSORT
743 /* XXX - should really support CIDR which means explicit masks always. */
744 static u_int32_t
745 net_mask(in) /*!< XXX - should really use system's version of this */
746 struct in_addr in;
748 register u_int32_t i = ntohl(in.s_addr);
750 if (IN_CLASSA(i))
751 return (htonl(IN_CLASSA_NET));
752 else if (IN_CLASSB(i))
753 return (htonl(IN_CLASSB_NET));
754 return (htonl(IN_CLASSC_NET));
756 #endif
758 static u_char srnd[16];
760 void
761 res_rndinit(res_state statp)
763 struct timeval now;
764 u_int32_t u32;
765 u_int16_t u16;
766 u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd;
768 gettimeofday(&now, NULL);
769 u32 = (u_int32_t)now.tv_sec;
770 memcpy(rnd, &u32, 4);
771 u32 = now.tv_usec;
772 memcpy(rnd + 4, &u32, 4);
773 u32 += (u_int32_t)now.tv_sec;
774 memcpy(rnd + 8, &u32, 4);
775 u16 = getpid();
776 memcpy(rnd + 12, &u16, 2);
779 u_int
780 res_nrandomid(res_state statp) {
781 struct timeval now;
782 u_int16_t u16;
783 MD5_CTX ctx;
784 u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd;
786 gettimeofday(&now, NULL);
787 u16 = (u_int16_t) (now.tv_sec ^ now.tv_usec);
788 memcpy(rnd + 14, &u16, 2);
789 #ifndef HAVE_MD5
790 MD5_Init(&ctx);
791 MD5_Update(&ctx, rnd, 16);
792 MD5_Final(rnd, &ctx);
793 #else
794 MD5Init(&ctx);
795 MD5Update(&ctx, rnd, 16);
796 MD5Final(rnd, &ctx);
797 #endif
798 memcpy(&u16, rnd + 14, 2);
799 return ((u_int) u16);
803 * This routine is for closing the socket if a virtual circuit is used and
804 * the program wants to close it. This provides support for endhostent()
805 * which expects to close the socket.
807 * This routine is not expected to be user visible.
809 void
810 res_nclose(res_state statp) {
811 int ns;
813 if (statp->_vcsock >= 0) {
814 (void) close(statp->_vcsock);
815 statp->_vcsock = -1;
816 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
818 for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
819 if (statp->_u._ext.nssocks[ns] != -1) {
820 (void) close(statp->_u._ext.nssocks[ns]);
821 statp->_u._ext.nssocks[ns] = -1;
826 void
827 res_ndestroy(res_state statp) {
828 res_nclose(statp);
829 if (statp->_u._ext.ext != NULL) {
830 if (statp->_u._ext.ext->kq != -1)
831 (void)close(statp->_u._ext.ext->kq);
832 if (statp->_u._ext.ext->resfd != -1)
833 (void)close(statp->_u._ext.ext->resfd);
834 free(statp->_u._ext.ext);
835 statp->_u._ext.ext = NULL;
837 if (statp->_rnd != NULL) {
838 free(statp->_rnd);
839 statp->_rnd = NULL;
841 statp->options &= ~RES_INIT;
844 const char *
845 res_get_nibblesuffix(res_state statp) {
846 if (statp->_u._ext.ext)
847 return (statp->_u._ext.ext->nsuffix);
848 return ("ip6.arpa");
851 const char *
852 res_get_nibblesuffix2(res_state statp) {
853 if (statp->_u._ext.ext)
854 return (statp->_u._ext.ext->nsuffix2);
855 return ("ip6.int");
858 void
859 res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) {
860 int i, nserv;
861 size_t size;
863 /* close open servers */
864 res_nclose(statp);
866 /* cause rtt times to be forgotten */
867 statp->_u._ext.nscount = 0;
869 nserv = 0;
870 for (i = 0; i < cnt && nserv < MAXNS; i++) {
871 switch (set->sin.sin_family) {
872 case AF_INET:
873 size = sizeof(set->sin);
874 if (statp->_u._ext.ext)
875 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
876 &set->sin, size);
877 if (size <= sizeof(statp->nsaddr_list[nserv]))
878 memcpy(&statp->nsaddr_list[nserv],
879 &set->sin, size);
880 else
881 statp->nsaddr_list[nserv].sin_family = 0;
882 nserv++;
883 break;
885 #ifdef HAS_INET6_STRUCTS
886 case AF_INET6:
887 size = sizeof(set->sin6);
888 if (statp->_u._ext.ext)
889 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
890 &set->sin6, size);
891 if (size <= sizeof(statp->nsaddr_list[nserv]))
892 memcpy(&statp->nsaddr_list[nserv],
893 &set->sin6, size);
894 else
895 statp->nsaddr_list[nserv].sin_family = 0;
896 nserv++;
897 break;
898 #endif
900 default:
901 break;
903 set++;
905 statp->nscount = nserv;
910 res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) {
911 int i;
912 size_t size;
913 u_int16_t family;
915 for (i = 0; i < statp->nscount && i < cnt; i++) {
916 if (statp->_u._ext.ext)
917 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
918 else
919 family = statp->nsaddr_list[i].sin_family;
921 switch (family) {
922 case AF_INET:
923 size = sizeof(set->sin);
924 if (statp->_u._ext.ext)
925 memcpy(&set->sin,
926 &statp->_u._ext.ext->nsaddrs[i],
927 size);
928 else
929 memcpy(&set->sin, &statp->nsaddr_list[i],
930 size);
931 break;
933 #ifdef HAS_INET6_STRUCTS
934 case AF_INET6:
935 size = sizeof(set->sin6);
936 if (statp->_u._ext.ext)
937 memcpy(&set->sin6,
938 &statp->_u._ext.ext->nsaddrs[i],
939 size);
940 else
941 memcpy(&set->sin6, &statp->nsaddr_list[i],
942 size);
943 break;
944 #endif
946 default:
947 set->sin.sin_family = 0;
948 break;
950 set++;
952 return (statp->nscount);
955 /*! \file */