source: rtems-libbsd/freebsd/lib/libc/net/gethostbydns.c @ 3d1e767

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 3d1e767 was 3d1e767, checked in by Sebastian Huber <sebastian.huber@…>, on 04/27/16 at 08:25:22

Directly use <sys/types.h> provided by Newlib

  • Property mode set to 100644
File size: 19.1 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*
4 * ++Copyright++ 1985, 1988, 1993
5 * -
6 * Copyright (c) 1985, 1988, 1993
7 *      The Regents of the University of California.  All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
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 * 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 * --Copyright--
52 */
53
54#if defined(LIBC_SCCS) && !defined(lint)
55static char sccsid[] = "@(#)gethostnamadr.c     8.1 (Berkeley) 6/4/93";
56static char fromrcsid[] = "From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vixie Exp $";
57#endif /* LIBC_SCCS and not lint */
58#include <sys/cdefs.h>
59__FBSDID("$FreeBSD$");
60
61#include <sys/types.h>
62#include <rtems/bsd/sys/param.h>
63#include <sys/socket.h>
64#include <netinet/in.h>
65#include <arpa/inet.h>
66#include <arpa/nameser.h>
67
68#include <stdio.h>
69#include <stdlib.h>
70#include <unistd.h>
71#include <string.h>
72#include <netdb.h>
73#include <resolv.h>
74#include <ctype.h>
75#include <errno.h>
76#include <syslog.h>
77#include <stdarg.h>
78#include <nsswitch.h>
79
80#include "netdb_private.h"
81#include "res_config.h"
82
83#define SPRINTF(x) ((size_t)sprintf x)
84
85static const char AskedForGot[] =
86                "gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
87
88#ifdef RESOLVSORT
89static void addrsort(char **, int, res_state);
90#endif
91
92#ifdef DEBUG
93static void dprintf(char *, int, res_state) __printflike(1, 0);
94#endif
95
96#define MAXPACKET       (64*1024)
97
98typedef union {
99    HEADER hdr;
100    u_char buf[MAXPACKET];
101} querybuf;
102
103typedef union {
104    int32_t al;
105    char ac;
106} align;
107
108int _dns_ttl_;
109
110#ifdef DEBUG
111static void
112dprintf(msg, num, res)
113        char *msg;
114        int num;
115        res_state res;
116{
117        if (res->options & RES_DEBUG) {
118                int save = errno;
119
120                printf(msg, num);
121                errno = save;
122        }
123}
124#else
125# define dprintf(msg, num, res) /*nada*/
126#endif
127
128#define BOUNDED_INCR(x) \
129        do { \
130                cp += x; \
131                if (cp > eom) { \
132                        RES_SET_H_ERRNO(statp, NO_RECOVERY); \
133                        return (-1); \
134                } \
135        } while (0)
136
137#define BOUNDS_CHECK(ptr, count) \
138        do { \
139                if ((ptr) + (count) > eom) { \
140                        RES_SET_H_ERRNO(statp, NO_RECOVERY); \
141                        return (-1); \
142                } \
143        } while (0)
144
145static int
146gethostanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
147    struct hostent *he, struct hostent_data *hed, res_state statp)
148{
149        const HEADER *hp;
150        const u_char *cp;
151        int n;
152        const u_char *eom, *erdata;
153        char *bp, *ep, **ap, **hap;
154        int type, class, ancount, qdcount;
155        int haveanswer, had_error;
156        int toobig = 0;
157        char tbuf[MAXDNAME];
158        const char *tname;
159        int (*name_ok)(const char *);
160
161        tname = qname;
162        he->h_name = NULL;
163        eom = answer->buf + anslen;
164        switch (qtype) {
165        case T_A:
166        case T_AAAA:
167                name_ok = res_hnok;
168                break;
169        case T_PTR:
170                name_ok = res_dnok;
171                break;
172        default:
173                RES_SET_H_ERRNO(statp, NO_RECOVERY);
174                return (-1);    /* XXX should be abort(); */
175        }
176        /*
177         * find first satisfactory answer
178         */
179        hp = &answer->hdr;
180        ancount = ntohs(hp->ancount);
181        qdcount = ntohs(hp->qdcount);
182        bp = hed->hostbuf;
183        ep = hed->hostbuf + sizeof hed->hostbuf;
184        cp = answer->buf;
185        BOUNDED_INCR(HFIXEDSZ);
186        if (qdcount != 1) {
187                RES_SET_H_ERRNO(statp, NO_RECOVERY);
188                return (-1);
189        }
190        n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
191        if ((n < 0) || !(*name_ok)(bp)) {
192                RES_SET_H_ERRNO(statp, NO_RECOVERY);
193                return (-1);
194        }
195        BOUNDED_INCR(n + QFIXEDSZ);
196        if (qtype == T_A || qtype == T_AAAA) {
197                /* res_send() has already verified that the query name is the
198                 * same as the one we sent; this just gets the expanded name
199                 * (i.e., with the succeeding search-domain tacked on).
200                 */
201                n = strlen(bp) + 1;             /* for the \0 */
202                if (n >= MAXHOSTNAMELEN) {
203                        RES_SET_H_ERRNO(statp, NO_RECOVERY);
204                        return (-1);
205                }
206                he->h_name = bp;
207                bp += n;
208                /* The qname can be abbreviated, but h_name is now absolute. */
209                qname = he->h_name;
210        }
211        ap = hed->host_aliases;
212        *ap = NULL;
213        he->h_aliases = hed->host_aliases;
214        hap = hed->h_addr_ptrs;
215        *hap = NULL;
216        he->h_addr_list = hed->h_addr_ptrs;
217        haveanswer = 0;
218        had_error = 0;
219        _dns_ttl_ = -1;
220        while (ancount-- > 0 && cp < eom && !had_error) {
221                n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
222                if ((n < 0) || !(*name_ok)(bp)) {
223                        had_error++;
224                        continue;
225                }
226                cp += n;                        /* name */
227                BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
228                type = _getshort(cp);
229                cp += INT16SZ;                  /* type */
230                class = _getshort(cp);
231                cp += INT16SZ;                  /* class */
232                if (qtype == T_A  && type == T_A)
233                        _dns_ttl_ = _getlong(cp);
234                cp += INT32SZ;                  /* TTL */
235                n = _getshort(cp);
236                cp += INT16SZ;                  /* len */
237                BOUNDS_CHECK(cp, n);
238                erdata = cp + n;
239                if (class != C_IN) {
240                        /* XXX - debug? syslog? */
241                        cp += n;
242                        continue;               /* XXX - had_error++ ? */
243                }
244                if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
245                        if (ap >= &hed->host_aliases[_MAXALIASES-1])
246                                continue;
247                        n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
248                        if ((n < 0) || !(*name_ok)(tbuf)) {
249                                had_error++;
250                                continue;
251                        }
252                        cp += n;
253                        if (cp != erdata) {
254                                RES_SET_H_ERRNO(statp, NO_RECOVERY);
255                                return (-1);
256                        }
257                        /* Store alias. */
258                        *ap++ = bp;
259                        n = strlen(bp) + 1;     /* for the \0 */
260                        if (n >= MAXHOSTNAMELEN) {
261                                had_error++;
262                                continue;
263                        }
264                        bp += n;
265                        /* Get canonical name. */
266                        n = strlen(tbuf) + 1;   /* for the \0 */
267                        if (n > ep - bp || n >= MAXHOSTNAMELEN) {
268                                had_error++;
269                                continue;
270                        }
271                        strcpy(bp, tbuf);
272                        he->h_name = bp;
273                        bp += n;
274                        continue;
275                }
276                if (qtype == T_PTR && type == T_CNAME) {
277                        n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
278                        if (n < 0 || !res_dnok(tbuf)) {
279                                had_error++;
280                                continue;
281                        }
282                        cp += n;
283                        if (cp != erdata) {
284                                RES_SET_H_ERRNO(statp, NO_RECOVERY);
285                                return (-1);
286                        }
287                        /* Get canonical name. */
288                        n = strlen(tbuf) + 1;   /* for the \0 */
289                        if (n > ep - bp || n >= MAXHOSTNAMELEN) {
290                                had_error++;
291                                continue;
292                        }
293                        strcpy(bp, tbuf);
294                        tname = bp;
295                        bp += n;
296                        continue;
297                }
298                if (type != qtype) {
299                        if (type != T_SIG && type != ns_t_dname)
300                                syslog(LOG_NOTICE|LOG_AUTH,
301        "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
302                                       qname, p_class(C_IN), p_type(qtype),
303                                       p_type(type));
304                        cp += n;
305                        continue;               /* XXX - had_error++ ? */
306                }
307                switch (type) {
308                case T_PTR:
309                        if (strcasecmp(tname, bp) != 0) {
310                                syslog(LOG_NOTICE|LOG_AUTH,
311                                       AskedForGot, qname, bp);
312                                cp += n;
313                                continue;       /* XXX - had_error++ ? */
314                        }
315                        n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
316                        if ((n < 0) || !res_hnok(bp)) {
317                                had_error++;
318                                break;
319                        }
320#if MULTI_PTRS_ARE_ALIASES
321                        cp += n;
322                        if (cp != erdata) {
323                                RES_SET_H_ERRNO(statp, NO_RECOVERY);
324                                return (-1);
325                        }
326                        if (!haveanswer)
327                                he->h_name = bp;
328                        else if (ap < &hed->host_aliases[_MAXALIASES-1])
329                                *ap++ = bp;
330                        else
331                                n = -1;
332                        if (n != -1) {
333                                n = strlen(bp) + 1;     /* for the \0 */
334                                if (n >= MAXHOSTNAMELEN) {
335                                        had_error++;
336                                        break;
337                                }
338                                bp += n;
339                        }
340                        break;
341#else
342                        he->h_name = bp;
343                        if (statp->options & RES_USE_INET6) {
344                                n = strlen(bp) + 1;     /* for the \0 */
345                                if (n >= MAXHOSTNAMELEN) {
346                                        had_error++;
347                                        break;
348                                }
349                                bp += n;
350                                _map_v4v6_hostent(he, &bp, ep);
351                        }
352                        RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
353                        return (0);
354#endif
355                case T_A:
356                case T_AAAA:
357                        if (strcasecmp(he->h_name, bp) != 0) {
358                                syslog(LOG_NOTICE|LOG_AUTH,
359                                       AskedForGot, he->h_name, bp);
360                                cp += n;
361                                continue;       /* XXX - had_error++ ? */
362                        }
363                        if (n != he->h_length) {
364                                cp += n;
365                                continue;
366                        }
367                        if (!haveanswer) {
368                                int nn;
369
370                                he->h_name = bp;
371                                nn = strlen(bp) + 1;    /* for the \0 */
372                                bp += nn;
373                        }
374
375                        bp += sizeof(align) - ((u_long)bp % sizeof(align));
376
377                        if (bp + n >= ep) {
378                                dprintf("size (%d) too big\n", n, statp);
379                                had_error++;
380                                continue;
381                        }
382                        if (hap >= &hed->h_addr_ptrs[_MAXADDRS-1]) {
383                                if (!toobig++)
384                                        dprintf("Too many addresses (%d)\n",
385                                                _MAXADDRS, statp);
386                                cp += n;
387                                continue;
388                        }
389                        memcpy(*hap++ = bp, cp, n);
390                        bp += n;
391                        cp += n;
392                        if (cp != erdata) {
393                                RES_SET_H_ERRNO(statp, NO_RECOVERY);
394                                return (-1);
395                        }
396                        break;
397                default:
398                        dprintf("Impossible condition (type=%d)\n", type,
399                            statp);
400                        RES_SET_H_ERRNO(statp, NO_RECOVERY);
401                        return (-1);
402                        /* BIND has abort() here, too risky on bad data */
403                }
404                if (!had_error)
405                        haveanswer++;
406        }
407        if (haveanswer) {
408                *ap = NULL;
409                *hap = NULL;
410# if defined(RESOLVSORT)
411                /*
412                 * Note: we sort even if host can take only one address
413                 * in its return structures - should give it the "best"
414                 * address in that case, not some random one
415                 */
416                if (statp->nsort && haveanswer > 1 && qtype == T_A)
417                        addrsort(hed->h_addr_ptrs, haveanswer, statp);
418# endif /*RESOLVSORT*/
419                if (!he->h_name) {
420                        n = strlen(qname) + 1;  /* for the \0 */
421                        if (n > ep - bp || n >= MAXHOSTNAMELEN)
422                                goto no_recovery;
423                        strcpy(bp, qname);
424                        he->h_name = bp;
425                        bp += n;
426                }
427                if (statp->options & RES_USE_INET6)
428                        _map_v4v6_hostent(he, &bp, ep);
429                RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
430                return (0);
431        }
432 no_recovery:
433        RES_SET_H_ERRNO(statp, NO_RECOVERY);
434        return (-1);
435}
436
437/* XXX: for async DNS resolver in ypserv */
438struct hostent *
439__dns_getanswer(const char *answer, int anslen, const char *qname, int qtype)
440{
441        struct hostent *he;
442        struct hostent_data *hed;
443        int error;
444        res_state statp;
445
446        statp = __res_state();
447        if ((he = __hostent_init()) == NULL ||
448            (hed = __hostent_data_init()) == NULL) {
449                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
450                return (NULL);
451        }
452        switch (qtype) {
453        case T_AAAA:
454                he->h_addrtype = AF_INET6;
455                he->h_length = NS_IN6ADDRSZ;
456                break;
457        case T_A:
458        default:
459                he->h_addrtype = AF_INET;
460                he->h_length = NS_INADDRSZ;
461                break;
462        }
463
464        error = gethostanswer((const querybuf *)answer, anslen, qname, qtype,
465            he, hed, statp);
466        return (error == 0) ? he : NULL;
467}
468
469int
470_dns_gethostbyname(void *rval, void *cb_data, va_list ap)
471{
472        const char *name;
473        int af;
474        char *buffer;
475        size_t buflen;
476        int *errnop, *h_errnop;
477        struct hostent *hptr, he;
478        struct hostent_data *hed;
479        querybuf *buf;
480        int n, type, error;
481        res_state statp;
482
483        name = va_arg(ap, const char *);
484        af = va_arg(ap, int);
485        hptr = va_arg(ap, struct hostent *);
486        buffer = va_arg(ap, char *);
487        buflen = va_arg(ap, size_t);
488        errnop = va_arg(ap, int *);
489        h_errnop = va_arg(ap, int *);
490
491        *((struct hostent **)rval) = NULL;
492
493        statp = __res_state();
494        if ((hed = __hostent_data_init()) == NULL) {
495                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
496                *h_errnop = statp->res_h_errno;
497                return (NS_NOTFOUND);
498        }
499
500        he.h_addrtype = af;
501        switch (af) {
502        case AF_INET:
503                he.h_length = NS_INADDRSZ;
504                type = T_A;
505                break;
506        case AF_INET6:
507                he.h_length = NS_IN6ADDRSZ;
508                type = T_AAAA;
509                break;
510        default:
511                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
512                *h_errnop = statp->res_h_errno;
513                errno = EAFNOSUPPORT;
514                return (NS_UNAVAIL);
515        }
516
517        if ((buf = malloc(sizeof(*buf))) == NULL) {
518                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
519                *h_errnop = statp->res_h_errno;
520                return (NS_NOTFOUND);
521        }
522        n = res_nsearch(statp, name, C_IN, type, buf->buf, sizeof(buf->buf));
523        if (n < 0) {
524                free(buf);
525                dprintf("res_nsearch failed (%d)\n", n, statp);
526                *h_errnop = statp->res_h_errno;
527                return (NS_NOTFOUND);
528        } else if (n > sizeof(buf->buf)) {
529                free(buf);
530                dprintf("static buffer is too small (%d)\n", n, statp);
531                *h_errnop = statp->res_h_errno;
532                return (NS_UNAVAIL);
533        }
534        error = gethostanswer(buf, n, name, type, &he, hed, statp);
535        free(buf);
536        if (error != 0) {
537                *h_errnop = statp->res_h_errno;
538                switch (statp->res_h_errno) {
539                case HOST_NOT_FOUND:
540                        return (NS_NOTFOUND);
541                case TRY_AGAIN:
542                        return (NS_TRYAGAIN);
543                default:
544                        return (NS_UNAVAIL);
545                }
546                /*NOTREACHED*/
547        }
548        if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
549                *errnop = errno;
550                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
551                *h_errnop = statp->res_h_errno;
552                return (NS_RETURN);
553        }
554        RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
555        *((struct hostent **)rval) = hptr;
556        return (NS_SUCCESS);
557}
558
559int
560_dns_gethostbyaddr(void *rval, void *cb_data, va_list ap)
561{
562        const void *addr;
563        socklen_t len;
564        int af;
565        char *buffer;
566        size_t buflen;
567        int *errnop, *h_errnop;
568        const u_char *uaddr;
569        struct hostent *hptr, he;
570        struct hostent_data *hed;
571        int n;
572        querybuf *buf;
573        char qbuf[MAXDNAME+1], *qp;
574        res_state statp;
575#ifdef SUNSECURITY
576        struct hostdata rhd;
577        struct hostent *rhe;
578        char **haddr;
579        u_long old_options;
580        char hname2[MAXDNAME+1], numaddr[46];
581        int ret_h_error;
582#endif /*SUNSECURITY*/
583
584        addr = va_arg(ap, const void *);
585        len = va_arg(ap, socklen_t);
586        af = va_arg(ap, int);
587        hptr = va_arg(ap, struct hostent *);
588        buffer = va_arg(ap, char *);
589        buflen = va_arg(ap, size_t);
590        errnop = va_arg(ap, int *);
591        h_errnop = va_arg(ap, int *);
592        uaddr = (const u_char *)addr;
593
594        *((struct hostent **)rval) = NULL;
595
596        statp = __res_state();
597        if ((hed = __hostent_data_init()) == NULL) {
598                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
599                *h_errnop = statp->res_h_errno;
600                return (NS_NOTFOUND);
601        }
602
603        switch (af) {
604        case AF_INET:
605                (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
606                               (uaddr[3] & 0xff),
607                               (uaddr[2] & 0xff),
608                               (uaddr[1] & 0xff),
609                               (uaddr[0] & 0xff));
610                break;
611        case AF_INET6:
612                qp = qbuf;
613                for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
614                        qp += SPRINTF((qp, "%x.%x.",
615                                       uaddr[n] & 0xf,
616                                       (uaddr[n] >> 4) & 0xf));
617                }
618                strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
619                break;
620        default:
621                abort();
622        }
623        if ((buf = malloc(sizeof(*buf))) == NULL) {
624                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
625                *h_errnop = statp->res_h_errno;
626                return NS_NOTFOUND;
627        }
628        n = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf->buf,
629            sizeof buf->buf);
630        if (n < 0) {
631                free(buf);
632                dprintf("res_nquery failed (%d)\n", n, statp);
633                *h_errnop = statp->res_h_errno;
634                return (NS_UNAVAIL);
635        }
636        if (n > sizeof buf->buf) {
637                free(buf);
638                dprintf("static buffer is too small (%d)\n", n, statp);
639                *h_errnop = statp->res_h_errno;
640                return (NS_UNAVAIL);
641        }
642        if (gethostanswer(buf, n, qbuf, T_PTR, &he, hed, statp) != 0) {
643                free(buf);
644                *h_errnop = statp->res_h_errno;
645                switch (statp->res_h_errno) {
646                case HOST_NOT_FOUND:
647                        return (NS_NOTFOUND);
648                case TRY_AGAIN:
649                        return (NS_TRYAGAIN);
650                default:
651                        return (NS_UNAVAIL);
652                }
653                /*NOTREACHED*/
654        }
655        free(buf);
656#ifdef SUNSECURITY
657        if (af == AF_INET) {
658            /*
659             * turn off search as the name should be absolute,
660             * 'localhost' should be matched by defnames
661             */
662            strncpy(hname2, he.h_name, MAXDNAME);
663            hname2[MAXDNAME] = '\0';
664            old_options = statp->options;
665            statp->options &= ~RES_DNSRCH;
666            statp->options |= RES_DEFNAMES;
667            memset(&rhd, 0, sizeof rhd);
668            rhe = gethostbyname_r(hname2, &rhd.host, &rhd.data,
669                sizeof(rhd.data), &ret_h_error);
670            if (rhe == NULL) {
671                if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
672                    strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
673                syslog(LOG_NOTICE|LOG_AUTH,
674                       "gethostbyaddr: No A record for %s (verifying [%s])",
675                       hname2, numaddr);
676                statp->options = old_options;
677                RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
678                *h_errnop = statp->res_h_errno;
679                return (NS_NOTFOUND);
680            }
681            statp->options = old_options;
682            for (haddr = rhe->h_addr_list; *haddr; haddr++)
683                if (!memcmp(*haddr, addr, NS_INADDRSZ))
684                        break;
685            if (!*haddr) {
686                if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
687                    strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
688                syslog(LOG_NOTICE|LOG_AUTH,
689                       "gethostbyaddr: A record of %s != PTR record [%s]",
690                       hname2, numaddr);
691                RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
692                *h_errnop = statp->res_h_errno;
693                return (NS_NOTFOUND);
694            }
695        }
696#endif /*SUNSECURITY*/
697        he.h_addrtype = af;
698        he.h_length = len;
699        memcpy(hed->host_addr, uaddr, len);
700        hed->h_addr_ptrs[0] = (char *)hed->host_addr;
701        hed->h_addr_ptrs[1] = NULL;
702        if (af == AF_INET && (statp->options & RES_USE_INET6)) {
703                _map_v4v6_address((char*)hed->host_addr, (char*)hed->host_addr);
704                he.h_addrtype = AF_INET6;
705                he.h_length = NS_IN6ADDRSZ;
706        }
707        if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
708                *errnop = errno;
709                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
710                *h_errnop = statp->res_h_errno;
711                return (NS_RETURN);
712        }
713        RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
714        *((struct hostent **)rval) = hptr;
715        return (NS_SUCCESS);
716}
717
718#ifdef RESOLVSORT
719static void
720addrsort(char **ap, int num, res_state res)
721{
722        int i, j;
723        char **p;
724        short aval[_MAXADDRS];
725        int needsort = 0;
726
727        p = ap;
728        for (i = 0; i < num; i++, p++) {
729            for (j = 0 ; (unsigned)j < res->nsort; j++)
730                if (res->sort_list[j].addr.s_addr ==
731                    (((struct in_addr *)(*p))->s_addr & res->sort_list[j].mask))
732                        break;
733            aval[i] = j;
734            if (needsort == 0 && i > 0 && j < aval[i-1])
735                needsort = i;
736        }
737        if (!needsort)
738            return;
739
740        while (needsort < num) {
741            for (j = needsort - 1; j >= 0; j--) {
742                if (aval[j] > aval[j+1]) {
743                    char *hp;
744
745                    i = aval[j];
746                    aval[j] = aval[j+1];
747                    aval[j+1] = i;
748
749                    hp = ap[j];
750                    ap[j] = ap[j+1];
751                    ap[j+1] = hp;
752
753                } else
754                    break;
755            }
756            needsort++;
757        }
758}
759#endif
760
761void
762_sethostdnsent(int stayopen)
763{
764        res_state statp;
765
766        statp = __res_state();
767        if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
768                return;
769        if (stayopen)
770                statp->options |= RES_STAYOPEN | RES_USEVC;
771}
772
773void
774_endhostdnsent()
775{
776        res_state statp;
777
778        statp = __res_state();
779        statp->options &= ~(RES_STAYOPEN | RES_USEVC);
780        res_nclose(statp);
781}
Note: See TracBrowser for help on using the repository browser.