1 /* $NetBSD: res_init.c,v 1.22 2009/10/24 17:24:01 christos Exp $ */
4 * Copyright (c) 1985, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
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)
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";
79 __RCSID("$NetBSD: res_init.c,v 1.22 2009/10/24 17:24:01 christos Exp $");
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>
92 #include <sys/event.h>
95 #include <netinet/in.h>
96 #include <arpa/inet.h>
97 #include <arpa/nameser.h>
111 # include "../dst/md5.h"
114 # include <sys/md5.h>
118 # define _MD5_H_ 1 /*%< make sure we do not include rsaref md5.h file */
121 #include "port_after.h"
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
)
137 /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
140 #include "res_private.h"
143 /*% Options. Should all be left alone. */
149 #include <sys/systeminfo.h>
152 static void res_setoptions
__P((res_state
, const char *, const char *));
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
));
160 #if !defined(isascii) /*%< XXX - could be a function */
161 # define isascii(c) (!(c & 0200))
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
) {
201 register char *cp
, **pp
;
204 int nserv
= 0; /*%< number of nameserver records read from file */
212 union res_sockaddr_union u
[2];
215 RES_SET_H_ERRNO(statp
, 0);
217 if ((statp
->options
& RES_INIT
) != 0U)
221 statp
->retrans
= RES_TIMEOUT
;
222 statp
->retry
= RES_DFLRETRY
;
223 statp
->options
= RES_DEFAULT
;
225 statp
->_rnd
= malloc(16);
227 statp
->id
= res_nrandomid(statp
);
229 memset(u
, 0, sizeof(u
));
231 u
[nserv
].sin
.sin_addr
= inet_makeaddr(IN_LOOPBACKNET
, 1);
233 u
[nserv
].sin
.sin_addr
.s_addr
= INADDR_ANY
;
235 u
[nserv
].sin
.sin_family
= AF_INET
;
236 u
[nserv
].sin
.sin_port
= htons(NAMESERVER_PORT
);
238 u
[nserv
].sin
.sin_len
= sizeof(struct sockaddr_in
);
241 #ifdef HAS_INET6_STRUCTS
243 u
[nserv
].sin6
.sin6_addr
= in6addr_loopback
;
245 u
[nserv
].sin6
.sin6_addr
= in6addr_any
;
247 u
[nserv
].sin6
.sin6_family
= AF_INET6
;
248 u
[nserv
].sin6
.sin6_port
= htons(NAMESERVER_PORT
);
250 u
[nserv
].sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
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");
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
281 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
287 res_setservers(statp
, u
, nserv
);
291 * The old libresolv derived the defaultdomain from NIS/NIS+.
292 * We want to keep this behaviour
295 char buf
[sizeof(statp
->defdname
)], *cp
;
298 if ((ret
= sysinfo(SI_SRPC_DOMAIN
, buf
, sizeof(buf
))) > 0 &&
299 (unsigned int)ret
<= sizeof(buf
)) {
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';
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
;
326 for (n
= 0; *cp
&& pp
< statp
->dnsrch
+ MAXDNSRCH
; cp
++) {
327 if (*cp
== '\n') /*%< silly backwards compat */
329 else if (*cp
== ' ' || *cp
== '\t') {
338 /* null terminate last domain if there are excess */
339 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t' && *cp
!= '\n')
345 #define MATCH(line, name) \
346 (!strncmp(line, name, sizeof(name) - 1) && \
347 (line[sizeof(name) - 1] == ' ' || \
348 line[sizeof(name) - 1] == '\t'))
351 if ((fp
= fopen(_PATH_RESCONF
, "r")) != NULL
) {
355 #endif /* !__minix */
357 /* read the config file */
358 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
360 if (*buf
== ';' || *buf
== '#')
362 /* read default domain name */
363 if (MATCH(buf
, "domain")) {
364 if (haveenv
) /*%< skip if have from environ */
366 cp
= buf
+ sizeof("domain") - 1;
367 while (*cp
== ' ' || *cp
== '\t')
369 if ((*cp
== '\0') || (*cp
== '\n'))
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
)
378 /* set search list */
379 if (MATCH(buf
, "search")) {
380 if (haveenv
) /*%< skip if have from environ */
382 cp
= buf
+ sizeof("search") - 1;
383 while (*cp
== ' ' || *cp
== '\t')
385 if ((*cp
== '\0') || (*cp
== '\n'))
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
)
392 * Set search list to be blank-separated strings
395 cp
= statp
->defdname
;
398 for (n
= 0; *cp
&& pp
< statp
->dnsrch
+ MAXDNSRCH
; cp
++) {
399 if (*cp
== ' ' || *cp
== '\t') {
407 /* null terminate last domain if there are excess */
408 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t')
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')
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
);
443 statp
->nsaddr_list
[nserv
].sin_family
= 0;
451 if (MATCH(buf
, "sortlist")) {
454 cp
= buf
+ sizeof("sortlist") - 1;
455 while (nsort
< MAXRESOLVSORT
) {
456 while (*cp
== ' ' || *cp
== '\t')
458 if (*cp
== '\0' || *cp
== '\n' || *cp
== ';')
461 while (*cp
&& !ISSORTMASK(*cp
) && *cp
!= ';' &&
462 isascii(*cp
) && !isspace((unsigned char)*cp
))
466 if (inet_aton(net
, &a
)) {
467 statp
->sort_list
[nsort
].addr
= a
;
471 while (*cp
&& *cp
!= ';' &&
473 !isspace((unsigned char)*cp
))
477 if (inet_aton(net
, &a
)) {
478 statp
->sort_list
[nsort
].mask
= a
.s_addr
;
480 statp
->sort_list
[nsort
].mask
=
481 net_mask(statp
->sort_list
[nsort
].addr
);
484 statp
->sort_list
[nsort
].mask
=
485 net_mask(statp
->sort_list
[nsort
].addr
);
494 if (MATCH(buf
, "options")) {
495 res_setoptions(statp
, buf
+ sizeof("options") - 1, "conf");
500 statp
->nscount
= nserv
;
502 statp
->nsort
= nsort
;
504 statp
->_u
._ext
.ext
->resfd
= dup(fileno(fp
));
506 if (fstat(statp
->_u
._ext
.ext
->resfd
, &st
) != -1)
507 __res_conf_time
= statp
->_u
._ext
.ext
->res_conf_time
=
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
);
518 statp
->_u
._ext
.ext
->kq
= -1;
519 #endif /* !__minix */
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
528 #ifdef NO_RESOLV_CONF
530 nserv
= get_nameservers(statp
);
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) {
541 *pp
++ = statp
->defdname
;
545 for (cp
= statp
->defdname
; *cp
; cp
++)
546 dots
+= (*cp
== '.');
548 cp
= statp
->defdname
;
549 while (pp
< statp
->dnsrch
+ MAXDFLSRCH
) {
550 if (dots
< LOCALDOMAINPARTS
)
552 cp
= strchr(cp
, '.') + 1; /*%< we know there is one */
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");
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
)
578 * XXX: No update on change.
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
, ==)) {
592 if (statp
->_u
._ext
.ext
->kq
== -1)
595 switch (kevent(statp
->_u
._ext
.ext
->kq
, NULL
, 0, &ke
, 1, &ts
)) {
600 *mtime
= __res_conf_time
;
606 (void)__res_vinit(statp
, 0);
608 *mtime
= __res_conf_time
;
610 #endif /* !__minix */
614 res_setoptions(res_state statp
, const char *options
, const char *source
)
616 const char *cp
= options
;
618 struct __res_state_ext
*ext
= statp
->_u
._ext
.ext
;
621 if (statp
->options
& RES_DEBUG
)
622 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
626 /* skip leading and inner runs of spaces */
627 while (*cp
== ' ' || *cp
== '\t')
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
)
635 statp
->ndots
= RES_MAXNDOTS
;
637 if (statp
->options
& RES_DEBUG
)
638 printf(";;\tndots=%d\n", statp
->ndots
);
640 } else if (!strncmp(cp
, "timeout:", sizeof("timeout:") - 1)) {
641 i
= atoi(cp
+ sizeof("timeout:") - 1);
642 if (i
<= RES_MAXRETRANS
)
645 statp
->retrans
= RES_MAXRETRANS
;
647 if (statp
->options
& RES_DEBUG
)
648 printf(";;\ttimeout=%d\n", statp
->retrans
);
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
)
671 statp
->retry
= RES_MAXRETRY
;
673 if (statp
->options
& RES_DEBUG
)
674 printf(";;\tattempts=%d\n", statp
->retry
);
676 } else if (!strncmp(cp
, "debug", sizeof("debug") - 1)) {
678 if (!(statp
->options
& RES_DEBUG
)) {
679 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
681 statp
->options
|= RES_DEBUG
;
683 printf(";;\tdebug\n");
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
;
699 else if (!strncmp(cp
, "edns0", sizeof("edns0") - 1)) {
700 statp
->options
|= RES_USE_EDNS0
;
703 else if (!strncmp(cp
, "dname", sizeof("dname") - 1)) {
704 statp
->options
|= RES_USE_DNAME
;
706 else if (!strncmp(cp
, "nibble:", sizeof("nibble:") - 1)) {
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)) {
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)) {
733 /* XXX - print a warning here? */
736 /* skip to next run of spaces */
737 while (*cp
&& *cp
!= ' ' && *cp
!= '\t')
743 /* XXX - should really support CIDR which means explicit masks always. */
745 net_mask(in
) /*!< XXX - should really use system's version of this */
748 register u_int32_t i
= ntohl(in
.s_addr
);
751 return (htonl(IN_CLASSA_NET
));
752 else if (IN_CLASSB(i
))
753 return (htonl(IN_CLASSB_NET
));
754 return (htonl(IN_CLASSC_NET
));
758 static u_char srnd
[16];
761 res_rndinit(res_state statp
)
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);
772 memcpy(rnd
+ 4, &u32
, 4);
773 u32
+= (u_int32_t
)now
.tv_sec
;
774 memcpy(rnd
+ 8, &u32
, 4);
776 memcpy(rnd
+ 12, &u16
, 2);
780 res_nrandomid(res_state statp
) {
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);
791 MD5_Update(&ctx
, rnd
, 16);
792 MD5_Final(rnd
, &ctx
);
795 MD5Update(&ctx
, rnd
, 16);
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.
810 res_nclose(res_state statp
) {
813 if (statp
->_vcsock
>= 0) {
814 (void) close(statp
->_vcsock
);
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;
827 res_ndestroy(res_state 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
) {
841 statp
->options
&= ~RES_INIT
;
845 res_get_nibblesuffix(res_state statp
) {
846 if (statp
->_u
._ext
.ext
)
847 return (statp
->_u
._ext
.ext
->nsuffix
);
852 res_get_nibblesuffix2(res_state statp
) {
853 if (statp
->_u
._ext
.ext
)
854 return (statp
->_u
._ext
.ext
->nsuffix2
);
859 res_setservers(res_state statp
, const union res_sockaddr_union
*set
, int cnt
) {
863 /* close open servers */
866 /* cause rtt times to be forgotten */
867 statp
->_u
._ext
.nscount
= 0;
870 for (i
= 0; i
< cnt
&& nserv
< MAXNS
; i
++) {
871 switch (set
->sin
.sin_family
) {
873 size
= sizeof(set
->sin
);
874 if (statp
->_u
._ext
.ext
)
875 memcpy(&statp
->_u
._ext
.ext
->nsaddrs
[nserv
],
877 if (size
<= sizeof(statp
->nsaddr_list
[nserv
]))
878 memcpy(&statp
->nsaddr_list
[nserv
],
881 statp
->nsaddr_list
[nserv
].sin_family
= 0;
885 #ifdef HAS_INET6_STRUCTS
887 size
= sizeof(set
->sin6
);
888 if (statp
->_u
._ext
.ext
)
889 memcpy(&statp
->_u
._ext
.ext
->nsaddrs
[nserv
],
891 if (size
<= sizeof(statp
->nsaddr_list
[nserv
]))
892 memcpy(&statp
->nsaddr_list
[nserv
],
895 statp
->nsaddr_list
[nserv
].sin_family
= 0;
905 statp
->nscount
= nserv
;
910 res_getservers(res_state statp
, union res_sockaddr_union
*set
, int cnt
) {
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
;
919 family
= statp
->nsaddr_list
[i
].sin_family
;
923 size
= sizeof(set
->sin
);
924 if (statp
->_u
._ext
.ext
)
926 &statp
->_u
._ext
.ext
->nsaddrs
[i
],
929 memcpy(&set
->sin
, &statp
->nsaddr_list
[i
],
933 #ifdef HAS_INET6_STRUCTS
935 size
= sizeof(set
->sin6
);
936 if (statp
->_u
._ext
.ext
)
938 &statp
->_u
._ext
.ext
->nsaddrs
[i
],
941 memcpy(&set
->sin6
, &statp
->nsaddr_list
[i
],
947 set
->sin
.sin_family
= 0;
952 return (statp
->nscount
);