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