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
234 /** Waiting to read number of peers */
237 /** Should find next peer index */
240 /** Should find colon */
243 /** Should read other peer index, space, or endline */
248 struct PeerConnection
253 struct PeerConnection *next;
256 * Pointer to daemon handle
258 struct GNUNET_TESTING_Daemon *daemon;
263 struct InternalStartContext
266 * Pointer to peerdata
268 struct PeerData *peer;
271 * Timeout for peer startup
273 struct GNUNET_TIME_Relative timeout;
276 * Client callback for hostkey notification
278 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
281 * Closure for hostkey_callback
286 * Client callback for peer start notification
288 GNUNET_TESTING_NotifyDaemonRunning start_cb;
296 * Hostname, where to start the peer
298 const char *hostname;
301 * Username to use when connecting to the
304 const char *username;
307 * Port to use for ssh.
313 struct ChurnRestartContext
316 * Number of restarts currently in flight.
318 unsigned int outstanding;
321 * Handle to the underlying churn context.
323 struct ChurnContext *churn_ctx;
326 * How long to allow the operation to take.
328 struct GNUNET_TIME_Relative timeout;
332 * Data we keep per peer.
337 * (Initial) configuration of the host.
338 * (initial because clients could change
339 * it and we would not know about those
342 struct GNUNET_CONFIGURATION_Handle *cfg;
345 * Handle for controlling the daemon.
347 struct GNUNET_TESTING_Daemon *daemon;
350 * The peergroup this peer belongs to.
352 struct GNUNET_TESTING_PeerGroup *pg;
355 * Hash map of allowed peer connections (F2F created topology)
357 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
360 * Hash map of blacklisted peers
362 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
365 * Hash map of peer connections
367 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
370 * Temporary hash map of peer connections
372 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
375 * Temporary variable for topology creation, should be reset before
376 * creating any topology so the count is valid once finished.
381 * Context to keep track of peers being started, to
382 * stagger hostkey generation and peer startup.
384 struct InternalStartContext internal_context;
389 * Linked list of per-host data.
399 * SSH username to use when connecting to this host.
404 * SSH port to use when connecting to this host.
409 * Lowest port that we have not yet used
415 struct TopologyIterateContext
418 * Callback for notifying of two connected peers.
420 GNUNET_TESTING_NotifyTopology topology_cb;
423 * Closure for topology_cb
428 * Number of peers currently connected to.
430 unsigned int connected;
433 * Number of peers we have finished iterating.
435 unsigned int completed;
438 * Number of peers total.
443 struct StatsIterateContext
446 * Continuation to call once all stats information has been retrieved.
448 GNUNET_STATISTICS_Callback cont;
451 * Proc function to call on each value received.
453 GNUNET_TESTING_STATISTICS_Iterator proc;
456 * Closure for topology_cb
461 * Number of peers currently connected to.
463 unsigned int connected;
466 * Number of peers we have finished iterating.
468 unsigned int completed;
471 * Number of peers total.
479 struct GNUNET_TESTING_Daemon *daemon;
482 struct StatsCoreContext
485 struct GNUNET_TESTING_Daemon *daemon;
487 * Handle to the statistics service.
489 struct GNUNET_STATISTICS_Handle *stats_handle;
492 * Handle for getting statistics.
494 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
498 * Handle to a group of GNUnet peers.
500 struct GNUNET_TESTING_PeerGroup
503 * Configuration template.
505 const struct GNUNET_CONFIGURATION_Handle *cfg;
508 * Function to call on each started daemon.
510 //GNUNET_TESTING_NotifyDaemonRunning cb;
518 * Function to call on each topology connection created
520 GNUNET_TESTING_NotifyConnection notify_connection;
523 * Callback for notify_connection
525 void *notify_connection_cls;
528 * Array of information about hosts.
530 struct HostData *hosts;
533 * Number of hosts (size of HostData)
535 unsigned int num_hosts;
538 * Array of "total" peers.
540 struct PeerData *peers;
543 * Number of peers in this group.
548 * At what time should we fail the peer startup process?
550 struct GNUNET_TIME_Absolute max_timeout;
553 * How many peers are being started right now?
555 unsigned int starting;
558 * How many peers have already been started?
560 unsigned int started;
565 struct GNUNET_CONFIGURATION_Handle *ret;
566 const struct GNUNET_CONFIGURATION_Handle *orig;
567 const char *hostname;
573 struct ConnectTopologyContext
576 * How many connections are left to create.
578 unsigned int remaining_connections;
581 * Handle to group of peers.
583 struct GNUNET_TESTING_PeerGroup *pg;
586 * Temp value set for each iteration.
588 struct PeerData *first;
591 * Notification that all peers are connected.
593 GNUNET_TESTING_NotifyCompletion notify_connections_done;
596 * Closure for notify.
601 struct ConnectContext
604 * Peer to connect second to.
606 struct GNUNET_TESTING_Daemon *first;
609 * Peer to connect first to.
611 struct GNUNET_TESTING_Daemon *second;
614 * Higher level topology connection context.
616 struct ConnectTopologyContext *ct_ctx;
619 struct UnblacklistContext
624 struct GNUNET_TESTING_PeerGroup *pg;
627 * uid of the first peer
633 * Convert unique ID to hash code.
635 * @param uid unique ID to convert
636 * @param hash set to uid (extended with zeros)
639 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
641 memset (hash, 0, sizeof (GNUNET_HashCode));
642 *((uint32_t *) hash) = uid;
646 * Convert hash code to unique ID.
648 * @param uid unique ID to convert
649 * @param hash set to uid (extended with zeros)
652 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
654 memcpy (uid, hash, sizeof (uint32_t));
658 * Number of connects we are waiting on, allows us to rate limit
661 static int outstanding_connects;
664 * Get a topology from a string input.
666 * @param topology where to write the retrieved topology
667 * @param topology_string The string to attempt to
668 * get a configuration value from
669 * @return GNUNET_YES if topology string matched a
670 * known topology, GNUNET_NO if not
673 GNUNET_TESTING_topology_get (enum GNUNET_TESTING_Topology *topology,
674 const char *topology_string)
677 * Strings representing topologies in enum
679 static const char *topology_strings[] = {
681 * A clique (everyone connected to everyone else).
686 * Small-world network (2d torus plus random links).
691 * Small-world network (ring plus random links).
711 * Certain percentage of peers are unable to communicate directly
712 * replicating NAT conditions
717 * Scale free topology.
722 * Straight line topology.
727 * All peers are disconnected.
732 * Read the topology from a file.
740 if (topology_string == NULL)
742 while (topology_strings[curr] != NULL)
744 if (strcasecmp (topology_strings[curr], topology_string) == 0)
751 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
757 * Get connect topology option from string input.
759 * @param topology_option where to write the retrieved topology
760 * @param topology_string The string to attempt to
761 * get a configuration value from
762 * @return GNUNET_YES if string matched a known
763 * topology option, GNUNET_NO if not
766 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption
768 const char *topology_string)
771 * Options for connecting a topology as strings.
773 static const char *topology_option_strings[] = {
775 * Try to connect all peers specified in the topology.
780 * Choose a random subset of connections to create.
782 "CONNECT_RANDOM_SUBSET",
785 * Create at least X connections for each peer.
790 * Using a depth first search, create one connection
791 * per peer. If any are missed (graph disconnected)
792 * start over at those peers until all have at least one
798 * Find the N closest peers to each allowed peer in the
799 * topology and make sure a connection to those peers
800 * exists in the connect topology.
805 * No options specified.
813 if (topology_string == NULL)
815 while (NULL != topology_option_strings[curr])
817 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
819 *topology_option = curr;
824 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
829 * Function to iterate over options. Copies
830 * the options to the target configuration,
831 * updating PORT values as needed.
834 * @param section name of the section
835 * @param option name of the option
836 * @param value value of the option
839 update_config (void *cls,
840 const char *section, const char *option, const char *value)
842 struct UpdateContext *ctx = cls;
846 char *single_variable;
847 char *per_host_variable;
848 unsigned long long num_per_host;
850 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
852 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
855 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
858 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
862 GNUNET_free (single_variable);
865 if (0 == strcmp (option, "UNIXPATH"))
867 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
868 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
870 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
873 GNUNET_snprintf (uval,
875 "/tmp/test-service-%s-%u", section, ctx->upnum++);
878 else if ((GNUNET_YES ==
879 GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
881 &num_per_host)) && (num_per_host > 0))
884 GNUNET_snprintf (uval,
886 "/tmp/test-service-%s-%u",
887 section, ctx->fdnum % num_per_host);
890 GNUNET_free (single_variable);
891 GNUNET_free (per_host_variable);
895 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
897 value = ctx->hostname;
900 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
905 * Create a new configuration using the given configuration
906 * as a template; however, each PORT in the existing cfg
907 * must be renumbered by incrementing "*port". If we run
908 * out of "*port" numbers, return NULL.
910 * @param cfg template configuration
911 * @param port port numbers to use, update to reflect
912 * port numbers that were used
913 * @param upnum number to make unix domain socket names unique
914 * @param hostname hostname of the controlling host, to allow control connections from
915 * @param fdnum number used to offset the unix domain socket for grouped processes
916 * (such as statistics or peerinfo, which can be shared among others)
918 * @return new configuration, NULL on error
920 static struct GNUNET_CONFIGURATION_Handle *
921 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
923 uint32_t * upnum, const char *hostname, uint32_t * fdnum)
925 struct UpdateContext uc;
934 uc.ret = GNUNET_CONFIGURATION_create ();
935 uc.hostname = hostname;
938 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
939 if (uc.nport >= HIGH_PORT)
942 GNUNET_CONFIGURATION_destroy (uc.ret);
946 if (GNUNET_CONFIGURATION_get_value_string
947 (cfg, "testing", "control_host", &control_host) == GNUNET_OK)
949 if (hostname != NULL)
950 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
953 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
955 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
957 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
958 "ACCEPT_FROM", allowed_hosts);
959 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
961 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
962 "ACCEPT_FROM", allowed_hosts);
963 GNUNET_free_non_null (control_host);
964 GNUNET_free (allowed_hosts);
968 /* arm needs to know to allow connections from the host on which it is running,
969 * otherwise gnunet-arm is unable to connect to it in some instances */
970 if (hostname != NULL)
972 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
973 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
975 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
977 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
979 GNUNET_free (allowed_hosts);
983 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
984 "BINDTO", "127.0.0.1");
985 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
986 "BINDTO", "127.0.0.1");
989 *port = (uint16_t) uc.nport;
998 * Add entries to the peers connect list
1000 * @param pg the peer group we are working with
1001 * @param first index of the first peer
1002 * @param second index of the second peer
1004 * @return the number of connections added
1005 * technically should only be 0 or 2
1009 add_actual_connections (struct GNUNET_TESTING_PeerGroup *pg,
1010 unsigned int first, unsigned int second)
1016 GNUNET_HashCode hash_first;
1017 GNUNET_HashCode hash_second;
1019 hash_from_uid (first, &hash_first);
1020 hash_from_uid (second, &hash_second);
1022 add_first = GNUNET_NO;
1024 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].connect_peers,
1027 add_first = GNUNET_YES;
1030 add_second = GNUNET_NO;
1032 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].connect_peers,
1035 add_second = GNUNET_YES;
1041 GNUNET_assert (GNUNET_OK ==
1042 GNUNET_CONTAINER_multihashmap_put (pg->
1044 [first].connect_peers,
1047 peers[second].daemon,
1048 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1049 pg->peers[first].num_connections++;
1055 GNUNET_assert (GNUNET_OK ==
1056 GNUNET_CONTAINER_multihashmap_put (pg->
1058 [second].connect_peers,
1061 peers[first].daemon,
1062 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1063 pg->peers[second].num_connections++;
1072 * Add entries to the peers allowed connections list
1074 * @param pg the peer group we are working with
1075 * @param first index of the first peer
1076 * @param second index of the second peer
1078 * @return the number of connections added (can be 0, 1 or 2)
1079 * technically should only be 0 or 2, but the small price
1080 * of iterating over the lists (hashmaps in the future)
1081 * for being sure doesn't bother me!
1085 add_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg,
1086 unsigned int first, unsigned int second)
1090 struct PeerConnection *first_iter;
1091 struct PeerConnection *second_iter;
1092 struct PeerConnection *new_first;
1093 struct PeerConnection *new_second;
1098 GNUNET_HashCode hash_first;
1099 GNUNET_HashCode hash_second;
1101 hash_from_uid (first, &hash_first);
1102 hash_from_uid (second, &hash_second);
1104 add_first = GNUNET_NO;
1106 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].allowed_peers,
1109 add_first = GNUNET_YES;
1112 add_second = GNUNET_NO;
1114 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].allowed_peers,
1117 add_second = GNUNET_YES;
1120 first_iter = pg->peers[first].connected_peers;
1121 while (first_iter != NULL)
1123 if (first_iter->daemon == pg->peers[second].daemon)
1124 add_first = GNUNET_NO;
1125 first_iter = first_iter->next;
1128 second_iter = pg->peers[second].connected_peers;
1129 add_second = GNUNET_YES;
1130 while (second_iter != NULL)
1132 if (second_iter->daemon == pg->peers[first].daemon)
1133 add_second = GNUNET_NO;
1134 second_iter = second_iter->next;
1141 GNUNET_assert (GNUNET_OK ==
1142 GNUNET_CONTAINER_multihashmap_put (pg->
1144 [first].allowed_peers,
1147 peers[second].daemon,
1148 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1150 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1151 new_first->daemon = pg->peers[second].daemon;
1152 new_first->next = pg->peers[first].connected_peers;
1153 pg->peers[first].connected_peers = new_first;
1155 pg->peers[first].num_connections++;
1161 GNUNET_assert (GNUNET_OK ==
1162 GNUNET_CONTAINER_multihashmap_put (pg->
1164 [second].allowed_peers,
1167 peers[first].daemon,
1168 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1170 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1171 new_second->daemon = pg->peers[first].daemon;
1172 new_second->next = pg->peers[second].connected_peers;
1173 pg->peers[second].connected_peers = new_second;
1174 pg->peers[first].num_connections++;
1176 pg->peers[second].num_connections++;
1184 * Add entries to the peers blacklisted list
1186 * @param pg the peer group we are working with
1187 * @param first index of the first peer
1188 * @param second index of the second peer
1190 * @return the number of connections added (can be 0, 1 or 2)
1194 blacklist_connections (struct GNUNET_TESTING_PeerGroup *pg,
1195 unsigned int first, unsigned int second)
1200 GNUNET_HashCode hash_first;
1201 GNUNET_HashCode hash_second;
1203 hash_from_uid (first, &hash_first);
1204 hash_from_uid (second, &hash_second);
1206 add_first = GNUNET_NO;
1208 GNUNET_CONTAINER_multihashmap_contains (pg->
1209 peers[first].blacklisted_peers,
1212 add_first = GNUNET_YES;
1215 add_second = GNUNET_NO;
1217 GNUNET_CONTAINER_multihashmap_contains (pg->
1218 peers[second].blacklisted_peers,
1221 add_second = GNUNET_YES;
1227 GNUNET_assert (GNUNET_OK ==
1228 GNUNET_CONTAINER_multihashmap_put (pg->
1230 [first].blacklisted_peers,
1233 peers[second].daemon,
1234 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1235 pg->peers[first].num_connections++;
1241 GNUNET_assert (GNUNET_OK ==
1242 GNUNET_CONTAINER_multihashmap_put (pg->
1244 [second].blacklisted_peers,
1247 peers[first].daemon,
1248 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1249 pg->peers[second].num_connections++;
1257 * Remove entries from the peers blacklisted list
1259 * @param pg the peer group we are working with
1260 * @param first index of the first peer
1261 * @param second index of the second peer
1263 * @return the number of connections removed (can be 0, 1 or 2)
1267 unblacklist_connections (struct GNUNET_TESTING_PeerGroup *pg,
1268 unsigned int first, unsigned int second)
1273 GNUNET_HashCode hash_first;
1274 GNUNET_HashCode hash_second;
1276 hash_from_uid (first, &hash_first);
1277 hash_from_uid (second, &hash_second);
1280 GNUNET_CONTAINER_multihashmap_contains (pg->
1281 peers[first].blacklisted_peers,
1284 GNUNET_CONTAINER_multihashmap_contains (pg->
1285 peers[second].blacklisted_peers,
1291 GNUNET_assert (GNUNET_YES ==
1292 GNUNET_CONTAINER_multihashmap_remove (pg->
1294 [first].blacklisted_peers,
1304 GNUNET_assert (GNUNET_YES ==
1305 GNUNET_CONTAINER_multihashmap_remove (pg->
1307 [second].blacklisted_peers,
1319 * Scale free network construction as described in:
1321 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1323 * Start with a network of "one" peer, then progressively add
1324 * peers up to the total number. At each step, iterate over
1325 * all possible peers and connect new peer based on number of
1326 * existing connections of the target peer.
1328 * @param pg the peer group we are dealing with
1329 * @param proc the connection processor to use
1331 * @return the number of connections created
1334 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg,
1335 GNUNET_TESTING_ConnectionProcessor proc)
1338 unsigned int total_connections;
1339 unsigned int outer_count;
1341 unsigned int previous_total_connections;
1345 GNUNET_assert (pg->total > 1);
1347 /* Add a connection between the first two nodes */
1348 total_connections = proc (pg, 0, 1);
1350 for (outer_count = 1; outer_count < pg->total; outer_count++)
1352 previous_total_connections = total_connections;
1353 for (i = 0; i < outer_count; i++)
1356 pg->peers[i].num_connections /
1357 (double) previous_total_connections;
1360 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1361 UINT64_MAX)) / ((double) UINT64_MAX);
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1364 "Considering connecting peer %d to peer %d\n",
1367 if (random < probability)
1370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1371 "Connecting peer %d to peer %d\n", outer_count, i);
1373 total_connections += proc (pg, outer_count, i);
1378 return total_connections;
1382 * Create a topology given a peer group (set of running peers)
1383 * and a connection processor. Creates a small world topology
1384 * according to the rewired ring construction. The basic
1385 * behavior is that a ring topology is created, but with some
1386 * probability instead of connecting a peer to the next
1387 * neighbor in the ring a connection will be created to a peer
1388 * selected uniformly at random. We use the TESTING
1389 * PERCENTAGE option to specify what number of
1390 * connections each peer should have. Default is 2,
1391 * which makes the ring, any given number is multiplied by
1392 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1393 * each peer have on average 2logn connections. The additional
1394 * connections are made at increasing distance around the ring
1395 * from the original peer, or to random peers based on the re-
1396 * wiring probability. The TESTING
1397 * PROBABILITY option is used as the probability that a given
1398 * connection is rewired.
1400 * @param pg the peergroup to create the topology on
1401 * @param proc the connection processor to call to actually set
1402 * up connections between two peers
1404 * @return the number of connections that were set up
1408 create_small_world_ring (struct GNUNET_TESTING_PeerGroup *pg,
1409 GNUNET_TESTING_ConnectionProcessor proc)
1413 unsigned int natLog;
1414 unsigned int randomPeer;
1415 double random, logNModifier, probability;
1416 unsigned int smallWorldConnections;
1421 unsigned int useAnd;
1422 int connect_attempts;
1424 logNModifier = 0.5; /* FIXME: default value? */
1425 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1430 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1431 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1433 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1434 p_string, "LOGNMODIFIER", "TESTING");
1435 GNUNET_free (p_string);
1437 probability = 0.5; /* FIXME: default percentage? */
1438 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1443 if (sscanf (p_string, "%lf", &probability) != 1)
1444 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1446 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1447 p_string, "PERCENTAGE", "TESTING");
1448 GNUNET_free (p_string);
1450 natLog = log (pg->total);
1451 connsPerPeer = ceil (natLog * logNModifier);
1453 if (connsPerPeer % 2 == 1)
1456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."), connsPerPeer);
1458 smallWorldConnections = 0;
1459 connect_attempts = 0;
1460 for (i = 0; i < pg->total; i++)
1463 max = i + connsPerPeer / 2;
1464 min = i - connsPerPeer / 2;
1466 if (max > pg->total - 1)
1468 max = max - pg->total;
1474 min = pg->total - 1 + min;
1478 for (j = 0; j < connsPerPeer / 2; j++)
1482 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1483 UINT64_MAX) / ((double) UINT64_MAX));
1484 if (random < probability)
1486 /* Connect to uniformly selected random peer */
1488 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1490 while ((((randomPeer < max) && (randomPeer > min))
1491 && (useAnd == 0)) || (((randomPeer > min)
1492 || (randomPeer < max))
1496 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1499 smallWorldConnections += proc (pg, i, randomPeer);
1503 nodeToConnect = i + j + 1;
1504 if (nodeToConnect > pg->total - 1)
1506 nodeToConnect = nodeToConnect - pg->total;
1508 connect_attempts += proc (pg, i, nodeToConnect);
1514 connect_attempts += smallWorldConnections;
1516 return connect_attempts;
1520 * Create a topology given a peer group (set of running peers)
1521 * and a connection processor.
1523 * @param pg the peergroup to create the topology on
1524 * @param proc the connection processor to call to actually set
1525 * up connections between two peers
1527 * @return the number of connections that were set up
1531 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg,
1532 GNUNET_TESTING_ConnectionProcessor proc)
1534 unsigned int outer_count, inner_count;
1535 unsigned int cutoff;
1536 int connect_attempts;
1537 double nat_percentage;
1540 nat_percentage = 0.6; /* FIXME: default percentage? */
1541 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1546 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1547 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1549 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1550 p_string, "PERCENTAGE", "TESTING");
1551 GNUNET_free (p_string);
1556 cutoff = (unsigned int) (nat_percentage * pg->total);
1558 connect_attempts = 0;
1560 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1562 for (inner_count = outer_count + 1; inner_count < pg->total;
1565 if ((outer_count > cutoff) || (inner_count > cutoff))
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1569 "Connecting peer %d to peer %d\n",
1570 outer_count, inner_count);
1572 connect_attempts += proc (pg, outer_count, inner_count);
1577 return connect_attempts;
1582 * Create a topology given a peer group (set of running peers)
1583 * and a connection processor.
1585 * @param pg the peergroup to create the topology on
1586 * @param proc the connection processor to call to actually set
1587 * up connections between two peers
1589 * @return the number of connections that were set up
1593 create_small_world (struct GNUNET_TESTING_PeerGroup *pg,
1594 GNUNET_TESTING_ConnectionProcessor proc)
1596 unsigned int i, j, k;
1597 unsigned int square;
1600 unsigned int toggle = 1;
1601 unsigned int nodeToConnect;
1602 unsigned int natLog;
1603 unsigned int node1Row;
1604 unsigned int node1Col;
1605 unsigned int node2Row;
1606 unsigned int node2Col;
1607 unsigned int distance;
1608 double probability, random, percentage;
1609 unsigned int smallWorldConnections;
1610 unsigned int small_world_it;
1612 int connect_attempts;
1613 square = floor (sqrt (pg->total));
1617 percentage = 0.5; /* FIXME: default percentage? */
1618 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1623 if (sscanf (p_string, "%lf", &percentage) != 1)
1624 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1626 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1627 p_string, "PERCENTAGE", "TESTING");
1628 GNUNET_free (p_string);
1630 if (percentage < 0.0)
1632 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1634 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1635 "PERCENTAGE", "TESTING", percentage);
1638 probability = 0.5; /* FIXME: default percentage? */
1639 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1644 if (sscanf (p_string, "%lf", &probability) != 1)
1645 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1647 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1648 p_string, "PROBABILITY", "TESTING");
1649 GNUNET_free (p_string);
1651 if (square * square != pg->total)
1653 while (rows * cols < pg->total)
1655 if (toggle % 2 == 0)
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1666 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1670 connect_attempts = 0;
1671 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1672 * to the node to its right and above. Once this is over, we'll have our torus!
1673 * Special case for the last node (if the rows and columns are not equal), connect
1674 * to the first in the row to maintain topology.
1676 for (i = 0; i < pg->total; i++)
1678 /* First connect to the node to the right */
1679 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1680 nodeToConnect = i + 1;
1681 else if (i + 1 == pg->total)
1682 nodeToConnect = rows * cols - cols;
1684 nodeToConnect = i - cols + 1;
1686 connect_attempts += proc (pg, i, nodeToConnect);
1689 nodeToConnect = (rows * cols) - cols + i;
1691 nodeToConnect = i - cols;
1693 if (nodeToConnect < pg->total)
1694 connect_attempts += proc (pg, i, nodeToConnect);
1696 natLog = log (pg->total);
1697 #if VERBOSE_TESTING > 2
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1699 _("natural log of %d is %d, will run %d iterations\n"),
1700 pg->total, natLog, (int) (natLog * percentage));
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1702 _("Total connections added thus far: %u!\n"), connect_attempts);
1704 smallWorldConnections = 0;
1705 small_world_it = (unsigned int) (natLog * percentage);
1706 if (small_world_it < 1)
1708 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
1709 for (i = 0; i < small_world_it; i++)
1711 for (j = 0; j < pg->total; j++)
1713 /* Determine the row and column of node at position j on the 2d torus */
1714 node1Row = j / cols;
1715 node1Col = j - (node1Row * cols);
1716 for (k = 0; k < pg->total; k++)
1718 /* Determine the row and column of node at position k on the 2d torus */
1719 node2Row = k / cols;
1720 node2Col = k - (node2Row * cols);
1721 /* Simple Cartesian distance */
1723 abs (node1Row - node2Row) + abs (node1Col - node2Col);
1726 /* Calculate probability as 1 over the square of the distance */
1727 probability = 1.0 / (distance * distance);
1728 /* Choose a random value between 0 and 1 */
1731 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1733 ((double) UINT64_MAX);
1734 /* If random < probability, then connect the two nodes */
1735 if (random < probability)
1736 smallWorldConnections += proc (pg, j, k);
1742 connect_attempts += smallWorldConnections;
1743 #if VERBOSE_TESTING > 2
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1745 _("Total connections added for small world: %d!\n"),
1746 smallWorldConnections);
1748 return connect_attempts;
1752 * Create a topology given a peer group (set of running peers)
1753 * and a connection processor.
1755 * @param pg the peergroup to create the topology on
1756 * @param proc the connection processor to call to actually set
1757 * up connections between two peers
1759 * @return the number of connections that were set up
1763 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg,
1764 GNUNET_TESTING_ConnectionProcessor proc)
1767 unsigned int outer_count;
1768 unsigned int inner_count;
1769 int connect_attempts;
1773 probability = 0.5; /* FIXME: default percentage? */
1774 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1779 if (sscanf (p_string, "%lf", &probability) != 1)
1780 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1782 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1783 p_string, "PROBABILITY", "TESTING");
1784 GNUNET_free (p_string);
1786 connect_attempts = 0;
1787 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1789 for (inner_count = outer_count + 1; inner_count < pg->total;
1794 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1795 UINT64_MAX)) / ((double) UINT64_MAX);
1797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1798 _("rand is %f probability is %f\n"), temp_rand,
1801 if (temp_rand < probability)
1803 connect_attempts += proc (pg, outer_count, inner_count);
1808 return connect_attempts;
1812 * Create a topology given a peer group (set of running peers)
1813 * and a connection processor. This particular function creates
1814 * the connections for a 2d-torus, plus additional "closest"
1815 * connections per peer.
1817 * @param pg the peergroup to create the topology on
1818 * @param proc the connection processor to call to actually set
1819 * up connections between two peers
1821 * @return the number of connections that were set up
1825 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg,
1826 GNUNET_TESTING_ConnectionProcessor proc)
1829 unsigned int square;
1832 unsigned int toggle = 1;
1833 unsigned int nodeToConnect;
1834 int connect_attempts;
1836 connect_attempts = 0;
1838 square = floor (sqrt (pg->total));
1842 if (square * square != pg->total)
1844 while (rows * cols < pg->total)
1846 if (toggle % 2 == 0)
1855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1857 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1860 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1861 * to the node to its right and above. Once this is over, we'll have our torus!
1862 * Special case for the last node (if the rows and columns are not equal), connect
1863 * to the first in the row to maintain topology.
1865 for (i = 0; i < pg->total; i++)
1867 /* First connect to the node to the right */
1868 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1869 nodeToConnect = i + 1;
1870 else if (i + 1 == pg->total)
1871 nodeToConnect = rows * cols - cols;
1873 nodeToConnect = i - cols + 1;
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876 "Connecting peer %d to peer %d\n", i, nodeToConnect);
1878 connect_attempts += proc (pg, i, nodeToConnect);
1880 /* Second connect to the node immediately above */
1882 nodeToConnect = (rows * cols) - cols + i;
1884 nodeToConnect = i - cols;
1886 if (nodeToConnect < pg->total)
1889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1890 "Connecting peer %d to peer %d\n", i, nodeToConnect);
1892 connect_attempts += proc (pg, i, nodeToConnect);
1897 return connect_attempts;
1902 * Create a topology given a peer group (set of running peers)
1903 * and a connection processor.
1905 * @param pg the peergroup to create the topology on
1906 * @param proc the connection processor to call to actually set
1907 * up connections between two peers
1909 * @return the number of connections that were set up
1913 create_clique (struct GNUNET_TESTING_PeerGroup *pg,
1914 GNUNET_TESTING_ConnectionProcessor proc)
1916 unsigned int outer_count;
1917 unsigned int inner_count;
1918 int connect_attempts;
1920 connect_attempts = 0;
1922 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1924 for (inner_count = outer_count + 1; inner_count < pg->total;
1928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1929 "Connecting peer %d to peer %d\n",
1930 outer_count, inner_count);
1932 connect_attempts += proc (pg, outer_count, inner_count);
1936 return connect_attempts;
1940 * Iterator over hash map entries.
1942 * @param cls closure the peer group
1943 * @param key the key stored in the hashmap is the
1944 * index of the peer to connect to
1945 * @param value value in the hash map, handle to the peer daemon
1946 * @return GNUNET_YES if we should continue to
1951 unblacklist_iterator (void *cls,
1952 const GNUNET_HashCode * key,
1955 struct UnblacklistContext *un_ctx = cls;
1956 uint32_t second_pos;
1958 uid_from_hash (key, &second_pos);
1960 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
1966 * Create a blacklist topology based on the allowed topology
1967 * which disallows any connections not in the allowed topology
1968 * at the transport level.
1970 * @param pg the peergroup to create the topology on
1971 * @param proc the connection processor to call to allow
1972 * up connections between two peers
1974 * @return the number of connections that were set up
1978 copy_allowed (struct GNUNET_TESTING_PeerGroup *pg,
1979 GNUNET_TESTING_ConnectionProcessor proc)
1981 struct UnblacklistContext un_ctx;
1987 for (count = 0; count < pg->total - 1; count++)
1989 un_ctx.first_uid = count;
1990 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers, &unblacklist_iterator, &un_ctx);
1992 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
1997 * Create a topology given a peer group (set of running peers)
1998 * and a connection processor.
2000 * @param pg the peergroup to create the topology on
2001 * @param proc the connection processor to call to actually set
2002 * up connections between two peers
2004 * @return the number of connections that were set up
2008 create_line (struct GNUNET_TESTING_PeerGroup *pg,
2009 GNUNET_TESTING_ConnectionProcessor proc)
2012 int connect_attempts;
2014 connect_attempts = 0;
2016 /* Connect each peer to the next highest numbered peer */
2017 for (count = 0; count < pg->total - 1; count++)
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2021 "Connecting peer %d to peer %d\n", count, count + 1);
2023 connect_attempts += proc (pg, count, count + 1);
2026 return connect_attempts;
2030 * Create a topology given a peer group (set of running peers)
2031 * and a connection processor.
2033 * @param pg the peergroup to create the topology on
2034 * @param filename the file to read topology information from
2035 * @param proc the connection processor to call to actually set
2036 * up connections between two peers
2038 * @return the number of connections that were set up
2042 create_from_file (struct GNUNET_TESTING_PeerGroup *pg,
2044 GNUNET_TESTING_ConnectionProcessor proc)
2046 int connect_attempts;
2047 unsigned int first_peer_index;
2048 unsigned int second_peer_index;
2049 connect_attempts = 0;
2054 unsigned int total_peers;
2056 enum States curr_state;
2058 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2059 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ
2060 | GNUNET_DISK_PERM_USER_WRITE);
2062 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2064 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2065 "Could not open file specified for topology!");
2066 return connect_attempts;
2069 data = GNUNET_malloc_large (frstat.st_size);
2070 GNUNET_assert(data != NULL);
2071 if (frstat.st_size !=
2072 GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2074 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2075 "Could not read file %s specified for host list, ending test!", filename);
2077 return connect_attempts;
2082 /* First line should contain a single integer, specifying the number of peers */
2083 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2084 curr_state = NUM_PEERS;
2085 while (count < frstat.st_size - 1)
2087 if ((buf[count] == '\n') || (buf[count] == ' '))
2096 if (1 != sscanf(&buf[count], "%u", &total_peers))
2098 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read number of peers from topology file!\n");
2099 GNUNET_free_non_null(data);
2100 return connect_attempts;
2102 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read %u total peers in topology\n", total_peers);
2103 curr_state = PEER_INDEX;
2104 while((buf[count] != '\n') && (count < frstat.st_size - 1))
2109 if (1 != sscanf(&buf[count], "%u", &first_peer_index))
2111 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read peer index from topology file!\n");
2112 GNUNET_free_non_null(data);
2113 return connect_attempts;
2115 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read next peer index %u\n", first_peer_index);
2116 while((buf[count] != ':') && (count < frstat.st_size - 1))
2119 curr_state = OTHER_PEER_INDEX;
2122 if (1 == sscanf(&buf[count], ":"))
2123 curr_state = OTHER_PEER_INDEX;
2126 case OTHER_PEER_INDEX:
2127 if (1 != sscanf(&buf[count], "%u", &second_peer_index))
2129 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to peer index from topology file!\n");
2130 GNUNET_free_non_null(data);
2131 return connect_attempts;
2133 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read second peer index %u\n", second_peer_index);
2134 while((buf[count] != '\n') && (buf[count] != ' ') && (count < frstat.st_size - 1))
2136 if (buf[count] == '\n')
2138 curr_state = PEER_INDEX;
2140 else if (buf[count] != ' ')
2142 curr_state = OTHER_PEER_INDEX;
2145 curr_state = OTHER_PEER_INDEX;
2148 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Found bad data in topology file while in state %d!\n", curr_state);
2150 return connect_attempts;
2155 /* Connect each peer to the next highest numbered peer */
2156 for (count = 0; count < pg->total - 1; count++)
2159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2160 "Connecting peer %d to peer %d\n", first_peer_index, second_peer_index);
2162 connect_attempts += proc (pg, first_peer_index, second_peer_index);
2165 return connect_attempts;
2169 * Create a topology given a peer group (set of running peers)
2170 * and a connection processor.
2172 * @param pg the peergroup to create the topology on
2173 * @param proc the connection processor to call to actually set
2174 * up connections between two peers
2176 * @return the number of connections that were set up
2180 create_ring (struct GNUNET_TESTING_PeerGroup *pg,
2181 GNUNET_TESTING_ConnectionProcessor proc)
2184 int connect_attempts;
2186 connect_attempts = 0;
2188 /* Connect each peer to the next highest numbered peer */
2189 for (count = 0; count < pg->total - 1; count++)
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2193 "Connecting peer %d to peer %d\n", count, count + 1);
2195 connect_attempts += proc (pg, count, count + 1);
2198 /* Connect the last peer to the first peer */
2199 connect_attempts += proc (pg, pg->total - 1, 0);
2201 return connect_attempts;
2206 * Iterator for writing friends of a peer to a file.
2208 * @param cls closure, an open writable file handle
2209 * @param key the key the daemon was stored under
2210 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2212 * @return GNUNET_YES to continue iteration
2214 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2215 * with a single file_iterator that takes a closure which contains
2216 * the prefix to write before the peer. Then this could be used
2217 * for blacklisting multiple transports and writing the friend
2218 * file. I'm sure *someone* will complain loudly about other
2219 * things that negate these functions even existing so no point in
2223 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2225 FILE *temp_friend_handle = cls;
2226 struct GNUNET_TESTING_Daemon *peer = value;
2227 struct GNUNET_PeerIdentity *temppeer;
2228 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2230 temppeer = &peer->id;
2231 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2232 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2237 struct BlacklistContext
2240 * The (open) file handle to write to
2242 FILE *temp_file_handle;
2245 * The transport that this peer will be blacklisted on.
2251 * Iterator for writing blacklist data to appropriate files.
2253 * @param cls closure, an open writable file handle
2254 * @param key the key the daemon was stored under
2255 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2257 * @return GNUNET_YES to continue iteration
2260 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2262 struct BlacklistContext *blacklist_ctx = cls;
2263 struct GNUNET_TESTING_Daemon *peer = value;
2264 struct GNUNET_PeerIdentity *temppeer;
2265 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2267 temppeer = &peer->id;
2268 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2269 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2270 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2271 blacklist_ctx->transport, (char *) &peer_enc);
2277 * Create the friend files based on the PeerConnection's
2278 * of each peer in the peer group, and copy the files
2279 * to the appropriate place
2281 * @param pg the peer group we are dealing with
2284 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
2286 FILE *temp_friend_handle;
2287 unsigned int pg_iter;
2288 char *temp_service_path;
2289 struct GNUNET_OS_Process **procarr;
2292 enum GNUNET_OS_ProcessStatusType type;
2293 unsigned long return_code;
2298 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2299 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2301 mytemp = GNUNET_DISK_mktemp ("friends");
2302 GNUNET_assert (mytemp != NULL);
2303 temp_friend_handle = fopen (mytemp, "wt");
2304 GNUNET_assert (temp_friend_handle != NULL);
2305 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2306 &friend_file_iterator,
2307 temp_friend_handle);
2308 fclose (temp_friend_handle);
2311 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2312 daemon->cfg, "PATHS",
2314 &temp_service_path))
2316 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2318 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2319 "SERVICEHOME", "PATHS");
2320 if (UNLINK (mytemp) != 0)
2321 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2323 GNUNET_free (mytemp);
2327 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2329 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2330 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2335 _("Copying file with command cp %s %s\n"), mytemp, arg);
2340 else /* Remote, scp the file to the correct place */
2342 if (NULL != pg->peers[pg_iter].daemon->username)
2343 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2344 pg->peers[pg_iter].daemon->username,
2345 pg->peers[pg_iter].daemon->hostname,
2348 GNUNET_asprintf (&arg, "%s:%s/friends",
2349 pg->peers[pg_iter].daemon->hostname,
2352 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2357 _("Copying file with command scp %s %s\n"), mytemp,
2362 GNUNET_free (temp_service_path);
2363 GNUNET_free (mytemp);
2367 ret = GNUNET_SYSERR;
2368 while ((count < max_wait) && (ret != GNUNET_OK))
2371 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2375 _("Checking copy status of file %d\n"), pg_iter);
2377 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2379 if (GNUNET_OS_process_status
2380 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2382 ret = GNUNET_SYSERR;
2384 else if ((type != GNUNET_OS_PROCESS_EXITED)
2385 || (return_code != 0))
2387 ret = GNUNET_SYSERR;
2391 GNUNET_OS_process_close (procarr[pg_iter]);
2392 procarr[pg_iter] = NULL;
2394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2395 _("File %d copied\n"), pg_iter);
2401 if (ret == GNUNET_SYSERR)
2403 /* FIXME: why sleep here? -CG */
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2410 _("Finished copying all friend files!\n"));
2412 GNUNET_free (procarr);
2418 * Create the blacklist files based on the PeerConnection's
2419 * of each peer in the peer group, and copy the files
2420 * to the appropriate place.
2422 * @param pg the peer group we are dealing with
2423 * @param transports space delimited list of transports to blacklist
2426 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg,
2427 const char *transports)
2429 FILE *temp_file_handle;
2430 static struct BlacklistContext blacklist_ctx;
2431 unsigned int pg_iter;
2432 char *temp_service_path;
2433 struct GNUNET_OS_Process **procarr;
2436 enum GNUNET_OS_ProcessStatusType type;
2437 unsigned long return_code;
2444 char *temp_transports;
2447 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2448 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2450 mytemp = GNUNET_DISK_mktemp ("blacklist");
2451 GNUNET_assert (mytemp != NULL);
2452 temp_file_handle = fopen (mytemp, "wt");
2453 GNUNET_assert (temp_file_handle != NULL);
2454 temp_transports = GNUNET_strdup (transports);
2455 blacklist_ctx.temp_file_handle = temp_file_handle;
2456 transport_len = strlen (temp_transports) + 1;
2459 for (i = 0; i < transport_len; i++)
2461 if ((temp_transports[i] == ' ') && (pos == NULL))
2462 continue; /* At start of string (whitespace) */
2463 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2465 temp_transports[i] = '\0';
2466 blacklist_ctx.transport = pos;
2467 entry_count = GNUNET_CONTAINER_multihashmap_iterate (pg->
2469 [pg_iter].blacklisted_peers,
2470 &blacklist_file_iterator,
2473 } /* At beginning of actual string */
2474 else if (pos == NULL)
2476 pos = &temp_transports[i];
2480 GNUNET_free (temp_transports);
2481 fclose (temp_file_handle);
2484 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2485 daemon->cfg, "PATHS",
2487 &temp_service_path))
2489 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2491 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2492 "SERVICEHOME", "PATHS");
2493 if (UNLINK (mytemp) != 0)
2494 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2496 GNUNET_free (mytemp);
2500 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2502 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2503 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2508 _("Copying file with command cp %s %s\n"), mytemp, arg);
2513 else /* Remote, scp the file to the correct place */
2515 if (NULL != pg->peers[pg_iter].daemon->username)
2516 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
2517 pg->peers[pg_iter].daemon->username,
2518 pg->peers[pg_iter].daemon->hostname,
2521 GNUNET_asprintf (&arg, "%s:%s/blacklist",
2522 pg->peers[pg_iter].daemon->hostname,
2525 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2530 _("Copying file with command scp %s %s\n"), mytemp,
2535 GNUNET_free (temp_service_path);
2536 GNUNET_free (mytemp);
2540 ret = GNUNET_SYSERR;
2541 while ((count < max_wait) && (ret != GNUNET_OK))
2544 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2548 _("Checking copy status of file %d\n"), pg_iter);
2550 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2552 if (GNUNET_OS_process_status
2553 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2555 ret = GNUNET_SYSERR;
2557 else if ((type != GNUNET_OS_PROCESS_EXITED)
2558 || (return_code != 0))
2560 ret = GNUNET_SYSERR;
2564 GNUNET_OS_process_close (procarr[pg_iter]);
2565 procarr[pg_iter] = NULL;
2567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2568 _("File %d copied\n"), pg_iter);
2574 if (ret == GNUNET_SYSERR)
2576 /* FIXME: why sleep here? -CG */
2582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2583 _("Finished copying all blacklist files!\n"));
2585 GNUNET_free (procarr);
2591 * Internal notification of a connection, kept so that we can ensure some connections
2592 * happen instead of flooding all testing daemons with requests to connect.
2595 internal_connect_notify (void *cls,
2596 const struct GNUNET_PeerIdentity *first,
2597 const struct GNUNET_PeerIdentity *second,
2599 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2600 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2601 struct GNUNET_TESTING_Daemon *first_daemon,
2602 struct GNUNET_TESTING_Daemon *second_daemon,
2605 struct ConnectTopologyContext *ct_ctx = cls;
2606 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
2607 outstanding_connects--;
2608 ct_ctx->remaining_connections--;
2609 if (ct_ctx->remaining_connections == 0)
2611 if (ct_ctx->notify_connections_done != NULL)
2612 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
2613 GNUNET_free (ct_ctx);
2616 if (pg->notify_connection != NULL)
2617 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
2618 first_cfg, second_cfg, first_daemon, second_daemon,
2624 * Either delay a connection (because there are too many outstanding)
2625 * or schedule it for right now.
2627 * @param cls a connection context
2628 * @param tc the task runtime context
2631 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2633 struct ConnectContext *connect_context = cls;
2635 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2638 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2640 #if VERBOSE_TESTING > 2
2641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2643 ("Delaying connect, we have too many outstanding connections!\n"));
2645 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2646 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
2647 &schedule_connect, connect_context);
2651 #if VERBOSE_TESTING > 2
2652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2653 _("Creating connection, outstanding_connections is %d\n"),
2654 outstanding_connects);
2656 outstanding_connects++;
2657 GNUNET_TESTING_daemons_connect (connect_context->first,
2658 connect_context->second,
2661 &internal_connect_notify,
2662 connect_context->ct_ctx);
2663 GNUNET_free (connect_context);
2669 * Iterator for actually scheduling connections to be created
2670 * between two peers.
2672 * @param cls closure, a GNUNET_TESTING_Daemon
2673 * @param key the key the second Daemon was stored under
2674 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2676 * @return GNUNET_YES to continue iteration
2679 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2681 struct ConnectTopologyContext *ct_ctx = cls;
2682 struct PeerData *first = ct_ctx->first;
2683 struct GNUNET_TESTING_Daemon *second = value;
2684 struct ConnectContext *connect_context;
2686 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
2687 connect_context->first = first->daemon;
2688 connect_context->second = second;
2689 connect_context->ct_ctx = ct_ctx;
2690 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
2697 * Iterator for copying all entries in the allowed hashmap to the
2700 * @param cls closure, a GNUNET_TESTING_Daemon
2701 * @param key the key the second Daemon was stored under
2702 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2704 * @return GNUNET_YES to continue iteration
2707 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2709 struct PeerData *first = cls;
2711 GNUNET_assert (GNUNET_OK ==
2712 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
2714 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2720 * Make the peers to connect the same as those that are allowed to be
2723 * @param pg the peer group
2726 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2728 unsigned int pg_iter;
2733 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2736 GNUNET_CONTAINER_multihashmap_iterate (pg->
2737 peers[pg_iter].allowed_peers,
2738 ©_topology_iterator,
2739 &pg->peers[pg_iter]);
2740 if (GNUNET_SYSERR == ret)
2741 return GNUNET_SYSERR;
2743 total = total + ret;
2751 * Connect the topology as specified by the PeerConnection's
2752 * of each peer in the peer group
2754 * @param pg the peer group we are dealing with
2755 * @param notify_callback callback to notify when finished
2756 * @param notify_cls closure for notify callback
2758 * @return the number of connections that will be attempted
2761 connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2762 GNUNET_TESTING_NotifyCompletion notify_callback,
2765 unsigned int pg_iter;
2768 struct ConnectTopologyContext *ct_ctx;
2770 struct PeerConnection *connection_iter;
2771 struct ConnectContext *connect_context;
2775 ct_ctx = GNUNET_malloc (sizeof (struct ConnectTopologyContext));
2776 ct_ctx->notify_connections_done = notify_callback;
2777 ct_ctx->notify_cls = notify_cls;
2780 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2783 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
2788 GNUNET_free (ct_ctx);
2791 ct_ctx->remaining_connections = total;
2794 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2796 ct_ctx->first = &pg->peers[pg_iter];
2798 GNUNET_CONTAINER_multihashmap_iterate (pg->
2799 peers[pg_iter].connect_peers,
2800 &connect_iterator, ct_ctx);
2801 GNUNET_assert (GNUNET_SYSERR != ret && ret >= 0);
2802 total = total + ret;
2805 connection_iter = FIXME;
2806 while (connection_iter != NULL)
2808 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
2809 connect_context->pg = pg;
2810 connect_context->first = FIXME;
2811 connect_context->second = connection_iter->daemon;
2812 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
2813 connection_iter = connection_iter->next;
2822 * Takes a peer group and creates a topology based on the
2823 * one specified. Creates a topology means generates friend
2824 * files for the peers so they can only connect to those allowed
2825 * by the topology. This will only have an effect once peers
2826 * are started if the FRIENDS_ONLY option is set in the base
2827 * config. Also takes an optional restrict topology which
2828 * disallows connections based on particular transports
2829 * UNLESS they are specified in the restricted topology.
2831 * @param pg the peer group struct representing the running peers
2832 * @param topology which topology to connect the peers in
2833 * @param restrict_topology disallow restrict_transports transport
2834 * connections to peers NOT in this topology
2835 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2836 * @param restrict_transports space delimited list of transports to blacklist
2837 * to create restricted topology
2839 * @return the maximum number of connections were all allowed peers
2840 * connected to each other
2843 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2844 enum GNUNET_TESTING_Topology topology,
2845 enum GNUNET_TESTING_Topology
2847 const char *restrict_transports)
2850 unsigned int num_connections;
2851 int unblacklisted_connections;
2856 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
2860 num_connections = create_clique (pg, &add_allowed_connections);
2862 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2865 _("Creating small world (ring) topology\n"));
2868 create_small_world_ring (pg, &add_allowed_connections);
2870 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2873 _("Creating small world (2d-torus) topology\n"));
2875 num_connections = create_small_world (pg, &add_allowed_connections);
2877 case GNUNET_TESTING_TOPOLOGY_RING:
2879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
2881 num_connections = create_ring (pg, &add_allowed_connections);
2883 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
2887 num_connections = create_2d_torus (pg, &add_allowed_connections);
2889 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2892 _("Creating Erdos-Renyi topology\n"));
2894 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2896 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
2900 num_connections = create_nated_internet (pg, &add_allowed_connections);
2902 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2905 _("Creating Scale Free topology\n"));
2907 num_connections = create_scale_free (pg, &add_allowed_connections);
2909 case GNUNET_TESTING_TOPOLOGY_LINE:
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2912 _("Creating straight line topology\n"));
2914 num_connections = create_line (pg, &add_allowed_connections);
2916 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
2918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2919 _("Creating topology from file!\n"));
2922 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing", "topology_file",
2924 num_connections = create_from_file (pg, filename, &add_allowed_connections);
2927 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!");
2928 num_connections = 0;
2931 case GNUNET_TESTING_TOPOLOGY_NONE:
2933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2935 ("Creating no allowed topology (all peers can connect at core level)\n"));
2937 num_connections = 0;
2940 num_connections = 0;
2945 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2947 ret = create_and_copy_friend_files (pg);
2948 if (ret != GNUNET_OK)
2951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2952 _("Failed during friend file copying!\n"));
2954 return GNUNET_SYSERR;
2959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2960 _("Friend files created/copied successfully!\n"));
2965 /* Use the create clique method to initially set all connections as blacklisted. */
2966 create_clique (pg, &blacklist_connections);
2968 unblacklisted_connections = 0;
2969 /* Un-blacklist connections as per the topology specified */
2970 switch (restrict_topology)
2972 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2975 _("Blacklisting all but clique topology\n"));
2977 unblacklisted_connections =
2978 create_clique (pg, &unblacklist_connections);
2980 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2983 _("Blacklisting all but small world (ring) topology\n"));
2985 unblacklisted_connections =
2986 create_small_world_ring (pg, &unblacklist_connections);
2988 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2992 ("Blacklisting all but small world (2d-torus) topology\n"));
2994 unblacklisted_connections =
2995 create_small_world (pg, &unblacklist_connections);
2997 case GNUNET_TESTING_TOPOLOGY_RING:
2999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3000 _("Blacklisting all but ring topology\n"));
3002 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
3004 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3007 _("Blacklisting all but 2d torus topology\n"));
3009 unblacklisted_connections =
3010 create_2d_torus (pg, &unblacklist_connections);
3012 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3015 _("Blacklisting all but Erdos-Renyi topology\n"));
3017 unblacklisted_connections =
3018 create_erdos_renyi (pg, &unblacklist_connections);
3020 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3023 _("Blacklisting all but InterNAT topology\n"));
3025 unblacklisted_connections =
3026 create_nated_internet (pg, &unblacklist_connections);
3028 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3031 _("Blacklisting all but Scale Free topology\n"));
3033 unblacklisted_connections =
3034 create_scale_free (pg, &unblacklist_connections);
3036 case GNUNET_TESTING_TOPOLOGY_LINE:
3038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3039 _("Blacklisting all but straight line topology\n"));
3041 unblacklisted_connections = create_line (pg, &unblacklist_connections);
3043 case GNUNET_TESTING_TOPOLOGY_NONE:
3044 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3048 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
3050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3052 ("Creating blacklist topology from allowed\n"));
3053 unblacklisted_connections = copy_allowed (pg, &unblacklist_connections);
3058 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
3060 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'", restrict_transports);
3061 ret = create_and_copy_blacklist_files (pg, restrict_transports);
3062 if (ret != GNUNET_OK)
3065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3066 _("Failed during blacklist file copying!\n"));
3073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3074 _("Blacklist files created/copied successfully!\n"));
3078 return num_connections;
3081 struct RandomContext
3086 struct GNUNET_TESTING_PeerGroup *pg;
3089 * uid of the first peer
3094 * Peer data for first peer.
3096 struct PeerData *first;
3099 * Random percentage to use
3104 struct MinimumContext
3109 struct GNUNET_TESTING_PeerGroup *pg;
3112 * uid of the first peer
3117 * Peer data for first peer.
3119 struct PeerData *first;
3122 * Number of conns per peer
3124 unsigned int num_to_add;
3127 * Permuted array of all possible connections. Only add the Nth
3128 * peer if it's in the Nth position.
3130 unsigned int *pg_array;
3133 * What number is the current element we are iterating over?
3135 unsigned int current;
3143 struct GNUNET_TESTING_PeerGroup *pg;
3146 * uid of the first peer
3151 * uid of the second peer
3153 uint32_t second_uid;
3156 * Peer data for first peer.
3158 struct PeerData *first;
3161 * Which peer has been chosen as the one to add?
3163 unsigned int chosen;
3166 * What number is the current element we are iterating over?
3168 unsigned int current;
3172 * Iterator for choosing random peers to connect.
3174 * @param cls closure, a RandomContext
3175 * @param key the key the second Daemon was stored under
3176 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3178 * @return GNUNET_YES to continue iteration
3181 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3183 struct RandomContext *random_ctx = cls;
3184 double random_number;
3185 uint32_t second_pos;
3186 GNUNET_HashCode first_hash;
3189 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3190 UINT64_MAX)) / ((double) UINT64_MAX);
3191 if (random_number < random_ctx->percentage)
3193 GNUNET_assert (GNUNET_OK ==
3194 GNUNET_CONTAINER_multihashmap_put (random_ctx->
3195 first->connect_peers_working_set,
3197 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3199 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
3200 uid_from_hash (key, &second_pos);
3201 hash_from_uid (random_ctx->first_uid, &first_hash);
3202 GNUNET_assert (random_ctx->pg->total > second_pos);
3203 GNUNET_assert (GNUNET_YES ==
3204 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
3206 [second_pos].connect_peers,
3215 * Iterator for adding at least X peers to a peers connection set.
3217 * @param cls closure, MinimumContext
3218 * @param key the key the second Daemon was stored under
3219 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3221 * @return GNUNET_YES to continue iteration
3224 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3226 struct MinimumContext *min_ctx = cls;
3227 uint32_t second_pos;
3228 GNUNET_HashCode first_hash;
3231 if (GNUNET_CONTAINER_multihashmap_size
3232 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
3234 for (i = 0; i < min_ctx->num_to_add; i++)
3236 if (min_ctx->pg_array[i] == min_ctx->current)
3238 GNUNET_assert (GNUNET_OK ==
3239 GNUNET_CONTAINER_multihashmap_put
3240 (min_ctx->first->connect_peers_working_set, key,
3242 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3243 uid_from_hash (key, &second_pos);
3244 hash_from_uid (min_ctx->first_uid, &first_hash);
3245 GNUNET_assert (min_ctx->pg->total > second_pos);
3246 GNUNET_assert (GNUNET_OK ==
3247 GNUNET_CONTAINER_multihashmap_put (min_ctx->
3249 [second_pos].connect_peers_working_set,
3253 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3254 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
3255 GNUNET_assert (GNUNET_YES ==
3256 GNUNET_CONTAINER_multihashmap_remove
3257 (min_ctx->pg->peers[second_pos].connect_peers,
3258 &first_hash, min_ctx->first->daemon));
3265 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
3271 * Iterator for adding peers to a connection set based on a depth first search.
3273 * @param cls closure, MinimumContext
3274 * @param key the key the second daemon was stored under
3275 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3277 * @return GNUNET_YES to continue iteration
3280 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3282 struct DFSContext *dfs_ctx = cls;
3283 GNUNET_HashCode first_hash;
3285 if (dfs_ctx->current == dfs_ctx->chosen)
3287 GNUNET_assert (GNUNET_OK ==
3288 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3289 first->connect_peers_working_set,
3291 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3292 uid_from_hash (key, &dfs_ctx->second_uid);
3293 hash_from_uid (dfs_ctx->first_uid, &first_hash);
3294 GNUNET_assert (GNUNET_OK ==
3295 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3297 [dfs_ctx->second_uid].connect_peers_working_set,
3301 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3302 GNUNET_assert (GNUNET_YES ==
3303 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
3305 [dfs_ctx->second_uid].connect_peers,
3309 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
3310 return GNUNET_NO; /* We have found our peer, don't iterate more */
3319 * From the set of connections possible, choose percentage percent of connections
3320 * to actually connect.
3322 * @param pg the peergroup we are dealing with
3323 * @param percentage what percent of total connections to make
3326 choose_random_connections (struct GNUNET_TESTING_PeerGroup *pg,
3329 struct RandomContext random_ctx;
3332 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3334 random_ctx.first_uid = pg_iter;
3335 random_ctx.first = &pg->peers[pg_iter];
3336 random_ctx.percentage = percentage;
3338 pg->peers[pg_iter].connect_peers_working_set =
3339 GNUNET_CONTAINER_multihashmap_create (pg->total);
3340 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3341 &random_connect_iterator,
3343 /* Now remove the old connections */
3344 GNUNET_CONTAINER_multihashmap_destroy (pg->
3345 peers[pg_iter].connect_peers);
3346 /* And replace with the random set */
3347 pg->peers[pg_iter].connect_peers =
3348 pg->peers[pg_iter].connect_peers_working_set;
3353 * From the set of connections possible, choose at least num connections per
3356 * @param pg the peergroup we are dealing with
3357 * @param num how many connections at least should each peer have (if possible)?
3360 choose_minimum (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3362 struct MinimumContext minimum_ctx;
3365 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3367 pg->peers[pg_iter].connect_peers_working_set =
3368 GNUNET_CONTAINER_multihashmap_create (num);
3371 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3373 minimum_ctx.first_uid = pg_iter;
3374 minimum_ctx.pg_array =
3375 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
3376 GNUNET_CONTAINER_multihashmap_size
3377 (pg->peers[pg_iter].connect_peers));
3378 minimum_ctx.first = &pg->peers[pg_iter];
3379 minimum_ctx.pg = pg;
3380 minimum_ctx.num_to_add = num;
3381 minimum_ctx.current = 0;
3382 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3383 &minimum_connect_iterator,
3387 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3389 /* Remove the "old" connections */
3390 GNUNET_CONTAINER_multihashmap_destroy (pg->
3391 peers[pg_iter].connect_peers);
3392 /* And replace with the working set */
3393 pg->peers[pg_iter].connect_peers =
3394 pg->peers[pg_iter].connect_peers_working_set;
3401 count_workingset_connections (struct GNUNET_TESTING_PeerGroup *pg)
3404 unsigned int pg_iter;
3408 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3411 GNUNET_CONTAINER_multihashmap_size (pg->
3413 [pg_iter].connect_peers_working_set);
3421 count_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg)
3424 unsigned int pg_iter;
3428 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3431 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3438 struct FindClosestContext
3441 * The currently known closest peer.
3443 struct GNUNET_TESTING_Daemon *closest;
3446 * The info for the peer we are adding connections for.
3448 struct PeerData *curr_peer;
3451 * The distance (bits) between the current
3452 * peer and the currently known closest.
3454 unsigned int closest_dist;
3457 * The offset of the closest known peer in
3460 unsigned int closest_num;
3464 * Iterator over hash map entries of the allowed
3465 * peer connections. Find the closest, not already
3466 * connected peer and return it.
3468 * @param cls closure (struct FindClosestContext)
3469 * @param key current key code (hash of offset in pg)
3470 * @param value value in the hash map - a GNUNET_TESTING_Daemon
3471 * @return GNUNET_YES if we should continue to
3476 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
3478 struct FindClosestContext *closest_ctx = cls;
3479 struct GNUNET_TESTING_Daemon *daemon = value;
3481 if (((closest_ctx->closest == NULL) ||
3482 (GNUNET_CRYPTO_hash_matching_bits
3483 (&daemon->id.hashPubKey,
3484 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
3485 closest_ctx->closest_dist))
3487 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
3488 curr_peer->connect_peers,
3491 closest_ctx->closest_dist =
3492 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
3493 &closest_ctx->curr_peer->daemon->
3495 closest_ctx->closest = daemon;
3496 uid_from_hash (key, &closest_ctx->closest_num);
3502 * From the set of connections possible, choose at num connections per
3503 * peer based on depth which are closest out of those allowed. Guaranteed
3504 * to add num peers to connect to, provided there are that many peers
3505 * in the underlay topology to connect to.
3507 * @param pg the peergroup we are dealing with
3508 * @param num how many connections at least should each peer have (if possible)?
3509 * @param proc processor to actually add the connections
3512 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
3513 GNUNET_TESTING_ConnectionProcessor proc)
3515 struct FindClosestContext closest_ctx;
3519 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
3521 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3523 closest_ctx.curr_peer = &pg->peers[pg_iter];
3524 closest_ctx.closest = NULL;
3525 closest_ctx.closest_dist = 0;
3526 closest_ctx.closest_num = 0;
3527 GNUNET_CONTAINER_multihashmap_iterate (pg->
3528 peers[pg_iter].allowed_peers,
3529 &find_closest_peers,
3531 if (closest_ctx.closest != NULL)
3533 GNUNET_assert (closest_ctx.closest_num < pg->total);
3534 proc (pg, pg_iter, closest_ctx.closest_num);
3541 * From the set of connections possible, choose at least num connections per
3542 * peer based on depth first traversal of peer connections. If DFS leaves
3543 * peers unconnected, ensure those peers get connections.
3545 * @param pg the peergroup we are dealing with
3546 * @param num how many connections at least should each peer have (if possible)?
3549 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3551 struct DFSContext dfs_ctx;
3554 uint32_t starting_peer;
3555 uint32_t least_connections;
3556 GNUNET_HashCode second_hash;
3558 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3560 pg->peers[pg_iter].connect_peers_working_set =
3561 GNUNET_CONTAINER_multihashmap_create (num);
3566 while ((count_workingset_connections (pg) < num * pg->total)
3567 && (count_allowed_connections (pg) > 0))
3569 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3571 least_connections = -1; /* Set to very high number */
3572 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3574 if (GNUNET_CONTAINER_multihashmap_size
3575 (pg->peers[pg_iter].connect_peers_working_set) <
3578 starting_peer = pg_iter;
3580 GNUNET_CONTAINER_multihashmap_size (pg->
3582 [pg_iter].connect_peers_working_set);
3587 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
3593 /* Choose a random peer from the chosen peers set of connections to add */
3595 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3596 GNUNET_CONTAINER_multihashmap_size
3597 (pg->peers[starting_peer].connect_peers));
3598 dfs_ctx.first_uid = starting_peer;
3599 dfs_ctx.first = &pg->peers[starting_peer];
3601 dfs_ctx.current = 0;
3603 GNUNET_CONTAINER_multihashmap_iterate (pg->
3605 [starting_peer].connect_peers,
3606 &dfs_connect_iterator, &dfs_ctx);
3607 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
3608 hash_from_uid (dfs_ctx.second_uid, &second_hash);
3609 GNUNET_assert (GNUNET_YES ==
3610 GNUNET_CONTAINER_multihashmap_remove (pg->peers
3611 [starting_peer].connect_peers,
3615 [dfs_ctx.second_uid].daemon));
3616 starting_peer = dfs_ctx.second_uid;
3619 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3621 /* Remove the "old" connections */
3622 GNUNET_CONTAINER_multihashmap_destroy (pg->
3623 peers[pg_iter].connect_peers);
3624 /* And replace with the working set */
3625 pg->peers[pg_iter].connect_peers =
3626 pg->peers[pg_iter].connect_peers_working_set;
3631 * Internal callback for topology information for a particular peer.
3634 internal_topology_callback (void *cls,
3635 const struct GNUNET_PeerIdentity *peer,
3636 const struct GNUNET_TRANSPORT_ATS_Information
3639 struct CoreContext *core_ctx = cls;
3640 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
3642 if (peer == NULL) /* Either finished, or something went wrong */
3644 iter_ctx->completed++;
3645 iter_ctx->connected--;
3646 /* One core context allocated per iteration, must free! */
3647 GNUNET_free (core_ctx);
3651 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id,
3655 if (iter_ctx->completed == iter_ctx->total)
3657 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
3658 /* Once all are done, free the iteration context */
3659 GNUNET_free (iter_ctx);
3665 * Check running topology iteration tasks, if below max start a new one, otherwise
3666 * schedule for some time in the future.
3669 schedule_get_topology (void *cls,
3670 const struct GNUNET_SCHEDULER_TaskContext *tc)
3672 struct CoreContext *core_context = cls;
3673 struct TopologyIterateContext *topology_context =
3674 (struct TopologyIterateContext *) core_context->iter_context;
3675 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3678 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3680 #if VERBOSE_TESTING > 2
3681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3683 ("Delaying connect, we have too many outstanding connections!\n"));
3685 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3686 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3687 &schedule_get_topology, core_context);
3691 #if VERBOSE_TESTING > 2
3692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3693 _("Creating connection, outstanding_connections is %d\n"),
3694 outstanding_connects);
3696 topology_context->connected++;
3699 GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
3700 &internal_topology_callback,
3703 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
3704 internal_topology_callback (core_context, NULL, NULL);
3710 * Iterate over all (running) peers in the peer group, retrieve
3711 * all connections that each currently has.
3714 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
3715 GNUNET_TESTING_NotifyTopology cb, void *cls)
3717 struct TopologyIterateContext *topology_context;
3718 struct CoreContext *core_ctx;
3720 unsigned int total_count;
3722 /* Allocate a single topology iteration context */
3723 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
3724 topology_context->topology_cb = cb;
3725 topology_context->cls = cls;
3727 for (i = 0; i < pg->total; i++)
3729 if (pg->peers[i].daemon->running == GNUNET_YES)
3731 /* Allocate one core context per core we need to connect to */
3732 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
3733 core_ctx->daemon = pg->peers[i].daemon;
3734 /* Set back pointer to topology iteration context */
3735 core_ctx->iter_context = topology_context;
3736 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
3740 if (total_count == 0)
3742 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
3743 GNUNET_free (topology_context);
3746 topology_context->total = total_count;
3751 * Callback function to process statistic values.
3752 * This handler is here only really to insert a peer
3753 * identity (or daemon) so the statistics can be uniquely
3754 * tied to a single running peer.
3756 * @param cls closure
3757 * @param subsystem name of subsystem that created the statistic
3758 * @param name the name of the datum
3759 * @param value the current value
3760 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
3761 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
3764 internal_stats_callback (void *cls,
3765 const char *subsystem,
3766 const char *name, uint64_t value, int is_persistent)
3768 struct StatsCoreContext *core_context = cls;
3769 struct StatsIterateContext *stats_context =
3770 (struct StatsIterateContext *) core_context->iter_context;
3772 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
3773 subsystem, name, value, is_persistent);
3777 * Internal continuation call for statistics iteration.
3779 * @param cls closure, the CoreContext for this iteration
3780 * @param success whether or not the statistics iterations
3781 * was canceled or not (we don't care)
3784 internal_stats_cont (void *cls, int success)
3786 struct StatsCoreContext *core_context = cls;
3787 struct StatsIterateContext *stats_context =
3788 (struct StatsIterateContext *) core_context->iter_context;
3790 stats_context->connected--;
3791 stats_context->completed++;
3793 if (stats_context->completed == stats_context->total)
3795 stats_context->cont (stats_context->cls, GNUNET_YES);
3796 GNUNET_free (stats_context);
3799 if (core_context->stats_handle != NULL)
3800 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
3802 GNUNET_free (core_context);
3806 * Check running topology iteration tasks, if below max start a new one, otherwise
3807 * schedule for some time in the future.
3810 schedule_get_statistics (void *cls,
3811 const struct GNUNET_SCHEDULER_TaskContext *tc)
3813 struct StatsCoreContext *core_context = cls;
3814 struct StatsIterateContext *stats_context =
3815 (struct StatsIterateContext *) core_context->iter_context;
3817 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3820 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3822 #if VERBOSE_TESTING > 2
3823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3825 ("Delaying connect, we have too many outstanding connections!\n"));
3827 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3828 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3829 &schedule_get_statistics, core_context);
3833 #if VERBOSE_TESTING > 2
3834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3835 _("Creating connection, outstanding_connections is %d\n"),
3836 outstanding_connects);
3839 stats_context->connected++;
3840 core_context->stats_handle =
3841 GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
3842 if (core_context->stats_handle == NULL)
3844 internal_stats_cont (core_context, GNUNET_NO);
3848 core_context->stats_get_handle =
3849 GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
3850 GNUNET_TIME_relative_get_forever (),
3851 &internal_stats_cont, &internal_stats_callback,
3853 if (core_context->stats_get_handle == NULL)
3854 internal_stats_cont (core_context, GNUNET_NO);
3859 struct DuplicateStats
3862 * Next item in the list
3864 struct DuplicateStats *next;
3867 * Nasty string, concatenation of relevant information.
3869 char *unique_string;
3873 * Check whether the combination of port/host/unix domain socket
3874 * already exists in the list of peers being checked for statistics.
3876 * @param pg the peergroup in question
3877 * @param specific_peer the peer we're concerned with
3878 * @param stats_list the list to return to the caller
3880 * @return GNUNET_YES if the statistics instance has been seen already,
3881 * GNUNET_NO if not (and we may have added it to the list)
3884 stats_check_existing (struct GNUNET_TESTING_PeerGroup *pg,
3885 struct PeerData *specific_peer,
3886 struct DuplicateStats **stats_list)
3888 struct DuplicateStats *pos;
3889 char *unix_domain_socket;
3890 unsigned long long port;
3893 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
3894 "single_statistics_per_host"))
3895 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
3899 GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg, "statistics",
3900 "unixpath", &unix_domain_socket))
3904 GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg, "statistics",
3907 GNUNET_free(unix_domain_socket);
3911 if (specific_peer->daemon->hostname != NULL)
3912 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
3913 unix_domain_socket, port);
3915 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
3919 if (0 == strcmp (to_match, pos->unique_string))
3921 GNUNET_free (unix_domain_socket);
3922 GNUNET_free (to_match);
3927 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
3928 pos->unique_string = to_match;
3929 pos->next = *stats_list;
3931 GNUNET_free (unix_domain_socket);
3936 * Iterate over all (running) peers in the peer group, retrieve
3937 * all statistics from each.
3940 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
3941 GNUNET_STATISTICS_Callback cont,
3942 GNUNET_TESTING_STATISTICS_Iterator proc,
3945 struct StatsIterateContext *stats_context;
3946 struct StatsCoreContext *core_ctx;
3948 unsigned int total_count;
3949 struct DuplicateStats *stats_list;
3950 struct DuplicateStats *pos;
3953 /* Allocate a single stats iteration context */
3954 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
3955 stats_context->cont = cont;
3956 stats_context->proc = proc;
3957 stats_context->cls = cls;
3960 for (i = 0; i < pg->total; i++)
3962 if ((pg->peers[i].daemon->running == GNUNET_YES)
3964 stats_check_existing (pg, &pg->peers[i], &stats_list)))
3966 /* Allocate one core context per core we need to connect to */
3967 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
3968 core_ctx->daemon = pg->peers[i].daemon;
3969 /* Set back pointer to topology iteration context */
3970 core_ctx->iter_context = stats_context;
3971 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
3976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3977 "Retrieving stats from %u total instances.\n", total_count);
3978 stats_context->total = total_count;
3979 if (stats_list != NULL)
3984 GNUNET_free (pos->unique_string);
3985 stats_list = pos->next;
3987 pos = stats_list->next;
3994 * There are many ways to connect peers that are supported by this function.
3995 * To connect peers in the same topology that was created via the
3996 * GNUNET_TESTING_create_topology, the topology variable must be set to
3997 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
3998 * a new instance of that topology will be generated and attempted to be
3999 * connected. This could result in some connections being impossible,
4000 * because some topologies are non-deterministic.
4002 * @param pg the peer group struct representing the running peers
4003 * @param topology which topology to connect the peers in
4004 * @param options options for connecting the topology
4005 * @param option_modifier modifier for options that take a parameter
4006 * @param notify_callback notification to be called once all connections completed
4007 * @param notify_cls closure for notification callback
4009 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
4012 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
4013 enum GNUNET_TESTING_Topology topology,
4014 enum GNUNET_TESTING_TopologyOption options,
4015 double option_modifier,
4016 GNUNET_TESTING_NotifyCompletion
4017 notify_callback, void *notify_cls)
4021 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4022 #if VERBOSE_TOPOLOGY
4023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4024 _("Creating clique CONNECT topology\n"));
4026 create_clique (pg, &add_actual_connections);
4028 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4029 #if VERBOSE_TOPOLOGY
4030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4031 _("Creating small world (ring) CONNECT topology\n"));
4033 create_small_world_ring (pg, &add_actual_connections);
4035 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4036 #if VERBOSE_TOPOLOGY
4037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4038 _("Creating small world (2d-torus) CONNECT topology\n"));
4040 create_small_world (pg, &add_actual_connections);
4042 case GNUNET_TESTING_TOPOLOGY_RING:
4043 #if VERBOSE_TOPOLOGY
4044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4045 _("Creating ring CONNECT topology\n"));
4047 create_ring (pg, &add_actual_connections);
4049 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4050 #if VERBOSE_TOPOLOGY
4051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4052 _("Creating 2d torus CONNECT topology\n"));
4054 create_2d_torus (pg, &add_actual_connections);
4056 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4057 #if VERBOSE_TOPOLOGY
4058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4059 _("Creating Erdos-Renyi CONNECT topology\n"));
4061 create_erdos_renyi (pg, &add_actual_connections);
4063 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4064 #if VERBOSE_TOPOLOGY
4065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4066 _("Creating InterNAT CONNECT topology\n"));
4068 create_nated_internet (pg, &add_actual_connections);
4070 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4071 #if VERBOSE_TOPOLOGY
4072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4073 _("Creating Scale Free CONNECT topology\n"));
4075 create_scale_free (pg, &add_actual_connections);
4077 case GNUNET_TESTING_TOPOLOGY_LINE:
4078 #if VERBOSE_TOPOLOGY
4079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4080 _("Creating straight line CONNECT topology\n"));
4082 create_line (pg, &add_actual_connections);
4084 case GNUNET_TESTING_TOPOLOGY_NONE:
4085 #if VERBOSE_TOPOLOGY
4086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4087 _("Creating no CONNECT topology\n"));
4089 copy_allowed_topology (pg);
4092 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4094 ("Unknown topology specification, can't connect peers!\n"));
4095 return GNUNET_SYSERR;
4100 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
4101 #if VERBOSE_TOPOLOGY
4102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4104 ("Connecting random subset (%'.2f percent) of possible peers\n"),
4105 100 * option_modifier);
4107 choose_random_connections (pg, option_modifier);
4109 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
4110 #if VERBOSE_TOPOLOGY
4111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4112 _("Connecting a minimum of %u peers each (if possible)\n"),
4113 (unsigned int) option_modifier);
4115 choose_minimum (pg, (unsigned int) option_modifier);
4117 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
4118 #if VERBOSE_TOPOLOGY
4119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4121 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
4122 (unsigned int) option_modifier);
4124 perform_dfs (pg, (int) option_modifier);
4126 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
4127 #if VERBOSE_TOPOLOGY
4128 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4130 ("Finding additional %u closest peers each (if possible)\n"),
4131 (unsigned int) option_modifier);
4133 add_closest (pg, (unsigned int) option_modifier,
4134 &add_actual_connections);
4136 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
4138 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
4144 return connect_topology (pg, notify_callback, notify_cls);
4148 * Callback that is called whenever a hostkey is generated
4149 * for a peer. Call the real callback and decrement the
4150 * starting counter for the peergroup.
4152 * @param cls closure
4153 * @param id identifier for the daemon, NULL on error
4154 * @param d handle for the daemon
4155 * @param emsg error message (NULL on success)
4158 internal_hostkey_callback (void *cls,
4159 const struct GNUNET_PeerIdentity *id,
4160 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4162 struct InternalStartContext *internal_context = cls;
4163 internal_context->peer->pg->starting--;
4164 internal_context->peer->pg->started++;
4165 if (internal_context->hostkey_callback != NULL)
4166 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
4168 else if (internal_context->peer->pg->started ==
4169 internal_context->peer->pg->total)
4171 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
4172 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
4177 * Callback that is called whenever a peer has finished starting.
4178 * Call the real callback and decrement the starting counter
4179 * for the peergroup.
4181 * @param cls closure
4182 * @param id identifier for the daemon, NULL on error
4184 * @param d handle for the daemon
4185 * @param emsg error message (NULL on success)
4188 internal_startup_callback (void *cls,
4189 const struct GNUNET_PeerIdentity *id,
4190 const struct GNUNET_CONFIGURATION_Handle *cfg,
4191 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4193 struct InternalStartContext *internal_context = cls;
4194 internal_context->peer->pg->starting--;
4195 if (internal_context->start_cb != NULL)
4196 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
4201 internal_continue_startup (void *cls,
4202 const struct GNUNET_SCHEDULER_TaskContext *tc)
4204 struct InternalStartContext *internal_context = cls;
4206 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4211 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
4213 internal_context->peer->pg->starting++;
4214 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
4218 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4219 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4220 &internal_continue_startup,
4227 * Callback for informing us about a successful
4228 * or unsuccessful churn start call.
4230 * @param cls a ChurnContext
4231 * @param id the peer identity of the started peer
4232 * @param cfg the handle to the configuration of the peer
4233 * @param d handle to the daemon for the peer
4234 * @param emsg NULL on success, non-NULL on failure
4238 churn_start_callback (void *cls,
4239 const struct GNUNET_PeerIdentity *id,
4240 const struct GNUNET_CONFIGURATION_Handle *cfg,
4241 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4243 struct ChurnRestartContext *startup_ctx = cls;
4244 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
4246 unsigned int total_left;
4247 char *error_message;
4249 error_message = NULL;
4252 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4253 "Churn stop callback failed with error `%s'\n", emsg);
4254 churn_ctx->num_failed_start++;
4258 churn_ctx->num_to_start--;
4262 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4263 "Started peer, %d left.\n", churn_ctx->num_to_start);
4267 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
4268 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4270 if (total_left == 0)
4272 if ((churn_ctx->num_failed_stop > 0)
4273 || (churn_ctx->num_failed_start > 0))
4274 GNUNET_asprintf (&error_message,
4275 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4276 churn_ctx->num_failed_start,
4277 churn_ctx->num_failed_stop);
4278 churn_ctx->cb (churn_ctx->cb_cls, error_message);
4279 GNUNET_free_non_null (error_message);
4280 GNUNET_free (churn_ctx);
4281 GNUNET_free (startup_ctx);
4287 schedule_churn_restart (void *cls,
4288 const struct GNUNET_SCHEDULER_TaskContext *tc)
4290 struct PeerRestartContext *peer_restart_ctx = cls;
4291 struct ChurnRestartContext *startup_ctx =
4292 peer_restart_ctx->churn_restart_ctx;
4294 if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
4295 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4296 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4297 &schedule_churn_restart, peer_restart_ctx);
4300 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
4301 startup_ctx->timeout,
4302 &churn_start_callback,
4304 GNUNET_free (peer_restart_ctx);
4309 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4311 struct InternalStartContext *internal_context = cls;
4313 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4318 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
4320 internal_context->peer->pg->starting++;
4321 internal_context->peer->daemon =
4322 GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
4323 internal_context->timeout,
4324 internal_context->hostname,
4325 internal_context->username,
4326 internal_context->sshport,
4327 &internal_hostkey_callback,
4329 &internal_startup_callback,
4334 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4335 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4336 &internal_start, internal_context);
4341 * Function which continues a peer group starting up
4342 * after successfully generating hostkeys for each peer.
4344 * @param pg the peer group to continue starting
4348 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg)
4353 for (i = 0; i < pg->total; i++)
4355 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
4356 &pg->peers[i].internal_context);
4357 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
4362 * Start count gnunet instances with the same set of transports and
4363 * applications. The port numbers (any option called "PORT") will be
4364 * adjusted to ensure that no two peers running on the same system
4365 * have the same port(s) in their respective configurations.
4367 * @param cfg configuration template to use
4368 * @param total number of daemons to start
4369 * @param timeout total time allowed for peers to start
4370 * @param hostkey_callback function to call on each peers hostkey generation
4371 * if NULL, peers will be started by this call, if non-null,
4372 * GNUNET_TESTING_daemons_continue_startup must be called after
4373 * successful hostkey generation
4374 * @param hostkey_cls closure for hostkey callback
4375 * @param cb function to call on each daemon that was started
4376 * @param cb_cls closure for cb
4377 * @param connect_callback function to call each time two hosts are connected
4378 * @param connect_callback_cls closure for connect_callback
4379 * @param hostnames linked list of host structs to use to start peers on
4380 * (NULL to run on localhost only)
4382 * @return NULL on error, otherwise handle to control peer group
4384 struct GNUNET_TESTING_PeerGroup *
4385 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
4387 struct GNUNET_TIME_Relative timeout,
4388 GNUNET_TESTING_NotifyHostkeyCreated
4389 hostkey_callback, void *hostkey_cls,
4390 GNUNET_TESTING_NotifyDaemonRunning cb,
4392 GNUNET_TESTING_NotifyConnection
4393 connect_callback, void *connect_callback_cls,
4394 const struct GNUNET_TESTING_Host *hostnames)
4396 struct GNUNET_TESTING_PeerGroup *pg;
4397 const struct GNUNET_TESTING_Host *hostpos;
4403 const char *hostname;
4404 const char *username;
4405 char *baseservicehome;
4406 char *newservicehome;
4408 struct GNUNET_CONFIGURATION_Handle *pcfg;
4410 unsigned int hostcnt;
4423 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
4425 pg->notify_connection = connect_callback;
4426 pg->notify_connection_cls = connect_callback_cls;
4428 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
4429 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
4430 if (NULL != hostnames)
4433 hostpos = hostnames;
4434 while (hostpos != NULL)
4436 hostpos = hostpos->next;
4439 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4442 hostpos = hostnames;
4443 while (hostpos != NULL)
4445 pg->hosts[off].minport = LOW_PORT;
4446 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
4447 if (hostpos->username != NULL)
4448 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
4449 pg->hosts[off].sshport = hostpos->port;
4450 hostpos = hostpos->next;
4460 pg->num_hosts = off;
4464 /* skip leading spaces */
4465 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
4468 while ('\0' != *rpos)
4470 if (isspace ((unsigned char) *rpos))
4474 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4476 start = GNUNET_strdup (hostnames);
4478 while ('\0' != *pos)
4480 if (isspace ((unsigned char) *pos))
4483 if (strlen (start) > 0)
4485 pg->hosts[off].minport = LOW_PORT;
4486 pg->hosts[off++].hostname = start;
4492 if (strlen (start) > 0)
4494 pg->hosts[off].minport = LOW_PORT;
4495 pg->hosts[off++].hostname = start;
4499 GNUNET_free (start);
4500 GNUNET_free (pg->hosts);
4504 minport = 0; /* make gcc happy */
4512 for (off = 0; off < total; off++)
4516 hostname = pg->hosts[off % hostcnt].hostname;
4517 username = pg->hosts[off % hostcnt].username;
4518 sshport = pg->hosts[off % hostcnt].sshport;
4519 pcfg = make_config (cfg,
4520 &pg->hosts[off % hostcnt].minport,
4521 &upnum, hostname, &fdnum);
4528 pcfg = make_config (cfg, &minport, &upnum, hostname, &fdnum);
4533 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4535 ("Could not create configuration for peer number %u on `%s'!\n"),
4536 off, hostname == NULL ? "localhost" : hostname);
4541 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
4544 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
4545 GNUNET_free (baseservicehome);
4549 tmpdir = getenv ("TMPDIR");
4550 tmpdir = tmpdir ? tmpdir : "/tmp";
4551 GNUNET_asprintf (&newservicehome,
4553 tmpdir, "gnunet-testing-test-test", off);
4555 GNUNET_CONFIGURATION_set_value_string (pcfg,
4557 "SERVICEHOME", newservicehome);
4558 GNUNET_free (newservicehome);
4559 pg->peers[off].cfg = pcfg;
4560 pg->peers[off].allowed_peers =
4561 GNUNET_CONTAINER_multihashmap_create (total);
4562 pg->peers[off].connect_peers =
4563 GNUNET_CONTAINER_multihashmap_create (total);
4564 pg->peers[off].blacklisted_peers =
4565 GNUNET_CONTAINER_multihashmap_create (total);
4566 pg->peers[off].pg = pg;
4568 pg->peers[off].internal_context.peer = &pg->peers[off];
4569 pg->peers[off].internal_context.timeout = timeout;
4570 pg->peers[off].internal_context.hostname = hostname;
4571 pg->peers[off].internal_context.username = username;
4572 pg->peers[off].internal_context.sshport = sshport;
4573 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
4574 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
4575 pg->peers[off].internal_context.start_cb = cb;
4576 pg->peers[off].internal_context.start_cb_cls = cb_cls;
4578 GNUNET_SCHEDULER_add_now (&internal_start,
4579 &pg->peers[off].internal_context);
4586 * Get a daemon by number, so callers don't have to do nasty
4587 * offsetting operation.
4589 struct GNUNET_TESTING_Daemon *
4590 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
4591 unsigned int position)
4593 if (position < pg->total)
4594 return pg->peers[position].daemon;
4600 * Get a daemon by peer identity, so callers can
4601 * retrieve the daemon without knowing it's offset.
4603 * @param pg the peer group to retrieve the daemon from
4604 * @param peer_id the peer identity of the daemon to retrieve
4606 * @return the daemon on success, or NULL if no such peer identity is found
4608 struct GNUNET_TESTING_Daemon *
4609 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
4610 struct GNUNET_PeerIdentity *peer_id)
4614 for (i = 0; i < pg->total; i++)
4617 memcmp (&pg->peers[i].daemon->id, peer_id,
4618 sizeof (struct GNUNET_PeerIdentity)))
4619 return pg->peers[i].daemon;
4626 * Prototype of a function that will be called when a
4627 * particular operation was completed the testing library.
4629 * @param cls closure (a struct RestartContext)
4630 * @param id id of the peer that was restarted
4631 * @param cfg handle to the configuration of the peer
4632 * @param d handle to the daemon that was restarted
4633 * @param emsg NULL on success
4636 restart_callback (void *cls,
4637 const struct GNUNET_PeerIdentity *id,
4638 const struct GNUNET_CONFIGURATION_Handle *cfg,
4639 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4641 struct RestartContext *restart_context = cls;
4645 restart_context->peers_restarted++;
4649 restart_context->peers_restart_failed++;
4652 if (restart_context->peers_restarted == restart_context->peer_group->total)
4654 restart_context->callback (restart_context->callback_cls, NULL);
4655 GNUNET_free (restart_context);
4657 else if (restart_context->peers_restart_failed +
4658 restart_context->peers_restarted ==
4659 restart_context->peer_group->total)
4661 restart_context->callback (restart_context->callback_cls,
4662 "Failed to restart peers!");
4663 GNUNET_free (restart_context);
4669 * Callback for informing us about a successful
4670 * or unsuccessful churn stop call.
4672 * @param cls a ChurnContext
4673 * @param emsg NULL on success, non-NULL on failure
4677 churn_stop_callback (void *cls, const char *emsg)
4679 struct ShutdownContext *shutdown_ctx = cls;
4680 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
4681 unsigned int total_left;
4682 char *error_message;
4684 error_message = NULL;
4685 shutdown_ctx->outstanding--;
4689 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4690 "Churn stop callback failed with error `%s'\n", emsg);
4691 churn_ctx->num_failed_stop++;
4695 churn_ctx->num_to_stop--;
4699 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4700 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
4703 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
4704 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4706 if (total_left == 0)
4708 if ((churn_ctx->num_failed_stop > 0)
4709 || (churn_ctx->num_failed_start > 0))
4711 GNUNET_asprintf (&error_message,
4712 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4713 churn_ctx->num_failed_start,
4714 churn_ctx->num_failed_stop);
4716 churn_ctx->cb (churn_ctx->cb_cls, error_message);
4717 GNUNET_free_non_null (error_message);
4718 GNUNET_free (churn_ctx);
4719 GNUNET_free (shutdown_ctx);
4724 * Count the number of running peers.
4726 * @param pg handle for the peer group
4728 * @return the number of currently running peers in the peer group
4731 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
4734 unsigned int running = 0;
4735 for (i = 0; i < pg->total; i++)
4737 if (pg->peers[i].daemon->running == GNUNET_YES)
4739 GNUNET_assert (running != -1);
4747 * Task to rate limit the number of outstanding peer shutdown
4748 * requests. This is necessary for making sure we don't do
4749 * too many ssh connections at once, but is generally nicer
4750 * to any system as well (graduated task starts, as opposed
4751 * to calling gnunet-arm N times all at once).
4754 schedule_churn_shutdown_task (void *cls,
4755 const struct GNUNET_SCHEDULER_TaskContext *tc)
4757 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4758 struct ShutdownContext *shutdown_ctx;
4760 GNUNET_assert (peer_shutdown_ctx != NULL);
4761 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4762 GNUNET_assert (shutdown_ctx != NULL);
4764 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4765 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4766 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4767 &schedule_churn_shutdown_task,
4771 shutdown_ctx->outstanding++;
4772 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
4773 shutdown_ctx->timeout, shutdown_ctx->cb,
4774 shutdown_ctx, GNUNET_NO, GNUNET_YES);
4775 GNUNET_free (peer_shutdown_ctx);
4780 * Simulate churn by stopping some peers (and possibly
4781 * re-starting others if churn is called multiple times). This
4782 * function can only be used to create leave-join churn (peers "never"
4783 * leave for good). First "voff" random peers that are currently
4784 * online will be taken offline; then "von" random peers that are then
4785 * offline will be put back online. No notifications will be
4786 * generated for any of these operations except for the callback upon
4789 * @param pg handle for the peer group
4790 * @param voff number of peers that should go offline
4791 * @param von number of peers that should come back online;
4792 * must be zero on first call (since "testbed_start"
4793 * always starts all of the peers)
4794 * @param timeout how long to wait for operations to finish before
4796 * @param cb function to call at the end
4797 * @param cb_cls closure for cb
4800 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
4803 struct GNUNET_TIME_Relative timeout,
4804 GNUNET_TESTING_NotifyCompletion cb,
4807 struct ChurnContext *churn_ctx;
4808 struct ShutdownContext *shutdown_ctx;
4809 struct PeerShutdownContext *peer_shutdown_ctx;
4810 struct PeerRestartContext *peer_restart_ctx;
4811 struct ChurnRestartContext *churn_startup_ctx;
4813 unsigned int running;
4814 unsigned int stopped;
4815 unsigned int total_running;
4816 unsigned int total_stopped;
4818 unsigned int *running_arr;
4819 unsigned int *stopped_arr;
4820 unsigned int *running_permute;
4821 unsigned int *stopped_permute;
4826 if ((von == 0) && (voff == 0)) /* No peers at all? */
4832 for (i = 0; i < pg->total; i++)
4834 if (pg->peers[i].daemon->running == GNUNET_YES)
4836 GNUNET_assert (running != -1);
4841 GNUNET_assert (stopped != -1);
4848 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4849 "Trying to stop more peers than are currently running!\n");
4850 cb (cb_cls, "Trying to stop more peers than are currently running!");
4856 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4857 "Trying to start more peers than are currently stopped!\n");
4858 cb (cb_cls, "Trying to start more peers than are currently stopped!");
4862 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
4866 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
4870 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
4872 running_permute = NULL;
4873 stopped_permute = NULL;
4877 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, running);
4880 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, stopped);
4882 total_running = running;
4883 total_stopped = stopped;
4887 churn_ctx->num_to_start = von;
4888 churn_ctx->num_to_stop = voff;
4890 churn_ctx->cb_cls = cb_cls;
4892 for (i = 0; i < pg->total; i++)
4894 if (pg->peers[i].daemon->running == GNUNET_YES)
4896 GNUNET_assert ((running_arr != NULL) && (total_running > running));
4897 running_arr[running] = i;
4902 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
4903 stopped_arr[stopped] = i;
4908 GNUNET_assert (running >= voff);
4911 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
4912 shutdown_ctx->cb = &churn_stop_callback;
4913 shutdown_ctx->cb_cls = churn_ctx;
4914 shutdown_ctx->total_peers = voff;
4915 shutdown_ctx->timeout = timeout;
4918 for (i = 0; i < voff; i++)
4921 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
4922 running_permute[i]);
4924 GNUNET_assert (running_arr != NULL);
4925 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
4926 peer_shutdown_ctx->daemon =
4927 pg->peers[running_arr[running_permute[i]]].daemon;
4928 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4929 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
4933 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
4935 &churn_stop_callback, churn_ctx,
4936 GNUNET_NO, GNUNET_YES); */
4939 GNUNET_assert (stopped >= von);
4942 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
4943 churn_startup_ctx->churn_ctx = churn_ctx;
4944 churn_startup_ctx->timeout = timeout;
4946 for (i = 0; i < von; i++)
4949 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
4950 stopped_permute[i]);
4952 GNUNET_assert (stopped_arr != NULL);
4953 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
4954 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
4955 peer_restart_ctx->daemon =
4956 pg->peers[stopped_arr[stopped_permute[i]]].daemon;
4957 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
4959 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
4960 timeout, &churn_start_callback, churn_ctx); */
4963 GNUNET_free_non_null (running_arr);
4964 GNUNET_free_non_null (stopped_arr);
4965 GNUNET_free_non_null (running_permute);
4966 GNUNET_free_non_null (stopped_permute);
4971 * Restart all peers in the given group.
4973 * @param pg the handle to the peer group
4974 * @param callback function to call on completion (or failure)
4975 * @param callback_cls closure for the callback function
4978 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
4979 GNUNET_TESTING_NotifyCompletion callback,
4982 struct RestartContext *restart_context;
4987 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
4988 restart_context->peer_group = pg;
4989 restart_context->peers_restarted = 0;
4990 restart_context->callback = callback;
4991 restart_context->callback_cls = callback_cls;
4993 for (off = 0; off < pg->total; off++)
4995 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
4996 &restart_callback, restart_context);
5002 * Start or stop an individual peer from the given group.
5004 * @param pg handle to the peer group
5005 * @param offset which peer to start or stop
5006 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
5007 * @param timeout how long to wait for shutdown
5008 * @param cb function to call at the end
5009 * @param cb_cls closure for cb
5012 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
5013 unsigned int offset,
5015 struct GNUNET_TIME_Relative timeout,
5016 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5018 struct ShutdownContext *shutdown_ctx;
5019 struct ChurnRestartContext *startup_ctx;
5020 struct ChurnContext *churn_ctx;
5022 if (GNUNET_NO == desired_status)
5024 if (NULL != pg->peers[offset].daemon)
5026 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5027 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5028 churn_ctx->num_to_start = 0;
5029 churn_ctx->num_to_stop = 1;
5031 churn_ctx->cb_cls = cb_cls;
5032 shutdown_ctx->cb_cls = churn_ctx;
5033 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon,
5034 timeout, &churn_stop_callback,
5035 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5038 else if (GNUNET_YES == desired_status)
5040 if (NULL == pg->peers[offset].daemon)
5042 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
5043 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5044 churn_ctx->num_to_start = 1;
5045 churn_ctx->num_to_stop = 0;
5047 churn_ctx->cb_cls = cb_cls;
5048 startup_ctx->churn_ctx = churn_ctx;
5049 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
5050 timeout, &churn_start_callback,
5060 * Callback for shutting down peers in a peer group.
5062 * @param cls closure (struct ShutdownContext)
5063 * @param emsg NULL on success
5066 internal_shutdown_callback (void *cls, const char *emsg)
5068 struct ShutdownContext *shutdown_ctx = cls;
5070 shutdown_ctx->outstanding--;
5073 shutdown_ctx->peers_down++;
5077 shutdown_ctx->peers_failed++;
5080 if ((shutdown_ctx->cb != NULL)
5081 && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed ==
5082 shutdown_ctx->total_peers))
5084 if (shutdown_ctx->peers_failed > 0)
5085 shutdown_ctx->cb (shutdown_ctx->cb_cls,
5086 "Not all peers successfully shut down!");
5088 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
5089 GNUNET_free (shutdown_ctx);
5095 * Task to rate limit the number of outstanding peer shutdown
5096 * requests. This is necessary for making sure we don't do
5097 * too many ssh connections at once, but is generally nicer
5098 * to any system as well (graduated task starts, as opposed
5099 * to calling gnunet-arm N times all at once).
5102 schedule_shutdown_task (void *cls,
5103 const struct GNUNET_SCHEDULER_TaskContext *tc)
5105 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5106 struct ShutdownContext *shutdown_ctx;
5108 GNUNET_assert (peer_shutdown_ctx != NULL);
5109 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5110 GNUNET_assert (shutdown_ctx != NULL);
5112 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
5113 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5114 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5115 &schedule_shutdown_task, peer_shutdown_ctx);
5118 shutdown_ctx->outstanding++;
5119 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5120 shutdown_ctx->timeout,
5121 &internal_shutdown_callback, shutdown_ctx,
5122 GNUNET_YES, GNUNET_NO);
5123 GNUNET_free (peer_shutdown_ctx);
5128 * Shutdown all peers started in the given group.
5130 * @param pg handle to the peer group
5131 * @param timeout how long to wait for shutdown
5132 * @param cb callback to notify upon success or failure
5133 * @param cb_cls closure for cb
5136 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
5137 struct GNUNET_TIME_Relative timeout,
5138 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5141 struct ShutdownContext *shutdown_ctx;
5142 struct PeerShutdownContext *peer_shutdown_ctx;
5144 GNUNET_assert (pg->total > 0);
5146 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5147 shutdown_ctx->cb = cb;
5148 shutdown_ctx->cb_cls = cb_cls;
5149 shutdown_ctx->total_peers = pg->total;
5150 shutdown_ctx->timeout = timeout;
5151 /* shtudown_ctx->outstanding = 0; */
5153 for (off = 0; off < pg->total; off++)
5155 GNUNET_assert (NULL != pg->peers[off].daemon);
5156 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
5157 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
5158 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
5159 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
5160 //GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
5161 if (NULL != pg->peers[off].cfg)
5162 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
5163 if (pg->peers[off].allowed_peers != NULL)
5164 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
5165 if (pg->peers[off].connect_peers != NULL)
5166 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
5167 if (pg->peers[off].blacklisted_peers != NULL)
5168 GNUNET_CONTAINER_multihashmap_destroy (pg->
5169 peers[off].blacklisted_peers);
5171 GNUNET_free (pg->peers);
5172 for (off = 0; off < pg->num_hosts; off++)
5174 GNUNET_free (pg->hosts[off].hostname);
5175 GNUNET_free_non_null (pg->hosts[off].username);
5177 GNUNET_free_non_null (pg->hosts);
5182 /* end of testing_group.c */