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 * How often reannounce regex.
69 static struct GNUNET_TIME_Relative reannounce_freq;
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;
94 * Task run during shutdown.
100 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
104 if (NULL != announce_handle)
106 GNUNET_REGEX_announce_cancel (announce_handle);
107 announce_handle = NULL;
110 if (NULL != dht_handle)
112 GNUNET_DHT_disconnect (dht_handle);
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
121 * Announce a previously announced regex re-using cached data.
123 * @param cls Closure (regex to announce if needed).
124 * @param tc TaskContext.
127 reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
129 struct GNUNET_PeerIdentity id;
130 struct GNUNET_TIME_Relative random_delay;
133 reannounce_task = GNUNET_SCHEDULER_NO_TASK;
134 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
141 GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
142 if (NULL == announce_handle && NULL != regex)
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145 "First time, creating regex: %s\n",
147 memset (&id, 0, sizeof (struct GNUNET_PeerIdentity));
148 announce_handle = GNUNET_REGEX_announce (dht_handle,
151 (unsigned int) max_path_compression,
156 GNUNET_assert (NULL != announce_handle);
157 GNUNET_REGEX_reannounce (announce_handle);
160 random_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
161 GNUNET_CRYPTO_random_u32 (
162 GNUNET_CRYPTO_QUALITY_WEAK,
165 GNUNET_SCHEDULER_add_delayed (
166 GNUNET_TIME_relative_add (reannounce_freq, random_delay),
173 * Announce the given regular expression using regex and the path compression
174 * length read from config.
176 * @param regex regular expression to announce on this peer's mesh.
179 announce_regex (const char * regex)
183 if (NULL == regex || 0 == strlen (regex))
185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
189 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == reannounce_task);
190 copy = GNUNET_strdup (regex);
191 reannounce_task = GNUNET_SCHEDULER_add_now (reannounce_regex, (void *) copy);
196 * Load regular expressions from filename into 'rxes' array. Array needs to be freed.
198 * @param filename filename of the file containing the regexes, one per line.
199 * @param rx string with the union of all regular expressions.
201 * @return number of regular expressions read from filename and in rxes array.
202 * FIXME use load regex lib function
205 load_regexes (const char *filename, char **rx)
213 if (GNUNET_YES != GNUNET_DISK_file_test (policy_filename))
215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
216 "Could not find policy file %s\n", policy_filename);
219 if (GNUNET_OK != GNUNET_DISK_file_size (policy_filename, &filesize, GNUNET_YES, GNUNET_YES))
223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Policy file %s is empty.\n", policy_filename);
226 data = GNUNET_malloc (filesize);
227 if (filesize != GNUNET_DISK_fn_read (policy_filename, data, filesize))
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not read policy file %s.\n",
237 while (offset < (filesize - 1))
240 if ((data[offset] == '\n') && (buf != &data[offset]))
243 buf = &data[offset + 1];
246 else if ((data[offset] == '\n') || (data[offset] == '\0'))
247 buf = &data[offset + 1];
257 * @brief Main function that will be run by the scheduler.
260 * @param args remaining command-line arguments
261 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
262 * @param cfg_ configuration
265 run (void *cls, char *const *args GNUNET_UNUSED,
266 const char *cfgfile GNUNET_UNUSED,
267 const struct GNUNET_CONFIGURATION_Handle *cfg_)
274 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER", "MAX_PATH_COMPRESSION",
275 &max_path_compression))
277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
279 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
280 "regexprofiler", "max_path_compression");
281 global_ret = GNUNET_SYSERR;
282 GNUNET_SCHEDULER_shutdown ();
287 GNUNET_CONFIGURATION_get_value_filename (cfg, "REGEXPROFILER",
288 "POLICY_FILE", &policy_filename))
290 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
293 "regexprofiler", "policy_file");
294 global_ret = GNUNET_SYSERR;
295 GNUNET_SCHEDULER_shutdown ();
300 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
301 "REGEX_PREFIX", ®ex_prefix))
303 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
305 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
306 "regexprofiler", "regex_prefix");
307 global_ret = GNUNET_SYSERR;
308 GNUNET_SCHEDULER_shutdown ();
313 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
314 "REANNOUNCE_FREQ", &reannounce_freq))
316 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
317 "reannounce_freq not given. Using 10 minutes.\n");
319 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
323 stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
325 dht_handle = GNUNET_DHT_connect (cfg, 1);
327 if (NULL == dht_handle)
329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
330 "Could not acquire dht handle. Exiting.\n");
331 global_ret = GNUNET_SYSERR;
332 GNUNET_SCHEDULER_shutdown ();
336 /* Read regexes from policy files */
337 if (0 == load_regexes (policy_filename, ®ex))
339 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
340 "Policy file %s contains no policies. Exiting.\n",
342 global_ret = GNUNET_SYSERR;
343 GNUNET_SCHEDULER_shutdown ();
347 /* Announcing regexes from policy_filename */
348 GNUNET_asprintf (&rx_with_pfx, "%s(%s)", regex_prefix, regex);
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_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &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 mesh."),
379 options, &run, NULL)) ? global_ret : 1;
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 */