home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ixemul-45.0-src.tgz / tar.out / contrib / ixemul / net / res_query.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  11KB  |  371 lines

  1. /*    $NetBSD: res_query.c,v 1.9 1995/02/25 06:58:58 cgd Exp $    */
  2.  
  3. /*-
  4.  * Copyright (c) 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.  * 3. All advertising materials mentioning features or use of this software
  16.  *    must display the following acknowledgement:
  17.  *    This product includes software developed by the University of
  18.  *    California, Berkeley and its contributors.
  19.  * 4. Neither the name of the University nor the names of its contributors
  20.  *    may be used to endorse or promote products derived from this software
  21.  *    without specific prior written permission.
  22.  *
  23.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33.  * SUCH DAMAGE.
  34.  * -
  35.  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  36.  * 
  37.  * Permission to use, copy, modify, and distribute this software for any
  38.  * purpose with or without fee is hereby granted, provided that the above
  39.  * copyright notice and this permission notice appear in all copies, and that
  40.  * the name of Digital Equipment Corporation not be used in advertising or
  41.  * publicity pertaining to distribution of the document or software without
  42.  * specific, written prior permission.
  43.  * 
  44.  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  45.  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  46.  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
  47.  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  48.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  49.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  50.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  51.  * SOFTWARE.
  52.  * -
  53.  * --Copyright--
  54.  */
  55.  
  56. #if defined(LIBC_SCCS) && !defined(lint)
  57. #if 0
  58. static char sccsid[] = "@(#)res_query.c    8.1 (Berkeley) 6/4/93";
  59. static char rcsid[] = "$Id: res_query.c,v 1.1 1993/06/01 09:42:14 vixie Exp vixie ";
  60. #else
  61. static char rcsid[] = "$NetBSD: res_query.c,v 1.9 1995/02/25 06:58:58 cgd Exp $";
  62. #endif
  63. #endif /* LIBC_SCCS and not lint */
  64.  
  65. #include <sys/param.h>
  66. #include <netinet/in.h>
  67. #include <arpa/inet.h>
  68. #include <arpa/nameser.h>
  69. #include <netdb.h>
  70. #include <resolv.h>
  71. #include <stdio.h>
  72. #include <ctype.h>
  73. #include <errno.h>
  74. #include <stdlib.h>
  75. #include <string.h>
  76.  
  77. #if PACKETSZ > 1024
  78. #define MAXPACKET    PACKETSZ
  79. #else
  80. #define MAXPACKET    1024
  81. #endif
  82.  
  83. char *__hostalias __P((const char *));
  84. int res_querydomain(const char *, char *, int, int, u_char *, int);
  85. int h_errno;
  86.  
  87. /*
  88.  * Formulate a normal query, send, and await answer.
  89.  * Returned answer is placed in supplied buffer "answer".
  90.  * Perform preliminary check of answer, returning success only
  91.  * if no error is indicated and the answer count is nonzero.
  92.  * Return the size of the response on success, -1 on error.
  93.  * Error number is left in h_errno.
  94.  * Caller must parse answer and determine whether it answers the question.
  95.  */
  96. int
  97. res_query(name, class, type, answer, anslen)
  98.     char *name;        /* domain name */
  99.     int class, type;    /* class and type of query */
  100.     u_char *answer;        /* buffer to put answer */
  101.     int anslen;        /* size of answer buffer */
  102. {
  103.     char buf[MAXPACKET];
  104.     HEADER *hp;
  105.     int n;
  106.  
  107.     if ((_res.options & RES_INIT) == 0 && res_init() == -1)
  108.         return (-1);
  109. #ifdef DEBUG
  110.     if (_res.options & RES_DEBUG)
  111.         printf(";; res_query(%s, %d, %d)\n", name, class, type);
  112. #endif
  113.     n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
  114.         buf, sizeof(buf));
  115.  
  116.     if (n <= 0) {
  117. #ifdef DEBUG
  118.         if (_res.options & RES_DEBUG)
  119.             printf(";; res_query: mkquery failed\n");
  120. #endif
  121.         h_errno = NO_RECOVERY;
  122.         return (n);
  123.     }
  124.     n = res_send(buf, n, (char *)answer, anslen);
  125.     if (n < 0) {
  126. #ifdef DEBUG
  127.         if (_res.options & RES_DEBUG)
  128.             printf(";; res_query: send error\n");
  129. #endif
  130.         h_errno = TRY_AGAIN;
  131.         return (n);
  132.     }
  133.  
  134.     hp = (HEADER *) answer;
  135.     if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
  136. #ifdef DEBUG
  137.         if (_res.options & RES_DEBUG)
  138.             printf(";; rcode = %d, ancount=%d\n", hp->rcode,
  139.                 ntohs(hp->ancount));
  140. #endif
  141.         switch (hp->rcode) {
  142.             case NXDOMAIN:
  143.                 h_errno = HOST_NOT_FOUND;
  144.                 break;
  145.             case SERVFAIL:
  146.                 h_errno = TRY_AGAIN;
  147.                 break;
  148.             case NOERROR:
  149.                 h_errno = NO_DATA;
  150.                 break;
  151.             case FORMERR:
  152.             case NOTIMP:
  153.             case REFUSED:
  154.             default:
  155.                 h_errno = NO_RECOVERY;
  156.                 break;
  157.         }
  158.         return (-1);
  159.     }
  160.     return (n);
  161. }
  162.  
  163. /*
  164.  * Formulate a normal query, send, and retrieve answer in supplied buffer.
  165.  * Return the size of the response on success, -1 on error.
  166.  * If enabled, implement search rules until answer or unrecoverable failure
  167.  * is detected.  Error number is left in h_errno.
  168.  * Only useful for queries in the same name hierarchy as the local host
  169.  * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
  170.  */
  171. int
  172. res_search(name, class, type, answer, anslen)
  173.     const char *name;    /* domain name */
  174.     int class, type;    /* class and type of query */
  175.     u_char *answer;        /* buffer to put answer */
  176.     int anslen;        /* size of answer */
  177. {
  178.     register char *cp, **domain;
  179.     int dots, trailing_dot, ret, got_nodata, saved_herrno, tried_as_is;
  180.  
  181.     if ((_res.options & RES_INIT) == 0 && res_init() == -1)
  182.         return (-1);
  183.  
  184.     got_nodata = 0;
  185.     errno = 0;
  186.     h_errno = HOST_NOT_FOUND;    /* default, if we never query */
  187.     dots = 0;
  188.     for (cp = (char *)name; *cp; cp++) {
  189.         if (*cp == '.')
  190.             dots++;
  191.     }
  192.     trailing_dot = 0;
  193.     if ((cp > name) && (*--cp == '.'))
  194.         trailing_dot++;
  195.  
  196.     /*
  197.      * if there aren't any dots, it could be a user-level alias
  198.      */
  199.     if (!dots && (cp = __hostalias(name)))
  200.         return (res_query(cp, class, type, answer, anslen));
  201.  
  202.     /*
  203.      * If there are dots in the name already, let's just give it a try
  204.      * 'as is'.  The threshold can be set with the "ndots" option.
  205.      */
  206.     saved_herrno = -1;
  207.     tried_as_is = 0;
  208.     if (dots >= _res.ndots) {
  209.         ret = res_querydomain(name, NULL, class, type, answer, anslen);
  210.         if (ret > 0)
  211.             return (ret);
  212.         saved_herrno = h_errno;
  213.         tried_as_is++;
  214.     }
  215.  
  216.     /*
  217.      * We do at least one level of search if
  218.      *    - there is no dot and RES_DEFNAME is set, or
  219.      *    - there is at least one dot, there is no trailing dot,
  220.      *      and RES_DNSRCH is set.
  221.      */
  222.     if ((!dots && (_res.options & RES_DEFNAMES)) ||
  223.         (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
  224.         for (domain = _res.dnsrch; *domain; domain++) {
  225.             int done = 0;
  226.  
  227.             ret = res_querydomain(name, *domain, class, type,
  228.                 answer, anslen);
  229.             if (ret > 0)
  230.                 return (ret);
  231.             /*
  232.              * If no server present, give up.
  233.              * If name isn't found in this domain,
  234.              * keep trying higher domains in the search list
  235.              * (if that's enabled).
  236.              * On a NO_DATA error, keep trying, otherwise
  237.              * a wildcard entry of another type could keep us
  238.              * from finding this entry higher in the domain.
  239.              * If we get some other error (negative answer or
  240.              * server failure), then stop searching up,
  241.              * but try the input name below in case it's
  242.              * fully-qualified.
  243.              */
  244.             if (errno == ECONNREFUSED) {
  245.                 h_errno = TRY_AGAIN;
  246.                 return (-1);
  247.             }
  248.  
  249.             switch (h_errno) {
  250.             case NO_DATA:
  251.                 got_nodata++;
  252.                 /* FALLTHROUGH */
  253.             case HOST_NOT_FOUND:
  254.                 /* keep trying */
  255.                 break;
  256.             default:
  257.                 /* anything else implies that we're done */
  258.                 done++;
  259.             }
  260.             /*
  261.              * if we got here for some reason other than DNSRCH,
  262.              * we only wanted one iteration of the loop, so stop.
  263.              */
  264.             if (!(_res.options & RES_DNSRCH))
  265.                     done++;
  266.  
  267.             if (done)
  268.                 break;
  269.         }
  270.     }
  271.  
  272.     /*
  273.      * if we have not already tried the name "as is", do that now.
  274.      * note that we do this regardless of how many dots were in the
  275.      * name or whether it ends with a dot.
  276.      */
  277.     if (!tried_as_is) {
  278.         ret = res_querydomain(name, NULL, class, type, answer, anslen);
  279.         if (ret > 0)
  280.             return (ret);
  281.         saved_herrno = h_errno;
  282.     }
  283.  
  284.     /*
  285.      * if we got here, we didn't satisfy the search.
  286.      * if we did an initial full query, return that query's h_errno
  287.      * (note that we wouldn't be here if that query had succeeded).
  288.      * else if we ever got a nodata, send that back as the reason.
  289.      * else send back meaningless h_errno, that being the one from
  290.      * the last DNSRCH we did.
  291.      */
  292.     if (saved_herrno != -1)
  293.         h_errno = saved_herrno;
  294.     else if (got_nodata)
  295.         h_errno = NO_DATA;
  296.     return (-1);
  297. }
  298.  
  299. /*
  300.  * Perform a call on res_query on the concatenation of name and domain,
  301.  * removing a trailing dot from name if domain is NULL.
  302.  */
  303. int
  304. res_querydomain(name, domain, class, type, answer, anslen)
  305.     const char *name;
  306.     char *domain;
  307.     int class, type;    /* class and type of query */
  308.     u_char *answer;        /* buffer to put answer */
  309.     int anslen;        /* size of answer */
  310. {
  311.     char nbuf[2*MAXDNAME+2];
  312.     char *longname = nbuf;
  313.     int n;
  314.  
  315. #ifdef DEBUG
  316.     if (_res.options & RES_DEBUG)
  317.         printf(";; res_querydomain(%s, %s, %d, %d)\n",
  318.             name, domain, class, type);
  319. #endif
  320.     if (domain == NULL) {
  321.         /*
  322.          * Check for trailing '.';
  323.          * copy without '.' if present.
  324.          */
  325.         n = strlen(name) - 1;
  326.         if (n != (0 - 1) && name[n] == '.' && n < sizeof(nbuf) - 1) {
  327.             bcopy(name, nbuf, n);
  328.             nbuf[n] = '\0';
  329.         } else
  330.             longname = (char *)name;
  331.     } else
  332.         (void)sprintf(nbuf, "%.*s.%.*s",
  333.             MAXDNAME, name, MAXDNAME, domain);
  334.  
  335.     return (res_query(longname, class, type, answer, anslen));
  336. }
  337.  
  338. char *
  339. __hostalias(name)
  340.     register const char *name;
  341. {
  342.     register char *cp1, *cp2;
  343.     FILE *fp;
  344.     char *file, *getenv(), *strcpy(), *strncpy();
  345.     char buf[BUFSIZ];
  346.     static char abuf[MAXDNAME];
  347.  
  348.     file = getenv("HOSTALIASES");
  349.     if (file == NULL || (fp = fopen(file, "r")) == NULL)
  350.         return (NULL);
  351.     buf[sizeof(buf) - 1] = '\0';
  352.     while (fgets(buf, sizeof(buf), fp)) {
  353.         for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1);
  354.         if (!*cp1)
  355.             break;
  356.         *cp1 = '\0';
  357.         if (!strcasecmp(buf, name)) {
  358.             while (isspace(*++cp1));
  359.             if (!*cp1)
  360.                 break;
  361.             for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2);
  362.             abuf[sizeof(abuf) - 1] = *cp2 = '\0';
  363.             (void)strncpy(abuf, cp1, sizeof(abuf) - 1);
  364.             fclose(fp);
  365.             return (abuf);
  366.         }
  367.     }
  368.     fclose(fp);
  369.     return (NULL);
  370. }
  371.