]> sjero.net Git - wget/blobdiff - src/connect.c
[svn] Look for and use socklen_t.
[wget] / src / connect.c
index 43ed0083512c06364365116c89dc968ea9b2e3ab..2fc232356ff07209d07122c4ce4c2963dbd93fee 100644 (file)
@@ -1,5 +1,5 @@
 /* Establishing and handling network connections.
-   Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
+   Copyright (C) 1995, 1996, 1997, 2001, 2002 Free Software Foundation, Inc.
 
 This file is part of GNU Wget.
 
@@ -15,27 +15,36 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with Wget; if not, write to the Free Software
-Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+In addition, as a special exception, the Free Software Foundation
+gives permission to link the code of its release of Wget with the
+OpenSSL project's "OpenSSL" library (or with modified versions of it
+that use the same license as the "OpenSSL" library), and distribute
+the linked executables.  You must obey the GNU General Public License
+in all respects for all of the code used other than "OpenSSL".  If you
+modify this file, you may extend this exception to your version of the
+file, but you are not obligated to do so.  If you do not wish to do
+so, delete this exception statement from your version.  */
 
 #include <config.h>
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/types.h>
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif
 #include <assert.h>
 
-#ifdef WINDOWS
-# include <winsock.h>
-#else
+#ifndef WINDOWS
 # include <sys/socket.h>
 # include <netdb.h>
 # include <netinet/in.h>
-#ifndef __BEOS__
-# include <arpa/inet.h>
-#endif
-#endif /* WINDOWS */
+# ifndef __BEOS__
+#  include <arpa/inet.h>
+# endif
+#endif /* not WINDOWS */
 
 #include <errno.h>
 #ifdef HAVE_STRING_H
@@ -48,8 +57,9 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #endif /* HAVE_SYS_SELECT_H */
 
 #include "wget.h"
-#include "connect.h"
+#include "utils.h"
 #include "host.h"
+#include "connect.h"
 
 #ifndef errno
 extern int errno;
@@ -59,6 +69,69 @@ extern int errno;
 static int msock = -1;
 static struct sockaddr *addr;
 
+static ip_address bind_address;
+static int bind_address_resolved;
+
+static void
+resolve_bind_address (void)
+{
+  struct address_list *al;
+
+  if (bind_address_resolved || opt.bind_address == NULL)
+    /* Nothing to do. */
+    return;
+
+  al = lookup_host (opt.bind_address, 1);
+  if (!al)
+    {
+      logprintf (LOG_NOTQUIET,
+                _("Unable to convert `%s' to a bind address.  Reverting to ANY.\n"),
+                opt.bind_address);
+      return;
+    }
+
+  address_list_copy_one (al, 0, &bind_address);
+  address_list_release (al);
+  bind_address_resolved = 1;
+}
+\f
+struct cwt_context {
+  int fd;
+  const struct sockaddr *addr;
+  socklen_t addrlen;
+  int result;
+};
+
+static void
+connect_with_timeout_callback (void *arg)
+{
+  struct cwt_context *ctx = (struct cwt_context *)arg;
+  ctx->result = connect (ctx->fd, ctx->addr, ctx->addrlen);
+}
+
+/* Like connect, but specifies a timeout.  If connecting takes longer
+   than TIMEOUT seconds, -1 is returned and errno is set to
+   ETIMEDOUT.  */
+
+static int
+connect_with_timeout (int fd, const struct sockaddr *addr, socklen_t addrlen,
+                     double timeout)
+{
+  struct cwt_context ctx;
+  ctx.fd = fd;
+  ctx.addr = addr;
+  ctx.addrlen = addrlen;
+
+  if (run_with_timeout (timeout, connect_with_timeout_callback, &ctx))
+    {
+      errno = ETIMEDOUT;
+      return -1;
+    }
+  if (ctx.result == -1 && errno == EINTR)
+    errno = ETIMEDOUT;
+  return ctx.result;
+}
+\f
 /* A kludge, but still better than passing the host name all the way
    to connect_to_one.  */
 static const char *connection_host_name;
@@ -101,21 +174,42 @@ connect_to_one (ip_address *addr, unsigned short port, int silent)
   if (sock < 0)
     goto out;
 
-  if (opt.bind_address)
+  /* For very small rate limits, set the buffer size (and hence,
+     hopefully, the size of the kernel window) to the size of the
+     limit.  That way we don't sleep for more than 1s between network
+     reads.  */
+  if (opt.limit_rate && opt.limit_rate < 8192)
+    {
+      int bufsize = opt.limit_rate;
+      if (bufsize < 512)
+       bufsize = 512;
+#ifdef SO_RCVBUF
+      setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
+                 (char *)&bufsize, sizeof (bufsize));
+#endif
+      /* When we add opt.limit_rate support for writing, as with
+        `--post-file', also set SO_SNDBUF here.  */
+    }
+
+  resolve_bind_address ();
+  if (bind_address_resolved)
     {
       /* Bind the client side to the requested address. */
-      if (bind (sock, (struct sockaddr *)opt.bind_address, sockaddr_len ()))
+      wget_sockaddr bsa;
+      wget_sockaddr_set_address (&bsa, ip_default_family, 0, &bind_address);
+      if (bind (sock, &bsa.sa, sockaddr_len ()))
        {
-         close (sock);
+         CLOSE (sock);
          sock = -1;
          goto out;
        }
     }
 
   /* Connect the socket to the remote host.  */
-  if (connect (sock, &sa.sa, sockaddr_len ()) < 0)
+  if (connect_with_timeout (sock, &sa.sa, sockaddr_len (),
+                           opt.connect_timeout) < 0)
     {
-      close (sock);
+      CLOSE (sock);
       sock = -1;
       goto out;
     }
@@ -214,15 +308,16 @@ bindport (unsigned short *port, int family)
 
   if ((msock = socket (family, SOCK_STREAM, 0)) < 0)
     return CONSOCKERR;
+
+#ifdef SO_REUSEADDR
   if (setsockopt (msock, SOL_SOCKET, SO_REUSEADDR,
                  (char *)&optval, sizeof (optval)) < 0)
     return CONSOCKERR;
+#endif
 
-  if (opt.bind_address == NULL)
-    wget_sockaddr_set_address (&srv, ip_default_family, htons (*port), NULL);
-  else
-    srv = *opt.bind_address;
-  wget_sockaddr_set_port (&srv, *port);
+  resolve_bind_address ();
+  wget_sockaddr_set_address (&srv, ip_default_family, htons (*port),
+                            bind_address_resolved ? &bind_address : NULL);
   if (bind (msock, &srv.sa, sockaddr_len ()) < 0)
     {
       CLOSE (msock);
@@ -232,10 +327,7 @@ bindport (unsigned short *port, int family)
   DEBUGP (("Master socket fd %d bound.\n", msock));
   if (!*port)
     {
-      /* #### addrlen should be a 32-bit type, which int is not
-         guaranteed to be.  Oh, and don't try to make it a size_t,
-         because that can be 64-bit.  */
-      int sa_len = sockaddr_len ();
+      socklen_t sa_len = sockaddr_len ();
       if (getsockname (msock, &srv.sa, &sa_len) < 0)
        {
          CLOSE (msock);
@@ -255,42 +347,52 @@ bindport (unsigned short *port, int family)
 }
 
 #ifdef HAVE_SELECT
-/* Wait for file descriptor FD to be readable, MAXTIME being the
-   timeout in seconds.  If WRITEP is non-zero, checks for FD being
-   writable instead.
+/* Wait for file descriptor FD to be available, timing out after
+   MAXTIME seconds.  "Available" means readable if writep is 0,
+   writeable otherwise.
+
+   Returns 1 if FD is available, 0 for timeout and -1 for error.  */
 
-   Returns 1 if FD is accessible, 0 for timeout and -1 for error in
-   select().  */
 int
-select_fd (int fd, int maxtime, int writep)
+select_fd (int fd, double maxtime, int writep)
 {
-  fd_set fds, exceptfds;
-  struct timeval timeout;
+  fd_set fds;
+  fd_set *rd = NULL, *wrt = NULL;
+  struct timeval tmout;
+  int result;
 
   FD_ZERO (&fds);
   FD_SET (fd, &fds);
-  FD_ZERO (&exceptfds);
-  FD_SET (fd, &exceptfds);
-  timeout.tv_sec = maxtime;
-  timeout.tv_usec = 0;
-  /* HPUX reportedly warns here.  What is the correct incantation?  */
-  return select (fd + 1, writep ? NULL : &fds, writep ? &fds : NULL,
-                &exceptfds, &timeout);
+  *(writep ? &wrt : &rd) = &fds;
+
+  tmout.tv_sec = (long)maxtime;
+  tmout.tv_usec = 1000000L * (maxtime - (long)maxtime);
+
+  do
+    result = select (fd + 1, rd, wrt, NULL, &tmout);
+  while (result < 0 && errno == EINTR);
+
+  /* When we've timed out, set errno to ETIMEDOUT for the convenience
+     of the caller. */
+  if (result == 0)
+    errno = ETIMEDOUT;
+
+  return result;
 }
 #endif /* HAVE_SELECT */
 
 /* Call accept() on MSOCK and store the result to *SOCK.  This assumes
    that bindport() has been used to initialize MSOCK to a correct
    value.  It blocks the caller until a connection is established.  If
-   no connection is established for OPT.TIMEOUT seconds, the function
-   exits with an error status.  */
+   no connection is established for OPT.CONNECT_TIMEOUT seconds, the
+   function exits with an error status.  */
 uerr_t
 acceptport (int *sock)
 {
-  int addrlen = sockaddr_len ();
+  socklen_t addrlen = sockaddr_len ();
 
 #ifdef HAVE_SELECT
-  if (select_fd (msock, opt.timeout, 0) <= 0)
+  if (select_fd (msock, opt.connect_timeout, 0) <= 0)
     return ACCEPTERR;
 #endif
   if ((*sock = accept (msock, addr, &addrlen)) < 0)
@@ -318,16 +420,13 @@ int
 conaddr (int fd, ip_address *ip)
 {
   wget_sockaddr mysrv;
-
-  /* see bindport() for discussion of using `int' here. */
-  int addrlen = sizeof (mysrv);        
-
-  if (getsockname (fd, &mysrv.sa, (int *)&addrlen) < 0)
+  socklen_t addrlen = sizeof (mysrv);  
+  if (getsockname (fd, &mysrv.sa, &addrlen) < 0)
     return 0;
 
   switch (mysrv.sa.sa_family)
     {
-#ifdef INET6
+#ifdef ENABLE_IPV6
     case AF_INET6:
       memcpy (ip, &mysrv.sin6.sin6_addr, 16);
       return 1;
@@ -344,45 +443,31 @@ conaddr (int fd, ip_address *ip)
 /* Read at most LEN bytes from FD, storing them to BUF.  This is
    virtually the same as read(), but takes care of EINTR braindamage
    and uses select() to timeout the stale connections (a connection is
-   stale if more than OPT.TIMEOUT time is spent in select() or
+   stale if more than OPT.READ_TIMEOUT time is spent in select() or
    read()).  */
+
 int
 iread (int fd, char *buf, int len)
 {
   int res;
 
-  do
-    {
 #ifdef HAVE_SELECT
-      if (opt.timeout)
-       {
-         do
-           {
-             res = select_fd (fd, opt.timeout, 0);
-           }
-         while (res == -1 && errno == EINTR);
-         if (res <= 0)
-           {
-             /* Set errno to ETIMEDOUT on timeout.  */
-             if (res == 0)
-               /* #### Potentially evil!  */
-               errno = ETIMEDOUT;
-             return -1;
-           }
-       }
+  if (opt.read_timeout)
+    if (select_fd (fd, opt.read_timeout, 0) <= 0)
+      return -1;
 #endif
-      res = READ (fd, buf, len);
-    }
+  do
+    res = READ (fd, buf, len);
   while (res == -1 && errno == EINTR);
 
   return res;
 }
 
 /* Write LEN bytes from BUF to FD.  This is similar to iread(), but
-   doesn't bother with select().  Unlike iread(), it makes sure that
-   all of BUF is actually written to FD, so callers needn't bother
-   with checking that the return value equals to LEN.  Instead, you
-   should simply check for -1.  */
+   unlike iread(), it makes sure that all of BUF is actually written
+   to FD, so callers needn't bother with checking that the return
+   value equals to LEN.  Instead, you should simply check for -1.  */
+
 int
 iwrite (int fd, char *buf, int len)
 {
@@ -394,28 +479,13 @@ iwrite (int fd, char *buf, int len)
      innermost loop deals with the same during select().  */
   while (len > 0)
     {
-      do
-       {
 #ifdef HAVE_SELECT
-         if (opt.timeout)
-           {
-             do
-               {
-                 res = select_fd (fd, opt.timeout, 1);
-               }
-             while (res == -1 && errno == EINTR);
-             if (res <= 0)
-               {
-                 /* Set errno to ETIMEDOUT on timeout.  */
-                 if (res == 0)
-                   /* #### Potentially evil!  */
-                   errno = ETIMEDOUT;
-                 return -1;
-               }
-           }
+      if (opt.read_timeout)
+       if (select_fd (fd, opt.read_timeout, 1) <= 0)
+         return -1;
 #endif
-         res = WRITE (fd, buf, len);
-       }
+      do
+       res = WRITE (fd, buf, len);
       while (res == -1 && errno == EINTR);
       if (res <= 0)
        break;