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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 2404264 was 2404264, checked in by Sebastian Huber <sebastian.huber@…>, on 10/30/13 at 12:23:59

Revert superfluous changes

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