]> sjero.net Git - wget/blobdiff - src/gnutls.c
Fix GCC warnings.
[wget] / src / gnutls.c
index adba856044e4f0eaa9bf9fc8906671c849d6e8c2..db9241678c45bf58d7662028112d9fe0526fe1a0 100644 (file)
@@ -1,5 +1,5 @@
 /* SSL support via GnuTLS library.
-   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
+   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software
    Foundation, Inc.
 
 This file is part of GNU Wget.
@@ -32,9 +32,7 @@ as that of the covered work.  */
 
 #include <assert.h>
 #include <errno.h>
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
+#include <unistd.h>
 #include <string.h>
 #include <stdio.h>
 #include <dirent.h>
@@ -42,6 +40,7 @@ as that of the covered work.  */
 
 #include <gnutls/gnutls.h>
 #include <gnutls/x509.h>
+#include <sys/ioctl.h>
 
 #include "utils.h"
 #include "connect.h"
@@ -67,6 +66,8 @@ ssl_init ()
 
   gnutls_global_init ();
   gnutls_certificate_allocate_credentials (&credentials);
+  gnutls_certificate_set_verify_flags(credentials,
+                                      GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
 
   ca_directory = opt.ca_directory ? opt.ca_directory : "/etc/ssl/certs";
 
@@ -104,7 +105,8 @@ ssl_init ()
   return true;
 }
 
-struct wgnutls_transport_context {
+struct wgnutls_transport_context
+{
   gnutls_session session;       /* GnuTLS session handle */
   int last_error;               /* last error returned by read/write/... */
 
@@ -140,10 +142,11 @@ wgnutls_read (int fd, char *buf, int bufsize, void *arg)
 
   do
     ret = gnutls_record_recv (ctx->session, buf, bufsize);
-  while (ret == GNUTLS_E_INTERRUPTED);
+  while (ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN);
 
   if (ret < 0)
     ctx->last_error = ret;
+
   return ret;
 }
 
@@ -154,7 +157,7 @@ wgnutls_write (int fd, char *buf, int bufsize, void *arg)
   struct wgnutls_transport_context *ctx = arg;
   do
     ret = gnutls_record_send (ctx->session, buf, bufsize);
-  while (ret == GNUTLS_E_INTERRUPTED);
+  while (ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN);
   if (ret < 0)
     ctx->last_error = ret;
   return ret;
@@ -171,30 +174,41 @@ wgnutls_poll (int fd, double timeout, int wait_for, void *arg)
 static int
 wgnutls_peek (int fd, char *buf, int bufsize, void *arg)
 {
-  int ret = 0;
+  int read = 0;
   struct wgnutls_transport_context *ctx = arg;
-  int offset = ctx->peeklen;
-
+  int offset = MIN (bufsize, ctx->peeklen);
   if (bufsize > sizeof ctx->peekbuf)
     bufsize = sizeof ctx->peekbuf;
 
-  if (offset)
+  if (ctx->peeklen)
     memcpy (buf, ctx->peekbuf, offset);
 
-  do
+  if (bufsize > offset)
     {
-      if (gnutls_record_check_pending (ctx->session)
-          || select_fd (fd, 0, WAIT_FOR_READ))
-        ret = gnutls_record_recv (ctx->session, buf + offset, bufsize - offset);
-    }
-  while (ret == GNUTLS_E_INTERRUPTED);
+      if (gnutls_record_check_pending (ctx->session) <= 0
+          && select_fd (fd, 0.0, WAIT_FOR_READ) <= 0)
+        read = 0;
+      else
+        read = gnutls_record_recv (ctx->session, buf + offset,
+                                   bufsize - offset);
+        
+      if (read < 0)
+        {
+          if (offset)
+            read = 0;
+          else
+            return read;
+        }
 
-  if (ret > 0)
-    {
-      memcpy (ctx->peekbuf + offset, buf + offset, ret);
-      ctx->peeklen += ret;
+      if (read > 0)
+        {
+          memcpy (ctx->peekbuf + offset, buf + offset,
+                  read);
+          ctx->peeklen += read;
+        }
     }
-  return ctx->peeklen;
+
+  return offset + read;
 }
 
 static const char *
@@ -217,7 +231,8 @@ wgnutls_close (int fd, void *arg)
 /* gnutls_transport is the singleton that describes the SSL transport
    methods provided by this file.  */
 
-static struct transport_implementation wgnutls_transport = {
+static struct transport_implementation wgnutls_transport =
+{
   wgnutls_read, wgnutls_write, wgnutls_poll,
   wgnutls_peek, wgnutls_errstr, wgnutls_close
 };