source: rtems-libbsd/freebsd/lib/libc/resolv/res_init.c @ d48955b

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since d48955b was d48955b, checked in by Sebastian Huber <sebastian.huber@…>, on 11/06/13 at 08:02:16

Add and use <machine/rtems-bsd-user-space.h>

  • Property mode set to 100644
File size: 24.3 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 * 4. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/*
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 */
51
52/*
53 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
54 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
55 *
56 * Permission to use, copy, modify, and distribute this software for any
57 * purpose with or without fee is hereby granted, provided that the above
58 * copyright notice and this permission notice appear in all copies.
59 *
60 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
61 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
62 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
63 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
64 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
65 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
66 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
67 */
68
69#if defined(LIBC_SCCS) && !defined(lint)
70static const char sccsid[] = "@(#)res_init.c    8.1 (Berkeley) 6/7/93";
71static const char rcsid[] = "$Id: res_init.c,v 1.16.18.7 2007/07/09 01:52:58 marka Exp $";
72#endif /* LIBC_SCCS and not lint */
73#include <sys/cdefs.h>
74__FBSDID("$FreeBSD$");
75
76#include "port_before.h"
77
78#include "namespace.h"
79
80#include <rtems/bsd/sys/types.h>
81#include <rtems/bsd/sys/param.h>
82#include <sys/socket.h>
83#include <rtems/bsd/sys/time.h>
84
85#include <netinet/in.h>
86#include <arpa/inet.h>
87#include <arpa/nameser.h>
88
89#include <ctype.h>
90#include <stdio.h>
91#include <stdlib.h>
92#include <string.h>
93#include <unistd.h>
94#include <netdb.h>
95
96#include "un-namespace.h"
97
98#include "port_after.h"
99
100/* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
101#include <resolv.h>
102
103#include "res_private.h"
104
105/*% Options.  Should all be left alone. */
106#define RESOLVSORT
107#define DEBUG
108
109#ifdef SOLARIS2
110#include <sys/systeminfo.h>
111#endif
112
113static void res_setoptions(res_state, const char *, const char *);
114
115#ifdef RESOLVSORT
116static const char sort_mask[] = "/&";
117#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
118static u_int32_t net_mask(struct in_addr);
119#endif
120
121#if !defined(isascii)   /*%< XXX - could be a function */
122# define isascii(c) (!(c & 0200))
123#endif
124
125/*
126 * Resolver state default settings.
127 */
128
129/*%
130 * Set up default settings.  If the configuration file exist, the values
131 * there will have precedence.  Otherwise, the server address is set to
132 * INADDR_ANY and the default domain name comes from the gethostname().
133 *
134 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
135 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
136 * since it was noted that INADDR_ANY actually meant ``the first interface
137 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
138 * it had to be "up" in order for you to reach your own name server.  It
139 * was later decided that since the recommended practice is to always
140 * install local static routes through 127.0.0.1 for all your network
141 * interfaces, that we could solve this problem without a code change.
142 *
143 * The configuration file should always be used, since it is the only way
144 * to specify a default domain.  If you are running a server on your local
145 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
146 * in the configuration file.
147 *
148 * Return 0 if completes successfully, -1 on error
149 */
150int
151res_ninit(res_state statp) {
152        extern int __res_vinit(res_state, int);
153
154        return (__res_vinit(statp, 0));
155}
156
157/*% This function has to be reachable by res_data.c but not publically. */
158int
159__res_vinit(res_state statp, int preinit) {
160        FILE *fp;
161        char *cp, **pp;
162        int n;
163        char buf[BUFSIZ];
164        int nserv = 0;    /*%< number of nameserver records read from file */
165        int haveenv = 0;
166        int havesearch = 0;
167#ifdef RESOLVSORT
168        int nsort = 0;
169        char *net;
170#endif
171        int dots;
172        union res_sockaddr_union u[2];
173        int maxns = MAXNS;
174
175        RES_SET_H_ERRNO(statp, 0);
176        if (statp->_u._ext.ext != NULL)
177                res_ndestroy(statp);
178
179        if (!preinit) {
180                statp->retrans = RES_TIMEOUT;
181                statp->retry = RES_DFLRETRY;
182                statp->options = RES_DEFAULT;
183                statp->id = res_randomid();
184        }
185
186        memset(u, 0, sizeof(u));
187#ifdef USELOOPBACK
188        u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
189#else
190        u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
191#endif
192        u[nserv].sin.sin_family = AF_INET;
193        u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
194#ifdef HAVE_SA_LEN
195        u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
196#endif
197        nserv++;
198#ifdef HAS_INET6_STRUCTS
199#ifdef USELOOPBACK
200        u[nserv].sin6.sin6_addr = in6addr_loopback;
201#else
202        u[nserv].sin6.sin6_addr = in6addr_any;
203#endif
204        u[nserv].sin6.sin6_family = AF_INET6;
205        u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
206#ifdef HAVE_SA_LEN
207        u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
208#endif
209        nserv++;
210#endif
211        statp->nscount = 0;
212        statp->ndots = 1;
213        statp->pfcode = 0;
214        statp->_vcsock = -1;
215        statp->_flags = 0;
216        statp->qhook = NULL;
217        statp->rhook = NULL;
218        statp->_u._ext.nscount = 0;
219        statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
220        if (statp->_u._ext.ext != NULL) {
221                memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
222                statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
223                strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
224                strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
225        } else {
226                /*
227                 * Historically res_init() rarely, if at all, failed.
228                 * Examples and applications exist which do not check
229                 * our return code.  Furthermore several applications
230                 * simply call us to get the systems domainname.  So
231                 * rather then immediately fail here we store the
232                 * failure, which is returned later, in h_errno.  And
233                 * prevent the collection of 'nameserver' information
234                 * by setting maxns to 0.  Thus applications that fail
235                 * to check our return code wont be able to make
236                 * queries anyhow.
237                 */
238                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
239                maxns = 0;
240        }
241#ifdef RESOLVSORT
242        statp->nsort = 0;
243#endif
244        res_setservers(statp, u, nserv);
245
246#ifdef  SOLARIS2
247        /*
248         * The old libresolv derived the defaultdomain from NIS/NIS+.
249         * We want to keep this behaviour
250         */
251        {
252                char buf[sizeof(statp->defdname)], *cp;
253                int ret;
254
255                if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 &&
256                        (unsigned int)ret <= sizeof(buf)) {
257                        if (buf[0] == '+')
258                                buf[0] = '.';
259                        cp = strchr(buf, '.');
260                        cp = (cp == NULL) ? buf : (cp + 1);
261                        strncpy(statp->defdname, cp,
262                                sizeof(statp->defdname) - 1);
263                        statp->defdname[sizeof(statp->defdname) - 1] = '\0';
264                }
265        }
266#endif  /* SOLARIS2 */
267
268        /* Allow user to override the local domain definition */
269        if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) {
270                (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
271                statp->defdname[sizeof(statp->defdname) - 1] = '\0';
272                haveenv++;
273
274                /*
275                 * Set search list to be blank-separated strings
276                 * from rest of env value.  Permits users of LOCALDOMAIN
277                 * to still have a search list, and anyone to set the
278                 * one that they want to use as an individual (even more
279                 * important now that the rfc1535 stuff restricts searches)
280                 */
281                cp = statp->defdname;
282                pp = statp->dnsrch;
283                *pp++ = cp;
284                for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
285                        if (*cp == '\n')        /*%< silly backwards compat */
286                                break;
287                        else if (*cp == ' ' || *cp == '\t') {
288                                *cp = 0;
289                                n = 1;
290                        } else if (n) {
291                                *pp++ = cp;
292                                n = 0;
293                                havesearch = 1;
294                        }
295                }
296                /* null terminate last domain if there are excess */
297                while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
298                        cp++;
299                *cp = '\0';
300                *pp++ = 0;
301        }
302
303#define MATCH(line, name) \
304        (!strncmp(line, name, sizeof(name) - 1) && \
305        (line[sizeof(name) - 1] == ' ' || \
306         line[sizeof(name) - 1] == '\t'))
307
308        nserv = 0;
309        if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
310            /* read the config file */
311            while (fgets(buf, sizeof(buf), fp) != NULL) {
312                /* skip comments */
313                if (*buf == ';' || *buf == '#')
314                        continue;
315                /* read default domain name */
316                if (MATCH(buf, "domain")) {
317                    if (haveenv)        /*%< skip if have from environ */
318                            continue;
319                    cp = buf + sizeof("domain") - 1;
320                    while (*cp == ' ' || *cp == '\t')
321                            cp++;
322                    if ((*cp == '\0') || (*cp == '\n'))
323                            continue;
324                    strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
325                    statp->defdname[sizeof(statp->defdname) - 1] = '\0';
326                    if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
327                            *cp = '\0';
328                    havesearch = 0;
329                    continue;
330                }
331                /* set search list */
332                if (MATCH(buf, "search")) {
333                    if (haveenv)        /*%< skip if have from environ */
334                            continue;
335                    cp = buf + sizeof("search") - 1;
336                    while (*cp == ' ' || *cp == '\t')
337                            cp++;
338                    if ((*cp == '\0') || (*cp == '\n'))
339                            continue;
340                    strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
341                    statp->defdname[sizeof(statp->defdname) - 1] = '\0';
342                    if ((cp = strchr(statp->defdname, '\n')) != NULL)
343                            *cp = '\0';
344                    /*
345                     * Set search list to be blank-separated strings
346                     * on rest of line.
347                     */
348                    cp = statp->defdname;
349                    pp = statp->dnsrch;
350                    *pp++ = cp;
351                    for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
352                            if (*cp == ' ' || *cp == '\t') {
353                                    *cp = 0;
354                                    n = 1;
355                            } else if (n) {
356                                    *pp++ = cp;
357                                    n = 0;
358                            }
359                    }
360                    /* null terminate last domain if there are excess */
361                    while (*cp != '\0' && *cp != ' ' && *cp != '\t')
362                            cp++;
363                    *cp = '\0';
364                    *pp++ = 0;
365                    havesearch = 1;
366                    continue;
367                }
368                /* read nameservers to query */
369                if (MATCH(buf, "nameserver") && nserv < maxns) {
370                    struct addrinfo hints, *ai;
371                    char sbuf[NI_MAXSERV];
372                    const size_t minsiz =
373                        sizeof(statp->_u._ext.ext->nsaddrs[0]);
374
375                    cp = buf + sizeof("nameserver") - 1;
376                    while (*cp == ' ' || *cp == '\t')
377                        cp++;
378                    cp[strcspn(cp, ";# \t\n")] = '\0';
379                    if ((*cp != '\0') && (*cp != '\n')) {
380                        memset(&hints, 0, sizeof(hints));
381                        hints.ai_family = PF_UNSPEC;
382                        hints.ai_socktype = SOCK_DGRAM; /*dummy*/
383                        hints.ai_flags = AI_NUMERICHOST;
384                        sprintf(sbuf, "%u", NAMESERVER_PORT);
385                        if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
386                            ai->ai_addrlen <= minsiz) {
387                            if (statp->_u._ext.ext != NULL) {
388                                memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
389                                    ai->ai_addr, ai->ai_addrlen);
390                            }
391                            if (ai->ai_addrlen <=
392                                sizeof(statp->nsaddr_list[nserv])) {
393                                memcpy(&statp->nsaddr_list[nserv],
394                                    ai->ai_addr, ai->ai_addrlen);
395                            } else
396                                statp->nsaddr_list[nserv].sin_family = 0;
397                            freeaddrinfo(ai);
398                            nserv++;
399                        }
400                    }
401                    continue;
402                }
403#ifdef RESOLVSORT
404                if (MATCH(buf, "sortlist")) {
405                    struct in_addr a;
406                    struct in6_addr a6;
407                    int m, i;
408                    u_char *u;
409                    struct __res_state_ext *ext = statp->_u._ext.ext;
410
411                    cp = buf + sizeof("sortlist") - 1;
412                    while (nsort < MAXRESOLVSORT) {
413                        while (*cp == ' ' || *cp == '\t')
414                            cp++;
415                        if (*cp == '\0' || *cp == '\n' || *cp == ';')
416                            break;
417                        net = cp;
418                        while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
419                               isascii(*cp) && !isspace((unsigned char)*cp))
420                                cp++;
421                        n = *cp;
422                        *cp = 0;
423                        if (inet_aton(net, &a)) {
424                            statp->sort_list[nsort].addr = a;
425                            if (ISSORTMASK(n)) {
426                                *cp++ = n;
427                                net = cp;
428                                while (*cp && *cp != ';' &&
429                                        isascii(*cp) &&
430                                        !isspace((unsigned char)*cp))
431                                    cp++;
432                                n = *cp;
433                                *cp = 0;
434                                if (inet_aton(net, &a)) {
435                                    statp->sort_list[nsort].mask = a.s_addr;
436                                } else {
437                                    statp->sort_list[nsort].mask =
438                                        net_mask(statp->sort_list[nsort].addr);
439                                }
440                            } else {
441                                statp->sort_list[nsort].mask =
442                                    net_mask(statp->sort_list[nsort].addr);
443                            }
444                            ext->sort_list[nsort].af = AF_INET;
445                            ext->sort_list[nsort].addr.ina =
446                                statp->sort_list[nsort].addr;
447                            ext->sort_list[nsort].mask.ina.s_addr =
448                                statp->sort_list[nsort].mask;
449                            nsort++;
450                        }
451                        else if (inet_pton(AF_INET6, net, &a6) == 1) {
452
453                            ext->sort_list[nsort].af = AF_INET6;
454                            ext->sort_list[nsort].addr.in6a = a6;
455                            u = (u_char *)&ext->sort_list[nsort].mask.in6a;
456                            *cp++ = n;
457                            net = cp;
458                            while (*cp && *cp != ';' &&
459                                    isascii(*cp) && !isspace(*cp))
460                                cp++;
461                            m = n;
462                            n = *cp;
463                            *cp = 0;
464                            switch (m) {
465                            case '/':
466                                m = atoi(net);
467                                break;
468                            case '&':
469                                if (inet_pton(AF_INET6, net, u) == 1) {
470                                    m = -1;
471                                    break;
472                                }
473                                /*FALLTHROUGH*/
474                            default:
475                                m = sizeof(struct in6_addr) * CHAR_BIT;
476                                break;
477                            }
478                            if (m >= 0) {
479                                for (i = 0; i < sizeof(struct in6_addr); i++) {
480                                    if (m <= 0) {
481                                        *u = 0;
482                                    } else {
483                                        m -= CHAR_BIT;
484                                        *u = (u_char)~0;
485                                        if (m < 0)
486                                            *u <<= -m;
487                                    }
488                                    u++;
489                                }
490                            }
491                            statp->sort_list[nsort].addr.s_addr =
492                                (u_int32_t)0xffffffff;
493                            statp->sort_list[nsort].mask =
494                                (u_int32_t)0xffffffff;
495                            nsort++;
496                        }
497                        *cp = n;
498                    }
499                    continue;
500                }
501#endif
502                if (MATCH(buf, "options")) {
503                    res_setoptions(statp, buf + sizeof("options") - 1, "conf");
504                    continue;
505                }
506            }
507            if (nserv > 0)
508                statp->nscount = nserv;
509#ifdef RESOLVSORT
510            statp->nsort = nsort;
511#endif
512            (void) fclose(fp);
513        }
514/*
515 * Last chance to get a nameserver.  This should not normally
516 * be necessary
517 */
518#ifdef NO_RESOLV_CONF
519        if(nserv == 0)
520                nserv = get_nameservers(statp);
521#endif
522
523        if (statp->defdname[0] == 0 &&
524            gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
525            (cp = strchr(buf, '.')) != NULL)
526                strcpy(statp->defdname, cp + 1);
527
528        /* find components of local domain that might be searched */
529        if (havesearch == 0) {
530                pp = statp->dnsrch;
531                *pp++ = statp->defdname;
532                *pp = NULL;
533
534                dots = 0;
535                for (cp = statp->defdname; *cp; cp++)
536                        dots += (*cp == '.');
537
538                cp = statp->defdname;
539                while (pp < statp->dnsrch + MAXDFLSRCH) {
540                        if (dots < LOCALDOMAINPARTS)
541                                break;
542                        cp = strchr(cp, '.') + 1;    /*%< we know there is one */
543                        *pp++ = cp;
544                        dots--;
545                }
546                *pp = NULL;
547#ifdef DEBUG
548                if (statp->options & RES_DEBUG) {
549                        printf(";; res_init()... default dnsrch list:\n");
550                        for (pp = statp->dnsrch; *pp; pp++)
551                                printf(";;\t%s\n", *pp);
552                        printf(";;\t..END..\n");
553                }
554#endif
555        }
556
557        if (issetugid())
558                statp->options |= RES_NOALIASES;
559        else if ((cp = getenv("RES_OPTIONS")) != NULL)
560                res_setoptions(statp, cp, "env");
561        statp->options |= RES_INIT;
562        return (statp->res_h_errno);
563}
564
565static void
566res_setoptions(res_state statp, const char *options, const char *source)
567{
568        const char *cp = options;
569        int i;
570#ifndef _LIBC
571        struct __res_state_ext *ext = statp->_u._ext.ext;
572#endif
573
574#ifdef DEBUG
575        if (statp->options & RES_DEBUG)
576                printf(";; res_setoptions(\"%s\", \"%s\")...\n",
577                       options, source);
578#endif
579        while (*cp) {
580                /* skip leading and inner runs of spaces */
581                while (*cp == ' ' || *cp == '\t')
582                        cp++;
583                /* search for and process individual options */
584                if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
585                        i = atoi(cp + sizeof("ndots:") - 1);
586                        if (i <= RES_MAXNDOTS)
587                                statp->ndots = i;
588                        else
589                                statp->ndots = RES_MAXNDOTS;
590#ifdef DEBUG
591                        if (statp->options & RES_DEBUG)
592                                printf(";;\tndots=%d\n", statp->ndots);
593#endif
594                } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
595                        i = atoi(cp + sizeof("timeout:") - 1);
596                        if (i <= RES_MAXRETRANS)
597                                statp->retrans = i;
598                        else
599                                statp->retrans = RES_MAXRETRANS;
600#ifdef DEBUG
601                        if (statp->options & RES_DEBUG)
602                                printf(";;\ttimeout=%d\n", statp->retrans);
603#endif
604#ifdef  SOLARIS2
605                } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) {
606                        /*
607                         * For backward compatibility, 'retrans' is
608                         * supported as an alias for 'timeout', though
609                         * without an imposed maximum.
610                         */
611                        statp->retrans = atoi(cp + sizeof("retrans:") - 1);
612                } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){
613                        /*
614                         * For backward compatibility, 'retry' is
615                         * supported as an alias for 'attempts', though
616                         * without an imposed maximum.
617                         */
618                        statp->retry = atoi(cp + sizeof("retry:") - 1);
619#endif  /* SOLARIS2 */
620                } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
621                        i = atoi(cp + sizeof("attempts:") - 1);
622                        if (i <= RES_MAXRETRY)
623                                statp->retry = i;
624                        else
625                                statp->retry = RES_MAXRETRY;
626#ifdef DEBUG
627                        if (statp->options & RES_DEBUG)
628                                printf(";;\tattempts=%d\n", statp->retry);
629#endif
630                } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
631#ifdef DEBUG
632                        if (!(statp->options & RES_DEBUG)) {
633                                printf(";; res_setoptions(\"%s\", \"%s\")..\n",
634                                       options, source);
635                                statp->options |= RES_DEBUG;
636                        }
637                        printf(";;\tdebug\n");
638#endif
639                } else if (!strncmp(cp, "no_tld_query",
640                                    sizeof("no_tld_query") - 1) ||
641                           !strncmp(cp, "no-tld-query",
642                                    sizeof("no-tld-query") - 1)) {
643                        statp->options |= RES_NOTLDQUERY;
644                } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
645                        statp->options |= RES_USE_INET6;
646                } else if (!strncmp(cp, "insecure1", sizeof("insecure1") - 1)) {
647                       statp->options |= RES_INSECURE1;
648                } else if (!strncmp(cp, "insecure2", sizeof("insecure2") - 1)) {
649                       statp->options |= RES_INSECURE2;
650                } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
651                        statp->options |= RES_ROTATE;
652                } else if (!strncmp(cp, "no-check-names",
653                                    sizeof("no-check-names") - 1)) {
654                        statp->options |= RES_NOCHECKNAME;
655                }
656#ifdef RES_USE_EDNS0
657                else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
658                        statp->options |= RES_USE_EDNS0;
659                }
660#endif
661#ifndef _LIBC
662                else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
663                        statp->options |= RES_USE_DNAME;
664                }
665                else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
666                        if (ext == NULL)
667                                goto skip;
668                        cp += sizeof("nibble:") - 1;
669                        i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
670                        strncpy(ext->nsuffix, cp, i);
671                        ext->nsuffix[i] = '\0';
672                }
673                else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
674                        if (ext == NULL)
675                                goto skip;
676                        cp += sizeof("nibble2:") - 1;
677                        i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
678                        strncpy(ext->nsuffix2, cp, i);
679                        ext->nsuffix2[i] = '\0';
680                }
681                else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
682                        cp += sizeof("v6revmode:") - 1;
683                        /* "nibble" and "bitstring" used to be valid */
684                        if (!strncmp(cp, "single", sizeof("single") - 1)) {
685                                statp->options |= RES_NO_NIBBLE2;
686                        } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
687                                statp->options &=
688                                         ~RES_NO_NIBBLE2;
689                        }
690                }
691#endif
692                else {
693                        /* XXX - print a warning here? */
694                }
695#ifndef _LIBC
696   skip:
697#endif
698                /* skip to next run of spaces */
699                while (*cp && *cp != ' ' && *cp != '\t')
700                        cp++;
701        }
702}
703
704#ifdef RESOLVSORT
705/* XXX - should really support CIDR which means explicit masks always. */
706static u_int32_t
707net_mask(in)            /*!< XXX - should really use system's version of this  */
708        struct in_addr in;
709{
710        u_int32_t i = ntohl(in.s_addr);
711
712        if (IN_CLASSA(i))
713                return (htonl(IN_CLASSA_NET));
714        else if (IN_CLASSB(i))
715                return (htonl(IN_CLASSB_NET));
716        return (htonl(IN_CLASSC_NET));
717}
718#endif
719
720u_int
721res_randomid(void) {
722        struct timeval now;
723
724        gettimeofday(&now, NULL);
725        return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
726}
727
728/*%
729 * This routine is for closing the socket if a virtual circuit is used and
730 * the program wants to close it.  This provides support for endhostent()
731 * which expects to close the socket.
732 *
733 * This routine is not expected to be user visible.
734 */
735void
736res_nclose(res_state statp) {
737        int ns;
738
739        if (statp->_vcsock >= 0) {
740                (void) _close(statp->_vcsock);
741                statp->_vcsock = -1;
742                statp->_flags &= ~(RES_F_VC | RES_F_CONN);
743        }
744        for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
745                if (statp->_u._ext.nssocks[ns] != -1) {
746                        (void) _close(statp->_u._ext.nssocks[ns]);
747                        statp->_u._ext.nssocks[ns] = -1;
748                }
749        }
750}
751
752void
753res_ndestroy(res_state statp) {
754        res_nclose(statp);
755        if (statp->_u._ext.ext != NULL)
756                free(statp->_u._ext.ext);
757        statp->options &= ~RES_INIT;
758        statp->_u._ext.ext = NULL;
759}
760
761#ifndef _LIBC
762const char *
763res_get_nibblesuffix(res_state statp) {
764        if (statp->_u._ext.ext)
765                return (statp->_u._ext.ext->nsuffix);
766        return ("ip6.arpa");
767}
768
769const char *
770res_get_nibblesuffix2(res_state statp) {
771        if (statp->_u._ext.ext)
772                return (statp->_u._ext.ext->nsuffix2);
773        return ("ip6.int");
774}
775#endif
776
777void
778res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) {
779        int i, nserv;
780        size_t size;
781
782        /* close open servers */
783        res_nclose(statp);
784
785        /* cause rtt times to be forgotten */
786        statp->_u._ext.nscount = 0;
787
788        nserv = 0;
789        for (i = 0; i < cnt && nserv < MAXNS; i++) {
790                switch (set->sin.sin_family) {
791                case AF_INET:
792                        size = sizeof(set->sin);
793                        if (statp->_u._ext.ext)
794                                memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
795                                        &set->sin, size);
796                        if (size <= sizeof(statp->nsaddr_list[nserv]))
797                                memcpy(&statp->nsaddr_list[nserv],
798                                        &set->sin, size);
799                        else
800                                statp->nsaddr_list[nserv].sin_family = 0;
801                        nserv++;
802                        break;
803
804#ifdef HAS_INET6_STRUCTS
805                case AF_INET6:
806                        size = sizeof(set->sin6);
807                        if (statp->_u._ext.ext)
808                                memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
809                                        &set->sin6, size);
810                        if (size <= sizeof(statp->nsaddr_list[nserv]))
811                                memcpy(&statp->nsaddr_list[nserv],
812                                        &set->sin6, size);
813                        else
814                                statp->nsaddr_list[nserv].sin_family = 0;
815                        nserv++;
816                        break;
817#endif
818
819                default:
820                        break;
821                }
822                set++;
823        }
824        statp->nscount = nserv;
825       
826}
827
828int
829res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) {
830        int i;
831        size_t size;
832        u_int16_t family;
833
834        for (i = 0; i < statp->nscount && i < cnt; i++) {
835                if (statp->_u._ext.ext)
836                        family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
837                else
838                        family = statp->nsaddr_list[i].sin_family;
839
840                switch (family) {
841                case AF_INET:
842                        size = sizeof(set->sin);
843                        if (statp->_u._ext.ext)
844                                memcpy(&set->sin,
845                                       &statp->_u._ext.ext->nsaddrs[i],
846                                       size);
847                        else
848                                memcpy(&set->sin, &statp->nsaddr_list[i],
849                                       size);
850                        break;
851
852#ifdef HAS_INET6_STRUCTS
853                case AF_INET6:
854                        size = sizeof(set->sin6);
855                        if (statp->_u._ext.ext)
856                                memcpy(&set->sin6,
857                                       &statp->_u._ext.ext->nsaddrs[i],
858                                       size);
859                        else
860                                memcpy(&set->sin6, &statp->nsaddr_list[i],
861                                       size);
862                        break;
863#endif
864
865                default:
866                        set->sin.sin_family = 0;
867                        break;
868                }
869                set++;
870        }
871        return (statp->nscount);
872}
873
874/*! \file */
Note: See TracBrowser for help on using the repository browser.