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 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.
17 * @file regex/gnunet-daemon-regexprofiler.c
18 * @brief daemon that uses cadet to announce a regular expression. Used in
19 * conjunction with gnunet-regex-profiler to announce regexes on serveral peers
20 * without the need to explicitly connect to the cadet service running on the
21 * peer from within the profiler.
22 * @author Maximilian Szengel
23 * @author Bartlomiej Polot
26 #include "gnunet_util_lib.h"
27 #include "regex_internal_lib.h"
28 #include "regex_test_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_statistics_service.h"
33 * Return value from 'main'.
35 static int global_ret;
38 * Configuration we use.
40 static const struct GNUNET_CONFIGURATION_Handle *cfg;
43 * Handle to the statistics service.
45 static struct GNUNET_STATISTICS_Handle *stats_handle;
50 static struct GNUNET_DHT_Handle *dht_handle;
53 * Peer's regex announce handle.
55 static struct REGEX_INTERNAL_Announcement *announce_handle;
58 * Periodically reannounce regex.
60 static struct GNUNET_SCHEDULER_Task * reannounce_task;
63 * What's the maximum reannounce period.
65 static struct GNUNET_TIME_Relative reannounce_period_max;
68 * Maximal path compression length for regex announcing.
70 static unsigned long long max_path_compression;
73 * Name of the file containing policies that this peer should announce. One
76 static char * policy_filename;
79 * Prefix to add before every regex we're announcing.
81 static char * regex_prefix;
86 static char *rx_with_pfx;
89 * How many put rounds should we do.
91 static unsigned int rounds = 3;
94 * Private key for this peer.
96 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
101 * Task run during shutdown.
106 shutdown_task (void *cls)
108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
110 if (NULL != announce_handle)
112 REGEX_INTERNAL_announce_cancel (announce_handle);
113 announce_handle = NULL;
115 if (NULL != reannounce_task)
117 GNUNET_free (GNUNET_SCHEDULER_cancel (reannounce_task));
118 reannounce_task = NULL;
120 if (NULL != dht_handle)
122 GNUNET_DHT_disconnect (dht_handle);
125 GNUNET_free (my_private_key);
126 my_private_key = NULL;
128 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
129 "Daemon for %s shutting down\n",
135 * Announce a previously announced regex re-using cached data.
137 * @param cls Closure (regex to announce if needed).
140 reannounce_regex (void *cls)
143 struct GNUNET_TIME_Relative random_delay;
145 reannounce_task = NULL;
149 GNUNET_SCHEDULER_shutdown ();
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
154 GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
155 if (NULL == announce_handle && NULL != regex)
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "First time, creating regex: %s\n",
160 announce_handle = REGEX_INTERNAL_announce (dht_handle,
163 (unsigned int) max_path_compression,
168 GNUNET_assert (NULL != announce_handle);
169 REGEX_INTERNAL_reannounce (announce_handle);
173 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,
174 GNUNET_CRYPTO_random_u32 (
175 GNUNET_CRYPTO_QUALITY_WEAK,
176 reannounce_period_max.rel_value_us));
177 reannounce_task = GNUNET_SCHEDULER_add_delayed (random_delay,
178 &reannounce_regex, cls);
183 * Announce the given regular expression using regex and the path compression
184 * length read from config.
186 * @param regex regular expression to announce on this peer's cadet.
189 announce_regex (const char *regex)
193 if (NULL == regex || 0 == strlen (regex))
195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
199 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
200 "Daemon for %s starting\n",
202 GNUNET_assert (NULL == reannounce_task);
203 copy = GNUNET_strdup (regex);
204 reannounce_task = GNUNET_SCHEDULER_add_now (&reannounce_regex,
210 * Scan through the policy_dir looking for the n-th filename.
212 * @param cls Closure (target number n).
213 * @param filename complete filename (absolute path).
214 * @return GNUNET_OK to continue to iterate,
215 * GNUNET_NO to stop when found
218 scan (void *cls, const char *filename)
225 policy_filename = GNUNET_strdup (filename);
234 * @brief Main function that will be run by the scheduler.
237 * @param args remaining command-line arguments
238 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
239 * @param cfg_ configuration
242 run (void *cls, char *const *args GNUNET_UNUSED,
243 const char *cfgfile GNUNET_UNUSED,
244 const struct GNUNET_CONFIGURATION_Handle *cfg_)
249 long long unsigned int peer_id;
253 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
254 GNUNET_assert (NULL != my_private_key);
256 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
257 "MAX_PATH_COMPRESSION",
258 &max_path_compression))
260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
262 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
263 "regexprofiler", "max_path_compression");
264 global_ret = GNUNET_SYSERR;
265 GNUNET_SCHEDULER_shutdown ();
269 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
270 "POLICY_DIR", &policy_dir))
272 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "POLICY_DIR");
273 global_ret = GNUNET_SYSERR;
274 GNUNET_SCHEDULER_shutdown ();
278 GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
281 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "TESTBED", "PEERID");
282 global_ret = GNUNET_SYSERR;
283 GNUNET_free (policy_dir);
284 GNUNET_SCHEDULER_shutdown ();
289 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
290 "REGEX_PREFIX", ®ex_prefix))
292 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "REGEX_PREFIX");
293 global_ret = GNUNET_SYSERR;
294 GNUNET_free (policy_dir);
295 GNUNET_SCHEDULER_shutdown ();
300 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
301 "REANNOUNCE_PERIOD_MAX",
302 &reannounce_period_max))
304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
305 "reannounce_period_max not given. Using 10 minutes.\n");
306 reannounce_period_max =
307 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
310 stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
312 dht_handle = GNUNET_DHT_connect (cfg, 1);
314 if (NULL == dht_handle)
316 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
317 "Could not acquire dht handle. Exiting.\n");
318 global_ret = GNUNET_SYSERR;
319 GNUNET_free (policy_dir);
320 GNUNET_SCHEDULER_shutdown ();
324 /* Read regexes from policy files */
325 GNUNET_assert (-1 != GNUNET_DISK_directory_scan (policy_dir, &scan,
326 (void *) (long) peer_id));
327 if (NULL == (components = REGEX_TEST_read_from_file (policy_filename)))
329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
330 "Policy file %s contains no policies. Exiting.\n",
332 global_ret = GNUNET_SYSERR;
333 GNUNET_free (policy_dir);
334 GNUNET_SCHEDULER_shutdown ();
337 GNUNET_free (policy_dir);
338 regex = REGEX_TEST_combine (components, 16);
339 REGEX_TEST_free_from_file (components);
341 /* Announcing regexes from policy_filename */
342 GNUNET_asprintf (&rx_with_pfx,
343 "%s(%s)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)*",
346 announce_regex (rx_with_pfx);
348 GNUNET_free (rx_with_pfx);
350 /* Scheduled the task to clean up when shutdown is called */
351 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
357 * The main function of the regexprofiler service.
359 * @param argc number of arguments from the command line
360 * @param argv command line arguments
361 * @return 0 ok, 1 on error
364 main (int argc, char *const *argv)
366 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
367 GNUNET_GETOPT_OPTION_END
370 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
373 GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
375 ("Daemon to announce regular expressions for the peer using cadet."),
376 options, &run, NULL)) ? global_ret : 1;
380 #if defined(LINUX) && defined(__GLIBC__)
384 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
386 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
388 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
389 mallopt (M_TOP_PAD, 1 * 1024);
395 /* end of gnunet-daemon-regexprofiler.c */