OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [libnetworking/] [libc/] [res_init.c] - Blame information for rev 535

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.