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