integrate changes from bind 4.9 (most of them); continue to use address
[unix-history] / usr / src / lib / libc / net / res_send.c
index 7d63146..4d9db11 100644 (file)
@@ -1,22 +1,33 @@
-/*
+/*-
  * Copyright (c) 1985, 1989 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1985, 1989 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, 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'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * %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.22 (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 */
 
 /*
@@ -28,16 +39,16 @@ static char sccsid[] = "@(#)res_send.c      6.22 (Berkeley) %G%";
 #include <sys/socket.h>
 #include <sys/uio.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
 #include <sys/uio.h>
 #include <netinet/in.h>
+#include <arpa/nameser.h>
+#include <arpa/inet.h>
 #include <stdio.h>
 #include <errno.h>
 #include <stdio.h>
 #include <errno.h>
-#include <arpa/nameser.h>
 #include <resolv.h>
 #include <resolv.h>
-
-extern int errno;
+#include <unistd.h>
+#include <string.h>
 
 static int s = -1;     /* socket used for communications */
 static struct sockaddr no_addr;
 
 static int s = -1;     /* socket used for communications */
 static struct sockaddr no_addr;
-  
 
 #ifndef FD_SET
 #define        NFDBITS         32
 
 #ifndef FD_SET
 #define        NFDBITS         32
@@ -49,7 +60,7 @@ static struct sockaddr no_addr;
 #endif
 
 res_send(buf, buflen, answer, anslen)
 #endif
 
 res_send(buf, buflen, answer, anslen)
-       char *buf;
+       const char *buf;
        int buflen;
        char *answer;
        int anslen;
        int buflen;
        char *answer;
        int anslen;
@@ -57,38 +68,44 @@ res_send(buf, buflen, answer, anslen)
        register int n;
        int try, v_circuit, resplen, ns;
        int gotsomewhere = 0, connected = 0;
        register int n;
        int try, v_circuit, resplen, ns;
        int gotsomewhere = 0, connected = 0;
+       int connreset = 0;
        u_short id, len;
        char *cp;
        fd_set dsmask;
        struct timeval timeout;
        HEADER *hp = (HEADER *) buf;
        HEADER *anhp = (HEADER *) answer;
        u_short id, len;
        char *cp;
        fd_set dsmask;
        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");
-               p_query(buf);
+       if ((_res.options & RES_DEBUG) || (_res.pfcode & RES_PRF_QUERY)) {
+               printf(";; res_send()\n");
+               __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;
@@ -104,17 +121,18 @@ res_send(buf, buflen, answer, anslen)
                                        terrno = errno;
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
                                        terrno = errno;
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
-                                           perror("socket failed");
-#endif DEBUG
+                                           perror("socket (vc) failed");
+#endif
                                        continue;
                                }
                                        continue;
                                }
-                               if (connect(s, &(_res.nsaddr_list[ns]),
-                                  sizeof(struct sockaddr)) < 0) {
+                               if (connect(s,
+                                   (struct sockaddr *)&(_res.nsaddr_list[ns]),
+                                   sizeof(struct sockaddr)) < 0) {
                                        terrno = errno;
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
                                            perror("connect failed");
                                        terrno = errno;
 #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;
@@ -126,14 +144,14 @@ res_send(buf, buflen, answer, anslen)
                        len = htons((u_short)buflen);
                        iov[0].iov_base = (caddr_t)&len;
                        iov[0].iov_len = sizeof(len);
                        len = htons((u_short)buflen);
                        iov[0].iov_base = (caddr_t)&len;
                        iov[0].iov_len = sizeof(len);
-                       iov[1].iov_base = buf;
+                       iov[1].iov_base = (char *)buf;
                        iov[1].iov_len = buflen;
                        if (writev(s, iov, 2) != sizeof(len) + buflen) {
                                terrno = errno;
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                                        perror("write failed");
                        iov[1].iov_len = buflen;
                        if (writev(s, iov, 2) != sizeof(len) + buflen) {
                                terrno = errno;
 #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;
@@ -153,17 +171,31 @@ 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;
+                               /*
+                                * A long running process might get its TCP
+                                * connection reset if the remote server was
+                                * restarted.  Requery the server instead of
+                                * trying a new one.  When there is only one
+                                * server, this means that a query might work
+                                * instead of failing.  We only allow one reset
+                                * per query to prevent looping.
+                                */
+                               if (terrno == ECONNRESET && !connreset) {
+                                       connreset = 1;
+                                       ns--;
+                               }
                                continue;
                        }
                        cp = answer;
                        if ((resplen = ntohs(*(u_short *)cp)) > anslen) {
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                                continue;
                        }
                        cp = answer;
                        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
@@ -178,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;
@@ -203,9 +235,17 @@ res_send(buf, buflen, answer, anslen)
                        /*
                         * Use datagrams.
                         */
                        /*
                         * Use datagrams.
                         */
-                       if (s < 0)
+                       if (s < 0) {
                                s = socket(AF_INET, SOCK_DGRAM, 0);
                                s = socket(AF_INET, SOCK_DGRAM, 0);
-#if    BSD >= 43
+                               if (s < 0) {
+                                       terrno = errno;
+#ifdef DEBUG
+                                       if (_res.options & RES_DEBUG)
+                                           perror("socket (dg) failed");
+#endif
+                                       continue;
+                               }
+                       }
                        /*
                         * 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,
@@ -229,12 +269,14 @@ res_send(buf, buflen, answer, anslen)
                                 * from another server.
                                 */
                                if (connected == 0) {
                                 * from another server.
                                 */
                                if (connected == 0) {
-                                       if (connect(s, &_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;
@@ -243,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 {
@@ -256,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,
                                if (sendto(s, buf, buflen, 0,
-                                   &_res.nsaddr_list[ns],
+                                   (struct sockaddr *)&_res.nsaddr_list[ns],
                                    sizeof(struct sockaddr)) != buflen) {
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
                                                perror("sendto");
                                    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
@@ -276,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:
@@ -288,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) {
@@ -297,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;
@@ -317,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");
-                                       p_query(answer);
+                               if ((_res.options & RES_DEBUG) ||
+                                   (_res.pfcode & RES_PRF_REPLY)) {
+                                       printf(";; old answer:\n");
+                                       __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;
@@ -331,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;
@@ -340,11 +389,12 @@ wait:
                        }
                }
 #ifdef DEBUG
                        }
                }
 #ifdef DEBUG
-               if (_res.options & RES_DEBUG) {
-                       printf("got answer:\n");
-                       p_query(answer);
-               }
-#endif DEBUG
+               if (_res.options & RES_DEBUG)
+                       printf(";; got answer:\n");
+               if ((_res.options & RES_DEBUG) ||
+                   (_res.pfcode & RES_PRF_REPLY))
+                       __p_query(answer);
+#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