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 3, 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 Nathan Evans
25 * @author Christian Grothoff
29 #include "gnunet_arm_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_core_service.h"
33 #define VERBOSE_TESTING GNUNET_NO
35 #define VERBOSE_TOPOLOGY GNUNET_YES
37 #define DEBUG_CHURN GNUNET_NO
40 * Lowest port used for GNUnet testing. Should be high enough to not
41 * conflict with other applications running on the hosts but be low
42 * enough to not conflict with client-ports (typically starting around
45 #define LOW_PORT 10000
48 * Highest port used for GNUnet testing. Should be low enough to not
49 * conflict with the port range for "local" ports (client apps; see
50 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
52 #define HIGH_PORT 56000
54 #define MAX_OUTSTANDING_CONNECTIONS 40
56 #define MAX_CONCURRENT_HOSTKEYS 10
58 #define MAX_CONCURRENT_STARTING 10
60 #define MAX_CONCURRENT_SHUTDOWN 10
62 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 200)
64 #define CONNECT_ATTEMPTS 21
67 * Prototype of a function called whenever two peers would be connected
68 * in a certain topology.
70 typedef unsigned int (*GNUNET_TESTING_ConnectionProcessor) (struct
71 GNUNET_TESTING_PeerGroup
80 * Context for handling churning a peer group
85 * Callback used to notify of churning finished
87 GNUNET_TESTING_NotifyCompletion cb;
90 * Closure for callback
95 * Number of peers that still need to be started
97 unsigned int num_to_start;
100 * Number of peers that still need to be stopped
102 unsigned int num_to_stop;
105 * Number of peers that failed to start
107 unsigned int num_failed_start;
110 * Number of peers that failed to stop
112 unsigned int num_failed_stop;
115 struct RestartContext
118 * The group of peers being restarted
120 struct GNUNET_TESTING_PeerGroup *peer_group;
123 * How many peers have been restarted thus far
125 unsigned int peers_restarted;
128 * How many peers got an error when restarting
130 unsigned int peers_restart_failed;
133 * The function to call once all peers have been restarted
135 GNUNET_TESTING_NotifyCompletion callback;
138 * Closure for callback function
145 struct ShutdownContext
148 * Total peers to wait for
150 unsigned int total_peers;
153 * Number of peers successfully shut down
155 unsigned int peers_down;
158 * Number of peers failed to shut down
160 unsigned int peers_failed;
163 * Number of peers we have started shutting
164 * down. If too many, wait on them.
166 unsigned int outstanding;
169 * Timeout for shutdown.
171 struct GNUNET_TIME_Relative timeout;
174 * Callback to call when all peers either
175 * shutdown or failed to shutdown
177 GNUNET_TESTING_NotifyCompletion cb;
186 * Individual shutdown context for a particular peer.
188 struct PeerShutdownContext
191 * Pointer to the high level shutdown context.
193 struct ShutdownContext *shutdown_ctx;
196 * The daemon handle for the peer to shut down.
198 struct GNUNET_TESTING_Daemon *daemon;
202 * Individual shutdown context for a particular peer.
204 struct PeerRestartContext
207 * Pointer to the high level restart context.
209 struct ChurnRestartContext *churn_restart_ctx;
212 * The daemon handle for the peer to shut down.
214 struct GNUNET_TESTING_Daemon *daemon;
218 struct CreateTopologyContext
222 * Function to call with number of connections
224 GNUNET_TESTING_NotifyConnections cont;
227 * Closure for connection notification
233 struct PeerConnection
238 struct PeerConnection *next;
241 * Pointer to daemon handle
243 struct GNUNET_TESTING_Daemon *daemon;
248 struct InternalStartContext
251 * Pointer to peerdata
253 struct PeerData *peer;
256 * Timeout for peer startup
258 struct GNUNET_TIME_Relative timeout;
261 * Client callback for hostkey notification
263 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
266 * Closure for hostkey_callback
271 * Client callback for peer start notification
273 GNUNET_TESTING_NotifyDaemonRunning start_cb;
281 * Hostname, where to start the peer
283 const char *hostname;
286 * Username to use when connecting to the
289 const char *username;
292 * Port to use for ssh.
298 struct ChurnRestartContext
301 * Number of restarts currently in flight.
303 unsigned int outstanding;
306 * Handle to the underlying churn context.
308 struct ChurnContext *churn_ctx;
311 * How long to allow the operation to take.
313 struct GNUNET_TIME_Relative timeout;
317 * Data we keep per peer.
322 * (Initial) configuration of the host.
323 * (initial because clients could change
324 * it and we would not know about those
327 struct GNUNET_CONFIGURATION_Handle *cfg;
330 * Handle for controlling the daemon.
332 struct GNUNET_TESTING_Daemon *daemon;
335 * The peergroup this peer belongs to.
337 struct GNUNET_TESTING_PeerGroup *pg;
340 * Hash map of allowed peer connections (F2F created topology)
342 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
345 * Hash map of blacklisted peers
347 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
350 * Hash map of peer connections
352 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
355 * Temporary hash map of peer connections
357 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
360 * Temporary variable for topology creation, should be reset before
361 * creating any topology so the count is valid once finished.
366 * Context to keep track of peers being started, to
367 * stagger hostkey generation and peer startup.
369 struct InternalStartContext internal_context;
374 * Linked list of per-host data.
384 * SSH username to use when connecting to this host.
389 * SSH port to use when connecting to this host.
394 * Lowest port that we have not yet used
400 struct TopologyIterateContext
403 * Callback for notifying of two connected peers.
405 GNUNET_TESTING_NotifyTopology topology_cb;
408 * Closure for topology_cb
413 * Number of peers currently connected to.
415 unsigned int connected;
418 * Number of peers we have finished iterating.
420 unsigned int completed;
423 * Number of peers total.
428 struct StatsIterateContext
431 * Continuation to call once all stats information has been retrieved.
433 GNUNET_STATISTICS_Callback cont;
436 * Proc function to call on each value received.
438 GNUNET_TESTING_STATISTICS_Iterator proc;
441 * Closure for topology_cb
446 * Number of peers currently connected to.
448 unsigned int connected;
451 * Number of peers we have finished iterating.
453 unsigned int completed;
456 * Number of peers total.
464 struct GNUNET_TESTING_Daemon *daemon;
467 struct StatsCoreContext
470 struct GNUNET_TESTING_Daemon *daemon;
472 * Handle to the statistics service.
474 struct GNUNET_STATISTICS_Handle *stats_handle;
477 * Handle for getting statistics.
479 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
483 * Handle to a group of GNUnet peers.
485 struct GNUNET_TESTING_PeerGroup
488 * Configuration template.
490 const struct GNUNET_CONFIGURATION_Handle *cfg;
493 * Function to call on each started daemon.
495 //GNUNET_TESTING_NotifyDaemonRunning cb;
503 * Function to call on each topology connection created
505 GNUNET_TESTING_NotifyConnection notify_connection;
508 * Callback for notify_connection
510 void *notify_connection_cls;
513 * Array of information about hosts.
515 struct HostData *hosts;
518 * Number of hosts (size of HostData)
520 unsigned int num_hosts;
523 * Array of "total" peers.
525 struct PeerData *peers;
528 * Number of peers in this group.
533 * At what time should we fail the peer startup process?
535 struct GNUNET_TIME_Absolute max_timeout;
538 * How many peers are being started right now?
540 unsigned int starting;
543 * How many peers have already been started?
545 unsigned int started;
550 struct GNUNET_CONFIGURATION_Handle *ret;
551 const struct GNUNET_CONFIGURATION_Handle *orig;
552 const char *hostname;
558 struct ConnectTopologyContext
561 * How many connections are left to create.
563 unsigned int remaining_connections;
566 * Handle to group of peers.
568 struct GNUNET_TESTING_PeerGroup *pg;
571 * Temp value set for each iteration.
573 struct PeerData *first;
576 * Notification that all peers are connected.
578 GNUNET_TESTING_NotifyCompletion notify_connections_done;
581 * Closure for notify.
586 struct ConnectContext
589 * Peer to connect second to.
591 struct GNUNET_TESTING_Daemon *first;
594 * Peer to connect first to.
596 struct GNUNET_TESTING_Daemon *second;
599 * Higher level topology connection context.
601 struct ConnectTopologyContext *ct_ctx;
605 * Convert unique ID to hash code.
607 * @param uid unique ID to convert
608 * @param hash set to uid (extended with zeros)
611 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
613 memset (hash, 0, sizeof (GNUNET_HashCode));
614 *((uint32_t *) hash) = uid;
618 * Convert hash code to unique ID.
620 * @param uid unique ID to convert
621 * @param hash set to uid (extended with zeros)
624 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
626 memcpy (uid, hash, sizeof (uint32_t));
630 * Number of connects we are waiting on, allows us to rate limit
633 static int outstanding_connects;
636 * Get a topology from a string input.
638 * @param topology where to write the retrieved topology
639 * @param topology_string The string to attempt to
640 * get a configuration value from
641 * @return GNUNET_YES if topology string matched a
642 * known topology, GNUNET_NO if not
645 GNUNET_TESTING_topology_get (enum GNUNET_TESTING_Topology *topology,
646 const char *topology_string)
649 * Strings representing topologies in enum
651 static const char *topology_strings[] = {
653 * A clique (everyone connected to everyone else).
658 * Small-world network (2d torus plus random links).
663 * Small-world network (ring plus random links).
683 * Certain percentage of peers are unable to communicate directly
684 * replicating NAT conditions
689 * Scale free topology.
694 * Straight line topology.
699 * All peers are disconnected.
707 if (topology_string == NULL)
709 while (topology_strings[curr] != NULL)
711 if (strcasecmp (topology_strings[curr], topology_string) == 0)
718 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
724 * Get connect topology option from string input.
726 * @param topology_option where to write the retrieved topology
727 * @param topology_string The string to attempt to
728 * get a configuration value from
729 * @return GNUNET_YES if string matched a known
730 * topology option, GNUNET_NO if not
733 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption
735 const char *topology_string)
738 * Options for connecting a topology as strings.
740 static const char *topology_option_strings[] = {
742 * Try to connect all peers specified in the topology.
747 * Choose a random subset of connections to create.
749 "CONNECT_RANDOM_SUBSET",
752 * Create at least X connections for each peer.
757 * Using a depth first search, create one connection
758 * per peer. If any are missed (graph disconnected)
759 * start over at those peers until all have at least one
765 * Find the N closest peers to each allowed peer in the
766 * topology and make sure a connection to those peers
767 * exists in the connect topology.
772 * No options specified.
780 if (topology_string == NULL)
782 while (NULL != topology_option_strings[curr])
784 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
786 *topology_option = curr;
791 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
796 * Function to iterate over options. Copies
797 * the options to the target configuration,
798 * updating PORT values as needed.
801 * @param section name of the section
802 * @param option name of the option
803 * @param value value of the option
806 update_config (void *cls,
807 const char *section, const char *option, const char *value)
809 struct UpdateContext *ctx = cls;
813 char *single_variable;
814 char *per_host_variable;
815 unsigned long long num_per_host;
817 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
819 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
822 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
825 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
829 GNUNET_free (single_variable);
832 if (0 == strcmp (option, "UNIXPATH"))
834 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
835 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
837 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
840 GNUNET_snprintf (uval,
842 "/tmp/test-service-%s-%u", section, ctx->upnum++);
845 else if ((GNUNET_YES ==
846 GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
848 &num_per_host)) && (num_per_host > 0))
851 GNUNET_snprintf (uval,
853 "/tmp/test-service-%s-%u",
854 section, ctx->fdnum % num_per_host);
857 GNUNET_free (single_variable);
858 GNUNET_free (per_host_variable);
862 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
864 value = ctx->hostname;
867 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
872 * Create a new configuration using the given configuration
873 * as a template; however, each PORT in the existing cfg
874 * must be renumbered by incrementing "*port". If we run
875 * out of "*port" numbers, return NULL.
877 * @param cfg template configuration
878 * @param port port numbers to use, update to reflect
879 * port numbers that were used
880 * @param upnum number to make unix domain socket names unique
881 * @param hostname hostname of the controlling host, to allow control connections from
882 * @param fdnum number used to offset the unix domain socket for grouped processes
883 * (such as statistics or peerinfo, which can be shared among others)
885 * @return new configuration, NULL on error
887 static struct GNUNET_CONFIGURATION_Handle *
888 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
890 uint32_t * upnum, const char *hostname, uint32_t * fdnum)
892 struct UpdateContext uc;
901 uc.ret = GNUNET_CONFIGURATION_create ();
902 uc.hostname = hostname;
905 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
906 if (uc.nport >= HIGH_PORT)
909 GNUNET_CONFIGURATION_destroy (uc.ret);
913 if (GNUNET_CONFIGURATION_get_value_string
914 (cfg, "testing", "control_host", &control_host) == GNUNET_OK)
916 if (hostname != NULL)
917 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
920 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
922 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
924 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
925 "ACCEPT_FROM", allowed_hosts);
926 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
928 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
929 "ACCEPT_FROM", allowed_hosts);
930 GNUNET_free_non_null (control_host);
931 GNUNET_free (allowed_hosts);
935 /* arm needs to know to allow connections from the host on which it is running,
936 * otherwise gnunet-arm is unable to connect to it in some instances */
937 if (hostname != NULL)
939 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
940 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
942 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
944 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
946 GNUNET_free (allowed_hosts);
950 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
951 "BINDTO", "127.0.0.1");
952 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
953 "BINDTO", "127.0.0.1");
956 *port = (uint16_t) uc.nport;
965 * Add entries to the peers connect list
967 * @param pg the peer group we are working with
968 * @param first index of the first peer
969 * @param second index of the second peer
971 * @return the number of connections added
972 * technically should only be 0 or 2
976 add_actual_connections (struct GNUNET_TESTING_PeerGroup *pg,
977 unsigned int first, unsigned int second)
983 GNUNET_HashCode hash_first;
984 GNUNET_HashCode hash_second;
986 hash_from_uid (first, &hash_first);
987 hash_from_uid (second, &hash_second);
989 add_first = GNUNET_NO;
991 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].connect_peers,
994 add_first = GNUNET_YES;
997 add_second = GNUNET_NO;
999 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].connect_peers,
1002 add_second = GNUNET_YES;
1008 GNUNET_assert (GNUNET_OK ==
1009 GNUNET_CONTAINER_multihashmap_put (pg->
1011 [first].connect_peers,
1014 peers[second].daemon,
1015 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1016 pg->peers[first].num_connections++;
1022 GNUNET_assert (GNUNET_OK ==
1023 GNUNET_CONTAINER_multihashmap_put (pg->
1025 [second].connect_peers,
1028 peers[first].daemon,
1029 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1030 pg->peers[second].num_connections++;
1039 * Add entries to the peers allowed connections list
1041 * @param pg the peer group we are working with
1042 * @param first index of the first peer
1043 * @param second index of the second peer
1045 * @return the number of connections added (can be 0, 1 or 2)
1046 * technically should only be 0 or 2, but the small price
1047 * of iterating over the lists (hashmaps in the future)
1048 * for being sure doesn't bother me!
1052 add_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg,
1053 unsigned int first, unsigned int second)
1057 struct PeerConnection *first_iter;
1058 struct PeerConnection *second_iter;
1059 struct PeerConnection *new_first;
1060 struct PeerConnection *new_second;
1065 GNUNET_HashCode hash_first;
1066 GNUNET_HashCode hash_second;
1068 hash_from_uid (first, &hash_first);
1069 hash_from_uid (second, &hash_second);
1071 add_first = GNUNET_NO;
1073 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].allowed_peers,
1076 add_first = GNUNET_YES;
1079 add_second = GNUNET_NO;
1081 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].allowed_peers,
1084 add_second = GNUNET_YES;
1087 first_iter = pg->peers[first].connected_peers;
1088 while (first_iter != NULL)
1090 if (first_iter->daemon == pg->peers[second].daemon)
1091 add_first = GNUNET_NO;
1092 first_iter = first_iter->next;
1095 second_iter = pg->peers[second].connected_peers;
1096 add_second = GNUNET_YES;
1097 while (second_iter != NULL)
1099 if (second_iter->daemon == pg->peers[first].daemon)
1100 add_second = GNUNET_NO;
1101 second_iter = second_iter->next;
1108 GNUNET_assert (GNUNET_OK ==
1109 GNUNET_CONTAINER_multihashmap_put (pg->
1111 [first].allowed_peers,
1114 peers[second].daemon,
1115 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1117 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1118 new_first->daemon = pg->peers[second].daemon;
1119 new_first->next = pg->peers[first].connected_peers;
1120 pg->peers[first].connected_peers = new_first;
1122 pg->peers[first].num_connections++;
1128 GNUNET_assert (GNUNET_OK ==
1129 GNUNET_CONTAINER_multihashmap_put (pg->
1131 [second].allowed_peers,
1134 peers[first].daemon,
1135 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1137 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1138 new_second->daemon = pg->peers[first].daemon;
1139 new_second->next = pg->peers[second].connected_peers;
1140 pg->peers[second].connected_peers = new_second;
1141 pg->peers[first].num_connections++;
1143 pg->peers[second].num_connections++;
1151 * Add entries to the peers blacklisted list
1153 * @param pg the peer group we are working with
1154 * @param first index of the first peer
1155 * @param second index of the second peer
1157 * @return the number of connections added (can be 0, 1 or 2)
1161 blacklist_connections (struct GNUNET_TESTING_PeerGroup *pg,
1162 unsigned int first, unsigned int second)
1167 GNUNET_HashCode hash_first;
1168 GNUNET_HashCode hash_second;
1170 hash_from_uid (first, &hash_first);
1171 hash_from_uid (second, &hash_second);
1173 add_first = GNUNET_NO;
1175 GNUNET_CONTAINER_multihashmap_contains (pg->
1176 peers[first].blacklisted_peers,
1179 add_first = GNUNET_YES;
1182 add_second = GNUNET_NO;
1184 GNUNET_CONTAINER_multihashmap_contains (pg->
1185 peers[second].blacklisted_peers,
1188 add_second = GNUNET_YES;
1194 GNUNET_assert (GNUNET_OK ==
1195 GNUNET_CONTAINER_multihashmap_put (pg->
1197 [first].blacklisted_peers,
1200 peers[second].daemon,
1201 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1202 pg->peers[first].num_connections++;
1208 GNUNET_assert (GNUNET_OK ==
1209 GNUNET_CONTAINER_multihashmap_put (pg->
1211 [second].blacklisted_peers,
1214 peers[first].daemon,
1215 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1216 pg->peers[second].num_connections++;
1224 * Remove entries from the peers blacklisted list
1226 * @param pg the peer group we are working with
1227 * @param first index of the first peer
1228 * @param second index of the second peer
1230 * @return the number of connections removed (can be 0, 1 or 2)
1234 unblacklist_connections (struct GNUNET_TESTING_PeerGroup *pg,
1235 unsigned int first, unsigned int second)
1240 GNUNET_HashCode hash_first;
1241 GNUNET_HashCode hash_second;
1243 hash_from_uid (first, &hash_first);
1244 hash_from_uid (second, &hash_second);
1247 GNUNET_CONTAINER_multihashmap_contains (pg->
1248 peers[first].blacklisted_peers,
1251 GNUNET_CONTAINER_multihashmap_contains (pg->
1252 peers[second].blacklisted_peers,
1258 GNUNET_assert (GNUNET_YES ==
1259 GNUNET_CONTAINER_multihashmap_remove (pg->
1261 [first].blacklisted_peers,
1271 GNUNET_assert (GNUNET_YES ==
1272 GNUNET_CONTAINER_multihashmap_remove (pg->
1274 [second].blacklisted_peers,
1286 * Scale free network construction as described in:
1288 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1290 * Start with a network of "one" peer, then progressively add
1291 * peers up to the total number. At each step, iterate over
1292 * all possible peers and connect new peer based on number of
1293 * existing connections of the target peer.
1295 * @param pg the peer group we are dealing with
1296 * @param proc the connection processor to use
1298 * @return the number of connections created
1301 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg,
1302 GNUNET_TESTING_ConnectionProcessor proc)
1305 unsigned int total_connections;
1306 unsigned int outer_count;
1308 unsigned int previous_total_connections;
1312 GNUNET_assert (pg->total > 1);
1314 /* Add a connection between the first two nodes */
1315 total_connections = proc (pg, 0, 1);
1317 for (outer_count = 1; outer_count < pg->total; outer_count++)
1319 previous_total_connections = total_connections;
1320 for (i = 0; i < outer_count; i++)
1323 pg->peers[i].num_connections /
1324 (double) previous_total_connections;
1327 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1328 UINT64_MAX)) / ((double) UINT64_MAX);
1330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1331 "Considering connecting peer %d to peer %d\n",
1334 if (random < probability)
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1338 "Connecting peer %d to peer %d\n", outer_count, i);
1340 total_connections += proc (pg, outer_count, i);
1345 return total_connections;
1349 * Create a topology given a peer group (set of running peers)
1350 * and a connection processor.
1352 * @param pg the peergroup to create the topology on
1353 * @param proc the connection processor to call to actually set
1354 * up connections between two peers
1356 * @return the number of connections that were set up
1360 create_small_world_ring (struct GNUNET_TESTING_PeerGroup *pg,
1361 GNUNET_TESTING_ConnectionProcessor proc)
1365 unsigned int natLog;
1366 unsigned int randomPeer;
1367 double random, logNModifier, percentage;
1368 unsigned int smallWorldConnections;
1373 unsigned int useAnd;
1374 int connect_attempts;
1376 logNModifier = 0.5; /* FIXME: default value? */
1377 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1382 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1383 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1385 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1386 p_string, "LOGNMODIFIER", "TESTING");
1387 GNUNET_free (p_string);
1389 percentage = 0.5; /* FIXME: default percentage? */
1390 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1395 if (sscanf (p_string, "%lf", &percentage) != 1)
1396 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1398 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1399 p_string, "PERCENTAGE", "TESTING");
1400 GNUNET_free (p_string);
1402 natLog = log (pg->total);
1403 connsPerPeer = ceil (natLog * logNModifier);
1405 if (connsPerPeer % 2 == 1)
1408 smallWorldConnections = 0;
1409 connect_attempts = 0;
1410 for (i = 0; i < pg->total; i++)
1413 max = i + connsPerPeer / 2;
1414 min = i - connsPerPeer / 2;
1416 if (max > pg->total - 1)
1418 max = max - pg->total;
1424 min = pg->total - 1 + min;
1428 for (j = 0; j < connsPerPeer / 2; j++)
1432 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1433 UINT64_MAX) / ((double) UINT64_MAX));
1434 if (random < percentage)
1436 /* Connect to uniformly selected random peer */
1438 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1440 while ((((randomPeer < max) && (randomPeer > min))
1441 && (useAnd == 0)) || (((randomPeer > min)
1442 || (randomPeer < max))
1446 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1449 smallWorldConnections += proc (pg, i, randomPeer);
1453 nodeToConnect = i + j + 1;
1454 if (nodeToConnect > pg->total - 1)
1456 nodeToConnect = nodeToConnect - pg->total;
1458 connect_attempts += proc (pg, i, nodeToConnect);
1464 connect_attempts += smallWorldConnections;
1466 return connect_attempts;
1470 * Create a topology given a peer group (set of running peers)
1471 * and a connection processor.
1473 * @param pg the peergroup to create the topology on
1474 * @param proc the connection processor to call to actually set
1475 * up connections between two peers
1477 * @return the number of connections that were set up
1481 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg,
1482 GNUNET_TESTING_ConnectionProcessor proc)
1484 unsigned int outer_count, inner_count;
1485 unsigned int cutoff;
1486 int connect_attempts;
1487 double nat_percentage;
1490 nat_percentage = 0.6; /* FIXME: default percentage? */
1491 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1496 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1497 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1499 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1500 p_string, "PERCENTAGE", "TESTING");
1501 GNUNET_free (p_string);
1506 cutoff = (unsigned int) (nat_percentage * pg->total);
1508 connect_attempts = 0;
1510 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1512 for (inner_count = outer_count + 1; inner_count < pg->total;
1515 if ((outer_count > cutoff) || (inner_count > cutoff))
1518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1519 "Connecting peer %d to peer %d\n",
1520 outer_count, inner_count);
1522 connect_attempts += proc (pg, outer_count, inner_count);
1527 return connect_attempts;
1532 * Create a topology given a peer group (set of running peers)
1533 * and a connection processor.
1535 * @param pg the peergroup to create the topology on
1536 * @param proc the connection processor to call to actually set
1537 * up connections between two peers
1539 * @return the number of connections that were set up
1543 create_small_world (struct GNUNET_TESTING_PeerGroup *pg,
1544 GNUNET_TESTING_ConnectionProcessor proc)
1546 unsigned int i, j, k;
1547 unsigned int square;
1550 unsigned int toggle = 1;
1551 unsigned int nodeToConnect;
1552 unsigned int natLog;
1553 unsigned int node1Row;
1554 unsigned int node1Col;
1555 unsigned int node2Row;
1556 unsigned int node2Col;
1557 unsigned int distance;
1558 double probability, random, percentage;
1559 unsigned int smallWorldConnections;
1560 unsigned int small_world_it;
1562 int connect_attempts;
1563 square = floor (sqrt (pg->total));
1567 percentage = 0.5; /* FIXME: default percentage? */
1568 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1573 if (sscanf (p_string, "%lf", &percentage) != 1)
1574 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1576 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1577 p_string, "PERCENTAGE", "TESTING");
1578 GNUNET_free (p_string);
1580 if (percentage < 0.0)
1582 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1584 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1585 "PERCENTAGE", "TESTING", percentage);
1588 probability = 0.5; /* FIXME: default percentage? */
1589 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1594 if (sscanf (p_string, "%lf", &probability) != 1)
1595 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1597 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1598 p_string, "PROBABILITY", "TESTING");
1599 GNUNET_free (p_string);
1601 if (square * square != pg->total)
1603 while (rows * cols < pg->total)
1605 if (toggle % 2 == 0)
1614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1616 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1620 connect_attempts = 0;
1621 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1622 * to the node to its right and above. Once this is over, we'll have our torus!
1623 * Special case for the last node (if the rows and columns are not equal), connect
1624 * to the first in the row to maintain topology.
1626 for (i = 0; i < pg->total; i++)
1628 /* First connect to the node to the right */
1629 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1630 nodeToConnect = i + 1;
1631 else if (i + 1 == pg->total)
1632 nodeToConnect = rows * cols - cols;
1634 nodeToConnect = i - cols + 1;
1636 connect_attempts += proc (pg, i, nodeToConnect);
1639 nodeToConnect = (rows * cols) - cols + i;
1641 nodeToConnect = i - cols;
1643 if (nodeToConnect < pg->total)
1644 connect_attempts += proc (pg, i, nodeToConnect);
1646 natLog = log (pg->total);
1647 #if VERBOSE_TESTING > 2
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1649 _("natural log of %d is %d, will run %d iterations\n"),
1650 pg->total, natLog, (int) (natLog * percentage));
1651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1652 _("Total connections added thus far: %u!\n"), connect_attempts);
1654 smallWorldConnections = 0;
1655 small_world_it = (unsigned int) (natLog * percentage);
1656 if (small_world_it < 1)
1658 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
1659 for (i = 0; i < small_world_it; i++)
1661 for (j = 0; j < pg->total; j++)
1663 /* Determine the row and column of node at position j on the 2d torus */
1664 node1Row = j / cols;
1665 node1Col = j - (node1Row * cols);
1666 for (k = 0; k < pg->total; k++)
1668 /* Determine the row and column of node at position k on the 2d torus */
1669 node2Row = k / cols;
1670 node2Col = k - (node2Row * cols);
1671 /* Simple Cartesian distance */
1673 abs (node1Row - node2Row) + abs (node1Col - node2Col);
1676 /* Calculate probability as 1 over the square of the distance */
1677 probability = 1.0 / (distance * distance);
1678 /* Choose a random value between 0 and 1 */
1681 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1683 ((double) UINT64_MAX);
1684 /* If random < probability, then connect the two nodes */
1685 if (random < probability)
1686 smallWorldConnections += proc (pg, j, k);
1692 connect_attempts += smallWorldConnections;
1693 #if VERBOSE_TESTING > 2
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 _("Total connections added for small world: %d!\n"),
1696 smallWorldConnections);
1698 return connect_attempts;
1702 * Create a topology given a peer group (set of running peers)
1703 * and a connection processor.
1705 * @param pg the peergroup to create the topology on
1706 * @param proc the connection processor to call to actually set
1707 * up connections between two peers
1709 * @return the number of connections that were set up
1713 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg,
1714 GNUNET_TESTING_ConnectionProcessor proc)
1717 unsigned int outer_count;
1718 unsigned int inner_count;
1719 int connect_attempts;
1723 probability = 0.5; /* FIXME: default percentage? */
1724 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1729 if (sscanf (p_string, "%lf", &probability) != 1)
1730 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1732 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1733 p_string, "PROBABILITY", "TESTING");
1734 GNUNET_free (p_string);
1736 connect_attempts = 0;
1737 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1739 for (inner_count = outer_count + 1; inner_count < pg->total;
1744 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1745 UINT64_MAX)) / ((double) UINT64_MAX);
1747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1748 _("rand is %f probability is %f\n"), temp_rand,
1751 if (temp_rand < probability)
1753 connect_attempts += proc (pg, outer_count, inner_count);
1758 return connect_attempts;
1762 * Create a topology given a peer group (set of running peers)
1763 * and a connection processor. This particular function creates
1764 * the connections for a 2d-torus, plus additional "closest"
1765 * connections per peer.
1767 * @param pg the peergroup to create the topology on
1768 * @param proc the connection processor to call to actually set
1769 * up connections between two peers
1771 * @return the number of connections that were set up
1775 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg,
1776 GNUNET_TESTING_ConnectionProcessor proc)
1779 unsigned int square;
1782 unsigned int toggle = 1;
1783 unsigned int nodeToConnect;
1784 int connect_attempts;
1786 connect_attempts = 0;
1788 square = floor (sqrt (pg->total));
1792 if (square * square != pg->total)
1794 while (rows * cols < pg->total)
1796 if (toggle % 2 == 0)
1805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1807 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1810 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1811 * to the node to its right and above. Once this is over, we'll have our torus!
1812 * Special case for the last node (if the rows and columns are not equal), connect
1813 * to the first in the row to maintain topology.
1815 for (i = 0; i < pg->total; i++)
1817 /* First connect to the node to the right */
1818 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1819 nodeToConnect = i + 1;
1820 else if (i + 1 == pg->total)
1821 nodeToConnect = rows * cols - cols;
1823 nodeToConnect = i - cols + 1;
1825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1826 "Connecting peer %d to peer %d\n", i, nodeToConnect);
1828 connect_attempts += proc (pg, i, nodeToConnect);
1830 /* Second connect to the node immediately above */
1832 nodeToConnect = (rows * cols) - cols + i;
1834 nodeToConnect = i - cols;
1836 if (nodeToConnect < pg->total)
1839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1840 "Connecting peer %d to peer %d\n", i, nodeToConnect);
1842 connect_attempts += proc (pg, i, nodeToConnect);
1847 return connect_attempts;
1852 * Create a topology given a peer group (set of running peers)
1853 * and a connection processor.
1855 * @param pg the peergroup to create the topology on
1856 * @param proc the connection processor to call to actually set
1857 * up connections between two peers
1859 * @return the number of connections that were set up
1863 create_clique (struct GNUNET_TESTING_PeerGroup *pg,
1864 GNUNET_TESTING_ConnectionProcessor proc)
1866 unsigned int outer_count;
1867 unsigned int inner_count;
1868 int connect_attempts;
1870 connect_attempts = 0;
1872 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1874 for (inner_count = outer_count + 1; inner_count < pg->total;
1878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1879 "Connecting peer %d to peer %d\n",
1880 outer_count, inner_count);
1882 connect_attempts += proc (pg, outer_count, inner_count);
1886 return connect_attempts;
1890 * Create a topology given a peer group (set of running peers)
1891 * and a connection processor.
1893 * @param pg the peergroup to create the topology on
1894 * @param proc the connection processor to call to actually set
1895 * up connections between two peers
1897 * @return the number of connections that were set up
1901 create_line (struct GNUNET_TESTING_PeerGroup *pg,
1902 GNUNET_TESTING_ConnectionProcessor proc)
1905 int connect_attempts;
1907 connect_attempts = 0;
1909 /* Connect each peer to the next highest numbered peer */
1910 for (count = 0; count < pg->total - 1; count++)
1913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1914 "Connecting peer %d to peer %d\n", count, count + 1);
1916 connect_attempts += proc (pg, count, count + 1);
1919 return connect_attempts;
1923 * Create a topology given a peer group (set of running peers)
1924 * and a connection processor.
1926 * @param pg the peergroup to create the topology on
1927 * @param proc the connection processor to call to actually set
1928 * up connections between two peers
1930 * @return the number of connections that were set up
1934 create_ring (struct GNUNET_TESTING_PeerGroup *pg,
1935 GNUNET_TESTING_ConnectionProcessor proc)
1938 int connect_attempts;
1940 connect_attempts = 0;
1942 /* Connect each peer to the next highest numbered peer */
1943 for (count = 0; count < pg->total - 1; count++)
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1947 "Connecting peer %d to peer %d\n", count, count + 1);
1949 connect_attempts += proc (pg, count, count + 1);
1952 /* Connect the last peer to the first peer */
1953 connect_attempts += proc (pg, pg->total - 1, 0);
1955 return connect_attempts;
1960 * Iterator for writing friends of a peer to a file.
1962 * @param cls closure, an open writable file handle
1963 * @param key the key the daemon was stored under
1964 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1966 * @return GNUNET_YES to continue iteration
1968 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1969 * with a single file_iterator that takes a closure which contains
1970 * the prefix to write before the peer. Then this could be used
1971 * for blacklisting multiple transports and writing the friend
1972 * file. I'm sure *someone* will complain loudly about other
1973 * things that negate these functions even existing so no point in
1977 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1979 FILE *temp_friend_handle = cls;
1980 struct GNUNET_TESTING_Daemon *peer = value;
1981 struct GNUNET_PeerIdentity *temppeer;
1982 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1984 temppeer = &peer->id;
1985 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
1986 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
1991 struct BlacklistContext
1994 * The (open) file handle to write to
1996 FILE *temp_file_handle;
1999 * The transport that this peer will be blacklisted on.
2005 * Iterator for writing blacklist data to appropriate files.
2007 * @param cls closure, an open writable file handle
2008 * @param key the key the daemon was stored under
2009 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2011 * @return GNUNET_YES to continue iteration
2014 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2016 struct BlacklistContext *blacklist_ctx = cls;
2017 //FILE *temp_blacklist_handle = cls;
2018 struct GNUNET_TESTING_Daemon *peer = value;
2019 struct GNUNET_PeerIdentity *temppeer;
2020 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2022 temppeer = &peer->id;
2023 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2024 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2025 blacklist_ctx->transport, (char *) &peer_enc);
2031 * Create the friend files based on the PeerConnection's
2032 * of each peer in the peer group, and copy the files
2033 * to the appropriate place
2035 * @param pg the peer group we are dealing with
2038 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
2040 FILE *temp_friend_handle;
2041 unsigned int pg_iter;
2042 char *temp_service_path;
2043 struct GNUNET_OS_Process **procarr;
2046 enum GNUNET_OS_ProcessStatusType type;
2047 unsigned long return_code;
2052 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2053 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2055 mytemp = GNUNET_DISK_mktemp ("friends");
2056 GNUNET_assert (mytemp != NULL);
2057 temp_friend_handle = fopen (mytemp, "wt");
2058 GNUNET_assert (temp_friend_handle != NULL);
2059 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2060 &friend_file_iterator,
2061 temp_friend_handle);
2062 fclose (temp_friend_handle);
2065 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2066 daemon->cfg, "PATHS",
2068 &temp_service_path))
2070 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2072 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2073 "SERVICEHOME", "PATHS");
2074 if (UNLINK (mytemp) != 0)
2075 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2077 GNUNET_free (mytemp);
2081 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2083 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2084 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2089 _("Copying file with command cp %s %s\n"), mytemp, arg);
2094 else /* Remote, scp the file to the correct place */
2096 if (NULL != pg->peers[pg_iter].daemon->username)
2097 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2098 pg->peers[pg_iter].daemon->username,
2099 pg->peers[pg_iter].daemon->hostname,
2102 GNUNET_asprintf (&arg, "%s:%s/friends",
2103 pg->peers[pg_iter].daemon->hostname,
2106 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2111 _("Copying file with command scp %s %s\n"), mytemp,
2116 GNUNET_free (temp_service_path);
2117 GNUNET_free (mytemp);
2121 ret = GNUNET_SYSERR;
2122 while ((count < max_wait) && (ret != GNUNET_OK))
2125 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2129 _("Checking copy status of file %d\n"), pg_iter);
2131 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2133 if (GNUNET_OS_process_status
2134 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2136 ret = GNUNET_SYSERR;
2138 else if ((type != GNUNET_OS_PROCESS_EXITED)
2139 || (return_code != 0))
2141 ret = GNUNET_SYSERR;
2145 GNUNET_OS_process_close (procarr[pg_iter]);
2146 procarr[pg_iter] = NULL;
2148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2149 _("File %d copied\n"), pg_iter);
2155 if (ret == GNUNET_SYSERR)
2157 /* FIXME: why sleep here? -CG */
2163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2164 _("Finished copying all friend files!\n"));
2166 GNUNET_free (procarr);
2172 * Create the blacklist files based on the PeerConnection's
2173 * of each peer in the peer group, and copy the files
2174 * to the appropriate place.
2176 * @param pg the peer group we are dealing with
2177 * @param transports space delimited list of transports to blacklist
2180 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg,
2181 const char *transports)
2183 FILE *temp_file_handle;
2184 static struct BlacklistContext blacklist_ctx;
2185 unsigned int pg_iter;
2186 char *temp_service_path;
2187 struct GNUNET_OS_Process **procarr;
2190 enum GNUNET_OS_ProcessStatusType type;
2191 unsigned long return_code;
2198 char *temp_transports;
2200 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2201 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2203 mytemp = GNUNET_DISK_mktemp ("blacklist");
2204 GNUNET_assert (mytemp != NULL);
2205 temp_file_handle = fopen (mytemp, "wt");
2206 GNUNET_assert (temp_file_handle != NULL);
2207 temp_transports = GNUNET_strdup (transports);
2208 blacklist_ctx.temp_file_handle = temp_file_handle;
2209 transport_len = strlen (temp_transports) + 1;
2212 for (i = 0; i < transport_len; i++)
2214 if ((temp_transports[i] == ' ') && (pos == NULL))
2215 continue; /* At start of string (whitespace) */
2216 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2218 temp_transports[i] = '\0';
2219 blacklist_ctx.transport = pos;
2220 GNUNET_CONTAINER_multihashmap_iterate (pg->
2222 [pg_iter].blacklisted_peers,
2223 &blacklist_file_iterator,
2226 } /* At beginning of actual string */
2227 else if (pos == NULL)
2229 pos = &temp_transports[i];
2233 GNUNET_free (temp_transports);
2234 fclose (temp_file_handle);
2237 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2238 daemon->cfg, "PATHS",
2240 &temp_service_path))
2242 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2244 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2245 "SERVICEHOME", "PATHS");
2246 if (UNLINK (mytemp) != 0)
2247 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2249 GNUNET_free (mytemp);
2253 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2255 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2256 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2261 _("Copying file with command cp %s %s\n"), mytemp, arg);
2266 else /* Remote, scp the file to the correct place */
2268 if (NULL != pg->peers[pg_iter].daemon->username)
2269 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
2270 pg->peers[pg_iter].daemon->username,
2271 pg->peers[pg_iter].daemon->hostname,
2274 GNUNET_asprintf (&arg, "%s:%s/blacklist",
2275 pg->peers[pg_iter].daemon->hostname,
2278 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2283 _("Copying file with command scp %s %s\n"), mytemp,
2288 GNUNET_free (temp_service_path);
2289 GNUNET_free (mytemp);
2293 ret = GNUNET_SYSERR;
2294 while ((count < max_wait) && (ret != GNUNET_OK))
2297 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2301 _("Checking copy status of file %d\n"), pg_iter);
2303 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2305 if (GNUNET_OS_process_status
2306 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2308 ret = GNUNET_SYSERR;
2310 else if ((type != GNUNET_OS_PROCESS_EXITED)
2311 || (return_code != 0))
2313 ret = GNUNET_SYSERR;
2317 GNUNET_OS_process_close (procarr[pg_iter]);
2318 procarr[pg_iter] = NULL;
2320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2321 _("File %d copied\n"), pg_iter);
2327 if (ret == GNUNET_SYSERR)
2329 /* FIXME: why sleep here? -CG */
2335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2336 _("Finished copying all blacklist files!\n"));
2338 GNUNET_free (procarr);
2344 * Internal notification of a connection, kept so that we can ensure some connections
2345 * happen instead of flooding all testing daemons with requests to connect.
2348 internal_connect_notify (void *cls,
2349 const struct GNUNET_PeerIdentity *first,
2350 const struct GNUNET_PeerIdentity *second,
2352 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2353 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2354 struct GNUNET_TESTING_Daemon *first_daemon,
2355 struct GNUNET_TESTING_Daemon *second_daemon,
2358 struct ConnectTopologyContext *ct_ctx = cls;
2359 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
2360 outstanding_connects--;
2361 ct_ctx->remaining_connections--;
2362 if (ct_ctx->remaining_connections == 0)
2364 if (ct_ctx->notify_connections_done != NULL)
2365 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
2366 GNUNET_free (ct_ctx);
2369 if (pg->notify_connection != NULL)
2370 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
2371 first_cfg, second_cfg, first_daemon, second_daemon,
2377 * Either delay a connection (because there are too many outstanding)
2378 * or schedule it for right now.
2380 * @param cls a connection context
2381 * @param tc the task runtime context
2384 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2386 struct ConnectContext *connect_context = cls;
2388 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2391 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2393 #if VERBOSE_TESTING > 2
2394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2396 ("Delaying connect, we have too many outstanding connections!\n"));
2398 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2399 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
2400 &schedule_connect, connect_context);
2404 #if VERBOSE_TESTING > 2
2405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2406 _("Creating connection, outstanding_connections is %d\n"),
2407 outstanding_connects);
2409 outstanding_connects++;
2410 GNUNET_TESTING_daemons_connect (connect_context->first,
2411 connect_context->second,
2414 &internal_connect_notify,
2415 connect_context->ct_ctx);
2416 GNUNET_free (connect_context);
2422 * Iterator for actually scheduling connections to be created
2423 * between two peers.
2425 * @param cls closure, a GNUNET_TESTING_Daemon
2426 * @param key the key the second Daemon was stored under
2427 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2429 * @return GNUNET_YES to continue iteration
2432 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2434 struct ConnectTopologyContext *ct_ctx = cls;
2435 struct PeerData *first = ct_ctx->first;
2436 struct GNUNET_TESTING_Daemon *second = value;
2437 struct ConnectContext *connect_context;
2439 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
2440 connect_context->first = first->daemon;
2441 connect_context->second = second;
2442 connect_context->ct_ctx = ct_ctx;
2443 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
2450 * Iterator for copying all entries in the allowed hashmap to the
2453 * @param cls closure, a GNUNET_TESTING_Daemon
2454 * @param key the key the second Daemon was stored under
2455 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2457 * @return GNUNET_YES to continue iteration
2460 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2462 struct PeerData *first = cls;
2464 GNUNET_assert (GNUNET_OK ==
2465 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
2467 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2473 * Make the peers to connect the same as those that are allowed to be
2476 * @param pg the peer group
2479 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2481 unsigned int pg_iter;
2486 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2489 GNUNET_CONTAINER_multihashmap_iterate (pg->
2490 peers[pg_iter].allowed_peers,
2491 ©_topology_iterator,
2492 &pg->peers[pg_iter]);
2493 if (GNUNET_SYSERR == ret)
2494 return GNUNET_SYSERR;
2496 total = total + ret;
2504 * Connect the topology as specified by the PeerConnection's
2505 * of each peer in the peer group
2507 * @param pg the peer group we are dealing with
2508 * @return the number of connections that will be attempted
2511 connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2512 GNUNET_TESTING_NotifyCompletion notify_callback,
2515 unsigned int pg_iter;
2518 struct ConnectTopologyContext *ct_ctx;
2520 struct PeerConnection *connection_iter;
2521 struct ConnectContext *connect_context;
2525 ct_ctx = GNUNET_malloc (sizeof (struct ConnectTopologyContext));
2526 ct_ctx->notify_connections_done = notify_callback;
2527 ct_ctx->notify_cls = notify_cls;
2530 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2533 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
2538 GNUNET_free (ct_ctx);
2541 ct_ctx->remaining_connections = total;
2544 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2546 ct_ctx->first = &pg->peers[pg_iter];
2548 GNUNET_CONTAINER_multihashmap_iterate (pg->
2549 peers[pg_iter].connect_peers,
2550 &connect_iterator, ct_ctx);
2551 GNUNET_assert (GNUNET_SYSERR != ret && ret >= 0);
2552 total = total + ret;
2555 connection_iter = FIXME;
2556 while (connection_iter != NULL)
2558 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
2559 connect_context->pg = pg;
2560 connect_context->first = FIXME;
2561 connect_context->second = connection_iter->daemon;
2562 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
2563 connection_iter = connection_iter->next;
2572 * Takes a peer group and creates a topology based on the
2573 * one specified. Creates a topology means generates friend
2574 * files for the peers so they can only connect to those allowed
2575 * by the topology. This will only have an effect once peers
2576 * are started if the FRIENDS_ONLY option is set in the base
2577 * config. Also takes an optional restrict topology which
2578 * disallows connections based on particular transports
2579 * UNLESS they are specified in the restricted topology.
2581 * @param pg the peer group struct representing the running peers
2582 * @param topology which topology to connect the peers in
2583 * @param restrict_topology disallow restrict_transports transport
2584 * connections to peers NOT in this topology
2585 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2586 * @param restrict_transports space delimited list of transports to blacklist
2587 * to create restricted topology
2589 * @return the maximum number of connections were all allowed peers
2590 * connected to each other
2593 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2594 enum GNUNET_TESTING_Topology topology,
2595 enum GNUNET_TESTING_Topology
2597 const char *restrict_transports)
2600 unsigned int num_connections;
2601 int unblacklisted_connections;
2605 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
2609 num_connections = create_clique (pg, &add_allowed_connections);
2611 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2614 _("Creating small world (ring) topology\n"));
2617 create_small_world_ring (pg, &add_allowed_connections);
2619 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2622 _("Creating small world (2d-torus) topology\n"));
2624 num_connections = create_small_world (pg, &add_allowed_connections);
2626 case GNUNET_TESTING_TOPOLOGY_RING:
2628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
2630 num_connections = create_ring (pg, &add_allowed_connections);
2632 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
2636 num_connections = create_2d_torus (pg, &add_allowed_connections);
2638 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2641 _("Creating Erdos-Renyi topology\n"));
2643 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2645 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
2649 num_connections = create_nated_internet (pg, &add_allowed_connections);
2651 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2654 _("Creating Scale Free topology\n"));
2656 num_connections = create_scale_free (pg, &add_allowed_connections);
2658 case GNUNET_TESTING_TOPOLOGY_LINE:
2660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2661 _("Creating straight line topology\n"));
2663 num_connections = create_line (pg, &add_allowed_connections);
2665 case GNUNET_TESTING_TOPOLOGY_NONE:
2667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2669 ("Creating no allowed topology (all peers can connect at core level)\n"));
2671 num_connections = 0;
2674 num_connections = 0;
2679 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2681 ret = create_and_copy_friend_files (pg);
2682 if (ret != GNUNET_OK)
2685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2686 _("Failed during friend file copying!\n"));
2688 return GNUNET_SYSERR;
2693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2694 _("Friend files created/copied successfully!\n"));
2699 /* Use the create clique method to initially set all connections as blacklisted. */
2700 if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2701 create_clique (pg, &blacklist_connections);
2703 unblacklisted_connections = 0;
2704 /* Un-blacklist connections as per the topology specified */
2705 switch (restrict_topology)
2707 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2710 _("Blacklisting all but clique topology\n"));
2712 unblacklisted_connections =
2713 create_clique (pg, &unblacklist_connections);
2715 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2718 _("Blacklisting all but small world (ring) topology\n"));
2720 unblacklisted_connections =
2721 create_small_world_ring (pg, &unblacklist_connections);
2723 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2727 ("Blacklisting all but small world (2d-torus) topology\n"));
2729 unblacklisted_connections =
2730 create_small_world (pg, &unblacklist_connections);
2732 case GNUNET_TESTING_TOPOLOGY_RING:
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2735 _("Blacklisting all but ring topology\n"));
2737 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2739 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2742 _("Blacklisting all but 2d torus topology\n"));
2744 unblacklisted_connections =
2745 create_2d_torus (pg, &unblacklist_connections);
2747 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2750 _("Blacklisting all but Erdos-Renyi topology\n"));
2752 unblacklisted_connections =
2753 create_erdos_renyi (pg, &unblacklist_connections);
2755 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2758 _("Blacklisting all but InterNAT topology\n"));
2760 unblacklisted_connections =
2761 create_nated_internet (pg, &unblacklist_connections);
2763 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2766 _("Blacklisting all but Scale Free topology\n"));
2768 unblacklisted_connections =
2769 create_scale_free (pg, &unblacklist_connections);
2771 case GNUNET_TESTING_TOPOLOGY_LINE:
2773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2774 _("Blacklisting all but straight line topology\n"));
2776 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2778 case GNUNET_TESTING_TOPOLOGY_NONE:
2780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2782 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
2788 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2790 ret = create_and_copy_blacklist_files (pg, restrict_transports);
2791 if (ret != GNUNET_OK)
2794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2795 _("Failed during blacklist file copying!\n"));
2802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2803 _("Blacklist files created/copied successfully!\n"));
2807 return num_connections;
2810 struct RandomContext
2815 struct GNUNET_TESTING_PeerGroup *pg;
2818 * uid of the first peer
2823 * Peer data for first peer.
2825 struct PeerData *first;
2828 * Random percentage to use
2833 struct MinimumContext
2838 struct GNUNET_TESTING_PeerGroup *pg;
2841 * uid of the first peer
2846 * Peer data for first peer.
2848 struct PeerData *first;
2851 * Number of conns per peer
2853 unsigned int num_to_add;
2856 * Permuted array of all possible connections. Only add the Nth
2857 * peer if it's in the Nth position.
2859 unsigned int *pg_array;
2862 * What number is the current element we are iterating over?
2864 unsigned int current;
2872 struct GNUNET_TESTING_PeerGroup *pg;
2875 * uid of the first peer
2880 * uid of the second peer
2882 uint32_t second_uid;
2885 * Peer data for first peer.
2887 struct PeerData *first;
2890 * Which peer has been chosen as the one to add?
2892 unsigned int chosen;
2895 * What number is the current element we are iterating over?
2897 unsigned int current;
2901 * Iterator for choosing random peers to connect.
2903 * @param cls closure, a RandomContext
2904 * @param key the key the second Daemon was stored under
2905 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2907 * @return GNUNET_YES to continue iteration
2910 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2912 struct RandomContext *random_ctx = cls;
2913 double random_number;
2914 uint32_t second_pos;
2915 GNUNET_HashCode first_hash;
2918 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2919 UINT64_MAX)) / ((double) UINT64_MAX);
2920 if (random_number < random_ctx->percentage)
2922 GNUNET_assert (GNUNET_OK ==
2923 GNUNET_CONTAINER_multihashmap_put (random_ctx->
2924 first->connect_peers_working_set,
2926 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2928 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2929 uid_from_hash (key, &second_pos);
2930 hash_from_uid (random_ctx->first_uid, &first_hash);
2931 GNUNET_assert (random_ctx->pg->total > second_pos);
2932 GNUNET_assert (GNUNET_YES ==
2933 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
2935 [second_pos].connect_peers,
2944 * Iterator for adding at least X peers to a peers connection set.
2946 * @param cls closure, MinimumContext
2947 * @param key the key the second Daemon was stored under
2948 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2950 * @return GNUNET_YES to continue iteration
2953 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2955 struct MinimumContext *min_ctx = cls;
2956 uint32_t second_pos;
2957 GNUNET_HashCode first_hash;
2960 if (GNUNET_CONTAINER_multihashmap_size
2961 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2963 for (i = 0; i < min_ctx->num_to_add; i++)
2965 if (min_ctx->pg_array[i] == min_ctx->current)
2967 GNUNET_assert (GNUNET_OK ==
2968 GNUNET_CONTAINER_multihashmap_put
2969 (min_ctx->first->connect_peers_working_set, key,
2971 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2972 uid_from_hash (key, &second_pos);
2973 hash_from_uid (min_ctx->first_uid, &first_hash);
2974 GNUNET_assert (min_ctx->pg->total > second_pos);
2975 GNUNET_assert (GNUNET_OK ==
2976 GNUNET_CONTAINER_multihashmap_put (min_ctx->
2978 [second_pos].connect_peers_working_set,
2982 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2983 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2984 GNUNET_assert (GNUNET_YES ==
2985 GNUNET_CONTAINER_multihashmap_remove
2986 (min_ctx->pg->peers[second_pos].connect_peers,
2987 &first_hash, min_ctx->first->daemon));
2994 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
3000 * Iterator for adding peers to a connection set based on a depth first search.
3002 * @param cls closure, MinimumContext
3003 * @param key the key the second daemon was stored under
3004 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3006 * @return GNUNET_YES to continue iteration
3009 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3011 struct DFSContext *dfs_ctx = cls;
3012 GNUNET_HashCode first_hash;
3014 if (dfs_ctx->current == dfs_ctx->chosen)
3016 GNUNET_assert (GNUNET_OK ==
3017 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3018 first->connect_peers_working_set,
3020 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3021 uid_from_hash (key, &dfs_ctx->second_uid);
3022 hash_from_uid (dfs_ctx->first_uid, &first_hash);
3023 GNUNET_assert (GNUNET_OK ==
3024 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3026 [dfs_ctx->second_uid].connect_peers_working_set,
3030 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3031 GNUNET_assert (GNUNET_YES ==
3032 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
3034 [dfs_ctx->second_uid].connect_peers,
3038 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
3039 return GNUNET_NO; /* We have found our peer, don't iterate more */
3048 * From the set of connections possible, choose percentage percent of connections
3049 * to actually connect.
3051 * @param pg the peergroup we are dealing with
3052 * @param percentage what percent of total connections to make
3055 choose_random_connections (struct GNUNET_TESTING_PeerGroup *pg,
3058 struct RandomContext random_ctx;
3061 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3063 random_ctx.first_uid = pg_iter;
3064 random_ctx.first = &pg->peers[pg_iter];
3065 random_ctx.percentage = percentage;
3067 pg->peers[pg_iter].connect_peers_working_set =
3068 GNUNET_CONTAINER_multihashmap_create (pg->total);
3069 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3070 &random_connect_iterator,
3072 /* Now remove the old connections */
3073 GNUNET_CONTAINER_multihashmap_destroy (pg->
3074 peers[pg_iter].connect_peers);
3075 /* And replace with the random set */
3076 pg->peers[pg_iter].connect_peers =
3077 pg->peers[pg_iter].connect_peers_working_set;
3082 * From the set of connections possible, choose at least num connections per
3085 * @param pg the peergroup we are dealing with
3086 * @param num how many connections at least should each peer have (if possible)?
3089 choose_minimum (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3091 struct MinimumContext minimum_ctx;
3094 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3096 pg->peers[pg_iter].connect_peers_working_set =
3097 GNUNET_CONTAINER_multihashmap_create (num);
3100 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3102 minimum_ctx.first_uid = pg_iter;
3103 minimum_ctx.pg_array =
3104 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
3105 GNUNET_CONTAINER_multihashmap_size
3106 (pg->peers[pg_iter].connect_peers));
3107 minimum_ctx.first = &pg->peers[pg_iter];
3108 minimum_ctx.pg = pg;
3109 minimum_ctx.num_to_add = num;
3110 minimum_ctx.current = 0;
3111 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3112 &minimum_connect_iterator,
3116 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3118 /* Remove the "old" connections */
3119 GNUNET_CONTAINER_multihashmap_destroy (pg->
3120 peers[pg_iter].connect_peers);
3121 /* And replace with the working set */
3122 pg->peers[pg_iter].connect_peers =
3123 pg->peers[pg_iter].connect_peers_working_set;
3130 count_workingset_connections (struct GNUNET_TESTING_PeerGroup *pg)
3133 unsigned int pg_iter;
3137 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3140 GNUNET_CONTAINER_multihashmap_size (pg->
3142 [pg_iter].connect_peers_working_set);
3150 count_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg)
3153 unsigned int pg_iter;
3157 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3160 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3167 struct FindClosestContext
3170 * The currently known closest peer.
3172 struct GNUNET_TESTING_Daemon *closest;
3175 * The info for the peer we are adding connections for.
3177 struct PeerData *curr_peer;
3180 * The distance (bits) between the current
3181 * peer and the currently known closest.
3183 unsigned int closest_dist;
3186 * The offset of the closest known peer in
3189 unsigned int closest_num;
3193 * Iterator over hash map entries of the allowed
3194 * peer connections. Find the closest, not already
3195 * connected peer and return it.
3197 * @param cls closure (struct FindClosestContext)
3198 * @param key current key code (hash of offset in pg)
3199 * @param value value in the hash map - a GNUNET_TESTING_Daemon
3200 * @return GNUNET_YES if we should continue to
3205 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
3207 struct FindClosestContext *closest_ctx = cls;
3208 struct GNUNET_TESTING_Daemon *daemon = value;
3210 if (((closest_ctx->closest == NULL) ||
3211 (GNUNET_CRYPTO_hash_matching_bits
3212 (&daemon->id.hashPubKey,
3213 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
3214 closest_ctx->closest_dist))
3216 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
3217 curr_peer->connect_peers,
3220 closest_ctx->closest_dist =
3221 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
3222 &closest_ctx->curr_peer->daemon->
3224 closest_ctx->closest = daemon;
3225 uid_from_hash (key, &closest_ctx->closest_num);
3231 * From the set of connections possible, choose at num connections per
3232 * peer based on depth which are closest out of those allowed. Guaranteed
3233 * to add num peers to connect to, provided there are that many peers
3234 * in the underlay topology to connect to.
3236 * @param pg the peergroup we are dealing with
3237 * @param num how many connections at least should each peer have (if possible)?
3238 * @param proc processor to actually add the connections
3241 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
3242 GNUNET_TESTING_ConnectionProcessor proc)
3244 struct FindClosestContext closest_ctx;
3248 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
3250 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3252 closest_ctx.curr_peer = &pg->peers[pg_iter];
3253 closest_ctx.closest = NULL;
3254 closest_ctx.closest_dist = 0;
3255 closest_ctx.closest_num = 0;
3256 GNUNET_CONTAINER_multihashmap_iterate (pg->
3257 peers[pg_iter].allowed_peers,
3258 &find_closest_peers,
3260 if (closest_ctx.closest != NULL)
3262 GNUNET_assert (closest_ctx.closest_num < pg->total);
3263 proc (pg, pg_iter, closest_ctx.closest_num);
3270 * From the set of connections possible, choose at least num connections per
3271 * peer based on depth first traversal of peer connections. If DFS leaves
3272 * peers unconnected, ensure those peers get connections.
3274 * @param pg the peergroup we are dealing with
3275 * @param num how many connections at least should each peer have (if possible)?
3278 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3280 struct DFSContext dfs_ctx;
3283 uint32_t starting_peer;
3284 uint32_t least_connections;
3285 GNUNET_HashCode second_hash;
3287 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3289 pg->peers[pg_iter].connect_peers_working_set =
3290 GNUNET_CONTAINER_multihashmap_create (num);
3295 while ((count_workingset_connections (pg) < num * pg->total)
3296 && (count_allowed_connections (pg) > 0))
3298 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3300 least_connections = -1; /* Set to very high number */
3301 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3303 if (GNUNET_CONTAINER_multihashmap_size
3304 (pg->peers[pg_iter].connect_peers_working_set) <
3307 starting_peer = pg_iter;
3309 GNUNET_CONTAINER_multihashmap_size (pg->
3311 [pg_iter].connect_peers_working_set);
3316 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
3322 /* Choose a random peer from the chosen peers set of connections to add */
3324 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3325 GNUNET_CONTAINER_multihashmap_size
3326 (pg->peers[starting_peer].connect_peers));
3327 dfs_ctx.first_uid = starting_peer;
3328 dfs_ctx.first = &pg->peers[starting_peer];
3330 dfs_ctx.current = 0;
3332 GNUNET_CONTAINER_multihashmap_iterate (pg->
3334 [starting_peer].connect_peers,
3335 &dfs_connect_iterator, &dfs_ctx);
3336 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
3337 hash_from_uid (dfs_ctx.second_uid, &second_hash);
3338 GNUNET_assert (GNUNET_YES ==
3339 GNUNET_CONTAINER_multihashmap_remove (pg->peers
3340 [starting_peer].connect_peers,
3344 [dfs_ctx.second_uid].daemon));
3345 starting_peer = dfs_ctx.second_uid;
3348 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3350 /* Remove the "old" connections */
3351 GNUNET_CONTAINER_multihashmap_destroy (pg->
3352 peers[pg_iter].connect_peers);
3353 /* And replace with the working set */
3354 pg->peers[pg_iter].connect_peers =
3355 pg->peers[pg_iter].connect_peers_working_set;
3360 * Internal callback for topology information for a particular peer.
3363 internal_topology_callback (void *cls,
3364 const struct GNUNET_PeerIdentity *peer,
3365 const struct GNUNET_TRANSPORT_ATS_Information
3368 struct CoreContext *core_ctx = cls;
3369 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
3371 if (peer == NULL) /* Either finished, or something went wrong */
3373 iter_ctx->completed++;
3374 iter_ctx->connected--;
3375 /* One core context allocated per iteration, must free! */
3376 GNUNET_free (core_ctx);
3380 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id,
3384 if (iter_ctx->completed == iter_ctx->total)
3386 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
3387 /* Once all are done, free the iteration context */
3388 GNUNET_free (iter_ctx);
3394 * Check running topology iteration tasks, if below max start a new one, otherwise
3395 * schedule for some time in the future.
3398 schedule_get_topology (void *cls,
3399 const struct GNUNET_SCHEDULER_TaskContext *tc)
3401 struct CoreContext *core_context = cls;
3402 struct TopologyIterateContext *topology_context =
3403 (struct TopologyIterateContext *) core_context->iter_context;
3404 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3407 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3409 #if VERBOSE_TESTING > 2
3410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3412 ("Delaying connect, we have too many outstanding connections!\n"));
3414 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3415 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3416 &schedule_get_topology, core_context);
3420 #if VERBOSE_TESTING > 2
3421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3422 _("Creating connection, outstanding_connections is %d\n"),
3423 outstanding_connects);
3425 topology_context->connected++;
3428 GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
3429 &internal_topology_callback,
3432 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
3433 internal_topology_callback (core_context, NULL, NULL);
3439 * Iterate over all (running) peers in the peer group, retrieve
3440 * all connections that each currently has.
3443 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
3444 GNUNET_TESTING_NotifyTopology cb, void *cls)
3446 struct TopologyIterateContext *topology_context;
3447 struct CoreContext *core_ctx;
3449 unsigned int total_count;
3451 /* Allocate a single topology iteration context */
3452 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
3453 topology_context->topology_cb = cb;
3454 topology_context->cls = cls;
3456 for (i = 0; i < pg->total; i++)
3458 if (pg->peers[i].daemon->running == GNUNET_YES)
3460 /* Allocate one core context per core we need to connect to */
3461 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
3462 core_ctx->daemon = pg->peers[i].daemon;
3463 /* Set back pointer to topology iteration context */
3464 core_ctx->iter_context = topology_context;
3465 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
3469 if (total_count == 0)
3471 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
3472 GNUNET_free (topology_context);
3475 topology_context->total = total_count;
3480 * Callback function to process statistic values.
3481 * This handler is here only really to insert a peer
3482 * identity (or daemon) so the statistics can be uniquely
3483 * tied to a single running peer.
3485 * @param cls closure
3486 * @param subsystem name of subsystem that created the statistic
3487 * @param name the name of the datum
3488 * @param value the current value
3489 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
3490 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
3493 internal_stats_callback (void *cls,
3494 const char *subsystem,
3495 const char *name, uint64_t value, int is_persistent)
3497 struct StatsCoreContext *core_context = cls;
3498 struct StatsIterateContext *stats_context =
3499 (struct StatsIterateContext *) core_context->iter_context;
3501 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
3502 subsystem, name, value, is_persistent);
3506 * Internal continuation call for statistics iteration.
3508 * @param cls closure, the CoreContext for this iteration
3509 * @param success whether or not the statistics iterations
3510 * was canceled or not (we don't care)
3513 internal_stats_cont (void *cls, int success)
3515 struct StatsCoreContext *core_context = cls;
3516 struct StatsIterateContext *stats_context =
3517 (struct StatsIterateContext *) core_context->iter_context;
3519 stats_context->connected--;
3520 stats_context->completed++;
3522 if (stats_context->completed == stats_context->total)
3524 stats_context->cont (stats_context->cls, GNUNET_YES);
3525 GNUNET_free (stats_context);
3528 if (core_context->stats_handle != NULL)
3529 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
3531 GNUNET_free (core_context);
3535 * Check running topology iteration tasks, if below max start a new one, otherwise
3536 * schedule for some time in the future.
3539 schedule_get_statistics (void *cls,
3540 const struct GNUNET_SCHEDULER_TaskContext *tc)
3542 struct StatsCoreContext *core_context = cls;
3543 struct StatsIterateContext *stats_context =
3544 (struct StatsIterateContext *) core_context->iter_context;
3546 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3549 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3551 #if VERBOSE_TESTING > 2
3552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3554 ("Delaying connect, we have too many outstanding connections!\n"));
3556 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3557 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3558 &schedule_get_statistics, core_context);
3562 #if VERBOSE_TESTING > 2
3563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3564 _("Creating connection, outstanding_connections is %d\n"),
3565 outstanding_connects);
3568 stats_context->connected++;
3569 core_context->stats_handle =
3570 GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
3571 if (core_context->stats_handle == NULL)
3573 internal_stats_cont (core_context, GNUNET_NO);
3577 core_context->stats_get_handle =
3578 GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
3579 GNUNET_TIME_relative_get_forever (),
3580 &internal_stats_cont, &internal_stats_callback,
3582 if (core_context->stats_get_handle == NULL)
3583 internal_stats_cont (core_context, GNUNET_NO);
3588 struct DuplicateStats
3591 * Next item in the list
3593 struct DuplicateStats *next;
3596 * Nasty string, concatenation of relevant information.
3598 char *unique_string;
3602 * Check whether the combination of port/host/unix domain socket
3603 * already exists in the list of peers being checked for statistics.
3605 * @param pg the peergroup in question
3606 * @param specific_peer the peer we're concerned with
3607 * @param stats_list the list to return to the caller
3609 * @return GNUNET_YES if the statistics instance has been seen already,
3610 * GNUNET_NO if not (and we may have added it to the list)
3613 stats_check_existing (struct GNUNET_TESTING_PeerGroup *pg,
3614 struct PeerData *specific_peer,
3615 struct DuplicateStats **stats_list)
3617 struct DuplicateStats *pos;
3618 char *unix_domain_socket;
3619 unsigned long long port;
3622 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
3623 "single_statistics_per_host"))
3624 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
3628 GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg, "statistics",
3629 "unixpath", &unix_domain_socket))
3633 GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg, "statistics",
3636 GNUNET_free(unix_domain_socket);
3640 if (specific_peer->daemon->hostname != NULL)
3641 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
3642 unix_domain_socket, port);
3644 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
3648 if (0 == strcmp (to_match, pos->unique_string))
3650 GNUNET_free (unix_domain_socket);
3651 GNUNET_free (to_match);
3656 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
3657 pos->unique_string = to_match;
3658 pos->next = *stats_list;
3660 GNUNET_free (unix_domain_socket);
3665 * Iterate over all (running) peers in the peer group, retrieve
3666 * all statistics from each.
3669 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
3670 GNUNET_STATISTICS_Callback cont,
3671 GNUNET_TESTING_STATISTICS_Iterator proc,
3674 struct StatsIterateContext *stats_context;
3675 struct StatsCoreContext *core_ctx;
3677 unsigned int total_count;
3678 struct DuplicateStats *stats_list;
3679 struct DuplicateStats *pos;
3682 /* Allocate a single stats iteration context */
3683 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
3684 stats_context->cont = cont;
3685 stats_context->proc = proc;
3686 stats_context->cls = cls;
3689 for (i = 0; i < pg->total; i++)
3691 if ((pg->peers[i].daemon->running == GNUNET_YES)
3693 stats_check_existing (pg, &pg->peers[i], &stats_list)))
3695 /* Allocate one core context per core we need to connect to */
3696 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
3697 core_ctx->daemon = pg->peers[i].daemon;
3698 /* Set back pointer to topology iteration context */
3699 core_ctx->iter_context = stats_context;
3700 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
3705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3706 "Retrieving stats from %u total instances.\n", total_count);
3707 stats_context->total = total_count;
3708 if (stats_list != NULL)
3713 GNUNET_free (pos->unique_string);
3714 stats_list = pos->next;
3716 pos = stats_list->next;
3723 * There are many ways to connect peers that are supported by this function.
3724 * To connect peers in the same topology that was created via the
3725 * GNUNET_TESTING_create_topology, the topology variable must be set to
3726 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
3727 * a new instance of that topology will be generated and attempted to be
3728 * connected. This could result in some connections being impossible,
3729 * because some topologies are non-deterministic.
3731 * @param pg the peer group struct representing the running peers
3732 * @param topology which topology to connect the peers in
3733 * @param options options for connecting the topology
3734 * @param option_modifier modifier for options that take a parameter
3735 * @param notify_callback notification to be called once all connections completed
3736 * @param notify_cls closure for notification callback
3738 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
3741 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3742 enum GNUNET_TESTING_Topology topology,
3743 enum GNUNET_TESTING_TopologyOption options,
3744 double option_modifier,
3745 GNUNET_TESTING_NotifyCompletion
3746 notify_callback, void *notify_cls)
3750 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3751 #if VERBOSE_TOPOLOGY
3752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3753 _("Creating clique CONNECT topology\n"));
3755 create_clique (pg, &add_actual_connections);
3757 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3758 #if VERBOSE_TOPOLOGY
3759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3760 _("Creating small world (ring) CONNECT topology\n"));
3762 create_small_world_ring (pg, &add_actual_connections);
3764 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3765 #if VERBOSE_TOPOLOGY
3766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3767 _("Creating small world (2d-torus) CONNECT topology\n"));
3769 create_small_world (pg, &add_actual_connections);
3771 case GNUNET_TESTING_TOPOLOGY_RING:
3772 #if VERBOSE_TOPOLOGY
3773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3774 _("Creating ring CONNECT topology\n"));
3776 create_ring (pg, &add_actual_connections);
3778 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3779 #if VERBOSE_TOPOLOGY
3780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3781 _("Creating 2d torus CONNECT topology\n"));
3783 create_2d_torus (pg, &add_actual_connections);
3785 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3786 #if VERBOSE_TOPOLOGY
3787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3788 _("Creating Erdos-Renyi CONNECT topology\n"));
3790 create_erdos_renyi (pg, &add_actual_connections);
3792 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3793 #if VERBOSE_TOPOLOGY
3794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3795 _("Creating InterNAT CONNECT topology\n"));
3797 create_nated_internet (pg, &add_actual_connections);
3799 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3800 #if VERBOSE_TOPOLOGY
3801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3802 _("Creating Scale Free CONNECT topology\n"));
3804 create_scale_free (pg, &add_actual_connections);
3806 case GNUNET_TESTING_TOPOLOGY_LINE:
3807 #if VERBOSE_TOPOLOGY
3808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3809 _("Creating straight line CONNECT topology\n"));
3811 create_line (pg, &add_actual_connections);
3813 case GNUNET_TESTING_TOPOLOGY_NONE:
3814 #if VERBOSE_TOPOLOGY
3815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3816 _("Creating no CONNECT topology\n"));
3818 copy_allowed_topology (pg);
3821 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3823 ("Unknown topology specification, can't connect peers!\n"));
3824 return GNUNET_SYSERR;
3829 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
3830 #if VERBOSE_TOPOLOGY
3831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3833 ("Connecting random subset (%'.2f percent) of possible peers\n"),
3834 100 * option_modifier);
3836 choose_random_connections (pg, option_modifier);
3838 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
3839 #if VERBOSE_TOPOLOGY
3840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3841 _("Connecting a minimum of %u peers each (if possible)\n"),
3842 (unsigned int) option_modifier);
3844 choose_minimum (pg, (unsigned int) option_modifier);
3846 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
3847 #if VERBOSE_TOPOLOGY
3848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3850 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
3851 (unsigned int) option_modifier);
3853 perform_dfs (pg, (int) option_modifier);
3855 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
3856 #if VERBOSE_TOPOLOGY
3857 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3859 ("Finding additional %u closest peers each (if possible)\n"),
3860 (unsigned int) option_modifier);
3862 add_closest (pg, (unsigned int) option_modifier,
3863 &add_actual_connections);
3865 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
3867 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
3873 return connect_topology (pg, notify_callback, notify_cls);
3877 * Callback that is called whenever a hostkey is generated
3878 * for a peer. Call the real callback and decrement the
3879 * starting counter for the peergroup.
3881 * @param cls closure
3882 * @param id identifier for the daemon, NULL on error
3883 * @param d handle for the daemon
3884 * @param emsg error message (NULL on success)
3887 internal_hostkey_callback (void *cls,
3888 const struct GNUNET_PeerIdentity *id,
3889 struct GNUNET_TESTING_Daemon *d, const char *emsg)
3891 struct InternalStartContext *internal_context = cls;
3892 internal_context->peer->pg->starting--;
3893 internal_context->peer->pg->started++;
3894 if (internal_context->hostkey_callback != NULL)
3895 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
3897 else if (internal_context->peer->pg->started ==
3898 internal_context->peer->pg->total)
3900 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
3901 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
3906 * Callback that is called whenever a peer has finished starting.
3907 * Call the real callback and decrement the starting counter
3908 * for the peergroup.
3910 * @param cls closure
3911 * @param id identifier for the daemon, NULL on error
3912 * @param d handle for the daemon
3913 * @param emsg error message (NULL on success)
3916 internal_startup_callback (void *cls,
3917 const struct GNUNET_PeerIdentity *id,
3918 const struct GNUNET_CONFIGURATION_Handle *cfg,
3919 struct GNUNET_TESTING_Daemon *d, const char *emsg)
3921 struct InternalStartContext *internal_context = cls;
3922 internal_context->peer->pg->starting--;
3923 if (internal_context->start_cb != NULL)
3924 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
3929 internal_continue_startup (void *cls,
3930 const struct GNUNET_SCHEDULER_TaskContext *tc)
3932 struct InternalStartContext *internal_context = cls;
3934 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3939 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
3941 internal_context->peer->pg->starting++;
3942 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
3946 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3947 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3948 &internal_continue_startup,
3955 * Callback for informing us about a successful
3956 * or unsuccessful churn start call.
3958 * @param cls a ChurnContext
3959 * @param id the peer identity of the started peer
3960 * @param cfg the handle to the configuration of the peer
3961 * @param d handle to the daemon for the peer
3962 * @param emsg NULL on success, non-NULL on failure
3966 churn_start_callback (void *cls,
3967 const struct GNUNET_PeerIdentity *id,
3968 const struct GNUNET_CONFIGURATION_Handle *cfg,
3969 struct GNUNET_TESTING_Daemon *d, const char *emsg)
3971 struct ChurnRestartContext *startup_ctx = cls;
3972 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
3974 unsigned int total_left;
3975 char *error_message;
3977 error_message = NULL;
3980 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3981 "Churn stop callback failed with error `%s'\n", emsg);
3982 churn_ctx->num_failed_start++;
3986 churn_ctx->num_to_start--;
3990 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3991 "Started peer, %d left.\n", churn_ctx->num_to_start);
3995 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
3996 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3998 if (total_left == 0)
4000 if ((churn_ctx->num_failed_stop > 0)
4001 || (churn_ctx->num_failed_start > 0))
4002 GNUNET_asprintf (&error_message,
4003 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4004 churn_ctx->num_failed_start,
4005 churn_ctx->num_failed_stop);
4006 churn_ctx->cb (churn_ctx->cb_cls, error_message);
4007 GNUNET_free_non_null (error_message);
4008 GNUNET_free (churn_ctx);
4009 GNUNET_free (startup_ctx);
4015 schedule_churn_restart (void *cls,
4016 const struct GNUNET_SCHEDULER_TaskContext *tc)
4018 struct PeerRestartContext *peer_restart_ctx = cls;
4019 struct ChurnRestartContext *startup_ctx =
4020 peer_restart_ctx->churn_restart_ctx;
4022 if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
4023 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4024 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4025 &schedule_churn_restart, peer_restart_ctx);
4028 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
4029 startup_ctx->timeout,
4030 &churn_start_callback,
4032 GNUNET_free (peer_restart_ctx);
4037 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4039 struct InternalStartContext *internal_context = cls;
4041 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4046 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
4048 internal_context->peer->pg->starting++;
4049 internal_context->peer->daemon =
4050 GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
4051 internal_context->timeout,
4052 internal_context->hostname,
4053 internal_context->username,
4054 internal_context->sshport,
4055 &internal_hostkey_callback,
4057 &internal_startup_callback,
4062 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4063 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4064 &internal_start, internal_context);
4069 * Function which continues a peer group starting up
4070 * after successfully generating hostkeys for each peer.
4072 * @param pg the peer group to continue starting
4076 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg)
4081 for (i = 0; i < pg->total; i++)
4083 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
4084 &pg->peers[i].internal_context);
4085 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
4090 * Start count gnunet instances with the same set of transports and
4091 * applications. The port numbers (any option called "PORT") will be
4092 * adjusted to ensure that no two peers running on the same system
4093 * have the same port(s) in their respective configurations.
4095 * @param cfg configuration template to use
4096 * @param total number of daemons to start
4097 * @param timeout total time allowed for peers to start
4098 * @param hostkey_callback function to call on each peers hostkey generation
4099 * if NULL, peers will be started by this call, if non-null,
4100 * GNUNET_TESTING_daemons_continue_startup must be called after
4101 * successful hostkey generation
4102 * @param hostkey_cls closure for hostkey callback
4103 * @param cb function to call on each daemon that was started
4104 * @param cb_cls closure for cb
4105 * @param connect_callback function to call each time two hosts are connected
4106 * @param connect_callback_cls closure for connect_callback
4107 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
4109 * @return NULL on error, otherwise handle to control peer group
4111 struct GNUNET_TESTING_PeerGroup *
4112 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
4114 struct GNUNET_TIME_Relative timeout,
4115 GNUNET_TESTING_NotifyHostkeyCreated
4116 hostkey_callback, void *hostkey_cls,
4117 GNUNET_TESTING_NotifyDaemonRunning cb,
4119 GNUNET_TESTING_NotifyConnection
4120 connect_callback, void *connect_callback_cls,
4121 const struct GNUNET_TESTING_Host *hostnames)
4123 struct GNUNET_TESTING_PeerGroup *pg;
4124 const struct GNUNET_TESTING_Host *hostpos;
4130 const char *hostname;
4131 const char *username;
4132 char *baseservicehome;
4133 char *newservicehome;
4135 struct GNUNET_CONFIGURATION_Handle *pcfg;
4137 unsigned int hostcnt;
4150 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
4152 pg->notify_connection = connect_callback;
4153 pg->notify_connection_cls = connect_callback_cls;
4155 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
4156 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
4157 if (NULL != hostnames)
4160 hostpos = hostnames;
4161 while (hostpos != NULL)
4163 hostpos = hostpos->next;
4166 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4169 hostpos = hostnames;
4170 while (hostpos != NULL)
4172 pg->hosts[off].minport = LOW_PORT;
4173 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
4174 if (hostpos->username != NULL)
4175 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
4176 pg->hosts[off].sshport = hostpos->port;
4177 hostpos = hostpos->next;
4187 pg->num_hosts = off;
4191 /* skip leading spaces */
4192 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
4195 while ('\0' != *rpos)
4197 if (isspace ((unsigned char) *rpos))
4201 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4203 start = GNUNET_strdup (hostnames);
4205 while ('\0' != *pos)
4207 if (isspace ((unsigned char) *pos))
4210 if (strlen (start) > 0)
4212 pg->hosts[off].minport = LOW_PORT;
4213 pg->hosts[off++].hostname = start;
4219 if (strlen (start) > 0)
4221 pg->hosts[off].minport = LOW_PORT;
4222 pg->hosts[off++].hostname = start;
4226 GNUNET_free (start);
4227 GNUNET_free (pg->hosts);
4231 minport = 0; /* make gcc happy */
4239 for (off = 0; off < total; off++)
4243 hostname = pg->hosts[off % hostcnt].hostname;
4244 username = pg->hosts[off % hostcnt].username;
4245 sshport = pg->hosts[off % hostcnt].sshport;
4246 pcfg = make_config (cfg,
4247 &pg->hosts[off % hostcnt].minport,
4248 &upnum, hostname, &fdnum);
4255 pcfg = make_config (cfg, &minport, &upnum, hostname, &fdnum);
4260 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4262 ("Could not create configuration for peer number %u on `%s'!\n"),
4263 off, hostname == NULL ? "localhost" : hostname);
4268 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
4271 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
4272 GNUNET_free (baseservicehome);
4276 tmpdir = getenv ("TMPDIR");
4277 tmpdir = tmpdir ? tmpdir : "/tmp";
4278 GNUNET_asprintf (&newservicehome,
4280 tmpdir, "gnunet-testing-test-test", off);
4282 GNUNET_CONFIGURATION_set_value_string (pcfg,
4284 "SERVICEHOME", newservicehome);
4285 GNUNET_free (newservicehome);
4286 pg->peers[off].cfg = pcfg;
4287 pg->peers[off].allowed_peers =
4288 GNUNET_CONTAINER_multihashmap_create (total);
4289 pg->peers[off].connect_peers =
4290 GNUNET_CONTAINER_multihashmap_create (total);
4291 pg->peers[off].blacklisted_peers =
4292 GNUNET_CONTAINER_multihashmap_create (total);
4293 pg->peers[off].pg = pg;
4295 pg->peers[off].internal_context.peer = &pg->peers[off];
4296 pg->peers[off].internal_context.timeout = timeout;
4297 pg->peers[off].internal_context.hostname = hostname;
4298 pg->peers[off].internal_context.username = username;
4299 pg->peers[off].internal_context.sshport = sshport;
4300 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
4301 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
4302 pg->peers[off].internal_context.start_cb = cb;
4303 pg->peers[off].internal_context.start_cb_cls = cb_cls;
4305 GNUNET_SCHEDULER_add_now (&internal_start,
4306 &pg->peers[off].internal_context);
4313 * Get a daemon by number, so callers don't have to do nasty
4314 * offsetting operation.
4316 struct GNUNET_TESTING_Daemon *
4317 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
4318 unsigned int position)
4320 if (position < pg->total)
4321 return pg->peers[position].daemon;
4327 * Get a daemon by peer identity, so callers can
4328 * retrieve the daemon without knowing it's offset.
4330 * @param pg the peer group to retrieve the daemon from
4331 * @param peer_id the peer identity of the daemon to retrieve
4333 * @return the daemon on success, or NULL if no such peer identity is found
4335 struct GNUNET_TESTING_Daemon *
4336 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
4337 struct GNUNET_PeerIdentity *peer_id)
4341 for (i = 0; i < pg->total; i++)
4344 memcmp (&pg->peers[i].daemon->id, peer_id,
4345 sizeof (struct GNUNET_PeerIdentity)))
4346 return pg->peers[i].daemon;
4353 * Prototype of a function that will be called when a
4354 * particular operation was completed the testing library.
4356 * @param cls closure (a struct RestartContext)
4357 * @param id id of the peer that was restarted
4358 * @param cfg handle to the configuration of the peer
4359 * @param d handle to the daemon that was restarted
4360 * @param emsg NULL on success
4363 restart_callback (void *cls,
4364 const struct GNUNET_PeerIdentity *id,
4365 const struct GNUNET_CONFIGURATION_Handle *cfg,
4366 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4368 struct RestartContext *restart_context = cls;
4372 restart_context->peers_restarted++;
4376 restart_context->peers_restart_failed++;
4379 if (restart_context->peers_restarted == restart_context->peer_group->total)
4381 restart_context->callback (restart_context->callback_cls, NULL);
4382 GNUNET_free (restart_context);
4384 else if (restart_context->peers_restart_failed +
4385 restart_context->peers_restarted ==
4386 restart_context->peer_group->total)
4388 restart_context->callback (restart_context->callback_cls,
4389 "Failed to restart peers!");
4390 GNUNET_free (restart_context);
4396 * Callback for informing us about a successful
4397 * or unsuccessful churn stop call.
4399 * @param cls a ChurnContext
4400 * @param emsg NULL on success, non-NULL on failure
4404 churn_stop_callback (void *cls, const char *emsg)
4406 struct ShutdownContext *shutdown_ctx = cls;
4407 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
4408 unsigned int total_left;
4409 char *error_message;
4411 error_message = NULL;
4412 shutdown_ctx->outstanding--;
4416 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4417 "Churn stop callback failed with error `%s'\n", emsg);
4418 churn_ctx->num_failed_stop++;
4422 churn_ctx->num_to_stop--;
4426 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4427 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
4430 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
4431 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4433 if (total_left == 0)
4435 if ((churn_ctx->num_failed_stop > 0)
4436 || (churn_ctx->num_failed_start > 0))
4438 GNUNET_asprintf (&error_message,
4439 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4440 churn_ctx->num_failed_start,
4441 churn_ctx->num_failed_stop);
4443 churn_ctx->cb (churn_ctx->cb_cls, error_message);
4444 GNUNET_free_non_null (error_message);
4445 GNUNET_free (churn_ctx);
4446 GNUNET_free (shutdown_ctx);
4451 * Count the number of running peers.
4453 * @param pg handle for the peer group
4455 * @return the number of currently running peers in the peer group
4458 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
4461 unsigned int running = 0;
4462 for (i = 0; i < pg->total; i++)
4464 if (pg->peers[i].daemon->running == GNUNET_YES)
4466 GNUNET_assert (running != -1);
4474 * Task to rate limit the number of outstanding peer shutdown
4475 * requests. This is necessary for making sure we don't do
4476 * too many ssh connections at once, but is generally nicer
4477 * to any system as well (graduated task starts, as opposed
4478 * to calling gnunet-arm N times all at once).
4481 schedule_churn_shutdown_task (void *cls,
4482 const struct GNUNET_SCHEDULER_TaskContext *tc)
4484 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4485 struct ShutdownContext *shutdown_ctx;
4487 GNUNET_assert (peer_shutdown_ctx != NULL);
4488 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4489 GNUNET_assert (shutdown_ctx != NULL);
4491 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4492 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4493 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4494 &schedule_churn_shutdown_task,
4498 shutdown_ctx->outstanding++;
4499 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
4500 shutdown_ctx->timeout, shutdown_ctx->cb,
4501 shutdown_ctx, GNUNET_NO, GNUNET_YES);
4502 GNUNET_free (peer_shutdown_ctx);
4507 * Simulate churn by stopping some peers (and possibly
4508 * re-starting others if churn is called multiple times). This
4509 * function can only be used to create leave-join churn (peers "never"
4510 * leave for good). First "voff" random peers that are currently
4511 * online will be taken offline; then "von" random peers that are then
4512 * offline will be put back online. No notifications will be
4513 * generated for any of these operations except for the callback upon
4516 * @param pg handle for the peer group
4517 * @param voff number of peers that should go offline
4518 * @param von number of peers that should come back online;
4519 * must be zero on first call (since "testbed_start"
4520 * always starts all of the peers)
4521 * @param timeout how long to wait for operations to finish before
4523 * @param cb function to call at the end
4524 * @param cb_cls closure for cb
4527 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
4530 struct GNUNET_TIME_Relative timeout,
4531 GNUNET_TESTING_NotifyCompletion cb,
4534 struct ChurnContext *churn_ctx;
4535 struct ShutdownContext *shutdown_ctx;
4536 struct PeerShutdownContext *peer_shutdown_ctx;
4537 struct PeerRestartContext *peer_restart_ctx;
4538 struct ChurnRestartContext *churn_startup_ctx;
4540 unsigned int running;
4541 unsigned int stopped;
4542 unsigned int total_running;
4543 unsigned int total_stopped;
4545 unsigned int *running_arr;
4546 unsigned int *stopped_arr;
4547 unsigned int *running_permute;
4548 unsigned int *stopped_permute;
4553 if ((von == 0) && (voff == 0)) /* No peers at all? */
4559 for (i = 0; i < pg->total; i++)
4561 if (pg->peers[i].daemon->running == GNUNET_YES)
4563 GNUNET_assert (running != -1);
4568 GNUNET_assert (stopped != -1);
4575 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4576 "Trying to stop more peers than are currently running!\n");
4577 cb (cb_cls, "Trying to stop more peers than are currently running!");
4583 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4584 "Trying to start more peers than are currently stopped!\n");
4585 cb (cb_cls, "Trying to start more peers than are currently stopped!");
4589 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
4593 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
4597 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
4599 running_permute = NULL;
4600 stopped_permute = NULL;
4604 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, running);
4607 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, stopped);
4609 total_running = running;
4610 total_stopped = stopped;
4614 churn_ctx->num_to_start = von;
4615 churn_ctx->num_to_stop = voff;
4617 churn_ctx->cb_cls = cb_cls;
4619 for (i = 0; i < pg->total; i++)
4621 if (pg->peers[i].daemon->running == GNUNET_YES)
4623 GNUNET_assert ((running_arr != NULL) && (total_running > running));
4624 running_arr[running] = i;
4629 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
4630 stopped_arr[stopped] = i;
4635 GNUNET_assert (running >= voff);
4638 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
4639 shutdown_ctx->cb = &churn_stop_callback;
4640 shutdown_ctx->cb_cls = churn_ctx;
4641 shutdown_ctx->total_peers = voff;
4642 shutdown_ctx->timeout = timeout;
4645 for (i = 0; i < voff; i++)
4648 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
4649 running_permute[i]);
4651 GNUNET_assert (running_arr != NULL);
4652 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
4653 peer_shutdown_ctx->daemon =
4654 pg->peers[running_arr[running_permute[i]]].daemon;
4655 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4656 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
4660 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
4662 &churn_stop_callback, churn_ctx,
4663 GNUNET_NO, GNUNET_YES); */
4666 GNUNET_assert (stopped >= von);
4669 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
4670 churn_startup_ctx->churn_ctx = churn_ctx;
4671 churn_startup_ctx->timeout = timeout;
4673 for (i = 0; i < von; i++)
4676 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
4677 stopped_permute[i]);
4679 GNUNET_assert (stopped_arr != NULL);
4680 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
4681 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
4682 peer_restart_ctx->daemon =
4683 pg->peers[stopped_arr[stopped_permute[i]]].daemon;
4684 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
4686 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
4687 timeout, &churn_start_callback, churn_ctx); */
4690 GNUNET_free_non_null (running_arr);
4691 GNUNET_free_non_null (stopped_arr);
4692 GNUNET_free_non_null (running_permute);
4693 GNUNET_free_non_null (stopped_permute);
4698 * Restart all peers in the given group.
4700 * @param pg the handle to the peer group
4701 * @param callback function to call on completion (or failure)
4702 * @param callback_cls closure for the callback function
4705 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
4706 GNUNET_TESTING_NotifyCompletion callback,
4709 struct RestartContext *restart_context;
4714 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
4715 restart_context->peer_group = pg;
4716 restart_context->peers_restarted = 0;
4717 restart_context->callback = callback;
4718 restart_context->callback_cls = callback_cls;
4720 for (off = 0; off < pg->total; off++)
4722 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
4723 &restart_callback, restart_context);
4729 * Start or stop an individual peer from the given group.
4731 * @param pg handle to the peer group
4732 * @param offset which peer to start or stop
4733 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
4734 * @param timeout how long to wait for shutdown
4735 * @param cb function to call at the end
4736 * @param cb_cls closure for cb
4739 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
4740 unsigned int offset,
4742 struct GNUNET_TIME_Relative timeout,
4743 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
4745 struct ShutdownContext *shutdown_ctx;
4746 struct ChurnRestartContext *startup_ctx;
4747 struct ChurnContext *churn_ctx;
4749 if (GNUNET_NO == desired_status)
4751 if (NULL != pg->peers[offset].daemon)
4753 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
4754 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
4755 churn_ctx->num_to_start = 0;
4756 churn_ctx->num_to_stop = 1;
4758 churn_ctx->cb_cls = cb_cls;
4759 shutdown_ctx->cb_cls = churn_ctx;
4760 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon,
4761 timeout, &churn_stop_callback,
4762 shutdown_ctx, GNUNET_NO, GNUNET_YES);
4765 else if (GNUNET_YES == desired_status)
4767 if (NULL == pg->peers[offset].daemon)
4769 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
4770 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
4771 churn_ctx->num_to_start = 1;
4772 churn_ctx->num_to_stop = 0;
4774 churn_ctx->cb_cls = cb_cls;
4775 startup_ctx->churn_ctx = churn_ctx;
4776 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
4777 timeout, &churn_start_callback,
4787 * Callback for shutting down peers in a peer group.
4789 * @param cls closure (struct ShutdownContext)
4790 * @param emsg NULL on success
4793 internal_shutdown_callback (void *cls, const char *emsg)
4795 struct ShutdownContext *shutdown_ctx = cls;
4797 shutdown_ctx->outstanding--;
4800 shutdown_ctx->peers_down++;
4804 shutdown_ctx->peers_failed++;
4807 if ((shutdown_ctx->cb != NULL)
4808 && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed ==
4809 shutdown_ctx->total_peers))
4811 if (shutdown_ctx->peers_failed > 0)
4812 shutdown_ctx->cb (shutdown_ctx->cb_cls,
4813 "Not all peers successfully shut down!");
4815 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
4816 GNUNET_free (shutdown_ctx);
4822 * Task to rate limit the number of outstanding peer shutdown
4823 * requests. This is necessary for making sure we don't do
4824 * too many ssh connections at once, but is generally nicer
4825 * to any system as well (graduated task starts, as opposed
4826 * to calling gnunet-arm N times all at once).
4829 schedule_shutdown_task (void *cls,
4830 const struct GNUNET_SCHEDULER_TaskContext *tc)
4832 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4833 struct ShutdownContext *shutdown_ctx;
4835 GNUNET_assert (peer_shutdown_ctx != NULL);
4836 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4837 GNUNET_assert (shutdown_ctx != NULL);
4839 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4840 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4841 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4842 &schedule_shutdown_task, peer_shutdown_ctx);
4845 shutdown_ctx->outstanding++;
4846 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
4847 shutdown_ctx->timeout,
4848 &internal_shutdown_callback, shutdown_ctx,
4849 GNUNET_YES, GNUNET_NO);
4850 GNUNET_free (peer_shutdown_ctx);
4855 * Shutdown all peers started in the given group.
4857 * @param pg handle to the peer group
4858 * @param timeout how long to wait for shutdown
4859 * @param cb callback to notify upon success or failure
4860 * @param cb_cls closure for cb
4863 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
4864 struct GNUNET_TIME_Relative timeout,
4865 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
4868 struct ShutdownContext *shutdown_ctx;
4869 struct PeerShutdownContext *peer_shutdown_ctx;
4871 GNUNET_assert (pg->total > 0);
4873 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
4874 shutdown_ctx->cb = cb;
4875 shutdown_ctx->cb_cls = cb_cls;
4876 shutdown_ctx->total_peers = pg->total;
4877 shutdown_ctx->timeout = timeout;
4878 /* shtudown_ctx->outstanding = 0; */
4880 for (off = 0; off < pg->total; off++)
4882 GNUNET_assert (NULL != pg->peers[off].daemon);
4883 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
4884 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
4885 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4886 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
4887 //GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4888 if (NULL != pg->peers[off].cfg)
4889 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
4890 if (pg->peers[off].allowed_peers != NULL)
4891 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
4892 if (pg->peers[off].connect_peers != NULL)
4893 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
4894 if (pg->peers[off].blacklisted_peers != NULL)
4895 GNUNET_CONTAINER_multihashmap_destroy (pg->
4896 peers[off].blacklisted_peers);
4898 GNUNET_free (pg->peers);
4899 for (off = 0; off < pg->num_hosts; off++)
4901 GNUNET_free (pg->hosts[off].hostname);
4902 GNUNET_free_non_null (pg->hosts[off].username);
4904 GNUNET_free_non_null (pg->hosts);
4909 /* end of testing_group.c */