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 /* Maximum time to delay connect attempt */
59 #define MAX_CONNECT_DELAY 300
61 #define MAX_CONCURRENT_HOSTKEYS 500
63 #define MAX_CONCURRENT_STARTING 200
65 #define MAX_CONCURRENT_SHUTDOWN 200
67 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
69 #define CONNECT_ATTEMPTS 12
72 * Which list of peers do we need to modify?
76 /** Modify allowed peers */
79 /** Modify connect peers */
82 /** Modify blacklist peers */
85 /** Modify workingset peers */
90 * Prototype of a function called whenever two peers would be connected
91 * in a certain topology.
93 typedef unsigned int (*GNUNET_TESTING_ConnectionProcessor) (struct
94 GNUNET_TESTING_PeerGroup
100 enum PeerLists list);
104 * Context for handling churning a peer group
109 * Callback used to notify of churning finished
111 GNUNET_TESTING_NotifyCompletion cb;
114 * Closure for callback
119 * Number of peers that still need to be started
121 unsigned int num_to_start;
124 * Number of peers that still need to be stopped
126 unsigned int num_to_stop;
129 * Number of peers that failed to start
131 unsigned int num_failed_start;
134 * Number of peers that failed to stop
136 unsigned int num_failed_stop;
139 struct RestartContext
142 * The group of peers being restarted
144 struct GNUNET_TESTING_PeerGroup *peer_group;
147 * How many peers have been restarted thus far
149 unsigned int peers_restarted;
152 * How many peers got an error when restarting
154 unsigned int peers_restart_failed;
157 * The function to call once all peers have been restarted
159 GNUNET_TESTING_NotifyCompletion callback;
162 * Closure for callback function
169 struct ShutdownContext
172 * Total peers to wait for
174 unsigned int total_peers;
177 * Number of peers successfully shut down
179 unsigned int peers_down;
182 * Number of peers failed to shut down
184 unsigned int peers_failed;
187 * Number of peers we have started shutting
188 * down. If too many, wait on them.
190 unsigned int outstanding;
193 * Timeout for shutdown.
195 struct GNUNET_TIME_Relative timeout;
198 * Callback to call when all peers either
199 * shutdown or failed to shutdown
201 GNUNET_TESTING_NotifyCompletion cb;
210 * Individual shutdown context for a particular peer.
212 struct PeerShutdownContext
215 * Pointer to the high level shutdown context.
217 struct ShutdownContext *shutdown_ctx;
220 * The daemon handle for the peer to shut down.
222 struct GNUNET_TESTING_Daemon *daemon;
226 * Individual shutdown context for a particular peer.
228 struct PeerRestartContext
231 * Pointer to the high level restart context.
233 struct ChurnRestartContext *churn_restart_ctx;
236 * The daemon handle for the peer to shut down.
238 struct GNUNET_TESTING_Daemon *daemon;
242 struct CreateTopologyContext
246 * Function to call with number of connections
248 GNUNET_TESTING_NotifyConnections cont;
251 * Closure for connection notification
258 /** Waiting to read number of peers */
261 /** Should find next peer index */
264 /** Should find colon */
267 /** Should read other peer index, space, or endline */
273 struct PeerConnection
278 struct PeerConnection *prev;
283 struct PeerConnection *next;
287 * Index of daemon in pg->peers
294 struct InternalStartContext
297 * Pointer to peerdata
299 struct PeerData *peer;
302 * Timeout for peer startup
304 struct GNUNET_TIME_Relative timeout;
307 * Client callback for hostkey notification
309 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
312 * Closure for hostkey_callback
317 * Client callback for peer start notification
319 GNUNET_TESTING_NotifyDaemonRunning start_cb;
327 * Hostname, where to start the peer
329 const char *hostname;
332 * Username to use when connecting to the
335 const char *username;
338 * Pointer to starting memory location of a hostkey
343 * Port to use for ssh.
349 struct ChurnRestartContext
352 * Number of restarts currently in flight.
354 unsigned int outstanding;
357 * Handle to the underlying churn context.
359 struct ChurnContext *churn_ctx;
362 * How long to allow the operation to take.
364 struct GNUNET_TIME_Relative timeout;
368 * Data we keep per peer.
373 * (Initial) configuration of the host.
374 * (initial because clients could change
375 * it and we would not know about those
378 struct GNUNET_CONFIGURATION_Handle *cfg;
381 * Handle for controlling the daemon.
383 struct GNUNET_TESTING_Daemon *daemon;
386 * The peergroup this peer belongs to.
388 struct GNUNET_TESTING_PeerGroup *pg;
392 * Linked list of allowed peer connections.
394 struct PeerConnection *allowed_peers_head;
397 * Linked list of allowed peer connections.
399 struct PeerConnection *allowed_peers_tail;
402 * Linked list of blacklisted peer connections.
404 struct PeerConnection *blacklisted_peers_head;
407 * Linked list of blacklisted peer connections.
409 struct PeerConnection *blacklisted_peers_tail;
412 * Linked list of connect peer connections.
414 struct PeerConnection *connect_peers_head;
417 * Linked list of connect peer connections.
419 struct PeerConnection *connect_peers_tail;
422 * Linked list of connect peer connections.
424 struct PeerConnection *connect_peers_working_set_head;
427 * Linked list of connect peer connections.
429 struct PeerConnection *connect_peers_working_set_tail;
433 * Hash map of allowed peer connections (F2F created topology)
435 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
438 * Hash map of blacklisted peers
440 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
443 * Hash map of peer connections
445 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
448 * Temporary hash map of peer connections
450 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
454 * Temporary variable for topology creation, should be reset before
455 * creating any topology so the count is valid once finished.
460 * Context to keep track of peers being started, to
461 * stagger hostkey generation and peer startup.
463 struct InternalStartContext internal_context;
468 * Linked list of per-host data.
478 * SSH username to use when connecting to this host.
483 * SSH port to use when connecting to this host.
488 * Lowest port that we have not yet used
494 struct TopologyIterateContext
497 * Callback for notifying of two connected peers.
499 GNUNET_TESTING_NotifyTopology topology_cb;
502 * Closure for topology_cb
507 * Number of peers currently connected to.
509 unsigned int connected;
512 * Number of peers we have finished iterating.
514 unsigned int completed;
517 * Number of peers total.
522 struct StatsIterateContext
525 * Continuation to call once all stats information has been retrieved.
527 GNUNET_STATISTICS_Callback cont;
530 * Proc function to call on each value received.
532 GNUNET_TESTING_STATISTICS_Iterator proc;
535 * Closure for topology_cb
540 * Number of peers currently connected to.
542 unsigned int connected;
545 * Number of peers we have finished iterating.
547 unsigned int completed;
550 * Number of peers total.
558 struct GNUNET_TESTING_Daemon *daemon;
561 struct StatsCoreContext
564 struct GNUNET_TESTING_Daemon *daemon;
566 * Handle to the statistics service.
568 struct GNUNET_STATISTICS_Handle *stats_handle;
571 * Handle for getting statistics.
573 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
577 * Handle to a group of GNUnet peers.
579 struct GNUNET_TESTING_PeerGroup
582 * Configuration template.
584 const struct GNUNET_CONFIGURATION_Handle *cfg;
587 * Function to call on each started daemon.
589 //GNUNET_TESTING_NotifyDaemonRunning cb;
597 * Function to call on each topology connection created
599 GNUNET_TESTING_NotifyConnection notify_connection;
602 * Callback for notify_connection
604 void *notify_connection_cls;
607 * Array of information about hosts.
609 struct HostData *hosts;
612 * Number of hosts (size of HostData)
614 unsigned int num_hosts;
617 * Array of "total" peers.
619 struct PeerData *peers;
622 * Number of peers in this group.
627 * At what time should we fail the peer startup process?
629 struct GNUNET_TIME_Absolute max_timeout;
632 * How many peers are being started right now?
634 unsigned int starting;
637 * How many peers have already been started?
639 unsigned int started;
642 * Hostkeys loaded from a file.
649 struct GNUNET_CONFIGURATION_Handle *ret;
650 const struct GNUNET_CONFIGURATION_Handle *orig;
651 const char *hostname;
657 struct ConnectTopologyContext
660 * How many connections are left to create.
662 unsigned int remaining_connections;
665 * Handle to group of peers.
667 struct GNUNET_TESTING_PeerGroup *pg;
671 * Temp value set for each iteration.
673 //struct PeerData *first;
676 * Notification that all peers are connected.
678 GNUNET_TESTING_NotifyCompletion notify_connections_done;
681 * Closure for notify.
686 struct ConnectContext
689 * Peer to connect second to.
691 struct GNUNET_TESTING_Daemon *first;
694 * Peer to connect first to.
696 struct GNUNET_TESTING_Daemon *second;
699 * Higher level topology connection context.
701 struct ConnectTopologyContext *ct_ctx;
704 * Whether this connection has been accounted for in the schedule_connect call.
709 struct UnblacklistContext
714 struct GNUNET_TESTING_PeerGroup *pg;
717 * uid of the first peer
727 struct GNUNET_TESTING_PeerGroup *pg;
730 * uid of the first peer
735 * Peer data for first peer.
737 struct PeerData *first;
740 * Random percentage to use
745 struct MinimumContext
750 struct GNUNET_TESTING_PeerGroup *pg;
753 * uid of the first peer
758 * Peer data for first peer.
760 struct PeerData *first;
763 * Number of conns per peer
765 unsigned int num_to_add;
768 * Permuted array of all possible connections. Only add the Nth
769 * peer if it's in the Nth position.
771 unsigned int *pg_array;
774 * What number is the current element we are iterating over?
776 unsigned int current;
784 struct GNUNET_TESTING_PeerGroup *pg;
787 * uid of the first peer
792 * uid of the second peer
797 * Peer data for first peer.
799 struct PeerData *first;
802 * Which peer has been chosen as the one to add?
807 * What number is the current element we are iterating over?
809 unsigned int current;
814 * Convert unique ID to hash code.
816 * @param uid unique ID to convert
817 * @param hash set to uid (extended with zeros)
820 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
822 memset (hash, 0, sizeof (GNUNET_HashCode));
823 *((uint32_t *) hash) = uid;
827 * Convert hash code to unique ID.
829 * @param uid unique ID to convert
830 * @param hash set to uid (extended with zeros)
833 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
835 memcpy (uid, hash, sizeof (uint32_t));
840 * Number of connects we are waiting on, allows us to rate limit
843 static int outstanding_connects;
846 * Get a topology from a string input.
848 * @param topology where to write the retrieved topology
849 * @param topology_string The string to attempt to
850 * get a configuration value from
851 * @return GNUNET_YES if topology string matched a
852 * known topology, GNUNET_NO if not
855 GNUNET_TESTING_topology_get (enum GNUNET_TESTING_Topology *topology,
856 const char *topology_string)
859 * Strings representing topologies in enum
861 static const char *topology_strings[] = {
863 * A clique (everyone connected to everyone else).
868 * Small-world network (2d torus plus random links).
873 * Small-world network (ring plus random links).
893 * Certain percentage of peers are unable to communicate directly
894 * replicating NAT conditions
899 * Scale free topology.
904 * Straight line topology.
909 * All peers are disconnected.
914 * Read the topology from a file.
922 if (topology_string == NULL)
924 while (topology_strings[curr] != NULL)
926 if (strcasecmp (topology_strings[curr], topology_string) == 0)
933 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
939 * Get connect topology option from string input.
941 * @param topology_option where to write the retrieved topology
942 * @param topology_string The string to attempt to
943 * get a configuration value from
944 * @return GNUNET_YES if string matched a known
945 * topology option, GNUNET_NO if not
948 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption
950 const char *topology_string)
953 * Options for connecting a topology as strings.
955 static const char *topology_option_strings[] = {
957 * Try to connect all peers specified in the topology.
962 * Choose a random subset of connections to create.
964 "CONNECT_RANDOM_SUBSET",
967 * Create at least X connections for each peer.
972 * Using a depth first search, create one connection
973 * per peer. If any are missed (graph disconnected)
974 * start over at those peers until all have at least one
980 * Find the N closest peers to each allowed peer in the
981 * topology and make sure a connection to those peers
982 * exists in the connect topology.
987 * No options specified.
995 if (topology_string == NULL)
997 while (NULL != topology_option_strings[curr])
999 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1001 *topology_option = curr;
1006 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1011 * Function to iterate over options. Copies
1012 * the options to the target configuration,
1013 * updating PORT values as needed.
1015 * @param cls closure
1016 * @param section name of the section
1017 * @param option name of the option
1018 * @param value value of the option
1021 update_config (void *cls,
1022 const char *section, const char *option, const char *value)
1024 struct UpdateContext *ctx = cls;
1028 char *single_variable;
1029 char *per_host_variable;
1030 unsigned long long num_per_host;
1032 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1034 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1037 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1040 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
1044 GNUNET_free (single_variable);
1047 if (0 == strcmp (option, "UNIXPATH"))
1049 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1050 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1052 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1055 GNUNET_snprintf (uval,
1057 "/tmp/test-service-%s-%u", section, ctx->upnum++);
1060 else if ((GNUNET_YES ==
1061 GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1063 &num_per_host)) && (num_per_host > 0))
1066 GNUNET_snprintf (uval,
1068 "/tmp/test-service-%s-%u",
1069 section, ctx->fdnum % num_per_host);
1072 GNUNET_free (single_variable);
1073 GNUNET_free (per_host_variable);
1076 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1078 value = ctx->hostname;
1081 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1086 * Create a new configuration using the given configuration
1087 * as a template; however, each PORT in the existing cfg
1088 * must be renumbered by incrementing "*port". If we run
1089 * out of "*port" numbers, return NULL.
1091 * @param cfg template configuration
1092 * @param port port numbers to use, update to reflect
1093 * port numbers that were used
1094 * @param upnum number to make unix domain socket names unique
1095 * @param hostname hostname of the controlling host, to allow control connections from
1096 * @param fdnum number used to offset the unix domain socket for grouped processes
1097 * (such as statistics or peerinfo, which can be shared among others)
1099 * @return new configuration, NULL on error
1101 static struct GNUNET_CONFIGURATION_Handle *
1102 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
1104 uint32_t * upnum, const char *hostname, uint32_t * fdnum)
1106 struct UpdateContext uc;
1109 char *allowed_hosts;
1115 uc.ret = GNUNET_CONFIGURATION_create ();
1116 uc.hostname = hostname;
1119 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1120 if (uc.nport >= HIGH_PORT)
1123 GNUNET_CONFIGURATION_destroy (uc.ret);
1127 if (GNUNET_CONFIGURATION_get_value_string
1128 (cfg, "testing", "control_host", &control_host) == GNUNET_OK)
1130 if (hostname != NULL)
1131 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1134 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1136 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1138 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH",
1140 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1141 "ACCEPT_FROM", allowed_hosts);
1142 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH",
1144 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1146 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH",
1148 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1149 "ACCEPT_FROM", allowed_hosts);
1150 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1152 GNUNET_free_non_null (control_host);
1153 GNUNET_free (allowed_hosts);
1157 /* arm needs to know to allow connections from the host on which it is running,
1158 * otherwise gnunet-arm is unable to connect to it in some instances */
1159 if (hostname != NULL)
1161 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1162 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1163 "BINDTO", hostname);
1164 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1165 "BINDTO", hostname);
1166 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1168 GNUNET_free (allowed_hosts);
1172 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1173 "BINDTO", "127.0.0.1");
1174 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1175 "BINDTO", "127.0.0.1");
1178 *port = (uint16_t) uc.nport;
1186 * Add entries to the some list
1188 * @param pg the peer group we are working with
1189 * @param first index of the first peer
1190 * @param second index of the second peer
1191 * @param list the peer list to use
1193 * @return the number of connections added (can be 0, 1 or 2)
1197 remove_connections (struct GNUNET_TESTING_PeerGroup *pg,
1198 unsigned int first, unsigned int second,
1199 enum PeerLists list)
1203 struct PeerConnection **first_list;
1204 struct PeerConnection **second_list;
1205 struct PeerConnection *first_iter;
1206 struct PeerConnection *second_iter;
1207 struct PeerConnection **first_tail;
1208 struct PeerConnection **second_tail;
1211 GNUNET_HashCode hash_first;
1212 GNUNET_HashCode hash_second;
1214 hash_from_uid (first, &hash_first);
1215 hash_from_uid (second, &hash_second);
1223 first_list = &pg->peers[first].allowed_peers_head;
1224 second_list = &pg->peers[second].allowed_peers_head;
1225 first_tail = &pg->peers[first].allowed_peers_tail;
1226 second_tail = &pg->peers[second].allowed_peers_tail;
1229 first_list = &pg->peers[first].connect_peers_head;
1230 second_list = &pg->peers[second].connect_peers_head;
1231 first_tail = &pg->peers[first].connect_peers_tail;
1232 second_tail = &pg->peers[second].connect_peers_tail;
1235 first_list = &pg->peers[first].blacklisted_peers_head;
1236 second_list = &pg->peers[second].blacklisted_peers_head;
1237 first_tail = &pg->peers[first].blacklisted_peers_tail;
1238 second_tail = &pg->peers[second].blacklisted_peers_tail;
1241 first_list = &pg->peers[first].connect_peers_working_set_head;
1242 second_list = &pg->peers[second].connect_peers_working_set_head;
1243 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1244 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1251 first_iter = *first_list;
1252 while (first_iter != NULL)
1254 if (first_iter->index == second)
1256 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1257 GNUNET_free(first_iter);
1261 first_iter = first_iter->next;
1264 second_iter = *second_list;
1265 while (second_iter != NULL)
1267 if (second_iter->index == first)
1269 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1270 GNUNET_free(second_iter);
1274 second_iter = second_iter->next;
1278 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1281 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1286 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1289 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1298 * Add entries to the some list
1300 * @param pg the peer group we are working with
1301 * @param first index of the first peer
1302 * @param second index of the second peer
1303 * @param list the list type that we should modify
1305 * @return the number of connections added (can be 0, 1 or 2)
1309 add_connections (struct GNUNET_TESTING_PeerGroup *pg,
1310 unsigned int first, unsigned int second,
1311 enum PeerLists list)
1317 struct PeerConnection **first_list;
1318 struct PeerConnection **second_list;
1319 struct PeerConnection *first_iter;
1320 struct PeerConnection *second_iter;
1321 struct PeerConnection *new_first;
1322 struct PeerConnection *new_second;
1323 struct PeerConnection **first_tail;
1324 struct PeerConnection **second_tail;
1326 GNUNET_HashCode hash_first;
1327 GNUNET_HashCode hash_second;
1329 hash_from_uid (first, &hash_first);
1330 hash_from_uid (second, &hash_second);
1337 first_list = &pg->peers[first].allowed_peers_head;
1338 second_list = &pg->peers[second].allowed_peers_head;
1339 first_tail = &pg->peers[first].allowed_peers_tail;
1340 second_tail = &pg->peers[second].allowed_peers_tail;
1343 first_list = &pg->peers[first].connect_peers_head;
1344 second_list = &pg->peers[second].connect_peers_head;
1345 first_tail = &pg->peers[first].connect_peers_tail;
1346 second_tail = &pg->peers[second].connect_peers_tail;
1349 first_list = &pg->peers[first].blacklisted_peers_head;
1350 second_list = &pg->peers[second].blacklisted_peers_head;
1351 first_tail = &pg->peers[first].blacklisted_peers_tail;
1352 second_tail = &pg->peers[second].blacklisted_peers_tail;
1355 first_list = &pg->peers[first].connect_peers_working_set_head;
1356 second_list = &pg->peers[second].connect_peers_working_set_head;
1357 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1358 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1365 add_first = GNUNET_YES;
1366 add_second = GNUNET_YES;
1368 first_iter = *first_list;
1369 while (first_iter != NULL)
1371 if (first_iter->index == second)
1373 add_first = GNUNET_NO;
1376 first_iter = first_iter->next;
1379 second_iter = *second_list;
1380 while (second_iter != NULL)
1382 if (second_iter->index == first)
1384 add_second = GNUNET_NO;
1387 second_iter = second_iter->next;
1391 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1394 add_first = GNUNET_YES;
1398 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1401 add_second = GNUNET_YES;
1409 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1410 new_first->index = second;
1411 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1413 new_first->next = *first_list;
1414 *first_list = new_first;*/
1416 GNUNET_assert (GNUNET_OK ==
1417 GNUNET_CONTAINER_multihashmap_put (pg->
1419 [first].blacklisted_peers,
1422 peers[second].daemon,
1423 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1425 pg->peers[first].num_connections++;
1432 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1433 new_second->index = first;
1434 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1436 new_second->next = *second_list;
1437 *second_list = new_second;
1440 GNUNET_assert (GNUNET_OK ==
1441 GNUNET_CONTAINER_multihashmap_put (pg->
1443 [second].blacklisted_peers,
1446 peers[first].daemon,
1447 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1449 pg->peers[second].num_connections++;
1458 * Scale free network construction as described in:
1460 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1462 * Start with a network of "one" peer, then progressively add
1463 * peers up to the total number. At each step, iterate over
1464 * all possible peers and connect new peer based on number of
1465 * existing connections of the target peer.
1467 * @param pg the peer group we are dealing with
1468 * @param proc the connection processor to use
1469 * @param list the peer list to use
1471 * @return the number of connections created
1474 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg,
1475 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1478 unsigned int total_connections;
1479 unsigned int outer_count;
1481 unsigned int previous_total_connections;
1485 GNUNET_assert (pg->total > 1);
1487 /* Add a connection between the first two nodes */
1488 total_connections = proc (pg, 0, 1, list);
1490 for (outer_count = 1; outer_count < pg->total; outer_count++)
1492 previous_total_connections = total_connections;
1493 for (i = 0; i < outer_count; i++)
1496 pg->peers[i].num_connections /
1497 (double) previous_total_connections;
1500 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1501 UINT64_MAX)) / ((double) UINT64_MAX);
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504 "Considering connecting peer %d to peer %d\n",
1507 if (random < probability)
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511 "Connecting peer %d to peer %d\n", outer_count, i);
1513 total_connections += proc (pg, outer_count, i, list);
1518 return total_connections;
1522 * Create a topology given a peer group (set of running peers)
1523 * and a connection processor. Creates a small world topology
1524 * according to the rewired ring construction. The basic
1525 * behavior is that a ring topology is created, but with some
1526 * probability instead of connecting a peer to the next
1527 * neighbor in the ring a connection will be created to a peer
1528 * selected uniformly at random. We use the TESTING
1529 * PERCENTAGE option to specify what number of
1530 * connections each peer should have. Default is 2,
1531 * which makes the ring, any given number is multiplied by
1532 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1533 * each peer have on average 2logn connections. The additional
1534 * connections are made at increasing distance around the ring
1535 * from the original peer, or to random peers based on the re-
1536 * wiring probability. The TESTING
1537 * PROBABILITY option is used as the probability that a given
1538 * connection is rewired.
1540 * @param pg the peergroup to create the topology on
1541 * @param proc the connection processor to call to actually set
1542 * up connections between two peers
1543 * @param list the peer list to use
1545 * @return the number of connections that were set up
1549 create_small_world_ring (struct GNUNET_TESTING_PeerGroup *pg,
1550 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1554 unsigned int natLog;
1555 unsigned int randomPeer;
1556 double random, logNModifier, probability;
1557 unsigned int smallWorldConnections;
1562 unsigned int useAnd;
1563 int connect_attempts;
1565 logNModifier = 0.5; /* FIXME: default value? */
1566 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1571 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1572 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1574 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1575 p_string, "LOGNMODIFIER", "TESTING");
1576 GNUNET_free (p_string);
1578 probability = 0.5; /* FIXME: default percentage? */
1579 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1584 if (sscanf (p_string, "%lf", &probability) != 1)
1585 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1587 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1588 p_string, "PERCENTAGE", "TESTING");
1589 GNUNET_free (p_string);
1591 natLog = log (pg->total);
1592 connsPerPeer = ceil (natLog * logNModifier);
1594 if (connsPerPeer % 2 == 1)
1597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."), connsPerPeer);
1599 smallWorldConnections = 0;
1600 connect_attempts = 0;
1601 for (i = 0; i < pg->total; i++)
1604 max = i + connsPerPeer / 2;
1605 min = i - connsPerPeer / 2;
1607 if (max > pg->total - 1)
1609 max = max - pg->total;
1615 min = pg->total - 1 + min;
1619 for (j = 0; j < connsPerPeer / 2; j++)
1623 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1624 UINT64_MAX) / ((double) UINT64_MAX));
1625 if (random < probability)
1627 /* Connect to uniformly selected random peer */
1629 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1631 while ((((randomPeer < max) && (randomPeer > min))
1632 && (useAnd == 0)) || (((randomPeer > min)
1633 || (randomPeer < max))
1637 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1640 smallWorldConnections += proc (pg, i, randomPeer, list);
1644 nodeToConnect = i + j + 1;
1645 if (nodeToConnect > pg->total - 1)
1647 nodeToConnect = nodeToConnect - pg->total;
1649 connect_attempts += proc (pg, i, nodeToConnect, list);
1655 connect_attempts += smallWorldConnections;
1657 return connect_attempts;
1661 * Create a topology given a peer group (set of running peers)
1662 * and a connection processor.
1664 * @param pg the peergroup to create the topology on
1665 * @param proc the connection processor to call to actually set
1666 * up connections between two peers
1667 * @param list the peer list to use
1669 * @return the number of connections that were set up
1673 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg,
1674 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1676 unsigned int outer_count, inner_count;
1677 unsigned int cutoff;
1678 int connect_attempts;
1679 double nat_percentage;
1682 nat_percentage = 0.6; /* FIXME: default percentage? */
1683 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1688 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1689 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1691 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1692 p_string, "PERCENTAGE", "TESTING");
1693 GNUNET_free (p_string);
1698 cutoff = (unsigned int) (nat_percentage * pg->total);
1700 connect_attempts = 0;
1702 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1704 for (inner_count = outer_count + 1; inner_count < pg->total;
1707 if ((outer_count > cutoff) || (inner_count > cutoff))
1710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1711 "Connecting peer %d to peer %d\n",
1712 outer_count, inner_count);
1714 connect_attempts += proc (pg, outer_count, inner_count, list);
1719 return connect_attempts;
1724 * Create a topology given a peer group (set of running peers)
1725 * and a connection processor.
1727 * @param pg the peergroup to create the topology on
1728 * @param proc the connection processor to call to actually set
1729 * up connections between two peers
1730 * @param list the peer list to use
1732 * @return the number of connections that were set up
1736 create_small_world (struct GNUNET_TESTING_PeerGroup *pg,
1737 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1739 unsigned int i, j, k;
1740 unsigned int square;
1743 unsigned int toggle = 1;
1744 unsigned int nodeToConnect;
1745 unsigned int natLog;
1746 unsigned int node1Row;
1747 unsigned int node1Col;
1748 unsigned int node2Row;
1749 unsigned int node2Col;
1750 unsigned int distance;
1751 double probability, random, percentage;
1752 unsigned int smallWorldConnections;
1753 unsigned int small_world_it;
1755 int connect_attempts;
1756 square = floor (sqrt (pg->total));
1760 percentage = 0.5; /* FIXME: default percentage? */
1761 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1766 if (sscanf (p_string, "%lf", &percentage) != 1)
1767 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1769 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1770 p_string, "PERCENTAGE", "TESTING");
1771 GNUNET_free (p_string);
1773 if (percentage < 0.0)
1775 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1777 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1778 "PERCENTAGE", "TESTING", percentage);
1781 probability = 0.5; /* FIXME: default percentage? */
1782 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1787 if (sscanf (p_string, "%lf", &probability) != 1)
1788 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1790 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1791 p_string, "PROBABILITY", "TESTING");
1792 GNUNET_free (p_string);
1794 if (square * square != pg->total)
1796 while (rows * cols < pg->total)
1798 if (toggle % 2 == 0)
1807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1809 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1813 connect_attempts = 0;
1814 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1815 * to the node to its right and above. Once this is over, we'll have our torus!
1816 * Special case for the last node (if the rows and columns are not equal), connect
1817 * to the first in the row to maintain topology.
1819 for (i = 0; i < pg->total; i++)
1821 /* First connect to the node to the right */
1822 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1823 nodeToConnect = i + 1;
1824 else if (i + 1 == pg->total)
1825 nodeToConnect = rows * cols - cols;
1827 nodeToConnect = i - cols + 1;
1829 connect_attempts += proc (pg, i, nodeToConnect, list);
1833 nodeToConnect = (rows * cols) - cols + i;
1834 if (nodeToConnect >= pg->total)
1835 nodeToConnect -= cols;
1838 nodeToConnect = i - cols;
1840 if (nodeToConnect < pg->total)
1841 connect_attempts += proc (pg, i, nodeToConnect, list);
1843 natLog = log (pg->total);
1844 #if VERBOSE_TESTING > 2
1845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1846 _("natural log of %d is %d, will run %d iterations\n"),
1847 pg->total, natLog, (int) (natLog * percentage));
1848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1849 _("Total connections added thus far: %u!\n"), connect_attempts);
1851 smallWorldConnections = 0;
1852 small_world_it = (unsigned int) (natLog * percentage);
1853 if (small_world_it < 1)
1855 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
1856 for (i = 0; i < small_world_it; i++)
1858 for (j = 0; j < pg->total; j++)
1860 /* Determine the row and column of node at position j on the 2d torus */
1861 node1Row = j / cols;
1862 node1Col = j - (node1Row * cols);
1863 for (k = 0; k < pg->total; k++)
1865 /* Determine the row and column of node at position k on the 2d torus */
1866 node2Row = k / cols;
1867 node2Col = k - (node2Row * cols);
1868 /* Simple Cartesian distance */
1870 abs (node1Row - node2Row) + abs (node1Col - node2Col);
1873 /* Calculate probability as 1 over the square of the distance */
1874 probability = 1.0 / (distance * distance);
1875 /* Choose a random value between 0 and 1 */
1878 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1880 ((double) UINT64_MAX);
1881 /* If random < probability, then connect the two nodes */
1882 if (random < probability)
1883 smallWorldConnections += proc (pg, j, k, list);
1889 connect_attempts += smallWorldConnections;
1890 #if VERBOSE_TESTING > 2
1891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1892 _("Total connections added for small world: %d!\n"),
1893 smallWorldConnections);
1895 return connect_attempts;
1899 * Create a topology given a peer group (set of running peers)
1900 * and a connection processor.
1902 * @param pg the peergroup to create the topology on
1903 * @param proc the connection processor to call to actually set
1904 * up connections between two peers
1905 * @param list the peer list to use
1907 * @return the number of connections that were set up
1911 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg,
1912 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1915 unsigned int outer_count;
1916 unsigned int inner_count;
1917 int connect_attempts;
1921 probability = 0.5; /* FIXME: default percentage? */
1922 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1927 if (sscanf (p_string, "%lf", &probability) != 1)
1928 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1930 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1931 p_string, "PROBABILITY", "TESTING");
1932 GNUNET_free (p_string);
1934 connect_attempts = 0;
1935 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1937 for (inner_count = outer_count + 1; inner_count < pg->total;
1942 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1943 UINT64_MAX)) / ((double) UINT64_MAX);
1945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1946 _("rand is %f probability is %f\n"), temp_rand,
1949 if (temp_rand < probability)
1951 connect_attempts += proc (pg, outer_count, inner_count, list);
1956 return connect_attempts;
1960 * Create a topology given a peer group (set of running peers)
1961 * and a connection processor. This particular function creates
1962 * the connections for a 2d-torus, plus additional "closest"
1963 * connections per peer.
1965 * @param pg the peergroup to create the topology on
1966 * @param proc the connection processor to call to actually set
1967 * up connections between two peers
1968 * @param list the peer list to use
1970 * @return the number of connections that were set up
1974 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg,
1975 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1978 unsigned int square;
1981 unsigned int toggle = 1;
1982 unsigned int nodeToConnect;
1983 int connect_attempts;
1985 connect_attempts = 0;
1987 square = floor (sqrt (pg->total));
1991 if (square * square != pg->total)
1993 while (rows * cols < pg->total)
1995 if (toggle % 2 == 0)
2004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2006 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2009 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2010 * to the node to its right and above. Once this is over, we'll have our torus!
2011 * Special case for the last node (if the rows and columns are not equal), connect
2012 * to the first in the row to maintain topology.
2014 for (i = 0; i < pg->total; i++)
2016 /* First connect to the node to the right */
2017 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2018 nodeToConnect = i + 1;
2019 else if (i + 1 == pg->total)
2020 nodeToConnect = rows * cols - cols;
2022 nodeToConnect = i - cols + 1;
2024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2025 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2027 connect_attempts += proc (pg, i, nodeToConnect, list);
2029 /* Second connect to the node immediately above */
2032 nodeToConnect = (rows * cols) - cols + i;
2033 if (nodeToConnect >= pg->total)
2034 nodeToConnect -= cols;
2037 nodeToConnect = i - cols;
2039 if (nodeToConnect < pg->total)
2042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2043 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2045 connect_attempts += proc (pg, i, nodeToConnect, list);
2050 return connect_attempts;
2055 * Create a topology given a peer group (set of running peers)
2056 * and a connection processor.
2058 * @param pg the peergroup to create the topology on
2059 * @param proc the connection processor to call to actually set
2060 * up connections between two peers
2061 * @param list the peer list to use
2063 * @return the number of connections that were set up
2067 create_clique (struct GNUNET_TESTING_PeerGroup *pg,
2068 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2070 unsigned int outer_count;
2071 unsigned int inner_count;
2072 int connect_attempts;
2074 connect_attempts = 0;
2076 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2078 for (inner_count = outer_count + 1; inner_count < pg->total;
2082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2083 "Connecting peer %d to peer %d\n",
2084 outer_count, inner_count);
2086 connect_attempts += proc (pg, outer_count, inner_count, list);
2090 return connect_attempts;
2095 * Iterator over hash map entries.
2097 * @param cls closure the peer group
2098 * @param key the key stored in the hashmap is the
2099 * index of the peer to connect to
2100 * @param value value in the hash map, handle to the peer daemon
2101 * @return GNUNET_YES if we should continue to
2106 unblacklist_iterator (void *cls,
2107 const GNUNET_HashCode * key,
2110 struct UnblacklistContext *un_ctx = cls;
2111 uint32_t second_pos;
2113 uid_from_hash (key, &second_pos);
2115 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2122 * Create a blacklist topology based on the allowed topology
2123 * which disallows any connections not in the allowed topology
2124 * at the transport level.
2126 * @param pg the peergroup to create the topology on
2127 * @param proc the connection processor to call to allow
2128 * up connections between two peers
2130 * @return the number of connections that were set up
2134 copy_allowed (struct GNUNET_TESTING_PeerGroup *pg,
2135 GNUNET_TESTING_ConnectionProcessor proc)
2137 struct UnblacklistContext un_ctx;
2140 struct PeerConnection *iter;
2144 for (count = 0; count < pg->total - 1; count++)
2146 un_ctx.first_uid = count;
2148 iter = pg->peers[count].allowed_peers_head;
2149 while (iter != NULL)
2151 remove_connections(pg, count, iter->index, BLACKLIST);
2152 //unblacklist_connections(pg, count, iter->index);
2156 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers, &unblacklist_iterator, &un_ctx);
2159 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2164 * Create a topology given a peer group (set of running peers)
2165 * and a connection processor.
2167 * @param pg the peergroup to create the topology on
2168 * @param proc the connection processor to call to actually set
2169 * up connections between two peers
2170 * @param list which list should be modified
2172 * @return the number of connections that were set up
2176 create_line (struct GNUNET_TESTING_PeerGroup *pg,
2177 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2180 int connect_attempts;
2182 connect_attempts = 0;
2184 /* Connect each peer to the next highest numbered peer */
2185 for (count = 0; count < pg->total - 1; count++)
2188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2189 "Connecting peer %d to peer %d\n", count, count + 1);
2191 connect_attempts += proc (pg, count, count + 1, list);
2194 return connect_attempts;
2198 * Create a topology given a peer group (set of running peers)
2199 * and a connection processor.
2201 * @param pg the peergroup to create the topology on
2202 * @param filename the file to read topology information from
2203 * @param proc the connection processor to call to actually set
2204 * up connections between two peers
2205 * @param list the peer list to use
2207 * @return the number of connections that were set up
2211 create_from_file (struct GNUNET_TESTING_PeerGroup *pg,
2213 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2215 int connect_attempts;
2216 unsigned int first_peer_index;
2217 unsigned int second_peer_index;
2218 connect_attempts = 0;
2223 unsigned int total_peers;
2225 enum States curr_state;
2227 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2228 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2230 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2233 "Could not open file `%s' specified for topology!", filename);
2234 return connect_attempts;
2237 data = GNUNET_malloc_large (frstat.st_size);
2238 GNUNET_assert(data != NULL);
2239 if (frstat.st_size !=
2240 GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2243 "Could not read file %s specified for host list, ending test!", filename);
2245 return connect_attempts;
2250 /* First line should contain a single integer, specifying the number of peers */
2251 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2252 curr_state = NUM_PEERS;
2253 while (count < frstat.st_size - 1)
2255 if ((buf[count] == '\n') || (buf[count] == ' '))
2264 if (1 != sscanf(&buf[count], "%u", &total_peers))
2266 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read number of peers from topology file!\n");
2267 GNUNET_free_non_null(data);
2268 return connect_attempts;
2270 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read %u total peers in topology\n", total_peers);
2271 GNUNET_assert(total_peers == pg->total);
2272 curr_state = PEER_INDEX;
2273 while((buf[count] != '\n') && (count < frstat.st_size - 1))
2278 if (1 != sscanf(&buf[count], "%u", &first_peer_index))
2280 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read peer index from topology file!\n");
2281 GNUNET_free_non_null(data);
2282 return connect_attempts;
2284 while((buf[count] != ':') && (count < frstat.st_size - 1))
2287 curr_state = OTHER_PEER_INDEX;
2290 if (1 == sscanf(&buf[count], ":"))
2291 curr_state = OTHER_PEER_INDEX;
2294 case OTHER_PEER_INDEX:
2295 if (1 != sscanf(&buf[count], "%u", &second_peer_index))
2297 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to peer index from topology file!\n");
2298 GNUNET_free_non_null(data);
2299 return connect_attempts;
2301 /* Assume file is written with first peer 1, but array index is 0 */
2302 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index - 1, list);
2303 while((buf[count] != '\n') && (buf[count] != ',') && (count < frstat.st_size - 1))
2305 if (buf[count] == '\n')
2307 curr_state = PEER_INDEX;
2309 else if (buf[count] != ',')
2311 curr_state = OTHER_PEER_INDEX;
2316 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Found bad data in topology file while in state %d!\n", curr_state);
2318 return connect_attempts;
2323 /* Connect each peer to the next highest numbered peer */
2324 for (count = 0; count < pg->total - 1; count++)
2327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2328 "Connecting peer %d to peer %d\n", first_peer_index, second_peer_index);
2330 connect_attempts += proc (pg, first_peer_index, second_peer_index);
2333 return connect_attempts;
2337 * Create a topology given a peer group (set of running peers)
2338 * and a connection processor.
2340 * @param pg the peergroup to create the topology on
2341 * @param proc the connection processor to call to actually set
2342 * up connections between two peers
2343 * @param list the peer list to use
2345 * @return the number of connections that were set up
2349 create_ring (struct GNUNET_TESTING_PeerGroup *pg,
2350 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2353 int connect_attempts;
2355 connect_attempts = 0;
2357 /* Connect each peer to the next highest numbered peer */
2358 for (count = 0; count < pg->total - 1; count++)
2361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2362 "Connecting peer %d to peer %d\n", count, count + 1);
2364 connect_attempts += proc (pg, count, count + 1, list);
2367 /* Connect the last peer to the first peer */
2368 connect_attempts += proc (pg, pg->total - 1, 0, list);
2370 return connect_attempts;
2375 * Iterator for writing friends of a peer to a file.
2377 * @param cls closure, an open writable file handle
2378 * @param key the key the daemon was stored under
2379 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2381 * @return GNUNET_YES to continue iteration
2383 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2384 * with a single file_iterator that takes a closure which contains
2385 * the prefix to write before the peer. Then this could be used
2386 * for blacklisting multiple transports and writing the friend
2387 * file. I'm sure *someone* will complain loudly about other
2388 * things that negate these functions even existing so no point in
2392 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2394 FILE *temp_friend_handle = cls;
2395 struct GNUNET_TESTING_Daemon *peer = value;
2396 struct GNUNET_PeerIdentity *temppeer;
2397 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2399 temppeer = &peer->id;
2400 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2401 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2406 struct BlacklistContext
2409 * The (open) file handle to write to
2411 FILE *temp_file_handle;
2414 * The transport that this peer will be blacklisted on.
2420 * Iterator for writing blacklist data to appropriate files.
2422 * @param cls closure, an open writable file handle
2423 * @param key the key the daemon was stored under
2424 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2426 * @return GNUNET_YES to continue iteration
2429 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2431 struct BlacklistContext *blacklist_ctx = cls;
2432 struct GNUNET_TESTING_Daemon *peer = value;
2433 struct GNUNET_PeerIdentity *temppeer;
2434 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2436 temppeer = &peer->id;
2437 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2438 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2439 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2440 blacklist_ctx->transport, (char *) &peer_enc);
2448 * Create the friend files based on the PeerConnection's
2449 * of each peer in the peer group, and copy the files
2450 * to the appropriate place
2452 * @param pg the peer group we are dealing with
2455 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
2457 FILE *temp_friend_handle;
2458 unsigned int pg_iter;
2459 char *temp_service_path;
2460 struct GNUNET_OS_Process **procarr;
2463 enum GNUNET_OS_ProcessStatusType type;
2464 unsigned long return_code;
2469 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2470 struct PeerConnection *conn_iter;
2472 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2473 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2475 mytemp = GNUNET_DISK_mktemp ("friends");
2476 GNUNET_assert (mytemp != NULL);
2477 temp_friend_handle = fopen (mytemp, "wt");
2478 GNUNET_assert (temp_friend_handle != NULL);
2480 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2481 while (conn_iter != NULL)
2483 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
2484 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2485 conn_iter = conn_iter->next;
2488 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2489 &friend_file_iterator,
2490 temp_friend_handle);
2492 fclose (temp_friend_handle);
2495 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2496 daemon->cfg, "PATHS",
2498 &temp_service_path))
2500 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2502 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2503 "SERVICEHOME", "PATHS");
2504 if (UNLINK (mytemp) != 0)
2505 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2507 GNUNET_free (mytemp);
2511 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2513 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2514 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2519 _("Copying file with command cp %s %s\n"), mytemp, arg);
2524 else /* Remote, scp the file to the correct place */
2526 if (NULL != pg->peers[pg_iter].daemon->username)
2527 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2528 pg->peers[pg_iter].daemon->username,
2529 pg->peers[pg_iter].daemon->hostname,
2532 GNUNET_asprintf (&arg, "%s:%s/friends",
2533 pg->peers[pg_iter].daemon->hostname,
2536 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2541 _("Copying file with command scp %s %s\n"), mytemp,
2546 GNUNET_free (temp_service_path);
2547 GNUNET_free (mytemp);
2551 ret = GNUNET_SYSERR;
2552 while ((count < max_wait) && (ret != GNUNET_OK))
2555 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2559 _("Checking copy status of file %d\n"), pg_iter);
2561 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2563 if (GNUNET_OS_process_status
2564 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2566 ret = GNUNET_SYSERR;
2568 else if ((type != GNUNET_OS_PROCESS_EXITED)
2569 || (return_code != 0))
2571 ret = GNUNET_SYSERR;
2575 GNUNET_OS_process_close (procarr[pg_iter]);
2576 procarr[pg_iter] = NULL;
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2579 _("File %d copied\n"), pg_iter);
2585 if (ret == GNUNET_SYSERR)
2587 /* FIXME: why sleep here? -CG */
2593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2594 _("Finished copying all friend files!\n"));
2596 GNUNET_free (procarr);
2602 * Create the blacklist files based on the PeerConnection's
2603 * of each peer in the peer group, and copy the files
2604 * to the appropriate place.
2606 * @param pg the peer group we are dealing with
2607 * @param transports space delimited list of transports to blacklist
2610 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg,
2611 const char *transports)
2613 FILE *temp_file_handle;
2614 unsigned int pg_iter;
2615 char *temp_service_path;
2616 struct GNUNET_OS_Process **procarr;
2619 enum GNUNET_OS_ProcessStatusType type;
2620 unsigned long return_code;
2627 char *temp_transports;
2630 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2631 struct PeerConnection *conn_iter;
2633 static struct BlacklistContext blacklist_ctx;
2636 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2637 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2639 mytemp = GNUNET_DISK_mktemp ("blacklist");
2640 GNUNET_assert (mytemp != NULL);
2641 temp_file_handle = fopen (mytemp, "wt");
2642 GNUNET_assert (temp_file_handle != NULL);
2643 temp_transports = GNUNET_strdup (transports);
2645 blacklist_ctx.temp_file_handle = temp_file_handle;
2647 transport_len = strlen (temp_transports) + 1;
2650 for (i = 0; i < transport_len; i++)
2652 if ((temp_transports[i] == ' ') && (pos == NULL))
2653 continue; /* At start of string (whitespace) */
2654 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2656 temp_transports[i] = '\0';
2658 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
2659 while (conn_iter != NULL)
2661 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
2662 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
2663 conn_iter = conn_iter->next;
2667 blacklist_ctx.transport = pos;
2668 entry_count = GNUNET_CONTAINER_multihashmap_iterate (pg->
2670 [pg_iter].blacklisted_peers,
2671 &blacklist_file_iterator,
2675 } /* At beginning of actual string */
2676 else if (pos == NULL)
2678 pos = &temp_transports[i];
2682 GNUNET_free (temp_transports);
2683 fclose (temp_file_handle);
2686 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2687 daemon->cfg, "PATHS",
2689 &temp_service_path))
2691 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2693 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2694 "SERVICEHOME", "PATHS");
2695 if (UNLINK (mytemp) != 0)
2696 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2698 GNUNET_free (mytemp);
2702 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2704 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2705 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2710 _("Copying file with command cp %s %s\n"), mytemp, arg);
2715 else /* Remote, scp the file to the correct place */
2717 if (NULL != pg->peers[pg_iter].daemon->username)
2718 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
2719 pg->peers[pg_iter].daemon->username,
2720 pg->peers[pg_iter].daemon->hostname,
2723 GNUNET_asprintf (&arg, "%s:%s/blacklist",
2724 pg->peers[pg_iter].daemon->hostname,
2727 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2732 _("Copying file with command scp %s %s\n"), mytemp,
2737 GNUNET_free (temp_service_path);
2738 GNUNET_free (mytemp);
2742 ret = GNUNET_SYSERR;
2743 while ((count < max_wait) && (ret != GNUNET_OK))
2746 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2750 _("Checking copy status of file %d\n"), pg_iter);
2752 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2754 if (GNUNET_OS_process_status
2755 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2757 ret = GNUNET_SYSERR;
2759 else if ((type != GNUNET_OS_PROCESS_EXITED)
2760 || (return_code != 0))
2762 ret = GNUNET_SYSERR;
2766 GNUNET_OS_process_close (procarr[pg_iter]);
2767 procarr[pg_iter] = NULL;
2769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2770 _("File %d copied\n"), pg_iter);
2776 if (ret == GNUNET_SYSERR)
2778 /* FIXME: why sleep here? -CG */
2784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2785 _("Finished copying all blacklist files!\n"));
2787 GNUNET_free (procarr);
2793 * Internal notification of a connection, kept so that we can ensure some connections
2794 * happen instead of flooding all testing daemons with requests to connect.
2797 internal_connect_notify (void *cls,
2798 const struct GNUNET_PeerIdentity *first,
2799 const struct GNUNET_PeerIdentity *second,
2801 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2802 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2803 struct GNUNET_TESTING_Daemon *first_daemon,
2804 struct GNUNET_TESTING_Daemon *second_daemon,
2807 struct ConnectTopologyContext *ct_ctx = cls;
2808 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
2809 outstanding_connects--;
2810 ct_ctx->remaining_connections--;
2811 if (ct_ctx->remaining_connections == 0)
2813 if (ct_ctx->notify_connections_done != NULL)
2814 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
2815 GNUNET_free (ct_ctx);
2818 if (pg->notify_connection != NULL)
2819 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
2820 first_cfg, second_cfg, first_daemon, second_daemon,
2826 * Either delay a connection (because there are too many outstanding)
2827 * or schedule it for right now.
2829 * @param cls a connection context
2830 * @param tc the task runtime context
2833 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2835 struct ConnectContext *connect_context = cls;
2837 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2840 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2842 #if VERBOSE_TESTING > 2
2843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2845 ("Delaying connect, we have too many outstanding connections!\n"));
2847 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2848 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
2849 &schedule_connect, connect_context);
2853 #if VERBOSE_TESTING > 2
2854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2855 _("Creating connection, outstanding_connections is %d\n"),
2856 outstanding_connects);
2858 outstanding_connects++;
2859 GNUNET_TESTING_daemons_connect (connect_context->first,
2860 connect_context->second,
2863 &internal_connect_notify,
2864 connect_context->ct_ctx);
2865 GNUNET_free (connect_context);
2871 * Iterator for actually scheduling connections to be created
2872 * between two peers.
2874 * @param cls closure, a GNUNET_TESTING_Daemon
2875 * @param key the key the second Daemon was stored under
2876 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2878 * @return GNUNET_YES to continue iteration
2881 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2883 struct ConnectTopologyContext *ct_ctx = cls;
2884 struct PeerData *first = ct_ctx->first;
2885 struct GNUNET_TESTING_Daemon *second = value;
2886 struct ConnectContext *connect_context;
2888 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
2889 connect_context->first = first->daemon;
2890 connect_context->second = second;
2891 connect_context->ct_ctx = ct_ctx;
2892 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
2900 * Iterator for copying all entries in the allowed hashmap to the
2903 * @param cls closure, a GNUNET_TESTING_Daemon
2904 * @param key the key the second Daemon was stored under
2905 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2907 * @return GNUNET_YES to continue iteration
2910 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2912 struct PeerData *first = cls;
2914 GNUNET_assert (GNUNET_OK ==
2915 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
2917 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2924 * Make the peers to connect the same as those that are allowed to be
2927 * @param pg the peer group
2930 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2932 unsigned int pg_iter;
2936 struct PeerConnection *iter;
2940 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2943 iter = pg->peers[pg_iter].allowed_peers_head;
2944 while (iter != NULL)
2946 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating connection between %d and %d\n", pg_iter, iter->index);
2947 total += add_connections(pg, pg_iter, iter->index, CONNECT);
2948 //total += add_actual_connections(pg, pg_iter, iter->index);
2953 GNUNET_CONTAINER_multihashmap_iterate (pg->
2954 peers[pg_iter].allowed_peers,
2955 ©_topology_iterator,
2956 &pg->peers[pg_iter]);
2958 if (GNUNET_SYSERR == ret)
2959 return GNUNET_SYSERR;
2961 total = total + ret;
2969 * Connect the topology as specified by the PeerConnection's
2970 * of each peer in the peer group
2972 * @param pg the peer group we are dealing with
2973 * @param notify_callback callback to notify when finished
2974 * @param notify_cls closure for notify callback
2976 * @return the number of connections that will be attempted
2979 connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2980 GNUNET_TESTING_NotifyCompletion notify_callback,
2983 unsigned int pg_iter;
2985 struct ConnectTopologyContext *ct_ctx;
2987 struct PeerConnection *connection_iter;
2988 struct ConnectContext *connect_context;
2994 ct_ctx = GNUNET_malloc (sizeof (struct ConnectTopologyContext));
2995 ct_ctx->notify_connections_done = notify_callback;
2996 ct_ctx->notify_cls = notify_cls;
2999 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3002 connection_iter = pg->peers[pg_iter].connect_peers_head;
3003 while (connection_iter != NULL)
3005 connection_iter = connection_iter->next;
3010 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3016 GNUNET_free (ct_ctx);
3019 ct_ctx->remaining_connections = total;
3022 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3025 connection_iter = pg->peers[pg_iter].connect_peers_head;
3026 while (connection_iter != NULL)
3028 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling connect of peer %d to peer %d\n", pg_iter, connection_iter->index);
3029 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3030 connect_context->first = pg->peers[pg_iter].daemon;
3031 connect_context->second = pg->peers[connection_iter->index].daemon;
3032 connect_context->ct_ctx = ct_ctx;
3033 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3034 connection_iter = connection_iter->next;
3039 GNUNET_CONTAINER_multihashmap_iterate (pg->
3040 peers[pg_iter].connect_peers,
3041 &connect_iterator, ct_ctx);
3042 GNUNET_assert (GNUNET_SYSERR != ret && ret >= 0);
3043 total = total + ret;
3051 * Takes a peer group and creates a topology based on the
3052 * one specified. Creates a topology means generates friend
3053 * files for the peers so they can only connect to those allowed
3054 * by the topology. This will only have an effect once peers
3055 * are started if the FRIENDS_ONLY option is set in the base
3056 * config. Also takes an optional restrict topology which
3057 * disallows connections based on particular transports
3058 * UNLESS they are specified in the restricted topology.
3060 * @param pg the peer group struct representing the running peers
3061 * @param topology which topology to connect the peers in
3062 * @param restrict_topology disallow restrict_transports transport
3063 * connections to peers NOT in this topology
3064 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3065 * @param restrict_transports space delimited list of transports to blacklist
3066 * to create restricted topology
3068 * @return the maximum number of connections were all allowed peers
3069 * connected to each other
3072 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
3073 enum GNUNET_TESTING_Topology topology,
3074 enum GNUNET_TESTING_Topology
3076 const char *restrict_transports)
3080 unsigned int num_connections;
3081 int unblacklisted_connections;
3086 for (i = 0; i < pg->total; i++)
3088 pg->peers[i].allowed_peers =
3089 GNUNET_CONTAINER_multihashmap_create (100);
3090 pg->peers[i].connect_peers =
3091 GNUNET_CONTAINER_multihashmap_create (100);
3092 pg->peers[i].blacklisted_peers =
3093 GNUNET_CONTAINER_multihashmap_create (100);
3094 pg->peers[i].pg = pg;
3100 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3104 num_connections = create_clique (pg, &add_connections, ALLOWED);
3106 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3109 _("Creating small world (ring) topology\n"));
3112 create_small_world_ring (pg, &add_connections, ALLOWED);
3114 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3117 _("Creating small world (2d-torus) topology\n"));
3119 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3121 case GNUNET_TESTING_TOPOLOGY_RING:
3123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3125 num_connections = create_ring (pg, &add_connections, ALLOWED);
3127 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3131 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3133 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3136 _("Creating Erdos-Renyi topology\n"));
3138 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3140 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3144 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3146 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3149 _("Creating Scale Free topology\n"));
3151 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3153 case GNUNET_TESTING_TOPOLOGY_LINE:
3155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3156 _("Creating straight line topology\n"));
3158 num_connections = create_line (pg, &add_connections, ALLOWED);
3160 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3163 _("Creating topology from file!\n"));
3166 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing", "topology_file",
3168 num_connections = create_from_file (pg, filename, &add_connections, ALLOWED);
3171 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3172 num_connections = 0;
3175 case GNUNET_TESTING_TOPOLOGY_NONE:
3177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3179 ("Creating no allowed topology (all peers can connect at core level)\n"));
3181 num_connections = pg->total * pg->total; /* Clique is allowed! */
3184 num_connections = 0;
3189 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
3191 ret = create_and_copy_friend_files (pg);
3192 if (ret != GNUNET_OK)
3195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3196 _("Failed during friend file copying!\n"));
3198 return GNUNET_SYSERR;
3203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3204 _("Friend files created/copied successfully!\n"));
3209 /* Use the create clique method to initially set all connections as blacklisted. */
3210 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
3211 create_clique (pg, &add_connections, BLACKLIST);
3213 unblacklisted_connections = 0;
3214 /* Un-blacklist connections as per the topology specified */
3215 switch (restrict_topology)
3217 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3220 _("Blacklisting all but clique topology\n"));
3222 unblacklisted_connections =
3223 create_clique (pg, &remove_connections, BLACKLIST);
3225 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3228 _("Blacklisting all but small world (ring) topology\n"));
3230 unblacklisted_connections =
3231 create_small_world_ring (pg, &remove_connections, BLACKLIST);
3233 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3237 ("Blacklisting all but small world (2d-torus) topology\n"));
3239 unblacklisted_connections =
3240 create_small_world (pg, &remove_connections, BLACKLIST);
3242 case GNUNET_TESTING_TOPOLOGY_RING:
3244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3245 _("Blacklisting all but ring topology\n"));
3247 unblacklisted_connections = create_ring (pg, &remove_connections, BLACKLIST);
3249 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3252 _("Blacklisting all but 2d torus topology\n"));
3254 unblacklisted_connections =
3255 create_2d_torus (pg, &remove_connections, BLACKLIST);
3257 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3260 _("Blacklisting all but Erdos-Renyi topology\n"));
3262 unblacklisted_connections =
3263 create_erdos_renyi (pg, &remove_connections, BLACKLIST);
3265 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3268 _("Blacklisting all but InterNAT topology\n"));
3270 unblacklisted_connections =
3271 create_nated_internet (pg, &remove_connections, BLACKLIST);
3273 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3276 _("Blacklisting all but Scale Free topology\n"));
3278 unblacklisted_connections =
3279 create_scale_free (pg, &remove_connections, BLACKLIST);
3281 case GNUNET_TESTING_TOPOLOGY_LINE:
3283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3284 _("Blacklisting all but straight line topology\n"));
3286 unblacklisted_connections = create_line (pg, &remove_connections, BLACKLIST);
3288 case GNUNET_TESTING_TOPOLOGY_NONE: /* Fall through */
3289 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3293 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3297 ("Creating blacklist topology from allowed\n"));
3298 unblacklisted_connections = copy_allowed (pg, &remove_connections);
3303 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
3305 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n", restrict_transports);
3306 ret = create_and_copy_blacklist_files (pg, restrict_transports);
3307 if (ret != GNUNET_OK)
3310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3311 _("Failed during blacklist file copying!\n"));
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3319 _("Blacklist files created/copied successfully!\n"));
3323 return num_connections;
3329 * Iterator for choosing random peers to connect.
3331 * @param cls closure, a RandomContext
3332 * @param key the key the second Daemon was stored under
3333 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3335 * @return GNUNET_YES to continue iteration
3338 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3340 struct RandomContext *random_ctx = cls;
3341 double random_number;
3342 uint32_t second_pos;
3343 GNUNET_HashCode first_hash;
3346 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3347 UINT64_MAX)) / ((double) UINT64_MAX);
3348 if (random_number < random_ctx->percentage)
3350 GNUNET_assert (GNUNET_OK ==
3351 GNUNET_CONTAINER_multihashmap_put (random_ctx->
3352 first->connect_peers_working_set,
3354 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3357 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
3358 uid_from_hash (key, &second_pos);
3359 hash_from_uid (random_ctx->first_uid, &first_hash);
3360 GNUNET_assert (random_ctx->pg->total > second_pos);
3361 GNUNET_assert (GNUNET_YES ==
3362 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
3364 [second_pos].connect_peers,
3374 * Iterator for adding at least X peers to a peers connection set.
3376 * @param cls closure, MinimumContext
3377 * @param key the key the second Daemon was stored under
3378 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3380 * @return GNUNET_YES to continue iteration
3383 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3385 struct MinimumContext *min_ctx = cls;
3386 uint32_t second_pos;
3387 GNUNET_HashCode first_hash;
3390 if (GNUNET_CONTAINER_multihashmap_size
3391 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
3393 for (i = 0; i < min_ctx->num_to_add; i++)
3395 if (min_ctx->pg_array[i] == min_ctx->current)
3397 GNUNET_assert (GNUNET_OK ==
3398 GNUNET_CONTAINER_multihashmap_put
3399 (min_ctx->first->connect_peers_working_set, key,
3401 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3402 uid_from_hash (key, &second_pos);
3403 hash_from_uid (min_ctx->first_uid, &first_hash);
3404 GNUNET_assert (min_ctx->pg->total > second_pos);
3405 GNUNET_assert (GNUNET_OK ==
3406 GNUNET_CONTAINER_multihashmap_put (min_ctx->
3408 [second_pos].connect_peers_working_set,
3412 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3413 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
3414 GNUNET_assert (GNUNET_YES ==
3415 GNUNET_CONTAINER_multihashmap_remove
3416 (min_ctx->pg->peers[second_pos].connect_peers,
3417 &first_hash, min_ctx->first->daemon));
3424 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
3430 * Iterator for adding peers to a connection set based on a depth first search.
3432 * @param cls closure, MinimumContext
3433 * @param key the key the second daemon was stored under
3434 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3436 * @return GNUNET_YES to continue iteration
3439 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3441 struct DFSContext *dfs_ctx = cls;
3442 GNUNET_HashCode first_hash;
3444 if (dfs_ctx->current == dfs_ctx->chosen)
3446 GNUNET_assert (GNUNET_OK ==
3447 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3448 first->connect_peers_working_set,
3450 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3451 uid_from_hash (key, &dfs_ctx->second_uid);
3452 hash_from_uid (dfs_ctx->first_uid, &first_hash);
3453 GNUNET_assert (GNUNET_OK ==
3454 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3456 [dfs_ctx->second_uid].connect_peers_working_set,
3460 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3461 GNUNET_assert (GNUNET_YES ==
3462 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
3464 [dfs_ctx->second_uid].connect_peers,
3468 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
3469 return GNUNET_NO; /* We have found our peer, don't iterate more */
3478 * From the set of connections possible, choose percentage percent of connections
3479 * to actually connect.
3481 * @param pg the peergroup we are dealing with
3482 * @param percentage what percent of total connections to make
3485 choose_random_connections (struct GNUNET_TESTING_PeerGroup *pg,
3488 struct RandomContext random_ctx;
3491 struct PeerConnection *temp_peers;
3492 struct PeerConnection *conn_iter;
3493 double random_number;
3496 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3498 random_ctx.first_uid = pg_iter;
3499 random_ctx.first = &pg->peers[pg_iter];
3500 random_ctx.percentage = percentage;
3504 conn_iter = pg->peers[pg_iter].connect_peers_head;
3505 while (conn_iter != NULL)
3509 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3510 UINT64_MAX)) / ((double) UINT64_MAX);
3511 if (random_number < percentage)
3513 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
3515 conn_iter = conn_iter->next;
3518 pg->peers[pg_iter].connect_peers_working_set =
3519 GNUNET_CONTAINER_multihashmap_create (pg->total);
3520 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3521 &random_connect_iterator,
3523 /* Now remove the old connections */
3524 GNUNET_CONTAINER_multihashmap_destroy (pg->
3525 peers[pg_iter].connect_peers);
3526 /* And replace with the random set */
3527 pg->peers[pg_iter].connect_peers =
3528 pg->peers[pg_iter].connect_peers_working_set;
3532 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3534 conn_iter = pg->peers[pg_iter].connect_peers_head;
3535 while (pg->peers[pg_iter].connect_peers_head != NULL)
3536 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
3538 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
3539 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
3540 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
3541 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
3547 * Count the number of connections in a linked list of connections.
3549 * @param conn_list the connection list to get the count of
3551 * @return the number of elements in the list
3554 count_connections (struct PeerConnection *conn_list)
3556 struct PeerConnection *iter;
3560 while (iter != NULL)
3570 count_workingset_connections (struct GNUNET_TESTING_PeerGroup *pg)
3573 unsigned int pg_iter;
3575 struct PeerConnection *conn_iter;
3579 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3582 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
3583 while (conn_iter != NULL)
3586 conn_iter = conn_iter->next;
3590 GNUNET_CONTAINER_multihashmap_size (pg->
3592 [pg_iter].connect_peers_working_set);
3601 count_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg)
3604 unsigned int pg_iter;
3606 struct PeerConnection *conn_iter;
3610 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3613 conn_iter = pg->peers[pg_iter].allowed_peers_head;
3614 while (conn_iter != NULL)
3617 conn_iter = conn_iter->next;
3621 GNUNET_CONTAINER_multihashmap_size (pg->
3623 [pg_iter].allowed_peers);
3632 * From the set of connections possible, choose at least num connections per
3635 * @param pg the peergroup we are dealing with
3636 * @param num how many connections at least should each peer have (if possible)?
3639 choose_minimum (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3642 struct MinimumContext minimum_ctx;
3644 struct PeerConnection *conn_iter;
3645 unsigned int temp_list_size;
3648 uint32_t random; /* Random list entry to connect peer to */
3653 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3655 temp_list_size = count_connections(pg->peers[pg_iter].connect_peers_head);
3656 if (temp_list_size == 0)
3658 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer %d has 0 connections!?!?\n", pg_iter);
3661 for (i = 0; i < num; i++)
3663 random = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, temp_list_size);
3664 conn_iter = pg->peers[pg_iter].connect_peers_head;
3665 for (count = 0; count < random; count++)
3666 conn_iter = conn_iter->next;
3667 /* We now have a random connection, connect it! */
3668 GNUNET_assert(conn_iter != NULL);
3669 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
3673 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3675 pg->peers[pg_iter].connect_peers_working_set =
3676 GNUNET_CONTAINER_multihashmap_create (num);
3679 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3681 minimum_ctx.first_uid = pg_iter;
3682 minimum_ctx.pg_array =
3683 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
3684 GNUNET_CONTAINER_multihashmap_size
3685 (pg->peers[pg_iter].connect_peers));
3686 minimum_ctx.first = &pg->peers[pg_iter];
3687 minimum_ctx.pg = pg;
3688 minimum_ctx.num_to_add = num;
3689 minimum_ctx.current = 0;
3690 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3691 &minimum_connect_iterator,
3695 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3697 /* Remove the "old" connections */
3698 GNUNET_CONTAINER_multihashmap_destroy (pg->
3699 peers[pg_iter].connect_peers);
3700 /* And replace with the working set */
3701 pg->peers[pg_iter].connect_peers =
3702 pg->peers[pg_iter].connect_peers_working_set;
3705 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3707 while (pg->peers[pg_iter].connect_peers_head != NULL)
3708 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
3710 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
3711 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
3712 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
3713 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
3718 struct FindClosestContext
3721 * The currently known closest peer.
3723 struct GNUNET_TESTING_Daemon *closest;
3726 * The info for the peer we are adding connections for.
3728 struct PeerData *curr_peer;
3731 * The distance (bits) between the current
3732 * peer and the currently known closest.
3734 unsigned int closest_dist;
3737 * The offset of the closest known peer in
3740 unsigned int closest_num;
3744 * Iterator over hash map entries of the allowed
3745 * peer connections. Find the closest, not already
3746 * connected peer and return it.
3748 * @param cls closure (struct FindClosestContext)
3749 * @param key current key code (hash of offset in pg)
3750 * @param value value in the hash map - a GNUNET_TESTING_Daemon
3751 * @return GNUNET_YES if we should continue to
3756 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
3758 struct FindClosestContext *closest_ctx = cls;
3759 struct GNUNET_TESTING_Daemon *daemon = value;
3761 if (((closest_ctx->closest == NULL) ||
3762 (GNUNET_CRYPTO_hash_matching_bits
3763 (&daemon->id.hashPubKey,
3764 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
3765 closest_ctx->closest_dist))
3767 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
3768 curr_peer->connect_peers,
3771 closest_ctx->closest_dist =
3772 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
3773 &closest_ctx->curr_peer->daemon->
3775 closest_ctx->closest = daemon;
3776 uid_from_hash (key, &closest_ctx->closest_num);
3783 * From the set of connections possible, choose at num connections per
3784 * peer based on depth which are closest out of those allowed. Guaranteed
3785 * to add num peers to connect to, provided there are that many peers
3786 * in the underlay topology to connect to.
3788 * @param pg the peergroup we are dealing with
3789 * @param num how many connections at least should each peer have (if possible)?
3790 * @param proc processor to actually add the connections
3791 * @param list the peer list to use
3794 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
3795 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
3800 struct FindClosestContext closest_ctx;
3805 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
3807 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3809 closest_ctx.curr_peer = &pg->peers[pg_iter];
3810 closest_ctx.closest = NULL;
3811 closest_ctx.closest_dist = 0;
3812 closest_ctx.closest_num = 0;
3813 GNUNET_CONTAINER_multihashmap_iterate (pg->
3814 peers[pg_iter].allowed_peers,
3815 &find_closest_peers,
3817 if (closest_ctx.closest != NULL)
3819 GNUNET_assert (closest_ctx.closest_num < pg->total);
3820 proc (pg, pg_iter, closest_ctx.closest_num, list);
3828 * From the set of connections possible, choose at least num connections per
3829 * peer based on depth first traversal of peer connections. If DFS leaves
3830 * peers unconnected, ensure those peers get connections.
3832 * @param pg the peergroup we are dealing with
3833 * @param num how many connections at least should each peer have (if possible)?
3836 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3840 uint32_t starting_peer;
3841 uint32_t least_connections;
3842 uint32_t random_connection;
3844 unsigned int temp_count;
3845 struct PeerConnection *peer_iter;
3847 struct DFSContext dfs_ctx;
3848 GNUNET_HashCode second_hash;
3854 while ((count_workingset_connections (pg) < num * pg->total)
3855 && (count_allowed_connections (pg) > 0))
3857 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3859 least_connections = -1; /* Set to very high number */
3860 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3862 temp_count = count_connections(pg->peers[pg_iter].connect_peers_working_set_head);
3863 if (temp_count < least_connections)
3865 starting_peer = pg_iter;
3866 least_connections = temp_count;
3871 temp_count = count_connections(pg->peers[starting_peer].connect_peers_head);
3872 if (temp_count == 0)
3873 continue; /* FIXME: infinite loop? */
3875 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, temp_count);
3877 peer_iter = pg->peers[starting_peer].connect_peers_head;
3878 while (temp_count < random_connection)
3880 peer_iter = peer_iter->next;
3883 GNUNET_assert(peer_iter != NULL);
3884 add_connections(pg, starting_peer, peer_iter->index, WORKING_SET);
3885 remove_connections(pg, starting_peer, peer_iter->index, CONNECT);
3886 starting_peer = peer_iter->index;
3891 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3893 pg->peers[pg_iter].connect_peers_working_set =
3894 GNUNET_CONTAINER_multihashmap_create (num);
3899 while ((count_workingset_connections (pg) < num * pg->total)
3900 && (count_allowed_connections (pg) > 0))
3902 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3904 least_connections = -1; /* Set to very high number */
3905 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3907 if (GNUNET_CONTAINER_multihashmap_size
3908 (pg->peers[pg_iter].connect_peers_working_set) <
3911 starting_peer = pg_iter;
3913 GNUNET_CONTAINER_multihashmap_size (pg->
3915 [pg_iter].connect_peers_working_set);
3920 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
3926 /* Choose a random peer from the chosen peers set of connections to add */
3928 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3929 GNUNET_CONTAINER_multihashmap_size
3930 (pg->peers[starting_peer].connect_peers));
3931 dfs_ctx.first_uid = starting_peer;
3932 dfs_ctx.first = &pg->peers[starting_peer];
3934 dfs_ctx.current = 0;
3936 GNUNET_CONTAINER_multihashmap_iterate (pg->
3938 [starting_peer].connect_peers,
3939 &dfs_connect_iterator, &dfs_ctx);
3940 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
3941 hash_from_uid (dfs_ctx.second_uid, &second_hash);
3942 GNUNET_assert (GNUNET_YES ==
3943 GNUNET_CONTAINER_multihashmap_remove (pg->peers
3944 [starting_peer].connect_peers,
3948 [dfs_ctx.second_uid].daemon));
3949 starting_peer = dfs_ctx.second_uid;
3952 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3954 /* Remove the "old" connections */
3955 GNUNET_CONTAINER_multihashmap_destroy (pg->
3956 peers[pg_iter].connect_peers);
3957 /* And replace with the working set */
3958 pg->peers[pg_iter].connect_peers =
3959 pg->peers[pg_iter].connect_peers_working_set;
3966 * Internal callback for topology information for a particular peer.
3969 internal_topology_callback (void *cls,
3970 const struct GNUNET_PeerIdentity *peer,
3971 const struct GNUNET_TRANSPORT_ATS_Information
3974 struct CoreContext *core_ctx = cls;
3975 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
3977 if (peer == NULL) /* Either finished, or something went wrong */
3979 iter_ctx->completed++;
3980 iter_ctx->connected--;
3981 /* One core context allocated per iteration, must free! */
3982 GNUNET_free (core_ctx);
3986 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id,
3990 if (iter_ctx->completed == iter_ctx->total)
3992 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
3993 /* Once all are done, free the iteration context */
3994 GNUNET_free (iter_ctx);
4000 * Check running topology iteration tasks, if below max start a new one, otherwise
4001 * schedule for some time in the future.
4004 schedule_get_topology (void *cls,
4005 const struct GNUNET_SCHEDULER_TaskContext *tc)
4007 struct CoreContext *core_context = cls;
4008 struct TopologyIterateContext *topology_context =
4009 (struct TopologyIterateContext *) core_context->iter_context;
4010 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4013 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
4015 #if VERBOSE_TESTING > 2
4016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4018 ("Delaying connect, we have too many outstanding connections!\n"));
4020 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4021 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4022 &schedule_get_topology, core_context);
4026 #if VERBOSE_TESTING > 2
4027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4028 _("Creating connection, outstanding_connections is %d\n"),
4029 outstanding_connects);
4031 topology_context->connected++;
4034 GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4035 &internal_topology_callback,
4038 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4039 internal_topology_callback (core_context, NULL, NULL);
4045 * Iterate over all (running) peers in the peer group, retrieve
4046 * all connections that each currently has.
4049 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
4050 GNUNET_TESTING_NotifyTopology cb, void *cls)
4052 struct TopologyIterateContext *topology_context;
4053 struct CoreContext *core_ctx;
4055 unsigned int total_count;
4057 /* Allocate a single topology iteration context */
4058 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4059 topology_context->topology_cb = cb;
4060 topology_context->cls = cls;
4062 for (i = 0; i < pg->total; i++)
4064 if (pg->peers[i].daemon->running == GNUNET_YES)
4066 /* Allocate one core context per core we need to connect to */
4067 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4068 core_ctx->daemon = pg->peers[i].daemon;
4069 /* Set back pointer to topology iteration context */
4070 core_ctx->iter_context = topology_context;
4071 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4075 if (total_count == 0)
4077 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4078 GNUNET_free (topology_context);
4081 topology_context->total = total_count;
4086 * Callback function to process statistic values.
4087 * This handler is here only really to insert a peer
4088 * identity (or daemon) so the statistics can be uniquely
4089 * tied to a single running peer.
4091 * @param cls closure
4092 * @param subsystem name of subsystem that created the statistic
4093 * @param name the name of the datum
4094 * @param value the current value
4095 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4096 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4099 internal_stats_callback (void *cls,
4100 const char *subsystem,
4101 const char *name, uint64_t value, int is_persistent)
4103 struct StatsCoreContext *core_context = cls;
4104 struct StatsIterateContext *stats_context =
4105 (struct StatsIterateContext *) core_context->iter_context;
4107 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4108 subsystem, name, value, is_persistent);
4112 * Internal continuation call for statistics iteration.
4114 * @param cls closure, the CoreContext for this iteration
4115 * @param success whether or not the statistics iterations
4116 * was canceled or not (we don't care)
4119 internal_stats_cont (void *cls, int success)
4121 struct StatsCoreContext *core_context = cls;
4122 struct StatsIterateContext *stats_context =
4123 (struct StatsIterateContext *) core_context->iter_context;
4125 stats_context->connected--;
4126 stats_context->completed++;
4128 if (stats_context->completed == stats_context->total)
4130 stats_context->cont (stats_context->cls, GNUNET_YES);
4131 GNUNET_free (stats_context);
4134 if (core_context->stats_handle != NULL)
4135 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4137 GNUNET_free (core_context);
4141 * Check running topology iteration tasks, if below max start a new one, otherwise
4142 * schedule for some time in the future.
4145 schedule_get_statistics (void *cls,
4146 const struct GNUNET_SCHEDULER_TaskContext *tc)
4148 struct StatsCoreContext *core_context = cls;
4149 struct StatsIterateContext *stats_context =
4150 (struct StatsIterateContext *) core_context->iter_context;
4152 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4155 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
4157 #if VERBOSE_TESTING > 2
4158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4160 ("Delaying connect, we have too many outstanding connections!\n"));
4162 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4163 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4164 &schedule_get_statistics, core_context);
4168 #if VERBOSE_TESTING > 2
4169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4170 _("Creating connection, outstanding_connections is %d\n"),
4171 outstanding_connects);
4174 stats_context->connected++;
4175 core_context->stats_handle =
4176 GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
4177 if (core_context->stats_handle == NULL)
4179 internal_stats_cont (core_context, GNUNET_NO);
4183 core_context->stats_get_handle =
4184 GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
4185 GNUNET_TIME_relative_get_forever (),
4186 &internal_stats_cont, &internal_stats_callback,
4188 if (core_context->stats_get_handle == NULL)
4189 internal_stats_cont (core_context, GNUNET_NO);
4194 struct DuplicateStats
4197 * Next item in the list
4199 struct DuplicateStats *next;
4202 * Nasty string, concatenation of relevant information.
4204 char *unique_string;
4208 * Check whether the combination of port/host/unix domain socket
4209 * already exists in the list of peers being checked for statistics.
4211 * @param pg the peergroup in question
4212 * @param specific_peer the peer we're concerned with
4213 * @param stats_list the list to return to the caller
4215 * @return GNUNET_YES if the statistics instance has been seen already,
4216 * GNUNET_NO if not (and we may have added it to the list)
4219 stats_check_existing (struct GNUNET_TESTING_PeerGroup *pg,
4220 struct PeerData *specific_peer,
4221 struct DuplicateStats **stats_list)
4223 struct DuplicateStats *pos;
4224 char *unix_domain_socket;
4225 unsigned long long port;
4228 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
4229 "single_statistics_per_host"))
4230 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
4234 GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg, "statistics",
4235 "unixpath", &unix_domain_socket))
4239 GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg, "statistics",
4242 GNUNET_free(unix_domain_socket);
4246 if (specific_peer->daemon->hostname != NULL)
4247 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
4248 unix_domain_socket, port);
4250 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
4254 if (0 == strcmp (to_match, pos->unique_string))
4256 GNUNET_free (unix_domain_socket);
4257 GNUNET_free (to_match);
4262 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
4263 pos->unique_string = to_match;
4264 pos->next = *stats_list;
4266 GNUNET_free (unix_domain_socket);
4271 * Iterate over all (running) peers in the peer group, retrieve
4272 * all statistics from each.
4275 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
4276 GNUNET_STATISTICS_Callback cont,
4277 GNUNET_TESTING_STATISTICS_Iterator proc,
4280 struct StatsIterateContext *stats_context;
4281 struct StatsCoreContext *core_ctx;
4283 unsigned int total_count;
4284 struct DuplicateStats *stats_list;
4285 struct DuplicateStats *pos;
4288 /* Allocate a single stats iteration context */
4289 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
4290 stats_context->cont = cont;
4291 stats_context->proc = proc;
4292 stats_context->cls = cls;
4295 for (i = 0; i < pg->total; i++)
4297 if ((pg->peers[i].daemon->running == GNUNET_YES)
4299 stats_check_existing (pg, &pg->peers[i], &stats_list)))
4301 /* Allocate one core context per core we need to connect to */
4302 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
4303 core_ctx->daemon = pg->peers[i].daemon;
4304 /* Set back pointer to topology iteration context */
4305 core_ctx->iter_context = stats_context;
4306 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
4311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4312 "Retrieving stats from %u total instances.\n", total_count);
4313 stats_context->total = total_count;
4314 if (stats_list != NULL)
4319 GNUNET_free (pos->unique_string);
4320 stats_list = pos->next;
4322 pos = stats_list->next;
4329 * There are many ways to connect peers that are supported by this function.
4330 * To connect peers in the same topology that was created via the
4331 * GNUNET_TESTING_create_topology, the topology variable must be set to
4332 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
4333 * a new instance of that topology will be generated and attempted to be
4334 * connected. This could result in some connections being impossible,
4335 * because some topologies are non-deterministic.
4337 * @param pg the peer group struct representing the running peers
4338 * @param topology which topology to connect the peers in
4339 * @param options options for connecting the topology
4340 * @param option_modifier modifier for options that take a parameter
4341 * @param notify_callback notification to be called once all connections completed
4342 * @param notify_cls closure for notification callback
4344 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
4347 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
4348 enum GNUNET_TESTING_Topology topology,
4349 enum GNUNET_TESTING_TopologyOption options,
4350 double option_modifier,
4351 GNUNET_TESTING_NotifyCompletion
4352 notify_callback, void *notify_cls)
4356 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4357 #if VERBOSE_TOPOLOGY
4358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4359 _("Creating clique CONNECT topology\n"));
4361 create_clique (pg, &add_connections, CONNECT);
4363 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4364 #if VERBOSE_TOPOLOGY
4365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4366 _("Creating small world (ring) CONNECT topology\n"));
4368 create_small_world_ring (pg, &add_connections, CONNECT);
4370 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4371 #if VERBOSE_TOPOLOGY
4372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4373 _("Creating small world (2d-torus) CONNECT topology\n"));
4375 create_small_world (pg, &add_connections, CONNECT);
4377 case GNUNET_TESTING_TOPOLOGY_RING:
4378 #if VERBOSE_TOPOLOGY
4379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4380 _("Creating ring CONNECT topology\n"));
4382 create_ring (pg, &add_connections, CONNECT);
4384 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4385 #if VERBOSE_TOPOLOGY
4386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4387 _("Creating 2d torus CONNECT topology\n"));
4389 create_2d_torus (pg, &add_connections, CONNECT);
4391 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4392 #if VERBOSE_TOPOLOGY
4393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4394 _("Creating Erdos-Renyi CONNECT topology\n"));
4396 create_erdos_renyi (pg, &add_connections, CONNECT);
4398 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4399 #if VERBOSE_TOPOLOGY
4400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4401 _("Creating InterNAT CONNECT topology\n"));
4403 create_nated_internet (pg, &add_connections, CONNECT);
4405 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4406 #if VERBOSE_TOPOLOGY
4407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4408 _("Creating Scale Free CONNECT topology\n"));
4410 create_scale_free (pg, &add_connections, CONNECT);
4412 case GNUNET_TESTING_TOPOLOGY_LINE:
4413 #if VERBOSE_TOPOLOGY
4414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4415 _("Creating straight line CONNECT topology\n"));
4417 create_line (pg, &add_connections, CONNECT);
4419 case GNUNET_TESTING_TOPOLOGY_NONE:
4420 #if VERBOSE_TOPOLOGY
4421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4422 _("Creating no CONNECT topology\n"));
4424 copy_allowed_topology (pg);
4427 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4429 ("Unknown topology specification, can't connect peers!\n"));
4430 return GNUNET_SYSERR;
4435 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
4436 #if VERBOSE_TOPOLOGY
4437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4439 ("Connecting random subset (%'.2f percent) of possible peers\n"),
4440 100 * option_modifier);
4442 choose_random_connections (pg, option_modifier);
4444 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
4445 #if VERBOSE_TOPOLOGY
4446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4447 _("Connecting a minimum of %u peers each (if possible)\n"),
4448 (unsigned int) option_modifier);
4450 choose_minimum (pg, (unsigned int) option_modifier);
4452 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
4453 #if VERBOSE_TOPOLOGY
4454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4456 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
4457 (unsigned int) option_modifier);
4460 perform_dfs (pg, (int) option_modifier);
4463 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
4464 #if VERBOSE_TOPOLOGY
4465 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4467 ("Finding additional %u closest peers each (if possible)\n"),
4468 (unsigned int) option_modifier);
4471 add_closest (pg, (unsigned int) option_modifier,
4472 &add_connections, CONNECT);
4475 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
4477 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
4483 return connect_topology (pg, notify_callback, notify_cls);
4487 * Callback that is called whenever a hostkey is generated
4488 * for a peer. Call the real callback and decrement the
4489 * starting counter for the peergroup.
4491 * @param cls closure
4492 * @param id identifier for the daemon, NULL on error
4493 * @param d handle for the daemon
4494 * @param emsg error message (NULL on success)
4497 internal_hostkey_callback (void *cls,
4498 const struct GNUNET_PeerIdentity *id,
4499 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4501 struct InternalStartContext *internal_context = cls;
4502 internal_context->peer->pg->starting--;
4503 internal_context->peer->pg->started++;
4504 if (internal_context->hostkey_callback != NULL)
4505 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
4507 else if (internal_context->peer->pg->started ==
4508 internal_context->peer->pg->total)
4510 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
4511 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
4516 * Callback that is called whenever a peer has finished starting.
4517 * Call the real callback and decrement the starting counter
4518 * for the peergroup.
4520 * @param cls closure
4521 * @param id identifier for the daemon, NULL on error
4523 * @param d handle for the daemon
4524 * @param emsg error message (NULL on success)
4527 internal_startup_callback (void *cls,
4528 const struct GNUNET_PeerIdentity *id,
4529 const struct GNUNET_CONFIGURATION_Handle *cfg,
4530 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4532 struct InternalStartContext *internal_context = cls;
4533 internal_context->peer->pg->starting--;
4534 if (internal_context->start_cb != NULL)
4535 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
4540 internal_continue_startup (void *cls,
4541 const struct GNUNET_SCHEDULER_TaskContext *tc)
4543 struct InternalStartContext *internal_context = cls;
4545 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4550 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
4552 internal_context->peer->pg->starting++;
4553 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
4557 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4558 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4559 &internal_continue_startup,
4566 * Callback for informing us about a successful
4567 * or unsuccessful churn start call.
4569 * @param cls a ChurnContext
4570 * @param id the peer identity of the started peer
4571 * @param cfg the handle to the configuration of the peer
4572 * @param d handle to the daemon for the peer
4573 * @param emsg NULL on success, non-NULL on failure
4577 churn_start_callback (void *cls,
4578 const struct GNUNET_PeerIdentity *id,
4579 const struct GNUNET_CONFIGURATION_Handle *cfg,
4580 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4582 struct ChurnRestartContext *startup_ctx = cls;
4583 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
4585 unsigned int total_left;
4586 char *error_message;
4588 error_message = NULL;
4591 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4592 "Churn stop callback failed with error `%s'\n", emsg);
4593 churn_ctx->num_failed_start++;
4597 churn_ctx->num_to_start--;
4601 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4602 "Started peer, %d left.\n", churn_ctx->num_to_start);
4606 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
4607 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4609 if (total_left == 0)
4611 if ((churn_ctx->num_failed_stop > 0)
4612 || (churn_ctx->num_failed_start > 0))
4613 GNUNET_asprintf (&error_message,
4614 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4615 churn_ctx->num_failed_start,
4616 churn_ctx->num_failed_stop);
4617 churn_ctx->cb (churn_ctx->cb_cls, error_message);
4618 GNUNET_free_non_null (error_message);
4619 GNUNET_free (churn_ctx);
4620 GNUNET_free (startup_ctx);
4626 schedule_churn_restart (void *cls,
4627 const struct GNUNET_SCHEDULER_TaskContext *tc)
4629 struct PeerRestartContext *peer_restart_ctx = cls;
4630 struct ChurnRestartContext *startup_ctx =
4631 peer_restart_ctx->churn_restart_ctx;
4633 if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
4634 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4635 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4636 &schedule_churn_restart, peer_restart_ctx);
4639 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
4640 startup_ctx->timeout,
4641 &churn_start_callback,
4643 GNUNET_free (peer_restart_ctx);
4648 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4650 struct InternalStartContext *internal_context = cls;
4652 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4657 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
4659 internal_context->peer->pg->starting++;
4660 internal_context->peer->daemon =
4661 GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
4662 internal_context->timeout,
4663 internal_context->hostname,
4664 internal_context->username,
4665 internal_context->sshport,
4666 internal_context->hostkey,
4667 &internal_hostkey_callback,
4669 &internal_startup_callback,
4674 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4675 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4676 &internal_start, internal_context);
4681 * Function which continues a peer group starting up
4682 * after successfully generating hostkeys for each peer.
4684 * @param pg the peer group to continue starting
4688 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg)
4693 for (i = 0; i < pg->total; i++)
4695 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
4696 &pg->peers[i].internal_context);
4697 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
4702 * Start count gnunet instances with the same set of transports and
4703 * applications. The port numbers (any option called "PORT") will be
4704 * adjusted to ensure that no two peers running on the same system
4705 * have the same port(s) in their respective configurations.
4707 * @param cfg configuration template to use
4708 * @param total number of daemons to start
4709 * @param timeout total time allowed for peers to start
4710 * @param hostkey_callback function to call on each peers hostkey generation
4711 * if NULL, peers will be started by this call, if non-null,
4712 * GNUNET_TESTING_daemons_continue_startup must be called after
4713 * successful hostkey generation
4714 * @param hostkey_cls closure for hostkey callback
4715 * @param cb function to call on each daemon that was started
4716 * @param cb_cls closure for cb
4717 * @param connect_callback function to call each time two hosts are connected
4718 * @param connect_callback_cls closure for connect_callback
4719 * @param hostnames linked list of host structs to use to start peers on
4720 * (NULL to run on localhost only)
4722 * @return NULL on error, otherwise handle to control peer group
4724 struct GNUNET_TESTING_PeerGroup *
4725 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
4727 struct GNUNET_TIME_Relative timeout,
4728 GNUNET_TESTING_NotifyHostkeyCreated
4729 hostkey_callback, void *hostkey_cls,
4730 GNUNET_TESTING_NotifyDaemonRunning cb,
4732 GNUNET_TESTING_NotifyConnection
4733 connect_callback, void *connect_callback_cls,
4734 const struct GNUNET_TESTING_Host *hostnames)
4736 struct GNUNET_TESTING_PeerGroup *pg;
4737 const struct GNUNET_TESTING_Host *hostpos;
4743 const char *hostname;
4744 const char *username;
4745 char *baseservicehome;
4746 char *newservicehome;
4748 char *hostkeys_file;
4751 struct GNUNET_DISK_FileHandle *fd;
4752 struct GNUNET_CONFIGURATION_Handle *pcfg;
4754 unsigned int hostcnt;
4761 uint64_t total_hostkeys;
4762 struct GNUNET_OS_Process *proc;
4772 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
4774 pg->notify_connection = connect_callback;
4775 pg->notify_connection_cls = connect_callback_cls;
4777 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
4778 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
4779 if (NULL != hostnames)
4782 hostpos = hostnames;
4783 while (hostpos != NULL)
4785 hostpos = hostpos->next;
4788 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4791 hostpos = hostnames;
4792 while (hostpos != NULL)
4794 pg->hosts[off].minport = LOW_PORT;
4795 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
4796 if (hostpos->username != NULL)
4797 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
4798 pg->hosts[off].sshport = hostpos->port;
4799 hostpos = hostpos->next;
4809 pg->num_hosts = off;
4813 /* skip leading spaces */
4814 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
4817 while ('\0' != *rpos)
4819 if (isspace ((unsigned char) *rpos))
4823 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4825 start = GNUNET_strdup (hostnames);
4827 while ('\0' != *pos)
4829 if (isspace ((unsigned char) *pos))
4832 if (strlen (start) > 0)
4834 pg->hosts[off].minport = LOW_PORT;
4835 pg->hosts[off++].hostname = start;
4841 if (strlen (start) > 0)
4843 pg->hosts[off].minport = LOW_PORT;
4844 pg->hosts[off++].hostname = start;
4848 GNUNET_free (start);
4849 GNUNET_free (pg->hosts);
4853 minport = 0; /* make gcc happy */
4862 /* Create the servicehome directory for each remote peer */
4863 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
4865 for (i = 0; i < pg->num_hosts; i++)
4868 if (NULL != pg->hosts[i].username)
4869 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username, pg->hosts[i].hostname);
4871 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
4872 if (pg->hosts[i].sshport != 0)
4874 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
4875 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
4876 "ssh", "-P", ssh_port_str,
4880 arg, "mkdir -p", baseservicehome, NULL);
4883 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
4884 "ssh", arg, "mkdir -p", baseservicehome, NULL);
4885 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating remote dir with command ssh %s %s %s\n", arg, " mkdir -p ", baseservicehome);
4886 GNUNET_OS_process_wait(proc);
4888 GNUNET_free(baseservicehome);
4890 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "HOSTKEYSFILE",
4893 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
4894 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!\n");
4897 /* Check hostkey file size, read entire thing into memory */
4898 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
4899 GNUNET_DISK_PERM_NONE);
4902 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
4906 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
4909 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Found file size %llu for hostkeys, expect hostkeys to be size %d\n", fs, HOSTKEYFILESIZE);
4911 if (fs % HOSTKEYFILESIZE != 0)
4913 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "File size %llu seems incorrect for hostkeys...\n", fs);
4917 total_hostkeys = fs / HOSTKEYFILESIZE;
4918 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Will read %llu hostkeys from file\n", total_hostkeys);
4919 pg->hostkey_data = GNUNET_malloc_large (fs);
4920 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
4921 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
4924 GNUNET_free(hostkeys_file);
4927 for (off = 0; off < total; off++)
4931 hostname = pg->hosts[off % hostcnt].hostname;
4932 username = pg->hosts[off % hostcnt].username;
4933 sshport = pg->hosts[off % hostcnt].sshport;
4934 pcfg = make_config (cfg,
4935 &pg->hosts[off % hostcnt].minport,
4936 &upnum, hostname, &fdnum);
4943 pcfg = make_config (cfg, &minport, &upnum, hostname, &fdnum);
4948 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4950 ("Could not create configuration for peer number %u on `%s'!\n"),
4951 off, hostname == NULL ? "localhost" : hostname);
4956 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
4959 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
4960 GNUNET_free (baseservicehome);
4964 tmpdir = getenv ("TMPDIR");
4965 tmpdir = tmpdir ? tmpdir : "/tmp";
4966 GNUNET_asprintf (&newservicehome,
4968 tmpdir, "gnunet-testing-test-test", off);
4970 GNUNET_CONFIGURATION_set_value_string (pcfg,
4972 "SERVICEHOME", newservicehome);
4973 GNUNET_free (newservicehome);
4974 pg->peers[off].cfg = pcfg;
4976 /* Can we do this later? */
4977 pg->peers[off].allowed_peers =
4978 GNUNET_CONTAINER_multihashmap_create (total);
4979 pg->peers[off].connect_peers =
4980 GNUNET_CONTAINER_multihashmap_create (total);
4981 pg->peers[off].blacklisted_peers =
4982 GNUNET_CONTAINER_multihashmap_create (total);
4985 pg->peers[off].pg = pg;
4986 pg->peers[off].internal_context.peer = &pg->peers[off];
4987 pg->peers[off].internal_context.timeout = timeout;
4988 pg->peers[off].internal_context.hostname = hostname;
4989 pg->peers[off].internal_context.username = username;
4990 pg->peers[off].internal_context.sshport = sshport;
4991 if (pg->hostkey_data != NULL)
4992 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off * HOSTKEYFILESIZE];
4993 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
4994 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
4995 pg->peers[off].internal_context.start_cb = cb;
4996 pg->peers[off].internal_context.start_cb_cls = cb_cls;
4998 GNUNET_SCHEDULER_add_now (&internal_start,
4999 &pg->peers[off].internal_context);
5006 * Get a daemon by number, so callers don't have to do nasty
5007 * offsetting operation.
5009 struct GNUNET_TESTING_Daemon *
5010 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
5011 unsigned int position)
5013 if (position < pg->total)
5014 return pg->peers[position].daemon;
5020 * Get a daemon by peer identity, so callers can
5021 * retrieve the daemon without knowing it's offset.
5023 * @param pg the peer group to retrieve the daemon from
5024 * @param peer_id the peer identity of the daemon to retrieve
5026 * @return the daemon on success, or NULL if no such peer identity is found
5028 struct GNUNET_TESTING_Daemon *
5029 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
5030 struct GNUNET_PeerIdentity *peer_id)
5034 for (i = 0; i < pg->total; i++)
5037 memcmp (&pg->peers[i].daemon->id, peer_id,
5038 sizeof (struct GNUNET_PeerIdentity)))
5039 return pg->peers[i].daemon;
5046 * Prototype of a function that will be called when a
5047 * particular operation was completed the testing library.
5049 * @param cls closure (a struct RestartContext)
5050 * @param id id of the peer that was restarted
5051 * @param cfg handle to the configuration of the peer
5052 * @param d handle to the daemon that was restarted
5053 * @param emsg NULL on success
5056 restart_callback (void *cls,
5057 const struct GNUNET_PeerIdentity *id,
5058 const struct GNUNET_CONFIGURATION_Handle *cfg,
5059 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5061 struct RestartContext *restart_context = cls;
5065 restart_context->peers_restarted++;
5069 restart_context->peers_restart_failed++;
5072 if (restart_context->peers_restarted == restart_context->peer_group->total)
5074 restart_context->callback (restart_context->callback_cls, NULL);
5075 GNUNET_free (restart_context);
5077 else if (restart_context->peers_restart_failed +
5078 restart_context->peers_restarted ==
5079 restart_context->peer_group->total)
5081 restart_context->callback (restart_context->callback_cls,
5082 "Failed to restart peers!");
5083 GNUNET_free (restart_context);
5089 * Callback for informing us about a successful
5090 * or unsuccessful churn stop call.
5092 * @param cls a ChurnContext
5093 * @param emsg NULL on success, non-NULL on failure
5097 churn_stop_callback (void *cls, const char *emsg)
5099 struct ShutdownContext *shutdown_ctx = cls;
5100 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
5101 unsigned int total_left;
5102 char *error_message;
5104 error_message = NULL;
5105 shutdown_ctx->outstanding--;
5109 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5110 "Churn stop callback failed with error `%s'\n", emsg);
5111 churn_ctx->num_failed_stop++;
5115 churn_ctx->num_to_stop--;
5119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5120 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
5123 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
5124 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5126 if (total_left == 0)
5128 if ((churn_ctx->num_failed_stop > 0)
5129 || (churn_ctx->num_failed_start > 0))
5131 GNUNET_asprintf (&error_message,
5132 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5133 churn_ctx->num_failed_start,
5134 churn_ctx->num_failed_stop);
5136 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5137 GNUNET_free_non_null (error_message);
5138 GNUNET_free (churn_ctx);
5139 GNUNET_free (shutdown_ctx);
5144 * Count the number of running peers.
5146 * @param pg handle for the peer group
5148 * @return the number of currently running peers in the peer group
5151 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
5154 unsigned int running = 0;
5155 for (i = 0; i < pg->total; i++)
5157 if (pg->peers[i].daemon->running == GNUNET_YES)
5159 GNUNET_assert (running != -1);
5167 * Task to rate limit the number of outstanding peer shutdown
5168 * requests. This is necessary for making sure we don't do
5169 * too many ssh connections at once, but is generally nicer
5170 * to any system as well (graduated task starts, as opposed
5171 * to calling gnunet-arm N times all at once).
5174 schedule_churn_shutdown_task (void *cls,
5175 const struct GNUNET_SCHEDULER_TaskContext *tc)
5177 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5178 struct ShutdownContext *shutdown_ctx;
5180 GNUNET_assert (peer_shutdown_ctx != NULL);
5181 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5182 GNUNET_assert (shutdown_ctx != NULL);
5184 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
5185 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5186 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5187 &schedule_churn_shutdown_task,
5191 shutdown_ctx->outstanding++;
5192 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5193 shutdown_ctx->timeout, shutdown_ctx->cb,
5194 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5195 GNUNET_free (peer_shutdown_ctx);
5200 * Simulate churn by stopping some peers (and possibly
5201 * re-starting others if churn is called multiple times). This
5202 * function can only be used to create leave-join churn (peers "never"
5203 * leave for good). First "voff" random peers that are currently
5204 * online will be taken offline; then "von" random peers that are then
5205 * offline will be put back online. No notifications will be
5206 * generated for any of these operations except for the callback upon
5209 * @param pg handle for the peer group
5210 * @param voff number of peers that should go offline
5211 * @param von number of peers that should come back online;
5212 * must be zero on first call (since "testbed_start"
5213 * always starts all of the peers)
5214 * @param timeout how long to wait for operations to finish before
5216 * @param cb function to call at the end
5217 * @param cb_cls closure for cb
5220 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
5223 struct GNUNET_TIME_Relative timeout,
5224 GNUNET_TESTING_NotifyCompletion cb,
5227 struct ChurnContext *churn_ctx;
5228 struct ShutdownContext *shutdown_ctx;
5229 struct PeerShutdownContext *peer_shutdown_ctx;
5230 struct PeerRestartContext *peer_restart_ctx;
5231 struct ChurnRestartContext *churn_startup_ctx;
5233 unsigned int running;
5234 unsigned int stopped;
5235 unsigned int total_running;
5236 unsigned int total_stopped;
5238 unsigned int *running_arr;
5239 unsigned int *stopped_arr;
5240 unsigned int *running_permute;
5241 unsigned int *stopped_permute;
5246 if ((von == 0) && (voff == 0)) /* No peers at all? */
5252 for (i = 0; i < pg->total; i++)
5254 if (pg->peers[i].daemon->running == GNUNET_YES)
5256 GNUNET_assert (running != -1);
5261 GNUNET_assert (stopped != -1);
5268 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5269 "Trying to stop more peers than are currently running!\n");
5270 cb (cb_cls, "Trying to stop more peers than are currently running!");
5276 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5277 "Trying to start more peers than are currently stopped!\n");
5278 cb (cb_cls, "Trying to start more peers than are currently stopped!");
5282 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5286 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
5290 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
5292 running_permute = NULL;
5293 stopped_permute = NULL;
5297 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, running);
5300 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, stopped);
5302 total_running = running;
5303 total_stopped = stopped;
5307 churn_ctx->num_to_start = von;
5308 churn_ctx->num_to_stop = voff;
5310 churn_ctx->cb_cls = cb_cls;
5312 for (i = 0; i < pg->total; i++)
5314 if (pg->peers[i].daemon->running == GNUNET_YES)
5316 GNUNET_assert ((running_arr != NULL) && (total_running > running));
5317 running_arr[running] = i;
5322 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
5323 stopped_arr[stopped] = i;
5328 GNUNET_assert (running >= voff);
5331 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5332 shutdown_ctx->cb = &churn_stop_callback;
5333 shutdown_ctx->cb_cls = churn_ctx;
5334 shutdown_ctx->total_peers = voff;
5335 shutdown_ctx->timeout = timeout;
5338 for (i = 0; i < voff; i++)
5341 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
5342 running_permute[i]);
5344 GNUNET_assert (running_arr != NULL);
5345 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
5346 peer_shutdown_ctx->daemon =
5347 pg->peers[running_arr[running_permute[i]]].daemon;
5348 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
5349 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
5353 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
5355 &churn_stop_callback, churn_ctx,
5356 GNUNET_NO, GNUNET_YES); */
5359 GNUNET_assert (stopped >= von);
5362 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
5363 churn_startup_ctx->churn_ctx = churn_ctx;
5364 churn_startup_ctx->timeout = timeout;
5366 for (i = 0; i < von; i++)
5369 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
5370 stopped_permute[i]);
5372 GNUNET_assert (stopped_arr != NULL);
5373 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
5374 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
5375 peer_restart_ctx->daemon =
5376 pg->peers[stopped_arr[stopped_permute[i]]].daemon;
5377 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
5379 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
5380 timeout, &churn_start_callback, churn_ctx); */
5383 GNUNET_free_non_null (running_arr);
5384 GNUNET_free_non_null (stopped_arr);
5385 GNUNET_free_non_null (running_permute);
5386 GNUNET_free_non_null (stopped_permute);
5391 * Restart all peers in the given group.
5393 * @param pg the handle to the peer group
5394 * @param callback function to call on completion (or failure)
5395 * @param callback_cls closure for the callback function
5398 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
5399 GNUNET_TESTING_NotifyCompletion callback,
5402 struct RestartContext *restart_context;
5407 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
5408 restart_context->peer_group = pg;
5409 restart_context->peers_restarted = 0;
5410 restart_context->callback = callback;
5411 restart_context->callback_cls = callback_cls;
5413 for (off = 0; off < pg->total; off++)
5415 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
5416 &restart_callback, restart_context);
5422 * Start or stop an individual peer from the given group.
5424 * @param pg handle to the peer group
5425 * @param offset which peer to start or stop
5426 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
5427 * @param timeout how long to wait for shutdown
5428 * @param cb function to call at the end
5429 * @param cb_cls closure for cb
5432 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
5433 unsigned int offset,
5435 struct GNUNET_TIME_Relative timeout,
5436 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5438 struct ShutdownContext *shutdown_ctx;
5439 struct ChurnRestartContext *startup_ctx;
5440 struct ChurnContext *churn_ctx;
5442 if (GNUNET_NO == desired_status)
5444 if (NULL != pg->peers[offset].daemon)
5446 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5447 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5448 churn_ctx->num_to_start = 0;
5449 churn_ctx->num_to_stop = 1;
5451 churn_ctx->cb_cls = cb_cls;
5452 shutdown_ctx->cb_cls = churn_ctx;
5453 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon,
5454 timeout, &churn_stop_callback,
5455 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5458 else if (GNUNET_YES == desired_status)
5460 if (NULL == pg->peers[offset].daemon)
5462 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
5463 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5464 churn_ctx->num_to_start = 1;
5465 churn_ctx->num_to_stop = 0;
5467 churn_ctx->cb_cls = cb_cls;
5468 startup_ctx->churn_ctx = churn_ctx;
5469 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
5470 timeout, &churn_start_callback,
5480 * Callback for shutting down peers in a peer group.
5482 * @param cls closure (struct ShutdownContext)
5483 * @param emsg NULL on success
5486 internal_shutdown_callback (void *cls, const char *emsg)
5488 struct ShutdownContext *shutdown_ctx = cls;
5490 shutdown_ctx->outstanding--;
5493 shutdown_ctx->peers_down++;
5497 shutdown_ctx->peers_failed++;
5500 if ((shutdown_ctx->cb != NULL)
5501 && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed ==
5502 shutdown_ctx->total_peers))
5504 if (shutdown_ctx->peers_failed > 0)
5505 shutdown_ctx->cb (shutdown_ctx->cb_cls,
5506 "Not all peers successfully shut down!");
5508 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
5509 GNUNET_free (shutdown_ctx);
5515 * Task to rate limit the number of outstanding peer shutdown
5516 * requests. This is necessary for making sure we don't do
5517 * too many ssh connections at once, but is generally nicer
5518 * to any system as well (graduated task starts, as opposed
5519 * to calling gnunet-arm N times all at once).
5522 schedule_shutdown_task (void *cls,
5523 const struct GNUNET_SCHEDULER_TaskContext *tc)
5525 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5526 struct ShutdownContext *shutdown_ctx;
5528 GNUNET_assert (peer_shutdown_ctx != NULL);
5529 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5530 GNUNET_assert (shutdown_ctx != NULL);
5532 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
5533 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5534 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5535 &schedule_shutdown_task, peer_shutdown_ctx);
5538 shutdown_ctx->outstanding++;
5539 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5540 shutdown_ctx->timeout,
5541 &internal_shutdown_callback, shutdown_ctx,
5542 GNUNET_YES, GNUNET_NO);
5543 GNUNET_free (peer_shutdown_ctx);
5548 * Shutdown all peers started in the given group.
5550 * @param pg handle to the peer group
5551 * @param timeout how long to wait for shutdown
5552 * @param cb callback to notify upon success or failure
5553 * @param cb_cls closure for cb
5556 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
5557 struct GNUNET_TIME_Relative timeout,
5558 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5561 struct ShutdownContext *shutdown_ctx;
5562 struct PeerShutdownContext *peer_shutdown_ctx;
5564 struct PeerConnection *conn_iter;
5565 struct PeerConnection *temp_conn;
5568 GNUNET_assert (pg->total > 0);
5570 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5571 shutdown_ctx->cb = cb;
5572 shutdown_ctx->cb_cls = cb_cls;
5573 shutdown_ctx->total_peers = pg->total;
5574 shutdown_ctx->timeout = timeout;
5575 /* shtudown_ctx->outstanding = 0; */
5577 for (off = 0; off < pg->total; off++)
5579 GNUNET_assert (NULL != pg->peers[off].daemon);
5580 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
5581 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
5582 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
5583 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
5585 if (NULL != pg->peers[off].cfg)
5586 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
5588 conn_iter = pg->peers[off].allowed_peers_head;
5589 while (conn_iter != NULL)
5591 temp_conn = conn_iter->next;
5592 GNUNET_free(conn_iter);
5593 conn_iter = temp_conn;
5596 conn_iter = pg->peers[off].connect_peers_head;
5597 while (conn_iter != NULL)
5599 temp_conn = conn_iter->next;
5600 GNUNET_free(conn_iter);
5601 conn_iter = temp_conn;
5604 conn_iter = pg->peers[off].blacklisted_peers_head;
5605 while (conn_iter != NULL)
5607 temp_conn = conn_iter->next;
5608 GNUNET_free(conn_iter);
5609 conn_iter = temp_conn;
5612 conn_iter = pg->peers[off].connect_peers_working_set_head;
5613 while (conn_iter != NULL)
5615 temp_conn = conn_iter->next;
5616 GNUNET_free(conn_iter);
5617 conn_iter = temp_conn;
5620 if (pg->peers[off].allowed_peers != NULL)
5621 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
5622 if (pg->peers[off].connect_peers != NULL)
5623 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
5624 if (pg->peers[off].blacklisted_peers != NULL)
5625 GNUNET_CONTAINER_multihashmap_destroy (pg->
5626 peers[off].blacklisted_peers);
5629 GNUNET_free (pg->peers);
5630 GNUNET_free_non_null(pg->hostkey_data);
5631 for (off = 0; off < pg->num_hosts; off++)
5633 GNUNET_free (pg->hosts[off].hostname);
5634 GNUNET_free_non_null (pg->hosts[off].username);
5636 GNUNET_free_non_null (pg->hosts);
5641 /* end of testing_group.c */