Back to index

glibc  2.9
res_query.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1988, 1993
00003  *    The Regents of the University of California.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 4. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
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 
00050 /*
00051  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
00052  *
00053  * Permission to use, copy, modify, and distribute this software for any
00054  * purpose with or without fee is hereby granted, provided that the above
00055  * copyright notice and this permission notice appear in all copies.
00056  *
00057  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00058  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00059  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00060  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00061  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00062  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00063  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00064  * SOFTWARE.
00065  */
00066 
00067 #if defined(LIBC_SCCS) && !defined(lint)
00068 static const char sccsid[] = "@(#)res_query.c    8.1 (Berkeley) 6/4/93";
00069 static const char rcsid[] = "$BINDId: res_query.c,v 8.20 2000/02/29 05:39:12 vixie Exp $";
00070 #endif /* LIBC_SCCS and not lint */
00071 
00072 #include <assert.h>
00073 #include <sys/types.h>
00074 #include <sys/param.h>
00075 #include <netinet/in.h>
00076 #include <arpa/inet.h>
00077 #include <arpa/nameser.h>
00078 #include <ctype.h>
00079 #include <errno.h>
00080 #include <netdb.h>
00081 #include <resolv.h>
00082 #include <stdio.h>
00083 #include <stdlib.h>
00084 #include <string.h>
00085 
00086 /* Options.  Leave them on. */
00087 /* #undef DEBUG */
00088 
00089 #if PACKETSZ > 65536
00090 #define MAXPACKET    PACKETSZ
00091 #else
00092 #define MAXPACKET    65536
00093 #endif
00094 
00095 #define QUERYSIZE    (HFIXEDSZ + QFIXEDSZ + MAXCDNAME + 1)
00096 
00097 static int
00098 __libc_res_nquerydomain(res_state statp, const char *name, const char *domain,
00099                      int class, int type, u_char *answer, int anslen,
00100                      u_char **answerp, u_char **answerp2, int *nanswerp2,
00101                      int *resplen2);
00102 
00103 /*
00104  * Formulate a normal query, send, and await answer.
00105  * Returned answer is placed in supplied buffer "answer".
00106  * Perform preliminary check of answer, returning success only
00107  * if no error is indicated and the answer count is nonzero.
00108  * Return the size of the response on success, -1 on error.
00109  * Error number is left in H_ERRNO.
00110  *
00111  * Caller must parse answer and determine whether it answers the question.
00112  */
00113 int
00114 __libc_res_nquery(res_state statp,
00115                 const char *name,  /* domain name */
00116                 int class, int type,      /* class and type of query */
00117                 u_char *answer,    /* buffer to put answer */
00118                 int anslen,        /* size of answer buffer */
00119                 u_char **answerp,  /* if buffer needs to be enlarged */
00120                 u_char **answerp2,
00121                 int *nanswerp2,
00122                 int *resplen2)
00123 {
00124        HEADER *hp = (HEADER *) answer;
00125        int n, use_malloc = 0;
00126         u_int oflags = statp->_flags;
00127 
00128        size_t bufsize = (type == T_UNSPEC ? 2 : 1) * QUERYSIZE;
00129        u_char *buf = alloca (bufsize);
00130        u_char *query1 = buf;
00131        int nquery1 = -1;
00132        u_char *query2 = NULL;
00133        int nquery2 = 0;
00134 
00135  again:
00136        hp->rcode = NOERROR; /* default */
00137 
00138 #ifdef DEBUG
00139        if (statp->options & RES_DEBUG)
00140               printf(";; res_query(%s, %d, %d)\n", name, class, type);
00141 #endif
00142 
00143        if (type == T_UNSPEC)
00144          {
00145            n = res_nmkquery(statp, QUERY, name, class, T_A, NULL, 0, NULL,
00146                           query1, bufsize);
00147            if (n > 0)
00148              {
00149               if ((oflags & RES_F_EDNS0ERR) == 0
00150                   && (statp->options & RES_USE_EDNS0) != 0)
00151                 {
00152                   n = __res_nopt(statp, n, query1, bufsize, anslen / 2);
00153                   if (n < 0)
00154                     goto unspec_nomem;
00155                 }
00156 
00157               nquery1 = n;
00158               /* Align the buffer.  */
00159               int npad = ((nquery1 + __alignof__ (HEADER) - 1)
00160                          & ~(__alignof__ (HEADER) - 1)) - nquery1;
00161               if (n > bufsize - npad)
00162                 {
00163                   n = -1;
00164                   goto unspec_nomem;
00165                 }
00166               int nused = n + npad;
00167               query2 = buf + nused;
00168               n = res_nmkquery(statp, QUERY, name, class, T_AAAA, NULL, 0,
00169                              NULL, query2, bufsize - nused);
00170               if (n > 0
00171                   && (oflags & RES_F_EDNS0ERR) == 0
00172                   && (statp->options & RES_USE_EDNS0) != 0)
00173                 n = __res_nopt(statp, n, query2, bufsize - nused - n,
00174                              anslen / 2);
00175               nquery2 = n;
00176              }
00177 
00178          unspec_nomem:;
00179          }
00180        else
00181          {
00182            n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
00183                           query1, bufsize);
00184 
00185            if (n > 0
00186               && (oflags & RES_F_EDNS0ERR) == 0
00187               && (statp->options & RES_USE_EDNS0) != 0)
00188              n = __res_nopt(statp, n, query1, bufsize, anslen);
00189 
00190            nquery1 = n;
00191          }
00192 
00193        if (__builtin_expect (n <= 0, 0) && !use_malloc) {
00194               /* Retry just in case res_nmkquery failed because of too
00195                  short buffer.  Shouldn't happen.  */
00196               bufsize = (type == T_UNSPEC ? 2 : 1) * MAXPACKET;
00197               buf = malloc (bufsize);
00198               if (buf != NULL) {
00199                      query1 = buf;
00200                      use_malloc = 1;
00201                      goto again;
00202               }
00203        }
00204        if (__builtin_expect (n <= 0, 0)) {
00205               /* If the query choked with EDNS0, retry without EDNS0.  */
00206               if ((statp->options & RES_USE_EDNS0) != 0
00207                   && ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
00208                      statp->_flags |= RES_F_EDNS0ERR;
00209 #ifdef DEBUG
00210                      if (statp->options & RES_DEBUG)
00211                             printf(";; res_nquery: retry without EDNS0\n");
00212 #endif
00213                         goto again;
00214               }
00215 #ifdef DEBUG
00216               if (statp->options & RES_DEBUG)
00217                      printf(";; res_query: mkquery failed\n");
00218 #endif
00219               RES_SET_H_ERRNO(statp, NO_RECOVERY);
00220               if (use_malloc)
00221                      free (buf);
00222               return (n);
00223        }
00224        assert (answerp == NULL || (void *) *answerp == (void *) answer);
00225        n = __libc_res_nsend(statp, query1, nquery1, query2, nquery2, answer,
00226                           anslen, answerp, answerp2, nanswerp2, resplen2);
00227        if (use_malloc)
00228               free (buf);
00229        if (n < 0) {
00230 #ifdef DEBUG
00231               if (statp->options & RES_DEBUG)
00232                      printf(";; res_query: send error\n");
00233 #endif
00234               RES_SET_H_ERRNO(statp, TRY_AGAIN);
00235               return (n);
00236        }
00237 
00238        if (answerp != NULL)
00239          /* __libc_res_nsend might have reallocated the buffer.  */
00240          hp = (HEADER *) *answerp;
00241 
00242        /* We simplify the following tests by assigning HP to HP2.  It
00243           is easy to verify that this is the same as ignoring all
00244           tests of HP2.  */
00245        HEADER *hp2 = answerp2 ? (HEADER *) *answerp2 : hp;
00246 
00247        if (n < (int) sizeof (HEADER) && answerp2 != NULL
00248            && *resplen2 > (int) sizeof (HEADER))
00249          {
00250            /* Special case of partial answer.  */
00251            assert (hp != hp2);
00252            hp = hp2;
00253          }
00254        else if (answerp2 != NULL && *resplen2 < (int) sizeof (HEADER)
00255                && n > (int) sizeof (HEADER))
00256          {
00257            /* Special case of partial answer.  */
00258            assert (hp != hp2);
00259            hp2 = hp;
00260          }
00261 
00262        if ((hp->rcode != NOERROR || ntohs(hp->ancount) == 0)
00263            && (hp2->rcode != NOERROR || ntohs(hp2->ancount) == 0)) {
00264 #ifdef DEBUG
00265               if (statp->options & RES_DEBUG) {
00266                      printf(";; rcode = %d, ancount=%d\n", hp->rcode,
00267                          ntohs(hp->ancount));
00268                      if (hp != hp2)
00269                        printf(";; rcode2 = %d, ancount2=%d\n", hp2->rcode,
00270                              ntohs(hp2->ancount));
00271               }
00272 #endif
00273               switch (hp->rcode == NOERROR ? hp2->rcode : hp->rcode) {
00274               case NXDOMAIN:
00275                      if ((hp->rcode == NOERROR && ntohs (hp->ancount) != 0)
00276                          || (hp2->rcode == NOERROR
00277                             && ntohs (hp2->ancount) != 0))
00278                             goto success;
00279                      RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
00280                      break;
00281               case SERVFAIL:
00282                      RES_SET_H_ERRNO(statp, TRY_AGAIN);
00283                      break;
00284               case NOERROR:
00285                      if (ntohs (hp->ancount) != 0
00286                          || ntohs (hp2->ancount) != 0)
00287                             goto success;
00288                      RES_SET_H_ERRNO(statp, NO_DATA);
00289                      break;
00290               case FORMERR:
00291               case NOTIMP:
00292               case REFUSED:
00293               default:
00294                      RES_SET_H_ERRNO(statp, NO_RECOVERY);
00295                      break;
00296               }
00297               return (-1);
00298        }
00299  success:
00300        return (n);
00301 }
00302 libresolv_hidden_def (__libc_res_nquery)
00303 
00304 int
00305 res_nquery(res_state statp,
00306           const char *name, /* domain name */
00307           int class, int type,     /* class and type of query */
00308           u_char *answer,   /* buffer to put answer */
00309           int anslen)              /* size of answer buffer */
00310 {
00311        return __libc_res_nquery(statp, name, class, type, answer, anslen,
00312                              NULL, NULL, NULL, NULL);
00313 }
00314 libresolv_hidden_def (res_nquery)
00315 
00316 /*
00317  * Formulate a normal query, send, and retrieve answer in supplied buffer.
00318  * Return the size of the response on success, -1 on error.
00319  * If enabled, implement search rules until answer or unrecoverable failure
00320  * is detected.  Error code, if any, is left in H_ERRNO.
00321  */
00322 int
00323 __libc_res_nsearch(res_state statp,
00324                  const char *name, /* domain name */
00325                  int class, int type,     /* class and type of query */
00326                  u_char *answer,   /* buffer to put answer */
00327                  int anslen,              /* size of answer */
00328                  u_char **answerp,
00329                  u_char **answerp2,
00330                  int *nanswerp2,
00331                  int *resplen2)
00332 {
00333        const char *cp, * const *domain;
00334        HEADER *hp = (HEADER *) answer;
00335        char tmp[NS_MAXDNAME];
00336        u_int dots;
00337        int trailing_dot, ret, saved_herrno;
00338        int got_nodata = 0, got_servfail = 0, root_on_list = 0;
00339        int tried_as_is = 0;
00340 
00341        __set_errno (0);
00342        RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);  /* True if we never query. */
00343 
00344        dots = 0;
00345        for (cp = name; *cp != '\0'; cp++)
00346               dots += (*cp == '.');
00347        trailing_dot = 0;
00348        if (cp > name && *--cp == '.')
00349               trailing_dot++;
00350 
00351        /* If there aren't any dots, it could be a user-level alias. */
00352        if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL)
00353               return (__libc_res_nquery(statp, cp, class, type, answer,
00354                                      anslen, answerp, answerp2,
00355                                      nanswerp2, resplen2));
00356 
00357 #ifdef DEBUG
00358        if (statp->options & RES_DEBUG)
00359               printf("dots=%d, statp->ndots=%d, trailing_dot=%d, name=%s\n",
00360                      (int)dots,(int)statp->ndots,(int)trailing_dot,name);
00361 #endif
00362 
00363        /*
00364         * If there are enough dots in the name, let's just give it a
00365         * try 'as is'. The threshold can be set with the "ndots" option.
00366         * Also, query 'as is', if there is a trailing dot in the name.
00367         */
00368        saved_herrno = -1;
00369        if (dots >= statp->ndots || trailing_dot) {
00370               ret = __libc_res_nquerydomain(statp, name, NULL, class, type,
00371                                          answer, anslen, answerp,
00372                                          answerp2, nanswerp2, resplen2);
00373               if (ret > 0 || trailing_dot)
00374                      return (ret);
00375               saved_herrno = h_errno;
00376               tried_as_is++;
00377               if (answerp && *answerp != answer) {
00378                      answer = *answerp;
00379                      anslen = MAXPACKET;
00380               }
00381               if (answerp2
00382                   && (*answerp2 < answer || *answerp2 >= answer + anslen))
00383                 {
00384                   free (*answerp2);
00385                   *answerp2 = NULL;
00386                 }
00387        }
00388 
00389        /*
00390         * We do at least one level of search if
00391         *     - there is no dot and RES_DEFNAME is set, or
00392         *     - there is at least one dot, there is no trailing dot,
00393         *       and RES_DNSRCH is set.
00394         */
00395        if ((!dots && (statp->options & RES_DEFNAMES) != 0) ||
00396            (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0)) {
00397               int done = 0;
00398 
00399               for (domain = (const char * const *)statp->dnsrch;
00400                    *domain && !done;
00401                    domain++) {
00402 
00403                      if (domain[0][0] == '\0' ||
00404                          (domain[0][0] == '.' && domain[0][1] == '\0'))
00405                             root_on_list++;
00406 
00407                      ret = __libc_res_nquerydomain(statp, name, *domain,
00408                                                 class, type,
00409                                                 answer, anslen, answerp,
00410                                                 answerp2, nanswerp2,
00411                                                 resplen2);
00412                      if (ret > 0)
00413                             return (ret);
00414 
00415                      if (answerp && *answerp != answer) {
00416                             answer = *answerp;
00417                             anslen = MAXPACKET;
00418                      }
00419                      if (answerp2
00420                          && (*answerp2 < answer
00421                             || *answerp2 >= answer + anslen))
00422                        {
00423                          free (*answerp2);
00424                          *answerp2 = NULL;
00425                        }
00426 
00427                      /*
00428                       * If no server present, give up.
00429                       * If name isn't found in this domain,
00430                       * keep trying higher domains in the search list
00431                       * (if that's enabled).
00432                       * On a NO_DATA error, keep trying, otherwise
00433                       * a wildcard entry of another type could keep us
00434                       * from finding this entry higher in the domain.
00435                       * If we get some other error (negative answer or
00436                       * server failure), then stop searching up,
00437                       * but try the input name below in case it's
00438                       * fully-qualified.
00439                       */
00440                      if (errno == ECONNREFUSED) {
00441                             RES_SET_H_ERRNO(statp, TRY_AGAIN);
00442                             return (-1);
00443                      }
00444 
00445                      switch (statp->res_h_errno) {
00446                      case NO_DATA:
00447                             got_nodata++;
00448                             /* FALLTHROUGH */
00449                      case HOST_NOT_FOUND:
00450                             /* keep trying */
00451                             break;
00452                      case TRY_AGAIN:
00453                             if (hp->rcode == SERVFAIL) {
00454                                    /* try next search element, if any */
00455                                    got_servfail++;
00456                                    break;
00457                             }
00458                             /* FALLTHROUGH */
00459                      default:
00460                             /* anything else implies that we're done */
00461                             done++;
00462                      }
00463 
00464                      /* if we got here for some reason other than DNSRCH,
00465                       * we only wanted one iteration of the loop, so stop.
00466                       */
00467                      if ((statp->options & RES_DNSRCH) == 0)
00468                             done++;
00469               }
00470        }
00471 
00472        /*
00473         * If the name has any dots at all, and no earlier 'as-is' query
00474         * for the name, and "." is not on the search list, then try an as-is
00475         * query now.
00476         */
00477        if (dots && !(tried_as_is || root_on_list)) {
00478               ret = __libc_res_nquerydomain(statp, name, NULL, class, type,
00479                                          answer, anslen, answerp,
00480                                          answerp2, nanswerp2, resplen2);
00481               if (ret > 0)
00482                      return (ret);
00483        }
00484 
00485        /* if we got here, we didn't satisfy the search.
00486         * if we did an initial full query, return that query's H_ERRNO
00487         * (note that we wouldn't be here if that query had succeeded).
00488         * else if we ever got a nodata, send that back as the reason.
00489         * else send back meaningless H_ERRNO, that being the one from
00490         * the last DNSRCH we did.
00491         */
00492        if (answerp2 && (*answerp2 < answer || *answerp2 >= answer + anslen))
00493          {
00494            free (*answerp2);
00495            *answerp2 = NULL;
00496          }
00497        if (saved_herrno != -1)
00498               RES_SET_H_ERRNO(statp, saved_herrno);
00499        else if (got_nodata)
00500               RES_SET_H_ERRNO(statp, NO_DATA);
00501        else if (got_servfail)
00502               RES_SET_H_ERRNO(statp, TRY_AGAIN);
00503        return (-1);
00504 }
00505 libresolv_hidden_def (__libc_res_nsearch)
00506 
00507 int
00508 res_nsearch(res_state statp,
00509            const char *name,       /* domain name */
00510            int class, int type,    /* class and type of query */
00511            u_char *answer,  /* buffer to put answer */
00512            int anslen)             /* size of answer */
00513 {
00514        return __libc_res_nsearch(statp, name, class, type, answer,
00515                               anslen, NULL, NULL, NULL, NULL);
00516 }
00517 libresolv_hidden_def (res_nsearch)
00518 
00519 /*
00520  * Perform a call on res_query on the concatenation of name and domain,
00521  * removing a trailing dot from name if domain is NULL.
00522  */
00523 static int
00524 __libc_res_nquerydomain(res_state statp,
00525                      const char *name,
00526                      const char *domain,
00527                      int class, int type, /* class and type of query */
00528                      u_char *answer,             /* buffer to put answer */
00529                      int anslen,                 /* size of answer */
00530                      u_char **answerp,
00531                      u_char **answerp2,
00532                      int *nanswerp2,
00533                      int *resplen2)
00534 {
00535        char nbuf[MAXDNAME];
00536        const char *longname = nbuf;
00537        int n, d;
00538 
00539 #ifdef DEBUG
00540        if (statp->options & RES_DEBUG)
00541               printf(";; res_nquerydomain(%s, %s, %d, %d)\n",
00542                      name, domain?domain:"<Nil>", class, type);
00543 #endif
00544        if (domain == NULL) {
00545               /*
00546                * Check for trailing '.';
00547                * copy without '.' if present.
00548                */
00549               n = strlen(name);
00550               if (n >= MAXDNAME) {
00551                      RES_SET_H_ERRNO(statp, NO_RECOVERY);
00552                      return (-1);
00553               }
00554               n--;
00555               if (n >= 0 && name[n] == '.') {
00556                      strncpy(nbuf, name, n);
00557                      nbuf[n] = '\0';
00558               } else
00559                      longname = name;
00560        } else {
00561               n = strlen(name);
00562               d = strlen(domain);
00563               if (n + d + 1 >= MAXDNAME) {
00564                      RES_SET_H_ERRNO(statp, NO_RECOVERY);
00565                      return (-1);
00566               }
00567               sprintf(nbuf, "%s.%s", name, domain);
00568        }
00569        return (__libc_res_nquery(statp, longname, class, type, answer,
00570                               anslen, answerp, answerp2, nanswerp2,
00571                               resplen2));
00572 }
00573 
00574 int
00575 res_nquerydomain(res_state statp,
00576            const char *name,
00577            const char *domain,
00578            int class, int type,    /* class and type of query */
00579            u_char *answer,         /* buffer to put answer */
00580            int anslen)             /* size of answer */
00581 {
00582        return __libc_res_nquerydomain(statp, name, domain, class, type,
00583                                    answer, anslen, NULL, NULL, NULL, NULL);
00584 }
00585 libresolv_hidden_def (res_nquerydomain)
00586 
00587 const char *
00588 res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) {
00589        char *file, *cp1, *cp2;
00590        char buf[BUFSIZ];
00591        FILE *fp;
00592 
00593        if (statp->options & RES_NOALIASES)
00594               return (NULL);
00595        file = getenv("HOSTALIASES");
00596        if (file == NULL || (fp = fopen(file, "r")) == NULL)
00597               return (NULL);
00598        setbuf(fp, NULL);
00599        buf[sizeof(buf) - 1] = '\0';
00600        while (fgets(buf, sizeof(buf), fp)) {
00601               for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1)
00602                      ;
00603               if (!*cp1)
00604                      break;
00605               *cp1 = '\0';
00606               if (ns_samename(buf, name) == 1) {
00607                      while (isspace(*++cp1))
00608                             ;
00609                      if (!*cp1)
00610                             break;
00611                      for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2)
00612                             ;
00613                      *cp2 = '\0';
00614                      strncpy(dst, cp1, siz - 1);
00615                      dst[siz - 1] = '\0';
00616                      fclose(fp);
00617                      return (dst);
00618               }
00619        }
00620        fclose(fp);
00621        return (NULL);
00622 }
00623 libresolv_hidden_def (res_hostalias)