2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing_group.c
23 * @brief convenience API for writing testcases for GNUnet
24 * @author Nathan Evans
25 * @author Christian Grothoff
29 #include "gnunet_arm_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_core_service.h"
33 #define VERBOSE_TESTING GNUNET_YES
35 #define VERBOSE_TOPOLOGY GNUNET_YES
37 #define DEBUG_CHURN GNUNET_NO
42 * Lowest port used for GNUnet testing. Should be high enough to not
43 * conflict with other applications running on the hosts but be low
44 * enough to not conflict with client-ports (typically starting around
47 #define LOW_PORT 10000
50 * Highest port used for GNUnet testing. Should be low enough to not
51 * conflict with the port range for "local" ports (client apps; see
52 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
54 #define HIGH_PORT 56000
56 #define MAX_OUTSTANDING_CONNECTIONS 200
58 #define MAX_CONCURRENT_HOSTKEYS 500
60 #define MAX_CONCURRENT_STARTING 200
62 #define MAX_CONCURRENT_SHUTDOWN 200
64 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 180)
66 #define CONNECT_ATTEMPTS 30
69 * Which list of peers do we need to modify?
73 /** Modify allowed peers */
76 /** Modify connect peers */
79 /** Modify blacklist peers */
82 /** Modify workingset peers */
87 * Prototype of a function called whenever two peers would be connected
88 * in a certain topology.
90 typedef unsigned int (*GNUNET_TESTING_ConnectionProcessor) (struct
91 GNUNET_TESTING_PeerGroup
101 * Context for handling churning a peer group
106 * Callback used to notify of churning finished
108 GNUNET_TESTING_NotifyCompletion cb;
111 * Closure for callback
116 * Number of peers that still need to be started
118 unsigned int num_to_start;
121 * Number of peers that still need to be stopped
123 unsigned int num_to_stop;
126 * Number of peers that failed to start
128 unsigned int num_failed_start;
131 * Number of peers that failed to stop
133 unsigned int num_failed_stop;
136 struct RestartContext
139 * The group of peers being restarted
141 struct GNUNET_TESTING_PeerGroup *peer_group;
144 * How many peers have been restarted thus far
146 unsigned int peers_restarted;
149 * How many peers got an error when restarting
151 unsigned int peers_restart_failed;
154 * The function to call once all peers have been restarted
156 GNUNET_TESTING_NotifyCompletion callback;
159 * Closure for callback function
166 struct ShutdownContext
169 * Total peers to wait for
171 unsigned int total_peers;
174 * Number of peers successfully shut down
176 unsigned int peers_down;
179 * Number of peers failed to shut down
181 unsigned int peers_failed;
184 * Number of peers we have started shutting
185 * down. If too many, wait on them.
187 unsigned int outstanding;
190 * Timeout for shutdown.
192 struct GNUNET_TIME_Relative timeout;
195 * Callback to call when all peers either
196 * shutdown or failed to shutdown
198 GNUNET_TESTING_NotifyCompletion cb;
207 * Individual shutdown context for a particular peer.
209 struct PeerShutdownContext
212 * Pointer to the high level shutdown context.
214 struct ShutdownContext *shutdown_ctx;
217 * The daemon handle for the peer to shut down.
219 struct GNUNET_TESTING_Daemon *daemon;
223 * Individual shutdown context for a particular peer.
225 struct PeerRestartContext
228 * Pointer to the high level restart context.
230 struct ChurnRestartContext *churn_restart_ctx;
233 * The daemon handle for the peer to shut down.
235 struct GNUNET_TESTING_Daemon *daemon;
239 struct CreateTopologyContext
243 * Function to call with number of connections
245 GNUNET_TESTING_NotifyConnections cont;
248 * Closure for connection notification
255 /** Waiting to read number of peers */
258 /** Should find next peer index */
261 /** Should find colon */
264 /** Should read other peer index, space, or endline */
270 struct PeerConnection
275 struct PeerConnection *prev;
280 struct PeerConnection *next;
284 * Index of daemon in pg->peers
291 struct InternalStartContext
294 * Pointer to peerdata
296 struct PeerData *peer;
299 * Timeout for peer startup
301 struct GNUNET_TIME_Relative timeout;
304 * Client callback for hostkey notification
306 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
309 * Closure for hostkey_callback
314 * Client callback for peer start notification
316 GNUNET_TESTING_NotifyDaemonRunning start_cb;
324 * Hostname, where to start the peer
326 const char *hostname;
329 * Username to use when connecting to the
332 const char *username;
335 * Pointer to starting memory location of a hostkey
340 * Port to use for ssh.
346 struct ChurnRestartContext
349 * Number of restarts currently in flight.
351 unsigned int outstanding;
354 * Handle to the underlying churn context.
356 struct ChurnContext *churn_ctx;
359 * How long to allow the operation to take.
361 struct GNUNET_TIME_Relative timeout;
365 * Data we keep per peer.
370 * (Initial) configuration of the host.
371 * (initial because clients could change
372 * it and we would not know about those
375 struct GNUNET_CONFIGURATION_Handle *cfg;
378 * Handle for controlling the daemon.
380 struct GNUNET_TESTING_Daemon *daemon;
383 * The peergroup this peer belongs to.
385 struct GNUNET_TESTING_PeerGroup *pg;
389 * Linked list of allowed peer connections.
391 struct PeerConnection *allowed_peers_head;
394 * Linked list of allowed peer connections.
396 struct PeerConnection *allowed_peers_tail;
399 * Linked list of blacklisted peer connections.
401 struct PeerConnection *blacklisted_peers_head;
404 * Linked list of blacklisted peer connections.
406 struct PeerConnection *blacklisted_peers_tail;
409 * Linked list of connect peer connections.
411 struct PeerConnection *connect_peers_head;
414 * Linked list of connect peer connections.
416 struct PeerConnection *connect_peers_tail;
419 * Linked list of connect peer connections.
421 struct PeerConnection *connect_peers_working_set_head;
424 * Linked list of connect peer connections.
426 struct PeerConnection *connect_peers_working_set_tail;
430 * Hash map of allowed peer connections (F2F created topology)
432 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
435 * Hash map of blacklisted peers
437 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
440 * Hash map of peer connections
442 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
445 * Temporary hash map of peer connections
447 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
451 * Temporary variable for topology creation, should be reset before
452 * creating any topology so the count is valid once finished.
457 * Context to keep track of peers being started, to
458 * stagger hostkey generation and peer startup.
460 struct InternalStartContext internal_context;
465 * Linked list of per-host data.
475 * SSH username to use when connecting to this host.
480 * SSH port to use when connecting to this host.
485 * Lowest port that we have not yet used
491 struct TopologyIterateContext
494 * Callback for notifying of two connected peers.
496 GNUNET_TESTING_NotifyTopology topology_cb;
499 * Closure for topology_cb
504 * Number of peers currently connected to.
506 unsigned int connected;
509 * Number of peers we have finished iterating.
511 unsigned int completed;
514 * Number of peers total.
519 struct StatsIterateContext
522 * Continuation to call once all stats information has been retrieved.
524 GNUNET_STATISTICS_Callback cont;
527 * Proc function to call on each value received.
529 GNUNET_TESTING_STATISTICS_Iterator proc;
532 * Closure for topology_cb
537 * Number of peers currently connected to.
539 unsigned int connected;
542 * Number of peers we have finished iterating.
544 unsigned int completed;
547 * Number of peers total.
555 struct GNUNET_TESTING_Daemon *daemon;
558 struct StatsCoreContext
561 struct GNUNET_TESTING_Daemon *daemon;
563 * Handle to the statistics service.
565 struct GNUNET_STATISTICS_Handle *stats_handle;
568 * Handle for getting statistics.
570 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
574 * Handle to a group of GNUnet peers.
576 struct GNUNET_TESTING_PeerGroup
579 * Configuration template.
581 const struct GNUNET_CONFIGURATION_Handle *cfg;
584 * Function to call on each started daemon.
586 //GNUNET_TESTING_NotifyDaemonRunning cb;
594 * Function to call on each topology connection created
596 GNUNET_TESTING_NotifyConnection notify_connection;
599 * Callback for notify_connection
601 void *notify_connection_cls;
604 * Array of information about hosts.
606 struct HostData *hosts;
609 * Number of hosts (size of HostData)
611 unsigned int num_hosts;
614 * Array of "total" peers.
616 struct PeerData *peers;
619 * Number of peers in this group.
624 * At what time should we fail the peer startup process?
626 struct GNUNET_TIME_Absolute max_timeout;
629 * How many peers are being started right now?
631 unsigned int starting;
634 * How many peers have already been started?
636 unsigned int started;
639 * Hostkeys loaded from a file.
646 struct GNUNET_CONFIGURATION_Handle *ret;
647 const struct GNUNET_CONFIGURATION_Handle *orig;
648 const char *hostname;
654 struct ConnectTopologyContext
657 * How many connections are left to create.
659 unsigned int remaining_connections;
662 * Handle to group of peers.
664 struct GNUNET_TESTING_PeerGroup *pg;
668 * Temp value set for each iteration.
670 //struct PeerData *first;
673 * Notification that all peers are connected.
675 GNUNET_TESTING_NotifyCompletion notify_connections_done;
678 * Closure for notify.
683 struct ConnectContext
686 * Peer to connect second to.
688 struct GNUNET_TESTING_Daemon *first;
691 * Peer to connect first to.
693 struct GNUNET_TESTING_Daemon *second;
696 * Higher level topology connection context.
698 struct ConnectTopologyContext *ct_ctx;
701 struct UnblacklistContext
706 struct GNUNET_TESTING_PeerGroup *pg;
709 * uid of the first peer
719 struct GNUNET_TESTING_PeerGroup *pg;
722 * uid of the first peer
727 * Peer data for first peer.
729 struct PeerData *first;
732 * Random percentage to use
737 struct MinimumContext
742 struct GNUNET_TESTING_PeerGroup *pg;
745 * uid of the first peer
750 * Peer data for first peer.
752 struct PeerData *first;
755 * Number of conns per peer
757 unsigned int num_to_add;
760 * Permuted array of all possible connections. Only add the Nth
761 * peer if it's in the Nth position.
763 unsigned int *pg_array;
766 * What number is the current element we are iterating over?
768 unsigned int current;
776 struct GNUNET_TESTING_PeerGroup *pg;
779 * uid of the first peer
784 * uid of the second peer
789 * Peer data for first peer.
791 struct PeerData *first;
794 * Which peer has been chosen as the one to add?
799 * What number is the current element we are iterating over?
801 unsigned int current;
806 * Convert unique ID to hash code.
808 * @param uid unique ID to convert
809 * @param hash set to uid (extended with zeros)
812 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
814 memset (hash, 0, sizeof (GNUNET_HashCode));
815 *((uint32_t *) hash) = uid;
819 * Convert hash code to unique ID.
821 * @param uid unique ID to convert
822 * @param hash set to uid (extended with zeros)
825 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
827 memcpy (uid, hash, sizeof (uint32_t));
832 * Number of connects we are waiting on, allows us to rate limit
835 static int outstanding_connects;
838 * Get a topology from a string input.
840 * @param topology where to write the retrieved topology
841 * @param topology_string The string to attempt to
842 * get a configuration value from
843 * @return GNUNET_YES if topology string matched a
844 * known topology, GNUNET_NO if not
847 GNUNET_TESTING_topology_get (enum GNUNET_TESTING_Topology *topology,
848 const char *topology_string)
851 * Strings representing topologies in enum
853 static const char *topology_strings[] = {
855 * A clique (everyone connected to everyone else).
860 * Small-world network (2d torus plus random links).
865 * Small-world network (ring plus random links).
885 * Certain percentage of peers are unable to communicate directly
886 * replicating NAT conditions
891 * Scale free topology.
896 * Straight line topology.
901 * All peers are disconnected.
906 * Read the topology from a file.
914 if (topology_string == NULL)
916 while (topology_strings[curr] != NULL)
918 if (strcasecmp (topology_strings[curr], topology_string) == 0)
925 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
931 * Get connect topology option from string input.
933 * @param topology_option where to write the retrieved topology
934 * @param topology_string The string to attempt to
935 * get a configuration value from
936 * @return GNUNET_YES if string matched a known
937 * topology option, GNUNET_NO if not
940 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption
942 const char *topology_string)
945 * Options for connecting a topology as strings.
947 static const char *topology_option_strings[] = {
949 * Try to connect all peers specified in the topology.
954 * Choose a random subset of connections to create.
956 "CONNECT_RANDOM_SUBSET",
959 * Create at least X connections for each peer.
964 * Using a depth first search, create one connection
965 * per peer. If any are missed (graph disconnected)
966 * start over at those peers until all have at least one
972 * Find the N closest peers to each allowed peer in the
973 * topology and make sure a connection to those peers
974 * exists in the connect topology.
979 * No options specified.
987 if (topology_string == NULL)
989 while (NULL != topology_option_strings[curr])
991 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
993 *topology_option = curr;
998 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1003 * Function to iterate over options. Copies
1004 * the options to the target configuration,
1005 * updating PORT values as needed.
1007 * @param cls closure
1008 * @param section name of the section
1009 * @param option name of the option
1010 * @param value value of the option
1013 update_config (void *cls,
1014 const char *section, const char *option, const char *value)
1016 struct UpdateContext *ctx = cls;
1020 char *single_variable;
1021 char *per_host_variable;
1022 unsigned long long num_per_host;
1024 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1026 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1029 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1032 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
1036 GNUNET_free (single_variable);
1039 if (0 == strcmp (option, "UNIXPATH"))
1041 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1042 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1044 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1047 GNUNET_snprintf (uval,
1049 "/tmp/test-service-%s-%u", section, ctx->upnum++);
1052 else if ((GNUNET_YES ==
1053 GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1055 &num_per_host)) && (num_per_host > 0))
1058 GNUNET_snprintf (uval,
1060 "/tmp/test-service-%s-%u",
1061 section, ctx->fdnum % num_per_host);
1064 GNUNET_free (single_variable);
1065 GNUNET_free (per_host_variable);
1068 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1070 value = ctx->hostname;
1073 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1078 * Create a new configuration using the given configuration
1079 * as a template; however, each PORT in the existing cfg
1080 * must be renumbered by incrementing "*port". If we run
1081 * out of "*port" numbers, return NULL.
1083 * @param cfg template configuration
1084 * @param port port numbers to use, update to reflect
1085 * port numbers that were used
1086 * @param upnum number to make unix domain socket names unique
1087 * @param hostname hostname of the controlling host, to allow control connections from
1088 * @param fdnum number used to offset the unix domain socket for grouped processes
1089 * (such as statistics or peerinfo, which can be shared among others)
1091 * @return new configuration, NULL on error
1093 static struct GNUNET_CONFIGURATION_Handle *
1094 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
1096 uint32_t * upnum, const char *hostname, uint32_t * fdnum)
1098 struct UpdateContext uc;
1101 char *allowed_hosts;
1107 uc.ret = GNUNET_CONFIGURATION_create ();
1108 uc.hostname = hostname;
1111 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1112 if (uc.nport >= HIGH_PORT)
1115 GNUNET_CONFIGURATION_destroy (uc.ret);
1119 if (GNUNET_CONFIGURATION_get_value_string
1120 (cfg, "testing", "control_host", &control_host) == GNUNET_OK)
1122 if (hostname != NULL)
1123 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1126 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1128 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1130 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH",
1132 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1133 "ACCEPT_FROM", allowed_hosts);
1134 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH",
1136 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1138 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH",
1140 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1141 "ACCEPT_FROM", allowed_hosts);
1142 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1144 GNUNET_free_non_null (control_host);
1145 GNUNET_free (allowed_hosts);
1149 /* arm needs to know to allow connections from the host on which it is running,
1150 * otherwise gnunet-arm is unable to connect to it in some instances */
1151 if (hostname != NULL)
1153 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1154 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1155 "BINDTO", hostname);
1156 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1157 "BINDTO", hostname);
1158 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1160 GNUNET_free (allowed_hosts);
1164 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1165 "BINDTO", "127.0.0.1");
1166 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1167 "BINDTO", "127.0.0.1");
1170 *port = (uint16_t) uc.nport;
1178 * Add entries to the some list
1180 * @param pg the peer group we are working with
1181 * @param first index of the first peer
1182 * @param second index of the second peer
1183 * @param list the peer list to use
1185 * @return the number of connections added (can be 0, 1 or 2)
1189 remove_connections (struct GNUNET_TESTING_PeerGroup *pg,
1190 unsigned int first, unsigned int second,
1191 enum PeerLists list)
1195 struct PeerConnection **first_list;
1196 struct PeerConnection **second_list;
1197 struct PeerConnection *first_iter;
1198 struct PeerConnection *second_iter;
1199 struct PeerConnection **first_tail;
1200 struct PeerConnection **second_tail;
1203 GNUNET_HashCode hash_first;
1204 GNUNET_HashCode hash_second;
1206 hash_from_uid (first, &hash_first);
1207 hash_from_uid (second, &hash_second);
1215 first_list = &pg->peers[first].allowed_peers_head;
1216 second_list = &pg->peers[second].allowed_peers_head;
1217 first_tail = &pg->peers[first].allowed_peers_tail;
1218 second_tail = &pg->peers[second].allowed_peers_tail;
1221 first_list = &pg->peers[first].connect_peers_head;
1222 second_list = &pg->peers[second].connect_peers_head;
1223 first_tail = &pg->peers[first].connect_peers_tail;
1224 second_tail = &pg->peers[second].connect_peers_tail;
1227 first_list = &pg->peers[first].blacklisted_peers_head;
1228 second_list = &pg->peers[second].blacklisted_peers_head;
1229 first_tail = &pg->peers[first].blacklisted_peers_tail;
1230 second_tail = &pg->peers[second].blacklisted_peers_tail;
1233 first_list = &pg->peers[first].connect_peers_working_set_head;
1234 second_list = &pg->peers[second].connect_peers_working_set_head;
1235 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1236 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1242 first_iter = *first_list;
1243 while (first_iter != NULL)
1245 if (first_iter->index == second)
1247 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1248 GNUNET_free(first_iter);
1252 first_iter = first_iter->next;
1255 second_iter = *second_list;
1256 while (second_iter != NULL)
1258 if (second_iter->index == first)
1260 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1261 GNUNET_free(second_iter);
1265 second_iter = second_iter->next;
1269 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1272 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1277 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1280 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1289 * Add entries to the some list
1291 * @param pg the peer group we are working with
1292 * @param first index of the first peer
1293 * @param second index of the second peer
1294 * @param list the list type that we should modify
1296 * @return the number of connections added (can be 0, 1 or 2)
1300 add_connections (struct GNUNET_TESTING_PeerGroup *pg,
1301 unsigned int first, unsigned int second,
1302 enum PeerLists list)
1308 struct PeerConnection **first_list;
1309 struct PeerConnection **second_list;
1310 struct PeerConnection *first_iter;
1311 struct PeerConnection *second_iter;
1312 struct PeerConnection *new_first;
1313 struct PeerConnection *new_second;
1314 struct PeerConnection **first_tail;
1315 struct PeerConnection **second_tail;
1317 GNUNET_HashCode hash_first;
1318 GNUNET_HashCode hash_second;
1320 hash_from_uid (first, &hash_first);
1321 hash_from_uid (second, &hash_second);
1328 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Adding to ALLOWED\n");
1329 first_list = &pg->peers[first].allowed_peers_head;
1330 second_list = &pg->peers[second].allowed_peers_head;
1331 first_tail = &pg->peers[first].allowed_peers_tail;
1332 second_tail = &pg->peers[second].allowed_peers_tail;
1335 first_list = &pg->peers[first].connect_peers_head;
1336 second_list = &pg->peers[second].connect_peers_head;
1337 first_tail = &pg->peers[first].connect_peers_tail;
1338 second_tail = &pg->peers[second].connect_peers_tail;
1341 first_list = &pg->peers[first].blacklisted_peers_head;
1342 second_list = &pg->peers[second].blacklisted_peers_head;
1343 first_tail = &pg->peers[first].blacklisted_peers_tail;
1344 second_tail = &pg->peers[second].blacklisted_peers_tail;
1347 first_list = &pg->peers[first].connect_peers_working_set_head;
1348 second_list = &pg->peers[second].connect_peers_working_set_head;
1349 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1350 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1356 add_first = GNUNET_YES;
1357 add_second = GNUNET_YES;
1359 first_iter = *first_list;
1360 while (first_iter != NULL)
1362 if (first_iter->index == second)
1364 add_first = GNUNET_NO;
1367 first_iter = first_iter->next;
1370 second_iter = *second_list;
1371 while (second_iter != NULL)
1373 if (second_iter->index == first)
1375 add_second = GNUNET_NO;
1378 second_iter = second_iter->next;
1382 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1385 add_first = GNUNET_YES;
1389 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1392 add_second = GNUNET_YES;
1400 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Adding peer %d to %d\n", second, first);
1401 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1402 new_first->index = second;
1403 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1405 new_first->next = *first_list;
1406 *first_list = new_first;*/
1408 GNUNET_assert (GNUNET_OK ==
1409 GNUNET_CONTAINER_multihashmap_put (pg->
1411 [first].blacklisted_peers,
1414 peers[second].daemon,
1415 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1417 pg->peers[first].num_connections++;
1424 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1425 new_second->index = first;
1426 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1428 new_second->next = *second_list;
1429 *second_list = new_second;
1432 GNUNET_assert (GNUNET_OK ==
1433 GNUNET_CONTAINER_multihashmap_put (pg->
1435 [second].blacklisted_peers,
1438 peers[first].daemon,
1439 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1441 pg->peers[second].num_connections++;
1450 * Scale free network construction as described in:
1452 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1454 * Start with a network of "one" peer, then progressively add
1455 * peers up to the total number. At each step, iterate over
1456 * all possible peers and connect new peer based on number of
1457 * existing connections of the target peer.
1459 * @param pg the peer group we are dealing with
1460 * @param proc the connection processor to use
1462 * @return the number of connections created
1465 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg,
1466 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1469 unsigned int total_connections;
1470 unsigned int outer_count;
1472 unsigned int previous_total_connections;
1476 GNUNET_assert (pg->total > 1);
1478 /* Add a connection between the first two nodes */
1479 total_connections = proc (pg, 0, 1, list);
1481 for (outer_count = 1; outer_count < pg->total; outer_count++)
1483 previous_total_connections = total_connections;
1484 for (i = 0; i < outer_count; i++)
1487 pg->peers[i].num_connections /
1488 (double) previous_total_connections;
1491 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1492 UINT64_MAX)) / ((double) UINT64_MAX);
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1495 "Considering connecting peer %d to peer %d\n",
1498 if (random < probability)
1501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1502 "Connecting peer %d to peer %d\n", outer_count, i);
1504 total_connections += proc (pg, outer_count, i, list);
1509 return total_connections;
1513 * Create a topology given a peer group (set of running peers)
1514 * and a connection processor. Creates a small world topology
1515 * according to the rewired ring construction. The basic
1516 * behavior is that a ring topology is created, but with some
1517 * probability instead of connecting a peer to the next
1518 * neighbor in the ring a connection will be created to a peer
1519 * selected uniformly at random. We use the TESTING
1520 * PERCENTAGE option to specify what number of
1521 * connections each peer should have. Default is 2,
1522 * which makes the ring, any given number is multiplied by
1523 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1524 * each peer have on average 2logn connections. The additional
1525 * connections are made at increasing distance around the ring
1526 * from the original peer, or to random peers based on the re-
1527 * wiring probability. The TESTING
1528 * PROBABILITY option is used as the probability that a given
1529 * connection is rewired.
1531 * @param pg the peergroup to create the topology on
1532 * @param proc the connection processor to call to actually set
1533 * up connections between two peers
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
1658 * @return the number of connections that were set up
1662 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg,
1663 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1665 unsigned int outer_count, inner_count;
1666 unsigned int cutoff;
1667 int connect_attempts;
1668 double nat_percentage;
1671 nat_percentage = 0.6; /* FIXME: default percentage? */
1672 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1677 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1678 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1680 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1681 p_string, "PERCENTAGE", "TESTING");
1682 GNUNET_free (p_string);
1687 cutoff = (unsigned int) (nat_percentage * pg->total);
1689 connect_attempts = 0;
1691 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1693 for (inner_count = outer_count + 1; inner_count < pg->total;
1696 if ((outer_count > cutoff) || (inner_count > cutoff))
1699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1700 "Connecting peer %d to peer %d\n",
1701 outer_count, inner_count);
1703 connect_attempts += proc (pg, outer_count, inner_count, list);
1708 return connect_attempts;
1713 * Create a topology given a peer group (set of running peers)
1714 * and a connection processor.
1716 * @param pg the peergroup to create the topology on
1717 * @param proc the connection processor to call to actually set
1718 * up connections between two peers
1720 * @return the number of connections that were set up
1724 create_small_world (struct GNUNET_TESTING_PeerGroup *pg,
1725 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1727 unsigned int i, j, k;
1728 unsigned int square;
1731 unsigned int toggle = 1;
1732 unsigned int nodeToConnect;
1733 unsigned int natLog;
1734 unsigned int node1Row;
1735 unsigned int node1Col;
1736 unsigned int node2Row;
1737 unsigned int node2Col;
1738 unsigned int distance;
1739 double probability, random, percentage;
1740 unsigned int smallWorldConnections;
1741 unsigned int small_world_it;
1743 int connect_attempts;
1744 square = floor (sqrt (pg->total));
1748 percentage = 0.5; /* FIXME: default percentage? */
1749 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1754 if (sscanf (p_string, "%lf", &percentage) != 1)
1755 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1757 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1758 p_string, "PERCENTAGE", "TESTING");
1759 GNUNET_free (p_string);
1761 if (percentage < 0.0)
1763 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1765 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1766 "PERCENTAGE", "TESTING", percentage);
1769 probability = 0.5; /* FIXME: default percentage? */
1770 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1775 if (sscanf (p_string, "%lf", &probability) != 1)
1776 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1778 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1779 p_string, "PROBABILITY", "TESTING");
1780 GNUNET_free (p_string);
1782 if (square * square != pg->total)
1784 while (rows * cols < pg->total)
1786 if (toggle % 2 == 0)
1795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1797 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1801 connect_attempts = 0;
1802 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1803 * to the node to its right and above. Once this is over, we'll have our torus!
1804 * Special case for the last node (if the rows and columns are not equal), connect
1805 * to the first in the row to maintain topology.
1807 for (i = 0; i < pg->total; i++)
1809 /* First connect to the node to the right */
1810 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1811 nodeToConnect = i + 1;
1812 else if (i + 1 == pg->total)
1813 nodeToConnect = rows * cols - cols;
1815 nodeToConnect = i - cols + 1;
1817 connect_attempts += proc (pg, i, nodeToConnect, list);
1820 nodeToConnect = (rows * cols) - cols + i;
1822 nodeToConnect = i - cols;
1824 if (nodeToConnect < pg->total)
1825 connect_attempts += proc (pg, i, nodeToConnect, list);
1827 natLog = log (pg->total);
1828 #if VERBOSE_TESTING > 2
1829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1830 _("natural log of %d is %d, will run %d iterations\n"),
1831 pg->total, natLog, (int) (natLog * percentage));
1832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1833 _("Total connections added thus far: %u!\n"), connect_attempts);
1835 smallWorldConnections = 0;
1836 small_world_it = (unsigned int) (natLog * percentage);
1837 if (small_world_it < 1)
1839 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
1840 for (i = 0; i < small_world_it; i++)
1842 for (j = 0; j < pg->total; j++)
1844 /* Determine the row and column of node at position j on the 2d torus */
1845 node1Row = j / cols;
1846 node1Col = j - (node1Row * cols);
1847 for (k = 0; k < pg->total; k++)
1849 /* Determine the row and column of node at position k on the 2d torus */
1850 node2Row = k / cols;
1851 node2Col = k - (node2Row * cols);
1852 /* Simple Cartesian distance */
1854 abs (node1Row - node2Row) + abs (node1Col - node2Col);
1857 /* Calculate probability as 1 over the square of the distance */
1858 probability = 1.0 / (distance * distance);
1859 /* Choose a random value between 0 and 1 */
1862 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1864 ((double) UINT64_MAX);
1865 /* If random < probability, then connect the two nodes */
1866 if (random < probability)
1867 smallWorldConnections += proc (pg, j, k, list);
1873 connect_attempts += smallWorldConnections;
1874 #if VERBOSE_TESTING > 2
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876 _("Total connections added for small world: %d!\n"),
1877 smallWorldConnections);
1879 return connect_attempts;
1883 * Create a topology given a peer group (set of running peers)
1884 * and a connection processor.
1886 * @param pg the peergroup to create the topology on
1887 * @param proc the connection processor to call to actually set
1888 * up connections between two peers
1890 * @return the number of connections that were set up
1894 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg,
1895 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1898 unsigned int outer_count;
1899 unsigned int inner_count;
1900 int connect_attempts;
1904 probability = 0.5; /* FIXME: default percentage? */
1905 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1910 if (sscanf (p_string, "%lf", &probability) != 1)
1911 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1913 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1914 p_string, "PROBABILITY", "TESTING");
1915 GNUNET_free (p_string);
1917 connect_attempts = 0;
1918 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1920 for (inner_count = outer_count + 1; inner_count < pg->total;
1925 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1926 UINT64_MAX)) / ((double) UINT64_MAX);
1928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1929 _("rand is %f probability is %f\n"), temp_rand,
1932 if (temp_rand < probability)
1934 connect_attempts += proc (pg, outer_count, inner_count, list);
1939 return connect_attempts;
1943 * Create a topology given a peer group (set of running peers)
1944 * and a connection processor. This particular function creates
1945 * the connections for a 2d-torus, plus additional "closest"
1946 * connections per peer.
1948 * @param pg the peergroup to create the topology on
1949 * @param proc the connection processor to call to actually set
1950 * up connections between two peers
1952 * @return the number of connections that were set up
1956 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg,
1957 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1960 unsigned int square;
1963 unsigned int toggle = 1;
1964 unsigned int nodeToConnect;
1965 int connect_attempts;
1967 connect_attempts = 0;
1969 square = floor (sqrt (pg->total));
1973 if (square * square != pg->total)
1975 while (rows * cols < pg->total)
1977 if (toggle % 2 == 0)
1986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1988 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1991 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1992 * to the node to its right and above. Once this is over, we'll have our torus!
1993 * Special case for the last node (if the rows and columns are not equal), connect
1994 * to the first in the row to maintain topology.
1996 for (i = 0; i < pg->total; i++)
1998 /* First connect to the node to the right */
1999 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2000 nodeToConnect = i + 1;
2001 else if (i + 1 == pg->total)
2002 nodeToConnect = rows * cols - cols;
2004 nodeToConnect = i - cols + 1;
2006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2007 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2009 connect_attempts += proc (pg, i, nodeToConnect, list);
2011 /* Second connect to the node immediately above */
2013 nodeToConnect = (rows * cols) - cols + i;
2015 nodeToConnect = i - cols;
2017 if (nodeToConnect < pg->total)
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2021 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2023 connect_attempts += proc (pg, i, nodeToConnect, list);
2028 return connect_attempts;
2033 * Create a topology given a peer group (set of running peers)
2034 * and a connection processor.
2036 * @param pg the peergroup to create the topology on
2037 * @param proc the connection processor to call to actually set
2038 * up connections between two peers
2040 * @return the number of connections that were set up
2044 create_clique (struct GNUNET_TESTING_PeerGroup *pg,
2045 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2047 unsigned int outer_count;
2048 unsigned int inner_count;
2049 int connect_attempts;
2051 connect_attempts = 0;
2053 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2055 for (inner_count = outer_count + 1; inner_count < pg->total;
2059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2060 "Connecting peer %d to peer %d\n",
2061 outer_count, inner_count);
2063 connect_attempts += proc (pg, outer_count, inner_count, list);
2067 return connect_attempts;
2072 * Iterator over hash map entries.
2074 * @param cls closure the peer group
2075 * @param key the key stored in the hashmap is the
2076 * index of the peer to connect to
2077 * @param value value in the hash map, handle to the peer daemon
2078 * @return GNUNET_YES if we should continue to
2083 unblacklist_iterator (void *cls,
2084 const GNUNET_HashCode * key,
2087 struct UnblacklistContext *un_ctx = cls;
2088 uint32_t second_pos;
2090 uid_from_hash (key, &second_pos);
2092 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2099 * Create a blacklist topology based on the allowed topology
2100 * which disallows any connections not in the allowed topology
2101 * at the transport level.
2103 * @param pg the peergroup to create the topology on
2104 * @param proc the connection processor to call to allow
2105 * up connections between two peers
2107 * @return the number of connections that were set up
2111 copy_allowed (struct GNUNET_TESTING_PeerGroup *pg,
2112 GNUNET_TESTING_ConnectionProcessor proc)
2114 struct UnblacklistContext un_ctx;
2117 struct PeerConnection *iter;
2121 for (count = 0; count < pg->total - 1; count++)
2123 un_ctx.first_uid = count;
2125 iter = pg->peers[count].allowed_peers_head;
2126 while (iter != NULL)
2128 remove_connections(pg, count, iter->index, BLACKLIST);
2129 //unblacklist_connections(pg, count, iter->index);
2133 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers, &unblacklist_iterator, &un_ctx);
2136 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2141 * Create a topology given a peer group (set of running peers)
2142 * and a connection processor.
2144 * @param pg the peergroup to create the topology on
2145 * @param proc the connection processor to call to actually set
2146 * up connections between two peers
2147 * @param list which list should be modified
2149 * @return the number of connections that were set up
2153 create_line (struct GNUNET_TESTING_PeerGroup *pg,
2154 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2157 int connect_attempts;
2159 connect_attempts = 0;
2161 /* Connect each peer to the next highest numbered peer */
2162 for (count = 0; count < pg->total - 1; count++)
2165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2166 "Connecting peer %d to peer %d\n", count, count + 1);
2168 connect_attempts += proc (pg, count, count + 1, list);
2171 return connect_attempts;
2175 * Create a topology given a peer group (set of running peers)
2176 * and a connection processor.
2178 * @param pg the peergroup to create the topology on
2179 * @param filename the file to read topology information from
2180 * @param proc the connection processor to call to actually set
2181 * up connections between two peers
2183 * @return the number of connections that were set up
2187 create_from_file (struct GNUNET_TESTING_PeerGroup *pg,
2189 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2191 int connect_attempts;
2192 unsigned int first_peer_index;
2193 unsigned int second_peer_index;
2194 connect_attempts = 0;
2199 unsigned int total_peers;
2201 enum States curr_state;
2203 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2204 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2206 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2209 "Could not open file `%s' specified for topology!", filename);
2210 return connect_attempts;
2213 data = GNUNET_malloc_large (frstat.st_size);
2214 GNUNET_assert(data != NULL);
2215 if (frstat.st_size !=
2216 GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2218 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2219 "Could not read file %s specified for host list, ending test!", filename);
2221 return connect_attempts;
2226 /* First line should contain a single integer, specifying the number of peers */
2227 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2228 curr_state = NUM_PEERS;
2229 while (count < frstat.st_size - 1)
2231 if ((buf[count] == '\n') || (buf[count] == ' '))
2240 if (1 != sscanf(&buf[count], "%u", &total_peers))
2242 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read number of peers from topology file!\n");
2243 GNUNET_free_non_null(data);
2244 return connect_attempts;
2246 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read %u total peers in topology\n", total_peers);
2247 GNUNET_assert(total_peers == pg->total);
2248 curr_state = PEER_INDEX;
2249 while((buf[count] != '\n') && (count < frstat.st_size - 1))
2254 if (1 != sscanf(&buf[count], "%u", &first_peer_index))
2256 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read peer index from topology file!\n");
2257 GNUNET_free_non_null(data);
2258 return connect_attempts;
2260 while((buf[count] != ':') && (count < frstat.st_size - 1))
2263 curr_state = OTHER_PEER_INDEX;
2266 if (1 == sscanf(&buf[count], ":"))
2267 curr_state = OTHER_PEER_INDEX;
2270 case OTHER_PEER_INDEX:
2271 if (1 != sscanf(&buf[count], "%u", &second_peer_index))
2273 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to peer index from topology file!\n");
2274 GNUNET_free_non_null(data);
2275 return connect_attempts;
2277 /* Assume file is written with first peer 1, but array index is 0 */
2278 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index - 1, list);
2279 while((buf[count] != '\n') && (buf[count] != ',') && (count < frstat.st_size - 1))
2281 if (buf[count] == '\n')
2283 curr_state = PEER_INDEX;
2285 else if (buf[count] != ',')
2287 curr_state = OTHER_PEER_INDEX;
2292 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Found bad data in topology file while in state %d!\n", curr_state);
2294 return connect_attempts;
2299 /* Connect each peer to the next highest numbered peer */
2300 for (count = 0; count < pg->total - 1; count++)
2303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2304 "Connecting peer %d to peer %d\n", first_peer_index, second_peer_index);
2306 connect_attempts += proc (pg, first_peer_index, second_peer_index);
2309 return connect_attempts;
2313 * Create a topology given a peer group (set of running peers)
2314 * and a connection processor.
2316 * @param pg the peergroup to create the topology on
2317 * @param proc the connection processor to call to actually set
2318 * up connections between two peers
2320 * @return the number of connections that were set up
2324 create_ring (struct GNUNET_TESTING_PeerGroup *pg,
2325 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2328 int connect_attempts;
2330 connect_attempts = 0;
2332 /* Connect each peer to the next highest numbered peer */
2333 for (count = 0; count < pg->total - 1; count++)
2336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2337 "Connecting peer %d to peer %d\n", count, count + 1);
2339 connect_attempts += proc (pg, count, count + 1, list);
2342 /* Connect the last peer to the first peer */
2343 connect_attempts += proc (pg, pg->total - 1, 0, list);
2345 return connect_attempts;
2350 * Iterator for writing friends of a peer to a file.
2352 * @param cls closure, an open writable file handle
2353 * @param key the key the daemon was stored under
2354 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2356 * @return GNUNET_YES to continue iteration
2358 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2359 * with a single file_iterator that takes a closure which contains
2360 * the prefix to write before the peer. Then this could be used
2361 * for blacklisting multiple transports and writing the friend
2362 * file. I'm sure *someone* will complain loudly about other
2363 * things that negate these functions even existing so no point in
2367 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2369 FILE *temp_friend_handle = cls;
2370 struct GNUNET_TESTING_Daemon *peer = value;
2371 struct GNUNET_PeerIdentity *temppeer;
2372 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2374 temppeer = &peer->id;
2375 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2376 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2381 struct BlacklistContext
2384 * The (open) file handle to write to
2386 FILE *temp_file_handle;
2389 * The transport that this peer will be blacklisted on.
2395 * Iterator for writing blacklist data to appropriate files.
2397 * @param cls closure, an open writable file handle
2398 * @param key the key the daemon was stored under
2399 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2401 * @return GNUNET_YES to continue iteration
2404 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2406 struct BlacklistContext *blacklist_ctx = cls;
2407 struct GNUNET_TESTING_Daemon *peer = value;
2408 struct GNUNET_PeerIdentity *temppeer;
2409 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2411 temppeer = &peer->id;
2412 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2413 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2414 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2415 blacklist_ctx->transport, (char *) &peer_enc);
2423 * Create the friend files based on the PeerConnection's
2424 * of each peer in the peer group, and copy the files
2425 * to the appropriate place
2427 * @param pg the peer group we are dealing with
2430 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
2432 FILE *temp_friend_handle;
2433 unsigned int pg_iter;
2434 char *temp_service_path;
2435 struct GNUNET_OS_Process **procarr;
2438 enum GNUNET_OS_ProcessStatusType type;
2439 unsigned long return_code;
2444 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2445 struct PeerConnection *conn_iter;
2447 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2448 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2450 mytemp = GNUNET_DISK_mktemp ("friends");
2451 GNUNET_assert (mytemp != NULL);
2452 temp_friend_handle = fopen (mytemp, "wt");
2453 GNUNET_assert (temp_friend_handle != NULL);
2455 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2456 while (conn_iter != NULL)
2458 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
2459 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2460 conn_iter = conn_iter->next;
2463 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2464 &friend_file_iterator,
2465 temp_friend_handle);
2467 fclose (temp_friend_handle);
2470 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2471 daemon->cfg, "PATHS",
2473 &temp_service_path))
2475 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2477 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2478 "SERVICEHOME", "PATHS");
2479 if (UNLINK (mytemp) != 0)
2480 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2482 GNUNET_free (mytemp);
2486 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2488 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2489 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2494 _("Copying file with command cp %s %s\n"), mytemp, arg);
2499 else /* Remote, scp the file to the correct place */
2501 if (NULL != pg->peers[pg_iter].daemon->username)
2502 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2503 pg->peers[pg_iter].daemon->username,
2504 pg->peers[pg_iter].daemon->hostname,
2507 GNUNET_asprintf (&arg, "%s:%s/friends",
2508 pg->peers[pg_iter].daemon->hostname,
2511 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2516 _("Copying file with command scp %s %s\n"), mytemp,
2521 GNUNET_free (temp_service_path);
2522 GNUNET_free (mytemp);
2526 ret = GNUNET_SYSERR;
2527 while ((count < max_wait) && (ret != GNUNET_OK))
2530 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2534 _("Checking copy status of file %d\n"), pg_iter);
2536 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2538 if (GNUNET_OS_process_status
2539 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2541 ret = GNUNET_SYSERR;
2543 else if ((type != GNUNET_OS_PROCESS_EXITED)
2544 || (return_code != 0))
2546 ret = GNUNET_SYSERR;
2550 GNUNET_OS_process_close (procarr[pg_iter]);
2551 procarr[pg_iter] = NULL;
2553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2554 _("File %d copied\n"), pg_iter);
2560 if (ret == GNUNET_SYSERR)
2562 /* FIXME: why sleep here? -CG */
2568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2569 _("Finished copying all friend files!\n"));
2571 GNUNET_free (procarr);
2577 * Create the blacklist files based on the PeerConnection's
2578 * of each peer in the peer group, and copy the files
2579 * to the appropriate place.
2581 * @param pg the peer group we are dealing with
2582 * @param transports space delimited list of transports to blacklist
2585 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg,
2586 const char *transports)
2588 FILE *temp_file_handle;
2589 unsigned int pg_iter;
2590 char *temp_service_path;
2591 struct GNUNET_OS_Process **procarr;
2594 enum GNUNET_OS_ProcessStatusType type;
2595 unsigned long return_code;
2602 char *temp_transports;
2605 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2606 struct PeerConnection *conn_iter;
2608 static struct BlacklistContext blacklist_ctx;
2611 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2612 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2614 mytemp = GNUNET_DISK_mktemp ("blacklist");
2615 GNUNET_assert (mytemp != NULL);
2616 temp_file_handle = fopen (mytemp, "wt");
2617 GNUNET_assert (temp_file_handle != NULL);
2618 temp_transports = GNUNET_strdup (transports);
2620 blacklist_ctx.temp_file_handle = temp_file_handle;
2622 transport_len = strlen (temp_transports) + 1;
2625 for (i = 0; i < transport_len; i++)
2627 if ((temp_transports[i] == ' ') && (pos == NULL))
2628 continue; /* At start of string (whitespace) */
2629 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2631 temp_transports[i] = '\0';
2633 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
2634 while (conn_iter != NULL)
2636 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
2637 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
2638 conn_iter = conn_iter->next;
2642 blacklist_ctx.transport = pos;
2643 entry_count = GNUNET_CONTAINER_multihashmap_iterate (pg->
2645 [pg_iter].blacklisted_peers,
2646 &blacklist_file_iterator,
2650 } /* At beginning of actual string */
2651 else if (pos == NULL)
2653 pos = &temp_transports[i];
2657 GNUNET_free (temp_transports);
2658 fclose (temp_file_handle);
2661 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2662 daemon->cfg, "PATHS",
2664 &temp_service_path))
2666 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2668 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2669 "SERVICEHOME", "PATHS");
2670 if (UNLINK (mytemp) != 0)
2671 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2673 GNUNET_free (mytemp);
2677 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2679 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2680 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2685 _("Copying file with command cp %s %s\n"), mytemp, arg);
2690 else /* Remote, scp the file to the correct place */
2692 if (NULL != pg->peers[pg_iter].daemon->username)
2693 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
2694 pg->peers[pg_iter].daemon->username,
2695 pg->peers[pg_iter].daemon->hostname,
2698 GNUNET_asprintf (&arg, "%s:%s/blacklist",
2699 pg->peers[pg_iter].daemon->hostname,
2702 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2707 _("Copying file with command scp %s %s\n"), mytemp,
2712 GNUNET_free (temp_service_path);
2713 GNUNET_free (mytemp);
2717 ret = GNUNET_SYSERR;
2718 while ((count < max_wait) && (ret != GNUNET_OK))
2721 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2725 _("Checking copy status of file %d\n"), pg_iter);
2727 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2729 if (GNUNET_OS_process_status
2730 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2732 ret = GNUNET_SYSERR;
2734 else if ((type != GNUNET_OS_PROCESS_EXITED)
2735 || (return_code != 0))
2737 ret = GNUNET_SYSERR;
2741 GNUNET_OS_process_close (procarr[pg_iter]);
2742 procarr[pg_iter] = NULL;
2744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2745 _("File %d copied\n"), pg_iter);
2751 if (ret == GNUNET_SYSERR)
2753 /* FIXME: why sleep here? -CG */
2759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2760 _("Finished copying all blacklist files!\n"));
2762 GNUNET_free (procarr);
2768 * Internal notification of a connection, kept so that we can ensure some connections
2769 * happen instead of flooding all testing daemons with requests to connect.
2772 internal_connect_notify (void *cls,
2773 const struct GNUNET_PeerIdentity *first,
2774 const struct GNUNET_PeerIdentity *second,
2776 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2777 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2778 struct GNUNET_TESTING_Daemon *first_daemon,
2779 struct GNUNET_TESTING_Daemon *second_daemon,
2782 struct ConnectTopologyContext *ct_ctx = cls;
2783 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
2784 outstanding_connects--;
2785 ct_ctx->remaining_connections--;
2786 if (ct_ctx->remaining_connections == 0)
2788 if (ct_ctx->notify_connections_done != NULL)
2789 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
2790 GNUNET_free (ct_ctx);
2793 if (pg->notify_connection != NULL)
2794 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
2795 first_cfg, second_cfg, first_daemon, second_daemon,
2801 * Either delay a connection (because there are too many outstanding)
2802 * or schedule it for right now.
2804 * @param cls a connection context
2805 * @param tc the task runtime context
2808 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2810 struct ConnectContext *connect_context = cls;
2812 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2815 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2817 #if VERBOSE_TESTING > 2
2818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2820 ("Delaying connect, we have too many outstanding connections!\n"));
2822 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2823 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
2824 &schedule_connect, connect_context);
2828 #if VERBOSE_TESTING > 2
2829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2830 _("Creating connection, outstanding_connections is %d\n"),
2831 outstanding_connects);
2833 outstanding_connects++;
2834 GNUNET_TESTING_daemons_connect (connect_context->first,
2835 connect_context->second,
2838 &internal_connect_notify,
2839 connect_context->ct_ctx);
2840 GNUNET_free (connect_context);
2846 * Iterator for actually scheduling connections to be created
2847 * between two peers.
2849 * @param cls closure, a GNUNET_TESTING_Daemon
2850 * @param key the key the second Daemon was stored under
2851 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2853 * @return GNUNET_YES to continue iteration
2856 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2858 struct ConnectTopologyContext *ct_ctx = cls;
2859 struct PeerData *first = ct_ctx->first;
2860 struct GNUNET_TESTING_Daemon *second = value;
2861 struct ConnectContext *connect_context;
2863 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
2864 connect_context->first = first->daemon;
2865 connect_context->second = second;
2866 connect_context->ct_ctx = ct_ctx;
2867 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
2875 * Iterator for copying all entries in the allowed hashmap to the
2878 * @param cls closure, a GNUNET_TESTING_Daemon
2879 * @param key the key the second Daemon was stored under
2880 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2882 * @return GNUNET_YES to continue iteration
2885 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2887 struct PeerData *first = cls;
2889 GNUNET_assert (GNUNET_OK ==
2890 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
2892 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2899 * Make the peers to connect the same as those that are allowed to be
2902 * @param pg the peer group
2905 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2907 unsigned int pg_iter;
2911 struct PeerConnection *iter;
2915 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2918 iter = pg->peers[pg_iter].allowed_peers_head;
2919 while (iter != NULL)
2921 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating connection between %d and %d\n", pg_iter, iter->index);
2922 total += add_connections(pg, pg_iter, iter->index, CONNECT);
2923 //total += add_actual_connections(pg, pg_iter, iter->index);
2928 GNUNET_CONTAINER_multihashmap_iterate (pg->
2929 peers[pg_iter].allowed_peers,
2930 ©_topology_iterator,
2931 &pg->peers[pg_iter]);
2933 if (GNUNET_SYSERR == ret)
2934 return GNUNET_SYSERR;
2936 total = total + ret;
2944 * Connect the topology as specified by the PeerConnection's
2945 * of each peer in the peer group
2947 * @param pg the peer group we are dealing with
2948 * @param notify_callback callback to notify when finished
2949 * @param notify_cls closure for notify callback
2951 * @return the number of connections that will be attempted
2954 connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2955 GNUNET_TESTING_NotifyCompletion notify_callback,
2958 unsigned int pg_iter;
2960 struct ConnectTopologyContext *ct_ctx;
2962 struct PeerConnection *connection_iter;
2963 struct ConnectContext *connect_context;
2969 ct_ctx = GNUNET_malloc (sizeof (struct ConnectTopologyContext));
2970 ct_ctx->notify_connections_done = notify_callback;
2971 ct_ctx->notify_cls = notify_cls;
2974 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2977 connection_iter = pg->peers[pg_iter].connect_peers_head;
2978 while (connection_iter != NULL)
2980 connection_iter = connection_iter->next;
2985 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
2991 GNUNET_free (ct_ctx);
2994 ct_ctx->remaining_connections = total;
2997 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3000 connection_iter = pg->peers[pg_iter].connect_peers_head;
3001 while (connection_iter != NULL)
3003 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling connect of peer %d to peer %d\n", pg_iter, connection_iter->index);
3004 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3005 connect_context->first = pg->peers[pg_iter].daemon;
3006 connect_context->second = pg->peers[connection_iter->index].daemon;
3007 connect_context->ct_ctx = ct_ctx;
3008 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3009 connection_iter = connection_iter->next;
3014 GNUNET_CONTAINER_multihashmap_iterate (pg->
3015 peers[pg_iter].connect_peers,
3016 &connect_iterator, ct_ctx);
3017 GNUNET_assert (GNUNET_SYSERR != ret && ret >= 0);
3018 total = total + ret;
3026 * Takes a peer group and creates a topology based on the
3027 * one specified. Creates a topology means generates friend
3028 * files for the peers so they can only connect to those allowed
3029 * by the topology. This will only have an effect once peers
3030 * are started if the FRIENDS_ONLY option is set in the base
3031 * config. Also takes an optional restrict topology which
3032 * disallows connections based on particular transports
3033 * UNLESS they are specified in the restricted topology.
3035 * @param pg the peer group struct representing the running peers
3036 * @param topology which topology to connect the peers in
3037 * @param restrict_topology disallow restrict_transports transport
3038 * connections to peers NOT in this topology
3039 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3040 * @param restrict_transports space delimited list of transports to blacklist
3041 * to create restricted topology
3043 * @return the maximum number of connections were all allowed peers
3044 * connected to each other
3047 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
3048 enum GNUNET_TESTING_Topology topology,
3049 enum GNUNET_TESTING_Topology
3051 const char *restrict_transports)
3055 unsigned int num_connections;
3056 int unblacklisted_connections;
3061 for (i = 0; i < pg->total; i++)
3063 pg->peers[i].allowed_peers =
3064 GNUNET_CONTAINER_multihashmap_create (100);
3065 pg->peers[i].connect_peers =
3066 GNUNET_CONTAINER_multihashmap_create (100);
3067 pg->peers[i].blacklisted_peers =
3068 GNUNET_CONTAINER_multihashmap_create (100);
3069 pg->peers[i].pg = pg;
3075 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3079 num_connections = create_clique (pg, &add_connections, ALLOWED);
3081 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3084 _("Creating small world (ring) topology\n"));
3087 create_small_world_ring (pg, &add_connections, ALLOWED);
3089 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3092 _("Creating small world (2d-torus) topology\n"));
3094 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3096 case GNUNET_TESTING_TOPOLOGY_RING:
3098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3100 num_connections = create_ring (pg, &add_connections, ALLOWED);
3102 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3106 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3108 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3111 _("Creating Erdos-Renyi topology\n"));
3113 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3115 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3119 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3121 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3124 _("Creating Scale Free topology\n"));
3126 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3128 case GNUNET_TESTING_TOPOLOGY_LINE:
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3131 _("Creating straight line topology\n"));
3133 num_connections = create_line (pg, &add_connections, ALLOWED);
3135 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3138 _("Creating topology from file!\n"));
3141 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing", "topology_file",
3143 num_connections = create_from_file (pg, filename, &add_connections, ALLOWED);
3146 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!");
3147 num_connections = 0;
3150 case GNUNET_TESTING_TOPOLOGY_NONE:
3152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3154 ("Creating no allowed topology (all peers can connect at core level)\n"));
3156 num_connections = 0;
3159 num_connections = 0;
3164 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
3166 ret = create_and_copy_friend_files (pg);
3167 if (ret != GNUNET_OK)
3170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3171 _("Failed during friend file copying!\n"));
3173 return GNUNET_SYSERR;
3178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3179 _("Friend files created/copied successfully!\n"));
3184 /* Use the create clique method to initially set all connections as blacklisted. */
3185 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
3186 create_clique (pg, &add_connections, BLACKLIST);
3188 unblacklisted_connections = 0;
3189 /* Un-blacklist connections as per the topology specified */
3190 switch (restrict_topology)
3192 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3195 _("Blacklisting all but clique topology\n"));
3197 unblacklisted_connections =
3198 create_clique (pg, &remove_connections, BLACKLIST);
3200 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3203 _("Blacklisting all but small world (ring) topology\n"));
3205 unblacklisted_connections =
3206 create_small_world_ring (pg, &remove_connections, BLACKLIST);
3208 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3212 ("Blacklisting all but small world (2d-torus) topology\n"));
3214 unblacklisted_connections =
3215 create_small_world (pg, &remove_connections, BLACKLIST);
3217 case GNUNET_TESTING_TOPOLOGY_RING:
3219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3220 _("Blacklisting all but ring topology\n"));
3222 unblacklisted_connections = create_ring (pg, &remove_connections, BLACKLIST);
3224 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3227 _("Blacklisting all but 2d torus topology\n"));
3229 unblacklisted_connections =
3230 create_2d_torus (pg, &remove_connections, BLACKLIST);
3232 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3235 _("Blacklisting all but Erdos-Renyi topology\n"));
3237 unblacklisted_connections =
3238 create_erdos_renyi (pg, &remove_connections, BLACKLIST);
3240 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3243 _("Blacklisting all but InterNAT topology\n"));
3245 unblacklisted_connections =
3246 create_nated_internet (pg, &remove_connections, BLACKLIST);
3248 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3251 _("Blacklisting all but Scale Free topology\n"));
3253 unblacklisted_connections =
3254 create_scale_free (pg, &remove_connections, BLACKLIST);
3256 case GNUNET_TESTING_TOPOLOGY_LINE:
3258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3259 _("Blacklisting all but straight line topology\n"));
3261 unblacklisted_connections = create_line (pg, &remove_connections, BLACKLIST);
3263 case GNUNET_TESTING_TOPOLOGY_NONE: /* Fall through */
3264 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3268 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
3270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3272 ("Creating blacklist topology from allowed\n"));
3273 unblacklisted_connections = copy_allowed (pg, &remove_connections);
3278 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
3280 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n", restrict_transports);
3281 ret = create_and_copy_blacklist_files (pg, restrict_transports);
3282 if (ret != GNUNET_OK)
3285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3286 _("Failed during blacklist file copying!\n"));
3293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3294 _("Blacklist files created/copied successfully!\n"));
3298 return num_connections;
3304 * Iterator for choosing random peers to connect.
3306 * @param cls closure, a RandomContext
3307 * @param key the key the second Daemon was stored under
3308 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3310 * @return GNUNET_YES to continue iteration
3313 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3315 struct RandomContext *random_ctx = cls;
3316 double random_number;
3317 uint32_t second_pos;
3318 GNUNET_HashCode first_hash;
3321 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3322 UINT64_MAX)) / ((double) UINT64_MAX);
3323 if (random_number < random_ctx->percentage)
3325 GNUNET_assert (GNUNET_OK ==
3326 GNUNET_CONTAINER_multihashmap_put (random_ctx->
3327 first->connect_peers_working_set,
3329 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3332 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
3333 uid_from_hash (key, &second_pos);
3334 hash_from_uid (random_ctx->first_uid, &first_hash);
3335 GNUNET_assert (random_ctx->pg->total > second_pos);
3336 GNUNET_assert (GNUNET_YES ==
3337 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
3339 [second_pos].connect_peers,
3349 * Iterator for adding at least X peers to a peers connection set.
3351 * @param cls closure, MinimumContext
3352 * @param key the key the second Daemon was stored under
3353 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3355 * @return GNUNET_YES to continue iteration
3358 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3360 struct MinimumContext *min_ctx = cls;
3361 uint32_t second_pos;
3362 GNUNET_HashCode first_hash;
3365 if (GNUNET_CONTAINER_multihashmap_size
3366 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
3368 for (i = 0; i < min_ctx->num_to_add; i++)
3370 if (min_ctx->pg_array[i] == min_ctx->current)
3372 GNUNET_assert (GNUNET_OK ==
3373 GNUNET_CONTAINER_multihashmap_put
3374 (min_ctx->first->connect_peers_working_set, key,
3376 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3377 uid_from_hash (key, &second_pos);
3378 hash_from_uid (min_ctx->first_uid, &first_hash);
3379 GNUNET_assert (min_ctx->pg->total > second_pos);
3380 GNUNET_assert (GNUNET_OK ==
3381 GNUNET_CONTAINER_multihashmap_put (min_ctx->
3383 [second_pos].connect_peers_working_set,
3387 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3388 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
3389 GNUNET_assert (GNUNET_YES ==
3390 GNUNET_CONTAINER_multihashmap_remove
3391 (min_ctx->pg->peers[second_pos].connect_peers,
3392 &first_hash, min_ctx->first->daemon));
3399 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
3405 * Iterator for adding peers to a connection set based on a depth first search.
3407 * @param cls closure, MinimumContext
3408 * @param key the key the second daemon was stored under
3409 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3411 * @return GNUNET_YES to continue iteration
3414 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3416 struct DFSContext *dfs_ctx = cls;
3417 GNUNET_HashCode first_hash;
3419 if (dfs_ctx->current == dfs_ctx->chosen)
3421 GNUNET_assert (GNUNET_OK ==
3422 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3423 first->connect_peers_working_set,
3425 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3426 uid_from_hash (key, &dfs_ctx->second_uid);
3427 hash_from_uid (dfs_ctx->first_uid, &first_hash);
3428 GNUNET_assert (GNUNET_OK ==
3429 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3431 [dfs_ctx->second_uid].connect_peers_working_set,
3435 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3436 GNUNET_assert (GNUNET_YES ==
3437 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
3439 [dfs_ctx->second_uid].connect_peers,
3443 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
3444 return GNUNET_NO; /* We have found our peer, don't iterate more */
3453 * From the set of connections possible, choose percentage percent of connections
3454 * to actually connect.
3456 * @param pg the peergroup we are dealing with
3457 * @param percentage what percent of total connections to make
3460 choose_random_connections (struct GNUNET_TESTING_PeerGroup *pg,
3463 struct RandomContext random_ctx;
3466 struct PeerConnection *temp_peers;
3467 struct PeerConnection *conn_iter;
3468 double random_number;
3471 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3473 random_ctx.first_uid = pg_iter;
3474 random_ctx.first = &pg->peers[pg_iter];
3475 random_ctx.percentage = percentage;
3479 conn_iter = pg->peers[pg_iter].connect_peers_head;
3480 while (conn_iter != NULL)
3484 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3485 UINT64_MAX)) / ((double) UINT64_MAX);
3486 if (random_number < percentage)
3488 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
3490 conn_iter = conn_iter->next;
3493 pg->peers[pg_iter].connect_peers_working_set =
3494 GNUNET_CONTAINER_multihashmap_create (pg->total);
3495 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3496 &random_connect_iterator,
3498 /* Now remove the old connections */
3499 GNUNET_CONTAINER_multihashmap_destroy (pg->
3500 peers[pg_iter].connect_peers);
3501 /* And replace with the random set */
3502 pg->peers[pg_iter].connect_peers =
3503 pg->peers[pg_iter].connect_peers_working_set;
3507 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3509 conn_iter = pg->peers[pg_iter].connect_peers_head;
3510 while (pg->peers[pg_iter].connect_peers_head != NULL)
3511 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
3513 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
3514 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
3515 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
3516 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
3522 * Count the number of connections in a linked list of connections.
3524 * @param conn_list the connection list to get the count of
3526 * @return the number of elements in the list
3529 count_connections (struct PeerConnection *conn_list)
3531 struct PeerConnection *iter;
3535 while (iter != NULL)
3545 count_workingset_connections (struct GNUNET_TESTING_PeerGroup *pg)
3548 unsigned int pg_iter;
3550 struct PeerConnection *conn_iter;
3554 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3557 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
3558 while (conn_iter != NULL)
3561 conn_iter = conn_iter->next;
3565 GNUNET_CONTAINER_multihashmap_size (pg->
3567 [pg_iter].connect_peers_working_set);
3576 count_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg)
3579 unsigned int pg_iter;
3581 struct PeerConnection *conn_iter;
3585 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3588 conn_iter = pg->peers[pg_iter].allowed_peers_head;
3589 while (conn_iter != NULL)
3592 conn_iter = conn_iter->next;
3596 GNUNET_CONTAINER_multihashmap_size (pg->
3598 [pg_iter].allowed_peers);
3607 * From the set of connections possible, choose at least num connections per
3610 * @param pg the peergroup we are dealing with
3611 * @param num how many connections at least should each peer have (if possible)?
3614 choose_minimum (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3617 struct MinimumContext minimum_ctx;
3619 struct PeerConnection *conn_iter;
3620 unsigned int temp_list_size;
3623 uint32_t random; /* Random list entry to connect peer to */
3628 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3630 temp_list_size = count_connections(pg->peers[pg_iter].connect_peers_head);
3631 if (temp_list_size == 0)
3633 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer %d has 0 connections!?!?\n", pg_iter);
3636 for (i = 0; i < num; i++)
3638 random = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, temp_list_size);
3639 conn_iter = pg->peers[pg_iter].connect_peers_head;
3640 for (count = 0; count < random; count++)
3641 conn_iter = conn_iter->next;
3642 /* We now have a random connection, connect it! */
3643 GNUNET_assert(conn_iter != NULL);
3644 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
3648 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3650 pg->peers[pg_iter].connect_peers_working_set =
3651 GNUNET_CONTAINER_multihashmap_create (num);
3654 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3656 minimum_ctx.first_uid = pg_iter;
3657 minimum_ctx.pg_array =
3658 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
3659 GNUNET_CONTAINER_multihashmap_size
3660 (pg->peers[pg_iter].connect_peers));
3661 minimum_ctx.first = &pg->peers[pg_iter];
3662 minimum_ctx.pg = pg;
3663 minimum_ctx.num_to_add = num;
3664 minimum_ctx.current = 0;
3665 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3666 &minimum_connect_iterator,
3670 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3672 /* Remove the "old" connections */
3673 GNUNET_CONTAINER_multihashmap_destroy (pg->
3674 peers[pg_iter].connect_peers);
3675 /* And replace with the working set */
3676 pg->peers[pg_iter].connect_peers =
3677 pg->peers[pg_iter].connect_peers_working_set;
3680 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3682 while (pg->peers[pg_iter].connect_peers_head != NULL)
3683 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
3685 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
3686 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
3687 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
3688 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
3693 struct FindClosestContext
3696 * The currently known closest peer.
3698 struct GNUNET_TESTING_Daemon *closest;
3701 * The info for the peer we are adding connections for.
3703 struct PeerData *curr_peer;
3706 * The distance (bits) between the current
3707 * peer and the currently known closest.
3709 unsigned int closest_dist;
3712 * The offset of the closest known peer in
3715 unsigned int closest_num;
3719 * Iterator over hash map entries of the allowed
3720 * peer connections. Find the closest, not already
3721 * connected peer and return it.
3723 * @param cls closure (struct FindClosestContext)
3724 * @param key current key code (hash of offset in pg)
3725 * @param value value in the hash map - a GNUNET_TESTING_Daemon
3726 * @return GNUNET_YES if we should continue to
3731 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
3733 struct FindClosestContext *closest_ctx = cls;
3734 struct GNUNET_TESTING_Daemon *daemon = value;
3736 if (((closest_ctx->closest == NULL) ||
3737 (GNUNET_CRYPTO_hash_matching_bits
3738 (&daemon->id.hashPubKey,
3739 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
3740 closest_ctx->closest_dist))
3742 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
3743 curr_peer->connect_peers,
3746 closest_ctx->closest_dist =
3747 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
3748 &closest_ctx->curr_peer->daemon->
3750 closest_ctx->closest = daemon;
3751 uid_from_hash (key, &closest_ctx->closest_num);
3758 * From the set of connections possible, choose at num connections per
3759 * peer based on depth which are closest out of those allowed. Guaranteed
3760 * to add num peers to connect to, provided there are that many peers
3761 * in the underlay topology to connect to.
3763 * @param pg the peergroup we are dealing with
3764 * @param num how many connections at least should each peer have (if possible)?
3765 * @param proc processor to actually add the connections
3768 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
3769 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
3774 struct FindClosestContext closest_ctx;
3779 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
3781 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3783 closest_ctx.curr_peer = &pg->peers[pg_iter];
3784 closest_ctx.closest = NULL;
3785 closest_ctx.closest_dist = 0;
3786 closest_ctx.closest_num = 0;
3787 GNUNET_CONTAINER_multihashmap_iterate (pg->
3788 peers[pg_iter].allowed_peers,
3789 &find_closest_peers,
3791 if (closest_ctx.closest != NULL)
3793 GNUNET_assert (closest_ctx.closest_num < pg->total);
3794 proc (pg, pg_iter, closest_ctx.closest_num, list);
3802 * From the set of connections possible, choose at least num connections per
3803 * peer based on depth first traversal of peer connections. If DFS leaves
3804 * peers unconnected, ensure those peers get connections.
3806 * @param pg the peergroup we are dealing with
3807 * @param num how many connections at least should each peer have (if possible)?
3810 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3814 uint32_t starting_peer;
3815 uint32_t least_connections;
3816 uint32_t random_connection;
3818 unsigned int temp_count;
3819 struct PeerConnection *peer_iter;
3821 struct DFSContext dfs_ctx;
3822 GNUNET_HashCode second_hash;
3828 while ((count_workingset_connections (pg) < num * pg->total)
3829 && (count_allowed_connections (pg) > 0))
3831 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3833 least_connections = -1; /* Set to very high number */
3834 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3836 temp_count = count_connections(pg->peers[pg_iter].connect_peers_working_set_head);
3837 if (temp_count < least_connections)
3839 starting_peer = pg_iter;
3840 least_connections = temp_count;
3845 temp_count = count_connections(pg->peers[starting_peer].connect_peers_head);
3846 if (temp_count == 0)
3847 continue; /* FIXME: infinite loop? */
3849 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, temp_count);
3851 peer_iter = pg->peers[starting_peer].connect_peers_head;
3852 while (temp_count < random_connection)
3854 peer_iter = peer_iter->next;
3857 GNUNET_assert(peer_iter != NULL);
3858 add_connections(pg, starting_peer, peer_iter->index, WORKING_SET);
3859 remove_connections(pg, starting_peer, peer_iter->index, CONNECT);
3860 starting_peer = peer_iter->index;
3865 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3867 pg->peers[pg_iter].connect_peers_working_set =
3868 GNUNET_CONTAINER_multihashmap_create (num);
3873 while ((count_workingset_connections (pg) < num * pg->total)
3874 && (count_allowed_connections (pg) > 0))
3876 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3878 least_connections = -1; /* Set to very high number */
3879 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3881 if (GNUNET_CONTAINER_multihashmap_size
3882 (pg->peers[pg_iter].connect_peers_working_set) <
3885 starting_peer = pg_iter;
3887 GNUNET_CONTAINER_multihashmap_size (pg->
3889 [pg_iter].connect_peers_working_set);
3894 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
3900 /* Choose a random peer from the chosen peers set of connections to add */
3902 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3903 GNUNET_CONTAINER_multihashmap_size
3904 (pg->peers[starting_peer].connect_peers));
3905 dfs_ctx.first_uid = starting_peer;
3906 dfs_ctx.first = &pg->peers[starting_peer];
3908 dfs_ctx.current = 0;
3910 GNUNET_CONTAINER_multihashmap_iterate (pg->
3912 [starting_peer].connect_peers,
3913 &dfs_connect_iterator, &dfs_ctx);
3914 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
3915 hash_from_uid (dfs_ctx.second_uid, &second_hash);
3916 GNUNET_assert (GNUNET_YES ==
3917 GNUNET_CONTAINER_multihashmap_remove (pg->peers
3918 [starting_peer].connect_peers,
3922 [dfs_ctx.second_uid].daemon));
3923 starting_peer = dfs_ctx.second_uid;
3926 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3928 /* Remove the "old" connections */
3929 GNUNET_CONTAINER_multihashmap_destroy (pg->
3930 peers[pg_iter].connect_peers);
3931 /* And replace with the working set */
3932 pg->peers[pg_iter].connect_peers =
3933 pg->peers[pg_iter].connect_peers_working_set;
3940 * Internal callback for topology information for a particular peer.
3943 internal_topology_callback (void *cls,
3944 const struct GNUNET_PeerIdentity *peer,
3945 const struct GNUNET_TRANSPORT_ATS_Information
3948 struct CoreContext *core_ctx = cls;
3949 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
3951 if (peer == NULL) /* Either finished, or something went wrong */
3953 iter_ctx->completed++;
3954 iter_ctx->connected--;
3955 /* One core context allocated per iteration, must free! */
3956 GNUNET_free (core_ctx);
3960 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id,
3964 if (iter_ctx->completed == iter_ctx->total)
3966 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
3967 /* Once all are done, free the iteration context */
3968 GNUNET_free (iter_ctx);
3974 * Check running topology iteration tasks, if below max start a new one, otherwise
3975 * schedule for some time in the future.
3978 schedule_get_topology (void *cls,
3979 const struct GNUNET_SCHEDULER_TaskContext *tc)
3981 struct CoreContext *core_context = cls;
3982 struct TopologyIterateContext *topology_context =
3983 (struct TopologyIterateContext *) core_context->iter_context;
3984 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3987 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3989 #if VERBOSE_TESTING > 2
3990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3992 ("Delaying connect, we have too many outstanding connections!\n"));
3994 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3995 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3996 &schedule_get_topology, core_context);
4000 #if VERBOSE_TESTING > 2
4001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4002 _("Creating connection, outstanding_connections is %d\n"),
4003 outstanding_connects);
4005 topology_context->connected++;
4008 GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4009 &internal_topology_callback,
4012 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4013 internal_topology_callback (core_context, NULL, NULL);
4019 * Iterate over all (running) peers in the peer group, retrieve
4020 * all connections that each currently has.
4023 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
4024 GNUNET_TESTING_NotifyTopology cb, void *cls)
4026 struct TopologyIterateContext *topology_context;
4027 struct CoreContext *core_ctx;
4029 unsigned int total_count;
4031 /* Allocate a single topology iteration context */
4032 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4033 topology_context->topology_cb = cb;
4034 topology_context->cls = cls;
4036 for (i = 0; i < pg->total; i++)
4038 if (pg->peers[i].daemon->running == GNUNET_YES)
4040 /* Allocate one core context per core we need to connect to */
4041 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4042 core_ctx->daemon = pg->peers[i].daemon;
4043 /* Set back pointer to topology iteration context */
4044 core_ctx->iter_context = topology_context;
4045 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4049 if (total_count == 0)
4051 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4052 GNUNET_free (topology_context);
4055 topology_context->total = total_count;
4060 * Callback function to process statistic values.
4061 * This handler is here only really to insert a peer
4062 * identity (or daemon) so the statistics can be uniquely
4063 * tied to a single running peer.
4065 * @param cls closure
4066 * @param subsystem name of subsystem that created the statistic
4067 * @param name the name of the datum
4068 * @param value the current value
4069 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4070 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4073 internal_stats_callback (void *cls,
4074 const char *subsystem,
4075 const char *name, uint64_t value, int is_persistent)
4077 struct StatsCoreContext *core_context = cls;
4078 struct StatsIterateContext *stats_context =
4079 (struct StatsIterateContext *) core_context->iter_context;
4081 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4082 subsystem, name, value, is_persistent);
4086 * Internal continuation call for statistics iteration.
4088 * @param cls closure, the CoreContext for this iteration
4089 * @param success whether or not the statistics iterations
4090 * was canceled or not (we don't care)
4093 internal_stats_cont (void *cls, int success)
4095 struct StatsCoreContext *core_context = cls;
4096 struct StatsIterateContext *stats_context =
4097 (struct StatsIterateContext *) core_context->iter_context;
4099 stats_context->connected--;
4100 stats_context->completed++;
4102 if (stats_context->completed == stats_context->total)
4104 stats_context->cont (stats_context->cls, GNUNET_YES);
4105 GNUNET_free (stats_context);
4108 if (core_context->stats_handle != NULL)
4109 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4111 GNUNET_free (core_context);
4115 * Check running topology iteration tasks, if below max start a new one, otherwise
4116 * schedule for some time in the future.
4119 schedule_get_statistics (void *cls,
4120 const struct GNUNET_SCHEDULER_TaskContext *tc)
4122 struct StatsCoreContext *core_context = cls;
4123 struct StatsIterateContext *stats_context =
4124 (struct StatsIterateContext *) core_context->iter_context;
4126 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4129 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
4131 #if VERBOSE_TESTING > 2
4132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4134 ("Delaying connect, we have too many outstanding connections!\n"));
4136 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4137 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4138 &schedule_get_statistics, core_context);
4142 #if VERBOSE_TESTING > 2
4143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4144 _("Creating connection, outstanding_connections is %d\n"),
4145 outstanding_connects);
4148 stats_context->connected++;
4149 core_context->stats_handle =
4150 GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
4151 if (core_context->stats_handle == NULL)
4153 internal_stats_cont (core_context, GNUNET_NO);
4157 core_context->stats_get_handle =
4158 GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
4159 GNUNET_TIME_relative_get_forever (),
4160 &internal_stats_cont, &internal_stats_callback,
4162 if (core_context->stats_get_handle == NULL)
4163 internal_stats_cont (core_context, GNUNET_NO);
4168 struct DuplicateStats
4171 * Next item in the list
4173 struct DuplicateStats *next;
4176 * Nasty string, concatenation of relevant information.
4178 char *unique_string;
4182 * Check whether the combination of port/host/unix domain socket
4183 * already exists in the list of peers being checked for statistics.
4185 * @param pg the peergroup in question
4186 * @param specific_peer the peer we're concerned with
4187 * @param stats_list the list to return to the caller
4189 * @return GNUNET_YES if the statistics instance has been seen already,
4190 * GNUNET_NO if not (and we may have added it to the list)
4193 stats_check_existing (struct GNUNET_TESTING_PeerGroup *pg,
4194 struct PeerData *specific_peer,
4195 struct DuplicateStats **stats_list)
4197 struct DuplicateStats *pos;
4198 char *unix_domain_socket;
4199 unsigned long long port;
4202 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
4203 "single_statistics_per_host"))
4204 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
4208 GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg, "statistics",
4209 "unixpath", &unix_domain_socket))
4213 GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg, "statistics",
4216 GNUNET_free(unix_domain_socket);
4220 if (specific_peer->daemon->hostname != NULL)
4221 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
4222 unix_domain_socket, port);
4224 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
4228 if (0 == strcmp (to_match, pos->unique_string))
4230 GNUNET_free (unix_domain_socket);
4231 GNUNET_free (to_match);
4236 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
4237 pos->unique_string = to_match;
4238 pos->next = *stats_list;
4240 GNUNET_free (unix_domain_socket);
4245 * Iterate over all (running) peers in the peer group, retrieve
4246 * all statistics from each.
4249 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
4250 GNUNET_STATISTICS_Callback cont,
4251 GNUNET_TESTING_STATISTICS_Iterator proc,
4254 struct StatsIterateContext *stats_context;
4255 struct StatsCoreContext *core_ctx;
4257 unsigned int total_count;
4258 struct DuplicateStats *stats_list;
4259 struct DuplicateStats *pos;
4262 /* Allocate a single stats iteration context */
4263 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
4264 stats_context->cont = cont;
4265 stats_context->proc = proc;
4266 stats_context->cls = cls;
4269 for (i = 0; i < pg->total; i++)
4271 if ((pg->peers[i].daemon->running == GNUNET_YES)
4273 stats_check_existing (pg, &pg->peers[i], &stats_list)))
4275 /* Allocate one core context per core we need to connect to */
4276 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
4277 core_ctx->daemon = pg->peers[i].daemon;
4278 /* Set back pointer to topology iteration context */
4279 core_ctx->iter_context = stats_context;
4280 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
4285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4286 "Retrieving stats from %u total instances.\n", total_count);
4287 stats_context->total = total_count;
4288 if (stats_list != NULL)
4293 GNUNET_free (pos->unique_string);
4294 stats_list = pos->next;
4296 pos = stats_list->next;
4303 * There are many ways to connect peers that are supported by this function.
4304 * To connect peers in the same topology that was created via the
4305 * GNUNET_TESTING_create_topology, the topology variable must be set to
4306 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
4307 * a new instance of that topology will be generated and attempted to be
4308 * connected. This could result in some connections being impossible,
4309 * because some topologies are non-deterministic.
4311 * @param pg the peer group struct representing the running peers
4312 * @param topology which topology to connect the peers in
4313 * @param options options for connecting the topology
4314 * @param option_modifier modifier for options that take a parameter
4315 * @param notify_callback notification to be called once all connections completed
4316 * @param notify_cls closure for notification callback
4318 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
4321 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
4322 enum GNUNET_TESTING_Topology topology,
4323 enum GNUNET_TESTING_TopologyOption options,
4324 double option_modifier,
4325 GNUNET_TESTING_NotifyCompletion
4326 notify_callback, void *notify_cls)
4330 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4331 #if VERBOSE_TOPOLOGY
4332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4333 _("Creating clique CONNECT topology\n"));
4335 create_clique (pg, &add_connections, CONNECT);
4337 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4338 #if VERBOSE_TOPOLOGY
4339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4340 _("Creating small world (ring) CONNECT topology\n"));
4342 create_small_world_ring (pg, &add_connections, CONNECT);
4344 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4345 #if VERBOSE_TOPOLOGY
4346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4347 _("Creating small world (2d-torus) CONNECT topology\n"));
4349 create_small_world (pg, &add_connections, CONNECT);
4351 case GNUNET_TESTING_TOPOLOGY_RING:
4352 #if VERBOSE_TOPOLOGY
4353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4354 _("Creating ring CONNECT topology\n"));
4356 create_ring (pg, &add_connections, CONNECT);
4358 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4359 #if VERBOSE_TOPOLOGY
4360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4361 _("Creating 2d torus CONNECT topology\n"));
4363 create_2d_torus (pg, &add_connections, CONNECT);
4365 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4366 #if VERBOSE_TOPOLOGY
4367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4368 _("Creating Erdos-Renyi CONNECT topology\n"));
4370 create_erdos_renyi (pg, &add_connections, CONNECT);
4372 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4373 #if VERBOSE_TOPOLOGY
4374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4375 _("Creating InterNAT CONNECT topology\n"));
4377 create_nated_internet (pg, &add_connections, CONNECT);
4379 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4380 #if VERBOSE_TOPOLOGY
4381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4382 _("Creating Scale Free CONNECT topology\n"));
4384 create_scale_free (pg, &add_connections, CONNECT);
4386 case GNUNET_TESTING_TOPOLOGY_LINE:
4387 #if VERBOSE_TOPOLOGY
4388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4389 _("Creating straight line CONNECT topology\n"));
4391 create_line (pg, &add_connections, CONNECT);
4393 case GNUNET_TESTING_TOPOLOGY_NONE:
4394 #if VERBOSE_TOPOLOGY
4395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4396 _("Creating no CONNECT topology\n"));
4398 copy_allowed_topology (pg);
4401 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4403 ("Unknown topology specification, can't connect peers!\n"));
4404 return GNUNET_SYSERR;
4409 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
4410 #if VERBOSE_TOPOLOGY
4411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4413 ("Connecting random subset (%'.2f percent) of possible peers\n"),
4414 100 * option_modifier);
4416 choose_random_connections (pg, option_modifier);
4418 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
4419 #if VERBOSE_TOPOLOGY
4420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4421 _("Connecting a minimum of %u peers each (if possible)\n"),
4422 (unsigned int) option_modifier);
4424 choose_minimum (pg, (unsigned int) option_modifier);
4426 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
4427 #if VERBOSE_TOPOLOGY
4428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4430 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
4431 (unsigned int) option_modifier);
4434 perform_dfs (pg, (int) option_modifier);
4437 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
4438 #if VERBOSE_TOPOLOGY
4439 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4441 ("Finding additional %u closest peers each (if possible)\n"),
4442 (unsigned int) option_modifier);
4445 add_closest (pg, (unsigned int) option_modifier,
4446 &add_connections, CONNECT);
4449 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
4451 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
4457 return connect_topology (pg, notify_callback, notify_cls);
4461 * Callback that is called whenever a hostkey is generated
4462 * for a peer. Call the real callback and decrement the
4463 * starting counter for the peergroup.
4465 * @param cls closure
4466 * @param id identifier for the daemon, NULL on error
4467 * @param d handle for the daemon
4468 * @param emsg error message (NULL on success)
4471 internal_hostkey_callback (void *cls,
4472 const struct GNUNET_PeerIdentity *id,
4473 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4475 struct InternalStartContext *internal_context = cls;
4476 internal_context->peer->pg->starting--;
4477 internal_context->peer->pg->started++;
4478 if (internal_context->hostkey_callback != NULL)
4479 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
4481 else if (internal_context->peer->pg->started ==
4482 internal_context->peer->pg->total)
4484 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
4485 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
4490 * Callback that is called whenever a peer has finished starting.
4491 * Call the real callback and decrement the starting counter
4492 * for the peergroup.
4494 * @param cls closure
4495 * @param id identifier for the daemon, NULL on error
4497 * @param d handle for the daemon
4498 * @param emsg error message (NULL on success)
4501 internal_startup_callback (void *cls,
4502 const struct GNUNET_PeerIdentity *id,
4503 const struct GNUNET_CONFIGURATION_Handle *cfg,
4504 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4506 struct InternalStartContext *internal_context = cls;
4507 internal_context->peer->pg->starting--;
4508 if (internal_context->start_cb != NULL)
4509 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
4514 internal_continue_startup (void *cls,
4515 const struct GNUNET_SCHEDULER_TaskContext *tc)
4517 struct InternalStartContext *internal_context = cls;
4519 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4524 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
4526 internal_context->peer->pg->starting++;
4527 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
4531 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4532 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4533 &internal_continue_startup,
4540 * Callback for informing us about a successful
4541 * or unsuccessful churn start call.
4543 * @param cls a ChurnContext
4544 * @param id the peer identity of the started peer
4545 * @param cfg the handle to the configuration of the peer
4546 * @param d handle to the daemon for the peer
4547 * @param emsg NULL on success, non-NULL on failure
4551 churn_start_callback (void *cls,
4552 const struct GNUNET_PeerIdentity *id,
4553 const struct GNUNET_CONFIGURATION_Handle *cfg,
4554 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4556 struct ChurnRestartContext *startup_ctx = cls;
4557 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
4559 unsigned int total_left;
4560 char *error_message;
4562 error_message = NULL;
4565 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4566 "Churn stop callback failed with error `%s'\n", emsg);
4567 churn_ctx->num_failed_start++;
4571 churn_ctx->num_to_start--;
4575 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4576 "Started peer, %d left.\n", churn_ctx->num_to_start);
4580 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
4581 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4583 if (total_left == 0)
4585 if ((churn_ctx->num_failed_stop > 0)
4586 || (churn_ctx->num_failed_start > 0))
4587 GNUNET_asprintf (&error_message,
4588 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4589 churn_ctx->num_failed_start,
4590 churn_ctx->num_failed_stop);
4591 churn_ctx->cb (churn_ctx->cb_cls, error_message);
4592 GNUNET_free_non_null (error_message);
4593 GNUNET_free (churn_ctx);
4594 GNUNET_free (startup_ctx);
4600 schedule_churn_restart (void *cls,
4601 const struct GNUNET_SCHEDULER_TaskContext *tc)
4603 struct PeerRestartContext *peer_restart_ctx = cls;
4604 struct ChurnRestartContext *startup_ctx =
4605 peer_restart_ctx->churn_restart_ctx;
4607 if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
4608 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4609 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4610 &schedule_churn_restart, peer_restart_ctx);
4613 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
4614 startup_ctx->timeout,
4615 &churn_start_callback,
4617 GNUNET_free (peer_restart_ctx);
4622 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4624 struct InternalStartContext *internal_context = cls;
4626 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4631 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
4633 internal_context->peer->pg->starting++;
4634 internal_context->peer->daemon =
4635 GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
4636 internal_context->timeout,
4637 internal_context->hostname,
4638 internal_context->username,
4639 internal_context->sshport,
4640 internal_context->hostkey,
4641 &internal_hostkey_callback,
4643 &internal_startup_callback,
4648 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4649 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4650 &internal_start, internal_context);
4655 * Function which continues a peer group starting up
4656 * after successfully generating hostkeys for each peer.
4658 * @param pg the peer group to continue starting
4662 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg)
4667 for (i = 0; i < pg->total; i++)
4669 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
4670 &pg->peers[i].internal_context);
4671 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
4676 * Start count gnunet instances with the same set of transports and
4677 * applications. The port numbers (any option called "PORT") will be
4678 * adjusted to ensure that no two peers running on the same system
4679 * have the same port(s) in their respective configurations.
4681 * @param cfg configuration template to use
4682 * @param total number of daemons to start
4683 * @param timeout total time allowed for peers to start
4684 * @param hostkey_callback function to call on each peers hostkey generation
4685 * if NULL, peers will be started by this call, if non-null,
4686 * GNUNET_TESTING_daemons_continue_startup must be called after
4687 * successful hostkey generation
4688 * @param hostkey_cls closure for hostkey callback
4689 * @param cb function to call on each daemon that was started
4690 * @param cb_cls closure for cb
4691 * @param connect_callback function to call each time two hosts are connected
4692 * @param connect_callback_cls closure for connect_callback
4693 * @param hostnames linked list of host structs to use to start peers on
4694 * (NULL to run on localhost only)
4696 * @return NULL on error, otherwise handle to control peer group
4698 struct GNUNET_TESTING_PeerGroup *
4699 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
4701 struct GNUNET_TIME_Relative timeout,
4702 GNUNET_TESTING_NotifyHostkeyCreated
4703 hostkey_callback, void *hostkey_cls,
4704 GNUNET_TESTING_NotifyDaemonRunning cb,
4706 GNUNET_TESTING_NotifyConnection
4707 connect_callback, void *connect_callback_cls,
4708 const struct GNUNET_TESTING_Host *hostnames)
4710 struct GNUNET_TESTING_PeerGroup *pg;
4711 const struct GNUNET_TESTING_Host *hostpos;
4717 const char *hostname;
4718 const char *username;
4719 char *baseservicehome;
4720 char *newservicehome;
4722 char *hostkeys_file;
4725 struct GNUNET_DISK_FileHandle *fd;
4726 struct GNUNET_CONFIGURATION_Handle *pcfg;
4728 unsigned int hostcnt;
4735 uint64_t total_hostkeys;
4736 struct GNUNET_OS_Process *proc;
4746 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
4748 pg->notify_connection = connect_callback;
4749 pg->notify_connection_cls = connect_callback_cls;
4751 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
4752 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
4753 if (NULL != hostnames)
4756 hostpos = hostnames;
4757 while (hostpos != NULL)
4759 hostpos = hostpos->next;
4762 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4765 hostpos = hostnames;
4766 while (hostpos != NULL)
4768 pg->hosts[off].minport = LOW_PORT;
4769 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
4770 if (hostpos->username != NULL)
4771 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
4772 pg->hosts[off].sshport = hostpos->port;
4773 hostpos = hostpos->next;
4783 pg->num_hosts = off;
4787 /* skip leading spaces */
4788 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
4791 while ('\0' != *rpos)
4793 if (isspace ((unsigned char) *rpos))
4797 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4799 start = GNUNET_strdup (hostnames);
4801 while ('\0' != *pos)
4803 if (isspace ((unsigned char) *pos))
4806 if (strlen (start) > 0)
4808 pg->hosts[off].minport = LOW_PORT;
4809 pg->hosts[off++].hostname = start;
4815 if (strlen (start) > 0)
4817 pg->hosts[off].minport = LOW_PORT;
4818 pg->hosts[off++].hostname = start;
4822 GNUNET_free (start);
4823 GNUNET_free (pg->hosts);
4827 minport = 0; /* make gcc happy */
4836 /* Create the servicehome directory for each remote peer */
4837 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
4839 for (i = 0; i < pg->num_hosts; i++)
4842 if (NULL != pg->hosts[i].username)
4843 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username, pg->hosts[i].hostname);
4845 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
4846 if (pg->hosts[i].sshport != 0)
4848 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
4849 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
4850 "ssh", "-P", ssh_port_str,
4854 arg, "mkdir -p", baseservicehome, NULL);
4857 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
4858 "ssh", arg, "mkdir -p", baseservicehome, NULL);
4859 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Creating remote dir with command ssh %s %s %s\n", arg, " mkdir -p ", baseservicehome);
4860 GNUNET_OS_process_wait(proc);
4862 GNUNET_free(baseservicehome);
4864 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "HOSTKEYSFILE",
4867 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
4868 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!");
4871 /* Check hostkey file size, read entire thing into memory */
4872 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
4873 GNUNET_DISK_PERM_NONE);
4876 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
4880 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
4883 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found file size %llu for hostkeys, expect hostkeys to be size %d\n", fs, HOSTKEYFILESIZE);
4885 if (fs % HOSTKEYFILESIZE != 0)
4887 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "File size %llu seems incorrect for hostkeys...\n", fs);
4891 total_hostkeys = fs / HOSTKEYFILESIZE;
4892 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Will read %llu hostkeys from file\n", total_hostkeys);
4893 pg->hostkey_data = GNUNET_malloc_large (fs);
4894 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
4895 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
4898 GNUNET_free(hostkeys_file);
4901 for (off = 0; off < total; off++)
4905 hostname = pg->hosts[off % hostcnt].hostname;
4906 username = pg->hosts[off % hostcnt].username;
4907 sshport = pg->hosts[off % hostcnt].sshport;
4908 pcfg = make_config (cfg,
4909 &pg->hosts[off % hostcnt].minport,
4910 &upnum, hostname, &fdnum);
4917 pcfg = make_config (cfg, &minport, &upnum, hostname, &fdnum);
4922 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4924 ("Could not create configuration for peer number %u on `%s'!\n"),
4925 off, hostname == NULL ? "localhost" : hostname);
4930 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
4933 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
4934 GNUNET_free (baseservicehome);
4938 tmpdir = getenv ("TMPDIR");
4939 tmpdir = tmpdir ? tmpdir : "/tmp";
4940 GNUNET_asprintf (&newservicehome,
4942 tmpdir, "gnunet-testing-test-test", off);
4944 GNUNET_CONFIGURATION_set_value_string (pcfg,
4946 "SERVICEHOME", newservicehome);
4947 GNUNET_free (newservicehome);
4948 pg->peers[off].cfg = pcfg;
4950 /* Can we do this later? */
4951 pg->peers[off].allowed_peers =
4952 GNUNET_CONTAINER_multihashmap_create (total);
4953 pg->peers[off].connect_peers =
4954 GNUNET_CONTAINER_multihashmap_create (total);
4955 pg->peers[off].blacklisted_peers =
4956 GNUNET_CONTAINER_multihashmap_create (total);
4959 pg->peers[off].pg = pg;
4960 pg->peers[off].internal_context.peer = &pg->peers[off];
4961 pg->peers[off].internal_context.timeout = timeout;
4962 pg->peers[off].internal_context.hostname = hostname;
4963 pg->peers[off].internal_context.username = username;
4964 pg->peers[off].internal_context.sshport = sshport;
4965 if (pg->hostkey_data != NULL)
4966 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off * HOSTKEYFILESIZE];
4967 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
4968 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
4969 pg->peers[off].internal_context.start_cb = cb;
4970 pg->peers[off].internal_context.start_cb_cls = cb_cls;
4972 GNUNET_SCHEDULER_add_now (&internal_start,
4973 &pg->peers[off].internal_context);
4980 * Get a daemon by number, so callers don't have to do nasty
4981 * offsetting operation.
4983 struct GNUNET_TESTING_Daemon *
4984 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
4985 unsigned int position)
4987 if (position < pg->total)
4988 return pg->peers[position].daemon;
4994 * Get a daemon by peer identity, so callers can
4995 * retrieve the daemon without knowing it's offset.
4997 * @param pg the peer group to retrieve the daemon from
4998 * @param peer_id the peer identity of the daemon to retrieve
5000 * @return the daemon on success, or NULL if no such peer identity is found
5002 struct GNUNET_TESTING_Daemon *
5003 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
5004 struct GNUNET_PeerIdentity *peer_id)
5008 for (i = 0; i < pg->total; i++)
5011 memcmp (&pg->peers[i].daemon->id, peer_id,
5012 sizeof (struct GNUNET_PeerIdentity)))
5013 return pg->peers[i].daemon;
5020 * Prototype of a function that will be called when a
5021 * particular operation was completed the testing library.
5023 * @param cls closure (a struct RestartContext)
5024 * @param id id of the peer that was restarted
5025 * @param cfg handle to the configuration of the peer
5026 * @param d handle to the daemon that was restarted
5027 * @param emsg NULL on success
5030 restart_callback (void *cls,
5031 const struct GNUNET_PeerIdentity *id,
5032 const struct GNUNET_CONFIGURATION_Handle *cfg,
5033 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5035 struct RestartContext *restart_context = cls;
5039 restart_context->peers_restarted++;
5043 restart_context->peers_restart_failed++;
5046 if (restart_context->peers_restarted == restart_context->peer_group->total)
5048 restart_context->callback (restart_context->callback_cls, NULL);
5049 GNUNET_free (restart_context);
5051 else if (restart_context->peers_restart_failed +
5052 restart_context->peers_restarted ==
5053 restart_context->peer_group->total)
5055 restart_context->callback (restart_context->callback_cls,
5056 "Failed to restart peers!");
5057 GNUNET_free (restart_context);
5063 * Callback for informing us about a successful
5064 * or unsuccessful churn stop call.
5066 * @param cls a ChurnContext
5067 * @param emsg NULL on success, non-NULL on failure
5071 churn_stop_callback (void *cls, const char *emsg)
5073 struct ShutdownContext *shutdown_ctx = cls;
5074 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
5075 unsigned int total_left;
5076 char *error_message;
5078 error_message = NULL;
5079 shutdown_ctx->outstanding--;
5083 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5084 "Churn stop callback failed with error `%s'\n", emsg);
5085 churn_ctx->num_failed_stop++;
5089 churn_ctx->num_to_stop--;
5093 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5094 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
5097 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
5098 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5100 if (total_left == 0)
5102 if ((churn_ctx->num_failed_stop > 0)
5103 || (churn_ctx->num_failed_start > 0))
5105 GNUNET_asprintf (&error_message,
5106 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5107 churn_ctx->num_failed_start,
5108 churn_ctx->num_failed_stop);
5110 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5111 GNUNET_free_non_null (error_message);
5112 GNUNET_free (churn_ctx);
5113 GNUNET_free (shutdown_ctx);
5118 * Count the number of running peers.
5120 * @param pg handle for the peer group
5122 * @return the number of currently running peers in the peer group
5125 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
5128 unsigned int running = 0;
5129 for (i = 0; i < pg->total; i++)
5131 if (pg->peers[i].daemon->running == GNUNET_YES)
5133 GNUNET_assert (running != -1);
5141 * Task to rate limit the number of outstanding peer shutdown
5142 * requests. This is necessary for making sure we don't do
5143 * too many ssh connections at once, but is generally nicer
5144 * to any system as well (graduated task starts, as opposed
5145 * to calling gnunet-arm N times all at once).
5148 schedule_churn_shutdown_task (void *cls,
5149 const struct GNUNET_SCHEDULER_TaskContext *tc)
5151 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5152 struct ShutdownContext *shutdown_ctx;
5154 GNUNET_assert (peer_shutdown_ctx != NULL);
5155 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5156 GNUNET_assert (shutdown_ctx != NULL);
5158 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
5159 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5160 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5161 &schedule_churn_shutdown_task,
5165 shutdown_ctx->outstanding++;
5166 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5167 shutdown_ctx->timeout, shutdown_ctx->cb,
5168 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5169 GNUNET_free (peer_shutdown_ctx);
5174 * Simulate churn by stopping some peers (and possibly
5175 * re-starting others if churn is called multiple times). This
5176 * function can only be used to create leave-join churn (peers "never"
5177 * leave for good). First "voff" random peers that are currently
5178 * online will be taken offline; then "von" random peers that are then
5179 * offline will be put back online. No notifications will be
5180 * generated for any of these operations except for the callback upon
5183 * @param pg handle for the peer group
5184 * @param voff number of peers that should go offline
5185 * @param von number of peers that should come back online;
5186 * must be zero on first call (since "testbed_start"
5187 * always starts all of the peers)
5188 * @param timeout how long to wait for operations to finish before
5190 * @param cb function to call at the end
5191 * @param cb_cls closure for cb
5194 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
5197 struct GNUNET_TIME_Relative timeout,
5198 GNUNET_TESTING_NotifyCompletion cb,
5201 struct ChurnContext *churn_ctx;
5202 struct ShutdownContext *shutdown_ctx;
5203 struct PeerShutdownContext *peer_shutdown_ctx;
5204 struct PeerRestartContext *peer_restart_ctx;
5205 struct ChurnRestartContext *churn_startup_ctx;
5207 unsigned int running;
5208 unsigned int stopped;
5209 unsigned int total_running;
5210 unsigned int total_stopped;
5212 unsigned int *running_arr;
5213 unsigned int *stopped_arr;
5214 unsigned int *running_permute;
5215 unsigned int *stopped_permute;
5220 if ((von == 0) && (voff == 0)) /* No peers at all? */
5226 for (i = 0; i < pg->total; i++)
5228 if (pg->peers[i].daemon->running == GNUNET_YES)
5230 GNUNET_assert (running != -1);
5235 GNUNET_assert (stopped != -1);
5242 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5243 "Trying to stop more peers than are currently running!\n");
5244 cb (cb_cls, "Trying to stop more peers than are currently running!");
5250 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5251 "Trying to start more peers than are currently stopped!\n");
5252 cb (cb_cls, "Trying to start more peers than are currently stopped!");
5256 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5260 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
5264 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
5266 running_permute = NULL;
5267 stopped_permute = NULL;
5271 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, running);
5274 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, stopped);
5276 total_running = running;
5277 total_stopped = stopped;
5281 churn_ctx->num_to_start = von;
5282 churn_ctx->num_to_stop = voff;
5284 churn_ctx->cb_cls = cb_cls;
5286 for (i = 0; i < pg->total; i++)
5288 if (pg->peers[i].daemon->running == GNUNET_YES)
5290 GNUNET_assert ((running_arr != NULL) && (total_running > running));
5291 running_arr[running] = i;
5296 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
5297 stopped_arr[stopped] = i;
5302 GNUNET_assert (running >= voff);
5305 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5306 shutdown_ctx->cb = &churn_stop_callback;
5307 shutdown_ctx->cb_cls = churn_ctx;
5308 shutdown_ctx->total_peers = voff;
5309 shutdown_ctx->timeout = timeout;
5312 for (i = 0; i < voff; i++)
5315 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
5316 running_permute[i]);
5318 GNUNET_assert (running_arr != NULL);
5319 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
5320 peer_shutdown_ctx->daemon =
5321 pg->peers[running_arr[running_permute[i]]].daemon;
5322 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
5323 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
5327 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
5329 &churn_stop_callback, churn_ctx,
5330 GNUNET_NO, GNUNET_YES); */
5333 GNUNET_assert (stopped >= von);
5336 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
5337 churn_startup_ctx->churn_ctx = churn_ctx;
5338 churn_startup_ctx->timeout = timeout;
5340 for (i = 0; i < von; i++)
5343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
5344 stopped_permute[i]);
5346 GNUNET_assert (stopped_arr != NULL);
5347 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
5348 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
5349 peer_restart_ctx->daemon =
5350 pg->peers[stopped_arr[stopped_permute[i]]].daemon;
5351 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
5353 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
5354 timeout, &churn_start_callback, churn_ctx); */
5357 GNUNET_free_non_null (running_arr);
5358 GNUNET_free_non_null (stopped_arr);
5359 GNUNET_free_non_null (running_permute);
5360 GNUNET_free_non_null (stopped_permute);
5365 * Restart all peers in the given group.
5367 * @param pg the handle to the peer group
5368 * @param callback function to call on completion (or failure)
5369 * @param callback_cls closure for the callback function
5372 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
5373 GNUNET_TESTING_NotifyCompletion callback,
5376 struct RestartContext *restart_context;
5381 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
5382 restart_context->peer_group = pg;
5383 restart_context->peers_restarted = 0;
5384 restart_context->callback = callback;
5385 restart_context->callback_cls = callback_cls;
5387 for (off = 0; off < pg->total; off++)
5389 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
5390 &restart_callback, restart_context);
5396 * Start or stop an individual peer from the given group.
5398 * @param pg handle to the peer group
5399 * @param offset which peer to start or stop
5400 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
5401 * @param timeout how long to wait for shutdown
5402 * @param cb function to call at the end
5403 * @param cb_cls closure for cb
5406 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
5407 unsigned int offset,
5409 struct GNUNET_TIME_Relative timeout,
5410 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5412 struct ShutdownContext *shutdown_ctx;
5413 struct ChurnRestartContext *startup_ctx;
5414 struct ChurnContext *churn_ctx;
5416 if (GNUNET_NO == desired_status)
5418 if (NULL != pg->peers[offset].daemon)
5420 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5421 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5422 churn_ctx->num_to_start = 0;
5423 churn_ctx->num_to_stop = 1;
5425 churn_ctx->cb_cls = cb_cls;
5426 shutdown_ctx->cb_cls = churn_ctx;
5427 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon,
5428 timeout, &churn_stop_callback,
5429 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5432 else if (GNUNET_YES == desired_status)
5434 if (NULL == pg->peers[offset].daemon)
5436 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
5437 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5438 churn_ctx->num_to_start = 1;
5439 churn_ctx->num_to_stop = 0;
5441 churn_ctx->cb_cls = cb_cls;
5442 startup_ctx->churn_ctx = churn_ctx;
5443 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
5444 timeout, &churn_start_callback,
5454 * Callback for shutting down peers in a peer group.
5456 * @param cls closure (struct ShutdownContext)
5457 * @param emsg NULL on success
5460 internal_shutdown_callback (void *cls, const char *emsg)
5462 struct ShutdownContext *shutdown_ctx = cls;
5464 shutdown_ctx->outstanding--;
5467 shutdown_ctx->peers_down++;
5471 shutdown_ctx->peers_failed++;
5474 if ((shutdown_ctx->cb != NULL)
5475 && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed ==
5476 shutdown_ctx->total_peers))
5478 if (shutdown_ctx->peers_failed > 0)
5479 shutdown_ctx->cb (shutdown_ctx->cb_cls,
5480 "Not all peers successfully shut down!");
5482 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
5483 GNUNET_free (shutdown_ctx);
5489 * Task to rate limit the number of outstanding peer shutdown
5490 * requests. This is necessary for making sure we don't do
5491 * too many ssh connections at once, but is generally nicer
5492 * to any system as well (graduated task starts, as opposed
5493 * to calling gnunet-arm N times all at once).
5496 schedule_shutdown_task (void *cls,
5497 const struct GNUNET_SCHEDULER_TaskContext *tc)
5499 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5500 struct ShutdownContext *shutdown_ctx;
5502 GNUNET_assert (peer_shutdown_ctx != NULL);
5503 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5504 GNUNET_assert (shutdown_ctx != NULL);
5506 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
5507 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5508 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5509 &schedule_shutdown_task, peer_shutdown_ctx);
5512 shutdown_ctx->outstanding++;
5513 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5514 shutdown_ctx->timeout,
5515 &internal_shutdown_callback, shutdown_ctx,
5516 GNUNET_YES, GNUNET_NO);
5517 GNUNET_free (peer_shutdown_ctx);
5522 * Shutdown all peers started in the given group.
5524 * @param pg handle to the peer group
5525 * @param timeout how long to wait for shutdown
5526 * @param cb callback to notify upon success or failure
5527 * @param cb_cls closure for cb
5530 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
5531 struct GNUNET_TIME_Relative timeout,
5532 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5535 struct ShutdownContext *shutdown_ctx;
5536 struct PeerShutdownContext *peer_shutdown_ctx;
5538 struct PeerConnection *conn_iter;
5539 struct PeerConnection *temp_conn;
5542 GNUNET_assert (pg->total > 0);
5544 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5545 shutdown_ctx->cb = cb;
5546 shutdown_ctx->cb_cls = cb_cls;
5547 shutdown_ctx->total_peers = pg->total;
5548 shutdown_ctx->timeout = timeout;
5549 /* shtudown_ctx->outstanding = 0; */
5551 for (off = 0; off < pg->total; off++)
5553 GNUNET_assert (NULL != pg->peers[off].daemon);
5554 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
5555 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
5556 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
5557 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
5559 if (NULL != pg->peers[off].cfg)
5560 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
5562 conn_iter = pg->peers[off].allowed_peers_head;
5563 while (conn_iter != NULL)
5565 temp_conn = conn_iter->next;
5566 GNUNET_free(conn_iter);
5567 conn_iter = temp_conn;
5570 conn_iter = pg->peers[off].connect_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].blacklisted_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].connect_peers_working_set_head;
5587 while (conn_iter != NULL)
5589 temp_conn = conn_iter->next;
5590 GNUNET_free(conn_iter);
5591 conn_iter = temp_conn;
5594 if (pg->peers[off].allowed_peers != NULL)
5595 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
5596 if (pg->peers[off].connect_peers != NULL)
5597 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
5598 if (pg->peers[off].blacklisted_peers != NULL)
5599 GNUNET_CONTAINER_multihashmap_destroy (pg->
5600 peers[off].blacklisted_peers);
5603 GNUNET_free (pg->peers);
5604 GNUNET_free_non_null(pg->hostkey_data);
5605 for (off = 0; off < pg->num_hosts; off++)
5607 GNUNET_free (pg->hosts[off].hostname);
5608 GNUNET_free_non_null (pg->hosts[off].username);
5610 GNUNET_free_non_null (pg->hosts);
5615 /* end of testing_group.c */