2 * SPDX-License-Identifier: (BSD-3-Clause AND ISC)
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 #if defined(LIBC_SCCS) && !defined(lint)
70 static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
71 static const char rcsid[] = "$Id: res_init.c,v 1.26 2008/12/11 09:59:00 marka Exp $";
72 #endif /* LIBC_SCCS and not lint */
73 #include <sys/cdefs.h>
74 #include "port_before.h"
76 #include "namespace.h"
78 #include <sys/param.h>
79 #include <sys/socket.h>
83 #include <netinet/in.h>
84 #include <arpa/inet.h>
85 #include <arpa/nameser.h>
95 # include "../dst/md5.h"
104 # define _MD5_H_ 1 /*%< make sure we do not include rsaref md5.h file */
107 #include "un-namespace.h"
109 #include "port_after.h"
111 /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
114 #include "res_private.h"
116 /*% Options. Should all be left alone. */
123 #include <sys/systeminfo.h>
126 static void res_setoptions(res_state, const char *, const char *);
129 static const char sort_mask[] = "/&";
130 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
131 static u_int32_t net_mask(struct in_addr);
134 #if !defined(isascii) /*%< XXX - could be a function */
135 # define isascii(c) (!(c & 0200))
139 * Resolver state default settings.
143 * Set up default settings. If the configuration file exist, the values
144 * there will have precedence. Otherwise, the server address is set to
145 * INADDR_ANY and the default domain name comes from the gethostname().
147 * An interim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
148 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
149 * since it was noted that INADDR_ANY actually meant ``the first interface
150 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
151 * it had to be "up" in order for you to reach your own name server. It
152 * was later decided that since the recommended practice is to always
153 * install local static routes through 127.0.0.1 for all your network
154 * interfaces, that we could solve this problem without a code change.
156 * The configuration file should always be used, since it is the only way
157 * to specify a default domain. If you are running a server on your local
158 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
159 * in the configuration file.
161 * Return 0 if completes successfully, -1 on error
164 res_ninit(res_state statp) {
165 extern int __res_vinit(res_state, int);
167 return (__res_vinit(statp, 0));
170 /*% This function has to be reachable by res_data.c but not publicly. */
172 __res_vinit(res_state statp, int preinit) {
177 int nserv = 0; /*%< number of nameserver records read from file */
185 union res_sockaddr_union u[2];
188 RES_SET_H_ERRNO(statp, 0);
189 if (statp->_u._ext.ext != NULL)
193 statp->retrans = RES_TIMEOUT;
194 statp->retry = RES_DFLRETRY;
195 statp->options = RES_DEFAULT;
198 statp->_rnd = malloc(16);
200 statp->id = res_nrandomid(statp);
202 memset(u, 0, sizeof(u));
203 u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
204 u[nserv].sin.sin_family = AF_INET;
205 u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
207 u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
210 #ifdef HAS_INET6_STRUCTS
211 u[nserv].sin6.sin6_addr = in6addr_any;
212 u[nserv].sin6.sin6_family = AF_INET6;
213 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
215 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
226 statp->_u._ext.nscount = 0;
227 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
228 if (statp->_u._ext.ext != NULL) {
229 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
230 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
231 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
232 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
233 statp->_u._ext.ext->reload_period = 2;
236 * Historically res_init() rarely, if at all, failed.
237 * Examples and applications exist which do not check
238 * our return code. Furthermore several applications
239 * simply call us to get the systems domainname. So
240 * rather then immediately fail here we store the
241 * failure, which is returned later, in h_errno. And
242 * prevent the collection of 'nameserver' information
243 * by setting maxns to 0. Thus applications that fail
244 * to check our return code wont be able to make
247 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
253 res_setservers(statp, u, nserv);
257 * The old libresolv derived the defaultdomain from NIS/NIS+.
258 * We want to keep this behaviour
261 char buf[sizeof(statp->defdname)], *cp;
264 if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 &&
265 (unsigned int)ret <= sizeof(buf)) {
268 cp = strchr(buf, '.');
269 cp = (cp == NULL) ? buf : (cp + 1);
270 strncpy(statp->defdname, cp,
271 sizeof(statp->defdname) - 1);
272 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
275 #endif /* SOLARIS2 */
277 /* Allow user to override the local domain definition */
278 if ((cp = secure_getenv("LOCALDOMAIN")) != NULL) {
279 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
280 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
284 * Set search list to be blank-separated strings
285 * from rest of env value. Permits users of LOCALDOMAIN
286 * to still have a search list, and anyone to set the
287 * one that they want to use as an individual (even more
288 * important now that the rfc1535 stuff restricts searches)
290 cp = statp->defdname;
293 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
294 if (*cp == '\n') /*%< silly backwards compat */
296 else if (*cp == ' ' || *cp == '\t') {
305 /* null terminate last domain if there are excess */
306 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
312 #define MATCH(line, name) \
313 (!strncmp(line, name, sizeof(name) - 1) && \
314 (line[sizeof(name) - 1] == ' ' || \
315 line[sizeof(name) - 1] == '\t'))
318 if ((fp = fopen(_PATH_RESCONF, "re")) != NULL) {
322 if (statp->_u._ext.ext != NULL) {
323 if (_fstat(fileno(fp), &sb) == 0) {
324 statp->_u._ext.ext->conf_mtim = sb.st_mtim;
325 if (clock_gettime(CLOCK_MONOTONIC_FAST, &now) == 0) {
326 statp->_u._ext.ext->conf_stat = now.tv_sec;
331 /* read the config file */
332 while (fgets(buf, sizeof(buf), fp) != NULL) {
334 if (*buf == ';' || *buf == '#')
336 /* read default domain name */
337 if (MATCH(buf, "domain")) {
338 if (haveenv) /*%< skip if have from environ */
340 cp = buf + sizeof("domain") - 1;
341 while (*cp == ' ' || *cp == '\t')
343 if ((*cp == '\0') || (*cp == '\n'))
345 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
346 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
347 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
352 /* set search list */
353 if (MATCH(buf, "search")) {
354 if (haveenv) /*%< skip if have from environ */
356 cp = buf + sizeof("search") - 1;
357 while (*cp == ' ' || *cp == '\t')
359 if ((*cp == '\0') || (*cp == '\n'))
361 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
362 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
363 if ((cp = strchr(statp->defdname, '\n')) != NULL)
366 * Set search list to be blank-separated strings
369 cp = statp->defdname;
372 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
373 if (*cp == ' ' || *cp == '\t') {
381 /* null terminate last domain if there are excess */
382 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
389 /* read nameservers to query */
390 if (MATCH(buf, "nameserver") && nserv < maxns) {
391 struct addrinfo hints, *ai;
392 char sbuf[NI_MAXSERV];
393 const size_t minsiz =
394 sizeof(statp->_u._ext.ext->nsaddrs[0]);
396 cp = buf + sizeof("nameserver") - 1;
397 while (*cp == ' ' || *cp == '\t')
399 cp[strcspn(cp, ";# \t\n")] = '\0';
400 if ((*cp != '\0') && (*cp != '\n')) {
401 memset(&hints, 0, sizeof(hints));
402 hints.ai_family = PF_UNSPEC;
403 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
404 hints.ai_flags = AI_NUMERICHOST;
405 sprintf(sbuf, "%u", NAMESERVER_PORT);
406 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0) {
407 if (ai->ai_addrlen <= minsiz) {
408 if (statp->_u._ext.ext != NULL) {
409 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
410 ai->ai_addr, ai->ai_addrlen);
412 if (ai->ai_addrlen <=
413 sizeof(statp->nsaddr_list[nserv])) {
414 memcpy(&statp->nsaddr_list[nserv],
415 ai->ai_addr, ai->ai_addrlen);
417 statp->nsaddr_list[nserv].sin_family = 0;
426 if (MATCH(buf, "sortlist")) {
431 struct __res_state_ext *ext = statp->_u._ext.ext;
433 cp = buf + sizeof("sortlist") - 1;
434 while (nsort < MAXRESOLVSORT) {
435 while (*cp == ' ' || *cp == '\t')
437 if (*cp == '\0' || *cp == '\n' || *cp == ';')
440 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
441 isascii(*cp) && !isspace((unsigned char)*cp))
445 if (inet_aton(net, &a)) {
446 statp->sort_list[nsort].addr = a;
450 while (*cp && *cp != ';' &&
452 !isspace((unsigned char)*cp))
456 if (inet_aton(net, &a)) {
457 statp->sort_list[nsort].mask = a.s_addr;
459 statp->sort_list[nsort].mask =
460 net_mask(statp->sort_list[nsort].addr);
463 statp->sort_list[nsort].mask =
464 net_mask(statp->sort_list[nsort].addr);
466 ext->sort_list[nsort].af = AF_INET;
467 ext->sort_list[nsort].addr.ina =
468 statp->sort_list[nsort].addr;
469 ext->sort_list[nsort].mask.ina.s_addr =
470 statp->sort_list[nsort].mask;
473 else if (inet_pton(AF_INET6, net, &a6) == 1) {
475 ext->sort_list[nsort].af = AF_INET6;
476 ext->sort_list[nsort].addr.in6a = a6;
477 u = (u_char *)&ext->sort_list[nsort].mask.in6a;
480 while (*cp && *cp != ';' &&
481 isascii(*cp) && !isspace(*cp))
491 if (inet_pton(AF_INET6, net, u) == 1) {
497 m = sizeof(struct in6_addr) * CHAR_BIT;
501 for (i = 0; i < sizeof(struct in6_addr); i++) {
513 statp->sort_list[nsort].addr.s_addr =
514 (u_int32_t)0xffffffff;
515 statp->sort_list[nsort].mask =
516 (u_int32_t)0xffffffff;
524 if (MATCH(buf, "options")) {
525 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
530 statp->nscount = nserv;
532 statp->nsort = nsort;
537 * Last chance to get a nameserver. This should not normally
540 #ifdef NO_RESOLV_CONF
542 nserv = get_nameservers(statp);
545 if (statp->defdname[0] == 0 &&
546 gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
547 (cp = strchr(buf, '.')) != NULL)
548 strcpy(statp->defdname, cp + 1);
550 /* find components of local domain that might be searched */
551 if (havesearch == 0) {
553 *pp++ = statp->defdname;
557 for (cp = statp->defdname; *cp; cp++)
558 dots += (*cp == '.');
560 cp = statp->defdname;
561 while (pp < statp->dnsrch + MAXDFLSRCH) {
562 if (dots < LOCALDOMAINPARTS)
564 cp = strchr(cp, '.') + 1; /*%< we know there is one */
570 if (statp->options & RES_DEBUG) {
571 printf(";; res_init()... default dnsrch list:\n");
572 for (pp = statp->dnsrch; *pp; pp++)
573 printf(";;\t%s\n", *pp);
574 printf(";;\t..END..\n");
580 statp->options |= RES_NOALIASES;
581 else if ((cp = getenv("RES_OPTIONS")) != NULL)
582 res_setoptions(statp, cp, "env");
583 statp->options |= RES_INIT;
584 return (statp->res_h_errno);
588 res_setoptions(res_state statp, const char *options, const char *source)
590 const char *cp = options;
592 struct __res_state_ext *ext = statp->_u._ext.ext;
595 if (statp->options & RES_DEBUG)
596 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
600 /* skip leading and inner runs of spaces */
601 while (*cp == ' ' || *cp == '\t')
603 /* search for and process individual options */
604 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
605 i = atoi(cp + sizeof("ndots:") - 1);
606 if (i <= RES_MAXNDOTS)
609 statp->ndots = RES_MAXNDOTS;
611 if (statp->options & RES_DEBUG)
612 printf(";;\tndots=%d\n", statp->ndots);
614 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
615 i = atoi(cp + sizeof("timeout:") - 1);
616 if (i <= RES_MAXRETRANS)
619 statp->retrans = RES_MAXRETRANS;
621 if (statp->options & RES_DEBUG)
622 printf(";;\ttimeout=%d\n", statp->retrans);
625 } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) {
627 * For backward compatibility, 'retrans' is
628 * supported as an alias for 'timeout', though
629 * without an imposed maximum.
631 statp->retrans = atoi(cp + sizeof("retrans:") - 1);
632 } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){
634 * For backward compatibility, 'retry' is
635 * supported as an alias for 'attempts', though
636 * without an imposed maximum.
638 statp->retry = atoi(cp + sizeof("retry:") - 1);
639 #endif /* SOLARIS2 */
640 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
641 i = atoi(cp + sizeof("attempts:") - 1);
642 if (i <= RES_MAXRETRY)
645 statp->retry = RES_MAXRETRY;
647 if (statp->options & RES_DEBUG)
648 printf(";;\tattempts=%d\n", statp->retry);
650 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
652 if (!(statp->options & RES_DEBUG)) {
653 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
655 statp->options |= RES_DEBUG;
657 printf(";;\tdebug\n");
659 } else if (!strncmp(cp, "no_tld_query",
660 sizeof("no_tld_query") - 1) ||
661 !strncmp(cp, "no-tld-query",
662 sizeof("no-tld-query") - 1)) {
663 statp->options |= RES_NOTLDQUERY;
664 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
665 statp->options |= RES_USE_INET6;
666 } else if (!strncmp(cp, "insecure1", sizeof("insecure1") - 1)) {
667 statp->options |= RES_INSECURE1;
668 } else if (!strncmp(cp, "insecure2", sizeof("insecure2") - 1)) {
669 statp->options |= RES_INSECURE2;
670 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
671 statp->options |= RES_ROTATE;
672 } else if (!strncmp(cp, "usevc", sizeof("usevc") - 1)) {
673 statp->options |= RES_USEVC;
674 } else if (!strncmp(cp, "no-check-names",
675 sizeof("no-check-names") - 1)) {
676 statp->options |= RES_NOCHECKNAME;
677 } else if (!strncmp(cp, "reload-period:",
678 sizeof("reload-period:") - 1)) {
680 ext->reload_period = (u_short)
681 atoi(cp + sizeof("reload-period:") - 1);
685 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
686 statp->options |= RES_USE_EDNS0;
690 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
691 statp->options |= RES_USE_DNAME;
693 else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
696 cp += sizeof("nibble:") - 1;
697 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
698 strncpy(ext->nsuffix, cp, i);
699 ext->nsuffix[i] = '\0';
701 else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
704 cp += sizeof("nibble2:") - 1;
705 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
706 strncpy(ext->nsuffix2, cp, i);
707 ext->nsuffix2[i] = '\0';
709 else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
710 cp += sizeof("v6revmode:") - 1;
711 /* "nibble" and "bitstring" used to be valid */
712 if (!strncmp(cp, "single", sizeof("single") - 1)) {
713 statp->options |= RES_NO_NIBBLE2;
714 } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
721 /* XXX - print a warning here? */
726 /* skip to next run of spaces */
727 while (*cp && *cp != ' ' && *cp != '\t')
733 /* XXX - should really support CIDR which means explicit masks always. */
735 net_mask(struct in_addr in) /*!< XXX - should really use system's version of this */
737 u_int32_t i = ntohl(in.s_addr);
740 return (htonl(IN_CLASSA_NET));
741 else if (IN_CLASSB(i))
742 return (htonl(IN_CLASSB_NET));
743 return (htonl(IN_CLASSC_NET));
747 static u_char srnd[16];
750 res_rndinit(res_state statp)
755 u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd;
757 gettimeofday(&now, NULL);
759 memcpy(rnd, &u32, 4);
761 memcpy(rnd + 4, &u32, 4);
763 memcpy(rnd + 8, &u32, 4);
765 memcpy(rnd + 12, &u16, 2);
769 res_nrandomid(res_state statp) {
773 u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd;
775 gettimeofday(&now, NULL);
776 u16 = (u_int16_t) (now.tv_sec ^ now.tv_usec);
777 memcpy(rnd + 14, &u16, 2);
780 MD5_Update(&ctx, rnd, 16);
781 MD5_Final(rnd, &ctx);
784 MD5Update(&ctx, rnd, 16);
787 memcpy(&u16, rnd + 14, 2);
788 return ((u_int) u16);
792 * This routine is for closing the socket if a virtual circuit is used and
793 * the program wants to close it. This provides support for endhostent()
794 * which expects to close the socket.
796 * This routine is not expected to be user visible.
799 res_nclose(res_state statp) {
802 if (statp->_vcsock >= 0) {
803 (void) _close(statp->_vcsock);
805 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
807 for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
808 if (statp->_u._ext.nssocks[ns] != -1) {
809 (void) _close(statp->_u._ext.nssocks[ns]);
810 statp->_u._ext.nssocks[ns] = -1;
816 res_ndestroy(res_state statp) {
818 if (statp->_u._ext.ext != NULL) {
819 free(statp->_u._ext.ext);
820 statp->_u._ext.ext = NULL;
822 if (statp->_rnd != NULL) {
826 statp->options &= ~RES_INIT;
831 res_get_nibblesuffix(res_state statp) {
832 if (statp->_u._ext.ext)
833 return (statp->_u._ext.ext->nsuffix);
838 res_get_nibblesuffix2(res_state statp) {
839 if (statp->_u._ext.ext)
840 return (statp->_u._ext.ext->nsuffix2);
846 res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) {
850 /* close open servers */
853 /* cause rtt times to be forgotten */
854 statp->_u._ext.nscount = 0;
857 for (i = 0; i < cnt && nserv < MAXNS; i++) {
858 switch (set->sin.sin_family) {
860 size = sizeof(set->sin);
861 if (statp->_u._ext.ext)
862 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
864 if (size <= sizeof(statp->nsaddr_list[nserv]))
865 memcpy(&statp->nsaddr_list[nserv],
868 statp->nsaddr_list[nserv].sin_family = 0;
872 #ifdef HAS_INET6_STRUCTS
874 size = sizeof(set->sin6);
875 if (statp->_u._ext.ext)
876 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
878 if (size <= sizeof(statp->nsaddr_list[nserv]))
879 memcpy(&statp->nsaddr_list[nserv],
882 statp->nsaddr_list[nserv].sin_family = 0;
892 statp->nscount = nserv;
897 res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) {
902 for (i = 0; i < statp->nscount && i < cnt; i++) {
903 if (statp->_u._ext.ext)
904 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
906 family = statp->nsaddr_list[i].sin_family;
910 size = sizeof(set->sin);
911 if (statp->_u._ext.ext)
913 &statp->_u._ext.ext->nsaddrs[i],
916 memcpy(&set->sin, &statp->nsaddr_list[i],
920 #ifdef HAS_INET6_STRUCTS
922 size = sizeof(set->sin6);
923 if (statp->_u._ext.ext)
925 &statp->_u._ext.ext->nsaddrs[i],
928 memcpy(&set->sin6, &statp->nsaddr_list[i],
934 set->sin.sin_family = 0;
939 return (statp->nscount);