aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing.c
diff options
context:
space:
mode:
authorng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
committerng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
commitd41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb (patch)
tree9efd18ea7d425652085ed0bd5e8e45604bc5f6b9 /src/testing/testing.c
parenta0fce305c565c0937d917a92712f15e9c5736260 (diff)
downloadgnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.tar.gz
gnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.zip
uncrustify as demanded.
Diffstat (limited to 'src/testing/testing.c')
-rw-r--r--src/testing/testing.c1474
1 files changed, 734 insertions, 740 deletions
diff --git a/src/testing/testing.c b/src/testing/testing.c
index c5c240b37..9c5c32012 100644
--- a/src/testing/testing.c
+++ b/src/testing/testing.c
@@ -34,7 +34,7 @@
34#include "gnunet_arm_service.h" 34#include "gnunet_arm_service.h"
35#include "gnunet_testing_lib.h" 35#include "gnunet_testing_lib.h"
36 36
37#define LOG(kind, ...) GNUNET_log_from (kind, "testing-api", __VA_ARGS__) 37#define LOG(kind, ...) GNUNET_log_from(kind, "testing-api", __VA_ARGS__)
38 38
39 39
40/** 40/**
@@ -63,8 +63,7 @@
63#define HIGH_PORT 56000 63#define HIGH_PORT 56000
64 64
65 65
66struct SharedServiceInstance 66struct SharedServiceInstance {
67{
68 struct SharedService *ss; 67 struct SharedService *ss;
69 68
70 char *cfg_fn; 69 char *cfg_fn;
@@ -78,8 +77,7 @@ struct SharedServiceInstance
78 unsigned int n_refs; 77 unsigned int n_refs;
79}; 78};
80 79
81struct SharedService 80struct SharedService {
82{
83 char *sname; 81 char *sname;
84 82
85 struct SharedServiceInstance **instances; 83 struct SharedServiceInstance **instances;
@@ -98,8 +96,7 @@ struct SharedService
98 * Handle for a system on which GNUnet peers are executed; 96 * Handle for a system on which GNUnet peers are executed;
99 * a system is used for reserving unique paths and ports. 97 * a system is used for reserving unique paths and ports.
100 */ 98 */
101struct GNUNET_TESTING_System 99struct GNUNET_TESTING_System {
102{
103 /** 100 /**
104 * Prefix (i.e. "/tmp/gnunet-testing/") we prepend to each 101 * Prefix (i.e. "/tmp/gnunet-testing/") we prepend to each
105 * GNUNET_HOME. 102 * GNUNET_HOME.
@@ -171,8 +168,7 @@ struct GNUNET_TESTING_System
171/** 168/**
172 * Handle for a GNUnet peer controlled by testing. 169 * Handle for a GNUnet peer controlled by testing.
173 */ 170 */
174struct GNUNET_TESTING_Peer 171struct GNUNET_TESTING_Peer {
175{
176 /** 172 /**
177 * The TESTING system associated with this peer 173 * The TESTING system associated with this peer
178 */ 174 */
@@ -253,56 +249,56 @@ struct GNUNET_TESTING_Peer
253 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error 249 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
254 */ 250 */
255static int 251static int
256hostkeys_load (struct GNUNET_TESTING_System *system) 252hostkeys_load(struct GNUNET_TESTING_System *system)
257{ 253{
258 uint64_t fs; 254 uint64_t fs;
259 char *data_dir; 255 char *data_dir;
260 char *filename; 256 char *filename;
261 struct GNUNET_DISK_FileHandle *fd; 257 struct GNUNET_DISK_FileHandle *fd;
262 258
263 GNUNET_assert (NULL == system->hostkeys_data); 259 GNUNET_assert(NULL == system->hostkeys_data);
264 data_dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR); 260 data_dir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_DATADIR);
265 GNUNET_asprintf (&filename, "%s/testing_hostkeys.ecc", data_dir); 261 GNUNET_asprintf(&filename, "%s/testing_hostkeys.ecc", data_dir);
266 GNUNET_free (data_dir); 262 GNUNET_free(data_dir);
267 263
268 if (GNUNET_YES != GNUNET_DISK_file_test (filename)) 264 if (GNUNET_YES != GNUNET_DISK_file_test(filename))
269 { 265 {
270 LOG (GNUNET_ERROR_TYPE_ERROR, 266 LOG(GNUNET_ERROR_TYPE_ERROR,
271 _ ("Hostkeys file not found: %s\n"), 267 _("Hostkeys file not found: %s\n"),
272 filename); 268 filename);
273 GNUNET_free (filename); 269 GNUNET_free(filename);
274 return GNUNET_SYSERR; 270 return GNUNET_SYSERR;
275 } 271 }
276 /* Check hostkey file size, read entire thing into memory */ 272 /* Check hostkey file size, read entire thing into memory */
277 if (GNUNET_OK != 273 if (GNUNET_OK !=
278 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES)) 274 GNUNET_DISK_file_size(filename, &fs, GNUNET_YES, GNUNET_YES))
279 fs = 0; 275 fs = 0;
280 if (0 == fs) 276 if (0 == fs)
281 { 277 {
282 GNUNET_free (filename); 278 GNUNET_free(filename);
283 return GNUNET_SYSERR; /* File is empty */ 279 return GNUNET_SYSERR; /* File is empty */
284 } 280 }
285 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE)) 281 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
286 { 282 {
287 LOG (GNUNET_ERROR_TYPE_ERROR, 283 LOG(GNUNET_ERROR_TYPE_ERROR,
288 _ ("Incorrect hostkey file format: %s\n"), 284 _("Incorrect hostkey file format: %s\n"),
289 filename); 285 filename);
290 GNUNET_free (filename); 286 GNUNET_free(filename);
291 return GNUNET_SYSERR; 287 return GNUNET_SYSERR;
292 } 288 }
293 fd = GNUNET_DISK_file_open (filename, 289 fd = GNUNET_DISK_file_open(filename,
294 GNUNET_DISK_OPEN_READ, 290 GNUNET_DISK_OPEN_READ,
295 GNUNET_DISK_PERM_NONE); 291 GNUNET_DISK_PERM_NONE);
296 if (NULL == fd) 292 if (NULL == fd)
297 { 293 {
298 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename); 294 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "open", filename);
299 GNUNET_free (filename); 295 GNUNET_free(filename);
300 return GNUNET_SYSERR; 296 return GNUNET_SYSERR;
301 } 297 }
302 GNUNET_free (filename); 298 GNUNET_free(filename);
303 system->hostkeys_data = 299 system->hostkeys_data =
304 GNUNET_DISK_file_map (fd, &system->map, GNUNET_DISK_MAP_TYPE_READ, fs); 300 GNUNET_DISK_file_map(fd, &system->map, GNUNET_DISK_MAP_TYPE_READ, fs);
305 GNUNET_DISK_file_close (fd); 301 GNUNET_DISK_file_close(fd);
306 if (NULL == system->hostkeys_data) 302 if (NULL == system->hostkeys_data)
307 return GNUNET_SYSERR; 303 return GNUNET_SYSERR;
308 system->total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE; 304 system->total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
@@ -316,11 +312,11 @@ hostkeys_load (struct GNUNET_TESTING_System *system)
316 * @param system the testing system handle 312 * @param system the testing system handle
317 */ 313 */
318static void 314static void
319hostkeys_unload (struct GNUNET_TESTING_System *system) 315hostkeys_unload(struct GNUNET_TESTING_System *system)
320{ 316{
321 GNUNET_break (NULL != system->hostkeys_data); 317 GNUNET_break(NULL != system->hostkeys_data);
322 system->hostkeys_data = NULL; 318 system->hostkeys_data = NULL;
323 GNUNET_DISK_file_unmap (system->map); 319 GNUNET_DISK_file_unmap(system->map);
324 system->map = NULL; 320 system->map = NULL;
325 system->hostkeys_data = NULL; 321 system->hostkeys_data = NULL;
326 system->total_hostkeys = 0; 322 system->total_hostkeys = 0;
@@ -336,14 +332,14 @@ hostkeys_unload (struct GNUNET_TESTING_System *system)
336 * @param value value of the option 332 * @param value value of the option
337 */ 333 */
338static void 334static void
339cfg_copy_iterator (void *cls, 335cfg_copy_iterator(void *cls,
340 const char *section, 336 const char *section,
341 const char *option, 337 const char *option,
342 const char *value) 338 const char *value)
343{ 339{
344 struct GNUNET_CONFIGURATION_Handle *cfg2 = cls; 340 struct GNUNET_CONFIGURATION_Handle *cfg2 = cls;
345 341
346 GNUNET_CONFIGURATION_set_value_string (cfg2, section, option, value); 342 GNUNET_CONFIGURATION_set_value_string(cfg2, section, option, value);
347} 343}
348 344
349 345
@@ -369,7 +365,7 @@ cfg_copy_iterator (void *cls,
369 * @return handle to this system, NULL on error 365 * @return handle to this system, NULL on error
370 */ 366 */
371struct GNUNET_TESTING_System * 367struct GNUNET_TESTING_System *
372GNUNET_TESTING_system_create_with_portrange ( 368GNUNET_TESTING_system_create_with_portrange(
373 const char *testdir, 369 const char *testdir,
374 const char *trusted_ip, 370 const char *trusted_ip,
375 const char *hostname, 371 const char *hostname,
@@ -382,53 +378,53 @@ GNUNET_TESTING_system_create_with_portrange (
382 struct SharedService *ss; 378 struct SharedService *ss;
383 unsigned int cnt; 379 unsigned int cnt;
384 380
385 GNUNET_assert (NULL != testdir); 381 GNUNET_assert(NULL != testdir);
386 system = GNUNET_new (struct GNUNET_TESTING_System); 382 system = GNUNET_new(struct GNUNET_TESTING_System);
387 if (NULL == (system->tmppath = getenv (GNUNET_TESTING_PREFIX))) 383 if (NULL == (system->tmppath = getenv(GNUNET_TESTING_PREFIX)))
388 system->tmppath = GNUNET_DISK_mkdtemp (testdir); 384 system->tmppath = GNUNET_DISK_mkdtemp(testdir);
389 else 385 else
390 system->tmppath = GNUNET_strdup (system->tmppath); 386 system->tmppath = GNUNET_strdup(system->tmppath);
391 system->lowport = lowport; 387 system->lowport = lowport;
392 system->highport = highport; 388 system->highport = highport;
393 if (NULL == system->tmppath) 389 if (NULL == system->tmppath)
394 { 390 {
395 GNUNET_free (system); 391 GNUNET_free(system);
396 return NULL; 392 return NULL;
397 } 393 }
398 if (NULL != trusted_ip) 394 if (NULL != trusted_ip)
399 system->trusted_ip = GNUNET_strdup (trusted_ip); 395 system->trusted_ip = GNUNET_strdup(trusted_ip);
400 if (NULL != hostname) 396 if (NULL != hostname)
401 system->hostname = GNUNET_strdup (hostname); 397 system->hostname = GNUNET_strdup(hostname);
402 if (GNUNET_OK != hostkeys_load (system)) 398 if (GNUNET_OK != hostkeys_load(system))
403 { 399 {
404 GNUNET_TESTING_system_destroy (system, GNUNET_YES); 400 GNUNET_TESTING_system_destroy(system, GNUNET_YES);
405 return NULL; 401 return NULL;
406 } 402 }
407 if (NULL == shared_services) 403 if (NULL == shared_services)
408 return system; 404 return system;
409 for (cnt = 0; NULL != shared_services[cnt].service; cnt++) 405 for (cnt = 0; NULL != shared_services[cnt].service; cnt++)
410 { 406 {
411 tss = shared_services[cnt]; 407 tss = shared_services[cnt];
412 ss = GNUNET_new (struct SharedService); 408 ss = GNUNET_new(struct SharedService);
413 ss->sname = GNUNET_strdup (tss.service); 409 ss->sname = GNUNET_strdup(tss.service);
414 ss->cfg = GNUNET_CONFIGURATION_create (); 410 ss->cfg = GNUNET_CONFIGURATION_create();
415 GNUNET_CONFIGURATION_iterate_section_values (tss.cfg, 411 GNUNET_CONFIGURATION_iterate_section_values(tss.cfg,
416 ss->sname, 412 ss->sname,
417 &cfg_copy_iterator, 413 &cfg_copy_iterator,
418 ss->cfg); 414 ss->cfg);
419 GNUNET_CONFIGURATION_iterate_section_values (tss.cfg, 415 GNUNET_CONFIGURATION_iterate_section_values(tss.cfg,
420 "TESTING", 416 "TESTING",
421 &cfg_copy_iterator, 417 &cfg_copy_iterator,
422 ss->cfg); 418 ss->cfg);
423 GNUNET_CONFIGURATION_iterate_section_values (tss.cfg, 419 GNUNET_CONFIGURATION_iterate_section_values(tss.cfg,
424 "PATHS", 420 "PATHS",
425 &cfg_copy_iterator, 421 &cfg_copy_iterator,
426 ss->cfg); 422 ss->cfg);
427 ss->share = tss.share; 423 ss->share = tss.share;
428 GNUNET_array_append (system->shared_services, 424 GNUNET_array_append(system->shared_services,
429 system->n_shared_services, 425 system->n_shared_services,
430 ss); 426 ss);
431 } 427 }
432 return system; 428 return system;
433} 429}
434 430
@@ -454,59 +450,59 @@ GNUNET_TESTING_system_create_with_portrange (
454 * @return handle to this system, NULL on error 450 * @return handle to this system, NULL on error
455 */ 451 */
456struct GNUNET_TESTING_System * 452struct GNUNET_TESTING_System *
457GNUNET_TESTING_system_create ( 453GNUNET_TESTING_system_create(
458 const char *testdir, 454 const char *testdir,
459 const char *trusted_ip, 455 const char *trusted_ip,
460 const char *hostname, 456 const char *hostname,
461 const struct GNUNET_TESTING_SharedService *shared_services) 457 const struct GNUNET_TESTING_SharedService *shared_services)
462{ 458{
463 return GNUNET_TESTING_system_create_with_portrange (testdir, 459 return GNUNET_TESTING_system_create_with_portrange(testdir,
464 trusted_ip, 460 trusted_ip,
465 hostname, 461 hostname,
466 shared_services, 462 shared_services,
467 LOW_PORT, 463 LOW_PORT,
468 HIGH_PORT); 464 HIGH_PORT);
469} 465}
470 466
471 467
472static void 468static void
473cleanup_shared_service_instance (struct SharedServiceInstance *i) 469cleanup_shared_service_instance(struct SharedServiceInstance *i)
474{ 470{
475 if (NULL != i->cfg_fn) 471 if (NULL != i->cfg_fn)
476 { 472 {
477 (void) unlink (i->cfg_fn); 473 (void)unlink(i->cfg_fn);
478 GNUNET_free (i->cfg_fn); 474 GNUNET_free(i->cfg_fn);
479 } 475 }
480 GNUNET_free_non_null (i->unix_sock); 476 GNUNET_free_non_null(i->unix_sock);
481 GNUNET_free_non_null (i->port_str); 477 GNUNET_free_non_null(i->port_str);
482 GNUNET_break (NULL == i->proc); 478 GNUNET_break(NULL == i->proc);
483 GNUNET_break (0 == i->n_refs); 479 GNUNET_break(0 == i->n_refs);
484 GNUNET_free (i); 480 GNUNET_free(i);
485} 481}
486 482
487 483
488static int 484static int
489start_shared_service_instance (struct SharedServiceInstance *i) 485start_shared_service_instance(struct SharedServiceInstance *i)
490{ 486{
491 char *binary; 487 char *binary;
492 char *libexec_binary; 488 char *libexec_binary;
493 489
494 GNUNET_assert (NULL == i->proc); 490 GNUNET_assert(NULL == i->proc);
495 GNUNET_assert (NULL != i->cfg_fn); 491 GNUNET_assert(NULL != i->cfg_fn);
496 (void) GNUNET_asprintf (&binary, "gnunet-service-%s", i->ss->sname); 492 (void)GNUNET_asprintf(&binary, "gnunet-service-%s", i->ss->sname);
497 libexec_binary = GNUNET_OS_get_libexec_binary_path (binary); 493 libexec_binary = GNUNET_OS_get_libexec_binary_path(binary);
498 GNUNET_free (binary); 494 GNUNET_free(binary);
499 i->proc = GNUNET_OS_start_process (PIPE_CONTROL, 495 i->proc = GNUNET_OS_start_process(PIPE_CONTROL,
500 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 496 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
501 NULL, 497 NULL,
502 NULL, 498 NULL,
503 NULL, 499 NULL,
504 libexec_binary, 500 libexec_binary,
505 libexec_binary, 501 libexec_binary,
506 "-c", 502 "-c",
507 i->cfg_fn, 503 i->cfg_fn,
508 NULL); 504 NULL);
509 GNUNET_free (libexec_binary); 505 GNUNET_free(libexec_binary);
510 if (NULL == i->proc) 506 if (NULL == i->proc)
511 return GNUNET_SYSERR; 507 return GNUNET_SYSERR;
512 return GNUNET_OK; 508 return GNUNET_OK;
@@ -514,15 +510,15 @@ start_shared_service_instance (struct SharedServiceInstance *i)
514 510
515 511
516static void 512static void
517stop_shared_service_instance (struct SharedServiceInstance *i) 513stop_shared_service_instance(struct SharedServiceInstance *i)
518{ 514{
519 GNUNET_break (0 == i->n_refs); 515 GNUNET_break(0 == i->n_refs);
520 if (0 != GNUNET_OS_process_kill (i->proc, GNUNET_TERM_SIG)) 516 if (0 != GNUNET_OS_process_kill(i->proc, GNUNET_TERM_SIG))
521 LOG (GNUNET_ERROR_TYPE_WARNING, 517 LOG(GNUNET_ERROR_TYPE_WARNING,
522 "Killing shared service instance (%s) failed\n", 518 "Killing shared service instance (%s) failed\n",
523 i->ss->sname); 519 i->ss->sname);
524 (void) GNUNET_OS_process_wait (i->proc); 520 (void)GNUNET_OS_process_wait(i->proc);
525 GNUNET_OS_process_destroy (i->proc); 521 GNUNET_OS_process_destroy(i->proc);
526 i->proc = NULL; 522 i->proc = NULL;
527} 523}
528 524
@@ -535,8 +531,8 @@ stop_shared_service_instance (struct SharedServiceInstance *i)
535 * be removed (clean up on shutdown)? 531 * be removed (clean up on shutdown)?
536 */ 532 */
537void 533void
538GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system, 534GNUNET_TESTING_system_destroy(struct GNUNET_TESTING_System *system,
539 int remove_paths) 535 int remove_paths)
540{ 536{
541 struct SharedService *ss; 537 struct SharedService *ss;
542 struct SharedServiceInstance *i; 538 struct SharedServiceInstance *i;
@@ -544,29 +540,29 @@ GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
544 unsigned int i_cnt; 540 unsigned int i_cnt;
545 541
546 if (NULL != system->hostkeys_data) 542 if (NULL != system->hostkeys_data)
547 hostkeys_unload (system); 543 hostkeys_unload(system);
548 for (ss_cnt = 0; ss_cnt < system->n_shared_services; ss_cnt++) 544 for (ss_cnt = 0; ss_cnt < system->n_shared_services; ss_cnt++)
549 {
550 ss = system->shared_services[ss_cnt];
551 for (i_cnt = 0; i_cnt < ss->n_instances; i_cnt++)
552 { 545 {
553 i = ss->instances[i_cnt]; 546 ss = system->shared_services[ss_cnt];
554 if (NULL != i->proc) 547 for (i_cnt = 0; i_cnt < ss->n_instances; i_cnt++)
555 stop_shared_service_instance (i); 548 {
556 cleanup_shared_service_instance (i); 549 i = ss->instances[i_cnt];
550 if (NULL != i->proc)
551 stop_shared_service_instance(i);
552 cleanup_shared_service_instance(i);
553 }
554 GNUNET_free_non_null(ss->instances);
555 GNUNET_CONFIGURATION_destroy(ss->cfg);
556 GNUNET_free(ss->sname);
557 GNUNET_free(ss);
557 } 558 }
558 GNUNET_free_non_null (ss->instances); 559 GNUNET_free_non_null(system->shared_services);
559 GNUNET_CONFIGURATION_destroy (ss->cfg);
560 GNUNET_free (ss->sname);
561 GNUNET_free (ss);
562 }
563 GNUNET_free_non_null (system->shared_services);
564 if (GNUNET_YES == remove_paths) 560 if (GNUNET_YES == remove_paths)
565 GNUNET_DISK_directory_remove (system->tmppath); 561 GNUNET_DISK_directory_remove(system->tmppath);
566 GNUNET_free (system->tmppath); 562 GNUNET_free(system->tmppath);
567 GNUNET_free_non_null (system->trusted_ip); 563 GNUNET_free_non_null(system->trusted_ip);
568 GNUNET_free_non_null (system->hostname); 564 GNUNET_free_non_null(system->hostname);
569 GNUNET_free (system); 565 GNUNET_free(system);
570} 566}
571 567
572 568
@@ -577,7 +573,7 @@ GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
577 * @return 0 if no free port was available 573 * @return 0 if no free port was available
578 */ 574 */
579uint16_t 575uint16_t
580GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system) 576GNUNET_TESTING_reserve_port(struct GNUNET_TESTING_System *system)
581{ 577{
582 struct GNUNET_NETWORK_Handle *socket; 578 struct GNUNET_NETWORK_Handle *socket;
583 struct addrinfo hint; 579 struct addrinfo hint;
@@ -592,14 +588,14 @@ GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system)
592 uint16_t pos; 588 uint16_t pos;
593 589
594 /* 590 /*
595 FIXME: Instead of using getaddrinfo we should try to determine the port 591 FIXME: Instead of using getaddrinfo we should try to determine the port
596 status by the following heurestics. 592 status by the following heurestics.
597 593
598 On systems which support both IPv4 and IPv6, only ports open on both 594 On systems which support both IPv4 and IPv6, only ports open on both
599 address families are considered open. 595 address families are considered open.
600 On system with either IPv4 or IPv6. A port is considered open if it's 596 On system with either IPv4 or IPv6. A port is considered open if it's
601 open in the respective address family 597 open in the respective address family
602 */ 598 */
603 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */ 599 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
604 hint.ai_socktype = 0; 600 hint.ai_socktype = 0;
605 hint.ai_protocol = 0; 601 hint.ai_protocol = 0;
@@ -611,57 +607,57 @@ GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system)
611 port_buckets = system->reserved_ports; 607 port_buckets = system->reserved_ports;
612 for (index = (system->lowport / 32) + 1; index < (system->highport / 32); 608 for (index = (system->lowport / 32) + 1; index < (system->highport / 32);
613 index++) 609 index++)
614 {
615 xor_image = (UINT32_MAX ^ port_buckets[index]);
616 if (0 == xor_image) /* Ports in the bucket are full */
617 continue;
618 pos = system->lowport % 32;
619 while (pos < 32)
620 { 610 {
621 if (0 == ((xor_image >> pos) & 1U)) 611 xor_image = (UINT32_MAX ^ port_buckets[index]);
622 { 612 if (0 == xor_image) /* Ports in the bucket are full */
623 pos++;
624 continue; 613 continue;
625 } 614 pos = system->lowport % 32;
626 open_port = (index * 32) + pos; 615 while (pos < 32)
627 if (open_port >= system->highport) 616 {
628 return 0; 617 if (0 == ((xor_image >> pos) & 1U))
629 GNUNET_asprintf (&open_port_str, "%u", (unsigned int) open_port); 618 {
630 ret = NULL; 619 pos++;
631 GNUNET_assert (0 == getaddrinfo (NULL, open_port_str, &hint, &ret)); 620 continue;
632 GNUNET_free (open_port_str); 621 }
633 bind_status = GNUNET_NO; 622 open_port = (index * 32) + pos;
634 for (ai = ret; NULL != ai; ai = ai->ai_next) 623 if (open_port >= system->highport)
635 { 624 return 0;
636 socket = GNUNET_NETWORK_socket_create (ai->ai_family, SOCK_STREAM, 0); 625 GNUNET_asprintf(&open_port_str, "%u", (unsigned int)open_port);
637 if (NULL == socket) 626 ret = NULL;
638 continue; 627 GNUNET_assert(0 == getaddrinfo(NULL, open_port_str, &hint, &ret));
639 bind_status = 628 GNUNET_free(open_port_str);
640 GNUNET_NETWORK_socket_bind (socket, ai->ai_addr, ai->ai_addrlen); 629 bind_status = GNUNET_NO;
641 GNUNET_NETWORK_socket_close (socket); 630 for (ai = ret; NULL != ai; ai = ai->ai_next)
642 if (GNUNET_OK != bind_status) 631 {
643 break; 632 socket = GNUNET_NETWORK_socket_create(ai->ai_family, SOCK_STREAM, 0);
644 socket = GNUNET_NETWORK_socket_create (ai->ai_family, SOCK_DGRAM, 0); 633 if (NULL == socket)
645 if (NULL == socket) 634 continue;
646 continue; 635 bind_status =
647 bind_status = 636 GNUNET_NETWORK_socket_bind(socket, ai->ai_addr, ai->ai_addrlen);
648 GNUNET_NETWORK_socket_bind (socket, ai->ai_addr, ai->ai_addrlen); 637 GNUNET_NETWORK_socket_close(socket);
649 GNUNET_NETWORK_socket_close (socket); 638 if (GNUNET_OK != bind_status)
650 if (GNUNET_OK != bind_status) 639 break;
651 break; 640 socket = GNUNET_NETWORK_socket_create(ai->ai_family, SOCK_DGRAM, 0);
652 } 641 if (NULL == socket)
653 port_buckets[index] |= (1U << pos); /* Set the port bit */ 642 continue;
654 freeaddrinfo (ret); 643 bind_status =
655 if (GNUNET_OK == bind_status) 644 GNUNET_NETWORK_socket_bind(socket, ai->ai_addr, ai->ai_addrlen);
656 { 645 GNUNET_NETWORK_socket_close(socket);
657 LOG (GNUNET_ERROR_TYPE_DEBUG, 646 if (GNUNET_OK != bind_status)
658 "Found a free port %u\n", 647 break;
659 (unsigned int) open_port); 648 }
660 return open_port; 649 port_buckets[index] |= (1U << pos); /* Set the port bit */
661 } 650 freeaddrinfo(ret);
662 pos++; 651 if (GNUNET_OK == bind_status)
652 {
653 LOG(GNUNET_ERROR_TYPE_DEBUG,
654 "Found a free port %u\n",
655 (unsigned int)open_port);
656 return open_port;
657 }
658 pos++;
659 }
663 } 660 }
664 }
665 return 0; 661 return 0;
666} 662}
667 663
@@ -674,8 +670,8 @@ GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system)
674 * @param port reserved port to release 670 * @param port reserved port to release
675 */ 671 */
676void 672void
677GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system, 673GNUNET_TESTING_release_port(struct GNUNET_TESTING_System *system,
678 uint16_t port) 674 uint16_t port)
679{ 675{
680 uint32_t *port_buckets; 676 uint32_t *port_buckets;
681 uint16_t bucket; 677 uint16_t bucket;
@@ -684,12 +680,12 @@ GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system,
684 port_buckets = system->reserved_ports; 680 port_buckets = system->reserved_ports;
685 bucket = port / 32; 681 bucket = port / 32;
686 pos = port % 32; 682 pos = port % 32;
687 LOG (GNUNET_ERROR_TYPE_DEBUG, "Releasing port %u\n", port); 683 LOG(GNUNET_ERROR_TYPE_DEBUG, "Releasing port %u\n", port);
688 if (0 == (port_buckets[bucket] & (1U << pos))) 684 if (0 == (port_buckets[bucket] & (1U << pos)))
689 { 685 {
690 GNUNET_break (0); /* Port was not reserved by us using reserve_port() */ 686 GNUNET_break(0); /* Port was not reserved by us using reserve_port() */
691 return; 687 return;
692 } 688 }
693 port_buckets[bucket] &= ~(1U << pos); 689 port_buckets[bucket] &= ~(1U << pos);
694} 690}
695 691
@@ -712,27 +708,27 @@ GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system,
712 * @return NULL on error (not enough keys) 708 * @return NULL on error (not enough keys)
713 */ 709 */
714struct GNUNET_CRYPTO_EddsaPrivateKey * 710struct GNUNET_CRYPTO_EddsaPrivateKey *
715GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system, 711GNUNET_TESTING_hostkey_get(const struct GNUNET_TESTING_System *system,
716 uint32_t key_number, 712 uint32_t key_number,
717 struct GNUNET_PeerIdentity *id) 713 struct GNUNET_PeerIdentity *id)
718{ 714{
719 struct GNUNET_CRYPTO_EddsaPrivateKey *private_key; 715 struct GNUNET_CRYPTO_EddsaPrivateKey *private_key;
720 716
721 if ((NULL == id) || (NULL == system->hostkeys_data)) 717 if ((NULL == id) || (NULL == system->hostkeys_data))
722 return NULL; 718 return NULL;
723 if (key_number >= system->total_hostkeys) 719 if (key_number >= system->total_hostkeys)
724 { 720 {
725 LOG (GNUNET_ERROR_TYPE_ERROR, 721 LOG(GNUNET_ERROR_TYPE_ERROR,
726 _ ("Key number %u does not exist\n"), 722 _("Key number %u does not exist\n"),
727 key_number); 723 key_number);
728 return NULL; 724 return NULL;
729 } 725 }
730 private_key = GNUNET_new (struct GNUNET_CRYPTO_EddsaPrivateKey); 726 private_key = GNUNET_new(struct GNUNET_CRYPTO_EddsaPrivateKey);
731 GNUNET_memcpy (private_key, 727 GNUNET_memcpy(private_key,
732 system->hostkeys_data + 728 system->hostkeys_data +
733 (key_number * GNUNET_TESTING_HOSTKEYFILESIZE), 729 (key_number * GNUNET_TESTING_HOSTKEYFILESIZE),
734 GNUNET_TESTING_HOSTKEYFILESIZE); 730 GNUNET_TESTING_HOSTKEYFILESIZE);
735 GNUNET_CRYPTO_eddsa_key_get_public (private_key, &id->public_key); 731 GNUNET_CRYPTO_eddsa_key_get_public(private_key, &id->public_key);
736 return private_key; 732 return private_key;
737} 733}
738 734
@@ -741,8 +737,7 @@ GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system,
741 * Structure for holding data to build new configurations from a configuration 737 * Structure for holding data to build new configurations from a configuration
742 * template 738 * template
743 */ 739 */
744struct UpdateContext 740struct UpdateContext {
745{
746 /** 741 /**
747 * The system for which we are building configurations 742 * The system for which we are building configurations
748 */ 743 */
@@ -788,10 +783,10 @@ struct UpdateContext
788 * @param value value of the option 783 * @param value value of the option
789 */ 784 */
790static void 785static void
791update_config (void *cls, 786update_config(void *cls,
792 const char *section, 787 const char *section,
793 const char *option, 788 const char *option,
794 const char *value) 789 const char *value)
795{ 790{
796 struct UpdateContext *uc = cls; 791 struct UpdateContext *uc = cls;
797 unsigned int ival; 792 unsigned int ival;
@@ -804,76 +799,76 @@ update_config (void *cls,
804 799
805 if (GNUNET_OK != uc->status) 800 if (GNUNET_OK != uc->status)
806 return; 801 return;
807 if (! ((0 == strcmp (option, "PORT")) || (0 == strcmp (option, "UNIXPATH")) || 802 if (!((0 == strcmp(option, "PORT")) || (0 == strcmp(option, "UNIXPATH")) ||
808 (0 == strcmp (option, "HOSTNAME")))) 803 (0 == strcmp(option, "HOSTNAME"))))
809 return; 804 return;
810 GNUNET_asprintf (&single_variable, "single_%s_per_host", section); 805 GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
811 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section); 806 GNUNET_asprintf(&per_host_variable, "num_%s_per_host", section);
812 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival))) 807 if ((0 == strcmp(option, "PORT")) && (1 == sscanf(value, "%u", &ival)))
813 {
814 if ((ival != 0) &&
815 (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno (uc->cfg,
816 "testing",
817 single_variable)))
818 { 808 {
819 new_port = GNUNET_TESTING_reserve_port (uc->system); 809 if ((ival != 0) &&
820 if (0 == new_port) 810 (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(uc->cfg,
821 { 811 "testing",
822 uc->status = GNUNET_SYSERR; 812 single_variable)))
823 GNUNET_free (single_variable); 813 {
824 GNUNET_free (per_host_variable); 814 new_port = GNUNET_TESTING_reserve_port(uc->system);
825 return; 815 if (0 == new_port)
826 } 816 {
827 GNUNET_snprintf (cval, sizeof (cval), "%u", new_port); 817 uc->status = GNUNET_SYSERR;
828 value = cval; 818 GNUNET_free(single_variable);
829 GNUNET_array_append (uc->ports, uc->nports, new_port); 819 GNUNET_free(per_host_variable);
830 } 820 return;
831 else if ((ival != 0) && 821 }
832 (GNUNET_YES == 822 GNUNET_snprintf(cval, sizeof(cval), "%u", new_port);
833 GNUNET_CONFIGURATION_get_value_yesno (uc->cfg, 823 value = cval;
834 "testing", 824 GNUNET_array_append(uc->ports, uc->nports, new_port);
835 single_variable)) && 825 }
836 GNUNET_CONFIGURATION_get_value_number (uc->cfg, 826 else if ((ival != 0) &&
837 "testing", 827 (GNUNET_YES ==
838 per_host_variable, 828 GNUNET_CONFIGURATION_get_value_yesno(uc->cfg,
839 &num_per_host)) 829 "testing",
840 { 830 single_variable)) &&
841 /* GNUNET_snprintf (cval, sizeof (cval), "%u", */ 831 GNUNET_CONFIGURATION_get_value_number(uc->cfg,
842 /* ival + ctx->fdnum % num_per_host); */ 832 "testing",
843 /* value = cval; */ 833 per_host_variable,
844 GNUNET_break (0); /* FIXME */ 834 &num_per_host))
835 {
836 /* GNUNET_snprintf (cval, sizeof (cval), "%u", */
837 /* ival + ctx->fdnum % num_per_host); */
838 /* value = cval; */
839 GNUNET_break(0); /* FIXME */
840 }
845 } 841 }
846 } 842 if (0 == strcmp(option, "UNIXPATH"))
847 if (0 == strcmp (option, "UNIXPATH"))
848 {
849 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno (uc->cfg,
850 "testing",
851 single_variable))
852 { 843 {
853 GNUNET_snprintf (uval, 844 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(uc->cfg,
854 sizeof (uval), 845 "testing",
855 "%s/%s.sock", 846 single_variable))
856 uc->gnunet_home, 847 {
857 section); 848 GNUNET_snprintf(uval,
858 value = uval; 849 sizeof(uval),
850 "%s/%s.sock",
851 uc->gnunet_home,
852 section);
853 value = uval;
854 }
855 else if ((GNUNET_YES ==
856 GNUNET_CONFIGURATION_get_value_number(uc->cfg,
857 "testing",
858 per_host_variable,
859 &num_per_host)) &&
860 (num_per_host > 0))
861 {
862 GNUNET_break(0); /* FIXME */
863 }
859 } 864 }
860 else if ((GNUNET_YES == 865 if (0 == strcmp(option, "HOSTNAME"))
861 GNUNET_CONFIGURATION_get_value_number (uc->cfg,
862 "testing",
863 per_host_variable,
864 &num_per_host)) &&
865 (num_per_host > 0))
866 { 866 {
867 GNUNET_break (0); /* FIXME */ 867 value = (NULL == uc->system->hostname) ? "localhost" : uc->system->hostname;
868 } 868 }
869 } 869 GNUNET_free(single_variable);
870 if (0 == strcmp (option, "HOSTNAME")) 870 GNUNET_free(per_host_variable);
871 { 871 GNUNET_CONFIGURATION_set_value_string(uc->cfg, section, option, value);
872 value = (NULL == uc->system->hostname) ? "localhost" : uc->system->hostname;
873 }
874 GNUNET_free (single_variable);
875 GNUNET_free (per_host_variable);
876 GNUNET_CONFIGURATION_set_value_string (uc->cfg, section, option, value);
877} 872}
878 873
879 874
@@ -885,7 +880,7 @@ update_config (void *cls,
885 * @param section name of the section 880 * @param section name of the section
886 */ 881 */
887static void 882static void
888update_config_sections (void *cls, const char *section) 883update_config_sections(void *cls, const char *section)
889{ 884{
890 struct UpdateContext *uc = cls; 885 struct UpdateContext *uc = cls;
891 char **ikeys; 886 char **ikeys;
@@ -902,98 +897,98 @@ update_config_sections (void *cls, const char *section)
902 /* Ignore certain options from sections. See 897 /* Ignore certain options from sections. See
903 https://gnunet.org/bugs/view.php?id=2476 */ 898 https://gnunet.org/bugs/view.php?id=2476 */
904 if (GNUNET_YES == 899 if (GNUNET_YES ==
905 GNUNET_CONFIGURATION_have_value (uc->cfg, section, "TESTING_IGNORE_KEYS")) 900 GNUNET_CONFIGURATION_have_value(uc->cfg, section, "TESTING_IGNORE_KEYS"))
906 { 901 {
907 GNUNET_assert (GNUNET_YES == 902 GNUNET_assert(GNUNET_YES ==
908 GNUNET_CONFIGURATION_get_value_string (uc->cfg, 903 GNUNET_CONFIGURATION_get_value_string(uc->cfg,
909 section, 904 section,
910 "TESTING_IGNORE_KEYS", 905 "TESTING_IGNORE_KEYS",
911 &val)); 906 &val));
912 ptr = val;
913 for (ikeys_cnt = 0; NULL != (ptr = strstr (ptr, ";")); ikeys_cnt++)
914 ptr++;
915 if (0 == ikeys_cnt)
916 GNUNET_break (0);
917 else
918 {
919 ikeys = GNUNET_malloc ((sizeof (char *)) * ikeys_cnt);
920 ptr = val; 907 ptr = val;
921 for (key = 0; key < ikeys_cnt; key++) 908 for (ikeys_cnt = 0; NULL != (ptr = strstr(ptr, ";")); ikeys_cnt++)
922 {
923 ikeys[key] = ptr;
924 ptr = strstr (ptr, ";");
925 GNUNET_assert (NULL != ptr); /* worked just before... */
926 *ptr = '\0';
927 ptr++; 909 ptr++;
928 } 910 if (0 == ikeys_cnt)
911 GNUNET_break(0);
912 else
913 {
914 ikeys = GNUNET_malloc((sizeof(char *)) * ikeys_cnt);
915 ptr = val;
916 for (key = 0; key < ikeys_cnt; key++)
917 {
918 ikeys[key] = ptr;
919 ptr = strstr(ptr, ";");
920 GNUNET_assert(NULL != ptr); /* worked just before... */
921 *ptr = '\0';
922 ptr++;
923 }
924 }
929 } 925 }
930 }
931 if (0 != ikeys_cnt) 926 if (0 != ikeys_cnt)
932 {
933 for (key = 0; key < ikeys_cnt; key++)
934 {
935 if (NULL != strstr (ikeys[key], "ADVERTISED_PORT"))
936 break;
937 }
938 if ((key == ikeys_cnt) &&
939 (GNUNET_YES ==
940 GNUNET_CONFIGURATION_have_value (uc->cfg, section, "ADVERTISED_PORT")))
941 { 927 {
942 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (uc->cfg, 928 for (key = 0; key < ikeys_cnt; key++)
943 section, 929 {
944 "PORT", 930 if (NULL != strstr(ikeys[key], "ADVERTISED_PORT"))
945 &ptr)) 931 break;
946 { 932 }
947 GNUNET_CONFIGURATION_set_value_string (uc->cfg, 933 if ((key == ikeys_cnt) &&
948 section, 934 (GNUNET_YES ==
949 "ADVERTISED_PORT", 935 GNUNET_CONFIGURATION_have_value(uc->cfg, section, "ADVERTISED_PORT")))
950 ptr); 936 {
951 GNUNET_free (ptr); 937 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(uc->cfg,
952 } 938 section,
953 } 939 "PORT",
954 for (key = 0; key < ikeys_cnt; key++) 940 &ptr))
955 { 941 {
956 if (NULL != strstr (ikeys[key], "ACCEPT_FROM")) 942 GNUNET_CONFIGURATION_set_value_string(uc->cfg,
957 { 943 section,
958 GNUNET_free (ikeys); 944 "ADVERTISED_PORT",
959 GNUNET_free (val); 945 ptr);
960 return; 946 GNUNET_free(ptr);
961 } 947 }
948 }
949 for (key = 0; key < ikeys_cnt; key++)
950 {
951 if (NULL != strstr(ikeys[key], "ACCEPT_FROM"))
952 {
953 GNUNET_free(ikeys);
954 GNUNET_free(val);
955 return;
956 }
957 }
958 GNUNET_free(ikeys);
962 } 959 }
963 GNUNET_free (ikeys); 960 GNUNET_free_non_null(val);
964 }
965 GNUNET_free_non_null (val);
966 ACCEPT_FROM_key = "ACCEPT_FROM"; 961 ACCEPT_FROM_key = "ACCEPT_FROM";
967 if ((NULL != uc->system->trusted_ip) && 962 if ((NULL != uc->system->trusted_ip) &&
968 (NULL != strstr (uc->system->trusted_ip, ":"))) /* IPv6 in use */ 963 (NULL != strstr(uc->system->trusted_ip, ":"))) /* IPv6 in use */
969 ACCEPT_FROM_key = "ACCEPT_FROM6"; 964 ACCEPT_FROM_key = "ACCEPT_FROM6";
970 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (uc->cfg, 965 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(uc->cfg,
971 section, 966 section,
972 ACCEPT_FROM_key, 967 ACCEPT_FROM_key,
973 &orig_allowed_hosts)) 968 &orig_allowed_hosts))
974 { 969 {
975 orig_allowed_hosts = GNUNET_strdup ("127.0.0.1;"); 970 orig_allowed_hosts = GNUNET_strdup("127.0.0.1;");
976 } 971 }
977 if (NULL == uc->system->trusted_ip) 972 if (NULL == uc->system->trusted_ip)
978 allowed_hosts = GNUNET_strdup (orig_allowed_hosts); 973 allowed_hosts = GNUNET_strdup(orig_allowed_hosts);
979 else 974 else
980 GNUNET_asprintf (&allowed_hosts, 975 GNUNET_asprintf(&allowed_hosts,
981 "%s%s;", 976 "%s%s;",
982 orig_allowed_hosts, 977 orig_allowed_hosts,
983 uc->system->trusted_ip); 978 uc->system->trusted_ip);
984 GNUNET_free (orig_allowed_hosts); 979 GNUNET_free(orig_allowed_hosts);
985 GNUNET_CONFIGURATION_set_value_string (uc->cfg, 980 GNUNET_CONFIGURATION_set_value_string(uc->cfg,
986 section, 981 section,
987 ACCEPT_FROM_key, 982 ACCEPT_FROM_key,
988 allowed_hosts); 983 allowed_hosts);
989 GNUNET_free (allowed_hosts); 984 GNUNET_free(allowed_hosts);
990} 985}
991 986
992 987
993static struct SharedServiceInstance * 988static struct SharedServiceInstance *
994associate_shared_service (struct GNUNET_TESTING_System *system, 989associate_shared_service(struct GNUNET_TESTING_System *system,
995 struct SharedService *ss, 990 struct SharedService *ss,
996 struct GNUNET_CONFIGURATION_Handle *cfg) 991 struct GNUNET_CONFIGURATION_Handle *cfg)
997{ 992{
998 struct SharedServiceInstance *i; 993 struct SharedServiceInstance *i;
999 struct GNUNET_CONFIGURATION_Handle *temp; 994 struct GNUNET_CONFIGURATION_Handle *temp;
@@ -1004,62 +999,62 @@ associate_shared_service (struct GNUNET_TESTING_System *system,
1004 if (((0 == ss->share) && (NULL == ss->instances)) || 999 if (((0 == ss->share) && (NULL == ss->instances)) ||
1005 ((0 != ss->share) && 1000 ((0 != ss->share) &&
1006 (ss->n_instances < ((ss->n_peers + ss->share - 1) / ss->share)))) 1001 (ss->n_instances < ((ss->n_peers + ss->share - 1) / ss->share))))
1007 { 1002 {
1008 i = GNUNET_new (struct SharedServiceInstance); 1003 i = GNUNET_new(struct SharedServiceInstance);
1009 i->ss = ss; 1004 i->ss = ss;
1010 (void) GNUNET_asprintf (&gnunet_home, 1005 (void)GNUNET_asprintf(&gnunet_home,
1011 "%s/shared/%s/%u", 1006 "%s/shared/%s/%u",
1012 system->tmppath, 1007 system->tmppath,
1013 ss->sname, 1008 ss->sname,
1014 ss->n_instances); 1009 ss->n_instances);
1015 (void) GNUNET_asprintf (&i->unix_sock, "%s/sock", gnunet_home); 1010 (void)GNUNET_asprintf(&i->unix_sock, "%s/sock", gnunet_home);
1016 port = GNUNET_TESTING_reserve_port (system); 1011 port = GNUNET_TESTING_reserve_port(system);
1017 if (0 == port) 1012 if (0 == port)
1018 { 1013 {
1019 GNUNET_free (gnunet_home); 1014 GNUNET_free(gnunet_home);
1020 cleanup_shared_service_instance (i); 1015 cleanup_shared_service_instance(i);
1021 return NULL; 1016 return NULL;
1017 }
1018 GNUNET_array_append(ss->instances, ss->n_instances, i);
1019 temp = GNUNET_CONFIGURATION_dup(ss->cfg);
1020 (void)GNUNET_asprintf(&i->port_str, "%u", port);
1021 (void)GNUNET_asprintf(&i->cfg_fn, "%s/config", gnunet_home);
1022 GNUNET_CONFIGURATION_set_value_string(temp,
1023 "PATHS",
1024 "GNUNET_HOME",
1025 gnunet_home);
1026 GNUNET_free(gnunet_home);
1027 GNUNET_CONFIGURATION_set_value_string(temp,
1028 ss->sname,
1029 "UNIXPATH",
1030 i->unix_sock);
1031 GNUNET_CONFIGURATION_set_value_string(temp,
1032 ss->sname,
1033 "PORT",
1034 i->port_str);
1035 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_write(temp, i->cfg_fn))
1036 {
1037 GNUNET_CONFIGURATION_destroy(temp);
1038 cleanup_shared_service_instance(i);
1039 return NULL;
1040 }
1041 GNUNET_CONFIGURATION_destroy(temp);
1022 } 1042 }
1023 GNUNET_array_append (ss->instances, ss->n_instances, i); 1043 else
1024 temp = GNUNET_CONFIGURATION_dup (ss->cfg);
1025 (void) GNUNET_asprintf (&i->port_str, "%u", port);
1026 (void) GNUNET_asprintf (&i->cfg_fn, "%s/config", gnunet_home);
1027 GNUNET_CONFIGURATION_set_value_string (temp,
1028 "PATHS",
1029 "GNUNET_HOME",
1030 gnunet_home);
1031 GNUNET_free (gnunet_home);
1032 GNUNET_CONFIGURATION_set_value_string (temp,
1033 ss->sname,
1034 "UNIXPATH",
1035 i->unix_sock);
1036 GNUNET_CONFIGURATION_set_value_string (temp,
1037 ss->sname,
1038 "PORT",
1039 i->port_str);
1040 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_write (temp, i->cfg_fn))
1041 { 1044 {
1042 GNUNET_CONFIGURATION_destroy (temp); 1045 GNUNET_assert(NULL != ss->instances);
1043 cleanup_shared_service_instance (i); 1046 GNUNET_assert(0 < ss->n_instances);
1044 return NULL; 1047 i = ss->instances[ss->n_instances - 1];
1045 } 1048 }
1046 GNUNET_CONFIGURATION_destroy (temp); 1049 GNUNET_CONFIGURATION_iterate_section_values(ss->cfg,
1047 } 1050 ss->sname,
1048 else 1051 &cfg_copy_iterator,
1049 { 1052 cfg);
1050 GNUNET_assert (NULL != ss->instances); 1053 GNUNET_CONFIGURATION_set_value_string(cfg,
1051 GNUNET_assert (0 < ss->n_instances); 1054 ss->sname,
1052 i = ss->instances[ss->n_instances - 1]; 1055 "UNIXPATH",
1053 } 1056 i->unix_sock);
1054 GNUNET_CONFIGURATION_iterate_section_values (ss->cfg, 1057 GNUNET_CONFIGURATION_set_value_string(cfg, ss->sname, "PORT", i->port_str);
1055 ss->sname,
1056 &cfg_copy_iterator,
1057 cfg);
1058 GNUNET_CONFIGURATION_set_value_string (cfg,
1059 ss->sname,
1060 "UNIXPATH",
1061 i->unix_sock);
1062 GNUNET_CONFIGURATION_set_value_string (cfg, ss->sname, "PORT", i->port_str);
1063 return i; 1058 return i;
1064} 1059}
1065 1060
@@ -1084,10 +1079,10 @@ associate_shared_service (struct GNUNET_TESTING_System *system,
1084 * be incomplete and should not be used there upon 1079 * be incomplete and should not be used there upon
1085 */ 1080 */
1086static int 1081static int
1087GNUNET_TESTING_configuration_create_ (struct GNUNET_TESTING_System *system, 1082GNUNET_TESTING_configuration_create_(struct GNUNET_TESTING_System *system,
1088 struct GNUNET_CONFIGURATION_Handle *cfg, 1083 struct GNUNET_CONFIGURATION_Handle *cfg,
1089 uint16_t **ports, 1084 uint16_t **ports,
1090 unsigned int *nports) 1085 unsigned int *nports)
1091{ 1086{
1092 struct UpdateContext uc; 1087 struct UpdateContext uc;
1093 char *default_config; 1088 char *default_config;
@@ -1097,35 +1092,35 @@ GNUNET_TESTING_configuration_create_ (struct GNUNET_TESTING_System *system,
1097 uc.status = GNUNET_OK; 1092 uc.status = GNUNET_OK;
1098 uc.ports = NULL; 1093 uc.ports = NULL;
1099 uc.nports = 0; 1094 uc.nports = 0;
1100 GNUNET_asprintf (&uc.gnunet_home, 1095 GNUNET_asprintf(&uc.gnunet_home,
1101 "%s/%u", 1096 "%s/%u",
1102 system->tmppath, 1097 system->tmppath,
1103 system->path_counter++); 1098 system->path_counter++);
1104 GNUNET_asprintf (&default_config, "%s/config", uc.gnunet_home); 1099 GNUNET_asprintf(&default_config, "%s/config", uc.gnunet_home);
1105 GNUNET_CONFIGURATION_set_value_string (cfg, 1100 GNUNET_CONFIGURATION_set_value_string(cfg,
1106 "PATHS", 1101 "PATHS",
1107 "DEFAULTCONFIG", 1102 "DEFAULTCONFIG",
1108 default_config); 1103 default_config);
1109 GNUNET_CONFIGURATION_set_value_string (cfg, "arm", "CONFIG", default_config); 1104 GNUNET_CONFIGURATION_set_value_string(cfg, "arm", "CONFIG", default_config);
1110 GNUNET_free (default_config); 1105 GNUNET_free(default_config);
1111 GNUNET_CONFIGURATION_set_value_string (cfg, 1106 GNUNET_CONFIGURATION_set_value_string(cfg,
1112 "PATHS", 1107 "PATHS",
1113 "GNUNET_HOME", 1108 "GNUNET_HOME",
1114 uc.gnunet_home); 1109 uc.gnunet_home);
1115 /* make PORTs and UNIXPATHs unique */ 1110 /* make PORTs and UNIXPATHs unique */
1116 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc); 1111 GNUNET_CONFIGURATION_iterate(cfg, &update_config, &uc);
1117 /* allow connections to services from system trusted_ip host */ 1112 /* allow connections to services from system trusted_ip host */
1118 GNUNET_CONFIGURATION_iterate_sections (cfg, &update_config_sections, &uc); 1113 GNUNET_CONFIGURATION_iterate_sections(cfg, &update_config_sections, &uc);
1119 /* enable loopback-based connections between peers */ 1114 /* enable loopback-based connections between peers */
1120 GNUNET_CONFIGURATION_set_value_string (cfg, "nat", "USE_LOCALADDR", "YES"); 1115 GNUNET_CONFIGURATION_set_value_string(cfg, "nat", "USE_LOCALADDR", "YES");
1121 GNUNET_free (uc.gnunet_home); 1116 GNUNET_free(uc.gnunet_home);
1122 if ((NULL != ports) && (NULL != nports)) 1117 if ((NULL != ports) && (NULL != nports))
1123 { 1118 {
1124 *ports = uc.ports; 1119 *ports = uc.ports;
1125 *nports = uc.nports; 1120 *nports = uc.nports;
1126 } 1121 }
1127 else 1122 else
1128 GNUNET_free_non_null (uc.ports); 1123 GNUNET_free_non_null(uc.ports);
1129 return uc.status; 1124 return uc.status;
1130} 1125}
1131 1126
@@ -1147,10 +1142,10 @@ GNUNET_TESTING_configuration_create_ (struct GNUNET_TESTING_System *system,
1147 * be incomplete and should not be used there upon 1142 * be incomplete and should not be used there upon
1148 */ 1143 */
1149int 1144int
1150GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system, 1145GNUNET_TESTING_configuration_create(struct GNUNET_TESTING_System *system,
1151 struct GNUNET_CONFIGURATION_Handle *cfg) 1146 struct GNUNET_CONFIGURATION_Handle *cfg)
1152{ 1147{
1153 return GNUNET_TESTING_configuration_create_ (system, cfg, NULL, NULL); 1148 return GNUNET_TESTING_configuration_create_(system, cfg, NULL, NULL);
1154} 1149}
1155 1150
1156 1151
@@ -1168,11 +1163,11 @@ GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
1168 * @return handle to the peer, NULL on error 1163 * @return handle to the peer, NULL on error
1169 */ 1164 */
1170struct GNUNET_TESTING_Peer * 1165struct GNUNET_TESTING_Peer *
1171GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system, 1166GNUNET_TESTING_peer_configure(struct GNUNET_TESTING_System *system,
1172 struct GNUNET_CONFIGURATION_Handle *cfg, 1167 struct GNUNET_CONFIGURATION_Handle *cfg,
1173 uint32_t key_number, 1168 uint32_t key_number,
1174 struct GNUNET_PeerIdentity *id, 1169 struct GNUNET_PeerIdentity *id,
1175 char **emsg) 1170 char **emsg)
1176{ 1171{
1177 struct GNUNET_TESTING_Peer *peer; 1172 struct GNUNET_TESTING_Peer *peer;
1178 struct GNUNET_DISK_FileHandle *fd; 1173 struct GNUNET_DISK_FileHandle *fd;
@@ -1193,141 +1188,141 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
1193 if (NULL != emsg) 1188 if (NULL != emsg)
1194 *emsg = NULL; 1189 *emsg = NULL;
1195 if (key_number >= system->total_hostkeys) 1190 if (key_number >= system->total_hostkeys)
1196 { 1191 {
1197 GNUNET_asprintf ( 1192 GNUNET_asprintf(
1198 &emsg_, 1193 &emsg_,
1199 _ ( 1194 _(
1200 "You attempted to create a testbed with more than %u hosts. Please precompute more hostkeys first.\n"), 1195 "You attempted to create a testbed with more than %u hosts. Please precompute more hostkeys first.\n"),
1201 (unsigned int) system->total_hostkeys); 1196 (unsigned int)system->total_hostkeys);
1202 goto err_ret; 1197 goto err_ret;
1203 } 1198 }
1204 pk = NULL; 1199 pk = NULL;
1205 if ((NULL != id) && 1200 if ((NULL != id) &&
1206 (NULL == (pk = GNUNET_TESTING_hostkey_get (system, key_number, id)))) 1201 (NULL == (pk = GNUNET_TESTING_hostkey_get(system, key_number, id))))
1207 { 1202 {
1208 GNUNET_asprintf (&emsg_, 1203 GNUNET_asprintf(&emsg_,
1209 _ ("Failed to initialize hostkey for peer %u\n"), 1204 _("Failed to initialize hostkey for peer %u\n"),
1210 (unsigned int) key_number); 1205 (unsigned int)key_number);
1211 goto err_ret; 1206 goto err_ret;
1212 } 1207 }
1213 if (NULL != pk) 1208 if (NULL != pk)
1214 GNUNET_free (pk); 1209 GNUNET_free(pk);
1215 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, "PEER", "PRIVATE_KEY")) 1210 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value(cfg, "PEER", "PRIVATE_KEY"))
1216 { 1211 {
1217 GNUNET_asprintf ( 1212 GNUNET_asprintf(
1218 &emsg_, 1213 &emsg_,
1219 _ ("PRIVATE_KEY option in PEER section missing in configuration\n")); 1214 _("PRIVATE_KEY option in PEER section missing in configuration\n"));
1220 goto err_ret; 1215 goto err_ret;
1221 } 1216 }
1222 /* Remove sections for shared services */ 1217 /* Remove sections for shared services */
1223 for (cnt = 0; cnt < system->n_shared_services; cnt++) 1218 for (cnt = 0; cnt < system->n_shared_services; cnt++)
1224 { 1219 {
1225 ss = system->shared_services[cnt]; 1220 ss = system->shared_services[cnt];
1226 GNUNET_CONFIGURATION_remove_section (cfg, ss->sname); 1221 GNUNET_CONFIGURATION_remove_section(cfg, ss->sname);
1227 } 1222 }
1228 if (GNUNET_OK != 1223 if (GNUNET_OK !=
1229 GNUNET_TESTING_configuration_create_ (system, cfg, &ports, &nports)) 1224 GNUNET_TESTING_configuration_create_(system, cfg, &ports, &nports))
1230 { 1225 {
1231 GNUNET_asprintf (&emsg_, 1226 GNUNET_asprintf(&emsg_,
1232 _ ("Failed to create configuration for peer " 1227 _("Failed to create configuration for peer "
1233 "(not enough free ports?)\n")); 1228 "(not enough free ports?)\n"));
1234 goto err_ret; 1229 goto err_ret;
1235 } 1230 }
1236 GNUNET_assert (GNUNET_OK == 1231 GNUNET_assert(GNUNET_OK ==
1237 GNUNET_CONFIGURATION_get_value_filename (cfg, 1232 GNUNET_CONFIGURATION_get_value_filename(cfg,
1238 "PEER", 1233 "PEER",
1239 "PRIVATE_KEY", 1234 "PRIVATE_KEY",
1240 &hostkey_filename)); 1235 &hostkey_filename));
1241 fd = GNUNET_DISK_file_open (hostkey_filename, 1236 fd = GNUNET_DISK_file_open(hostkey_filename,
1242 GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_WRITE, 1237 GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_WRITE,
1243 GNUNET_DISK_PERM_USER_READ | 1238 GNUNET_DISK_PERM_USER_READ |
1244 GNUNET_DISK_PERM_USER_WRITE); 1239 GNUNET_DISK_PERM_USER_WRITE);
1245 if (NULL == fd) 1240 if (NULL == fd)
1246 { 1241 {
1247 GNUNET_asprintf (&emsg_, 1242 GNUNET_asprintf(&emsg_,
1248 _ ("Cannot open hostkey file `%s': %s\n"), 1243 _("Cannot open hostkey file `%s': %s\n"),
1249 hostkey_filename, 1244 hostkey_filename,
1250 strerror (errno)); 1245 strerror(errno));
1251 GNUNET_free (hostkey_filename); 1246 GNUNET_free(hostkey_filename);
1252 goto err_ret; 1247 goto err_ret;
1253 } 1248 }
1254 GNUNET_free (hostkey_filename); 1249 GNUNET_free(hostkey_filename);
1255 if (GNUNET_TESTING_HOSTKEYFILESIZE != 1250 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
1256 GNUNET_DISK_file_write (fd, 1251 GNUNET_DISK_file_write(fd,
1257 system->hostkeys_data + 1252 system->hostkeys_data +
1258 (key_number * GNUNET_TESTING_HOSTKEYFILESIZE), 1253 (key_number * GNUNET_TESTING_HOSTKEYFILESIZE),
1259 GNUNET_TESTING_HOSTKEYFILESIZE)) 1254 GNUNET_TESTING_HOSTKEYFILESIZE))
1260 { 1255 {
1261 GNUNET_asprintf (&emsg_, 1256 GNUNET_asprintf(&emsg_,
1262 _ ("Failed to write hostkey file for peer %u: %s\n"), 1257 _("Failed to write hostkey file for peer %u: %s\n"),
1263 (unsigned int) key_number, 1258 (unsigned int)key_number,
1264 strerror (errno)); 1259 strerror(errno));
1265 GNUNET_DISK_file_close (fd); 1260 GNUNET_DISK_file_close(fd);
1266 goto err_ret; 1261 goto err_ret;
1267 } 1262 }
1268 GNUNET_DISK_file_close (fd); 1263 GNUNET_DISK_file_close(fd);
1269 ss_instances = GNUNET_malloc (sizeof (struct SharedServiceInstance *) * 1264 ss_instances = GNUNET_malloc(sizeof(struct SharedServiceInstance *) *
1270 system->n_shared_services); 1265 system->n_shared_services);
1271 for (cnt = 0; cnt < system->n_shared_services; cnt++) 1266 for (cnt = 0; cnt < system->n_shared_services; cnt++)
1272 {
1273 ss = system->shared_services[cnt];
1274 ss_instances[cnt] = associate_shared_service (system, ss, cfg);
1275 if (NULL == ss_instances[cnt])
1276 { 1267 {
1277 emsg_ = GNUNET_strdup ("FIXME"); 1268 ss = system->shared_services[cnt];
1269 ss_instances[cnt] = associate_shared_service(system, ss, cfg);
1270 if (NULL == ss_instances[cnt])
1271 {
1272 emsg_ = GNUNET_strdup("FIXME");
1273 goto err_ret;
1274 }
1275 }
1276 GNUNET_assert(GNUNET_OK ==
1277 GNUNET_CONFIGURATION_get_value_filename(cfg,
1278 "PATHS",
1279 "DEFAULTCONFIG",
1280 &config_filename));
1281 if (GNUNET_OK != GNUNET_CONFIGURATION_write(cfg, config_filename))
1282 {
1283 GNUNET_asprintf(&emsg_,
1284 _(
1285 "Failed to write configuration file `%s' for peer %u: %s\n"),
1286 config_filename,
1287 (unsigned int)key_number,
1288 strerror(errno));
1289 GNUNET_free(config_filename);
1278 goto err_ret; 1290 goto err_ret;
1279 } 1291 }
1280 } 1292 peer = GNUNET_new(struct GNUNET_TESTING_Peer);
1281 GNUNET_assert (GNUNET_OK ==
1282 GNUNET_CONFIGURATION_get_value_filename (cfg,
1283 "PATHS",
1284 "DEFAULTCONFIG",
1285 &config_filename));
1286 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config_filename))
1287 {
1288 GNUNET_asprintf (&emsg_,
1289 _ (
1290 "Failed to write configuration file `%s' for peer %u: %s\n"),
1291 config_filename,
1292 (unsigned int) key_number,
1293 strerror (errno));
1294 GNUNET_free (config_filename);
1295 goto err_ret;
1296 }
1297 peer = GNUNET_new (struct GNUNET_TESTING_Peer);
1298 peer->ss_instances = ss_instances; 1293 peer->ss_instances = ss_instances;
1299 peer->cfgfile = config_filename; /* Free in peer_destroy */ 1294 peer->cfgfile = config_filename; /* Free in peer_destroy */
1300 peer->cfg = GNUNET_CONFIGURATION_dup (cfg); 1295 peer->cfg = GNUNET_CONFIGURATION_dup(cfg);
1301 libexec_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm"); 1296 libexec_binary = GNUNET_OS_get_libexec_binary_path("gnunet-service-arm");
1302 if (GNUNET_SYSERR == 1297 if (GNUNET_SYSERR ==
1303 GNUNET_CONFIGURATION_get_value_string (cfg, 1298 GNUNET_CONFIGURATION_get_value_string(cfg,
1304 "arm", 1299 "arm",
1305 "PREFIX", 1300 "PREFIX",
1306 &peer->main_binary)) 1301 &peer->main_binary))
1307 { 1302 {
1308 /* No prefix */ 1303 /* No prefix */
1309 GNUNET_asprintf (&peer->main_binary, "%s", libexec_binary); 1304 GNUNET_asprintf(&peer->main_binary, "%s", libexec_binary);
1310 peer->args = GNUNET_strdup (""); 1305 peer->args = GNUNET_strdup("");
1311 } 1306 }
1312 else 1307 else
1313 { 1308 {
1314 peer->args = GNUNET_strdup (libexec_binary); 1309 peer->args = GNUNET_strdup(libexec_binary);
1315 } 1310 }
1316 peer->system = system; 1311 peer->system = system;
1317 peer->key_number = key_number; 1312 peer->key_number = key_number;
1318 GNUNET_free (libexec_binary); 1313 GNUNET_free(libexec_binary);
1319 peer->ports = ports; /* Free in peer_destroy */ 1314 peer->ports = ports; /* Free in peer_destroy */
1320 peer->nports = nports; 1315 peer->nports = nports;
1321 return peer; 1316 return peer;
1322 1317
1323err_ret: 1318err_ret:
1324 GNUNET_free_non_null (ss_instances); 1319 GNUNET_free_non_null(ss_instances);
1325 GNUNET_free_non_null (ports); 1320 GNUNET_free_non_null(ports);
1326 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_); 1321 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s", emsg_);
1327 if (NULL != emsg) 1322 if (NULL != emsg)
1328 *emsg = emsg_; 1323 *emsg = emsg_;
1329 else 1324 else
1330 GNUNET_free (emsg_); 1325 GNUNET_free(emsg_);
1331 return NULL; 1326 return NULL;
1332} 1327}
1333 1328
@@ -1339,18 +1334,18 @@ err_ret:
1339 * @param id identifier for the daemon, will be set 1334 * @param id identifier for the daemon, will be set
1340 */ 1335 */
1341void 1336void
1342GNUNET_TESTING_peer_get_identity (struct GNUNET_TESTING_Peer *peer, 1337GNUNET_TESTING_peer_get_identity(struct GNUNET_TESTING_Peer *peer,
1343 struct GNUNET_PeerIdentity *id) 1338 struct GNUNET_PeerIdentity *id)
1344{ 1339{
1345 if (NULL != peer->id) 1340 if (NULL != peer->id)
1346 { 1341 {
1347 GNUNET_memcpy (id, peer->id, sizeof (struct GNUNET_PeerIdentity)); 1342 GNUNET_memcpy(id, peer->id, sizeof(struct GNUNET_PeerIdentity));
1348 return; 1343 return;
1349 } 1344 }
1350 peer->id = GNUNET_new (struct GNUNET_PeerIdentity); 1345 peer->id = GNUNET_new(struct GNUNET_PeerIdentity);
1351 GNUNET_free ( 1346 GNUNET_free(
1352 GNUNET_TESTING_hostkey_get (peer->system, peer->key_number, peer->id)); 1347 GNUNET_TESTING_hostkey_get(peer->system, peer->key_number, peer->id));
1353 GNUNET_memcpy (id, peer->id, sizeof (struct GNUNET_PeerIdentity)); 1348 GNUNET_memcpy(id, peer->id, sizeof(struct GNUNET_PeerIdentity));
1354} 1349}
1355 1350
1356 1351
@@ -1361,44 +1356,44 @@ GNUNET_TESTING_peer_get_identity (struct GNUNET_TESTING_Peer *peer,
1361 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (i.e. peer already running) 1356 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (i.e. peer already running)
1362 */ 1357 */
1363int 1358int
1364GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer) 1359GNUNET_TESTING_peer_start(struct GNUNET_TESTING_Peer *peer)
1365{ 1360{
1366 struct SharedServiceInstance *i; 1361 struct SharedServiceInstance *i;
1367 unsigned int cnt; 1362 unsigned int cnt;
1368 1363
1369 if (NULL != peer->main_process) 1364 if (NULL != peer->main_process)
1370 { 1365 {
1371 GNUNET_break (0); 1366 GNUNET_break(0);
1372 return GNUNET_SYSERR;
1373 }
1374 GNUNET_assert (NULL != peer->cfgfile);
1375 for (cnt = 0; cnt < peer->system->n_shared_services; cnt++)
1376 {
1377 i = peer->ss_instances[cnt];
1378 if ((0 == i->n_refs) &&
1379 (GNUNET_SYSERR == start_shared_service_instance (i)))
1380 return GNUNET_SYSERR; 1367 return GNUNET_SYSERR;
1381 i->n_refs++; 1368 }
1382 } 1369 GNUNET_assert(NULL != peer->cfgfile);
1370 for (cnt = 0; cnt < peer->system->n_shared_services; cnt++)
1371 {
1372 i = peer->ss_instances[cnt];
1373 if ((0 == i->n_refs) &&
1374 (GNUNET_SYSERR == start_shared_service_instance(i)))
1375 return GNUNET_SYSERR;
1376 i->n_refs++;
1377 }
1383 peer->main_binary = 1378 peer->main_binary =
1384 GNUNET_CONFIGURATION_expand_dollar (peer->cfg, peer->main_binary); 1379 GNUNET_CONFIGURATION_expand_dollar(peer->cfg, peer->main_binary);
1385 peer->main_process = 1380 peer->main_process =
1386 GNUNET_OS_start_process_s (PIPE_CONTROL, 1381 GNUNET_OS_start_process_s(PIPE_CONTROL,
1387 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 1382 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
1388 NULL, 1383 NULL,
1389 peer->main_binary, 1384 peer->main_binary,
1390 peer->args, 1385 peer->args,
1391 "-c", 1386 "-c",
1392 peer->cfgfile, 1387 peer->cfgfile,
1393 NULL); 1388 NULL);
1394 if (NULL == peer->main_process) 1389 if (NULL == peer->main_process)
1395 { 1390 {
1396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1391 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1397 _ ("Failed to start `%s': %s\n"), 1392 _("Failed to start `%s': %s\n"),
1398 peer->main_binary, 1393 peer->main_binary,
1399 strerror (errno)); 1394 strerror(errno));
1400 return GNUNET_SYSERR; 1395 return GNUNET_SYSERR;
1401 } 1396 }
1402 return GNUNET_OK; 1397 return GNUNET_OK;
1403} 1398}
1404 1399
@@ -1411,26 +1406,26 @@ GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer)
1411 * or upon any error while sending SIGTERM 1406 * or upon any error while sending SIGTERM
1412 */ 1407 */
1413int 1408int
1414GNUNET_TESTING_peer_kill (struct GNUNET_TESTING_Peer *peer) 1409GNUNET_TESTING_peer_kill(struct GNUNET_TESTING_Peer *peer)
1415{ 1410{
1416 struct SharedServiceInstance *i; 1411 struct SharedServiceInstance *i;
1417 unsigned int cnt; 1412 unsigned int cnt;
1418 1413
1419 if (NULL == peer->main_process) 1414 if (NULL == peer->main_process)
1420 { 1415 {
1421 GNUNET_break (0); 1416 GNUNET_break(0);
1422 return GNUNET_SYSERR; 1417 return GNUNET_SYSERR;
1423 } 1418 }
1424 if (0 != GNUNET_OS_process_kill (peer->main_process, GNUNET_TERM_SIG)) 1419 if (0 != GNUNET_OS_process_kill(peer->main_process, GNUNET_TERM_SIG))
1425 return GNUNET_SYSERR; 1420 return GNUNET_SYSERR;
1426 for (cnt = 0; cnt < peer->system->n_shared_services; cnt++) 1421 for (cnt = 0; cnt < peer->system->n_shared_services; cnt++)
1427 { 1422 {
1428 i = peer->ss_instances[cnt]; 1423 i = peer->ss_instances[cnt];
1429 GNUNET_assert (0 != i->n_refs); 1424 GNUNET_assert(0 != i->n_refs);
1430 i->n_refs--; 1425 i->n_refs--;
1431 if (0 == i->n_refs) 1426 if (0 == i->n_refs)
1432 stop_shared_service_instance (i); 1427 stop_shared_service_instance(i);
1433 } 1428 }
1434 return GNUNET_OK; 1429 return GNUNET_OK;
1435} 1430}
1436 1431
@@ -1443,17 +1438,17 @@ GNUNET_TESTING_peer_kill (struct GNUNET_TESTING_Peer *peer)
1443 * or upon any error while waiting 1438 * or upon any error while waiting
1444 */ 1439 */
1445int 1440int
1446GNUNET_TESTING_peer_wait (struct GNUNET_TESTING_Peer *peer) 1441GNUNET_TESTING_peer_wait(struct GNUNET_TESTING_Peer *peer)
1447{ 1442{
1448 int ret; 1443 int ret;
1449 1444
1450 if (NULL == peer->main_process) 1445 if (NULL == peer->main_process)
1451 { 1446 {
1452 GNUNET_break (0); 1447 GNUNET_break(0);
1453 return GNUNET_SYSERR; 1448 return GNUNET_SYSERR;
1454 } 1449 }
1455 ret = GNUNET_OS_process_wait (peer->main_process); 1450 ret = GNUNET_OS_process_wait(peer->main_process);
1456 GNUNET_OS_process_destroy (peer->main_process); 1451 GNUNET_OS_process_destroy(peer->main_process);
1457 peer->main_process = NULL; 1452 peer->main_process = NULL;
1458 return ret; 1453 return ret;
1459} 1454}
@@ -1466,11 +1461,11 @@ GNUNET_TESTING_peer_wait (struct GNUNET_TESTING_Peer *peer)
1466 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1461 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1467 */ 1462 */
1468int 1463int
1469GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer) 1464GNUNET_TESTING_peer_stop(struct GNUNET_TESTING_Peer *peer)
1470{ 1465{
1471 if (GNUNET_SYSERR == GNUNET_TESTING_peer_kill (peer)) 1466 if (GNUNET_SYSERR == GNUNET_TESTING_peer_kill(peer))
1472 return GNUNET_SYSERR; 1467 return GNUNET_SYSERR;
1473 if (GNUNET_SYSERR == GNUNET_TESTING_peer_wait (peer)) 1468 if (GNUNET_SYSERR == GNUNET_TESTING_peer_wait(peer))
1474 return GNUNET_SYSERR; 1469 return GNUNET_SYSERR;
1475 return GNUNET_OK; 1470 return GNUNET_OK;
1476} 1471}
@@ -1484,24 +1479,24 @@ GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer)
1484 * #GNUNET_SYSERR on error. 1479 * #GNUNET_SYSERR on error.
1485 */ 1480 */
1486static void 1481static void
1487disconn_status (void *cls, int connected) 1482disconn_status(void *cls, int connected)
1488{ 1483{
1489 struct GNUNET_TESTING_Peer *peer = cls; 1484 struct GNUNET_TESTING_Peer *peer = cls;
1490 1485
1491 if (GNUNET_SYSERR == connected) 1486 if (GNUNET_SYSERR == connected)
1492 { 1487 {
1493 peer->cb (peer->cb_cls, peer, connected); 1488 peer->cb(peer->cb_cls, peer, connected);
1494 return; 1489 return;
1495 } 1490 }
1496 if (GNUNET_YES == connected) 1491 if (GNUNET_YES == connected)
1497 { 1492 {
1498 GNUNET_break (GNUNET_OK == GNUNET_TESTING_peer_kill (peer)); 1493 GNUNET_break(GNUNET_OK == GNUNET_TESTING_peer_kill(peer));
1499 return; 1494 return;
1500 } 1495 }
1501 GNUNET_break (GNUNET_OK == GNUNET_TESTING_peer_wait (peer)); 1496 GNUNET_break(GNUNET_OK == GNUNET_TESTING_peer_wait(peer));
1502 GNUNET_ARM_disconnect (peer->ah); 1497 GNUNET_ARM_disconnect(peer->ah);
1503 peer->ah = NULL; 1498 peer->ah = NULL;
1504 peer->cb (peer->cb_cls, peer, GNUNET_YES); 1499 peer->cb(peer->cb_cls, peer, GNUNET_YES);
1505} 1500}
1506 1501
1507 1502
@@ -1517,13 +1512,13 @@ disconn_status (void *cls, int connected)
1517 * upon any error. 1512 * upon any error.
1518 */ 1513 */
1519int 1514int
1520GNUNET_TESTING_peer_stop_async (struct GNUNET_TESTING_Peer *peer, 1515GNUNET_TESTING_peer_stop_async(struct GNUNET_TESTING_Peer *peer,
1521 GNUNET_TESTING_PeerStopCallback cb, 1516 GNUNET_TESTING_PeerStopCallback cb,
1522 void *cb_cls) 1517 void *cb_cls)
1523{ 1518{
1524 if (NULL == peer->main_process) 1519 if (NULL == peer->main_process)
1525 return GNUNET_SYSERR; 1520 return GNUNET_SYSERR;
1526 peer->ah = GNUNET_ARM_connect (peer->cfg, &disconn_status, peer); 1521 peer->ah = GNUNET_ARM_connect(peer->cfg, &disconn_status, peer);
1527 if (NULL == peer->ah) 1522 if (NULL == peer->ah)
1528 return GNUNET_SYSERR; 1523 return GNUNET_SYSERR;
1529 peer->cb = cb; 1524 peer->cb = cb;
@@ -1542,10 +1537,10 @@ GNUNET_TESTING_peer_stop_async (struct GNUNET_TESTING_Peer *peer,
1542 * before. 1537 * before.
1543 */ 1538 */
1544void 1539void
1545GNUNET_TESTING_peer_stop_async_cancel (struct GNUNET_TESTING_Peer *peer) 1540GNUNET_TESTING_peer_stop_async_cancel(struct GNUNET_TESTING_Peer *peer)
1546{ 1541{
1547 GNUNET_assert (NULL != peer->ah); 1542 GNUNET_assert(NULL != peer->ah);
1548 GNUNET_ARM_disconnect (peer->ah); 1543 GNUNET_ARM_disconnect(peer->ah);
1549 peer->ah = NULL; 1544 peer->ah = NULL;
1550} 1545}
1551 1546
@@ -1558,28 +1553,28 @@ GNUNET_TESTING_peer_stop_async_cancel (struct GNUNET_TESTING_Peer *peer)
1558 * @param peer peer to destroy 1553 * @param peer peer to destroy
1559 */ 1554 */
1560void 1555void
1561GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer) 1556GNUNET_TESTING_peer_destroy(struct GNUNET_TESTING_Peer *peer)
1562{ 1557{
1563 unsigned int cnt; 1558 unsigned int cnt;
1564 1559
1565 if (NULL != peer->main_process) 1560 if (NULL != peer->main_process)
1566 GNUNET_TESTING_peer_stop (peer); 1561 GNUNET_TESTING_peer_stop(peer);
1567 if (NULL != peer->ah) 1562 if (NULL != peer->ah)
1568 GNUNET_ARM_disconnect (peer->ah); 1563 GNUNET_ARM_disconnect(peer->ah);
1569 GNUNET_free (peer->cfgfile); 1564 GNUNET_free(peer->cfgfile);
1570 if (NULL != peer->cfg) 1565 if (NULL != peer->cfg)
1571 GNUNET_CONFIGURATION_destroy (peer->cfg); 1566 GNUNET_CONFIGURATION_destroy(peer->cfg);
1572 GNUNET_free (peer->main_binary); 1567 GNUNET_free(peer->main_binary);
1573 GNUNET_free (peer->args); 1568 GNUNET_free(peer->args);
1574 GNUNET_free_non_null (peer->id); 1569 GNUNET_free_non_null(peer->id);
1575 GNUNET_free_non_null (peer->ss_instances); 1570 GNUNET_free_non_null(peer->ss_instances);
1576 if (NULL != peer->ports) 1571 if (NULL != peer->ports)
1577 { 1572 {
1578 for (cnt = 0; cnt < peer->nports; cnt++) 1573 for (cnt = 0; cnt < peer->nports; cnt++)
1579 GNUNET_TESTING_release_port (peer->system, peer->ports[cnt]); 1574 GNUNET_TESTING_release_port(peer->system, peer->ports[cnt]);
1580 GNUNET_free (peer->ports); 1575 GNUNET_free(peer->ports);
1581 } 1576 }
1582 GNUNET_free (peer); 1577 GNUNET_free(peer);
1583} 1578}
1584 1579
1585 1580
@@ -1600,20 +1595,19 @@ GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer)
1600 * @return 0 on success, 1 on error 1595 * @return 0 on success, 1 on error
1601 */ 1596 */
1602int 1597int
1603GNUNET_TESTING_peer_run (const char *testdir, 1598GNUNET_TESTING_peer_run(const char *testdir,
1604 const char *cfgfilename, 1599 const char *cfgfilename,
1605 GNUNET_TESTING_TestMain tm, 1600 GNUNET_TESTING_TestMain tm,
1606 void *tm_cls) 1601 void *tm_cls)
1607{ 1602{
1608 return GNUNET_TESTING_service_run (testdir, "arm", cfgfilename, tm, tm_cls); 1603 return GNUNET_TESTING_service_run(testdir, "arm", cfgfilename, tm, tm_cls);
1609} 1604}
1610 1605
1611 1606
1612/** 1607/**
1613 * Structure for holding service data 1608 * Structure for holding service data
1614 */ 1609 */
1615struct ServiceContext 1610struct ServiceContext {
1616{
1617 /** 1611 /**
1618 * The configuration of the peer in which the service is run 1612 * The configuration of the peer in which the service is run
1619 */ 1613 */
@@ -1642,11 +1636,11 @@ struct ServiceContext
1642 * @param cls the ServiceContext 1636 * @param cls the ServiceContext
1643 */ 1637 */
1644static void 1638static void
1645service_run_main (void *cls) 1639service_run_main(void *cls)
1646{ 1640{
1647 struct ServiceContext *sc = cls; 1641 struct ServiceContext *sc = cls;
1648 1642
1649 sc->tm (sc->tm_cls, sc->cfg, sc->peer); 1643 sc->tm(sc->tm_cls, sc->cfg, sc->peer);
1650} 1644}
1651 1645
1652 1646
@@ -1672,11 +1666,11 @@ service_run_main (void *cls)
1672 * @return 0 on success, 1 on error 1666 * @return 0 on success, 1 on error
1673 */ 1667 */
1674int 1668int
1675GNUNET_TESTING_service_run (const char *testdir, 1669GNUNET_TESTING_service_run(const char *testdir,
1676 const char *service_name, 1670 const char *service_name,
1677 const char *cfgfilename, 1671 const char *cfgfilename,
1678 GNUNET_TESTING_TestMain tm, 1672 GNUNET_TESTING_TestMain tm,
1679 void *tm_cls) 1673 void *tm_cls)
1680{ 1674{
1681 struct ServiceContext sc; 1675 struct ServiceContext sc;
1682 struct GNUNET_TESTING_System *system; 1676 struct GNUNET_TESTING_System *system;
@@ -1685,70 +1679,70 @@ GNUNET_TESTING_service_run (const char *testdir,
1685 char *binary; 1679 char *binary;
1686 char *libexec_binary; 1680 char *libexec_binary;
1687 1681
1688 GNUNET_log_setup (testdir, "WARNING", NULL); 1682 GNUNET_log_setup(testdir, "WARNING", NULL);
1689 system = GNUNET_TESTING_system_create (testdir, "127.0.0.1", NULL, NULL); 1683 system = GNUNET_TESTING_system_create(testdir, "127.0.0.1", NULL, NULL);
1690 if (NULL == system) 1684 if (NULL == system)
1691 return 1; 1685 return 1;
1692 cfg = GNUNET_CONFIGURATION_create (); 1686 cfg = GNUNET_CONFIGURATION_create();
1693 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfgfilename)) 1687 if (GNUNET_OK != GNUNET_CONFIGURATION_load(cfg, cfgfilename))
1694 { 1688 {
1695 LOG (GNUNET_ERROR_TYPE_ERROR, 1689 LOG(GNUNET_ERROR_TYPE_ERROR,
1696 _ ("Failed to load configuration from %s\n"), 1690 _("Failed to load configuration from %s\n"),
1697 cfgfilename); 1691 cfgfilename);
1698 GNUNET_CONFIGURATION_destroy (cfg); 1692 GNUNET_CONFIGURATION_destroy(cfg);
1699 GNUNET_TESTING_system_destroy (system, GNUNET_YES); 1693 GNUNET_TESTING_system_destroy(system, GNUNET_YES);
1700 return 1; 1694 return 1;
1701 } 1695 }
1702 peer = GNUNET_TESTING_peer_configure (system, cfg, 0, NULL, NULL); 1696 peer = GNUNET_TESTING_peer_configure(system, cfg, 0, NULL, NULL);
1703 if (NULL == peer) 1697 if (NULL == peer)
1704 { 1698 {
1705 GNUNET_CONFIGURATION_destroy (cfg); 1699 GNUNET_CONFIGURATION_destroy(cfg);
1706 hostkeys_unload (system); 1700 hostkeys_unload(system);
1707 GNUNET_TESTING_system_destroy (system, GNUNET_YES); 1701 GNUNET_TESTING_system_destroy(system, GNUNET_YES);
1708 return 1; 1702 return 1;
1709 } 1703 }
1710 GNUNET_free (peer->main_binary); 1704 GNUNET_free(peer->main_binary);
1711 GNUNET_free (peer->args); 1705 GNUNET_free(peer->args);
1712 GNUNET_asprintf (&binary, "gnunet-service-%s", service_name); 1706 GNUNET_asprintf(&binary, "gnunet-service-%s", service_name);
1713 libexec_binary = GNUNET_OS_get_libexec_binary_path (binary); 1707 libexec_binary = GNUNET_OS_get_libexec_binary_path(binary);
1714 if (GNUNET_SYSERR == 1708 if (GNUNET_SYSERR ==
1715 GNUNET_CONFIGURATION_get_value_string (cfg, 1709 GNUNET_CONFIGURATION_get_value_string(cfg,
1716 service_name, 1710 service_name,
1717 "PREFIX", 1711 "PREFIX",
1718 &peer->main_binary)) 1712 &peer->main_binary))
1719 { 1713 {
1720 /* No prefix */ 1714 /* No prefix */
1721 GNUNET_asprintf (&peer->main_binary, "%s", libexec_binary); 1715 GNUNET_asprintf(&peer->main_binary, "%s", libexec_binary);
1722 peer->args = GNUNET_strdup (""); 1716 peer->args = GNUNET_strdup("");
1723 } 1717 }
1724 else 1718 else
1725 peer->args = GNUNET_strdup (libexec_binary); 1719 peer->args = GNUNET_strdup(libexec_binary);
1726 1720
1727 GNUNET_free (libexec_binary); 1721 GNUNET_free(libexec_binary);
1728 GNUNET_free (binary); 1722 GNUNET_free(binary);
1729 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer)) 1723 if (GNUNET_OK != GNUNET_TESTING_peer_start(peer))
1730 { 1724 {
1731 GNUNET_TESTING_peer_destroy (peer); 1725 GNUNET_TESTING_peer_destroy(peer);
1732 GNUNET_CONFIGURATION_destroy (cfg); 1726 GNUNET_CONFIGURATION_destroy(cfg);
1733 GNUNET_TESTING_system_destroy (system, GNUNET_YES); 1727 GNUNET_TESTING_system_destroy(system, GNUNET_YES);
1734 return 1; 1728 return 1;
1735 } 1729 }
1736 sc.cfg = cfg; 1730 sc.cfg = cfg;
1737 sc.tm = tm; 1731 sc.tm = tm;
1738 sc.tm_cls = tm_cls; 1732 sc.tm_cls = tm_cls;
1739 sc.peer = peer; 1733 sc.peer = peer;
1740 GNUNET_SCHEDULER_run (&service_run_main, &sc); /* Scheduler loop */ 1734 GNUNET_SCHEDULER_run(&service_run_main, &sc); /* Scheduler loop */
1741 if ((NULL != peer->main_process) && 1735 if ((NULL != peer->main_process) &&
1742 (GNUNET_OK != GNUNET_TESTING_peer_stop (peer))) 1736 (GNUNET_OK != GNUNET_TESTING_peer_stop(peer)))
1743 { 1737 {
1744 GNUNET_TESTING_peer_destroy (peer); 1738 GNUNET_TESTING_peer_destroy(peer);
1745 GNUNET_CONFIGURATION_destroy (cfg); 1739 GNUNET_CONFIGURATION_destroy(cfg);
1746 GNUNET_TESTING_system_destroy (system, GNUNET_YES); 1740 GNUNET_TESTING_system_destroy(system, GNUNET_YES);
1747 return 1; 1741 return 1;
1748 } 1742 }
1749 GNUNET_TESTING_peer_destroy (peer); 1743 GNUNET_TESTING_peer_destroy(peer);
1750 GNUNET_CONFIGURATION_destroy (cfg); 1744 GNUNET_CONFIGURATION_destroy(cfg);
1751 GNUNET_TESTING_system_destroy (system, GNUNET_YES); 1745 GNUNET_TESTING_system_destroy(system, GNUNET_YES);
1752 return 0; 1746 return 0;
1753} 1747}
1754 1748
@@ -1767,22 +1761,22 @@ GNUNET_TESTING_service_run (const char *testdir,
1767 * NULL if argv0 has no '_' 1761 * NULL if argv0 has no '_'
1768 */ 1762 */
1769char * 1763char *
1770GNUNET_TESTING_get_testname_from_underscore (const char *argv0) 1764GNUNET_TESTING_get_testname_from_underscore(const char *argv0)
1771{ 1765{
1772 size_t slen = strlen (argv0) + 1; 1766 size_t slen = strlen(argv0) + 1;
1773 char sbuf[slen]; 1767 char sbuf[slen];
1774 char *ret; 1768 char *ret;
1775 char *dot; 1769 char *dot;
1776 1770
1777 GNUNET_memcpy (sbuf, argv0, slen); 1771 GNUNET_memcpy(sbuf, argv0, slen);
1778 ret = strrchr (sbuf, '_'); 1772 ret = strrchr(sbuf, '_');
1779 if (NULL == ret) 1773 if (NULL == ret)
1780 return NULL; 1774 return NULL;
1781 ret++; /* skip underscore */ 1775 ret++; /* skip underscore */
1782 dot = strchr (ret, '.'); 1776 dot = strchr(ret, '.');
1783 if (NULL != dot) 1777 if (NULL != dot)
1784 *dot = '\0'; 1778 *dot = '\0';
1785 return GNUNET_strdup (ret); 1779 return GNUNET_strdup(ret);
1786} 1780}
1787 1781
1788 1782