nicer loop structure
[oweals/gnunet.git] / src / regex / gnunet-daemon-regexprofiler.c
index 3f4dd087fd4caed0994120d1547282e59cedb871..f1c00a55450e0bb82addac871be1654d3765ad7a 100644 (file)
@@ -1,28 +1,28 @@
 /*
      This file is part of GNUnet.
-     (C) 2012, 2013 Christian Grothoff
+     Copyright (C) 2012, 2013 Christian Grothoff
 
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-     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.
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 
 /**
  * @file regex/gnunet-daemon-regexprofiler.c
- * @brief daemon that uses mesh to announce a regular expression. Used in
+ * @brief daemon that uses cadet to announce a regular expression. Used in
  * conjunction with gnunet-regex-profiler to announce regexes on serveral peers
- * without the need to explicitly connect to the mesh service running on the
+ * without the need to explicitly connect to the cadet service running on the
  * peer from within the profiler.
  * @author Maximilian Szengel
  * @author Bartlomiej Polot
@@ -62,7 +62,7 @@ static struct REGEX_INTERNAL_Announcement *announce_handle;
 /**
  * Periodically reannounce regex.
  */
-static GNUNET_SCHEDULER_TaskIdentifier reannounce_task;
+static struct GNUNET_SCHEDULER_Task * reannounce_task;
 
 /**
  * What's the maximum reannounce period.
@@ -93,17 +93,22 @@ static char *rx_with_pfx;
 /**
  * How many put rounds should we do.
  */
-static unsigned int rounds = 5;
+static unsigned int rounds = 3;
+
+/**
+ * Private key for this peer.
+ */
+static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
+
 
 
 /**
  * Task run during shutdown.
  *
  * @param cls unused
- * @param tc unused
  */
 static void
-shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_task (void *cls)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
 
@@ -112,12 +117,18 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     REGEX_INTERNAL_announce_cancel (announce_handle);
     announce_handle = NULL;
   }
-
+  if (NULL != reannounce_task)
+  {
+    GNUNET_free (GNUNET_SCHEDULER_cancel (reannounce_task));
+    reannounce_task = NULL;
+  }
   if (NULL != dht_handle)
   {
     GNUNET_DHT_disconnect (dht_handle);
     dht_handle = NULL;
   }
+  GNUNET_free (my_private_key);
+  my_private_key = NULL;
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Daemon for %s shutting down\n",
@@ -127,24 +138,16 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
 /**
  * Announce a previously announced regex re-using cached data.
- * 
+ *
  * @param cls Closure (regex to announce if needed).
- * @param tc TaskContext.
  */
 static void
-reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+reannounce_regex (void *cls)
 {
-  struct GNUNET_PeerIdentity id;
-  struct GNUNET_TIME_Relative random_delay;
   char *regex = cls;
+  struct GNUNET_TIME_Relative random_delay;
 
-  reannounce_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-  {
-    GNUNET_free (regex);
-    return;
-  }
-
+  reannounce_task = NULL;
   if (0 == rounds--)
   {
     global_ret = 0;
@@ -159,12 +162,11 @@ reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "First time, creating regex: %s\n",
                 regex);
-    memset (&id, 0, sizeof (struct GNUNET_PeerIdentity));
     announce_handle = REGEX_INTERNAL_announce (dht_handle,
-                                            &id,
-                                            regex,
-                                            (unsigned int) max_path_compression,
-                                            stats_handle);
+                                              my_private_key,
+                                              regex,
+                                              (unsigned int) max_path_compression,
+                                              stats_handle);
   }
   else
   {
@@ -173,10 +175,10 @@ reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   }
 
   random_delay =
-    GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
+    GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,
                                    GNUNET_CRYPTO_random_u32 (
                                      GNUNET_CRYPTO_QUALITY_WEAK,
-                                     reannounce_period_max.rel_value));
+                                     reannounce_period_max.rel_value_us));
   reannounce_task = GNUNET_SCHEDULER_add_delayed (random_delay,
                                                   &reannounce_regex, cls);
 }
@@ -186,10 +188,10 @@ reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * 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.
+ * @param regex regular expression to announce on this peer's cadet.
  */
 static void
-announce_regex (const char * regex)
+announce_regex (const char *regex)
 {
   char *copy;
 
@@ -202,9 +204,10 @@ announce_regex (const char * regex)
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Daemon for %s starting\n",
               policy_filename);
-  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == reannounce_task);
+  GNUNET_assert (NULL == reannounce_task);
   copy = GNUNET_strdup (regex);
-  reannounce_task = GNUNET_SCHEDULER_add_now (reannounce_regex, (void *) copy);
+  reannounce_task = GNUNET_SCHEDULER_add_now (&reannounce_regex,
+                                             (void *) copy);
 }
 
 
@@ -252,6 +255,8 @@ run (void *cls, char *const *args GNUNET_UNUSED,
 
   cfg = cfg_;
 
+  my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
+  GNUNET_assert (NULL != my_private_key);
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
                                              "MAX_PATH_COMPRESSION",
@@ -269,10 +274,7 @@ run (void *cls, char *const *args GNUNET_UNUSED,
       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", "policy_dir");
+    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "POLICY_DIR");
     global_ret = GNUNET_SYSERR;
     GNUNET_SCHEDULER_shutdown ();
     return;
@@ -281,11 +283,9 @@ run (void *cls, char *const *args GNUNET_UNUSED,
       GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
                                              "PEERID", &peer_id))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _
-                ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
-                "regexprofiler", "policy_file");
+    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "TESTBED", "PEERID");
     global_ret = GNUNET_SYSERR;
+    GNUNET_free (policy_dir);
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
@@ -294,11 +294,9 @@ run (void *cls, char *const *args GNUNET_UNUSED,
       GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
                                              "REGEX_PREFIX", &regex_prefix))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _
-                ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
-                "regexprofiler", "regex_prefix");
+    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "REGEX_PREFIX");
     global_ret = GNUNET_SYSERR;
+    GNUNET_free (policy_dir);
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
@@ -308,7 +306,7 @@ run (void *cls, char *const *args GNUNET_UNUSED,
                                            "REANNOUNCE_PERIOD_MAX",
                                            &reannounce_period_max))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "reannounce_period_max not given. Using 10 minutes.\n");
     reannounce_period_max =
       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
@@ -323,6 +321,7 @@ run (void *cls, char *const *args GNUNET_UNUSED,
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Could not acquire dht handle. Exiting.\n");
     global_ret = GNUNET_SYSERR;
+    GNUNET_free (policy_dir);
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
@@ -336,21 +335,26 @@ run (void *cls, char *const *args GNUNET_UNUSED,
                 "Policy file %s contains no policies. Exiting.\n",
                 policy_filename);
     global_ret = GNUNET_SYSERR;
+    GNUNET_free (policy_dir);
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  regex = REGEX_TEST_combine (components);
+  GNUNET_free (policy_dir);
+  regex = REGEX_TEST_combine (components, 16);
   REGEX_TEST_free_from_file (components);
 
   /* Announcing regexes from policy_filename */
-  GNUNET_asprintf (&rx_with_pfx, "%s(%s)(0|1)*", regex_prefix, regex);
+  GNUNET_asprintf (&rx_with_pfx,
+                  "%s(%s)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)*",
+                  regex_prefix,
+                  regex);
   announce_regex (rx_with_pfx);
   GNUNET_free (regex);
   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,
-                                NULL);
+  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
+                                NULL);
 }
 
 
@@ -373,12 +377,12 @@ main (int argc, char *const *argv)
   return (GNUNET_OK ==
           GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
                               gettext_noop
-                              ("Daemon to announce regular expressions for the peer using mesh."),
+                              ("Daemon to announce regular expressions for the peer using cadet."),
                               options, &run, NULL)) ? global_ret : 1;
 }
 
 
-#ifdef LINUX
+#if defined(LINUX) && defined(__GLIBC__)
 #include <malloc.h>
 
 /**