Blame SOURCES/wget-1.14-sslreadtimeout.patch

87e294
diff -up wget-1.14/src/openssl.c.ssltimeout wget-1.14/src/openssl.c
87e294
--- wget-1.14/src/openssl.c.ssltimeout	2012-08-09 14:30:14.987964706 +0200
87e294
+++ wget-1.14/src/openssl.c	2012-08-09 14:44:05.467660741 +0200
87e294
@@ -256,19 +256,42 @@ struct openssl_transport_context {
87e294
   char *last_error;             /* last error printed with openssl_errstr */
87e294
 };
87e294
 
87e294
-static int
87e294
-openssl_read (int fd, char *buf, int bufsize, void *arg)
87e294
-{
87e294
-  int ret;
87e294
-  struct openssl_transport_context *ctx = arg;
87e294
+struct openssl_read_args {
87e294
+  int fd;
87e294
+  struct openssl_transport_context *ctx;
87e294
+  char *buf;
87e294
+  int bufsize;
87e294
+  int retval;
87e294
+};
87e294
+
87e294
+static void openssl_read_callback(void *arg) {
87e294
+  struct openssl_read_args *args = (struct openssl_read_args *) arg;
87e294
+  struct openssl_transport_context *ctx = args->ctx;
87e294
   SSL *conn = ctx->conn;
87e294
+  char *buf = args->buf;
87e294
+  int bufsize = args->bufsize;
87e294
+  int ret;
87e294
+
87e294
   do
87e294
     ret = SSL_read (conn, buf, bufsize);
87e294
-  while (ret == -1
87e294
-         && SSL_get_error (conn, ret) == SSL_ERROR_SYSCALL
87e294
+  while (ret == -1 && SSL_get_error (conn, ret) == SSL_ERROR_SYSCALL
87e294
          && errno == EINTR);
87e294
+  args->retval = ret;
87e294
+}
87e294
 
87e294
-  return ret;
87e294
+static int
87e294
+openssl_read (int fd, char *buf, int bufsize, void *arg)
87e294
+{
87e294
+  struct openssl_read_args args;
87e294
+  args.fd = fd;
87e294
+  args.buf = buf;
87e294
+  args.bufsize = bufsize;
87e294
+  args.ctx = (struct openssl_transport_context*) arg;
87e294
+
87e294
+  if (run_with_timeout(opt.read_timeout, openssl_read_callback, &args)) {
87e294
+    return -1;
87e294
+  }
87e294
+  return args.retval;
87e294
 }
87e294
 
87e294
 static int
87e294
@@ -386,6 +409,18 @@ static struct transport_implementation o
87e294
   openssl_peek, openssl_errstr, openssl_close
87e294
 };
87e294
 
87e294
+struct scwt_context {
87e294
+  SSL *ssl;
87e294
+  int result;
87e294
+};
87e294
+
87e294
+static void
87e294
+ssl_connect_with_timeout_callback(void *arg)
87e294
+{
87e294
+  struct scwt_context *ctx = (struct scwt_context *)arg;
87e294
+  ctx->result = SSL_connect(ctx->ssl);
87e294
+}
87e294
+
87e294
 /* Perform the SSL handshake on file descriptor FD, which is assumed
87e294
    to be connected to an SSL server.  The SSL handle provided by
87e294
    OpenSSL is registered with the file descriptor FD using
87e294
@@ -398,6 +433,7 @@ bool
87e294
 ssl_connect_wget (int fd, const char *hostname)
87e294
 {
87e294
   SSL *conn;
87e294
+  struct scwt_context scwt_ctx;
87e294
   struct openssl_transport_context *ctx;
87e294
 
87e294
   DEBUGP (("Initiating SSL handshake.\n"));
87e294
@@ -425,7 +461,14 @@ ssl_connect_wget (int fd, const char *ho
87e294
   if (!SSL_set_fd (conn, FD_TO_SOCKET (fd)))
87e294
     goto error;
87e294
   SSL_set_connect_state (conn);
87e294
-  if (SSL_connect (conn) <= 0 || conn->state != SSL_ST_OK)
87e294
+
87e294
+  scwt_ctx.ssl = conn;
87e294
+  if (run_with_timeout(opt.read_timeout, ssl_connect_with_timeout_callback, 
87e294
+                       &scwt_ctx)) {
87e294
+    DEBUGP (("SSL handshake timed out.\n"));
87e294
+    goto timeout;
87e294
+  }
87e294
+  if (scwt_ctx.result <= 0 || conn->state != SSL_ST_OK)
87e294
     goto error;
87e294
 
87e294
   ctx = xnew0 (struct openssl_transport_context);
87e294
@@ -441,6 +484,7 @@ ssl_connect_wget (int fd, const char *ho
87e294
  error:
87e294
   DEBUGP (("SSL handshake failed.\n"));
87e294
   print_errors ();
87e294
+  timeout:
87e294
   if (conn)
87e294
     SSL_free (conn);
87e294
   return false;