-ensure stats queues do not grow too big
[oweals/gnunet.git] / src / gns / gnunet-service-gns.c
index d9eae6a0976dee1f04861143691fa58fdf1c559e..e2bb0ad8c4f658fae2e78aa1bd54b8aa49986fac 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2011-2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2011-2013 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 /**
  * @file gns/gnunet-service-gns.c
@@ -30,6 +30,7 @@
 #include "gnunet_dht_service.h"
 #include "gnunet_namecache_service.h"
 #include "gnunet_namestore_service.h"
+#include "gnunet_identity_service.h"
 #include "gnunet_gns_service.h"
 #include "gnunet_statistics_service.h"
 #include "gns.h"
@@ -148,6 +149,17 @@ static struct GNUNET_NAMESTORE_Handle *namestore_handle;
  */
 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
 
+/**
+ * Our handle to the identity service
+ */
+static struct GNUNET_IDENTITY_Handle *identity_handle;
+
+/**
+ * Our handle to the identity operation to find the master zone
+ * for intercepted queries.
+ */
+static struct GNUNET_IDENTITY_Operation *identity_op;
+
 /**
  * Handle to iterate over our authoritative zone in namestore
  */
@@ -218,7 +230,7 @@ static struct GNUNET_TIME_Relative zone_publish_time_window;
 /**
  * zone publish task
  */
-static GNUNET_SCHEDULER_TaskIdentifier zone_publish_task;
+static struct GNUNET_SCHEDULER_Task * zone_publish_task;
 
 /**
  * #GNUNET_YES if zone has never been published before
@@ -248,24 +260,37 @@ static struct GNUNET_STATISTICS_Handle *statistics;
  * @param tc unused
  */
 static void
-shutdown_task (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_task (void *cls)
 {
   struct ClientLookupHandle *clh;
   struct MonitorActivity *ma;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Shutting down!\n");
-  GNUNET_SERVER_notification_context_destroy (nc);
+  if (NULL != nc)
+  {
+    GNUNET_SERVER_notification_context_destroy (nc);
+    nc = NULL;
+  }
   while (NULL != (clh = clh_head))
   {
-    GNUNET_SERVER_client_set_user_context (clh->client, (void *)NULL);
+    GNUNET_SERVER_client_set_user_context (clh->client, NULL);
     GNS_resolver_lookup_cancel (clh->lookup);
     GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
     GNUNET_free (clh);
   }
 
   GNS_interceptor_done ();
+  if (NULL != identity_op)
+  {
+    GNUNET_IDENTITY_cancel (identity_op);
+    identity_op = NULL;
+  }
+  if (NULL != identity_handle)
+  {
+    GNUNET_IDENTITY_disconnect (identity_handle);
+    identity_handle = NULL;
+  }
   GNS_resolver_done ();
   GNS_shorten_done ();
   while (NULL != (ma = ma_head))
@@ -281,10 +306,10 @@ shutdown_task (void *cls,
     GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
     statistics = NULL;
   }
-  if (GNUNET_SCHEDULER_NO_TASK != zone_publish_task)
+  if (NULL != zone_publish_task)
   {
     GNUNET_SCHEDULER_cancel (zone_publish_task);
-    zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
+    zone_publish_task = NULL;
   }
   if (NULL != namestore_iter)
   {
@@ -323,13 +348,11 @@ shutdown_task (void *cls,
  * Method called periodically that triggers iteration over authoritative records
  *
  * @param cls closure
- * @param tc task context
  */
 static void
-publish_zone_dht_next (void *cls,
-                       const struct GNUNET_SCHEDULER_TaskContext *tc)
+publish_zone_dht_next (void *cls)
 {
-  zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
+  zone_publish_task = NULL;
   GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
 }
 
@@ -338,11 +361,9 @@ publish_zone_dht_next (void *cls,
  * Periodically iterate over our zone and store everything in dht
  *
  * @param cls NULL
- * @param tc task context
  */
 static void
-publish_zone_dht_start (void *cls,
-                       const struct GNUNET_SCHEDULER_TaskContext *tc);
+publish_zone_dht_start (void *cls);
 
 
 /**
@@ -489,7 +510,6 @@ perform_dht_put (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
                         block_size,
                         block,
                         expire,
-                        DHT_OPERATION_TIMEOUT,
                         &dht_put_continuation,
                         pc_arg);
   GNUNET_free (block);
@@ -603,13 +623,11 @@ put_gns_record (void *cls,
  * Periodically iterate over all zones and store everything in DHT
  *
  * @param cls NULL
- * @param tc task context
  */
 static void
-publish_zone_dht_start (void *cls,
-                       const struct GNUNET_SCHEDULER_TaskContext *tc)
+publish_zone_dht_start (void *cls)
 {
-  zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
+  zone_publish_task = NULL;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Starting DHT zone update!\n");
@@ -688,9 +706,8 @@ send_lookup_response (void* cls,
   size_t len;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Sending `%s' message with %d results\n",
-              "LOOKUP_RESULT",
-             rd_count);
+             "Sending LOOKUP_RESULT message with %u results\n",
+             (unsigned int) rd_count);
 
   len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
   rmsg = GNUNET_malloc (len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage));
@@ -706,7 +723,7 @@ send_lookup_response (void* cls,
                                              GNUNET_NO);
   GNUNET_free (rmsg);
   GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
-  GNUNET_SERVER_client_set_user_context (clh->client, (void *)NULL);
+  GNUNET_SERVER_client_set_user_context (clh->client, NULL);
   GNUNET_free (clh);
   GNUNET_STATISTICS_update (statistics,
                             "Completed lookups", 1,
@@ -739,8 +756,7 @@ handle_lookup (void *cls,
   const struct GNUNET_GNS_ClientLookupMessage *sh_msg;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Received `%s' message\n",
-             "LOOKUP");
+             "Received LOOKUP message\n");
   msg_size = ntohs (message->size);
   if (msg_size < sizeof (struct GNUNET_GNS_ClientLookupMessage))
   {
@@ -835,6 +851,52 @@ monitor_sync_event (void *cls)
 }
 
 
+/**
+ * Method called to inform about the ego to be used for the master zone
+ * for DNS interceptions.
+ *
+ * This function is only called ONCE, and 'NULL' being passed in
+ * @a ego does indicate that interception is not configured.
+ * If @a ego is non-NULL, we should start to intercept DNS queries
+ * and resolve ".gnu" queries using the given ego as the master zone.
+ *
+ * @param cls closure, our `const struct GNUNET_CONFIGURATION_Handle *c`
+ * @param ego ego handle
+ * @param ctx context for application to store data for this ego
+ *                 (during the lifetime of this process, initially NULL)
+ * @param name name assigned by the user for this ego,
+ *                   NULL if the user just deleted the ego and it
+ *                   must thus no longer be used
+ */
+static void
+identity_intercept_cb (void *cls,
+                       struct GNUNET_IDENTITY_Ego *ego,
+                       void **ctx,
+                       const char *name)
+{
+  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
+  struct GNUNET_CRYPTO_EcdsaPublicKey dns_root;
+
+  identity_op = NULL;
+  if (NULL == ego)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+               _("No ego configured for `%s`\n"),
+               "gns-intercept");
+    return;
+  }
+  GNUNET_IDENTITY_ego_get_public_key (ego,
+                                     &dns_root);
+  if (GNUNET_SYSERR ==
+      GNS_interceptor_init (&dns_root, cfg))
+  {
+    GNUNET_break (0);
+    GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
+    return;
+  }
+}
+
+
 /**
  * Process GNS requests.
  *
@@ -843,16 +905,15 @@ monitor_sync_event (void *cls)
  * @param c configuration to use
  */
 static void
-run (void *cls, struct GNUNET_SERVER_Handle *server,
+run (void *cls,
+     struct GNUNET_SERVER_Handle *server,
      const struct GNUNET_CONFIGURATION_Handle *c)
 {
   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
     { &handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
     {NULL, NULL, 0, 0}
   };
-  struct GNUNET_CRYPTO_EcdsaPublicKey dns_root;
   unsigned long long max_parallel_bg_queries = 0;
-  char *dns_root_name;
 
   v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
   v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
@@ -907,32 +968,22 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     return;
   }
 
-  if (GNUNET_OK ==
-      GNUNET_CONFIGURATION_get_value_string (c, "gns", "DNS_ROOT",
-                                            &dns_root_name))
+  identity_handle = GNUNET_IDENTITY_connect (c,
+                                             NULL,
+                                             NULL);
+  if (NULL == identity_handle)
   {
-    if (GNUNET_OK !=
-       GNUNET_CRYPTO_ecdsa_public_key_from_string (dns_root_name,
-                                                    strlen (dns_root_name),
-                                                    &dns_root))
-    {
-      GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
-                                "gns", "DNS_ROOT",
-                                _("valid public key required"));
-      GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
-      GNUNET_free (dns_root_name);
-      return;
-    }
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-               "DNS hijacking with root `%s' enabled. Connecting to DNS service.\n",
-               dns_root_name);
-    GNUNET_free (dns_root_name);
-    if (GNUNET_SYSERR ==
-       GNS_interceptor_init (&dns_root, c))
-    {
-      GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
-      return;
-    }
+               "Could not connect to identity service!\n");
+  }
+  else
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "Looking for gns-intercept ego\n");
+    identity_op = GNUNET_IDENTITY_get (identity_handle,
+                                       "gns-intercept",
+                                       &identity_intercept_cb,
+                                       (void *) c);
   }
   GNS_resolver_init (namecache_handle,
                      dht_handle,
@@ -956,8 +1007,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
                                               &monitor_sync_event,
                                               NULL);
   GNUNET_break (NULL != zmon);
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                               &shutdown_task, NULL);
+  GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
 }