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_YES
35 #define VERBOSE_TOPOLOGY GNUNET_YES
37 #define DEBUG_CHURN GNUNET_NO
42 * Lowest port used for GNUnet testing. Should be high enough to not
43 * conflict with other applications running on the hosts but be low
44 * enough to not conflict with client-ports (typically starting around
47 #define LOW_PORT 10000
50 * Highest port used for GNUnet testing. Should be low enough to not
51 * conflict with the port range for "local" ports (client apps; see
52 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
54 #define HIGH_PORT 56000
56 #define MAX_OUTSTANDING_CONNECTIONS 200
58 #define MAX_CONCURRENT_HOSTKEYS 500
60 #define MAX_CONCURRENT_STARTING 200
62 #define MAX_CONCURRENT_SHUTDOWN 200
64 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 180)
66 #define CONNECT_ATTEMPTS 30
69 * Which list of peers do we need to modify?
73 /** Modify allowed peers */
76 /** Modify connect peers */
79 /** Modify blacklist peers */
82 /** Modify workingset peers */
87 * Prototype of a function called whenever two peers would be connected
88 * in a certain topology.
90 typedef unsigned int (*GNUNET_TESTING_ConnectionProcessor) (struct
91 GNUNET_TESTING_PeerGroup
101 * Context for handling churning a peer group
106 * Callback used to notify of churning finished
108 GNUNET_TESTING_NotifyCompletion cb;
111 * Closure for callback
116 * Number of peers that still need to be started
118 unsigned int num_to_start;
121 * Number of peers that still need to be stopped
123 unsigned int num_to_stop;
126 * Number of peers that failed to start
128 unsigned int num_failed_start;
131 * Number of peers that failed to stop
133 unsigned int num_failed_stop;
136 struct RestartContext
139 * The group of peers being restarted
141 struct GNUNET_TESTING_PeerGroup *peer_group;
144 * How many peers have been restarted thus far
146 unsigned int peers_restarted;
149 * How many peers got an error when restarting
151 unsigned int peers_restart_failed;
154 * The function to call once all peers have been restarted
156 GNUNET_TESTING_NotifyCompletion callback;
159 * Closure for callback function
166 struct ShutdownContext
169 * Total peers to wait for
171 unsigned int total_peers;
174 * Number of peers successfully shut down
176 unsigned int peers_down;
179 * Number of peers failed to shut down
181 unsigned int peers_failed;
184 * Number of peers we have started shutting
185 * down. If too many, wait on them.
187 unsigned int outstanding;
190 * Timeout for shutdown.
192 struct GNUNET_TIME_Relative timeout;
195 * Callback to call when all peers either
196 * shutdown or failed to shutdown
198 GNUNET_TESTING_NotifyCompletion cb;
207 * Individual shutdown context for a particular peer.
209 struct PeerShutdownContext
212 * Pointer to the high level shutdown context.
214 struct ShutdownContext *shutdown_ctx;
217 * The daemon handle for the peer to shut down.
219 struct GNUNET_TESTING_Daemon *daemon;
223 * Individual shutdown context for a particular peer.
225 struct PeerRestartContext
228 * Pointer to the high level restart context.
230 struct ChurnRestartContext *churn_restart_ctx;
233 * The daemon handle for the peer to shut down.
235 struct GNUNET_TESTING_Daemon *daemon;
239 struct CreateTopologyContext
243 * Function to call with number of connections
245 GNUNET_TESTING_NotifyConnections cont;
248 * Closure for connection notification
255 /** Waiting to read number of peers */
258 /** Should find next peer index */
261 /** Should find colon */
264 /** Should read other peer index, space, or endline */
270 struct PeerConnection
275 struct PeerConnection *prev;
280 struct PeerConnection *next;
284 * Index of daemon in pg->peers
291 struct InternalStartContext
294 * Pointer to peerdata
296 struct PeerData *peer;
299 * Timeout for peer startup
301 struct GNUNET_TIME_Relative timeout;
304 * Client callback for hostkey notification
306 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
309 * Closure for hostkey_callback
314 * Client callback for peer start notification
316 GNUNET_TESTING_NotifyDaemonRunning start_cb;
324 * Hostname, where to start the peer
326 const char *hostname;
329 * Username to use when connecting to the
332 const char *username;
335 * Pointer to starting memory location of a hostkey
340 * Port to use for ssh.
346 struct ChurnRestartContext
349 * Number of restarts currently in flight.
351 unsigned int outstanding;
354 * Handle to the underlying churn context.
356 struct ChurnContext *churn_ctx;
359 * How long to allow the operation to take.
361 struct GNUNET_TIME_Relative timeout;
365 * Data we keep per peer.
370 * (Initial) configuration of the host.
371 * (initial because clients could change
372 * it and we would not know about those
375 struct GNUNET_CONFIGURATION_Handle *cfg;
378 * Handle for controlling the daemon.
380 struct GNUNET_TESTING_Daemon *daemon;
383 * The peergroup this peer belongs to.
385 struct GNUNET_TESTING_PeerGroup *pg;
389 * Linked list of allowed peer connections.
391 struct PeerConnection *allowed_peers_head;
394 * Linked list of allowed peer connections.
396 struct PeerConnection *allowed_peers_tail;
399 * Linked list of blacklisted peer connections.
401 struct PeerConnection *blacklisted_peers_head;
404 * Linked list of blacklisted peer connections.
406 struct PeerConnection *blacklisted_peers_tail;
409 * Linked list of connect peer connections.
411 struct PeerConnection *connect_peers_head;
414 * Linked list of connect peer connections.
416 struct PeerConnection *connect_peers_tail;
419 * Linked list of connect peer connections.
421 struct PeerConnection *connect_peers_working_set_head;
424 * Linked list of connect peer connections.
426 struct PeerConnection *connect_peers_working_set_tail;
430 * Hash map of allowed peer connections (F2F created topology)
432 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
435 * Hash map of blacklisted peers
437 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
440 * Hash map of peer connections
442 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
445 * Temporary hash map of peer connections
447 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
451 * Temporary variable for topology creation, should be reset before
452 * creating any topology so the count is valid once finished.
457 * Context to keep track of peers being started, to
458 * stagger hostkey generation and peer startup.
460 struct InternalStartContext internal_context;
465 * Linked list of per-host data.
475 * SSH username to use when connecting to this host.
480 * SSH port to use when connecting to this host.
485 * Lowest port that we have not yet used
491 struct TopologyIterateContext
494 * Callback for notifying of two connected peers.
496 GNUNET_TESTING_NotifyTopology topology_cb;
499 * Closure for topology_cb
504 * Number of peers currently connected to.
506 unsigned int connected;
509 * Number of peers we have finished iterating.
511 unsigned int completed;
514 * Number of peers total.
519 struct StatsIterateContext
522 * Continuation to call once all stats information has been retrieved.
524 GNUNET_STATISTICS_Callback cont;
527 * Proc function to call on each value received.
529 GNUNET_TESTING_STATISTICS_Iterator proc;
532 * Closure for topology_cb
537 * Number of peers currently connected to.
539 unsigned int connected;
542 * Number of peers we have finished iterating.
544 unsigned int completed;
547 * Number of peers total.
555 struct GNUNET_TESTING_Daemon *daemon;
558 struct StatsCoreContext
561 struct GNUNET_TESTING_Daemon *daemon;
563 * Handle to the statistics service.
565 struct GNUNET_STATISTICS_Handle *stats_handle;
568 * Handle for getting statistics.
570 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
574 * Handle to a group of GNUnet peers.
576 struct GNUNET_TESTING_PeerGroup
579 * Configuration template.
581 const struct GNUNET_CONFIGURATION_Handle *cfg;
584 * Function to call on each started daemon.
586 //GNUNET_TESTING_NotifyDaemonRunning cb;
594 * Function to call on each topology connection created
596 GNUNET_TESTING_NotifyConnection notify_connection;
599 * Callback for notify_connection
601 void *notify_connection_cls;
604 * Array of information about hosts.
606 struct HostData *hosts;
609 * Number of hosts (size of HostData)
611 unsigned int num_hosts;
614 * Array of "total" peers.
616 struct PeerData *peers;
619 * Number of peers in this group.
624 * At what time should we fail the peer startup process?
626 struct GNUNET_TIME_Absolute max_timeout;
629 * How many peers are being started right now?
631 unsigned int starting;
634 * How many peers have already been started?
636 unsigned int started;
639 * Hostkeys loaded from a file.
646 struct GNUNET_CONFIGURATION_Handle *ret;
647 const struct GNUNET_CONFIGURATION_Handle *orig;
648 const char *hostname;
654 struct ConnectTopologyContext
657 * How many connections are left to create.
659 unsigned int remaining_connections;
662 * Handle to group of peers.
664 struct GNUNET_TESTING_PeerGroup *pg;
668 * Temp value set for each iteration.
670 //struct PeerData *first;
673 * Notification that all peers are connected.
675 GNUNET_TESTING_NotifyCompletion notify_connections_done;
678 * Closure for notify.
683 struct ConnectContext
686 * Peer to connect second to.
688 struct GNUNET_TESTING_Daemon *first;
691 * Peer to connect first to.
693 struct GNUNET_TESTING_Daemon *second;
696 * Higher level topology connection context.
698 struct ConnectTopologyContext *ct_ctx;
701 struct UnblacklistContext
706 struct GNUNET_TESTING_PeerGroup *pg;
709 * uid of the first peer
719 struct GNUNET_TESTING_PeerGroup *pg;
722 * uid of the first peer
727 * Peer data for first peer.
729 struct PeerData *first;
732 * Random percentage to use
737 struct MinimumContext
742 struct GNUNET_TESTING_PeerGroup *pg;
745 * uid of the first peer
750 * Peer data for first peer.
752 struct PeerData *first;
755 * Number of conns per peer
757 unsigned int num_to_add;
760 * Permuted array of all possible connections. Only add the Nth
761 * peer if it's in the Nth position.
763 unsigned int *pg_array;
766 * What number is the current element we are iterating over?
768 unsigned int current;
776 struct GNUNET_TESTING_PeerGroup *pg;
779 * uid of the first peer
784 * uid of the second peer
789 * Peer data for first peer.
791 struct PeerData *first;
794 * Which peer has been chosen as the one to add?
799 * What number is the current element we are iterating over?
801 unsigned int current;
806 * Convert unique ID to hash code.
808 * @param uid unique ID to convert
809 * @param hash set to uid (extended with zeros)
812 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
814 memset (hash, 0, sizeof (GNUNET_HashCode));
815 *((uint32_t *) hash) = uid;
819 * Convert hash code to unique ID.
821 * @param uid unique ID to convert
822 * @param hash set to uid (extended with zeros)
825 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
827 memcpy (uid, hash, sizeof (uint32_t));
832 * Number of connects we are waiting on, allows us to rate limit
835 static int outstanding_connects;
838 * Get a topology from a string input.
840 * @param topology where to write the retrieved topology
841 * @param topology_string The string to attempt to
842 * get a configuration value from
843 * @return GNUNET_YES if topology string matched a
844 * known topology, GNUNET_NO if not
847 GNUNET_TESTING_topology_get (enum GNUNET_TESTING_Topology *topology,
848 const char *topology_string)
851 * Strings representing topologies in enum
853 static const char *topology_strings[] = {
855 * A clique (everyone connected to everyone else).
860 * Small-world network (2d torus plus random links).
865 * Small-world network (ring plus random links).
885 * Certain percentage of peers are unable to communicate directly
886 * replicating NAT conditions
891 * Scale free topology.
896 * Straight line topology.
901 * All peers are disconnected.
906 * Read the topology from a file.
914 if (topology_string == NULL)
916 while (topology_strings[curr] != NULL)
918 if (strcasecmp (topology_strings[curr], topology_string) == 0)
925 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
931 * Get connect topology option from string input.
933 * @param topology_option where to write the retrieved topology
934 * @param topology_string The string to attempt to
935 * get a configuration value from
936 * @return GNUNET_YES if string matched a known
937 * topology option, GNUNET_NO if not
940 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption
942 const char *topology_string)
945 * Options for connecting a topology as strings.
947 static const char *topology_option_strings[] = {
949 * Try to connect all peers specified in the topology.
954 * Choose a random subset of connections to create.
956 "CONNECT_RANDOM_SUBSET",
959 * Create at least X connections for each peer.
964 * Using a depth first search, create one connection
965 * per peer. If any are missed (graph disconnected)
966 * start over at those peers until all have at least one
972 * Find the N closest peers to each allowed peer in the
973 * topology and make sure a connection to those peers
974 * exists in the connect topology.
979 * No options specified.
987 if (topology_string == NULL)
989 while (NULL != topology_option_strings[curr])
991 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
993 *topology_option = curr;
998 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1003 * Function to iterate over options. Copies
1004 * the options to the target configuration,
1005 * updating PORT values as needed.
1007 * @param cls closure
1008 * @param section name of the section
1009 * @param option name of the option
1010 * @param value value of the option
1013 update_config (void *cls,
1014 const char *section, const char *option, const char *value)
1016 struct UpdateContext *ctx = cls;
1020 char *single_variable;
1021 char *per_host_variable;
1022 unsigned long long num_per_host;
1024 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1026 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1029 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1032 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
1036 GNUNET_free (single_variable);
1039 if (0 == strcmp (option, "UNIXPATH"))
1041 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1042 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1044 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1047 GNUNET_snprintf (uval,
1049 "/tmp/test-service-%s-%u", section, ctx->upnum++);
1052 else if ((GNUNET_YES ==
1053 GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1055 &num_per_host)) && (num_per_host > 0))
1058 GNUNET_snprintf (uval,
1060 "/tmp/test-service-%s-%u",
1061 section, ctx->fdnum % num_per_host);
1064 GNUNET_free (single_variable);
1065 GNUNET_free (per_host_variable);
1068 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1070 value = ctx->hostname;
1073 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1078 * Create a new configuration using the given configuration
1079 * as a template; however, each PORT in the existing cfg
1080 * must be renumbered by incrementing "*port". If we run
1081 * out of "*port" numbers, return NULL.
1083 * @param cfg template configuration
1084 * @param port port numbers to use, update to reflect
1085 * port numbers that were used
1086 * @param upnum number to make unix domain socket names unique
1087 * @param hostname hostname of the controlling host, to allow control connections from
1088 * @param fdnum number used to offset the unix domain socket for grouped processes
1089 * (such as statistics or peerinfo, which can be shared among others)
1091 * @return new configuration, NULL on error
1093 static struct GNUNET_CONFIGURATION_Handle *
1094 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
1096 uint32_t * upnum, const char *hostname, uint32_t * fdnum)
1098 struct UpdateContext uc;
1101 char *allowed_hosts;
1107 uc.ret = GNUNET_CONFIGURATION_create ();
1108 uc.hostname = hostname;
1111 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1112 if (uc.nport >= HIGH_PORT)
1115 GNUNET_CONFIGURATION_destroy (uc.ret);
1119 if (GNUNET_CONFIGURATION_get_value_string
1120 (cfg, "testing", "control_host", &control_host) == GNUNET_OK)
1122 if (hostname != NULL)
1123 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1126 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1128 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1130 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH",
1132 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1133 "ACCEPT_FROM", allowed_hosts);
1134 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH",
1136 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1138 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH",
1140 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1141 "ACCEPT_FROM", allowed_hosts);
1142 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1144 GNUNET_free_non_null (control_host);
1145 GNUNET_free (allowed_hosts);
1149 /* arm needs to know to allow connections from the host on which it is running,
1150 * otherwise gnunet-arm is unable to connect to it in some instances */
1151 if (hostname != NULL)
1153 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1154 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1155 "BINDTO", hostname);
1156 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1157 "BINDTO", hostname);
1158 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1160 GNUNET_free (allowed_hosts);
1164 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1165 "BINDTO", "127.0.0.1");
1166 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1167 "BINDTO", "127.0.0.1");
1170 *port = (uint16_t) uc.nport;
1178 * Add entries to the some list
1180 * @param pg the peer group we are working with
1181 * @param first index of the first peer
1182 * @param second index of the second peer
1183 * @param list the peer list to use
1185 * @return the number of connections added (can be 0, 1 or 2)
1189 remove_connections (struct GNUNET_TESTING_PeerGroup *pg,
1190 unsigned int first, unsigned int second,
1191 enum PeerLists list)
1195 struct PeerConnection **first_list;
1196 struct PeerConnection **second_list;
1197 struct PeerConnection *first_iter;
1198 struct PeerConnection *second_iter;
1199 struct PeerConnection **first_tail;
1200 struct PeerConnection **second_tail;
1203 GNUNET_HashCode hash_first;
1204 GNUNET_HashCode hash_second;
1206 hash_from_uid (first, &hash_first);
1207 hash_from_uid (second, &hash_second);
1215 first_list = &pg->peers[first].allowed_peers_head;
1216 second_list = &pg->peers[second].allowed_peers_head;
1217 first_tail = &pg->peers[first].allowed_peers_tail;
1218 second_tail = &pg->peers[second].allowed_peers_tail;
1221 first_list = &pg->peers[first].connect_peers_head;
1222 second_list = &pg->peers[second].connect_peers_head;
1223 first_tail = &pg->peers[first].connect_peers_tail;
1224 second_tail = &pg->peers[second].connect_peers_tail;
1227 first_list = &pg->peers[first].blacklisted_peers_head;
1228 second_list = &pg->peers[second].blacklisted_peers_head;
1229 first_tail = &pg->peers[first].blacklisted_peers_tail;
1230 second_tail = &pg->peers[second].blacklisted_peers_tail;
1233 first_list = &pg->peers[first].connect_peers_working_set_head;
1234 second_list = &pg->peers[second].connect_peers_working_set_head;
1235 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1236 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1242 first_iter = *first_list;
1243 while (first_iter != NULL)
1245 if (first_iter->index == second)
1247 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1248 GNUNET_free(first_iter);
1252 first_iter = first_iter->next;
1255 second_iter = *second_list;
1256 while (second_iter != NULL)
1258 if (second_iter->index == first)
1260 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1261 GNUNET_free(second_iter);
1265 second_iter = second_iter->next;
1269 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1272 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1277 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1280 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1289 * Add entries to the some list
1291 * @param pg the peer group we are working with
1292 * @param first index of the first peer
1293 * @param second index of the second peer
1294 * @param list the list type that we should modify
1296 * @return the number of connections added (can be 0, 1 or 2)
1300 add_connections (struct GNUNET_TESTING_PeerGroup *pg,
1301 unsigned int first, unsigned int second,
1302 enum PeerLists list)
1308 struct PeerConnection **first_list;
1309 struct PeerConnection **second_list;
1310 struct PeerConnection *first_iter;
1311 struct PeerConnection *second_iter;
1312 struct PeerConnection *new_first;
1313 struct PeerConnection *new_second;
1314 struct PeerConnection **first_tail;
1315 struct PeerConnection **second_tail;
1317 GNUNET_HashCode hash_first;
1318 GNUNET_HashCode hash_second;
1320 hash_from_uid (first, &hash_first);
1321 hash_from_uid (second, &hash_second);
1328 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Adding to ALLOWED\n");
1329 first_list = &pg->peers[first].allowed_peers_head;
1330 second_list = &pg->peers[second].allowed_peers_head;
1331 first_tail = &pg->peers[first].allowed_peers_tail;
1332 second_tail = &pg->peers[second].allowed_peers_tail;
1335 first_list = &pg->peers[first].connect_peers_head;
1336 second_list = &pg->peers[second].connect_peers_head;
1337 first_tail = &pg->peers[first].connect_peers_tail;
1338 second_tail = &pg->peers[second].connect_peers_tail;
1341 first_list = &pg->peers[first].blacklisted_peers_head;
1342 second_list = &pg->peers[second].blacklisted_peers_head;
1343 first_tail = &pg->peers[first].blacklisted_peers_tail;
1344 second_tail = &pg->peers[second].blacklisted_peers_tail;
1347 first_list = &pg->peers[first].connect_peers_working_set_head;
1348 second_list = &pg->peers[second].connect_peers_working_set_head;
1349 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1350 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1356 add_first = GNUNET_YES;
1357 add_second = GNUNET_YES;
1359 first_iter = *first_list;
1360 while (first_iter != NULL)
1362 if (first_iter->index == second)
1364 add_first = GNUNET_NO;
1367 first_iter = first_iter->next;
1370 second_iter = *second_list;
1371 while (second_iter != NULL)
1373 if (second_iter->index == first)
1375 add_second = GNUNET_NO;
1378 second_iter = second_iter->next;
1382 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1385 add_first = GNUNET_YES;
1389 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1392 add_second = GNUNET_YES;
1400 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Adding peer %d to %d\n", second, first);
1401 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1402 new_first->index = second;
1403 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1405 new_first->next = *first_list;
1406 *first_list = new_first;*/
1408 GNUNET_assert (GNUNET_OK ==
1409 GNUNET_CONTAINER_multihashmap_put (pg->
1411 [first].blacklisted_peers,
1414 peers[second].daemon,
1415 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1417 pg->peers[first].num_connections++;
1424 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1425 new_second->index = first;
1426 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1428 new_second->next = *second_list;
1429 *second_list = new_second;
1432 GNUNET_assert (GNUNET_OK ==
1433 GNUNET_CONTAINER_multihashmap_put (pg->
1435 [second].blacklisted_peers,
1438 peers[first].daemon,
1439 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1441 pg->peers[second].num_connections++;
1450 * Scale free network construction as described in:
1452 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1454 * Start with a network of "one" peer, then progressively add
1455 * peers up to the total number. At each step, iterate over
1456 * all possible peers and connect new peer based on number of
1457 * existing connections of the target peer.
1459 * @param pg the peer group we are dealing with
1460 * @param proc the connection processor to use
1461 * @param list the peer list to use
1463 * @return the number of connections created
1466 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg,
1467 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1470 unsigned int total_connections;
1471 unsigned int outer_count;
1473 unsigned int previous_total_connections;
1477 GNUNET_assert (pg->total > 1);
1479 /* Add a connection between the first two nodes */
1480 total_connections = proc (pg, 0, 1, list);
1482 for (outer_count = 1; outer_count < pg->total; outer_count++)
1484 previous_total_connections = total_connections;
1485 for (i = 0; i < outer_count; i++)
1488 pg->peers[i].num_connections /
1489 (double) previous_total_connections;
1492 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1493 UINT64_MAX)) / ((double) UINT64_MAX);
1495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1496 "Considering connecting peer %d to peer %d\n",
1499 if (random < probability)
1502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1503 "Connecting peer %d to peer %d\n", outer_count, i);
1505 total_connections += proc (pg, outer_count, i, list);
1510 return total_connections;
1514 * Create a topology given a peer group (set of running peers)
1515 * and a connection processor. Creates a small world topology
1516 * according to the rewired ring construction. The basic
1517 * behavior is that a ring topology is created, but with some
1518 * probability instead of connecting a peer to the next
1519 * neighbor in the ring a connection will be created to a peer
1520 * selected uniformly at random. We use the TESTING
1521 * PERCENTAGE option to specify what number of
1522 * connections each peer should have. Default is 2,
1523 * which makes the ring, any given number is multiplied by
1524 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1525 * each peer have on average 2logn connections. The additional
1526 * connections are made at increasing distance around the ring
1527 * from the original peer, or to random peers based on the re-
1528 * wiring probability. The TESTING
1529 * PROBABILITY option is used as the probability that a given
1530 * connection is rewired.
1532 * @param pg the peergroup to create the topology on
1533 * @param proc the connection processor to call to actually set
1534 * up connections between two peers
1535 * @param list the peer list to use
1537 * @return the number of connections that were set up
1541 create_small_world_ring (struct GNUNET_TESTING_PeerGroup *pg,
1542 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1546 unsigned int natLog;
1547 unsigned int randomPeer;
1548 double random, logNModifier, probability;
1549 unsigned int smallWorldConnections;
1554 unsigned int useAnd;
1555 int connect_attempts;
1557 logNModifier = 0.5; /* FIXME: default value? */
1558 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1563 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1564 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1566 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1567 p_string, "LOGNMODIFIER", "TESTING");
1568 GNUNET_free (p_string);
1570 probability = 0.5; /* FIXME: default percentage? */
1571 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1576 if (sscanf (p_string, "%lf", &probability) != 1)
1577 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1579 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1580 p_string, "PERCENTAGE", "TESTING");
1581 GNUNET_free (p_string);
1583 natLog = log (pg->total);
1584 connsPerPeer = ceil (natLog * logNModifier);
1586 if (connsPerPeer % 2 == 1)
1589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."), connsPerPeer);
1591 smallWorldConnections = 0;
1592 connect_attempts = 0;
1593 for (i = 0; i < pg->total; i++)
1596 max = i + connsPerPeer / 2;
1597 min = i - connsPerPeer / 2;
1599 if (max > pg->total - 1)
1601 max = max - pg->total;
1607 min = pg->total - 1 + min;
1611 for (j = 0; j < connsPerPeer / 2; j++)
1615 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1616 UINT64_MAX) / ((double) UINT64_MAX));
1617 if (random < probability)
1619 /* Connect to uniformly selected random peer */
1621 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1623 while ((((randomPeer < max) && (randomPeer > min))
1624 && (useAnd == 0)) || (((randomPeer > min)
1625 || (randomPeer < max))
1629 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1632 smallWorldConnections += proc (pg, i, randomPeer, list);
1636 nodeToConnect = i + j + 1;
1637 if (nodeToConnect > pg->total - 1)
1639 nodeToConnect = nodeToConnect - pg->total;
1641 connect_attempts += proc (pg, i, nodeToConnect, list);
1647 connect_attempts += smallWorldConnections;
1649 return connect_attempts;
1653 * Create a topology given a peer group (set of running peers)
1654 * and a connection processor.
1656 * @param pg the peergroup to create the topology on
1657 * @param proc the connection processor to call to actually set
1658 * up connections between two peers
1659 * @param list the peer list to use
1661 * @return the number of connections that were set up
1665 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg,
1666 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1668 unsigned int outer_count, inner_count;
1669 unsigned int cutoff;
1670 int connect_attempts;
1671 double nat_percentage;
1674 nat_percentage = 0.6; /* FIXME: default percentage? */
1675 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1680 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1681 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1683 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1684 p_string, "PERCENTAGE", "TESTING");
1685 GNUNET_free (p_string);
1690 cutoff = (unsigned int) (nat_percentage * pg->total);
1692 connect_attempts = 0;
1694 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1696 for (inner_count = outer_count + 1; inner_count < pg->total;
1699 if ((outer_count > cutoff) || (inner_count > cutoff))
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1703 "Connecting peer %d to peer %d\n",
1704 outer_count, inner_count);
1706 connect_attempts += proc (pg, outer_count, inner_count, list);
1711 return connect_attempts;
1716 * Create a topology given a peer group (set of running peers)
1717 * and a connection processor.
1719 * @param pg the peergroup to create the topology on
1720 * @param proc the connection processor to call to actually set
1721 * up connections between two peers
1722 * @param list the peer list to use
1724 * @return the number of connections that were set up
1728 create_small_world (struct GNUNET_TESTING_PeerGroup *pg,
1729 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1731 unsigned int i, j, k;
1732 unsigned int square;
1735 unsigned int toggle = 1;
1736 unsigned int nodeToConnect;
1737 unsigned int natLog;
1738 unsigned int node1Row;
1739 unsigned int node1Col;
1740 unsigned int node2Row;
1741 unsigned int node2Col;
1742 unsigned int distance;
1743 double probability, random, percentage;
1744 unsigned int smallWorldConnections;
1745 unsigned int small_world_it;
1747 int connect_attempts;
1748 square = floor (sqrt (pg->total));
1752 percentage = 0.5; /* FIXME: default percentage? */
1753 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1758 if (sscanf (p_string, "%lf", &percentage) != 1)
1759 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1761 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1762 p_string, "PERCENTAGE", "TESTING");
1763 GNUNET_free (p_string);
1765 if (percentage < 0.0)
1767 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1769 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1770 "PERCENTAGE", "TESTING", percentage);
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 if (square * square != pg->total)
1788 while (rows * cols < pg->total)
1790 if (toggle % 2 == 0)
1799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1801 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1805 connect_attempts = 0;
1806 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1807 * to the node to its right and above. Once this is over, we'll have our torus!
1808 * Special case for the last node (if the rows and columns are not equal), connect
1809 * to the first in the row to maintain topology.
1811 for (i = 0; i < pg->total; i++)
1813 /* First connect to the node to the right */
1814 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1815 nodeToConnect = i + 1;
1816 else if (i + 1 == pg->total)
1817 nodeToConnect = rows * cols - cols;
1819 nodeToConnect = i - cols + 1;
1821 connect_attempts += proc (pg, i, nodeToConnect, list);
1824 nodeToConnect = (rows * cols) - cols + i;
1826 nodeToConnect = i - cols;
1828 if (nodeToConnect < pg->total)
1829 connect_attempts += proc (pg, i, nodeToConnect, list);
1831 natLog = log (pg->total);
1832 #if VERBOSE_TESTING > 2
1833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1834 _("natural log of %d is %d, will run %d iterations\n"),
1835 pg->total, natLog, (int) (natLog * percentage));
1836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1837 _("Total connections added thus far: %u!\n"), connect_attempts);
1839 smallWorldConnections = 0;
1840 small_world_it = (unsigned int) (natLog * percentage);
1841 if (small_world_it < 1)
1843 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
1844 for (i = 0; i < small_world_it; i++)
1846 for (j = 0; j < pg->total; j++)
1848 /* Determine the row and column of node at position j on the 2d torus */
1849 node1Row = j / cols;
1850 node1Col = j - (node1Row * cols);
1851 for (k = 0; k < pg->total; k++)
1853 /* Determine the row and column of node at position k on the 2d torus */
1854 node2Row = k / cols;
1855 node2Col = k - (node2Row * cols);
1856 /* Simple Cartesian distance */
1858 abs (node1Row - node2Row) + abs (node1Col - node2Col);
1861 /* Calculate probability as 1 over the square of the distance */
1862 probability = 1.0 / (distance * distance);
1863 /* Choose a random value between 0 and 1 */
1866 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1868 ((double) UINT64_MAX);
1869 /* If random < probability, then connect the two nodes */
1870 if (random < probability)
1871 smallWorldConnections += proc (pg, j, k, list);
1877 connect_attempts += smallWorldConnections;
1878 #if VERBOSE_TESTING > 2
1879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1880 _("Total connections added for small world: %d!\n"),
1881 smallWorldConnections);
1883 return connect_attempts;
1887 * Create a topology given a peer group (set of running peers)
1888 * and a connection processor.
1890 * @param pg the peergroup to create the topology on
1891 * @param proc the connection processor to call to actually set
1892 * up connections between two peers
1893 * @param list the peer list to use
1895 * @return the number of connections that were set up
1899 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg,
1900 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1903 unsigned int outer_count;
1904 unsigned int inner_count;
1905 int connect_attempts;
1909 probability = 0.5; /* FIXME: default percentage? */
1910 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1915 if (sscanf (p_string, "%lf", &probability) != 1)
1916 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1918 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1919 p_string, "PROBABILITY", "TESTING");
1920 GNUNET_free (p_string);
1922 connect_attempts = 0;
1923 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1925 for (inner_count = outer_count + 1; inner_count < pg->total;
1930 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1931 UINT64_MAX)) / ((double) UINT64_MAX);
1933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1934 _("rand is %f probability is %f\n"), temp_rand,
1937 if (temp_rand < probability)
1939 connect_attempts += proc (pg, outer_count, inner_count, list);
1944 return connect_attempts;
1948 * Create a topology given a peer group (set of running peers)
1949 * and a connection processor. This particular function creates
1950 * the connections for a 2d-torus, plus additional "closest"
1951 * connections per peer.
1953 * @param pg the peergroup to create the topology on
1954 * @param proc the connection processor to call to actually set
1955 * up connections between two peers
1956 * @param list the peer list to use
1958 * @return the number of connections that were set up
1962 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg,
1963 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1966 unsigned int square;
1969 unsigned int toggle = 1;
1970 unsigned int nodeToConnect;
1971 int connect_attempts;
1973 connect_attempts = 0;
1975 square = floor (sqrt (pg->total));
1979 if (square * square != pg->total)
1981 while (rows * cols < pg->total)
1983 if (toggle % 2 == 0)
1992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1994 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1997 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1998 * to the node to its right and above. Once this is over, we'll have our torus!
1999 * Special case for the last node (if the rows and columns are not equal), connect
2000 * to the first in the row to maintain topology.
2002 for (i = 0; i < pg->total; i++)
2004 /* First connect to the node to the right */
2005 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2006 nodeToConnect = i + 1;
2007 else if (i + 1 == pg->total)
2008 nodeToConnect = rows * cols - cols;
2010 nodeToConnect = i - cols + 1;
2012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2013 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2015 connect_attempts += proc (pg, i, nodeToConnect, list);
2017 /* Second connect to the node immediately above */
2019 nodeToConnect = (rows * cols) - cols + i;
2021 nodeToConnect = i - cols;
2023 if (nodeToConnect < pg->total)
2026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2027 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2029 connect_attempts += proc (pg, i, nodeToConnect, list);
2034 return connect_attempts;
2039 * Create a topology given a peer group (set of running peers)
2040 * and a connection processor.
2042 * @param pg the peergroup to create the topology on
2043 * @param proc the connection processor to call to actually set
2044 * up connections between two peers
2045 * @param list the peer list to use
2047 * @return the number of connections that were set up
2051 create_clique (struct GNUNET_TESTING_PeerGroup *pg,
2052 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2054 unsigned int outer_count;
2055 unsigned int inner_count;
2056 int connect_attempts;
2058 connect_attempts = 0;
2060 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2062 for (inner_count = outer_count + 1; inner_count < pg->total;
2066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2067 "Connecting peer %d to peer %d\n",
2068 outer_count, inner_count);
2070 connect_attempts += proc (pg, outer_count, inner_count, list);
2074 return connect_attempts;
2079 * Iterator over hash map entries.
2081 * @param cls closure the peer group
2082 * @param key the key stored in the hashmap is the
2083 * index of the peer to connect to
2084 * @param value value in the hash map, handle to the peer daemon
2085 * @return GNUNET_YES if we should continue to
2090 unblacklist_iterator (void *cls,
2091 const GNUNET_HashCode * key,
2094 struct UnblacklistContext *un_ctx = cls;
2095 uint32_t second_pos;
2097 uid_from_hash (key, &second_pos);
2099 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2106 * Create a blacklist topology based on the allowed topology
2107 * which disallows any connections not in the allowed topology
2108 * at the transport level.
2110 * @param pg the peergroup to create the topology on
2111 * @param proc the connection processor to call to allow
2112 * up connections between two peers
2114 * @return the number of connections that were set up
2118 copy_allowed (struct GNUNET_TESTING_PeerGroup *pg,
2119 GNUNET_TESTING_ConnectionProcessor proc)
2121 struct UnblacklistContext un_ctx;
2124 struct PeerConnection *iter;
2128 for (count = 0; count < pg->total - 1; count++)
2130 un_ctx.first_uid = count;
2132 iter = pg->peers[count].allowed_peers_head;
2133 while (iter != NULL)
2135 remove_connections(pg, count, iter->index, BLACKLIST);
2136 //unblacklist_connections(pg, count, iter->index);
2140 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers, &unblacklist_iterator, &un_ctx);
2143 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2148 * Create a topology given a peer group (set of running peers)
2149 * and a connection processor.
2151 * @param pg the peergroup to create the topology on
2152 * @param proc the connection processor to call to actually set
2153 * up connections between two peers
2154 * @param list which list should be modified
2156 * @return the number of connections that were set up
2160 create_line (struct GNUNET_TESTING_PeerGroup *pg,
2161 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2164 int connect_attempts;
2166 connect_attempts = 0;
2168 /* Connect each peer to the next highest numbered peer */
2169 for (count = 0; count < pg->total - 1; count++)
2172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2173 "Connecting peer %d to peer %d\n", count, count + 1);
2175 connect_attempts += proc (pg, count, count + 1, list);
2178 return connect_attempts;
2182 * Create a topology given a peer group (set of running peers)
2183 * and a connection processor.
2185 * @param pg the peergroup to create the topology on
2186 * @param filename the file to read topology information from
2187 * @param proc the connection processor to call to actually set
2188 * up connections between two peers
2189 * @param list the peer list to use
2191 * @return the number of connections that were set up
2195 create_from_file (struct GNUNET_TESTING_PeerGroup *pg,
2197 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2199 int connect_attempts;
2200 unsigned int first_peer_index;
2201 unsigned int second_peer_index;
2202 connect_attempts = 0;
2207 unsigned int total_peers;
2209 enum States curr_state;
2211 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2212 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2214 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2217 "Could not open file `%s' specified for topology!", filename);
2218 return connect_attempts;
2221 data = GNUNET_malloc_large (frstat.st_size);
2222 GNUNET_assert(data != NULL);
2223 if (frstat.st_size !=
2224 GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2227 "Could not read file %s specified for host list, ending test!", filename);
2229 return connect_attempts;
2234 /* First line should contain a single integer, specifying the number of peers */
2235 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2236 curr_state = NUM_PEERS;
2237 while (count < frstat.st_size - 1)
2239 if ((buf[count] == '\n') || (buf[count] == ' '))
2248 if (1 != sscanf(&buf[count], "%u", &total_peers))
2250 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read number of peers from topology file!\n");
2251 GNUNET_free_non_null(data);
2252 return connect_attempts;
2254 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read %u total peers in topology\n", total_peers);
2255 GNUNET_assert(total_peers == pg->total);
2256 curr_state = PEER_INDEX;
2257 while((buf[count] != '\n') && (count < frstat.st_size - 1))
2262 if (1 != sscanf(&buf[count], "%u", &first_peer_index))
2264 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read peer index from topology file!\n");
2265 GNUNET_free_non_null(data);
2266 return connect_attempts;
2268 while((buf[count] != ':') && (count < frstat.st_size - 1))
2271 curr_state = OTHER_PEER_INDEX;
2274 if (1 == sscanf(&buf[count], ":"))
2275 curr_state = OTHER_PEER_INDEX;
2278 case OTHER_PEER_INDEX:
2279 if (1 != sscanf(&buf[count], "%u", &second_peer_index))
2281 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to peer index from topology file!\n");
2282 GNUNET_free_non_null(data);
2283 return connect_attempts;
2285 /* Assume file is written with first peer 1, but array index is 0 */
2286 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index - 1, list);
2287 while((buf[count] != '\n') && (buf[count] != ',') && (count < frstat.st_size - 1))
2289 if (buf[count] == '\n')
2291 curr_state = PEER_INDEX;
2293 else if (buf[count] != ',')
2295 curr_state = OTHER_PEER_INDEX;
2300 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Found bad data in topology file while in state %d!\n", curr_state);
2302 return connect_attempts;
2307 /* Connect each peer to the next highest numbered peer */
2308 for (count = 0; count < pg->total - 1; count++)
2311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2312 "Connecting peer %d to peer %d\n", first_peer_index, second_peer_index);
2314 connect_attempts += proc (pg, first_peer_index, second_peer_index);
2317 return connect_attempts;
2321 * Create a topology given a peer group (set of running peers)
2322 * and a connection processor.
2324 * @param pg the peergroup to create the topology on
2325 * @param proc the connection processor to call to actually set
2326 * up connections between two peers
2327 * @param list the peer list to use
2329 * @return the number of connections that were set up
2333 create_ring (struct GNUNET_TESTING_PeerGroup *pg,
2334 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2337 int connect_attempts;
2339 connect_attempts = 0;
2341 /* Connect each peer to the next highest numbered peer */
2342 for (count = 0; count < pg->total - 1; count++)
2345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2346 "Connecting peer %d to peer %d\n", count, count + 1);
2348 connect_attempts += proc (pg, count, count + 1, list);
2351 /* Connect the last peer to the first peer */
2352 connect_attempts += proc (pg, pg->total - 1, 0, list);
2354 return connect_attempts;
2359 * Iterator for writing friends of a peer to a file.
2361 * @param cls closure, an open writable file handle
2362 * @param key the key the daemon was stored under
2363 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2365 * @return GNUNET_YES to continue iteration
2367 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2368 * with a single file_iterator that takes a closure which contains
2369 * the prefix to write before the peer. Then this could be used
2370 * for blacklisting multiple transports and writing the friend
2371 * file. I'm sure *someone* will complain loudly about other
2372 * things that negate these functions even existing so no point in
2376 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2378 FILE *temp_friend_handle = cls;
2379 struct GNUNET_TESTING_Daemon *peer = value;
2380 struct GNUNET_PeerIdentity *temppeer;
2381 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2383 temppeer = &peer->id;
2384 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2385 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2390 struct BlacklistContext
2393 * The (open) file handle to write to
2395 FILE *temp_file_handle;
2398 * The transport that this peer will be blacklisted on.
2404 * Iterator for writing blacklist data to appropriate files.
2406 * @param cls closure, an open writable file handle
2407 * @param key the key the daemon was stored under
2408 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2410 * @return GNUNET_YES to continue iteration
2413 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2415 struct BlacklistContext *blacklist_ctx = cls;
2416 struct GNUNET_TESTING_Daemon *peer = value;
2417 struct GNUNET_PeerIdentity *temppeer;
2418 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2420 temppeer = &peer->id;
2421 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2422 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2423 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2424 blacklist_ctx->transport, (char *) &peer_enc);
2432 * Create the friend files based on the PeerConnection's
2433 * of each peer in the peer group, and copy the files
2434 * to the appropriate place
2436 * @param pg the peer group we are dealing with
2439 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
2441 FILE *temp_friend_handle;
2442 unsigned int pg_iter;
2443 char *temp_service_path;
2444 struct GNUNET_OS_Process **procarr;
2447 enum GNUNET_OS_ProcessStatusType type;
2448 unsigned long return_code;
2453 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2454 struct PeerConnection *conn_iter;
2456 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2457 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2459 mytemp = GNUNET_DISK_mktemp ("friends");
2460 GNUNET_assert (mytemp != NULL);
2461 temp_friend_handle = fopen (mytemp, "wt");
2462 GNUNET_assert (temp_friend_handle != NULL);
2464 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2465 while (conn_iter != NULL)
2467 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
2468 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2469 conn_iter = conn_iter->next;
2472 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2473 &friend_file_iterator,
2474 temp_friend_handle);
2476 fclose (temp_friend_handle);
2479 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2480 daemon->cfg, "PATHS",
2482 &temp_service_path))
2484 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2486 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2487 "SERVICEHOME", "PATHS");
2488 if (UNLINK (mytemp) != 0)
2489 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2491 GNUNET_free (mytemp);
2495 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2497 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2498 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2503 _("Copying file with command cp %s %s\n"), mytemp, arg);
2508 else /* Remote, scp the file to the correct place */
2510 if (NULL != pg->peers[pg_iter].daemon->username)
2511 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2512 pg->peers[pg_iter].daemon->username,
2513 pg->peers[pg_iter].daemon->hostname,
2516 GNUNET_asprintf (&arg, "%s:%s/friends",
2517 pg->peers[pg_iter].daemon->hostname,
2520 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2525 _("Copying file with command scp %s %s\n"), mytemp,
2530 GNUNET_free (temp_service_path);
2531 GNUNET_free (mytemp);
2535 ret = GNUNET_SYSERR;
2536 while ((count < max_wait) && (ret != GNUNET_OK))
2539 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2543 _("Checking copy status of file %d\n"), pg_iter);
2545 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2547 if (GNUNET_OS_process_status
2548 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2550 ret = GNUNET_SYSERR;
2552 else if ((type != GNUNET_OS_PROCESS_EXITED)
2553 || (return_code != 0))
2555 ret = GNUNET_SYSERR;
2559 GNUNET_OS_process_close (procarr[pg_iter]);
2560 procarr[pg_iter] = NULL;
2562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2563 _("File %d copied\n"), pg_iter);
2569 if (ret == GNUNET_SYSERR)
2571 /* FIXME: why sleep here? -CG */
2577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2578 _("Finished copying all friend files!\n"));
2580 GNUNET_free (procarr);
2586 * Create the blacklist files based on the PeerConnection's
2587 * of each peer in the peer group, and copy the files
2588 * to the appropriate place.
2590 * @param pg the peer group we are dealing with
2591 * @param transports space delimited list of transports to blacklist
2594 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg,
2595 const char *transports)
2597 FILE *temp_file_handle;
2598 unsigned int pg_iter;
2599 char *temp_service_path;
2600 struct GNUNET_OS_Process **procarr;
2603 enum GNUNET_OS_ProcessStatusType type;
2604 unsigned long return_code;
2611 char *temp_transports;
2614 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2615 struct PeerConnection *conn_iter;
2617 static struct BlacklistContext blacklist_ctx;
2620 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2621 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2623 mytemp = GNUNET_DISK_mktemp ("blacklist");
2624 GNUNET_assert (mytemp != NULL);
2625 temp_file_handle = fopen (mytemp, "wt");
2626 GNUNET_assert (temp_file_handle != NULL);
2627 temp_transports = GNUNET_strdup (transports);
2629 blacklist_ctx.temp_file_handle = temp_file_handle;
2631 transport_len = strlen (temp_transports) + 1;
2634 for (i = 0; i < transport_len; i++)
2636 if ((temp_transports[i] == ' ') && (pos == NULL))
2637 continue; /* At start of string (whitespace) */
2638 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2640 temp_transports[i] = '\0';
2642 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
2643 while (conn_iter != NULL)
2645 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
2646 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
2647 conn_iter = conn_iter->next;
2651 blacklist_ctx.transport = pos;
2652 entry_count = GNUNET_CONTAINER_multihashmap_iterate (pg->
2654 [pg_iter].blacklisted_peers,
2655 &blacklist_file_iterator,
2659 } /* At beginning of actual string */
2660 else if (pos == NULL)
2662 pos = &temp_transports[i];
2666 GNUNET_free (temp_transports);
2667 fclose (temp_file_handle);
2670 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2671 daemon->cfg, "PATHS",
2673 &temp_service_path))
2675 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2677 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2678 "SERVICEHOME", "PATHS");
2679 if (UNLINK (mytemp) != 0)
2680 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2682 GNUNET_free (mytemp);
2686 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2688 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2689 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2694 _("Copying file with command cp %s %s\n"), mytemp, arg);
2699 else /* Remote, scp the file to the correct place */
2701 if (NULL != pg->peers[pg_iter].daemon->username)
2702 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
2703 pg->peers[pg_iter].daemon->username,
2704 pg->peers[pg_iter].daemon->hostname,
2707 GNUNET_asprintf (&arg, "%s:%s/blacklist",
2708 pg->peers[pg_iter].daemon->hostname,
2711 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2716 _("Copying file with command scp %s %s\n"), mytemp,
2721 GNUNET_free (temp_service_path);
2722 GNUNET_free (mytemp);
2726 ret = GNUNET_SYSERR;
2727 while ((count < max_wait) && (ret != GNUNET_OK))
2730 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2734 _("Checking copy status of file %d\n"), pg_iter);
2736 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2738 if (GNUNET_OS_process_status
2739 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2741 ret = GNUNET_SYSERR;
2743 else if ((type != GNUNET_OS_PROCESS_EXITED)
2744 || (return_code != 0))
2746 ret = GNUNET_SYSERR;
2750 GNUNET_OS_process_close (procarr[pg_iter]);
2751 procarr[pg_iter] = NULL;
2753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2754 _("File %d copied\n"), pg_iter);
2760 if (ret == GNUNET_SYSERR)
2762 /* FIXME: why sleep here? -CG */
2768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2769 _("Finished copying all blacklist files!\n"));
2771 GNUNET_free (procarr);
2777 * Internal notification of a connection, kept so that we can ensure some connections
2778 * happen instead of flooding all testing daemons with requests to connect.
2781 internal_connect_notify (void *cls,
2782 const struct GNUNET_PeerIdentity *first,
2783 const struct GNUNET_PeerIdentity *second,
2785 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2786 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2787 struct GNUNET_TESTING_Daemon *first_daemon,
2788 struct GNUNET_TESTING_Daemon *second_daemon,
2791 struct ConnectTopologyContext *ct_ctx = cls;
2792 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
2793 outstanding_connects--;
2794 ct_ctx->remaining_connections--;
2795 if (ct_ctx->remaining_connections == 0)
2797 if (ct_ctx->notify_connections_done != NULL)
2798 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
2799 GNUNET_free (ct_ctx);
2802 if (pg->notify_connection != NULL)
2803 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
2804 first_cfg, second_cfg, first_daemon, second_daemon,
2810 * Either delay a connection (because there are too many outstanding)
2811 * or schedule it for right now.
2813 * @param cls a connection context
2814 * @param tc the task runtime context
2817 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2819 struct ConnectContext *connect_context = cls;
2821 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2824 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2826 #if VERBOSE_TESTING > 2
2827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2829 ("Delaying connect, we have too many outstanding connections!\n"));
2831 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2832 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
2833 &schedule_connect, connect_context);
2837 #if VERBOSE_TESTING > 2
2838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2839 _("Creating connection, outstanding_connections is %d\n"),
2840 outstanding_connects);
2842 outstanding_connects++;
2843 GNUNET_TESTING_daemons_connect (connect_context->first,
2844 connect_context->second,
2847 &internal_connect_notify,
2848 connect_context->ct_ctx);
2849 GNUNET_free (connect_context);
2855 * Iterator for actually scheduling connections to be created
2856 * between two peers.
2858 * @param cls closure, a GNUNET_TESTING_Daemon
2859 * @param key the key the second Daemon was stored under
2860 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2862 * @return GNUNET_YES to continue iteration
2865 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2867 struct ConnectTopologyContext *ct_ctx = cls;
2868 struct PeerData *first = ct_ctx->first;
2869 struct GNUNET_TESTING_Daemon *second = value;
2870 struct ConnectContext *connect_context;
2872 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
2873 connect_context->first = first->daemon;
2874 connect_context->second = second;
2875 connect_context->ct_ctx = ct_ctx;
2876 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
2884 * Iterator for copying all entries in the allowed hashmap to the
2887 * @param cls closure, a GNUNET_TESTING_Daemon
2888 * @param key the key the second Daemon was stored under
2889 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2891 * @return GNUNET_YES to continue iteration
2894 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2896 struct PeerData *first = cls;
2898 GNUNET_assert (GNUNET_OK ==
2899 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
2901 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2908 * Make the peers to connect the same as those that are allowed to be
2911 * @param pg the peer group
2914 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2916 unsigned int pg_iter;
2920 struct PeerConnection *iter;
2924 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2927 iter = pg->peers[pg_iter].allowed_peers_head;
2928 while (iter != NULL)
2930 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating connection between %d and %d\n", pg_iter, iter->index);
2931 total += add_connections(pg, pg_iter, iter->index, CONNECT);
2932 //total += add_actual_connections(pg, pg_iter, iter->index);
2937 GNUNET_CONTAINER_multihashmap_iterate (pg->
2938 peers[pg_iter].allowed_peers,
2939 ©_topology_iterator,
2940 &pg->peers[pg_iter]);
2942 if (GNUNET_SYSERR == ret)
2943 return GNUNET_SYSERR;
2945 total = total + ret;
2953 * Connect the topology as specified by the PeerConnection's
2954 * of each peer in the peer group
2956 * @param pg the peer group we are dealing with
2957 * @param notify_callback callback to notify when finished
2958 * @param notify_cls closure for notify callback
2960 * @return the number of connections that will be attempted
2963 connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2964 GNUNET_TESTING_NotifyCompletion notify_callback,
2967 unsigned int pg_iter;
2969 struct ConnectTopologyContext *ct_ctx;
2971 struct PeerConnection *connection_iter;
2972 struct ConnectContext *connect_context;
2978 ct_ctx = GNUNET_malloc (sizeof (struct ConnectTopologyContext));
2979 ct_ctx->notify_connections_done = notify_callback;
2980 ct_ctx->notify_cls = notify_cls;
2983 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2986 connection_iter = pg->peers[pg_iter].connect_peers_head;
2987 while (connection_iter != NULL)
2989 connection_iter = connection_iter->next;
2994 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3000 GNUNET_free (ct_ctx);
3003 ct_ctx->remaining_connections = total;
3006 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3009 connection_iter = pg->peers[pg_iter].connect_peers_head;
3010 while (connection_iter != NULL)
3012 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling connect of peer %d to peer %d\n", pg_iter, connection_iter->index);
3013 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3014 connect_context->first = pg->peers[pg_iter].daemon;
3015 connect_context->second = pg->peers[connection_iter->index].daemon;
3016 connect_context->ct_ctx = ct_ctx;
3017 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3018 connection_iter = connection_iter->next;
3023 GNUNET_CONTAINER_multihashmap_iterate (pg->
3024 peers[pg_iter].connect_peers,
3025 &connect_iterator, ct_ctx);
3026 GNUNET_assert (GNUNET_SYSERR != ret && ret >= 0);
3027 total = total + ret;
3035 * Takes a peer group and creates a topology based on the
3036 * one specified. Creates a topology means generates friend
3037 * files for the peers so they can only connect to those allowed
3038 * by the topology. This will only have an effect once peers
3039 * are started if the FRIENDS_ONLY option is set in the base
3040 * config. Also takes an optional restrict topology which
3041 * disallows connections based on particular transports
3042 * UNLESS they are specified in the restricted topology.
3044 * @param pg the peer group struct representing the running peers
3045 * @param topology which topology to connect the peers in
3046 * @param restrict_topology disallow restrict_transports transport
3047 * connections to peers NOT in this topology
3048 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3049 * @param restrict_transports space delimited list of transports to blacklist
3050 * to create restricted topology
3052 * @return the maximum number of connections were all allowed peers
3053 * connected to each other
3056 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
3057 enum GNUNET_TESTING_Topology topology,
3058 enum GNUNET_TESTING_Topology
3060 const char *restrict_transports)
3064 unsigned int num_connections;
3065 int unblacklisted_connections;
3070 for (i = 0; i < pg->total; i++)
3072 pg->peers[i].allowed_peers =
3073 GNUNET_CONTAINER_multihashmap_create (100);
3074 pg->peers[i].connect_peers =
3075 GNUNET_CONTAINER_multihashmap_create (100);
3076 pg->peers[i].blacklisted_peers =
3077 GNUNET_CONTAINER_multihashmap_create (100);
3078 pg->peers[i].pg = pg;
3084 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3088 num_connections = create_clique (pg, &add_connections, ALLOWED);
3090 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3093 _("Creating small world (ring) topology\n"));
3096 create_small_world_ring (pg, &add_connections, ALLOWED);
3098 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3101 _("Creating small world (2d-torus) topology\n"));
3103 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3105 case GNUNET_TESTING_TOPOLOGY_RING:
3107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3109 num_connections = create_ring (pg, &add_connections, ALLOWED);
3111 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3115 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3117 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3120 _("Creating Erdos-Renyi topology\n"));
3122 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3124 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3128 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3130 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3133 _("Creating Scale Free topology\n"));
3135 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3137 case GNUNET_TESTING_TOPOLOGY_LINE:
3139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3140 _("Creating straight line topology\n"));
3142 num_connections = create_line (pg, &add_connections, ALLOWED);
3144 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3147 _("Creating topology from file!\n"));
3150 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing", "topology_file",
3152 num_connections = create_from_file (pg, filename, &add_connections, ALLOWED);
3155 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!");
3156 num_connections = 0;
3159 case GNUNET_TESTING_TOPOLOGY_NONE:
3161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3163 ("Creating no allowed topology (all peers can connect at core level)\n"));
3165 num_connections = pg->total * pg->total; /* Clique is allowed! */
3168 num_connections = 0;
3173 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
3175 ret = create_and_copy_friend_files (pg);
3176 if (ret != GNUNET_OK)
3179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3180 _("Failed during friend file copying!\n"));
3182 return GNUNET_SYSERR;
3187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3188 _("Friend files created/copied successfully!\n"));
3193 /* Use the create clique method to initially set all connections as blacklisted. */
3194 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
3195 create_clique (pg, &add_connections, BLACKLIST);
3197 unblacklisted_connections = 0;
3198 /* Un-blacklist connections as per the topology specified */
3199 switch (restrict_topology)
3201 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3204 _("Blacklisting all but clique topology\n"));
3206 unblacklisted_connections =
3207 create_clique (pg, &remove_connections, BLACKLIST);
3209 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3212 _("Blacklisting all but small world (ring) topology\n"));
3214 unblacklisted_connections =
3215 create_small_world_ring (pg, &remove_connections, BLACKLIST);
3217 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3221 ("Blacklisting all but small world (2d-torus) topology\n"));
3223 unblacklisted_connections =
3224 create_small_world (pg, &remove_connections, BLACKLIST);
3226 case GNUNET_TESTING_TOPOLOGY_RING:
3228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3229 _("Blacklisting all but ring topology\n"));
3231 unblacklisted_connections = create_ring (pg, &remove_connections, BLACKLIST);
3233 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3236 _("Blacklisting all but 2d torus topology\n"));
3238 unblacklisted_connections =
3239 create_2d_torus (pg, &remove_connections, BLACKLIST);
3241 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3244 _("Blacklisting all but Erdos-Renyi topology\n"));
3246 unblacklisted_connections =
3247 create_erdos_renyi (pg, &remove_connections, BLACKLIST);
3249 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3252 _("Blacklisting all but InterNAT topology\n"));
3254 unblacklisted_connections =
3255 create_nated_internet (pg, &remove_connections, BLACKLIST);
3257 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3260 _("Blacklisting all but Scale Free topology\n"));
3262 unblacklisted_connections =
3263 create_scale_free (pg, &remove_connections, BLACKLIST);
3265 case GNUNET_TESTING_TOPOLOGY_LINE:
3267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3268 _("Blacklisting all but straight line topology\n"));
3270 unblacklisted_connections = create_line (pg, &remove_connections, BLACKLIST);
3272 case GNUNET_TESTING_TOPOLOGY_NONE: /* Fall through */
3273 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3277 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
3279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3281 ("Creating blacklist topology from allowed\n"));
3282 unblacklisted_connections = copy_allowed (pg, &remove_connections);
3287 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
3289 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n", restrict_transports);
3290 ret = create_and_copy_blacklist_files (pg, restrict_transports);
3291 if (ret != GNUNET_OK)
3294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3295 _("Failed during blacklist file copying!\n"));
3302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3303 _("Blacklist files created/copied successfully!\n"));
3307 return num_connections;
3313 * Iterator for choosing random peers to connect.
3315 * @param cls closure, a RandomContext
3316 * @param key the key the second Daemon was stored under
3317 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3319 * @return GNUNET_YES to continue iteration
3322 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3324 struct RandomContext *random_ctx = cls;
3325 double random_number;
3326 uint32_t second_pos;
3327 GNUNET_HashCode first_hash;
3330 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3331 UINT64_MAX)) / ((double) UINT64_MAX);
3332 if (random_number < random_ctx->percentage)
3334 GNUNET_assert (GNUNET_OK ==
3335 GNUNET_CONTAINER_multihashmap_put (random_ctx->
3336 first->connect_peers_working_set,
3338 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3341 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
3342 uid_from_hash (key, &second_pos);
3343 hash_from_uid (random_ctx->first_uid, &first_hash);
3344 GNUNET_assert (random_ctx->pg->total > second_pos);
3345 GNUNET_assert (GNUNET_YES ==
3346 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
3348 [second_pos].connect_peers,
3358 * Iterator for adding at least X peers to a peers connection set.
3360 * @param cls closure, MinimumContext
3361 * @param key the key the second Daemon was stored under
3362 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3364 * @return GNUNET_YES to continue iteration
3367 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3369 struct MinimumContext *min_ctx = cls;
3370 uint32_t second_pos;
3371 GNUNET_HashCode first_hash;
3374 if (GNUNET_CONTAINER_multihashmap_size
3375 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
3377 for (i = 0; i < min_ctx->num_to_add; i++)
3379 if (min_ctx->pg_array[i] == min_ctx->current)
3381 GNUNET_assert (GNUNET_OK ==
3382 GNUNET_CONTAINER_multihashmap_put
3383 (min_ctx->first->connect_peers_working_set, key,
3385 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3386 uid_from_hash (key, &second_pos);
3387 hash_from_uid (min_ctx->first_uid, &first_hash);
3388 GNUNET_assert (min_ctx->pg->total > second_pos);
3389 GNUNET_assert (GNUNET_OK ==
3390 GNUNET_CONTAINER_multihashmap_put (min_ctx->
3392 [second_pos].connect_peers_working_set,
3396 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3397 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
3398 GNUNET_assert (GNUNET_YES ==
3399 GNUNET_CONTAINER_multihashmap_remove
3400 (min_ctx->pg->peers[second_pos].connect_peers,
3401 &first_hash, min_ctx->first->daemon));
3408 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
3414 * Iterator for adding peers to a connection set based on a depth first search.
3416 * @param cls closure, MinimumContext
3417 * @param key the key the second daemon was stored under
3418 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3420 * @return GNUNET_YES to continue iteration
3423 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3425 struct DFSContext *dfs_ctx = cls;
3426 GNUNET_HashCode first_hash;
3428 if (dfs_ctx->current == dfs_ctx->chosen)
3430 GNUNET_assert (GNUNET_OK ==
3431 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3432 first->connect_peers_working_set,
3434 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3435 uid_from_hash (key, &dfs_ctx->second_uid);
3436 hash_from_uid (dfs_ctx->first_uid, &first_hash);
3437 GNUNET_assert (GNUNET_OK ==
3438 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3440 [dfs_ctx->second_uid].connect_peers_working_set,
3444 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3445 GNUNET_assert (GNUNET_YES ==
3446 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
3448 [dfs_ctx->second_uid].connect_peers,
3452 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
3453 return GNUNET_NO; /* We have found our peer, don't iterate more */
3462 * From the set of connections possible, choose percentage percent of connections
3463 * to actually connect.
3465 * @param pg the peergroup we are dealing with
3466 * @param percentage what percent of total connections to make
3469 choose_random_connections (struct GNUNET_TESTING_PeerGroup *pg,
3472 struct RandomContext random_ctx;
3475 struct PeerConnection *temp_peers;
3476 struct PeerConnection *conn_iter;
3477 double random_number;
3480 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3482 random_ctx.first_uid = pg_iter;
3483 random_ctx.first = &pg->peers[pg_iter];
3484 random_ctx.percentage = percentage;
3488 conn_iter = pg->peers[pg_iter].connect_peers_head;
3489 while (conn_iter != NULL)
3493 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3494 UINT64_MAX)) / ((double) UINT64_MAX);
3495 if (random_number < percentage)
3497 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
3499 conn_iter = conn_iter->next;
3502 pg->peers[pg_iter].connect_peers_working_set =
3503 GNUNET_CONTAINER_multihashmap_create (pg->total);
3504 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3505 &random_connect_iterator,
3507 /* Now remove the old connections */
3508 GNUNET_CONTAINER_multihashmap_destroy (pg->
3509 peers[pg_iter].connect_peers);
3510 /* And replace with the random set */
3511 pg->peers[pg_iter].connect_peers =
3512 pg->peers[pg_iter].connect_peers_working_set;
3516 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3518 conn_iter = pg->peers[pg_iter].connect_peers_head;
3519 while (pg->peers[pg_iter].connect_peers_head != NULL)
3520 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
3522 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
3523 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
3524 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
3525 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
3531 * Count the number of connections in a linked list of connections.
3533 * @param conn_list the connection list to get the count of
3535 * @return the number of elements in the list
3538 count_connections (struct PeerConnection *conn_list)
3540 struct PeerConnection *iter;
3544 while (iter != NULL)
3554 count_workingset_connections (struct GNUNET_TESTING_PeerGroup *pg)
3557 unsigned int pg_iter;
3559 struct PeerConnection *conn_iter;
3563 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3566 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
3567 while (conn_iter != NULL)
3570 conn_iter = conn_iter->next;
3574 GNUNET_CONTAINER_multihashmap_size (pg->
3576 [pg_iter].connect_peers_working_set);
3585 count_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg)
3588 unsigned int pg_iter;
3590 struct PeerConnection *conn_iter;
3594 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3597 conn_iter = pg->peers[pg_iter].allowed_peers_head;
3598 while (conn_iter != NULL)
3601 conn_iter = conn_iter->next;
3605 GNUNET_CONTAINER_multihashmap_size (pg->
3607 [pg_iter].allowed_peers);
3616 * From the set of connections possible, choose at least num connections per
3619 * @param pg the peergroup we are dealing with
3620 * @param num how many connections at least should each peer have (if possible)?
3623 choose_minimum (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3626 struct MinimumContext minimum_ctx;
3628 struct PeerConnection *conn_iter;
3629 unsigned int temp_list_size;
3632 uint32_t random; /* Random list entry to connect peer to */
3637 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3639 temp_list_size = count_connections(pg->peers[pg_iter].connect_peers_head);
3640 if (temp_list_size == 0)
3642 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer %d has 0 connections!?!?\n", pg_iter);
3645 for (i = 0; i < num; i++)
3647 random = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, temp_list_size);
3648 conn_iter = pg->peers[pg_iter].connect_peers_head;
3649 for (count = 0; count < random; count++)
3650 conn_iter = conn_iter->next;
3651 /* We now have a random connection, connect it! */
3652 GNUNET_assert(conn_iter != NULL);
3653 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
3657 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3659 pg->peers[pg_iter].connect_peers_working_set =
3660 GNUNET_CONTAINER_multihashmap_create (num);
3663 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3665 minimum_ctx.first_uid = pg_iter;
3666 minimum_ctx.pg_array =
3667 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
3668 GNUNET_CONTAINER_multihashmap_size
3669 (pg->peers[pg_iter].connect_peers));
3670 minimum_ctx.first = &pg->peers[pg_iter];
3671 minimum_ctx.pg = pg;
3672 minimum_ctx.num_to_add = num;
3673 minimum_ctx.current = 0;
3674 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3675 &minimum_connect_iterator,
3679 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3681 /* Remove the "old" connections */
3682 GNUNET_CONTAINER_multihashmap_destroy (pg->
3683 peers[pg_iter].connect_peers);
3684 /* And replace with the working set */
3685 pg->peers[pg_iter].connect_peers =
3686 pg->peers[pg_iter].connect_peers_working_set;
3689 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3691 while (pg->peers[pg_iter].connect_peers_head != NULL)
3692 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
3694 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
3695 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
3696 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
3697 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
3702 struct FindClosestContext
3705 * The currently known closest peer.
3707 struct GNUNET_TESTING_Daemon *closest;
3710 * The info for the peer we are adding connections for.
3712 struct PeerData *curr_peer;
3715 * The distance (bits) between the current
3716 * peer and the currently known closest.
3718 unsigned int closest_dist;
3721 * The offset of the closest known peer in
3724 unsigned int closest_num;
3728 * Iterator over hash map entries of the allowed
3729 * peer connections. Find the closest, not already
3730 * connected peer and return it.
3732 * @param cls closure (struct FindClosestContext)
3733 * @param key current key code (hash of offset in pg)
3734 * @param value value in the hash map - a GNUNET_TESTING_Daemon
3735 * @return GNUNET_YES if we should continue to
3740 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
3742 struct FindClosestContext *closest_ctx = cls;
3743 struct GNUNET_TESTING_Daemon *daemon = value;
3745 if (((closest_ctx->closest == NULL) ||
3746 (GNUNET_CRYPTO_hash_matching_bits
3747 (&daemon->id.hashPubKey,
3748 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
3749 closest_ctx->closest_dist))
3751 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
3752 curr_peer->connect_peers,
3755 closest_ctx->closest_dist =
3756 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
3757 &closest_ctx->curr_peer->daemon->
3759 closest_ctx->closest = daemon;
3760 uid_from_hash (key, &closest_ctx->closest_num);
3767 * From the set of connections possible, choose at num connections per
3768 * peer based on depth which are closest out of those allowed. Guaranteed
3769 * to add num peers to connect to, provided there are that many peers
3770 * in the underlay topology to connect to.
3772 * @param pg the peergroup we are dealing with
3773 * @param num how many connections at least should each peer have (if possible)?
3774 * @param proc processor to actually add the connections
3775 * @param list the peer list to use
3778 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
3779 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
3784 struct FindClosestContext closest_ctx;
3789 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
3791 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3793 closest_ctx.curr_peer = &pg->peers[pg_iter];
3794 closest_ctx.closest = NULL;
3795 closest_ctx.closest_dist = 0;
3796 closest_ctx.closest_num = 0;
3797 GNUNET_CONTAINER_multihashmap_iterate (pg->
3798 peers[pg_iter].allowed_peers,
3799 &find_closest_peers,
3801 if (closest_ctx.closest != NULL)
3803 GNUNET_assert (closest_ctx.closest_num < pg->total);
3804 proc (pg, pg_iter, closest_ctx.closest_num, list);
3812 * From the set of connections possible, choose at least num connections per
3813 * peer based on depth first traversal of peer connections. If DFS leaves
3814 * peers unconnected, ensure those peers get connections.
3816 * @param pg the peergroup we are dealing with
3817 * @param num how many connections at least should each peer have (if possible)?
3820 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3824 uint32_t starting_peer;
3825 uint32_t least_connections;
3826 uint32_t random_connection;
3828 unsigned int temp_count;
3829 struct PeerConnection *peer_iter;
3831 struct DFSContext dfs_ctx;
3832 GNUNET_HashCode second_hash;
3838 while ((count_workingset_connections (pg) < num * pg->total)
3839 && (count_allowed_connections (pg) > 0))
3841 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3843 least_connections = -1; /* Set to very high number */
3844 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3846 temp_count = count_connections(pg->peers[pg_iter].connect_peers_working_set_head);
3847 if (temp_count < least_connections)
3849 starting_peer = pg_iter;
3850 least_connections = temp_count;
3855 temp_count = count_connections(pg->peers[starting_peer].connect_peers_head);
3856 if (temp_count == 0)
3857 continue; /* FIXME: infinite loop? */
3859 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, temp_count);
3861 peer_iter = pg->peers[starting_peer].connect_peers_head;
3862 while (temp_count < random_connection)
3864 peer_iter = peer_iter->next;
3867 GNUNET_assert(peer_iter != NULL);
3868 add_connections(pg, starting_peer, peer_iter->index, WORKING_SET);
3869 remove_connections(pg, starting_peer, peer_iter->index, CONNECT);
3870 starting_peer = peer_iter->index;
3875 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3877 pg->peers[pg_iter].connect_peers_working_set =
3878 GNUNET_CONTAINER_multihashmap_create (num);
3883 while ((count_workingset_connections (pg) < num * pg->total)
3884 && (count_allowed_connections (pg) > 0))
3886 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3888 least_connections = -1; /* Set to very high number */
3889 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3891 if (GNUNET_CONTAINER_multihashmap_size
3892 (pg->peers[pg_iter].connect_peers_working_set) <
3895 starting_peer = pg_iter;
3897 GNUNET_CONTAINER_multihashmap_size (pg->
3899 [pg_iter].connect_peers_working_set);
3904 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
3910 /* Choose a random peer from the chosen peers set of connections to add */
3912 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3913 GNUNET_CONTAINER_multihashmap_size
3914 (pg->peers[starting_peer].connect_peers));
3915 dfs_ctx.first_uid = starting_peer;
3916 dfs_ctx.first = &pg->peers[starting_peer];
3918 dfs_ctx.current = 0;
3920 GNUNET_CONTAINER_multihashmap_iterate (pg->
3922 [starting_peer].connect_peers,
3923 &dfs_connect_iterator, &dfs_ctx);
3924 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
3925 hash_from_uid (dfs_ctx.second_uid, &second_hash);
3926 GNUNET_assert (GNUNET_YES ==
3927 GNUNET_CONTAINER_multihashmap_remove (pg->peers
3928 [starting_peer].connect_peers,
3932 [dfs_ctx.second_uid].daemon));
3933 starting_peer = dfs_ctx.second_uid;
3936 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3938 /* Remove the "old" connections */
3939 GNUNET_CONTAINER_multihashmap_destroy (pg->
3940 peers[pg_iter].connect_peers);
3941 /* And replace with the working set */
3942 pg->peers[pg_iter].connect_peers =
3943 pg->peers[pg_iter].connect_peers_working_set;
3950 * Internal callback for topology information for a particular peer.
3953 internal_topology_callback (void *cls,
3954 const struct GNUNET_PeerIdentity *peer,
3955 const struct GNUNET_TRANSPORT_ATS_Information
3958 struct CoreContext *core_ctx = cls;
3959 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
3961 if (peer == NULL) /* Either finished, or something went wrong */
3963 iter_ctx->completed++;
3964 iter_ctx->connected--;
3965 /* One core context allocated per iteration, must free! */
3966 GNUNET_free (core_ctx);
3970 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id,
3974 if (iter_ctx->completed == iter_ctx->total)
3976 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
3977 /* Once all are done, free the iteration context */
3978 GNUNET_free (iter_ctx);
3984 * Check running topology iteration tasks, if below max start a new one, otherwise
3985 * schedule for some time in the future.
3988 schedule_get_topology (void *cls,
3989 const struct GNUNET_SCHEDULER_TaskContext *tc)
3991 struct CoreContext *core_context = cls;
3992 struct TopologyIterateContext *topology_context =
3993 (struct TopologyIterateContext *) core_context->iter_context;
3994 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3997 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3999 #if VERBOSE_TESTING > 2
4000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4002 ("Delaying connect, we have too many outstanding connections!\n"));
4004 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4005 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4006 &schedule_get_topology, core_context);
4010 #if VERBOSE_TESTING > 2
4011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4012 _("Creating connection, outstanding_connections is %d\n"),
4013 outstanding_connects);
4015 topology_context->connected++;
4018 GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4019 &internal_topology_callback,
4022 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4023 internal_topology_callback (core_context, NULL, NULL);
4029 * Iterate over all (running) peers in the peer group, retrieve
4030 * all connections that each currently has.
4033 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
4034 GNUNET_TESTING_NotifyTopology cb, void *cls)
4036 struct TopologyIterateContext *topology_context;
4037 struct CoreContext *core_ctx;
4039 unsigned int total_count;
4041 /* Allocate a single topology iteration context */
4042 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4043 topology_context->topology_cb = cb;
4044 topology_context->cls = cls;
4046 for (i = 0; i < pg->total; i++)
4048 if (pg->peers[i].daemon->running == GNUNET_YES)
4050 /* Allocate one core context per core we need to connect to */
4051 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4052 core_ctx->daemon = pg->peers[i].daemon;
4053 /* Set back pointer to topology iteration context */
4054 core_ctx->iter_context = topology_context;
4055 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4059 if (total_count == 0)
4061 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4062 GNUNET_free (topology_context);
4065 topology_context->total = total_count;
4070 * Callback function to process statistic values.
4071 * This handler is here only really to insert a peer
4072 * identity (or daemon) so the statistics can be uniquely
4073 * tied to a single running peer.
4075 * @param cls closure
4076 * @param subsystem name of subsystem that created the statistic
4077 * @param name the name of the datum
4078 * @param value the current value
4079 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4080 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4083 internal_stats_callback (void *cls,
4084 const char *subsystem,
4085 const char *name, uint64_t value, int is_persistent)
4087 struct StatsCoreContext *core_context = cls;
4088 struct StatsIterateContext *stats_context =
4089 (struct StatsIterateContext *) core_context->iter_context;
4091 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4092 subsystem, name, value, is_persistent);
4096 * Internal continuation call for statistics iteration.
4098 * @param cls closure, the CoreContext for this iteration
4099 * @param success whether or not the statistics iterations
4100 * was canceled or not (we don't care)
4103 internal_stats_cont (void *cls, int success)
4105 struct StatsCoreContext *core_context = cls;
4106 struct StatsIterateContext *stats_context =
4107 (struct StatsIterateContext *) core_context->iter_context;
4109 stats_context->connected--;
4110 stats_context->completed++;
4112 if (stats_context->completed == stats_context->total)
4114 stats_context->cont (stats_context->cls, GNUNET_YES);
4115 GNUNET_free (stats_context);
4118 if (core_context->stats_handle != NULL)
4119 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4121 GNUNET_free (core_context);
4125 * Check running topology iteration tasks, if below max start a new one, otherwise
4126 * schedule for some time in the future.
4129 schedule_get_statistics (void *cls,
4130 const struct GNUNET_SCHEDULER_TaskContext *tc)
4132 struct StatsCoreContext *core_context = cls;
4133 struct StatsIterateContext *stats_context =
4134 (struct StatsIterateContext *) core_context->iter_context;
4136 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4139 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
4141 #if VERBOSE_TESTING > 2
4142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4144 ("Delaying connect, we have too many outstanding connections!\n"));
4146 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4147 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4148 &schedule_get_statistics, core_context);
4152 #if VERBOSE_TESTING > 2
4153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4154 _("Creating connection, outstanding_connections is %d\n"),
4155 outstanding_connects);
4158 stats_context->connected++;
4159 core_context->stats_handle =
4160 GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
4161 if (core_context->stats_handle == NULL)
4163 internal_stats_cont (core_context, GNUNET_NO);
4167 core_context->stats_get_handle =
4168 GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
4169 GNUNET_TIME_relative_get_forever (),
4170 &internal_stats_cont, &internal_stats_callback,
4172 if (core_context->stats_get_handle == NULL)
4173 internal_stats_cont (core_context, GNUNET_NO);
4178 struct DuplicateStats
4181 * Next item in the list
4183 struct DuplicateStats *next;
4186 * Nasty string, concatenation of relevant information.
4188 char *unique_string;
4192 * Check whether the combination of port/host/unix domain socket
4193 * already exists in the list of peers being checked for statistics.
4195 * @param pg the peergroup in question
4196 * @param specific_peer the peer we're concerned with
4197 * @param stats_list the list to return to the caller
4199 * @return GNUNET_YES if the statistics instance has been seen already,
4200 * GNUNET_NO if not (and we may have added it to the list)
4203 stats_check_existing (struct GNUNET_TESTING_PeerGroup *pg,
4204 struct PeerData *specific_peer,
4205 struct DuplicateStats **stats_list)
4207 struct DuplicateStats *pos;
4208 char *unix_domain_socket;
4209 unsigned long long port;
4212 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
4213 "single_statistics_per_host"))
4214 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
4218 GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg, "statistics",
4219 "unixpath", &unix_domain_socket))
4223 GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg, "statistics",
4226 GNUNET_free(unix_domain_socket);
4230 if (specific_peer->daemon->hostname != NULL)
4231 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
4232 unix_domain_socket, port);
4234 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
4238 if (0 == strcmp (to_match, pos->unique_string))
4240 GNUNET_free (unix_domain_socket);
4241 GNUNET_free (to_match);
4246 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
4247 pos->unique_string = to_match;
4248 pos->next = *stats_list;
4250 GNUNET_free (unix_domain_socket);
4255 * Iterate over all (running) peers in the peer group, retrieve
4256 * all statistics from each.
4259 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
4260 GNUNET_STATISTICS_Callback cont,
4261 GNUNET_TESTING_STATISTICS_Iterator proc,
4264 struct StatsIterateContext *stats_context;
4265 struct StatsCoreContext *core_ctx;
4267 unsigned int total_count;
4268 struct DuplicateStats *stats_list;
4269 struct DuplicateStats *pos;
4272 /* Allocate a single stats iteration context */
4273 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
4274 stats_context->cont = cont;
4275 stats_context->proc = proc;
4276 stats_context->cls = cls;
4279 for (i = 0; i < pg->total; i++)
4281 if ((pg->peers[i].daemon->running == GNUNET_YES)
4283 stats_check_existing (pg, &pg->peers[i], &stats_list)))
4285 /* Allocate one core context per core we need to connect to */
4286 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
4287 core_ctx->daemon = pg->peers[i].daemon;
4288 /* Set back pointer to topology iteration context */
4289 core_ctx->iter_context = stats_context;
4290 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
4295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4296 "Retrieving stats from %u total instances.\n", total_count);
4297 stats_context->total = total_count;
4298 if (stats_list != NULL)
4303 GNUNET_free (pos->unique_string);
4304 stats_list = pos->next;
4306 pos = stats_list->next;
4313 * There are many ways to connect peers that are supported by this function.
4314 * To connect peers in the same topology that was created via the
4315 * GNUNET_TESTING_create_topology, the topology variable must be set to
4316 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
4317 * a new instance of that topology will be generated and attempted to be
4318 * connected. This could result in some connections being impossible,
4319 * because some topologies are non-deterministic.
4321 * @param pg the peer group struct representing the running peers
4322 * @param topology which topology to connect the peers in
4323 * @param options options for connecting the topology
4324 * @param option_modifier modifier for options that take a parameter
4325 * @param notify_callback notification to be called once all connections completed
4326 * @param notify_cls closure for notification callback
4328 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
4331 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
4332 enum GNUNET_TESTING_Topology topology,
4333 enum GNUNET_TESTING_TopologyOption options,
4334 double option_modifier,
4335 GNUNET_TESTING_NotifyCompletion
4336 notify_callback, void *notify_cls)
4340 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4341 #if VERBOSE_TOPOLOGY
4342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4343 _("Creating clique CONNECT topology\n"));
4345 create_clique (pg, &add_connections, CONNECT);
4347 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4348 #if VERBOSE_TOPOLOGY
4349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4350 _("Creating small world (ring) CONNECT topology\n"));
4352 create_small_world_ring (pg, &add_connections, CONNECT);
4354 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4355 #if VERBOSE_TOPOLOGY
4356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4357 _("Creating small world (2d-torus) CONNECT topology\n"));
4359 create_small_world (pg, &add_connections, CONNECT);
4361 case GNUNET_TESTING_TOPOLOGY_RING:
4362 #if VERBOSE_TOPOLOGY
4363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4364 _("Creating ring CONNECT topology\n"));
4366 create_ring (pg, &add_connections, CONNECT);
4368 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4369 #if VERBOSE_TOPOLOGY
4370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4371 _("Creating 2d torus CONNECT topology\n"));
4373 create_2d_torus (pg, &add_connections, CONNECT);
4375 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4376 #if VERBOSE_TOPOLOGY
4377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4378 _("Creating Erdos-Renyi CONNECT topology\n"));
4380 create_erdos_renyi (pg, &add_connections, CONNECT);
4382 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4383 #if VERBOSE_TOPOLOGY
4384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4385 _("Creating InterNAT CONNECT topology\n"));
4387 create_nated_internet (pg, &add_connections, CONNECT);
4389 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4390 #if VERBOSE_TOPOLOGY
4391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4392 _("Creating Scale Free CONNECT topology\n"));
4394 create_scale_free (pg, &add_connections, CONNECT);
4396 case GNUNET_TESTING_TOPOLOGY_LINE:
4397 #if VERBOSE_TOPOLOGY
4398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4399 _("Creating straight line CONNECT topology\n"));
4401 create_line (pg, &add_connections, CONNECT);
4403 case GNUNET_TESTING_TOPOLOGY_NONE:
4404 #if VERBOSE_TOPOLOGY
4405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4406 _("Creating no CONNECT topology\n"));
4408 copy_allowed_topology (pg);
4411 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4413 ("Unknown topology specification, can't connect peers!\n"));
4414 return GNUNET_SYSERR;
4419 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
4420 #if VERBOSE_TOPOLOGY
4421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4423 ("Connecting random subset (%'.2f percent) of possible peers\n"),
4424 100 * option_modifier);
4426 choose_random_connections (pg, option_modifier);
4428 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
4429 #if VERBOSE_TOPOLOGY
4430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4431 _("Connecting a minimum of %u peers each (if possible)\n"),
4432 (unsigned int) option_modifier);
4434 choose_minimum (pg, (unsigned int) option_modifier);
4436 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
4437 #if VERBOSE_TOPOLOGY
4438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4440 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
4441 (unsigned int) option_modifier);
4444 perform_dfs (pg, (int) option_modifier);
4447 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
4448 #if VERBOSE_TOPOLOGY
4449 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4451 ("Finding additional %u closest peers each (if possible)\n"),
4452 (unsigned int) option_modifier);
4455 add_closest (pg, (unsigned int) option_modifier,
4456 &add_connections, CONNECT);
4459 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
4461 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
4467 return connect_topology (pg, notify_callback, notify_cls);
4471 * Callback that is called whenever a hostkey is generated
4472 * for a peer. Call the real callback and decrement the
4473 * starting counter for the peergroup.
4475 * @param cls closure
4476 * @param id identifier for the daemon, NULL on error
4477 * @param d handle for the daemon
4478 * @param emsg error message (NULL on success)
4481 internal_hostkey_callback (void *cls,
4482 const struct GNUNET_PeerIdentity *id,
4483 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4485 struct InternalStartContext *internal_context = cls;
4486 internal_context->peer->pg->starting--;
4487 internal_context->peer->pg->started++;
4488 if (internal_context->hostkey_callback != NULL)
4489 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
4491 else if (internal_context->peer->pg->started ==
4492 internal_context->peer->pg->total)
4494 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
4495 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
4500 * Callback that is called whenever a peer has finished starting.
4501 * Call the real callback and decrement the starting counter
4502 * for the peergroup.
4504 * @param cls closure
4505 * @param id identifier for the daemon, NULL on error
4507 * @param d handle for the daemon
4508 * @param emsg error message (NULL on success)
4511 internal_startup_callback (void *cls,
4512 const struct GNUNET_PeerIdentity *id,
4513 const struct GNUNET_CONFIGURATION_Handle *cfg,
4514 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4516 struct InternalStartContext *internal_context = cls;
4517 internal_context->peer->pg->starting--;
4518 if (internal_context->start_cb != NULL)
4519 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
4524 internal_continue_startup (void *cls,
4525 const struct GNUNET_SCHEDULER_TaskContext *tc)
4527 struct InternalStartContext *internal_context = cls;
4529 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4534 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
4536 internal_context->peer->pg->starting++;
4537 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
4541 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4542 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4543 &internal_continue_startup,
4550 * Callback for informing us about a successful
4551 * or unsuccessful churn start call.
4553 * @param cls a ChurnContext
4554 * @param id the peer identity of the started peer
4555 * @param cfg the handle to the configuration of the peer
4556 * @param d handle to the daemon for the peer
4557 * @param emsg NULL on success, non-NULL on failure
4561 churn_start_callback (void *cls,
4562 const struct GNUNET_PeerIdentity *id,
4563 const struct GNUNET_CONFIGURATION_Handle *cfg,
4564 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4566 struct ChurnRestartContext *startup_ctx = cls;
4567 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
4569 unsigned int total_left;
4570 char *error_message;
4572 error_message = NULL;
4575 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4576 "Churn stop callback failed with error `%s'\n", emsg);
4577 churn_ctx->num_failed_start++;
4581 churn_ctx->num_to_start--;
4585 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4586 "Started peer, %d left.\n", churn_ctx->num_to_start);
4590 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
4591 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4593 if (total_left == 0)
4595 if ((churn_ctx->num_failed_stop > 0)
4596 || (churn_ctx->num_failed_start > 0))
4597 GNUNET_asprintf (&error_message,
4598 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4599 churn_ctx->num_failed_start,
4600 churn_ctx->num_failed_stop);
4601 churn_ctx->cb (churn_ctx->cb_cls, error_message);
4602 GNUNET_free_non_null (error_message);
4603 GNUNET_free (churn_ctx);
4604 GNUNET_free (startup_ctx);
4610 schedule_churn_restart (void *cls,
4611 const struct GNUNET_SCHEDULER_TaskContext *tc)
4613 struct PeerRestartContext *peer_restart_ctx = cls;
4614 struct ChurnRestartContext *startup_ctx =
4615 peer_restart_ctx->churn_restart_ctx;
4617 if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
4618 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4619 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4620 &schedule_churn_restart, peer_restart_ctx);
4623 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
4624 startup_ctx->timeout,
4625 &churn_start_callback,
4627 GNUNET_free (peer_restart_ctx);
4632 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4634 struct InternalStartContext *internal_context = cls;
4636 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4641 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
4643 internal_context->peer->pg->starting++;
4644 internal_context->peer->daemon =
4645 GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
4646 internal_context->timeout,
4647 internal_context->hostname,
4648 internal_context->username,
4649 internal_context->sshport,
4650 internal_context->hostkey,
4651 &internal_hostkey_callback,
4653 &internal_startup_callback,
4658 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4659 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4660 &internal_start, internal_context);
4665 * Function which continues a peer group starting up
4666 * after successfully generating hostkeys for each peer.
4668 * @param pg the peer group to continue starting
4672 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg)
4677 for (i = 0; i < pg->total; i++)
4679 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
4680 &pg->peers[i].internal_context);
4681 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
4686 * Start count gnunet instances with the same set of transports and
4687 * applications. The port numbers (any option called "PORT") will be
4688 * adjusted to ensure that no two peers running on the same system
4689 * have the same port(s) in their respective configurations.
4691 * @param cfg configuration template to use
4692 * @param total number of daemons to start
4693 * @param timeout total time allowed for peers to start
4694 * @param hostkey_callback function to call on each peers hostkey generation
4695 * if NULL, peers will be started by this call, if non-null,
4696 * GNUNET_TESTING_daemons_continue_startup must be called after
4697 * successful hostkey generation
4698 * @param hostkey_cls closure for hostkey callback
4699 * @param cb function to call on each daemon that was started
4700 * @param cb_cls closure for cb
4701 * @param connect_callback function to call each time two hosts are connected
4702 * @param connect_callback_cls closure for connect_callback
4703 * @param hostnames linked list of host structs to use to start peers on
4704 * (NULL to run on localhost only)
4706 * @return NULL on error, otherwise handle to control peer group
4708 struct GNUNET_TESTING_PeerGroup *
4709 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
4711 struct GNUNET_TIME_Relative timeout,
4712 GNUNET_TESTING_NotifyHostkeyCreated
4713 hostkey_callback, void *hostkey_cls,
4714 GNUNET_TESTING_NotifyDaemonRunning cb,
4716 GNUNET_TESTING_NotifyConnection
4717 connect_callback, void *connect_callback_cls,
4718 const struct GNUNET_TESTING_Host *hostnames)
4720 struct GNUNET_TESTING_PeerGroup *pg;
4721 const struct GNUNET_TESTING_Host *hostpos;
4727 const char *hostname;
4728 const char *username;
4729 char *baseservicehome;
4730 char *newservicehome;
4732 char *hostkeys_file;
4735 struct GNUNET_DISK_FileHandle *fd;
4736 struct GNUNET_CONFIGURATION_Handle *pcfg;
4738 unsigned int hostcnt;
4745 uint64_t total_hostkeys;
4746 struct GNUNET_OS_Process *proc;
4756 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
4758 pg->notify_connection = connect_callback;
4759 pg->notify_connection_cls = connect_callback_cls;
4761 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
4762 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
4763 if (NULL != hostnames)
4766 hostpos = hostnames;
4767 while (hostpos != NULL)
4769 hostpos = hostpos->next;
4772 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4775 hostpos = hostnames;
4776 while (hostpos != NULL)
4778 pg->hosts[off].minport = LOW_PORT;
4779 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
4780 if (hostpos->username != NULL)
4781 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
4782 pg->hosts[off].sshport = hostpos->port;
4783 hostpos = hostpos->next;
4793 pg->num_hosts = off;
4797 /* skip leading spaces */
4798 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
4801 while ('\0' != *rpos)
4803 if (isspace ((unsigned char) *rpos))
4807 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4809 start = GNUNET_strdup (hostnames);
4811 while ('\0' != *pos)
4813 if (isspace ((unsigned char) *pos))
4816 if (strlen (start) > 0)
4818 pg->hosts[off].minport = LOW_PORT;
4819 pg->hosts[off++].hostname = start;
4825 if (strlen (start) > 0)
4827 pg->hosts[off].minport = LOW_PORT;
4828 pg->hosts[off++].hostname = start;
4832 GNUNET_free (start);
4833 GNUNET_free (pg->hosts);
4837 minport = 0; /* make gcc happy */
4846 /* Create the servicehome directory for each remote peer */
4847 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
4849 for (i = 0; i < pg->num_hosts; i++)
4852 if (NULL != pg->hosts[i].username)
4853 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username, pg->hosts[i].hostname);
4855 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
4856 if (pg->hosts[i].sshport != 0)
4858 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
4859 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
4860 "ssh", "-P", ssh_port_str,
4864 arg, "mkdir -p", baseservicehome, NULL);
4867 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
4868 "ssh", arg, "mkdir -p", baseservicehome, NULL);
4869 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Creating remote dir with command ssh %s %s %s\n", arg, " mkdir -p ", baseservicehome);
4870 GNUNET_OS_process_wait(proc);
4872 GNUNET_free(baseservicehome);
4874 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "HOSTKEYSFILE",
4877 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
4878 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!");
4881 /* Check hostkey file size, read entire thing into memory */
4882 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
4883 GNUNET_DISK_PERM_NONE);
4886 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
4890 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
4893 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found file size %llu for hostkeys, expect hostkeys to be size %d\n", fs, HOSTKEYFILESIZE);
4895 if (fs % HOSTKEYFILESIZE != 0)
4897 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "File size %llu seems incorrect for hostkeys...\n", fs);
4901 total_hostkeys = fs / HOSTKEYFILESIZE;
4902 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Will read %llu hostkeys from file\n", total_hostkeys);
4903 pg->hostkey_data = GNUNET_malloc_large (fs);
4904 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
4905 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
4908 GNUNET_free(hostkeys_file);
4911 for (off = 0; off < total; off++)
4915 hostname = pg->hosts[off % hostcnt].hostname;
4916 username = pg->hosts[off % hostcnt].username;
4917 sshport = pg->hosts[off % hostcnt].sshport;
4918 pcfg = make_config (cfg,
4919 &pg->hosts[off % hostcnt].minport,
4920 &upnum, hostname, &fdnum);
4927 pcfg = make_config (cfg, &minport, &upnum, hostname, &fdnum);
4932 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4934 ("Could not create configuration for peer number %u on `%s'!\n"),
4935 off, hostname == NULL ? "localhost" : hostname);
4940 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
4943 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
4944 GNUNET_free (baseservicehome);
4948 tmpdir = getenv ("TMPDIR");
4949 tmpdir = tmpdir ? tmpdir : "/tmp";
4950 GNUNET_asprintf (&newservicehome,
4952 tmpdir, "gnunet-testing-test-test", off);
4954 GNUNET_CONFIGURATION_set_value_string (pcfg,
4956 "SERVICEHOME", newservicehome);
4957 GNUNET_free (newservicehome);
4958 pg->peers[off].cfg = pcfg;
4960 /* Can we do this later? */
4961 pg->peers[off].allowed_peers =
4962 GNUNET_CONTAINER_multihashmap_create (total);
4963 pg->peers[off].connect_peers =
4964 GNUNET_CONTAINER_multihashmap_create (total);
4965 pg->peers[off].blacklisted_peers =
4966 GNUNET_CONTAINER_multihashmap_create (total);
4969 pg->peers[off].pg = pg;
4970 pg->peers[off].internal_context.peer = &pg->peers[off];
4971 pg->peers[off].internal_context.timeout = timeout;
4972 pg->peers[off].internal_context.hostname = hostname;
4973 pg->peers[off].internal_context.username = username;
4974 pg->peers[off].internal_context.sshport = sshport;
4975 if (pg->hostkey_data != NULL)
4976 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off * HOSTKEYFILESIZE];
4977 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
4978 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
4979 pg->peers[off].internal_context.start_cb = cb;
4980 pg->peers[off].internal_context.start_cb_cls = cb_cls;
4982 GNUNET_SCHEDULER_add_now (&internal_start,
4983 &pg->peers[off].internal_context);
4990 * Get a daemon by number, so callers don't have to do nasty
4991 * offsetting operation.
4993 struct GNUNET_TESTING_Daemon *
4994 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
4995 unsigned int position)
4997 if (position < pg->total)
4998 return pg->peers[position].daemon;
5004 * Get a daemon by peer identity, so callers can
5005 * retrieve the daemon without knowing it's offset.
5007 * @param pg the peer group to retrieve the daemon from
5008 * @param peer_id the peer identity of the daemon to retrieve
5010 * @return the daemon on success, or NULL if no such peer identity is found
5012 struct GNUNET_TESTING_Daemon *
5013 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
5014 struct GNUNET_PeerIdentity *peer_id)
5018 for (i = 0; i < pg->total; i++)
5021 memcmp (&pg->peers[i].daemon->id, peer_id,
5022 sizeof (struct GNUNET_PeerIdentity)))
5023 return pg->peers[i].daemon;
5030 * Prototype of a function that will be called when a
5031 * particular operation was completed the testing library.
5033 * @param cls closure (a struct RestartContext)
5034 * @param id id of the peer that was restarted
5035 * @param cfg handle to the configuration of the peer
5036 * @param d handle to the daemon that was restarted
5037 * @param emsg NULL on success
5040 restart_callback (void *cls,
5041 const struct GNUNET_PeerIdentity *id,
5042 const struct GNUNET_CONFIGURATION_Handle *cfg,
5043 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5045 struct RestartContext *restart_context = cls;
5049 restart_context->peers_restarted++;
5053 restart_context->peers_restart_failed++;
5056 if (restart_context->peers_restarted == restart_context->peer_group->total)
5058 restart_context->callback (restart_context->callback_cls, NULL);
5059 GNUNET_free (restart_context);
5061 else if (restart_context->peers_restart_failed +
5062 restart_context->peers_restarted ==
5063 restart_context->peer_group->total)
5065 restart_context->callback (restart_context->callback_cls,
5066 "Failed to restart peers!");
5067 GNUNET_free (restart_context);
5073 * Callback for informing us about a successful
5074 * or unsuccessful churn stop call.
5076 * @param cls a ChurnContext
5077 * @param emsg NULL on success, non-NULL on failure
5081 churn_stop_callback (void *cls, const char *emsg)
5083 struct ShutdownContext *shutdown_ctx = cls;
5084 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
5085 unsigned int total_left;
5086 char *error_message;
5088 error_message = NULL;
5089 shutdown_ctx->outstanding--;
5093 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5094 "Churn stop callback failed with error `%s'\n", emsg);
5095 churn_ctx->num_failed_stop++;
5099 churn_ctx->num_to_stop--;
5103 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5104 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
5107 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
5108 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5110 if (total_left == 0)
5112 if ((churn_ctx->num_failed_stop > 0)
5113 || (churn_ctx->num_failed_start > 0))
5115 GNUNET_asprintf (&error_message,
5116 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5117 churn_ctx->num_failed_start,
5118 churn_ctx->num_failed_stop);
5120 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5121 GNUNET_free_non_null (error_message);
5122 GNUNET_free (churn_ctx);
5123 GNUNET_free (shutdown_ctx);
5128 * Count the number of running peers.
5130 * @param pg handle for the peer group
5132 * @return the number of currently running peers in the peer group
5135 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
5138 unsigned int running = 0;
5139 for (i = 0; i < pg->total; i++)
5141 if (pg->peers[i].daemon->running == GNUNET_YES)
5143 GNUNET_assert (running != -1);
5151 * Task to rate limit the number of outstanding peer shutdown
5152 * requests. This is necessary for making sure we don't do
5153 * too many ssh connections at once, but is generally nicer
5154 * to any system as well (graduated task starts, as opposed
5155 * to calling gnunet-arm N times all at once).
5158 schedule_churn_shutdown_task (void *cls,
5159 const struct GNUNET_SCHEDULER_TaskContext *tc)
5161 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5162 struct ShutdownContext *shutdown_ctx;
5164 GNUNET_assert (peer_shutdown_ctx != NULL);
5165 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5166 GNUNET_assert (shutdown_ctx != NULL);
5168 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
5169 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5170 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5171 &schedule_churn_shutdown_task,
5175 shutdown_ctx->outstanding++;
5176 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5177 shutdown_ctx->timeout, shutdown_ctx->cb,
5178 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5179 GNUNET_free (peer_shutdown_ctx);
5184 * Simulate churn by stopping some peers (and possibly
5185 * re-starting others if churn is called multiple times). This
5186 * function can only be used to create leave-join churn (peers "never"
5187 * leave for good). First "voff" random peers that are currently
5188 * online will be taken offline; then "von" random peers that are then
5189 * offline will be put back online. No notifications will be
5190 * generated for any of these operations except for the callback upon
5193 * @param pg handle for the peer group
5194 * @param voff number of peers that should go offline
5195 * @param von number of peers that should come back online;
5196 * must be zero on first call (since "testbed_start"
5197 * always starts all of the peers)
5198 * @param timeout how long to wait for operations to finish before
5200 * @param cb function to call at the end
5201 * @param cb_cls closure for cb
5204 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
5207 struct GNUNET_TIME_Relative timeout,
5208 GNUNET_TESTING_NotifyCompletion cb,
5211 struct ChurnContext *churn_ctx;
5212 struct ShutdownContext *shutdown_ctx;
5213 struct PeerShutdownContext *peer_shutdown_ctx;
5214 struct PeerRestartContext *peer_restart_ctx;
5215 struct ChurnRestartContext *churn_startup_ctx;
5217 unsigned int running;
5218 unsigned int stopped;
5219 unsigned int total_running;
5220 unsigned int total_stopped;
5222 unsigned int *running_arr;
5223 unsigned int *stopped_arr;
5224 unsigned int *running_permute;
5225 unsigned int *stopped_permute;
5230 if ((von == 0) && (voff == 0)) /* No peers at all? */
5236 for (i = 0; i < pg->total; i++)
5238 if (pg->peers[i].daemon->running == GNUNET_YES)
5240 GNUNET_assert (running != -1);
5245 GNUNET_assert (stopped != -1);
5252 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5253 "Trying to stop more peers than are currently running!\n");
5254 cb (cb_cls, "Trying to stop more peers than are currently running!");
5260 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5261 "Trying to start more peers than are currently stopped!\n");
5262 cb (cb_cls, "Trying to start more peers than are currently stopped!");
5266 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5270 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
5274 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
5276 running_permute = NULL;
5277 stopped_permute = NULL;
5281 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, running);
5284 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, stopped);
5286 total_running = running;
5287 total_stopped = stopped;
5291 churn_ctx->num_to_start = von;
5292 churn_ctx->num_to_stop = voff;
5294 churn_ctx->cb_cls = cb_cls;
5296 for (i = 0; i < pg->total; i++)
5298 if (pg->peers[i].daemon->running == GNUNET_YES)
5300 GNUNET_assert ((running_arr != NULL) && (total_running > running));
5301 running_arr[running] = i;
5306 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
5307 stopped_arr[stopped] = i;
5312 GNUNET_assert (running >= voff);
5315 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5316 shutdown_ctx->cb = &churn_stop_callback;
5317 shutdown_ctx->cb_cls = churn_ctx;
5318 shutdown_ctx->total_peers = voff;
5319 shutdown_ctx->timeout = timeout;
5322 for (i = 0; i < voff; i++)
5325 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
5326 running_permute[i]);
5328 GNUNET_assert (running_arr != NULL);
5329 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
5330 peer_shutdown_ctx->daemon =
5331 pg->peers[running_arr[running_permute[i]]].daemon;
5332 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
5333 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
5337 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
5339 &churn_stop_callback, churn_ctx,
5340 GNUNET_NO, GNUNET_YES); */
5343 GNUNET_assert (stopped >= von);
5346 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
5347 churn_startup_ctx->churn_ctx = churn_ctx;
5348 churn_startup_ctx->timeout = timeout;
5350 for (i = 0; i < von; i++)
5353 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
5354 stopped_permute[i]);
5356 GNUNET_assert (stopped_arr != NULL);
5357 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
5358 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
5359 peer_restart_ctx->daemon =
5360 pg->peers[stopped_arr[stopped_permute[i]]].daemon;
5361 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
5363 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
5364 timeout, &churn_start_callback, churn_ctx); */
5367 GNUNET_free_non_null (running_arr);
5368 GNUNET_free_non_null (stopped_arr);
5369 GNUNET_free_non_null (running_permute);
5370 GNUNET_free_non_null (stopped_permute);
5375 * Restart all peers in the given group.
5377 * @param pg the handle to the peer group
5378 * @param callback function to call on completion (or failure)
5379 * @param callback_cls closure for the callback function
5382 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
5383 GNUNET_TESTING_NotifyCompletion callback,
5386 struct RestartContext *restart_context;
5391 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
5392 restart_context->peer_group = pg;
5393 restart_context->peers_restarted = 0;
5394 restart_context->callback = callback;
5395 restart_context->callback_cls = callback_cls;
5397 for (off = 0; off < pg->total; off++)
5399 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
5400 &restart_callback, restart_context);
5406 * Start or stop an individual peer from the given group.
5408 * @param pg handle to the peer group
5409 * @param offset which peer to start or stop
5410 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
5411 * @param timeout how long to wait for shutdown
5412 * @param cb function to call at the end
5413 * @param cb_cls closure for cb
5416 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
5417 unsigned int offset,
5419 struct GNUNET_TIME_Relative timeout,
5420 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5422 struct ShutdownContext *shutdown_ctx;
5423 struct ChurnRestartContext *startup_ctx;
5424 struct ChurnContext *churn_ctx;
5426 if (GNUNET_NO == desired_status)
5428 if (NULL != pg->peers[offset].daemon)
5430 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5431 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5432 churn_ctx->num_to_start = 0;
5433 churn_ctx->num_to_stop = 1;
5435 churn_ctx->cb_cls = cb_cls;
5436 shutdown_ctx->cb_cls = churn_ctx;
5437 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon,
5438 timeout, &churn_stop_callback,
5439 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5442 else if (GNUNET_YES == desired_status)
5444 if (NULL == pg->peers[offset].daemon)
5446 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
5447 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5448 churn_ctx->num_to_start = 1;
5449 churn_ctx->num_to_stop = 0;
5451 churn_ctx->cb_cls = cb_cls;
5452 startup_ctx->churn_ctx = churn_ctx;
5453 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
5454 timeout, &churn_start_callback,
5464 * Callback for shutting down peers in a peer group.
5466 * @param cls closure (struct ShutdownContext)
5467 * @param emsg NULL on success
5470 internal_shutdown_callback (void *cls, const char *emsg)
5472 struct ShutdownContext *shutdown_ctx = cls;
5474 shutdown_ctx->outstanding--;
5477 shutdown_ctx->peers_down++;
5481 shutdown_ctx->peers_failed++;
5484 if ((shutdown_ctx->cb != NULL)
5485 && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed ==
5486 shutdown_ctx->total_peers))
5488 if (shutdown_ctx->peers_failed > 0)
5489 shutdown_ctx->cb (shutdown_ctx->cb_cls,
5490 "Not all peers successfully shut down!");
5492 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
5493 GNUNET_free (shutdown_ctx);
5499 * Task to rate limit the number of outstanding peer shutdown
5500 * requests. This is necessary for making sure we don't do
5501 * too many ssh connections at once, but is generally nicer
5502 * to any system as well (graduated task starts, as opposed
5503 * to calling gnunet-arm N times all at once).
5506 schedule_shutdown_task (void *cls,
5507 const struct GNUNET_SCHEDULER_TaskContext *tc)
5509 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5510 struct ShutdownContext *shutdown_ctx;
5512 GNUNET_assert (peer_shutdown_ctx != NULL);
5513 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5514 GNUNET_assert (shutdown_ctx != NULL);
5516 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
5517 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5518 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5519 &schedule_shutdown_task, peer_shutdown_ctx);
5522 shutdown_ctx->outstanding++;
5523 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5524 shutdown_ctx->timeout,
5525 &internal_shutdown_callback, shutdown_ctx,
5526 GNUNET_YES, GNUNET_NO);
5527 GNUNET_free (peer_shutdown_ctx);
5532 * Shutdown all peers started in the given group.
5534 * @param pg handle to the peer group
5535 * @param timeout how long to wait for shutdown
5536 * @param cb callback to notify upon success or failure
5537 * @param cb_cls closure for cb
5540 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
5541 struct GNUNET_TIME_Relative timeout,
5542 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5545 struct ShutdownContext *shutdown_ctx;
5546 struct PeerShutdownContext *peer_shutdown_ctx;
5548 struct PeerConnection *conn_iter;
5549 struct PeerConnection *temp_conn;
5552 GNUNET_assert (pg->total > 0);
5554 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5555 shutdown_ctx->cb = cb;
5556 shutdown_ctx->cb_cls = cb_cls;
5557 shutdown_ctx->total_peers = pg->total;
5558 shutdown_ctx->timeout = timeout;
5559 /* shtudown_ctx->outstanding = 0; */
5561 for (off = 0; off < pg->total; off++)
5563 GNUNET_assert (NULL != pg->peers[off].daemon);
5564 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
5565 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
5566 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
5567 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
5569 if (NULL != pg->peers[off].cfg)
5570 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
5572 conn_iter = pg->peers[off].allowed_peers_head;
5573 while (conn_iter != NULL)
5575 temp_conn = conn_iter->next;
5576 GNUNET_free(conn_iter);
5577 conn_iter = temp_conn;
5580 conn_iter = pg->peers[off].connect_peers_head;
5581 while (conn_iter != NULL)
5583 temp_conn = conn_iter->next;
5584 GNUNET_free(conn_iter);
5585 conn_iter = temp_conn;
5588 conn_iter = pg->peers[off].blacklisted_peers_head;
5589 while (conn_iter != NULL)
5591 temp_conn = conn_iter->next;
5592 GNUNET_free(conn_iter);
5593 conn_iter = temp_conn;
5596 conn_iter = pg->peers[off].connect_peers_working_set_head;
5597 while (conn_iter != NULL)
5599 temp_conn = conn_iter->next;
5600 GNUNET_free(conn_iter);
5601 conn_iter = temp_conn;
5604 if (pg->peers[off].allowed_peers != NULL)
5605 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
5606 if (pg->peers[off].connect_peers != NULL)
5607 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
5608 if (pg->peers[off].blacklisted_peers != NULL)
5609 GNUNET_CONTAINER_multihashmap_destroy (pg->
5610 peers[off].blacklisted_peers);
5613 GNUNET_free (pg->peers);
5614 GNUNET_free_non_null(pg->hostkey_data);
5615 for (off = 0; off < pg->num_hosts; off++)
5617 GNUNET_free (pg->hosts[off].hostname);
5618 GNUNET_free_non_null (pg->hosts[off].username);
5620 GNUNET_free_non_null (pg->hosts);
5625 /* end of testing_group.c */