-doxygen fixes
[oweals/gnunet.git] / src / regex / gnunet-daemon-regexprofiler.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012, 2013 Christian Grothoff
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file regex/gnunet-daemon-regexprofiler.c
23  * @brief daemon that uses cadet 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 cadet service running on the
26  * peer from within the profiler.
27  * @author Maximilian Szengel
28  * @author Bartlomiej Polot
29  */
30 #include "platform.h"
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"
36
37 /**
38  * Return value from 'main'.
39  */
40 static int global_ret;
41
42 /**
43  * Configuration we use.
44  */
45 static const struct GNUNET_CONFIGURATION_Handle *cfg;
46
47 /**
48  * Handle to the statistics service.
49  */
50 static struct GNUNET_STATISTICS_Handle *stats_handle;
51
52 /**
53  * Peer's dht handle.
54  */
55 static struct GNUNET_DHT_Handle *dht_handle;
56
57 /**
58  * Peer's regex announce handle.
59  */
60 static struct REGEX_INTERNAL_Announcement *announce_handle;
61
62 /**
63  * Periodically reannounce regex.
64  */
65 static struct GNUNET_SCHEDULER_Task * reannounce_task;
66
67 /**
68  * What's the maximum reannounce period.
69  */
70 static struct GNUNET_TIME_Relative reannounce_period_max;
71
72 /**
73  * Maximal path compression length for regex announcing.
74  */
75 static unsigned long long max_path_compression;
76
77 /**
78  * Name of the file containing policies that this peer should announce. One
79  * policy per line.
80  */
81 static char * policy_filename;
82
83 /**
84  * Prefix to add before every regex we're announcing.
85  */
86 static char * regex_prefix;
87
88 /**
89  * Regex with prefix.
90  */
91 static char *rx_with_pfx;
92
93 /**
94  * How many put rounds should we do.
95  */
96 static unsigned int rounds = 3;
97
98 /**
99  * Private key for this peer.
100  */
101 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
102
103
104
105 /**
106  * Task run during shutdown.
107  *
108  * @param cls unused
109  * @param tc unused
110  */
111 static void
112 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
113 {
114   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
115
116   if (NULL != announce_handle)
117   {
118     REGEX_INTERNAL_announce_cancel (announce_handle);
119     announce_handle = NULL;
120   }
121
122   if (NULL != dht_handle)
123   {
124     GNUNET_DHT_disconnect (dht_handle);
125     dht_handle = NULL;
126   }
127   GNUNET_free (my_private_key);
128   my_private_key = NULL;
129
130   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
131               "Daemon for %s shutting down\n",
132               policy_filename);
133 }
134
135
136 /**
137  * Announce a previously announced regex re-using cached data.
138  *
139  * @param cls Closure (regex to announce if needed).
140  * @param tc TaskContext.
141  */
142 static void
143 reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
144 {
145   struct GNUNET_TIME_Relative random_delay;
146   char *regex = cls;
147
148   reannounce_task = NULL;
149   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
150   {
151     GNUNET_free (regex);
152     return;
153   }
154
155   if (0 == rounds--)
156   {
157     global_ret = 0;
158     GNUNET_SCHEDULER_shutdown ();
159     GNUNET_free (regex);
160     return;
161   }
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)
165   {
166     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167                 "First time, creating regex: %s\n",
168                 regex);
169     announce_handle = REGEX_INTERNAL_announce (dht_handle,
170                                                my_private_key,
171                                                regex,
172                                                (unsigned int) max_path_compression,
173                                                stats_handle);
174   }
175   else
176   {
177     GNUNET_assert (NULL != announce_handle);
178     REGEX_INTERNAL_reannounce (announce_handle);
179   }
180
181   random_delay =
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);
188 }
189
190
191 /**
192  * Announce the given regular expression using regex and the path compression
193  * length read from config.
194  *
195  * @param regex regular expression to announce on this peer's cadet.
196  */
197 static void
198 announce_regex (const char * regex)
199 {
200   char *copy;
201
202   if (NULL == regex || 0 == strlen (regex))
203   {
204     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
205     return;
206   }
207
208   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
209               "Daemon for %s starting\n",
210               policy_filename);
211   GNUNET_assert (NULL == reannounce_task);
212   copy = GNUNET_strdup (regex);
213   reannounce_task = GNUNET_SCHEDULER_add_now (reannounce_regex, (void *) copy);
214 }
215
216
217 /**
218  * Scan through the policy_dir looking for the n-th filename.
219  *
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
224  */
225 static int
226 scan (void *cls, const char *filename)
227 {
228   long n = (long) cls;
229   static long c = 0;
230
231   if (c == n)
232   {
233     policy_filename = GNUNET_strdup (filename);
234     return GNUNET_NO;
235   }
236   c++;
237   return GNUNET_OK;
238 }
239
240
241 /**
242  * @brief Main function that will be run by the scheduler.
243  *
244  * @param cls closure
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
248  */
249 static void
250 run (void *cls, char *const *args GNUNET_UNUSED,
251      const char *cfgfile GNUNET_UNUSED,
252      const struct GNUNET_CONFIGURATION_Handle *cfg_)
253 {
254   char *regex = NULL;
255   char **components;
256   char *policy_dir;
257   long long unsigned int peer_id;
258
259   cfg = cfg_;
260
261   my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
262   GNUNET_assert (NULL != my_private_key);
263   if (GNUNET_OK !=
264       GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
265                                              "MAX_PATH_COMPRESSION",
266                                              &max_path_compression))
267   {
268     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
269                 _
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 ();
274     return;
275   }
276   if (GNUNET_OK !=
277       GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
278                                              "POLICY_DIR", &policy_dir))
279   {
280     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "POLICY_DIR");
281     global_ret = GNUNET_SYSERR;
282     GNUNET_SCHEDULER_shutdown ();
283     return;
284   }
285   if (GNUNET_OK !=
286       GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
287                                              "PEERID", &peer_id))
288   {
289     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "TESTBED", "PEERID");
290     global_ret = GNUNET_SYSERR;
291     GNUNET_free (policy_dir);
292     GNUNET_SCHEDULER_shutdown ();
293     return;
294   }
295
296   if (GNUNET_OK !=
297       GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
298                                              "REGEX_PREFIX", &regex_prefix))
299   {
300     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "REGEX_PREFIX");
301     global_ret = GNUNET_SYSERR;
302     GNUNET_free (policy_dir);
303     GNUNET_SCHEDULER_shutdown ();
304     return;
305   }
306
307   if (GNUNET_OK !=
308       GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
309                                            "REANNOUNCE_PERIOD_MAX",
310                                            &reannounce_period_max))
311   {
312     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
313                 "reannounce_period_max not given. Using 10 minutes.\n");
314     reannounce_period_max =
315       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
316   }
317
318   stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
319
320   dht_handle = GNUNET_DHT_connect (cfg, 1);
321
322   if (NULL == dht_handle)
323   {
324     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
325                 "Could not acquire dht handle. Exiting.\n");
326     global_ret = GNUNET_SYSERR;
327     GNUNET_free (policy_dir);
328     GNUNET_SCHEDULER_shutdown ();
329     return;
330   }
331
332   /* Read regexes from policy files */
333   GNUNET_assert (-1 != GNUNET_DISK_directory_scan (policy_dir, &scan,
334                                                    (void *) (long) peer_id));
335   if (NULL == (components = REGEX_TEST_read_from_file (policy_filename)))
336   {
337     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
338                 "Policy file %s contains no policies. Exiting.\n",
339                 policy_filename);
340     global_ret = GNUNET_SYSERR;
341     GNUNET_free (policy_dir);
342     GNUNET_SCHEDULER_shutdown ();
343     return;
344   }
345   GNUNET_free (policy_dir);
346   regex = REGEX_TEST_combine (components);
347   REGEX_TEST_free_from_file (components);
348
349   /* Announcing regexes from policy_filename */
350   GNUNET_asprintf (&rx_with_pfx, "%s(%s)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)*", regex_prefix, regex);
351   announce_regex (rx_with_pfx);
352   GNUNET_free (regex);
353   GNUNET_free (rx_with_pfx);
354
355   /* Scheduled the task to clean up when shutdown is called */
356   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
357                                 NULL);
358 }
359
360
361 /**
362  * The main function of the regexprofiler service.
363  *
364  * @param argc number of arguments from the command line
365  * @param argv command line arguments
366  * @return 0 ok, 1 on error
367  */
368 int
369 main (int argc, char *const *argv)
370 {
371   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
372     GNUNET_GETOPT_OPTION_END
373   };
374
375   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
376     return 2;
377   return (GNUNET_OK ==
378           GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
379                               gettext_noop
380                               ("Daemon to announce regular expressions for the peer using cadet."),
381                               options, &run, NULL)) ? global_ret : 1;
382 }
383
384
385 #if defined(LINUX) && defined(__GLIBC__)
386 #include <malloc.h>
387
388 /**
389  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
390  */
391 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
392 {
393   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
394   mallopt (M_TOP_PAD, 1 * 1024);
395   malloc_trim (0);
396 }
397 #endif
398
399
400 /* end of gnunet-daemon-regexprofiler.c */