Dummy dl-procinfo.c file for platforms which don't have one.
[glibc.git] / resolv / res_init.c
blobbb66b02b6c16c822a58e6d09f34b2a524b5dd749
1 /*
2 * Copyright (c) 1985, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies, and that
36 * the name of Digital Equipment Corporation not be used in advertising or
37 * publicity pertaining to distribution of the document or software without
38 * specific, written prior permission.
40 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
43 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
47 * SOFTWARE.
51 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
53 * Permission to use, copy, modify, and distribute this software for any
54 * purpose with or without fee is hereby granted, provided that the above
55 * copyright notice and this permission notice appear in all copies.
57 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
58 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
60 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
61 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
62 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
63 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
64 * SOFTWARE.
67 #if defined(LIBC_SCCS) && !defined(lint)
68 static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
69 static const char rcsid[] = "$BINDId: res_init.c,v 8.16 2000/05/09 07:10:12 vixie Exp $";
70 #endif /* LIBC_SCCS and not lint */
72 #include <sys/types.h>
73 #include <sys/param.h>
74 #include <sys/socket.h>
75 #include <sys/time.h>
77 #include <netinet/in.h>
78 #include <arpa/inet.h>
79 #include <arpa/nameser.h>
81 #include <ctype.h>
82 #include <resolv.h>
83 #include <stdio.h>
84 #include <stdio_ext.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <unistd.h>
89 /* Options. Should all be left alone. */
90 #define RESOLVSORT
91 #define RFC1535
92 /* #undef DEBUG */
94 static void res_setoptions (res_state, const char *, const char *)
95 internal_function;
97 #ifdef RESOLVSORT
98 static const char sort_mask[] = "/&";
99 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
100 static u_int32_t net_mask __P((struct in_addr));
101 #endif
103 #if !defined(isascii) /* XXX - could be a function */
104 # define isascii(c) (!(c & 0200))
105 #endif
108 * Resolver state default settings.
112 * Set up default settings. If the configuration file exist, the values
113 * there will have precedence. Otherwise, the server address is set to
114 * INADDR_ANY and the default domain name comes from the gethostname().
116 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
117 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
118 * since it was noted that INADDR_ANY actually meant ``the first interface
119 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
120 * it had to be "up" in order for you to reach your own name server. It
121 * was later decided that since the recommended practice is to always
122 * install local static routes through 127.0.0.1 for all your network
123 * interfaces, that we could solve this problem without a code change.
125 * The configuration file should always be used, since it is the only way
126 * to specify a default domain. If you are running a server on your local
127 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
128 * in the configuration file.
130 * Return 0 if completes successfully, -1 on error
133 res_ninit(res_state statp) {
134 extern int __res_vinit(res_state, int);
136 return (__res_vinit(statp, 0));
139 /* This function has to be reachable by res_data.c but not publically. */
141 __res_vinit(res_state statp, int preinit) {
142 register FILE *fp;
143 register char *cp, **pp;
144 register int n;
145 char buf[BUFSIZ];
146 int nserv = 0; /* number of nameserver records read from file */
147 #ifdef _LIBC
148 int nservall = 0; /* number of NS records read, nserv IPv4 only */
149 #endif
150 int haveenv = 0;
151 int havesearch = 0;
152 #ifdef RESOLVSORT
153 int nsort = 0;
154 char *net;
155 #endif
156 #ifndef RFC1535
157 int dots;
158 #endif
160 if (!preinit) {
161 statp->retrans = RES_TIMEOUT;
162 statp->retry = RES_DFLRETRY;
163 statp->options = RES_DEFAULT;
164 statp->id = res_randomid();
167 #ifdef USELOOPBACK
168 statp->nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
169 #else
170 statp->nsaddr.sin_addr.s_addr = INADDR_ANY;
171 #endif
172 statp->nsaddr.sin_family = AF_INET;
173 statp->nsaddr.sin_port = htons(NAMESERVER_PORT);
174 statp->nscount = 1;
175 statp->ndots = 1;
176 statp->pfcode = 0;
177 statp->_vcsock = -1;
178 statp->_flags = 0;
179 statp->qhook = NULL;
180 statp->rhook = NULL;
181 statp->_u._ext.nsinit = 0;
182 statp->_u._ext.nscount = 0;
183 #ifdef _LIBC
184 statp->_u._ext.nscount6 = 0;
185 for (n = 0; n < MAXNS; n++)
186 statp->_u._ext.nsaddrs[n] = NULL;
187 #endif
189 /* Allow user to override the local domain definition */
190 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
191 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
192 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
193 haveenv++;
196 * Set search list to be blank-separated strings
197 * from rest of env value. Permits users of LOCALDOMAIN
198 * to still have a search list, and anyone to set the
199 * one that they want to use as an individual (even more
200 * important now that the rfc1535 stuff restricts searches)
202 cp = statp->defdname;
203 pp = statp->dnsrch;
204 *pp++ = cp;
205 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
206 if (*cp == '\n') /* silly backwards compat */
207 break;
208 else if (*cp == ' ' || *cp == '\t') {
209 *cp = 0;
210 n = 1;
211 } else if (n) {
212 *pp++ = cp;
213 n = 0;
214 havesearch = 1;
217 /* null terminate last domain if there are excess */
218 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
219 cp++;
220 *cp = '\0';
221 *pp++ = 0;
224 #define MATCH(line, name) \
225 (!strncmp(line, name, sizeof(name) - 1) && \
226 (line[sizeof(name) - 1] == ' ' || \
227 line[sizeof(name) - 1] == '\t'))
229 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
230 /* No threads use this stream. */
231 __fsetlocking (fp, FSETLOCKING_BYCALLER);
232 /* read the config file */
233 while (fgets_unlocked(buf, sizeof(buf), fp) != NULL) {
234 /* skip comments */
235 if (*buf == ';' || *buf == '#')
236 continue;
237 /* read default domain name */
238 if (MATCH(buf, "domain")) {
239 if (haveenv) /* skip if have from environ */
240 continue;
241 cp = buf + sizeof("domain") - 1;
242 while (*cp == ' ' || *cp == '\t')
243 cp++;
244 if ((*cp == '\0') || (*cp == '\n'))
245 continue;
246 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
247 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
248 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
249 *cp = '\0';
250 havesearch = 0;
251 continue;
253 /* set search list */
254 if (MATCH(buf, "search")) {
255 if (haveenv) /* skip if have from environ */
256 continue;
257 cp = buf + sizeof("search") - 1;
258 while (*cp == ' ' || *cp == '\t')
259 cp++;
260 if ((*cp == '\0') || (*cp == '\n'))
261 continue;
262 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
263 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
264 if ((cp = strchr(statp->defdname, '\n')) != NULL)
265 *cp = '\0';
267 * Set search list to be blank-separated strings
268 * on rest of line.
270 cp = statp->defdname;
271 pp = statp->dnsrch;
272 *pp++ = cp;
273 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
274 if (*cp == ' ' || *cp == '\t') {
275 *cp = 0;
276 n = 1;
277 } else if (n) {
278 *pp++ = cp;
279 n = 0;
282 /* null terminate last domain if there are excess */
283 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
284 cp++;
285 *cp = '\0';
286 *pp++ = 0;
287 havesearch = 1;
288 continue;
290 /* read nameservers to query */
291 #ifdef _LIBC
292 if (MATCH(buf, "nameserver") && nservall < MAXNS) {
293 #else
294 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
295 #endif
296 struct in_addr a;
298 cp = buf + sizeof("nameserver") - 1;
299 while (*cp == ' ' || *cp == '\t')
300 cp++;
301 if ((*cp != '\0') && (*cp != '\n')
302 && __inet_aton(cp, &a)) {
303 statp->nsaddr_list[nserv].sin_addr = a;
304 statp->nsaddr_list[nserv].sin_family = AF_INET;
305 statp->nsaddr_list[nserv].sin_port =
306 htons(NAMESERVER_PORT);
307 nserv++;
308 #ifdef _LIBC
309 nservall++;
310 } else {
311 struct in6_addr a6;
312 char *el;
314 if ((el = strchr(cp, '\n')) != NULL)
315 *el = '\0';
316 if ((*cp != '\0') &&
317 (inet_pton(AF_INET6, cp, &a6) > 0)) {
318 struct sockaddr_in6 *sa6;
320 sa6 = malloc(sizeof(*sa6));
321 if (sa6 != NULL) {
322 sa6->sin6_addr = a6;
323 sa6->sin6_family = AF_INET6;
324 sa6->sin6_port = htons(NAMESERVER_PORT);
325 statp->_u._ext.nsaddrs[nservall] = sa6;
326 statp->_u._ext.nstimes[nservall] = RES_MAXTIME;
327 statp->_u._ext.nssocks[nservall] = -1;
328 nservall++;
331 #endif
333 continue;
335 #ifdef RESOLVSORT
336 if (MATCH(buf, "sortlist")) {
337 struct in_addr a;
339 cp = buf + sizeof("sortlist") - 1;
340 while (nsort < MAXRESOLVSORT) {
341 while (*cp == ' ' || *cp == '\t')
342 cp++;
343 if (*cp == '\0' || *cp == '\n' || *cp == ';')
344 break;
345 net = cp;
346 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
347 isascii(*cp) && !isspace(*cp))
348 cp++;
349 n = *cp;
350 *cp = 0;
351 if (__inet_aton(net, &a)) {
352 statp->sort_list[nsort].addr = a;
353 if (ISSORTMASK(n)) {
354 *cp++ = n;
355 net = cp;
356 while (*cp && *cp != ';' &&
357 isascii(*cp) && !isspace(*cp))
358 cp++;
359 n = *cp;
360 *cp = 0;
361 if (__inet_aton(net, &a)) {
362 statp->sort_list[nsort].mask = a.s_addr;
363 } else {
364 statp->sort_list[nsort].mask =
365 net_mask(statp->sort_list[nsort].addr);
367 } else {
368 statp->sort_list[nsort].mask =
369 net_mask(statp->sort_list[nsort].addr);
371 nsort++;
373 *cp = n;
375 continue;
377 #endif
378 if (MATCH(buf, "options")) {
379 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
380 continue;
383 if (nserv > 1)
384 statp->nscount = nserv;
385 #ifdef _LIBC
386 if (nservall - nserv > 0)
387 statp->_u._ext.nscount6 = nservall - nserv;
388 #endif
389 #ifdef RESOLVSORT
390 statp->nsort = nsort;
391 #endif
392 (void) fclose(fp);
394 if (statp->defdname[0] == 0 &&
395 __gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
396 (cp = strchr(buf, '.')) != NULL)
397 strcpy(statp->defdname, cp + 1);
399 /* find components of local domain that might be searched */
400 if (havesearch == 0) {
401 pp = statp->dnsrch;
402 *pp++ = statp->defdname;
403 *pp = NULL;
405 #ifndef RFC1535
406 dots = 0;
407 for (cp = statp->defdname; *cp; cp++)
408 dots += (*cp == '.');
410 cp = statp->defdname;
411 while (pp < statp->dnsrch + MAXDFLSRCH) {
412 if (dots < LOCALDOMAINPARTS)
413 break;
414 cp = __rawmemchr(cp, '.') + 1; /* we know there is one */
415 *pp++ = cp;
416 dots--;
418 *pp = NULL;
419 #ifdef DEBUG
420 if (statp->options & RES_DEBUG) {
421 printf(";; res_init()... default dnsrch list:\n");
422 for (pp = statp->dnsrch; *pp; pp++)
423 printf(";;\t%s\n", *pp);
424 printf(";;\t..END..\n");
426 #endif
427 #endif /* !RFC1535 */
430 if ((cp = getenv("RES_OPTIONS")) != NULL)
431 res_setoptions(statp, cp, "env");
432 statp->options |= RES_INIT;
433 return (0);
436 static void
437 internal_function
438 res_setoptions(res_state statp, const char *options, const char *source) {
439 const char *cp = options;
440 int i;
442 #ifdef DEBUG
443 if (statp->options & RES_DEBUG)
444 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
445 options, source);
446 #endif
447 while (*cp) {
448 /* skip leading and inner runs of spaces */
449 while (*cp == ' ' || *cp == '\t')
450 cp++;
451 /* search for and process individual options */
452 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
453 i = atoi(cp + sizeof("ndots:") - 1);
454 if (i <= RES_MAXNDOTS)
455 statp->ndots = i;
456 else
457 statp->ndots = RES_MAXNDOTS;
458 #ifdef DEBUG
459 if (statp->options & RES_DEBUG)
460 printf(";;\tndots=%d\n", statp->ndots);
461 #endif
462 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
463 i = atoi(cp + sizeof("timeout:") - 1);
464 if (i <= RES_MAXRETRANS)
465 statp->retrans = i;
466 else
467 statp->retrans = RES_MAXRETRANS;
468 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
469 i = atoi(cp + sizeof("attempts:") - 1);
470 if (i <= RES_MAXRETRY)
471 statp->retry = i;
472 else
473 statp->retry = RES_MAXRETRY;
474 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
475 #ifdef DEBUG
476 if (!(statp->options & RES_DEBUG)) {
477 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
478 options, source);
479 statp->options |= RES_DEBUG;
481 printf(";;\tdebug\n");
482 #endif
483 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
484 statp->options |= RES_USE_INET6;
485 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
486 statp->options |= RES_ROTATE;
487 } else if (!strncmp(cp, "no-check-names",
488 sizeof("no-check-names") - 1)) {
489 statp->options |= RES_NOCHECKNAME;
490 } else {
491 /* XXX - print a warning here? */
493 /* skip to next run of spaces */
494 while (*cp && *cp != ' ' && *cp != '\t')
495 cp++;
499 #ifdef RESOLVSORT
500 /* XXX - should really support CIDR which means explicit masks always. */
501 static u_int32_t
502 net_mask(in) /* XXX - should really use system's version of this */
503 struct in_addr in;
505 register u_int32_t i = ntohl(in.s_addr);
507 if (IN_CLASSA(i))
508 return (htonl(IN_CLASSA_NET));
509 else if (IN_CLASSB(i))
510 return (htonl(IN_CLASSB_NET));
511 return (htonl(IN_CLASSC_NET));
513 #endif
515 u_int
516 res_randomid(void) {
517 struct timeval now;
519 __gettimeofday(&now, NULL);
520 return (0xffff & (now.tv_sec ^ now.tv_usec ^ __getpid()));
524 * This routine is for closing the socket if a virtual circuit is used and
525 * the program wants to close it. This provides support for endhostent()
526 * which expects to close the socket.
528 * This routine is not expected to be user visible.
530 void
531 res_nclose(res_state statp) {
532 int ns;
534 if (statp->_vcsock >= 0) {
535 (void) __close(statp->_vcsock);
536 statp->_vcsock = -1;
537 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
539 #ifdef _LIBC
540 for (ns = 0; ns < statp->_u._ext.nscount + statp->_u._ext.nscount6;
541 ns++)
542 #else
543 for (ns = 0; ns < statp->_u._ext.nscount; ns++)
544 #endif
546 if (statp->_u._ext.nssocks[ns] != -1) {
547 (void) __close(statp->_u._ext.nssocks[ns]);
548 statp->_u._ext.nssocks[ns] = -1;
551 statp->_u._ext.nsinit = 0;