#include <config.h>
#include <stdio.h>
+#include <stdlib.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
static int
connect_with_timeout (int fd, const struct sockaddr *addr, int addrlen,
- int timeout)
+ double timeout)
{
struct cwt_context ctx;
ctx.fd = fd;
if (sock < 0)
goto out;
+#ifdef SO_RCVBUF
+ /* For very small rate limits, set the buffer size (and hence,
+ hopefully, the size of the kernel window) to the size of the
+ limit. */
+ if (opt.limit_rate && opt.limit_rate < 8192)
+ {
+ int bufsize = opt.limit_rate;
+ if (bufsize < 512)
+ bufsize = 512;
+ setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
+ (char *)&bufsize, sizeof (bufsize));
+ }
+#endif
+
resolve_bind_address ();
if (bind_address_resolved)
{
}
/* 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);
sock = -1;
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),
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;
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);
/* 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 ();
#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)
switch (mysrv.sa.sa_family)
{
-#ifdef INET6
+#ifdef ENABLE_IPV6
case AF_INET6:
memcpy (ip, &mysrv.sin6.sin6_addr, 16);
return 1;
/* 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
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
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