aboutsummaryrefslogtreecommitdiff
path: root/src/regex/gnunet-daemon-regexprofiler.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regex/gnunet-daemon-regexprofiler.c')
-rw-r--r--src/regex/gnunet-daemon-regexprofiler.c339
1 files changed, 172 insertions, 167 deletions
diff --git a/src/regex/gnunet-daemon-regexprofiler.c b/src/regex/gnunet-daemon-regexprofiler.c
index b9c32f9c7..a23336808 100644
--- a/src/regex/gnunet-daemon-regexprofiler.c
+++ b/src/regex/gnunet-daemon-regexprofiler.c
@@ -62,7 +62,7 @@ static struct REGEX_INTERNAL_Announcement *announce_handle;
62/** 62/**
63 * Periodically reannounce regex. 63 * Periodically reannounce regex.
64 */ 64 */
65static struct GNUNET_SCHEDULER_Task * reannounce_task; 65static struct GNUNET_SCHEDULER_Task *reannounce_task;
66 66
67/** 67/**
68 * What's the maximum reannounce period. 68 * What's the maximum reannounce period.
@@ -78,12 +78,12 @@ static unsigned long long max_path_compression;
78 * Name of the file containing policies that this peer should announce. One 78 * Name of the file containing policies that this peer should announce. One
79 * policy per line. 79 * policy per line.
80 */ 80 */
81static char * policy_filename; 81static char *policy_filename;
82 82
83/** 83/**
84 * Prefix to add before every regex we're announcing. 84 * Prefix to add before every regex we're announcing.
85 */ 85 */
86static char * regex_prefix; 86static char *regex_prefix;
87 87
88/** 88/**
89 * Regex with prefix. 89 * Regex with prefix.
@@ -108,31 +108,31 @@ static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
108 * @param cls unused 108 * @param cls unused
109 */ 109 */
110static void 110static void
111shutdown_task(void *cls) 111shutdown_task (void *cls)
112{ 112{
113 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "shutting down\n"); 113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
114 114
115 if (NULL != announce_handle) 115 if (NULL != announce_handle)
116 { 116 {
117 REGEX_INTERNAL_announce_cancel(announce_handle); 117 REGEX_INTERNAL_announce_cancel (announce_handle);
118 announce_handle = NULL; 118 announce_handle = NULL;
119 } 119 }
120 if (NULL != reannounce_task) 120 if (NULL != reannounce_task)
121 { 121 {
122 GNUNET_free(GNUNET_SCHEDULER_cancel(reannounce_task)); 122 GNUNET_free (GNUNET_SCHEDULER_cancel (reannounce_task));
123 reannounce_task = NULL; 123 reannounce_task = NULL;
124 } 124 }
125 if (NULL != dht_handle) 125 if (NULL != dht_handle)
126 { 126 {
127 GNUNET_DHT_disconnect(dht_handle); 127 GNUNET_DHT_disconnect (dht_handle);
128 dht_handle = NULL; 128 dht_handle = NULL;
129 } 129 }
130 GNUNET_free(my_private_key); 130 GNUNET_free (my_private_key);
131 my_private_key = NULL; 131 my_private_key = NULL;
132 132
133 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 133 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
134 "Daemon for %s shutting down\n", 134 "Daemon for %s shutting down\n",
135 policy_filename); 135 policy_filename);
136} 136}
137 137
138 138
@@ -142,45 +142,46 @@ shutdown_task(void *cls)
142 * @param cls Closure (regex to announce if needed). 142 * @param cls Closure (regex to announce if needed).
143 */ 143 */
144static void 144static void
145reannounce_regex(void *cls) 145reannounce_regex (void *cls)
146{ 146{
147 char *regex = cls; 147 char *regex = cls;
148 struct GNUNET_TIME_Relative random_delay; 148 struct GNUNET_TIME_Relative random_delay;
149 149
150 reannounce_task = NULL; 150 reannounce_task = NULL;
151 if (0 == rounds--) 151 if (0 == rounds--)
152 { 152 {
153 global_ret = 0; 153 global_ret = 0;
154 GNUNET_SCHEDULER_shutdown(); 154 GNUNET_SCHEDULER_shutdown ();
155 GNUNET_free(regex); 155 GNUNET_free (regex);
156 return; 156 return;
157 } 157 }
158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex); 158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
159 GNUNET_STATISTICS_update(stats_handle, "# regexes announced", 1, GNUNET_NO); 159 GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
160 if (NULL == announce_handle && NULL != regex) 160 if ((NULL == announce_handle)&&(NULL != regex))
161 { 161 {
162 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163 "First time, creating regex: %s\n", 163 "First time, creating regex: %s\n",
164 regex); 164 regex);
165 announce_handle = REGEX_INTERNAL_announce(dht_handle, 165 announce_handle = REGEX_INTERNAL_announce (dht_handle,
166 my_private_key, 166 my_private_key,
167 regex, 167 regex,
168 (unsigned int)max_path_compression, 168 (unsigned
169 stats_handle); 169 int) max_path_compression,
170 } 170 stats_handle);
171 }
171 else 172 else
172 { 173 {
173 GNUNET_assert(NULL != announce_handle); 174 GNUNET_assert (NULL != announce_handle);
174 REGEX_INTERNAL_reannounce(announce_handle); 175 REGEX_INTERNAL_reannounce (announce_handle);
175 } 176 }
176 177
177 random_delay = 178 random_delay =
178 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MICROSECONDS, 179 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,
179 GNUNET_CRYPTO_random_u32( 180 GNUNET_CRYPTO_random_u32 (
180 GNUNET_CRYPTO_QUALITY_WEAK, 181 GNUNET_CRYPTO_QUALITY_WEAK,
181 reannounce_period_max.rel_value_us)); 182 reannounce_period_max.rel_value_us));
182 reannounce_task = GNUNET_SCHEDULER_add_delayed(random_delay, 183 reannounce_task = GNUNET_SCHEDULER_add_delayed (random_delay,
183 &reannounce_regex, cls); 184 &reannounce_regex, cls);
184} 185}
185 186
186 187
@@ -191,23 +192,23 @@ reannounce_regex(void *cls)
191 * @param regex regular expression to announce on this peer's cadet. 192 * @param regex regular expression to announce on this peer's cadet.
192 */ 193 */
193static void 194static void
194announce_regex(const char *regex) 195announce_regex (const char *regex)
195{ 196{
196 char *copy; 197 char *copy;
197 198
198 if (NULL == regex || 0 == strlen(regex)) 199 if ((NULL == regex)||(0 == strlen (regex)))
199 { 200 {
200 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n"); 201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
201 return; 202 return;
202 } 203 }
203 204
204 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 205 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
205 "Daemon for %s starting\n", 206 "Daemon for %s starting\n",
206 policy_filename); 207 policy_filename);
207 GNUNET_assert(NULL == reannounce_task); 208 GNUNET_assert (NULL == reannounce_task);
208 copy = GNUNET_strdup(regex); 209 copy = GNUNET_strdup (regex);
209 reannounce_task = GNUNET_SCHEDULER_add_now(&reannounce_regex, 210 reannounce_task = GNUNET_SCHEDULER_add_now (&reannounce_regex,
210 (void *)copy); 211 (void *) copy);
211} 212}
212 213
213 214
@@ -220,16 +221,16 @@ announce_regex(const char *regex)
220 * GNUNET_NO to stop when found 221 * GNUNET_NO to stop when found
221 */ 222 */
222static int 223static int
223scan(void *cls, const char *filename) 224scan (void *cls, const char *filename)
224{ 225{
225 long n = (long)cls; 226 long n = (long) cls;
226 static long c = 0; 227 static long c = 0;
227 228
228 if (c == n) 229 if (c == n)
229 { 230 {
230 policy_filename = GNUNET_strdup(filename); 231 policy_filename = GNUNET_strdup (filename);
231 return GNUNET_NO; 232 return GNUNET_NO;
232 } 233 }
233 c++; 234 c++;
234 return GNUNET_OK; 235 return GNUNET_OK;
235} 236}
@@ -244,9 +245,9 @@ scan(void *cls, const char *filename)
244 * @param cfg_ configuration 245 * @param cfg_ configuration
245 */ 246 */
246static void 247static void
247run(void *cls, char *const *args GNUNET_UNUSED, 248run (void *cls, char *const *args GNUNET_UNUSED,
248 const char *cfgfile GNUNET_UNUSED, 249 const char *cfgfile GNUNET_UNUSED,
249 const struct GNUNET_CONFIGURATION_Handle *cfg_) 250 const struct GNUNET_CONFIGURATION_Handle *cfg_)
250{ 251{
251 char *regex = NULL; 252 char *regex = NULL;
252 char **components; 253 char **components;
@@ -255,106 +256,109 @@ run(void *cls, char *const *args GNUNET_UNUSED,
255 256
256 cfg = cfg_; 257 cfg = cfg_;
257 258
258 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration(cfg); 259 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
259 GNUNET_assert(NULL != my_private_key); 260 GNUNET_assert (NULL != my_private_key);
260 if (GNUNET_OK != 261 if (GNUNET_OK !=
261 GNUNET_CONFIGURATION_get_value_number(cfg, "REGEXPROFILER", 262 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
262 "MAX_PATH_COMPRESSION", 263 "MAX_PATH_COMPRESSION",
263 &max_path_compression)) 264 &max_path_compression))
264 { 265 {
265 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
266 _ 267 _
267 ("%s service is lacking key configuration settings (%s). Exiting.\n"), 268 (
268 "regexprofiler", "max_path_compression"); 269 "%s service is lacking key configuration settings (%s). Exiting.\n"),
269 global_ret = GNUNET_SYSERR; 270 "regexprofiler", "max_path_compression");
270 GNUNET_SCHEDULER_shutdown(); 271 global_ret = GNUNET_SYSERR;
271 return; 272 GNUNET_SCHEDULER_shutdown ();
272 } 273 return;
274 }
273 if (GNUNET_OK != 275 if (GNUNET_OK !=
274 GNUNET_CONFIGURATION_get_value_string(cfg, "REGEXPROFILER", 276 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
275 "POLICY_DIR", &policy_dir)) 277 "POLICY_DIR", &policy_dir))
276 { 278 {
277 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "POLICY_DIR"); 279 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER",
278 global_ret = GNUNET_SYSERR; 280 "POLICY_DIR");
279 GNUNET_SCHEDULER_shutdown(); 281 global_ret = GNUNET_SYSERR;
280 return; 282 GNUNET_SCHEDULER_shutdown ();
281 } 283 return;
284 }
282 if (GNUNET_OK != 285 if (GNUNET_OK !=
283 GNUNET_CONFIGURATION_get_value_number(cfg, "TESTBED", 286 GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
284 "PEERID", &peer_id)) 287 "PEERID", &peer_id))
285 { 288 {
286 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "TESTBED", "PEERID"); 289 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "TESTBED", "PEERID");
287 global_ret = GNUNET_SYSERR; 290 global_ret = GNUNET_SYSERR;
288 GNUNET_free(policy_dir); 291 GNUNET_free (policy_dir);
289 GNUNET_SCHEDULER_shutdown(); 292 GNUNET_SCHEDULER_shutdown ();
290 return; 293 return;
291 } 294 }
292 295
293 if (GNUNET_OK != 296 if (GNUNET_OK !=
294 GNUNET_CONFIGURATION_get_value_string(cfg, "REGEXPROFILER", 297 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
295 "REGEX_PREFIX", &regex_prefix)) 298 "REGEX_PREFIX", &regex_prefix))
296 { 299 {
297 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "REGEX_PREFIX"); 300 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER",
298 global_ret = GNUNET_SYSERR; 301 "REGEX_PREFIX");
299 GNUNET_free(policy_dir); 302 global_ret = GNUNET_SYSERR;
300 GNUNET_SCHEDULER_shutdown(); 303 GNUNET_free (policy_dir);
301 return; 304 GNUNET_SCHEDULER_shutdown ();
302 } 305 return;
306 }
303 307
304 if (GNUNET_OK != 308 if (GNUNET_OK !=
305 GNUNET_CONFIGURATION_get_value_time(cfg, "REGEXPROFILER", 309 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
306 "REANNOUNCE_PERIOD_MAX", 310 "REANNOUNCE_PERIOD_MAX",
307 &reannounce_period_max)) 311 &reannounce_period_max))
308 { 312 {
309 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310 "reannounce_period_max not given. Using 10 minutes.\n"); 314 "reannounce_period_max not given. Using 10 minutes.\n");
311 reannounce_period_max = 315 reannounce_period_max =
312 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 10); 316 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
313 } 317 }
314 318
315 stats_handle = GNUNET_STATISTICS_create("regexprofiler", cfg); 319 stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
316 320
317 dht_handle = GNUNET_DHT_connect(cfg, 1); 321 dht_handle = GNUNET_DHT_connect (cfg, 1);
318 322
319 if (NULL == dht_handle) 323 if (NULL == dht_handle)
320 { 324 {
321 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
322 "Could not acquire dht handle. Exiting.\n"); 326 "Could not acquire dht handle. Exiting.\n");
323 global_ret = GNUNET_SYSERR; 327 global_ret = GNUNET_SYSERR;
324 GNUNET_free(policy_dir); 328 GNUNET_free (policy_dir);
325 GNUNET_SCHEDULER_shutdown(); 329 GNUNET_SCHEDULER_shutdown ();
326 return; 330 return;
327 } 331 }
328 332
329 /* Read regexes from policy files */ 333 /* Read regexes from policy files */
330 GNUNET_assert(-1 != GNUNET_DISK_directory_scan(policy_dir, &scan, 334 GNUNET_assert (-1 != GNUNET_DISK_directory_scan (policy_dir, &scan,
331 (void *)(long)peer_id)); 335 (void *) (long) peer_id));
332 if (NULL == (components = REGEX_TEST_read_from_file(policy_filename))) 336 if (NULL == (components = REGEX_TEST_read_from_file (policy_filename)))
333 { 337 {
334 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 338 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
335 "Policy file %s contains no policies. Exiting.\n", 339 "Policy file %s contains no policies. Exiting.\n",
336 policy_filename); 340 policy_filename);
337 global_ret = GNUNET_SYSERR; 341 global_ret = GNUNET_SYSERR;
338 GNUNET_free(policy_dir); 342 GNUNET_free (policy_dir);
339 GNUNET_SCHEDULER_shutdown(); 343 GNUNET_SCHEDULER_shutdown ();
340 return; 344 return;
341 } 345 }
342 GNUNET_free(policy_dir); 346 GNUNET_free (policy_dir);
343 regex = REGEX_TEST_combine(components, 16); 347 regex = REGEX_TEST_combine (components, 16);
344 REGEX_TEST_free_from_file(components); 348 REGEX_TEST_free_from_file (components);
345 349
346 /* Announcing regexes from policy_filename */ 350 /* Announcing regexes from policy_filename */
347 GNUNET_asprintf(&rx_with_pfx, 351 GNUNET_asprintf (&rx_with_pfx,
348 "%s(%s)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)*", 352 "%s(%s)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)*",
349 regex_prefix, 353 regex_prefix,
350 regex); 354 regex);
351 announce_regex(rx_with_pfx); 355 announce_regex (rx_with_pfx);
352 GNUNET_free(regex); 356 GNUNET_free (regex);
353 GNUNET_free(rx_with_pfx); 357 GNUNET_free (rx_with_pfx);
354 358
355 /* Scheduled the task to clean up when shutdown is called */ 359 /* Scheduled the task to clean up when shutdown is called */
356 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, 360 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
357 NULL); 361 NULL);
358} 362}
359 363
360 364
@@ -366,19 +370,20 @@ run(void *cls, char *const *args GNUNET_UNUSED,
366 * @return 0 ok, 1 on error 370 * @return 0 ok, 1 on error
367 */ 371 */
368int 372int
369main(int argc, char *const *argv) 373main (int argc, char *const *argv)
370{ 374{
371 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 375 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
372 GNUNET_GETOPT_OPTION_END 376 GNUNET_GETOPT_OPTION_END
373 }; 377 };
374 378
375 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) 379 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
376 return 2; 380 return 2;
377 return (GNUNET_OK == 381 return (GNUNET_OK ==
378 GNUNET_PROGRAM_run(argc, argv, "regexprofiler", 382 GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
379 gettext_noop 383 gettext_noop
380 ("Daemon to announce regular expressions for the peer using cadet."), 384 (
381 options, &run, NULL)) ? global_ret : 1; 385 "Daemon to announce regular expressions for the peer using cadet."),
386 options, &run, NULL)) ? global_ret : 1;
382} 387}
383 388
384 389
@@ -388,11 +393,11 @@ main(int argc, char *const *argv)
388/** 393/**
389 * MINIMIZE heap size (way below 128k) since this process doesn't need much. 394 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
390 */ 395 */
391void __attribute__ ((constructor)) GNUNET_ARM_memory_init() 396void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
392{ 397{
393 mallopt(M_TRIM_THRESHOLD, 4 * 1024); 398 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
394 mallopt(M_TOP_PAD, 1 * 1024); 399 mallopt (M_TOP_PAD, 1 * 1024);
395 malloc_trim(0); 400 malloc_trim (0);
396} 401}
397#endif 402#endif
398 403