2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing_group.c
23 * @brief convenience API for writing testcases for GNUnet
24 * @author Christian Grothoff
27 #include "gnunet_arm_service.h"
28 #include "gnunet_testing_lib.h"
30 #define VERBOSE_TESTING GNUNET_NO
32 #define DEBUG_CHURN GNUNET_NO
35 * Lowest port used for GNUnet testing. Should be high enough to not
36 * conflict with other applications running on the hosts but be low
37 * enough to not conflict with client-ports (typically starting around
40 #define LOW_PORT 10000
43 * Highest port used for GNUnet testing. Should be low enough to not
44 * conflict with the port range for "local" ports (client apps; see
45 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
47 #define HIGH_PORT 32000
49 #define MAX_OUTSTANDING_CONNECTIONS 50
51 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
53 #define CONNECT_ATTEMPTS 8
56 * Prototype of a function called whenever two peers would be connected
57 * in a certain topology.
59 typedef int (*GNUNET_TESTING_ConnectionProcessor)
60 (struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second);
63 * Context for handling churning a peer group
68 * Callback used to notify of churning finished
70 GNUNET_TESTING_NotifyCompletion cb;
73 * Closure for callback
78 * Number of peers that still need to be started
80 unsigned int num_to_start;
83 * Number of peers that still need to be stopped
85 unsigned int num_to_stop;
88 * Number of peers that failed to start
90 unsigned int num_failed_start;
93 * Number of peers that failed to stop
95 unsigned int num_failed_stop;
101 * The group of peers being restarted
103 struct GNUNET_TESTING_PeerGroup *peer_group;
106 * How many peers have been restarted thus far
108 unsigned int peers_restarted;
111 * How many peers got an error when restarting
113 unsigned int peers_restart_failed;
116 * The function to call once all peers have been restarted
118 GNUNET_TESTING_NotifyCompletion callback;
121 * Closure for callback function
127 struct CreateTopologyContext
131 * Function to call with number of connections
133 GNUNET_TESTING_NotifyConnections cont;
136 * Closure for connection notification
142 struct PeerConnection
147 struct PeerConnection *next;
150 * Pointer to daemon handle
152 struct GNUNET_TESTING_Daemon *daemon;
158 * Data we keep per peer.
163 * (Initial) configuration of the host.
164 * (initial because clients could change
165 * it and we would not know about those
168 struct GNUNET_CONFIGURATION_Handle *cfg;
171 * Handle for controlling the daemon.
173 struct GNUNET_TESTING_Daemon *daemon;
176 * The peergroup this peer belongs to.
178 struct GNUNET_TESTING_PeerGroup *pg;
181 * Linked list of peer connections (pointers)
183 //struct PeerConnection *connected_peers;
185 * Hash map of allowed peer connections (F2F created topology)
187 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
190 * Hash map of blacklisted peers
192 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
195 * Hash map of peer connections
197 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
200 * Temporary hash map of peer connections
202 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
205 * Temporary variable for topology creation, should be reset before
206 * creating any topology so the count is valid once finished.
213 * Data we keep per host.
223 * Lowest port that we have not yet used
231 * Handle to a group of GNUnet peers.
233 struct GNUNET_TESTING_PeerGroup
238 struct GNUNET_SCHEDULER_Handle *sched;
241 * Configuration template.
243 const struct GNUNET_CONFIGURATION_Handle *cfg;
246 * Function to call on each started daemon.
248 GNUNET_TESTING_NotifyDaemonRunning cb;
256 * Function to call on each topology connection created
258 GNUNET_TESTING_NotifyConnection notify_connection;
261 * Callback for notify_connection
263 void *notify_connection_cls;
266 * NULL-terminated array of information about
269 struct HostData *hosts;
272 * Array of "total" peers.
274 struct PeerData *peers;
277 * Number of peers in this group.
282 * At what time should we fail the peer startup process?
284 struct GNUNET_TIME_Absolute max_timeout;
288 * Convert unique ID to hash code.
290 * @param uid unique ID to convert
291 * @param hash set to uid (extended with zeros)
294 hash_from_uid (uint32_t uid,
295 GNUNET_HashCode *hash)
297 memset (hash, 0, sizeof(GNUNET_HashCode));
298 *((uint32_t*)hash) = uid;
302 * Convert hash code to unique ID.
304 * @param uid unique ID to convert
305 * @param hash set to uid (extended with zeros)
308 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
310 memcpy (uid, hash, sizeof(uint32_t));
315 struct GNUNET_CONFIGURATION_Handle *ret;
317 const char *hostname;
321 struct ConnectContext
323 struct GNUNET_TESTING_Daemon *first;
325 struct GNUNET_TESTING_Daemon *second;
327 struct GNUNET_TESTING_PeerGroup *pg;
331 * Number of connects we are waiting on, allows us to rate limit
334 static int outstanding_connects;
338 * Function to iterate over options. Copies
339 * the options to the target configuration,
340 * updating PORT values as needed.
343 * @param section name of the section
344 * @param option name of the option
345 * @param value value of the option
348 update_config (void *cls,
349 const char *section, const char *option, const char *value)
351 struct UpdateContext *ctx = cls;
355 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
357 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
361 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
363 value = ctx->hostname;
366 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
371 * Create a new configuration using the given configuration
372 * as a template; however, each PORT in the existing cfg
373 * must be renumbered by incrementing "*port". If we run
374 * out of "*port" numbers, return NULL.
376 * @param cfg template configuration
377 * @param port port numbers to use, update to reflect
378 * port numbers that were used
379 * @param hostname hostname of the controlling host, to allow control connections from
381 * @return new configuration, NULL on error
383 static struct GNUNET_CONFIGURATION_Handle *
384 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg, uint16_t * port, const char *hostname)
386 struct UpdateContext uc;
393 uc.ret = GNUNET_CONFIGURATION_create ();
394 uc.hostname = hostname;
396 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
397 if (uc.nport >= HIGH_PORT)
400 GNUNET_CONFIGURATION_destroy (uc.ret);
404 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
406 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
407 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
408 GNUNET_free_non_null(control_host);
409 GNUNET_free(allowed_hosts);
413 /* arm needs to know to allow connections from the host on which it is running,
414 * otherwise gnunet-arm is unable to connect to it in some instances */
415 if (hostname != NULL)
417 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
418 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
419 GNUNET_free(allowed_hosts);
422 *port = (uint16_t) uc.nport;
428 * Add entries to the peers connect list
430 * @param pg the peer group we are working with
431 * @param first index of the first peer
432 * @param second index of the second peer
434 * @return the number of connections added (can be 0, 1 or 2)
435 * technically should only be 0 or 2, but the small price
436 * of iterating over the lists (hashmaps in the future)
437 * for being sure doesn't bother me!
441 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
447 GNUNET_HashCode hash_first;
448 GNUNET_HashCode hash_second;
450 hash_from_uid(first, &hash_first);
451 hash_from_uid(second, &hash_second);
453 add_first = GNUNET_NO;
454 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
456 add_first = GNUNET_YES;
459 add_second = GNUNET_NO;
460 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
462 add_second = GNUNET_YES;
468 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
469 pg->peers[first].num_connections++;
475 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
476 pg->peers[second].num_connections++;
485 * Add entries to the peers allowed connections list
487 * @param pg the peer group we are working with
488 * @param first index of the first peer
489 * @param second index of the second peer
491 * @return the number of connections added (can be 0, 1 or 2)
492 * technically should only be 0 or 2, but the small price
493 * of iterating over the lists (hashmaps in the future)
494 * for being sure doesn't bother me!
498 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
502 struct PeerConnection *first_iter;
503 struct PeerConnection *second_iter;
504 struct PeerConnection *new_first;
505 struct PeerConnection *new_second;
510 GNUNET_HashCode hash_first;
511 GNUNET_HashCode hash_second;
513 hash_from_uid(first, &hash_first);
514 hash_from_uid(second, &hash_second);
516 add_first = GNUNET_NO;
517 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
519 add_first = GNUNET_YES;
522 add_second = GNUNET_NO;
523 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
525 add_second = GNUNET_YES;
528 first_iter = pg->peers[first].connected_peers;
529 while (first_iter != NULL)
531 if (first_iter->daemon == pg->peers[second].daemon)
532 add_first = GNUNET_NO;
533 first_iter = first_iter->next;
536 second_iter = pg->peers[second].connected_peers;
537 add_second = GNUNET_YES;
538 while (second_iter != NULL)
540 if (second_iter->daemon == pg->peers[first].daemon)
541 add_second = GNUNET_NO;
542 second_iter = second_iter->next;
549 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
551 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
552 new_first->daemon = pg->peers[second].daemon;
553 new_first->next = pg->peers[first].connected_peers;
554 pg->peers[first].connected_peers = new_first;
556 pg->peers[first].num_connections++;
562 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
564 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
565 new_second->daemon = pg->peers[first].daemon;
566 new_second->next = pg->peers[second].connected_peers;
567 pg->peers[second].connected_peers = new_second;
568 pg->peers[first].num_connections++;
570 pg->peers[second].num_connections++;
578 * Add entries to the peers blacklisted list
580 * @param pg the peer group we are working with
581 * @param first index of the first peer
582 * @param second index of the second peer
584 * @return the number of connections added (can be 0, 1 or 2)
588 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
593 GNUNET_HashCode hash_first;
594 GNUNET_HashCode hash_second;
596 hash_from_uid(first, &hash_first);
597 hash_from_uid(second, &hash_second);
599 add_first = GNUNET_NO;
600 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
602 add_first = GNUNET_YES;
605 add_second = GNUNET_NO;
606 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
608 add_second = GNUNET_YES;
614 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
615 pg->peers[first].num_connections++;
621 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
622 pg->peers[second].num_connections++;
630 * Remove entries from the peers blacklisted list
632 * @param pg the peer group we are working with
633 * @param first index of the first peer
634 * @param second index of the second peer
636 * @return the number of connections removed (can be 0, 1 or 2)
640 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
645 GNUNET_HashCode hash_first;
646 GNUNET_HashCode hash_second;
648 hash_from_uid(first, &hash_first);
649 hash_from_uid(second, &hash_second);
651 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
652 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
657 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
663 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
671 * Scale free network construction as described in:
673 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
675 * Start with a network of "one" peer, then progressively add
676 * peers up to the total number. At each step, iterate over
677 * all possible peers and connect new peer based on number of
678 * existing connections of the target peer.
680 * @param pg the peer group we are dealing with
681 * @param proc the connection processor to use
683 * @return the number of connections created
686 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
689 unsigned int total_connections;
690 unsigned int outer_count;
692 unsigned int previous_total_connections;
696 GNUNET_assert(pg->total > 1);
698 /* Add a connection between the first two nodes */
699 total_connections = proc(pg, 0, 1);
701 for (outer_count = 1; outer_count < pg->total; outer_count++)
703 previous_total_connections = total_connections;
704 for (i = 0; i < outer_count; i++)
706 probability = pg->peers[i].num_connections / (double)previous_total_connections;
707 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
708 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
711 "Considering connecting peer %d to peer %d\n",
714 if (random < probability)
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718 "Connecting peer %d to peer %d\n",
721 total_connections += proc(pg, outer_count, i);
726 return total_connections;
730 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
735 unsigned int randomPeer;
736 double random, logNModifier, percentage;
737 unsigned int smallWorldConnections;
743 int connect_attempts;
745 logNModifier = 0.5; /* FIXME: default value? */
746 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
751 if (sscanf(p_string, "%lf", &logNModifier) != 1)
752 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
753 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
757 GNUNET_free (p_string);
759 percentage = 0.5; /* FIXME: default percentage? */
760 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
765 if (sscanf(p_string, "%lf", &percentage) != 1)
766 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
767 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
771 GNUNET_free (p_string);
773 natLog = log (pg->total);
774 connsPerPeer = ceil (natLog * logNModifier);
776 if (connsPerPeer % 2 == 1)
779 smallWorldConnections = 0;
780 connect_attempts = 0;
781 for (i = 0; i < pg->total; i++)
784 max = i + connsPerPeer / 2;
785 min = i - connsPerPeer / 2;
787 if (max > pg->total - 1)
789 max = max - pg->total;
795 min = pg->total - 1 + min;
799 for (j = 0; j < connsPerPeer / 2; j++)
801 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
802 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
803 if (random < percentage)
805 /* Connect to uniformly selected random peer */
807 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
809 while ((((randomPeer < max) && (randomPeer > min))
810 && (useAnd == 0)) || (((randomPeer > min)
811 || (randomPeer < max))
815 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
818 smallWorldConnections +=
819 proc (pg, i, randomPeer);
823 nodeToConnect = i + j + 1;
824 if (nodeToConnect > pg->total - 1)
826 nodeToConnect = nodeToConnect - pg->total;
829 proc (pg, i, nodeToConnect);
835 connect_attempts += smallWorldConnections;
837 return connect_attempts;
842 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
844 unsigned int outer_count, inner_count;
846 int connect_attempts;
847 double nat_percentage;
850 nat_percentage = 0.6; /* FIXME: default percentage? */
851 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
856 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
857 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
858 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
862 GNUNET_free (p_string);
867 cutoff = (unsigned int) (nat_percentage * pg->total);
869 connect_attempts = 0;
871 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
873 for (inner_count = outer_count + 1; inner_count < pg->total;
876 if ((outer_count > cutoff) || (inner_count > cutoff))
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
880 "Connecting peer %d to peer %d\n",
881 outer_count, inner_count);
883 connect_attempts += proc(pg, outer_count, inner_count);
888 return connect_attempts;
895 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
897 unsigned int i, j, k;
901 unsigned int toggle = 1;
902 unsigned int nodeToConnect;
904 unsigned int node1Row;
905 unsigned int node1Col;
906 unsigned int node2Row;
907 unsigned int node2Col;
908 unsigned int distance;
909 double probability, random, percentage;
910 unsigned int smallWorldConnections;
912 int connect_attempts;
913 square = floor (sqrt (pg->total));
917 percentage = 0.5; /* FIXME: default percentage? */
918 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
923 if (sscanf(p_string, "%lf", &percentage) != 1)
924 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
925 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
929 GNUNET_free (p_string);
931 probability = 0.5; /* FIXME: default percentage? */
932 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
937 if (sscanf(p_string, "%lf", &probability) != 1)
938 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
939 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
943 GNUNET_free (p_string);
945 if (square * square != pg->total)
947 while (rows * cols < pg->total)
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
959 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
963 connect_attempts = 0;
964 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
965 * to the node to its right and above. Once this is over, we'll have our torus!
966 * Special case for the last node (if the rows and columns are not equal), connect
967 * to the first in the row to maintain topology.
969 for (i = 0; i < pg->total; i++)
971 /* First connect to the node to the right */
972 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
973 nodeToConnect = i + 1;
974 else if (i + 1 == pg->total)
975 nodeToConnect = rows * cols - cols;
977 nodeToConnect = i - cols + 1;
979 connect_attempts += proc (pg, i, nodeToConnect);
982 nodeToConnect = (rows * cols) - cols + i;
984 nodeToConnect = i - cols;
986 if (nodeToConnect < pg->total)
987 connect_attempts += proc (pg, i, nodeToConnect);
989 natLog = log (pg->total);
990 #if VERBOSE_TESTING > 2
991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
992 _("natural log of %d is %d, will run %d iterations\n"),
993 pg->total, natLog, (int) (natLog * percentage));
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
996 smallWorldConnections = 0;
997 for (i = 0; i < (int) (natLog * percentage); i++)
999 for (j = 0; j < pg->total; j++)
1001 /* Determine the row and column of node at position j on the 2d torus */
1002 node1Row = j / cols;
1003 node1Col = j - (node1Row * cols);
1004 for (k = 0; k < pg->total; k++)
1006 /* Determine the row and column of node at position k on the 2d torus */
1007 node2Row = k / cols;
1008 node2Col = k - (node2Row * cols);
1009 /* Simple Cartesian distance */
1010 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1013 /* Calculate probability as 1 over the square of the distance */
1014 probability = 1.0 / (distance * distance);
1015 /* Choose a random value between 0 and 1 */
1016 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1017 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
1018 /* If random < probability, then connect the two nodes */
1019 if (random < probability)
1020 smallWorldConnections += proc (pg, j, k);
1026 connect_attempts += smallWorldConnections;
1027 #if VERBOSE_TESTING > 2
1028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1029 _("Total connections added for small world: %d!\n"),
1030 smallWorldConnections);
1032 return connect_attempts;
1038 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1041 unsigned int outer_count;
1042 unsigned int inner_count;
1043 int connect_attempts;
1047 probability = 0.5; /* FIXME: default percentage? */
1048 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1053 if (sscanf(p_string, "%lf", &probability) != 1)
1054 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1055 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1059 GNUNET_free (p_string);
1061 connect_attempts = 0;
1062 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1064 for (inner_count = outer_count + 1; inner_count < pg->total;
1067 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1068 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 _("rand is %f probability is %f\n"), temp_rand,
1074 if (temp_rand < probability)
1076 connect_attempts += proc (pg, outer_count, inner_count);
1081 return connect_attempts;
1085 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1088 unsigned int square;
1091 unsigned int toggle = 1;
1092 unsigned int nodeToConnect;
1093 int connect_attempts;
1095 connect_attempts = 0;
1097 square = floor (sqrt (pg->total));
1101 if (square * square != pg->total)
1103 while (rows * cols < pg->total)
1105 if (toggle % 2 == 0)
1114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1115 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1118 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1119 * to the node to its right and above. Once this is over, we'll have our torus!
1120 * Special case for the last node (if the rows and columns are not equal), connect
1121 * to the first in the row to maintain topology.
1123 for (i = 0; i < pg->total; i++)
1125 /* First connect to the node to the right */
1126 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1127 nodeToConnect = i + 1;
1128 else if (i + 1 == pg->total)
1129 nodeToConnect = rows * cols - cols;
1131 nodeToConnect = i - cols + 1;
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1134 "Connecting peer %d to peer %d\n",
1137 connect_attempts += proc(pg, i, nodeToConnect);
1139 /* Second connect to the node immediately above */
1141 nodeToConnect = (rows * cols) - cols + i;
1143 nodeToConnect = i - cols;
1145 if (nodeToConnect < pg->total)
1148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1149 "Connecting peer %d to peer %d\n",
1152 connect_attempts += proc(pg, i, nodeToConnect);
1157 return connect_attempts;
1163 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1165 unsigned int outer_count;
1166 unsigned int inner_count;
1167 int connect_attempts;
1169 connect_attempts = 0;
1171 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1173 for (inner_count = outer_count + 1; inner_count < pg->total;
1177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1178 "Connecting peer %d to peer %d\n",
1179 outer_count, inner_count);
1181 connect_attempts += proc(pg, outer_count, inner_count);
1185 return connect_attempts;
1190 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1193 int connect_attempts;
1195 connect_attempts = 0;
1197 /* Connect each peer to the next highest numbered peer */
1198 for (count = 0; count < pg->total - 1; count++)
1201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1202 "Connecting peer %d to peer %d\n",
1205 connect_attempts += proc(pg, count, count + 1);
1208 /* Connect the last peer to the first peer */
1209 connect_attempts += proc(pg, pg->total - 1, 0);
1211 return connect_attempts;
1216 * Iterator for writing friends of a peer to a file.
1218 * @param cls closure, an open writable file handle
1219 * @param key the key the daemon was stored under
1220 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1222 * @return GNUNET_YES to continue iteration
1224 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1225 * with a single file_iterator that takes a closure which contains
1226 * the prefix to write before the peer. Then this could be used
1227 * for blacklisting multiple transports and writing the friend
1228 * file. I'm sure *someone* will complain loudly about other
1229 * things that negate these functions even existing so no point in
1233 friend_file_iterator (void *cls,
1234 const GNUNET_HashCode * key,
1237 FILE *temp_friend_handle = cls;
1238 struct GNUNET_TESTING_Daemon *peer = value;
1239 struct GNUNET_PeerIdentity *temppeer;
1240 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1242 temppeer = &peer->id;
1243 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1244 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1249 struct BlacklistContext
1252 * The (open) file handle to write to
1254 FILE *temp_file_handle;
1257 * The transport that this peer will be blacklisted on.
1263 * Iterator for writing blacklist data to appropriate files.
1265 * @param cls closure, an open writable file handle
1266 * @param key the key the daemon was stored under
1267 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1269 * @return GNUNET_YES to continue iteration
1272 blacklist_file_iterator (void *cls,
1273 const GNUNET_HashCode * key,
1276 struct BlacklistContext *blacklist_ctx = cls;
1277 //FILE *temp_blacklist_handle = cls;
1278 struct GNUNET_TESTING_Daemon *peer = value;
1279 struct GNUNET_PeerIdentity *temppeer;
1280 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1282 temppeer = &peer->id;
1283 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1284 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1290 * Create the friend files based on the PeerConnection's
1291 * of each peer in the peer group, and copy the files
1292 * to the appropriate place
1294 * @param pg the peer group we are dealing with
1297 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1299 FILE *temp_friend_handle;
1300 unsigned int pg_iter;
1301 char *temp_service_path;
1305 enum GNUNET_OS_ProcessStatusType type;
1306 unsigned long return_code;
1311 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1312 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1314 mytemp = GNUNET_DISK_mktemp("friends");
1315 GNUNET_assert(mytemp != NULL);
1316 temp_friend_handle = fopen (mytemp, "wt");
1317 GNUNET_assert(temp_friend_handle != NULL);
1318 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1319 fclose(temp_friend_handle);
1322 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1324 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1325 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1328 if (UNLINK (mytemp) != 0)
1329 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1330 GNUNET_free (mytemp);
1334 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1336 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1337 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1338 "mv", mytemp, arg, NULL);
1340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1341 _("Copying file with command cp %s %s\n"), mytemp, arg);
1346 else /* Remote, scp the file to the correct place */
1348 if (NULL != pg->peers[pg_iter].daemon->username)
1349 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1351 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1352 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1353 "scp", mytemp, arg, NULL);
1356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1357 _("Copying file with command scp %s %s\n"), mytemp, arg);
1361 GNUNET_free (temp_service_path);
1362 GNUNET_free (mytemp);
1366 ret = GNUNET_SYSERR;
1367 while ((count < max_wait) && (ret != GNUNET_OK))
1370 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1374 _("Checking copy status of file %d\n"), pg_iter);
1376 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1378 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1380 ret = GNUNET_SYSERR;
1382 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1384 ret = GNUNET_SYSERR;
1388 pidarr[pg_iter] = 0;
1390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1391 _("File %d copied\n"), pg_iter);
1397 if (ret == GNUNET_SYSERR)
1399 /* FIXME: why sleep here? -CG */
1405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1406 _("Finished copying all friend files!\n"));
1408 GNUNET_free(pidarr);
1414 * Create the blacklist files based on the PeerConnection's
1415 * of each peer in the peer group, and copy the files
1416 * to the appropriate place.
1418 * @param pg the peer group we are dealing with
1419 * @param transports space delimited list of transports to blacklist
1422 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
1424 FILE *temp_file_handle;
1425 static struct BlacklistContext blacklist_ctx;
1426 unsigned int pg_iter;
1427 char *temp_service_path;
1431 enum GNUNET_OS_ProcessStatusType type;
1432 unsigned long return_code;
1439 char *temp_transports;
1441 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1442 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1444 mytemp = GNUNET_DISK_mktemp("blacklist");
1445 GNUNET_assert(mytemp != NULL);
1446 temp_file_handle = fopen (mytemp, "wt");
1447 GNUNET_assert(temp_file_handle != NULL);
1448 temp_transports = GNUNET_strdup(transports);
1449 blacklist_ctx.temp_file_handle = temp_file_handle;
1450 transport_len = strlen(temp_transports) + 1;
1453 for (i = 0; i < transport_len; i++)
1455 if ((temp_transports[i] == ' ') && (pos == NULL))
1456 continue; /* At start of string (whitespace) */
1457 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
1459 temp_transports[i] = '\0';
1460 blacklist_ctx.transport = pos;
1461 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
1463 } /* At beginning of actual string */
1464 else if (pos == NULL)
1466 pos = &temp_transports[i];
1470 GNUNET_free_non_null(temp_transports);
1471 fclose(temp_file_handle);
1474 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1476 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1477 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1480 if (UNLINK (mytemp) != 0)
1481 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1482 GNUNET_free (mytemp);
1486 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1488 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
1489 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1490 "mv", mytemp, arg, NULL);
1492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1493 _("Copying file with command cp %s %s\n"), mytemp, arg);
1498 else /* Remote, scp the file to the correct place */
1500 if (NULL != pg->peers[pg_iter].daemon->username)
1501 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1503 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1504 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1505 "scp", mytemp, arg, NULL);
1508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1509 _("Copying file with command scp %s %s\n"), mytemp, arg);
1513 GNUNET_free (temp_service_path);
1514 GNUNET_free (mytemp);
1518 ret = GNUNET_SYSERR;
1519 while ((count < max_wait) && (ret != GNUNET_OK))
1522 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1526 _("Checking copy status of file %d\n"), pg_iter);
1528 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1530 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1532 ret = GNUNET_SYSERR;
1534 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1536 ret = GNUNET_SYSERR;
1540 pidarr[pg_iter] = 0;
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 _("File %d copied\n"), pg_iter);
1549 if (ret == GNUNET_SYSERR)
1551 /* FIXME: why sleep here? -CG */
1557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1558 _("Finished copying all blacklist files!\n"));
1560 GNUNET_free(pidarr);
1566 * Internal notification of a connection, kept so that we can ensure some connections
1567 * happen instead of flooding all testing daemons with requests to connect.
1569 static void internal_connect_notify (void *cls,
1570 const struct GNUNET_PeerIdentity *first,
1571 const struct GNUNET_PeerIdentity *second,
1572 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
1573 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
1574 struct GNUNET_TESTING_Daemon *first_daemon,
1575 struct GNUNET_TESTING_Daemon *second_daemon,
1578 struct GNUNET_TESTING_PeerGroup *pg = cls;
1579 outstanding_connects--;
1581 pg->notify_connection(pg->notify_connection_cls, first, second, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
1585 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1587 struct ConnectContext *connect_context = cls;
1589 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1592 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
1594 #if VERBOSE_TESTING > 2
1595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1596 _("Delaying connect, we have too many outstanding connections!\n"));
1598 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &schedule_connect, connect_context);
1602 #if VERBOSE_TESTING > 2
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
1606 outstanding_connects++;
1607 GNUNET_TESTING_daemons_connect (connect_context->first,
1608 connect_context->second,
1611 &internal_connect_notify,
1612 connect_context->pg);
1613 GNUNET_free(connect_context);
1618 * Iterator for actually scheduling connections to be created
1619 * between two peers.
1621 * @param cls closure, a GNUNET_TESTING_Daemon
1622 * @param key the key the second Daemon was stored under
1623 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1625 * @return GNUNET_YES to continue iteration
1628 connect_iterator (void *cls,
1629 const GNUNET_HashCode * key,
1632 struct PeerData *first = cls;
1633 struct GNUNET_TESTING_Daemon *second = value;
1634 struct ConnectContext *connect_context;
1636 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1637 connect_context->pg = first->pg;
1638 connect_context->first = first->daemon;
1639 connect_context->second = second;
1640 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
1646 * Iterator for copying all entries in the allowed hashmap to the
1649 * @param cls closure, a GNUNET_TESTING_Daemon
1650 * @param key the key the second Daemon was stored under
1651 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1653 * @return GNUNET_YES to continue iteration
1656 copy_topology_iterator (void *cls,
1657 const GNUNET_HashCode * key,
1660 struct PeerData *first = cls;
1662 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1668 * Make the peers to connect the same as those that are allowed to be
1671 * @param pg the peer group
1674 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
1676 unsigned int pg_iter;
1681 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1683 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
1684 if (GNUNET_SYSERR == ret)
1685 return GNUNET_SYSERR;
1687 total = total + ret;
1695 * Connect the topology as specified by the PeerConnection's
1696 * of each peer in the peer group
1698 * @param pg the peer group we are dealing with
1699 * @return the number of connections that will be attempted
1702 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
1704 unsigned int pg_iter;
1708 struct PeerConnection *connection_iter;
1709 struct ConnectContext *connect_context;
1713 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1715 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
1716 if (GNUNET_SYSERR == ret)
1717 return GNUNET_SYSERR;
1719 total = total + ret;
1723 while (connection_iter != NULL)
1725 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1726 connect_context->pg = pg;
1727 connect_context->first = ;
1728 connect_context->second = connection_iter->daemon;
1729 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
1730 connection_iter = connection_iter->next;
1739 * Takes a peer group and creates a topology based on the
1740 * one specified. Creates a topology means generates friend
1741 * files for the peers so they can only connect to those allowed
1742 * by the topology. This will only have an effect once peers
1743 * are started if the FRIENDS_ONLY option is set in the base
1744 * config. Also takes an optional restrict topology which
1745 * disallows direct TCP connections UNLESS they are specified in
1746 * the restricted topology.
1748 * @param pg the peer group struct representing the running peers
1749 * @param topology which topology to connect the peers in
1750 * @param restrict_topology allow only direct TCP connections in this topology
1751 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
1752 * @param restrict_transports space delimited list of transports to blacklist
1753 * to create restricted topology
1755 * @return the maximum number of connections were all allowed peers
1756 * connected to each other
1759 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
1760 enum GNUNET_TESTING_Topology topology,
1761 enum GNUNET_TESTING_Topology restrict_topology,
1762 char *restrict_transports)
1765 int num_connections;
1766 int unblacklisted_connections;
1768 GNUNET_assert (pg->notify_connection != NULL);
1773 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
1775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1776 _("Creating clique topology\n"));
1778 num_connections = create_clique (pg, &add_allowed_connections);
1780 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
1782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1783 _("Creating small world (ring) topology\n"));
1785 num_connections = create_small_world_ring (pg, &add_allowed_connections);
1787 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
1789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1790 _("Creating small world (2d-torus) topology\n"));
1792 num_connections = create_small_world (pg, &add_allowed_connections);
1794 case GNUNET_TESTING_TOPOLOGY_RING:
1796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1797 _("Creating ring topology\n"));
1799 num_connections = create_ring (pg, &add_allowed_connections);
1801 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
1803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1804 _("Creating 2d torus topology\n"));
1806 num_connections = create_2d_torus (pg, &add_allowed_connections);
1808 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
1810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1811 _("Creating Erdos-Renyi topology\n"));
1813 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
1815 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
1817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1818 _("Creating InterNAT topology\n"));
1820 num_connections = create_nated_internet (pg, &add_allowed_connections);
1822 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
1824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1825 _("Creating Scale Free topology\n"));
1827 num_connections = create_scale_free (pg, &add_allowed_connections);
1829 case GNUNET_TESTING_TOPOLOGY_NONE:
1830 num_connections = 0;
1833 num_connections = 0;
1836 if (num_connections < 1)
1837 return GNUNET_SYSERR;
1839 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
1841 ret = create_and_copy_friend_files(pg);
1844 if (ret != GNUNET_OK)
1847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1848 _("Failed during friend file copying!\n"));
1850 return GNUNET_SYSERR;
1855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1856 _("Friend files created/copied successfully!\n"));
1860 /* Use the create clique method to initially set all connections as blacklisted. */
1861 create_clique (pg, &blacklist_connections);
1862 unblacklisted_connections = 0;
1863 /* Un-blacklist connections as per the topology specified */
1864 switch (restrict_topology)
1866 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
1868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1869 _("Blacklisting all but clique topology\n"));
1871 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
1873 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876 _("Blacklisting all but small world (ring) topology\n"));
1878 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
1880 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
1882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1883 _("Blacklisting all but small world (2d-torus) topology\n"));
1885 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
1887 case GNUNET_TESTING_TOPOLOGY_RING:
1889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1890 _("Blacklisting all but ring topology\n"));
1892 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
1894 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
1896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1897 _("Blacklisting all but 2d torus topology\n"));
1899 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
1901 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
1903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1904 _("Blacklisting all but Erdos-Renyi topology\n"));
1906 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
1908 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
1910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1911 _("Blacklisting all but InterNAT topology\n"));
1913 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
1915 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1918 _("Blacklisting all but Scale Free topology\n"));
1920 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
1922 case GNUNET_TESTING_TOPOLOGY_NONE:
1928 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
1930 ret = create_and_copy_blacklist_files(pg, restrict_transports);
1931 if (ret != GNUNET_OK)
1934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1935 _("Failed during blacklist file copying!\n"));
1937 return GNUNET_SYSERR;
1942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1943 _("Blacklist files created/copied successfully!\n"));
1947 return num_connections;
1950 struct RandomContext
1955 struct GNUNET_TESTING_PeerGroup *pg;
1958 * uid of the first peer
1963 * Peer data for first peer.
1965 struct PeerData *first;
1968 * Random percentage to use
1973 struct MinimumContext
1978 struct GNUNET_TESTING_PeerGroup *pg;
1981 * uid of the first peer
1986 * Peer data for first peer.
1988 struct PeerData *first;
1991 * Number of conns per peer
1993 unsigned int num_to_add;
1996 * Permuted array of all possible connections. Only add the Nth
1997 * peer if it's in the Nth position.
1999 unsigned int *pg_array;
2002 * What number is the current element we are iterating over?
2004 unsigned int current;
2012 struct GNUNET_TESTING_PeerGroup *pg;
2015 * uid of the first peer
2020 * uid of the second peer
2022 uint32_t second_uid;
2025 * Peer data for first peer.
2027 struct PeerData *first;
2030 * Which peer has been chosen as the one to add?
2032 unsigned int chosen;
2035 * What number is the current element we are iterating over?
2037 unsigned int current;
2041 * Iterator for choosing random peers to connect.
2043 * @param cls closure, a RandomContext
2044 * @param key the key the second Daemon was stored under
2045 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2047 * @return GNUNET_YES to continue iteration
2050 random_connect_iterator (void *cls,
2051 const GNUNET_HashCode * key,
2054 struct RandomContext *random_ctx = cls;
2055 double random_number;
2056 uint32_t second_pos;
2057 GNUNET_HashCode first_hash;
2058 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2059 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
2060 if (random_number < random_ctx->percentage)
2062 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2064 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2065 uid_from_hash(key, &second_pos);
2066 hash_from_uid(random_ctx->first_uid, &first_hash);
2067 GNUNET_assert(random_ctx->pg->total > second_pos);
2068 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2074 * Iterator for adding at least X peers to a peers connection set.
2076 * @param cls closure, MinimumContext
2077 * @param key the key the second Daemon was stored under
2078 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2080 * @return GNUNET_YES to continue iteration
2083 minimum_connect_iterator (void *cls,
2084 const GNUNET_HashCode * key,
2087 struct MinimumContext *min_ctx = cls;
2088 uint32_t second_pos;
2089 GNUNET_HashCode first_hash;
2092 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2094 for (i = 0; i < min_ctx->num_to_add; i++)
2096 if (min_ctx->pg_array[i] == min_ctx->current)
2098 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2099 uid_from_hash(key, &second_pos);
2100 hash_from_uid(min_ctx->first_uid, &first_hash);
2101 GNUNET_assert(min_ctx->pg->total > second_pos);
2102 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->pg->peers[second_pos].connect_peers_working_set, &first_hash, min_ctx->first->daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2103 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2104 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2111 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2117 * Iterator for adding peers to a connection set based on a depth first search.
2119 * @param cls closure, MinimumContext
2120 * @param key the key the second daemon was stored under
2121 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2123 * @return GNUNET_YES to continue iteration
2126 dfs_connect_iterator (void *cls,
2127 const GNUNET_HashCode * key,
2130 struct DFSContext *dfs_ctx = cls;
2131 GNUNET_HashCode first_hash;
2133 if (dfs_ctx->current == dfs_ctx->chosen)
2135 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2136 uid_from_hash(key, &dfs_ctx->second_uid);
2137 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2138 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers_working_set, &first_hash, dfs_ctx->first->daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2139 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2140 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2141 return GNUNET_NO; /* We have found our peer, don't iterate more */
2150 * From the set of connections possible, choose percentage percent of connections
2151 * to actually connect.
2153 * @param pg the peergroup we are dealing with
2154 * @param percentage what percent of total connections to make
2157 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2159 struct RandomContext random_ctx;
2162 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2164 random_ctx.first_uid = pg_iter;
2165 random_ctx.first = &pg->peers[pg_iter];
2166 random_ctx.percentage = percentage;
2168 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2169 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2170 /* Now remove the old connections */
2171 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2172 /* And replace with the random set */
2173 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2178 * From the set of connections possible, choose at least num connections per
2181 * @param pg the peergroup we are dealing with
2182 * @param num how many connections at least should each peer have (if possible)?
2185 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2187 struct MinimumContext minimum_ctx;
2190 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2192 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2195 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2197 minimum_ctx.first_uid = pg_iter;
2198 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2199 minimum_ctx.first = &pg->peers[pg_iter];
2200 minimum_ctx.pg = pg;
2201 minimum_ctx.num_to_add = num;
2202 minimum_ctx.current = 0;
2203 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2204 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &minimum_connect_iterator, &minimum_ctx);
2207 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2209 /* Remove the "old" connections */
2210 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2211 /* And replace with the working set */
2212 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2213 fprintf(stderr, "Finished! Hashmap size %u\n", GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2219 static unsigned int count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2222 unsigned int pg_iter;
2226 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2228 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2235 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2238 unsigned int pg_iter;
2242 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2244 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2251 * From the set of connections possible, choose at least num connections per
2252 * peer based on depth first traversal of peer connections. If DFS leaves
2253 * peers unconnected, ensure those peers get connections.
2255 * @param pg the peergroup we are dealing with
2256 * @param num how many connections at least should each peer have (if possible)?
2259 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2261 struct DFSContext dfs_ctx;
2264 uint32_t starting_peer;
2265 uint32_t least_connections;
2266 GNUNET_HashCode second_hash;
2268 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2270 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2275 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
2277 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
2279 least_connections = -1; /* Set to very high number */
2280 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2282 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
2284 starting_peer = pg_iter;
2285 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2290 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
2296 /* Choose a random peer from the chosen peers set of connections to add */
2297 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
2298 dfs_ctx.first_uid = starting_peer;
2299 dfs_ctx.first = &pg->peers[starting_peer];
2301 dfs_ctx.current = 0;
2303 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
2304 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
2305 hash_from_uid(dfs_ctx.second_uid, &second_hash);
2306 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
2307 starting_peer = dfs_ctx.second_uid;
2310 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2315 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2317 /* Remove the "old" connections */
2318 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2319 /* And replace with the working set */
2320 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2321 fprintf(stderr, "Finished! Hashmap size %u\n", GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2327 * There are many ways to connect peers that are supported by this function.
2328 * To connect peers in the same topology that was created via the
2329 * GNUNET_TESTING_create_topology, the topology variable must be set to
2330 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
2331 * a new instance of that topology will be generated and attempted to be
2332 * connected. This could result in some connections being impossible,
2333 * because some topologies are non-deterministic.
2335 * @param pg the peer group struct representing the running peers
2336 * @param topology which topology to connect the peers in
2337 * @param options options for connecting the topology
2338 * @param option_modifier modifier for options that take a parameter
2339 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
2342 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2343 enum GNUNET_TESTING_Topology topology,
2344 enum GNUNET_TESTING_TopologyOption options,
2345 double option_modifier)
2349 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2352 _("Creating clique topology\n"));
2354 create_clique (pg, &add_actual_connections);
2356 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2359 _("Creating small world (ring) topology\n"));
2361 create_small_world_ring (pg, &add_actual_connections);
2363 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2366 _("Creating small world (2d-torus) topology\n"));
2368 create_small_world (pg, &add_actual_connections);
2370 case GNUNET_TESTING_TOPOLOGY_RING:
2372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2373 _("Creating ring topology\n"));
2375 create_ring (pg, &add_actual_connections);
2377 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2380 _("Creating 2d torus topology\n"));
2382 create_2d_torus (pg, &add_actual_connections);
2384 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2387 _("Creating Erdos-Renyi topology\n"));
2389 create_erdos_renyi (pg, &add_actual_connections);
2391 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2394 _("Creating InterNAT topology\n"));
2396 create_nated_internet (pg, &add_actual_connections);
2398 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2401 _("Creating Scale Free topology\n"));
2403 create_scale_free (pg, &add_actual_connections);
2405 case GNUNET_TESTING_TOPOLOGY_NONE:
2406 copy_allowed_topology(pg);
2409 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2410 _("Unknown topology specification, can't connect peers!\n"));
2411 return GNUNET_SYSERR;
2416 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
2417 choose_random_connections(pg, option_modifier);
2419 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
2420 choose_minimum(pg, (unsigned int)option_modifier);
2422 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
2423 perform_dfs(pg, (int)option_modifier);
2425 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
2427 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
2433 return connect_topology(pg);
2437 * Function which continues a peer group starting up
2438 * after successfully generating hostkeys for each peer.
2440 * @param pg the peer group to continue starting
2444 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
2448 for (i = 0; i < pg->total; i++)
2450 GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
2455 * Start count gnunetd processes with the same set of transports and
2456 * applications. The port numbers (any option called "PORT") will be
2457 * adjusted to ensure that no two peers running on the same system
2458 * have the same port(s) in their respective configurations.
2460 * @param sched scheduler to use
2461 * @param cfg configuration template to use
2462 * @param total number of daemons to start
2463 * @param timeout total time allowed for peers to start
2464 * @param hostkey_callback function to call on each peers hostkey generation
2465 * if NULL, peers will be started by this call, if non-null,
2466 * GNUNET_TESTING_daemons_continue_startup must be called after
2467 * successful hostkey generation
2468 * @param hostkey_cls closure for hostkey callback
2469 * @param cb function to call on each daemon that was started
2470 * @param cb_cls closure for cb
2471 * @param connect_callback function to call each time two hosts are connected
2472 * @param connect_callback_cls closure for connect_callback
2473 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
2474 * everything on localhost).
2475 * @return NULL on error, otherwise handle to control peer group
2477 struct GNUNET_TESTING_PeerGroup *
2478 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
2479 const struct GNUNET_CONFIGURATION_Handle *cfg,
2481 struct GNUNET_TIME_Relative timeout,
2482 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
2484 GNUNET_TESTING_NotifyDaemonRunning cb,
2486 GNUNET_TESTING_NotifyConnection
2487 connect_callback, void *connect_callback_cls,
2488 const char *hostnames)
2490 struct GNUNET_TESTING_PeerGroup *pg;
2494 const char *hostname;
2495 char *baseservicehome;
2496 char *newservicehome;
2498 struct GNUNET_CONFIGURATION_Handle *pcfg;
2500 unsigned int hostcnt;
2509 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
2513 pg->cb_cls = cb_cls;
2514 pg->notify_connection = connect_callback;
2515 pg->notify_connection_cls = connect_callback_cls;
2517 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
2518 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
2519 if (NULL != hostnames)
2522 /* skip leading spaces */
2523 while ((0 != *hostnames) && (isspace (*hostnames)))
2526 while ('\0' != *rpos)
2528 if (isspace (*rpos))
2532 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
2534 start = GNUNET_strdup (hostnames);
2536 while ('\0' != *pos)
2541 if (strlen (start) > 0)
2543 pg->hosts[off].minport = LOW_PORT;
2544 pg->hosts[off++].hostname = start;
2550 if (strlen (start) > 0)
2552 pg->hosts[off].minport = LOW_PORT;
2553 pg->hosts[off++].hostname = start;
2557 GNUNET_free (start);
2558 GNUNET_free (pg->hosts);
2562 minport = 0; /* make gcc happy */
2569 for (off = 0; off < total; off++)
2573 hostname = pg->hosts[off % hostcnt].hostname;
2574 pcfg = make_config (cfg, &pg->hosts[off % hostcnt].minport, hostname);
2579 pcfg = make_config (cfg, &minport, hostname);
2584 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2586 ("Could not create configuration for peer number %u on `%s'!\n"),
2587 off, hostname == NULL ? "localhost" : hostname);
2592 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
2595 GNUNET_asprintf (&newservicehome,
2596 "%s/%d/", baseservicehome, off);
2597 GNUNET_free (baseservicehome);
2601 tmpdir = getenv ("TMPDIR");
2602 tmpdir = tmpdir ? tmpdir : "/tmp";
2603 GNUNET_asprintf (&newservicehome,
2606 "gnunet-testing-test-test", off);
2608 GNUNET_CONFIGURATION_set_value_string (pcfg,
2610 "SERVICEHOME", newservicehome);
2611 GNUNET_free (newservicehome);
2612 pg->peers[off].cfg = pcfg;
2613 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
2614 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
2615 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
2616 pg->peers[off].pg = pg;
2617 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (sched,
2624 if (NULL == pg->peers[off].daemon)
2625 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2626 _("Could not start peer number %u!\n"), off);
2633 * Get a daemon by number, so callers don't have to do nasty
2634 * offsetting operation.
2636 struct GNUNET_TESTING_Daemon *
2637 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
2639 if (position < pg->total)
2640 return pg->peers[position].daemon;
2646 * Prototype of a function that will be called when a
2647 * particular operation was completed the testing library.
2649 * @param id id of the peer that was restarted
2650 * @param cfg handle to the configuration of the peer
2651 * @param d handle to the daemon that was restarted
2652 * @param emsg NULL on success
2654 void restart_callback (void *cls,
2655 const struct GNUNET_PeerIdentity *id,
2656 const struct GNUNET_CONFIGURATION_Handle *cfg,
2657 struct GNUNET_TESTING_Daemon *d,
2660 struct RestartContext *restart_context = cls;
2664 restart_context->peers_restarted++;
2668 restart_context->peers_restart_failed++;
2671 if (restart_context->peers_restarted == restart_context->peer_group->total)
2673 restart_context->callback(restart_context->callback_cls, NULL);
2674 GNUNET_free(restart_context);
2676 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
2678 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
2679 GNUNET_free(restart_context);
2685 * Callback for informing us about a successful
2686 * or unsuccessful churn stop call.
2688 * @param cls a ChurnContext
2689 * @param emsg NULL on success, non-NULL on failure
2693 churn_stop_callback (void *cls, const char *emsg)
2695 struct ChurnContext *churn_ctx = cls;
2696 unsigned int total_left;
2697 char *error_message;
2699 error_message = NULL;
2702 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2703 "Churn stop callback failed with error `%s'\n", emsg);
2704 churn_ctx->num_failed_stop++;
2708 churn_ctx->num_to_stop--;
2712 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2713 "Stopped peer, %d left.\n",
2714 churn_ctx->num_to_stop);
2716 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
2718 if (total_left == 0)
2720 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
2722 GNUNET_asprintf(&error_message,
2723 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
2724 churn_ctx->num_failed_start,
2725 churn_ctx->num_failed_stop);
2727 churn_ctx->cb(churn_ctx->cb_cls, error_message);
2728 GNUNET_free_non_null(error_message);
2729 GNUNET_free(churn_ctx);
2734 * Callback for informing us about a successful
2735 * or unsuccessful churn start call.
2737 * @param cls a ChurnContext
2738 * @param id the peer identity of the started peer
2739 * @param cfg the handle to the configuration of the peer
2740 * @param d handle to the daemon for the peer
2741 * @param emsg NULL on success, non-NULL on failure
2745 churn_start_callback (void *cls,
2746 const struct GNUNET_PeerIdentity *id,
2747 const struct GNUNET_CONFIGURATION_Handle *cfg,
2748 struct GNUNET_TESTING_Daemon *d,
2751 struct ChurnContext *churn_ctx = cls;
2752 unsigned int total_left;
2753 char *error_message;
2755 error_message = NULL;
2758 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2759 "Churn stop callback failed with error `%s'\n",
2761 churn_ctx->num_failed_start++;
2765 churn_ctx->num_to_start--;
2769 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2770 "Started peer, %d left.\n",
2771 churn_ctx->num_to_start);
2774 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
2776 if (total_left == 0)
2778 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
2779 GNUNET_asprintf(&error_message,
2780 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
2781 churn_ctx->num_failed_start,
2782 churn_ctx->num_failed_stop);
2783 churn_ctx->cb(churn_ctx->cb_cls, error_message);
2784 GNUNET_free_non_null(error_message);
2785 GNUNET_free(churn_ctx);
2791 * Simulate churn by stopping some peers (and possibly
2792 * re-starting others if churn is called multiple times). This
2793 * function can only be used to create leave-join churn (peers "never"
2794 * leave for good). First "voff" random peers that are currently
2795 * online will be taken offline; then "von" random peers that are then
2796 * offline will be put back online. No notifications will be
2797 * generated for any of these operations except for the callback upon
2800 * @param pg handle for the peer group
2801 * @param voff number of peers that should go offline
2802 * @param von number of peers that should come back online;
2803 * must be zero on first call (since "testbed_start"
2804 * always starts all of the peers)
2805 * @param timeout how long to wait for operations to finish before
2807 * @param cb function to call at the end
2808 * @param cb_cls closure for cb
2811 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
2814 struct GNUNET_TIME_Relative timeout,
2815 GNUNET_TESTING_NotifyCompletion cb,
2818 struct ChurnContext *churn_ctx;
2819 unsigned int running;
2820 unsigned int stopped;
2822 unsigned int *running_arr;
2823 unsigned int *stopped_arr;
2824 unsigned int *running_permute;
2825 unsigned int *stopped_permute;
2830 if ((von == 0) && (voff == 0)) /* No peers at all? */
2836 for (i = 0; i < pg->total; i++)
2838 if (pg->peers[i].daemon->running == GNUNET_YES)
2850 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
2851 cb(cb_cls, "Trying to stop more peers than are currently running!");
2857 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
2858 cb(cb_cls, "Trying to start more peers than are currently stopped!");
2862 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
2863 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
2864 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
2866 running_permute = NULL;
2867 stopped_permute = NULL;
2870 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
2872 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
2877 churn_ctx->num_to_start = von;
2878 churn_ctx->num_to_stop = voff;
2880 churn_ctx->cb_cls = cb_cls;
2882 for (i = 0; i < pg->total; i++)
2884 if (pg->peers[i].daemon->running == GNUNET_YES)
2886 running_arr[running] = i;
2891 stopped_arr[stopped] = i;
2896 for (i = 0; i < voff; i++)
2899 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
2901 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
2903 &churn_stop_callback, churn_ctx,
2904 GNUNET_NO, GNUNET_YES);
2907 for (i = 0; i < von; i++)
2910 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
2912 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
2913 timeout, &churn_start_callback, churn_ctx);
2916 GNUNET_free(running_arr);
2917 GNUNET_free(stopped_arr);
2918 GNUNET_free_non_null(running_permute);
2919 GNUNET_free_non_null(stopped_permute);
2924 * Restart all peers in the given group.
2926 * @param pg the handle to the peer group
2927 * @param callback function to call on completion (or failure)
2928 * @param callback_cls closure for the callback function
2931 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyCompletion callback, void *callback_cls)
2933 struct RestartContext *restart_context;
2938 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
2939 restart_context->peer_group = pg;
2940 restart_context->peers_restarted = 0;
2941 restart_context->callback = callback;
2942 restart_context->callback_cls = callback_cls;
2944 for (off = 0; off < pg->total; off++)
2946 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
2952 * Start or stop an individual peer from the given group.
2954 * @param pg handle to the peer group
2955 * @param offset which peer to start or stop
2956 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
2957 * @param timeout how long to wait for shutdown
2958 * @param cb function to call at the end
2959 * @param cb_cls closure for cb
2962 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
2963 unsigned int offset,
2965 struct GNUNET_TIME_Relative timeout,
2966 GNUNET_TESTING_NotifyCompletion cb,
2969 struct ChurnContext *churn_ctx;
2971 if (GNUNET_NO == desired_status)
2973 if (NULL != pg->peers[offset].daemon)
2975 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
2976 churn_ctx->num_to_start = 0;
2977 churn_ctx->num_to_stop = 1;
2979 churn_ctx->cb_cls = cb_cls;
2980 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
2981 timeout, &churn_stop_callback, churn_ctx,
2982 GNUNET_NO, GNUNET_YES);
2985 else if (GNUNET_YES == desired_status)
2987 if (NULL == pg->peers[offset].daemon)
2989 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
2990 churn_ctx->num_to_start = 1;
2991 churn_ctx->num_to_stop = 0;
2993 churn_ctx->cb_cls = cb_cls;
2994 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
2995 timeout, &churn_start_callback, churn_ctx);
3004 * Shutdown all peers started in the given group.
3006 * @param pg handle to the peer group
3007 * @param timeout how long to wait for shutdown
3010 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
3011 struct GNUNET_TIME_Relative timeout)
3015 for (off = 0; off < pg->total; off++)
3017 /* FIXME: should we wait for our continuations to be called
3018 here? This would require us to take a continuation as
3021 if (NULL != pg->peers[off].daemon)
3022 GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, NULL, NULL, GNUNET_YES, GNUNET_NO);
3023 if (NULL != pg->peers[off].cfg)
3024 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
3025 if (pg->peers[off].allowed_peers != NULL)
3026 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
3027 if (pg->peers[off].connect_peers != NULL)
3028 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
3029 if (pg->peers[off].blacklisted_peers != NULL)
3030 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
3032 GNUNET_free (pg->peers);
3033 if (NULL != pg->hosts)
3035 GNUNET_free (pg->hosts[0].hostname);
3036 GNUNET_free (pg->hosts);
3042 /* end of testing_group.c */