#endif /* HAVE_SYS_SELECT_H */
#include "wget.h"
+#include "utils.h"
#include "host.h"
#include "connect.h"
address_list_release (al);
bind_address_resolved = 1;
}
+\f
+struct cwt_context {
+ int fd;
+ const struct sockaddr *addr;
+ int 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, int addrlen,
+ int 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;
}
/* 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.timeout) < 0)
{
close (sock);
sock = -1;