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.8 2003/02/18 17:29:24 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/nameser8_compat.h>
93 #include <resolv8_compat.h>
94 #if defined(BSD) && (BSD >= 199103)
99 # include "portability.h"
102 #if defined(USE_OPTIONS_H)
103 # include "options.h"
106 static void res_setoptions
__P((char *, char *));
109 static const char sort_mask
[] = "/&";
110 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
111 static u_int32_t net_mask
__P((struct in_addr
));
114 #if !defined(isascii) /* XXX - could be a function */
115 # define isascii(c) (!(c & 0200))
119 * Resolver state default settings.
122 #if defined(__APPLE__)
125 struct __res_state _res
;
128 * Set up default settings. If the configuration file exist, the values
129 * there will have precedence. Otherwise, the server address is set to
130 * INADDR_ANY and the default domain name comes from the gethostname().
132 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
133 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
134 * since it was noted that INADDR_ANY actually meant ``the first interface
135 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
136 * it had to be "up" in order for you to reach your own name server. It
137 * was later decided that since the recommended practice is to always
138 * install local static routes through 127.0.0.1 for all your network
139 * interfaces, that we could solve this problem without a code change.
141 * The configuration file should always be used, since it is the only way
142 * to specify a default domain. If you are running a server on your local
143 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
144 * in the configuration file.
146 * Return 0 if completes successfully, -1 on error
152 register char *cp
, **pp
;
155 int nserv
= 0; /* number of nameserver records read from file */
167 * These three fields used to be statically initialized. This made
168 * it hard to use this code in a shared library. It is necessary,
169 * now that we're doing dynamic initialization here, that we preserve
170 * the old semantics: if an application modifies one of these three
171 * fields of _res before res_init() is called, res_init() will not
172 * alter them. Of course, if an application is setting them to
173 * _zero_ before calling res_init(), hoping to override what used
174 * to be the static default, we can't detect it and unexpected results
175 * will follow. Zero for any of these fields would make no sense,
176 * so one can safely assume that the applications were already getting
177 * unexpected results.
179 * _res.options is tricky since some apps were known to diddle the bits
180 * before res_init() was first called. We can't replicate that semantic
181 * with dynamic initialization (they may have turned bits off that are
182 * set in RES_DEFAULT). Our solution is to declare such applications
183 * "broken". They could fool us by setting RES_INIT but none do (yet).
186 _res
.retrans
= RES_TIMEOUT
;
189 if (!(_res
.options
& RES_INIT
))
190 _res
.options
= RES_DEFAULT
;
193 * This one used to initialize implicitly to zero, so unless the app
194 * has set it to something in particular, we can randomize it now.
197 _res
.id
= res_randomid();
200 _res
.nsaddr
.sin_addr
= inet_makeaddr(IN_LOOPBACKNET
, 1);
202 _res
.nsaddr
.sin_addr
.s_addr
= INADDR_ANY
;
204 _res
.nsaddr
.sin_family
= AF_INET
;
205 _res
.nsaddr
.sin_port
= htons(NAMESERVER_PORT
);
210 /* Allow user to override the local domain definition */
211 if ((cp
= getenv("LOCALDOMAIN")) != NULL
) {
212 (void)strncpy(_res
.defdname
, cp
, sizeof(_res
.defdname
) - 1);
216 * Set search list to be blank-separated strings
217 * from rest of env value. Permits users of LOCALDOMAIN
218 * to still have a search list, and anyone to set the
219 * one that they want to use as an individual (even more
220 * important now that the rfc1535 stuff restricts searches)
225 for (n
= 0; *cp
&& pp
< _res
.dnsrch
+ MAXDNSRCH
; cp
++) {
226 if (*cp
== '\n') /* silly backwards compat */
228 else if (*cp
== ' ' || *cp
== '\t') {
237 /* null terminate last domain if there are excess */
238 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t' && *cp
!= '\n')
244 #define MATCH(line, name) \
245 (!strncmp(line, name, sizeof(name) - 1) && \
246 (line[sizeof(name) - 1] == ' ' || \
247 line[sizeof(name) - 1] == '\t'))
249 if ((fp
= fopen(_PATH_RESCONF
, "r")) != NULL
) {
250 /* read the config file */
251 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
253 if (*buf
== ';' || *buf
== '#')
255 /* read default domain name */
256 if (MATCH(buf
, "domain")) {
257 if (haveenv
) /* skip if have from environ */
259 cp
= buf
+ sizeof("domain") - 1;
260 while (*cp
== ' ' || *cp
== '\t')
262 if ((*cp
== '\0') || (*cp
== '\n'))
264 strncpy(_res
.defdname
, cp
, sizeof(_res
.defdname
) - 1);
265 if ((cp
= strpbrk(_res
.defdname
, " \t\n")) != NULL
)
270 /* set search list */
271 if (MATCH(buf
, "search")) {
272 if (haveenv
) /* skip if have from environ */
274 cp
= buf
+ sizeof("search") - 1;
275 while (*cp
== ' ' || *cp
== '\t')
277 if ((*cp
== '\0') || (*cp
== '\n'))
279 strncpy(_res
.defdname
, cp
, sizeof(_res
.defdname
) - 1);
280 if ((cp
= strchr(_res
.defdname
, '\n')) != NULL
)
283 * Set search list to be blank-separated strings
289 for (n
= 0; *cp
&& pp
< _res
.dnsrch
+ MAXDNSRCH
; cp
++) {
290 if (*cp
== ' ' || *cp
== '\t') {
298 /* null terminate last domain if there are excess */
299 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t')
306 /* read nameservers to query */
307 if (MATCH(buf
, "nameserver") && nserv
< MAXNS
) {
310 cp
= buf
+ sizeof("nameserver") - 1;
311 while (*cp
== ' ' || *cp
== '\t')
313 if ((*cp
!= '\0') && (*cp
!= '\n') && inet_aton(cp
, &a
)) {
314 _res
.nsaddr_list
[nserv
].sin_addr
= a
;
315 _res
.nsaddr_list
[nserv
].sin_family
= AF_INET
;
316 _res
.nsaddr_list
[nserv
].sin_port
=
317 htons(NAMESERVER_PORT
);
323 if (MATCH(buf
, "sortlist")) {
326 cp
= buf
+ sizeof("sortlist") - 1;
327 while (nsort
< MAXRESOLVSORT
) {
328 while (*cp
== ' ' || *cp
== '\t')
330 if (*cp
== '\0' || *cp
== '\n' || *cp
== ';')
333 while (*cp
&& !ISSORTMASK(*cp
) && *cp
!= ';' &&
334 isascii(*cp
) && !isspace(*cp
))
338 if (inet_aton(net
, &a
)) {
339 _res
.sort_list
[nsort
].addr
= a
;
343 while (*cp
&& *cp
!= ';' &&
344 isascii(*cp
) && !isspace(*cp
))
348 if (inet_aton(net
, &a
)) {
349 _res
.sort_list
[nsort
].mask
= a
.s_addr
;
351 _res
.sort_list
[nsort
].mask
=
352 net_mask(_res
.sort_list
[nsort
].addr
);
355 _res
.sort_list
[nsort
].mask
=
356 net_mask(_res
.sort_list
[nsort
].addr
);
365 if (MATCH(buf
, "options")) {
366 res_setoptions(buf
+ sizeof("options") - 1, "conf");
371 _res
.nscount
= nserv
;
378 if (_res
.defdname
[0] == 0 &&
379 gethostname(buf
, sizeof(_res
.defdname
) - 1) == 0 &&
380 (cp
= strchr(buf
, '.')) != NULL
)
381 strcpy(_res
.defdname
, cp
+ 1);
383 /* find components of local domain that might be searched */
384 if (havesearch
== 0) {
386 *pp
++ = _res
.defdname
;
391 for (cp
= _res
.defdname
; *cp
; cp
++)
392 dots
+= (*cp
== '.');
395 while (pp
< _res
.dnsrch
+ MAXDFLSRCH
) {
396 if (dots
< LOCALDOMAINPARTS
)
398 cp
= strchr(cp
, '.') + 1; /* we know there is one */
404 if (_res
.options
& RES_DEBUG
) {
405 printf(";; res_init()... default dnsrch list:\n");
406 for (pp
= _res
.dnsrch
; *pp
; pp
++)
407 printf(";;\t%s\n", *pp
);
408 printf(";;\t..END..\n");
411 #endif /* !RFC1535 */
414 if ((cp
= getenv("RES_OPTIONS")) != NULL
)
415 res_setoptions(cp
, "env");
416 _res
.options
|= RES_INIT
;
421 res_setoptions(options
, source
)
422 char *options
, *source
;
428 if (_res
.options
& RES_DEBUG
)
429 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
433 /* skip leading and inner runs of spaces */
434 while (*cp
== ' ' || *cp
== '\t')
436 /* search for and process individual options */
437 if (!strncmp(cp
, "ndots:", sizeof("ndots:") - 1)) {
438 i
= atoi(cp
+ sizeof("ndots:") - 1);
439 if (i
<= RES_MAXNDOTS
)
442 _res
.ndots
= RES_MAXNDOTS
;
444 if (_res
.options
& RES_DEBUG
)
445 printf(";;\tndots=%d\n", _res
.ndots
);
447 } else if (!strncmp(cp
, "debug", sizeof("debug") - 1)) {
449 if (!(_res
.options
& RES_DEBUG
)) {
450 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
452 _res
.options
|= RES_DEBUG
;
454 printf(";;\tdebug\n");
457 /* XXX - print a warning here? */
459 /* skip to next run of spaces */
460 while (*cp
&& *cp
!= ' ' && *cp
!= '\t')
466 /* XXX - should really support CIDR which means explicit masks always. */
468 net_mask(in
) /* XXX - should really use system's version of this */
471 register u_int32_t i
= ntohl(in
.s_addr
);
474 return (htonl(IN_CLASSA_NET
));
475 else if (IN_CLASSB(i
))
476 return (htonl(IN_CLASSB_NET
));
477 return (htonl(IN_CLASSC_NET
));
486 gettimeofday(&now
, NULL
);
487 return (0xffff & (now
.tv_sec
^ now
.tv_usec
^ getpid()));