integrate changes from bind 4.9 (most of them); continue to use address
[unix-history] / usr / src / lib / libc / net / res_query.c
index 297c4cb..7b39e1f 100644 (file)
@@ -1,30 +1,46 @@
-/*
+/*-
  * Copyright (c) 1988 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1988 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at Berkeley. The name of the University
- * may not be used to endorse or promote products derived from this
- * software without specific prior written permission. This software
- * is provided ``as is'' without express or implied warranty.
+ * %sccs.include.redist.c%
+ * -
+ * Portions Copyright (c) 1993 by Digital Equipment Corporation.
+ * 
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies, and that
+ * the name of Digital Equipment Corporation not be used in advertising or
+ * publicity pertaining to distribution of the document or software without
+ * specific, written prior permission.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
+ * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+ * SOFTWARE.
+ * -
+ * --Copyright--
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)res_query.c        5.3 (Berkeley) %G%";
+static char sccsid[] = "@(#)res_query.c        5.12 (Berkeley) %G%";
+static char rcsid[] = "$Id: res_query.c,v 1.1 1993/06/01 09:42:14 vixie Exp vixie $";
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
-#include <sys/socket.h>
 #include <netinet/in.h>
 #include <netinet/in.h>
-#include <ctype.h>
-#include <netdb.h>
-#include <stdio.h>
-#include <errno.h>
-#include <strings.h>
 #include <arpa/inet.h>
 #include <arpa/nameser.h>
 #include <arpa/inet.h>
 #include <arpa/nameser.h>
+#include <netdb.h>
 #include <resolv.h>
 #include <resolv.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
 
 #if PACKETSZ > 1024
 #define MAXPACKET      PACKETSZ
 
 #if PACKETSZ > 1024
 #define MAXPACKET      PACKETSZ
@@ -32,7 +48,6 @@ static char sccsid[] = "@(#)res_query.c       5.3 (Berkeley) %G%";
 #define MAXPACKET      1024
 #endif
 
 #define MAXPACKET      1024
 #endif
 
-extern int errno;
 int h_errno;
 
 /*
 int h_errno;
 
 /*
@@ -58,7 +73,7 @@ res_query(name, class, type, answer, anslen)
                return (-1);
 #ifdef DEBUG
        if (_res.options & RES_DEBUG)
                return (-1);
 #ifdef DEBUG
        if (_res.options & RES_DEBUG)
-               printf("res_query(%s, %d, %d)\n", name, class, type);
+               printf(";; res_query(%s, %d, %d)\n", name, class, type);
 #endif
        n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
            buf, sizeof(buf));
 #endif
        n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
            buf, sizeof(buf));
@@ -66,16 +81,16 @@ res_query(name, class, type, answer, anslen)
        if (n <= 0) {
 #ifdef DEBUG
                if (_res.options & RES_DEBUG)
        if (n <= 0) {
 #ifdef DEBUG
                if (_res.options & RES_DEBUG)
-                       printf("res_query: mkquery failed\n");
+                       printf(";; res_query: mkquery failed\n");
 #endif
                h_errno = NO_RECOVERY;
                return (n);
        }
 #endif
                h_errno = NO_RECOVERY;
                return (n);
        }
-       n = res_send(buf, n, answer, anslen);
+       n = res_send(buf, n, (char *)answer, anslen);
        if (n < 0) {
 #ifdef DEBUG
                if (_res.options & RES_DEBUG)
        if (n < 0) {
 #ifdef DEBUG
                if (_res.options & RES_DEBUG)
-                       printf("res_query: send error\n");
+                       printf(";; res_query: send error\n");
 #endif
                h_errno = TRY_AGAIN;
                return(n);
 #endif
                h_errno = TRY_AGAIN;
                return(n);
@@ -85,7 +100,7 @@ res_query(name, class, type, answer, anslen)
        if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
 #ifdef DEBUG
                if (_res.options & RES_DEBUG)
        if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
 #ifdef DEBUG
                if (_res.options & RES_DEBUG)
-                       printf("rcode = %d, ancount=%d\n", hp->rcode,
+                       printf(";; rcode = %d, ancount=%d\n", hp->rcode,
                            ntohs(hp->ancount));
 #endif
                switch (hp->rcode) {
                            ntohs(hp->ancount));
 #endif
                switch (hp->rcode) {
@@ -118,6 +133,7 @@ res_query(name, class, type, answer, anslen)
  * Only useful for queries in the same name hierarchy as the local host
  * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
  */
  * Only useful for queries in the same name hierarchy as the local host
  * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
  */
+int
 res_search(name, class, type, answer, anslen)
        char *name;             /* domain name */
        int class, type;        /* class and type of query */
 res_search(name, class, type, answer, anslen)
        char *name;             /* domain name */
        int class, type;        /* class and type of query */
@@ -126,21 +142,28 @@ res_search(name, class, type, answer, anslen)
 {
        register char *cp, **domain;
        int n, ret, got_nodata = 0;
 {
        register char *cp, **domain;
        int n, ret, got_nodata = 0;
-       char *hostalias();
+       char *__hostalias();
 
        if ((_res.options & RES_INIT) == 0 && res_init() == -1)
                return (-1);
 
        errno = 0;
 
        if ((_res.options & RES_INIT) == 0 && res_init() == -1)
                return (-1);
 
        errno = 0;
-       h_errno = HOST_NOT_FOUND;               /* default, if we never query */
+       h_errno = HOST_NOT_FOUND;       /* default, if we never query */
        for (cp = name, n = 0; *cp; cp++)
                if (*cp == '.')
                        n++;
        for (cp = name, n = 0; *cp; cp++)
                if (*cp == '.')
                        n++;
-       if (n == 0 && (cp = hostalias(name)))
+       if (n == 0 && (cp = __hostalias(name)))
                return (res_query(cp, class, type, answer, anslen));
 
                return (res_query(cp, class, type, answer, anslen));
 
-       if ((n == 0 || *--cp != '.') && (_res.options & RES_DEFNAMES))
-           for (domain = _res.dnsrch; *domain; domain++) {
+       /*
+        * We do at least one level of search if
+        *      - there is no dot and RES_DEFNAME is set, or
+        *      - there is at least one dot, there is no trailing dot,
+        *        and RES_DNSRCH is set.
+        */
+       if ((n == 0 && _res.options & RES_DEFNAMES) ||
+          (n != 0 && *--cp != '.' && _res.options & RES_DNSRCH))
+            for (domain = _res.dnsrch; *domain; domain++) {
                ret = res_querydomain(name, *domain, class, type,
                    answer, anslen);
                if (ret > 0)
                ret = res_querydomain(name, *domain, class, type,
                    answer, anslen);
                if (ret > 0)
@@ -153,8 +176,8 @@ res_search(name, class, type, answer, anslen)
                 * On a NO_DATA error, keep trying, otherwise
                 * a wildcard entry of another type could keep us
                 * from finding this entry higher in the domain.
                 * On a NO_DATA error, keep trying, otherwise
                 * a wildcard entry of another type could keep us
                 * from finding this entry higher in the domain.
-                * If we get some other error (non-authoritative negative
-                * answer or server failure), then stop searching up,
+                * If we get some other error (negative answer or
+                * server failure), then stop searching up,
                 * but try the input name below in case it's fully-qualified.
                 */
                if (errno == ECONNREFUSED) {
                 * but try the input name below in case it's fully-qualified.
                 */
                if (errno == ECONNREFUSED) {
@@ -166,15 +189,17 @@ res_search(name, class, type, answer, anslen)
                if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) ||
                    (_res.options & RES_DNSRCH) == 0)
                        break;
                if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) ||
                    (_res.options & RES_DNSRCH) == 0)
                        break;
-               h_errno = 0;
        }
        /*
         * If the search/default failed, try the name as fully-qualified,
         * but only if it contained at least one dot (even trailing).
        }
        /*
         * If the search/default failed, try the name as fully-qualified,
         * but only if it contained at least one dot (even trailing).
+        * This is purely a heuristic; we assume that any reasonable query
+        * about a top-level domain (for servers, SOA, etc) will not use
+        * res_search.
         */
         */
-       if (n)
-               return (res_querydomain(name, (char *)NULL, class, type,
-                   answer, anslen));
+       if (n && (ret = res_querydomain(name, (char *)NULL, class, type,
+           answer, anslen)) > 0)
+               return (ret);
        if (got_nodata)
                h_errno = NO_DATA;
        return (-1);
        if (got_nodata)
                h_errno = NO_DATA;
        return (-1);
@@ -196,7 +221,7 @@ res_querydomain(name, domain, class, type, answer, anslen)
 
 #ifdef DEBUG
        if (_res.options & RES_DEBUG)
 
 #ifdef DEBUG
        if (_res.options & RES_DEBUG)
-               printf("res_querydomain(%s, %s, %d, %d)\n",
+               printf(";; res_querydomain(%s, %s, %d, %d)\n",
                    name, domain, class, type);
 #endif
        if (domain == NULL) {
                    name, domain, class, type);
 #endif
        if (domain == NULL) {
@@ -218,8 +243,8 @@ res_querydomain(name, domain, class, type, answer, anslen)
 }
 
 char *
 }
 
 char *
-hostalias(name)
-       register char *name;
+__hostalias(name)
+       register const char *name;
 {
        register char *C1, *C2;
        FILE *fp;
 {
        register char *C1, *C2;
        FILE *fp;