2 This file is part of GNUnet.
3 (C) 2012, 2013 Christian Grothoff
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file regex/gnunet-daemon-regexprofiler.c
23 * @brief daemon that uses mesh 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 mesh 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 GNUNET_SCHEDULER_TaskIdentifier 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 = 5;
99 * Private key for this peer.
101 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
106 * Task run during shutdown.
112 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
116 if (NULL != announce_handle)
118 REGEX_INTERNAL_announce_cancel (announce_handle);
119 announce_handle = NULL;
122 if (NULL != dht_handle)
124 GNUNET_DHT_disconnect (dht_handle);
127 GNUNET_CRYPTO_ecc_key_free (my_private_key);
128 my_private_key = NULL;
130 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
131 "Daemon for %s shutting down\n",
137 * Announce a previously announced regex re-using cached data.
139 * @param cls Closure (regex to announce if needed).
140 * @param tc TaskContext.
143 reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
145 struct GNUNET_TIME_Relative random_delay;
148 reannounce_task = GNUNET_SCHEDULER_NO_TASK;
149 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
158 GNUNET_SCHEDULER_shutdown ();
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
163 GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
164 if (NULL == announce_handle && NULL != regex)
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167 "First time, creating regex: %s\n",
169 announce_handle = REGEX_INTERNAL_announce (dht_handle,
172 (unsigned int) max_path_compression,
177 GNUNET_assert (NULL != announce_handle);
178 REGEX_INTERNAL_reannounce (announce_handle);
182 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,
183 GNUNET_CRYPTO_random_u32 (
184 GNUNET_CRYPTO_QUALITY_WEAK,
185 reannounce_period_max.rel_value_us));
186 reannounce_task = GNUNET_SCHEDULER_add_delayed (random_delay,
187 &reannounce_regex, cls);
192 * Announce the given regular expression using regex and the path compression
193 * length read from config.
195 * @param regex regular expression to announce on this peer's mesh.
198 announce_regex (const char * regex)
202 if (NULL == regex || 0 == strlen (regex))
204 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
208 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
209 "Daemon for %s starting\n",
211 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == reannounce_task);
212 copy = GNUNET_strdup (regex);
213 reannounce_task = GNUNET_SCHEDULER_add_now (reannounce_regex, (void *) copy);
218 * Scan through the policy_dir looking for the n-th filename.
220 * @param cls Closure (target number n).
221 * @param filename complete filename (absolute path).
222 * @return GNUNET_OK to continue to iterate,
223 * GNUNET_NO to stop when found
226 scan (void *cls, const char *filename)
233 policy_filename = GNUNET_strdup (filename);
242 * @brief Main function that will be run by the scheduler.
245 * @param args remaining command-line arguments
246 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
247 * @param cfg_ configuration
250 run (void *cls, char *const *args GNUNET_UNUSED,
251 const char *cfgfile GNUNET_UNUSED,
252 const struct GNUNET_CONFIGURATION_Handle *cfg_)
257 long long unsigned int peer_id;
261 my_private_key = GNUNET_CRYPTO_ecc_key_create_from_configuration (cfg);
262 GNUNET_assert (NULL != my_private_key);
264 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
265 "MAX_PATH_COMPRESSION",
266 &max_path_compression))
268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
270 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
271 "regexprofiler", "max_path_compression");
272 global_ret = GNUNET_SYSERR;
273 GNUNET_SCHEDULER_shutdown ();
277 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
278 "POLICY_DIR", &policy_dir))
280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
282 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
283 "regexprofiler", "policy_dir");
284 global_ret = GNUNET_SYSERR;
285 GNUNET_SCHEDULER_shutdown ();
289 GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
294 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
295 "regexprofiler", "policy_file");
296 global_ret = GNUNET_SYSERR;
297 GNUNET_SCHEDULER_shutdown ();
302 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
303 "REGEX_PREFIX", ®ex_prefix))
305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
308 "regexprofiler", "regex_prefix");
309 global_ret = GNUNET_SYSERR;
310 GNUNET_SCHEDULER_shutdown ();
315 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
316 "REANNOUNCE_PERIOD_MAX",
317 &reannounce_period_max))
319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
320 "reannounce_period_max not given. Using 10 minutes.\n");
321 reannounce_period_max =
322 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
325 stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
327 dht_handle = GNUNET_DHT_connect (cfg, 1);
329 if (NULL == dht_handle)
331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
332 "Could not acquire dht handle. Exiting.\n");
333 global_ret = GNUNET_SYSERR;
334 GNUNET_SCHEDULER_shutdown ();
338 /* Read regexes from policy files */
339 GNUNET_assert (-1 != GNUNET_DISK_directory_scan (policy_dir, &scan,
340 (void *) (long) peer_id));
341 if (NULL == (components = REGEX_TEST_read_from_file (policy_filename)))
343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
344 "Policy file %s contains no policies. Exiting.\n",
346 global_ret = GNUNET_SYSERR;
347 GNUNET_SCHEDULER_shutdown ();
350 regex = REGEX_TEST_combine (components);
351 REGEX_TEST_free_from_file (components);
353 /* Announcing regexes from policy_filename */
354 GNUNET_asprintf (&rx_with_pfx, "%s(%s)(0|1)*", regex_prefix, regex);
355 announce_regex (rx_with_pfx);
357 GNUNET_free (rx_with_pfx);
359 /* Scheduled the task to clean up when shutdown is called */
360 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
366 * The main function of the regexprofiler service.
368 * @param argc number of arguments from the command line
369 * @param argv command line arguments
370 * @return 0 ok, 1 on error
373 main (int argc, char *const *argv)
375 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
376 GNUNET_GETOPT_OPTION_END
379 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
382 GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
384 ("Daemon to announce regular expressions for the peer using mesh."),
385 options, &run, NULL)) ? global_ret : 1;
393 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
395 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
397 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
398 mallopt (M_TOP_PAD, 1 * 1024);
404 /* end of gnunet-daemon-regexprofiler.c */