global reindent, now with uncrustify hook enabled
[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      SPDX-License-Identifier: AGPL3.0-or-later
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  */
110 static void
111 shutdown_task (void *cls)
112 {
113   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
114
115   if (NULL != announce_handle)
116   {
117     REGEX_INTERNAL_announce_cancel (announce_handle);
118     announce_handle = NULL;
119   }
120   if (NULL != reannounce_task)
121   {
122     GNUNET_free (GNUNET_SCHEDULER_cancel (reannounce_task));
123     reannounce_task = NULL;
124   }
125   if (NULL != dht_handle)
126   {
127     GNUNET_DHT_disconnect (dht_handle);
128     dht_handle = NULL;
129   }
130   GNUNET_free (my_private_key);
131   my_private_key = NULL;
132
133   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
134               "Daemon for %s shutting down\n",
135               policy_filename);
136 }
137
138
139 /**
140  * Announce a previously announced regex re-using cached data.
141  *
142  * @param cls Closure (regex to announce if needed).
143  */
144 static void
145 reannounce_regex (void *cls)
146 {
147   char *regex = cls;
148   struct GNUNET_TIME_Relative random_delay;
149
150   reannounce_task = NULL;
151   if (0 == rounds--)
152   {
153     global_ret = 0;
154     GNUNET_SCHEDULER_shutdown ();
155     GNUNET_free (regex);
156     return;
157   }
158   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
159   GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
160   if ((NULL == announce_handle)&&(NULL != regex))
161   {
162     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163                 "First time, creating regex: %s\n",
164                 regex);
165     announce_handle = REGEX_INTERNAL_announce (dht_handle,
166                                                my_private_key,
167                                                regex,
168                                                (unsigned
169                                                 int) max_path_compression,
170                                                stats_handle);
171   }
172   else
173   {
174     GNUNET_assert (NULL != announce_handle);
175     REGEX_INTERNAL_reannounce (announce_handle);
176   }
177
178   random_delay =
179     GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,
180                                    GNUNET_CRYPTO_random_u32 (
181                                      GNUNET_CRYPTO_QUALITY_WEAK,
182                                      reannounce_period_max.rel_value_us));
183   reannounce_task = GNUNET_SCHEDULER_add_delayed (random_delay,
184                                                   &reannounce_regex, cls);
185 }
186
187
188 /**
189  * Announce the given regular expression using regex and the path compression
190  * length read from config.
191  *
192  * @param regex regular expression to announce on this peer's cadet.
193  */
194 static void
195 announce_regex (const char *regex)
196 {
197   char *copy;
198
199   if ((NULL == regex)||(0 == strlen (regex)))
200   {
201     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
202     return;
203   }
204
205   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
206               "Daemon for %s starting\n",
207               policy_filename);
208   GNUNET_assert (NULL == reannounce_task);
209   copy = GNUNET_strdup (regex);
210   reannounce_task = GNUNET_SCHEDULER_add_now (&reannounce_regex,
211                                               (void *) copy);
212 }
213
214
215 /**
216  * Scan through the policy_dir looking for the n-th filename.
217  *
218  * @param cls Closure (target number n).
219  * @param filename complete filename (absolute path).
220  * @return GNUNET_OK to continue to iterate,
221  *  GNUNET_NO to stop when found
222  */
223 static int
224 scan (void *cls, const char *filename)
225 {
226   long n = (long) cls;
227   static long c = 0;
228
229   if (c == n)
230   {
231     policy_filename = GNUNET_strdup (filename);
232     return GNUNET_NO;
233   }
234   c++;
235   return GNUNET_OK;
236 }
237
238
239 /**
240  * @brief Main function that will be run by the scheduler.
241  *
242  * @param cls closure
243  * @param args remaining command-line arguments
244  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
245  * @param cfg_ configuration
246  */
247 static void
248 run (void *cls, char *const *args GNUNET_UNUSED,
249      const char *cfgfile GNUNET_UNUSED,
250      const struct GNUNET_CONFIGURATION_Handle *cfg_)
251 {
252   char *regex = NULL;
253   char **components;
254   char *policy_dir;
255   long long unsigned int peer_id;
256
257   cfg = cfg_;
258
259   my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
260   GNUNET_assert (NULL != my_private_key);
261   if (GNUNET_OK !=
262       GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
263                                              "MAX_PATH_COMPRESSION",
264                                              &max_path_compression))
265   {
266     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
267                 _
268                 (
269                   "%s service is lacking key configuration settings (%s).  Exiting.\n"),
270                 "regexprofiler", "max_path_compression");
271     global_ret = GNUNET_SYSERR;
272     GNUNET_SCHEDULER_shutdown ();
273     return;
274   }
275   if (GNUNET_OK !=
276       GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
277                                              "POLICY_DIR", &policy_dir))
278   {
279     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER",
280                                "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",
301                                "REGEX_PREFIX");
302     global_ret = GNUNET_SYSERR;
303     GNUNET_free (policy_dir);
304     GNUNET_SCHEDULER_shutdown ();
305     return;
306   }
307
308   if (GNUNET_OK !=
309       GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
310                                            "REANNOUNCE_PERIOD_MAX",
311                                            &reannounce_period_max))
312   {
313     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
314                 "reannounce_period_max not given. Using 10 minutes.\n");
315     reannounce_period_max =
316       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
317   }
318
319   stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
320
321   dht_handle = GNUNET_DHT_connect (cfg, 1);
322
323   if (NULL == dht_handle)
324   {
325     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
326                 "Could not acquire dht handle. Exiting.\n");
327     global_ret = GNUNET_SYSERR;
328     GNUNET_free (policy_dir);
329     GNUNET_SCHEDULER_shutdown ();
330     return;
331   }
332
333   /* Read regexes from policy files */
334   GNUNET_assert (-1 != GNUNET_DISK_directory_scan (policy_dir, &scan,
335                                                    (void *) (long) peer_id));
336   if (NULL == (components = REGEX_TEST_read_from_file (policy_filename)))
337   {
338     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
339                 "Policy file %s contains no policies. Exiting.\n",
340                 policy_filename);
341     global_ret = GNUNET_SYSERR;
342     GNUNET_free (policy_dir);
343     GNUNET_SCHEDULER_shutdown ();
344     return;
345   }
346   GNUNET_free (policy_dir);
347   regex = REGEX_TEST_combine (components, 16);
348   REGEX_TEST_free_from_file (components);
349
350   /* Announcing regexes from policy_filename */
351   GNUNET_asprintf (&rx_with_pfx,
352                    "%s(%s)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)*",
353                    regex_prefix,
354                    regex);
355   announce_regex (rx_with_pfx);
356   GNUNET_free (regex);
357   GNUNET_free (rx_with_pfx);
358
359   /* Scheduled the task to clean up when shutdown is called */
360   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
361                                  NULL);
362 }
363
364
365 /**
366  * The main function of the regexprofiler service.
367  *
368  * @param argc number of arguments from the command line
369  * @param argv command line arguments
370  * @return 0 ok, 1 on error
371  */
372 int
373 main (int argc, char *const *argv)
374 {
375   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
376     GNUNET_GETOPT_OPTION_END
377   };
378
379   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
380     return 2;
381   return (GNUNET_OK ==
382           GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
383                               gettext_noop
384                               (
385                                 "Daemon to announce regular expressions for the peer using cadet."),
386                               options, &run, NULL)) ? global_ret : 1;
387 }
388
389
390 #if defined(LINUX) && defined(__GLIBC__)
391 #include <malloc.h>
392
393 /**
394  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
395  */
396 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
397 {
398   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
399   mallopt (M_TOP_PAD, 1 * 1024);
400   malloc_trim (0);
401 }
402 #endif
403
404
405 /* end of gnunet-daemon-regexprofiler.c */