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