1 /* $NetBSD: res_init.c,v 1.30 2015/02/24 17:56:20 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
54 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
56 * Permission to use, copy, modify, and distribute this software for any
57 * purpose with or without fee is hereby granted, provided that the above
58 * copyright notice and this permission notice appear in all copies.
60 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
61 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
62 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
63 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
64 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
65 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
66 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
69 #include <sys/cdefs.h>
70 #if defined(LIBC_SCCS) && !defined(lint)
72 static const char sccsid
[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
73 static const char rcsid
[] = "Id: res_init.c,v 1.26 2008/12/11 09:59:00 marka Exp";
75 __RCSID("$NetBSD: res_init.c,v 1.30 2015/02/24 17:56:20 christos Exp $");
77 #endif /* LIBC_SCCS and not lint */
79 #include "port_before.h"
81 #include "namespace.h"
82 #include <sys/types.h>
83 #include <sys/param.h>
84 #include <sys/socket.h>
88 #include <sys/event.h>
89 #endif /* !defined(__minix) */
91 #include <netinet/in.h>
92 #include <arpa/inet.h>
93 #include <arpa/nameser.h>
107 # include "../dst/md5.h"
110 # include <sys/md5.h>
114 # define _MD5_H_ 1 /*%< make sure we do not include rsaref md5.h file */
117 #include "port_after.h"
121 __weak_alias(res_ninit
,_res_ninit
)
122 __weak_alias(res_randomid
,__res_randomid
)
123 __weak_alias(res_nclose
,_res_nclose
)
124 __weak_alias(res_ndestroy
,_res_ndestroy
)
125 __weak_alias(res_get_nibblesuffix
,__res_get_nibblesuffix
)
126 __weak_alias(res_get_nibblesuffix2
,__res_get_nibblesuffix2
)
127 __weak_alias(res_getservers
,__res_getservers
)
128 __weak_alias(res_setservers
,__res_setservers
)
133 /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
136 #include "res_private.h"
139 /*% Options. Should all be left alone. */
145 #include <sys/systeminfo.h>
148 static void res_setoptions(res_state
, const char *, const char *);
151 static const char sort_mask
[] = "/&";
152 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
153 static uint32_t net_mask(struct in_addr
);
156 #if !defined(isascii) /*%< XXX - could be a function */
157 # define isascii(c) (!(c & 0200))
160 static struct timespec __res_conf_time
;
161 #if !defined(__minix)
162 static const struct timespec ts
= { 0, 0 };
163 #endif /* !defined(__minix) */
165 const char *__res_conf_name
= _PATH_RESCONF
;
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 than 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(__res_conf_name
, "re")) != NULL
) {
353 #if !defined(__minix)
355 #endif /* !defined(__minix) */
357 /* read the config file */
358 while (fgets(buf
, (int)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
= fcntl(fileno(fp
), F_DUPFD_CLOEXEC
);
506 if (fstat(statp
->_u
._ext
.ext
->resfd
, &st
) != -1)
507 __res_conf_time
= statp
->_u
._ext
.ext
->res_conf_time
=
509 #if !defined(__minix)
510 statp
->_u
._ext
.ext
->kq
= kqueue1(O_CLOEXEC
);
511 EV_SET(&kc
, statp
->_u
._ext
.ext
->resfd
, EVFILT_VNODE
,
512 EV_ADD
|EV_ENABLE
|EV_CLEAR
, NOTE_DELETE
|NOTE_WRITE
| NOTE_EXTEND
|
513 NOTE_ATTRIB
|NOTE_LINK
|NOTE_RENAME
|NOTE_REVOKE
, 0, 0);
514 (void)kevent(statp
->_u
._ext
.ext
->kq
, &kc
, 1, NULL
, 0, &ts
);
516 statp
->_u
._ext
.ext
->kq
= -1;
517 #endif /* !defined(__minix) */
519 statp
->_u
._ext
.ext
->kq
= -1;
520 statp
->_u
._ext
.ext
->resfd
= -1;
523 * Last chance to get a nameserver. This should not normally
526 #ifdef NO_RESOLV_CONF
528 nserv
= get_nameservers(statp
);
531 if (statp
->defdname
[0] == 0 &&
532 gethostname(buf
, sizeof(statp
->defdname
) - 1) == 0 &&
533 (cp
= strchr(buf
, '.')) != NULL
)
534 strcpy(statp
->defdname
, cp
+ 1);
536 /* find components of local domain that might be searched */
537 if (havesearch
== 0) {
539 *pp
++ = statp
->defdname
;
543 for (cp
= statp
->defdname
; *cp
; cp
++)
544 dots
+= (*cp
== '.');
546 cp
= statp
->defdname
;
547 while (pp
< statp
->dnsrch
+ MAXDFLSRCH
) {
548 if (dots
< LOCALDOMAINPARTS
)
550 cp
= strchr(cp
, '.') + 1; /*%< we know there is one */
556 if (statp
->options
& RES_DEBUG
) {
557 printf(";; res_init()... default dnsrch list:\n");
558 for (pp
= statp
->dnsrch
; *pp
; pp
++)
559 printf(";;\t%s\n", *pp
);
560 printf(";;\t..END..\n");
565 if ((cp
= getenv("RES_OPTIONS")) != NULL
)
566 res_setoptions(statp
, cp
, "env");
567 statp
->options
|= RES_INIT
;
568 return (statp
->res_h_errno
);
572 res_check(res_state statp
, struct timespec
*mtime
)
576 * XXX: No update on change.
581 * If the times are equal, then we check if there
582 * was a kevent related to resolv.conf and reload.
583 * If the times are not equal, then we don't bother
584 * to check the kevent, because another thread already
585 * did, loaded and changed the time.
587 if (timespeccmp(&statp
->_u
._ext
.ext
->res_conf_time
,
588 &__res_conf_time
, ==)) {
590 if (statp
->_u
._ext
.ext
->kq
== -1)
593 switch (kevent(statp
->_u
._ext
.ext
->kq
, NULL
, 0, &ke
, 1, &ts
)) {
598 *mtime
= __res_conf_time
;
604 (void)__res_vinit(statp
, 0);
606 *mtime
= __res_conf_time
;
608 #endif /* defined(__minix) */
612 res_setoptions(res_state statp
, const char *options
, const char *source
)
614 const char *cp
= options
;
617 struct __res_state_ext
*ext
= statp
->_u
._ext
.ext
;
620 if (statp
->options
& RES_DEBUG
)
621 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
625 /* skip leading and inner runs of spaces */
626 while (*cp
== ' ' || *cp
== '\t')
628 /* search for and process individual options */
629 if (!strncmp(cp
, "ndots:", sizeof("ndots:") - 1)) {
630 i
= atoi(cp
+ sizeof("ndots:") - 1);
631 if (i
<= RES_MAXNDOTS
)
634 statp
->ndots
= RES_MAXNDOTS
;
636 if (statp
->options
& RES_DEBUG
)
637 printf(";;\tndots=%d\n", statp
->ndots
);
639 } else if (!strncmp(cp
, "timeout:", sizeof("timeout:") - 1)) {
640 i
= atoi(cp
+ sizeof("timeout:") - 1);
641 if (i
<= RES_MAXRETRANS
)
644 statp
->retrans
= RES_MAXRETRANS
;
646 if (statp
->options
& RES_DEBUG
)
647 printf(";;\ttimeout=%d\n", statp
->retrans
);
650 } else if (!strncmp(cp
, "retrans:", sizeof("retrans:") - 1)) {
652 * For backward compatibility, 'retrans' is
653 * supported as an alias for 'timeout', though
654 * without an imposed maximum.
656 statp
->retrans
= atoi(cp
+ sizeof("retrans:") - 1);
657 } else if (!strncmp(cp
, "retry:", sizeof("retry:") - 1)){
659 * For backward compatibility, 'retry' is
660 * supported as an alias for 'attempts', though
661 * without an imposed maximum.
663 statp
->retry
= atoi(cp
+ sizeof("retry:") - 1);
664 #endif /* SOLARIS2 */
665 } else if (!strncmp(cp
, "attempts:", sizeof("attempts:") - 1)){
666 i
= atoi(cp
+ sizeof("attempts:") - 1);
667 if (i
<= RES_MAXRETRY
)
670 statp
->retry
= RES_MAXRETRY
;
672 if (statp
->options
& RES_DEBUG
)
673 printf(";;\tattempts=%d\n", statp
->retry
);
675 } else if (!strncmp(cp
, "debug", sizeof("debug") - 1)) {
677 if (!(statp
->options
& RES_DEBUG
)) {
678 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
680 statp
->options
|= RES_DEBUG
;
682 printf(";;\tdebug\n");
684 } else if (!strncmp(cp
, "no_tld_query",
685 sizeof("no_tld_query") - 1) ||
686 !strncmp(cp
, "no-tld-query",
687 sizeof("no-tld-query") - 1)) {
688 statp
->options
|= RES_NOTLDQUERY
;
689 } else if (!strncmp(cp
, "inet6", sizeof("inet6") - 1)) {
690 statp
->options
|= RES_USE_INET6
;
691 } else if (!strncmp(cp
, "rotate", sizeof("rotate") - 1)) {
692 statp
->options
|= RES_ROTATE
;
693 } else if (!strncmp(cp
, "no-check-names",
694 sizeof("no-check-names") - 1)) {
695 statp
->options
|= RES_NOCHECKNAME
;
696 } else if (!strncmp(cp
, "check-names",
697 sizeof("check-names") - 1)) {
698 statp
->options
&= ~RES_NOCHECKNAME
;
701 else if (!strncmp(cp
, "edns0", sizeof("edns0") - 1)) {
702 statp
->options
|= RES_USE_EDNS0
;
705 else if (!strncmp(cp
, "dname", sizeof("dname") - 1)) {
706 statp
->options
|= RES_USE_DNAME
;
708 else if (!strncmp(cp
, "nibble:", sizeof("nibble:") - 1)) {
711 cp
+= sizeof("nibble:") - 1;
712 j
= MIN(strcspn(cp
, " \t"), sizeof(ext
->nsuffix
) - 1);
713 strncpy(ext
->nsuffix
, cp
, j
);
714 ext
->nsuffix
[j
] = '\0';
716 else if (!strncmp(cp
, "nibble2:", sizeof("nibble2:") - 1)) {
719 cp
+= sizeof("nibble2:") - 1;
720 j
= MIN(strcspn(cp
, " \t"), sizeof(ext
->nsuffix2
) - 1);
721 strncpy(ext
->nsuffix2
, cp
, j
);
722 ext
->nsuffix2
[j
] = '\0';
724 else if (!strncmp(cp
, "v6revmode:", sizeof("v6revmode:") - 1)) {
725 cp
+= sizeof("v6revmode:") - 1;
726 /* "nibble" and "bitstring" used to be valid */
727 if (!strncmp(cp
, "single", sizeof("single") - 1)) {
728 statp
->options
|= RES_NO_NIBBLE2
;
729 } else if (!strncmp(cp
, "both", sizeof("both") - 1)) {
735 /* XXX - print a warning here? */
738 /* skip to next run of spaces */
739 while (*cp
&& *cp
!= ' ' && *cp
!= '\t')
745 /* XXX - should really support CIDR which means explicit masks always. */
747 net_mask(struct in_addr in
) /*!< XXX - should really use system's version of this */
749 register uint32_t i
= ntohl(in
.s_addr
);
752 return (htonl(IN_CLASSA_NET
));
753 else if (IN_CLASSB(i
))
754 return (htonl(IN_CLASSB_NET
));
755 return (htonl(IN_CLASSC_NET
));
759 static u_char srnd
[16];
762 res_rndinit(res_state statp
)
767 u_char
*rnd
= statp
->_rnd
== NULL
? srnd
: statp
->_rnd
;
769 gettimeofday(&now
, NULL
);
770 u32
= (uint32_t)now
.tv_sec
;
771 memcpy(rnd
, &u32
, 4);
773 memcpy(rnd
+ 4, &u32
, 4);
774 u32
+= (uint32_t)now
.tv_sec
;
775 memcpy(rnd
+ 8, &u32
, 4);
777 memcpy(rnd
+ 12, &u16
, 2);
781 res_nrandomid(res_state statp
)
786 u_char
*rnd
= statp
->_rnd
== NULL
? srnd
: statp
->_rnd
;
788 gettimeofday(&now
, NULL
);
789 u16
= (uint16_t) (now
.tv_sec
^ now
.tv_usec
);
790 memcpy(rnd
+ 14, &u16
, 2);
793 MD5_Update(&ctx
, rnd
, 16);
794 MD5_Final(rnd
, &ctx
);
797 MD5Update(&ctx
, rnd
, 16);
800 memcpy(&u16
, rnd
+ 14, 2);
801 return ((u_int
) u16
);
805 * This routine is for closing the socket if a virtual circuit is used and
806 * the program wants to close it. This provides support for endhostent()
807 * which expects to close the socket.
809 * This routine is not expected to be user visible.
812 res_nclose(res_state statp
)
816 if (statp
->_vcsock
>= 0) {
817 (void) close(statp
->_vcsock
);
819 statp
->_flags
&= ~(RES_F_VC
| RES_F_CONN
);
821 for (ns
= 0; ns
< statp
->_u
._ext
.nscount
; ns
++) {
822 if (statp
->_u
._ext
.nssocks
[ns
] != -1) {
823 (void) close(statp
->_u
._ext
.nssocks
[ns
]);
824 statp
->_u
._ext
.nssocks
[ns
] = -1;
830 res_ndestroy(res_state statp
)
833 if (statp
->_u
._ext
.ext
!= NULL
) {
834 if (statp
->_u
._ext
.ext
->kq
!= -1)
835 (void)close(statp
->_u
._ext
.ext
->kq
);
836 if (statp
->_u
._ext
.ext
->resfd
!= -1)
837 (void)close(statp
->_u
._ext
.ext
->resfd
);
838 free(statp
->_u
._ext
.ext
);
839 statp
->_u
._ext
.ext
= NULL
;
841 if (statp
->_rnd
!= NULL
) {
845 statp
->options
&= ~RES_INIT
;
849 res_get_nibblesuffix(res_state statp
)
851 if (statp
->_u
._ext
.ext
)
852 return (statp
->_u
._ext
.ext
->nsuffix
);
857 res_get_nibblesuffix2(res_state statp
)
859 if (statp
->_u
._ext
.ext
)
860 return (statp
->_u
._ext
.ext
->nsuffix2
);
865 res_setservers(res_state statp
, const union res_sockaddr_union
*set
, int cnt
)
870 /* close open servers */
873 /* cause rtt times to be forgotten */
874 statp
->_u
._ext
.nscount
= 0;
877 for (i
= 0; i
< cnt
&& nserv
< MAXNS
; i
++) {
878 switch (set
->sin
.sin_family
) {
880 size
= sizeof(set
->sin
);
881 if (statp
->_u
._ext
.ext
)
882 memcpy(&statp
->_u
._ext
.ext
->nsaddrs
[nserv
],
884 if (size
<= sizeof(statp
->nsaddr_list
[nserv
]))
885 memcpy(&statp
->nsaddr_list
[nserv
],
888 statp
->nsaddr_list
[nserv
].sin_family
= 0;
892 #ifdef HAS_INET6_STRUCTS
894 size
= sizeof(set
->sin6
);
895 if (statp
->_u
._ext
.ext
)
896 memcpy(&statp
->_u
._ext
.ext
->nsaddrs
[nserv
],
898 if (size
<= sizeof(statp
->nsaddr_list
[nserv
]))
899 memcpy(&statp
->nsaddr_list
[nserv
],
902 statp
->nsaddr_list
[nserv
].sin_family
= 0;
912 statp
->nscount
= nserv
;
917 res_getservers(res_state statp
, union res_sockaddr_union
*set
, int cnt
)
923 for (i
= 0; i
< statp
->nscount
&& i
< cnt
; i
++) {
924 if (statp
->_u
._ext
.ext
)
925 family
= statp
->_u
._ext
.ext
->nsaddrs
[i
].sin
.sin_family
;
927 family
= statp
->nsaddr_list
[i
].sin_family
;
931 size
= sizeof(set
->sin
);
932 if (statp
->_u
._ext
.ext
)
934 &statp
->_u
._ext
.ext
->nsaddrs
[i
],
937 memcpy(&set
->sin
, &statp
->nsaddr_list
[i
],
941 #ifdef HAS_INET6_STRUCTS
943 size
= sizeof(set
->sin6
);
944 if (statp
->_u
._ext
.ext
)
946 &statp
->_u
._ext
.ext
->nsaddrs
[i
],
949 memcpy(&set
->sin6
, &statp
->nsaddr_list
[i
],
955 set
->sin
.sin_family
= 0;
960 return (statp
->nscount
);