source: rtems/cpukit/libnetworking/libc/res_init.c @ b90307a

4.104.114.84.95
Last change on this file since b90307a was b90307a, checked in by Ralf Corsepius <ralf.corsepius@…>, on 02/03/05 at 05:49:30

2005-02-03 Ralf Corsepius <ralf.corsepius@…>

PR 755/rtems

  • libnetworking/libc/res_init.c, libnetworking/lib/rtems_bsdnet_ntp.c, libnetworking/nfs/bootp_subr.c: Include <rtems/bsdnet/servers.h>.
  • 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 */
96#include <rtems/rtems_bsdnet_internal.h>
97#include <rtems/bsdnet/servers.h>
98
99static void res_setoptions __P((char *, char *));
100
101#ifdef RESOLVSORT
102static const char sort_mask[] = "/&";
103#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
104static u_int32_t net_mask __P((struct in_addr));
105#endif
106
107#if !defined(isascii) /* XXX - could be a function */
108# define isascii(c) (!(c & 0200))
109#endif
110
111/*
112 * Resolver state default settings.
113 */
114
115struct __res_state _res
116# if defined(__BIND_RES_TEXT)
117        = { RES_TIMEOUT, }      /* Motorola, et al. */
118# endif
119        ;
120
121
122/*
123 * Set up default settings.  If the configuration file exist, the values
124 * there will have precedence.  Otherwise, the server address is set to
125 * INADDR_ANY and the default domain name comes from the gethostname().
126 *
127 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
128 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
129 * since it was noted that INADDR_ANY actually meant ``the first interface
130 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
131 * it had to be "up" in order for you to reach your own name server.  It
132 * was later decided that since the recommended practice is to always
133 * install local static routes through 127.0.0.1 for all your network
134 * interfaces, that we could solve this problem without a code change.
135 *
136 * The configuration file should always be used, since it is the only way
137 * to specify a default domain.  If you are running a server on your local
138 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
139 * in the configuration file.
140 *
141 * Return 0 if completes successfully, -1 on error
142 */
143int
144res_init()
145{
146        register FILE *fp;
147        register char *cp, **pp;
148        register int n;
149        char buf[MAXDNAME];
150        int nserv = 0;    /* number of nameserver records read from file */
151        int haveenv = 0;
152        int havesearch = 0;
153#ifdef RESOLVSORT
154        int nsort = 0;
155        char *net;
156#endif
157#ifndef RFC1535
158        int dots;
159#endif
160
161        /*
162         * These three fields used to be statically initialized.  This made
163         * it hard to use this code in a shared library.  It is necessary,
164         * now that we're doing dynamic initialization here, that we preserve
165         * the old semantics: if an application modifies one of these three
166         * fields of _res before res_init() is called, res_init() will not
167         * alter them.  Of course, if an application is setting them to
168         * _zero_ before calling res_init(), hoping to override what used
169         * to be the static default, we can't detect it and unexpected results
170         * will follow.  Zero for any of these fields would make no sense,
171         * so one can safely assume that the applications were already getting
172         * unexpected results.
173         *
174         * _res.options is tricky since some apps were known to diddle the bits
175         * before res_init() was first called. We can't replicate that semantic
176         * with dynamic initialization (they may have turned bits off that are
177         * set in RES_DEFAULT).  Our solution is to declare such applications
178         * "broken".  They could fool us by setting RES_INIT but none do (yet).
179         */
180        if (!_res.retrans)
181                _res.retrans = RES_TIMEOUT;
182        if (!_res.retry)
183                _res.retry = 4;
184        if (!(_res.options & RES_INIT))
185                _res.options = RES_DEFAULT;
186
187        /*
188         * This one used to initialize implicitly to zero, so unless the app
189         * has set it to something in particular, we can randomize it now.
190         */
191        if (!_res.id)
192                _res.id = res_randomid();
193
194#ifdef USELOOPBACK
195        _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
196#else
197        _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
198#endif
199        _res.nsaddr.sin_family = AF_INET;
200        _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
201        _res.nscount = 1;
202        _res.ndots = 1;
203        _res.pfcode = 0;
204
205        /*
206         * RTEMS -- Set up name servers
207         */
208        {
209        int n = 0;
210        while ((n < rtems_bsdnet_nameserver_count) && (nserv < MAXNS)) {
211                _res.nsaddr_list[nserv].sin_addr = rtems_bsdnet_nameserver[n];
212                _res.nsaddr_list[nserv].sin_family = AF_INET;
213                _res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
214                nserv++;
215                n++;
216        }
217        if (rtems_bsdnet_domain_name)
218                (void)strncpy(_res.defdname, rtems_bsdnet_domain_name, sizeof(_res.defdname) - 1);
219        }
220
221        /* Allow user to override the local domain definition */
222        if ((cp = getenv("LOCALDOMAIN")) != NULL) {
223                (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
224                haveenv++;
225
226                /*
227                 * Set search list to be blank-separated strings
228                 * from rest of env value.  Permits users of LOCALDOMAIN
229                 * to still have a search list, and anyone to set the
230                 * one that they want to use as an individual (even more
231                 * important now that the rfc1535 stuff restricts searches)
232                 */
233                cp = _res.defdname;
234                pp = _res.dnsrch;
235                *pp++ = cp;
236                for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
237                        if (*cp == '\n')        /* silly backwards compat */
238                                break;
239                        else if (*cp == ' ' || *cp == '\t') {
240                                *cp = 0;
241                                n = 1;
242                        } else if (n) {
243                                *pp++ = cp;
244                                n = 0;
245                                havesearch = 1;
246                        }
247                }
248                /* null terminate last domain if there are excess */
249                while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
250                        cp++;
251                *cp = '\0';
252                *pp++ = 0;
253        }
254
255#define MATCH(line, name) \
256        (!strncmp(line, name, sizeof(name) - 1) && \
257        (line[sizeof(name) - 1] == ' ' || \
258         line[sizeof(name) - 1] == '\t'))
259
260        if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
261            /* read the config file */
262            while (fgets(buf, sizeof(buf), fp) != NULL) {
263                /* skip comments */
264                if (*buf == ';' || *buf == '#')
265                        continue;
266                /* read default domain name */
267                if (MATCH(buf, "domain")) {
268                    if (haveenv)        /* skip if have from environ */
269                            continue;
270                    cp = buf + sizeof("domain") - 1;
271                    while (*cp == ' ' || *cp == '\t')
272                            cp++;
273                    if ((*cp == '\0') || (*cp == '\n'))
274                            continue;
275                    strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
276                    if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)
277                            *cp = '\0';
278                    havesearch = 0;
279                    continue;
280                }
281                /* set search list */
282                if (MATCH(buf, "search")) {
283                    if (haveenv)        /* skip if have from environ */
284                            continue;
285                    cp = buf + sizeof("search") - 1;
286                    while (*cp == ' ' || *cp == '\t')
287                            cp++;
288                    if ((*cp == '\0') || (*cp == '\n'))
289                            continue;
290                    strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
291                    if ((cp = strchr(_res.defdname, '\n')) != NULL)
292                            *cp = '\0';
293                    /*
294                     * Set search list to be blank-separated strings
295                     * on rest of line.
296                     */
297                    cp = _res.defdname;
298                    pp = _res.dnsrch;
299                    *pp++ = cp;
300                    for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
301                            if (*cp == ' ' || *cp == '\t') {
302                                    *cp = 0;
303                                    n = 1;
304                            } else if (n) {
305                                    *pp++ = cp;
306                                    n = 0;
307                            }
308                    }
309                    /* null terminate last domain if there are excess */
310                    while (*cp != '\0' && *cp != ' ' && *cp != '\t')
311                            cp++;
312                    *cp = '\0';
313                    *pp++ = 0;
314                    havesearch = 1;
315                    continue;
316                }
317                /* read nameservers to query */
318                if (MATCH(buf, "nameserver") && nserv < MAXNS) {
319                    struct in_addr a;
320
321                    cp = buf + sizeof("nameserver") - 1;
322                    while (*cp == ' ' || *cp == '\t')
323                        cp++;
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);
329                        nserv++;
330                    }
331                    continue;
332                }
333#ifdef RESOLVSORT
334                if (MATCH(buf, "sortlist")) {
335                    struct in_addr a;
336
337                    cp = buf + sizeof("sortlist") - 1;
338                    while (nsort < MAXRESOLVSORT) {
339                        while (*cp == ' ' || *cp == '\t')
340                            cp++;
341                        if (*cp == '\0' || *cp == '\n' || *cp == ';')
342                            break;
343                        net = cp;
344                        while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
345                               isascii((int)*cp) && !isspace((int)*cp))
346                                cp++;
347                        n = *cp;
348                        *cp = 0;
349                        if (inet_aton(net, &a)) {
350                            _res.sort_list[nsort].addr = a;
351                            if (ISSORTMASK(n)) {
352                                *cp++ = n;
353                                net = cp;
354                                while (*cp && *cp != ';' &&
355                                        isascii((int)*cp) && !isspace((int)*cp))
356                                    cp++;
357                                n = *cp;
358                                *cp = 0;
359                                if (inet_aton(net, &a)) {
360                                    _res.sort_list[nsort].mask = a.s_addr;
361                                } else {
362                                    _res.sort_list[nsort].mask =
363                                        net_mask(_res.sort_list[nsort].addr);
364                                }
365                            } else {
366                                _res.sort_list[nsort].mask =
367                                    net_mask(_res.sort_list[nsort].addr);
368                            }
369                            nsort++;
370                        }
371                        *cp = n;
372                    }
373                    continue;
374                }
375#endif
376                if (MATCH(buf, "options")) {
377                    res_setoptions(buf + sizeof("options") - 1, "conf");
378                    continue;
379                }
380            }
381            if (nserv > 1)
382                _res.nscount = nserv;
383#ifdef RESOLVSORT
384            _res.nsort = nsort;
385#endif
386            (void) fclose(fp);
387        }
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);
392
393        /* find components of local domain that might be searched */
394        if (havesearch == 0) {
395                pp = _res.dnsrch;
396                *pp++ = _res.defdname;
397                *pp = NULL;
398
399#ifndef RFC1535
400                dots = 0;
401                for (cp = _res.defdname; *cp; cp++)
402                        dots += (*cp == '.');
403
404                cp = _res.defdname;
405                while (pp < _res.dnsrch + MAXDFLSRCH) {
406                        if (dots < LOCALDOMAINPARTS)
407                                break;
408                        cp = strchr(cp, '.') + 1;    /* we know there is one */
409                        *pp++ = cp;
410                        dots--;
411                }
412                *pp = NULL;
413#ifdef DEBUG
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");
419                }
420#endif
421#endif /* !RFC1535 */
422        }
423
424        if ((cp = getenv("RES_OPTIONS")) != NULL)
425                res_setoptions(cp, "env");
426        _res.options |= RES_INIT;
427        return (0);
428}
429
430static void
431res_setoptions(options, source)
432        char *options, *source;
433{
434        char *cp = options;
435        int i;
436
437#ifdef DEBUG
438        if (_res.options & RES_DEBUG)
439                printf(";; res_setoptions(\"%s\", \"%s\")...\n",
440                       options, source);
441#endif
442        while (*cp) {
443                /* skip leading and inner runs of spaces */
444                while (*cp == ' ' || *cp == '\t')
445                        cp++;
446                /* search for and process individual options */
447                if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
448                        i = atoi(cp + sizeof("ndots:") - 1);
449                        if (i <= RES_MAXNDOTS)
450                                _res.ndots = i;
451                        else
452                                _res.ndots = RES_MAXNDOTS;
453#ifdef DEBUG
454                        if (_res.options & RES_DEBUG)
455                                printf(";;\tndots=%d\n", _res.ndots);
456#endif
457                } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
458#ifdef DEBUG
459                        if (!(_res.options & RES_DEBUG)) {
460                                printf(";; res_setoptions(\"%s\", \"%s\")..\n",
461                                       options, source);
462                                _res.options |= RES_DEBUG;
463                        }
464                        printf(";;\tdebug\n");
465#endif
466                } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
467                        _res.options |= RES_USE_INET6;
468                } else if (!strncmp(cp, "no_tld_query", sizeof("no_tld_query") - 1)) {
469                        _res.options |= RES_NOTLDQUERY;
470                } else {
471                        /* XXX - print a warning here? */
472                }
473                /* skip to next run of spaces */
474                while (*cp && *cp != ' ' && *cp != '\t')
475                        cp++;
476        }
477}
478
479#ifdef RESOLVSORT
480/* XXX - should really support CIDR which means explicit masks always. */
481static u_int32_t
482net_mask(in)            /* XXX - should really use system's version of this */
483        struct in_addr in;
484{
485        register u_int32_t i = ntohl(in.s_addr);
486
487        if (IN_CLASSA(i))
488                return (htonl(IN_CLASSA_NET));
489        else if (IN_CLASSB(i))
490                return (htonl(IN_CLASSB_NET));
491        return (htonl(IN_CLASSC_NET));
492}
493#endif
494
495u_int
496res_randomid()
497{
498        struct timeval now;
499
500        gettimeofday(&now, NULL);
501        return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
502}
Note: See TracBrowser for help on using the repository browser.