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