source: rtems/c/src/exec/libnetworking/libc/res_init.c @ f0864664

4.104.114.84.95
Last change on this file since f0864664 was f0864664, checked in by Joel Sherrill <joel.sherrill@…>, on 11/25/00 at 19:45:56

2000-11-25 Eric Norum <eric.norum@…>

  • libc/res_init.c: Move include to avoid compiler dumping core.
  • Property mode set to 100644
File size: 15.3 KB
Line 
1/*
2 * Copyright (c) 1985, 1989, 1993
3 *    The Regents of the University of California.  All rights reserved.
4 *
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 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *      This product includes software developed by the University of
16 *      California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34/*
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
36 *
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
43 *
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 * SOFTWARE.
52 */
53
54/*
55 * Portions Copyright (c) 1996 by Internet Software Consortium.
56 *
57 * Permission to use, copy, modify, and distribute this software for any
58 * purpose with or without fee is hereby granted, provided that the above
59 * copyright notice and this permission notice appear in all copies.
60 *
61 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
62 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
63 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
64 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
65 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
66 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
67 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
68 * SOFTWARE.
69 */
70
71#if defined(LIBC_SCCS) && !defined(lint)
72static char sccsid[] = "@(#)res_init.c  8.1 (Berkeley) 6/7/93";
73static char orig_rcsid[] = "From: Id: res_init.c,v 8.7 1996/11/18 09:10:04 vixie Exp $";
74static char rcsid[] = "$Id$";
75#endif /* LIBC_SCCS and not lint */
76
77#include <sys/types.h>
78#include <sys/param.h>
79#include <sys/socket.h>
80#include <sys/time.h>
81#include <netinet/in.h>
82#include <arpa/inet.h>
83#include <arpa/nameser.h>
84#include <ctype.h>
85#include <resolv.h>
86#include <stdio.h>
87#include <stdlib.h>
88#include <string.h>
89#include <unistd.h>
90
91#include "res_config.h"
92
93/*
94 * RTEMS -- set up name servers from global variable
95 */
96include <rtems/rtems_bsdnet_internal.h>
97
98static void res_setoptions __P((char *, char *));
99
100#ifdef RESOLVSORT
101static const char sort_mask[] = "/&";
102#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
103static u_int32_t net_mask __P((struct in_addr));
104#endif
105
106#if !defined(isascii) /* XXX - could be a function */
107# define isascii(c) (!(c & 0200))
108#endif
109
110/*
111 * Resolver state default settings.
112 */
113
114struct __res_state _res
115# if defined(__BIND_RES_TEXT)
116        = { RES_TIMEOUT, }      /* Motorola, et al. */
117# endif
118        ;
119
120
121/*
122 * Set up default settings.  If the configuration file exist, the values
123 * there will have precedence.  Otherwise, the server address is set to
124 * INADDR_ANY and the default domain name comes from the gethostname().
125 *
126 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
127 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
128 * since it was noted that INADDR_ANY actually meant ``the first interface
129 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
130 * it had to be "up" in order for you to reach your own name server.  It
131 * was later decided that since the recommended practice is to always
132 * install local static routes through 127.0.0.1 for all your network
133 * interfaces, that we could solve this problem without a code change.
134 *
135 * The configuration file should always be used, since it is the only way
136 * to specify a default domain.  If you are running a server on your local
137 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
138 * in the configuration file.
139 *
140 * Return 0 if completes successfully, -1 on error
141 */
142int
143res_init()
144{
145        register FILE *fp;
146        register char *cp, **pp;
147        register int n;
148        char buf[MAXDNAME];
149        int nserv = 0;    /* number of nameserver records read from file */
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
159
160        /*
161         * These three fields used to be statically initialized.  This made
162         * it hard to use this code in a shared library.  It is necessary,
163         * now that we're doing dynamic initialization here, that we preserve
164         * the old semantics: if an application modifies one of these three
165         * fields of _res before res_init() is called, res_init() will not
166         * alter them.  Of course, if an application is setting them to
167         * _zero_ before calling res_init(), hoping to override what used
168         * to be the static default, we can't detect it and unexpected results
169         * will follow.  Zero for any of these fields would make no sense,
170         * so one can safely assume that the applications were already getting
171         * unexpected results.
172         *
173         * _res.options is tricky since some apps were known to diddle the bits
174         * before res_init() was first called. We can't replicate that semantic
175         * with dynamic initialization (they may have turned bits off that are
176         * set in RES_DEFAULT).  Our solution is to declare such applications
177         * "broken".  They could fool us by setting RES_INIT but none do (yet).
178         */
179        if (!_res.retrans)
180                _res.retrans = RES_TIMEOUT;
181        if (!_res.retry)
182                _res.retry = 4;
183        if (!(_res.options & RES_INIT))
184                _res.options = RES_DEFAULT;
185
186        /*
187         * This one used to initialize implicitly to zero, so unless the app
188         * has set it to something in particular, we can randomize it now.
189         */
190        if (!_res.id)
191                _res.id = res_randomid();
192
193#ifdef USELOOPBACK
194        _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
195#else
196        _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
197#endif
198        _res.nsaddr.sin_family = AF_INET;
199        _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
200        _res.nscount = 1;
201        _res.ndots = 1;
202        _res.pfcode = 0;
203
204        /*
205         * RTEMS -- Set up name servers
206         */
207        {
208        int n = 0;
209        while ((n < rtems_bsdnet_nameserver_count) && (nserv < MAXNS)) {
210                _res.nsaddr_list[nserv].sin_addr = rtems_bsdnet_nameserver[n];
211                _res.nsaddr_list[nserv].sin_family = AF_INET;
212                _res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
213                nserv++;
214                n++;
215        }
216        if (rtems_bsdnet_domain_name)
217                (void)strncpy(_res.defdname, rtems_bsdnet_domain_name, sizeof(_res.defdname) - 1);
218        }
219
220        /* Allow user to override the local domain definition */
221        if ((cp = getenv("LOCALDOMAIN")) != NULL) {
222                (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
223                haveenv++;
224
225                /*
226                 * Set search list to be blank-separated strings
227                 * from rest of env value.  Permits users of LOCALDOMAIN
228                 * to still have a search list, and anyone to set the
229                 * one that they want to use as an individual (even more
230                 * important now that the rfc1535 stuff restricts searches)
231                 */
232                cp = _res.defdname;
233                pp = _res.dnsrch;
234                *pp++ = cp;
235                for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
236                        if (*cp == '\n')        /* silly backwards compat */
237                                break;
238                        else if (*cp == ' ' || *cp == '\t') {
239                                *cp = 0;
240                                n = 1;
241                        } else if (n) {
242                                *pp++ = cp;
243                                n = 0;
244                                havesearch = 1;
245                        }
246                }
247                /* null terminate last domain if there are excess */
248                while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
249                        cp++;
250                *cp = '\0';
251                *pp++ = 0;
252        }
253
254#define MATCH(line, name) \
255        (!strncmp(line, name, sizeof(name) - 1) && \
256        (line[sizeof(name) - 1] == ' ' || \
257         line[sizeof(name) - 1] == '\t'))
258
259        if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
260            /* read the config file */
261            while (fgets(buf, sizeof(buf), fp) != NULL) {
262                /* skip comments */
263                if (*buf == ';' || *buf == '#')
264                        continue;
265                /* read default domain name */
266                if (MATCH(buf, "domain")) {
267                    if (haveenv)        /* skip if have from environ */
268                            continue;
269                    cp = buf + sizeof("domain") - 1;
270                    while (*cp == ' ' || *cp == '\t')
271                            cp++;
272                    if ((*cp == '\0') || (*cp == '\n'))
273                            continue;
274                    strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
275                    if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)
276                            *cp = '\0';
277                    havesearch = 0;
278                    continue;
279                }
280                /* set search list */
281                if (MATCH(buf, "search")) {
282                    if (haveenv)        /* skip if have from environ */
283                            continue;
284                    cp = buf + sizeof("search") - 1;
285                    while (*cp == ' ' || *cp == '\t')
286                            cp++;
287                    if ((*cp == '\0') || (*cp == '\n'))
288                            continue;
289                    strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
290                    if ((cp = strchr(_res.defdname, '\n')) != NULL)
291                            *cp = '\0';
292                    /*
293                     * Set search list to be blank-separated strings
294                     * on rest of line.
295                     */
296                    cp = _res.defdname;
297                    pp = _res.dnsrch;
298                    *pp++ = cp;
299                    for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
300                            if (*cp == ' ' || *cp == '\t') {
301                                    *cp = 0;
302                                    n = 1;
303                            } else if (n) {
304                                    *pp++ = cp;
305                                    n = 0;
306                            }
307                    }
308                    /* null terminate last domain if there are excess */
309                    while (*cp != '\0' && *cp != ' ' && *cp != '\t')
310                            cp++;
311                    *cp = '\0';
312                    *pp++ = 0;
313                    havesearch = 1;
314                    continue;
315                }
316                /* read nameservers to query */
317                if (MATCH(buf, "nameserver") && nserv < MAXNS) {
318                    struct in_addr a;
319
320                    cp = buf + sizeof("nameserver") - 1;
321                    while (*cp == ' ' || *cp == '\t')
322                        cp++;
323                    if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) {
324                        _res.nsaddr_list[nserv].sin_addr = a;
325                        _res.nsaddr_list[nserv].sin_family = AF_INET;
326                        _res.nsaddr_list[nserv].sin_port =
327                                htons(NAMESERVER_PORT);
328                        nserv++;
329                    }
330                    continue;
331                }
332#ifdef RESOLVSORT
333                if (MATCH(buf, "sortlist")) {
334                    struct in_addr a;
335
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((int)*cp) && !isspace((int)*cp))
345                                cp++;
346                        n = *cp;
347                        *cp = 0;
348                        if (inet_aton(net, &a)) {
349                            _res.sort_list[nsort].addr = a;
350                            if (ISSORTMASK(n)) {
351                                *cp++ = n;
352                                net = cp;
353                                while (*cp && *cp != ';' &&
354                                        isascii((int)*cp) && !isspace((int)*cp))
355                                    cp++;
356                                n = *cp;
357                                *cp = 0;
358                                if (inet_aton(net, &a)) {
359                                    _res.sort_list[nsort].mask = a.s_addr;
360                                } else {
361                                    _res.sort_list[nsort].mask =
362                                        net_mask(_res.sort_list[nsort].addr);
363                                }
364                            } else {
365                                _res.sort_list[nsort].mask =
366                                    net_mask(_res.sort_list[nsort].addr);
367                            }
368                            nsort++;
369                        }
370                        *cp = n;
371                    }
372                    continue;
373                }
374#endif
375                if (MATCH(buf, "options")) {
376                    res_setoptions(buf + sizeof("options") - 1, "conf");
377                    continue;
378                }
379            }
380            if (nserv > 1)
381                _res.nscount = nserv;
382#ifdef RESOLVSORT
383            _res.nsort = nsort;
384#endif
385            (void) fclose(fp);
386        }
387        if (_res.defdname[0] == 0 &&
388            gethostname(buf, sizeof(_res.defdname) - 1) == 0 &&
389            (cp = strchr(buf, '.')) != NULL)
390                strcpy(_res.defdname, cp + 1);
391
392        /* find components of local domain that might be searched */
393        if (havesearch == 0) {
394                pp = _res.dnsrch;
395                *pp++ = _res.defdname;
396                *pp = NULL;
397
398#ifndef RFC1535
399                dots = 0;
400                for (cp = _res.defdname; *cp; cp++)
401                        dots += (*cp == '.');
402
403                cp = _res.defdname;
404                while (pp < _res.dnsrch + MAXDFLSRCH) {
405                        if (dots < LOCALDOMAINPARTS)
406                                break;
407                        cp = strchr(cp, '.') + 1;    /* we know there is one */
408                        *pp++ = cp;
409                        dots--;
410                }
411                *pp = NULL;
412#ifdef DEBUG
413                if (_res.options & RES_DEBUG) {
414                        printf(";; res_init()... default dnsrch list:\n");
415                        for (pp = _res.dnsrch; *pp; pp++)
416                                printf(";;\t%s\n", *pp);
417                        printf(";;\t..END..\n");
418                }
419#endif
420#endif /* !RFC1535 */
421        }
422
423        if ((cp = getenv("RES_OPTIONS")) != NULL)
424                res_setoptions(cp, "env");
425        _res.options |= RES_INIT;
426        return (0);
427}
428
429static void
430res_setoptions(options, source)
431        char *options, *source;
432{
433        char *cp = options;
434        int i;
435
436#ifdef DEBUG
437        if (_res.options & RES_DEBUG)
438                printf(";; res_setoptions(\"%s\", \"%s\")...\n",
439                       options, source);
440#endif
441        while (*cp) {
442                /* skip leading and inner runs of spaces */
443                while (*cp == ' ' || *cp == '\t')
444                        cp++;
445                /* search for and process individual options */
446                if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
447                        i = atoi(cp + sizeof("ndots:") - 1);
448                        if (i <= RES_MAXNDOTS)
449                                _res.ndots = i;
450                        else
451                                _res.ndots = RES_MAXNDOTS;
452#ifdef DEBUG
453                        if (_res.options & RES_DEBUG)
454                                printf(";;\tndots=%d\n", _res.ndots);
455#endif
456                } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
457#ifdef DEBUG
458                        if (!(_res.options & RES_DEBUG)) {
459                                printf(";; res_setoptions(\"%s\", \"%s\")..\n",
460                                       options, source);
461                                _res.options |= RES_DEBUG;
462                        }
463                        printf(";;\tdebug\n");
464#endif
465                } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
466                        _res.options |= RES_USE_INET6;
467                } else if (!strncmp(cp, "no_tld_query", sizeof("no_tld_query") - 1)) {
468                        _res.options |= RES_NOTLDQUERY;
469                } else {
470                        /* XXX - print a warning here? */
471                }
472                /* skip to next run of spaces */
473                while (*cp && *cp != ' ' && *cp != '\t')
474                        cp++;
475        }
476}
477
478#ifdef RESOLVSORT
479/* XXX - should really support CIDR which means explicit masks always. */
480static u_int32_t
481net_mask(in)            /* XXX - should really use system's version of this */
482        struct in_addr in;
483{
484        register u_int32_t i = ntohl(in.s_addr);
485
486        if (IN_CLASSA(i))
487                return (htonl(IN_CLASSA_NET));
488        else if (IN_CLASSB(i))
489                return (htonl(IN_CLASSB_NET));
490        return (htonl(IN_CLASSC_NET));
491}
492#endif
493
494u_int
495res_randomid()
496{
497        struct timeval now;
498
499        gettimeofday(&now, NULL);
500        return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
501}
Note: See TracBrowser for help on using the repository browser.