]>
git.saurik.com Git - apple/libinfo.git/blob - dns.subproj/res_init.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * ++Copyright++ 1985, 1989, 1993
27 * Copyright (c) 1985, 1989, 1993
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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, and that
63 * the name of Digital Equipment Corporation not be used in advertising or
64 * publicity pertaining to distribution of the document or software without
65 * specific, written prior permission.
67 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
68 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
69 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
70 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
71 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
72 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
73 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
79 #if defined(LIBC_SCCS) && !defined(lint)
80 static char sccsid
[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
81 static char rcsid
[] = "$Id: res_init.c,v 1.5.88.1 2002/11/06 17:30:17 majka Exp $";
82 #endif /* LIBC_SCCS and not lint */
84 #include <sys/param.h>
85 #include <sys/socket.h>
87 #include <netinet/in.h>
88 #include <arpa/inet.h>
89 #include <arpa/nameser.h>
94 #if defined(BSD) && (BSD >= 199103)
99 # include "portability.h"
102 /*-------------------------------------- info about "sortlist" --------------
103 * Marc Majka 1994/04/16
104 * Allan Nathanson 1994/10/29 (BIND 4.9.3.x)
106 * NetInfo resolver configuration directory support.
108 * Allow a NetInfo directory to be created in the hierarchy which
109 * contains the same information as the resolver configuration file.
111 * - The local domain name is stored as the value of the "domain" property.
112 * - The Internet address(es) of the name server(s) are stored as values
113 * of the "nameserver" property.
114 * - The name server addresses are stored as values of the "nameserver"
116 * - The search list for host-name lookup is stored as values of the
118 * - The sortlist comprised of IP address netmask pairs are stored as
119 * values of the "sortlist" property. The IP address and optional netmask
120 * should be seperated by a slash (/) or ampersand (&) character.
121 * - Internal resolver variables can be set from the value of the "options"
124 #if defined(__APPLE__) && defined(CONFIGURE_RESOLVER_FROM_NETINFO)
125 # include <netinfo/ni.h>
126 # define NI_PATH_RESCONF "/locations/resolver"
127 # define NI_TIMEOUT 10
128 static int netinfo_res_init
__P((int *haveenv
, int *havesearch
));
131 #if defined(USE_OPTIONS_H)
132 # include "options.h"
135 static void res_setoptions
__P((char *, char *));
138 static const char sort_mask
[] = "/&";
139 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
140 static u_int32_t net_mask
__P((struct in_addr
));
143 #if !defined(isascii) /* XXX - could be a function */
144 # define isascii(c) (!(c & 0200))
148 * Resolver state default settings.
151 #if defined(__APPLE__)
154 struct __res_state _res
;
155 extern struct __res_state _res_shadow
;
158 * Set up default settings. If the configuration file exist, the values
159 * there will have precedence. Otherwise, the server address is set to
160 * INADDR_ANY and the default domain name comes from the gethostname().
162 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
163 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
164 * since it was noted that INADDR_ANY actually meant ``the first interface
165 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
166 * it had to be "up" in order for you to reach your own name server. It
167 * was later decided that since the recommended practice is to always
168 * install local static routes through 127.0.0.1 for all your network
169 * interfaces, that we could solve this problem without a code change.
171 * The configuration file should always be used, since it is the only way
172 * to specify a default domain. If you are running a server on your local
173 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
174 * in the configuration file.
176 * Return 0 if completes successfully, -1 on error
182 register char *cp
, **pp
;
185 int nserv
= 0; /* number of nameserver records read from file */
197 * These three fields used to be statically initialized. This made
198 * it hard to use this code in a shared library. It is necessary,
199 * now that we're doing dynamic initialization here, that we preserve
200 * the old semantics: if an application modifies one of these three
201 * fields of _res before res_init() is called, res_init() will not
202 * alter them. Of course, if an application is setting them to
203 * _zero_ before calling res_init(), hoping to override what used
204 * to be the static default, we can't detect it and unexpected results
205 * will follow. Zero for any of these fields would make no sense,
206 * so one can safely assume that the applications were already getting
207 * unexpected results.
209 * _res.options is tricky since some apps were known to diddle the bits
210 * before res_init() was first called. We can't replicate that semantic
211 * with dynamic initialization (they may have turned bits off that are
212 * set in RES_DEFAULT). Our solution is to declare such applications
213 * "broken". They could fool us by setting RES_INIT but none do (yet).
216 _res
.retrans
= RES_TIMEOUT
;
219 if (!(_res
.options
& RES_INIT
))
220 _res
.options
= RES_DEFAULT
;
223 * This one used to initialize implicitly to zero, so unless the app
224 * has set it to something in particular, we can randomize it now.
227 _res
.id
= res_randomid();
230 _res
.nsaddr
.sin_addr
= inet_makeaddr(IN_LOOPBACKNET
, 1);
232 _res
.nsaddr
.sin_addr
.s_addr
= INADDR_ANY
;
234 _res
.nsaddr
.sin_family
= AF_INET
;
235 _res
.nsaddr
.sin_port
= htons(NAMESERVER_PORT
);
240 /* Allow user to override the local domain definition */
241 if ((cp
= getenv("LOCALDOMAIN")) != NULL
) {
242 (void)strncpy(_res
.defdname
, cp
, sizeof(_res
.defdname
) - 1);
246 * Set search list to be blank-separated strings
247 * from rest of env value. Permits users of LOCALDOMAIN
248 * to still have a search list, and anyone to set the
249 * one that they want to use as an individual (even more
250 * important now that the rfc1535 stuff restricts searches)
255 for (n
= 0; *cp
&& pp
< _res
.dnsrch
+ MAXDNSRCH
; cp
++) {
256 if (*cp
== '\n') /* silly backwards compat */
258 else if (*cp
== ' ' || *cp
== '\t') {
267 /* null terminate last domain if there are excess */
268 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t' && *cp
!= '\n')
274 #define MATCH(line, name) \
275 (!strncmp(line, name, sizeof(name) - 1) && \
276 (line[sizeof(name) - 1] == ' ' || \
277 line[sizeof(name) - 1] == '\t'))
279 if ((fp
= fopen(_PATH_RESCONF
, "r")) != NULL
) {
280 /* read the config file */
281 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
283 if (*buf
== ';' || *buf
== '#')
285 /* read default domain name */
286 if (MATCH(buf
, "domain")) {
287 if (haveenv
) /* skip if have from environ */
289 cp
= buf
+ sizeof("domain") - 1;
290 while (*cp
== ' ' || *cp
== '\t')
292 if ((*cp
== '\0') || (*cp
== '\n'))
294 strncpy(_res
.defdname
, cp
, sizeof(_res
.defdname
) - 1);
295 if ((cp
= strpbrk(_res
.defdname
, " \t\n")) != NULL
)
300 /* set search list */
301 if (MATCH(buf
, "search")) {
302 if (haveenv
) /* skip if have from environ */
304 cp
= buf
+ sizeof("search") - 1;
305 while (*cp
== ' ' || *cp
== '\t')
307 if ((*cp
== '\0') || (*cp
== '\n'))
309 strncpy(_res
.defdname
, cp
, sizeof(_res
.defdname
) - 1);
310 if ((cp
= strchr(_res
.defdname
, '\n')) != NULL
)
313 * Set search list to be blank-separated strings
319 for (n
= 0; *cp
&& pp
< _res
.dnsrch
+ MAXDNSRCH
; cp
++) {
320 if (*cp
== ' ' || *cp
== '\t') {
328 /* null terminate last domain if there are excess */
329 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t')
336 /* read nameservers to query */
337 if (MATCH(buf
, "nameserver") && nserv
< MAXNS
) {
340 cp
= buf
+ sizeof("nameserver") - 1;
341 while (*cp
== ' ' || *cp
== '\t')
343 if ((*cp
!= '\0') && (*cp
!= '\n') && inet_aton(cp
, &a
)) {
344 _res
.nsaddr_list
[nserv
].sin_addr
= a
;
345 _res
.nsaddr_list
[nserv
].sin_family
= AF_INET
;
346 _res
.nsaddr_list
[nserv
].sin_port
=
347 htons(NAMESERVER_PORT
);
353 if (MATCH(buf
, "sortlist")) {
356 cp
= buf
+ sizeof("sortlist") - 1;
357 while (nsort
< MAXRESOLVSORT
) {
358 while (*cp
== ' ' || *cp
== '\t')
360 if (*cp
== '\0' || *cp
== '\n' || *cp
== ';')
363 while (*cp
&& !ISSORTMASK(*cp
) && *cp
!= ';' &&
364 isascii(*cp
) && !isspace(*cp
))
368 if (inet_aton(net
, &a
)) {
369 _res
.sort_list
[nsort
].addr
= a
;
373 while (*cp
&& *cp
!= ';' &&
374 isascii(*cp
) && !isspace(*cp
))
378 if (inet_aton(net
, &a
)) {
379 _res
.sort_list
[nsort
].mask
= a
.s_addr
;
381 _res
.sort_list
[nsort
].mask
=
382 net_mask(_res
.sort_list
[nsort
].addr
);
385 _res
.sort_list
[nsort
].mask
=
386 net_mask(_res
.sort_list
[nsort
].addr
);
395 if (MATCH(buf
, "options")) {
396 res_setoptions(buf
+ sizeof("options") - 1, "conf");
401 _res
.nscount
= nserv
;
407 #if defined(__APPLE__) && defined(CONFIGURE_RESOLVER_FROM_NETINFO)
408 else netinfo_res_init(&haveenv
, &havesearch
);
411 if (_res
.defdname
[0] == 0 &&
412 gethostname(buf
, sizeof(_res
.defdname
) - 1) == 0 &&
413 (cp
= strchr(buf
, '.')) != NULL
)
414 strcpy(_res
.defdname
, cp
+ 1);
416 /* find components of local domain that might be searched */
417 if (havesearch
== 0) {
419 *pp
++ = _res
.defdname
;
424 for (cp
= _res
.defdname
; *cp
; cp
++)
425 dots
+= (*cp
== '.');
428 while (pp
< _res
.dnsrch
+ MAXDFLSRCH
) {
429 if (dots
< LOCALDOMAINPARTS
)
431 cp
= strchr(cp
, '.') + 1; /* we know there is one */
437 if (_res
.options
& RES_DEBUG
) {
438 printf(";; res_init()... default dnsrch list:\n");
439 for (pp
= _res
.dnsrch
; *pp
; pp
++)
440 printf(";;\t%s\n", *pp
);
441 printf(";;\t..END..\n");
444 #endif /* !RFC1535 */
447 if ((cp
= getenv("RES_OPTIONS")) != NULL
)
448 res_setoptions(cp
, "env");
449 _res
.options
|= RES_INIT
;
455 res_setoptions(options
, source
)
456 char *options
, *source
;
462 if (_res
.options
& RES_DEBUG
)
463 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
467 /* skip leading and inner runs of spaces */
468 while (*cp
== ' ' || *cp
== '\t')
470 /* search for and process individual options */
471 if (!strncmp(cp
, "ndots:", sizeof("ndots:") - 1)) {
472 i
= atoi(cp
+ sizeof("ndots:") - 1);
473 if (i
<= RES_MAXNDOTS
)
476 _res
.ndots
= RES_MAXNDOTS
;
478 if (_res
.options
& RES_DEBUG
)
479 printf(";;\tndots=%d\n", _res
.ndots
);
481 } else if (!strncmp(cp
, "debug", sizeof("debug") - 1)) {
483 if (!(_res
.options
& RES_DEBUG
)) {
484 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
486 _res
.options
|= RES_DEBUG
;
488 printf(";;\tdebug\n");
491 /* XXX - print a warning here? */
493 /* skip to next run of spaces */
494 while (*cp
&& *cp
!= ' ' && *cp
!= '\t')
500 /* XXX - should really support CIDR which means explicit masks always. */
502 net_mask(in
) /* XXX - should really use system's version of this */
505 register u_int32_t i
= ntohl(in
.s_addr
);
508 return (htonl(IN_CLASSA_NET
));
509 else if (IN_CLASSB(i
))
510 return (htonl(IN_CLASSB_NET
));
511 return (htonl(IN_CLASSC_NET
));
515 #if defined(__APPLE__) && defined(CONFIGURE_RESOLVER_FROM_NETINFO)
517 netinfo_res_init(haveenv
, havesearch
)
522 void *domain
, *parent
;
531 status
= ni_open(NULL
, ".", &domain
);
532 if (status
== NI_OK
) {
533 ni_setreadtimeout(domain
, NI_TIMEOUT
);
534 ni_setabort(domain
, 1);
536 /* climb the NetInfo hierarchy to find a resolver directory */
537 while (status
== NI_OK
) {
538 status
= ni_pathsearch(domain
, &dir
, NI_PATH_RESCONF
);
539 if (status
== NI_OK
) {
540 /* found a resolver directory */
543 /* get the default domain name */
544 status
= ni_lookupprop(domain
, &dir
, "domain", &nl
);
545 if (status
== NI_OK
&& nl
.ni_namelist_len
> 0) {
546 (void)strncpy(_res
.defdname
,
547 nl
.ni_namelist_val
[0],
548 sizeof(_res
.defdname
) - 1);
549 _res
.defdname
[sizeof(_res
.defdname
) - 1] = '\0';
550 ni_namelist_free(&nl
);
554 /* get search list */
555 status
= ni_lookupprop(domain
, &dir
, "search", &nl
);
556 if (status
== NI_OK
&& nl
.ni_namelist_len
> 0) {
557 (void)strncpy(_res
.defdname
,
558 nl
.ni_namelist_val
[0],
559 sizeof(_res
.defdname
) - 1);
560 _res
.defdname
[sizeof(_res
.defdname
) - 1] = '\0';
563 n
< nl
.ni_namelist_len
&& n
< MAXDNSRCH
;
565 /* duplicate up to MAXDNSRCH servers */
566 char *cp
= nl
.ni_namelist_val
[n
];
568 strcpy((char *)malloc(strlen(cp
) + 1), cp
);
570 ni_namelist_free(&nl
);
575 /* get list of nameservers */
576 status
= ni_lookupprop(domain
, &dir
, "nameserver", &nl
);
577 if (status
== NI_OK
&& nl
.ni_namelist_len
> 0) {
578 /* copy up to MAXNS servers */
580 n
< nl
.ni_namelist_len
&& nserv
< MAXNS
;
584 if (inet_aton(nl
.ni_namelist_val
[n
], &a
)) {
585 _res
.nsaddr_list
[nserv
].sin_addr
= a
;
586 _res
.nsaddr_list
[nserv
].sin_family
= AF_INET
;
587 _res
.nsaddr_list
[nserv
].sin_port
=
588 htons(NAMESERVER_PORT
);
592 ni_namelist_free(&nl
);
596 _res
.nscount
= nserv
;
600 status
= ni_lookupprop(domain
, &dir
, "sortlist", &nl
);
601 if (status
== NI_OK
&& nl
.ni_namelist_len
> 0) {
603 /* copy up to MAXRESOLVSORT address/netmask pairs */
605 n
< nl
.ni_namelist_len
&& nsort
< MAXRESOLVSORT
;
613 for (sp
= sort_mask
; *sp
; sp
++) {
615 cp1
= strchr(nl
.ni_namelist_val
[n
], *sp
);
617 cp
= (cp
< cp1
)? cp
: cp1
;
626 if (inet_aton(nl
.ni_namelist_val
[n
], &a
)) {
627 _res
.sort_list
[nsort
].addr
= a
;
628 if (*cp
&& ISSORTMASK(ch
)) {
630 if (inet_aton(cp
, &a
)) {
631 _res
.sort_list
[nsort
].mask
= a
.s_addr
;
633 _res
.sort_list
[nsort
].mask
=
634 net_mask(_res
.sort_list
[nsort
].addr
);
637 _res
.sort_list
[nsort
].mask
=
638 net_mask(_res
.sort_list
[nsort
].addr
);
643 ni_namelist_free(&nl
);
649 /* get resolver options */
650 status
= ni_lookupprop(domain
, &dir
, "options", &nl
);
651 if (status
== NI_OK
&& nl
.ni_namelist_len
> 0) {
652 res_setoptions(nl
.ni_namelist_val
[0], "conf");
653 ni_namelist_free(&nl
);
657 return(1); /* using DNS configuration from NetInfo */
660 status
= ni_open(domain
, "..", &parent
);
666 return(0); /* if not using DNS configuration from NetInfo */
668 #endif /* __APPLE__ */
675 gettimeofday(&now
, NULL
);
676 return (0xffff & (now
.tv_sec
^ now
.tv_usec
^ getpid()));