Back to index

glibc  2.9
gethnamaddr.c
Go to the documentation of this file.
00001 /*
00002  * ++Copyright++ 1985, 1988, 1993
00003  * -
00004  * Copyright (c) 1985, 1988, 1993
00005  *    The Regents of the University of California.  All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  * 4. Neither the name of the University nor the names of its contributors
00016  *    may be used to endorse or promote products derived from this software
00017  *    without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00020  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00023  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00025  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00026  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00027  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00028  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00029  * SUCH DAMAGE.
00030  * -
00031  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
00032  *
00033  * Permission to use, copy, modify, and distribute this software for any
00034  * purpose with or without fee is hereby granted, provided that the above
00035  * copyright notice and this permission notice appear in all copies, and that
00036  * the name of Digital Equipment Corporation not be used in advertising or
00037  * publicity pertaining to distribution of the document or software without
00038  * specific, written prior permission.
00039  *
00040  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
00041  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
00042  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
00043  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00044  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00045  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00046  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00047  * SOFTWARE.
00048  * -
00049  * --Copyright--
00050  */
00051 
00052 #if defined(LIBC_SCCS) && !defined(lint)
00053 static char sccsid[] = "@(#)gethostnamadr.c      8.1 (Berkeley) 6/4/93";
00054 #endif /* LIBC_SCCS and not lint */
00055 
00056 #include <sys/types.h>
00057 #include <sys/param.h>
00058 #include <sys/socket.h>
00059 #include <netinet/in.h>
00060 #include <arpa/inet.h>
00061 #include <arpa/nameser.h>
00062 
00063 #include <stdio.h>
00064 #include <netdb.h>
00065 #include <resolv.h>
00066 #include <ctype.h>
00067 #include <errno.h>
00068 #include <syslog.h>
00069 
00070 #define RESOLVSORT
00071 
00072 #ifndef LOG_AUTH
00073 # define LOG_AUTH 0
00074 #endif
00075 
00076 #define MULTI_PTRS_ARE_ALIASES 1   /* XXX - experimental */
00077 
00078 #if defined(BSD) && (BSD >= 199103) && defined(AF_INET6)
00079 # include <stdlib.h>
00080 # include <string.h>
00081 #else
00082 # include "../conf/portability.h"
00083 #endif
00084 
00085 #if defined(USE_OPTIONS_H)
00086 # include <../conf/options.h>
00087 #endif
00088 
00089 #ifdef SPRINTF_CHAR
00090 # define SPRINTF(x) strlen(sprintfx)
00091 #else
00092 # define SPRINTF(x) ((size_t)sprintf x)
00093 #endif
00094 
00095 #define       MAXALIASES    35
00096 #define       MAXADDRS      35
00097 
00098 static const char AskedForGot[] =
00099                        "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
00100 
00101 static char *h_addr_ptrs[MAXADDRS + 1];
00102 
00103 static struct hostent host;
00104 static char *host_aliases[MAXALIASES];
00105 static char hostbuf[8*1024];
00106 static u_char host_addr[16];       /* IPv4 or IPv6 */
00107 static FILE *hostf = NULL;
00108 static int stayopen = 0;
00109 
00110 static void map_v4v6_address (const char *src, char *dst) __THROW;
00111 static void map_v4v6_hostent (struct hostent *hp, char **bp, int *len) __THROW;
00112 
00113 #ifdef RESOLVSORT
00114 extern void addrsort (char **, int) __THROW;
00115 #endif
00116 
00117 #if PACKETSZ > 65536
00118 #define       MAXPACKET     PACKETSZ
00119 #else
00120 #define       MAXPACKET     65536
00121 #endif
00122 
00123 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length.  */
00124 #ifdef MAXHOSTNAMELEN
00125 # undef MAXHOSTNAMELEN
00126 #endif
00127 #define MAXHOSTNAMELEN 256
00128 
00129 typedef union {
00130     HEADER hdr;
00131     u_char buf[MAXPACKET];
00132 } querybuf;
00133 
00134 typedef union {
00135     int32_t al;
00136     char ac;
00137 } align;
00138 
00139 #ifndef h_errno
00140 extern int h_errno;
00141 #endif
00142 
00143 #ifdef DEBUG
00144 static void
00145 Dprintf(msg, num)
00146        char *msg;
00147        int num;
00148 {
00149        if (_res.options & RES_DEBUG) {
00150               int save = errno;
00151 
00152               printf(msg, num);
00153               __set_errno (save);
00154        }
00155 }
00156 #else
00157 # define Dprintf(msg, num) /*nada*/
00158 #endif
00159 
00160 #define BOUNDED_INCR(x) \
00161        do { \
00162               cp += x; \
00163               if (cp > eom) { \
00164                      __set_h_errno (NO_RECOVERY); \
00165                      return (NULL); \
00166               } \
00167        } while (0)
00168 
00169 #define BOUNDS_CHECK(ptr, count) \
00170        do { \
00171               if ((ptr) + (count) > eom) { \
00172                      __set_h_errno (NO_RECOVERY); \
00173                      return (NULL); \
00174               } \
00175        } while (0)
00176 
00177 
00178 static struct hostent *
00179 getanswer (const querybuf *answer, int anslen, const char *qname, int qtype)
00180 {
00181        register const HEADER *hp;
00182        register const u_char *cp;
00183        register int n;
00184        const u_char *eom, *erdata;
00185        char *bp, **ap, **hap;
00186        int type, class, buflen, ancount, qdcount;
00187        int haveanswer, had_error;
00188        int toobig = 0;
00189        char tbuf[MAXDNAME];
00190        const char *tname;
00191        int (*name_ok) (const char *);
00192 
00193        tname = qname;
00194        host.h_name = NULL;
00195        eom = answer->buf + anslen;
00196        switch (qtype) {
00197        case T_A:
00198        case T_AAAA:
00199               name_ok = res_hnok;
00200               break;
00201        case T_PTR:
00202               name_ok = res_dnok;
00203               break;
00204        default:
00205               return (NULL);       /* XXX should be abort(); */
00206        }
00207        /*
00208         * find first satisfactory answer
00209         */
00210        hp = &answer->hdr;
00211        ancount = ntohs(hp->ancount);
00212        qdcount = ntohs(hp->qdcount);
00213        bp = hostbuf;
00214        buflen = sizeof hostbuf;
00215        cp = answer->buf;
00216        BOUNDED_INCR(HFIXEDSZ);
00217        if (qdcount != 1) {
00218               __set_h_errno (NO_RECOVERY);
00219               return (NULL);
00220        }
00221        n = dn_expand(answer->buf, eom, cp, bp, buflen);
00222        if ((n < 0) || !(*name_ok)(bp)) {
00223               __set_h_errno (NO_RECOVERY);
00224               return (NULL);
00225        }
00226        BOUNDED_INCR(n + QFIXEDSZ);
00227        if (qtype == T_A || qtype == T_AAAA) {
00228               /* res_send() has already verified that the query name is the
00229                * same as the one we sent; this just gets the expanded name
00230                * (i.e., with the succeeding search-domain tacked on).
00231                */
00232               n = strlen(bp) + 1;         /* for the \0 */
00233               if (n >= MAXHOSTNAMELEN) {
00234                      __set_h_errno (NO_RECOVERY);
00235                      return (NULL);
00236               }
00237               host.h_name = bp;
00238               bp += n;
00239               buflen -= n;
00240               /* The qname can be abbreviated, but h_name is now absolute. */
00241               qname = host.h_name;
00242        }
00243        ap = host_aliases;
00244        *ap = NULL;
00245        host.h_aliases = host_aliases;
00246        hap = h_addr_ptrs;
00247        *hap = NULL;
00248        host.h_addr_list = h_addr_ptrs;
00249        haveanswer = 0;
00250        had_error = 0;
00251        while (ancount-- > 0 && cp < eom && !had_error) {
00252               n = dn_expand(answer->buf, eom, cp, bp, buflen);
00253               if ((n < 0) || !(*name_ok)(bp)) {
00254                      had_error++;
00255                      continue;
00256               }
00257               cp += n;                    /* name */
00258               BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
00259               type = ns_get16(cp);
00260               cp += INT16SZ;                     /* type */
00261               class = ns_get16(cp);
00262               cp += INT16SZ + INT32SZ;    /* class, TTL */
00263               n = ns_get16(cp);
00264               cp += INT16SZ;                     /* len */
00265               BOUNDS_CHECK(cp, n);
00266               erdata = cp + n;
00267               if (class != C_IN) {
00268                      /* XXX - debug? syslog? */
00269                      cp += n;
00270                      continue;            /* XXX - had_error++ ? */
00271               }
00272               if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
00273                      if (ap >= &host_aliases[MAXALIASES-1])
00274                             continue;
00275                      n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
00276                      if ((n < 0) || !(*name_ok)(tbuf)) {
00277                             had_error++;
00278                             continue;
00279                      }
00280                      cp += n;
00281                      if (cp != erdata) {
00282                             __set_h_errno (NO_RECOVERY);
00283                             return (NULL);
00284                      }
00285                      /* Store alias. */
00286                      *ap++ = bp;
00287                      n = strlen(bp) + 1;  /* for the \0 */
00288                      if (n >= MAXHOSTNAMELEN) {
00289                             had_error++;
00290                             continue;
00291                      }
00292                      bp += n;
00293                      buflen -= n;
00294                      /* Get canonical name. */
00295                      n = strlen(tbuf) + 1;       /* for the \0 */
00296                      if (n > buflen || n >= MAXHOSTNAMELEN) {
00297                             had_error++;
00298                             continue;
00299                      }
00300                      strcpy(bp, tbuf);
00301                      host.h_name = bp;
00302                      bp += n;
00303                      buflen -= n;
00304                      continue;
00305               }
00306               if (qtype == T_PTR && type == T_CNAME) {
00307                      n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
00308                      if (n < 0 || !res_dnok(tbuf)) {
00309                             had_error++;
00310                             continue;
00311                      }
00312                      cp += n;
00313                      if (cp != erdata) {
00314                             __set_h_errno (NO_RECOVERY);
00315                             return (NULL);
00316                      }
00317                      /* Get canonical name. */
00318                      n = strlen(tbuf) + 1;       /* for the \0 */
00319                      if (n > buflen || n >= MAXHOSTNAMELEN) {
00320                             had_error++;
00321                             continue;
00322                      }
00323                      strcpy(bp, tbuf);
00324                      tname = bp;
00325                      bp += n;
00326                      buflen -= n;
00327                      continue;
00328               }
00329               if ((type == T_SIG) || (type == T_KEY) || (type == T_NXT)) {
00330                      /* We don't support DNSSEC yet.  For now, ignore
00331                       * the record and send a low priority message
00332                       * to syslog.
00333                       */
00334                      syslog(LOG_DEBUG|LOG_AUTH,
00335               "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
00336                             qname, p_class(C_IN), p_type(qtype),
00337                             p_type(type));
00338                      cp += n;
00339                      continue;
00340               }
00341               if (type != qtype) {
00342                      syslog(LOG_NOTICE|LOG_AUTH,
00343               "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
00344                             qname, p_class(C_IN), p_type(qtype),
00345                             p_type(type));
00346                      cp += n;
00347                      continue;            /* XXX - had_error++ ? */
00348               }
00349               switch (type) {
00350               case T_PTR:
00351                      if (strcasecmp(tname, bp) != 0) {
00352                             syslog(LOG_NOTICE|LOG_AUTH,
00353                                    AskedForGot, qname, bp);
00354                             cp += n;
00355                             continue;     /* XXX - had_error++ ? */
00356                      }
00357                      n = dn_expand(answer->buf, eom, cp, bp, buflen);
00358                      if ((n < 0) || !res_hnok(bp)) {
00359                             had_error++;
00360                             break;
00361                      }
00362 #if MULTI_PTRS_ARE_ALIASES
00363                      cp += n;
00364                      if (cp != erdata) {
00365                             __set_h_errno (NO_RECOVERY);
00366                             return (NULL);
00367                      }
00368                      if (!haveanswer)
00369                             host.h_name = bp;
00370                      else if (ap < &host_aliases[MAXALIASES-1])
00371                             *ap++ = bp;
00372                      else
00373                             n = -1;
00374                      if (n != -1) {
00375                             n = strlen(bp) + 1;  /* for the \0 */
00376                             if (n >= MAXHOSTNAMELEN) {
00377                                    had_error++;
00378                                    break;
00379                             }
00380                             bp += n;
00381                             buflen -= n;
00382                      }
00383                      break;
00384 #else
00385                      host.h_name = bp;
00386                      if (_res.options & RES_USE_INET6) {
00387                             n = strlen(bp) + 1;  /* for the \0 */
00388                             if (n >= MAXHOSTNAMELEN) {
00389                                    had_error++;
00390                                    break;
00391                             }
00392                             bp += n;
00393                             buflen -= n;
00394                             map_v4v6_hostent(&host, &bp, &buflen);
00395                      }
00396                      __set_h_errno (NETDB_SUCCESS);
00397                      return (&host);
00398 #endif
00399               case T_A:
00400               case T_AAAA:
00401                      if (strcasecmp(host.h_name, bp) != 0) {
00402                             syslog(LOG_NOTICE|LOG_AUTH,
00403                                    AskedForGot, host.h_name, bp);
00404                             cp += n;
00405                             continue;     /* XXX - had_error++ ? */
00406                      }
00407                      if (n != host.h_length) {
00408                             cp += n;
00409                             continue;
00410                      }
00411                      if (!haveanswer) {
00412                             register int nn;
00413 
00414                             host.h_name = bp;
00415                             nn = strlen(bp) + 1; /* for the \0 */
00416                             bp += nn;
00417                             buflen -= nn;
00418                      }
00419 
00420                      /* XXX: when incrementing bp, we have to decrement
00421                       * buflen by the same amount --okir */
00422                      buflen -= sizeof(align) - ((u_long)bp % sizeof(align));
00423 
00424                      bp += sizeof(align) - ((u_long)bp % sizeof(align));
00425 
00426                      if (bp + n >= &hostbuf[sizeof hostbuf]) {
00427                             Dprintf("size (%d) too big\n", n);
00428                             had_error++;
00429                             continue;
00430                      }
00431                      if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
00432                             if (!toobig++) {
00433                                    Dprintf("Too many addresses (%d)\n",
00434                                           MAXADDRS);
00435                             }
00436                             cp += n;
00437                             continue;
00438                      }
00439                      memmove(*hap++ = bp, cp, n);
00440                      bp += n;
00441                      buflen -= n;
00442                      cp += n;
00443                      if (cp != erdata) {
00444                             __set_h_errno (NO_RECOVERY);
00445                             return (NULL);
00446                      }
00447                      break;
00448               default:
00449                      abort();
00450               }
00451               if (!had_error)
00452                      haveanswer++;
00453        }
00454        if (haveanswer) {
00455               *ap = NULL;
00456               *hap = NULL;
00457 # if defined(RESOLVSORT)
00458               /*
00459                * Note: we sort even if host can take only one address
00460                * in its return structures - should give it the "best"
00461                * address in that case, not some random one
00462                */
00463               if (_res.nsort && haveanswer > 1 && qtype == T_A)
00464                      addrsort(h_addr_ptrs, haveanswer);
00465 # endif /*RESOLVSORT*/
00466               if (!host.h_name) {
00467                      n = strlen(qname) + 1;      /* for the \0 */
00468                      if (n > buflen || n >= MAXHOSTNAMELEN)
00469                             goto no_recovery;
00470                      strcpy(bp, qname);
00471                      host.h_name = bp;
00472                      bp += n;
00473                      buflen -= n;
00474               }
00475               if (_res.options & RES_USE_INET6)
00476                      map_v4v6_hostent(&host, &bp, &buflen);
00477               __set_h_errno (NETDB_SUCCESS);
00478               return (&host);
00479        }
00480  no_recovery:
00481        __set_h_errno (NO_RECOVERY);
00482        return (NULL);
00483 }
00484 
00485 extern struct hostent *gethostbyname2(const char *name, int af);
00486 libresolv_hidden_proto (gethostbyname2)
00487 
00488 struct hostent *
00489 gethostbyname(name)
00490        const char *name;
00491 {
00492        struct hostent *hp;
00493 
00494        if (__res_maybe_init (&_res, 0) == -1) {
00495               __set_h_errno (NETDB_INTERNAL);
00496               return (NULL);
00497        }
00498        if (_res.options & RES_USE_INET6) {
00499               hp = gethostbyname2(name, AF_INET6);
00500               if (hp)
00501                      return (hp);
00502        }
00503        return (gethostbyname2(name, AF_INET));
00504 }
00505 
00506 struct hostent *
00507 gethostbyname2(name, af)
00508        const char *name;
00509        int af;
00510 {
00511        union
00512        {
00513          querybuf *buf;
00514          u_char *ptr;
00515        } buf;
00516        querybuf *origbuf;
00517        register const char *cp;
00518        char *bp;
00519        int n, size, type, len;
00520        struct hostent *ret;
00521 
00522        if (__res_maybe_init (&_res, 0) == -1) {
00523               __set_h_errno (NETDB_INTERNAL);
00524               return (NULL);
00525        }
00526 
00527        switch (af) {
00528        case AF_INET:
00529               size = INADDRSZ;
00530               type = T_A;
00531               break;
00532        case AF_INET6:
00533               size = IN6ADDRSZ;
00534               type = T_AAAA;
00535               break;
00536        default:
00537               __set_h_errno (NETDB_INTERNAL);
00538               __set_errno (EAFNOSUPPORT);
00539               return (NULL);
00540        }
00541 
00542        host.h_addrtype = af;
00543        host.h_length = size;
00544 
00545        /*
00546         * if there aren't any dots, it could be a user-level alias.
00547         * this is also done in res_query() since we are not the only
00548         * function that looks up host names.
00549         */
00550        if (!strchr(name, '.') && (cp = __hostalias(name)))
00551               name = cp;
00552 
00553        /*
00554         * disallow names consisting only of digits/dots, unless
00555         * they end in a dot.
00556         */
00557        if (isdigit(name[0]))
00558               for (cp = name;; ++cp) {
00559                      if (!*cp) {
00560                             if (*--cp == '.')
00561                                    break;
00562                             /*
00563                              * All-numeric, no dot at the end.
00564                              * Fake up a hostent as if we'd actually
00565                              * done a lookup.
00566                              */
00567                             if (inet_pton(af, name, host_addr) <= 0) {
00568                                    __set_h_errno (HOST_NOT_FOUND);
00569                                    return (NULL);
00570                             }
00571                             strncpy(hostbuf, name, MAXDNAME);
00572                             hostbuf[MAXDNAME] = '\0';
00573                             bp = hostbuf + MAXDNAME;
00574                             len = sizeof hostbuf - MAXDNAME;
00575                             host.h_name = hostbuf;
00576                             host.h_aliases = host_aliases;
00577                             host_aliases[0] = NULL;
00578                             h_addr_ptrs[0] = (char *)host_addr;
00579                             h_addr_ptrs[1] = NULL;
00580                             host.h_addr_list = h_addr_ptrs;
00581                             if (_res.options & RES_USE_INET6)
00582                                    map_v4v6_hostent(&host, &bp, &len);
00583                             __set_h_errno (NETDB_SUCCESS);
00584                             return (&host);
00585                      }
00586                      if (!isdigit(*cp) && *cp != '.')
00587                             break;
00588                }
00589        if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
00590            name[0] == ':')
00591               for (cp = name;; ++cp) {
00592                      if (!*cp) {
00593                             if (*--cp == '.')
00594                                    break;
00595                             /*
00596                              * All-IPv6-legal, no dot at the end.
00597                              * Fake up a hostent as if we'd actually
00598                              * done a lookup.
00599                              */
00600                             if (inet_pton(af, name, host_addr) <= 0) {
00601                                    __set_h_errno (HOST_NOT_FOUND);
00602                                    return (NULL);
00603                             }
00604                             strncpy(hostbuf, name, MAXDNAME);
00605                             hostbuf[MAXDNAME] = '\0';
00606                             bp = hostbuf + MAXDNAME;
00607                             len = sizeof hostbuf - MAXDNAME;
00608                             host.h_name = hostbuf;
00609                             host.h_aliases = host_aliases;
00610                             host_aliases[0] = NULL;
00611                             h_addr_ptrs[0] = (char *)host_addr;
00612                             h_addr_ptrs[1] = NULL;
00613                             host.h_addr_list = h_addr_ptrs;
00614                             __set_h_errno (NETDB_SUCCESS);
00615                             return (&host);
00616                      }
00617                      if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
00618                             break;
00619               }
00620 
00621        buf.buf = origbuf = (querybuf *) alloca (1024);
00622 
00623        if ((n = __libc_res_nsearch(&_res, name, C_IN, type, buf.buf->buf, 1024,
00624                                 &buf.ptr, NULL, NULL, NULL)) < 0) {
00625               if (buf.buf != origbuf)
00626                      free (buf.buf);
00627               Dprintf("res_nsearch failed (%d)\n", n);
00628               if (errno == ECONNREFUSED)
00629                      return (_gethtbyname2(name, af));
00630               return (NULL);
00631        }
00632        ret = getanswer(buf.buf, n, name, type);
00633        if (buf.buf != origbuf)
00634               free (buf.buf);
00635        return ret;
00636 }
00637 libresolv_hidden_def (gethostbyname2)
00638 
00639 struct hostent *
00640 gethostbyaddr(addr, len, af)
00641        const void *addr;
00642        socklen_t len;
00643        int af;
00644 {
00645        const u_char *uaddr = (const u_char *)addr;
00646        static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
00647        static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
00648        int n;
00649        socklen_t size;
00650        union
00651        {
00652          querybuf *buf;
00653          u_char *ptr;
00654        } buf;
00655        querybuf *orig_buf;
00656        register struct hostent *hp;
00657        char qbuf[MAXDNAME+1], *qp = NULL;
00658 #ifdef SUNSECURITY
00659        register struct hostent *rhp;
00660        char **haddr;
00661        u_long old_options;
00662        char hname2[MAXDNAME+1];
00663 #endif /*SUNSECURITY*/
00664 
00665        if (__res_maybe_init (&_res, 0) == -1) {
00666               __set_h_errno (NETDB_INTERNAL);
00667               return (NULL);
00668        }
00669        if (af == AF_INET6 && len == IN6ADDRSZ &&
00670            (!bcmp(uaddr, mapped, sizeof mapped) ||
00671             !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
00672               /* Unmap. */
00673               addr += sizeof mapped;
00674               uaddr += sizeof mapped;
00675               af = AF_INET;
00676               len = INADDRSZ;
00677        }
00678        switch (af) {
00679        case AF_INET:
00680               size = INADDRSZ;
00681               break;
00682        case AF_INET6:
00683               size = IN6ADDRSZ;
00684               break;
00685        default:
00686               __set_errno (EAFNOSUPPORT);
00687               __set_h_errno (NETDB_INTERNAL);
00688               return (NULL);
00689        }
00690        if (size != len) {
00691               __set_errno (EINVAL);
00692               __set_h_errno (NETDB_INTERNAL);
00693               return (NULL);
00694        }
00695        switch (af) {
00696        case AF_INET:
00697               (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
00698                             (uaddr[3] & 0xff),
00699                             (uaddr[2] & 0xff),
00700                             (uaddr[1] & 0xff),
00701                             (uaddr[0] & 0xff));
00702               break;
00703        case AF_INET6:
00704               qp = qbuf;
00705               for (n = IN6ADDRSZ - 1; n >= 0; n--) {
00706                      qp += SPRINTF((qp, "%x.%x.",
00707                                    uaddr[n] & 0xf,
00708                                    (uaddr[n] >> 4) & 0xf));
00709               }
00710               strcpy(qp, "ip6.arpa");
00711               break;
00712        default:
00713               abort();
00714        }
00715 
00716        buf.buf = orig_buf = (querybuf *) alloca (1024);
00717 
00718        n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf, 1024,
00719                            &buf.ptr, NULL, NULL, NULL);
00720        if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0) {
00721               strcpy(qp, "ip6.int");
00722               n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf,
00723                                   buf.buf != orig_buf ? MAXPACKET : 1024,
00724                                   &buf.ptr, NULL, NULL, NULL);
00725        }
00726        if (n < 0) {
00727               if (buf.buf != orig_buf)
00728                      free (buf.buf);
00729               Dprintf("res_nquery failed (%d)\n", n);
00730               if (errno == ECONNREFUSED)
00731                      return (_gethtbyaddr(addr, len, af));
00732               return (NULL);
00733        }
00734        hp = getanswer(buf.buf, n, qbuf, T_PTR);
00735        if (buf.buf != orig_buf)
00736               free (buf.buf);
00737        if (!hp)
00738               return (NULL);       /* h_errno was set by getanswer() */
00739 #ifdef SUNSECURITY
00740        if (af == AF_INET) {
00741            /*
00742             * turn off search as the name should be absolute,
00743             * 'localhost' should be matched by defnames
00744             */
00745            strncpy(hname2, hp->h_name, MAXDNAME);
00746            hname2[MAXDNAME] = '\0';
00747            old_options = _res.options;
00748            _res.options &= ~RES_DNSRCH;
00749            _res.options |= RES_DEFNAMES;
00750            if (!(rhp = gethostbyname(hname2))) {
00751               syslog(LOG_NOTICE|LOG_AUTH,
00752                      "gethostbyaddr: No A record for %s (verifying [%s])",
00753                      hname2, inet_ntoa(*((struct in_addr *)addr)));
00754               _res.options = old_options;
00755               __set_h_errno (HOST_NOT_FOUND);
00756               return (NULL);
00757            }
00758            _res.options = old_options;
00759            for (haddr = rhp->h_addr_list; *haddr; haddr++)
00760               if (!memcmp(*haddr, addr, INADDRSZ))
00761                      break;
00762            if (!*haddr) {
00763               syslog(LOG_NOTICE|LOG_AUTH,
00764                      "gethostbyaddr: A record of %s != PTR record [%s]",
00765                      hname2, inet_ntoa(*((struct in_addr *)addr)));
00766               __set_h_errno (HOST_NOT_FOUND);
00767               return (NULL);
00768            }
00769        }
00770 #endif /*SUNSECURITY*/
00771        hp->h_addrtype = af;
00772        hp->h_length = len;
00773        memmove(host_addr, addr, len);
00774        h_addr_ptrs[0] = (char *)host_addr;
00775        h_addr_ptrs[1] = NULL;
00776        if (af == AF_INET && (_res.options & RES_USE_INET6)) {
00777               map_v4v6_address((char*)host_addr, (char*)host_addr);
00778               hp->h_addrtype = AF_INET6;
00779               hp->h_length = IN6ADDRSZ;
00780        }
00781        __set_h_errno (NETDB_SUCCESS);
00782        return (hp);
00783 }
00784 
00785 void
00786 _sethtent(f)
00787        int f;
00788 {
00789        if (!hostf)
00790               hostf = fopen(_PATH_HOSTS, "r" );
00791        else
00792               rewind(hostf);
00793        stayopen = f;
00794 }
00795 libresolv_hidden_def (_sethtent)
00796 
00797 void
00798 _endhtent()
00799 {
00800        if (hostf && !stayopen) {
00801               (void) fclose(hostf);
00802               hostf = NULL;
00803        }
00804 }
00805 
00806 struct hostent *
00807 _gethtent()
00808 {
00809        char *p;
00810        register char *cp, **q;
00811        int af, len;
00812 
00813        if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
00814               __set_h_errno (NETDB_INTERNAL);
00815               return (NULL);
00816        }
00817  again:
00818        if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
00819               __set_h_errno (HOST_NOT_FOUND);
00820               return (NULL);
00821        }
00822        if (*p == '#')
00823               goto again;
00824        if (!(cp = strpbrk(p, "#\n")))
00825               goto again;
00826        *cp = '\0';
00827        if (!(cp = strpbrk(p, " \t")))
00828               goto again;
00829        *cp++ = '\0';
00830        if (inet_pton(AF_INET6, p, host_addr) > 0) {
00831               af = AF_INET6;
00832               len = IN6ADDRSZ;
00833        } else if (inet_pton(AF_INET, p, host_addr) > 0) {
00834               if (_res.options & RES_USE_INET6) {
00835                      map_v4v6_address((char*)host_addr, (char*)host_addr);
00836                      af = AF_INET6;
00837                      len = IN6ADDRSZ;
00838               } else {
00839                      af = AF_INET;
00840                      len = INADDRSZ;
00841               }
00842        } else {
00843               goto again;
00844        }
00845        h_addr_ptrs[0] = (char *)host_addr;
00846        h_addr_ptrs[1] = NULL;
00847        host.h_addr_list = h_addr_ptrs;
00848        host.h_length = len;
00849        host.h_addrtype = af;
00850        while (*cp == ' ' || *cp == '\t')
00851               cp++;
00852        host.h_name = cp;
00853        q = host.h_aliases = host_aliases;
00854        if ((cp = strpbrk(cp, " \t")))
00855               *cp++ = '\0';
00856        while (cp && *cp) {
00857               if (*cp == ' ' || *cp == '\t') {
00858                      cp++;
00859                      continue;
00860               }
00861               if (q < &host_aliases[MAXALIASES - 1])
00862                      *q++ = cp;
00863               if ((cp = strpbrk(cp, " \t")))
00864                      *cp++ = '\0';
00865        }
00866        *q = NULL;
00867        __set_h_errno (NETDB_SUCCESS);
00868        return (&host);
00869 }
00870 libresolv_hidden_def (_gethtent)
00871 
00872 struct hostent *
00873 _gethtbyname(name)
00874        const char *name;
00875 {
00876        struct hostent *hp;
00877 
00878        if (_res.options & RES_USE_INET6) {
00879               hp = _gethtbyname2(name, AF_INET6);
00880               if (hp)
00881                      return (hp);
00882        }
00883        return (_gethtbyname2(name, AF_INET));
00884 }
00885 
00886 struct hostent *
00887 _gethtbyname2(name, af)
00888        const char *name;
00889        int af;
00890 {
00891        register struct hostent *p;
00892        register char **cp;
00893 
00894        _sethtent(0);
00895        while ((p = _gethtent())) {
00896               if (p->h_addrtype != af)
00897                      continue;
00898               if (strcasecmp(p->h_name, name) == 0)
00899                      break;
00900               for (cp = p->h_aliases; *cp != 0; cp++)
00901                      if (strcasecmp(*cp, name) == 0)
00902                             goto found;
00903        }
00904  found:
00905        _endhtent();
00906        return (p);
00907 }
00908 libresolv_hidden_def (_gethtbyname2)
00909 
00910 struct hostent *
00911 _gethtbyaddr(addr, len, af)
00912        const char *addr;
00913        size_t len;
00914        int af;
00915 {
00916        register struct hostent *p;
00917 
00918        _sethtent(0);
00919        while ((p = _gethtent()))
00920               if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
00921                      break;
00922        _endhtent();
00923        return (p);
00924 }
00925 libresolv_hidden_def (_gethtbyaddr)
00926 
00927 static void
00928 map_v4v6_address(src, dst)
00929        const char *src;
00930        char *dst;
00931 {
00932        u_char *p = (u_char *)dst;
00933        char tmp[INADDRSZ];
00934        int i;
00935 
00936        /* Stash a temporary copy so our caller can update in place. */
00937        memcpy(tmp, src, INADDRSZ);
00938        /* Mark this ipv6 addr as a mapped ipv4. */
00939        for (i = 0; i < 10; i++)
00940               *p++ = 0x00;
00941        *p++ = 0xff;
00942        *p++ = 0xff;
00943        /* Retrieve the saved copy and we're done. */
00944        memcpy((void*)p, tmp, INADDRSZ);
00945 }
00946 
00947 static void
00948 map_v4v6_hostent(hp, bpp, lenp)
00949        struct hostent *hp;
00950        char **bpp;
00951        int *lenp;
00952 {
00953        char **ap;
00954 
00955        if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
00956               return;
00957        hp->h_addrtype = AF_INET6;
00958        hp->h_length = IN6ADDRSZ;
00959        for (ap = hp->h_addr_list; *ap; ap++) {
00960               int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
00961 
00962               if (*lenp < (i + IN6ADDRSZ)) {
00963                      /* Out of memory.  Truncate address list here.  XXX */
00964                      *ap = NULL;
00965                      return;
00966               }
00967               *bpp += i;
00968               *lenp -= i;
00969               map_v4v6_address(*ap, *bpp);
00970               *ap = *bpp;
00971               *bpp += IN6ADDRSZ;
00972               *lenp -= IN6ADDRSZ;
00973        }
00974 }
00975 
00976 #ifdef RESOLVSORT
00977 extern void
00978 addrsort(ap, num)
00979        char **ap;
00980        int num;
00981 {
00982        int i, j;
00983        char **p;
00984        short aval[MAXADDRS];
00985        int needsort = 0;
00986 
00987        p = ap;
00988        for (i = 0; i < num; i++, p++) {
00989            for (j = 0 ; (unsigned)j < _res.nsort; j++)
00990               if (_res.sort_list[j].addr.s_addr ==
00991                   (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
00992                      break;
00993            aval[i] = j;
00994            if (needsort == 0 && i > 0 && j < aval[i-1])
00995               needsort = i;
00996        }
00997        if (!needsort)
00998            return;
00999 
01000        while (needsort < num) {
01001            for (j = needsort - 1; j >= 0; j--) {
01002               if (aval[j] > aval[j+1]) {
01003                   char *hp;
01004 
01005                   i = aval[j];
01006                   aval[j] = aval[j+1];
01007                   aval[j+1] = i;
01008 
01009                   hp = ap[j];
01010                   ap[j] = ap[j+1];
01011                   ap[j+1] = hp;
01012 
01013               } else
01014                   break;
01015            }
01016            needsort++;
01017        }
01018 }
01019 #endif
01020 
01021 #if defined(BSD43_BSD43_NFS) || defined(sun)
01022 /* some libc's out there are bound internally to these names (UMIPS) */
01023 void
01024 ht_sethostent(stayopen)
01025        int stayopen;
01026 {
01027        _sethtent(stayopen);
01028 }
01029 
01030 void
01031 ht_endhostent()
01032 {
01033        _endhtent();
01034 }
01035 
01036 struct hostent *
01037 ht_gethostbyname(name)
01038        char *name;
01039 {
01040        return (_gethtbyname(name));
01041 }
01042 
01043 struct hostent *
01044 ht_gethostbyaddr(addr, len, af)
01045        const char *addr;
01046        size_t len;
01047        int af;
01048 {
01049        return (_gethtbyaddr(addr, len, af));
01050 }
01051 
01052 struct hostent *
01053 gethostent()
01054 {
01055        return (_gethtent());
01056 }
01057 
01058 void
01059 dns_service()
01060 {
01061        return;
01062 }
01063 
01064 #undef dn_skipname
01065 dn_skipname(comp_dn, eom)
01066        const u_char *comp_dn, *eom;
01067 {
01068        return (__dn_skipname(comp_dn, eom));
01069 }
01070 #endif /*old-style libc with yp junk in it*/