- remove regex daemon dependency to peers' public keys: allow to use HEAD regex with...
[oweals/gnunet.git] / src / regex / gnunet-daemon-regexprofiler.c
1 /*
2      This file is part of GNUnet.
3      (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 mesh 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 mesh 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 "gnunet_regex_lib.h"
33 #include "gnunet_dht_service.h"
34 #include "gnunet_statistics_service.h"
35
36 /**
37  * Return value from 'main'.
38  */
39 static int global_ret;
40
41 /**
42  * Configuration we use.
43  */
44 static const struct GNUNET_CONFIGURATION_Handle *cfg;
45
46 /**
47  * Handle to the statistics service.
48  */
49 static struct GNUNET_STATISTICS_Handle *stats_handle;
50
51 /**
52  * Peer's dht handle.
53  */
54 static struct GNUNET_DHT_Handle *dht_handle;
55
56 /**
57  * Peer's regex announce handle.
58  */
59 static struct GNUNET_REGEX_announce_handle *announce_handle;
60
61 /**
62  * Periodically reannounce regex.
63  */
64 static GNUNET_SCHEDULER_TaskIdentifier reannounce_task;
65
66 /**
67  * How often reannounce regex.
68  */
69 static struct GNUNET_TIME_Relative reannounce_freq;
70
71 /**
72  * Random delay to spread out load on the DHT.
73  */
74 static struct GNUNET_TIME_Relative announce_delay;
75
76 /**
77  * Maximal path compression length for regex announcing.
78  */
79 static unsigned long long max_path_compression;
80
81 /**
82  * Name of the file containing policies that this peer should announce. One
83  * policy per line.
84  */
85 static char * policy_filename;
86
87 /**
88  * Prefix to add before every regex we're announcing.
89  */
90 static char * regex_prefix;
91
92 /**
93  * Regex with prefix.
94  */
95 static char *rx_with_pfx;
96
97
98 /**
99  * Task run during shutdown.
100  *
101  * @param cls unused
102  * @param tc unused
103  */
104 static void
105 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
106 {
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
108
109   if (NULL != announce_handle)
110   {
111     GNUNET_REGEX_announce_cancel (announce_handle);
112     announce_handle = NULL;
113   }
114
115   if (NULL != dht_handle)
116   {
117     GNUNET_DHT_disconnect (dht_handle);
118     dht_handle = NULL;
119   }
120
121   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
122 }
123
124
125 /**
126  * Announce a previously announced regex re-using cached data.
127  * 
128  * @param cls Closure (regex to announce if needed).
129  * @param tc TaskContext.
130  */
131 static void
132 reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
133 {
134   struct GNUNET_PeerIdentity id;
135   char *regex = cls;
136
137   reannounce_task = GNUNET_SCHEDULER_NO_TASK;
138   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
139   {
140     GNUNET_free (regex);
141     return;
142   }
143
144   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
145   GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
146   if (NULL == announce_handle && NULL != regex)
147   {
148     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149                 "First time, creating regex: %s\n",
150                 regex);
151     memset (&id, 0, sizeof (struct GNUNET_PeerIdentity));
152     announce_handle = GNUNET_REGEX_announce (dht_handle,
153                                             &id,
154                                             regex,
155                                             (unsigned int) max_path_compression,
156                                             stats_handle);
157   }
158   else
159   {
160     GNUNET_assert (NULL != announce_handle);
161     GNUNET_REGEX_reannounce (announce_handle);
162   }
163
164   reannounce_task = 
165     GNUNET_SCHEDULER_add_delayed (
166       GNUNET_TIME_relative_add (reannounce_freq,
167                                 GNUNET_TIME_relative_multiply (
168                                   GNUNET_TIME_UNIT_SECONDS,
169                                   GNUNET_CRYPTO_random_u32 (
170                                     GNUNET_CRYPTO_QUALITY_WEAK,
171                                     600))),
172       &reannounce_regex,
173       cls);
174 }
175
176
177 /**
178  * Announce the given regular expression using Mesh and the path compression
179  * length read from config.
180  *
181  * @param regex regular expression to announce on this peer's mesh.
182  */
183 static void
184 announce_regex (const char * regex)
185 {
186   char *copy;
187
188   if (NULL == regex || 0 == strlen (regex))
189   {
190     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
191     return;
192   }
193
194   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == reannounce_task);
195   copy = GNUNET_strdup (regex);
196   reannounce_task = GNUNET_SCHEDULER_add_delayed (announce_delay,
197                                                   reannounce_regex,
198                                                   (void *) copy);
199 }
200
201
202 /**
203  * Load regular expressions from filename into 'rxes' array. Array needs to be freed.
204  *
205  * @param filename filename of the file containing the regexes, one per line.
206  * @param rx string with the union of all regular expressions.
207  *
208  * @return number of regular expressions read from filename and in rxes array.
209  * FIXME use load regex lib function
210  */
211 static unsigned int
212 load_regexes (const char *filename, char **rx)
213 {
214   char *data;
215   char *buf;
216   uint64_t filesize;
217   unsigned int offset;
218   unsigned int rx_cnt;
219
220   if (GNUNET_YES != GNUNET_DISK_file_test (policy_filename))
221   {
222     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
223                 "Could not find policy file %s\n", policy_filename);
224     return 0;
225   }
226   if (GNUNET_OK != GNUNET_DISK_file_size (policy_filename, &filesize, GNUNET_YES, GNUNET_YES))
227     filesize = 0;
228   if (0 == filesize)
229   {
230     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Policy file %s is empty.\n", policy_filename);
231     return 0;
232   }
233   data = GNUNET_malloc (filesize);
234   if (filesize != GNUNET_DISK_fn_read (policy_filename, data, filesize))
235   {
236     GNUNET_free (data);
237     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not read policy file %s.\n",
238                 policy_filename);
239     return 0;
240   }
241   buf = data;
242   offset = 0;
243   rx_cnt = 0;
244   while (offset < (filesize - 1))
245   {
246     offset++;
247     if ((data[offset] == '\n') && (buf != &data[offset]))
248     {
249       data[offset] = '|';
250       buf = &data[offset + 1];
251       rx_cnt++;
252     }
253     else if ((data[offset] == '\n') || (data[offset] == '\0'))
254       buf = &data[offset + 1];
255   }
256   data[offset] = '\0';
257   *rx = data;
258
259   return rx_cnt;
260 }
261
262
263 /**
264  * @brief Main function that will be run by the scheduler.
265  *
266  * @param cls closure
267  * @param args remaining command-line arguments
268  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
269  * @param cfg_ configuration
270  */
271 static void
272 run (void *cls, char *const *args GNUNET_UNUSED,
273      const char *cfgfile GNUNET_UNUSED,
274      const struct GNUNET_CONFIGURATION_Handle *cfg_)
275 {
276   char *regex = NULL;
277
278   cfg = cfg_;
279
280   if (GNUNET_OK !=
281       GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER", "MAX_PATH_COMPRESSION",
282                                              &max_path_compression))
283   {
284     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
285                 _
286                 ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
287                 "regexprofiler", "max_path_compression");
288     global_ret = GNUNET_SYSERR;
289     GNUNET_SCHEDULER_shutdown ();
290     return;
291   }
292
293   if (GNUNET_OK !=
294       GNUNET_CONFIGURATION_get_value_filename (cfg, "REGEXPROFILER",
295                                                "POLICY_FILE", &policy_filename))
296   {
297     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
298                 _
299                 ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
300                 "regexprofiler", "policy_file");
301     global_ret = GNUNET_SYSERR;
302     GNUNET_SCHEDULER_shutdown ();
303     return;
304   }
305
306   if (GNUNET_OK !=
307       GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
308                                              "REGEX_PREFIX", &regex_prefix))
309   {
310     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
311                 _
312                 ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
313                 "regexprofiler", "regex_prefix");
314     global_ret = GNUNET_SYSERR;
315     GNUNET_SCHEDULER_shutdown ();
316     return;
317   }
318
319   if (GNUNET_OK !=
320       GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
321                                            "REANNOUNCE_FREQ", &reannounce_freq))
322   {
323     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
324                 "reannounce_freq not given. Using 10 minutes.\n");
325     reannounce_freq =
326       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
327
328   }
329     announce_delay =
330     GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
331                                    GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 600));
332
333   stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
334
335   dht_handle = GNUNET_DHT_connect (cfg, 1);
336
337   if (NULL == dht_handle)
338   {
339     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
340                 "Could not acquire dht handle. Exiting.\n");
341     global_ret = GNUNET_SYSERR;
342     GNUNET_SCHEDULER_shutdown ();
343     return;
344   }
345
346   /* Read regexes from policy files */
347   if (0 == load_regexes (policy_filename, &regex))
348   {
349     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
350                 "Policy file %s contains no policies. Exiting.\n",
351                 policy_filename);
352     global_ret = GNUNET_SYSERR;
353     GNUNET_SCHEDULER_shutdown ();
354     return;
355   }
356
357   /* Announcing regexes from policy_filename */
358   GNUNET_asprintf (&rx_with_pfx, "%s(%s)", regex_prefix, regex);
359   announce_regex (rx_with_pfx);
360   GNUNET_free (regex);
361   GNUNET_free (rx_with_pfx);
362
363   /* Scheduled the task to clean up when shutdown is called */
364   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
365                                 NULL);
366 }
367
368
369 /**
370  * The main function of the regexprofiler service.
371  *
372  * @param argc number of arguments from the command line
373  * @param argv command line arguments
374  * @return 0 ok, 1 on error
375  */
376 int
377 main (int argc, char *const *argv)
378 {
379   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
380     GNUNET_GETOPT_OPTION_END
381   };
382
383   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
384     return 2;
385   return (GNUNET_OK ==
386           GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
387                               gettext_noop
388                               ("Daemon to announce regular expressions for the peer using mesh."),
389                               options, &run, NULL)) ? global_ret : 1;
390 }
391
392
393 #ifdef LINUX
394 #include <malloc.h>
395
396 /**
397  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
398  */
399 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
400 {
401   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
402   mallopt (M_TOP_PAD, 1 * 1024);
403   malloc_trim (0);
404 }
405 #endif
406
407
408 /* end of gnunet-daemon-regexprofiler.c */