-add identity service to standard build
[oweals/gnunet.git] / src / nse / gnunet-service-nse.c
index 3047860b82fa524fa600a7060311bf9996e166bb..b39891e7e5f30a5d1b818df64e4897c8c28ed7d4 100644 (file)
@@ -44,6 +44,9 @@
 #include "gnunet_statistics_service.h"
 #include "gnunet_core_service.h"
 #include "gnunet_nse_service.h"
+#if ENABLE_NSE_HISTOGRAM
+#include "gnunet_testbed_logger_service.h"
+#endif
 #include "nse.h"
 #include <gcrypt.h>
 
 #define USE_RANDOM_DELAYS GNUNET_YES
 
 /**
- * Should we generate a histogram with the time stamps of when we received
- * NSE messages to disk? (for performance evaluation only, not useful in
- * production).  The associated code should also probably be removed
- * once we're done with experiments.
+ * Generate extensive debug-level log messages?
  */
-#define ENABLE_HISTOGRAM GNUNET_NO
+#define DEBUG_NSE GNUNET_NO
 
 /**
  * Over how many values do we calculate the weighted average?
@@ -92,11 +92,11 @@ static struct GNUNET_TIME_Relative gnunet_nse_interval;
  */
 static struct GNUNET_TIME_Relative proof_find_delay;
 
-#if ENABLE_HISTOGRAM
+#if ENABLE_NSE_HISTOGRAM
 /**
  * Handle for writing when we received messages to disk.
  */
-static struct GNUNET_BIO_WriteHandle *wh;
+static struct GNUNET_TESTBED_LOGGER_Handle *lh;
 #endif
 
 
@@ -128,7 +128,7 @@ struct NSEPeerEntry
    */
   int previous_round;
 
-#if ENABLE_HISTOGRAM
+#if ENABLE_NSE_HISTOGRAM
 
   /**
    * Amount of messages received from this peer on this round.
@@ -311,11 +311,6 @@ static uint64_t my_proof;
  */
 static struct GNUNET_SERVER_Handle *srv;
 
-/**
- * Hostkey generation context
- */
-static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
-
 
 /**
  * Initialize a message to clients with the current network
@@ -492,7 +487,7 @@ pow_hash (const void *buf,
 {
   GNUNET_break (0 == 
                gcry_kdf_derive (buf, buf_len,
-                                GCRY_KDF_PBKDF2 /* FIX: use SCRYPT! */,
+                                GCRY_KDF_SCRYPT,
                                 1 /* subalgo */,
                                 "gnunet-proof-of-work", strlen ("gnunet-proof-of-work"),
                                 2 /* iterations; keep cost of individual op small */,
@@ -636,7 +631,7 @@ transmit_ready (void *cls, size_t size, void *buf)
     GNUNET_STATISTICS_update (stats, "# flood messages started", 1, GNUNET_NO);
   GNUNET_STATISTICS_update (stats, "# flood messages transmitted", 1,
                             GNUNET_NO);
-#if ENABLE_HISTOGRAM
+#if ENABLE_NSE_HISTOGRAM
   peer_entry->transmitted_messages++;
   peer_entry->last_transmitted_size = 
       ntohl(size_estimate_messages[idx].matching_bits);
@@ -751,7 +746,7 @@ schedule_current_round (void *cls,
     GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
     peer_entry->previous_round = GNUNET_NO;
   }
-#if ENABLE_HISTOGRAM
+#if ENABLE_NSE_HISTOGRAM
   if (peer_entry->received_messages > 1)
     GNUNET_STATISTICS_update(stats, "# extra messages",
                              peer_entry->received_messages - 1, GNUNET_NO);
@@ -1028,8 +1023,6 @@ update_flood_times (void *cls, const struct GNUNET_HashCode * key, void *value)
  * @param cls closure unused
  * @param message message
  * @param peer peer identity this message is from (ignored)
- * @param atsi performance data (ignored)
- * @param atsi_count number of records in 'atsi'
  */
 static int
 handle_p2p_size_estimate (void *cls, const struct GNUNET_PeerIdentity *peer,
@@ -1041,9 +1034,13 @@ handle_p2p_size_estimate (void *cls, const struct GNUNET_PeerIdentity *peer,
   uint32_t matching_bits;
   unsigned int idx;
 
-#if ENABLE_HISTOGRAM
-  if (NULL != wh)
-    GNUNET_break (GNUNET_OK == GNUNET_BIO_write_int64 (wh, GNUNET_TIME_absolute_get ().abs_value));
+#if ENABLE_NSE_HISTOGRAM
+  {
+    uint64_t t;
+
+    t = GNUNET_TIME_absolute_get().abs_value;
+    GNUNET_TESTBED_LOGGER_write (lh, &t, sizeof (uint64_t));
+  }
 #endif
   incoming_flood = (const struct GNUNET_NSE_FloodMessage *) message;
   GNUNET_STATISTICS_update (stats, "# flood messages received", 1, GNUNET_NO);
@@ -1060,9 +1057,8 @@ handle_p2p_size_estimate (void *cls, const struct GNUNET_PeerIdentity *peer,
     GNUNET_snprintf (origin, sizeof (origin), "%s", GNUNET_i2s (&os));
     GNUNET_snprintf (pred, sizeof (pred), "%s", GNUNET_i2s (peer));
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Flood at %llu from `%s' via `%s' at `%s' with bits %u\n",
-                (unsigned long long)
-                GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp).abs_value,
+                "Flood at %s from `%s' via `%s' at `%s' with bits %u\n",
+                GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp)),
                 origin, pred, GNUNET_i2s (&my_identity),
                 (unsigned int) matching_bits);
   }
@@ -1074,7 +1070,7 @@ handle_p2p_size_estimate (void *cls, const struct GNUNET_PeerIdentity *peer,
     GNUNET_break (0);
     return GNUNET_OK;
   }
-#if ENABLE_HISTOGRAM
+#if ENABLE_NSE_HISTOGRAM
   peer_entry->received_messages++;
   if (peer_entry->transmitted_messages > 0 && 
       peer_entry->last_transmitted_size >= matching_bits)
@@ -1211,8 +1207,6 @@ handle_p2p_size_estimate (void *cls, const struct GNUNET_PeerIdentity *peer,
  *
  * @param cls closure
  * @param peer peer identity this notification is about
- * @param atsi performance data
- * @param atsi_count number of records in 'atsi'
  */
 static void
 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
@@ -1271,6 +1265,23 @@ handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
 }
 
 
+#if ENABLE_NSE_HISTOGRAM
+/**
+ * Functions of this type are called to notify a successful transmission of the
+ * message to the logger service
+ *
+ * @param cls NULL
+ * @param size the amount of data sent
+ */
+static void 
+flush_comp_cb (void *cls, size_t size)
+{
+  GNUNET_TESTBED_LOGGER_disconnect (lh);
+  lh = NULL;
+}
+#endif
+
+
 /**
  * Task run during shutdown.
  *
@@ -1291,11 +1302,6 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     proof_task = GNUNET_SCHEDULER_NO_TASK;
     write_proof ();             /* remember progress */
   }
-  if (NULL != keygen)
-  {
-    GNUNET_CRYPTO_ecc_key_create_stop (keygen);
-    keygen = NULL;
-  }
   if (NULL != nc)
   {
     GNUNET_SERVER_notification_context_destroy (nc);
@@ -1321,12 +1327,10 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_CRYPTO_ecc_key_free (my_private_key);
     my_private_key = NULL;
   }
-#if ENABLE_HISTOGRAM
-  if (NULL != wh)
-  {
-    GNUNET_break (GNUNET_OK == GNUNET_BIO_write_close (wh));
-    wh = NULL;
-  }
+#if ENABLE_NSE_HISTOGRAM
+  struct GNUNET_TIME_Relative timeout;
+  timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
+  GNUNET_TESTBED_LOGGER_flush (lh, timeout, &flush_comp_cb, NULL);
 #endif
 }
 
@@ -1379,16 +1383,16 @@ core_init (void *cls, struct GNUNET_CORE_Handle *server,
 
 
 /**
- * Callback for hostkey read/generation
+ * Handle network size estimate clients.
  *
- * @param cls NULL
- * @param pk the private key
- * @param emsg error message
+ * @param cls closure
+ * @param server the initialized server
+ * @param c configuration to use
  */
 static void
-key_generation_cb (void *cls,
-                   struct GNUNET_CRYPTO_EccPrivateKey *pk,
-                   const char *emsg)
+run (void *cls, 
+     struct GNUNET_SERVER_Handle *server,
+     const struct GNUNET_CONFIGURATION_Handle *c)
 {
   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
     {&handle_start_message, NULL, GNUNET_MESSAGE_TYPE_NSE_START,
@@ -1401,16 +1405,61 @@ key_generation_cb (void *cls,
     {NULL, 0, 0}
   };
   char *proof;
+  char *keyfile;
+  struct GNUNET_CRYPTO_EccPrivateKey *pk;
 
-  keygen = NULL;
-  if (NULL == pk)
+  cfg = c;
+  srv = server;  
+  if ((GNUNET_OK !=
+       GNUNET_CONFIGURATION_get_value_time (cfg, "NSE", "INTERVAL",
+                                            &gnunet_nse_interval)) ||
+      (GNUNET_OK !=
+       GNUNET_CONFIGURATION_get_value_time (cfg, "NSE", "WORKDELAY",
+                                            &proof_find_delay)) ||
+      (GNUNET_OK !=
+       GNUNET_CONFIGURATION_get_value_number (cfg, "NSE", "WORKBITS",
+                                              &nse_work_required)))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _
+                ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
+                "NSE", "interval/workdelay/workbits");
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+  if (nse_work_required >= sizeof (struct GNUNET_HashCode) * 8)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("Invalid work requirement for NSE service. Exiting.\n"));
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
+                                               &keyfile))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _
+                ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
+                "NSE", "peer/privatekey");
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+#if ENABLE_NSE_HISTOGRAM
+  if (NULL == (lh = GNUNET_TESTBED_LOGGER_connect (cfg)))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("NSE service could not access hostkey: %s\n"),
-               emsg);
+                "Cannot connect to the testbed logger.  Exiting.\n");
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
+#endif
+
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
+                                NULL);
+  pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
+  GNUNET_free (keyfile);
+  GNUNET_assert (NULL != pk);
   my_private_key = pk;
   GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
   GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
@@ -1454,75 +1503,7 @@ key_generation_cb (void *cls,
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-#if ENABLE_HISTOGRAM
-  if (GNUNET_OK ==
-      GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE", "HISTOGRAM", &proof))
-  {
-    wh = GNUNET_BIO_write_open (proof);
-    GNUNET_free (proof);
-  }
-#endif
   stats = GNUNET_STATISTICS_create ("nse", cfg);
-  GNUNET_SERVER_resume (srv);
-}
-
-
-/**
- * Handle network size estimate clients.
- *
- * @param cls closure
- * @param server the initialized server
- * @param c configuration to use
- */
-static void
-run (void *cls, 
-     struct GNUNET_SERVER_Handle *server,
-     const struct GNUNET_CONFIGURATION_Handle *c)
-{
-  char *keyfile;
-
-  cfg = c;
-  srv = server;  
-  if ((GNUNET_OK !=
-       GNUNET_CONFIGURATION_get_value_time (cfg, "NSE", "INTERVAL",
-                                            &gnunet_nse_interval)) ||
-      (GNUNET_OK !=
-       GNUNET_CONFIGURATION_get_value_time (cfg, "NSE", "WORKDELAY",
-                                            &proof_find_delay)) ||
-      (GNUNET_OK !=
-       GNUNET_CONFIGURATION_get_value_number (cfg, "NSE", "WORKBITS",
-                                              &nse_work_required)))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _
-                ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
-                "NSE", "interval/workdelay/workbits");
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
-  if (nse_work_required >= sizeof (struct GNUNET_HashCode) * 8)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Invalid work requirement for NSE service. Exiting.\n"));
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
-                                               &keyfile))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _
-                ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
-                "NSE", "peer/privatekey");
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
-                                NULL);
-  GNUNET_SERVER_suspend (srv);
-  keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile, &key_generation_cb, NULL);
-  GNUNET_free (keyfile);
 }