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 first_list = &pg->peers[first].allowed_peers_head;
1329 second_list = &pg->peers[second].allowed_peers_head;
1330 first_tail = &pg->peers[first].allowed_peers_tail;
1331 second_tail = &pg->peers[second].allowed_peers_tail;
1334 first_list = &pg->peers[first].connect_peers_head;
1335 second_list = &pg->peers[second].connect_peers_head;
1336 first_tail = &pg->peers[first].connect_peers_tail;
1337 second_tail = &pg->peers[second].connect_peers_tail;
1340 first_list = &pg->peers[first].blacklisted_peers_head;
1341 second_list = &pg->peers[second].blacklisted_peers_head;
1342 first_tail = &pg->peers[first].blacklisted_peers_tail;
1343 second_tail = &pg->peers[second].blacklisted_peers_tail;
1346 first_list = &pg->peers[first].connect_peers_working_set_head;
1347 second_list = &pg->peers[second].connect_peers_working_set_head;
1348 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1349 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1355 add_first = GNUNET_YES;
1356 add_second = GNUNET_YES;
1358 first_iter = *first_list;
1359 while (first_iter != NULL)
1361 if (first_iter->index == second)
1363 add_first = GNUNET_NO;
1366 first_iter = first_iter->next;
1369 second_iter = *second_list;
1370 while (second_iter != NULL)
1372 if (second_iter->index == first)
1374 add_second = GNUNET_NO;
1377 second_iter = second_iter->next;
1381 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1384 add_first = GNUNET_YES;
1388 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1391 add_second = GNUNET_YES;
1399 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1400 new_first->index = second;
1401 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1403 new_first->next = *first_list;
1404 *first_list = new_first;*/
1406 GNUNET_assert (GNUNET_OK ==
1407 GNUNET_CONTAINER_multihashmap_put (pg->
1409 [first].blacklisted_peers,
1412 peers[second].daemon,
1413 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1415 pg->peers[first].num_connections++;
1422 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1423 new_second->index = first;
1424 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1426 new_second->next = *second_list;
1427 *second_list = new_second;
1430 GNUNET_assert (GNUNET_OK ==
1431 GNUNET_CONTAINER_multihashmap_put (pg->
1433 [second].blacklisted_peers,
1436 peers[first].daemon,
1437 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1439 pg->peers[second].num_connections++;
1448 * Scale free network construction as described in:
1450 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1452 * Start with a network of "one" peer, then progressively add
1453 * peers up to the total number. At each step, iterate over
1454 * all possible peers and connect new peer based on number of
1455 * existing connections of the target peer.
1457 * @param pg the peer group we are dealing with
1458 * @param proc the connection processor to use
1459 * @param list the peer list to use
1461 * @return the number of connections created
1464 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg,
1465 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1468 unsigned int total_connections;
1469 unsigned int outer_count;
1471 unsigned int previous_total_connections;
1475 GNUNET_assert (pg->total > 1);
1477 /* Add a connection between the first two nodes */
1478 total_connections = proc (pg, 0, 1, list);
1480 for (outer_count = 1; outer_count < pg->total; outer_count++)
1482 previous_total_connections = total_connections;
1483 for (i = 0; i < outer_count; i++)
1486 pg->peers[i].num_connections /
1487 (double) previous_total_connections;
1490 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1491 UINT64_MAX)) / ((double) UINT64_MAX);
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1494 "Considering connecting peer %d to peer %d\n",
1497 if (random < probability)
1500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1501 "Connecting peer %d to peer %d\n", outer_count, i);
1503 total_connections += proc (pg, outer_count, i, list);
1508 return total_connections;
1512 * Create a topology given a peer group (set of running peers)
1513 * and a connection processor. Creates a small world topology
1514 * according to the rewired ring construction. The basic
1515 * behavior is that a ring topology is created, but with some
1516 * probability instead of connecting a peer to the next
1517 * neighbor in the ring a connection will be created to a peer
1518 * selected uniformly at random. We use the TESTING
1519 * PERCENTAGE option to specify what number of
1520 * connections each peer should have. Default is 2,
1521 * which makes the ring, any given number is multiplied by
1522 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1523 * each peer have on average 2logn connections. The additional
1524 * connections are made at increasing distance around the ring
1525 * from the original peer, or to random peers based on the re-
1526 * wiring probability. The TESTING
1527 * PROBABILITY option is used as the probability that a given
1528 * connection is rewired.
1530 * @param pg the peergroup to create the topology on
1531 * @param proc the connection processor to call to actually set
1532 * up connections between two peers
1533 * @param list the peer list to use
1535 * @return the number of connections that were set up
1539 create_small_world_ring (struct GNUNET_TESTING_PeerGroup *pg,
1540 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1544 unsigned int natLog;
1545 unsigned int randomPeer;
1546 double random, logNModifier, probability;
1547 unsigned int smallWorldConnections;
1552 unsigned int useAnd;
1553 int connect_attempts;
1555 logNModifier = 0.5; /* FIXME: default value? */
1556 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1561 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1562 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1564 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1565 p_string, "LOGNMODIFIER", "TESTING");
1566 GNUNET_free (p_string);
1568 probability = 0.5; /* FIXME: default percentage? */
1569 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1574 if (sscanf (p_string, "%lf", &probability) != 1)
1575 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1577 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1578 p_string, "PERCENTAGE", "TESTING");
1579 GNUNET_free (p_string);
1581 natLog = log (pg->total);
1582 connsPerPeer = ceil (natLog * logNModifier);
1584 if (connsPerPeer % 2 == 1)
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."), connsPerPeer);
1589 smallWorldConnections = 0;
1590 connect_attempts = 0;
1591 for (i = 0; i < pg->total; i++)
1594 max = i + connsPerPeer / 2;
1595 min = i - connsPerPeer / 2;
1597 if (max > pg->total - 1)
1599 max = max - pg->total;
1605 min = pg->total - 1 + min;
1609 for (j = 0; j < connsPerPeer / 2; j++)
1613 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1614 UINT64_MAX) / ((double) UINT64_MAX));
1615 if (random < probability)
1617 /* Connect to uniformly selected random peer */
1619 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1621 while ((((randomPeer < max) && (randomPeer > min))
1622 && (useAnd == 0)) || (((randomPeer > min)
1623 || (randomPeer < max))
1627 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1630 smallWorldConnections += proc (pg, i, randomPeer, list);
1634 nodeToConnect = i + j + 1;
1635 if (nodeToConnect > pg->total - 1)
1637 nodeToConnect = nodeToConnect - pg->total;
1639 connect_attempts += proc (pg, i, nodeToConnect, list);
1645 connect_attempts += smallWorldConnections;
1647 return connect_attempts;
1651 * Create a topology given a peer group (set of running peers)
1652 * and a connection processor.
1654 * @param pg the peergroup to create the topology on
1655 * @param proc the connection processor to call to actually set
1656 * up connections between two peers
1657 * @param list the peer list to use
1659 * @return the number of connections that were set up
1663 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg,
1664 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1666 unsigned int outer_count, inner_count;
1667 unsigned int cutoff;
1668 int connect_attempts;
1669 double nat_percentage;
1672 nat_percentage = 0.6; /* FIXME: default percentage? */
1673 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1678 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1679 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1681 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1682 p_string, "PERCENTAGE", "TESTING");
1683 GNUNET_free (p_string);
1688 cutoff = (unsigned int) (nat_percentage * pg->total);
1690 connect_attempts = 0;
1692 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1694 for (inner_count = outer_count + 1; inner_count < pg->total;
1697 if ((outer_count > cutoff) || (inner_count > cutoff))
1700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1701 "Connecting peer %d to peer %d\n",
1702 outer_count, inner_count);
1704 connect_attempts += proc (pg, outer_count, inner_count, list);
1709 return connect_attempts;
1714 * Create a topology given a peer group (set of running peers)
1715 * and a connection processor.
1717 * @param pg the peergroup to create the topology on
1718 * @param proc the connection processor to call to actually set
1719 * up connections between two peers
1720 * @param list the peer list to use
1722 * @return the number of connections that were set up
1726 create_small_world (struct GNUNET_TESTING_PeerGroup *pg,
1727 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1729 unsigned int i, j, k;
1730 unsigned int square;
1733 unsigned int toggle = 1;
1734 unsigned int nodeToConnect;
1735 unsigned int natLog;
1736 unsigned int node1Row;
1737 unsigned int node1Col;
1738 unsigned int node2Row;
1739 unsigned int node2Col;
1740 unsigned int distance;
1741 double probability, random, percentage;
1742 unsigned int smallWorldConnections;
1743 unsigned int small_world_it;
1745 int connect_attempts;
1746 square = floor (sqrt (pg->total));
1750 percentage = 0.5; /* FIXME: default percentage? */
1751 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1756 if (sscanf (p_string, "%lf", &percentage) != 1)
1757 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1759 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1760 p_string, "PERCENTAGE", "TESTING");
1761 GNUNET_free (p_string);
1763 if (percentage < 0.0)
1765 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1767 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1768 "PERCENTAGE", "TESTING", percentage);
1771 probability = 0.5; /* FIXME: default percentage? */
1772 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1777 if (sscanf (p_string, "%lf", &probability) != 1)
1778 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1780 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1781 p_string, "PROBABILITY", "TESTING");
1782 GNUNET_free (p_string);
1784 if (square * square != pg->total)
1786 while (rows * cols < pg->total)
1788 if (toggle % 2 == 0)
1797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1799 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1803 connect_attempts = 0;
1804 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1805 * to the node to its right and above. Once this is over, we'll have our torus!
1806 * Special case for the last node (if the rows and columns are not equal), connect
1807 * to the first in the row to maintain topology.
1809 for (i = 0; i < pg->total; i++)
1811 /* First connect to the node to the right */
1812 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1813 nodeToConnect = i + 1;
1814 else if (i + 1 == pg->total)
1815 nodeToConnect = rows * cols - cols;
1817 nodeToConnect = i - cols + 1;
1819 connect_attempts += proc (pg, i, nodeToConnect, list);
1822 nodeToConnect = (rows * cols) - cols + i;
1824 nodeToConnect = i - cols;
1826 if (nodeToConnect < pg->total)
1827 connect_attempts += proc (pg, i, nodeToConnect, list);
1829 natLog = log (pg->total);
1830 #if VERBOSE_TESTING > 2
1831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1832 _("natural log of %d is %d, will run %d iterations\n"),
1833 pg->total, natLog, (int) (natLog * percentage));
1834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1835 _("Total connections added thus far: %u!\n"), connect_attempts);
1837 smallWorldConnections = 0;
1838 small_world_it = (unsigned int) (natLog * percentage);
1839 if (small_world_it < 1)
1841 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
1842 for (i = 0; i < small_world_it; i++)
1844 for (j = 0; j < pg->total; j++)
1846 /* Determine the row and column of node at position j on the 2d torus */
1847 node1Row = j / cols;
1848 node1Col = j - (node1Row * cols);
1849 for (k = 0; k < pg->total; k++)
1851 /* Determine the row and column of node at position k on the 2d torus */
1852 node2Row = k / cols;
1853 node2Col = k - (node2Row * cols);
1854 /* Simple Cartesian distance */
1856 abs (node1Row - node2Row) + abs (node1Col - node2Col);
1859 /* Calculate probability as 1 over the square of the distance */
1860 probability = 1.0 / (distance * distance);
1861 /* Choose a random value between 0 and 1 */
1864 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1866 ((double) UINT64_MAX);
1867 /* If random < probability, then connect the two nodes */
1868 if (random < probability)
1869 smallWorldConnections += proc (pg, j, k, list);
1875 connect_attempts += smallWorldConnections;
1876 #if VERBOSE_TESTING > 2
1877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1878 _("Total connections added for small world: %d!\n"),
1879 smallWorldConnections);
1881 return connect_attempts;
1885 * Create a topology given a peer group (set of running peers)
1886 * and a connection processor.
1888 * @param pg the peergroup to create the topology on
1889 * @param proc the connection processor to call to actually set
1890 * up connections between two peers
1891 * @param list the peer list to use
1893 * @return the number of connections that were set up
1897 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg,
1898 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1901 unsigned int outer_count;
1902 unsigned int inner_count;
1903 int connect_attempts;
1907 probability = 0.5; /* FIXME: default percentage? */
1908 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1913 if (sscanf (p_string, "%lf", &probability) != 1)
1914 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1916 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1917 p_string, "PROBABILITY", "TESTING");
1918 GNUNET_free (p_string);
1920 connect_attempts = 0;
1921 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1923 for (inner_count = outer_count + 1; inner_count < pg->total;
1928 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1929 UINT64_MAX)) / ((double) UINT64_MAX);
1931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1932 _("rand is %f probability is %f\n"), temp_rand,
1935 if (temp_rand < probability)
1937 connect_attempts += proc (pg, outer_count, inner_count, list);
1942 return connect_attempts;
1946 * Create a topology given a peer group (set of running peers)
1947 * and a connection processor. This particular function creates
1948 * the connections for a 2d-torus, plus additional "closest"
1949 * connections per peer.
1951 * @param pg the peergroup to create the topology on
1952 * @param proc the connection processor to call to actually set
1953 * up connections between two peers
1954 * @param list the peer list to use
1956 * @return the number of connections that were set up
1960 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg,
1961 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1964 unsigned int square;
1967 unsigned int toggle = 1;
1968 unsigned int nodeToConnect;
1969 int connect_attempts;
1971 connect_attempts = 0;
1973 square = floor (sqrt (pg->total));
1977 if (square * square != pg->total)
1979 while (rows * cols < pg->total)
1981 if (toggle % 2 == 0)
1990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1992 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1995 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1996 * to the node to its right and above. Once this is over, we'll have our torus!
1997 * Special case for the last node (if the rows and columns are not equal), connect
1998 * to the first in the row to maintain topology.
2000 for (i = 0; i < pg->total; i++)
2002 /* First connect to the node to the right */
2003 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2004 nodeToConnect = i + 1;
2005 else if (i + 1 == pg->total)
2006 nodeToConnect = rows * cols - cols;
2008 nodeToConnect = i - cols + 1;
2010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2011 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2013 connect_attempts += proc (pg, i, nodeToConnect, list);
2015 /* Second connect to the node immediately above */
2017 nodeToConnect = (rows * cols) - cols + i;
2019 nodeToConnect = i - cols;
2021 if (nodeToConnect < pg->total)
2024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2025 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2027 connect_attempts += proc (pg, i, nodeToConnect, list);
2032 return connect_attempts;
2037 * Create a topology given a peer group (set of running peers)
2038 * and a connection processor.
2040 * @param pg the peergroup to create the topology on
2041 * @param proc the connection processor to call to actually set
2042 * up connections between two peers
2043 * @param list the peer list to use
2045 * @return the number of connections that were set up
2049 create_clique (struct GNUNET_TESTING_PeerGroup *pg,
2050 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2052 unsigned int outer_count;
2053 unsigned int inner_count;
2054 int connect_attempts;
2056 connect_attempts = 0;
2058 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2060 for (inner_count = outer_count + 1; inner_count < pg->total;
2064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2065 "Connecting peer %d to peer %d\n",
2066 outer_count, inner_count);
2068 connect_attempts += proc (pg, outer_count, inner_count, list);
2072 return connect_attempts;
2077 * Iterator over hash map entries.
2079 * @param cls closure the peer group
2080 * @param key the key stored in the hashmap is the
2081 * index of the peer to connect to
2082 * @param value value in the hash map, handle to the peer daemon
2083 * @return GNUNET_YES if we should continue to
2088 unblacklist_iterator (void *cls,
2089 const GNUNET_HashCode * key,
2092 struct UnblacklistContext *un_ctx = cls;
2093 uint32_t second_pos;
2095 uid_from_hash (key, &second_pos);
2097 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2104 * Create a blacklist topology based on the allowed topology
2105 * which disallows any connections not in the allowed topology
2106 * at the transport level.
2108 * @param pg the peergroup to create the topology on
2109 * @param proc the connection processor to call to allow
2110 * up connections between two peers
2112 * @return the number of connections that were set up
2116 copy_allowed (struct GNUNET_TESTING_PeerGroup *pg,
2117 GNUNET_TESTING_ConnectionProcessor proc)
2119 struct UnblacklistContext un_ctx;
2122 struct PeerConnection *iter;
2126 for (count = 0; count < pg->total - 1; count++)
2128 un_ctx.first_uid = count;
2130 iter = pg->peers[count].allowed_peers_head;
2131 while (iter != NULL)
2133 remove_connections(pg, count, iter->index, BLACKLIST);
2134 //unblacklist_connections(pg, count, iter->index);
2138 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers, &unblacklist_iterator, &un_ctx);
2141 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2146 * Create a topology given a peer group (set of running peers)
2147 * and a connection processor.
2149 * @param pg the peergroup to create the topology on
2150 * @param proc the connection processor to call to actually set
2151 * up connections between two peers
2152 * @param list which list should be modified
2154 * @return the number of connections that were set up
2158 create_line (struct GNUNET_TESTING_PeerGroup *pg,
2159 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2162 int connect_attempts;
2164 connect_attempts = 0;
2166 /* Connect each peer to the next highest numbered peer */
2167 for (count = 0; count < pg->total - 1; count++)
2170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2171 "Connecting peer %d to peer %d\n", count, count + 1);
2173 connect_attempts += proc (pg, count, count + 1, list);
2176 return connect_attempts;
2180 * Create a topology given a peer group (set of running peers)
2181 * and a connection processor.
2183 * @param pg the peergroup to create the topology on
2184 * @param filename the file to read topology information from
2185 * @param proc the connection processor to call to actually set
2186 * up connections between two peers
2187 * @param list the peer list to use
2189 * @return the number of connections that were set up
2193 create_from_file (struct GNUNET_TESTING_PeerGroup *pg,
2195 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2197 int connect_attempts;
2198 unsigned int first_peer_index;
2199 unsigned int second_peer_index;
2200 connect_attempts = 0;
2205 unsigned int total_peers;
2207 enum States curr_state;
2209 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2210 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2212 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2214 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2215 "Could not open file `%s' specified for topology!", filename);
2216 return connect_attempts;
2219 data = GNUNET_malloc_large (frstat.st_size);
2220 GNUNET_assert(data != NULL);
2221 if (frstat.st_size !=
2222 GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2225 "Could not read file %s specified for host list, ending test!", filename);
2227 return connect_attempts;
2232 /* First line should contain a single integer, specifying the number of peers */
2233 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2234 curr_state = NUM_PEERS;
2235 while (count < frstat.st_size - 1)
2237 if ((buf[count] == '\n') || (buf[count] == ' '))
2246 if (1 != sscanf(&buf[count], "%u", &total_peers))
2248 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read number of peers from topology file!\n");
2249 GNUNET_free_non_null(data);
2250 return connect_attempts;
2252 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read %u total peers in topology\n", total_peers);
2253 GNUNET_assert(total_peers == pg->total);
2254 curr_state = PEER_INDEX;
2255 while((buf[count] != '\n') && (count < frstat.st_size - 1))
2260 if (1 != sscanf(&buf[count], "%u", &first_peer_index))
2262 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read peer index from topology file!\n");
2263 GNUNET_free_non_null(data);
2264 return connect_attempts;
2266 while((buf[count] != ':') && (count < frstat.st_size - 1))
2269 curr_state = OTHER_PEER_INDEX;
2272 if (1 == sscanf(&buf[count], ":"))
2273 curr_state = OTHER_PEER_INDEX;
2276 case OTHER_PEER_INDEX:
2277 if (1 != sscanf(&buf[count], "%u", &second_peer_index))
2279 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to peer index from topology file!\n");
2280 GNUNET_free_non_null(data);
2281 return connect_attempts;
2283 /* Assume file is written with first peer 1, but array index is 0 */
2284 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index - 1, list);
2285 while((buf[count] != '\n') && (buf[count] != ',') && (count < frstat.st_size - 1))
2287 if (buf[count] == '\n')
2289 curr_state = PEER_INDEX;
2291 else if (buf[count] != ',')
2293 curr_state = OTHER_PEER_INDEX;
2298 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Found bad data in topology file while in state %d!\n", curr_state);
2300 return connect_attempts;
2305 /* Connect each peer to the next highest numbered peer */
2306 for (count = 0; count < pg->total - 1; count++)
2309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2310 "Connecting peer %d to peer %d\n", first_peer_index, second_peer_index);
2312 connect_attempts += proc (pg, first_peer_index, second_peer_index);
2315 return connect_attempts;
2319 * Create a topology given a peer group (set of running peers)
2320 * and a connection processor.
2322 * @param pg the peergroup to create the topology on
2323 * @param proc the connection processor to call to actually set
2324 * up connections between two peers
2325 * @param list the peer list to use
2327 * @return the number of connections that were set up
2331 create_ring (struct GNUNET_TESTING_PeerGroup *pg,
2332 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2335 int connect_attempts;
2337 connect_attempts = 0;
2339 /* Connect each peer to the next highest numbered peer */
2340 for (count = 0; count < pg->total - 1; count++)
2343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2344 "Connecting peer %d to peer %d\n", count, count + 1);
2346 connect_attempts += proc (pg, count, count + 1, list);
2349 /* Connect the last peer to the first peer */
2350 connect_attempts += proc (pg, pg->total - 1, 0, list);
2352 return connect_attempts;
2357 * Iterator for writing friends of a peer to a file.
2359 * @param cls closure, an open writable file handle
2360 * @param key the key the daemon was stored under
2361 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2363 * @return GNUNET_YES to continue iteration
2365 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2366 * with a single file_iterator that takes a closure which contains
2367 * the prefix to write before the peer. Then this could be used
2368 * for blacklisting multiple transports and writing the friend
2369 * file. I'm sure *someone* will complain loudly about other
2370 * things that negate these functions even existing so no point in
2374 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2376 FILE *temp_friend_handle = cls;
2377 struct GNUNET_TESTING_Daemon *peer = value;
2378 struct GNUNET_PeerIdentity *temppeer;
2379 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2381 temppeer = &peer->id;
2382 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2383 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2388 struct BlacklistContext
2391 * The (open) file handle to write to
2393 FILE *temp_file_handle;
2396 * The transport that this peer will be blacklisted on.
2402 * Iterator for writing blacklist data to appropriate files.
2404 * @param cls closure, an open writable file handle
2405 * @param key the key the daemon was stored under
2406 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2408 * @return GNUNET_YES to continue iteration
2411 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2413 struct BlacklistContext *blacklist_ctx = cls;
2414 struct GNUNET_TESTING_Daemon *peer = value;
2415 struct GNUNET_PeerIdentity *temppeer;
2416 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2418 temppeer = &peer->id;
2419 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2420 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2421 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2422 blacklist_ctx->transport, (char *) &peer_enc);
2430 * Create the friend files based on the PeerConnection's
2431 * of each peer in the peer group, and copy the files
2432 * to the appropriate place
2434 * @param pg the peer group we are dealing with
2437 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
2439 FILE *temp_friend_handle;
2440 unsigned int pg_iter;
2441 char *temp_service_path;
2442 struct GNUNET_OS_Process **procarr;
2445 enum GNUNET_OS_ProcessStatusType type;
2446 unsigned long return_code;
2451 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2452 struct PeerConnection *conn_iter;
2454 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2455 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2457 mytemp = GNUNET_DISK_mktemp ("friends");
2458 GNUNET_assert (mytemp != NULL);
2459 temp_friend_handle = fopen (mytemp, "wt");
2460 GNUNET_assert (temp_friend_handle != NULL);
2462 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2463 while (conn_iter != NULL)
2465 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
2466 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2467 conn_iter = conn_iter->next;
2470 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2471 &friend_file_iterator,
2472 temp_friend_handle);
2474 fclose (temp_friend_handle);
2477 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2478 daemon->cfg, "PATHS",
2480 &temp_service_path))
2482 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2484 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2485 "SERVICEHOME", "PATHS");
2486 if (UNLINK (mytemp) != 0)
2487 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2489 GNUNET_free (mytemp);
2493 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2495 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2496 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2501 _("Copying file with command cp %s %s\n"), mytemp, arg);
2506 else /* Remote, scp the file to the correct place */
2508 if (NULL != pg->peers[pg_iter].daemon->username)
2509 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2510 pg->peers[pg_iter].daemon->username,
2511 pg->peers[pg_iter].daemon->hostname,
2514 GNUNET_asprintf (&arg, "%s:%s/friends",
2515 pg->peers[pg_iter].daemon->hostname,
2518 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2523 _("Copying file with command scp %s %s\n"), mytemp,
2528 GNUNET_free (temp_service_path);
2529 GNUNET_free (mytemp);
2533 ret = GNUNET_SYSERR;
2534 while ((count < max_wait) && (ret != GNUNET_OK))
2537 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2541 _("Checking copy status of file %d\n"), pg_iter);
2543 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2545 if (GNUNET_OS_process_status
2546 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2548 ret = GNUNET_SYSERR;
2550 else if ((type != GNUNET_OS_PROCESS_EXITED)
2551 || (return_code != 0))
2553 ret = GNUNET_SYSERR;
2557 GNUNET_OS_process_close (procarr[pg_iter]);
2558 procarr[pg_iter] = NULL;
2560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2561 _("File %d copied\n"), pg_iter);
2567 if (ret == GNUNET_SYSERR)
2569 /* FIXME: why sleep here? -CG */
2575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2576 _("Finished copying all friend files!\n"));
2578 GNUNET_free (procarr);
2584 * Create the blacklist files based on the PeerConnection's
2585 * of each peer in the peer group, and copy the files
2586 * to the appropriate place.
2588 * @param pg the peer group we are dealing with
2589 * @param transports space delimited list of transports to blacklist
2592 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg,
2593 const char *transports)
2595 FILE *temp_file_handle;
2596 unsigned int pg_iter;
2597 char *temp_service_path;
2598 struct GNUNET_OS_Process **procarr;
2601 enum GNUNET_OS_ProcessStatusType type;
2602 unsigned long return_code;
2609 char *temp_transports;
2612 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2613 struct PeerConnection *conn_iter;
2615 static struct BlacklistContext blacklist_ctx;
2618 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2619 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2621 mytemp = GNUNET_DISK_mktemp ("blacklist");
2622 GNUNET_assert (mytemp != NULL);
2623 temp_file_handle = fopen (mytemp, "wt");
2624 GNUNET_assert (temp_file_handle != NULL);
2625 temp_transports = GNUNET_strdup (transports);
2627 blacklist_ctx.temp_file_handle = temp_file_handle;
2629 transport_len = strlen (temp_transports) + 1;
2632 for (i = 0; i < transport_len; i++)
2634 if ((temp_transports[i] == ' ') && (pos == NULL))
2635 continue; /* At start of string (whitespace) */
2636 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2638 temp_transports[i] = '\0';
2640 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
2641 while (conn_iter != NULL)
2643 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
2644 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
2645 conn_iter = conn_iter->next;
2649 blacklist_ctx.transport = pos;
2650 entry_count = GNUNET_CONTAINER_multihashmap_iterate (pg->
2652 [pg_iter].blacklisted_peers,
2653 &blacklist_file_iterator,
2657 } /* At beginning of actual string */
2658 else if (pos == NULL)
2660 pos = &temp_transports[i];
2664 GNUNET_free (temp_transports);
2665 fclose (temp_file_handle);
2668 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2669 daemon->cfg, "PATHS",
2671 &temp_service_path))
2673 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2675 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2676 "SERVICEHOME", "PATHS");
2677 if (UNLINK (mytemp) != 0)
2678 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2680 GNUNET_free (mytemp);
2684 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2686 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2687 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2692 _("Copying file with command cp %s %s\n"), mytemp, arg);
2697 else /* Remote, scp the file to the correct place */
2699 if (NULL != pg->peers[pg_iter].daemon->username)
2700 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
2701 pg->peers[pg_iter].daemon->username,
2702 pg->peers[pg_iter].daemon->hostname,
2705 GNUNET_asprintf (&arg, "%s:%s/blacklist",
2706 pg->peers[pg_iter].daemon->hostname,
2709 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2714 _("Copying file with command scp %s %s\n"), mytemp,
2719 GNUNET_free (temp_service_path);
2720 GNUNET_free (mytemp);
2724 ret = GNUNET_SYSERR;
2725 while ((count < max_wait) && (ret != GNUNET_OK))
2728 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2732 _("Checking copy status of file %d\n"), pg_iter);
2734 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2736 if (GNUNET_OS_process_status
2737 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2739 ret = GNUNET_SYSERR;
2741 else if ((type != GNUNET_OS_PROCESS_EXITED)
2742 || (return_code != 0))
2744 ret = GNUNET_SYSERR;
2748 GNUNET_OS_process_close (procarr[pg_iter]);
2749 procarr[pg_iter] = NULL;
2751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2752 _("File %d copied\n"), pg_iter);
2758 if (ret == GNUNET_SYSERR)
2760 /* FIXME: why sleep here? -CG */
2766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2767 _("Finished copying all blacklist files!\n"));
2769 GNUNET_free (procarr);
2775 * Internal notification of a connection, kept so that we can ensure some connections
2776 * happen instead of flooding all testing daemons with requests to connect.
2779 internal_connect_notify (void *cls,
2780 const struct GNUNET_PeerIdentity *first,
2781 const struct GNUNET_PeerIdentity *second,
2783 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2784 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2785 struct GNUNET_TESTING_Daemon *first_daemon,
2786 struct GNUNET_TESTING_Daemon *second_daemon,
2789 struct ConnectTopologyContext *ct_ctx = cls;
2790 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
2791 outstanding_connects--;
2792 ct_ctx->remaining_connections--;
2793 if (ct_ctx->remaining_connections == 0)
2795 if (ct_ctx->notify_connections_done != NULL)
2796 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
2797 GNUNET_free (ct_ctx);
2800 if (pg->notify_connection != NULL)
2801 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
2802 first_cfg, second_cfg, first_daemon, second_daemon,
2808 * Either delay a connection (because there are too many outstanding)
2809 * or schedule it for right now.
2811 * @param cls a connection context
2812 * @param tc the task runtime context
2815 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2817 struct ConnectContext *connect_context = cls;
2819 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2822 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2824 #if VERBOSE_TESTING > 2
2825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2827 ("Delaying connect, we have too many outstanding connections!\n"));
2829 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2830 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
2831 &schedule_connect, connect_context);
2835 #if VERBOSE_TESTING > 2
2836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2837 _("Creating connection, outstanding_connections is %d\n"),
2838 outstanding_connects);
2840 outstanding_connects++;
2841 GNUNET_TESTING_daemons_connect (connect_context->first,
2842 connect_context->second,
2845 &internal_connect_notify,
2846 connect_context->ct_ctx);
2847 GNUNET_free (connect_context);
2853 * Iterator for actually scheduling connections to be created
2854 * between two peers.
2856 * @param cls closure, a GNUNET_TESTING_Daemon
2857 * @param key the key the second Daemon was stored under
2858 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2860 * @return GNUNET_YES to continue iteration
2863 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2865 struct ConnectTopologyContext *ct_ctx = cls;
2866 struct PeerData *first = ct_ctx->first;
2867 struct GNUNET_TESTING_Daemon *second = value;
2868 struct ConnectContext *connect_context;
2870 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
2871 connect_context->first = first->daemon;
2872 connect_context->second = second;
2873 connect_context->ct_ctx = ct_ctx;
2874 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
2882 * Iterator for copying all entries in the allowed hashmap to the
2885 * @param cls closure, a GNUNET_TESTING_Daemon
2886 * @param key the key the second Daemon was stored under
2887 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2889 * @return GNUNET_YES to continue iteration
2892 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2894 struct PeerData *first = cls;
2896 GNUNET_assert (GNUNET_OK ==
2897 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
2899 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2906 * Make the peers to connect the same as those that are allowed to be
2909 * @param pg the peer group
2912 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2914 unsigned int pg_iter;
2918 struct PeerConnection *iter;
2922 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2925 iter = pg->peers[pg_iter].allowed_peers_head;
2926 while (iter != NULL)
2928 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating connection between %d and %d\n", pg_iter, iter->index);
2929 total += add_connections(pg, pg_iter, iter->index, CONNECT);
2930 //total += add_actual_connections(pg, pg_iter, iter->index);
2935 GNUNET_CONTAINER_multihashmap_iterate (pg->
2936 peers[pg_iter].allowed_peers,
2937 ©_topology_iterator,
2938 &pg->peers[pg_iter]);
2940 if (GNUNET_SYSERR == ret)
2941 return GNUNET_SYSERR;
2943 total = total + ret;
2951 * Connect the topology as specified by the PeerConnection's
2952 * of each peer in the peer group
2954 * @param pg the peer group we are dealing with
2955 * @param notify_callback callback to notify when finished
2956 * @param notify_cls closure for notify callback
2958 * @return the number of connections that will be attempted
2961 connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2962 GNUNET_TESTING_NotifyCompletion notify_callback,
2965 unsigned int pg_iter;
2967 struct ConnectTopologyContext *ct_ctx;
2969 struct PeerConnection *connection_iter;
2970 struct ConnectContext *connect_context;
2976 ct_ctx = GNUNET_malloc (sizeof (struct ConnectTopologyContext));
2977 ct_ctx->notify_connections_done = notify_callback;
2978 ct_ctx->notify_cls = notify_cls;
2981 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2984 connection_iter = pg->peers[pg_iter].connect_peers_head;
2985 while (connection_iter != NULL)
2987 connection_iter = connection_iter->next;
2992 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
2998 GNUNET_free (ct_ctx);
3001 ct_ctx->remaining_connections = total;
3004 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3007 connection_iter = pg->peers[pg_iter].connect_peers_head;
3008 while (connection_iter != NULL)
3010 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling connect of peer %d to peer %d\n", pg_iter, connection_iter->index);
3011 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3012 connect_context->first = pg->peers[pg_iter].daemon;
3013 connect_context->second = pg->peers[connection_iter->index].daemon;
3014 connect_context->ct_ctx = ct_ctx;
3015 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3016 connection_iter = connection_iter->next;
3021 GNUNET_CONTAINER_multihashmap_iterate (pg->
3022 peers[pg_iter].connect_peers,
3023 &connect_iterator, ct_ctx);
3024 GNUNET_assert (GNUNET_SYSERR != ret && ret >= 0);
3025 total = total + ret;
3033 * Takes a peer group and creates a topology based on the
3034 * one specified. Creates a topology means generates friend
3035 * files for the peers so they can only connect to those allowed
3036 * by the topology. This will only have an effect once peers
3037 * are started if the FRIENDS_ONLY option is set in the base
3038 * config. Also takes an optional restrict topology which
3039 * disallows connections based on particular transports
3040 * UNLESS they are specified in the restricted topology.
3042 * @param pg the peer group struct representing the running peers
3043 * @param topology which topology to connect the peers in
3044 * @param restrict_topology disallow restrict_transports transport
3045 * connections to peers NOT in this topology
3046 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3047 * @param restrict_transports space delimited list of transports to blacklist
3048 * to create restricted topology
3050 * @return the maximum number of connections were all allowed peers
3051 * connected to each other
3054 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
3055 enum GNUNET_TESTING_Topology topology,
3056 enum GNUNET_TESTING_Topology
3058 const char *restrict_transports)
3062 unsigned int num_connections;
3063 int unblacklisted_connections;
3068 for (i = 0; i < pg->total; i++)
3070 pg->peers[i].allowed_peers =
3071 GNUNET_CONTAINER_multihashmap_create (100);
3072 pg->peers[i].connect_peers =
3073 GNUNET_CONTAINER_multihashmap_create (100);
3074 pg->peers[i].blacklisted_peers =
3075 GNUNET_CONTAINER_multihashmap_create (100);
3076 pg->peers[i].pg = pg;
3082 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3086 num_connections = create_clique (pg, &add_connections, ALLOWED);
3088 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3091 _("Creating small world (ring) topology\n"));
3094 create_small_world_ring (pg, &add_connections, ALLOWED);
3096 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3099 _("Creating small world (2d-torus) topology\n"));
3101 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3103 case GNUNET_TESTING_TOPOLOGY_RING:
3105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3107 num_connections = create_ring (pg, &add_connections, ALLOWED);
3109 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3113 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3115 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3118 _("Creating Erdos-Renyi topology\n"));
3120 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3122 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3126 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3128 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3131 _("Creating Scale Free topology\n"));
3133 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3135 case GNUNET_TESTING_TOPOLOGY_LINE:
3137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3138 _("Creating straight line topology\n"));
3140 num_connections = create_line (pg, &add_connections, ALLOWED);
3142 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3145 _("Creating topology from file!\n"));
3148 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing", "topology_file",
3150 num_connections = create_from_file (pg, filename, &add_connections, ALLOWED);
3153 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3154 num_connections = 0;
3157 case GNUNET_TESTING_TOPOLOGY_NONE:
3159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3161 ("Creating no allowed topology (all peers can connect at core level)\n"));
3163 num_connections = pg->total * pg->total; /* Clique is allowed! */
3166 num_connections = 0;
3171 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
3173 ret = create_and_copy_friend_files (pg);
3174 if (ret != GNUNET_OK)
3177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3178 _("Failed during friend file copying!\n"));
3180 return GNUNET_SYSERR;
3185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3186 _("Friend files created/copied successfully!\n"));
3191 /* Use the create clique method to initially set all connections as blacklisted. */
3192 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
3193 create_clique (pg, &add_connections, BLACKLIST);
3195 unblacklisted_connections = 0;
3196 /* Un-blacklist connections as per the topology specified */
3197 switch (restrict_topology)
3199 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3202 _("Blacklisting all but clique topology\n"));
3204 unblacklisted_connections =
3205 create_clique (pg, &remove_connections, BLACKLIST);
3207 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3210 _("Blacklisting all but small world (ring) topology\n"));
3212 unblacklisted_connections =
3213 create_small_world_ring (pg, &remove_connections, BLACKLIST);
3215 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3219 ("Blacklisting all but small world (2d-torus) topology\n"));
3221 unblacklisted_connections =
3222 create_small_world (pg, &remove_connections, BLACKLIST);
3224 case GNUNET_TESTING_TOPOLOGY_RING:
3226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3227 _("Blacklisting all but ring topology\n"));
3229 unblacklisted_connections = create_ring (pg, &remove_connections, BLACKLIST);
3231 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3234 _("Blacklisting all but 2d torus topology\n"));
3236 unblacklisted_connections =
3237 create_2d_torus (pg, &remove_connections, BLACKLIST);
3239 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3242 _("Blacklisting all but Erdos-Renyi topology\n"));
3244 unblacklisted_connections =
3245 create_erdos_renyi (pg, &remove_connections, BLACKLIST);
3247 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3250 _("Blacklisting all but InterNAT topology\n"));
3252 unblacklisted_connections =
3253 create_nated_internet (pg, &remove_connections, BLACKLIST);
3255 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3258 _("Blacklisting all but Scale Free topology\n"));
3260 unblacklisted_connections =
3261 create_scale_free (pg, &remove_connections, BLACKLIST);
3263 case GNUNET_TESTING_TOPOLOGY_LINE:
3265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3266 _("Blacklisting all but straight line topology\n"));
3268 unblacklisted_connections = create_line (pg, &remove_connections, BLACKLIST);
3270 case GNUNET_TESTING_TOPOLOGY_NONE: /* Fall through */
3271 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3275 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
3277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3279 ("Creating blacklist topology from allowed\n"));
3280 unblacklisted_connections = copy_allowed (pg, &remove_connections);
3285 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
3287 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n", restrict_transports);
3288 ret = create_and_copy_blacklist_files (pg, restrict_transports);
3289 if (ret != GNUNET_OK)
3292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3293 _("Failed during blacklist file copying!\n"));
3300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3301 _("Blacklist files created/copied successfully!\n"));
3305 return num_connections;
3311 * Iterator for choosing random peers to connect.
3313 * @param cls closure, a RandomContext
3314 * @param key the key the second Daemon was stored under
3315 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3317 * @return GNUNET_YES to continue iteration
3320 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3322 struct RandomContext *random_ctx = cls;
3323 double random_number;
3324 uint32_t second_pos;
3325 GNUNET_HashCode first_hash;
3328 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3329 UINT64_MAX)) / ((double) UINT64_MAX);
3330 if (random_number < random_ctx->percentage)
3332 GNUNET_assert (GNUNET_OK ==
3333 GNUNET_CONTAINER_multihashmap_put (random_ctx->
3334 first->connect_peers_working_set,
3336 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3339 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
3340 uid_from_hash (key, &second_pos);
3341 hash_from_uid (random_ctx->first_uid, &first_hash);
3342 GNUNET_assert (random_ctx->pg->total > second_pos);
3343 GNUNET_assert (GNUNET_YES ==
3344 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
3346 [second_pos].connect_peers,
3356 * Iterator for adding at least X peers to a peers connection set.
3358 * @param cls closure, MinimumContext
3359 * @param key the key the second Daemon was stored under
3360 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3362 * @return GNUNET_YES to continue iteration
3365 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3367 struct MinimumContext *min_ctx = cls;
3368 uint32_t second_pos;
3369 GNUNET_HashCode first_hash;
3372 if (GNUNET_CONTAINER_multihashmap_size
3373 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
3375 for (i = 0; i < min_ctx->num_to_add; i++)
3377 if (min_ctx->pg_array[i] == min_ctx->current)
3379 GNUNET_assert (GNUNET_OK ==
3380 GNUNET_CONTAINER_multihashmap_put
3381 (min_ctx->first->connect_peers_working_set, key,
3383 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3384 uid_from_hash (key, &second_pos);
3385 hash_from_uid (min_ctx->first_uid, &first_hash);
3386 GNUNET_assert (min_ctx->pg->total > second_pos);
3387 GNUNET_assert (GNUNET_OK ==
3388 GNUNET_CONTAINER_multihashmap_put (min_ctx->
3390 [second_pos].connect_peers_working_set,
3394 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3395 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
3396 GNUNET_assert (GNUNET_YES ==
3397 GNUNET_CONTAINER_multihashmap_remove
3398 (min_ctx->pg->peers[second_pos].connect_peers,
3399 &first_hash, min_ctx->first->daemon));
3406 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
3412 * Iterator for adding peers to a connection set based on a depth first search.
3414 * @param cls closure, MinimumContext
3415 * @param key the key the second daemon was stored under
3416 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3418 * @return GNUNET_YES to continue iteration
3421 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3423 struct DFSContext *dfs_ctx = cls;
3424 GNUNET_HashCode first_hash;
3426 if (dfs_ctx->current == dfs_ctx->chosen)
3428 GNUNET_assert (GNUNET_OK ==
3429 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3430 first->connect_peers_working_set,
3432 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3433 uid_from_hash (key, &dfs_ctx->second_uid);
3434 hash_from_uid (dfs_ctx->first_uid, &first_hash);
3435 GNUNET_assert (GNUNET_OK ==
3436 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3438 [dfs_ctx->second_uid].connect_peers_working_set,
3442 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3443 GNUNET_assert (GNUNET_YES ==
3444 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
3446 [dfs_ctx->second_uid].connect_peers,
3450 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
3451 return GNUNET_NO; /* We have found our peer, don't iterate more */
3460 * From the set of connections possible, choose percentage percent of connections
3461 * to actually connect.
3463 * @param pg the peergroup we are dealing with
3464 * @param percentage what percent of total connections to make
3467 choose_random_connections (struct GNUNET_TESTING_PeerGroup *pg,
3470 struct RandomContext random_ctx;
3473 struct PeerConnection *temp_peers;
3474 struct PeerConnection *conn_iter;
3475 double random_number;
3478 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3480 random_ctx.first_uid = pg_iter;
3481 random_ctx.first = &pg->peers[pg_iter];
3482 random_ctx.percentage = percentage;
3486 conn_iter = pg->peers[pg_iter].connect_peers_head;
3487 while (conn_iter != NULL)
3491 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3492 UINT64_MAX)) / ((double) UINT64_MAX);
3493 if (random_number < percentage)
3495 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
3497 conn_iter = conn_iter->next;
3500 pg->peers[pg_iter].connect_peers_working_set =
3501 GNUNET_CONTAINER_multihashmap_create (pg->total);
3502 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3503 &random_connect_iterator,
3505 /* Now remove the old connections */
3506 GNUNET_CONTAINER_multihashmap_destroy (pg->
3507 peers[pg_iter].connect_peers);
3508 /* And replace with the random set */
3509 pg->peers[pg_iter].connect_peers =
3510 pg->peers[pg_iter].connect_peers_working_set;
3514 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3516 conn_iter = pg->peers[pg_iter].connect_peers_head;
3517 while (pg->peers[pg_iter].connect_peers_head != NULL)
3518 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
3520 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
3521 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
3522 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
3523 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
3529 * Count the number of connections in a linked list of connections.
3531 * @param conn_list the connection list to get the count of
3533 * @return the number of elements in the list
3536 count_connections (struct PeerConnection *conn_list)
3538 struct PeerConnection *iter;
3542 while (iter != NULL)
3552 count_workingset_connections (struct GNUNET_TESTING_PeerGroup *pg)
3555 unsigned int pg_iter;
3557 struct PeerConnection *conn_iter;
3561 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3564 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
3565 while (conn_iter != NULL)
3568 conn_iter = conn_iter->next;
3572 GNUNET_CONTAINER_multihashmap_size (pg->
3574 [pg_iter].connect_peers_working_set);
3583 count_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg)
3586 unsigned int pg_iter;
3588 struct PeerConnection *conn_iter;
3592 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3595 conn_iter = pg->peers[pg_iter].allowed_peers_head;
3596 while (conn_iter != NULL)
3599 conn_iter = conn_iter->next;
3603 GNUNET_CONTAINER_multihashmap_size (pg->
3605 [pg_iter].allowed_peers);
3614 * From the set of connections possible, choose at least num connections per
3617 * @param pg the peergroup we are dealing with
3618 * @param num how many connections at least should each peer have (if possible)?
3621 choose_minimum (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3624 struct MinimumContext minimum_ctx;
3626 struct PeerConnection *conn_iter;
3627 unsigned int temp_list_size;
3630 uint32_t random; /* Random list entry to connect peer to */
3635 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3637 temp_list_size = count_connections(pg->peers[pg_iter].connect_peers_head);
3638 if (temp_list_size == 0)
3640 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer %d has 0 connections!?!?\n", pg_iter);
3643 for (i = 0; i < num; i++)
3645 random = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, temp_list_size);
3646 conn_iter = pg->peers[pg_iter].connect_peers_head;
3647 for (count = 0; count < random; count++)
3648 conn_iter = conn_iter->next;
3649 /* We now have a random connection, connect it! */
3650 GNUNET_assert(conn_iter != NULL);
3651 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
3655 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3657 pg->peers[pg_iter].connect_peers_working_set =
3658 GNUNET_CONTAINER_multihashmap_create (num);
3661 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3663 minimum_ctx.first_uid = pg_iter;
3664 minimum_ctx.pg_array =
3665 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
3666 GNUNET_CONTAINER_multihashmap_size
3667 (pg->peers[pg_iter].connect_peers));
3668 minimum_ctx.first = &pg->peers[pg_iter];
3669 minimum_ctx.pg = pg;
3670 minimum_ctx.num_to_add = num;
3671 minimum_ctx.current = 0;
3672 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3673 &minimum_connect_iterator,
3677 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3679 /* Remove the "old" connections */
3680 GNUNET_CONTAINER_multihashmap_destroy (pg->
3681 peers[pg_iter].connect_peers);
3682 /* And replace with the working set */
3683 pg->peers[pg_iter].connect_peers =
3684 pg->peers[pg_iter].connect_peers_working_set;
3687 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3689 while (pg->peers[pg_iter].connect_peers_head != NULL)
3690 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
3692 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
3693 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
3694 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
3695 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
3700 struct FindClosestContext
3703 * The currently known closest peer.
3705 struct GNUNET_TESTING_Daemon *closest;
3708 * The info for the peer we are adding connections for.
3710 struct PeerData *curr_peer;
3713 * The distance (bits) between the current
3714 * peer and the currently known closest.
3716 unsigned int closest_dist;
3719 * The offset of the closest known peer in
3722 unsigned int closest_num;
3726 * Iterator over hash map entries of the allowed
3727 * peer connections. Find the closest, not already
3728 * connected peer and return it.
3730 * @param cls closure (struct FindClosestContext)
3731 * @param key current key code (hash of offset in pg)
3732 * @param value value in the hash map - a GNUNET_TESTING_Daemon
3733 * @return GNUNET_YES if we should continue to
3738 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
3740 struct FindClosestContext *closest_ctx = cls;
3741 struct GNUNET_TESTING_Daemon *daemon = value;
3743 if (((closest_ctx->closest == NULL) ||
3744 (GNUNET_CRYPTO_hash_matching_bits
3745 (&daemon->id.hashPubKey,
3746 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
3747 closest_ctx->closest_dist))
3749 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
3750 curr_peer->connect_peers,
3753 closest_ctx->closest_dist =
3754 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
3755 &closest_ctx->curr_peer->daemon->
3757 closest_ctx->closest = daemon;
3758 uid_from_hash (key, &closest_ctx->closest_num);
3765 * From the set of connections possible, choose at num connections per
3766 * peer based on depth which are closest out of those allowed. Guaranteed
3767 * to add num peers to connect to, provided there are that many peers
3768 * in the underlay topology to connect to.
3770 * @param pg the peergroup we are dealing with
3771 * @param num how many connections at least should each peer have (if possible)?
3772 * @param proc processor to actually add the connections
3773 * @param list the peer list to use
3776 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
3777 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
3782 struct FindClosestContext closest_ctx;
3787 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
3789 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3791 closest_ctx.curr_peer = &pg->peers[pg_iter];
3792 closest_ctx.closest = NULL;
3793 closest_ctx.closest_dist = 0;
3794 closest_ctx.closest_num = 0;
3795 GNUNET_CONTAINER_multihashmap_iterate (pg->
3796 peers[pg_iter].allowed_peers,
3797 &find_closest_peers,
3799 if (closest_ctx.closest != NULL)
3801 GNUNET_assert (closest_ctx.closest_num < pg->total);
3802 proc (pg, pg_iter, closest_ctx.closest_num, list);
3810 * From the set of connections possible, choose at least num connections per
3811 * peer based on depth first traversal of peer connections. If DFS leaves
3812 * peers unconnected, ensure those peers get connections.
3814 * @param pg the peergroup we are dealing with
3815 * @param num how many connections at least should each peer have (if possible)?
3818 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3822 uint32_t starting_peer;
3823 uint32_t least_connections;
3824 uint32_t random_connection;
3826 unsigned int temp_count;
3827 struct PeerConnection *peer_iter;
3829 struct DFSContext dfs_ctx;
3830 GNUNET_HashCode second_hash;
3836 while ((count_workingset_connections (pg) < num * pg->total)
3837 && (count_allowed_connections (pg) > 0))
3839 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3841 least_connections = -1; /* Set to very high number */
3842 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3844 temp_count = count_connections(pg->peers[pg_iter].connect_peers_working_set_head);
3845 if (temp_count < least_connections)
3847 starting_peer = pg_iter;
3848 least_connections = temp_count;
3853 temp_count = count_connections(pg->peers[starting_peer].connect_peers_head);
3854 if (temp_count == 0)
3855 continue; /* FIXME: infinite loop? */
3857 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, temp_count);
3859 peer_iter = pg->peers[starting_peer].connect_peers_head;
3860 while (temp_count < random_connection)
3862 peer_iter = peer_iter->next;
3865 GNUNET_assert(peer_iter != NULL);
3866 add_connections(pg, starting_peer, peer_iter->index, WORKING_SET);
3867 remove_connections(pg, starting_peer, peer_iter->index, CONNECT);
3868 starting_peer = peer_iter->index;
3873 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3875 pg->peers[pg_iter].connect_peers_working_set =
3876 GNUNET_CONTAINER_multihashmap_create (num);
3881 while ((count_workingset_connections (pg) < num * pg->total)
3882 && (count_allowed_connections (pg) > 0))
3884 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3886 least_connections = -1; /* Set to very high number */
3887 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3889 if (GNUNET_CONTAINER_multihashmap_size
3890 (pg->peers[pg_iter].connect_peers_working_set) <
3893 starting_peer = pg_iter;
3895 GNUNET_CONTAINER_multihashmap_size (pg->
3897 [pg_iter].connect_peers_working_set);
3902 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
3908 /* Choose a random peer from the chosen peers set of connections to add */
3910 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3911 GNUNET_CONTAINER_multihashmap_size
3912 (pg->peers[starting_peer].connect_peers));
3913 dfs_ctx.first_uid = starting_peer;
3914 dfs_ctx.first = &pg->peers[starting_peer];
3916 dfs_ctx.current = 0;
3918 GNUNET_CONTAINER_multihashmap_iterate (pg->
3920 [starting_peer].connect_peers,
3921 &dfs_connect_iterator, &dfs_ctx);
3922 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
3923 hash_from_uid (dfs_ctx.second_uid, &second_hash);
3924 GNUNET_assert (GNUNET_YES ==
3925 GNUNET_CONTAINER_multihashmap_remove (pg->peers
3926 [starting_peer].connect_peers,
3930 [dfs_ctx.second_uid].daemon));
3931 starting_peer = dfs_ctx.second_uid;
3934 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3936 /* Remove the "old" connections */
3937 GNUNET_CONTAINER_multihashmap_destroy (pg->
3938 peers[pg_iter].connect_peers);
3939 /* And replace with the working set */
3940 pg->peers[pg_iter].connect_peers =
3941 pg->peers[pg_iter].connect_peers_working_set;
3948 * Internal callback for topology information for a particular peer.
3951 internal_topology_callback (void *cls,
3952 const struct GNUNET_PeerIdentity *peer,
3953 const struct GNUNET_TRANSPORT_ATS_Information
3956 struct CoreContext *core_ctx = cls;
3957 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
3959 if (peer == NULL) /* Either finished, or something went wrong */
3961 iter_ctx->completed++;
3962 iter_ctx->connected--;
3963 /* One core context allocated per iteration, must free! */
3964 GNUNET_free (core_ctx);
3968 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id,
3972 if (iter_ctx->completed == iter_ctx->total)
3974 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
3975 /* Once all are done, free the iteration context */
3976 GNUNET_free (iter_ctx);
3982 * Check running topology iteration tasks, if below max start a new one, otherwise
3983 * schedule for some time in the future.
3986 schedule_get_topology (void *cls,
3987 const struct GNUNET_SCHEDULER_TaskContext *tc)
3989 struct CoreContext *core_context = cls;
3990 struct TopologyIterateContext *topology_context =
3991 (struct TopologyIterateContext *) core_context->iter_context;
3992 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3995 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3997 #if VERBOSE_TESTING > 2
3998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4000 ("Delaying connect, we have too many outstanding connections!\n"));
4002 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4003 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4004 &schedule_get_topology, core_context);
4008 #if VERBOSE_TESTING > 2
4009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4010 _("Creating connection, outstanding_connections is %d\n"),
4011 outstanding_connects);
4013 topology_context->connected++;
4016 GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4017 &internal_topology_callback,
4020 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4021 internal_topology_callback (core_context, NULL, NULL);
4027 * Iterate over all (running) peers in the peer group, retrieve
4028 * all connections that each currently has.
4031 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
4032 GNUNET_TESTING_NotifyTopology cb, void *cls)
4034 struct TopologyIterateContext *topology_context;
4035 struct CoreContext *core_ctx;
4037 unsigned int total_count;
4039 /* Allocate a single topology iteration context */
4040 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4041 topology_context->topology_cb = cb;
4042 topology_context->cls = cls;
4044 for (i = 0; i < pg->total; i++)
4046 if (pg->peers[i].daemon->running == GNUNET_YES)
4048 /* Allocate one core context per core we need to connect to */
4049 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4050 core_ctx->daemon = pg->peers[i].daemon;
4051 /* Set back pointer to topology iteration context */
4052 core_ctx->iter_context = topology_context;
4053 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4057 if (total_count == 0)
4059 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4060 GNUNET_free (topology_context);
4063 topology_context->total = total_count;
4068 * Callback function to process statistic values.
4069 * This handler is here only really to insert a peer
4070 * identity (or daemon) so the statistics can be uniquely
4071 * tied to a single running peer.
4073 * @param cls closure
4074 * @param subsystem name of subsystem that created the statistic
4075 * @param name the name of the datum
4076 * @param value the current value
4077 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4078 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4081 internal_stats_callback (void *cls,
4082 const char *subsystem,
4083 const char *name, uint64_t value, int is_persistent)
4085 struct StatsCoreContext *core_context = cls;
4086 struct StatsIterateContext *stats_context =
4087 (struct StatsIterateContext *) core_context->iter_context;
4089 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4090 subsystem, name, value, is_persistent);
4094 * Internal continuation call for statistics iteration.
4096 * @param cls closure, the CoreContext for this iteration
4097 * @param success whether or not the statistics iterations
4098 * was canceled or not (we don't care)
4101 internal_stats_cont (void *cls, int success)
4103 struct StatsCoreContext *core_context = cls;
4104 struct StatsIterateContext *stats_context =
4105 (struct StatsIterateContext *) core_context->iter_context;
4107 stats_context->connected--;
4108 stats_context->completed++;
4110 if (stats_context->completed == stats_context->total)
4112 stats_context->cont (stats_context->cls, GNUNET_YES);
4113 GNUNET_free (stats_context);
4116 if (core_context->stats_handle != NULL)
4117 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4119 GNUNET_free (core_context);
4123 * Check running topology iteration tasks, if below max start a new one, otherwise
4124 * schedule for some time in the future.
4127 schedule_get_statistics (void *cls,
4128 const struct GNUNET_SCHEDULER_TaskContext *tc)
4130 struct StatsCoreContext *core_context = cls;
4131 struct StatsIterateContext *stats_context =
4132 (struct StatsIterateContext *) core_context->iter_context;
4134 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4137 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
4139 #if VERBOSE_TESTING > 2
4140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4142 ("Delaying connect, we have too many outstanding connections!\n"));
4144 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4145 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4146 &schedule_get_statistics, core_context);
4150 #if VERBOSE_TESTING > 2
4151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4152 _("Creating connection, outstanding_connections is %d\n"),
4153 outstanding_connects);
4156 stats_context->connected++;
4157 core_context->stats_handle =
4158 GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
4159 if (core_context->stats_handle == NULL)
4161 internal_stats_cont (core_context, GNUNET_NO);
4165 core_context->stats_get_handle =
4166 GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
4167 GNUNET_TIME_relative_get_forever (),
4168 &internal_stats_cont, &internal_stats_callback,
4170 if (core_context->stats_get_handle == NULL)
4171 internal_stats_cont (core_context, GNUNET_NO);
4176 struct DuplicateStats
4179 * Next item in the list
4181 struct DuplicateStats *next;
4184 * Nasty string, concatenation of relevant information.
4186 char *unique_string;
4190 * Check whether the combination of port/host/unix domain socket
4191 * already exists in the list of peers being checked for statistics.
4193 * @param pg the peergroup in question
4194 * @param specific_peer the peer we're concerned with
4195 * @param stats_list the list to return to the caller
4197 * @return GNUNET_YES if the statistics instance has been seen already,
4198 * GNUNET_NO if not (and we may have added it to the list)
4201 stats_check_existing (struct GNUNET_TESTING_PeerGroup *pg,
4202 struct PeerData *specific_peer,
4203 struct DuplicateStats **stats_list)
4205 struct DuplicateStats *pos;
4206 char *unix_domain_socket;
4207 unsigned long long port;
4210 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
4211 "single_statistics_per_host"))
4212 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
4216 GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg, "statistics",
4217 "unixpath", &unix_domain_socket))
4221 GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg, "statistics",
4224 GNUNET_free(unix_domain_socket);
4228 if (specific_peer->daemon->hostname != NULL)
4229 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
4230 unix_domain_socket, port);
4232 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
4236 if (0 == strcmp (to_match, pos->unique_string))
4238 GNUNET_free (unix_domain_socket);
4239 GNUNET_free (to_match);
4244 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
4245 pos->unique_string = to_match;
4246 pos->next = *stats_list;
4248 GNUNET_free (unix_domain_socket);
4253 * Iterate over all (running) peers in the peer group, retrieve
4254 * all statistics from each.
4257 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
4258 GNUNET_STATISTICS_Callback cont,
4259 GNUNET_TESTING_STATISTICS_Iterator proc,
4262 struct StatsIterateContext *stats_context;
4263 struct StatsCoreContext *core_ctx;
4265 unsigned int total_count;
4266 struct DuplicateStats *stats_list;
4267 struct DuplicateStats *pos;
4270 /* Allocate a single stats iteration context */
4271 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
4272 stats_context->cont = cont;
4273 stats_context->proc = proc;
4274 stats_context->cls = cls;
4277 for (i = 0; i < pg->total; i++)
4279 if ((pg->peers[i].daemon->running == GNUNET_YES)
4281 stats_check_existing (pg, &pg->peers[i], &stats_list)))
4283 /* Allocate one core context per core we need to connect to */
4284 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
4285 core_ctx->daemon = pg->peers[i].daemon;
4286 /* Set back pointer to topology iteration context */
4287 core_ctx->iter_context = stats_context;
4288 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
4293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4294 "Retrieving stats from %u total instances.\n", total_count);
4295 stats_context->total = total_count;
4296 if (stats_list != NULL)
4301 GNUNET_free (pos->unique_string);
4302 stats_list = pos->next;
4304 pos = stats_list->next;
4311 * There are many ways to connect peers that are supported by this function.
4312 * To connect peers in the same topology that was created via the
4313 * GNUNET_TESTING_create_topology, the topology variable must be set to
4314 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
4315 * a new instance of that topology will be generated and attempted to be
4316 * connected. This could result in some connections being impossible,
4317 * because some topologies are non-deterministic.
4319 * @param pg the peer group struct representing the running peers
4320 * @param topology which topology to connect the peers in
4321 * @param options options for connecting the topology
4322 * @param option_modifier modifier for options that take a parameter
4323 * @param notify_callback notification to be called once all connections completed
4324 * @param notify_cls closure for notification callback
4326 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
4329 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
4330 enum GNUNET_TESTING_Topology topology,
4331 enum GNUNET_TESTING_TopologyOption options,
4332 double option_modifier,
4333 GNUNET_TESTING_NotifyCompletion
4334 notify_callback, void *notify_cls)
4338 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4339 #if VERBOSE_TOPOLOGY
4340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4341 _("Creating clique CONNECT topology\n"));
4343 create_clique (pg, &add_connections, CONNECT);
4345 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4346 #if VERBOSE_TOPOLOGY
4347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4348 _("Creating small world (ring) CONNECT topology\n"));
4350 create_small_world_ring (pg, &add_connections, CONNECT);
4352 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4353 #if VERBOSE_TOPOLOGY
4354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4355 _("Creating small world (2d-torus) CONNECT topology\n"));
4357 create_small_world (pg, &add_connections, CONNECT);
4359 case GNUNET_TESTING_TOPOLOGY_RING:
4360 #if VERBOSE_TOPOLOGY
4361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4362 _("Creating ring CONNECT topology\n"));
4364 create_ring (pg, &add_connections, CONNECT);
4366 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4367 #if VERBOSE_TOPOLOGY
4368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4369 _("Creating 2d torus CONNECT topology\n"));
4371 create_2d_torus (pg, &add_connections, CONNECT);
4373 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4374 #if VERBOSE_TOPOLOGY
4375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4376 _("Creating Erdos-Renyi CONNECT topology\n"));
4378 create_erdos_renyi (pg, &add_connections, CONNECT);
4380 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4381 #if VERBOSE_TOPOLOGY
4382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4383 _("Creating InterNAT CONNECT topology\n"));
4385 create_nated_internet (pg, &add_connections, CONNECT);
4387 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4388 #if VERBOSE_TOPOLOGY
4389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4390 _("Creating Scale Free CONNECT topology\n"));
4392 create_scale_free (pg, &add_connections, CONNECT);
4394 case GNUNET_TESTING_TOPOLOGY_LINE:
4395 #if VERBOSE_TOPOLOGY
4396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4397 _("Creating straight line CONNECT topology\n"));
4399 create_line (pg, &add_connections, CONNECT);
4401 case GNUNET_TESTING_TOPOLOGY_NONE:
4402 #if VERBOSE_TOPOLOGY
4403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4404 _("Creating no CONNECT topology\n"));
4406 copy_allowed_topology (pg);
4409 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4411 ("Unknown topology specification, can't connect peers!\n"));
4412 return GNUNET_SYSERR;
4417 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
4418 #if VERBOSE_TOPOLOGY
4419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4421 ("Connecting random subset (%'.2f percent) of possible peers\n"),
4422 100 * option_modifier);
4424 choose_random_connections (pg, option_modifier);
4426 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
4427 #if VERBOSE_TOPOLOGY
4428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4429 _("Connecting a minimum of %u peers each (if possible)\n"),
4430 (unsigned int) option_modifier);
4432 choose_minimum (pg, (unsigned int) option_modifier);
4434 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
4435 #if VERBOSE_TOPOLOGY
4436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4438 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
4439 (unsigned int) option_modifier);
4442 perform_dfs (pg, (int) option_modifier);
4445 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
4446 #if VERBOSE_TOPOLOGY
4447 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4449 ("Finding additional %u closest peers each (if possible)\n"),
4450 (unsigned int) option_modifier);
4453 add_closest (pg, (unsigned int) option_modifier,
4454 &add_connections, CONNECT);
4457 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
4459 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
4465 return connect_topology (pg, notify_callback, notify_cls);
4469 * Callback that is called whenever a hostkey is generated
4470 * for a peer. Call the real callback and decrement the
4471 * starting counter for the peergroup.
4473 * @param cls closure
4474 * @param id identifier for the daemon, NULL on error
4475 * @param d handle for the daemon
4476 * @param emsg error message (NULL on success)
4479 internal_hostkey_callback (void *cls,
4480 const struct GNUNET_PeerIdentity *id,
4481 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4483 struct InternalStartContext *internal_context = cls;
4484 internal_context->peer->pg->starting--;
4485 internal_context->peer->pg->started++;
4486 if (internal_context->hostkey_callback != NULL)
4487 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
4489 else if (internal_context->peer->pg->started ==
4490 internal_context->peer->pg->total)
4492 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
4493 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
4498 * Callback that is called whenever a peer has finished starting.
4499 * Call the real callback and decrement the starting counter
4500 * for the peergroup.
4502 * @param cls closure
4503 * @param id identifier for the daemon, NULL on error
4505 * @param d handle for the daemon
4506 * @param emsg error message (NULL on success)
4509 internal_startup_callback (void *cls,
4510 const struct GNUNET_PeerIdentity *id,
4511 const struct GNUNET_CONFIGURATION_Handle *cfg,
4512 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4514 struct InternalStartContext *internal_context = cls;
4515 internal_context->peer->pg->starting--;
4516 if (internal_context->start_cb != NULL)
4517 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
4522 internal_continue_startup (void *cls,
4523 const struct GNUNET_SCHEDULER_TaskContext *tc)
4525 struct InternalStartContext *internal_context = cls;
4527 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4532 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
4534 internal_context->peer->pg->starting++;
4535 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
4539 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4540 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4541 &internal_continue_startup,
4548 * Callback for informing us about a successful
4549 * or unsuccessful churn start call.
4551 * @param cls a ChurnContext
4552 * @param id the peer identity of the started peer
4553 * @param cfg the handle to the configuration of the peer
4554 * @param d handle to the daemon for the peer
4555 * @param emsg NULL on success, non-NULL on failure
4559 churn_start_callback (void *cls,
4560 const struct GNUNET_PeerIdentity *id,
4561 const struct GNUNET_CONFIGURATION_Handle *cfg,
4562 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4564 struct ChurnRestartContext *startup_ctx = cls;
4565 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
4567 unsigned int total_left;
4568 char *error_message;
4570 error_message = NULL;
4573 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4574 "Churn stop callback failed with error `%s'\n", emsg);
4575 churn_ctx->num_failed_start++;
4579 churn_ctx->num_to_start--;
4583 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4584 "Started peer, %d left.\n", churn_ctx->num_to_start);
4588 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
4589 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4591 if (total_left == 0)
4593 if ((churn_ctx->num_failed_stop > 0)
4594 || (churn_ctx->num_failed_start > 0))
4595 GNUNET_asprintf (&error_message,
4596 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4597 churn_ctx->num_failed_start,
4598 churn_ctx->num_failed_stop);
4599 churn_ctx->cb (churn_ctx->cb_cls, error_message);
4600 GNUNET_free_non_null (error_message);
4601 GNUNET_free (churn_ctx);
4602 GNUNET_free (startup_ctx);
4608 schedule_churn_restart (void *cls,
4609 const struct GNUNET_SCHEDULER_TaskContext *tc)
4611 struct PeerRestartContext *peer_restart_ctx = cls;
4612 struct ChurnRestartContext *startup_ctx =
4613 peer_restart_ctx->churn_restart_ctx;
4615 if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
4616 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4617 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4618 &schedule_churn_restart, peer_restart_ctx);
4621 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
4622 startup_ctx->timeout,
4623 &churn_start_callback,
4625 GNUNET_free (peer_restart_ctx);
4630 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4632 struct InternalStartContext *internal_context = cls;
4634 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4639 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
4641 internal_context->peer->pg->starting++;
4642 internal_context->peer->daemon =
4643 GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
4644 internal_context->timeout,
4645 internal_context->hostname,
4646 internal_context->username,
4647 internal_context->sshport,
4648 internal_context->hostkey,
4649 &internal_hostkey_callback,
4651 &internal_startup_callback,
4656 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4657 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4658 &internal_start, internal_context);
4663 * Function which continues a peer group starting up
4664 * after successfully generating hostkeys for each peer.
4666 * @param pg the peer group to continue starting
4670 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg)
4675 for (i = 0; i < pg->total; i++)
4677 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
4678 &pg->peers[i].internal_context);
4679 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
4684 * Start count gnunet instances with the same set of transports and
4685 * applications. The port numbers (any option called "PORT") will be
4686 * adjusted to ensure that no two peers running on the same system
4687 * have the same port(s) in their respective configurations.
4689 * @param cfg configuration template to use
4690 * @param total number of daemons to start
4691 * @param timeout total time allowed for peers to start
4692 * @param hostkey_callback function to call on each peers hostkey generation
4693 * if NULL, peers will be started by this call, if non-null,
4694 * GNUNET_TESTING_daemons_continue_startup must be called after
4695 * successful hostkey generation
4696 * @param hostkey_cls closure for hostkey callback
4697 * @param cb function to call on each daemon that was started
4698 * @param cb_cls closure for cb
4699 * @param connect_callback function to call each time two hosts are connected
4700 * @param connect_callback_cls closure for connect_callback
4701 * @param hostnames linked list of host structs to use to start peers on
4702 * (NULL to run on localhost only)
4704 * @return NULL on error, otherwise handle to control peer group
4706 struct GNUNET_TESTING_PeerGroup *
4707 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
4709 struct GNUNET_TIME_Relative timeout,
4710 GNUNET_TESTING_NotifyHostkeyCreated
4711 hostkey_callback, void *hostkey_cls,
4712 GNUNET_TESTING_NotifyDaemonRunning cb,
4714 GNUNET_TESTING_NotifyConnection
4715 connect_callback, void *connect_callback_cls,
4716 const struct GNUNET_TESTING_Host *hostnames)
4718 struct GNUNET_TESTING_PeerGroup *pg;
4719 const struct GNUNET_TESTING_Host *hostpos;
4725 const char *hostname;
4726 const char *username;
4727 char *baseservicehome;
4728 char *newservicehome;
4730 char *hostkeys_file;
4733 struct GNUNET_DISK_FileHandle *fd;
4734 struct GNUNET_CONFIGURATION_Handle *pcfg;
4736 unsigned int hostcnt;
4743 uint64_t total_hostkeys;
4744 struct GNUNET_OS_Process *proc;
4754 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
4756 pg->notify_connection = connect_callback;
4757 pg->notify_connection_cls = connect_callback_cls;
4759 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
4760 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
4761 if (NULL != hostnames)
4764 hostpos = hostnames;
4765 while (hostpos != NULL)
4767 hostpos = hostpos->next;
4770 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4773 hostpos = hostnames;
4774 while (hostpos != NULL)
4776 pg->hosts[off].minport = LOW_PORT;
4777 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
4778 if (hostpos->username != NULL)
4779 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
4780 pg->hosts[off].sshport = hostpos->port;
4781 hostpos = hostpos->next;
4791 pg->num_hosts = off;
4795 /* skip leading spaces */
4796 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
4799 while ('\0' != *rpos)
4801 if (isspace ((unsigned char) *rpos))
4805 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4807 start = GNUNET_strdup (hostnames);
4809 while ('\0' != *pos)
4811 if (isspace ((unsigned char) *pos))
4814 if (strlen (start) > 0)
4816 pg->hosts[off].minport = LOW_PORT;
4817 pg->hosts[off++].hostname = start;
4823 if (strlen (start) > 0)
4825 pg->hosts[off].minport = LOW_PORT;
4826 pg->hosts[off++].hostname = start;
4830 GNUNET_free (start);
4831 GNUNET_free (pg->hosts);
4835 minport = 0; /* make gcc happy */
4844 /* Create the servicehome directory for each remote peer */
4845 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
4847 for (i = 0; i < pg->num_hosts; i++)
4850 if (NULL != pg->hosts[i].username)
4851 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username, pg->hosts[i].hostname);
4853 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
4854 if (pg->hosts[i].sshport != 0)
4856 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
4857 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
4858 "ssh", "-P", ssh_port_str,
4862 arg, "mkdir -p", baseservicehome, NULL);
4865 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
4866 "ssh", arg, "mkdir -p", baseservicehome, NULL);
4867 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating remote dir with command ssh %s %s %s\n", arg, " mkdir -p ", baseservicehome);
4868 GNUNET_OS_process_wait(proc);
4870 GNUNET_free(baseservicehome);
4872 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "HOSTKEYSFILE",
4875 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
4876 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!\n");
4879 /* Check hostkey file size, read entire thing into memory */
4880 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
4881 GNUNET_DISK_PERM_NONE);
4884 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
4888 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
4891 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found file size %llu for hostkeys, expect hostkeys to be size %d\n", fs, HOSTKEYFILESIZE);
4893 if (fs % HOSTKEYFILESIZE != 0)
4895 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "File size %llu seems incorrect for hostkeys...\n", fs);
4899 total_hostkeys = fs / HOSTKEYFILESIZE;
4900 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Will read %llu hostkeys from file\n", total_hostkeys);
4901 pg->hostkey_data = GNUNET_malloc_large (fs);
4902 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
4903 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
4906 GNUNET_free(hostkeys_file);
4909 for (off = 0; off < total; off++)
4913 hostname = pg->hosts[off % hostcnt].hostname;
4914 username = pg->hosts[off % hostcnt].username;
4915 sshport = pg->hosts[off % hostcnt].sshport;
4916 pcfg = make_config (cfg,
4917 &pg->hosts[off % hostcnt].minport,
4918 &upnum, hostname, &fdnum);
4925 pcfg = make_config (cfg, &minport, &upnum, hostname, &fdnum);
4930 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4932 ("Could not create configuration for peer number %u on `%s'!\n"),
4933 off, hostname == NULL ? "localhost" : hostname);
4938 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
4941 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
4942 GNUNET_free (baseservicehome);
4946 tmpdir = getenv ("TMPDIR");
4947 tmpdir = tmpdir ? tmpdir : "/tmp";
4948 GNUNET_asprintf (&newservicehome,
4950 tmpdir, "gnunet-testing-test-test", off);
4952 GNUNET_CONFIGURATION_set_value_string (pcfg,
4954 "SERVICEHOME", newservicehome);
4955 GNUNET_free (newservicehome);
4956 pg->peers[off].cfg = pcfg;
4958 /* Can we do this later? */
4959 pg->peers[off].allowed_peers =
4960 GNUNET_CONTAINER_multihashmap_create (total);
4961 pg->peers[off].connect_peers =
4962 GNUNET_CONTAINER_multihashmap_create (total);
4963 pg->peers[off].blacklisted_peers =
4964 GNUNET_CONTAINER_multihashmap_create (total);
4967 pg->peers[off].pg = pg;
4968 pg->peers[off].internal_context.peer = &pg->peers[off];
4969 pg->peers[off].internal_context.timeout = timeout;
4970 pg->peers[off].internal_context.hostname = hostname;
4971 pg->peers[off].internal_context.username = username;
4972 pg->peers[off].internal_context.sshport = sshport;
4973 if (pg->hostkey_data != NULL)
4974 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off * HOSTKEYFILESIZE];
4975 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
4976 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
4977 pg->peers[off].internal_context.start_cb = cb;
4978 pg->peers[off].internal_context.start_cb_cls = cb_cls;
4980 GNUNET_SCHEDULER_add_now (&internal_start,
4981 &pg->peers[off].internal_context);
4988 * Get a daemon by number, so callers don't have to do nasty
4989 * offsetting operation.
4991 struct GNUNET_TESTING_Daemon *
4992 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
4993 unsigned int position)
4995 if (position < pg->total)
4996 return pg->peers[position].daemon;
5002 * Get a daemon by peer identity, so callers can
5003 * retrieve the daemon without knowing it's offset.
5005 * @param pg the peer group to retrieve the daemon from
5006 * @param peer_id the peer identity of the daemon to retrieve
5008 * @return the daemon on success, or NULL if no such peer identity is found
5010 struct GNUNET_TESTING_Daemon *
5011 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
5012 struct GNUNET_PeerIdentity *peer_id)
5016 for (i = 0; i < pg->total; i++)
5019 memcmp (&pg->peers[i].daemon->id, peer_id,
5020 sizeof (struct GNUNET_PeerIdentity)))
5021 return pg->peers[i].daemon;
5028 * Prototype of a function that will be called when a
5029 * particular operation was completed the testing library.
5031 * @param cls closure (a struct RestartContext)
5032 * @param id id of the peer that was restarted
5033 * @param cfg handle to the configuration of the peer
5034 * @param d handle to the daemon that was restarted
5035 * @param emsg NULL on success
5038 restart_callback (void *cls,
5039 const struct GNUNET_PeerIdentity *id,
5040 const struct GNUNET_CONFIGURATION_Handle *cfg,
5041 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5043 struct RestartContext *restart_context = cls;
5047 restart_context->peers_restarted++;
5051 restart_context->peers_restart_failed++;
5054 if (restart_context->peers_restarted == restart_context->peer_group->total)
5056 restart_context->callback (restart_context->callback_cls, NULL);
5057 GNUNET_free (restart_context);
5059 else if (restart_context->peers_restart_failed +
5060 restart_context->peers_restarted ==
5061 restart_context->peer_group->total)
5063 restart_context->callback (restart_context->callback_cls,
5064 "Failed to restart peers!");
5065 GNUNET_free (restart_context);
5071 * Callback for informing us about a successful
5072 * or unsuccessful churn stop call.
5074 * @param cls a ChurnContext
5075 * @param emsg NULL on success, non-NULL on failure
5079 churn_stop_callback (void *cls, const char *emsg)
5081 struct ShutdownContext *shutdown_ctx = cls;
5082 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
5083 unsigned int total_left;
5084 char *error_message;
5086 error_message = NULL;
5087 shutdown_ctx->outstanding--;
5091 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5092 "Churn stop callback failed with error `%s'\n", emsg);
5093 churn_ctx->num_failed_stop++;
5097 churn_ctx->num_to_stop--;
5101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5102 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
5105 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
5106 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5108 if (total_left == 0)
5110 if ((churn_ctx->num_failed_stop > 0)
5111 || (churn_ctx->num_failed_start > 0))
5113 GNUNET_asprintf (&error_message,
5114 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5115 churn_ctx->num_failed_start,
5116 churn_ctx->num_failed_stop);
5118 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5119 GNUNET_free_non_null (error_message);
5120 GNUNET_free (churn_ctx);
5121 GNUNET_free (shutdown_ctx);
5126 * Count the number of running peers.
5128 * @param pg handle for the peer group
5130 * @return the number of currently running peers in the peer group
5133 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
5136 unsigned int running = 0;
5137 for (i = 0; i < pg->total; i++)
5139 if (pg->peers[i].daemon->running == GNUNET_YES)
5141 GNUNET_assert (running != -1);
5149 * Task to rate limit the number of outstanding peer shutdown
5150 * requests. This is necessary for making sure we don't do
5151 * too many ssh connections at once, but is generally nicer
5152 * to any system as well (graduated task starts, as opposed
5153 * to calling gnunet-arm N times all at once).
5156 schedule_churn_shutdown_task (void *cls,
5157 const struct GNUNET_SCHEDULER_TaskContext *tc)
5159 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5160 struct ShutdownContext *shutdown_ctx;
5162 GNUNET_assert (peer_shutdown_ctx != NULL);
5163 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5164 GNUNET_assert (shutdown_ctx != NULL);
5166 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
5167 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5168 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5169 &schedule_churn_shutdown_task,
5173 shutdown_ctx->outstanding++;
5174 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5175 shutdown_ctx->timeout, shutdown_ctx->cb,
5176 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5177 GNUNET_free (peer_shutdown_ctx);
5182 * Simulate churn by stopping some peers (and possibly
5183 * re-starting others if churn is called multiple times). This
5184 * function can only be used to create leave-join churn (peers "never"
5185 * leave for good). First "voff" random peers that are currently
5186 * online will be taken offline; then "von" random peers that are then
5187 * offline will be put back online. No notifications will be
5188 * generated for any of these operations except for the callback upon
5191 * @param pg handle for the peer group
5192 * @param voff number of peers that should go offline
5193 * @param von number of peers that should come back online;
5194 * must be zero on first call (since "testbed_start"
5195 * always starts all of the peers)
5196 * @param timeout how long to wait for operations to finish before
5198 * @param cb function to call at the end
5199 * @param cb_cls closure for cb
5202 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
5205 struct GNUNET_TIME_Relative timeout,
5206 GNUNET_TESTING_NotifyCompletion cb,
5209 struct ChurnContext *churn_ctx;
5210 struct ShutdownContext *shutdown_ctx;
5211 struct PeerShutdownContext *peer_shutdown_ctx;
5212 struct PeerRestartContext *peer_restart_ctx;
5213 struct ChurnRestartContext *churn_startup_ctx;
5215 unsigned int running;
5216 unsigned int stopped;
5217 unsigned int total_running;
5218 unsigned int total_stopped;
5220 unsigned int *running_arr;
5221 unsigned int *stopped_arr;
5222 unsigned int *running_permute;
5223 unsigned int *stopped_permute;
5228 if ((von == 0) && (voff == 0)) /* No peers at all? */
5234 for (i = 0; i < pg->total; i++)
5236 if (pg->peers[i].daemon->running == GNUNET_YES)
5238 GNUNET_assert (running != -1);
5243 GNUNET_assert (stopped != -1);
5250 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5251 "Trying to stop more peers than are currently running!\n");
5252 cb (cb_cls, "Trying to stop more peers than are currently running!");
5258 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5259 "Trying to start more peers than are currently stopped!\n");
5260 cb (cb_cls, "Trying to start more peers than are currently stopped!");
5264 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5268 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
5272 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
5274 running_permute = NULL;
5275 stopped_permute = NULL;
5279 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, running);
5282 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, stopped);
5284 total_running = running;
5285 total_stopped = stopped;
5289 churn_ctx->num_to_start = von;
5290 churn_ctx->num_to_stop = voff;
5292 churn_ctx->cb_cls = cb_cls;
5294 for (i = 0; i < pg->total; i++)
5296 if (pg->peers[i].daemon->running == GNUNET_YES)
5298 GNUNET_assert ((running_arr != NULL) && (total_running > running));
5299 running_arr[running] = i;
5304 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
5305 stopped_arr[stopped] = i;
5310 GNUNET_assert (running >= voff);
5313 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5314 shutdown_ctx->cb = &churn_stop_callback;
5315 shutdown_ctx->cb_cls = churn_ctx;
5316 shutdown_ctx->total_peers = voff;
5317 shutdown_ctx->timeout = timeout;
5320 for (i = 0; i < voff; i++)
5323 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
5324 running_permute[i]);
5326 GNUNET_assert (running_arr != NULL);
5327 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
5328 peer_shutdown_ctx->daemon =
5329 pg->peers[running_arr[running_permute[i]]].daemon;
5330 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
5331 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
5335 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
5337 &churn_stop_callback, churn_ctx,
5338 GNUNET_NO, GNUNET_YES); */
5341 GNUNET_assert (stopped >= von);
5344 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
5345 churn_startup_ctx->churn_ctx = churn_ctx;
5346 churn_startup_ctx->timeout = timeout;
5348 for (i = 0; i < von; i++)
5351 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
5352 stopped_permute[i]);
5354 GNUNET_assert (stopped_arr != NULL);
5355 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
5356 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
5357 peer_restart_ctx->daemon =
5358 pg->peers[stopped_arr[stopped_permute[i]]].daemon;
5359 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
5361 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
5362 timeout, &churn_start_callback, churn_ctx); */
5365 GNUNET_free_non_null (running_arr);
5366 GNUNET_free_non_null (stopped_arr);
5367 GNUNET_free_non_null (running_permute);
5368 GNUNET_free_non_null (stopped_permute);
5373 * Restart all peers in the given group.
5375 * @param pg the handle to the peer group
5376 * @param callback function to call on completion (or failure)
5377 * @param callback_cls closure for the callback function
5380 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
5381 GNUNET_TESTING_NotifyCompletion callback,
5384 struct RestartContext *restart_context;
5389 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
5390 restart_context->peer_group = pg;
5391 restart_context->peers_restarted = 0;
5392 restart_context->callback = callback;
5393 restart_context->callback_cls = callback_cls;
5395 for (off = 0; off < pg->total; off++)
5397 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
5398 &restart_callback, restart_context);
5404 * Start or stop an individual peer from the given group.
5406 * @param pg handle to the peer group
5407 * @param offset which peer to start or stop
5408 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
5409 * @param timeout how long to wait for shutdown
5410 * @param cb function to call at the end
5411 * @param cb_cls closure for cb
5414 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
5415 unsigned int offset,
5417 struct GNUNET_TIME_Relative timeout,
5418 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5420 struct ShutdownContext *shutdown_ctx;
5421 struct ChurnRestartContext *startup_ctx;
5422 struct ChurnContext *churn_ctx;
5424 if (GNUNET_NO == desired_status)
5426 if (NULL != pg->peers[offset].daemon)
5428 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5429 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5430 churn_ctx->num_to_start = 0;
5431 churn_ctx->num_to_stop = 1;
5433 churn_ctx->cb_cls = cb_cls;
5434 shutdown_ctx->cb_cls = churn_ctx;
5435 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon,
5436 timeout, &churn_stop_callback,
5437 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5440 else if (GNUNET_YES == desired_status)
5442 if (NULL == pg->peers[offset].daemon)
5444 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
5445 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5446 churn_ctx->num_to_start = 1;
5447 churn_ctx->num_to_stop = 0;
5449 churn_ctx->cb_cls = cb_cls;
5450 startup_ctx->churn_ctx = churn_ctx;
5451 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
5452 timeout, &churn_start_callback,
5462 * Callback for shutting down peers in a peer group.
5464 * @param cls closure (struct ShutdownContext)
5465 * @param emsg NULL on success
5468 internal_shutdown_callback (void *cls, const char *emsg)
5470 struct ShutdownContext *shutdown_ctx = cls;
5472 shutdown_ctx->outstanding--;
5475 shutdown_ctx->peers_down++;
5479 shutdown_ctx->peers_failed++;
5482 if ((shutdown_ctx->cb != NULL)
5483 && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed ==
5484 shutdown_ctx->total_peers))
5486 if (shutdown_ctx->peers_failed > 0)
5487 shutdown_ctx->cb (shutdown_ctx->cb_cls,
5488 "Not all peers successfully shut down!");
5490 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
5491 GNUNET_free (shutdown_ctx);
5497 * Task to rate limit the number of outstanding peer shutdown
5498 * requests. This is necessary for making sure we don't do
5499 * too many ssh connections at once, but is generally nicer
5500 * to any system as well (graduated task starts, as opposed
5501 * to calling gnunet-arm N times all at once).
5504 schedule_shutdown_task (void *cls,
5505 const struct GNUNET_SCHEDULER_TaskContext *tc)
5507 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5508 struct ShutdownContext *shutdown_ctx;
5510 GNUNET_assert (peer_shutdown_ctx != NULL);
5511 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5512 GNUNET_assert (shutdown_ctx != NULL);
5514 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
5515 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5516 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5517 &schedule_shutdown_task, peer_shutdown_ctx);
5520 shutdown_ctx->outstanding++;
5521 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5522 shutdown_ctx->timeout,
5523 &internal_shutdown_callback, shutdown_ctx,
5524 GNUNET_YES, GNUNET_NO);
5525 GNUNET_free (peer_shutdown_ctx);
5530 * Shutdown all peers started in the given group.
5532 * @param pg handle to the peer group
5533 * @param timeout how long to wait for shutdown
5534 * @param cb callback to notify upon success or failure
5535 * @param cb_cls closure for cb
5538 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
5539 struct GNUNET_TIME_Relative timeout,
5540 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5543 struct ShutdownContext *shutdown_ctx;
5544 struct PeerShutdownContext *peer_shutdown_ctx;
5546 struct PeerConnection *conn_iter;
5547 struct PeerConnection *temp_conn;
5550 GNUNET_assert (pg->total > 0);
5552 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5553 shutdown_ctx->cb = cb;
5554 shutdown_ctx->cb_cls = cb_cls;
5555 shutdown_ctx->total_peers = pg->total;
5556 shutdown_ctx->timeout = timeout;
5557 /* shtudown_ctx->outstanding = 0; */
5559 for (off = 0; off < pg->total; off++)
5561 GNUNET_assert (NULL != pg->peers[off].daemon);
5562 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
5563 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
5564 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
5565 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
5567 if (NULL != pg->peers[off].cfg)
5568 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
5570 conn_iter = pg->peers[off].allowed_peers_head;
5571 while (conn_iter != NULL)
5573 temp_conn = conn_iter->next;
5574 GNUNET_free(conn_iter);
5575 conn_iter = temp_conn;
5578 conn_iter = pg->peers[off].connect_peers_head;
5579 while (conn_iter != NULL)
5581 temp_conn = conn_iter->next;
5582 GNUNET_free(conn_iter);
5583 conn_iter = temp_conn;
5586 conn_iter = pg->peers[off].blacklisted_peers_head;
5587 while (conn_iter != NULL)
5589 temp_conn = conn_iter->next;
5590 GNUNET_free(conn_iter);
5591 conn_iter = temp_conn;
5594 conn_iter = pg->peers[off].connect_peers_working_set_head;
5595 while (conn_iter != NULL)
5597 temp_conn = conn_iter->next;
5598 GNUNET_free(conn_iter);
5599 conn_iter = temp_conn;
5602 if (pg->peers[off].allowed_peers != NULL)
5603 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
5604 if (pg->peers[off].connect_peers != NULL)
5605 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
5606 if (pg->peers[off].blacklisted_peers != NULL)
5607 GNUNET_CONTAINER_multihashmap_destroy (pg->
5608 peers[off].blacklisted_peers);
5611 GNUNET_free (pg->peers);
5612 GNUNET_free_non_null(pg->hostkey_data);
5613 for (off = 0; off < pg->num_hosts; off++)
5615 GNUNET_free (pg->hosts[off].hostname);
5616 GNUNET_free_non_null (pg->hosts[off].username);
5618 GNUNET_free_non_null (pg->hosts);
5623 /* end of testing_group.c */