]> sjero.net Git - wget/commitdiff
gnutls: honor connect timeout
authorTim Ruehsen <tim.ruehsen@gmx.de>
Thu, 11 Jul 2013 12:29:20 +0000 (14:29 +0200)
committerGiuseppe Scrivano <gscrivano@gnu.org>
Thu, 11 Jul 2013 22:07:05 +0000 (00:07 +0200)
src/ChangeLog
src/gnutls.c

index 186b33d4723b56afcb3a4a86ace58fad4bf8bd1a..8822973ed75a076f91d0b811dda1d7852a8a96dd 100644 (file)
@@ -1,3 +1,7 @@
+2013-07-11  Tim Ruehsen  <tim.ruehsen@gmx.de>
+
+        * gnutls.c (ssl_connect_wget): respect connect timeout.
+
 2013-07-11  Tomas Hozza  <thozza@redhat.com>
 
        * ftp.c (ftp_loop): Use ftp_retrieve_glob() also in case
index 54422fcf4930d62b954f93f8334cabffa8affdc6..06f90200c514ca303c8dd3739c7c59c7cbcafe52 100644 (file)
@@ -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);