From: Tim Ruehsen Date: Thu, 11 Jul 2013 12:29:20 +0000 (+0200) Subject: gnutls: honor connect timeout X-Git-Tag: v1.15~41 X-Git-Url: http://sjero.net/git/?p=wget;a=commitdiff_plain;h=91f0f99e9a607cc72cd5dc8aa1f57a299f30948e gnutls: honor connect timeout --- diff --git a/src/ChangeLog b/src/ChangeLog index 186b33d4..8822973e 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2013-07-11 Tim Ruehsen + + * gnutls.c (ssl_connect_wget): respect connect timeout. + 2013-07-11 Tomas Hozza * ftp.c (ftp_loop): Use ftp_retrieve_glob() also in case diff --git a/src/gnutls.c b/src/gnutls.c index 54422fcf..06f90200 100644 --- a/src/gnutls.c +++ b/src/gnutls.c @@ -374,6 +374,9 @@ static struct transport_implementation wgnutls_transport = bool ssl_connect_wget (int fd, const char *hostname) { +#ifdef F_GETFL + int flags = 0; +#endif struct wgnutls_transport_context *ctx; gnutls_session_t session; int err,alert; @@ -441,11 +444,54 @@ ssl_connect_wget (int fd, const char *hostname) return false; } + if (opt.connect_timeout) + { +#ifdef F_GETFL + flags = fcntl (fd, F_GETFL, 0); + if (flags < 0) + return flags; + if (fcntl (fd, F_SETFL, flags | O_NONBLOCK)) + return -1; +#else + /* XXX: Assume it was blocking before. */ + const int one = 1; + if (ioctl (fd, FIONBIO, &one) < 0) + return -1; +#endif + } + /* We don't stop the handshake process for non-fatal errors */ do { err = gnutls_handshake (session); - if (err < 0) + + if (opt.connect_timeout && err == GNUTLS_E_AGAIN) + { + if (gnutls_record_get_direction (session)) + { + /* wait for writeability */ + err = select_fd (fd, opt.connect_timeout, WAIT_FOR_WRITE); + } + else + { + /* wait for readability */ + err = select_fd (fd, opt.connect_timeout, WAIT_FOR_READ); + } + + if (err <= 0) + { + if (err == 0) + { + errno = ETIMEDOUT; + err = -1; + } + break; + } + + if (err <= 0) + break; + } + else if (err < 0) { logprintf (LOG_NOTQUIET, "GnuTLS: %s\n", gnutls_strerror (err)); if (err == GNUTLS_E_WARNING_ALERT_RECEIVED || @@ -461,6 +507,18 @@ ssl_connect_wget (int fd, const char *hostname) } while (err == GNUTLS_E_WARNING_ALERT_RECEIVED && gnutls_error_is_fatal (err) == 0); + if (opt.connect_timeout) + { +#ifdef F_GETFL + if (fcntl (fd, F_SETFL, flags) < 0) + return -1; +#else + const int zero = 0; + if (ioctl (fd, FIONBIO, &zero) < 0) + return -1; +#endif + } + if (err < 0) { gnutls_deinit (session);