Update.
[glibc.git] / resolv / res_init.c
blob1b14f94e762e0b8d99ecfa870e6cb1ee800d8e70
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 <stdlib.h>
85 #include <string.h>
86 #include <unistd.h>
88 /* Options. Should all be left alone. */
89 #define RESOLVSORT
90 #define RFC1535
91 /* #undef DEBUG */
93 static void res_setoptions (res_state, const char *, const char *)
94 internal_function;
96 #ifdef RESOLVSORT
97 static const char sort_mask[] = "/&";
98 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
99 static u_int32_t net_mask __P((struct in_addr));
100 #endif
102 #if !defined(isascii) /* XXX - could be a function */
103 # define isascii(c) (!(c & 0200))
104 #endif
107 * Resolver state default settings.
111 * Set up default settings. If the configuration file exist, the values
112 * there will have precedence. Otherwise, the server address is set to
113 * INADDR_ANY and the default domain name comes from the gethostname().
115 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
116 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
117 * since it was noted that INADDR_ANY actually meant ``the first interface
118 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
119 * it had to be "up" in order for you to reach your own name server. It
120 * was later decided that since the recommended practice is to always
121 * install local static routes through 127.0.0.1 for all your network
122 * interfaces, that we could solve this problem without a code change.
124 * The configuration file should always be used, since it is the only way
125 * to specify a default domain. If you are running a server on your local
126 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
127 * in the configuration file.
129 * Return 0 if completes successfully, -1 on error
132 res_ninit(res_state statp) {
133 extern int __res_vinit(res_state, int);
135 return (__res_vinit(statp, 0));
138 /* This function has to be reachable by res_data.c but not publically. */
140 __res_vinit(res_state statp, int preinit) {
141 register FILE *fp;
142 register char *cp, **pp;
143 register int n;
144 char buf[BUFSIZ];
145 int nserv = 0; /* number of nameserver records read from file */
146 #ifdef _LIBC
147 int nservall = 0; /* number of NS records read, nserv IPv4 only */
148 #endif
149 int haveenv = 0;
150 int havesearch = 0;
151 #ifdef RESOLVSORT
152 int nsort = 0;
153 char *net;
154 #endif
155 #ifndef RFC1535
156 int dots;
157 #endif
159 if (!preinit) {
160 statp->retrans = RES_TIMEOUT;
161 statp->retry = RES_DFLRETRY;
162 statp->options = RES_DEFAULT;
163 statp->id = res_randomid();
166 #ifdef USELOOPBACK
167 statp->nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
168 #else
169 statp->nsaddr.sin_addr.s_addr = INADDR_ANY;
170 #endif
171 statp->nsaddr.sin_family = AF_INET;
172 statp->nsaddr.sin_port = htons(NAMESERVER_PORT);
173 statp->nscount = 1;
174 statp->ndots = 1;
175 statp->pfcode = 0;
176 statp->_vcsock = -1;
177 statp->_flags = 0;
178 statp->qhook = NULL;
179 statp->rhook = NULL;
180 statp->_u._ext.nsinit = 0;
181 statp->_u._ext.nscount = 0;
182 #ifdef _LIBC
183 statp->_u._ext.nscount6 = 0;
184 for (n = 0; n < MAXNS; n++)
185 statp->_u._ext.nsaddrs[n] = NULL;
186 #endif
188 /* Allow user to override the local domain definition */
189 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
190 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
191 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
192 haveenv++;
195 * Set search list to be blank-separated strings
196 * from rest of env value. Permits users of LOCALDOMAIN
197 * to still have a search list, and anyone to set the
198 * one that they want to use as an individual (even more
199 * important now that the rfc1535 stuff restricts searches)
201 cp = statp->defdname;
202 pp = statp->dnsrch;
203 *pp++ = cp;
204 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
205 if (*cp == '\n') /* silly backwards compat */
206 break;
207 else if (*cp == ' ' || *cp == '\t') {
208 *cp = 0;
209 n = 1;
210 } else if (n) {
211 *pp++ = cp;
212 n = 0;
213 havesearch = 1;
216 /* null terminate last domain if there are excess */
217 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
218 cp++;
219 *cp = '\0';
220 *pp++ = 0;
223 #define MATCH(line, name) \
224 (!strncmp(line, name, sizeof(name) - 1) && \
225 (line[sizeof(name) - 1] == ' ' || \
226 line[sizeof(name) - 1] == '\t'))
228 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
229 /* read the config file */
230 while (fgets_unlocked(buf, sizeof(buf), fp) != NULL) {
231 /* skip comments */
232 if (*buf == ';' || *buf == '#')
233 continue;
234 /* read default domain name */
235 if (MATCH(buf, "domain")) {
236 if (haveenv) /* skip if have from environ */
237 continue;
238 cp = buf + sizeof("domain") - 1;
239 while (*cp == ' ' || *cp == '\t')
240 cp++;
241 if ((*cp == '\0') || (*cp == '\n'))
242 continue;
243 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
244 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
245 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
246 *cp = '\0';
247 havesearch = 0;
248 continue;
250 /* set search list */
251 if (MATCH(buf, "search")) {
252 if (haveenv) /* skip if have from environ */
253 continue;
254 cp = buf + sizeof("search") - 1;
255 while (*cp == ' ' || *cp == '\t')
256 cp++;
257 if ((*cp == '\0') || (*cp == '\n'))
258 continue;
259 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
260 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
261 if ((cp = strchr(statp->defdname, '\n')) != NULL)
262 *cp = '\0';
264 * Set search list to be blank-separated strings
265 * on rest of line.
267 cp = statp->defdname;
268 pp = statp->dnsrch;
269 *pp++ = cp;
270 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
271 if (*cp == ' ' || *cp == '\t') {
272 *cp = 0;
273 n = 1;
274 } else if (n) {
275 *pp++ = cp;
276 n = 0;
279 /* null terminate last domain if there are excess */
280 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
281 cp++;
282 *cp = '\0';
283 *pp++ = 0;
284 havesearch = 1;
285 continue;
287 /* read nameservers to query */
288 #ifdef _LIBC
289 if (MATCH(buf, "nameserver") && nservall < MAXNS) {
290 #else
291 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
292 #endif
293 struct in_addr a;
295 cp = buf + sizeof("nameserver") - 1;
296 while (*cp == ' ' || *cp == '\t')
297 cp++;
298 if ((*cp != '\0') && (*cp != '\n')
299 && __inet_aton(cp, &a)) {
300 statp->nsaddr_list[nserv].sin_addr = a;
301 statp->nsaddr_list[nserv].sin_family = AF_INET;
302 statp->nsaddr_list[nserv].sin_port =
303 htons(NAMESERVER_PORT);
304 nserv++;
305 #ifdef _LIBC
306 nservall++;
307 } else {
308 struct in6_addr a6;
309 char *el;
311 if ((el = strchr(cp, '\n')) != NULL)
312 *el = '\0';
313 if ((*cp != '\0') &&
314 (inet_pton(AF_INET6, cp, &a6) > 0)) {
315 struct sockaddr_in6 *sa6;
317 sa6 = malloc(sizeof(*sa6));
318 if (sa6 != NULL) {
319 sa6->sin6_addr = a6;
320 sa6->sin6_family = AF_INET6;
321 sa6->sin6_port = htons(NAMESERVER_PORT);
322 statp->_u._ext.nsaddrs[nservall] = sa6;
323 statp->_u._ext.nstimes[nservall] = RES_MAXTIME;
324 statp->_u._ext.nssocks[nservall] = -1;
325 nservall++;
328 #endif
330 continue;
332 #ifdef RESOLVSORT
333 if (MATCH(buf, "sortlist")) {
334 struct in_addr a;
336 cp = buf + sizeof("sortlist") - 1;
337 while (nsort < MAXRESOLVSORT) {
338 while (*cp == ' ' || *cp == '\t')
339 cp++;
340 if (*cp == '\0' || *cp == '\n' || *cp == ';')
341 break;
342 net = cp;
343 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
344 isascii(*cp) && !isspace(*cp))
345 cp++;
346 n = *cp;
347 *cp = 0;
348 if (__inet_aton(net, &a)) {
349 statp->sort_list[nsort].addr = a;
350 if (ISSORTMASK(n)) {
351 *cp++ = n;
352 net = cp;
353 while (*cp && *cp != ';' &&
354 isascii(*cp) && !isspace(*cp))
355 cp++;
356 n = *cp;
357 *cp = 0;
358 if (__inet_aton(net, &a)) {
359 statp->sort_list[nsort].mask = a.s_addr;
360 } else {
361 statp->sort_list[nsort].mask =
362 net_mask(statp->sort_list[nsort].addr);
364 } else {
365 statp->sort_list[nsort].mask =
366 net_mask(statp->sort_list[nsort].addr);
368 nsort++;
370 *cp = n;
372 continue;
374 #endif
375 if (MATCH(buf, "options")) {
376 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
377 continue;
380 if (nserv > 1)
381 statp->nscount = nserv;
382 #ifdef _LIBC
383 if (nservall - nserv > 0)
384 statp->_u._ext.nscount6 = nservall - nserv;
385 #endif
386 #ifdef RESOLVSORT
387 statp->nsort = nsort;
388 #endif
389 (void) fclose(fp);
391 if (statp->defdname[0] == 0 &&
392 __gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
393 (cp = strchr(buf, '.')) != NULL)
394 strcpy(statp->defdname, cp + 1);
396 /* find components of local domain that might be searched */
397 if (havesearch == 0) {
398 pp = statp->dnsrch;
399 *pp++ = statp->defdname;
400 *pp = NULL;
402 #ifndef RFC1535
403 dots = 0;
404 for (cp = statp->defdname; *cp; cp++)
405 dots += (*cp == '.');
407 cp = statp->defdname;
408 while (pp < statp->dnsrch + MAXDFLSRCH) {
409 if (dots < LOCALDOMAINPARTS)
410 break;
411 cp = __rawmemchr(cp, '.') + 1; /* we know there is one */
412 *pp++ = cp;
413 dots--;
415 *pp = NULL;
416 #ifdef DEBUG
417 if (statp->options & RES_DEBUG) {
418 printf(";; res_init()... default dnsrch list:\n");
419 for (pp = statp->dnsrch; *pp; pp++)
420 printf(";;\t%s\n", *pp);
421 printf(";;\t..END..\n");
423 #endif
424 #endif /* !RFC1535 */
427 if ((cp = getenv("RES_OPTIONS")) != NULL)
428 res_setoptions(statp, cp, "env");
429 statp->options |= RES_INIT;
430 return (0);
433 static void
434 internal_function
435 res_setoptions(res_state statp, const char *options, const char *source) {
436 const char *cp = options;
437 int i;
439 #ifdef DEBUG
440 if (statp->options & RES_DEBUG)
441 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
442 options, source);
443 #endif
444 while (*cp) {
445 /* skip leading and inner runs of spaces */
446 while (*cp == ' ' || *cp == '\t')
447 cp++;
448 /* search for and process individual options */
449 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
450 i = atoi(cp + sizeof("ndots:") - 1);
451 if (i <= RES_MAXNDOTS)
452 statp->ndots = i;
453 else
454 statp->ndots = RES_MAXNDOTS;
455 #ifdef DEBUG
456 if (statp->options & RES_DEBUG)
457 printf(";;\tndots=%d\n", statp->ndots);
458 #endif
459 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
460 i = atoi(cp + sizeof("timeout:") - 1);
461 if (i <= RES_MAXRETRANS)
462 statp->retrans = i;
463 else
464 statp->retrans = RES_MAXRETRANS;
465 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
466 i = atoi(cp + sizeof("attempts:") - 1);
467 if (i <= RES_MAXRETRY)
468 statp->retry = i;
469 else
470 statp->retry = RES_MAXRETRY;
471 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
472 #ifdef DEBUG
473 if (!(statp->options & RES_DEBUG)) {
474 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
475 options, source);
476 statp->options |= RES_DEBUG;
478 printf(";;\tdebug\n");
479 #endif
480 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
481 statp->options |= RES_USE_INET6;
482 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
483 statp->options |= RES_ROTATE;
484 } else if (!strncmp(cp, "no-check-names",
485 sizeof("no-check-names") - 1)) {
486 statp->options |= RES_NOCHECKNAME;
487 } else {
488 /* XXX - print a warning here? */
490 /* skip to next run of spaces */
491 while (*cp && *cp != ' ' && *cp != '\t')
492 cp++;
496 #ifdef RESOLVSORT
497 /* XXX - should really support CIDR which means explicit masks always. */
498 static u_int32_t
499 net_mask(in) /* XXX - should really use system's version of this */
500 struct in_addr in;
502 register u_int32_t i = ntohl(in.s_addr);
504 if (IN_CLASSA(i))
505 return (htonl(IN_CLASSA_NET));
506 else if (IN_CLASSB(i))
507 return (htonl(IN_CLASSB_NET));
508 return (htonl(IN_CLASSC_NET));
510 #endif
512 u_int
513 res_randomid(void) {
514 struct timeval now;
516 __gettimeofday(&now, NULL);
517 return (0xffff & (now.tv_sec ^ now.tv_usec ^ __getpid()));
521 * This routine is for closing the socket if a virtual circuit is used and
522 * the program wants to close it. This provides support for endhostent()
523 * which expects to close the socket.
525 * This routine is not expected to be user visible.
527 void
528 res_nclose(res_state statp) {
529 int ns;
531 if (statp->_vcsock >= 0) {
532 (void) __close(statp->_vcsock);
533 statp->_vcsock = -1;
534 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
536 #ifdef _LIBC
537 for (ns = 0; ns < statp->_u._ext.nscount + statp->_u._ext.nscount6;
538 ns++)
539 #else
540 for (ns = 0; ns < statp->_u._ext.nscount; ns++)
541 #endif
543 if (statp->_u._ext.nssocks[ns] != -1) {
544 (void) __close(statp->_u._ext.nssocks[ns]);
545 statp->_u._ext.nssocks[ns] = -1;
548 statp->_u._ext.nsinit = 0;