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 * @param notify_callback callback to notify when finished
2509 * @param notify_cls closure for notify callback
2511 * @return the number of connections that will be attempted
2514 connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2515 GNUNET_TESTING_NotifyCompletion notify_callback,
2518 unsigned int pg_iter;
2521 struct ConnectTopologyContext *ct_ctx;
2523 struct PeerConnection *connection_iter;
2524 struct ConnectContext *connect_context;
2528 ct_ctx = GNUNET_malloc (sizeof (struct ConnectTopologyContext));
2529 ct_ctx->notify_connections_done = notify_callback;
2530 ct_ctx->notify_cls = notify_cls;
2533 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2536 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
2541 GNUNET_free (ct_ctx);
2544 ct_ctx->remaining_connections = total;
2547 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2549 ct_ctx->first = &pg->peers[pg_iter];
2551 GNUNET_CONTAINER_multihashmap_iterate (pg->
2552 peers[pg_iter].connect_peers,
2553 &connect_iterator, ct_ctx);
2554 GNUNET_assert (GNUNET_SYSERR != ret && ret >= 0);
2555 total = total + ret;
2558 connection_iter = FIXME;
2559 while (connection_iter != NULL)
2561 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
2562 connect_context->pg = pg;
2563 connect_context->first = FIXME;
2564 connect_context->second = connection_iter->daemon;
2565 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
2566 connection_iter = connection_iter->next;
2575 * Takes a peer group and creates a topology based on the
2576 * one specified. Creates a topology means generates friend
2577 * files for the peers so they can only connect to those allowed
2578 * by the topology. This will only have an effect once peers
2579 * are started if the FRIENDS_ONLY option is set in the base
2580 * config. Also takes an optional restrict topology which
2581 * disallows connections based on particular transports
2582 * UNLESS they are specified in the restricted topology.
2584 * @param pg the peer group struct representing the running peers
2585 * @param topology which topology to connect the peers in
2586 * @param restrict_topology disallow restrict_transports transport
2587 * connections to peers NOT in this topology
2588 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2589 * @param restrict_transports space delimited list of transports to blacklist
2590 * to create restricted topology
2592 * @return the maximum number of connections were all allowed peers
2593 * connected to each other
2596 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2597 enum GNUNET_TESTING_Topology topology,
2598 enum GNUNET_TESTING_Topology
2600 const char *restrict_transports)
2603 unsigned int num_connections;
2604 int unblacklisted_connections;
2608 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
2612 num_connections = create_clique (pg, &add_allowed_connections);
2614 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2617 _("Creating small world (ring) topology\n"));
2620 create_small_world_ring (pg, &add_allowed_connections);
2622 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2625 _("Creating small world (2d-torus) topology\n"));
2627 num_connections = create_small_world (pg, &add_allowed_connections);
2629 case GNUNET_TESTING_TOPOLOGY_RING:
2631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
2633 num_connections = create_ring (pg, &add_allowed_connections);
2635 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
2639 num_connections = create_2d_torus (pg, &add_allowed_connections);
2641 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2644 _("Creating Erdos-Renyi topology\n"));
2646 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2648 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
2652 num_connections = create_nated_internet (pg, &add_allowed_connections);
2654 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2657 _("Creating Scale Free topology\n"));
2659 num_connections = create_scale_free (pg, &add_allowed_connections);
2661 case GNUNET_TESTING_TOPOLOGY_LINE:
2663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2664 _("Creating straight line topology\n"));
2666 num_connections = create_line (pg, &add_allowed_connections);
2668 case GNUNET_TESTING_TOPOLOGY_NONE:
2670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2672 ("Creating no allowed topology (all peers can connect at core level)\n"));
2674 num_connections = 0;
2677 num_connections = 0;
2682 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2684 ret = create_and_copy_friend_files (pg);
2685 if (ret != GNUNET_OK)
2688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2689 _("Failed during friend file copying!\n"));
2691 return GNUNET_SYSERR;
2696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2697 _("Friend files created/copied successfully!\n"));
2702 /* Use the create clique method to initially set all connections as blacklisted. */
2703 if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2704 create_clique (pg, &blacklist_connections);
2706 unblacklisted_connections = 0;
2707 /* Un-blacklist connections as per the topology specified */
2708 switch (restrict_topology)
2710 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2713 _("Blacklisting all but clique topology\n"));
2715 unblacklisted_connections =
2716 create_clique (pg, &unblacklist_connections);
2718 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2721 _("Blacklisting all but small world (ring) topology\n"));
2723 unblacklisted_connections =
2724 create_small_world_ring (pg, &unblacklist_connections);
2726 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2730 ("Blacklisting all but small world (2d-torus) topology\n"));
2732 unblacklisted_connections =
2733 create_small_world (pg, &unblacklist_connections);
2735 case GNUNET_TESTING_TOPOLOGY_RING:
2737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2738 _("Blacklisting all but ring topology\n"));
2740 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2742 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2745 _("Blacklisting all but 2d torus topology\n"));
2747 unblacklisted_connections =
2748 create_2d_torus (pg, &unblacklist_connections);
2750 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2753 _("Blacklisting all but Erdos-Renyi topology\n"));
2755 unblacklisted_connections =
2756 create_erdos_renyi (pg, &unblacklist_connections);
2758 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2761 _("Blacklisting all but InterNAT topology\n"));
2763 unblacklisted_connections =
2764 create_nated_internet (pg, &unblacklist_connections);
2766 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2769 _("Blacklisting all but Scale Free topology\n"));
2771 unblacklisted_connections =
2772 create_scale_free (pg, &unblacklist_connections);
2774 case GNUNET_TESTING_TOPOLOGY_LINE:
2776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2777 _("Blacklisting all but straight line topology\n"));
2779 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2781 case GNUNET_TESTING_TOPOLOGY_NONE:
2783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2785 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
2791 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2793 ret = create_and_copy_blacklist_files (pg, restrict_transports);
2794 if (ret != GNUNET_OK)
2797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2798 _("Failed during blacklist file copying!\n"));
2805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2806 _("Blacklist files created/copied successfully!\n"));
2810 return num_connections;
2813 struct RandomContext
2818 struct GNUNET_TESTING_PeerGroup *pg;
2821 * uid of the first peer
2826 * Peer data for first peer.
2828 struct PeerData *first;
2831 * Random percentage to use
2836 struct MinimumContext
2841 struct GNUNET_TESTING_PeerGroup *pg;
2844 * uid of the first peer
2849 * Peer data for first peer.
2851 struct PeerData *first;
2854 * Number of conns per peer
2856 unsigned int num_to_add;
2859 * Permuted array of all possible connections. Only add the Nth
2860 * peer if it's in the Nth position.
2862 unsigned int *pg_array;
2865 * What number is the current element we are iterating over?
2867 unsigned int current;
2875 struct GNUNET_TESTING_PeerGroup *pg;
2878 * uid of the first peer
2883 * uid of the second peer
2885 uint32_t second_uid;
2888 * Peer data for first peer.
2890 struct PeerData *first;
2893 * Which peer has been chosen as the one to add?
2895 unsigned int chosen;
2898 * What number is the current element we are iterating over?
2900 unsigned int current;
2904 * Iterator for choosing random peers to connect.
2906 * @param cls closure, a RandomContext
2907 * @param key the key the second Daemon was stored under
2908 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2910 * @return GNUNET_YES to continue iteration
2913 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2915 struct RandomContext *random_ctx = cls;
2916 double random_number;
2917 uint32_t second_pos;
2918 GNUNET_HashCode first_hash;
2921 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2922 UINT64_MAX)) / ((double) UINT64_MAX);
2923 if (random_number < random_ctx->percentage)
2925 GNUNET_assert (GNUNET_OK ==
2926 GNUNET_CONTAINER_multihashmap_put (random_ctx->
2927 first->connect_peers_working_set,
2929 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2931 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2932 uid_from_hash (key, &second_pos);
2933 hash_from_uid (random_ctx->first_uid, &first_hash);
2934 GNUNET_assert (random_ctx->pg->total > second_pos);
2935 GNUNET_assert (GNUNET_YES ==
2936 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
2938 [second_pos].connect_peers,
2947 * Iterator for adding at least X peers to a peers connection set.
2949 * @param cls closure, MinimumContext
2950 * @param key the key the second Daemon was stored under
2951 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2953 * @return GNUNET_YES to continue iteration
2956 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2958 struct MinimumContext *min_ctx = cls;
2959 uint32_t second_pos;
2960 GNUNET_HashCode first_hash;
2963 if (GNUNET_CONTAINER_multihashmap_size
2964 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2966 for (i = 0; i < min_ctx->num_to_add; i++)
2968 if (min_ctx->pg_array[i] == min_ctx->current)
2970 GNUNET_assert (GNUNET_OK ==
2971 GNUNET_CONTAINER_multihashmap_put
2972 (min_ctx->first->connect_peers_working_set, key,
2974 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2975 uid_from_hash (key, &second_pos);
2976 hash_from_uid (min_ctx->first_uid, &first_hash);
2977 GNUNET_assert (min_ctx->pg->total > second_pos);
2978 GNUNET_assert (GNUNET_OK ==
2979 GNUNET_CONTAINER_multihashmap_put (min_ctx->
2981 [second_pos].connect_peers_working_set,
2985 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2986 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2987 GNUNET_assert (GNUNET_YES ==
2988 GNUNET_CONTAINER_multihashmap_remove
2989 (min_ctx->pg->peers[second_pos].connect_peers,
2990 &first_hash, min_ctx->first->daemon));
2997 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
3003 * Iterator for adding peers to a connection set based on a depth first search.
3005 * @param cls closure, MinimumContext
3006 * @param key the key the second daemon was stored under
3007 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3009 * @return GNUNET_YES to continue iteration
3012 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3014 struct DFSContext *dfs_ctx = cls;
3015 GNUNET_HashCode first_hash;
3017 if (dfs_ctx->current == dfs_ctx->chosen)
3019 GNUNET_assert (GNUNET_OK ==
3020 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3021 first->connect_peers_working_set,
3023 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3024 uid_from_hash (key, &dfs_ctx->second_uid);
3025 hash_from_uid (dfs_ctx->first_uid, &first_hash);
3026 GNUNET_assert (GNUNET_OK ==
3027 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3029 [dfs_ctx->second_uid].connect_peers_working_set,
3033 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3034 GNUNET_assert (GNUNET_YES ==
3035 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
3037 [dfs_ctx->second_uid].connect_peers,
3041 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
3042 return GNUNET_NO; /* We have found our peer, don't iterate more */
3051 * From the set of connections possible, choose percentage percent of connections
3052 * to actually connect.
3054 * @param pg the peergroup we are dealing with
3055 * @param percentage what percent of total connections to make
3058 choose_random_connections (struct GNUNET_TESTING_PeerGroup *pg,
3061 struct RandomContext random_ctx;
3064 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3066 random_ctx.first_uid = pg_iter;
3067 random_ctx.first = &pg->peers[pg_iter];
3068 random_ctx.percentage = percentage;
3070 pg->peers[pg_iter].connect_peers_working_set =
3071 GNUNET_CONTAINER_multihashmap_create (pg->total);
3072 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3073 &random_connect_iterator,
3075 /* Now remove the old connections */
3076 GNUNET_CONTAINER_multihashmap_destroy (pg->
3077 peers[pg_iter].connect_peers);
3078 /* And replace with the random set */
3079 pg->peers[pg_iter].connect_peers =
3080 pg->peers[pg_iter].connect_peers_working_set;
3085 * From the set of connections possible, choose at least num connections per
3088 * @param pg the peergroup we are dealing with
3089 * @param num how many connections at least should each peer have (if possible)?
3092 choose_minimum (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3094 struct MinimumContext minimum_ctx;
3097 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3099 pg->peers[pg_iter].connect_peers_working_set =
3100 GNUNET_CONTAINER_multihashmap_create (num);
3103 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3105 minimum_ctx.first_uid = pg_iter;
3106 minimum_ctx.pg_array =
3107 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
3108 GNUNET_CONTAINER_multihashmap_size
3109 (pg->peers[pg_iter].connect_peers));
3110 minimum_ctx.first = &pg->peers[pg_iter];
3111 minimum_ctx.pg = pg;
3112 minimum_ctx.num_to_add = num;
3113 minimum_ctx.current = 0;
3114 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3115 &minimum_connect_iterator,
3119 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3121 /* Remove the "old" connections */
3122 GNUNET_CONTAINER_multihashmap_destroy (pg->
3123 peers[pg_iter].connect_peers);
3124 /* And replace with the working set */
3125 pg->peers[pg_iter].connect_peers =
3126 pg->peers[pg_iter].connect_peers_working_set;
3133 count_workingset_connections (struct GNUNET_TESTING_PeerGroup *pg)
3136 unsigned int pg_iter;
3140 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3143 GNUNET_CONTAINER_multihashmap_size (pg->
3145 [pg_iter].connect_peers_working_set);
3153 count_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg)
3156 unsigned int pg_iter;
3160 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3163 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3170 struct FindClosestContext
3173 * The currently known closest peer.
3175 struct GNUNET_TESTING_Daemon *closest;
3178 * The info for the peer we are adding connections for.
3180 struct PeerData *curr_peer;
3183 * The distance (bits) between the current
3184 * peer and the currently known closest.
3186 unsigned int closest_dist;
3189 * The offset of the closest known peer in
3192 unsigned int closest_num;
3196 * Iterator over hash map entries of the allowed
3197 * peer connections. Find the closest, not already
3198 * connected peer and return it.
3200 * @param cls closure (struct FindClosestContext)
3201 * @param key current key code (hash of offset in pg)
3202 * @param value value in the hash map - a GNUNET_TESTING_Daemon
3203 * @return GNUNET_YES if we should continue to
3208 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
3210 struct FindClosestContext *closest_ctx = cls;
3211 struct GNUNET_TESTING_Daemon *daemon = value;
3213 if (((closest_ctx->closest == NULL) ||
3214 (GNUNET_CRYPTO_hash_matching_bits
3215 (&daemon->id.hashPubKey,
3216 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
3217 closest_ctx->closest_dist))
3219 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
3220 curr_peer->connect_peers,
3223 closest_ctx->closest_dist =
3224 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
3225 &closest_ctx->curr_peer->daemon->
3227 closest_ctx->closest = daemon;
3228 uid_from_hash (key, &closest_ctx->closest_num);
3234 * From the set of connections possible, choose at num connections per
3235 * peer based on depth which are closest out of those allowed. Guaranteed
3236 * to add num peers to connect to, provided there are that many peers
3237 * in the underlay topology to connect to.
3239 * @param pg the peergroup we are dealing with
3240 * @param num how many connections at least should each peer have (if possible)?
3241 * @param proc processor to actually add the connections
3244 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
3245 GNUNET_TESTING_ConnectionProcessor proc)
3247 struct FindClosestContext closest_ctx;
3251 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
3253 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3255 closest_ctx.curr_peer = &pg->peers[pg_iter];
3256 closest_ctx.closest = NULL;
3257 closest_ctx.closest_dist = 0;
3258 closest_ctx.closest_num = 0;
3259 GNUNET_CONTAINER_multihashmap_iterate (pg->
3260 peers[pg_iter].allowed_peers,
3261 &find_closest_peers,
3263 if (closest_ctx.closest != NULL)
3265 GNUNET_assert (closest_ctx.closest_num < pg->total);
3266 proc (pg, pg_iter, closest_ctx.closest_num);
3273 * From the set of connections possible, choose at least num connections per
3274 * peer based on depth first traversal of peer connections. If DFS leaves
3275 * peers unconnected, ensure those peers get connections.
3277 * @param pg the peergroup we are dealing with
3278 * @param num how many connections at least should each peer have (if possible)?
3281 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3283 struct DFSContext dfs_ctx;
3286 uint32_t starting_peer;
3287 uint32_t least_connections;
3288 GNUNET_HashCode second_hash;
3290 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3292 pg->peers[pg_iter].connect_peers_working_set =
3293 GNUNET_CONTAINER_multihashmap_create (num);
3298 while ((count_workingset_connections (pg) < num * pg->total)
3299 && (count_allowed_connections (pg) > 0))
3301 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3303 least_connections = -1; /* Set to very high number */
3304 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3306 if (GNUNET_CONTAINER_multihashmap_size
3307 (pg->peers[pg_iter].connect_peers_working_set) <
3310 starting_peer = pg_iter;
3312 GNUNET_CONTAINER_multihashmap_size (pg->
3314 [pg_iter].connect_peers_working_set);
3319 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
3325 /* Choose a random peer from the chosen peers set of connections to add */
3327 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3328 GNUNET_CONTAINER_multihashmap_size
3329 (pg->peers[starting_peer].connect_peers));
3330 dfs_ctx.first_uid = starting_peer;
3331 dfs_ctx.first = &pg->peers[starting_peer];
3333 dfs_ctx.current = 0;
3335 GNUNET_CONTAINER_multihashmap_iterate (pg->
3337 [starting_peer].connect_peers,
3338 &dfs_connect_iterator, &dfs_ctx);
3339 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
3340 hash_from_uid (dfs_ctx.second_uid, &second_hash);
3341 GNUNET_assert (GNUNET_YES ==
3342 GNUNET_CONTAINER_multihashmap_remove (pg->peers
3343 [starting_peer].connect_peers,
3347 [dfs_ctx.second_uid].daemon));
3348 starting_peer = dfs_ctx.second_uid;
3351 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3353 /* Remove the "old" connections */
3354 GNUNET_CONTAINER_multihashmap_destroy (pg->
3355 peers[pg_iter].connect_peers);
3356 /* And replace with the working set */
3357 pg->peers[pg_iter].connect_peers =
3358 pg->peers[pg_iter].connect_peers_working_set;
3363 * Internal callback for topology information for a particular peer.
3366 internal_topology_callback (void *cls,
3367 const struct GNUNET_PeerIdentity *peer,
3368 const struct GNUNET_TRANSPORT_ATS_Information
3371 struct CoreContext *core_ctx = cls;
3372 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
3374 if (peer == NULL) /* Either finished, or something went wrong */
3376 iter_ctx->completed++;
3377 iter_ctx->connected--;
3378 /* One core context allocated per iteration, must free! */
3379 GNUNET_free (core_ctx);
3383 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id,
3387 if (iter_ctx->completed == iter_ctx->total)
3389 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
3390 /* Once all are done, free the iteration context */
3391 GNUNET_free (iter_ctx);
3397 * Check running topology iteration tasks, if below max start a new one, otherwise
3398 * schedule for some time in the future.
3401 schedule_get_topology (void *cls,
3402 const struct GNUNET_SCHEDULER_TaskContext *tc)
3404 struct CoreContext *core_context = cls;
3405 struct TopologyIterateContext *topology_context =
3406 (struct TopologyIterateContext *) core_context->iter_context;
3407 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3410 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3412 #if VERBOSE_TESTING > 2
3413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3415 ("Delaying connect, we have too many outstanding connections!\n"));
3417 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3418 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3419 &schedule_get_topology, core_context);
3423 #if VERBOSE_TESTING > 2
3424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3425 _("Creating connection, outstanding_connections is %d\n"),
3426 outstanding_connects);
3428 topology_context->connected++;
3431 GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
3432 &internal_topology_callback,
3435 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
3436 internal_topology_callback (core_context, NULL, NULL);
3442 * Iterate over all (running) peers in the peer group, retrieve
3443 * all connections that each currently has.
3446 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
3447 GNUNET_TESTING_NotifyTopology cb, void *cls)
3449 struct TopologyIterateContext *topology_context;
3450 struct CoreContext *core_ctx;
3452 unsigned int total_count;
3454 /* Allocate a single topology iteration context */
3455 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
3456 topology_context->topology_cb = cb;
3457 topology_context->cls = cls;
3459 for (i = 0; i < pg->total; i++)
3461 if (pg->peers[i].daemon->running == GNUNET_YES)
3463 /* Allocate one core context per core we need to connect to */
3464 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
3465 core_ctx->daemon = pg->peers[i].daemon;
3466 /* Set back pointer to topology iteration context */
3467 core_ctx->iter_context = topology_context;
3468 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
3472 if (total_count == 0)
3474 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
3475 GNUNET_free (topology_context);
3478 topology_context->total = total_count;
3483 * Callback function to process statistic values.
3484 * This handler is here only really to insert a peer
3485 * identity (or daemon) so the statistics can be uniquely
3486 * tied to a single running peer.
3488 * @param cls closure
3489 * @param subsystem name of subsystem that created the statistic
3490 * @param name the name of the datum
3491 * @param value the current value
3492 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
3493 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
3496 internal_stats_callback (void *cls,
3497 const char *subsystem,
3498 const char *name, uint64_t value, int is_persistent)
3500 struct StatsCoreContext *core_context = cls;
3501 struct StatsIterateContext *stats_context =
3502 (struct StatsIterateContext *) core_context->iter_context;
3504 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
3505 subsystem, name, value, is_persistent);
3509 * Internal continuation call for statistics iteration.
3511 * @param cls closure, the CoreContext for this iteration
3512 * @param success whether or not the statistics iterations
3513 * was canceled or not (we don't care)
3516 internal_stats_cont (void *cls, int success)
3518 struct StatsCoreContext *core_context = cls;
3519 struct StatsIterateContext *stats_context =
3520 (struct StatsIterateContext *) core_context->iter_context;
3522 stats_context->connected--;
3523 stats_context->completed++;
3525 if (stats_context->completed == stats_context->total)
3527 stats_context->cont (stats_context->cls, GNUNET_YES);
3528 GNUNET_free (stats_context);
3531 if (core_context->stats_handle != NULL)
3532 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
3534 GNUNET_free (core_context);
3538 * Check running topology iteration tasks, if below max start a new one, otherwise
3539 * schedule for some time in the future.
3542 schedule_get_statistics (void *cls,
3543 const struct GNUNET_SCHEDULER_TaskContext *tc)
3545 struct StatsCoreContext *core_context = cls;
3546 struct StatsIterateContext *stats_context =
3547 (struct StatsIterateContext *) core_context->iter_context;
3549 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3552 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3554 #if VERBOSE_TESTING > 2
3555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3557 ("Delaying connect, we have too many outstanding connections!\n"));
3559 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3560 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3561 &schedule_get_statistics, core_context);
3565 #if VERBOSE_TESTING > 2
3566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3567 _("Creating connection, outstanding_connections is %d\n"),
3568 outstanding_connects);
3571 stats_context->connected++;
3572 core_context->stats_handle =
3573 GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
3574 if (core_context->stats_handle == NULL)
3576 internal_stats_cont (core_context, GNUNET_NO);
3580 core_context->stats_get_handle =
3581 GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
3582 GNUNET_TIME_relative_get_forever (),
3583 &internal_stats_cont, &internal_stats_callback,
3585 if (core_context->stats_get_handle == NULL)
3586 internal_stats_cont (core_context, GNUNET_NO);
3591 struct DuplicateStats
3594 * Next item in the list
3596 struct DuplicateStats *next;
3599 * Nasty string, concatenation of relevant information.
3601 char *unique_string;
3605 * Check whether the combination of port/host/unix domain socket
3606 * already exists in the list of peers being checked for statistics.
3608 * @param pg the peergroup in question
3609 * @param specific_peer the peer we're concerned with
3610 * @param stats_list the list to return to the caller
3612 * @return GNUNET_YES if the statistics instance has been seen already,
3613 * GNUNET_NO if not (and we may have added it to the list)
3616 stats_check_existing (struct GNUNET_TESTING_PeerGroup *pg,
3617 struct PeerData *specific_peer,
3618 struct DuplicateStats **stats_list)
3620 struct DuplicateStats *pos;
3621 char *unix_domain_socket;
3622 unsigned long long port;
3625 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
3626 "single_statistics_per_host"))
3627 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
3631 GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg, "statistics",
3632 "unixpath", &unix_domain_socket))
3636 GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg, "statistics",
3639 GNUNET_free(unix_domain_socket);
3643 if (specific_peer->daemon->hostname != NULL)
3644 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
3645 unix_domain_socket, port);
3647 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
3651 if (0 == strcmp (to_match, pos->unique_string))
3653 GNUNET_free (unix_domain_socket);
3654 GNUNET_free (to_match);
3659 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
3660 pos->unique_string = to_match;
3661 pos->next = *stats_list;
3663 GNUNET_free (unix_domain_socket);
3668 * Iterate over all (running) peers in the peer group, retrieve
3669 * all statistics from each.
3672 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
3673 GNUNET_STATISTICS_Callback cont,
3674 GNUNET_TESTING_STATISTICS_Iterator proc,
3677 struct StatsIterateContext *stats_context;
3678 struct StatsCoreContext *core_ctx;
3680 unsigned int total_count;
3681 struct DuplicateStats *stats_list;
3682 struct DuplicateStats *pos;
3685 /* Allocate a single stats iteration context */
3686 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
3687 stats_context->cont = cont;
3688 stats_context->proc = proc;
3689 stats_context->cls = cls;
3692 for (i = 0; i < pg->total; i++)
3694 if ((pg->peers[i].daemon->running == GNUNET_YES)
3696 stats_check_existing (pg, &pg->peers[i], &stats_list)))
3698 /* Allocate one core context per core we need to connect to */
3699 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
3700 core_ctx->daemon = pg->peers[i].daemon;
3701 /* Set back pointer to topology iteration context */
3702 core_ctx->iter_context = stats_context;
3703 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
3708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3709 "Retrieving stats from %u total instances.\n", total_count);
3710 stats_context->total = total_count;
3711 if (stats_list != NULL)
3716 GNUNET_free (pos->unique_string);
3717 stats_list = pos->next;
3719 pos = stats_list->next;
3726 * There are many ways to connect peers that are supported by this function.
3727 * To connect peers in the same topology that was created via the
3728 * GNUNET_TESTING_create_topology, the topology variable must be set to
3729 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
3730 * a new instance of that topology will be generated and attempted to be
3731 * connected. This could result in some connections being impossible,
3732 * because some topologies are non-deterministic.
3734 * @param pg the peer group struct representing the running peers
3735 * @param topology which topology to connect the peers in
3736 * @param options options for connecting the topology
3737 * @param option_modifier modifier for options that take a parameter
3738 * @param notify_callback notification to be called once all connections completed
3739 * @param notify_cls closure for notification callback
3741 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
3744 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3745 enum GNUNET_TESTING_Topology topology,
3746 enum GNUNET_TESTING_TopologyOption options,
3747 double option_modifier,
3748 GNUNET_TESTING_NotifyCompletion
3749 notify_callback, void *notify_cls)
3753 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3754 #if VERBOSE_TOPOLOGY
3755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3756 _("Creating clique CONNECT topology\n"));
3758 create_clique (pg, &add_actual_connections);
3760 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3761 #if VERBOSE_TOPOLOGY
3762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3763 _("Creating small world (ring) CONNECT topology\n"));
3765 create_small_world_ring (pg, &add_actual_connections);
3767 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3768 #if VERBOSE_TOPOLOGY
3769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3770 _("Creating small world (2d-torus) CONNECT topology\n"));
3772 create_small_world (pg, &add_actual_connections);
3774 case GNUNET_TESTING_TOPOLOGY_RING:
3775 #if VERBOSE_TOPOLOGY
3776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3777 _("Creating ring CONNECT topology\n"));
3779 create_ring (pg, &add_actual_connections);
3781 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3782 #if VERBOSE_TOPOLOGY
3783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3784 _("Creating 2d torus CONNECT topology\n"));
3786 create_2d_torus (pg, &add_actual_connections);
3788 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3789 #if VERBOSE_TOPOLOGY
3790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3791 _("Creating Erdos-Renyi CONNECT topology\n"));
3793 create_erdos_renyi (pg, &add_actual_connections);
3795 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3796 #if VERBOSE_TOPOLOGY
3797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3798 _("Creating InterNAT CONNECT topology\n"));
3800 create_nated_internet (pg, &add_actual_connections);
3802 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3803 #if VERBOSE_TOPOLOGY
3804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3805 _("Creating Scale Free CONNECT topology\n"));
3807 create_scale_free (pg, &add_actual_connections);
3809 case GNUNET_TESTING_TOPOLOGY_LINE:
3810 #if VERBOSE_TOPOLOGY
3811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3812 _("Creating straight line CONNECT topology\n"));
3814 create_line (pg, &add_actual_connections);
3816 case GNUNET_TESTING_TOPOLOGY_NONE:
3817 #if VERBOSE_TOPOLOGY
3818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3819 _("Creating no CONNECT topology\n"));
3821 copy_allowed_topology (pg);
3824 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3826 ("Unknown topology specification, can't connect peers!\n"));
3827 return GNUNET_SYSERR;
3832 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
3833 #if VERBOSE_TOPOLOGY
3834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3836 ("Connecting random subset (%'.2f percent) of possible peers\n"),
3837 100 * option_modifier);
3839 choose_random_connections (pg, option_modifier);
3841 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
3842 #if VERBOSE_TOPOLOGY
3843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3844 _("Connecting a minimum of %u peers each (if possible)\n"),
3845 (unsigned int) option_modifier);
3847 choose_minimum (pg, (unsigned int) option_modifier);
3849 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
3850 #if VERBOSE_TOPOLOGY
3851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3853 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
3854 (unsigned int) option_modifier);
3856 perform_dfs (pg, (int) option_modifier);
3858 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
3859 #if VERBOSE_TOPOLOGY
3860 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3862 ("Finding additional %u closest peers each (if possible)\n"),
3863 (unsigned int) option_modifier);
3865 add_closest (pg, (unsigned int) option_modifier,
3866 &add_actual_connections);
3868 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
3870 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
3876 return connect_topology (pg, notify_callback, notify_cls);
3880 * Callback that is called whenever a hostkey is generated
3881 * for a peer. Call the real callback and decrement the
3882 * starting counter for the peergroup.
3884 * @param cls closure
3885 * @param id identifier for the daemon, NULL on error
3886 * @param d handle for the daemon
3887 * @param emsg error message (NULL on success)
3890 internal_hostkey_callback (void *cls,
3891 const struct GNUNET_PeerIdentity *id,
3892 struct GNUNET_TESTING_Daemon *d, const char *emsg)
3894 struct InternalStartContext *internal_context = cls;
3895 internal_context->peer->pg->starting--;
3896 internal_context->peer->pg->started++;
3897 if (internal_context->hostkey_callback != NULL)
3898 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
3900 else if (internal_context->peer->pg->started ==
3901 internal_context->peer->pg->total)
3903 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
3904 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
3909 * Callback that is called whenever a peer has finished starting.
3910 * Call the real callback and decrement the starting counter
3911 * for the peergroup.
3913 * @param cls closure
3914 * @param id identifier for the daemon, NULL on error
3915 * @param d handle for the daemon
3916 * @param emsg error message (NULL on success)
3919 internal_startup_callback (void *cls,
3920 const struct GNUNET_PeerIdentity *id,
3921 const struct GNUNET_CONFIGURATION_Handle *cfg,
3922 struct GNUNET_TESTING_Daemon *d, const char *emsg)
3924 struct InternalStartContext *internal_context = cls;
3925 internal_context->peer->pg->starting--;
3926 if (internal_context->start_cb != NULL)
3927 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
3932 internal_continue_startup (void *cls,
3933 const struct GNUNET_SCHEDULER_TaskContext *tc)
3935 struct InternalStartContext *internal_context = cls;
3937 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3942 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
3944 internal_context->peer->pg->starting++;
3945 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
3949 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3950 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3951 &internal_continue_startup,
3958 * Callback for informing us about a successful
3959 * or unsuccessful churn start call.
3961 * @param cls a ChurnContext
3962 * @param id the peer identity of the started peer
3963 * @param cfg the handle to the configuration of the peer
3964 * @param d handle to the daemon for the peer
3965 * @param emsg NULL on success, non-NULL on failure
3969 churn_start_callback (void *cls,
3970 const struct GNUNET_PeerIdentity *id,
3971 const struct GNUNET_CONFIGURATION_Handle *cfg,
3972 struct GNUNET_TESTING_Daemon *d, const char *emsg)
3974 struct ChurnRestartContext *startup_ctx = cls;
3975 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
3977 unsigned int total_left;
3978 char *error_message;
3980 error_message = NULL;
3983 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3984 "Churn stop callback failed with error `%s'\n", emsg);
3985 churn_ctx->num_failed_start++;
3989 churn_ctx->num_to_start--;
3993 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3994 "Started peer, %d left.\n", churn_ctx->num_to_start);
3998 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
3999 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4001 if (total_left == 0)
4003 if ((churn_ctx->num_failed_stop > 0)
4004 || (churn_ctx->num_failed_start > 0))
4005 GNUNET_asprintf (&error_message,
4006 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4007 churn_ctx->num_failed_start,
4008 churn_ctx->num_failed_stop);
4009 churn_ctx->cb (churn_ctx->cb_cls, error_message);
4010 GNUNET_free_non_null (error_message);
4011 GNUNET_free (churn_ctx);
4012 GNUNET_free (startup_ctx);
4018 schedule_churn_restart (void *cls,
4019 const struct GNUNET_SCHEDULER_TaskContext *tc)
4021 struct PeerRestartContext *peer_restart_ctx = cls;
4022 struct ChurnRestartContext *startup_ctx =
4023 peer_restart_ctx->churn_restart_ctx;
4025 if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
4026 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4027 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4028 &schedule_churn_restart, peer_restart_ctx);
4031 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
4032 startup_ctx->timeout,
4033 &churn_start_callback,
4035 GNUNET_free (peer_restart_ctx);
4040 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4042 struct InternalStartContext *internal_context = cls;
4044 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4049 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
4051 internal_context->peer->pg->starting++;
4052 internal_context->peer->daemon =
4053 GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
4054 internal_context->timeout,
4055 internal_context->hostname,
4056 internal_context->username,
4057 internal_context->sshport,
4058 &internal_hostkey_callback,
4060 &internal_startup_callback,
4065 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4066 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4067 &internal_start, internal_context);
4072 * Function which continues a peer group starting up
4073 * after successfully generating hostkeys for each peer.
4075 * @param pg the peer group to continue starting
4079 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg)
4084 for (i = 0; i < pg->total; i++)
4086 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
4087 &pg->peers[i].internal_context);
4088 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
4093 * Start count gnunet instances with the same set of transports and
4094 * applications. The port numbers (any option called "PORT") will be
4095 * adjusted to ensure that no two peers running on the same system
4096 * have the same port(s) in their respective configurations.
4098 * @param cfg configuration template to use
4099 * @param total number of daemons to start
4100 * @param timeout total time allowed for peers to start
4101 * @param hostkey_callback function to call on each peers hostkey generation
4102 * if NULL, peers will be started by this call, if non-null,
4103 * GNUNET_TESTING_daemons_continue_startup must be called after
4104 * successful hostkey generation
4105 * @param hostkey_cls closure for hostkey callback
4106 * @param cb function to call on each daemon that was started
4107 * @param cb_cls closure for cb
4108 * @param connect_callback function to call each time two hosts are connected
4109 * @param connect_callback_cls closure for connect_callback
4110 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
4112 * @return NULL on error, otherwise handle to control peer group
4114 struct GNUNET_TESTING_PeerGroup *
4115 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
4117 struct GNUNET_TIME_Relative timeout,
4118 GNUNET_TESTING_NotifyHostkeyCreated
4119 hostkey_callback, void *hostkey_cls,
4120 GNUNET_TESTING_NotifyDaemonRunning cb,
4122 GNUNET_TESTING_NotifyConnection
4123 connect_callback, void *connect_callback_cls,
4124 const struct GNUNET_TESTING_Host *hostnames)
4126 struct GNUNET_TESTING_PeerGroup *pg;
4127 const struct GNUNET_TESTING_Host *hostpos;
4133 const char *hostname;
4134 const char *username;
4135 char *baseservicehome;
4136 char *newservicehome;
4138 struct GNUNET_CONFIGURATION_Handle *pcfg;
4140 unsigned int hostcnt;
4153 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
4155 pg->notify_connection = connect_callback;
4156 pg->notify_connection_cls = connect_callback_cls;
4158 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
4159 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
4160 if (NULL != hostnames)
4163 hostpos = hostnames;
4164 while (hostpos != NULL)
4166 hostpos = hostpos->next;
4169 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4172 hostpos = hostnames;
4173 while (hostpos != NULL)
4175 pg->hosts[off].minport = LOW_PORT;
4176 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
4177 if (hostpos->username != NULL)
4178 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
4179 pg->hosts[off].sshport = hostpos->port;
4180 hostpos = hostpos->next;
4190 pg->num_hosts = off;
4194 /* skip leading spaces */
4195 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
4198 while ('\0' != *rpos)
4200 if (isspace ((unsigned char) *rpos))
4204 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4206 start = GNUNET_strdup (hostnames);
4208 while ('\0' != *pos)
4210 if (isspace ((unsigned char) *pos))
4213 if (strlen (start) > 0)
4215 pg->hosts[off].minport = LOW_PORT;
4216 pg->hosts[off++].hostname = start;
4222 if (strlen (start) > 0)
4224 pg->hosts[off].minport = LOW_PORT;
4225 pg->hosts[off++].hostname = start;
4229 GNUNET_free (start);
4230 GNUNET_free (pg->hosts);
4234 minport = 0; /* make gcc happy */
4242 for (off = 0; off < total; off++)
4246 hostname = pg->hosts[off % hostcnt].hostname;
4247 username = pg->hosts[off % hostcnt].username;
4248 sshport = pg->hosts[off % hostcnt].sshport;
4249 pcfg = make_config (cfg,
4250 &pg->hosts[off % hostcnt].minport,
4251 &upnum, hostname, &fdnum);
4258 pcfg = make_config (cfg, &minport, &upnum, hostname, &fdnum);
4263 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4265 ("Could not create configuration for peer number %u on `%s'!\n"),
4266 off, hostname == NULL ? "localhost" : hostname);
4271 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
4274 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
4275 GNUNET_free (baseservicehome);
4279 tmpdir = getenv ("TMPDIR");
4280 tmpdir = tmpdir ? tmpdir : "/tmp";
4281 GNUNET_asprintf (&newservicehome,
4283 tmpdir, "gnunet-testing-test-test", off);
4285 GNUNET_CONFIGURATION_set_value_string (pcfg,
4287 "SERVICEHOME", newservicehome);
4288 GNUNET_free (newservicehome);
4289 pg->peers[off].cfg = pcfg;
4290 pg->peers[off].allowed_peers =
4291 GNUNET_CONTAINER_multihashmap_create (total);
4292 pg->peers[off].connect_peers =
4293 GNUNET_CONTAINER_multihashmap_create (total);
4294 pg->peers[off].blacklisted_peers =
4295 GNUNET_CONTAINER_multihashmap_create (total);
4296 pg->peers[off].pg = pg;
4298 pg->peers[off].internal_context.peer = &pg->peers[off];
4299 pg->peers[off].internal_context.timeout = timeout;
4300 pg->peers[off].internal_context.hostname = hostname;
4301 pg->peers[off].internal_context.username = username;
4302 pg->peers[off].internal_context.sshport = sshport;
4303 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
4304 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
4305 pg->peers[off].internal_context.start_cb = cb;
4306 pg->peers[off].internal_context.start_cb_cls = cb_cls;
4308 GNUNET_SCHEDULER_add_now (&internal_start,
4309 &pg->peers[off].internal_context);
4316 * Get a daemon by number, so callers don't have to do nasty
4317 * offsetting operation.
4319 struct GNUNET_TESTING_Daemon *
4320 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
4321 unsigned int position)
4323 if (position < pg->total)
4324 return pg->peers[position].daemon;
4330 * Get a daemon by peer identity, so callers can
4331 * retrieve the daemon without knowing it's offset.
4333 * @param pg the peer group to retrieve the daemon from
4334 * @param peer_id the peer identity of the daemon to retrieve
4336 * @return the daemon on success, or NULL if no such peer identity is found
4338 struct GNUNET_TESTING_Daemon *
4339 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
4340 struct GNUNET_PeerIdentity *peer_id)
4344 for (i = 0; i < pg->total; i++)
4347 memcmp (&pg->peers[i].daemon->id, peer_id,
4348 sizeof (struct GNUNET_PeerIdentity)))
4349 return pg->peers[i].daemon;
4356 * Prototype of a function that will be called when a
4357 * particular operation was completed the testing library.
4359 * @param cls closure (a struct RestartContext)
4360 * @param id id of the peer that was restarted
4361 * @param cfg handle to the configuration of the peer
4362 * @param d handle to the daemon that was restarted
4363 * @param emsg NULL on success
4366 restart_callback (void *cls,
4367 const struct GNUNET_PeerIdentity *id,
4368 const struct GNUNET_CONFIGURATION_Handle *cfg,
4369 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4371 struct RestartContext *restart_context = cls;
4375 restart_context->peers_restarted++;
4379 restart_context->peers_restart_failed++;
4382 if (restart_context->peers_restarted == restart_context->peer_group->total)
4384 restart_context->callback (restart_context->callback_cls, NULL);
4385 GNUNET_free (restart_context);
4387 else if (restart_context->peers_restart_failed +
4388 restart_context->peers_restarted ==
4389 restart_context->peer_group->total)
4391 restart_context->callback (restart_context->callback_cls,
4392 "Failed to restart peers!");
4393 GNUNET_free (restart_context);
4399 * Callback for informing us about a successful
4400 * or unsuccessful churn stop call.
4402 * @param cls a ChurnContext
4403 * @param emsg NULL on success, non-NULL on failure
4407 churn_stop_callback (void *cls, const char *emsg)
4409 struct ShutdownContext *shutdown_ctx = cls;
4410 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
4411 unsigned int total_left;
4412 char *error_message;
4414 error_message = NULL;
4415 shutdown_ctx->outstanding--;
4419 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4420 "Churn stop callback failed with error `%s'\n", emsg);
4421 churn_ctx->num_failed_stop++;
4425 churn_ctx->num_to_stop--;
4429 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4430 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
4433 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
4434 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4436 if (total_left == 0)
4438 if ((churn_ctx->num_failed_stop > 0)
4439 || (churn_ctx->num_failed_start > 0))
4441 GNUNET_asprintf (&error_message,
4442 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4443 churn_ctx->num_failed_start,
4444 churn_ctx->num_failed_stop);
4446 churn_ctx->cb (churn_ctx->cb_cls, error_message);
4447 GNUNET_free_non_null (error_message);
4448 GNUNET_free (churn_ctx);
4449 GNUNET_free (shutdown_ctx);
4454 * Count the number of running peers.
4456 * @param pg handle for the peer group
4458 * @return the number of currently running peers in the peer group
4461 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
4464 unsigned int running = 0;
4465 for (i = 0; i < pg->total; i++)
4467 if (pg->peers[i].daemon->running == GNUNET_YES)
4469 GNUNET_assert (running != -1);
4477 * Task to rate limit the number of outstanding peer shutdown
4478 * requests. This is necessary for making sure we don't do
4479 * too many ssh connections at once, but is generally nicer
4480 * to any system as well (graduated task starts, as opposed
4481 * to calling gnunet-arm N times all at once).
4484 schedule_churn_shutdown_task (void *cls,
4485 const struct GNUNET_SCHEDULER_TaskContext *tc)
4487 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4488 struct ShutdownContext *shutdown_ctx;
4490 GNUNET_assert (peer_shutdown_ctx != NULL);
4491 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4492 GNUNET_assert (shutdown_ctx != NULL);
4494 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4495 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4496 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4497 &schedule_churn_shutdown_task,
4501 shutdown_ctx->outstanding++;
4502 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
4503 shutdown_ctx->timeout, shutdown_ctx->cb,
4504 shutdown_ctx, GNUNET_NO, GNUNET_YES);
4505 GNUNET_free (peer_shutdown_ctx);
4510 * Simulate churn by stopping some peers (and possibly
4511 * re-starting others if churn is called multiple times). This
4512 * function can only be used to create leave-join churn (peers "never"
4513 * leave for good). First "voff" random peers that are currently
4514 * online will be taken offline; then "von" random peers that are then
4515 * offline will be put back online. No notifications will be
4516 * generated for any of these operations except for the callback upon
4519 * @param pg handle for the peer group
4520 * @param voff number of peers that should go offline
4521 * @param von number of peers that should come back online;
4522 * must be zero on first call (since "testbed_start"
4523 * always starts all of the peers)
4524 * @param timeout how long to wait for operations to finish before
4526 * @param cb function to call at the end
4527 * @param cb_cls closure for cb
4530 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
4533 struct GNUNET_TIME_Relative timeout,
4534 GNUNET_TESTING_NotifyCompletion cb,
4537 struct ChurnContext *churn_ctx;
4538 struct ShutdownContext *shutdown_ctx;
4539 struct PeerShutdownContext *peer_shutdown_ctx;
4540 struct PeerRestartContext *peer_restart_ctx;
4541 struct ChurnRestartContext *churn_startup_ctx;
4543 unsigned int running;
4544 unsigned int stopped;
4545 unsigned int total_running;
4546 unsigned int total_stopped;
4548 unsigned int *running_arr;
4549 unsigned int *stopped_arr;
4550 unsigned int *running_permute;
4551 unsigned int *stopped_permute;
4556 if ((von == 0) && (voff == 0)) /* No peers at all? */
4562 for (i = 0; i < pg->total; i++)
4564 if (pg->peers[i].daemon->running == GNUNET_YES)
4566 GNUNET_assert (running != -1);
4571 GNUNET_assert (stopped != -1);
4578 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4579 "Trying to stop more peers than are currently running!\n");
4580 cb (cb_cls, "Trying to stop more peers than are currently running!");
4586 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4587 "Trying to start more peers than are currently stopped!\n");
4588 cb (cb_cls, "Trying to start more peers than are currently stopped!");
4592 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
4596 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
4600 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
4602 running_permute = NULL;
4603 stopped_permute = NULL;
4607 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, running);
4610 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, stopped);
4612 total_running = running;
4613 total_stopped = stopped;
4617 churn_ctx->num_to_start = von;
4618 churn_ctx->num_to_stop = voff;
4620 churn_ctx->cb_cls = cb_cls;
4622 for (i = 0; i < pg->total; i++)
4624 if (pg->peers[i].daemon->running == GNUNET_YES)
4626 GNUNET_assert ((running_arr != NULL) && (total_running > running));
4627 running_arr[running] = i;
4632 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
4633 stopped_arr[stopped] = i;
4638 GNUNET_assert (running >= voff);
4641 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
4642 shutdown_ctx->cb = &churn_stop_callback;
4643 shutdown_ctx->cb_cls = churn_ctx;
4644 shutdown_ctx->total_peers = voff;
4645 shutdown_ctx->timeout = timeout;
4648 for (i = 0; i < voff; i++)
4651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
4652 running_permute[i]);
4654 GNUNET_assert (running_arr != NULL);
4655 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
4656 peer_shutdown_ctx->daemon =
4657 pg->peers[running_arr[running_permute[i]]].daemon;
4658 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4659 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
4663 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
4665 &churn_stop_callback, churn_ctx,
4666 GNUNET_NO, GNUNET_YES); */
4669 GNUNET_assert (stopped >= von);
4672 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
4673 churn_startup_ctx->churn_ctx = churn_ctx;
4674 churn_startup_ctx->timeout = timeout;
4676 for (i = 0; i < von; i++)
4679 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
4680 stopped_permute[i]);
4682 GNUNET_assert (stopped_arr != NULL);
4683 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
4684 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
4685 peer_restart_ctx->daemon =
4686 pg->peers[stopped_arr[stopped_permute[i]]].daemon;
4687 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
4689 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
4690 timeout, &churn_start_callback, churn_ctx); */
4693 GNUNET_free_non_null (running_arr);
4694 GNUNET_free_non_null (stopped_arr);
4695 GNUNET_free_non_null (running_permute);
4696 GNUNET_free_non_null (stopped_permute);
4701 * Restart all peers in the given group.
4703 * @param pg the handle to the peer group
4704 * @param callback function to call on completion (or failure)
4705 * @param callback_cls closure for the callback function
4708 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
4709 GNUNET_TESTING_NotifyCompletion callback,
4712 struct RestartContext *restart_context;
4717 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
4718 restart_context->peer_group = pg;
4719 restart_context->peers_restarted = 0;
4720 restart_context->callback = callback;
4721 restart_context->callback_cls = callback_cls;
4723 for (off = 0; off < pg->total; off++)
4725 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
4726 &restart_callback, restart_context);
4732 * Start or stop an individual peer from the given group.
4734 * @param pg handle to the peer group
4735 * @param offset which peer to start or stop
4736 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
4737 * @param timeout how long to wait for shutdown
4738 * @param cb function to call at the end
4739 * @param cb_cls closure for cb
4742 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
4743 unsigned int offset,
4745 struct GNUNET_TIME_Relative timeout,
4746 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
4748 struct ShutdownContext *shutdown_ctx;
4749 struct ChurnRestartContext *startup_ctx;
4750 struct ChurnContext *churn_ctx;
4752 if (GNUNET_NO == desired_status)
4754 if (NULL != pg->peers[offset].daemon)
4756 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
4757 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
4758 churn_ctx->num_to_start = 0;
4759 churn_ctx->num_to_stop = 1;
4761 churn_ctx->cb_cls = cb_cls;
4762 shutdown_ctx->cb_cls = churn_ctx;
4763 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon,
4764 timeout, &churn_stop_callback,
4765 shutdown_ctx, GNUNET_NO, GNUNET_YES);
4768 else if (GNUNET_YES == desired_status)
4770 if (NULL == pg->peers[offset].daemon)
4772 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
4773 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
4774 churn_ctx->num_to_start = 1;
4775 churn_ctx->num_to_stop = 0;
4777 churn_ctx->cb_cls = cb_cls;
4778 startup_ctx->churn_ctx = churn_ctx;
4779 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
4780 timeout, &churn_start_callback,
4790 * Callback for shutting down peers in a peer group.
4792 * @param cls closure (struct ShutdownContext)
4793 * @param emsg NULL on success
4796 internal_shutdown_callback (void *cls, const char *emsg)
4798 struct ShutdownContext *shutdown_ctx = cls;
4800 shutdown_ctx->outstanding--;
4803 shutdown_ctx->peers_down++;
4807 shutdown_ctx->peers_failed++;
4810 if ((shutdown_ctx->cb != NULL)
4811 && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed ==
4812 shutdown_ctx->total_peers))
4814 if (shutdown_ctx->peers_failed > 0)
4815 shutdown_ctx->cb (shutdown_ctx->cb_cls,
4816 "Not all peers successfully shut down!");
4818 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
4819 GNUNET_free (shutdown_ctx);
4825 * Task to rate limit the number of outstanding peer shutdown
4826 * requests. This is necessary for making sure we don't do
4827 * too many ssh connections at once, but is generally nicer
4828 * to any system as well (graduated task starts, as opposed
4829 * to calling gnunet-arm N times all at once).
4832 schedule_shutdown_task (void *cls,
4833 const struct GNUNET_SCHEDULER_TaskContext *tc)
4835 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4836 struct ShutdownContext *shutdown_ctx;
4838 GNUNET_assert (peer_shutdown_ctx != NULL);
4839 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4840 GNUNET_assert (shutdown_ctx != NULL);
4842 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4843 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4844 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4845 &schedule_shutdown_task, peer_shutdown_ctx);
4848 shutdown_ctx->outstanding++;
4849 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
4850 shutdown_ctx->timeout,
4851 &internal_shutdown_callback, shutdown_ctx,
4852 GNUNET_YES, GNUNET_NO);
4853 GNUNET_free (peer_shutdown_ctx);
4858 * Shutdown all peers started in the given group.
4860 * @param pg handle to the peer group
4861 * @param timeout how long to wait for shutdown
4862 * @param cb callback to notify upon success or failure
4863 * @param cb_cls closure for cb
4866 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
4867 struct GNUNET_TIME_Relative timeout,
4868 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
4871 struct ShutdownContext *shutdown_ctx;
4872 struct PeerShutdownContext *peer_shutdown_ctx;
4874 GNUNET_assert (pg->total > 0);
4876 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
4877 shutdown_ctx->cb = cb;
4878 shutdown_ctx->cb_cls = cb_cls;
4879 shutdown_ctx->total_peers = pg->total;
4880 shutdown_ctx->timeout = timeout;
4881 /* shtudown_ctx->outstanding = 0; */
4883 for (off = 0; off < pg->total; off++)
4885 GNUNET_assert (NULL != pg->peers[off].daemon);
4886 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
4887 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
4888 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4889 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
4890 //GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4891 if (NULL != pg->peers[off].cfg)
4892 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
4893 if (pg->peers[off].allowed_peers != NULL)
4894 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
4895 if (pg->peers[off].connect_peers != NULL)
4896 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
4897 if (pg->peers[off].blacklisted_peers != NULL)
4898 GNUNET_CONTAINER_multihashmap_destroy (pg->
4899 peers[off].blacklisted_peers);
4901 GNUNET_free (pg->peers);
4902 for (off = 0; off < pg->num_hosts; off++)
4904 GNUNET_free (pg->hosts[off].hostname);
4905 GNUNET_free_non_null (pg->hosts[off].username);
4907 GNUNET_free_non_null (pg->hosts);
4912 /* end of testing_group.c */