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

5
Last change on this file since cb68253 was cb68253, checked in by Sebastian Huber <sebastian.huber@…>, on 09/07/18 at 04:19:02

network: Use kernel/user space header files

Add and use <machine/rtems-bsd-kernel-space.h> and
<machine/rtems-bsd-user-space.h> similar to the libbsd to avoid command
line defines and defines scattered throught the code base.

Simplify cpukit/libnetworking/Makefile.am.

Update #3375.

  • Property mode set to 100644
File size: 15.1 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*
4 * Copyright (c) 1985, 1989, 1993
5 *    The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *      This product includes software developed by the University of
18 *      California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36/*
37 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
38 *
39 * Permission to use, copy, modify, and distribute this software for any
40 * purpose with or without fee is hereby granted, provided that the above
41 * copyright notice and this permission notice appear in all copies, and that
42 * the name of Digital Equipment Corporation not be used in advertising or
43 * publicity pertaining to distribution of the document or software without
44 * specific, written prior permission.
45 *
46 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
47 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
49 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53 * SOFTWARE.
54 */
55
56/*
57 * Portions Copyright (c) 1996 by Internet Software Consortium.
58 *
59 * Permission to use, copy, modify, and distribute this software for any
60 * purpose with or without fee is hereby granted, provided that the above
61 * copyright notice and this permission notice appear in all copies.
62 *
63 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
64 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
65 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
66 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
67 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
68 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
69 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
70 * SOFTWARE.
71 */
72
73#if HAVE_CONFIG_H
74#include "config.h"
75#endif
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(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(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(void)
145{
146        FILE *fp;
147        char *cp, **pp;
148        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((unsigned char)*cp) && !isspace((unsigned char)*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((unsigned char)*cp) && !isspace((unsigned char)*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(
432        char *options,
433        char *source)
434{
435        char *cp = options;
436        int i;
437
438#ifdef DEBUG
439        if (_res.options & RES_DEBUG)
440                printf(";; res_setoptions(\"%s\", \"%s\")...\n",
441                       options, source);
442#endif
443        while (*cp) {
444                /* skip leading and inner runs of spaces */
445                while (*cp == ' ' || *cp == '\t')
446                        cp++;
447                /* search for and process individual options */
448                if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
449                        i = atoi(cp + sizeof("ndots:") - 1);
450                        if (i <= RES_MAXNDOTS)
451                                _res.ndots = i;
452                        else
453                                _res.ndots = RES_MAXNDOTS;
454#ifdef DEBUG
455                        if (_res.options & RES_DEBUG)
456                                printf(";;\tndots=%d\n", _res.ndots);
457#endif
458                } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
459#ifdef DEBUG
460                        if (!(_res.options & RES_DEBUG)) {
461                                printf(";; res_setoptions(\"%s\", \"%s\")..\n",
462                                       options, source);
463                                _res.options |= RES_DEBUG;
464                        }
465                        printf(";;\tdebug\n");
466#endif
467                } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
468                        _res.options |= RES_USE_INET6;
469                } else if (!strncmp(cp, "no_tld_query", sizeof("no_tld_query") - 1)) {
470                        _res.options |= RES_NOTLDQUERY;
471                } else {
472                        /* XXX - print a warning here? */
473                }
474                /* skip to next run of spaces */
475                while (*cp && *cp != ' ' && *cp != '\t')
476                        cp++;
477        }
478}
479
480#ifdef RESOLVSORT
481/* XXX - should really support CIDR which means explicit masks always. */
482static u_int32_t
483net_mask(               /* XXX - should really use system's version of this */
484        struct in_addr in)
485{
486        u_int32_t i = ntohl(in.s_addr);
487
488        if (IN_CLASSA(i))
489                return (htonl(IN_CLASSA_NET));
490        else if (IN_CLASSB(i))
491                return (htonl(IN_CLASSB_NET));
492        return (htonl(IN_CLASSC_NET));
493}
494#endif
495
496u_int
497res_randomid(void)
498{
499        struct timeval now;
500
501        gettimeofday(&now, NULL);
502        return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
503}
Note: See TracBrowser for help on using the repository browser.