integrate changes from bind 4.9 (most of them); continue to use address
authorMike Karels <karels@ucbvax.Berkeley.EDU>
Thu, 3 Jun 1993 08:05:01 +0000 (00:05 -0800)
committerMike Karels <karels@ucbvax.Berkeley.EDU>
Thu, 3 Jun 1993 08:05:01 +0000 (00:05 -0800)
INADDR_ANY rather than 127.1; convert u_short to u_int16_t as appropriate

SCCS-vsn: lib/libc/net/gethostnamadr.c 6.49
SCCS-vsn: lib/libc/net/herror.c 6.7
SCCS-vsn: lib/libc/net/res_comp.c 6.23
SCCS-vsn: lib/libc/net/res_debug.c 5.37
SCCS-vsn: lib/libc/net/res_init.c 6.16
SCCS-vsn: lib/libc/net/res_mkquery.c 6.17
SCCS-vsn: lib/libc/net/res_query.c 5.12
SCCS-vsn: lib/libc/net/res_send.c 6.28

usr/src/lib/libc/net/gethostnamadr.c
usr/src/lib/libc/net/herror.c
usr/src/lib/libc/net/res_comp.c
usr/src/lib/libc/net/res_debug.c
usr/src/lib/libc/net/res_init.c
usr/src/lib/libc/net/res_mkquery.c
usr/src/lib/libc/net/res_query.c
usr/src/lib/libc/net/res_send.c

index 492e856..9d938ee 100644 (file)
@@ -1,12 +1,33 @@
-/*
+/*-
  * Copyright (c) 1985, 1988 Regents of the University of California.
  * All rights reserved.
  *
  * %sccs.include.redist.c%
  * Copyright (c) 1985, 1988 Regents of the University of California.
  * All rights reserved.
  *
  * %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[] = "@(#)gethostnamadr.c    6.48 (Berkeley) %G%";
+static char sccsid[] = "@(#)gethostnamadr.c    6.49 (Berkeley) %G%";
+static char rcsid[] = "$Id: gethnamaddr.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel $";
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
@@ -48,11 +69,11 @@ typedef union {
 } querybuf;
 
 typedef union {
 } querybuf;
 
 typedef union {
-    long al;
+    int32_t al;
     char ac;
 } align;
 
     char ac;
 } align;
 
-int h_errno;
+extern int h_errno;
 
 static struct hostent *
 getanswer(answer, anslen, iquery)
 
 static struct hostent *
 getanswer(answer, anslen, iquery)
@@ -118,11 +139,11 @@ getanswer(answer, anslen, iquery)
                        break;
                cp += n;
                type = _getshort(cp);
                        break;
                cp += n;
                type = _getshort(cp);
-               cp += sizeof(u_short);
+               cp += sizeof(u_int16_t);
                class = _getshort(cp);
                class = _getshort(cp);
-               cp += sizeof(u_short) + sizeof(u_long);
+               cp += sizeof(u_int16_t) + sizeof(u_int32_t);
                n = _getshort(cp);
                n = _getshort(cp);
-               cp += sizeof(u_short);
+               cp += sizeof(u_int16_t);
                if (type == T_CNAME) {
                        cp += n;
                        if (ap >= &host_aliases[MAXALIASES-1])
                if (type == T_CNAME) {
                        cp += n;
                        if (ap >= &host_aliases[MAXALIASES-1])
@@ -170,7 +191,7 @@ getanswer(answer, anslen, iquery)
                        }
                }
 
                        }
                }
 
-               bp += sizeof(align) - ((u_long)bp % sizeof(align));
+               bp += sizeof(align) - ((u_int32_t)bp % sizeof(align));
 
                if (bp + n >= &hostbuf[sizeof(hostbuf)]) {
 #ifdef DEBUG
 
                if (bp + n >= &hostbuf[sizeof(hostbuf)]) {
 #ifdef DEBUG
@@ -229,7 +250,7 @@ gethostbyname(name)
                                host.h_aliases = host_aliases;
                                host_aliases[0] = NULL;
                                host.h_addrtype = AF_INET;
                                host.h_aliases = host_aliases;
                                host_aliases[0] = NULL;
                                host.h_addrtype = AF_INET;
-                               host.h_length = sizeof(u_long);
+                               host.h_length = sizeof(u_int32_t);
                                h_addr_ptrs[0] = (char *)&host_addr;
                                h_addr_ptrs[1] = (char *)0;
 #if BSD >= 43 || defined(h_addr)       /* new-style hostent structure */
                                h_addr_ptrs[0] = (char *)&host_addr;
                                h_addr_ptrs[1] = (char *)0;
 #if BSD >= 43 || defined(h_addr)       /* new-style hostent structure */
@@ -298,6 +319,7 @@ gethostbyaddr(addr, len, type)
        return(hp);
 }
 
        return(hp);
 }
 
+void
 _sethtent(f)
        int f;
 {
 _sethtent(f)
        int f;
 {
@@ -308,6 +330,7 @@ _sethtent(f)
        stayopen |= f;
 }
 
        stayopen |= f;
 }
 
+void
 _endhtent()
 {
        if (hostf && !stayopen) {
 _endhtent()
 {
        if (hostf && !stayopen) {
@@ -342,8 +365,8 @@ again:
        host.h_addr_list = host_addrs;
 #endif
        host.h_addr = hostaddr;
        host.h_addr_list = host_addrs;
 #endif
        host.h_addr = hostaddr;
-       *((u_long *)host.h_addr) = inet_addr(p);
-       host.h_length = sizeof (u_long);
+       *((u_int32_t *)host.h_addr) = inet_addr(p);
+       host.h_length = sizeof (u_int32_t);
        host.h_addrtype = AF_INET;
        while (*cp == ' ' || *cp == '\t')
                cp++;
        host.h_addrtype = AF_INET;
        while (*cp == ' ' || *cp == '\t')
                cp++;
index ff8ef85..b730b98 100644 (file)
@@ -1,12 +1,33 @@
-/*
+/*-
  * Copyright (c) 1987 Regents of the University of California.
  * All rights reserved.
  *
  * %sccs.include.redist.c%
  * Copyright (c) 1987 Regents of the University of California.
  * All rights reserved.
  *
  * %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[] = "@(#)herror.c   6.6 (Berkeley) %G%";
+static char sccsid[] = "@(#)herror.c   6.7 (Berkeley) %G%";
+static char rcsid[] = "$Id: herror.c,v 4.9.1.1 1993/05/02 23:14:35 vixie Rel $";
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
@@ -53,3 +74,10 @@ herror(s)
        v->iov_len = 1;
        writev(STDERR_FILENO, iov, (v - iov) + 1);
 }
        v->iov_len = 1;
        writev(STDERR_FILENO, iov, (v - iov) + 1);
 }
+
+char *
+hstrerror(err)
+       int err;
+{
+       return (u_int)err < h_nerr ? h_errlist[err] : "Unknown resolver error";
+}
index f1b2826..8dd1f75 100644 (file)
@@ -1,12 +1,33 @@
-/*
+/*-
  * Copyright (c) 1985 Regents of the University of California.
  * All rights reserved.
  *
  * %sccs.include.redist.c%
  * Copyright (c) 1985 Regents of the University of California.
  * All rights reserved.
  *
  * %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_comp.c 6.22 (Berkeley) %G%";
+static char sccsid[] = "@(#)res_comp.c 6.23 (Berkeley) %G%";
+static char rcsid[] = "$Id: res_comp.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel $";
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
@@ -15,7 +36,7 @@ static char sccsid[] = "@(#)res_comp.c        6.22 (Berkeley) %G%";
 #include <resolv.h>
 #include <stdio.h>
 
 #include <resolv.h>
 #include <stdio.h>
 
-static dn_find();
+static int dn_find();
 
 /*
  * Expand compressed domain name 'comp_dn' to full domain name.
 
 /*
  * Expand compressed domain name 'comp_dn' to full domain name.
@@ -213,7 +234,7 @@ __dn_skipname(comp_dn, eom)
  * dnptrs is the pointer to the first name on the list,
  * not the pointer to the start of the message.
  */
  * dnptrs is the pointer to the first name on the list,
  * not the pointer to the start of the message.
  */
-static
+static int
 dn_find(exp_dn, msg, dnptrs, lastdnptr)
        u_char *exp_dn, *msg;
        u_char **dnptrs, **lastdnptr;
 dn_find(exp_dn, msg, dnptrs, lastdnptr)
        u_char *exp_dn, *msg;
        u_char **dnptrs, **lastdnptr;
@@ -269,55 +290,40 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
 
 u_short
 _getshort(msgp)
 
 u_short
 _getshort(msgp)
-       u_char *msgp;
+       register u_char *msgp;
 {
 {
-       register u_char *p = (u_char *) msgp;
-#ifdef vax
-       /*
-        * vax compiler doesn't put shorts in registers
-        */
-       register u_long u;
-#else
-       register u_short u;
-#endif
+       register u_int16_t u;
 
 
-       u = *p++ << 8;
-       return ((u_short)(u | *p));
+       GETSHORT(u, msgp);
+       return (u);
 }
 
 }
 
-u_long
+u_int32_t
 _getlong(msgp)
 _getlong(msgp)
-       u_char *msgp;
+       register u_char *msgp;
 {
 {
-       register u_char *p = (u_char *) msgp;
-       register u_long u;
+       register u_int32_t u;
 
 
-       u = *p++; u <<= 8;
-       u |= *p++; u <<= 8;
-       u |= *p++; u <<= 8;
-       return (u | *p);
+       GETLONG(u, msgp);
+       return (u);
 }
 
 void
 }
 
 void
-#ifdef __STDC__
+#if defined(__STDC__) || defined(__cplusplus)
 __putshort(register u_short s, register u_char *msgp)
 #else
 __putshort(s, msgp)
 __putshort(register u_short s, register u_char *msgp)
 #else
 __putshort(s, msgp)
-       register u_short s;
+       register u_int16_t s;
        register u_char *msgp;
 #endif
 {
        register u_char *msgp;
 #endif
 {
-       msgp[1] = s;
-       msgp[0] = s >> 8;
+       PUTSHORT(s, msgp);
 }
 
 void
 __putlong(l, msgp)
 }
 
 void
 __putlong(l, msgp)
-       register u_long l;
+       register u_int32_t l;
        register u_char *msgp;
 {
        register u_char *msgp;
 {
-       msgp[3] = l;
-       msgp[2] = (l >>= 8);
-       msgp[1] = (l >>= 8);
-       msgp[0] = l >> 8;
+       PUTLONG(l, msgp);
 }
 }
index bac8d0b..34096b2 100644 (file)
@@ -3,12 +3,30 @@
  * All rights reserved.
  *
  * %sccs.include.redist.c%
  * All rights reserved.
  *
  * %sccs.include.redist.c%
- *
- *     @(#)res_debug.c 5.36 (Berkeley) %G%
+ * -
+ * 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_debug.c        5.36 (Berkeley) %G%";
+static char sccsid[] = "@(#)res_debug.c        5.37 (Berkeley) %G%";
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
@@ -21,7 +39,8 @@ static char sccsid[] = "@(#)res_debug.c       5.36 (Berkeley) %G%";
 
 void __fp_query();
 char *__p_class(), *__p_time(), *__p_type();
 
 void __fp_query();
 char *__p_class(), *__p_time(), *__p_type();
-static char *p_cdname(), *p_rr();
+char *p_cdname(), *p_fqname(), *p_rr();
+static char *p_option __P((u_int32_t));
 
 char *_res_opcodes[] = {
        "QUERY",
 
 char *_res_opcodes[] = {
        "QUERY",
@@ -61,10 +80,135 @@ char *_res_resultcodes[] = {
        "NOCHANGE",
 };
 
        "NOCHANGE",
 };
 
+static char retbuf[16];
+
+static char *
+dewks(wks)
+       int wks;
+{
+       switch (wks) {
+       case 5: return("rje");
+       case 7: return("echo");
+       case 9: return("discard");
+       case 11: return("systat");
+       case 13: return("daytime");
+       case 15: return("netstat");
+       case 17: return("qotd");
+       case 19: return("chargen");
+       case 20: return("ftp-data");
+       case 21: return("ftp");
+       case 23: return("telnet");
+       case 25: return("smtp");
+       case 37: return("time");
+       case 39: return("rlp");
+       case 42: return("name");
+       case 43: return("whois");
+       case 53: return("domain");
+       case 57: return("apts");
+       case 59: return("apfs");
+       case 67: return("bootps");
+       case 68: return("bootpc");
+       case 69: return("tftp");
+       case 77: return("rje");
+       case 79: return("finger");
+       case 87: return("link");
+       case 95: return("supdup");
+       case 100: return("newacct");
+       case 101: return("hostnames");
+       case 102: return("iso-tsap");
+       case 103: return("x400");
+       case 104: return("x400-snd");
+       case 105: return("csnet-ns");
+       case 109: return("pop-2");
+       case 111: return("sunrpc");
+       case 113: return("auth");
+       case 115: return("sftp");
+       case 117: return("uucp-path");
+       case 119: return("nntp");
+       case 121: return("erpc");
+       case 123: return("ntp");
+       case 133: return("statsrv");
+       case 136: return("profile");
+       case 144: return("NeWS");
+       case 161: return("snmp");
+       case 162: return("snmp-trap");
+       case 170: return("print-srv");
+       default: (void) sprintf(retbuf, "%d", wks); return(retbuf);
+       }
+}
+
+static char *
+deproto(protonum)
+       int protonum;
+{
+       switch (protonum) {
+       case 1: return("icmp");
+       case 2: return("igmp");
+       case 3: return("ggp");
+       case 5: return("st");
+       case 6: return("tcp");
+       case 7: return("ucl");
+       case 8: return("egp");
+       case 9: return("igp");
+       case 11: return("nvp-II");
+       case 12: return("pup");
+       case 16: return("chaos");
+       case 17: return("udp");
+       default: (void) sprintf(retbuf, "%d", protonum); return(retbuf);
+       }
+}
+
+static char *
+do_rrset(msg, cp, cnt, pflag, file, hs)
+       int cnt, pflag;
+       char *cp,*msg, *hs;
+       FILE *file;
+{
+       int n;
+       int sflag;
+       /*
+        * Print  answer records
+        */
+       sflag = (_res.pfcode & pflag);
+       if (n = ntohs(cnt)) {
+               if ((!_res.pfcode) || ((sflag) && (_res.pfcode & RES_PRF_HEAD1)))
+                       fprintf(file, hs);
+               while (--n >= 0) {
+                       cp = p_rr(cp, msg, file);
+                       if ((cp-msg) > PACKETSZ)
+                               return (NULL);
+               }
+               if ((!_res.pfcode) || ((sflag) && (_res.pfcode & RES_PRF_HEAD1)))
+                       putc('\n', file);
+       }
+       return(cp);
+}
+
 __p_query(msg)
        char *msg;
 {
 __p_query(msg)
        char *msg;
 {
-       __fp_query(msg,stdout);
+       __fp_query(msg, stdout);
+}
+
+/*
+ * Print the current options.
+ * This is intended to be primarily a debugging routine.
+ */
+void
+__fp_resstat(statp, file)
+       struct __res_state *statp;
+       FILE *file;
+{
+       int bit;
+
+       fprintf(file, ";; res options:");
+       if (!statp)
+               statp = &_res;
+       for (bit = 0;  bit < 32;  bit++) {      /* XXX 32 - bad assumption! */
+               if (statp->options & (1<<bit))
+                       fprintf(file, " %s", p_option(1<<bit));
+       }
+       putc('\n', file);
 }
 
 /*
 }
 
 /*
@@ -85,83 +229,87 @@ __fp_query(msg,file)
         */
        hp = (HEADER *)msg;
        cp = msg + sizeof(HEADER);
         */
        hp = (HEADER *)msg;
        cp = msg + sizeof(HEADER);
-       fprintf(file,"HEADER:\n");
-       fprintf(file,"\topcode = %s", _res_opcodes[hp->opcode]);
-       fprintf(file,", id = %d", ntohs(hp->id));
-       fprintf(file,", rcode = %s\n", _res_resultcodes[hp->rcode]);
-       fprintf(file,"\theader flags: ");
-       if (hp->qr)
-               fprintf(file," qr");
-       if (hp->aa)
-               fprintf(file," aa");
-       if (hp->tc)
-               fprintf(file," tc");
-       if (hp->rd)
-               fprintf(file," rd");
-       if (hp->ra)
-               fprintf(file," ra");
-       if (hp->pr)
-               fprintf(file," pr");
-       fprintf(file,"\n\tqdcount = %d", ntohs(hp->qdcount));
-       fprintf(file,", ancount = %d", ntohs(hp->ancount));
-       fprintf(file,", nscount = %d", ntohs(hp->nscount));
-       fprintf(file,", arcount = %d\n\n", ntohs(hp->arcount));
+       if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX) || hp->rcode) {
+               fprintf(file,";; ->>HEADER<<- opcode: %s, status: %s, id: %d",
+                       _res_opcodes[hp->opcode],
+                       _res_resultcodes[hp->rcode],
+                       ntohs(hp->id));
+               putc('\n', file);
+       }
+       putc(';', file);
+       if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD2)) {
+               fprintf(file,"; flags:");
+               if (hp->qr)
+                       fprintf(file," qr");
+               if (hp->aa)
+                       fprintf(file," aa");
+               if (hp->tc)
+                       fprintf(file," tc");
+               if (hp->rd)
+                       fprintf(file," rd");
+               if (hp->ra)
+                       fprintf(file," ra");
+               if (hp->pr)
+                       fprintf(file," pr");
+       }
+       if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD1)) {
+               fprintf(file,"; Ques: %d", ntohs(hp->qdcount));
+               fprintf(file,", Ans: %d", ntohs(hp->ancount));
+               fprintf(file,", Auth: %d", ntohs(hp->nscount));
+               fprintf(file,", Addit: %d\n", ntohs(hp->arcount));
+       }
+#if 0
+       if (_res.pfcode & (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1)) {
+               putc('\n',file);
+       }
+#endif
        /*
         * Print question records.
         */
        if (n = ntohs(hp->qdcount)) {
        /*
         * Print question records.
         */
        if (n = ntohs(hp->qdcount)) {
-               fprintf(file,"QUESTIONS:\n");
+               if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
+                       fprintf(file,";; QUESTIONS:\n");
                while (--n >= 0) {
                while (--n >= 0) {
-                       fprintf(file,"\t");
+                       fprintf(file,";;\t");
                        cp = p_cdname(cp, msg, file);
                        if (cp == NULL)
                                return;
                        cp = p_cdname(cp, msg, file);
                        if (cp == NULL)
                                return;
-                       fprintf(file,", type = %s", __p_type(_getshort(cp)));
-                       cp += sizeof(u_short);
-                       fprintf(file,
-                           ", class = %s\n\n", __p_class(_getshort(cp)));
-                       cp += sizeof(u_short);
+                       if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
+                               fprintf(file, ", type = %s",
+                                       __p_type(_getshort(cp)));
+                       cp += sizeof(u_int16_t);
+                       if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
+                               fprintf(file, ", class = %s\n\n",
+                                       __p_class(_getshort(cp)));
+                       cp += sizeof(u_int16_t);
                }
        }
        /*
         * Print authoritative answer records
         */
                }
        }
        /*
         * Print authoritative answer records
         */
-       if (n = ntohs(hp->ancount)) {
-               fprintf(file,"ANSWERS:\n");
-               while (--n >= 0) {
-                       fprintf(file,"\t");
-                       cp = p_rr(cp, msg, file);
-                       if (cp == NULL)
-                               return;
-               }
-       }
+       cp = do_rrset(msg, cp, hp->ancount, RES_PRF_ANS, file,
+                     ";; ANSWERS:\n");
+       if (cp == NULL)
+               return;
+
        /*
         * print name server records
         */
        /*
         * print name server records
         */
-       if (n = ntohs(hp->nscount)) {
-               fprintf(file,"NAME SERVERS:\n");
-               while (--n >= 0) {
-                       fprintf(file,"\t");
-                       cp = p_rr(cp, msg, file);
-                       if (cp == NULL)
-                               return;
-               }
-       }
+       cp = do_rrset(msg, cp, hp->nscount, RES_PRF_AUTH, file,
+                     ";; AUTHORITY RECORDS:\n");
+       if (!cp)
+               return;
+
        /*
         * print additional records
         */
        /*
         * print additional records
         */
-       if (n = ntohs(hp->arcount)) {
-               fprintf(file,"ADDITIONAL RECORDS:\n");
-               while (--n >= 0) {
-                       fprintf(file,"\t");
-                       cp = p_rr(cp, msg, file);
-                       if (cp == NULL)
-                               return;
-               }
-       }
+       cp = do_rrset(msg, cp, hp->arcount, RES_PRF_ADD, file,
+                     ";; ADDITIONAL RECORDS:\n");
+       if (!cp)
+               return;
 }
 
 }
 
-static char *
+char *
 p_cdname(cp, msg, file)
        char *cp, *msg;
        FILE *file;
 p_cdname(cp, msg, file)
        char *cp, *msg;
        FILE *file;
@@ -169,21 +317,41 @@ p_cdname(cp, msg, file)
        char name[MAXDNAME];
        int n;
 
        char name[MAXDNAME];
        int n;
 
-       if ((n = dn_expand((u_char *)msg, (u_char *)msg + 512, (u_char *)cp,
-           (u_char *)name, sizeof(name))) < 0)
+       if ((n = dn_expand((u_char *)msg, (u_char *)msg + MAXCDNAME,
+           (u_char *)cp, (u_char *)name, sizeof(name))) < 0)
+               return (NULL);
+       if (name[0] == '\0')
+               putc('.', file);
+       else
+               fputs(name, file);
+       return (cp + n);
+}
+
+char *
+p_fqname(cp, msg, file)
+       char *cp, *msg;
+       FILE *file;
+{
+       char name[MAXDNAME];
+       int n, len;
+
+       if ((n = dn_expand((u_char *)msg, (u_char *)msg + MAXCDNAME,
+           (u_char *)cp, (u_char *)name, sizeof(name))) < 0)
                return (NULL);
        if (name[0] == '\0') {
                return (NULL);
        if (name[0] == '\0') {
-               name[0] = '.';
-               name[1] = '\0';
+               putc('.', file);
+       } else {
+               fputs(name, file);
+               if (name[strlen(name) - 1] != '.')
+                       putc('.', file);
        }
        }
-       fputs(name, file);
        return (cp + n);
 }
 
 /*
  * Print resource record fields in human readable form.
  */
        return (cp + n);
 }
 
 /*
  * Print resource record fields in human readable form.
  */
-static char *
+char *
 p_rr(cp, msg, file)
        char *cp, *msg;
        FILE *file;
 p_rr(cp, msg, file)
        char *cp, *msg;
        FILE *file;
@@ -191,18 +359,25 @@ p_rr(cp, msg, file)
        int type, class, dlen, n, c;
        struct in_addr inaddr;
        char *cp1, *cp2;
        int type, class, dlen, n, c;
        struct in_addr inaddr;
        char *cp1, *cp2;
+       u_int32_t tmpttl, t;
+       int lcnt;
 
 
-       if ((cp = p_cdname(cp, msg, file)) == NULL)
+       if ((cp = p_fqname(cp, msg, file)) == NULL)
                return (NULL);                  /* compression error */
                return (NULL);                  /* compression error */
-       fprintf(file,"\n\ttype = %s", __p_type(type = _getshort(cp)));
-       cp += sizeof(u_short);
-       fprintf(file,", class = %s", __p_class(class = _getshort(cp)));
-       cp += sizeof(u_short);
-       fprintf(file,", ttl = %s", __p_time(_getlong(cp)));
-       cp += sizeof(u_long);
-       fprintf(file,", dlen = %d\n", dlen = _getshort(cp));
-       cp += sizeof(u_short);
+       type = _getshort(cp);
+       cp += sizeof(u_int16_t);
+       class = _getshort(cp);
+       cp += sizeof(u_int16_t);
+       tmpttl = _getlong(cp);
+       cp += sizeof(u_int32_t);
+       dlen = _getshort(cp);
+       cp += sizeof(u_int16_t);
        cp1 = cp;
        cp1 = cp;
+       if ((!_res.pfcode) || (_res.pfcode & RES_PRF_TTLID))
+               fprintf(file, "\t%lu", tmpttl);
+       if ((!_res.pfcode) || (_res.pfcode & RES_PRF_CLASS))
+               fprintf(file, "\t%s", __p_class(class));
+       fprintf(file, "\t%s", __p_type(type));
        /*
         * Print type specific data, if appropriate
         */
        /*
         * Print type specific data, if appropriate
         */
@@ -213,16 +388,21 @@ p_rr(cp, msg, file)
                case C_HS:
                        bcopy(cp, (char *)&inaddr, sizeof(inaddr));
                        if (dlen == 4) {
                case C_HS:
                        bcopy(cp, (char *)&inaddr, sizeof(inaddr));
                        if (dlen == 4) {
-                               fprintf(file,"\tinternet address = %s\n",
-                                       inet_ntoa(inaddr));
+                               fprintf(file,"\t%s", inet_ntoa(inaddr));
                                cp += dlen;
                        } else if (dlen == 7) {
                                cp += dlen;
                        } else if (dlen == 7) {
-                               fprintf(file,"\tinternet address = %s",
-                                       inet_ntoa(inaddr));
-                               fprintf(file,", protocol = %d", cp[4]);
-                               fprintf(file,", port = %d\n",
-                                       (cp[5] << 8) + cp[6]);
-                               cp += dlen;
+                               char *address;
+                               u_char protocol;
+                               u_short port;
+
+                               address = inet_ntoa(inaddr);
+                               cp += sizeof(inaddr);
+                               protocol = *(u_char*)cp;
+                               cp += sizeof(u_char);
+                               port = _getshort(cp);
+                               cp += sizeof(u_int16_t);
+                               fprintf(file, "\t%s\t; proto %d, port %d",
+                                       address, protocol, port);
                        }
                        break;
                default:
                        }
                        break;
                default:
@@ -235,44 +415,43 @@ p_rr(cp, msg, file)
        case T_MR:
        case T_NS:
        case T_PTR:
        case T_MR:
        case T_NS:
        case T_PTR:
-               fprintf(file,"\tdomain name = ");
-               cp = p_cdname(cp, msg, file);
-               fprintf(file,"\n");
+               putc('\t', file);
+               cp = p_fqname(cp, msg, file);
                break;
 
        case T_HINFO:
                if (n = *cp++) {
                break;
 
        case T_HINFO:
                if (n = *cp++) {
-                       fprintf(file,"\tCPU=%.*s\n", n, cp);
+                       fprintf(file,"\t%.*s", n, cp);
                        cp += n;
                }
                if (n = *cp++) {
                        cp += n;
                }
                if (n = *cp++) {
-                       fprintf(file,"\tOS=%.*s\n", n, cp);
+                       fprintf(file,"\t%.*s", n, cp);
                        cp += n;
                }
                break;
 
        case T_SOA:
                        cp += n;
                }
                break;
 
        case T_SOA:
-               fprintf(file,"\torigin = ");
-               cp = p_cdname(cp, msg, file);
-               fprintf(file,"\n\tmail addr = ");
-               cp = p_cdname(cp, msg, file);
-               fprintf(file,"\n\tserial = %ld", _getlong(cp));
-               cp += sizeof(u_long);
-               fprintf(file,"\n\trefresh = %s", __p_time(_getlong(cp)));
-               cp += sizeof(u_long);
-               fprintf(file,"\n\tretry = %s", __p_time(_getlong(cp)));
-               cp += sizeof(u_long);
-               fprintf(file,"\n\texpire = %s", __p_time(_getlong(cp)));
-               cp += sizeof(u_long);
-               fprintf(file,"\n\tmin = %s\n", __p_time(_getlong(cp)));
-               cp += sizeof(u_long);
+               putc('\t', file);
+               cp = p_fqname(cp, msg, file);   /* origin */
+               putc(' ', file);
+               cp = p_fqname(cp, msg, file);   /* mail addr */
+               fputs(" (\n", file);
+               t = _getlong(cp);  cp += sizeof(u_int32_t);
+               fprintf(file,"\t\t\t%lu\t; serial\n", t);
+               t = _getlong(cp);  cp += sizeof(u_int32_t);
+               fprintf(file,"\t\t\t%lu\t; refresh (%s)\n", t, __p_time(t));
+               t = _getlong(cp);  cp += sizeof(u_int32_t);
+               fprintf(file,"\t\t\t%lu\t; retry (%s)\n", t, __p_time(t));
+               t = _getlong(cp);  cp += sizeof(u_int32_t);
+               fprintf(file,"\t\t\t%lu\t; expire (%s)\n", t, __p_time(t));
+               t = _getlong(cp);  cp += sizeof(u_int32_t);
+               fprintf(file,"\t\t\t%lu )\t; minimum (%s)", t, __p_time(t));
                break;
 
        case T_MX:
                break;
 
        case T_MX:
-               fprintf(file,"\tpreference = %ld,",_getshort(cp));
-               cp += sizeof(u_short);
-               fprintf(file," name = ");
-               cp = p_cdname(cp, msg, file);
+               fprintf(file,"\t%d ", _getshort(cp));
+               cp += sizeof(u_int16_t);
+               cp = p_fqname(cp, msg, file);
                break;
 
        case T_TXT:
                break;
 
        case T_TXT:
@@ -288,46 +467,58 @@ p_rr(cp, msg, file)
                                            (void) putc(*cp++, file);
                        }
                }
                                            (void) putc(*cp++, file);
                        }
                }
-               (void) fputs("\"\n", file);
+               putc('"', file);
                break;
 
        case T_MINFO:
                break;
 
        case T_MINFO:
-               fprintf(file,"\trequests = ");
-               cp = p_cdname(cp, msg, file);
-               fprintf(file,"\n\terrors = ");
-               cp = p_cdname(cp, msg, file);
+       case T_RP:
+               putc('\t', file);
+               cp = p_fqname(cp, msg, file);
+               putc(' ', file);
+               cp = p_fqname(cp, msg, file);
                break;
 
        case T_UINFO:
                break;
 
        case T_UINFO:
-               fprintf(file,"\t%s\n", cp);
+               putc('\t', file);
+               fputs(cp, file);
                cp += dlen;
                break;
 
        case T_UID:
        case T_GID:
                if (dlen == 4) {
                cp += dlen;
                break;
 
        case T_UID:
        case T_GID:
                if (dlen == 4) {
-                       fprintf(file,"\t%ld\n", _getlong(cp));
-                       cp += sizeof(int);
+                       fprintf(file,"\t%u", _getlong(cp));
+                       cp += sizeof(int32_t);
                }
                break;
 
        case T_WKS:
                }
                break;
 
        case T_WKS:
-               if (dlen < sizeof(u_long) + 1)
+               if (dlen < sizeof(u_int32_t) + 1)
                        break;
                bcopy(cp, (char *)&inaddr, sizeof(inaddr));
                        break;
                bcopy(cp, (char *)&inaddr, sizeof(inaddr));
-               cp += sizeof(u_long);
-               fprintf(file,"\tinternet address = %s, protocol = %d\n\t",
-                       inet_ntoa(inaddr), *cp++);
+               cp += sizeof(u_int32_t);
+               fprintf(file, "\t%s %s ( ",
+                       inet_ntoa(inaddr),
+                       deproto((int) *cp));
+               cp += sizeof(u_char);
                n = 0;
                n = 0;
+               lcnt = 0;
                while (cp < cp1 + dlen) {
                        c = *cp++;
                        do {
                while (cp < cp1 + dlen) {
                        c = *cp++;
                        do {
-                               if (c & 0200)
-                                       fprintf(file," %d", n);
+                               if (c & 0200) {
+                                       if (lcnt == 0) {
+                                               fputs("\n\t\t\t", file);
+                                               lcnt = 5;
+                                       }
+                                       fputs(dewks(n), file);
+                                       putc(' ', file);
+                                       lcnt--;
+                               }
                                c <<= 1;
                        } while (++n & 07);
                }
                                c <<= 1;
                        } while (++n & 07);
                }
-               putc('\n',file);
+               putc(')', file);
                break;
 
 #ifdef ALLOW_T_UNSPEC
                break;
 
 #ifdef ALLOW_T_UNSPEC
@@ -342,21 +533,25 @@ p_rr(cp, msg, file)
                                NumBytes);
                        for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++)
                                fprintf(file, " %x", *DataPtr);
                                NumBytes);
                        for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++)
                                fprintf(file, " %x", *DataPtr);
-                       fputs("\n", file);
                        cp += dlen;
                }
                break;
 #endif /* ALLOW_T_UNSPEC */
 
        default:
                        cp += dlen;
                }
                break;
 #endif /* ALLOW_T_UNSPEC */
 
        default:
-               fprintf(file,"\t???\n");
+               fprintf(file,"\t?%d?", type);
                cp += dlen;
        }
                cp += dlen;
        }
-       if (cp != cp1 + dlen) {
-               fprintf(file,"packet size error (%#x != %#x)\n", cp, cp1+dlen);
+#if 0
+       fprintf(file, "\t; dlen=%d, ttl %s\n", dlen, __p_time(tmpttl));
+#else
+       putc('\n', file);
+#endif
+       if (cp - cp1 != dlen) {
+               fprintf(file,";; packet size error (found %d, dlen was %d)\n",
+                       cp - cp1, dlen);
                cp = NULL;
        }
                cp = NULL;
        }
-       fprintf(file,"\n");
        return (cp);
 }
 
        return (cp);
 }
 
@@ -398,6 +593,8 @@ __p_type(type)
                return("MX");
        case T_TXT:             /* text */
                return("TXT");
                return("MX");
        case T_TXT:             /* text */
                return("TXT");
+       case T_RP:              /* responsible person */
+               return("RP");
        case T_AXFR:            /* zone transfer */
                return("AXFR");
        case T_MAILB:           /* mail box */
        case T_AXFR:            /* zone transfer */
                return("AXFR");
        case T_MAILB:           /* mail box */
@@ -443,14 +640,36 @@ __p_class(class)
        }
 }
 
        }
 }
 
+/*
+ * Return a mnemonic for an option
+ */
+static char *
+p_option(option)
+       u_int32_t option;
+{
+       switch (option) {
+       case RES_INIT:          return "init";
+       case RES_DEBUG:         return "debug";
+       case RES_AAONLY:        return "aaonly";
+       case RES_USEVC:         return "usevc";
+       case RES_PRIMARY:       return "primry";
+       case RES_IGNTC:         return "igntc";
+       case RES_RECURSE:       return "recurs";
+       case RES_DEFNAMES:      return "defnam";
+       case RES_STAYOPEN:      return "styopn";
+       case RES_DNSRCH:        return "dnsrch";
+       default:                sprintf(nbuf, "?0x%x?", option); return nbuf;
+       }
+}
+
 /*
  * Return a mnemonic for a time to live
  */
 char *
 __p_time(value)
 /*
  * Return a mnemonic for a time to live
  */
 char *
 __p_time(value)
-       u_long value;
+       u_int32_t value;
 {
 {
-       int secs, mins, hours;
+       int secs, mins, hours, days;
        register char *p;
 
        if (value == 0) {
        register char *p;
 
        if (value == 0) {
@@ -464,27 +683,29 @@ __p_time(value)
        value /= 60;
        hours = value % 24;
        value /= 24;
        value /= 60;
        hours = value % 24;
        value /= 24;
+       days = value;
+       value = 0;
 
 #define        PLURALIZE(x)    x, (x == 1) ? "" : "s"
        p = nbuf;
 
 #define        PLURALIZE(x)    x, (x == 1) ? "" : "s"
        p = nbuf;
-       if (value) {
-               (void)sprintf(p, "%d day%s", PLURALIZE(value));
+       if (days) {
+               (void)sprintf(p, "%d day%s", PLURALIZE(days));
                while (*++p);
        }
        if (hours) {
                while (*++p);
        }
        if (hours) {
-               if (value)
+               if (days)
                        *p++ = ' ';
                (void)sprintf(p, "%d hour%s", PLURALIZE(hours));
                while (*++p);
        }
        if (mins) {
                        *p++ = ' ';
                (void)sprintf(p, "%d hour%s", PLURALIZE(hours));
                while (*++p);
        }
        if (mins) {
-               if (value || hours)
+               if (days || hours)
                        *p++ = ' ';
                (void)sprintf(p, "%d min%s", PLURALIZE(mins));
                while (*++p);
        }
                        *p++ = ' ';
                (void)sprintf(p, "%d min%s", PLURALIZE(mins));
                while (*++p);
        }
-       if (secs || ! (value || hours || mins)) {
-               if (value || hours || mins)
+       if (secs || ! (days || hours || mins)) {
+               if (days || hours || mins)
                        *p++ = ' ';
                (void)sprintf(p, "%d sec%s", PLURALIZE(secs));
        }
                        *p++ = ' ';
                (void)sprintf(p, "%d sec%s", PLURALIZE(secs));
        }
index 2c0c6f8..4a0b97e 100644 (file)
@@ -3,10 +3,31 @@
  * All rights reserved.
  *
  * %sccs.include.redist.c%
  * All rights reserved.
  *
  * %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_init.c 6.15 (Berkeley) %G%";
+static char sccsid[] = "@(#)res_init.c 6.16 (Berkeley) %G%";
+static char rcsid[] = "$Id: res_init.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel $";
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
@@ -24,7 +45,7 @@ static char sccsid[] = "@(#)res_init.c        6.15 (Berkeley) %G%";
  * Resolver state default settings
  */
 
  * Resolver state default settings
  */
 
-struct state _res = {
+struct __res_state _res = {
        RES_TIMEOUT,                    /* retransmition time interval */
        4,                              /* number of times to retransmit */
        RES_DEFAULT,                    /* options flags */
        RES_TIMEOUT,                    /* retransmition time interval */
        4,                              /* number of times to retransmit */
        RES_DEFAULT,                    /* options flags */
@@ -51,20 +72,30 @@ res_init()
        int haveenv = 0;
        int havesearch = 0;
 
        int haveenv = 0;
        int havesearch = 0;
 
+#ifdef USELOOPBACK
+       _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
+#else
        _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
        _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
+#endif
        _res.nsaddr.sin_family = AF_INET;
        _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
        _res.nscount = 1;
        _res.nsaddr.sin_family = AF_INET;
        _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
        _res.nscount = 1;
+       _res.pfcode = 0;
 
        /* Allow user to override the local domain definition */
        if ((cp = getenv("LOCALDOMAIN")) != NULL) {
                (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
 
        /* Allow user to override the local domain definition */
        if ((cp = getenv("LOCALDOMAIN")) != NULL) {
                (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
+               if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)
+                       *cp = '\0';
                haveenv++;
        }
 
        if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
            /* read the config file */
            while (fgets(buf, sizeof(buf), fp) != NULL) {
                haveenv++;
        }
 
        if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
            /* read the config file */
            while (fgets(buf, sizeof(buf), fp) != NULL) {
+               /* skip comments */
+               if ((*buf == ';') || (*buf == '#'))
+                       continue;
                /* read default domain name */
                if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
                    if (haveenv)        /* skip if have from environ */
                /* read default domain name */
                if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
                    if (haveenv)        /* skip if have from environ */
@@ -74,8 +105,9 @@ res_init()
                            cp++;
                    if ((*cp == '\0') || (*cp == '\n'))
                            continue;
                            cp++;
                    if ((*cp == '\0') || (*cp == '\n'))
                            continue;
-                   (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
-                   if ((cp = index(_res.defdname, '\n')) != NULL)
+                   (void)strncpy(_res.defdname, cp,
+                                 sizeof(_res.defdname) - 1);
+                   if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)
                            *cp = '\0';
                    havesearch = 0;
                    continue;
                            *cp = '\0';
                    havesearch = 0;
                    continue;
@@ -89,7 +121,8 @@ res_init()
                            cp++;
                    if ((*cp == '\0') || (*cp == '\n'))
                            continue;
                            cp++;
                    if ((*cp == '\0') || (*cp == '\n'))
                            continue;
-                   (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
+                   (void)strncpy(_res.defdname, cp,
+                                 sizeof(_res.defdname) - 1);
                    if ((cp = index(_res.defdname, '\n')) != NULL)
                            *cp = '\0';
                    /*
                    if ((cp = index(_res.defdname, '\n')) != NULL)
                            *cp = '\0';
                    /*
@@ -119,20 +152,18 @@ res_init()
                /* read nameservers to query */
                if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) &&
                   nserv < MAXNS) {
                /* read nameservers to query */
                if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) &&
                   nserv < MAXNS) {
+                  struct in_addr a;
+
                    cp = buf + sizeof("nameserver") - 1;
                    while (*cp == ' ' || *cp == '\t')
                    cp = buf + sizeof("nameserver") - 1;
                    while (*cp == ' ' || *cp == '\t')
-                           cp++;
-                   if ((*cp == '\0') || (*cp == '\n'))
-                           continue;
-                   if ((_res.nsaddr_list[nserv].sin_addr.s_addr =
-                       inet_addr(cp)) == (unsigned)-1) {
-                           _res.nsaddr_list[nserv].sin_addr.s_addr
-                               = INADDR_ANY;
-                           continue;
+                       cp++;
+                   if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) {
+                       _res.nsaddr_list[nserv].sin_addr = a;
+                       _res.nsaddr_list[nserv].sin_family = AF_INET;
+                       _res.nsaddr_list[nserv].sin_port =
+                               htons(NAMESERVER_PORT);
+                       nserv++;
                    }
                    }
-                   _res.nsaddr_list[nserv].sin_family = AF_INET;
-                   _res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
-                   nserv++;
                    continue;
                }
            }
                    continue;
                }
            }
index 886ce56..09d26a3 100644 (file)
@@ -1,12 +1,33 @@
-/*
+/*-
  * Copyright (c) 1985 Regents of the University of California.
  * All rights reserved.
  *
  * %sccs.include.redist.c%
  * Copyright (c) 1985 Regents of the University of California.
  * All rights reserved.
  *
  * %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_mkquery.c      6.16 (Berkeley) %G%";
+static char sccsid[] = "@(#)res_mkquery.c      6.17 (Berkeley) %G%";
+static char rcsid[] = "$Id: res_mkquery.c,v 4.9.1.2 1993/05/17 10:00:01 vixie Exp $";
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
@@ -20,25 +41,27 @@ static char sccsid[] = "@(#)res_mkquery.c   6.16 (Berkeley) %G%";
  * Form all types of queries.
  * Returns the size of the result or -1.
  */
  * Form all types of queries.
  * Returns the size of the result or -1.
  */
-res_mkquery(op, dname, class, type, data, datalen, newrr, buf, buflen)
+res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen)
        int op;                 /* opcode of query */
        const char *dname;              /* domain name */
        int class, type;        /* class and type of query */
        const char *data;               /* resource record data */
        int datalen;            /* length of data */
        int op;                 /* opcode of query */
        const char *dname;              /* domain name */
        int class, type;        /* class and type of query */
        const char *data;               /* resource record data */
        int datalen;            /* length of data */
-       const struct rrec *newrr;       /* new rr for modify or append */
+       const char *newrr_in;   /* new rr for modify or append */
        char *buf;              /* buffer to put query */
        int buflen;             /* size of buffer */
 {
        register HEADER *hp;
        register char *cp;
        register int n;
        char *buf;              /* buffer to put query */
        int buflen;             /* size of buffer */
 {
        register HEADER *hp;
        register char *cp;
        register int n;
+       struct rrec *newrr = (struct rrec *) newrr_in;
        char *dnptrs[10], **dpp, **lastdnptr;
 
 #ifdef DEBUG
        if (_res.options & RES_DEBUG)
        char *dnptrs[10], **dpp, **lastdnptr;
 
 #ifdef DEBUG
        if (_res.options & RES_DEBUG)
-               printf("res_mkquery(%d, %s, %d, %d)\n", op, dname, class, type);
-#endif DEBUG
+               printf(";; res_mkquery(%d, %s, %d, %d)\n",
+                      op, dname, class, type);
+#endif
        /*
         * Initialize header fields.
         */
        /*
         * Initialize header fields.
         */
@@ -70,9 +93,9 @@ res_mkquery(op, dname, class, type, data, datalen, newrr, buf, buflen)
                cp += n;
                buflen -= n;
                __putshort(type, (u_char *)cp);
                cp += n;
                buflen -= n;
                __putshort(type, (u_char *)cp);
-               cp += sizeof(u_short);
+               cp += sizeof(u_int16_t);
                __putshort(class, (u_char *)cp);
                __putshort(class, (u_char *)cp);
-               cp += sizeof(u_short);
+               cp += sizeof(u_int16_t);
                hp->qdcount = htons(1);
                if (op == QUERY || data == NULL)
                        break;
                hp->qdcount = htons(1);
                if (op == QUERY || data == NULL)
                        break;
@@ -86,13 +109,13 @@ res_mkquery(op, dname, class, type, data, datalen, newrr, buf, buflen)
                cp += n;
                buflen -= n;
                __putshort(T_NULL, (u_char *)cp);
                cp += n;
                buflen -= n;
                __putshort(T_NULL, (u_char *)cp);
-               cp += sizeof(u_short);
+               cp += sizeof(u_int16_t);
                __putshort(class, (u_char *)cp);
                __putshort(class, (u_char *)cp);
-               cp += sizeof(u_short);
+               cp += sizeof(u_int16_t);
                __putlong(0, (u_char *)cp);
                __putlong(0, (u_char *)cp);
-               cp += sizeof(u_long);
+               cp += sizeof(u_int32_t);
                __putshort(0, (u_char *)cp);
                __putshort(0, (u_char *)cp);
-               cp += sizeof(u_short);
+               cp += sizeof(u_int16_t);
                hp->arcount = htons(1);
                break;
 
                hp->arcount = htons(1);
                break;
 
@@ -104,13 +127,13 @@ res_mkquery(op, dname, class, type, data, datalen, newrr, buf, buflen)
                        return (-1);
                *cp++ = '\0';   /* no domain name */
                __putshort(type, (u_char *)cp);
                        return (-1);
                *cp++ = '\0';   /* no domain name */
                __putshort(type, (u_char *)cp);
-               cp += sizeof(u_short);
+               cp += sizeof(u_int16_t);
                __putshort(class, (u_char *)cp);
                __putshort(class, (u_char *)cp);
-               cp += sizeof(u_short);
+               cp += sizeof(u_int16_t);
                __putlong(0, (u_char *)cp);
                __putlong(0, (u_char *)cp);
-               cp += sizeof(u_long);
+               cp += sizeof(u_int32_t);
                __putshort(datalen, (u_char *)cp);
                __putshort(datalen, (u_char *)cp);
-               cp += sizeof(u_short);
+               cp += sizeof(u_int16_t);
                if (datalen) {
                        bcopy(data, cp, datalen);
                        cp += datalen;
                if (datalen) {
                        bcopy(data, cp, datalen);
                        cp += datalen;
@@ -138,13 +161,13 @@ res_mkquery(op, dname, class, type, data, datalen, newrr, buf, buflen)
                        return (-1);
                cp += n;
                __putshort(type, cp);
                        return (-1);
                cp += n;
                __putshort(type, cp);
-                cp += sizeof(u_short);
+                cp += sizeof(u_int16_t);
                 __putshort(class, cp);
                 __putshort(class, cp);
-                cp += sizeof(u_short);
+                cp += sizeof(u_int16_t);
                __putlong(0, cp);
                __putlong(0, cp);
-               cp += sizeof(u_long);
+               cp += sizeof(u_int32_t);
                __putshort(datalen, cp);
                __putshort(datalen, cp);
-                cp += sizeof(u_short);
+                cp += sizeof(u_int16_t);
                if (datalen) {
                        bcopy(data, cp, datalen);
                        cp += datalen;
                if (datalen) {
                        bcopy(data, cp, datalen);
                        cp += datalen;
@@ -161,13 +184,13 @@ res_mkquery(op, dname, class, type, data, datalen, newrr, buf, buflen)
                        return (-1);
                cp += n;
                __putshort(newrr->r_type, cp);
                        return (-1);
                cp += n;
                __putshort(newrr->r_type, cp);
-                cp += sizeof(u_short);
+                cp += sizeof(u_int16_t);
                 __putshort(newrr->r_class, cp);
                 __putshort(newrr->r_class, cp);
-                cp += sizeof(u_short);
+                cp += sizeof(u_int16_t);
                __putlong(0, cp);
                __putlong(0, cp);
-               cp += sizeof(u_long);
+               cp += sizeof(u_int32_t);
                __putshort(newrr->r_size, cp);
                __putshort(newrr->r_size, cp);
-                cp += sizeof(u_short);
+                cp += sizeof(u_int16_t);
                if (newrr->r_size) {
                        bcopy(newrr->r_data, cp, newrr->r_size);
                        cp += newrr->r_size;
                if (newrr->r_size) {
                        bcopy(newrr->r_data, cp, newrr->r_size);
                        cp += newrr->r_size;
@@ -175,7 +198,7 @@ res_mkquery(op, dname, class, type, data, datalen, newrr, buf, buflen)
                hp->ancount = htons(0);
                break;
 
                hp->ancount = htons(0);
                break;
 
-#endif ALLOW_UPDATES
+#endif /* ALLOW_UPDATES */
        }
        return (cp - buf);
 }
        }
        return (cp - buf);
 }
index ef1e24b..7b39e1f 100644 (file)
@@ -1,12 +1,33 @@
-/*
+/*-
  * Copyright (c) 1988 Regents of the University of California.
  * All rights reserved.
  *
  * %sccs.include.redist.c%
  * Copyright (c) 1988 Regents of the University of California.
  * All rights reserved.
  *
  * %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.11 (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>
@@ -52,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));
@@ -60,7 +81,7 @@ 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);
@@ -69,7 +90,7 @@ 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: send error\n");
+                       printf(";; res_query: send error\n");
 #endif
                h_errno = TRY_AGAIN;
                return(n);
 #endif
                h_errno = TRY_AGAIN;
                return(n);
@@ -79,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) {
@@ -112,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,7 +148,7 @@ res_search(name, class, type, answer, anslen)
                return (-1);
 
        errno = 0;
                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++;
@@ -199,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) {
index 2118794..4d9db11 100644 (file)
@@ -1,12 +1,33 @@
-/*
+/*-
  * Copyright (c) 1985, 1989 Regents of the University of California.
  * All rights reserved.
  *
  * %sccs.include.redist.c%
  * Copyright (c) 1985, 1989 Regents of the University of California.
  * All rights reserved.
  *
  * %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_send.c 6.27 (Berkeley) %G%";
+static char sccsid[] = "@(#)res_send.c 6.28 (Berkeley) %G%";
+static char rcsid[] = "$Id: res_send.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel $";
 #endif /* LIBC_SCCS and not lint */
 
 /*
 #endif /* LIBC_SCCS and not lint */
 
 /*
@@ -19,6 +40,7 @@ static char sccsid[] = "@(#)res_send.c        6.27 (Berkeley) %G%";
 #include <sys/uio.h>
 #include <netinet/in.h>
 #include <arpa/nameser.h>
 #include <sys/uio.h>
 #include <netinet/in.h>
 #include <arpa/nameser.h>
+#include <arpa/inet.h>
 #include <stdio.h>
 #include <errno.h>
 #include <resolv.h>
 #include <stdio.h>
 #include <errno.h>
 #include <resolv.h>
@@ -53,32 +75,37 @@ res_send(buf, buflen, answer, anslen)
        struct timeval timeout;
        HEADER *hp = (HEADER *) buf;
        HEADER *anhp = (HEADER *) answer;
        struct timeval timeout;
        HEADER *hp = (HEADER *) buf;
        HEADER *anhp = (HEADER *) answer;
+       u_int badns;            /* XXX NSMAX can't exceed #/bits per this */
        struct iovec iov[2];
        int terrno = ETIMEDOUT;
        char junk[512];
 
 #ifdef DEBUG
        struct iovec iov[2];
        int terrno = ETIMEDOUT;
        char junk[512];
 
 #ifdef DEBUG
-       if (_res.options & RES_DEBUG) {
-               printf("res_send()\n");
+       if ((_res.options & RES_DEBUG) || (_res.pfcode & RES_PRF_QUERY)) {
+               printf(";; res_send()\n");
                __p_query(buf);
        }
                __p_query(buf);
        }
-#endif DEBUG
+#endif
        if (!(_res.options & RES_INIT))
                if (res_init() == -1) {
                        return(-1);
                }
        v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ;
        id = hp->id;
        if (!(_res.options & RES_INIT))
                if (res_init() == -1) {
                        return(-1);
                }
        v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ;
        id = hp->id;
+       badns = 0;
        /*
         * Send request, RETRY times, or until successful
         */
        for (try = 0; try < _res.retry; try++) {
        /*
         * Send request, RETRY times, or until successful
         */
        for (try = 0; try < _res.retry; try++) {
-          for (ns = 0; ns < _res.nscount; ns++) {
+           for (ns = 0; ns < _res.nscount; ns++) {
+               if (badns & (1<<ns))
+                       continue;
 #ifdef DEBUG
                if (_res.options & RES_DEBUG)
 #ifdef DEBUG
                if (_res.options & RES_DEBUG)
-                       printf("Querying server (# %d) address = %s\n", ns+1,
-                             inet_ntoa(_res.nsaddr_list[ns].sin_addr));
-#endif DEBUG
+                       printf(";; Querying server (# %d) address = %s\n",
+                              ns+1,
+                              inet_ntoa(_res.nsaddr_list[ns].sin_addr));
+#endif
        usevc:
                if (v_circuit) {
                        int truncated = 0;
        usevc:
                if (v_circuit) {
                        int truncated = 0;
@@ -95,7 +122,7 @@ res_send(buf, buflen, answer, anslen)
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
                                            perror("socket (vc) failed");
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
                                            perror("socket (vc) failed");
-#endif DEBUG
+#endif
                                        continue;
                                }
                                if (connect(s,
                                        continue;
                                }
                                if (connect(s,
@@ -105,7 +132,7 @@ res_send(buf, buflen, answer, anslen)
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
                                            perror("connect failed");
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
                                            perror("connect failed");
-#endif DEBUG
+#endif
                                        (void) close(s);
                                        s = -1;
                                        continue;
                                        (void) close(s);
                                        s = -1;
                                        continue;
@@ -124,7 +151,7 @@ res_send(buf, buflen, answer, anslen)
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                                        perror("write failed");
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                                        perror("write failed");
-#endif DEBUG
+#endif
                                (void) close(s);
                                s = -1;
                                continue;
                                (void) close(s);
                                s = -1;
                                continue;
@@ -144,7 +171,7 @@ res_send(buf, buflen, answer, anslen)
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                                        perror("read failed");
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                                        perror("read failed");
-#endif DEBUG
+#endif
                                (void) close(s);
                                s = -1;
                                /*
                                (void) close(s);
                                s = -1;
                                /*
@@ -166,8 +193,9 @@ res_send(buf, buflen, answer, anslen)
                        if ((resplen = ntohs(*(u_short *)cp)) > anslen) {
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                        if ((resplen = ntohs(*(u_short *)cp)) > anslen) {
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
-                                       fprintf(stderr, "response truncated\n");
-#endif DEBUG
+                                       fprintf(stderr,
+                                               ";; response truncated\n");
+#endif
                                len = anslen;
                                truncated = 1;
                        } else
                                len = anslen;
                                truncated = 1;
                        } else
@@ -182,7 +210,7 @@ res_send(buf, buflen, answer, anslen)
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                                        perror("read failed");
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                                        perror("read failed");
-#endif DEBUG
+#endif
                                (void) close(s);
                                s = -1;
                                continue;
                                (void) close(s);
                                s = -1;
                                continue;
@@ -214,11 +242,10 @@ res_send(buf, buflen, answer, anslen)
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
                                            perror("socket (dg) failed");
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
                                            perror("socket (dg) failed");
-#endif DEBUG
+#endif
                                        continue;
                                }
                        }
                                        continue;
                                }
                        }
-#if    BSD >= 43
                        /*
                         * I'm tired of answering this question, so:
                         * On a 4.3BSD+ machine (client and server,
                        /*
                         * I'm tired of answering this question, so:
                         * On a 4.3BSD+ machine (client and server,
@@ -242,12 +269,14 @@ res_send(buf, buflen, answer, anslen)
                                 * from another server.
                                 */
                                if (connected == 0) {
                                 * from another server.
                                 */
                                if (connected == 0) {
-                       if (connect(s, (struct sockaddr *)&_res.nsaddr_list[ns],
+                                       if (connect(s,
+                                           (struct sockaddr *)
+                                           &_res.nsaddr_list[ns],
                                            sizeof(struct sockaddr)) < 0) {
 #ifdef DEBUG
                                                if (_res.options & RES_DEBUG)
                                                        perror("connect");
                                            sizeof(struct sockaddr)) < 0) {
 #ifdef DEBUG
                                                if (_res.options & RES_DEBUG)
                                                        perror("connect");
-#endif DEBUG
+#endif
                                                continue;
                                        }
                                        connected = 1;
                                                continue;
                                        }
                                        connected = 1;
@@ -256,7 +285,7 @@ res_send(buf, buflen, answer, anslen)
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
                                                perror("send");
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
                                                perror("send");
-#endif DEBUG
+#endif
                                        continue;
                                }
                        } else {
                                        continue;
                                }
                        } else {
@@ -269,19 +298,16 @@ res_send(buf, buflen, answer, anslen)
                                            sizeof(no_addr));
                                        connected = 0;
                                }
                                            sizeof(no_addr));
                                        connected = 0;
                                }
-#endif BSD
                                if (sendto(s, buf, buflen, 0,
                                    (struct sockaddr *)&_res.nsaddr_list[ns],
                                    sizeof(struct sockaddr)) != buflen) {
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
                                                perror("sendto");
                                if (sendto(s, buf, buflen, 0,
                                    (struct sockaddr *)&_res.nsaddr_list[ns],
                                    sizeof(struct sockaddr)) != buflen) {
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
                                                perror("sendto");
-#endif DEBUG
+#endif
                                        continue;
                                }
                                        continue;
                                }
-#if    BSD >= 43
                        }
                        }
-#endif
 
                        /*
                         * Wait for reply
 
                        /*
                         * Wait for reply
@@ -289,7 +315,7 @@ res_send(buf, buflen, answer, anslen)
                        timeout.tv_sec = (_res.retrans << try);
                        if (try > 0)
                                timeout.tv_sec /= _res.nscount;
                        timeout.tv_sec = (_res.retrans << try);
                        if (try > 0)
                                timeout.tv_sec /= _res.nscount;
-                       if (timeout.tv_sec <= 0)
+                       if ((long) timeout.tv_sec <= 0)
                                timeout.tv_sec = 1;
                        timeout.tv_usec = 0;
 wait:
                                timeout.tv_sec = 1;
                        timeout.tv_usec = 0;
 wait:
@@ -301,7 +327,7 @@ wait:
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                                        perror("select");
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                                        perror("select");
-#endif DEBUG
+#endif
                                continue;
                        }
                        if (n == 0) {
                                continue;
                        }
                        if (n == 0) {
@@ -310,18 +336,16 @@ wait:
                                 */
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                                 */
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
-                                       printf("timeout\n");
-#endif DEBUG
-#if BSD >= 43
-                               gotsomewhere = 1;
+                                       printf(";; timeout\n");
 #endif
 #endif
+                               gotsomewhere = 1;
                                continue;
                        }
                        if ((resplen = recv(s, answer, anslen, 0)) <= 0) {
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                                        perror("recvfrom");
                                continue;
                        }
                        if ((resplen = recv(s, answer, anslen, 0)) <= 0) {
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                                        perror("recvfrom");
-#endif DEBUG
+#endif
                                continue;
                        }
                        gotsomewhere = 1;
                                continue;
                        }
                        gotsomewhere = 1;
@@ -330,13 +354,25 @@ wait:
                                 * response from old query, ignore it
                                 */
 #ifdef DEBUG
                                 * response from old query, ignore it
                                 */
 #ifdef DEBUG
-                               if (_res.options & RES_DEBUG) {
-                                       printf("old answer:\n");
+                               if ((_res.options & RES_DEBUG) ||
+                                   (_res.pfcode & RES_PRF_REPLY)) {
+                                       printf(";; old answer:\n");
                                        __p_query(answer);
                                }
                                        __p_query(answer);
                                }
-#endif DEBUG
+#endif
                                goto wait;
                        }
                                goto wait;
                        }
+                       if (anhp->rcode == SERVFAIL || anhp->rcode == NOTIMP ||
+                           anhp->rcode == REFUSED) {
+#ifdef DEBUG
+                               if (_res.options & RES_DEBUG) {
+                                       printf("server rejected query:\n");
+                                       __p_query(answer);
+                               }
+#endif
+                               badns |= (1<<ns);
+                               continue;
+                       }
                        if (!(_res.options & RES_IGNTC) && anhp->tc) {
                                /*
                                 * get rest of answer;
                        if (!(_res.options & RES_IGNTC) && anhp->tc) {
                                /*
                                 * get rest of answer;
@@ -344,8 +380,8 @@ wait:
                                 */
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                                 */
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
-                                       printf("truncated answer\n");
-#endif DEBUG
+                                       printf(";; truncated answer\n");
+#endif
                                (void) close(s);
                                s = -1;
                                v_circuit = 1;
                                (void) close(s);
                                s = -1;
                                v_circuit = 1;
@@ -353,11 +389,12 @@ wait:
                        }
                }
 #ifdef DEBUG
                        }
                }
 #ifdef DEBUG
-               if (_res.options & RES_DEBUG) {
-                       printf("got answer:\n");
+               if (_res.options & RES_DEBUG)
+                       printf(";; got answer:\n");
+               if ((_res.options & RES_DEBUG) ||
+                   (_res.pfcode & RES_PRF_REPLY))
                        __p_query(answer);
                        __p_query(answer);
-               }
-#endif DEBUG
+#endif
                /*
                 * If using virtual circuits, we assume that the first server
                 * is preferred * over the rest (i.e. it is on the local
                /*
                 * If using virtual circuits, we assume that the first server
                 * is preferred * over the rest (i.e. it is on the local