]> sjero.net Git - wget/blobdiff - src/connect.c
[svn] Look for and use socklen_t.
[wget] / src / connect.c
index 99f0909d1c6d711fd3bbc136a09f9ab983855ac7..2fc232356ff07209d07122c4ce4c2963dbd93fee 100644 (file)
@@ -30,22 +30,21 @@ 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
@@ -99,7 +98,7 @@ resolve_bind_address (void)
 struct cwt_context {
   int fd;
   const struct sockaddr *addr;
-  int addrlen;
+  socklen_t addrlen;
   int result;
 };
 
@@ -115,8 +114,8 @@ connect_with_timeout_callback (void *arg)
    ETIMEDOUT.  */
 
 static int
-connect_with_timeout (int fd, const struct sockaddr *addr, int addrlen,
-                     int timeout)
+connect_with_timeout (int fd, const struct sockaddr *addr, socklen_t addrlen,
+                     double timeout)
 {
   struct cwt_context ctx;
   ctx.fd = fd;
@@ -175,6 +174,23 @@ connect_to_one (ip_address *addr, unsigned short port, int silent)
   if (sock < 0)
     goto out;
 
+  /* 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)
     {
@@ -183,16 +199,17 @@ connect_to_one (ip_address *addr, unsigned short port, int silent)
       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_with_timeout (sock, &sa.sa, sockaddr_len (), opt.timeout) < 0)
+  if (connect_with_timeout (sock, &sa.sa, sockaddr_len (),
+                           opt.connect_timeout) < 0)
     {
-      close (sock);
+      CLOSE (sock);
       sock = -1;
       goto out;
     }
@@ -291,9 +308,12 @@ 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
 
   resolve_bind_address ();
   wget_sockaddr_set_address (&srv, ip_default_family, htons (*port),
@@ -307,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);
@@ -337,7 +354,7 @@ bindport (unsigned short *port, int family)
    Returns 1 if FD is available, 0 for timeout and -1 for error.  */
 
 int
-select_fd (int fd, int maxtime, int writep)
+select_fd (int fd, double maxtime, int writep)
 {
   fd_set fds;
   fd_set *rd = NULL, *wrt = NULL;
@@ -348,8 +365,8 @@ select_fd (int fd, int maxtime, int writep)
   FD_SET (fd, &fds);
   *(writep ? &wrt : &rd) = &fds;
 
-  tmout.tv_sec = maxtime;
-  tmout.tv_usec = 0;
+  tmout.tv_sec = (long)maxtime;
+  tmout.tv_usec = 1000000L * (maxtime - (long)maxtime);
 
   do
     result = select (fd + 1, rd, wrt, NULL, &tmout);
@@ -367,15 +384,15 @@ select_fd (int fd, int maxtime, int writep)
 /* 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)
@@ -403,11 +420,8 @@ 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)
@@ -429,7 +443,7 @@ 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
@@ -438,8 +452,8 @@ iread (int fd, char *buf, int len)
   int res;
 
 #ifdef HAVE_SELECT
-  if (opt.timeout)
-    if (select_fd (fd, opt.timeout, 0) <= 0)
+  if (opt.read_timeout)
+    if (select_fd (fd, opt.read_timeout, 0) <= 0)
       return -1;
 #endif
   do
@@ -466,8 +480,8 @@ iwrite (int fd, char *buf, int len)
   while (len > 0)
     {
 #ifdef HAVE_SELECT
-      if (opt.timeout)
-       if (select_fd (fd, opt.timeout, 1) <= 0)
+      if (opt.read_timeout)
+       if (select_fd (fd, opt.read_timeout, 1) <= 0)
          return -1;
 #endif
       do