Add tizen dlog for debugging
[platform/upstream/glib-networking.git] / tls / gnutls / gtlsbackend-gnutls.c
index 55ec1a5..d977e1b 100644 (file)
@@ -1,11 +1,13 @@
-/* GIO - GLib Input, Output and Streaming Library
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ * GIO - GLib Input, Output and Streaming Library
  *
  * Copyright 2010 Red Hat, Inc
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -15,6 +17,9 @@
  * You should have received a copy of the GNU Lesser General
  * Public License along with this library; if not, see
  * <http://www.gnu.org/licenses/>.
+ *
+ * In addition, when the library is used with OpenSSL, a special
+ * exception applies. Refer to the LICENSE_EXCEPTION file for details.
  */
 
 #include "config.h"
 #include "gtlsfiledatabase-gnutls.h"
 #include "gtlsserverconnection-gnutls.h"
 
-struct _GTlsBackendGnutlsPrivate
+#include "TIZEN.h"
+
+struct _GTlsBackendGnutls
 {
+  GObject parent_instance;
+
   GMutex mutex;
   GTlsDatabase *default_database;
 };
@@ -40,8 +49,8 @@ struct _GTlsBackendGnutlsPrivate
 static void g_tls_backend_gnutls_interface_init (GTlsBackendInterface *iface);
 
 G_DEFINE_DYNAMIC_TYPE_EXTENDED (GTlsBackendGnutls, g_tls_backend_gnutls, G_TYPE_OBJECT, 0,
-                               G_IMPLEMENT_INTERFACE_DYNAMIC (G_TYPE_TLS_BACKEND,
-                                                              g_tls_backend_gnutls_interface_init);)
+                                G_IMPLEMENT_INTERFACE_DYNAMIC (G_TYPE_TLS_BACKEND,
+                                                               g_tls_backend_gnutls_interface_init);)
 
 #ifdef GTLS_GNUTLS_DEBUG
 static void
@@ -54,6 +63,8 @@ gtls_log_func (int level, const char *msg)
 static gpointer
 gtls_gnutls_init (gpointer data)
 {
+  GTypePlugin *plugin;
+
   gnutls_global_init ();
 
 #ifdef GTLS_GNUTLS_DEBUG
@@ -62,10 +73,14 @@ gtls_gnutls_init (gpointer data)
 #endif
 
   /* Leak the module to keep it from being unloaded. */
-  g_type_plugin_use (g_type_get_plugin (G_TYPE_TLS_BACKEND_GNUTLS));
+  plugin = g_type_get_plugin (G_TYPE_TLS_BACKEND_GNUTLS);
+  if (plugin != NULL)
+    g_type_plugin_use (plugin);
   return NULL;
 }
 
+GNUTLS_SKIP_GLOBAL_INIT
+
 static GOnce gnutls_inited = G_ONCE_INIT;
 
 static void
@@ -80,8 +95,7 @@ g_tls_backend_gnutls_init (GTlsBackendGnutls *backend)
    */
   g_once (&gnutls_inited, gtls_gnutls_init, NULL);
 
-  backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend, G_TYPE_TLS_BACKEND_GNUTLS, GTlsBackendGnutlsPrivate);
-  g_mutex_init (&backend->priv->mutex);
+  g_mutex_init (&backend->mutex);
 }
 
 static void
@@ -89,31 +103,18 @@ g_tls_backend_gnutls_finalize (GObject *object)
 {
   GTlsBackendGnutls *backend = G_TLS_BACKEND_GNUTLS (object);
 
-  if (backend->priv->default_database)
-    g_object_unref (backend->priv->default_database);
-  g_mutex_clear (&backend->priv->mutex);
+  g_clear_object (&backend->default_database);
+  g_mutex_clear (&backend->mutex);
 
   G_OBJECT_CLASS (g_tls_backend_gnutls_parent_class)->finalize (object);
 }
 
-static GTlsDatabase*
-g_tls_backend_gnutls_real_create_database (GTlsBackendGnutls  *self,
-                                           GError            **error)
-{
-  const gchar *anchor_file = NULL;
-#ifdef GTLS_SYSTEM_CA_FILE
-  anchor_file = GTLS_SYSTEM_CA_FILE;
-#endif
-  return g_tls_file_database_new (anchor_file, error);
-}
-
 static void
 g_tls_backend_gnutls_class_init (GTlsBackendGnutlsClass *backend_class)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (backend_class);
+
   gobject_class->finalize = g_tls_backend_gnutls_finalize;
-  backend_class->create_database = g_tls_backend_gnutls_real_create_database;
-  g_type_class_add_private (backend_class, sizeof (GTlsBackendGnutlsPrivate));
 }
 
 static void
@@ -128,30 +129,28 @@ g_tls_backend_gnutls_get_default_database (GTlsBackend *backend)
   GTlsDatabase *result;
   GError *error = NULL;
 
-  g_mutex_lock (&self->priv->mutex);
+  g_mutex_lock (&self->mutex);
 
-  if (self->priv->default_database)
+  if (self->default_database)
     {
-      result = g_object_ref (self->priv->default_database);
+      result = g_object_ref (self->default_database);
     }
   else
     {
-      g_assert (G_TLS_BACKEND_GNUTLS_GET_CLASS (self)->create_database);
-      result = G_TLS_BACKEND_GNUTLS_GET_CLASS (self)->create_database (self, &error);
+      result = G_TLS_DATABASE (g_tls_database_gnutls_new (&error));
       if (error)
         {
-          g_warning ("couldn't load TLS file database: %s",
-                     error->message);
+          g_warning ("Failed to load TLS database: %s", error->message);
           g_clear_error (&error);
         }
       else
         {
           g_assert (result);
-          self->priv->default_database = g_object_ref (result);
+          self->default_database = g_object_ref (result);
         }
     }
 
-  g_mutex_unlock (&self->priv->mutex);
+  g_mutex_unlock (&self->mutex);
 
   return result;
 }
@@ -164,6 +163,8 @@ g_tls_backend_gnutls_interface_init (GTlsBackendInterface *iface)
   iface->get_server_connection_type = g_tls_server_connection_gnutls_get_type;
   iface->get_file_database_type =     g_tls_file_database_gnutls_get_type;
   iface->get_default_database =       g_tls_backend_gnutls_get_default_database;
+  iface->get_dtls_client_connection_type = g_tls_client_connection_gnutls_get_type;
+  iface->get_dtls_server_connection_type = g_tls_server_connection_gnutls_get_type;
 }
 
 /* Session cache support; all the details are sort of arbitrary. Note
@@ -197,7 +198,7 @@ session_cache_cleanup (GHashTable *cache)
     {
       cache_data = value;
       if (cache_data->last_used < expired)
-       g_hash_table_iter_remove (&iter);
+        g_hash_table_iter_remove (&iter);
     }
 }
 
@@ -208,12 +209,12 @@ cache_data_free (gpointer data)
 
   g_bytes_unref (cache_data->session_id);
   g_bytes_unref (cache_data->session_data);
-  g_slice_free (GTlsBackendGnutlsCacheData, cache_data);
+  g_free (cache_data);
 }
 
 static GHashTable *
 get_session_cache (unsigned int            type,
-                  gboolean                create)
+                   gboolean                create)
 {
   GHashTable **cache_p;
 
@@ -221,15 +222,15 @@ get_session_cache (unsigned int            type,
   if (!*cache_p && create)
     {
       *cache_p = g_hash_table_new_full (g_bytes_hash, g_bytes_equal,
-                                       NULL, cache_data_free);
+                                        NULL, cache_data_free);
     }
   return *cache_p;
 }
 
 void
 g_tls_backend_gnutls_store_session (unsigned int             type,
-                                   GBytes                  *session_id,
-                                   GBytes                  *session_data)
+                                    GBytes                  *session_id,
+                                    GBytes                  *session_data)
 {
   GTlsBackendGnutlsCacheData *cache_data;
   GHashTable *cache;
@@ -241,17 +242,17 @@ g_tls_backend_gnutls_store_session (unsigned int             type,
   if (cache_data)
     {
       if (!g_bytes_equal (cache_data->session_data, session_data))
-       {
-         g_bytes_unref (cache_data->session_data);
-         cache_data->session_data = g_bytes_ref (session_data);
-       }
+        {
+          g_bytes_unref (cache_data->session_data);
+          cache_data->session_data = g_bytes_ref (session_data);
+        }
     }
   else
     {
       if (g_hash_table_size (cache) >= SESSION_CACHE_MAX_SIZE)
-       session_cache_cleanup (cache);
+        session_cache_cleanup (cache);
 
-      cache_data = g_slice_new (GTlsBackendGnutlsCacheData);
+      cache_data = g_new (GTlsBackendGnutlsCacheData, 1);
       cache_data->session_id = g_bytes_ref (session_id);
       cache_data->session_data = g_bytes_ref (session_data);
 
@@ -264,7 +265,7 @@ g_tls_backend_gnutls_store_session (unsigned int             type,
 
 void
 g_tls_backend_gnutls_remove_session (unsigned int             type,
-                                    GBytes                  *session_id)
+                                     GBytes                  *session_id)
 {
   GHashTable *cache;
 
@@ -279,7 +280,7 @@ g_tls_backend_gnutls_remove_session (unsigned int             type,
 
 GBytes *
 g_tls_backend_gnutls_lookup_session (unsigned int             type,
-                                    GBytes                  *session_id)
+                                     GBytes                  *session_id)
 {
   GTlsBackendGnutlsCacheData *cache_data;
   GBytes *session_data = NULL;
@@ -292,10 +293,10 @@ g_tls_backend_gnutls_lookup_session (unsigned int             type,
     {
       cache_data = g_hash_table_lookup (cache, session_id);
       if (cache_data)
-       {
-         cache_data->last_used = time (NULL);
-         session_data = g_bytes_ref (cache_data->session_data);
-       }
+        {
+          cache_data->last_used = time (NULL);
+          session_data = g_bytes_ref (cache_data->session_data);
+        }
     }
 
   G_UNLOCK (session_cache_lock);
@@ -307,8 +308,10 @@ void
 g_tls_backend_gnutls_register (GIOModule *module)
 {
   g_tls_backend_gnutls_register_type (G_TYPE_MODULE (module));
+  if (module == NULL)
+    g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
   g_io_extension_point_implement (G_TLS_BACKEND_EXTENSION_POINT_NAME,
-                                 g_tls_backend_gnutls_get_type(),
-                                 "gnutls",
-                                 0);
+                                  g_tls_backend_gnutls_get_type(),
+                                  "gnutls",
+                                  0);
 }