[glib-networking/mcatanzaro/tls-thread: 20/26] progress



commit 2a8211453bd9f75671d15b4ab039d4cdffde6be0
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Mon Dec 23 16:11:18 2019 -0600

    progress

 tls/base/gtlsconnection-base.c             | 23 +++++++++++---
 tls/base/gtlsconnection-base.h             |  2 ++
 tls/base/gtlsoperationsthread-base.c       |  8 ++---
 tls/gnutls/gtlsclientconnection-gnutls.c   |  7 +++--
 tls/gnutls/gtlsconnection-gnutls.c         |  2 +-
 tls/gnutls/gtlsoperationsthread-gnutls.c   | 49 +++++++++++++++++++-----------
 tls/openssl/gtlsoperationsthread-openssl.c | 12 +++++---
 tls/openssl/gtlsoperationsthread-openssl.h |  4 ++-
 8 files changed, 72 insertions(+), 35 deletions(-)
---
diff --git a/tls/base/gtlsconnection-base.c b/tls/base/gtlsconnection-base.c
index 88b47b2..4391625 100644
--- a/tls/base/gtlsconnection-base.c
+++ b/tls/base/gtlsconnection-base.c
@@ -235,8 +235,12 @@ g_tls_connection_base_initable_init (GInitable    *initable,
   GTlsConnectionBasePrivate *priv = g_tls_connection_base_get_instance_private (tls);
 
   priv->thread = G_TLS_CONNECTION_BASE_GET_CLASS (tls)->create_op_thread (tls);
+  if (!priv->thread)
+    return FALSE;
+
   if (priv->certificate)
     g_tls_operations_thread_base_set_own_certificate (priv->thread, priv->certificate);
+
   if (priv->interaction)
     g_tls_operations_thread_base_set_interaction (priv->thread, priv->interaction);
 
@@ -748,7 +752,7 @@ yield_op (GTlsConnectionBase       *tls,
   g_mutex_unlock (&priv->op_mutex);
 }
 
-/* FIXME: removable? */
+/* FIXME: removable? It's only here for OpenSSL GTlsBio */
 void
 g_tls_connection_base_push_io (GTlsConnectionBase *tls,
                                GIOCondition        direction,
@@ -758,15 +762,22 @@ g_tls_connection_base_push_io (GTlsConnectionBase *tls,
   g_assert (direction & (G_IO_IN | G_IO_OUT));
   g_return_if_fail (G_IS_TLS_CONNECTION_BASE (tls));
 
-  G_TLS_CONNECTION_BASE_GET_CLASS (tls)->push_io (tls, direction,
-                                                  timeout, cancellable);
+  if (G_TLS_CONNECTION_BASE_GET_CLASS (tls)->push_io)
+    {
+      G_TLS_CONNECTION_BASE_GET_CLASS (tls)->push_io (tls, direction,
+                                                      timeout, cancellable);
+    }
 }
 
 /* FIXME: rename, if push_io is removed? */
+/* FIXME: this is almost certainly inappropriate because it is called on the
+ * op thread. It needs to move to the op thread class.
+ */
 static GTlsConnectionBaseStatus
 g_tls_connection_base_real_pop_io (GTlsConnectionBase  *tls,
                                    GIOCondition         direction,
                                    gboolean             success,
+                                   GError              *op_error,
                                    GError             **error)
 {
   GTlsConnectionBasePrivate *priv = g_tls_connection_base_get_instance_private (tls);
@@ -777,6 +788,9 @@ g_tls_connection_base_real_pop_io (GTlsConnectionBase  *tls,
   if (success)
     return G_TLS_CONNECTION_BASE_OK;
 
+  g_assert (op_error);
+  g_propagate_error (&my_error, op_error);
+
   if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
     {
       g_propagate_error (error, my_error);
@@ -831,6 +845,7 @@ GTlsConnectionBaseStatus
 g_tls_connection_base_pop_io (GTlsConnectionBase  *tls,
                               GIOCondition         direction,
                               gboolean             success,
+                              GError              *op_error,
                               GError             **error)
 {
   g_assert (direction & (G_IO_IN | G_IO_OUT));
@@ -838,7 +853,7 @@ g_tls_connection_base_pop_io (GTlsConnectionBase  *tls,
   g_return_val_if_fail (G_IS_TLS_CONNECTION_BASE (tls), G_TLS_CONNECTION_BASE_ERROR);
 
   return G_TLS_CONNECTION_BASE_GET_CLASS (tls)->pop_io (tls, direction,
-                                                        success, error);
+                                                        success, op_error, error);
 }
 
 /* Checks whether the underlying base stream or GDatagramBased meets
diff --git a/tls/base/gtlsconnection-base.h b/tls/base/gtlsconnection-base.h
index 3b8f6e6..8c5a200 100644
--- a/tls/base/gtlsconnection-base.h
+++ b/tls/base/gtlsconnection-base.h
@@ -65,6 +65,7 @@ struct _GTlsConnectionBaseClass
   GTlsConnectionBaseStatus    (*pop_io)                     (GTlsConnectionBase   *tls,
                                                              GIOCondition          direction,
                                                              gboolean              success,
+                                                             GError               *op_error,
                                                              GError              **error);
 
   void                        (*set_accepted_cas)           (GTlsConnectionBase    *tls,
@@ -78,6 +79,7 @@ void                      g_tls_connection_base_push_io                 (GTlsCon
 GTlsConnectionBaseStatus  g_tls_connection_base_pop_io                  (GTlsConnectionBase  *tls,
                                                                          GIOCondition         direction,
                                                                          gboolean             success,
+                                                                         GError              *op_error,
                                                                          GError             **error);
 
 gssize                    g_tls_connection_base_read                    (GTlsConnectionBase  *tls,
diff --git a/tls/base/gtlsoperationsthread-base.c b/tls/base/gtlsoperationsthread-base.c
index bb135f8..46ead3b 100644
--- a/tls/base/gtlsoperationsthread-base.c
+++ b/tls/base/gtlsoperationsthread-base.c
@@ -291,14 +291,13 @@ g_tls_operations_thread_base_get_is_missing_requested_client_certificate (GTlsOp
 }
 
 static HandshakeContext *
-handshake_context_new (GMainContext              *caller_context,
-                       GTlsVerifyCertificateFunc  verify_callback,
+handshake_context_new (GTlsVerifyCertificateFunc  verify_callback,
                        gpointer                   user_data)
 {
   HandshakeContext *context;
 
   context = g_new0 (HandshakeContext, 1);
-  context->caller_context = g_main_context_ref (caller_context);
+  context->caller_context = g_main_context_ref_thread_default ();
   context->verify_callback = verify_callback;
   context->user_data = user_data;
 
@@ -742,8 +741,7 @@ g_tls_operations_thread_base_handshake (GTlsOperationsThreadBase   *self,
   priv->missing_requested_client_certificate = FALSE;
   g_mutex_unlock (&priv->mutex);
 
-  context = handshake_context_new (g_main_context_get_thread_default (),
-                                   verify_callback,
+  context = handshake_context_new (verify_callback,
                                    user_data);
 
   op = g_tls_thread_handshake_operation_new (self,
diff --git a/tls/gnutls/gtlsclientconnection-gnutls.c b/tls/gnutls/gtlsclientconnection-gnutls.c
index 96d2893..a30f169 100644
--- a/tls/gnutls/gtlsclientconnection-gnutls.c
+++ b/tls/gnutls/gtlsclientconnection-gnutls.c
@@ -110,12 +110,14 @@ g_tls_client_connection_gnutls_initable_init (GInitable       *initable,
                                               GError         **error)
 {
   GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (initable);
-  GTlsOperationsThreadBase *thread = g_tls_connection_base_get_op_thread (G_TLS_CONNECTION_BASE (gnutls));
+  GTlsOperationsThreadBase *thread;
   const gchar *hostname;
 
   if (!g_tls_client_connection_gnutls_parent_initable_iface->init (initable, cancellable, error))
     return FALSE;
 
+  thread = g_tls_connection_base_get_op_thread (G_TLS_CONNECTION_BASE (gnutls));
+
   hostname = get_server_identity (G_TLS_CLIENT_CONNECTION_GNUTLS (gnutls));
   if (hostname)
     g_tls_operations_thread_base_set_server_identity (thread, hostname);
@@ -180,7 +182,8 @@ g_tls_client_connection_gnutls_set_property (GObject      *object,
           GTlsOperationsThreadBase *thread;
 
           thread = g_tls_connection_base_get_op_thread (G_TLS_CONNECTION_BASE (gnutls));
-          g_tls_operations_thread_base_set_server_identity (thread, hostname);
+          if (thread)
+            g_tls_operations_thread_base_set_server_identity (thread, hostname);
         }
       break;
 
diff --git a/tls/gnutls/gtlsconnection-gnutls.c b/tls/gnutls/gtlsconnection-gnutls.c
index 90e96aa..f7aa928 100644
--- a/tls/gnutls/gtlsconnection-gnutls.c
+++ b/tls/gnutls/gtlsconnection-gnutls.c
@@ -107,7 +107,7 @@ g_tls_connection_gnutls_create_op_thread (GTlsConnectionBase *tls)
                 NULL);
 
   /* Ensure we are in TLS mode or DTLS mode. */
-  g_return_val_if_fail (!!base_io_stream != !!base_socket, FALSE);
+  g_assert (!!base_io_stream != !!base_socket);
 
   if (base_socket)
     flags |= GNUTLS_DATAGRAM;
diff --git a/tls/gnutls/gtlsoperationsthread-gnutls.c b/tls/gnutls/gtlsoperationsthread-gnutls.c
index d0185f6..29a91be 100644
--- a/tls/gnutls/gtlsoperationsthread-gnutls.c
+++ b/tls/gnutls/gtlsoperationsthread-gnutls.c
@@ -121,6 +121,22 @@ is_server (GTlsOperationsThreadGnutls *self)
   return self->init_flags & GNUTLS_SERVER;
 }
 
+static void
+begin_gnutls_io (GTlsOperationsThreadGnutls *self,
+                 GIOCondition                direction,
+                 GCancellable               *cancellable)
+{
+  GTlsConnectionBase *tls;
+
+  tls = g_tls_operations_thread_base_get_connection (G_TLS_OPERATIONS_THREAD_BASE (self));
+
+  g_assert (!self->op_error);
+  g_assert (!self->op_cancellable);
+  self->op_cancellable = cancellable;
+
+  g_tls_connection_base_push_io (tls, direction, 0, cancellable);
+}
+
 static GTlsConnectionBaseStatus
 end_gnutls_io (GTlsOperationsThreadGnutls  *self,
                GIOCondition                 direction,
@@ -131,6 +147,7 @@ end_gnutls_io (GTlsOperationsThreadGnutls  *self,
   GTlsConnectionBase *tls;
   GTlsConnectionBaseStatus status;
   GError *my_error = NULL;
+  GError *op_error = NULL;
 
   /* We intentionally do not check for GNUTLS_E_INTERRUPTED here
    * Instead, the caller may poll for the source to become ready again.
@@ -142,9 +159,12 @@ end_gnutls_io (GTlsOperationsThreadGnutls  *self,
       ret == GNUTLS_E_WARNING_ALERT_RECEIVED)
     return G_TLS_CONNECTION_BASE_TRY_AGAIN;
 
+  self->op_cancellable = NULL;
+  op_error = g_steal_pointer (&self->op_error);
+
   tls = g_tls_operations_thread_base_get_connection (G_TLS_OPERATIONS_THREAD_BASE (self));
 
-  status = g_tls_connection_base_pop_io (tls, direction, ret >= 0, &my_error);
+  status = g_tls_connection_base_pop_io (tls, direction, ret >= 0, op_error, &my_error);
   if (status == G_TLS_CONNECTION_BASE_OK ||
       status == G_TLS_CONNECTION_BASE_WOULD_BLOCK ||
       status == G_TLS_CONNECTION_BASE_TIMED_OUT)
@@ -269,21 +289,12 @@ end_gnutls_io (GTlsOperationsThreadGnutls  *self,
 /* FIXME: do not use GTlsConnectionBase at all. */
 
 #define BEGIN_GNUTLS_IO(self, direction, cancellable)          \
-  g_assert (!self->op_error);                                  \
-  g_assert (!self->op_cancellable);                            \
-  self->op_cancellable = cancellable;                          \
-  g_tls_connection_base_push_io (g_tls_operations_thread_base_get_connection (G_TLS_OPERATIONS_THREAD_BASE 
(self)),        \
-                                 direction, 0, cancellable);   \
+  begin_gnutls_io (self, direction, cancellable);              \
   do {
 
 #define END_GNUTLS_IO(self, direction, ret, status, errmsg, err)      \
     status = end_gnutls_io (self, direction, ret, err, errmsg);       \
-  } while (status == G_TLS_CONNECTION_BASE_TRY_AGAIN);                \
-  self->op_cancellable = NULL;                                        \
-  if (self->op_error) {                                               \
-    g_propagate_error (err, self->op_error);                          \
-    self->op_error = NULL;                                            \
-  }
+  } while (status == G_TLS_CONNECTION_BASE_TRY_AGAIN);
 
 static void
 initialize_gnutls_priority (void)
@@ -1386,15 +1397,19 @@ g_tls_operations_thread_gnutls_set_property (GObject      *object,
   switch (prop_id)
     {
     case PROP_BASE_IO_STREAM:
-      g_assert (!self->base_socket);
       self->base_iostream = g_value_get_object (value);
-      self->base_istream = g_io_stream_get_input_stream (self->base_iostream);
-      self->base_ostream = g_io_stream_get_output_stream (self->base_iostream);
+      if (self->base_iostream)
+        {
+          self->base_istream = g_io_stream_get_input_stream (self->base_iostream);
+          self->base_ostream = g_io_stream_get_output_stream (self->base_iostream);
+          g_assert (!self->base_socket);
+        }
       break;
 
     case PROP_BASE_SOCKET:
-      g_assert (!self->base_iostream);
       self->base_socket = g_value_get_object (value);
+      if (self->base_socket)
+        g_assert (!self->base_iostream);
       break;
 
     case PROP_GNUTLS_FLAGS:
@@ -1566,7 +1581,7 @@ g_tls_operations_thread_gnutls_new (GTlsConnectionGnutls *connection,
 {
   return g_initable_new (G_TYPE_TLS_OPERATIONS_THREAD_GNUTLS,
                          NULL, NULL,
-                         "base-iostream", base_iostream,
+                         "base-io-stream", base_iostream,
                          "base-socket", base_socket,
                          "gnutls-flags", flags,
                          "tls-connection", connection,
diff --git a/tls/openssl/gtlsoperationsthread-openssl.c b/tls/openssl/gtlsoperationsthread-openssl.c
index 3825a39..9a0a36c 100644
--- a/tls/openssl/gtlsoperationsthread-openssl.c
+++ b/tls/openssl/gtlsoperationsthread-openssl.c
@@ -316,10 +316,12 @@ g_tls_operations_thread_openssl_initable_iface_init (GInitableIface *iface)
 }
 
 GTlsOperationsThreadBase *
-g_tls_operations_thread_openssl_new (GTlsConnectionOpenssl *tls)
+g_tls_operations_thread_openssl_new (GTlsConnectionOpenssl *tls,
+                                     GIOStream             *base_iostream)
 {
-  return g_initable_init (G_TYPE_TLS_OPERATIONS_THREAD_OPENSSL,
-                          NULL, NULL,
-                          "tls-connection", tls,
-                          NULL);
+  return g_initable_new (G_TYPE_TLS_OPERATIONS_THREAD_OPENSSL,
+                         NULL, NULL,
+                         "base-iostream", base_iostream,
+                         "tls-connection", tls,
+                         NULL);
 }
diff --git a/tls/openssl/gtlsoperationsthread-openssl.h b/tls/openssl/gtlsoperationsthread-openssl.h
index da6daa4..7441f3c 100644
--- a/tls/openssl/gtlsoperationsthread-openssl.h
+++ b/tls/openssl/gtlsoperationsthread-openssl.h
@@ -3,6 +3,7 @@
  * GIO - GLib Input, Output and Streaming Library
  *
  * Copyright 2019 Igalia S.L.
+ * Copyright 2019 Metrological Group B.V.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -35,6 +36,7 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GTlsOperationsThreadOpenssl, g_tls_operations_thread_openssl, G, 
TLS_OPERATIONS_THREAD_OPENSSL, GTlsOperationsThreadBase)
 
-GTlsOperationsThreadBase *g_tls_operations_thread_openssl_new (GTlsConnectionOpenssl *tls);
+GTlsOperationsThreadBase *g_tls_operations_thread_openssl_new (GTlsConnectionOpenssl *tls,
+                                                               GIOStream             *base_iostream);
 
 G_END_DECLS


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]