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",
850 GNUNET_snprintf (uval,
852 "/tmp/test-service-%s-%u",
853 section, ctx->fdnum % num_per_host);
856 GNUNET_free (single_variable);
857 GNUNET_free (per_host_variable);
861 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
863 value = ctx->hostname;
866 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
871 * Create a new configuration using the given configuration
872 * as a template; however, each PORT in the existing cfg
873 * must be renumbered by incrementing "*port". If we run
874 * out of "*port" numbers, return NULL.
876 * @param cfg template configuration
877 * @param port port numbers to use, update to reflect
878 * port numbers that were used
879 * @param upnum number to make unix domain socket names unique
880 * @param hostname hostname of the controlling host, to allow control connections from
881 * @param fdnum number used to offset the unix domain socket for grouped processes
882 * (such as statistics or peerinfo, which can be shared among others)
884 * @return new configuration, NULL on error
886 static struct GNUNET_CONFIGURATION_Handle *
887 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
889 uint32_t * upnum, const char *hostname, uint32_t * fdnum)
891 struct UpdateContext uc;
900 uc.ret = GNUNET_CONFIGURATION_create ();
901 uc.hostname = hostname;
904 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
905 if (uc.nport >= HIGH_PORT)
908 GNUNET_CONFIGURATION_destroy (uc.ret);
912 if (GNUNET_CONFIGURATION_get_value_string
913 (cfg, "testing", "control_host", &control_host) == GNUNET_OK)
915 if (hostname != NULL)
916 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
919 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
921 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
923 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
924 "ACCEPT_FROM", allowed_hosts);
925 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
927 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
928 "ACCEPT_FROM", allowed_hosts);
929 GNUNET_free_non_null (control_host);
930 GNUNET_free (allowed_hosts);
934 /* arm needs to know to allow connections from the host on which it is running,
935 * otherwise gnunet-arm is unable to connect to it in some instances */
936 if (hostname != NULL)
938 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
939 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
941 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
943 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
945 GNUNET_free (allowed_hosts);
949 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
950 "BINDTO", "127.0.0.1");
951 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
952 "BINDTO", "127.0.0.1");
955 *port = (uint16_t) uc.nport;
964 * Add entries to the peers connect list
966 * @param pg the peer group we are working with
967 * @param first index of the first peer
968 * @param second index of the second peer
970 * @return the number of connections added
971 * technically should only be 0 or 2
975 add_actual_connections (struct GNUNET_TESTING_PeerGroup *pg,
976 unsigned int first, unsigned int second)
982 GNUNET_HashCode hash_first;
983 GNUNET_HashCode hash_second;
985 hash_from_uid (first, &hash_first);
986 hash_from_uid (second, &hash_second);
988 add_first = GNUNET_NO;
990 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].connect_peers,
993 add_first = GNUNET_YES;
996 add_second = GNUNET_NO;
998 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].connect_peers,
1001 add_second = GNUNET_YES;
1007 GNUNET_assert (GNUNET_OK ==
1008 GNUNET_CONTAINER_multihashmap_put (pg->
1010 [first].connect_peers,
1013 peers[second].daemon,
1014 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1015 pg->peers[first].num_connections++;
1021 GNUNET_assert (GNUNET_OK ==
1022 GNUNET_CONTAINER_multihashmap_put (pg->
1024 [second].connect_peers,
1027 peers[first].daemon,
1028 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1029 pg->peers[second].num_connections++;
1038 * Add entries to the peers allowed connections list
1040 * @param pg the peer group we are working with
1041 * @param first index of the first peer
1042 * @param second index of the second peer
1044 * @return the number of connections added (can be 0, 1 or 2)
1045 * technically should only be 0 or 2, but the small price
1046 * of iterating over the lists (hashmaps in the future)
1047 * for being sure doesn't bother me!
1051 add_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg,
1052 unsigned int first, unsigned int second)
1056 struct PeerConnection *first_iter;
1057 struct PeerConnection *second_iter;
1058 struct PeerConnection *new_first;
1059 struct PeerConnection *new_second;
1064 GNUNET_HashCode hash_first;
1065 GNUNET_HashCode hash_second;
1067 hash_from_uid (first, &hash_first);
1068 hash_from_uid (second, &hash_second);
1070 add_first = GNUNET_NO;
1072 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].allowed_peers,
1075 add_first = GNUNET_YES;
1078 add_second = GNUNET_NO;
1080 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].allowed_peers,
1083 add_second = GNUNET_YES;
1086 first_iter = pg->peers[first].connected_peers;
1087 while (first_iter != NULL)
1089 if (first_iter->daemon == pg->peers[second].daemon)
1090 add_first = GNUNET_NO;
1091 first_iter = first_iter->next;
1094 second_iter = pg->peers[second].connected_peers;
1095 add_second = GNUNET_YES;
1096 while (second_iter != NULL)
1098 if (second_iter->daemon == pg->peers[first].daemon)
1099 add_second = GNUNET_NO;
1100 second_iter = second_iter->next;
1107 GNUNET_assert (GNUNET_OK ==
1108 GNUNET_CONTAINER_multihashmap_put (pg->
1110 [first].allowed_peers,
1113 peers[second].daemon,
1114 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1116 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1117 new_first->daemon = pg->peers[second].daemon;
1118 new_first->next = pg->peers[first].connected_peers;
1119 pg->peers[first].connected_peers = new_first;
1121 pg->peers[first].num_connections++;
1127 GNUNET_assert (GNUNET_OK ==
1128 GNUNET_CONTAINER_multihashmap_put (pg->
1130 [second].allowed_peers,
1133 peers[first].daemon,
1134 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1136 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1137 new_second->daemon = pg->peers[first].daemon;
1138 new_second->next = pg->peers[second].connected_peers;
1139 pg->peers[second].connected_peers = new_second;
1140 pg->peers[first].num_connections++;
1142 pg->peers[second].num_connections++;
1150 * Add entries to the peers blacklisted list
1152 * @param pg the peer group we are working with
1153 * @param first index of the first peer
1154 * @param second index of the second peer
1156 * @return the number of connections added (can be 0, 1 or 2)
1160 blacklist_connections (struct GNUNET_TESTING_PeerGroup *pg,
1161 unsigned int first, unsigned int second)
1166 GNUNET_HashCode hash_first;
1167 GNUNET_HashCode hash_second;
1169 hash_from_uid (first, &hash_first);
1170 hash_from_uid (second, &hash_second);
1172 add_first = GNUNET_NO;
1174 GNUNET_CONTAINER_multihashmap_contains (pg->
1175 peers[first].blacklisted_peers,
1178 add_first = GNUNET_YES;
1181 add_second = GNUNET_NO;
1183 GNUNET_CONTAINER_multihashmap_contains (pg->
1184 peers[second].blacklisted_peers,
1187 add_second = GNUNET_YES;
1193 GNUNET_assert (GNUNET_OK ==
1194 GNUNET_CONTAINER_multihashmap_put (pg->
1196 [first].blacklisted_peers,
1199 peers[second].daemon,
1200 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1201 pg->peers[first].num_connections++;
1207 GNUNET_assert (GNUNET_OK ==
1208 GNUNET_CONTAINER_multihashmap_put (pg->
1210 [second].blacklisted_peers,
1213 peers[first].daemon,
1214 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1215 pg->peers[second].num_connections++;
1223 * Remove entries from the peers blacklisted list
1225 * @param pg the peer group we are working with
1226 * @param first index of the first peer
1227 * @param second index of the second peer
1229 * @return the number of connections removed (can be 0, 1 or 2)
1233 unblacklist_connections (struct GNUNET_TESTING_PeerGroup *pg,
1234 unsigned int first, unsigned int second)
1239 GNUNET_HashCode hash_first;
1240 GNUNET_HashCode hash_second;
1242 hash_from_uid (first, &hash_first);
1243 hash_from_uid (second, &hash_second);
1246 GNUNET_CONTAINER_multihashmap_contains (pg->
1247 peers[first].blacklisted_peers,
1250 GNUNET_CONTAINER_multihashmap_contains (pg->
1251 peers[second].blacklisted_peers,
1257 GNUNET_assert (GNUNET_YES ==
1258 GNUNET_CONTAINER_multihashmap_remove (pg->
1260 [first].blacklisted_peers,
1270 GNUNET_assert (GNUNET_YES ==
1271 GNUNET_CONTAINER_multihashmap_remove (pg->
1273 [second].blacklisted_peers,
1285 * Scale free network construction as described in:
1287 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1289 * Start with a network of "one" peer, then progressively add
1290 * peers up to the total number. At each step, iterate over
1291 * all possible peers and connect new peer based on number of
1292 * existing connections of the target peer.
1294 * @param pg the peer group we are dealing with
1295 * @param proc the connection processor to use
1297 * @return the number of connections created
1300 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg,
1301 GNUNET_TESTING_ConnectionProcessor proc)
1304 unsigned int total_connections;
1305 unsigned int outer_count;
1307 unsigned int previous_total_connections;
1311 GNUNET_assert (pg->total > 1);
1313 /* Add a connection between the first two nodes */
1314 total_connections = proc (pg, 0, 1);
1316 for (outer_count = 1; outer_count < pg->total; outer_count++)
1318 previous_total_connections = total_connections;
1319 for (i = 0; i < outer_count; i++)
1322 pg->peers[i].num_connections /
1323 (double) previous_total_connections;
1326 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1327 UINT64_MAX)) / ((double) UINT64_MAX);
1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1330 "Considering connecting peer %d to peer %d\n",
1333 if (random < probability)
1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1337 "Connecting peer %d to peer %d\n", outer_count, i);
1339 total_connections += proc (pg, outer_count, i);
1344 return total_connections;
1348 * Create a topology given a peer group (set of running peers)
1349 * and a connection processor.
1351 * @param pg the peergroup to create the topology on
1352 * @param proc the connection processor to call to actually set
1353 * up connections between two peers
1355 * @return the number of connections that were set up
1359 create_small_world_ring (struct GNUNET_TESTING_PeerGroup *pg,
1360 GNUNET_TESTING_ConnectionProcessor proc)
1364 unsigned int natLog;
1365 unsigned int randomPeer;
1366 double random, logNModifier, percentage;
1367 unsigned int smallWorldConnections;
1372 unsigned int useAnd;
1373 int connect_attempts;
1375 logNModifier = 0.5; /* FIXME: default value? */
1376 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1381 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1382 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1384 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1385 p_string, "LOGNMODIFIER", "TESTING");
1386 GNUNET_free (p_string);
1388 percentage = 0.5; /* FIXME: default percentage? */
1389 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1394 if (sscanf (p_string, "%lf", &percentage) != 1)
1395 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1397 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1398 p_string, "PERCENTAGE", "TESTING");
1399 GNUNET_free (p_string);
1401 natLog = log (pg->total);
1402 connsPerPeer = ceil (natLog * logNModifier);
1404 if (connsPerPeer % 2 == 1)
1407 smallWorldConnections = 0;
1408 connect_attempts = 0;
1409 for (i = 0; i < pg->total; i++)
1412 max = i + connsPerPeer / 2;
1413 min = i - connsPerPeer / 2;
1415 if (max > pg->total - 1)
1417 max = max - pg->total;
1423 min = pg->total - 1 + min;
1427 for (j = 0; j < connsPerPeer / 2; j++)
1431 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1432 UINT64_MAX) / ((double) UINT64_MAX));
1433 if (random < percentage)
1435 /* Connect to uniformly selected random peer */
1437 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1439 while ((((randomPeer < max) && (randomPeer > min))
1440 && (useAnd == 0)) || (((randomPeer > min)
1441 || (randomPeer < max))
1445 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1448 smallWorldConnections += proc (pg, i, randomPeer);
1452 nodeToConnect = i + j + 1;
1453 if (nodeToConnect > pg->total - 1)
1455 nodeToConnect = nodeToConnect - pg->total;
1457 connect_attempts += proc (pg, i, nodeToConnect);
1463 connect_attempts += smallWorldConnections;
1465 return connect_attempts;
1469 * Create a topology given a peer group (set of running peers)
1470 * and a connection processor.
1472 * @param pg the peergroup to create the topology on
1473 * @param proc the connection processor to call to actually set
1474 * up connections between two peers
1476 * @return the number of connections that were set up
1480 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg,
1481 GNUNET_TESTING_ConnectionProcessor proc)
1483 unsigned int outer_count, inner_count;
1484 unsigned int cutoff;
1485 int connect_attempts;
1486 double nat_percentage;
1489 nat_percentage = 0.6; /* FIXME: default percentage? */
1490 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1495 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1496 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1498 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1499 p_string, "PERCENTAGE", "TESTING");
1500 GNUNET_free (p_string);
1505 cutoff = (unsigned int) (nat_percentage * pg->total);
1507 connect_attempts = 0;
1509 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1511 for (inner_count = outer_count + 1; inner_count < pg->total;
1514 if ((outer_count > cutoff) || (inner_count > cutoff))
1517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1518 "Connecting peer %d to peer %d\n",
1519 outer_count, inner_count);
1521 connect_attempts += proc (pg, outer_count, inner_count);
1526 return connect_attempts;
1531 * Create a topology given a peer group (set of running peers)
1532 * and a connection processor.
1534 * @param pg the peergroup to create the topology on
1535 * @param proc the connection processor to call to actually set
1536 * up connections between two peers
1538 * @return the number of connections that were set up
1542 create_small_world (struct GNUNET_TESTING_PeerGroup *pg,
1543 GNUNET_TESTING_ConnectionProcessor proc)
1545 unsigned int i, j, k;
1546 unsigned int square;
1549 unsigned int toggle = 1;
1550 unsigned int nodeToConnect;
1551 unsigned int natLog;
1552 unsigned int node1Row;
1553 unsigned int node1Col;
1554 unsigned int node2Row;
1555 unsigned int node2Col;
1556 unsigned int distance;
1557 double probability, random, percentage;
1558 unsigned int smallWorldConnections;
1559 unsigned int small_world_it;
1561 int connect_attempts;
1562 square = floor (sqrt (pg->total));
1566 percentage = 0.5; /* FIXME: default percentage? */
1567 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1572 if (sscanf (p_string, "%lf", &percentage) != 1)
1573 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1575 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1576 p_string, "PERCENTAGE", "TESTING");
1577 GNUNET_free (p_string);
1579 if (percentage < 0.0)
1581 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1583 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1584 "PERCENTAGE", "TESTING", percentage);
1587 probability = 0.5; /* FIXME: default percentage? */
1588 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1593 if (sscanf (p_string, "%lf", &probability) != 1)
1594 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1596 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1597 p_string, "PROBABILITY", "TESTING");
1598 GNUNET_free (p_string);
1600 if (square * square != pg->total)
1602 while (rows * cols < pg->total)
1604 if (toggle % 2 == 0)
1613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1615 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1619 connect_attempts = 0;
1620 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1621 * to the node to its right and above. Once this is over, we'll have our torus!
1622 * Special case for the last node (if the rows and columns are not equal), connect
1623 * to the first in the row to maintain topology.
1625 for (i = 0; i < pg->total; i++)
1627 /* First connect to the node to the right */
1628 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1629 nodeToConnect = i + 1;
1630 else if (i + 1 == pg->total)
1631 nodeToConnect = rows * cols - cols;
1633 nodeToConnect = i - cols + 1;
1635 connect_attempts += proc (pg, i, nodeToConnect);
1638 nodeToConnect = (rows * cols) - cols + i;
1640 nodeToConnect = i - cols;
1642 if (nodeToConnect < pg->total)
1643 connect_attempts += proc (pg, i, nodeToConnect);
1645 natLog = log (pg->total);
1646 #if VERBOSE_TESTING > 2
1647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1648 _("natural log of %d is %d, will run %d iterations\n"),
1649 pg->total, natLog, (int) (natLog * percentage));
1650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1651 _("Total connections added thus far: %u!\n"), connect_attempts);
1653 smallWorldConnections = 0;
1654 small_world_it = (unsigned int) (natLog * percentage);
1655 if (small_world_it < 1)
1657 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
1658 for (i = 0; i < small_world_it; i++)
1660 for (j = 0; j < pg->total; j++)
1662 /* Determine the row and column of node at position j on the 2d torus */
1663 node1Row = j / cols;
1664 node1Col = j - (node1Row * cols);
1665 for (k = 0; k < pg->total; k++)
1667 /* Determine the row and column of node at position k on the 2d torus */
1668 node2Row = k / cols;
1669 node2Col = k - (node2Row * cols);
1670 /* Simple Cartesian distance */
1672 abs (node1Row - node2Row) + abs (node1Col - node2Col);
1675 /* Calculate probability as 1 over the square of the distance */
1676 probability = 1.0 / (distance * distance);
1677 /* Choose a random value between 0 and 1 */
1680 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1682 ((double) UINT64_MAX);
1683 /* If random < probability, then connect the two nodes */
1684 if (random < probability)
1685 smallWorldConnections += proc (pg, j, k);
1691 connect_attempts += smallWorldConnections;
1692 #if VERBOSE_TESTING > 2
1693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1694 _("Total connections added for small world: %d!\n"),
1695 smallWorldConnections);
1697 return connect_attempts;
1701 * Create a topology given a peer group (set of running peers)
1702 * and a connection processor.
1704 * @param pg the peergroup to create the topology on
1705 * @param proc the connection processor to call to actually set
1706 * up connections between two peers
1708 * @return the number of connections that were set up
1712 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg,
1713 GNUNET_TESTING_ConnectionProcessor proc)
1716 unsigned int outer_count;
1717 unsigned int inner_count;
1718 int connect_attempts;
1722 probability = 0.5; /* FIXME: default percentage? */
1723 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1728 if (sscanf (p_string, "%lf", &probability) != 1)
1729 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1731 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1732 p_string, "PROBABILITY", "TESTING");
1733 GNUNET_free (p_string);
1735 connect_attempts = 0;
1736 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1738 for (inner_count = outer_count + 1; inner_count < pg->total;
1743 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1744 UINT64_MAX)) / ((double) UINT64_MAX);
1746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1747 _("rand is %f probability is %f\n"), temp_rand,
1750 if (temp_rand < probability)
1752 connect_attempts += proc (pg, outer_count, inner_count);
1757 return connect_attempts;
1761 * Create a topology given a peer group (set of running peers)
1762 * and a connection processor. This particular function creates
1763 * the connections for a 2d-torus, plus additional "closest"
1764 * connections per peer.
1766 * @param pg the peergroup to create the topology on
1767 * @param proc the connection processor to call to actually set
1768 * up connections between two peers
1770 * @return the number of connections that were set up
1774 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg,
1775 GNUNET_TESTING_ConnectionProcessor proc)
1778 unsigned int square;
1781 unsigned int toggle = 1;
1782 unsigned int nodeToConnect;
1783 int connect_attempts;
1785 connect_attempts = 0;
1787 square = floor (sqrt (pg->total));
1791 if (square * square != pg->total)
1793 while (rows * cols < pg->total)
1795 if (toggle % 2 == 0)
1804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1806 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1809 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1810 * to the node to its right and above. Once this is over, we'll have our torus!
1811 * Special case for the last node (if the rows and columns are not equal), connect
1812 * to the first in the row to maintain topology.
1814 for (i = 0; i < pg->total; i++)
1816 /* First connect to the node to the right */
1817 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1818 nodeToConnect = i + 1;
1819 else if (i + 1 == pg->total)
1820 nodeToConnect = rows * cols - cols;
1822 nodeToConnect = i - cols + 1;
1824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1825 "Connecting peer %d to peer %d\n", i, nodeToConnect);
1827 connect_attempts += proc (pg, i, nodeToConnect);
1829 /* Second connect to the node immediately above */
1831 nodeToConnect = (rows * cols) - cols + i;
1833 nodeToConnect = i - cols;
1835 if (nodeToConnect < pg->total)
1838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1839 "Connecting peer %d to peer %d\n", i, nodeToConnect);
1841 connect_attempts += proc (pg, i, nodeToConnect);
1846 return connect_attempts;
1851 * Create a topology given a peer group (set of running peers)
1852 * and a connection processor.
1854 * @param pg the peergroup to create the topology on
1855 * @param proc the connection processor to call to actually set
1856 * up connections between two peers
1858 * @return the number of connections that were set up
1862 create_clique (struct GNUNET_TESTING_PeerGroup *pg,
1863 GNUNET_TESTING_ConnectionProcessor proc)
1865 unsigned int outer_count;
1866 unsigned int inner_count;
1867 int connect_attempts;
1869 connect_attempts = 0;
1871 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1873 for (inner_count = outer_count + 1; inner_count < pg->total;
1877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1878 "Connecting peer %d to peer %d\n",
1879 outer_count, inner_count);
1881 connect_attempts += proc (pg, outer_count, inner_count);
1885 return connect_attempts;
1889 * Create a topology given a peer group (set of running peers)
1890 * and a connection processor.
1892 * @param pg the peergroup to create the topology on
1893 * @param proc the connection processor to call to actually set
1894 * up connections between two peers
1896 * @return the number of connections that were set up
1900 create_line (struct GNUNET_TESTING_PeerGroup *pg,
1901 GNUNET_TESTING_ConnectionProcessor proc)
1904 int connect_attempts;
1906 connect_attempts = 0;
1908 /* Connect each peer to the next highest numbered peer */
1909 for (count = 0; count < pg->total - 1; count++)
1912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1913 "Connecting peer %d to peer %d\n", count, count + 1);
1915 connect_attempts += proc (pg, count, count + 1);
1918 return connect_attempts;
1922 * Create a topology given a peer group (set of running peers)
1923 * and a connection processor.
1925 * @param pg the peergroup to create the topology on
1926 * @param proc the connection processor to call to actually set
1927 * up connections between two peers
1929 * @return the number of connections that were set up
1933 create_ring (struct GNUNET_TESTING_PeerGroup *pg,
1934 GNUNET_TESTING_ConnectionProcessor proc)
1937 int connect_attempts;
1939 connect_attempts = 0;
1941 /* Connect each peer to the next highest numbered peer */
1942 for (count = 0; count < pg->total - 1; count++)
1945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1946 "Connecting peer %d to peer %d\n", count, count + 1);
1948 connect_attempts += proc (pg, count, count + 1);
1951 /* Connect the last peer to the first peer */
1952 connect_attempts += proc (pg, pg->total - 1, 0);
1954 return connect_attempts;
1959 * Iterator for writing friends of a peer to a file.
1961 * @param cls closure, an open writable file handle
1962 * @param key the key the daemon was stored under
1963 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1965 * @return GNUNET_YES to continue iteration
1967 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1968 * with a single file_iterator that takes a closure which contains
1969 * the prefix to write before the peer. Then this could be used
1970 * for blacklisting multiple transports and writing the friend
1971 * file. I'm sure *someone* will complain loudly about other
1972 * things that negate these functions even existing so no point in
1976 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1978 FILE *temp_friend_handle = cls;
1979 struct GNUNET_TESTING_Daemon *peer = value;
1980 struct GNUNET_PeerIdentity *temppeer;
1981 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1983 temppeer = &peer->id;
1984 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
1985 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
1990 struct BlacklistContext
1993 * The (open) file handle to write to
1995 FILE *temp_file_handle;
1998 * The transport that this peer will be blacklisted on.
2004 * Iterator for writing blacklist data to appropriate files.
2006 * @param cls closure, an open writable file handle
2007 * @param key the key the daemon was stored under
2008 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2010 * @return GNUNET_YES to continue iteration
2013 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2015 struct BlacklistContext *blacklist_ctx = cls;
2016 //FILE *temp_blacklist_handle = cls;
2017 struct GNUNET_TESTING_Daemon *peer = value;
2018 struct GNUNET_PeerIdentity *temppeer;
2019 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2021 temppeer = &peer->id;
2022 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2023 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2024 blacklist_ctx->transport, (char *) &peer_enc);
2030 * Create the friend files based on the PeerConnection's
2031 * of each peer in the peer group, and copy the files
2032 * to the appropriate place
2034 * @param pg the peer group we are dealing with
2037 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
2039 FILE *temp_friend_handle;
2040 unsigned int pg_iter;
2041 char *temp_service_path;
2042 struct GNUNET_OS_Process **procarr;
2045 enum GNUNET_OS_ProcessStatusType type;
2046 unsigned long return_code;
2051 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2052 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2054 mytemp = GNUNET_DISK_mktemp ("friends");
2055 GNUNET_assert (mytemp != NULL);
2056 temp_friend_handle = fopen (mytemp, "wt");
2057 GNUNET_assert (temp_friend_handle != NULL);
2058 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2059 &friend_file_iterator,
2060 temp_friend_handle);
2061 fclose (temp_friend_handle);
2064 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2065 daemon->cfg, "PATHS",
2067 &temp_service_path))
2069 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2071 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2072 "SERVICEHOME", "PATHS");
2073 if (UNLINK (mytemp) != 0)
2074 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2076 GNUNET_free (mytemp);
2080 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2082 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2083 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2088 _("Copying file with command cp %s %s\n"), mytemp, arg);
2093 else /* Remote, scp the file to the correct place */
2095 if (NULL != pg->peers[pg_iter].daemon->username)
2096 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2097 pg->peers[pg_iter].daemon->username,
2098 pg->peers[pg_iter].daemon->hostname,
2101 GNUNET_asprintf (&arg, "%s:%s/friends",
2102 pg->peers[pg_iter].daemon->hostname,
2105 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2110 _("Copying file with command scp %s %s\n"), mytemp,
2115 GNUNET_free (temp_service_path);
2116 GNUNET_free (mytemp);
2120 ret = GNUNET_SYSERR;
2121 while ((count < max_wait) && (ret != GNUNET_OK))
2124 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2128 _("Checking copy status of file %d\n"), pg_iter);
2130 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2132 if (GNUNET_OS_process_status
2133 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2135 ret = GNUNET_SYSERR;
2137 else if ((type != GNUNET_OS_PROCESS_EXITED)
2138 || (return_code != 0))
2140 ret = GNUNET_SYSERR;
2144 GNUNET_OS_process_close (procarr[pg_iter]);
2145 procarr[pg_iter] = NULL;
2147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2148 _("File %d copied\n"), pg_iter);
2154 if (ret == GNUNET_SYSERR)
2156 /* FIXME: why sleep here? -CG */
2162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2163 _("Finished copying all friend files!\n"));
2165 GNUNET_free (procarr);
2171 * Create the blacklist files based on the PeerConnection's
2172 * of each peer in the peer group, and copy the files
2173 * to the appropriate place.
2175 * @param pg the peer group we are dealing with
2176 * @param transports space delimited list of transports to blacklist
2179 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg,
2180 const char *transports)
2182 FILE *temp_file_handle;
2183 static struct BlacklistContext blacklist_ctx;
2184 unsigned int pg_iter;
2185 char *temp_service_path;
2186 struct GNUNET_OS_Process **procarr;
2189 enum GNUNET_OS_ProcessStatusType type;
2190 unsigned long return_code;
2197 char *temp_transports;
2199 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2200 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2202 mytemp = GNUNET_DISK_mktemp ("blacklist");
2203 GNUNET_assert (mytemp != NULL);
2204 temp_file_handle = fopen (mytemp, "wt");
2205 GNUNET_assert (temp_file_handle != NULL);
2206 temp_transports = GNUNET_strdup (transports);
2207 blacklist_ctx.temp_file_handle = temp_file_handle;
2208 transport_len = strlen (temp_transports) + 1;
2211 for (i = 0; i < transport_len; i++)
2213 if ((temp_transports[i] == ' ') && (pos == NULL))
2214 continue; /* At start of string (whitespace) */
2215 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2217 temp_transports[i] = '\0';
2218 blacklist_ctx.transport = pos;
2219 GNUNET_CONTAINER_multihashmap_iterate (pg->
2221 [pg_iter].blacklisted_peers,
2222 &blacklist_file_iterator,
2225 } /* At beginning of actual string */
2226 else if (pos == NULL)
2228 pos = &temp_transports[i];
2232 GNUNET_free (temp_transports);
2233 fclose (temp_file_handle);
2236 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2237 daemon->cfg, "PATHS",
2239 &temp_service_path))
2241 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2243 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2244 "SERVICEHOME", "PATHS");
2245 if (UNLINK (mytemp) != 0)
2246 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2248 GNUNET_free (mytemp);
2252 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2254 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2255 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2260 _("Copying file with command cp %s %s\n"), mytemp, arg);
2265 else /* Remote, scp the file to the correct place */
2267 if (NULL != pg->peers[pg_iter].daemon->username)
2268 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
2269 pg->peers[pg_iter].daemon->username,
2270 pg->peers[pg_iter].daemon->hostname,
2273 GNUNET_asprintf (&arg, "%s:%s/blacklist",
2274 pg->peers[pg_iter].daemon->hostname,
2277 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2282 _("Copying file with command scp %s %s\n"), mytemp,
2287 GNUNET_free (temp_service_path);
2288 GNUNET_free (mytemp);
2292 ret = GNUNET_SYSERR;
2293 while ((count < max_wait) && (ret != GNUNET_OK))
2296 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2300 _("Checking copy status of file %d\n"), pg_iter);
2302 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2304 if (GNUNET_OS_process_status
2305 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2307 ret = GNUNET_SYSERR;
2309 else if ((type != GNUNET_OS_PROCESS_EXITED)
2310 || (return_code != 0))
2312 ret = GNUNET_SYSERR;
2316 GNUNET_OS_process_close (procarr[pg_iter]);
2317 procarr[pg_iter] = NULL;
2319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2320 _("File %d copied\n"), pg_iter);
2326 if (ret == GNUNET_SYSERR)
2328 /* FIXME: why sleep here? -CG */
2334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2335 _("Finished copying all blacklist files!\n"));
2337 GNUNET_free (procarr);
2343 * Internal notification of a connection, kept so that we can ensure some connections
2344 * happen instead of flooding all testing daemons with requests to connect.
2347 internal_connect_notify (void *cls,
2348 const struct GNUNET_PeerIdentity *first,
2349 const struct GNUNET_PeerIdentity *second,
2351 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2352 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2353 struct GNUNET_TESTING_Daemon *first_daemon,
2354 struct GNUNET_TESTING_Daemon *second_daemon,
2357 struct ConnectTopologyContext *ct_ctx = cls;
2358 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
2359 outstanding_connects--;
2360 ct_ctx->remaining_connections--;
2361 if (ct_ctx->remaining_connections == 0)
2363 if (ct_ctx->notify_connections_done != NULL)
2364 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
2365 GNUNET_free (ct_ctx);
2368 if (pg->notify_connection != NULL)
2369 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
2370 first_cfg, second_cfg, first_daemon, second_daemon,
2376 * Either delay a connection (because there are too many outstanding)
2377 * or schedule it for right now.
2379 * @param cls a connection context
2380 * @param tc the task runtime context
2383 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2385 struct ConnectContext *connect_context = cls;
2387 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2390 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2392 #if VERBOSE_TESTING > 2
2393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2395 ("Delaying connect, we have too many outstanding connections!\n"));
2397 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2398 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
2399 &schedule_connect, connect_context);
2403 #if VERBOSE_TESTING > 2
2404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2405 _("Creating connection, outstanding_connections is %d\n"),
2406 outstanding_connects);
2408 outstanding_connects++;
2409 GNUNET_TESTING_daemons_connect (connect_context->first,
2410 connect_context->second,
2413 &internal_connect_notify,
2414 connect_context->ct_ctx);
2415 GNUNET_free (connect_context);
2421 * Iterator for actually scheduling connections to be created
2422 * between two peers.
2424 * @param cls closure, a GNUNET_TESTING_Daemon
2425 * @param key the key the second Daemon was stored under
2426 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2428 * @return GNUNET_YES to continue iteration
2431 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2433 struct ConnectTopologyContext *ct_ctx = cls;
2434 struct PeerData *first = ct_ctx->first;
2435 struct GNUNET_TESTING_Daemon *second = value;
2436 struct ConnectContext *connect_context;
2438 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
2439 connect_context->first = first->daemon;
2440 connect_context->second = second;
2441 connect_context->ct_ctx = ct_ctx;
2442 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
2449 * Iterator for copying all entries in the allowed hashmap to the
2452 * @param cls closure, a GNUNET_TESTING_Daemon
2453 * @param key the key the second Daemon was stored under
2454 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2456 * @return GNUNET_YES to continue iteration
2459 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2461 struct PeerData *first = cls;
2463 GNUNET_assert (GNUNET_OK ==
2464 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
2466 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2472 * Make the peers to connect the same as those that are allowed to be
2475 * @param pg the peer group
2478 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2480 unsigned int pg_iter;
2485 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2488 GNUNET_CONTAINER_multihashmap_iterate (pg->
2489 peers[pg_iter].allowed_peers,
2490 ©_topology_iterator,
2491 &pg->peers[pg_iter]);
2492 if (GNUNET_SYSERR == ret)
2493 return GNUNET_SYSERR;
2495 total = total + ret;
2503 * Connect the topology as specified by the PeerConnection's
2504 * of each peer in the peer group
2506 * @param pg the peer group we are dealing with
2507 * @return the number of connections that will be attempted
2510 connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2511 GNUNET_TESTING_NotifyCompletion notify_callback,
2514 unsigned int pg_iter;
2517 struct ConnectTopologyContext *ct_ctx;
2519 struct PeerConnection *connection_iter;
2520 struct ConnectContext *connect_context;
2524 ct_ctx = GNUNET_malloc (sizeof (struct ConnectTopologyContext));
2525 ct_ctx->notify_connections_done = notify_callback;
2526 ct_ctx->notify_cls = notify_cls;
2529 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2532 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
2537 GNUNET_free (ct_ctx);
2540 ct_ctx->remaining_connections = total;
2543 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2545 ct_ctx->first = &pg->peers[pg_iter];
2547 GNUNET_CONTAINER_multihashmap_iterate (pg->
2548 peers[pg_iter].connect_peers,
2549 &connect_iterator, ct_ctx);
2550 GNUNET_assert (GNUNET_SYSERR != ret && ret >= 0);
2551 total = total + ret;
2554 connection_iter = FIXME;
2555 while (connection_iter != NULL)
2557 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
2558 connect_context->pg = pg;
2559 connect_context->first = FIXME;
2560 connect_context->second = connection_iter->daemon;
2561 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
2562 connection_iter = connection_iter->next;
2571 * Takes a peer group and creates a topology based on the
2572 * one specified. Creates a topology means generates friend
2573 * files for the peers so they can only connect to those allowed
2574 * by the topology. This will only have an effect once peers
2575 * are started if the FRIENDS_ONLY option is set in the base
2576 * config. Also takes an optional restrict topology which
2577 * disallows connections based on particular transports
2578 * UNLESS they are specified in the restricted topology.
2580 * @param pg the peer group struct representing the running peers
2581 * @param topology which topology to connect the peers in
2582 * @param restrict_topology disallow restrict_transports transport
2583 * connections to peers NOT in this topology
2584 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2585 * @param restrict_transports space delimited list of transports to blacklist
2586 * to create restricted topology
2588 * @return the maximum number of connections were all allowed peers
2589 * connected to each other
2592 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2593 enum GNUNET_TESTING_Topology topology,
2594 enum GNUNET_TESTING_Topology
2596 const char *restrict_transports)
2599 unsigned int num_connections;
2600 int unblacklisted_connections;
2604 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
2608 num_connections = create_clique (pg, &add_allowed_connections);
2610 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2613 _("Creating small world (ring) topology\n"));
2616 create_small_world_ring (pg, &add_allowed_connections);
2618 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2621 _("Creating small world (2d-torus) topology\n"));
2623 num_connections = create_small_world (pg, &add_allowed_connections);
2625 case GNUNET_TESTING_TOPOLOGY_RING:
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
2629 num_connections = create_ring (pg, &add_allowed_connections);
2631 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
2635 num_connections = create_2d_torus (pg, &add_allowed_connections);
2637 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2640 _("Creating Erdos-Renyi topology\n"));
2642 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2644 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
2648 num_connections = create_nated_internet (pg, &add_allowed_connections);
2650 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2653 _("Creating Scale Free topology\n"));
2655 num_connections = create_scale_free (pg, &add_allowed_connections);
2657 case GNUNET_TESTING_TOPOLOGY_LINE:
2659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2660 _("Creating straight line topology\n"));
2662 num_connections = create_line (pg, &add_allowed_connections);
2664 case GNUNET_TESTING_TOPOLOGY_NONE:
2666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2668 ("Creating no allowed topology (all peers can connect at core level)\n"));
2670 num_connections = 0;
2673 num_connections = 0;
2678 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2680 ret = create_and_copy_friend_files (pg);
2681 if (ret != GNUNET_OK)
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2685 _("Failed during friend file copying!\n"));
2687 return GNUNET_SYSERR;
2692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2693 _("Friend files created/copied successfully!\n"));
2698 /* Use the create clique method to initially set all connections as blacklisted. */
2699 if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2700 create_clique (pg, &blacklist_connections);
2702 unblacklisted_connections = 0;
2703 /* Un-blacklist connections as per the topology specified */
2704 switch (restrict_topology)
2706 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2709 _("Blacklisting all but clique topology\n"));
2711 unblacklisted_connections =
2712 create_clique (pg, &unblacklist_connections);
2714 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2717 _("Blacklisting all but small world (ring) topology\n"));
2719 unblacklisted_connections =
2720 create_small_world_ring (pg, &unblacklist_connections);
2722 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2726 ("Blacklisting all but small world (2d-torus) topology\n"));
2728 unblacklisted_connections =
2729 create_small_world (pg, &unblacklist_connections);
2731 case GNUNET_TESTING_TOPOLOGY_RING:
2733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2734 _("Blacklisting all but ring topology\n"));
2736 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2738 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2741 _("Blacklisting all but 2d torus topology\n"));
2743 unblacklisted_connections =
2744 create_2d_torus (pg, &unblacklist_connections);
2746 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2749 _("Blacklisting all but Erdos-Renyi topology\n"));
2751 unblacklisted_connections =
2752 create_erdos_renyi (pg, &unblacklist_connections);
2754 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2757 _("Blacklisting all but InterNAT topology\n"));
2759 unblacklisted_connections =
2760 create_nated_internet (pg, &unblacklist_connections);
2762 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2765 _("Blacklisting all but Scale Free topology\n"));
2767 unblacklisted_connections =
2768 create_scale_free (pg, &unblacklist_connections);
2770 case GNUNET_TESTING_TOPOLOGY_LINE:
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2773 _("Blacklisting all but straight line topology\n"));
2775 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2777 case GNUNET_TESTING_TOPOLOGY_NONE:
2779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2781 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
2787 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2789 ret = create_and_copy_blacklist_files (pg, restrict_transports);
2790 if (ret != GNUNET_OK)
2793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2794 _("Failed during blacklist file copying!\n"));
2801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2802 _("Blacklist files created/copied successfully!\n"));
2806 return num_connections;
2809 struct RandomContext
2814 struct GNUNET_TESTING_PeerGroup *pg;
2817 * uid of the first peer
2822 * Peer data for first peer.
2824 struct PeerData *first;
2827 * Random percentage to use
2832 struct MinimumContext
2837 struct GNUNET_TESTING_PeerGroup *pg;
2840 * uid of the first peer
2845 * Peer data for first peer.
2847 struct PeerData *first;
2850 * Number of conns per peer
2852 unsigned int num_to_add;
2855 * Permuted array of all possible connections. Only add the Nth
2856 * peer if it's in the Nth position.
2858 unsigned int *pg_array;
2861 * What number is the current element we are iterating over?
2863 unsigned int current;
2871 struct GNUNET_TESTING_PeerGroup *pg;
2874 * uid of the first peer
2879 * uid of the second peer
2881 uint32_t second_uid;
2884 * Peer data for first peer.
2886 struct PeerData *first;
2889 * Which peer has been chosen as the one to add?
2891 unsigned int chosen;
2894 * What number is the current element we are iterating over?
2896 unsigned int current;
2900 * Iterator for choosing random peers to connect.
2902 * @param cls closure, a RandomContext
2903 * @param key the key the second Daemon was stored under
2904 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2906 * @return GNUNET_YES to continue iteration
2909 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2911 struct RandomContext *random_ctx = cls;
2912 double random_number;
2913 uint32_t second_pos;
2914 GNUNET_HashCode first_hash;
2917 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2918 UINT64_MAX)) / ((double) UINT64_MAX);
2919 if (random_number < random_ctx->percentage)
2921 GNUNET_assert (GNUNET_OK ==
2922 GNUNET_CONTAINER_multihashmap_put (random_ctx->
2923 first->connect_peers_working_set,
2925 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2927 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2928 uid_from_hash (key, &second_pos);
2929 hash_from_uid (random_ctx->first_uid, &first_hash);
2930 GNUNET_assert (random_ctx->pg->total > second_pos);
2931 GNUNET_assert (GNUNET_YES ==
2932 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
2934 [second_pos].connect_peers,
2943 * Iterator for adding at least X peers to a peers connection set.
2945 * @param cls closure, MinimumContext
2946 * @param key the key the second Daemon was stored under
2947 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2949 * @return GNUNET_YES to continue iteration
2952 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2954 struct MinimumContext *min_ctx = cls;
2955 uint32_t second_pos;
2956 GNUNET_HashCode first_hash;
2959 if (GNUNET_CONTAINER_multihashmap_size
2960 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2962 for (i = 0; i < min_ctx->num_to_add; i++)
2964 if (min_ctx->pg_array[i] == min_ctx->current)
2966 GNUNET_assert (GNUNET_OK ==
2967 GNUNET_CONTAINER_multihashmap_put
2968 (min_ctx->first->connect_peers_working_set, key,
2970 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2971 uid_from_hash (key, &second_pos);
2972 hash_from_uid (min_ctx->first_uid, &first_hash);
2973 GNUNET_assert (min_ctx->pg->total > second_pos);
2974 GNUNET_assert (GNUNET_OK ==
2975 GNUNET_CONTAINER_multihashmap_put (min_ctx->
2977 [second_pos].connect_peers_working_set,
2981 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2982 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2983 GNUNET_assert (GNUNET_YES ==
2984 GNUNET_CONTAINER_multihashmap_remove
2985 (min_ctx->pg->peers[second_pos].connect_peers,
2986 &first_hash, min_ctx->first->daemon));
2993 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2999 * Iterator for adding peers to a connection set based on a depth first search.
3001 * @param cls closure, MinimumContext
3002 * @param key the key the second daemon was stored under
3003 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3005 * @return GNUNET_YES to continue iteration
3008 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3010 struct DFSContext *dfs_ctx = cls;
3011 GNUNET_HashCode first_hash;
3013 if (dfs_ctx->current == dfs_ctx->chosen)
3015 GNUNET_assert (GNUNET_OK ==
3016 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3017 first->connect_peers_working_set,
3019 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3020 uid_from_hash (key, &dfs_ctx->second_uid);
3021 hash_from_uid (dfs_ctx->first_uid, &first_hash);
3022 GNUNET_assert (GNUNET_OK ==
3023 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3025 [dfs_ctx->second_uid].connect_peers_working_set,
3029 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3030 GNUNET_assert (GNUNET_YES ==
3031 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
3033 [dfs_ctx->second_uid].connect_peers,
3037 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
3038 return GNUNET_NO; /* We have found our peer, don't iterate more */
3047 * From the set of connections possible, choose percentage percent of connections
3048 * to actually connect.
3050 * @param pg the peergroup we are dealing with
3051 * @param percentage what percent of total connections to make
3054 choose_random_connections (struct GNUNET_TESTING_PeerGroup *pg,
3057 struct RandomContext random_ctx;
3060 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3062 random_ctx.first_uid = pg_iter;
3063 random_ctx.first = &pg->peers[pg_iter];
3064 random_ctx.percentage = percentage;
3066 pg->peers[pg_iter].connect_peers_working_set =
3067 GNUNET_CONTAINER_multihashmap_create (pg->total);
3068 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3069 &random_connect_iterator,
3071 /* Now remove the old connections */
3072 GNUNET_CONTAINER_multihashmap_destroy (pg->
3073 peers[pg_iter].connect_peers);
3074 /* And replace with the random set */
3075 pg->peers[pg_iter].connect_peers =
3076 pg->peers[pg_iter].connect_peers_working_set;
3081 * From the set of connections possible, choose at least num connections per
3084 * @param pg the peergroup we are dealing with
3085 * @param num how many connections at least should each peer have (if possible)?
3088 choose_minimum (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3090 struct MinimumContext minimum_ctx;
3093 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3095 pg->peers[pg_iter].connect_peers_working_set =
3096 GNUNET_CONTAINER_multihashmap_create (num);
3099 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3101 minimum_ctx.first_uid = pg_iter;
3102 minimum_ctx.pg_array =
3103 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
3104 GNUNET_CONTAINER_multihashmap_size
3105 (pg->peers[pg_iter].connect_peers));
3106 minimum_ctx.first = &pg->peers[pg_iter];
3107 minimum_ctx.pg = pg;
3108 minimum_ctx.num_to_add = num;
3109 minimum_ctx.current = 0;
3110 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3111 &minimum_connect_iterator,
3115 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3117 /* Remove the "old" connections */
3118 GNUNET_CONTAINER_multihashmap_destroy (pg->
3119 peers[pg_iter].connect_peers);
3120 /* And replace with the working set */
3121 pg->peers[pg_iter].connect_peers =
3122 pg->peers[pg_iter].connect_peers_working_set;
3129 count_workingset_connections (struct GNUNET_TESTING_PeerGroup *pg)
3132 unsigned int pg_iter;
3136 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3139 GNUNET_CONTAINER_multihashmap_size (pg->
3141 [pg_iter].connect_peers_working_set);
3149 count_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg)
3152 unsigned int pg_iter;
3156 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3159 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3166 struct FindClosestContext
3169 * The currently known closest peer.
3171 struct GNUNET_TESTING_Daemon *closest;
3174 * The info for the peer we are adding connections for.
3176 struct PeerData *curr_peer;
3179 * The distance (bits) between the current
3180 * peer and the currently known closest.
3182 unsigned int closest_dist;
3185 * The offset of the closest known peer in
3188 unsigned int closest_num;
3192 * Iterator over hash map entries of the allowed
3193 * peer connections. Find the closest, not already
3194 * connected peer and return it.
3196 * @param cls closure (struct FindClosestContext)
3197 * @param key current key code (hash of offset in pg)
3198 * @param value value in the hash map - a GNUNET_TESTING_Daemon
3199 * @return GNUNET_YES if we should continue to
3204 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
3206 struct FindClosestContext *closest_ctx = cls;
3207 struct GNUNET_TESTING_Daemon *daemon = value;
3209 if (((closest_ctx->closest == NULL) ||
3210 (GNUNET_CRYPTO_hash_matching_bits
3211 (&daemon->id.hashPubKey,
3212 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
3213 closest_ctx->closest_dist))
3215 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
3216 curr_peer->connect_peers,
3219 closest_ctx->closest_dist =
3220 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
3221 &closest_ctx->curr_peer->daemon->
3223 closest_ctx->closest = daemon;
3224 uid_from_hash (key, &closest_ctx->closest_num);
3230 * From the set of connections possible, choose at num connections per
3231 * peer based on depth which are closest out of those allowed. Guaranteed
3232 * to add num peers to connect to, provided there are that many peers
3233 * in the underlay topology to connect to.
3235 * @param pg the peergroup we are dealing with
3236 * @param num how many connections at least should each peer have (if possible)?
3237 * @param proc processor to actually add the connections
3240 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
3241 GNUNET_TESTING_ConnectionProcessor proc)
3243 struct FindClosestContext closest_ctx;
3247 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
3249 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3251 closest_ctx.curr_peer = &pg->peers[pg_iter];
3252 closest_ctx.closest = NULL;
3253 closest_ctx.closest_dist = 0;
3254 closest_ctx.closest_num = 0;
3255 GNUNET_CONTAINER_multihashmap_iterate (pg->
3256 peers[pg_iter].allowed_peers,
3257 &find_closest_peers,
3259 if (closest_ctx.closest != NULL)
3261 GNUNET_assert (closest_ctx.closest_num < pg->total);
3262 proc (pg, pg_iter, closest_ctx.closest_num);
3269 * From the set of connections possible, choose at least num connections per
3270 * peer based on depth first traversal of peer connections. If DFS leaves
3271 * peers unconnected, ensure those peers get connections.
3273 * @param pg the peergroup we are dealing with
3274 * @param num how many connections at least should each peer have (if possible)?
3277 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3279 struct DFSContext dfs_ctx;
3282 uint32_t starting_peer;
3283 uint32_t least_connections;
3284 GNUNET_HashCode second_hash;
3286 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3288 pg->peers[pg_iter].connect_peers_working_set =
3289 GNUNET_CONTAINER_multihashmap_create (num);
3294 while ((count_workingset_connections (pg) < num * pg->total)
3295 && (count_allowed_connections (pg) > 0))
3297 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3299 least_connections = -1; /* Set to very high number */
3300 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3302 if (GNUNET_CONTAINER_multihashmap_size
3303 (pg->peers[pg_iter].connect_peers_working_set) <
3306 starting_peer = pg_iter;
3308 GNUNET_CONTAINER_multihashmap_size (pg->
3310 [pg_iter].connect_peers_working_set);
3315 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
3321 /* Choose a random peer from the chosen peers set of connections to add */
3323 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3324 GNUNET_CONTAINER_multihashmap_size
3325 (pg->peers[starting_peer].connect_peers));
3326 dfs_ctx.first_uid = starting_peer;
3327 dfs_ctx.first = &pg->peers[starting_peer];
3329 dfs_ctx.current = 0;
3331 GNUNET_CONTAINER_multihashmap_iterate (pg->
3333 [starting_peer].connect_peers,
3334 &dfs_connect_iterator, &dfs_ctx);
3335 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
3336 hash_from_uid (dfs_ctx.second_uid, &second_hash);
3337 GNUNET_assert (GNUNET_YES ==
3338 GNUNET_CONTAINER_multihashmap_remove (pg->peers
3339 [starting_peer].connect_peers,
3343 [dfs_ctx.second_uid].daemon));
3344 starting_peer = dfs_ctx.second_uid;
3347 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3349 /* Remove the "old" connections */
3350 GNUNET_CONTAINER_multihashmap_destroy (pg->
3351 peers[pg_iter].connect_peers);
3352 /* And replace with the working set */
3353 pg->peers[pg_iter].connect_peers =
3354 pg->peers[pg_iter].connect_peers_working_set;
3359 * Internal callback for topology information for a particular peer.
3362 internal_topology_callback (void *cls,
3363 const struct GNUNET_PeerIdentity *peer,
3364 const struct GNUNET_TRANSPORT_ATS_Information
3367 struct CoreContext *core_ctx = cls;
3368 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
3370 if (peer == NULL) /* Either finished, or something went wrong */
3372 iter_ctx->completed++;
3373 iter_ctx->connected--;
3374 /* One core context allocated per iteration, must free! */
3375 GNUNET_free (core_ctx);
3379 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id,
3383 if (iter_ctx->completed == iter_ctx->total)
3385 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
3386 /* Once all are done, free the iteration context */
3387 GNUNET_free (iter_ctx);
3393 * Check running topology iteration tasks, if below max start a new one, otherwise
3394 * schedule for some time in the future.
3397 schedule_get_topology (void *cls,
3398 const struct GNUNET_SCHEDULER_TaskContext *tc)
3400 struct CoreContext *core_context = cls;
3401 struct TopologyIterateContext *topology_context =
3402 (struct TopologyIterateContext *) core_context->iter_context;
3403 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3406 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3408 #if VERBOSE_TESTING > 2
3409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3411 ("Delaying connect, we have too many outstanding connections!\n"));
3413 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3414 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3415 &schedule_get_topology, core_context);
3419 #if VERBOSE_TESTING > 2
3420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3421 _("Creating connection, outstanding_connections is %d\n"),
3422 outstanding_connects);
3424 topology_context->connected++;
3427 GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
3428 &internal_topology_callback,
3431 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
3432 internal_topology_callback (core_context, NULL, NULL);
3438 * Iterate over all (running) peers in the peer group, retrieve
3439 * all connections that each currently has.
3442 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
3443 GNUNET_TESTING_NotifyTopology cb, void *cls)
3445 struct TopologyIterateContext *topology_context;
3446 struct CoreContext *core_ctx;
3448 unsigned int total_count;
3450 /* Allocate a single topology iteration context */
3451 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
3452 topology_context->topology_cb = cb;
3453 topology_context->cls = cls;
3455 for (i = 0; i < pg->total; i++)
3457 if (pg->peers[i].daemon->running == GNUNET_YES)
3459 /* Allocate one core context per core we need to connect to */
3460 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
3461 core_ctx->daemon = pg->peers[i].daemon;
3462 /* Set back pointer to topology iteration context */
3463 core_ctx->iter_context = topology_context;
3464 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
3468 if (total_count == 0)
3470 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
3471 GNUNET_free (topology_context);
3474 topology_context->total = total_count;
3479 * Callback function to process statistic values.
3480 * This handler is here only really to insert a peer
3481 * identity (or daemon) so the statistics can be uniquely
3482 * tied to a single running peer.
3484 * @param cls closure
3485 * @param subsystem name of subsystem that created the statistic
3486 * @param name the name of the datum
3487 * @param value the current value
3488 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
3489 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
3492 internal_stats_callback (void *cls,
3493 const char *subsystem,
3494 const char *name, uint64_t value, int is_persistent)
3496 struct StatsCoreContext *core_context = cls;
3497 struct StatsIterateContext *stats_context =
3498 (struct StatsIterateContext *) core_context->iter_context;
3500 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
3501 subsystem, name, value, is_persistent);
3505 * Internal continuation call for statistics iteration.
3507 * @param cls closure, the CoreContext for this iteration
3508 * @param success whether or not the statistics iterations
3509 * was canceled or not (we don't care)
3512 internal_stats_cont (void *cls, int success)
3514 struct StatsCoreContext *core_context = cls;
3515 struct StatsIterateContext *stats_context =
3516 (struct StatsIterateContext *) core_context->iter_context;
3518 stats_context->connected--;
3519 stats_context->completed++;
3521 if (stats_context->completed == stats_context->total)
3523 stats_context->cont (stats_context->cls, GNUNET_YES);
3524 GNUNET_free (stats_context);
3527 if (core_context->stats_handle != NULL)
3528 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
3530 GNUNET_free (core_context);
3534 * Check running topology iteration tasks, if below max start a new one, otherwise
3535 * schedule for some time in the future.
3538 schedule_get_statistics (void *cls,
3539 const struct GNUNET_SCHEDULER_TaskContext *tc)
3541 struct StatsCoreContext *core_context = cls;
3542 struct StatsIterateContext *stats_context =
3543 (struct StatsIterateContext *) core_context->iter_context;
3545 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3548 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3550 #if VERBOSE_TESTING > 2
3551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3553 ("Delaying connect, we have too many outstanding connections!\n"));
3555 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3556 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3557 &schedule_get_statistics, core_context);
3561 #if VERBOSE_TESTING > 2
3562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3563 _("Creating connection, outstanding_connections is %d\n"),
3564 outstanding_connects);
3567 stats_context->connected++;
3568 core_context->stats_handle =
3569 GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
3570 if (core_context->stats_handle == NULL)
3572 internal_stats_cont (core_context, GNUNET_NO);
3576 core_context->stats_get_handle =
3577 GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
3578 GNUNET_TIME_relative_get_forever (),
3579 &internal_stats_cont, &internal_stats_callback,
3581 if (core_context->stats_get_handle == NULL)
3582 internal_stats_cont (core_context, GNUNET_NO);
3587 struct DuplicateStats
3590 * Next item in the list
3592 struct DuplicateStats *next;
3595 * Nasty string, concatenation of relevant information.
3597 char *unique_string;
3601 * Check whether the combination of port/host/unix domain socket
3602 * already exists in the list of peers being checked for statistics.
3604 * @param pg the peergroup in question
3605 * @param specific_peer the peer we're concerned with
3606 * @param stats_list the list to return to the caller
3608 * @return GNUNET_YES if the statistics instance has been seen already,
3609 * GNUNET_NO if not (and we may have added it to the list)
3612 stats_check_existing (struct GNUNET_TESTING_PeerGroup *pg,
3613 struct PeerData *specific_peer,
3614 struct DuplicateStats **stats_list)
3616 struct DuplicateStats *pos;
3617 char *unix_domain_socket;
3618 unsigned long long port;
3621 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
3622 "single_statistics_per_host"))
3623 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
3627 GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg, "statistics",
3628 "unixpath", &unix_domain_socket))
3632 GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg, "statistics",
3636 if (specific_peer->daemon->hostname != NULL)
3637 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
3638 unix_domain_socket, port);
3640 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
3644 if (0 == strcmp (to_match, pos->unique_string))
3646 GNUNET_free (unix_domain_socket);
3647 GNUNET_free (to_match);
3652 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
3653 pos->unique_string = to_match;
3654 pos->next = *stats_list;
3656 GNUNET_free (unix_domain_socket);
3661 * Iterate over all (running) peers in the peer group, retrieve
3662 * all statistics from each.
3665 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
3666 GNUNET_STATISTICS_Callback cont,
3667 GNUNET_TESTING_STATISTICS_Iterator proc,
3670 struct StatsIterateContext *stats_context;
3671 struct StatsCoreContext *core_ctx;
3673 unsigned int total_count;
3674 struct DuplicateStats *stats_list;
3675 struct DuplicateStats *pos;
3678 /* Allocate a single stats iteration context */
3679 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
3680 stats_context->cont = cont;
3681 stats_context->proc = proc;
3682 stats_context->cls = cls;
3685 for (i = 0; i < pg->total; i++)
3687 if ((pg->peers[i].daemon->running == GNUNET_YES)
3689 stats_check_existing (pg, &pg->peers[i], &stats_list)))
3691 /* Allocate one core context per core we need to connect to */
3692 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
3693 core_ctx->daemon = pg->peers[i].daemon;
3694 /* Set back pointer to topology iteration context */
3695 core_ctx->iter_context = stats_context;
3696 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
3701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3702 "Retrieving stats from %u total instances.\n", total_count);
3703 stats_context->total = total_count;
3704 if (stats_list != NULL)
3709 GNUNET_free (pos->unique_string);
3710 stats_list = pos->next;
3712 pos = stats_list->next;
3719 * There are many ways to connect peers that are supported by this function.
3720 * To connect peers in the same topology that was created via the
3721 * GNUNET_TESTING_create_topology, the topology variable must be set to
3722 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
3723 * a new instance of that topology will be generated and attempted to be
3724 * connected. This could result in some connections being impossible,
3725 * because some topologies are non-deterministic.
3727 * @param pg the peer group struct representing the running peers
3728 * @param topology which topology to connect the peers in
3729 * @param options options for connecting the topology
3730 * @param option_modifier modifier for options that take a parameter
3731 * @param notify_callback notification to be called once all connections completed
3732 * @param notify_cls closure for notification callback
3734 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
3737 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3738 enum GNUNET_TESTING_Topology topology,
3739 enum GNUNET_TESTING_TopologyOption options,
3740 double option_modifier,
3741 GNUNET_TESTING_NotifyCompletion
3742 notify_callback, void *notify_cls)
3746 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3747 #if VERBOSE_TOPOLOGY
3748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3749 _("Creating clique CONNECT topology\n"));
3751 create_clique (pg, &add_actual_connections);
3753 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3754 #if VERBOSE_TOPOLOGY
3755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3756 _("Creating small world (ring) CONNECT topology\n"));
3758 create_small_world_ring (pg, &add_actual_connections);
3760 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3761 #if VERBOSE_TOPOLOGY
3762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3763 _("Creating small world (2d-torus) CONNECT topology\n"));
3765 create_small_world (pg, &add_actual_connections);
3767 case GNUNET_TESTING_TOPOLOGY_RING:
3768 #if VERBOSE_TOPOLOGY
3769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3770 _("Creating ring CONNECT topology\n"));
3772 create_ring (pg, &add_actual_connections);
3774 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3775 #if VERBOSE_TOPOLOGY
3776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3777 _("Creating 2d torus CONNECT topology\n"));
3779 create_2d_torus (pg, &add_actual_connections);
3781 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3782 #if VERBOSE_TOPOLOGY
3783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3784 _("Creating Erdos-Renyi CONNECT topology\n"));
3786 create_erdos_renyi (pg, &add_actual_connections);
3788 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3789 #if VERBOSE_TOPOLOGY
3790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3791 _("Creating InterNAT CONNECT topology\n"));
3793 create_nated_internet (pg, &add_actual_connections);
3795 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3796 #if VERBOSE_TOPOLOGY
3797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3798 _("Creating Scale Free CONNECT topology\n"));
3800 create_scale_free (pg, &add_actual_connections);
3802 case GNUNET_TESTING_TOPOLOGY_LINE:
3803 #if VERBOSE_TOPOLOGY
3804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3805 _("Creating straight line CONNECT topology\n"));
3807 create_line (pg, &add_actual_connections);
3809 case GNUNET_TESTING_TOPOLOGY_NONE:
3810 #if VERBOSE_TOPOLOGY
3811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3812 _("Creating no CONNECT topology\n"));
3814 copy_allowed_topology (pg);
3817 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3819 ("Unknown topology specification, can't connect peers!\n"));
3820 return GNUNET_SYSERR;
3825 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
3826 #if VERBOSE_TOPOLOGY
3827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3829 ("Connecting random subset (%'.2f percent) of possible peers\n"),
3830 100 * option_modifier);
3832 choose_random_connections (pg, option_modifier);
3834 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
3835 #if VERBOSE_TOPOLOGY
3836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3837 _("Connecting a minimum of %u peers each (if possible)\n"),
3838 (unsigned int) option_modifier);
3840 choose_minimum (pg, (unsigned int) option_modifier);
3842 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
3843 #if VERBOSE_TOPOLOGY
3844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3846 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
3847 (unsigned int) option_modifier);
3849 perform_dfs (pg, (int) option_modifier);
3851 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
3852 #if VERBOSE_TOPOLOGY
3853 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3855 ("Finding additional %u closest peers each (if possible)\n"),
3856 (unsigned int) option_modifier);
3858 add_closest (pg, (unsigned int) option_modifier,
3859 &add_actual_connections);
3861 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
3863 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
3869 return connect_topology (pg, notify_callback, notify_cls);
3873 * Callback that is called whenever a hostkey is generated
3874 * for a peer. Call the real callback and decrement the
3875 * starting counter for the peergroup.
3877 * @param cls closure
3878 * @param id identifier for the daemon, NULL on error
3879 * @param d handle for the daemon
3880 * @param emsg error message (NULL on success)
3883 internal_hostkey_callback (void *cls,
3884 const struct GNUNET_PeerIdentity *id,
3885 struct GNUNET_TESTING_Daemon *d, const char *emsg)
3887 struct InternalStartContext *internal_context = cls;
3888 internal_context->peer->pg->starting--;
3889 internal_context->peer->pg->started++;
3890 if (internal_context->hostkey_callback != NULL)
3891 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
3893 else if (internal_context->peer->pg->started ==
3894 internal_context->peer->pg->total)
3896 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
3897 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
3902 * Callback that is called whenever a peer has finished starting.
3903 * Call the real callback and decrement the starting counter
3904 * for the peergroup.
3906 * @param cls closure
3907 * @param id identifier for the daemon, NULL on error
3908 * @param d handle for the daemon
3909 * @param emsg error message (NULL on success)
3912 internal_startup_callback (void *cls,
3913 const struct GNUNET_PeerIdentity *id,
3914 const struct GNUNET_CONFIGURATION_Handle *cfg,
3915 struct GNUNET_TESTING_Daemon *d, const char *emsg)
3917 struct InternalStartContext *internal_context = cls;
3918 internal_context->peer->pg->starting--;
3919 if (internal_context->start_cb != NULL)
3920 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
3925 internal_continue_startup (void *cls,
3926 const struct GNUNET_SCHEDULER_TaskContext *tc)
3928 struct InternalStartContext *internal_context = cls;
3930 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3935 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
3937 internal_context->peer->pg->starting++;
3938 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
3942 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3943 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3944 &internal_continue_startup,
3951 * Callback for informing us about a successful
3952 * or unsuccessful churn start call.
3954 * @param cls a ChurnContext
3955 * @param id the peer identity of the started peer
3956 * @param cfg the handle to the configuration of the peer
3957 * @param d handle to the daemon for the peer
3958 * @param emsg NULL on success, non-NULL on failure
3962 churn_start_callback (void *cls,
3963 const struct GNUNET_PeerIdentity *id,
3964 const struct GNUNET_CONFIGURATION_Handle *cfg,
3965 struct GNUNET_TESTING_Daemon *d, const char *emsg)
3967 struct ChurnRestartContext *startup_ctx = cls;
3968 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
3970 unsigned int total_left;
3971 char *error_message;
3973 error_message = NULL;
3976 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3977 "Churn stop callback failed with error `%s'\n", emsg);
3978 churn_ctx->num_failed_start++;
3982 churn_ctx->num_to_start--;
3986 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3987 "Started peer, %d left.\n", churn_ctx->num_to_start);
3991 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
3992 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3994 if (total_left == 0)
3996 if ((churn_ctx->num_failed_stop > 0)
3997 || (churn_ctx->num_failed_start > 0))
3998 GNUNET_asprintf (&error_message,
3999 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4000 churn_ctx->num_failed_start,
4001 churn_ctx->num_failed_stop);
4002 churn_ctx->cb (churn_ctx->cb_cls, error_message);
4003 GNUNET_free_non_null (error_message);
4004 GNUNET_free (churn_ctx);
4005 GNUNET_free (startup_ctx);
4011 schedule_churn_restart (void *cls,
4012 const struct GNUNET_SCHEDULER_TaskContext *tc)
4014 struct PeerRestartContext *peer_restart_ctx = cls;
4015 struct ChurnRestartContext *startup_ctx =
4016 peer_restart_ctx->churn_restart_ctx;
4018 if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
4019 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4020 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4021 &schedule_churn_restart, peer_restart_ctx);
4024 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
4025 startup_ctx->timeout,
4026 &churn_start_callback,
4028 GNUNET_free (peer_restart_ctx);
4033 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4035 struct InternalStartContext *internal_context = cls;
4037 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4042 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
4044 internal_context->peer->pg->starting++;
4045 internal_context->peer->daemon =
4046 GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
4047 internal_context->timeout,
4048 internal_context->hostname,
4049 internal_context->username,
4050 internal_context->sshport,
4051 &internal_hostkey_callback,
4053 &internal_startup_callback,
4058 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4059 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4060 &internal_start, internal_context);
4065 * Function which continues a peer group starting up
4066 * after successfully generating hostkeys for each peer.
4068 * @param pg the peer group to continue starting
4072 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg)
4077 for (i = 0; i < pg->total; i++)
4079 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
4080 &pg->peers[i].internal_context);
4081 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
4086 * Start count gnunet instances with the same set of transports and
4087 * applications. The port numbers (any option called "PORT") will be
4088 * adjusted to ensure that no two peers running on the same system
4089 * have the same port(s) in their respective configurations.
4091 * @param cfg configuration template to use
4092 * @param total number of daemons to start
4093 * @param timeout total time allowed for peers to start
4094 * @param hostkey_callback function to call on each peers hostkey generation
4095 * if NULL, peers will be started by this call, if non-null,
4096 * GNUNET_TESTING_daemons_continue_startup must be called after
4097 * successful hostkey generation
4098 * @param hostkey_cls closure for hostkey callback
4099 * @param cb function to call on each daemon that was started
4100 * @param cb_cls closure for cb
4101 * @param connect_callback function to call each time two hosts are connected
4102 * @param connect_callback_cls closure for connect_callback
4103 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
4105 * @return NULL on error, otherwise handle to control peer group
4107 struct GNUNET_TESTING_PeerGroup *
4108 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
4110 struct GNUNET_TIME_Relative timeout,
4111 GNUNET_TESTING_NotifyHostkeyCreated
4112 hostkey_callback, void *hostkey_cls,
4113 GNUNET_TESTING_NotifyDaemonRunning cb,
4115 GNUNET_TESTING_NotifyConnection
4116 connect_callback, void *connect_callback_cls,
4117 const struct GNUNET_TESTING_Host *hostnames)
4119 struct GNUNET_TESTING_PeerGroup *pg;
4120 const struct GNUNET_TESTING_Host *hostpos;
4126 const char *hostname;
4127 const char *username;
4128 char *baseservicehome;
4129 char *newservicehome;
4131 struct GNUNET_CONFIGURATION_Handle *pcfg;
4133 unsigned int hostcnt;
4146 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
4148 pg->notify_connection = connect_callback;
4149 pg->notify_connection_cls = connect_callback_cls;
4151 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
4152 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
4153 if (NULL != hostnames)
4156 hostpos = hostnames;
4157 while (hostpos != NULL)
4159 hostpos = hostpos->next;
4162 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4165 hostpos = hostnames;
4166 while (hostpos != NULL)
4168 pg->hosts[off].minport = LOW_PORT;
4169 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
4170 if (hostpos->username != NULL)
4171 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
4172 pg->hosts[off].sshport = hostpos->port;
4173 hostpos = hostpos->next;
4183 pg->num_hosts = off;
4187 /* skip leading spaces */
4188 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
4191 while ('\0' != *rpos)
4193 if (isspace ((unsigned char) *rpos))
4197 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4199 start = GNUNET_strdup (hostnames);
4201 while ('\0' != *pos)
4203 if (isspace ((unsigned char) *pos))
4206 if (strlen (start) > 0)
4208 pg->hosts[off].minport = LOW_PORT;
4209 pg->hosts[off++].hostname = start;
4215 if (strlen (start) > 0)
4217 pg->hosts[off].minport = LOW_PORT;
4218 pg->hosts[off++].hostname = start;
4222 GNUNET_free (start);
4223 GNUNET_free (pg->hosts);
4227 minport = 0; /* make gcc happy */
4235 for (off = 0; off < total; off++)
4239 hostname = pg->hosts[off % hostcnt].hostname;
4240 username = pg->hosts[off % hostcnt].username;
4241 sshport = pg->hosts[off % hostcnt].sshport;
4242 pcfg = make_config (cfg,
4243 &pg->hosts[off % hostcnt].minport,
4244 &upnum, hostname, &fdnum);
4251 pcfg = make_config (cfg, &minport, &upnum, hostname, &fdnum);
4256 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4258 ("Could not create configuration for peer number %u on `%s'!\n"),
4259 off, hostname == NULL ? "localhost" : hostname);
4264 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
4267 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
4268 GNUNET_free (baseservicehome);
4272 tmpdir = getenv ("TMPDIR");
4273 tmpdir = tmpdir ? tmpdir : "/tmp";
4274 GNUNET_asprintf (&newservicehome,
4276 tmpdir, "gnunet-testing-test-test", off);
4278 GNUNET_CONFIGURATION_set_value_string (pcfg,
4280 "SERVICEHOME", newservicehome);
4281 GNUNET_free (newservicehome);
4282 pg->peers[off].cfg = pcfg;
4283 pg->peers[off].allowed_peers =
4284 GNUNET_CONTAINER_multihashmap_create (total);
4285 pg->peers[off].connect_peers =
4286 GNUNET_CONTAINER_multihashmap_create (total);
4287 pg->peers[off].blacklisted_peers =
4288 GNUNET_CONTAINER_multihashmap_create (total);
4289 pg->peers[off].pg = pg;
4291 pg->peers[off].internal_context.peer = &pg->peers[off];
4292 pg->peers[off].internal_context.timeout = timeout;
4293 pg->peers[off].internal_context.hostname = hostname;
4294 pg->peers[off].internal_context.username = username;
4295 pg->peers[off].internal_context.sshport = sshport;
4296 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
4297 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
4298 pg->peers[off].internal_context.start_cb = cb;
4299 pg->peers[off].internal_context.start_cb_cls = cb_cls;
4301 GNUNET_SCHEDULER_add_now (&internal_start,
4302 &pg->peers[off].internal_context);
4309 * Get a daemon by number, so callers don't have to do nasty
4310 * offsetting operation.
4312 struct GNUNET_TESTING_Daemon *
4313 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
4314 unsigned int position)
4316 if (position < pg->total)
4317 return pg->peers[position].daemon;
4323 * Get a daemon by peer identity, so callers can
4324 * retrieve the daemon without knowing it's offset.
4326 * @param pg the peer group to retrieve the daemon from
4327 * @param peer_id the peer identity of the daemon to retrieve
4329 * @return the daemon on success, or NULL if no such peer identity is found
4331 struct GNUNET_TESTING_Daemon *
4332 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
4333 struct GNUNET_PeerIdentity *peer_id)
4337 for (i = 0; i < pg->total; i++)
4340 memcmp (&pg->peers[i].daemon->id, peer_id,
4341 sizeof (struct GNUNET_PeerIdentity)))
4342 return pg->peers[i].daemon;
4349 * Prototype of a function that will be called when a
4350 * particular operation was completed the testing library.
4352 * @param cls closure (a struct RestartContext)
4353 * @param id id of the peer that was restarted
4354 * @param cfg handle to the configuration of the peer
4355 * @param d handle to the daemon that was restarted
4356 * @param emsg NULL on success
4359 restart_callback (void *cls,
4360 const struct GNUNET_PeerIdentity *id,
4361 const struct GNUNET_CONFIGURATION_Handle *cfg,
4362 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4364 struct RestartContext *restart_context = cls;
4368 restart_context->peers_restarted++;
4372 restart_context->peers_restart_failed++;
4375 if (restart_context->peers_restarted == restart_context->peer_group->total)
4377 restart_context->callback (restart_context->callback_cls, NULL);
4378 GNUNET_free (restart_context);
4380 else if (restart_context->peers_restart_failed +
4381 restart_context->peers_restarted ==
4382 restart_context->peer_group->total)
4384 restart_context->callback (restart_context->callback_cls,
4385 "Failed to restart peers!");
4386 GNUNET_free (restart_context);
4392 * Callback for informing us about a successful
4393 * or unsuccessful churn stop call.
4395 * @param cls a ChurnContext
4396 * @param emsg NULL on success, non-NULL on failure
4400 churn_stop_callback (void *cls, const char *emsg)
4402 struct ShutdownContext *shutdown_ctx = cls;
4403 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
4404 unsigned int total_left;
4405 char *error_message;
4407 error_message = NULL;
4408 shutdown_ctx->outstanding--;
4412 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4413 "Churn stop callback failed with error `%s'\n", emsg);
4414 churn_ctx->num_failed_stop++;
4418 churn_ctx->num_to_stop--;
4422 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4423 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
4426 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
4427 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4429 if (total_left == 0)
4431 if ((churn_ctx->num_failed_stop > 0)
4432 || (churn_ctx->num_failed_start > 0))
4434 GNUNET_asprintf (&error_message,
4435 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4436 churn_ctx->num_failed_start,
4437 churn_ctx->num_failed_stop);
4439 churn_ctx->cb (churn_ctx->cb_cls, error_message);
4440 GNUNET_free_non_null (error_message);
4441 GNUNET_free (churn_ctx);
4442 GNUNET_free (shutdown_ctx);
4447 * Count the number of running peers.
4449 * @param pg handle for the peer group
4451 * @return the number of currently running peers in the peer group
4454 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
4457 unsigned int running = 0;
4458 for (i = 0; i < pg->total; i++)
4460 if (pg->peers[i].daemon->running == GNUNET_YES)
4462 GNUNET_assert (running != -1);
4470 * Task to rate limit the number of outstanding peer shutdown
4471 * requests. This is necessary for making sure we don't do
4472 * too many ssh connections at once, but is generally nicer
4473 * to any system as well (graduated task starts, as opposed
4474 * to calling gnunet-arm N times all at once).
4477 schedule_churn_shutdown_task (void *cls,
4478 const struct GNUNET_SCHEDULER_TaskContext *tc)
4480 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4481 struct ShutdownContext *shutdown_ctx;
4483 GNUNET_assert (peer_shutdown_ctx != NULL);
4484 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4485 GNUNET_assert (shutdown_ctx != NULL);
4487 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4488 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4489 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4490 &schedule_churn_shutdown_task,
4494 shutdown_ctx->outstanding++;
4495 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
4496 shutdown_ctx->timeout, shutdown_ctx->cb,
4497 shutdown_ctx, GNUNET_NO, GNUNET_YES);
4498 GNUNET_free (peer_shutdown_ctx);
4503 * Simulate churn by stopping some peers (and possibly
4504 * re-starting others if churn is called multiple times). This
4505 * function can only be used to create leave-join churn (peers "never"
4506 * leave for good). First "voff" random peers that are currently
4507 * online will be taken offline; then "von" random peers that are then
4508 * offline will be put back online. No notifications will be
4509 * generated for any of these operations except for the callback upon
4512 * @param pg handle for the peer group
4513 * @param voff number of peers that should go offline
4514 * @param von number of peers that should come back online;
4515 * must be zero on first call (since "testbed_start"
4516 * always starts all of the peers)
4517 * @param timeout how long to wait for operations to finish before
4519 * @param cb function to call at the end
4520 * @param cb_cls closure for cb
4523 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
4526 struct GNUNET_TIME_Relative timeout,
4527 GNUNET_TESTING_NotifyCompletion cb,
4530 struct ChurnContext *churn_ctx;
4531 struct ShutdownContext *shutdown_ctx;
4532 struct PeerShutdownContext *peer_shutdown_ctx;
4533 struct PeerRestartContext *peer_restart_ctx;
4534 struct ChurnRestartContext *churn_startup_ctx;
4536 unsigned int running;
4537 unsigned int stopped;
4538 unsigned int total_running;
4539 unsigned int total_stopped;
4541 unsigned int *running_arr;
4542 unsigned int *stopped_arr;
4543 unsigned int *running_permute;
4544 unsigned int *stopped_permute;
4549 if ((von == 0) && (voff == 0)) /* No peers at all? */
4555 for (i = 0; i < pg->total; i++)
4557 if (pg->peers[i].daemon->running == GNUNET_YES)
4559 GNUNET_assert (running != -1);
4564 GNUNET_assert (stopped != -1);
4571 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4572 "Trying to stop more peers than are currently running!\n");
4573 cb (cb_cls, "Trying to stop more peers than are currently running!");
4579 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4580 "Trying to start more peers than are currently stopped!\n");
4581 cb (cb_cls, "Trying to start more peers than are currently stopped!");
4585 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
4589 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
4593 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
4595 running_permute = NULL;
4596 stopped_permute = NULL;
4600 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, running);
4603 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, stopped);
4605 total_running = running;
4606 total_stopped = stopped;
4610 churn_ctx->num_to_start = von;
4611 churn_ctx->num_to_stop = voff;
4613 churn_ctx->cb_cls = cb_cls;
4615 for (i = 0; i < pg->total; i++)
4617 if (pg->peers[i].daemon->running == GNUNET_YES)
4619 GNUNET_assert ((running_arr != NULL) && (total_running > running));
4620 running_arr[running] = i;
4625 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
4626 stopped_arr[stopped] = i;
4631 GNUNET_assert (running >= voff);
4634 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
4635 shutdown_ctx->cb = &churn_stop_callback;
4636 shutdown_ctx->cb_cls = churn_ctx;
4637 shutdown_ctx->total_peers = voff;
4638 shutdown_ctx->timeout = timeout;
4641 for (i = 0; i < voff; i++)
4644 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
4645 running_permute[i]);
4647 GNUNET_assert (running_arr != NULL);
4648 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
4649 peer_shutdown_ctx->daemon =
4650 pg->peers[running_arr[running_permute[i]]].daemon;
4651 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4652 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
4656 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
4658 &churn_stop_callback, churn_ctx,
4659 GNUNET_NO, GNUNET_YES); */
4662 GNUNET_assert (stopped >= von);
4665 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
4666 churn_startup_ctx->churn_ctx = churn_ctx;
4667 churn_startup_ctx->timeout = timeout;
4669 for (i = 0; i < von; i++)
4672 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
4673 stopped_permute[i]);
4675 GNUNET_assert (stopped_arr != NULL);
4676 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
4677 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
4678 peer_restart_ctx->daemon =
4679 pg->peers[stopped_arr[stopped_permute[i]]].daemon;
4680 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
4682 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
4683 timeout, &churn_start_callback, churn_ctx); */
4686 GNUNET_free_non_null (running_arr);
4687 GNUNET_free_non_null (stopped_arr);
4688 GNUNET_free_non_null (running_permute);
4689 GNUNET_free_non_null (stopped_permute);
4694 * Restart all peers in the given group.
4696 * @param pg the handle to the peer group
4697 * @param callback function to call on completion (or failure)
4698 * @param callback_cls closure for the callback function
4701 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
4702 GNUNET_TESTING_NotifyCompletion callback,
4705 struct RestartContext *restart_context;
4710 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
4711 restart_context->peer_group = pg;
4712 restart_context->peers_restarted = 0;
4713 restart_context->callback = callback;
4714 restart_context->callback_cls = callback_cls;
4716 for (off = 0; off < pg->total; off++)
4718 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
4719 &restart_callback, restart_context);
4725 * Start or stop an individual peer from the given group.
4727 * @param pg handle to the peer group
4728 * @param offset which peer to start or stop
4729 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
4730 * @param timeout how long to wait for shutdown
4731 * @param cb function to call at the end
4732 * @param cb_cls closure for cb
4735 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
4736 unsigned int offset,
4738 struct GNUNET_TIME_Relative timeout,
4739 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
4741 struct ShutdownContext *shutdown_ctx;
4742 struct ChurnRestartContext *startup_ctx;
4743 struct ChurnContext *churn_ctx;
4745 if (GNUNET_NO == desired_status)
4747 if (NULL != pg->peers[offset].daemon)
4749 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
4750 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
4751 churn_ctx->num_to_start = 0;
4752 churn_ctx->num_to_stop = 1;
4754 churn_ctx->cb_cls = cb_cls;
4755 shutdown_ctx->cb_cls = churn_ctx;
4756 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon,
4757 timeout, &churn_stop_callback,
4758 shutdown_ctx, GNUNET_NO, GNUNET_YES);
4761 else if (GNUNET_YES == desired_status)
4763 if (NULL == pg->peers[offset].daemon)
4765 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
4766 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
4767 churn_ctx->num_to_start = 1;
4768 churn_ctx->num_to_stop = 0;
4770 churn_ctx->cb_cls = cb_cls;
4771 startup_ctx->churn_ctx = churn_ctx;
4772 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
4773 timeout, &churn_start_callback,
4783 * Callback for shutting down peers in a peer group.
4785 * @param cls closure (struct ShutdownContext)
4786 * @param emsg NULL on success
4789 internal_shutdown_callback (void *cls, const char *emsg)
4791 struct ShutdownContext *shutdown_ctx = cls;
4793 shutdown_ctx->outstanding--;
4796 shutdown_ctx->peers_down++;
4800 shutdown_ctx->peers_failed++;
4803 if ((shutdown_ctx->cb != NULL)
4804 && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed ==
4805 shutdown_ctx->total_peers))
4807 if (shutdown_ctx->peers_failed > 0)
4808 shutdown_ctx->cb (shutdown_ctx->cb_cls,
4809 "Not all peers successfully shut down!");
4811 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
4812 GNUNET_free (shutdown_ctx);
4818 * Task to rate limit the number of outstanding peer shutdown
4819 * requests. This is necessary for making sure we don't do
4820 * too many ssh connections at once, but is generally nicer
4821 * to any system as well (graduated task starts, as opposed
4822 * to calling gnunet-arm N times all at once).
4825 schedule_shutdown_task (void *cls,
4826 const struct GNUNET_SCHEDULER_TaskContext *tc)
4828 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4829 struct ShutdownContext *shutdown_ctx;
4831 GNUNET_assert (peer_shutdown_ctx != NULL);
4832 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4833 GNUNET_assert (shutdown_ctx != NULL);
4835 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4836 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4837 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4838 &schedule_shutdown_task, peer_shutdown_ctx);
4841 shutdown_ctx->outstanding++;
4842 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
4843 shutdown_ctx->timeout,
4844 &internal_shutdown_callback, shutdown_ctx,
4845 GNUNET_YES, GNUNET_NO);
4846 GNUNET_free (peer_shutdown_ctx);
4851 * Shutdown all peers started in the given group.
4853 * @param pg handle to the peer group
4854 * @param timeout how long to wait for shutdown
4855 * @param cb callback to notify upon success or failure
4856 * @param cb_cls closure for cb
4859 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
4860 struct GNUNET_TIME_Relative timeout,
4861 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
4864 struct ShutdownContext *shutdown_ctx;
4865 struct PeerShutdownContext *peer_shutdown_ctx;
4867 GNUNET_assert (pg->total > 0);
4869 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
4870 shutdown_ctx->cb = cb;
4871 shutdown_ctx->cb_cls = cb_cls;
4872 shutdown_ctx->total_peers = pg->total;
4873 shutdown_ctx->timeout = timeout;
4874 /* shtudown_ctx->outstanding = 0; */
4876 for (off = 0; off < pg->total; off++)
4878 GNUNET_assert (NULL != pg->peers[off].daemon);
4879 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
4880 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
4881 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4882 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
4883 //GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4884 if (NULL != pg->peers[off].cfg)
4885 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
4886 if (pg->peers[off].allowed_peers != NULL)
4887 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
4888 if (pg->peers[off].connect_peers != NULL)
4889 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
4890 if (pg->peers[off].blacklisted_peers != NULL)
4891 GNUNET_CONTAINER_multihashmap_destroy (pg->
4892 peers[off].blacklisted_peers);
4894 GNUNET_free (pg->peers);
4895 for (off = 0; off < pg->num_hosts; off++)
4897 GNUNET_free (pg->hosts[off].hostname);
4898 GNUNET_free_non_null (pg->hosts[off].username);
4900 GNUNET_free_non_null (pg->hosts);
4905 /* end of testing_group.c */