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/>.
20 * @file regex/gnunet-daemon-regexprofiler.c
21 * @brief daemon that uses cadet to announce a regular expression. Used in
22 * conjunction with gnunet-regex-profiler to announce regexes on serveral peers
23 * without the need to explicitly connect to the cadet service running on the
24 * peer from within the profiler.
25 * @author Maximilian Szengel
26 * @author Bartlomiej Polot
29 #include "gnunet_util_lib.h"
30 #include "regex_internal_lib.h"
31 #include "regex_test_lib.h"
32 #include "gnunet_dht_service.h"
33 #include "gnunet_statistics_service.h"
36 * Return value from 'main'.
38 static int global_ret;
41 * Configuration we use.
43 static const struct GNUNET_CONFIGURATION_Handle *cfg;
46 * Handle to the statistics service.
48 static struct GNUNET_STATISTICS_Handle *stats_handle;
53 static struct GNUNET_DHT_Handle *dht_handle;
56 * Peer's regex announce handle.
58 static struct REGEX_INTERNAL_Announcement *announce_handle;
61 * Periodically reannounce regex.
63 static struct GNUNET_SCHEDULER_Task * reannounce_task;
66 * What's the maximum reannounce period.
68 static struct GNUNET_TIME_Relative reannounce_period_max;
71 * Maximal path compression length for regex announcing.
73 static unsigned long long max_path_compression;
76 * Name of the file containing policies that this peer should announce. One
79 static char * policy_filename;
82 * Prefix to add before every regex we're announcing.
84 static char * regex_prefix;
89 static char *rx_with_pfx;
92 * How many put rounds should we do.
94 static unsigned int rounds = 3;
97 * Private key for this peer.
99 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
104 * Task run during shutdown.
109 shutdown_task (void *cls)
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
113 if (NULL != announce_handle)
115 REGEX_INTERNAL_announce_cancel (announce_handle);
116 announce_handle = NULL;
118 if (NULL != reannounce_task)
120 GNUNET_free (GNUNET_SCHEDULER_cancel (reannounce_task));
121 reannounce_task = NULL;
123 if (NULL != dht_handle)
125 GNUNET_DHT_disconnect (dht_handle);
128 GNUNET_free (my_private_key);
129 my_private_key = NULL;
131 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
132 "Daemon for %s shutting down\n",
138 * Announce a previously announced regex re-using cached data.
140 * @param cls Closure (regex to announce if needed).
143 reannounce_regex (void *cls)
146 struct GNUNET_TIME_Relative random_delay;
148 reannounce_task = NULL;
152 GNUNET_SCHEDULER_shutdown ();
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
157 GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
158 if (NULL == announce_handle && NULL != regex)
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161 "First time, creating regex: %s\n",
163 announce_handle = REGEX_INTERNAL_announce (dht_handle,
166 (unsigned int) max_path_compression,
171 GNUNET_assert (NULL != announce_handle);
172 REGEX_INTERNAL_reannounce (announce_handle);
176 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,
177 GNUNET_CRYPTO_random_u32 (
178 GNUNET_CRYPTO_QUALITY_WEAK,
179 reannounce_period_max.rel_value_us));
180 reannounce_task = GNUNET_SCHEDULER_add_delayed (random_delay,
181 &reannounce_regex, cls);
186 * Announce the given regular expression using regex and the path compression
187 * length read from config.
189 * @param regex regular expression to announce on this peer's cadet.
192 announce_regex (const char *regex)
196 if (NULL == regex || 0 == strlen (regex))
198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
202 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
203 "Daemon for %s starting\n",
205 GNUNET_assert (NULL == reannounce_task);
206 copy = GNUNET_strdup (regex);
207 reannounce_task = GNUNET_SCHEDULER_add_now (&reannounce_regex,
213 * Scan through the policy_dir looking for the n-th filename.
215 * @param cls Closure (target number n).
216 * @param filename complete filename (absolute path).
217 * @return GNUNET_OK to continue to iterate,
218 * GNUNET_NO to stop when found
221 scan (void *cls, const char *filename)
228 policy_filename = GNUNET_strdup (filename);
237 * @brief Main function that will be run by the scheduler.
240 * @param args remaining command-line arguments
241 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
242 * @param cfg_ configuration
245 run (void *cls, char *const *args GNUNET_UNUSED,
246 const char *cfgfile GNUNET_UNUSED,
247 const struct GNUNET_CONFIGURATION_Handle *cfg_)
252 long long unsigned int peer_id;
256 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
257 GNUNET_assert (NULL != my_private_key);
259 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
260 "MAX_PATH_COMPRESSION",
261 &max_path_compression))
263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
265 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
266 "regexprofiler", "max_path_compression");
267 global_ret = GNUNET_SYSERR;
268 GNUNET_SCHEDULER_shutdown ();
272 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
273 "POLICY_DIR", &policy_dir))
275 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "POLICY_DIR");
276 global_ret = GNUNET_SYSERR;
277 GNUNET_SCHEDULER_shutdown ();
281 GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
284 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "TESTBED", "PEERID");
285 global_ret = GNUNET_SYSERR;
286 GNUNET_free (policy_dir);
287 GNUNET_SCHEDULER_shutdown ();
292 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
293 "REGEX_PREFIX", ®ex_prefix))
295 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "REGEX_PREFIX");
296 global_ret = GNUNET_SYSERR;
297 GNUNET_free (policy_dir);
298 GNUNET_SCHEDULER_shutdown ();
303 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
304 "REANNOUNCE_PERIOD_MAX",
305 &reannounce_period_max))
307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
308 "reannounce_period_max not given. Using 10 minutes.\n");
309 reannounce_period_max =
310 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
313 stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
315 dht_handle = GNUNET_DHT_connect (cfg, 1);
317 if (NULL == dht_handle)
319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
320 "Could not acquire dht handle. Exiting.\n");
321 global_ret = GNUNET_SYSERR;
322 GNUNET_free (policy_dir);
323 GNUNET_SCHEDULER_shutdown ();
327 /* Read regexes from policy files */
328 GNUNET_assert (-1 != GNUNET_DISK_directory_scan (policy_dir, &scan,
329 (void *) (long) peer_id));
330 if (NULL == (components = REGEX_TEST_read_from_file (policy_filename)))
332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
333 "Policy file %s contains no policies. Exiting.\n",
335 global_ret = GNUNET_SYSERR;
336 GNUNET_free (policy_dir);
337 GNUNET_SCHEDULER_shutdown ();
340 GNUNET_free (policy_dir);
341 regex = REGEX_TEST_combine (components, 16);
342 REGEX_TEST_free_from_file (components);
344 /* Announcing regexes from policy_filename */
345 GNUNET_asprintf (&rx_with_pfx,
346 "%s(%s)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)*",
349 announce_regex (rx_with_pfx);
351 GNUNET_free (rx_with_pfx);
353 /* Scheduled the task to clean up when shutdown is called */
354 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
360 * The main function of the regexprofiler service.
362 * @param argc number of arguments from the command line
363 * @param argv command line arguments
364 * @return 0 ok, 1 on error
367 main (int argc, char *const *argv)
369 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
370 GNUNET_GETOPT_OPTION_END
373 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
376 GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
378 ("Daemon to announce regular expressions for the peer using cadet."),
379 options, &run, NULL)) ? global_ret : 1;
383 #if defined(LINUX) && defined(__GLIBC__)
387 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
389 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
391 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
392 mallopt (M_TOP_PAD, 1 * 1024);
398 /* end of gnunet-daemon-regexprofiler.c */