add $(GN_LIBINTL) to Makefile.am (fixes 0005902)
[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  * Task run during shutdown.
106  *
107  * @param cls unused
108  */
109 static void
110 shutdown_task (void *cls)
111 {
112   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
113
114   if (NULL != announce_handle)
115   {
116     REGEX_INTERNAL_announce_cancel (announce_handle);
117     announce_handle = NULL;
118   }
119   if (NULL != reannounce_task)
120   {
121     GNUNET_free (GNUNET_SCHEDULER_cancel (reannounce_task));
122     reannounce_task = NULL;
123   }
124   if (NULL != dht_handle)
125   {
126     GNUNET_DHT_disconnect (dht_handle);
127     dht_handle = NULL;
128   }
129   GNUNET_free (my_private_key);
130   my_private_key = NULL;
131
132   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
133               "Daemon for %s shutting down\n",
134               policy_filename);
135 }
136
137
138 /**
139  * Announce a previously announced regex re-using cached data.
140  *
141  * @param cls Closure (regex to announce if needed).
142  */
143 static void
144 reannounce_regex (void *cls)
145 {
146   char *regex = cls;
147   struct GNUNET_TIME_Relative random_delay;
148
149   reannounce_task = NULL;
150   if (0 == rounds--)
151   {
152     global_ret = 0;
153     GNUNET_SCHEDULER_shutdown ();
154     GNUNET_free (regex);
155     return;
156   }
157   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
158   GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
159   if ((NULL == announce_handle) && (NULL != regex))
160   {
161     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
162                 "First time, creating regex: %s\n",
163                 regex);
164     announce_handle = REGEX_INTERNAL_announce (dht_handle,
165                                                my_private_key,
166                                                regex,
167                                                (unsigned
168                                                 int) max_path_compression,
169                                                stats_handle);
170   }
171   else
172   {
173     GNUNET_assert (NULL != announce_handle);
174     REGEX_INTERNAL_reannounce (announce_handle);
175   }
176
177   random_delay =
178     GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,
179                                    GNUNET_CRYPTO_random_u32 (
180                                      GNUNET_CRYPTO_QUALITY_WEAK,
181                                      reannounce_period_max.rel_value_us));
182   reannounce_task = GNUNET_SCHEDULER_add_delayed (random_delay,
183                                                   &reannounce_regex, cls);
184 }
185
186
187 /**
188  * Announce the given regular expression using regex and the path compression
189  * length read from config.
190  *
191  * @param regex regular expression to announce on this peer's cadet.
192  */
193 static void
194 announce_regex (const char *regex)
195 {
196   char *copy;
197
198   if ((NULL == regex) || (0 == strlen (regex)))
199   {
200     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
201     return;
202   }
203
204   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
205               "Daemon for %s starting\n",
206               policy_filename);
207   GNUNET_assert (NULL == reannounce_task);
208   copy = GNUNET_strdup (regex);
209   reannounce_task = GNUNET_SCHEDULER_add_now (&reannounce_regex,
210                                               (void *) copy);
211 }
212
213
214 /**
215  * Scan through the policy_dir looking for the n-th filename.
216  *
217  * @param cls Closure (target number n).
218  * @param filename complete filename (absolute path).
219  * @return GNUNET_OK to continue to iterate,
220  *  GNUNET_NO to stop when found
221  */
222 static int
223 scan (void *cls, const char *filename)
224 {
225   long n = (long) cls;
226   static long c = 0;
227
228   if (c == n)
229   {
230     policy_filename = GNUNET_strdup (filename);
231     return GNUNET_NO;
232   }
233   c++;
234   return GNUNET_OK;
235 }
236
237
238 /**
239  * @brief Main function that will be run by the scheduler.
240  *
241  * @param cls closure
242  * @param args remaining command-line arguments
243  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
244  * @param cfg_ configuration
245  */
246 static void
247 run (void *cls, char *const *args GNUNET_UNUSED,
248      const char *cfgfile GNUNET_UNUSED,
249      const struct GNUNET_CONFIGURATION_Handle *cfg_)
250 {
251   char *regex = NULL;
252   char **components;
253   char *policy_dir;
254   long long unsigned int peer_id;
255
256   cfg = cfg_;
257
258   my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
259   GNUNET_assert (NULL != my_private_key);
260   if (GNUNET_OK !=
261       GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
262                                              "MAX_PATH_COMPRESSION",
263                                              &max_path_compression))
264   {
265     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
266                 _
267                 (
268                   "%s service is lacking key configuration settings (%s).  Exiting.\n"),
269                 "regexprofiler", "max_path_compression");
270     global_ret = GNUNET_SYSERR;
271     GNUNET_SCHEDULER_shutdown ();
272     return;
273   }
274   if (GNUNET_OK !=
275       GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
276                                              "POLICY_DIR", &policy_dir))
277   {
278     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER",
279                                "POLICY_DIR");
280     global_ret = GNUNET_SYSERR;
281     GNUNET_SCHEDULER_shutdown ();
282     return;
283   }
284   if (GNUNET_OK !=
285       GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
286                                              "PEERID", &peer_id))
287   {
288     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "TESTBED", "PEERID");
289     global_ret = GNUNET_SYSERR;
290     GNUNET_free (policy_dir);
291     GNUNET_SCHEDULER_shutdown ();
292     return;
293   }
294
295   if (GNUNET_OK !=
296       GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
297                                              "REGEX_PREFIX", &regex_prefix))
298   {
299     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER",
300                                "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, 16);
347   REGEX_TEST_free_from_file (components);
348
349   /* Announcing regexes from policy_filename */
350   GNUNET_asprintf (&rx_with_pfx,
351                    "%s(%s)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)*",
352                    regex_prefix,
353                    regex);
354   announce_regex (rx_with_pfx);
355   GNUNET_free (regex);
356   GNUNET_free (rx_with_pfx);
357
358   /* Scheduled the task to clean up when shutdown is called */
359   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
360                                  NULL);
361 }
362
363
364 /**
365  * The main function of the regexprofiler service.
366  *
367  * @param argc number of arguments from the command line
368  * @param argv command line arguments
369  * @return 0 ok, 1 on error
370  */
371 int
372 main (int argc, char *const *argv)
373 {
374   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
375     GNUNET_GETOPT_OPTION_END
376   };
377
378   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
379     return 2;
380   return (GNUNET_OK ==
381           GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
382                               gettext_noop
383                               (
384                                 "Daemon to announce regular expressions for the peer using cadet."),
385                               options, &run, NULL)) ? global_ret : 1;
386 }
387
388
389 #if defined(LINUX) && defined(__GLIBC__)
390 #include <malloc.h>
391
392 /**
393  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
394  */
395 void __attribute__ ((constructor))
396 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
403
404 #endif
405
406
407 /* end of gnunet-daemon-regexprofiler.c */