2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013 Christian Grothoff
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file regex/gnunet-daemon-regexprofiler.c
23 * @brief daemon that uses cadet to announce a regular expression. Used in
24 * conjunction with gnunet-regex-profiler to announce regexes on serveral peers
25 * without the need to explicitly connect to the cadet service running on the
26 * peer from within the profiler.
27 * @author Maximilian Szengel
28 * @author Bartlomiej Polot
31 #include "gnunet_util_lib.h"
32 #include "regex_internal_lib.h"
33 #include "regex_test_lib.h"
34 #include "gnunet_dht_service.h"
35 #include "gnunet_statistics_service.h"
38 * Return value from 'main'.
40 static int global_ret;
43 * Configuration we use.
45 static const struct GNUNET_CONFIGURATION_Handle *cfg;
48 * Handle to the statistics service.
50 static struct GNUNET_STATISTICS_Handle *stats_handle;
55 static struct GNUNET_DHT_Handle *dht_handle;
58 * Peer's regex announce handle.
60 static struct REGEX_INTERNAL_Announcement *announce_handle;
63 * Periodically reannounce regex.
65 static struct GNUNET_SCHEDULER_Task * reannounce_task;
68 * What's the maximum reannounce period.
70 static struct GNUNET_TIME_Relative reannounce_period_max;
73 * Maximal path compression length for regex announcing.
75 static unsigned long long max_path_compression;
78 * Name of the file containing policies that this peer should announce. One
81 static char * policy_filename;
84 * Prefix to add before every regex we're announcing.
86 static char * regex_prefix;
91 static char *rx_with_pfx;
94 * How many put rounds should we do.
96 static unsigned int rounds = 3;
99 * Private key for this peer.
101 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
106 * Task run during shutdown.
111 shutdown_task (void *cls)
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
115 if (NULL != announce_handle)
117 REGEX_INTERNAL_announce_cancel (announce_handle);
118 announce_handle = NULL;
120 if (NULL != reannounce_task)
122 GNUNET_free (GNUNET_SCHEDULER_cancel (reannounce_task));
123 reannounce_task = NULL;
125 if (NULL != dht_handle)
127 GNUNET_DHT_disconnect (dht_handle);
130 GNUNET_free (my_private_key);
131 my_private_key = NULL;
133 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
134 "Daemon for %s shutting down\n",
140 * Announce a previously announced regex re-using cached data.
142 * @param cls Closure (regex to announce if needed).
145 reannounce_regex (void *cls)
148 struct GNUNET_TIME_Relative random_delay;
150 reannounce_task = NULL;
154 GNUNET_SCHEDULER_shutdown ();
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
159 GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
160 if (NULL == announce_handle && NULL != regex)
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163 "First time, creating regex: %s\n",
165 announce_handle = REGEX_INTERNAL_announce (dht_handle,
168 (unsigned int) max_path_compression,
173 GNUNET_assert (NULL != announce_handle);
174 REGEX_INTERNAL_reannounce (announce_handle);
178 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,
179 GNUNET_CRYPTO_random_u32 (
180 GNUNET_CRYPTO_QUALITY_WEAK,
181 reannounce_period_max.rel_value_us));
182 reannounce_task = GNUNET_SCHEDULER_add_delayed (random_delay,
183 &reannounce_regex, cls);
188 * Announce the given regular expression using regex and the path compression
189 * length read from config.
191 * @param regex regular expression to announce on this peer's cadet.
194 announce_regex (const char *regex)
198 if (NULL == regex || 0 == strlen (regex))
200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
204 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
205 "Daemon for %s starting\n",
207 GNUNET_assert (NULL == reannounce_task);
208 copy = GNUNET_strdup (regex);
209 reannounce_task = GNUNET_SCHEDULER_add_now (&reannounce_regex,
215 * Scan through the policy_dir looking for the n-th filename.
217 * @param cls Closure (target number n).
218 * @param filename complete filename (absolute path).
219 * @return GNUNET_OK to continue to iterate,
220 * GNUNET_NO to stop when found
223 scan (void *cls, const char *filename)
230 policy_filename = GNUNET_strdup (filename);
239 * @brief Main function that will be run by the scheduler.
242 * @param args remaining command-line arguments
243 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
244 * @param cfg_ configuration
247 run (void *cls, char *const *args GNUNET_UNUSED,
248 const char *cfgfile GNUNET_UNUSED,
249 const struct GNUNET_CONFIGURATION_Handle *cfg_)
254 long long unsigned int peer_id;
258 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
259 GNUNET_assert (NULL != my_private_key);
261 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
262 "MAX_PATH_COMPRESSION",
263 &max_path_compression))
265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
267 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
268 "regexprofiler", "max_path_compression");
269 global_ret = GNUNET_SYSERR;
270 GNUNET_SCHEDULER_shutdown ();
274 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
275 "POLICY_DIR", &policy_dir))
277 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "POLICY_DIR");
278 global_ret = GNUNET_SYSERR;
279 GNUNET_SCHEDULER_shutdown ();
283 GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
286 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "TESTBED", "PEERID");
287 global_ret = GNUNET_SYSERR;
288 GNUNET_free (policy_dir);
289 GNUNET_SCHEDULER_shutdown ();
294 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
295 "REGEX_PREFIX", ®ex_prefix))
297 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "REGEX_PREFIX");
298 global_ret = GNUNET_SYSERR;
299 GNUNET_free (policy_dir);
300 GNUNET_SCHEDULER_shutdown ();
305 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
306 "REANNOUNCE_PERIOD_MAX",
307 &reannounce_period_max))
309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310 "reannounce_period_max not given. Using 10 minutes.\n");
311 reannounce_period_max =
312 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
315 stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
317 dht_handle = GNUNET_DHT_connect (cfg, 1);
319 if (NULL == dht_handle)
321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
322 "Could not acquire dht handle. Exiting.\n");
323 global_ret = GNUNET_SYSERR;
324 GNUNET_free (policy_dir);
325 GNUNET_SCHEDULER_shutdown ();
329 /* Read regexes from policy files */
330 GNUNET_assert (-1 != GNUNET_DISK_directory_scan (policy_dir, &scan,
331 (void *) (long) peer_id));
332 if (NULL == (components = REGEX_TEST_read_from_file (policy_filename)))
334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
335 "Policy file %s contains no policies. Exiting.\n",
337 global_ret = GNUNET_SYSERR;
338 GNUNET_free (policy_dir);
339 GNUNET_SCHEDULER_shutdown ();
342 GNUNET_free (policy_dir);
343 regex = REGEX_TEST_combine (components, 16);
344 REGEX_TEST_free_from_file (components);
346 /* Announcing regexes from policy_filename */
347 GNUNET_asprintf (&rx_with_pfx,
348 "%s(%s)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)*",
351 announce_regex (rx_with_pfx);
353 GNUNET_free (rx_with_pfx);
355 /* Scheduled the task to clean up when shutdown is called */
356 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
362 * The main function of the regexprofiler service.
364 * @param argc number of arguments from the command line
365 * @param argv command line arguments
366 * @return 0 ok, 1 on error
369 main (int argc, char *const *argv)
371 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
372 GNUNET_GETOPT_OPTION_END
375 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
378 GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
380 ("Daemon to announce regular expressions for the peer using cadet."),
381 options, &run, NULL)) ? global_ret : 1;
385 #if defined(LINUX) && defined(__GLIBC__)
389 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
391 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
393 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
394 mallopt (M_TOP_PAD, 1 * 1024);
400 /* end of gnunet-daemon-regexprofiler.c */