2 This file is part of GNUnet.
3 (C) 2010, 2011, 2012 Christian Grothoff
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.
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.
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.
22 * @file mesh/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
30 #include "gnunet_mesh_service.h"
31 #include "gnunet_statistics_service.h"
34 * Return value from 'main'.
36 static int global_ret;
39 * Configuration we use.
41 static const struct GNUNET_CONFIGURATION_Handle *cfg;
44 * Handle to the statistics service.
46 static struct GNUNET_STATISTICS_Handle *stats_handle;
51 struct GNUNET_MESH_Handle *mesh_handle;
54 * Peer's mesh tunnel handle.
56 struct GNUNET_MESH_Tunnel *mesh_tunnel_handle;
59 * Maximal path compression length for regex announcing.
61 static unsigned long long max_path_compression;
64 * Name of the file containing policies that this peer should announce. One
67 static char * policy_filename;
70 * Prefix to add before every regex we're announcing.
72 static char * regex_prefix;
75 * Task run during shutdown.
81 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
85 if (NULL != mesh_tunnel_handle)
87 GNUNET_MESH_tunnel_destroy (mesh_tunnel_handle);
88 mesh_tunnel_handle = NULL;
91 if (NULL != mesh_handle)
93 GNUNET_MESH_disconnect (mesh_handle);
97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
102 * Announce the given regular expression using Mesh and the path compression
103 * length read from config.
105 * @param regex regular expression to announce on this peer's mesh.
108 announce_regex (const char * regex)
110 if (NULL == regex || 0 == strlen (regex))
112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
117 GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
118 GNUNET_MESH_announce_regex (mesh_handle, regex, (unsigned int)max_path_compression);
123 * Load regular expressions from filename into 'rxes' array. Array needs to be freed.
125 * @param filename filename of the file containing the regexes, one per line.
126 * @param rx string with the union of all regular expressions.
127 * @return number of regular expressions read from filename and in rxes array.
130 load_regexes (const char *filename, char **rx)
138 if (GNUNET_YES != GNUNET_DISK_file_test (policy_filename))
140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
141 "Could not find policy file %s\n", policy_filename);
144 if (GNUNET_OK != GNUNET_DISK_file_size (policy_filename, &filesize, GNUNET_YES, GNUNET_YES))
148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Policy file %s is empty.\n", policy_filename);
151 data = GNUNET_malloc (filesize);
152 if (filesize != GNUNET_DISK_fn_read (policy_filename, data, filesize))
155 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not read policy file %s.\n",
162 while (offset < (filesize - 1))
165 if ((data[offset] == '\n') && (buf != &data[offset]))
168 buf = &data[offset + 1];
171 else if ((data[offset] == '\n') || (data[offset] == '\0'))
172 buf = &data[offset + 1];
182 * @brief Main function that will be run by the scheduler.
185 * @param args remaining command-line arguments
186 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
187 * @param cfg_ configuration
190 run (void *cls, char *const *args GNUNET_UNUSED,
191 const char *cfgfile GNUNET_UNUSED,
192 const struct GNUNET_CONFIGURATION_Handle *cfg_)
196 const GNUNET_MESH_ApplicationType app = (GNUNET_MESH_ApplicationType)0;
197 static struct GNUNET_MESH_MessageHandler handlers[] = {
204 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER", "MAX_PATH_COMPRESSION",
205 &max_path_compression))
207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
209 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
210 "regexprofiler", "max_path_compression");
211 global_ret = GNUNET_SYSERR;
212 GNUNET_SCHEDULER_shutdown ();
217 GNUNET_CONFIGURATION_get_value_filename (cfg, "REGEXPROFILER", "POLICY_FILE",
220 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
222 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
223 "regexprofiler", "policy_file");
224 global_ret = GNUNET_SYSERR;
225 GNUNET_SCHEDULER_shutdown ();
230 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER", "REGEX_PREFIX",
233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
236 "regexprofiler", "regex_prefix");
237 global_ret = GNUNET_SYSERR;
238 GNUNET_SCHEDULER_shutdown ();
242 stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
245 GNUNET_MESH_connect (cfg, NULL, NULL, NULL, handlers, &app);
247 if (NULL == mesh_handle)
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not acquire mesh handle. Exiting.\n");
250 global_ret = GNUNET_SYSERR;
251 GNUNET_SCHEDULER_shutdown ();
255 /* Read regexes from policy files */
256 if (0 == load_regexes (policy_filename, ®ex))
258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
259 "Policy file %s contains no policies. Exiting.\n",
261 global_ret = GNUNET_SYSERR;
262 GNUNET_SCHEDULER_shutdown ();
266 /* Announcing regexes from policy_filename */
267 GNUNET_asprintf (&rx_with_pfx, "%s(%s)", regex_prefix, regex);
268 announce_regex (rx_with_pfx);
269 GNUNET_free (rx_with_pfx);
272 /* Scheduled the task to clean up when shutdown is called */
273 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
279 * The main function of the regexprofiler service.
281 * @param argc number of arguments from the command line
282 * @param argv command line arguments
283 * @return 0 ok, 1 on error
286 main (int argc, char *const *argv)
288 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
289 GNUNET_GETOPT_OPTION_END
292 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
296 GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
298 ("Daemon to announce regular expressions for the peer using mesh."),
299 options, &run, NULL)) ? global_ret : 1;
307 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
309 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
311 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
312 mallopt (M_TOP_PAD, 1 * 1024);
318 /* end of gnunet-daemon-regexprofiler.c */