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
26 * FIXME: have connection processor functions take a cls argument
27 * which specifies where to write the connection information
28 * instead of assuming it's certain peergroup places. (maybe?)
29 * FIXME: create static struct which contains the TOPOLOGY enum, the
30 * associated string, and the function used to create it.
31 * Then replace the create_X calls with topology_struct[i][2]
32 * or something. (Store function pointers instead of using
36 #include "gnunet_arm_service.h"
37 #include "gnunet_testing_lib.h"
39 #define VERBOSE_TESTING GNUNET_NO
41 #define VERBOSE_TOPOLOGY GNUNET_NO
43 #define DEBUG_CHURN GNUNET_NO
46 * Lowest port used for GNUnet testing. Should be high enough to not
47 * conflict with other applications running on the hosts but be low
48 * enough to not conflict with client-ports (typically starting around
51 #define LOW_PORT 10000
54 * Highest port used for GNUnet testing. Should be low enough to not
55 * conflict with the port range for "local" ports (client apps; see
56 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
58 #define HIGH_PORT 32000
60 #define MAX_OUTSTANDING_CONNECTIONS 50
62 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
64 #define CONNECT_ATTEMPTS 8
67 * Prototype of a function called whenever two peers would be connected
68 * in a certain topology.
70 typedef int (*GNUNET_TESTING_ConnectionProcessor)
71 (struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second);
74 * Strings representing topologies in enum
76 static char * GNUNET_TESTING_TopologyStrings[] =
79 * A clique (everyone connected to everyone else).
84 * Small-world network (2d torus plus random links).
89 * Small-world network (ring plus random links).
109 * Certain percentage of peers are unable to communicate directly
110 * replicating NAT conditions
115 * Scale free topology.
120 * Straight line topology.
125 * All peers are disconnected.
131 * Options for connecting a topology as strings.
133 static char * GNUNET_TESTING_TopologyOptionStrings[] =
136 * Try to connect all peers specified in the topology.
141 * Choose a random subset of connections to create.
143 "CONNECT_RANDOM_SUBSET",
146 * Create at least X connections for each peer.
151 * Using a depth first search, create one connection
152 * per peer. If any are missed (graph disconnected)
153 * start over at those peers until all have at least one
159 * No options specified.
165 * Context for handling churning a peer group
170 * Callback used to notify of churning finished
172 GNUNET_TESTING_NotifyCompletion cb;
175 * Closure for callback
180 * Number of peers that still need to be started
182 unsigned int num_to_start;
185 * Number of peers that still need to be stopped
187 unsigned int num_to_stop;
190 * Number of peers that failed to start
192 unsigned int num_failed_start;
195 * Number of peers that failed to stop
197 unsigned int num_failed_stop;
200 struct RestartContext
203 * The group of peers being restarted
205 struct GNUNET_TESTING_PeerGroup *peer_group;
208 * How many peers have been restarted thus far
210 unsigned int peers_restarted;
213 * How many peers got an error when restarting
215 unsigned int peers_restart_failed;
218 * The function to call once all peers have been restarted
220 GNUNET_TESTING_NotifyCompletion callback;
223 * Closure for callback function
229 struct CreateTopologyContext
233 * Function to call with number of connections
235 GNUNET_TESTING_NotifyConnections cont;
238 * Closure for connection notification
244 struct PeerConnection
249 struct PeerConnection *next;
252 * Pointer to daemon handle
254 struct GNUNET_TESTING_Daemon *daemon;
260 * Data we keep per peer.
265 * (Initial) configuration of the host.
266 * (initial because clients could change
267 * it and we would not know about those
270 struct GNUNET_CONFIGURATION_Handle *cfg;
273 * Handle for controlling the daemon.
275 struct GNUNET_TESTING_Daemon *daemon;
278 * The peergroup this peer belongs to.
280 struct GNUNET_TESTING_PeerGroup *pg;
283 * Linked list of peer connections (pointers)
285 //struct PeerConnection *connected_peers;
287 * Hash map of allowed peer connections (F2F created topology)
289 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
292 * Hash map of blacklisted peers
294 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
297 * Hash map of peer connections
299 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
302 * Temporary hash map of peer connections
304 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
307 * Temporary variable for topology creation, should be reset before
308 * creating any topology so the count is valid once finished.
315 * Data we keep per host.
325 * Lowest port that we have not yet used
333 * Handle to a group of GNUnet peers.
335 struct GNUNET_TESTING_PeerGroup
340 struct GNUNET_SCHEDULER_Handle *sched;
343 * Configuration template.
345 const struct GNUNET_CONFIGURATION_Handle *cfg;
348 * Function to call on each started daemon.
350 GNUNET_TESTING_NotifyDaemonRunning cb;
358 * Function to call on each topology connection created
360 GNUNET_TESTING_NotifyConnection notify_connection;
363 * Callback for notify_connection
365 void *notify_connection_cls;
368 * NULL-terminated array of information about
371 struct HostData *hosts;
374 * Array of "total" peers.
376 struct PeerData *peers;
379 * Number of peers in this group.
384 * At what time should we fail the peer startup process?
386 struct GNUNET_TIME_Absolute max_timeout;
390 * Convert unique ID to hash code.
392 * @param uid unique ID to convert
393 * @param hash set to uid (extended with zeros)
396 hash_from_uid (uint32_t uid,
397 GNUNET_HashCode *hash)
399 memset (hash, 0, sizeof(GNUNET_HashCode));
400 *((uint32_t*)hash) = uid;
404 * Convert hash code to unique ID.
406 * @param uid unique ID to convert
407 * @param hash set to uid (extended with zeros)
410 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
412 memcpy (uid, hash, sizeof(uint32_t));
417 struct GNUNET_CONFIGURATION_Handle *ret;
418 const char *hostname;
424 struct ConnectContext
426 struct GNUNET_TESTING_Daemon *first;
428 struct GNUNET_TESTING_Daemon *second;
430 struct GNUNET_TESTING_PeerGroup *pg;
434 * Number of connects we are waiting on, allows us to rate limit
437 static int outstanding_connects;
440 * Get a topology from a string input.
442 * @param topology where to write the retrieved topology
443 * @param topology_string The string to attempt to
444 * get a configuration value from
445 * @return GNUNET_YES if topology string matched a
446 * known topology, GNUNET_NO if not
449 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string)
454 if (topology_string == NULL)
459 if (strcmp(GNUNET_TESTING_TopologyStrings[curr], topology_string) == 0)
465 } while (strcmp(GNUNET_TESTING_TopologyStrings[curr], "NONE") != 0);
474 * Get connect topology option from string input.
476 * @param topology where to write the retrieved topology
477 * @param topology_string The string to attempt to
478 * get a configuration value from
479 * @return GNUNET_YES if string matched a known
480 * topology option, GNUNET_NO if not
483 GNUNET_TESTING_topology_option_get(enum GNUNET_TESTING_TopologyOption *topology, char * topology_string)
488 if (topology_string == NULL)
493 if (strcmp(GNUNET_TESTING_TopologyOptionStrings[curr], topology_string) == 0)
499 } while (strcmp(GNUNET_TESTING_TopologyOptionStrings[curr], "CONNECT_NONE") != 0);
508 * Function to iterate over options. Copies
509 * the options to the target configuration,
510 * updating PORT values as needed.
513 * @param section name of the section
514 * @param option name of the option
515 * @param value value of the option
518 update_config (void *cls,
519 const char *section, const char *option, const char *value)
521 struct UpdateContext *ctx = cls;
526 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
530 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
535 if (0 == strcmp (option, "UNIXPATH"))
537 GNUNET_snprintf (uval,
539 "/tmp/test-service-%s-%u",
545 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
547 value = ctx->hostname;
550 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
555 * Create a new configuration using the given configuration
556 * as a template; however, each PORT in the existing cfg
557 * must be renumbered by incrementing "*port". If we run
558 * out of "*port" numbers, return NULL.
560 * @param cfg template configuration
561 * @param port port numbers to use, update to reflect
562 * port numbers that were used
563 * @param upnum number to make unix domain socket names unique
564 * @param hostname hostname of the controlling host, to allow control connections from
566 * @return new configuration, NULL on error
568 static struct GNUNET_CONFIGURATION_Handle *
569 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
572 const char *hostname)
574 struct UpdateContext uc;
582 uc.ret = GNUNET_CONFIGURATION_create ();
583 uc.hostname = hostname;
585 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
586 if (uc.nport >= HIGH_PORT)
589 GNUNET_CONFIGURATION_destroy (uc.ret);
593 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
595 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
596 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
597 GNUNET_free_non_null(control_host);
598 GNUNET_free(allowed_hosts);
602 /* arm needs to know to allow connections from the host on which it is running,
603 * otherwise gnunet-arm is unable to connect to it in some instances */
604 if (hostname != NULL)
606 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
607 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
608 GNUNET_free(allowed_hosts);
611 *port = (uint16_t) uc.nport;
618 * Add entries to the peers connect list
620 * @param pg the peer group we are working with
621 * @param first index of the first peer
622 * @param second index of the second peer
624 * @return the number of connections added (can be 0, 1 or 2)
625 * technically should only be 0 or 2, but the small price
626 * of iterating over the lists (hashmaps in the future)
627 * for being sure doesn't bother me!
631 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
637 GNUNET_HashCode hash_first;
638 GNUNET_HashCode hash_second;
640 hash_from_uid(first, &hash_first);
641 hash_from_uid(second, &hash_second);
643 add_first = GNUNET_NO;
644 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
646 add_first = GNUNET_YES;
649 add_second = GNUNET_NO;
650 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
652 add_second = GNUNET_YES;
658 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
659 pg->peers[first].num_connections++;
665 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
666 pg->peers[second].num_connections++;
675 * Add entries to the peers allowed connections list
677 * @param pg the peer group we are working with
678 * @param first index of the first peer
679 * @param second index of the second peer
681 * @return the number of connections added (can be 0, 1 or 2)
682 * technically should only be 0 or 2, but the small price
683 * of iterating over the lists (hashmaps in the future)
684 * for being sure doesn't bother me!
688 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
692 struct PeerConnection *first_iter;
693 struct PeerConnection *second_iter;
694 struct PeerConnection *new_first;
695 struct PeerConnection *new_second;
700 GNUNET_HashCode hash_first;
701 GNUNET_HashCode hash_second;
703 hash_from_uid(first, &hash_first);
704 hash_from_uid(second, &hash_second);
706 add_first = GNUNET_NO;
707 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
709 add_first = GNUNET_YES;
712 add_second = GNUNET_NO;
713 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
715 add_second = GNUNET_YES;
718 first_iter = pg->peers[first].connected_peers;
719 while (first_iter != NULL)
721 if (first_iter->daemon == pg->peers[second].daemon)
722 add_first = GNUNET_NO;
723 first_iter = first_iter->next;
726 second_iter = pg->peers[second].connected_peers;
727 add_second = GNUNET_YES;
728 while (second_iter != NULL)
730 if (second_iter->daemon == pg->peers[first].daemon)
731 add_second = GNUNET_NO;
732 second_iter = second_iter->next;
739 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
741 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
742 new_first->daemon = pg->peers[second].daemon;
743 new_first->next = pg->peers[first].connected_peers;
744 pg->peers[first].connected_peers = new_first;
746 pg->peers[first].num_connections++;
752 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
754 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
755 new_second->daemon = pg->peers[first].daemon;
756 new_second->next = pg->peers[second].connected_peers;
757 pg->peers[second].connected_peers = new_second;
758 pg->peers[first].num_connections++;
760 pg->peers[second].num_connections++;
768 * Add entries to the peers blacklisted list
770 * @param pg the peer group we are working with
771 * @param first index of the first peer
772 * @param second index of the second peer
774 * @return the number of connections added (can be 0, 1 or 2)
778 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
783 GNUNET_HashCode hash_first;
784 GNUNET_HashCode hash_second;
786 hash_from_uid(first, &hash_first);
787 hash_from_uid(second, &hash_second);
789 add_first = GNUNET_NO;
790 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
792 add_first = GNUNET_YES;
795 add_second = GNUNET_NO;
796 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
798 add_second = GNUNET_YES;
804 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
805 pg->peers[first].num_connections++;
811 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
812 pg->peers[second].num_connections++;
820 * Remove entries from the peers blacklisted list
822 * @param pg the peer group we are working with
823 * @param first index of the first peer
824 * @param second index of the second peer
826 * @return the number of connections removed (can be 0, 1 or 2)
830 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
835 GNUNET_HashCode hash_first;
836 GNUNET_HashCode hash_second;
838 hash_from_uid(first, &hash_first);
839 hash_from_uid(second, &hash_second);
841 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
842 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
847 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
853 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
861 * Scale free network construction as described in:
863 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
865 * Start with a network of "one" peer, then progressively add
866 * peers up to the total number. At each step, iterate over
867 * all possible peers and connect new peer based on number of
868 * existing connections of the target peer.
870 * @param pg the peer group we are dealing with
871 * @param proc the connection processor to use
873 * @return the number of connections created
876 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
879 unsigned int total_connections;
880 unsigned int outer_count;
882 unsigned int previous_total_connections;
886 GNUNET_assert(pg->total > 1);
888 /* Add a connection between the first two nodes */
889 total_connections = proc(pg, 0, 1);
891 for (outer_count = 1; outer_count < pg->total; outer_count++)
893 previous_total_connections = total_connections;
894 for (i = 0; i < outer_count; i++)
896 probability = pg->peers[i].num_connections / (double)previous_total_connections;
897 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
898 UINT64_MAX)) / ( (double) UINT64_MAX);
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "Considering connecting peer %d to peer %d\n",
904 if (random < probability)
907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
908 "Connecting peer %d to peer %d\n",
911 total_connections += proc(pg, outer_count, i);
916 return total_connections;
920 * Create a topology given a peer group (set of running peers)
921 * and a connection processor.
923 * @param pg the peergroup to create the topology on
924 * @param proc the connection processor to call to actually set
925 * up connections between two peers
927 * @return the number of connections that were set up
931 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
936 unsigned int randomPeer;
937 double random, logNModifier, percentage;
938 unsigned int smallWorldConnections;
944 int connect_attempts;
946 logNModifier = 0.5; /* FIXME: default value? */
947 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
952 if (sscanf(p_string, "%lf", &logNModifier) != 1)
953 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
954 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
958 GNUNET_free (p_string);
960 percentage = 0.5; /* FIXME: default percentage? */
961 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
966 if (sscanf(p_string, "%lf", &percentage) != 1)
967 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
968 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
972 GNUNET_free (p_string);
974 natLog = log (pg->total);
975 connsPerPeer = ceil (natLog * logNModifier);
977 if (connsPerPeer % 2 == 1)
980 smallWorldConnections = 0;
981 connect_attempts = 0;
982 for (i = 0; i < pg->total; i++)
985 max = i + connsPerPeer / 2;
986 min = i - connsPerPeer / 2;
988 if (max > pg->total - 1)
990 max = max - pg->total;
996 min = pg->total - 1 + min;
1000 for (j = 0; j < connsPerPeer / 2; j++)
1002 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1003 UINT64_MAX) / ( (double) UINT64_MAX));
1004 if (random < percentage)
1006 /* Connect to uniformly selected random peer */
1008 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1010 while ((((randomPeer < max) && (randomPeer > min))
1011 && (useAnd == 0)) || (((randomPeer > min)
1012 || (randomPeer < max))
1016 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1019 smallWorldConnections +=
1020 proc (pg, i, randomPeer);
1024 nodeToConnect = i + j + 1;
1025 if (nodeToConnect > pg->total - 1)
1027 nodeToConnect = nodeToConnect - pg->total;
1030 proc (pg, i, nodeToConnect);
1036 connect_attempts += smallWorldConnections;
1038 return connect_attempts;
1042 * Create a topology given a peer group (set of running peers)
1043 * and a connection processor.
1045 * @param pg the peergroup to create the topology on
1046 * @param proc the connection processor to call to actually set
1047 * up connections between two peers
1049 * @return the number of connections that were set up
1053 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1055 unsigned int outer_count, inner_count;
1056 unsigned int cutoff;
1057 int connect_attempts;
1058 double nat_percentage;
1061 nat_percentage = 0.6; /* FIXME: default percentage? */
1062 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1067 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1068 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1069 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1073 GNUNET_free (p_string);
1078 cutoff = (unsigned int) (nat_percentage * pg->total);
1080 connect_attempts = 0;
1082 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1084 for (inner_count = outer_count + 1; inner_count < pg->total;
1087 if ((outer_count > cutoff) || (inner_count > cutoff))
1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1091 "Connecting peer %d to peer %d\n",
1092 outer_count, inner_count);
1094 connect_attempts += proc(pg, outer_count, inner_count);
1099 return connect_attempts;
1104 * Create a topology given a peer group (set of running peers)
1105 * and a connection processor.
1107 * @param pg the peergroup to create the topology on
1108 * @param proc the connection processor to call to actually set
1109 * up connections between two peers
1111 * @return the number of connections that were set up
1115 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1117 unsigned int i, j, k;
1118 unsigned int square;
1121 unsigned int toggle = 1;
1122 unsigned int nodeToConnect;
1123 unsigned int natLog;
1124 unsigned int node1Row;
1125 unsigned int node1Col;
1126 unsigned int node2Row;
1127 unsigned int node2Col;
1128 unsigned int distance;
1129 double probability, random, percentage;
1130 unsigned int smallWorldConnections;
1132 int connect_attempts;
1133 square = floor (sqrt (pg->total));
1137 percentage = 0.5; /* FIXME: default percentage? */
1138 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1143 if (sscanf(p_string, "%lf", &percentage) != 1)
1144 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1145 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1149 GNUNET_free (p_string);
1151 probability = 0.5; /* FIXME: default percentage? */
1152 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1157 if (sscanf(p_string, "%lf", &probability) != 1)
1158 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1159 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1163 GNUNET_free (p_string);
1165 if (square * square != pg->total)
1167 while (rows * cols < pg->total)
1169 if (toggle % 2 == 0)
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1179 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1183 connect_attempts = 0;
1184 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1185 * to the node to its right and above. Once this is over, we'll have our torus!
1186 * Special case for the last node (if the rows and columns are not equal), connect
1187 * to the first in the row to maintain topology.
1189 for (i = 0; i < pg->total; i++)
1191 /* First connect to the node to the right */
1192 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1193 nodeToConnect = i + 1;
1194 else if (i + 1 == pg->total)
1195 nodeToConnect = rows * cols - cols;
1197 nodeToConnect = i - cols + 1;
1199 connect_attempts += proc (pg, i, nodeToConnect);
1202 nodeToConnect = (rows * cols) - cols + i;
1204 nodeToConnect = i - cols;
1206 if (nodeToConnect < pg->total)
1207 connect_attempts += proc (pg, i, nodeToConnect);
1209 natLog = log (pg->total);
1210 #if VERBOSE_TESTING > 2
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1212 _("natural log of %d is %d, will run %d iterations\n"),
1213 pg->total, natLog, (int) (natLog * percentage));
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1216 smallWorldConnections = 0;
1217 for (i = 0; i < (int) (natLog * percentage); i++)
1219 for (j = 0; j < pg->total; j++)
1221 /* Determine the row and column of node at position j on the 2d torus */
1222 node1Row = j / cols;
1223 node1Col = j - (node1Row * cols);
1224 for (k = 0; k < pg->total; k++)
1226 /* Determine the row and column of node at position k on the 2d torus */
1227 node2Row = k / cols;
1228 node2Col = k - (node2Row * cols);
1229 /* Simple Cartesian distance */
1230 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1233 /* Calculate probability as 1 over the square of the distance */
1234 probability = 1.0 / (distance * distance);
1235 /* Choose a random value between 0 and 1 */
1236 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1237 UINT64_MAX)) / ( (double) UINT64_MAX);
1238 /* If random < probability, then connect the two nodes */
1239 if (random < probability)
1240 smallWorldConnections += proc (pg, j, k);
1246 connect_attempts += smallWorldConnections;
1247 #if VERBOSE_TESTING > 2
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 _("Total connections added for small world: %d!\n"),
1250 smallWorldConnections);
1252 return connect_attempts;
1256 * Create a topology given a peer group (set of running peers)
1257 * and a connection processor.
1259 * @param pg the peergroup to create the topology on
1260 * @param proc the connection processor to call to actually set
1261 * up connections between two peers
1263 * @return the number of connections that were set up
1267 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1270 unsigned int outer_count;
1271 unsigned int inner_count;
1272 int connect_attempts;
1276 probability = 0.5; /* FIXME: default percentage? */
1277 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1282 if (sscanf(p_string, "%lf", &probability) != 1)
1283 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1284 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1288 GNUNET_free (p_string);
1290 connect_attempts = 0;
1291 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1293 for (inner_count = outer_count + 1; inner_count < pg->total;
1296 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1297 UINT64_MAX)) / ( (double) UINT64_MAX);
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1300 _("rand is %f probability is %f\n"), temp_rand,
1303 if (temp_rand < probability)
1305 connect_attempts += proc (pg, outer_count, inner_count);
1310 return connect_attempts;
1314 * Create a topology given a peer group (set of running peers)
1315 * and a connection processor.
1317 * @param pg the peergroup to create the topology on
1318 * @param proc the connection processor to call to actually set
1319 * up connections between two peers
1321 * @return the number of connections that were set up
1325 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1328 unsigned int square;
1331 unsigned int toggle = 1;
1332 unsigned int nodeToConnect;
1333 int connect_attempts;
1335 connect_attempts = 0;
1337 square = floor (sqrt (pg->total));
1341 if (square * square != pg->total)
1343 while (rows * cols < pg->total)
1345 if (toggle % 2 == 0)
1354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1355 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1358 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1359 * to the node to its right and above. Once this is over, we'll have our torus!
1360 * Special case for the last node (if the rows and columns are not equal), connect
1361 * to the first in the row to maintain topology.
1363 for (i = 0; i < pg->total; i++)
1365 /* First connect to the node to the right */
1366 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1367 nodeToConnect = i + 1;
1368 else if (i + 1 == pg->total)
1369 nodeToConnect = rows * cols - cols;
1371 nodeToConnect = i - cols + 1;
1373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1374 "Connecting peer %d to peer %d\n",
1377 connect_attempts += proc(pg, i, nodeToConnect);
1379 /* Second connect to the node immediately above */
1381 nodeToConnect = (rows * cols) - cols + i;
1383 nodeToConnect = i - cols;
1385 if (nodeToConnect < pg->total)
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1389 "Connecting peer %d to peer %d\n",
1392 connect_attempts += proc(pg, i, nodeToConnect);
1397 return connect_attempts;
1402 * Create a topology given a peer group (set of running peers)
1403 * and a connection processor.
1405 * @param pg the peergroup to create the topology on
1406 * @param proc the connection processor to call to actually set
1407 * up connections between two peers
1409 * @return the number of connections that were set up
1413 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1415 unsigned int outer_count;
1416 unsigned int inner_count;
1417 int connect_attempts;
1419 connect_attempts = 0;
1421 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1423 for (inner_count = outer_count + 1; inner_count < pg->total;
1427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1428 "Connecting peer %d to peer %d\n",
1429 outer_count, inner_count);
1431 connect_attempts += proc(pg, outer_count, inner_count);
1435 return connect_attempts;
1439 * Create a topology given a peer group (set of running peers)
1440 * and a connection processor.
1442 * @param pg the peergroup to create the topology on
1443 * @param proc the connection processor to call to actually set
1444 * up connections between two peers
1446 * @return the number of connections that were set up
1450 create_line (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1453 int connect_attempts;
1455 connect_attempts = 0;
1457 /* Connect each peer to the next highest numbered peer */
1458 for (count = 0; count < pg->total - 1; count++)
1461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1462 "Connecting peer %d to peer %d\n",
1465 connect_attempts += proc(pg, count, count + 1);
1468 return connect_attempts;
1472 * Create a topology given a peer group (set of running peers)
1473 * and a connection processor.
1475 * @param pg the peergroup to create the topology on
1476 * @param proc the connection processor to call to actually set
1477 * up connections between two peers
1479 * @return the number of connections that were set up
1483 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1486 int connect_attempts;
1488 connect_attempts = 0;
1490 /* Connect each peer to the next highest numbered peer */
1491 for (count = 0; count < pg->total - 1; count++)
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1495 "Connecting peer %d to peer %d\n",
1498 connect_attempts += proc(pg, count, count + 1);
1501 /* Connect the last peer to the first peer */
1502 connect_attempts += proc(pg, pg->total - 1, 0);
1504 return connect_attempts;
1509 * Iterator for writing friends of a peer to a file.
1511 * @param cls closure, an open writable file handle
1512 * @param key the key the daemon was stored under
1513 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1515 * @return GNUNET_YES to continue iteration
1517 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1518 * with a single file_iterator that takes a closure which contains
1519 * the prefix to write before the peer. Then this could be used
1520 * for blacklisting multiple transports and writing the friend
1521 * file. I'm sure *someone* will complain loudly about other
1522 * things that negate these functions even existing so no point in
1526 friend_file_iterator (void *cls,
1527 const GNUNET_HashCode * key,
1530 FILE *temp_friend_handle = cls;
1531 struct GNUNET_TESTING_Daemon *peer = value;
1532 struct GNUNET_PeerIdentity *temppeer;
1533 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1535 temppeer = &peer->id;
1536 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1537 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1542 struct BlacklistContext
1545 * The (open) file handle to write to
1547 FILE *temp_file_handle;
1550 * The transport that this peer will be blacklisted on.
1556 * Iterator for writing blacklist data to appropriate files.
1558 * @param cls closure, an open writable file handle
1559 * @param key the key the daemon was stored under
1560 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1562 * @return GNUNET_YES to continue iteration
1565 blacklist_file_iterator (void *cls,
1566 const GNUNET_HashCode * key,
1569 struct BlacklistContext *blacklist_ctx = cls;
1570 //FILE *temp_blacklist_handle = cls;
1571 struct GNUNET_TESTING_Daemon *peer = value;
1572 struct GNUNET_PeerIdentity *temppeer;
1573 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1575 temppeer = &peer->id;
1576 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1577 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1583 * Create the friend files based on the PeerConnection's
1584 * of each peer in the peer group, and copy the files
1585 * to the appropriate place
1587 * @param pg the peer group we are dealing with
1590 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1592 FILE *temp_friend_handle;
1593 unsigned int pg_iter;
1594 char *temp_service_path;
1598 enum GNUNET_OS_ProcessStatusType type;
1599 unsigned long return_code;
1604 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1605 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1607 mytemp = GNUNET_DISK_mktemp("friends");
1608 GNUNET_assert(mytemp != NULL);
1609 temp_friend_handle = fopen (mytemp, "wt");
1610 GNUNET_assert(temp_friend_handle != NULL);
1611 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1612 fclose(temp_friend_handle);
1615 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1617 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1618 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1621 if (UNLINK (mytemp) != 0)
1622 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1623 GNUNET_free (mytemp);
1627 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1629 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1630 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1631 "mv", mytemp, arg, NULL);
1633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1634 _("Copying file with command cp %s %s\n"), mytemp, arg);
1639 else /* Remote, scp the file to the correct place */
1641 if (NULL != pg->peers[pg_iter].daemon->username)
1642 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1644 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1645 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1646 "scp", mytemp, arg, NULL);
1649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1650 _("Copying file with command scp %s %s\n"), mytemp, arg);
1654 GNUNET_free (temp_service_path);
1655 GNUNET_free (mytemp);
1659 ret = GNUNET_SYSERR;
1660 while ((count < max_wait) && (ret != GNUNET_OK))
1663 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1667 _("Checking copy status of file %d\n"), pg_iter);
1669 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1671 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1673 ret = GNUNET_SYSERR;
1675 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1677 ret = GNUNET_SYSERR;
1681 pidarr[pg_iter] = 0;
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1684 _("File %d copied\n"), pg_iter);
1690 if (ret == GNUNET_SYSERR)
1692 /* FIXME: why sleep here? -CG */
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1699 _("Finished copying all friend files!\n"));
1701 GNUNET_free(pidarr);
1707 * Create the blacklist files based on the PeerConnection's
1708 * of each peer in the peer group, and copy the files
1709 * to the appropriate place.
1711 * @param pg the peer group we are dealing with
1712 * @param transports space delimited list of transports to blacklist
1715 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
1717 FILE *temp_file_handle;
1718 static struct BlacklistContext blacklist_ctx;
1719 unsigned int pg_iter;
1720 char *temp_service_path;
1724 enum GNUNET_OS_ProcessStatusType type;
1725 unsigned long return_code;
1732 char *temp_transports;
1734 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1735 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1737 mytemp = GNUNET_DISK_mktemp("blacklist");
1738 GNUNET_assert(mytemp != NULL);
1739 temp_file_handle = fopen (mytemp, "wt");
1740 GNUNET_assert(temp_file_handle != NULL);
1741 temp_transports = GNUNET_strdup(transports);
1742 blacklist_ctx.temp_file_handle = temp_file_handle;
1743 transport_len = strlen(temp_transports) + 1;
1746 for (i = 0; i < transport_len; i++)
1748 if ((temp_transports[i] == ' ') && (pos == NULL))
1749 continue; /* At start of string (whitespace) */
1750 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
1752 temp_transports[i] = '\0';
1753 blacklist_ctx.transport = pos;
1754 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
1756 } /* At beginning of actual string */
1757 else if (pos == NULL)
1759 pos = &temp_transports[i];
1763 GNUNET_free (temp_transports);
1764 fclose(temp_file_handle);
1767 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1769 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1770 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1773 if (UNLINK (mytemp) != 0)
1774 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1775 GNUNET_free (mytemp);
1779 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1781 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
1782 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1783 "mv", mytemp, arg, NULL);
1785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786 _("Copying file with command cp %s %s\n"), mytemp, arg);
1791 else /* Remote, scp the file to the correct place */
1793 if (NULL != pg->peers[pg_iter].daemon->username)
1794 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1796 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1797 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1798 "scp", mytemp, arg, NULL);
1801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1802 _("Copying file with command scp %s %s\n"), mytemp, arg);
1806 GNUNET_free (temp_service_path);
1807 GNUNET_free (mytemp);
1811 ret = GNUNET_SYSERR;
1812 while ((count < max_wait) && (ret != GNUNET_OK))
1815 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1819 _("Checking copy status of file %d\n"), pg_iter);
1821 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1823 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1825 ret = GNUNET_SYSERR;
1827 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1829 ret = GNUNET_SYSERR;
1833 pidarr[pg_iter] = 0;
1835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1836 _("File %d copied\n"), pg_iter);
1842 if (ret == GNUNET_SYSERR)
1844 /* FIXME: why sleep here? -CG */
1850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1851 _("Finished copying all blacklist files!\n"));
1853 GNUNET_free(pidarr);
1859 * Internal notification of a connection, kept so that we can ensure some connections
1860 * happen instead of flooding all testing daemons with requests to connect.
1862 static void internal_connect_notify (void *cls,
1863 const struct GNUNET_PeerIdentity *first,
1864 const struct GNUNET_PeerIdentity *second,
1866 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
1867 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
1868 struct GNUNET_TESTING_Daemon *first_daemon,
1869 struct GNUNET_TESTING_Daemon *second_daemon,
1872 struct GNUNET_TESTING_PeerGroup *pg = cls;
1873 outstanding_connects--;
1876 pg->notify_connection(pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
1878 pg->notify_connection(pg->notify_connection_cls,
1881 first_cfg, second_cfg,
1882 first_daemon, second_daemon,
1889 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1891 struct ConnectContext *connect_context = cls;
1893 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1896 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
1898 #if VERBOSE_TESTING > 2
1899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1900 _("Delaying connect, we have too many outstanding connections!\n"));
1902 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &schedule_connect, connect_context);
1906 #if VERBOSE_TESTING > 2
1907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1908 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
1910 outstanding_connects++;
1911 GNUNET_TESTING_daemons_connect (connect_context->first,
1912 connect_context->second,
1915 &internal_connect_notify,
1916 connect_context->pg);
1917 GNUNET_free(connect_context);
1923 * Iterator for actually scheduling connections to be created
1924 * between two peers.
1926 * @param cls closure, a GNUNET_TESTING_Daemon
1927 * @param key the key the second Daemon was stored under
1928 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1930 * @return GNUNET_YES to continue iteration
1933 connect_iterator (void *cls,
1934 const GNUNET_HashCode * key,
1937 struct PeerData *first = cls;
1938 struct GNUNET_TESTING_Daemon *second = value;
1939 struct ConnectContext *connect_context;
1941 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1942 connect_context->pg = first->pg;
1943 connect_context->first = first->daemon;
1944 connect_context->second = second;
1945 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
1952 * Iterator for copying all entries in the allowed hashmap to the
1955 * @param cls closure, a GNUNET_TESTING_Daemon
1956 * @param key the key the second Daemon was stored under
1957 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1959 * @return GNUNET_YES to continue iteration
1962 copy_topology_iterator (void *cls,
1963 const GNUNET_HashCode * key,
1966 struct PeerData *first = cls;
1968 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1974 * Make the peers to connect the same as those that are allowed to be
1977 * @param pg the peer group
1980 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
1982 unsigned int pg_iter;
1987 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1989 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
1990 if (GNUNET_SYSERR == ret)
1991 return GNUNET_SYSERR;
1993 total = total + ret;
2001 * Connect the topology as specified by the PeerConnection's
2002 * of each peer in the peer group
2004 * @param pg the peer group we are dealing with
2005 * @return the number of connections that will be attempted
2008 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
2010 unsigned int pg_iter;
2014 struct PeerConnection *connection_iter;
2015 struct ConnectContext *connect_context;
2019 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2021 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
2022 if (GNUNET_SYSERR == ret)
2023 return GNUNET_SYSERR;
2025 total = total + ret;
2029 while (connection_iter != NULL)
2031 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2032 connect_context->pg = pg;
2033 connect_context->first = ;
2034 connect_context->second = connection_iter->daemon;
2035 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
2036 connection_iter = connection_iter->next;
2045 * Takes a peer group and creates a topology based on the
2046 * one specified. Creates a topology means generates friend
2047 * files for the peers so they can only connect to those allowed
2048 * by the topology. This will only have an effect once peers
2049 * are started if the FRIENDS_ONLY option is set in the base
2050 * config. Also takes an optional restrict topology which
2051 * disallows direct TCP connections UNLESS they are specified in
2052 * the restricted topology.
2054 * @param pg the peer group struct representing the running peers
2055 * @param topology which topology to connect the peers in
2056 * @param restrict_topology allow only direct TCP connections in this topology
2057 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2058 * @param restrict_transports space delimited list of transports to blacklist
2059 * to create restricted topology
2061 * @return the maximum number of connections were all allowed peers
2062 * connected to each other
2065 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2066 enum GNUNET_TESTING_Topology topology,
2067 enum GNUNET_TESTING_Topology restrict_topology,
2068 char *restrict_transports)
2071 int num_connections;
2072 int unblacklisted_connections;
2074 GNUNET_assert (pg->notify_connection != NULL);
2079 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2080 #if VERBOSE_TOPOLOGY
2081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2082 _("Creating clique topology\n"));
2084 num_connections = create_clique (pg, &add_allowed_connections);
2086 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2087 #if VERBOSE_TOPOLOGY
2088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2089 _("Creating small world (ring) topology\n"));
2091 num_connections = create_small_world_ring (pg, &add_allowed_connections);
2093 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2094 #if VERBOSE_TOPOLOGY
2095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2096 _("Creating small world (2d-torus) topology\n"));
2098 num_connections = create_small_world (pg, &add_allowed_connections);
2100 case GNUNET_TESTING_TOPOLOGY_RING:
2101 #if VERBOSE_TOPOLOGY
2102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2103 _("Creating ring topology\n"));
2105 num_connections = create_ring (pg, &add_allowed_connections);
2107 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2108 #if VERBOSE_TOPOLOGY
2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2110 _("Creating 2d torus topology\n"));
2112 num_connections = create_2d_torus (pg, &add_allowed_connections);
2114 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2115 #if VERBOSE_TOPOLOGY
2116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2117 _("Creating Erdos-Renyi topology\n"));
2119 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2121 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2122 #if VERBOSE_TOPOLOGY
2123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2124 _("Creating InterNAT topology\n"));
2126 num_connections = create_nated_internet (pg, &add_allowed_connections);
2128 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2131 _("Creating Scale Free topology\n"));
2133 num_connections = create_scale_free (pg, &add_allowed_connections);
2135 case GNUNET_TESTING_TOPOLOGY_LINE:
2137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2138 _("Creating straight line topology\n"));
2140 num_connections = create_line (pg, &add_allowed_connections);
2142 case GNUNET_TESTING_TOPOLOGY_NONE:
2143 num_connections = 0;
2146 num_connections = 0;
2149 if (num_connections < 1)
2150 return GNUNET_SYSERR;
2152 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2154 ret = create_and_copy_friend_files(pg);
2157 if (ret != GNUNET_OK)
2160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2161 _("Failed during friend file copying!\n"));
2163 return GNUNET_SYSERR;
2168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2169 _("Friend files created/copied successfully!\n"));
2173 /* Use the create clique method to initially set all connections as blacklisted. */
2174 create_clique (pg, &blacklist_connections);
2175 unblacklisted_connections = 0;
2176 /* Un-blacklist connections as per the topology specified */
2177 switch (restrict_topology)
2179 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2182 _("Blacklisting all but clique topology\n"));
2184 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2186 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2189 _("Blacklisting all but small world (ring) topology\n"));
2191 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2193 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2196 _("Blacklisting all but small world (2d-torus) topology\n"));
2198 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2200 case GNUNET_TESTING_TOPOLOGY_RING:
2202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2203 _("Blacklisting all but ring topology\n"));
2205 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2207 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2210 _("Blacklisting all but 2d torus topology\n"));
2212 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2214 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2217 _("Blacklisting all but Erdos-Renyi topology\n"));
2219 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2221 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2224 _("Blacklisting all but InterNAT topology\n"));
2226 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2228 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2231 _("Blacklisting all but Scale Free topology\n"));
2233 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2235 case GNUNET_TESTING_TOPOLOGY_LINE:
2237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2238 _("Blacklisting all but straight line topology\n"));
2240 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2242 case GNUNET_TESTING_TOPOLOGY_NONE:
2248 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2250 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2251 if (ret != GNUNET_OK)
2254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2255 _("Failed during blacklist file copying!\n"));
2257 return GNUNET_SYSERR;
2262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2263 _("Blacklist files created/copied successfully!\n"));
2267 return num_connections;
2270 struct RandomContext
2275 struct GNUNET_TESTING_PeerGroup *pg;
2278 * uid of the first peer
2283 * Peer data for first peer.
2285 struct PeerData *first;
2288 * Random percentage to use
2293 struct MinimumContext
2298 struct GNUNET_TESTING_PeerGroup *pg;
2301 * uid of the first peer
2306 * Peer data for first peer.
2308 struct PeerData *first;
2311 * Number of conns per peer
2313 unsigned int num_to_add;
2316 * Permuted array of all possible connections. Only add the Nth
2317 * peer if it's in the Nth position.
2319 unsigned int *pg_array;
2322 * What number is the current element we are iterating over?
2324 unsigned int current;
2332 struct GNUNET_TESTING_PeerGroup *pg;
2335 * uid of the first peer
2340 * uid of the second peer
2342 uint32_t second_uid;
2345 * Peer data for first peer.
2347 struct PeerData *first;
2350 * Which peer has been chosen as the one to add?
2352 unsigned int chosen;
2355 * What number is the current element we are iterating over?
2357 unsigned int current;
2361 * Iterator for choosing random peers to connect.
2363 * @param cls closure, a RandomContext
2364 * @param key the key the second Daemon was stored under
2365 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2367 * @return GNUNET_YES to continue iteration
2370 random_connect_iterator (void *cls,
2371 const GNUNET_HashCode * key,
2374 struct RandomContext *random_ctx = cls;
2375 double random_number;
2376 uint32_t second_pos;
2377 GNUNET_HashCode first_hash;
2378 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2379 UINT64_MAX)) / ( (double) UINT64_MAX);
2380 if (random_number < random_ctx->percentage)
2382 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2384 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2385 uid_from_hash(key, &second_pos);
2386 hash_from_uid(random_ctx->first_uid, &first_hash);
2387 GNUNET_assert(random_ctx->pg->total > second_pos);
2388 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2394 * Iterator for adding at least X peers to a peers connection set.
2396 * @param cls closure, MinimumContext
2397 * @param key the key the second Daemon was stored under
2398 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2400 * @return GNUNET_YES to continue iteration
2403 minimum_connect_iterator (void *cls,
2404 const GNUNET_HashCode * key,
2407 struct MinimumContext *min_ctx = cls;
2408 uint32_t second_pos;
2409 GNUNET_HashCode first_hash;
2412 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2414 for (i = 0; i < min_ctx->num_to_add; i++)
2416 if (min_ctx->pg_array[i] == min_ctx->current)
2418 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2419 uid_from_hash(key, &second_pos);
2420 hash_from_uid(min_ctx->first_uid, &first_hash);
2421 GNUNET_assert(min_ctx->pg->total > second_pos);
2422 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));
2423 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2424 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2431 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2437 * Iterator for adding peers to a connection set based on a depth first search.
2439 * @param cls closure, MinimumContext
2440 * @param key the key the second daemon was stored under
2441 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2443 * @return GNUNET_YES to continue iteration
2446 dfs_connect_iterator (void *cls,
2447 const GNUNET_HashCode * key,
2450 struct DFSContext *dfs_ctx = cls;
2451 GNUNET_HashCode first_hash;
2453 if (dfs_ctx->current == dfs_ctx->chosen)
2455 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2456 uid_from_hash(key, &dfs_ctx->second_uid);
2457 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2458 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));
2459 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2460 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2461 return GNUNET_NO; /* We have found our peer, don't iterate more */
2470 * From the set of connections possible, choose percentage percent of connections
2471 * to actually connect.
2473 * @param pg the peergroup we are dealing with
2474 * @param percentage what percent of total connections to make
2477 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2479 struct RandomContext random_ctx;
2482 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2484 random_ctx.first_uid = pg_iter;
2485 random_ctx.first = &pg->peers[pg_iter];
2486 random_ctx.percentage = percentage;
2488 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2489 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2490 /* Now remove the old connections */
2491 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2492 /* And replace with the random set */
2493 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2498 * From the set of connections possible, choose at least num connections per
2501 * @param pg the peergroup we are dealing with
2502 * @param num how many connections at least should each peer have (if possible)?
2505 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2507 struct MinimumContext minimum_ctx;
2510 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2512 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2515 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2517 minimum_ctx.first_uid = pg_iter;
2518 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2519 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2520 minimum_ctx.first = &pg->peers[pg_iter];
2521 minimum_ctx.pg = pg;
2522 minimum_ctx.num_to_add = num;
2523 minimum_ctx.current = 0;
2524 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2525 &minimum_connect_iterator,
2529 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2531 /* Remove the "old" connections */
2532 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2533 /* And replace with the working set */
2534 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2541 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2544 unsigned int pg_iter;
2548 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2550 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2557 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2560 unsigned int pg_iter;
2564 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2566 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2573 * From the set of connections possible, choose at least num connections per
2574 * peer based on depth first traversal of peer connections. If DFS leaves
2575 * peers unconnected, ensure those peers get connections.
2577 * @param pg the peergroup we are dealing with
2578 * @param num how many connections at least should each peer have (if possible)?
2581 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2583 struct DFSContext dfs_ctx;
2586 uint32_t starting_peer;
2587 uint32_t least_connections;
2588 GNUNET_HashCode second_hash;
2590 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2592 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2597 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
2599 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
2601 least_connections = -1; /* Set to very high number */
2602 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2604 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
2606 starting_peer = pg_iter;
2607 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2612 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
2618 /* Choose a random peer from the chosen peers set of connections to add */
2619 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
2620 dfs_ctx.first_uid = starting_peer;
2621 dfs_ctx.first = &pg->peers[starting_peer];
2623 dfs_ctx.current = 0;
2625 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
2626 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
2627 hash_from_uid(dfs_ctx.second_uid, &second_hash);
2628 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
2629 starting_peer = dfs_ctx.second_uid;
2632 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2634 /* Remove the "old" connections */
2635 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2636 /* And replace with the working set */
2637 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2642 * There are many ways to connect peers that are supported by this function.
2643 * To connect peers in the same topology that was created via the
2644 * GNUNET_TESTING_create_topology, the topology variable must be set to
2645 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
2646 * a new instance of that topology will be generated and attempted to be
2647 * connected. This could result in some connections being impossible,
2648 * because some topologies are non-deterministic.
2650 * @param pg the peer group struct representing the running peers
2651 * @param topology which topology to connect the peers in
2652 * @param options options for connecting the topology
2653 * @param option_modifier modifier for options that take a parameter
2654 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
2657 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2658 enum GNUNET_TESTING_Topology topology,
2659 enum GNUNET_TESTING_TopologyOption options,
2660 double option_modifier)
2664 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2665 #if VERBOSE_TOPOLOGY
2666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2667 _("Creating clique CONNECT topology\n"));
2669 create_clique (pg, &add_actual_connections);
2671 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2672 #if VERBOSE_TOPOLOGY
2673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2674 _("Creating small world (ring) CONNECT topology\n"));
2676 create_small_world_ring (pg, &add_actual_connections);
2678 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2679 #if VERBOSE_TOPOLOGY
2680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2681 _("Creating small world (2d-torus) CONNECT topology\n"));
2683 create_small_world (pg, &add_actual_connections);
2685 case GNUNET_TESTING_TOPOLOGY_RING:
2686 #if VERBOSE_TOPOLOGY
2687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2688 _("Creating ring CONNECT topology\n"));
2690 create_ring (pg, &add_actual_connections);
2692 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2693 #if VERBOSE_TOPOLOGY
2694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2695 _("Creating 2d torus CONNECT topology\n"));
2697 create_2d_torus (pg, &add_actual_connections);
2699 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2700 #if VERBOSE_TOPOLOGY
2701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2702 _("Creating Erdos-Renyi CONNECT topology\n"));
2704 create_erdos_renyi (pg, &add_actual_connections);
2706 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2707 #if VERBOSE_TOPOLOGY
2708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2709 _("Creating InterNAT CONNECT topology\n"));
2711 create_nated_internet (pg, &add_actual_connections);
2713 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2714 #if VERBOSE_TOPOLOGY
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2716 _("Creating Scale Free CONNECT topology\n"));
2718 create_scale_free (pg, &add_actual_connections);
2720 case GNUNET_TESTING_TOPOLOGY_LINE:
2721 #if VERBOSE_TOPOLOGY
2722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2723 _("Creating straight line CONNECT topology\n"));
2725 create_line (pg, &add_actual_connections);
2727 case GNUNET_TESTING_TOPOLOGY_NONE:
2728 #if VERBOSE_TOPOLOGY
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2730 _("Creating no CONNECT topology\n"));
2732 copy_allowed_topology(pg);
2735 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2736 _("Unknown topology specification, can't connect peers!\n"));
2737 return GNUNET_SYSERR;
2742 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
2743 #if VERBOSE_TOPOLOGY
2744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2745 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
2747 choose_random_connections(pg, option_modifier);
2749 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
2750 #if VERBOSE_TOPOLOGY
2751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2752 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
2754 choose_minimum(pg, (unsigned int)option_modifier);
2756 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
2757 #if VERBOSE_TOPOLOGY
2758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2759 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
2761 perform_dfs(pg, (int)option_modifier);
2763 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
2765 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
2771 return connect_topology(pg);
2775 * Function which continues a peer group starting up
2776 * after successfully generating hostkeys for each peer.
2778 * @param pg the peer group to continue starting
2782 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
2786 for (i = 0; i < pg->total; i++)
2788 GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
2793 * Start count gnunetd processes with the same set of transports and
2794 * applications. The port numbers (any option called "PORT") will be
2795 * adjusted to ensure that no two peers running on the same system
2796 * have the same port(s) in their respective configurations.
2798 * @param sched scheduler to use
2799 * @param cfg configuration template to use
2800 * @param total number of daemons to start
2801 * @param timeout total time allowed for peers to start
2802 * @param hostkey_callback function to call on each peers hostkey generation
2803 * if NULL, peers will be started by this call, if non-null,
2804 * GNUNET_TESTING_daemons_continue_startup must be called after
2805 * successful hostkey generation
2806 * @param hostkey_cls closure for hostkey callback
2807 * @param cb function to call on each daemon that was started
2808 * @param cb_cls closure for cb
2809 * @param connect_callback function to call each time two hosts are connected
2810 * @param connect_callback_cls closure for connect_callback
2811 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
2812 * everything on localhost).
2813 * @return NULL on error, otherwise handle to control peer group
2815 struct GNUNET_TESTING_PeerGroup *
2816 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
2817 const struct GNUNET_CONFIGURATION_Handle *cfg,
2819 struct GNUNET_TIME_Relative timeout,
2820 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
2822 GNUNET_TESTING_NotifyDaemonRunning cb,
2824 GNUNET_TESTING_NotifyConnection
2825 connect_callback, void *connect_callback_cls,
2826 const char *hostnames)
2828 struct GNUNET_TESTING_PeerGroup *pg;
2832 const char *hostname;
2833 char *baseservicehome;
2834 char *newservicehome;
2836 struct GNUNET_CONFIGURATION_Handle *pcfg;
2838 unsigned int hostcnt;
2848 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
2852 pg->cb_cls = cb_cls;
2853 pg->notify_connection = connect_callback;
2854 pg->notify_connection_cls = connect_callback_cls;
2856 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
2857 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
2858 if (NULL != hostnames)
2861 /* skip leading spaces */
2862 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
2865 while ('\0' != *rpos)
2867 if (isspace ( (unsigned char) *rpos))
2871 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
2873 start = GNUNET_strdup (hostnames);
2875 while ('\0' != *pos)
2877 if (isspace ( (unsigned char) *pos))
2880 if (strlen (start) > 0)
2882 pg->hosts[off].minport = LOW_PORT;
2883 pg->hosts[off++].hostname = start;
2889 if (strlen (start) > 0)
2891 pg->hosts[off].minport = LOW_PORT;
2892 pg->hosts[off++].hostname = start;
2896 GNUNET_free (start);
2897 GNUNET_free (pg->hosts);
2901 minport = 0; /* make gcc happy */
2908 for (off = 0; off < total; off++)
2912 hostname = pg->hosts[off % hostcnt].hostname;
2913 pcfg = make_config (cfg,
2914 &pg->hosts[off % hostcnt].minport,
2921 pcfg = make_config (cfg,
2929 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2931 ("Could not create configuration for peer number %u on `%s'!\n"),
2932 off, hostname == NULL ? "localhost" : hostname);
2937 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
2940 GNUNET_asprintf (&newservicehome,
2941 "%s/%d/", baseservicehome, off);
2942 GNUNET_free (baseservicehome);
2946 tmpdir = getenv ("TMPDIR");
2947 tmpdir = tmpdir ? tmpdir : "/tmp";
2948 GNUNET_asprintf (&newservicehome,
2951 "gnunet-testing-test-test", off);
2953 GNUNET_CONFIGURATION_set_value_string (pcfg,
2955 "SERVICEHOME", newservicehome);
2956 GNUNET_free (newservicehome);
2957 pg->peers[off].cfg = pcfg;
2958 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
2959 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
2960 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
2961 pg->peers[off].pg = pg;
2962 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (sched,
2969 if (NULL == pg->peers[off].daemon)
2970 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2971 _("Could not start peer number %u!\n"), off);
2978 * Get a daemon by number, so callers don't have to do nasty
2979 * offsetting operation.
2981 struct GNUNET_TESTING_Daemon *
2982 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
2984 if (position < pg->total)
2985 return pg->peers[position].daemon;
2991 * Prototype of a function that will be called when a
2992 * particular operation was completed the testing library.
2994 * @param cls closure (a struct RestartContext)
2995 * @param id id of the peer that was restarted
2996 * @param cfg handle to the configuration of the peer
2997 * @param d handle to the daemon that was restarted
2998 * @param emsg NULL on success
3000 void restart_callback (void *cls,
3001 const struct GNUNET_PeerIdentity *id,
3002 const struct GNUNET_CONFIGURATION_Handle *cfg,
3003 struct GNUNET_TESTING_Daemon *d,
3006 struct RestartContext *restart_context = cls;
3010 restart_context->peers_restarted++;
3014 restart_context->peers_restart_failed++;
3017 if (restart_context->peers_restarted == restart_context->peer_group->total)
3019 restart_context->callback(restart_context->callback_cls, NULL);
3020 GNUNET_free(restart_context);
3022 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
3024 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
3025 GNUNET_free(restart_context);
3031 * Callback for informing us about a successful
3032 * or unsuccessful churn stop call.
3034 * @param cls a ChurnContext
3035 * @param emsg NULL on success, non-NULL on failure
3039 churn_stop_callback (void *cls, const char *emsg)
3041 struct ChurnContext *churn_ctx = cls;
3042 unsigned int total_left;
3043 char *error_message;
3045 error_message = NULL;
3048 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3049 "Churn stop callback failed with error `%s'\n", emsg);
3050 churn_ctx->num_failed_stop++;
3054 churn_ctx->num_to_stop--;
3058 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3059 "Stopped peer, %d left.\n",
3060 churn_ctx->num_to_stop);
3062 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3064 if (total_left == 0)
3066 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3068 GNUNET_asprintf(&error_message,
3069 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3070 churn_ctx->num_failed_start,
3071 churn_ctx->num_failed_stop);
3073 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3074 GNUNET_free_non_null(error_message);
3075 GNUNET_free(churn_ctx);
3080 * Callback for informing us about a successful
3081 * or unsuccessful churn start call.
3083 * @param cls a ChurnContext
3084 * @param id the peer identity of the started peer
3085 * @param cfg the handle to the configuration of the peer
3086 * @param d handle to the daemon for the peer
3087 * @param emsg NULL on success, non-NULL on failure
3091 churn_start_callback (void *cls,
3092 const struct GNUNET_PeerIdentity *id,
3093 const struct GNUNET_CONFIGURATION_Handle *cfg,
3094 struct GNUNET_TESTING_Daemon *d,
3097 struct ChurnContext *churn_ctx = cls;
3098 unsigned int total_left;
3099 char *error_message;
3101 error_message = NULL;
3104 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3105 "Churn stop callback failed with error `%s'\n",
3107 churn_ctx->num_failed_start++;
3111 churn_ctx->num_to_start--;
3115 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3116 "Started peer, %d left.\n",
3117 churn_ctx->num_to_start);
3120 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3122 if (total_left == 0)
3124 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3125 GNUNET_asprintf(&error_message,
3126 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3127 churn_ctx->num_failed_start,
3128 churn_ctx->num_failed_stop);
3129 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3130 GNUNET_free_non_null(error_message);
3131 GNUNET_free(churn_ctx);
3137 * Simulate churn by stopping some peers (and possibly
3138 * re-starting others if churn is called multiple times). This
3139 * function can only be used to create leave-join churn (peers "never"
3140 * leave for good). First "voff" random peers that are currently
3141 * online will be taken offline; then "von" random peers that are then
3142 * offline will be put back online. No notifications will be
3143 * generated for any of these operations except for the callback upon
3146 * @param pg handle for the peer group
3147 * @param voff number of peers that should go offline
3148 * @param von number of peers that should come back online;
3149 * must be zero on first call (since "testbed_start"
3150 * always starts all of the peers)
3151 * @param timeout how long to wait for operations to finish before
3153 * @param cb function to call at the end
3154 * @param cb_cls closure for cb
3157 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
3160 struct GNUNET_TIME_Relative timeout,
3161 GNUNET_TESTING_NotifyCompletion cb,
3164 struct ChurnContext *churn_ctx;
3165 unsigned int running;
3166 unsigned int stopped;
3168 unsigned int *running_arr;
3169 unsigned int *stopped_arr;
3170 unsigned int *running_permute;
3171 unsigned int *stopped_permute;
3176 if ((von == 0) && (voff == 0)) /* No peers at all? */
3182 for (i = 0; i < pg->total; i++)
3184 if (pg->peers[i].daemon->running == GNUNET_YES)
3186 GNUNET_assert(running != -1);
3191 GNUNET_assert(stopped != -1);
3198 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
3199 cb(cb_cls, "Trying to stop more peers than are currently running!");
3205 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
3206 cb(cb_cls, "Trying to start more peers than are currently stopped!");
3210 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3211 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
3212 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
3214 running_permute = NULL;
3215 stopped_permute = NULL;
3218 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
3220 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
3225 churn_ctx->num_to_start = von;
3226 churn_ctx->num_to_stop = voff;
3228 churn_ctx->cb_cls = cb_cls;
3230 for (i = 0; i < pg->total; i++)
3232 if (pg->peers[i].daemon->running == GNUNET_YES)
3234 running_arr[running] = i;
3239 stopped_arr[stopped] = i;
3244 for (i = 0; i < voff; i++)
3247 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
3249 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
3251 &churn_stop_callback, churn_ctx,
3252 GNUNET_NO, GNUNET_YES);
3255 for (i = 0; i < von; i++)
3258 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
3260 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
3261 timeout, &churn_start_callback, churn_ctx);
3264 GNUNET_free(running_arr);
3265 GNUNET_free(stopped_arr);
3266 GNUNET_free_non_null(running_permute);
3267 GNUNET_free_non_null(stopped_permute);
3272 * Restart all peers in the given group.
3274 * @param pg the handle to the peer group
3275 * @param callback function to call on completion (or failure)
3276 * @param callback_cls closure for the callback function
3279 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyCompletion callback, void *callback_cls)
3281 struct RestartContext *restart_context;
3286 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
3287 restart_context->peer_group = pg;
3288 restart_context->peers_restarted = 0;
3289 restart_context->callback = callback;
3290 restart_context->callback_cls = callback_cls;
3292 for (off = 0; off < pg->total; off++)
3294 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
3300 * Start or stop an individual peer from the given group.
3302 * @param pg handle to the peer group
3303 * @param offset which peer to start or stop
3304 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
3305 * @param timeout how long to wait for shutdown
3306 * @param cb function to call at the end
3307 * @param cb_cls closure for cb
3310 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
3311 unsigned int offset,
3313 struct GNUNET_TIME_Relative timeout,
3314 GNUNET_TESTING_NotifyCompletion cb,
3317 struct ChurnContext *churn_ctx;
3319 if (GNUNET_NO == desired_status)
3321 if (NULL != pg->peers[offset].daemon)
3323 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3324 churn_ctx->num_to_start = 0;
3325 churn_ctx->num_to_stop = 1;
3327 churn_ctx->cb_cls = cb_cls;
3328 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
3329 timeout, &churn_stop_callback, churn_ctx,
3330 GNUNET_NO, GNUNET_YES);
3333 else if (GNUNET_YES == desired_status)
3335 if (NULL == pg->peers[offset].daemon)
3337 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3338 churn_ctx->num_to_start = 1;
3339 churn_ctx->num_to_stop = 0;
3341 churn_ctx->cb_cls = cb_cls;
3342 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
3343 timeout, &churn_start_callback, churn_ctx);
3352 * Shutdown all peers started in the given group.
3354 * @param pg handle to the peer group
3355 * @param timeout how long to wait for shutdown
3358 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
3359 struct GNUNET_TIME_Relative timeout)
3363 for (off = 0; off < pg->total; off++)
3365 /* FIXME: should we wait for our continuations to be called
3366 here? This would require us to take a continuation as
3369 if (NULL != pg->peers[off].daemon)
3370 GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, NULL, NULL, GNUNET_YES, GNUNET_NO);
3371 if (NULL != pg->peers[off].cfg)
3372 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
3373 if (pg->peers[off].allowed_peers != NULL)
3374 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
3375 if (pg->peers[off].connect_peers != NULL)
3376 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
3377 if (pg->peers[off].blacklisted_peers != NULL)
3378 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
3380 GNUNET_free (pg->peers);
3381 if (NULL != pg->hosts)
3383 GNUNET_free (pg->hosts[0].hostname);
3384 GNUNET_free (pg->hosts);
3390 /* end of testing_group.c */