cleanups on handling SIGHUP; other nits
[unix-history] / usr / src / lib / libc / net / res_send.c
index f33e798..2118794 100644 (file)
@@ -1,22 +1,12 @@
 /*
 /*
- * Copyright (c) 1985 Regents of the University of California.
+ * Copyright (c) 1985, 1989 Regents of the University of California.
  * All rights reserved.
  *
  * 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%
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)res_send.c 6.21 (Berkeley) %G%";
+static char sccsid[] = "@(#)res_send.c 6.27 (Berkeley) %G%";
 #endif /* LIBC_SCCS and not lint */
 
 /*
 #endif /* LIBC_SCCS and not lint */
 
 /*
@@ -28,16 +18,15 @@ static char sccsid[] = "@(#)res_send.c      6.21 (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 <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
@@ -48,17 +37,16 @@ static struct sockaddr no_addr;
 #define FD_ZERO(p)     bzero((char *)(p), sizeof(*(p)))
 #endif
 
 #define FD_ZERO(p)     bzero((char *)(p), sizeof(*(p)))
 #endif
 
-#define KEEPOPEN (RES_USEVC|RES_STAYOPEN)
-
 res_send(buf, buflen, answer, anslen)
 res_send(buf, buflen, answer, anslen)
-       char *buf;
+       const char *buf;
        int buflen;
        char *answer;
        int anslen;
 {
        register int n;
        int buflen;
        char *answer;
        int anslen;
 {
        register int n;
-       int retry, v_circuit, resplen, ns;
+       int try, v_circuit, resplen, ns;
        int gotsomewhere = 0, connected = 0;
        int gotsomewhere = 0, connected = 0;
+       int connreset = 0;
        u_short id, len;
        char *cp;
        fd_set dsmask;
        u_short id, len;
        char *cp;
        fd_set dsmask;
@@ -72,7 +60,7 @@ res_send(buf, buflen, answer, anslen)
 #ifdef DEBUG
        if (_res.options & RES_DEBUG) {
                printf("res_send()\n");
 #ifdef DEBUG
        if (_res.options & RES_DEBUG) {
                printf("res_send()\n");
-               p_query(buf);
+               __p_query(buf);
        }
 #endif DEBUG
        if (!(_res.options & RES_INIT))
        }
 #endif DEBUG
        if (!(_res.options & RES_INIT))
@@ -84,31 +72,35 @@ res_send(buf, buflen, answer, anslen)
        /*
         * Send request, RETRY times, or until successful
         */
        /*
         * Send request, RETRY times, or until successful
         */
-       for (retry = _res.retry; retry > 0; retry--) {
+       for (try = 0; try < _res.retry; try++) {
           for (ns = 0; ns < _res.nscount; ns++) {
 #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
           for (ns = 0; ns < _res.nscount; ns++) {
 #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
+       usevc:
                if (v_circuit) {
                        int truncated = 0;
 
                        /*
                if (v_circuit) {
                        int truncated = 0;
 
                        /*
-                        * Use virtual circuit.
+                        * Use virtual circuit;
+                        * at most one attempt per server.
                         */
                         */
+                       try = _res.retry;
                        if (s < 0) {
                                s = socket(AF_INET, SOCK_STREAM, 0);
                                if (s < 0) {
                                        terrno = errno;
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
                        if (s < 0) {
                                s = socket(AF_INET, SOCK_STREAM, 0);
                                if (s < 0) {
                                        terrno = errno;
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
-                                           perror("socket failed");
+                                           perror("socket (vc) failed");
 #endif DEBUG
                                        continue;
                                }
 #endif DEBUG
                                        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)
                                        terrno = errno;
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
@@ -125,7 +117,7 @@ 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;
                        iov[1].iov_len = buflen;
                        if (writev(s, iov, 2) != sizeof(len) + buflen) {
                                terrno = errno;
@@ -155,6 +147,19 @@ res_send(buf, buflen, answer, anslen)
 #endif DEBUG
                                (void) close(s);
                                s = -1;
 #endif DEBUG
                                (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;
                                continue;
                        }
                        cp = answer;
@@ -202,17 +207,42 @@ 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 (s < 0) {
+                                       terrno = errno;
+#ifdef DEBUG
+                                       if (_res.options & RES_DEBUG)
+                                           perror("socket (dg) failed");
+#endif DEBUG
+                                       continue;
+                               }
+                       }
 #if    BSD >= 43
 #if    BSD >= 43
-                       if (_res.nscount == 1 || retry == _res.retry) {
+                       /*
+                        * I'm tired of answering this question, so:
+                        * On a 4.3BSD+ machine (client and server,
+                        * actually), sending to a nameserver datagram
+                        * port with no nameserver will cause an
+                        * ICMP port unreachable message to be returned.
+                        * If our datagram socket is "connected" to the
+                        * server, we get an ECONNREFUSED error on the next
+                        * socket operation, and select returns if the
+                        * error message is received.  We can thus detect
+                        * the absence of a nameserver without timing out.
+                        * If we have sent queries to at least two servers,
+                        * however, we don't want to remain connected,
+                        * as we wish to receive answers from the first
+                        * server to respond.
+                        */
+                       if (_res.nscount == 1 || (try == 0 && ns == 0)) {
                                /*
                                 * Don't use connect if we might
                                 * still receive a response
                                 * from another server.
                                 */
                                if (connected == 0) {
                                /*
                                 * Don't use connect if we might
                                 * still receive a response
                                 * 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)
                                            sizeof(struct sockaddr)) < 0) {
 #ifdef DEBUG
                                                if (_res.options & RES_DEBUG)
@@ -241,7 +271,7 @@ res_send(buf, buflen, answer, anslen)
                                }
 #endif BSD
                                if (sendto(s, buf, buflen, 0,
                                }
 #endif BSD
                                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)
                                    sizeof(struct sockaddr)) != buflen) {
 #ifdef DEBUG
                                        if (_res.options & RES_DEBUG)
@@ -256,8 +286,9 @@ res_send(buf, buflen, answer, anslen)
                        /*
                         * Wait for reply
                         */
                        /*
                         * Wait for reply
                         */
-                       timeout.tv_sec = (_res.retrans << (_res.retry - retry))
-                               / _res.nscount;
+                       timeout.tv_sec = (_res.retrans << try);
+                       if (try > 0)
+                               timeout.tv_sec /= _res.nscount;
                        if (timeout.tv_sec <= 0)
                                timeout.tv_sec = 1;
                        timeout.tv_usec = 0;
                        if (timeout.tv_sec <= 0)
                                timeout.tv_sec = 1;
                        timeout.tv_usec = 0;
@@ -281,7 +312,9 @@ wait:
                                if (_res.options & RES_DEBUG)
                                        printf("timeout\n");
 #endif DEBUG
                                if (_res.options & RES_DEBUG)
                                        printf("timeout\n");
 #endif DEBUG
+#if BSD >= 43
                                gotsomewhere = 1;
                                gotsomewhere = 1;
+#endif
                                continue;
                        }
                        if ((resplen = recv(s, answer, anslen, 0)) <= 0) {
                                continue;
                        }
                        if ((resplen = recv(s, answer, anslen, 0)) <= 0) {
@@ -299,14 +332,15 @@ wait:
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG) {
                                        printf("old answer:\n");
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG) {
                                        printf("old answer:\n");
-                                       p_query(answer);
+                                       __p_query(answer);
                                }
 #endif DEBUG
                                goto wait;
                        }
                        if (!(_res.options & RES_IGNTC) && anhp->tc) {
                                /*
                                }
 #endif DEBUG
                                goto wait;
                        }
                        if (!(_res.options & RES_IGNTC) && anhp->tc) {
                                /*
-                                * get rest of answer
+                                * get rest of answer;
+                                * use TCP with same server.
                                 */
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
                                 */
 #ifdef DEBUG
                                if (_res.options & RES_DEBUG)
@@ -314,27 +348,27 @@ wait:
 #endif DEBUG
                                (void) close(s);
                                s = -1;
 #endif DEBUG
                                (void) close(s);
                                s = -1;
-                               /*
-                                * retry decremented on continue
-                                * to desired starting value
-                                */
-                               retry = _res.retry + 1;
                                v_circuit = 1;
                                v_circuit = 1;
-                               continue;
+                               goto usevc;
                        }
                }
 #ifdef DEBUG
                if (_res.options & RES_DEBUG) {
                        printf("got answer:\n");
                        }
                }
 #ifdef DEBUG
                if (_res.options & RES_DEBUG) {
                        printf("got answer:\n");
-                       p_query(answer);
+                       __p_query(answer);
                }
 #endif DEBUG
                /*
                }
 #endif DEBUG
                /*
-                * We are going to assume that the first server is preferred
-                * over the rest (i.e. it is on the local machine) and only
-                * keep that one open.
+                * If using virtual circuits, we assume that the first server
+                * is preferred * over the rest (i.e. it is on the local
+                * machine) and only keep that one open.
+                * If we have temporarily opened a virtual circuit,
+                * or if we haven't been asked to keep a socket open,
+                * close the socket.
                 */
                 */
-               if ((_res.options & KEEPOPEN) == 0 || ns != 0) {
+               if ((v_circuit &&
+                   ((_res.options & RES_USEVC) == 0 || ns != 0)) ||
+                   (_res.options & RES_STAYOPEN) == 0) {
                        (void) close(s);
                        s = -1;
                }
                        (void) close(s);
                        s = -1;
                }
@@ -347,9 +381,9 @@ wait:
        }
        if (v_circuit == 0)
                if (gotsomewhere == 0)
        }
        if (v_circuit == 0)
                if (gotsomewhere == 0)
-                       errno = ECONNREFUSED;
+                       errno = ECONNREFUSED;   /* no nameservers found */
                else
                else
-                       errno = ETIMEDOUT;
+                       errno = ETIMEDOUT;      /* no answer obtained */
        else
                errno = terrno;
        return (-1);
        else
                errno = terrno;
        return (-1);