]> sjero.net Git - wget/blobdiff - src/gnutls.c
Fix GCC warnings.
[wget] / src / gnutls.c
index 17c7c9287662b10292cc5c7b22babd33b3ae750e..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,7 +174,7 @@ 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 = MIN (bufsize, ctx->peeklen);
   if (bufsize > sizeof ctx->peekbuf)
@@ -182,24 +185,30 @@ wgnutls_peek (int fd, char *buf, int bufsize, void *arg)
 
   if (bufsize > offset)
     {
-      do
+      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 (gnutls_record_check_pending (ctx->session)
-              || select_fd (fd, 0, WAIT_FOR_READ))
-            ret = gnutls_record_recv (ctx->session, buf + offset,
-                                      bufsize - offset);
+          if (offset)
+            read = 0;
+          else
+            return read;
         }
-      while (ret == GNUTLS_E_INTERRUPTED);
 
-      if (ret > 0)
+      if (read > 0)
         {
           memcpy (ctx->peekbuf + offset, buf + offset,
-                  ret);
-          ctx->peeklen += ret;
+                  read);
+          ctx->peeklen += read;
         }
     }
 
-  return offset + ret;
+  return offset + read;
 }
 
 static const char *
@@ -222,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
 };