res_init.c revision 1.3 1 /* $NetBSD: res_init.c,v 1.3 2004/05/20 19:43:39 christos Exp $ */
2
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.
22 *
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.
34 */
35
36 /*
37 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
38 *
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.
45 *
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.
54 */
55
56 /*
57 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
58 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
59 *
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.
63 *
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.
71 */
72
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.9.2.5.4.2 2004/03/16 12:34:18 marka Exp";
78 #else
79 __RCSID("$NetBSD: res_init.c,v 1.3 2004/05/20 19:43:39 christos Exp $");
80 #endif
81 #endif /* LIBC_SCCS and not lint */
82
83 #include "port_before.h"
84
85 #include <sys/types.h>
86 #include <sys/param.h>
87 #include <sys/socket.h>
88 #include <sys/time.h>
89
90 #include <netinet/in.h>
91 #include <arpa/inet.h>
92 #include <arpa/nameser.h>
93
94 #include <ctype.h>
95 #include <stdio.h>
96 #include <stdlib.h>
97 #include <string.h>
98 #include <unistd.h>
99 #include <netdb.h>
100
101 #include "port_after.h"
102
103 /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
104 #include <resolv.h>
105
106 #include "res_private.h"
107
108 /* Options. Should all be left alone. */
109 #define RESOLVSORT
110 #define DEBUG
111
112 static void res_setoptions __P((res_state, const char *, const char *));
113
114 #ifdef RESOLVSORT
115 static const char sort_mask[] = "/&";
116 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
117 static u_int32_t net_mask __P((struct in_addr));
118 #endif
119
120 #if !defined(isascii) /* XXX - could be a function */
121 # define isascii(c) (!(c & 0200))
122 #endif
123
124 /*
125 * Resolver state default settings.
126 */
127
128 /*
129 * Set up default settings. If the configuration file exist, the values
130 * there will have precedence. Otherwise, the server address is set to
131 * INADDR_ANY and the default domain name comes from the gethostname().
132 *
133 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
134 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
135 * since it was noted that INADDR_ANY actually meant ``the first interface
136 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
137 * it had to be "up" in order for you to reach your own name server. It
138 * was later decided that since the recommended practice is to always
139 * install local static routes through 127.0.0.1 for all your network
140 * interfaces, that we could solve this problem without a code change.
141 *
142 * The configuration file should always be used, since it is the only way
143 * to specify a default domain. If you are running a server on your local
144 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
145 * in the configuration file.
146 *
147 * Return 0 if completes successfully, -1 on error
148 */
149 int
150 res_ninit(res_state statp) {
151 extern int __res_vinit(res_state, int);
152
153 return (__res_vinit(statp, 0));
154 }
155
156 /* This function has to be reachable by res_data.c but not publically. */
157 int
158 __res_vinit(res_state statp, int preinit) {
159 register FILE *fp;
160 register char *cp, **pp;
161 register int n;
162 char buf[BUFSIZ];
163 int nserv = 0; /* number of nameserver records read from file */
164 int haveenv = 0;
165 int havesearch = 0;
166 #ifdef RESOLVSORT
167 int nsort = 0;
168 char *net;
169 #endif
170 int dots;
171 union res_sockaddr_union u[2];
172
173 if (!preinit) {
174 statp->retrans = RES_TIMEOUT;
175 statp->retry = RES_DFLRETRY;
176 statp->options = RES_DEFAULT;
177 statp->id = res_randomid();
178 }
179
180 if ((statp->options & RES_INIT) != 0U)
181 res_ndestroy(statp);
182
183 memset(u, 0, sizeof(u));
184 #ifdef USELOOPBACK
185 u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
186 #else
187 u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
188 #endif
189 u[nserv].sin.sin_family = AF_INET;
190 u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
191 #ifdef HAVE_SA_LEN
192 u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
193 #endif
194 nserv++;
195 #ifdef HAS_INET6_STRUCTS
196 #ifdef USELOOPBACK
197 u[nserv].sin6.sin6_addr = in6addr_loopback;
198 #else
199 u[nserv].sin6.sin6_addr = in6addr_any;
200 #endif
201 u[nserv].sin6.sin6_family = AF_INET6;
202 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
203 #ifdef HAVE_SA_LEN
204 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
205 #endif
206 nserv++;
207 #endif
208 statp->nscount = 0;
209 statp->ndots = 1;
210 statp->pfcode = 0;
211 statp->_vcsock = -1;
212 statp->_flags = 0;
213 statp->qhook = NULL;
214 statp->rhook = NULL;
215 statp->_u._ext.nscount = 0;
216 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
217 if (statp->_u._ext.ext != NULL) {
218 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
219 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
220 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
221 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
222 }
223 #ifdef RESOLVSORT
224 statp->nsort = 0;
225 #endif
226 res_setservers(statp, u, nserv);
227
228 /* Allow user to override the local domain definition */
229 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
230 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
231 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
232 haveenv++;
233
234 /*
235 * Set search list to be blank-separated strings
236 * from rest of env value. Permits users of LOCALDOMAIN
237 * to still have a search list, and anyone to set the
238 * one that they want to use as an individual (even more
239 * important now that the rfc1535 stuff restricts searches)
240 */
241 cp = statp->defdname;
242 pp = statp->dnsrch;
243 *pp++ = cp;
244 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
245 if (*cp == '\n') /* silly backwards compat */
246 break;
247 else if (*cp == ' ' || *cp == '\t') {
248 *cp = 0;
249 n = 1;
250 } else if (n) {
251 *pp++ = cp;
252 n = 0;
253 havesearch = 1;
254 }
255 }
256 /* null terminate last domain if there are excess */
257 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
258 cp++;
259 *cp = '\0';
260 *pp++ = 0;
261 }
262
263 #define MATCH(line, name) \
264 (!strncmp(line, name, sizeof(name) - 1) && \
265 (line[sizeof(name) - 1] == ' ' || \
266 line[sizeof(name) - 1] == '\t'))
267
268 nserv = 0;
269 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
270 /* read the config file */
271 while (fgets(buf, sizeof(buf), fp) != NULL) {
272 /* skip comments */
273 if (*buf == ';' || *buf == '#')
274 continue;
275 /* read default domain name */
276 if (MATCH(buf, "domain")) {
277 if (haveenv) /* skip if have from environ */
278 continue;
279 cp = buf + sizeof("domain") - 1;
280 while (*cp == ' ' || *cp == '\t')
281 cp++;
282 if ((*cp == '\0') || (*cp == '\n'))
283 continue;
284 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
285 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
286 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
287 *cp = '\0';
288 havesearch = 0;
289 continue;
290 }
291 /* set search list */
292 if (MATCH(buf, "search")) {
293 if (haveenv) /* skip if have from environ */
294 continue;
295 cp = buf + sizeof("search") - 1;
296 while (*cp == ' ' || *cp == '\t')
297 cp++;
298 if ((*cp == '\0') || (*cp == '\n'))
299 continue;
300 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
301 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
302 if ((cp = strchr(statp->defdname, '\n')) != NULL)
303 *cp = '\0';
304 /*
305 * Set search list to be blank-separated strings
306 * on rest of line.
307 */
308 cp = statp->defdname;
309 pp = statp->dnsrch;
310 *pp++ = cp;
311 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
312 if (*cp == ' ' || *cp == '\t') {
313 *cp = 0;
314 n = 1;
315 } else if (n) {
316 *pp++ = cp;
317 n = 0;
318 }
319 }
320 /* null terminate last domain if there are excess */
321 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
322 cp++;
323 *cp = '\0';
324 *pp++ = 0;
325 havesearch = 1;
326 continue;
327 }
328 /* read nameservers to query */
329 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
330 struct addrinfo hints, *ai;
331 char sbuf[NI_MAXSERV];
332 const size_t minsiz =
333 sizeof(statp->_u._ext.ext->nsaddrs[0]);
334
335 cp = buf + sizeof("nameserver") - 1;
336 while (*cp == ' ' || *cp == '\t')
337 cp++;
338 cp[strcspn(cp, ";# \t\n")] = '\0';
339 if ((*cp != '\0') && (*cp != '\n')) {
340 memset(&hints, 0, sizeof(hints));
341 hints.ai_family = PF_UNSPEC;
342 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
343 hints.ai_flags = AI_NUMERICHOST;
344 sprintf(sbuf, "%u", NAMESERVER_PORT);
345 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
346 ai->ai_addrlen <= minsiz) {
347 if (statp->_u._ext.ext != NULL) {
348 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
349 ai->ai_addr, ai->ai_addrlen);
350 }
351 if (ai->ai_addrlen <=
352 sizeof(statp->nsaddr_list[nserv])) {
353 memcpy(&statp->nsaddr_list[nserv],
354 ai->ai_addr, ai->ai_addrlen);
355 } else
356 statp->nsaddr_list[nserv].sin_family = 0;
357 freeaddrinfo(ai);
358 nserv++;
359 }
360 }
361 continue;
362 }
363 #ifdef RESOLVSORT
364 if (MATCH(buf, "sortlist")) {
365 struct in_addr a;
366
367 cp = buf + sizeof("sortlist") - 1;
368 while (nsort < MAXRESOLVSORT) {
369 while (*cp == ' ' || *cp == '\t')
370 cp++;
371 if (*cp == '\0' || *cp == '\n' || *cp == ';')
372 break;
373 net = cp;
374 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
375 isascii(*cp) && !isspace((unsigned char)*cp))
376 cp++;
377 n = *cp;
378 *cp = 0;
379 if (inet_aton(net, &a)) {
380 statp->sort_list[nsort].addr = a;
381 if (ISSORTMASK(n)) {
382 *cp++ = n;
383 net = cp;
384 while (*cp && *cp != ';' &&
385 isascii(*cp) &&
386 !isspace((unsigned char)*cp))
387 cp++;
388 n = *cp;
389 *cp = 0;
390 if (inet_aton(net, &a)) {
391 statp->sort_list[nsort].mask = a.s_addr;
392 } else {
393 statp->sort_list[nsort].mask =
394 net_mask(statp->sort_list[nsort].addr);
395 }
396 } else {
397 statp->sort_list[nsort].mask =
398 net_mask(statp->sort_list[nsort].addr);
399 }
400 nsort++;
401 }
402 *cp = n;
403 }
404 continue;
405 }
406 #endif
407 if (MATCH(buf, "options")) {
408 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
409 continue;
410 }
411 }
412 if (nserv > 0)
413 statp->nscount = nserv;
414 #ifdef RESOLVSORT
415 statp->nsort = nsort;
416 #endif
417 (void) fclose(fp);
418 }
419 /*
420 * Last chance to get a nameserver. This should not normally
421 * be necessary
422 */
423 #ifdef NO_RESOLV_CONF
424 if(nserv == 0)
425 nserv = get_nameservers(statp);
426 #endif
427
428 if (statp->defdname[0] == 0 &&
429 gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
430 (cp = strchr(buf, '.')) != NULL)
431 strcpy(statp->defdname, cp + 1);
432
433 /* find components of local domain that might be searched */
434 if (havesearch == 0) {
435 pp = statp->dnsrch;
436 *pp++ = statp->defdname;
437 *pp = NULL;
438
439 dots = 0;
440 for (cp = statp->defdname; *cp; cp++)
441 dots += (*cp == '.');
442
443 cp = statp->defdname;
444 while (pp < statp->dnsrch + MAXDFLSRCH) {
445 if (dots < LOCALDOMAINPARTS)
446 break;
447 cp = strchr(cp, '.') + 1; /* we know there is one */
448 *pp++ = cp;
449 dots--;
450 }
451 *pp = NULL;
452 #ifdef DEBUG
453 if (statp->options & RES_DEBUG) {
454 printf(";; res_init()... default dnsrch list:\n");
455 for (pp = statp->dnsrch; *pp; pp++)
456 printf(";;\t%s\n", *pp);
457 printf(";;\t..END..\n");
458 }
459 #endif
460 }
461
462 if ((cp = getenv("RES_OPTIONS")) != NULL)
463 res_setoptions(statp, cp, "env");
464 statp->options |= RES_INIT;
465 return (0);
466 }
467
468 static void
469 res_setoptions(res_state statp, const char *options, const char *source)
470 {
471 const char *cp = options;
472 int i;
473 struct __res_state_ext *ext = statp->_u._ext.ext;
474
475 #ifdef DEBUG
476 if (statp->options & RES_DEBUG)
477 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
478 options, source);
479 #endif
480 while (*cp) {
481 /* skip leading and inner runs of spaces */
482 while (*cp == ' ' || *cp == '\t')
483 cp++;
484 /* search for and process individual options */
485 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
486 i = atoi(cp + sizeof("ndots:") - 1);
487 if (i <= RES_MAXNDOTS)
488 statp->ndots = i;
489 else
490 statp->ndots = RES_MAXNDOTS;
491 #ifdef DEBUG
492 if (statp->options & RES_DEBUG)
493 printf(";;\tndots=%d\n", statp->ndots);
494 #endif
495 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
496 i = atoi(cp + sizeof("timeout:") - 1);
497 if (i <= RES_MAXRETRANS)
498 statp->retrans = i;
499 else
500 statp->retrans = RES_MAXRETRANS;
501 #ifdef DEBUG
502 if (statp->options & RES_DEBUG)
503 printf(";;\ttimeout=%d\n", statp->retrans);
504 #endif
505 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
506 i = atoi(cp + sizeof("attempts:") - 1);
507 if (i <= RES_MAXRETRY)
508 statp->retry = i;
509 else
510 statp->retry = RES_MAXRETRY;
511 #ifdef DEBUG
512 if (statp->options & RES_DEBUG)
513 printf(";;\tattempts=%d\n", statp->retry);
514 #endif
515 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
516 #ifdef DEBUG
517 if (!(statp->options & RES_DEBUG)) {
518 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
519 options, source);
520 statp->options |= RES_DEBUG;
521 }
522 printf(";;\tdebug\n");
523 #endif
524 } else if (!strncmp(cp, "no_tld_query",
525 sizeof("no_tld_query") - 1) ||
526 !strncmp(cp, "no-tld-query",
527 sizeof("no-tld-query") - 1)) {
528 statp->options |= RES_NOTLDQUERY;
529 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
530 statp->options |= RES_USE_INET6;
531 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
532 statp->options |= RES_ROTATE;
533 } else if (!strncmp(cp, "no-check-names",
534 sizeof("no-check-names") - 1)) {
535 statp->options |= RES_NOCHECKNAME;
536 }
537 #ifdef RES_USE_EDNS0
538 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
539 statp->options |= RES_USE_EDNS0;
540 }
541 #endif
542 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
543 statp->options |= RES_USE_DNAME;
544 }
545 else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
546 if (ext == NULL)
547 goto skip;
548 cp += sizeof("nibble:") - 1;
549 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
550 strncpy(ext->nsuffix, cp, (size_t)i);
551 ext->nsuffix[i] = '\0';
552 }
553 else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
554 if (ext == NULL)
555 goto skip;
556 cp += sizeof("nibble2:") - 1;
557 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
558 strncpy(ext->nsuffix2, cp, (size_t)i);
559 ext->nsuffix2[i] = '\0';
560 }
561 else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
562 cp += sizeof("v6revmode:") - 1;
563 /* "nibble" and "bitstring" used to be valid */
564 if (!strncmp(cp, "single", sizeof("single") - 1)) {
565 statp->options |= RES_NO_NIBBLE2;
566 } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
567 statp->options &=
568 ~RES_NO_NIBBLE2;
569 }
570 }
571 else {
572 /* XXX - print a warning here? */
573 }
574 skip:
575 /* skip to next run of spaces */
576 while (*cp && *cp != ' ' && *cp != '\t')
577 cp++;
578 }
579 }
580
581 #ifdef RESOLVSORT
582 /* XXX - should really support CIDR which means explicit masks always. */
583 static u_int32_t
584 net_mask(in) /* XXX - should really use system's version of this */
585 struct in_addr in;
586 {
587 register u_int32_t i = ntohl(in.s_addr);
588
589 if (IN_CLASSA(i))
590 return (htonl(IN_CLASSA_NET));
591 else if (IN_CLASSB(i))
592 return (htonl(IN_CLASSB_NET));
593 return (htonl(IN_CLASSC_NET));
594 }
595 #endif
596
597 u_int
598 res_randomid(void) {
599 struct timeval now;
600
601 gettimeofday(&now, NULL);
602 return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
603 }
604
605 /*
606 * This routine is for closing the socket if a virtual circuit is used and
607 * the program wants to close it. This provides support for endhostent()
608 * which expects to close the socket.
609 *
610 * This routine is not expected to be user visible.
611 */
612 void
613 res_nclose(res_state statp) {
614 int ns;
615
616 if (statp->_vcsock >= 0) {
617 (void) close(statp->_vcsock);
618 statp->_vcsock = -1;
619 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
620 }
621 for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
622 if (statp->_u._ext.nssocks[ns] != -1) {
623 (void) close(statp->_u._ext.nssocks[ns]);
624 statp->_u._ext.nssocks[ns] = -1;
625 }
626 }
627 }
628
629 void
630 res_ndestroy(res_state statp) {
631 res_nclose(statp);
632 if (statp->_u._ext.ext != NULL)
633 free(statp->_u._ext.ext);
634 statp->options &= ~RES_INIT;
635 statp->_u._ext.ext = NULL;
636 }
637
638 const char *
639 res_get_nibblesuffix(res_state statp) {
640 if (statp->_u._ext.ext)
641 return (statp->_u._ext.ext->nsuffix);
642 return ("ip6.arpa");
643 }
644
645 const char *
646 res_get_nibblesuffix2(res_state statp) {
647 if (statp->_u._ext.ext)
648 return (statp->_u._ext.ext->nsuffix2);
649 return ("ip6.int");
650 }
651
652 void
653 res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) {
654 int i, nserv;
655 size_t size;
656
657 /* close open servers */
658 res_nclose(statp);
659
660 /* cause rtt times to be forgotten */
661 statp->_u._ext.nscount = 0;
662
663 nserv = 0;
664 for (i = 0; i < cnt && nserv < MAXNS; i++) {
665 switch (set->sin.sin_family) {
666 case AF_INET:
667 size = sizeof(set->sin);
668 if (statp->_u._ext.ext)
669 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
670 &set->sin, size);
671 if (size <= sizeof(statp->nsaddr_list[nserv]))
672 memcpy(&statp->nsaddr_list[nserv],
673 &set->sin, size);
674 else
675 statp->nsaddr_list[nserv].sin_family = 0;
676 nserv++;
677 break;
678
679 #ifdef HAS_INET6_STRUCTS
680 case AF_INET6:
681 size = sizeof(set->sin6);
682 if (statp->_u._ext.ext)
683 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
684 &set->sin6, size);
685 if (size <= sizeof(statp->nsaddr_list[nserv]))
686 memcpy(&statp->nsaddr_list[nserv],
687 &set->sin6, size);
688 else
689 statp->nsaddr_list[nserv].sin_family = 0;
690 nserv++;
691 break;
692 #endif
693
694 default:
695 break;
696 }
697 set++;
698 }
699 statp->nscount = nserv;
700
701 }
702
703 int
704 res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) {
705 int i;
706 size_t size;
707 u_int16_t family;
708
709 for (i = 0; i < statp->nscount && i < cnt; i++) {
710 if (statp->_u._ext.ext)
711 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
712 else
713 family = statp->nsaddr_list[i].sin_family;
714
715 switch (family) {
716 case AF_INET:
717 size = sizeof(set->sin);
718 if (statp->_u._ext.ext)
719 memcpy(&set->sin,
720 &statp->_u._ext.ext->nsaddrs[i],
721 size);
722 else
723 memcpy(&set->sin, &statp->nsaddr_list[i],
724 size);
725 break;
726
727 #ifdef HAS_INET6_STRUCTS
728 case AF_INET6:
729 size = sizeof(set->sin6);
730 if (statp->_u._ext.ext)
731 memcpy(&set->sin6,
732 &statp->_u._ext.ext->nsaddrs[i],
733 size);
734 else
735 memcpy(&set->sin6, &statp->nsaddr_list[i],
736 size);
737 break;
738 #endif
739
740 default:
741 set->sin.sin_family = 0;
742 break;
743 }
744 set++;
745 }
746 return (statp->nscount);
747 }
748