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