Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / resolv / res_init.c
blob42e8b1fb6d7aa39846bc0cb8ad70b5adbbf2efe6
1 /*
2 * Copyright (c) 1985, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 * SOFTWARE.
55 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
56 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
58 * Permission to use, copy, modify, and distribute this software for any
59 * purpose with or without fee is hereby granted, provided that the above
60 * copyright notice and this permission notice appear in all copies.
62 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
63 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
64 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
65 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
66 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
67 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
68 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
71 #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.9.2.7 2004/03/16 12:35:35 marka Exp $";
74 #endif /* LIBC_SCCS and not lint */
76 #include "port_before.h"
78 #include <sys/types.h>
79 #include <sys/param.h>
80 #include <sys/socket.h>
81 #include <sys/time.h>
83 #include <netinet/in.h>
84 #include <arpa/inet.h>
85 #include <arpa/nameser.h>
87 #include <ctype.h>
88 #include <stdio.h>
89 #include <stdlib.h>
90 #include <string.h>
91 #include <unistd.h>
92 #include <netdb.h>
94 #include "port_after.h"
96 /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
97 #include <resolv.h>
99 #include "res_private.h"
101 /* Options. Should all be left alone. */
102 #define RESOLVSORT
103 #define DEBUG
105 static void res_setoptions __P((res_state, const char *, const char *));
107 #ifdef RESOLVSORT
108 static const char sort_mask[] = "/&";
109 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
110 static u_int32_t net_mask __P((struct in_addr));
111 #endif
113 #if !defined(isascii) /* XXX - could be a function */
114 # define isascii(c) (!(c & 0200))
115 #endif
118 * Resolver state default settings.
122 * Set up default settings. If the configuration file exist, the values
123 * there will have precedence. Otherwise, the server address is set to
124 * INADDR_ANY and the default domain name comes from the gethostname().
126 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
127 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
128 * since it was noted that INADDR_ANY actually meant ``the first interface
129 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
130 * it had to be "up" in order for you to reach your own name server. It
131 * was later decided that since the recommended practice is to always
132 * install local static routes through 127.0.0.1 for all your network
133 * interfaces, that we could solve this problem without a code change.
135 * The configuration file should always be used, since it is the only way
136 * to specify a default domain. If you are running a server on your local
137 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
138 * in the configuration file.
140 * Return 0 if completes successfully, -1 on error
143 res_ninit(res_state statp) {
144 extern int __res_vinit(res_state, int);
146 return (__res_vinit(statp, 0));
149 /* This function has to be reachable by res_data.c but not publically. */
151 __res_vinit(res_state statp, int preinit) {
152 register FILE *fp;
153 register char *cp, **pp;
154 register int n;
155 char buf[BUFSIZ];
156 int nserv = 0; /* number of nameserver records read from file */
157 int haveenv = 0;
158 int havesearch = 0;
159 #ifdef RESOLVSORT
160 int nsort = 0;
161 char *net;
162 #endif
163 int dots;
164 union res_sockaddr_union u[2];
166 if (!preinit) {
167 statp->retrans = RES_TIMEOUT;
168 statp->retry = RES_DFLRETRY;
169 statp->options = RES_DEFAULT;
170 statp->id = res_randomid();
173 if ((statp->options & RES_INIT) != 0U)
174 res_ndestroy(statp);
176 memset(u, 0, sizeof(u));
177 #ifdef USELOOPBACK
178 u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
179 #else
180 u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
181 #endif
182 u[nserv].sin.sin_family = AF_INET;
183 u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
184 #ifdef HAVE_SA_LEN
185 u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
186 #endif
187 nserv++;
188 #ifdef HAS_INET6_STRUCTS
189 #ifdef USELOOPBACK
190 u[nserv].sin6.sin6_addr = in6addr_loopback;
191 #else
192 u[nserv].sin6.sin6_addr = in6addr_any;
193 #endif
194 u[nserv].sin6.sin6_family = AF_INET6;
195 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
196 #ifdef HAVE_SA_LEN
197 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
198 #endif
199 nserv++;
200 #endif
201 statp->nscount = 0;
202 statp->ndots = 1;
203 statp->pfcode = 0;
204 statp->_vcsock = -1;
205 statp->_flags = 0;
206 statp->qhook = NULL;
207 statp->rhook = NULL;
208 statp->_u._ext.nscount = 0;
209 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
210 if (statp->_u._ext.ext != NULL) {
211 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
212 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
213 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
214 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
216 #ifdef RESOLVSORT
217 statp->nsort = 0;
218 #endif
219 res_setservers(statp, u, nserv);
221 /* Allow user to override the local domain definition */
222 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
223 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
224 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
225 haveenv++;
228 * Set search list to be blank-separated strings
229 * from rest of env value. Permits users of LOCALDOMAIN
230 * to still have a search list, and anyone to set the
231 * one that they want to use as an individual (even more
232 * important now that the rfc1535 stuff restricts searches)
234 cp = statp->defdname;
235 pp = statp->dnsrch;
236 *pp++ = cp;
237 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
238 if (*cp == '\n') /* silly backwards compat */
239 break;
240 else if (*cp == ' ' || *cp == '\t') {
241 *cp = 0;
242 n = 1;
243 } else if (n) {
244 *pp++ = cp;
245 n = 0;
246 havesearch = 1;
249 /* null terminate last domain if there are excess */
250 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
251 cp++;
252 *cp = '\0';
253 *pp++ = 0;
256 #define MATCH(line, name) \
257 (!strncmp(line, name, sizeof(name) - 1) && \
258 (line[sizeof(name) - 1] == ' ' || \
259 line[sizeof(name) - 1] == '\t'))
261 nserv = 0;
262 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
263 /* read the config file */
264 while (fgets(buf, sizeof(buf), fp) != NULL) {
265 /* skip comments */
266 if (*buf == ';' || *buf == '#')
267 continue;
268 /* read default domain name */
269 if (MATCH(buf, "domain")) {
270 if (haveenv) /* skip if have from environ */
271 continue;
272 cp = buf + sizeof("domain") - 1;
273 while (*cp == ' ' || *cp == '\t')
274 cp++;
275 if ((*cp == '\0') || (*cp == '\n'))
276 continue;
277 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
278 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
279 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
280 *cp = '\0';
281 havesearch = 0;
282 continue;
284 /* set search list */
285 if (MATCH(buf, "search")) {
286 if (haveenv) /* skip if have from environ */
287 continue;
288 cp = buf + sizeof("search") - 1;
289 while (*cp == ' ' || *cp == '\t')
290 cp++;
291 if ((*cp == '\0') || (*cp == '\n'))
292 continue;
293 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
294 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
295 if ((cp = strchr(statp->defdname, '\n')) != NULL)
296 *cp = '\0';
298 * Set search list to be blank-separated strings
299 * on rest of line.
301 cp = statp->defdname;
302 pp = statp->dnsrch;
303 *pp++ = cp;
304 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
305 if (*cp == ' ' || *cp == '\t') {
306 *cp = 0;
307 n = 1;
308 } else if (n) {
309 *pp++ = cp;
310 n = 0;
313 /* null terminate last domain if there are excess */
314 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
315 cp++;
316 *cp = '\0';
317 *pp++ = 0;
318 havesearch = 1;
319 continue;
321 /* read nameservers to query */
322 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
323 struct addrinfo hints, *ai;
324 char sbuf[NI_MAXSERV];
325 const size_t minsiz =
326 sizeof(statp->_u._ext.ext->nsaddrs[0]);
328 cp = buf + sizeof("nameserver") - 1;
329 while (*cp == ' ' || *cp == '\t')
330 cp++;
331 cp[strcspn(cp, ";# \t\n")] = '\0';
332 if ((*cp != '\0') && (*cp != '\n')) {
333 memset(&hints, 0, sizeof(hints));
334 hints.ai_family = PF_UNSPEC;
335 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
336 hints.ai_flags = AI_NUMERICHOST;
337 sprintf(sbuf, "%u", NAMESERVER_PORT);
338 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
339 ai->ai_addrlen <= minsiz) {
340 if (statp->_u._ext.ext != NULL) {
341 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
342 ai->ai_addr, ai->ai_addrlen);
344 if (ai->ai_addrlen <=
345 sizeof(statp->nsaddr_list[nserv])) {
346 memcpy(&statp->nsaddr_list[nserv],
347 ai->ai_addr, ai->ai_addrlen);
348 } else
349 statp->nsaddr_list[nserv].sin_family = 0;
350 freeaddrinfo(ai);
351 nserv++;
354 continue;
356 #ifdef RESOLVSORT
357 if (MATCH(buf, "sortlist")) {
358 struct in_addr a;
360 cp = buf + sizeof("sortlist") - 1;
361 while (nsort < MAXRESOLVSORT) {
362 while (*cp == ' ' || *cp == '\t')
363 cp++;
364 if (*cp == '\0' || *cp == '\n' || *cp == ';')
365 break;
366 net = cp;
367 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
368 isascii(*cp) && !isspace((unsigned char)*cp))
369 cp++;
370 n = *cp;
371 *cp = 0;
372 if (inet_aton(net, &a)) {
373 statp->sort_list[nsort].addr = a;
374 if (ISSORTMASK(n)) {
375 *cp++ = n;
376 net = cp;
377 while (*cp && *cp != ';' &&
378 isascii(*cp) &&
379 !isspace((unsigned char)*cp))
380 cp++;
381 n = *cp;
382 *cp = 0;
383 if (inet_aton(net, &a)) {
384 statp->sort_list[nsort].mask = a.s_addr;
385 } else {
386 statp->sort_list[nsort].mask =
387 net_mask(statp->sort_list[nsort].addr);
389 } else {
390 statp->sort_list[nsort].mask =
391 net_mask(statp->sort_list[nsort].addr);
393 nsort++;
395 *cp = n;
397 continue;
399 #endif
400 if (MATCH(buf, "options")) {
401 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
402 continue;
405 if (nserv > 0)
406 statp->nscount = nserv;
407 #ifdef RESOLVSORT
408 statp->nsort = nsort;
409 #endif
410 (void) fclose(fp);
413 * Last chance to get a nameserver. This should not normally
414 * be necessary
416 #ifdef NO_RESOLV_CONF
417 if(nserv == 0)
418 nserv = get_nameservers(statp);
419 #endif
421 if (statp->defdname[0] == 0 &&
422 gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
423 (cp = strchr(buf, '.')) != NULL)
424 strcpy(statp->defdname, cp + 1);
426 /* find components of local domain that might be searched */
427 if (havesearch == 0) {
428 pp = statp->dnsrch;
429 *pp++ = statp->defdname;
430 *pp = NULL;
432 dots = 0;
433 for (cp = statp->defdname; *cp; cp++)
434 dots += (*cp == '.');
436 cp = statp->defdname;
437 while (pp < statp->dnsrch + MAXDFLSRCH) {
438 if (dots < LOCALDOMAINPARTS)
439 break;
440 cp = strchr(cp, '.') + 1; /* we know there is one */
441 *pp++ = cp;
442 dots--;
444 *pp = NULL;
445 #ifdef DEBUG
446 if (statp->options & RES_DEBUG) {
447 printf(";; res_init()... default dnsrch list:\n");
448 for (pp = statp->dnsrch; *pp; pp++)
449 printf(";;\t%s\n", *pp);
450 printf(";;\t..END..\n");
452 #endif
455 if ((cp = getenv("RES_OPTIONS")) != NULL)
456 res_setoptions(statp, cp, "env");
457 statp->options |= RES_INIT;
458 return (0);
461 static void
462 res_setoptions(res_state statp, const char *options, const char *source)
464 const char *cp = options;
465 int i;
466 struct __res_state_ext *ext = statp->_u._ext.ext;
468 #ifdef DEBUG
469 if (statp->options & RES_DEBUG)
470 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
471 options, source);
472 #endif
473 while (*cp) {
474 /* skip leading and inner runs of spaces */
475 while (*cp == ' ' || *cp == '\t')
476 cp++;
477 /* search for and process individual options */
478 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
479 i = atoi(cp + sizeof("ndots:") - 1);
480 if (i <= RES_MAXNDOTS)
481 statp->ndots = i;
482 else
483 statp->ndots = RES_MAXNDOTS;
484 #ifdef DEBUG
485 if (statp->options & RES_DEBUG)
486 printf(";;\tndots=%d\n", statp->ndots);
487 #endif
488 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
489 i = atoi(cp + sizeof("timeout:") - 1);
490 if (i <= RES_MAXRETRANS)
491 statp->retrans = i;
492 else
493 statp->retrans = RES_MAXRETRANS;
494 #ifdef DEBUG
495 if (statp->options & RES_DEBUG)
496 printf(";;\ttimeout=%d\n", statp->retrans);
497 #endif
498 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
499 i = atoi(cp + sizeof("attempts:") - 1);
500 if (i <= RES_MAXRETRY)
501 statp->retry = i;
502 else
503 statp->retry = RES_MAXRETRY;
504 #ifdef DEBUG
505 if (statp->options & RES_DEBUG)
506 printf(";;\tattempts=%d\n", statp->retry);
507 #endif
508 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
509 #ifdef DEBUG
510 if (!(statp->options & RES_DEBUG)) {
511 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
512 options, source);
513 statp->options |= RES_DEBUG;
515 printf(";;\tdebug\n");
516 #endif
517 } else if (!strncmp(cp, "no_tld_query",
518 sizeof("no_tld_query") - 1) ||
519 !strncmp(cp, "no-tld-query",
520 sizeof("no-tld-query") - 1)) {
521 statp->options |= RES_NOTLDQUERY;
522 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
523 statp->options |= RES_USE_INET6;
524 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
525 statp->options |= RES_ROTATE;
526 } else if (!strncmp(cp, "no-check-names",
527 sizeof("no-check-names") - 1)) {
528 statp->options |= RES_NOCHECKNAME;
530 #ifdef RES_USE_EDNS0
531 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
532 statp->options |= RES_USE_EDNS0;
534 #endif
535 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
536 statp->options |= RES_USE_DNAME;
538 else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
539 if (ext == NULL)
540 goto skip;
541 cp += sizeof("nibble:") - 1;
542 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
543 strncpy(ext->nsuffix, cp, i);
544 ext->nsuffix[i] = '\0';
546 else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
547 if (ext == NULL)
548 goto skip;
549 cp += sizeof("nibble2:") - 1;
550 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
551 strncpy(ext->nsuffix2, cp, i);
552 ext->nsuffix2[i] = '\0';
554 else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
555 cp += sizeof("v6revmode:") - 1;
556 /* "nibble" and "bitstring" used to be valid */
557 if (!strncmp(cp, "single", sizeof("single") - 1)) {
558 statp->options |= RES_NO_NIBBLE2;
559 } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
560 statp->options &=
561 ~RES_NO_NIBBLE2;
564 else {
565 /* XXX - print a warning here? */
567 skip:
568 /* skip to next run of spaces */
569 while (*cp && *cp != ' ' && *cp != '\t')
570 cp++;
574 #ifdef RESOLVSORT
575 /* XXX - should really support CIDR which means explicit masks always. */
576 static u_int32_t
577 net_mask(in) /* XXX - should really use system's version of this */
578 struct in_addr in;
580 register u_int32_t i = ntohl(in.s_addr);
582 if (IN_CLASSA(i))
583 return (htonl(IN_CLASSA_NET));
584 else if (IN_CLASSB(i))
585 return (htonl(IN_CLASSB_NET));
586 return (htonl(IN_CLASSC_NET));
588 #endif
590 u_int
591 res_randomid(void) {
592 struct timeval now;
594 gettimeofday(&now, NULL);
595 return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
599 * This routine is for closing the socket if a virtual circuit is used and
600 * the program wants to close it. This provides support for endhostent()
601 * which expects to close the socket.
603 * This routine is not expected to be user visible.
605 void
606 res_nclose(res_state statp) {
607 int ns;
609 if (statp->_vcsock >= 0) {
610 (void) close(statp->_vcsock);
611 statp->_vcsock = -1;
612 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
614 for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
615 if (statp->_u._ext.nssocks[ns] != -1) {
616 (void) close(statp->_u._ext.nssocks[ns]);
617 statp->_u._ext.nssocks[ns] = -1;
622 void
623 res_ndestroy(res_state statp) {
624 res_nclose(statp);
625 if (statp->_u._ext.ext != NULL)
626 free(statp->_u._ext.ext);
627 statp->options &= ~RES_INIT;
628 statp->_u._ext.ext = NULL;
631 const char *
632 res_get_nibblesuffix(res_state statp) {
633 if (statp->_u._ext.ext)
634 return (statp->_u._ext.ext->nsuffix);
635 return ("ip6.arpa");
638 const char *
639 res_get_nibblesuffix2(res_state statp) {
640 if (statp->_u._ext.ext)
641 return (statp->_u._ext.ext->nsuffix2);
642 return ("ip6.int");
645 void
646 res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) {
647 int i, nserv;
648 size_t size;
650 /* close open servers */
651 res_nclose(statp);
653 /* cause rtt times to be forgotten */
654 statp->_u._ext.nscount = 0;
656 nserv = 0;
657 for (i = 0; i < cnt && nserv < MAXNS; i++) {
658 switch (set->sin.sin_family) {
659 case AF_INET:
660 size = sizeof(set->sin);
661 if (statp->_u._ext.ext)
662 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
663 &set->sin, size);
664 if (size <= sizeof(statp->nsaddr_list[nserv]))
665 memcpy(&statp->nsaddr_list[nserv],
666 &set->sin, size);
667 else
668 statp->nsaddr_list[nserv].sin_family = 0;
669 nserv++;
670 break;
672 #ifdef HAS_INET6_STRUCTS
673 case AF_INET6:
674 size = sizeof(set->sin6);
675 if (statp->_u._ext.ext)
676 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
677 &set->sin6, size);
678 if (size <= sizeof(statp->nsaddr_list[nserv]))
679 memcpy(&statp->nsaddr_list[nserv],
680 &set->sin6, size);
681 else
682 statp->nsaddr_list[nserv].sin_family = 0;
683 nserv++;
684 break;
685 #endif
687 default:
688 break;
690 set++;
692 statp->nscount = nserv;
697 res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) {
698 int i;
699 size_t size;
700 u_int16_t family;
702 for (i = 0; i < statp->nscount && i < cnt; i++) {
703 if (statp->_u._ext.ext)
704 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
705 else
706 family = statp->nsaddr_list[i].sin_family;
708 switch (family) {
709 case AF_INET:
710 size = sizeof(set->sin);
711 if (statp->_u._ext.ext)
712 memcpy(&set->sin,
713 &statp->_u._ext.ext->nsaddrs[i],
714 size);
715 else
716 memcpy(&set->sin, &statp->nsaddr_list[i],
717 size);
718 break;
720 #ifdef HAS_INET6_STRUCTS
721 case AF_INET6:
722 size = sizeof(set->sin6);
723 if (statp->_u._ext.ext)
724 memcpy(&set->sin6,
725 &statp->_u._ext.ext->nsaddrs[i],
726 size);
727 else
728 memcpy(&set->sin6, &statp->nsaddr_list[i],
729 size);
730 break;
731 #endif
733 default:
734 set->sin.sin_family = 0;
735 break;
737 set++;
739 return (statp->nscount);