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 "gnunet_regex_lib.h"
33 #include "gnunet_dht_service.h"
34 #include "gnunet_statistics_service.h"
37 * Return value from 'main'.
39 static int global_ret;
42 * Configuration we use.
44 static const struct GNUNET_CONFIGURATION_Handle *cfg;
47 * Handle to the statistics service.
49 static struct GNUNET_STATISTICS_Handle *stats_handle;
54 static struct GNUNET_DHT_Handle *dht_handle;
57 * Peer's regex announce handle.
59 static struct GNUNET_REGEX_announce_handle *announce_handle;
62 * Hostkey generation context
64 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
67 * Periodically reannounce regex.
69 static GNUNET_SCHEDULER_TaskIdentifier reannounce_task;
72 * How often reannounce regex.
74 static struct GNUNET_TIME_Relative reannounce_freq;
77 * Random delay to spread out load on the DHT.
79 static struct GNUNET_TIME_Relative announce_delay;
82 * Local peer's PeerID.
84 static struct GNUNET_PeerIdentity my_full_id;
87 * Maximal path compression length for regex announcing.
89 static unsigned long long max_path_compression;
92 * Name of the file containing policies that this peer should announce. One
95 static char * policy_filename;
98 * Prefix to add before every regex we're announcing.
100 static char * regex_prefix;
105 static char *rx_with_pfx;
109 * Task run during shutdown.
115 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
121 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
124 if (NULL != announce_handle)
126 GNUNET_REGEX_announce_cancel (announce_handle);
127 announce_handle = NULL;
130 if (NULL != dht_handle)
132 GNUNET_DHT_disconnect (dht_handle);
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
141 * Announce a previously announced regex re-using cached data.
143 * @param cls Closure (regex to announce if needed).
144 * @param tc TaskContext.
147 reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
150 reannounce_task = GNUNET_SCHEDULER_NO_TASK;
151 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
158 GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
159 if (NULL == announce_handle && NULL != regex)
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
162 "First time, creating regex: %s\n",
164 announce_handle = GNUNET_REGEX_announce (dht_handle,
167 (unsigned int) max_path_compression,
172 GNUNET_assert (NULL != announce_handle);
173 GNUNET_REGEX_reannounce (announce_handle);
177 GNUNET_SCHEDULER_add_delayed (
178 GNUNET_TIME_relative_add (reannounce_freq,
179 GNUNET_TIME_relative_multiply (
180 GNUNET_TIME_UNIT_SECONDS,
181 GNUNET_CRYPTO_random_u32 (
182 GNUNET_CRYPTO_QUALITY_WEAK,
190 * Announce the given regular expression using Mesh and the path compression
191 * length read from config.
193 * @param regex regular expression to announce on this peer's mesh.
196 announce_regex (const char * regex)
200 if (NULL == regex || 0 == strlen (regex))
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
206 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == reannounce_task);
207 copy = GNUNET_strdup (regex);
208 reannounce_task = GNUNET_SCHEDULER_add_delayed (announce_delay,
215 * Load regular expressions from filename into 'rxes' array. Array needs to be freed.
217 * @param filename filename of the file containing the regexes, one per line.
218 * @param rx string with the union of all regular expressions.
220 * @return number of regular expressions read from filename and in rxes array.
221 * FIXME use load regex lib function
224 load_regexes (const char *filename, char **rx)
232 if (GNUNET_YES != GNUNET_DISK_file_test (policy_filename))
234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235 "Could not find policy file %s\n", policy_filename);
238 if (GNUNET_OK != GNUNET_DISK_file_size (policy_filename, &filesize, GNUNET_YES, GNUNET_YES))
242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Policy file %s is empty.\n", policy_filename);
245 data = GNUNET_malloc (filesize);
246 if (filesize != GNUNET_DISK_fn_read (policy_filename, data, filesize))
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not read policy file %s.\n",
256 while (offset < (filesize - 1))
259 if ((data[offset] == '\n') && (buf != &data[offset]))
262 buf = &data[offset + 1];
265 else if ((data[offset] == '\n') || (data[offset] == '\0'))
266 buf = &data[offset + 1];
276 * Callback for hostkey read/generation
278 * @param cls Closure (not used).
279 * @param pk The private key of the local peer.
280 * @param emsg Error message if applicable.
283 key_generation_cb (void *cls,
284 struct GNUNET_CRYPTO_EccPrivateKey *pk,
287 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
293 _("Regexprofiler could not access hostkey: %s. Exiting.\n"),
295 GNUNET_SCHEDULER_shutdown ();
299 GNUNET_CRYPTO_ecc_key_get_public (pk, &my_public_key);
300 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
301 &my_full_id.hashPubKey);
303 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
304 "Regexprofiler for peer [%s] starting\n",
305 GNUNET_i2s(&my_full_id));
306 announce_regex (rx_with_pfx);
307 GNUNET_free (rx_with_pfx);
312 * @brief Main function that will be run by the scheduler.
315 * @param args remaining command-line arguments
316 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
317 * @param cfg_ configuration
320 run (void *cls, char *const *args GNUNET_UNUSED,
321 const char *cfgfile GNUNET_UNUSED,
322 const struct GNUNET_CONFIGURATION_Handle *cfg_)
330 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY",
333 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
335 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
336 "regexdaemon", "peer/privatekey");
337 GNUNET_SCHEDULER_shutdown ();
342 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER", "MAX_PATH_COMPRESSION",
343 &max_path_compression))
345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
347 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
348 "regexprofiler", "max_path_compression");
349 global_ret = GNUNET_SYSERR;
350 GNUNET_SCHEDULER_shutdown ();
355 GNUNET_CONFIGURATION_get_value_filename (cfg, "REGEXPROFILER",
356 "POLICY_FILE", &policy_filename))
358 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
360 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
361 "regexprofiler", "policy_file");
362 global_ret = GNUNET_SYSERR;
363 GNUNET_SCHEDULER_shutdown ();
368 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
369 "REGEX_PREFIX", ®ex_prefix))
371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
373 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
374 "regexprofiler", "regex_prefix");
375 global_ret = GNUNET_SYSERR;
376 GNUNET_SCHEDULER_shutdown ();
381 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
382 "REANNOUNCE_FREQ", &reannounce_freq))
384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
385 "reannounce_freq not given. Using 10 minutes.\n");
387 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
391 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
392 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 600));
394 stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
396 dht_handle = GNUNET_DHT_connect (cfg, 1);
398 if (NULL == dht_handle)
400 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
401 "Could not acquire dht handle. Exiting.\n");
402 global_ret = GNUNET_SYSERR;
403 GNUNET_SCHEDULER_shutdown ();
407 /* Read regexes from policy files */
408 if (0 == load_regexes (policy_filename, ®ex))
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
411 "Policy file %s contains no policies. Exiting.\n",
413 global_ret = GNUNET_SYSERR;
414 GNUNET_SCHEDULER_shutdown ();
418 /* Announcing regexes from policy_filename */
419 GNUNET_asprintf (&rx_with_pfx, "%s(%s)", regex_prefix, regex);
422 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
425 GNUNET_free (keyfile);
427 /* Scheduled the task to clean up when shutdown is called */
428 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
434 * The main function of the regexprofiler service.
436 * @param argc number of arguments from the command line
437 * @param argv command line arguments
438 * @return 0 ok, 1 on error
441 main (int argc, char *const *argv)
443 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
444 GNUNET_GETOPT_OPTION_END
447 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
450 GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
452 ("Daemon to announce regular expressions for the peer using mesh."),
453 options, &run, NULL)) ? global_ret : 1;
461 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
463 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
465 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
466 mallopt (M_TOP_PAD, 1 * 1024);
472 /* end of gnunet-daemon-regexprofiler.c */