-move struct RegexBlock into regex_block_lib
[oweals/gnunet.git] / src / regex / gnunet-daemon-regexprofiler.c
index a542facd7e5543d823c64a9e5406e7ddfbe14060..d4c9cb7cb28f2a433799ecdd24a527bff7fa2aa1 100644 (file)
@@ -29,7 +29,8 @@
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
-#include "gnunet_regex_lib.h"
+#include "regex_internal_lib.h"
+#include "regex_test_lib.h"
 #include "gnunet_dht_service.h"
 #include "gnunet_statistics_service.h"
 
@@ -56,12 +57,7 @@ static struct GNUNET_DHT_Handle *dht_handle;
 /**
  * Peer's regex announce handle.
  */
-static struct GNUNET_REGEX_announce_handle *announce_handle;
-
-/**
- * Hostkey generation context
- */
-static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
+static struct REGEX_INTERNAL_Announcement *announce_handle;
 
 /**
  * Periodically reannounce regex.
@@ -69,19 +65,9 @@ static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
 static GNUNET_SCHEDULER_TaskIdentifier reannounce_task;
 
 /**
- * How often reannounce regex.
- */
-static struct GNUNET_TIME_Relative reannounce_freq;
-
-/**
- * Random delay to spread out load on the DHT.
- */
-static struct GNUNET_TIME_Relative announce_delay;
-
-/**
- * Local peer's PeerID.
+ * What's the maximum reannounce period.
  */
-static struct GNUNET_PeerIdentity my_full_id;
+static struct GNUNET_TIME_Relative reannounce_period_max;
 
 /**
  * Maximal path compression length for regex announcing.
@@ -104,6 +90,17 @@ static char * regex_prefix;
  */
 static char *rx_with_pfx;
 
+/**
+ * How many put rounds should we do.
+ */
+static unsigned int rounds = 5;
+
+/**
+ * Private key for this peer.
+ */
+static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
+
+
 
 /**
  * Task run during shutdown.
@@ -116,14 +113,9 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
 
-  if (NULL != keygen)
-  {
-    GNUNET_CRYPTO_rsa_key_create_stop (keygen);
-    keygen = NULL;
-  }
   if (NULL != announce_handle)
   {
-    GNUNET_REGEX_announce_cancel (announce_handle);
+    REGEX_INTERNAL_announce_cancel (announce_handle);
     announce_handle = NULL;
   }
 
@@ -132,8 +124,12 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_DHT_disconnect (dht_handle);
     dht_handle = NULL;
   }
+  GNUNET_CRYPTO_ecc_key_free (my_private_key);
+  my_private_key = NULL;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Daemon for %s shutting down\n",
+              policy_filename);
 }
 
 
@@ -146,11 +142,23 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 static void
 reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  struct GNUNET_TIME_Relative random_delay;
   char *regex = cls;
+
   reannounce_task = GNUNET_SCHEDULER_NO_TASK;
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+  {
+    GNUNET_free (regex);
     return;
+  }
 
+  if (0 == rounds--)
+  {
+    global_ret = 0;
+    GNUNET_SCHEDULER_shutdown ();
+    GNUNET_free (regex);
+    return;
+  }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
   GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
   if (NULL == announce_handle && NULL != regex)
@@ -158,25 +166,30 @@ reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "First time, creating regex: %s\n",
                 regex);
-    announce_handle = GNUNET_REGEX_announce (dht_handle,
-                                            &my_full_id,
-                                            regex,
-                                            (unsigned int) max_path_compression,
-                                            stats_handle);
+    announce_handle = REGEX_INTERNAL_announce (dht_handle,
+                                              my_private_key,
+                                              regex,
+                                              (unsigned int) max_path_compression,
+                                              stats_handle);
   }
   else
   {
     GNUNET_assert (NULL != announce_handle);
-    GNUNET_REGEX_reannounce (announce_handle);
+    REGEX_INTERNAL_reannounce (announce_handle);
   }
-  reannounce_task = GNUNET_SCHEDULER_add_delayed (reannounce_freq,
-                                                  &reannounce_regex,
-                                                  cls);
+
+  random_delay =
+    GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
+                                   GNUNET_CRYPTO_random_u32 (
+                                     GNUNET_CRYPTO_QUALITY_WEAK,
+                                     reannounce_period_max.rel_value));
+  reannounce_task = GNUNET_SCHEDULER_add_delayed (random_delay,
+                                                  &reannounce_regex, cls);
 }
 
 
 /**
- * Announce the given regular expression using Mesh and the path compression
+ * Announce the given regular expression using regex and the path compression
  * length read from config.
  *
  * @param regex regular expression to announce on this peer's mesh.
@@ -184,113 +197,44 @@ reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 static void
 announce_regex (const char * regex)
 {
+  char *copy;
+
   if (NULL == regex || 0 == strlen (regex))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
     return;
   }
 
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Daemon for %s starting\n",
+              policy_filename);
   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == reannounce_task);
-  reannounce_task = GNUNET_SCHEDULER_add_delayed (announce_delay,
-                                                  reannounce_regex,
-                                                  (void *) regex);
-}
-
-
-/**
- * Load regular expressions from filename into 'rxes' array. Array needs to be freed.
- *
- * @param filename filename of the file containing the regexes, one per line.
- * @param rx string with the union of all regular expressions.
- *
- * @return number of regular expressions read from filename and in rxes array.
- * FIXME use load regex lib function
- */
-static unsigned int
-load_regexes (const char *filename, char **rx)
-{
-  char *data;
-  char *buf;
-  uint64_t filesize;
-  unsigned int offset;
-  unsigned int rx_cnt;
-
-  if (GNUNET_YES != GNUNET_DISK_file_test (policy_filename))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Could not find policy file %s\n", policy_filename);
-    return 0;
-  }
-  if (GNUNET_OK != GNUNET_DISK_file_size (policy_filename, &filesize, GNUNET_YES, GNUNET_YES))
-    filesize = 0;
-  if (0 == filesize)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Policy file %s is empty.\n", policy_filename);
-    return 0;
-  }
-  data = GNUNET_malloc (filesize);
-  if (filesize != GNUNET_DISK_fn_read (policy_filename, data, filesize))
-  {
-    GNUNET_free (data);
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not read policy file %s.\n",
-                policy_filename);
-    return 0;
-  }
-  buf = data;
-  offset = 0;
-  rx_cnt = 0;
-  while (offset < (filesize - 1))
-  {
-    offset++;
-    if ((data[offset] == '\n') && (buf != &data[offset]))
-    {
-      data[offset] = '|';
-      buf = &data[offset + 1];
-      rx_cnt++;
-    }
-    else if ((data[offset] == '\n') || (data[offset] == '\0'))
-      buf = &data[offset + 1];
-  }
-  data[offset] = '\0';
-  *rx = data;
-
-  return rx_cnt;
+  copy = GNUNET_strdup (regex);
+  reannounce_task = GNUNET_SCHEDULER_add_now (reannounce_regex, (void *) copy);
 }
 
 
 /**
- * Callback for hostkey read/generation
+ * Scan through the policy_dir looking for the n-th filename.
  *
- * @param cls Closure (not used).
- * @param pk The private key of the local peer.
- * @param emsg Error message if applicable.
+ * @param cls Closure (target number n).
+ * @param filename complete filename (absolute path).
+ * @return GNUNET_OK to continue to iterate,
+ *  GNUNET_NO to stop when found
  */
-static void
-key_generation_cb (void *cls,
-                   struct GNUNET_CRYPTO_RsaPrivateKey *pk,
-                   const char *emsg)
+static int
+scan (void *cls, const char *filename)
 {
-  struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
+  long n = (long) cls;
+  static long c = 0;
 
-  keygen = NULL;
-  if (NULL == pk)
+  if (c == n)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Regexprofiler could not access hostkey: %s. Exiting.\n"),
-                emsg);
-    GNUNET_SCHEDULER_shutdown ();
-    return;
+    policy_filename = GNUNET_strdup (filename);
+    return GNUNET_NO;
   }
-
-  GNUNET_CRYPTO_rsa_key_get_public (pk, &my_public_key);
-  GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
-                      &my_full_id.hashPubKey);
-
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Regexprofiler for peer [%s] starting\n",
-              GNUNET_i2s(&my_full_id));
-  announce_regex (rx_with_pfx);
-  GNUNET_free (rx_with_pfx);
+  c++;
+  return GNUNET_OK;
 }
 
 
@@ -308,38 +252,42 @@ run (void *cls, char *const *args GNUNET_UNUSED,
      const struct GNUNET_CONFIGURATION_Handle *cfg_)
 {
   char *regex = NULL;
-  char *keyfile;
+  char **components;
+  char *policy_dir;
+  long long unsigned int peer_id;
 
   cfg = cfg_;
 
+  my_private_key = GNUNET_CRYPTO_ecc_key_create_from_configuration (cfg);
+  GNUNET_assert (NULL != my_private_key);
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
-                                               &keyfile))
+      GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
+                                             "MAX_PATH_COMPRESSION",
+                                             &max_path_compression))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _
                 ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
-                "regexprofiler", "hostkey");
+                "regexprofiler", "max_path_compression");
+    global_ret = GNUNET_SYSERR;
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER", "MAX_PATH_COMPRESSION",
-                                             &max_path_compression))
+      GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
+                                             "POLICY_DIR", &policy_dir))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _
                 ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
-                "regexprofiler", "max_path_compression");
+                "regexprofiler", "policy_dir");
     global_ret = GNUNET_SYSERR;
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_filename (cfg, "REGEXPROFILER",
-                                               "POLICY_FILE", &policy_filename))
+      GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
+                                             "PEERID", &peer_id))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _
@@ -365,17 +313,14 @@ run (void *cls, char *const *args GNUNET_UNUSED,
 
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
-                                           "REANNOUNCE_FREQ", &reannounce_freq))
+                                           "REANNOUNCE_PERIOD_MAX",
+                                           &reannounce_period_max))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
-                "reannounce_freq not given. Using 10 minutes.\n");
-    reannounce_freq =
+                "reannounce_period_max not given. Using 10 minutes.\n");
+    reannounce_period_max =
       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
-
   }
-    announce_delay =
-    GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
-                                   GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 600));
 
   stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
 
@@ -391,7 +336,9 @@ run (void *cls, char *const *args GNUNET_UNUSED,
   }
 
   /* Read regexes from policy files */
-  if (0 == load_regexes (policy_filename, &regex))
+  GNUNET_assert (-1 != GNUNET_DISK_directory_scan (policy_dir, &scan,
+                                                   (void *) (long) peer_id));
+  if (NULL == (components = REGEX_TEST_read_from_file (policy_filename)))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Policy file %s contains no policies. Exiting.\n",
@@ -400,15 +347,14 @@ run (void *cls, char *const *args GNUNET_UNUSED,
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
+  regex = REGEX_TEST_combine (components);
+  REGEX_TEST_free_from_file (components);
 
   /* Announcing regexes from policy_filename */
-  GNUNET_asprintf (&rx_with_pfx, "%s(%s)", regex_prefix, regex);
+  GNUNET_asprintf (&rx_with_pfx, "%s(%s)(0|1)*", regex_prefix, regex);
+  announce_regex (rx_with_pfx);
   GNUNET_free (regex);
-
-  keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile,
-                                               &key_generation_cb,
-                                               NULL);
-  GNUNET_free (keyfile);
+  GNUNET_free (rx_with_pfx);
 
   /* Scheduled the task to clean up when shutdown is called */
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,