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 * Periodically reannounce regex.
64 static GNUNET_SCHEDULER_TaskIdentifier reannounce_task;
67 * What's the maximum reannounce period.
69 static struct GNUNET_TIME_Relative reannounce_period_max;
72 * Maximal path compression length for regex announcing.
74 static unsigned long long max_path_compression;
77 * Name of the file containing policies that this peer should announce. One
80 static char * policy_filename;
83 * Prefix to add before every regex we're announcing.
85 static char * regex_prefix;
90 static char *rx_with_pfx;
93 * How many put rounds should we do.
95 static unsigned int rounds = 5;
99 * Task run during shutdown.
105 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
109 if (NULL != announce_handle)
111 GNUNET_REGEX_announce_cancel (announce_handle);
112 announce_handle = NULL;
115 if (NULL != dht_handle)
117 GNUNET_DHT_disconnect (dht_handle);
121 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
122 "Daemon for %s shutting down\n",
128 * Announce a previously announced regex re-using cached data.
130 * @param cls Closure (regex to announce if needed).
131 * @param tc TaskContext.
134 reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 struct GNUNET_PeerIdentity id;
137 struct GNUNET_TIME_Relative random_delay;
140 reannounce_task = GNUNET_SCHEDULER_NO_TASK;
141 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
150 GNUNET_SCHEDULER_shutdown ();
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
155 GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
156 if (NULL == announce_handle && NULL != regex)
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "First time, creating regex: %s\n",
161 memset (&id, 0, sizeof (struct GNUNET_PeerIdentity));
162 announce_handle = GNUNET_REGEX_announce (dht_handle,
165 (unsigned int) max_path_compression,
170 GNUNET_assert (NULL != announce_handle);
171 GNUNET_REGEX_reannounce (announce_handle);
175 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
176 GNUNET_CRYPTO_random_u32 (
177 GNUNET_CRYPTO_QUALITY_WEAK,
178 reannounce_period_max.rel_value));
179 reannounce_task = GNUNET_SCHEDULER_add_delayed (random_delay,
180 &reannounce_regex, cls);
185 * Announce the given regular expression using regex and the path compression
186 * length read from config.
188 * @param regex regular expression to announce on this peer's mesh.
191 announce_regex (const char * regex)
195 if (NULL == regex || 0 == strlen (regex))
197 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
201 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
202 "Daemon for %s starting\n",
204 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == reannounce_task);
205 copy = GNUNET_strdup (regex);
206 reannounce_task = GNUNET_SCHEDULER_add_now (reannounce_regex, (void *) copy);
211 * Load regular expressions from filename into 'rxes' array. Array needs to be freed.
213 * @param filename filename of the file containing the regexes, one per line.
214 * @param rx string with the union of all regular expressions.
216 * @return number of regular expressions read from filename and in rxes array.
217 * FIXME use load regex lib function
220 load_regexes (const char *filename, char **rx)
228 if (GNUNET_YES != GNUNET_DISK_file_test (policy_filename))
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
231 "Could not find policy file %s\n", policy_filename);
234 if (GNUNET_OK != GNUNET_DISK_file_size (policy_filename, &filesize, GNUNET_YES, GNUNET_YES))
238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Policy file %s is empty.\n", policy_filename);
241 data = GNUNET_malloc (filesize);
242 if (filesize != GNUNET_DISK_fn_read (policy_filename, data, filesize))
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not read policy file %s.\n",
252 while (offset < (filesize - 1))
255 if ((data[offset] == '\n') && (buf != &data[offset]))
258 buf = &data[offset + 1];
261 else if ((data[offset] == '\n') || (data[offset] == '\0'))
262 buf = &data[offset + 1];
272 * @brief Main function that will be run by the scheduler.
275 * @param args remaining command-line arguments
276 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
277 * @param cfg_ configuration
280 run (void *cls, char *const *args GNUNET_UNUSED,
281 const char *cfgfile GNUNET_UNUSED,
282 const struct GNUNET_CONFIGURATION_Handle *cfg_)
289 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER", "MAX_PATH_COMPRESSION",
290 &max_path_compression))
292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
294 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
295 "regexprofiler", "max_path_compression");
296 global_ret = GNUNET_SYSERR;
297 GNUNET_SCHEDULER_shutdown ();
302 GNUNET_CONFIGURATION_get_value_filename (cfg, "REGEXPROFILER",
303 "POLICY_FILE", &policy_filename))
305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
308 "regexprofiler", "policy_file");
309 global_ret = GNUNET_SYSERR;
310 GNUNET_SCHEDULER_shutdown ();
315 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
316 "REGEX_PREFIX", ®ex_prefix))
318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
320 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
321 "regexprofiler", "regex_prefix");
322 global_ret = GNUNET_SYSERR;
323 GNUNET_SCHEDULER_shutdown ();
328 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
329 "REANNOUNCE_PERIOD_MAX",
330 &reannounce_period_max))
332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
333 "reannounce_period_max not given. Using 10 minutes.\n");
334 reannounce_period_max =
335 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
338 stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
340 dht_handle = GNUNET_DHT_connect (cfg, 1);
342 if (NULL == dht_handle)
344 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
345 "Could not acquire dht handle. Exiting.\n");
346 global_ret = GNUNET_SYSERR;
347 GNUNET_SCHEDULER_shutdown ();
351 /* Read regexes from policy files */
352 if (0 == load_regexes (policy_filename, ®ex))
354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
355 "Policy file %s contains no policies. Exiting.\n",
357 global_ret = GNUNET_SYSERR;
358 GNUNET_SCHEDULER_shutdown ();
362 /* Announcing regexes from policy_filename */
363 GNUNET_asprintf (&rx_with_pfx, "%s(%s)", regex_prefix, regex);
364 announce_regex (rx_with_pfx);
366 GNUNET_free (rx_with_pfx);
368 /* Scheduled the task to clean up when shutdown is called */
369 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
375 * The main function of the regexprofiler service.
377 * @param argc number of arguments from the command line
378 * @param argv command line arguments
379 * @return 0 ok, 1 on error
382 main (int argc, char *const *argv)
384 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
385 GNUNET_GETOPT_OPTION_END
388 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
391 GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
393 ("Daemon to announce regular expressions for the peer using mesh."),
394 options, &run, NULL)) ? global_ret : 1;
402 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
404 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
406 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
407 mallopt (M_TOP_PAD, 1 * 1024);
413 /* end of gnunet-daemon-regexprofiler.c */