]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/res_init.c
2 * Copyright (c) 1985, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Portions copyright (c) 1999, 2000
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley, Intel Corporation, and its contributors.
26 * 4. Neither the name of University, Intel Corporation, or their respective
27 * contributors may be used to endorse or promote products derived from
28 * this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND
31 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
32 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,
34 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
51 * Permission to use, copy, modify, and distribute this software for any
52 * purpose with or without fee is hereby granted, provided that the above
53 * copyright notice and this permission notice appear in all copies, and that
54 * the name of Digital Equipment Corporation not be used in advertising or
55 * publicity pertaining to distribution of the document or software without
56 * specific, written prior permission.
58 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
59 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
60 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
61 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
62 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
63 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
64 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
69 * Portions Copyright (c) 1996 by Internet Software Consortium.
71 * Permission to use, copy, modify, and distribute this software for any
72 * purpose with or without fee is hereby granted, provided that the above
73 * copyright notice and this permission notice appear in all copies.
75 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
76 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
77 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
78 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
79 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
80 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
81 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
85 #if defined(LIBC_SCCS) && !defined(lint)
86 static char sccsid
[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
87 static char orig_rcsid
[] = "From: Id: res_init.c,v 8.7 1996/11/18 09:10:04 vixie Exp $";
88 static char rcsid
[] = "$Id: res_init.c,v 1.1.1.1 2003/11/19 01:51:37 kyu3 Exp $";
89 #endif /* LIBC_SCCS and not lint */
91 #include <sys/types.h>
92 #include <sys/param.h>
93 #include <sys/socket.h>
95 #include <netinet/in.h>
96 #include <arpa/inet.h>
97 #include <arpa/nameser.h>
105 #include "res_config.h"
107 static void res_setoptions
__P((char *, char *));
110 static const char sort_mask
[] = "/&";
111 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
112 static u_int32_t net_mask
__P((struct in_addr
));
115 #if !defined(isascii) /* XXX - could be a function */
116 # define isascii(c) (!(c & 0200))
120 * Resolver state default settings.
123 struct __res_state _res
124 # if defined(__BIND_RES_TEXT)
125 = { RES_TIMEOUT
, } /* Motorola, et al. */
131 * Set up default settings. If the configuration file exist, the values
132 * there will have precedence. Otherwise, the server address is set to
133 * INADDR_ANY and the default domain name comes from the gethostname().
135 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
136 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
137 * since it was noted that INADDR_ANY actually meant ``the first interface
138 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
139 * it had to be "up" in order for you to reach your own name server. It
140 * was later decided that since the recommended practice is to always
141 * install local static routes through 127.0.0.1 for all your network
142 * interfaces, that we could solve this problem without a code change.
144 * The configuration file should always be used, since it is the only way
145 * to specify a default domain. If you are running a server on your local
146 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
147 * in the configuration file.
149 * Return 0 if completes successfully, -1 on error
155 register char *cp
, **pp
;
158 int nserv
= 0; /* number of nameserver records read from file */
170 * These three fields used to be statically initialized. This made
171 * it hard to use this code in a shared library. It is necessary,
172 * now that we're doing dynamic initialization here, that we preserve
173 * the old semantics: if an application modifies one of these three
174 * fields of _res before res_init() is called, res_init() will not
175 * alter them. Of course, if an application is setting them to
176 * _zero_ before calling res_init(), hoping to override what used
177 * to be the static default, we can't detect it and unexpected results
178 * will follow. Zero for any of these fields would make no sense,
179 * so one can safely assume that the applications were already getting
180 * unexpected results.
182 * _res.options is tricky since some apps were known to diddle the bits
183 * before res_init() was first called. We can't replicate that semantic
184 * with dynamic initialization (they may have turned bits off that are
185 * set in RES_DEFAULT). Our solution is to declare such applications
186 * "broken". They could fool us by setting RES_INIT but none do (yet).
189 _res
.retrans
= RES_TIMEOUT
;
192 if (!(_res
.options
& RES_INIT
))
193 _res
.options
= RES_DEFAULT
;
196 * This one used to initialize implicitly to zero, so unless the app
197 * has set it to something in particular, we can randomize it now.
200 _res
.id
= (u_short
)res_randomid();
203 _res
.nsaddr
.sin_addr
= inet_makeaddr(IN_LOOPBACKNET
, 1);
205 _res
.nsaddr
.sin_addr
.s_addr
= INADDR_ANY
;
207 _res
.nsaddr
.sin_family
= AF_INET
;
208 _res
.nsaddr
.sin_port
= htons(NAMESERVER_PORT
);
213 /* Allow user to override the local domain definition */
215 if (issetugid() == 0 && (cp
= getenv("LOCALDOMAIN")) != NULL
)
217 if ((cp
= getenv("LOCALDOMAIN")) != NULL
)
220 (void)strncpy(_res
.defdname
, cp
, sizeof(_res
.defdname
) - 1);
221 _res
.defdname
[sizeof(_res
.defdname
) - 1] = '\0';
225 * Set search list to be blank-separated strings
226 * from rest of env value. Permits users of LOCALDOMAIN
227 * to still have a search list, and anyone to set the
228 * one that they want to use as an individual (even more
229 * important now that the rfc1535 stuff restricts searches)
234 for (n
= 0; *cp
&& pp
< _res
.dnsrch
+ MAXDNSRCH
; cp
++) {
235 if (*cp
== '\n') /* silly backwards compat */
237 else if (*cp
== ' ' || *cp
== '\t') {
246 /* null terminate last domain if there are excess */
247 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t' && *cp
!= '\n')
253 #define MATCH(line, name) \
254 (!strncmp(line, name, sizeof(name) - 1) && \
255 (line[sizeof(name) - 1] == ' ' || \
256 line[sizeof(name) - 1] == '\t'))
258 if ((fp
= fopen(_PATH_RESCONF
, "r")) != NULL
) {
259 /* read the config file */
260 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
262 if (*buf
== ';' || *buf
== '#')
264 /* read default domain name */
265 if (MATCH(buf
, "domain")) {
266 if (haveenv
) /* skip if have from environ */
268 cp
= buf
+ sizeof("domain") - 1;
269 while (*cp
== ' ' || *cp
== '\t')
271 if ((*cp
== '\0') || (*cp
== '\n'))
273 strncpy(_res
.defdname
, cp
, sizeof(_res
.defdname
) - 1);
274 _res
.defdname
[sizeof(_res
.defdname
) - 1] = '\0';
275 if ((cp
= strpbrk(_res
.defdname
, " \t\n")) != NULL
)
280 /* set search list */
281 if (MATCH(buf
, "search")) {
282 if (haveenv
) /* skip if have from environ */
284 cp
= buf
+ sizeof("search") - 1;
285 while (*cp
== ' ' || *cp
== '\t')
287 if ((*cp
== '\0') || (*cp
== '\n'))
289 strncpy(_res
.defdname
, cp
, sizeof(_res
.defdname
) - 1);
290 _res
.defdname
[sizeof(_res
.defdname
) - 1] = '\0';
291 if ((cp
= strchr(_res
.defdname
, '\n')) != NULL
)
294 * Set search list to be blank-separated strings
300 for (n
= 0; *cp
&& pp
< _res
.dnsrch
+ MAXDNSRCH
; cp
++) {
301 if (*cp
== ' ' || *cp
== '\t') {
309 /* null terminate last domain if there are excess */
310 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t')
317 /* read nameservers to query */
318 if (MATCH(buf
, "nameserver") && nserv
< MAXNS
) {
321 cp
= buf
+ sizeof("nameserver") - 1;
322 while (*cp
== ' ' || *cp
== '\t')
324 if ((*cp
!= '\0') && (*cp
!= '\n') && inet_aton(cp
, &a
)) {
325 _res
.nsaddr_list
[nserv
].sin_addr
= a
;
326 _res
.nsaddr_list
[nserv
].sin_family
= AF_INET
;
327 _res
.nsaddr_list
[nserv
].sin_port
=
328 htons(NAMESERVER_PORT
);
334 if (MATCH(buf
, "sortlist")) {
337 cp
= buf
+ sizeof("sortlist") - 1;
338 while (nsort
< MAXRESOLVSORT
) {
339 while (*cp
== ' ' || *cp
== '\t')
341 if (*cp
== '\0' || *cp
== '\n' || *cp
== ';')
344 while (*cp
&& !ISSORTMASK(*cp
) && *cp
!= ';' &&
345 isascii(*cp
) && !isspace(*cp
))
349 if (inet_aton(net
, &a
)) {
350 _res
.sort_list
[nsort
].addr
= a
;
354 while (*cp
&& *cp
!= ';' &&
355 isascii(*cp
) && !isspace(*cp
))
359 if (inet_aton(net
, &a
)) {
360 _res
.sort_list
[nsort
].mask
= a
.s_addr
;
362 _res
.sort_list
[nsort
].mask
=
363 net_mask(_res
.sort_list
[nsort
].addr
);
366 _res
.sort_list
[nsort
].mask
=
367 net_mask(_res
.sort_list
[nsort
].addr
);
376 if (MATCH(buf
, "options")) {
377 res_setoptions(buf
+ sizeof("options") - 1, "conf");
382 _res
.nscount
= nserv
;
388 if (_res
.defdname
[0] == 0 &&
389 gethostname(buf
, sizeof(_res
.defdname
) - 1) == 0 &&
390 (cp
= strchr(buf
, '.')) != NULL
)
391 strcpy(_res
.defdname
, cp
+ 1);
393 /* find components of local domain that might be searched */
394 if (havesearch
== 0) {
396 *pp
++ = _res
.defdname
;
401 for (cp
= _res
.defdname
; *cp
; cp
++)
402 dots
+= (*cp
== '.');
405 while (pp
< _res
.dnsrch
+ MAXDFLSRCH
) {
406 if (dots
< LOCALDOMAINPARTS
)
408 cp
= strchr(cp
, '.') + 1; /* we know there is one */
414 if (_res
.options
& RES_DEBUG
) {
415 printf(";; res_init()... default dnsrch list:\n");
416 for (pp
= _res
.dnsrch
; *pp
; pp
++)
417 printf(";;\t%s\n", *pp
);
418 printf(";;\t..END..\n");
421 #endif /* !RFC1535 */
429 _res
.options
|= RES_NOALIASES
;
430 else if ((cp
= getenv("RES_OPTIONS")) != NULL
)
431 res_setoptions(cp
, "env");
432 _res
.options
|= RES_INIT
;
449 if (_res
.options
& RES_DEBUG
)
450 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
454 /* skip leading and inner runs of spaces */
455 while (*cp
== ' ' || *cp
== '\t')
457 /* search for and process individual options */
458 if (!strncmp(cp
, "ndots:", sizeof("ndots:") - 1)) {
459 i
= atoi(cp
+ sizeof("ndots:") - 1);
460 if (i
<= RES_MAXNDOTS
)
463 _res
.ndots
= RES_MAXNDOTS
;
465 if (_res
.options
& RES_DEBUG
)
466 printf(";;\tndots=%d\n", _res
.ndots
);
468 } else if (!strncmp(cp
, "debug", sizeof("debug") - 1)) {
470 if (!(_res
.options
& RES_DEBUG
)) {
471 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
473 _res
.options
|= RES_DEBUG
;
475 printf(";;\tdebug\n");
477 } else if (!strncmp(cp
, "inet6", sizeof("inet6") - 1)) {
478 _res
.options
|= RES_USE_INET6
;
479 } else if (!strncmp(cp
, "no_tld_query", sizeof("no_tld_query") - 1)) {
480 _res
.options
|= RES_NOTLDQUERY
;
482 /* XXX - print a warning here? */
484 /* skip to next run of spaces */
485 while (*cp
&& *cp
!= ' ' && *cp
!= '\t')
491 /* XXX - should really support CIDR which means explicit masks always. */
497 register u_int32_t i
= ntohl(in
.s_addr
);
500 return (htonl(IN_CLASSA_NET
));
501 else if (IN_CLASSB(i
))
502 return (htonl(IN_CLASSB_NET
));
503 return (htonl(IN_CLASSC_NET
));
512 gettimeofday(&now
, NULL
);
513 return (0xffff & (now
.tv_sec
^ now
.tv_usec
/* ^ getpid() */));