aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/gnunet-helper-testbed.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testbed/gnunet-helper-testbed.c')
-rw-r--r--src/testbed/gnunet-helper-testbed.c202
1 files changed, 99 insertions, 103 deletions
diff --git a/src/testbed/gnunet-helper-testbed.c b/src/testbed/gnunet-helper-testbed.c
index aaa65ed61..72d7fe16f 100644
--- a/src/testbed/gnunet-helper-testbed.c
+++ b/src/testbed/gnunet-helper-testbed.c
@@ -49,14 +49,12 @@
49/** 49/**
50 * Generic logging shortcut 50 * Generic logging shortcut
51 */ 51 */
52#define LOG(kind, ...) \ 52#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
53 GNUNET_log (kind, __VA_ARGS__)
54 53
55/** 54/**
56 * Debug logging shorthand 55 * Debug logging shorthand
57 */ 56 */
58#define LOG_DEBUG(...) \ 57#define LOG_DEBUG(...) LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
59 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
60 58
61 59
62/** 60/**
@@ -213,13 +211,12 @@ write_task (void *cls)
213 211
214 GNUNET_assert (NULL != wc); 212 GNUNET_assert (NULL != wc);
215 write_task_id = NULL; 213 write_task_id = NULL;
216 bytes_wrote = 214 bytes_wrote = GNUNET_DISK_file_write (stdout_fd,
217 GNUNET_DISK_file_write (stdout_fd, wc->data + wc->pos, 215 wc->data + wc->pos,
218 wc->length - wc->pos); 216 wc->length - wc->pos);
219 if (GNUNET_SYSERR == bytes_wrote) 217 if (GNUNET_SYSERR == bytes_wrote)
220 { 218 {
221 LOG (GNUNET_ERROR_TYPE_WARNING, 219 LOG (GNUNET_ERROR_TYPE_WARNING, "Cannot reply back configuration\n");
222 "Cannot reply back configuration\n");
223 GNUNET_free (wc->data); 220 GNUNET_free (wc->data);
224 GNUNET_free (wc); 221 GNUNET_free (wc);
225 return; 222 return;
@@ -231,10 +228,10 @@ write_task (void *cls)
231 GNUNET_free (wc); 228 GNUNET_free (wc);
232 return; 229 return;
233 } 230 }
234 write_task_id = 231 write_task_id = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
235 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, 232 stdout_fd,
236 stdout_fd, 233 &write_task,
237 &write_task, wc); 234 wc);
238} 235}
239 236
240 237
@@ -273,14 +270,16 @@ child_death_task (void *cls)
273 if (0 != PLIBC_KILL (0, GNUNET_TERM_SIG)) 270 if (0 != PLIBC_KILL (0, GNUNET_TERM_SIG))
274 { 271 {
275 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "signal"); 272 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "signal");
276 GNUNET_SCHEDULER_shutdown (); /* Couldn't send the signal, we shutdown frowning */ 273 GNUNET_SCHEDULER_shutdown (); /* Couldn't send the signal, we shutdown frowning */
277 } 274 }
278 return; 275 return;
279 } 276 }
280 LOG_DEBUG ("Child hasn't died. Resuming to monitor its status\n"); 277 LOG_DEBUG ("Child hasn't died. Resuming to monitor its status\n");
281 child_death_task_id = 278 child_death_task_id =
282 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 279 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
283 pr, &child_death_task, NULL); 280 pr,
281 &child_death_task,
282 NULL);
284} 283}
285 284
286 285
@@ -297,8 +296,7 @@ child_death_task (void *cls)
297 * #GNUNET_SYSERR to stop further processing with error 296 * #GNUNET_SYSERR to stop further processing with error
298 */ 297 */
299static int 298static int
300tokenizer_cb (void *cls, 299tokenizer_cb (void *cls, const struct GNUNET_MessageHeader *message)
301 const struct GNUNET_MessageHeader *message)
302{ 300{
303 const struct GNUNET_TESTBED_HelperInit *msg; 301 const struct GNUNET_TESTBED_HelperInit *msg;
304 struct GNUNET_TESTBED_HelperReply *reply; 302 struct GNUNET_TESTBED_HelperReply *reply;
@@ -322,8 +320,7 @@ tokenizer_cb (void *cls,
322 if ((sizeof (struct GNUNET_TESTBED_HelperInit) >= msize) || 320 if ((sizeof (struct GNUNET_TESTBED_HelperInit) >= msize) ||
323 (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT != ntohs (message->type))) 321 (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT != ntohs (message->type)))
324 { 322 {
325 LOG (GNUNET_ERROR_TYPE_WARNING, 323 LOG (GNUNET_ERROR_TYPE_WARNING, "Received unexpected message -- exiting\n");
326 "Received unexpected message -- exiting\n");
327 goto error; 324 goto error;
328 } 325 }
329 msg = (const struct GNUNET_TESTBED_HelperInit *) message; 326 msg = (const struct GNUNET_TESTBED_HelperInit *) message;
@@ -344,28 +341,39 @@ tokenizer_cb (void *cls,
344 } 341 }
345 ul_config_size = (uLongf) ntohs (msg->config_size); 342 ul_config_size = (uLongf) ntohs (msg->config_size);
346 config = GNUNET_malloc (ul_config_size); 343 config = GNUNET_malloc (ul_config_size);
347 xconfig_size = 344 xconfig_size = msize - (trusted_ip_size + 1 + hostname_size +
348 msize - (trusted_ip_size + 1 + hostname_size + 345 sizeof (struct GNUNET_TESTBED_HelperInit));
349 sizeof (struct GNUNET_TESTBED_HelperInit)); 346 int ret = uncompress ((Bytef *) config,
350 if (Z_OK != 347 &ul_config_size,
351 uncompress ((Bytef *) config, &ul_config_size, 348 (const Bytef *) (trusted_ip + trusted_ip_size + 1 +
352 (const Bytef *) (trusted_ip + trusted_ip_size + 1 + 349 hostname_size),
353 hostname_size), (uLongf) xconfig_size)) 350 (uLongf) xconfig_size);
351 if (Z_OK != ret)
354 { 352 {
355 LOG (GNUNET_ERROR_TYPE_WARNING, 353 switch (ret)
354 {
355 case Z_MEM_ERROR:
356 LOG (GNUNET_ERROR_TYPE_ERROR, "Not enough memory for decompression\n");
357 break;
358 case Z_BUF_ERROR:
359 LOG (GNUNET_ERROR_TYPE_ERROR, "Output buffer too small\n");
360 break;
361 case Z_DATA_ERROR:
362 LOG (GNUNET_ERROR_TYPE_ERROR, "Data corrupted/incomplete\n");
363 break;
364 default:
365 GNUNET_break (0);
366 }
367 LOG (GNUNET_ERROR_TYPE_ERROR,
356 "Error while uncompressing config -- exiting\n"); 368 "Error while uncompressing config -- exiting\n");
357 GNUNET_free (config); 369 GNUNET_free (config);
358 goto error; 370 goto error;
359 } 371 }
360 cfg = GNUNET_CONFIGURATION_create (); 372 cfg = GNUNET_CONFIGURATION_create ();
361 if (GNUNET_OK != 373 if (GNUNET_OK !=
362 GNUNET_CONFIGURATION_deserialize (cfg, 374 GNUNET_CONFIGURATION_deserialize (cfg, config, ul_config_size, NULL))
363 config,
364 ul_config_size,
365 NULL))
366 { 375 {
367 LOG (GNUNET_ERROR_TYPE_WARNING, 376 LOG (GNUNET_ERROR_TYPE_ERROR, "Unable to deserialize config -- exiting\n");
368 "Unable to deserialize config -- exiting\n");
369 GNUNET_free (config); 377 GNUNET_free (config);
370 goto error; 378 goto error;
371 } 379 }
@@ -391,19 +399,15 @@ tokenizer_cb (void *cls,
391#endif 399#endif
392 } 400 }
393 test_system = 401 test_system =
394 GNUNET_TESTING_system_create ("testbed-helper", trusted_ip, hostname, 402 GNUNET_TESTING_system_create ("testbed-helper", trusted_ip, hostname, NULL);
395 NULL);
396 if (NULL != evstr) 403 if (NULL != evstr)
397 { 404 {
398#ifdef WINDOWS 405#ifdef WINDOWS
399 GNUNET_assert (0 != SetEnvironmentVariable (GNUNET_TESTING_PREFIX, 406 GNUNET_assert (0 != SetEnvironmentVariable (GNUNET_TESTING_PREFIX, evstr));
400 evstr));
401#else 407#else
402 char *evar; 408 char *evar;
403 409
404 GNUNET_asprintf (&evar, 410 GNUNET_asprintf (&evar, GNUNET_TESTING_PREFIX "=%s", evstr);
405 GNUNET_TESTING_PREFIX "=%s",
406 evstr);
407 GNUNET_assert (0 == putenv (evar)); /* consumes 'evar', 411 GNUNET_assert (0 == putenv (evar)); /* consumes 'evar',
408 see putenv(): becomes part of envrionment! */ 412 see putenv(): becomes part of envrionment! */
409#endif 413#endif
@@ -416,13 +420,15 @@ tokenizer_cb (void *cls,
416 GNUNET_assert (GNUNET_OK == 420 GNUNET_assert (GNUNET_OK ==
417 GNUNET_TESTING_configuration_create (test_system, cfg)); 421 GNUNET_TESTING_configuration_create (test_system, cfg));
418 GNUNET_assert (GNUNET_OK == 422 GNUNET_assert (GNUNET_OK ==
419 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", 423 GNUNET_CONFIGURATION_get_value_filename (cfg,
420 "DEFAULTCONFIG", 424 "PATHS",
421 &config)); 425 "DEFAULTCONFIG",
426 &config));
422 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config)) 427 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config))
423 { 428 {
424 LOG (GNUNET_ERROR_TYPE_WARNING, 429 LOG (GNUNET_ERROR_TYPE_WARNING,
425 "Unable to write config file: %s -- exiting\n", config); 430 "Unable to write config file: %s -- exiting\n",
431 config);
426 GNUNET_CONFIGURATION_destroy (cfg); 432 GNUNET_CONFIGURATION_destroy (cfg);
427 GNUNET_free (config); 433 GNUNET_free (config);
428 goto error; 434 goto error;
@@ -433,22 +439,21 @@ tokenizer_cb (void *cls,
433 char *evar; 439 char *evar;
434 440
435 /* expose testbed configuration through env variable */ 441 /* expose testbed configuration through env variable */
436 GNUNET_asprintf (&evar, 442 GNUNET_asprintf (&evar, "%s=%s", ENV_TESTBED_CONFIG, config);
437 "%s=%s", 443 GNUNET_assert (0 == putenv (evar)); /* consumes 'evar',
438 ENV_TESTBED_CONFIG,
439 config);
440 GNUNET_assert (0 == putenv (evar)); /* consumes 'evar',
441 see putenv(): becomes part of envrionment! */ 444 see putenv(): becomes part of envrionment! */
442 evstr = NULL; 445 evstr = NULL;
443 } 446 }
444 testbed = 447 testbed = GNUNET_OS_start_process (PIPE_CONTROL,
445 GNUNET_OS_start_process (PIPE_CONTROL, 448 GNUNET_OS_INHERIT_STD_ERR /*verbose? */,
446 GNUNET_OS_INHERIT_STD_ERR /*verbose? */ , 449 NULL,
447 NULL, NULL, NULL, 450 NULL,
448 binary, 451 NULL,
449 "gnunet-service-testbed", 452 binary,
450 "-c", config, 453 "gnunet-service-testbed",
451 NULL); 454 "-c",
455 config,
456 NULL);
452 GNUNET_free (binary); 457 GNUNET_free (binary);
453 GNUNET_free (config); 458 GNUNET_free (config);
454 if (NULL == testbed) 459 if (NULL == testbed)
@@ -463,7 +468,7 @@ tokenizer_cb (void *cls,
463 GNUNET_CONFIGURATION_destroy (cfg); 468 GNUNET_CONFIGURATION_destroy (cfg);
464 cfg = NULL; 469 cfg = NULL;
465 xconfig_size = 470 xconfig_size =
466 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig); 471 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
467 GNUNET_free (config); 472 GNUNET_free (config);
468 wc = GNUNET_new (struct WriteContext); 473 wc = GNUNET_new (struct WriteContext);
469 wc->length = xconfig_size + sizeof (struct GNUNET_TESTBED_HelperReply); 474 wc->length = xconfig_size + sizeof (struct GNUNET_TESTBED_HelperReply);
@@ -473,15 +478,15 @@ tokenizer_cb (void *cls,
473 reply->header.size = htons ((uint16_t) wc->length); 478 reply->header.size = htons ((uint16_t) wc->length);
474 reply->config_size = htons ((uint16_t) config_size); 479 reply->config_size = htons ((uint16_t) config_size);
475 wc->data = reply; 480 wc->data = reply;
476 write_task_id = 481 write_task_id = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
477 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, 482 stdout_fd,
478 stdout_fd, 483 &write_task,
479 &write_task, wc); 484 wc);
480 child_death_task_id = 485 child_death_task_id = GNUNET_SCHEDULER_add_read_file (
481 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 486 GNUNET_TIME_UNIT_FOREVER_REL,
482 GNUNET_DISK_pipe_handle (sigpipe, 487 GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ),
483 GNUNET_DISK_PIPE_END_READ), 488 &child_death_task,
484 &child_death_task, NULL); 489 NULL);
485 return GNUNET_OK; 490 return GNUNET_OK;
486 491
487error: 492error:
@@ -503,11 +508,8 @@ read_task (void *cls)
503 ssize_t sread; 508 ssize_t sread;
504 509
505 read_task_id = NULL; 510 read_task_id = NULL;
506 sread = GNUNET_DISK_file_read (stdin_fd, 511 sread = GNUNET_DISK_file_read (stdin_fd, buf, sizeof (buf));
507 buf, 512 if ((GNUNET_SYSERR == sread) || (0 == sread))
508 sizeof (buf));
509 if ( (GNUNET_SYSERR == sread) ||
510 (0 == sread) )
511 { 513 {
512 LOG_DEBUG ("STDIN closed\n"); 514 LOG_DEBUG ("STDIN closed\n");
513 GNUNET_SCHEDULER_shutdown (); 515 GNUNET_SCHEDULER_shutdown ();
@@ -520,24 +522,19 @@ read_task (void *cls)
520 GNUNET_SCHEDULER_shutdown (); 522 GNUNET_SCHEDULER_shutdown ();
521 return; 523 return;
522 } 524 }
523 LOG_DEBUG ("Read %u bytes\n", 525 LOG_DEBUG ("Read %u bytes\n", (unsigned int) sread);
524 (unsigned int) sread);
525 /* FIXME: could introduce a GNUNET_MST_read2 to read 526 /* FIXME: could introduce a GNUNET_MST_read2 to read
526 directly from 'stdin_fd' and save a memcpy() here */ 527 directly from 'stdin_fd' and save a memcpy() here */
527 if (GNUNET_OK != 528 if (GNUNET_OK !=
528 GNUNET_MST_from_buffer (tokenizer, 529 GNUNET_MST_from_buffer (tokenizer, buf, sread, GNUNET_NO, GNUNET_NO))
529 buf,
530 sread,
531 GNUNET_NO,
532 GNUNET_NO))
533 { 530 {
534 GNUNET_break (0); 531 GNUNET_break (0);
535 GNUNET_SCHEDULER_shutdown (); 532 GNUNET_SCHEDULER_shutdown ();
536 return; 533 return;
537 } 534 }
538 read_task_id /* No timeout while reading */ 535 read_task_id /* No timeout while reading */
539 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 536 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
540 stdin_fd, 537 stdin_fd,
541 &read_task, 538 &read_task,
542 NULL); 539 NULL);
543} 540}
@@ -561,12 +558,11 @@ run (void *cls,
561 tokenizer = GNUNET_MST_create (&tokenizer_cb, NULL); 558 tokenizer = GNUNET_MST_create (&tokenizer_cb, NULL);
562 stdin_fd = GNUNET_DISK_get_handle_from_native (stdin); 559 stdin_fd = GNUNET_DISK_get_handle_from_native (stdin);
563 stdout_fd = GNUNET_DISK_get_handle_from_native (stdout); 560 stdout_fd = GNUNET_DISK_get_handle_from_native (stdout);
564 read_task_id = 561 read_task_id = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
565 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 562 stdin_fd,
566 stdin_fd, 563 &read_task,
567 &read_task, NULL); 564 NULL);
568 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, 565 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
569 NULL);
570} 566}
571 567
572 568
@@ -577,13 +573,15 @@ static void
577sighandler_child_death () 573sighandler_child_death ()
578{ 574{
579 static char c; 575 static char c;
580 int old_errno; /* back-up errno */ 576 int old_errno; /* back-up errno */
581 577
582 old_errno = errno; 578 old_errno = errno;
583 GNUNET_break (1 == 579 GNUNET_break (
584 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle 580 1 ==
585 (sigpipe, GNUNET_DISK_PIPE_END_WRITE), 581 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
586 &c, sizeof (c))); 582 GNUNET_DISK_PIPE_END_WRITE),
583 &c,
584 sizeof (c)));
587 errno = old_errno; 585 errno = old_errno;
588} 586}
589 587
@@ -596,25 +594,23 @@ sighandler_child_death ()
596 * @return return code 594 * @return return code
597 */ 595 */
598int 596int
599main (int argc, 597main (int argc, char **argv)
600 char **argv)
601{ 598{
602 struct GNUNET_SIGNAL_Context *shc_chld; 599 struct GNUNET_SIGNAL_Context *shc_chld;
603 struct GNUNET_GETOPT_CommandLineOption options[] = { 600 struct GNUNET_GETOPT_CommandLineOption options[] = {GNUNET_GETOPT_OPTION_END};
604 GNUNET_GETOPT_OPTION_END
605 };
606 int ret; 601 int ret;
607 602
608 status = GNUNET_OK; 603 status = GNUNET_OK;
609 if (NULL == (sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, 604 if (NULL ==
610 GNUNET_NO, GNUNET_NO))) 605 (sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO)))
611 { 606 {
612 GNUNET_break (0); 607 GNUNET_break (0);
613 return 1; 608 return 1;
614 } 609 }
615 shc_chld = GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, 610 shc_chld =
616 &sighandler_child_death); 611 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
617 ret = GNUNET_PROGRAM_run (argc, argv, 612 ret = GNUNET_PROGRAM_run (argc,
613 argv,
618 "gnunet-helper-testbed", 614 "gnunet-helper-testbed",
619 "Helper for starting gnunet-service-testbed", 615 "Helper for starting gnunet-service-testbed",
620 options, 616 options,