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 /* Maximum time to delay connect attempt */
57 #define MAX_CONNECT_DELAY 300
59 #define MAX_CONCURRENT_HOSTKEYS 500
62 * Which list of peers do we need to modify?
66 /** Modify allowed peers */
69 /** Modify connect peers */
72 /** Modify blacklist peers */
75 /** Modify workingset peers */
80 * Prototype of a function called whenever two peers would be connected
81 * in a certain topology.
83 typedef unsigned int (*GNUNET_TESTING_ConnectionProcessor) (struct
84 GNUNET_TESTING_PeerGroup
94 * Context for handling churning a peer group
99 * The peergroup we are dealing with.
101 struct GNUNET_TESTING_PeerGroup *pg;
104 * Callback used to notify of churning finished
106 GNUNET_TESTING_NotifyCompletion cb;
109 * Closure for callback
114 * Number of peers that still need to be started
116 unsigned int num_to_start;
119 * Number of peers that still need to be stopped
121 unsigned int num_to_stop;
124 * Number of peers that failed to start
126 unsigned int num_failed_start;
129 * Number of peers that failed to stop
131 unsigned int num_failed_stop;
134 struct RestartContext
137 * The group of peers being restarted
139 struct GNUNET_TESTING_PeerGroup *peer_group;
142 * How many peers have been restarted thus far
144 unsigned int peers_restarted;
147 * How many peers got an error when restarting
149 unsigned int peers_restart_failed;
152 * The function to call once all peers have been restarted
154 GNUNET_TESTING_NotifyCompletion callback;
157 * Closure for callback function
164 struct ShutdownContext
166 struct GNUNET_TESTING_PeerGroup *pg;
168 * Total peers to wait for
170 unsigned int total_peers;
173 * Number of peers successfully shut down
175 unsigned int peers_down;
178 * Number of peers failed to shut down
180 unsigned int peers_failed;
183 * Number of peers we have started shutting
184 * down. If too many, wait on them.
186 unsigned int outstanding;
189 * Timeout for shutdown.
191 struct GNUNET_TIME_Relative timeout;
194 * Callback to call when all peers either
195 * shutdown or failed to shutdown
197 GNUNET_TESTING_NotifyCompletion cb;
206 * Individual shutdown context for a particular peer.
208 struct PeerShutdownContext
211 * Pointer to the high level shutdown context.
213 struct ShutdownContext *shutdown_ctx;
216 * The daemon handle for the peer to shut down.
218 struct GNUNET_TESTING_Daemon *daemon;
222 * Individual shutdown context for a particular peer.
224 struct PeerRestartContext
227 * Pointer to the high level restart context.
229 struct ChurnRestartContext *churn_restart_ctx;
232 * The daemon handle for the peer to shut down.
234 struct GNUNET_TESTING_Daemon *daemon;
238 struct CreateTopologyContext
242 * Function to call with number of connections
244 GNUNET_TESTING_NotifyConnections cont;
247 * Closure for connection notification
254 /** Waiting to read number of peers */
257 /** Should find next peer index */
260 /** Should find colon */
263 /** Should read other peer index, space, or endline */
269 struct PeerConnection
274 struct PeerConnection *prev;
279 struct PeerConnection *next;
283 * Index of daemon in pg->peers
290 struct InternalStartContext
293 * Pointer to peerdata
295 struct PeerData *peer;
298 * Timeout for peer startup
300 struct GNUNET_TIME_Relative timeout;
303 * Client callback for hostkey notification
305 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
308 * Closure for hostkey_callback
313 * Client callback for peer start notification
315 GNUNET_TESTING_NotifyDaemonRunning start_cb;
323 * Hostname, where to start the peer
325 const char *hostname;
328 * Username to use when connecting to the
331 const char *username;
334 * Pointer to starting memory location of a hostkey
339 * Port to use for ssh.
345 struct ChurnRestartContext
348 * PeerGroup that we are working with.
350 struct GNUNET_TESTING_PeerGroup *pg;
353 * Number of restarts currently in flight.
355 unsigned int outstanding;
358 * Handle to the underlying churn context.
360 struct ChurnContext *churn_ctx;
363 * How long to allow the operation to take.
365 struct GNUNET_TIME_Relative timeout;
369 * Data we keep per peer.
374 * (Initial) configuration of the host.
375 * (initial because clients could change
376 * it and we would not know about those
379 struct GNUNET_CONFIGURATION_Handle *cfg;
382 * Handle for controlling the daemon.
384 struct GNUNET_TESTING_Daemon *daemon;
387 * The peergroup this peer belongs to.
389 struct GNUNET_TESTING_PeerGroup *pg;
393 * Linked list of allowed peer connections.
395 struct PeerConnection *allowed_peers_head;
398 * Linked list of allowed peer connections.
400 struct PeerConnection *allowed_peers_tail;
403 * Linked list of blacklisted peer connections.
405 struct PeerConnection *blacklisted_peers_head;
408 * Linked list of blacklisted peer connections.
410 struct PeerConnection *blacklisted_peers_tail;
413 * Linked list of connect peer connections.
415 struct PeerConnection *connect_peers_head;
418 * Linked list of connect peer connections.
420 struct PeerConnection *connect_peers_tail;
423 * Linked list of connect peer connections.
425 struct PeerConnection *connect_peers_working_set_head;
428 * Linked list of connect peer connections.
430 struct PeerConnection *connect_peers_working_set_tail;
434 * Hash map of allowed peer connections (F2F created topology)
436 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
439 * Hash map of blacklisted peers
441 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
444 * Hash map of peer connections
446 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
449 * Temporary hash map of peer connections
451 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
455 * Temporary variable for topology creation, should be reset before
456 * creating any topology so the count is valid once finished.
461 * Context to keep track of peers being started, to
462 * stagger hostkey generation and peer startup.
464 struct InternalStartContext internal_context;
469 * Linked list of per-host data.
479 * SSH username to use when connecting to this host.
484 * SSH port to use when connecting to this host.
489 * Lowest port that we have not yet used
495 struct TopologyIterateContext
498 * The peergroup we are working with.
500 struct GNUNET_TESTING_PeerGroup *pg;
503 * Callback for notifying of two connected peers.
505 GNUNET_TESTING_NotifyTopology topology_cb;
508 * Closure for topology_cb
513 * Number of peers currently connected to.
515 unsigned int connected;
518 * Number of peers we have finished iterating.
520 unsigned int completed;
523 * Number of peers total.
528 struct StatsIterateContext
531 * The peergroup that we are dealing with.
533 struct GNUNET_TESTING_PeerGroup *pg;
536 * Continuation to call once all stats information has been retrieved.
538 GNUNET_STATISTICS_Callback cont;
541 * Proc function to call on each value received.
543 GNUNET_TESTING_STATISTICS_Iterator proc;
546 * Closure for topology_cb
551 * Number of peers currently connected to.
553 unsigned int connected;
556 * Number of peers we have finished iterating.
558 unsigned int completed;
561 * Number of peers total.
569 struct GNUNET_TESTING_Daemon *daemon;
572 struct StatsCoreContext
575 struct GNUNET_TESTING_Daemon *daemon;
577 * Handle to the statistics service.
579 struct GNUNET_STATISTICS_Handle *stats_handle;
582 * Handle for getting statistics.
584 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
588 * Handle to a group of GNUnet peers.
590 struct GNUNET_TESTING_PeerGroup
593 * Configuration template.
595 const struct GNUNET_CONFIGURATION_Handle *cfg;
598 * Function to call on each started daemon.
600 //GNUNET_TESTING_NotifyDaemonRunning cb;
608 * Function to call on each topology connection created
610 GNUNET_TESTING_NotifyConnection notify_connection;
613 * Callback for notify_connection
615 void *notify_connection_cls;
618 * Array of information about hosts.
620 struct HostData *hosts;
623 * Number of hosts (size of HostData)
625 unsigned int num_hosts;
628 * Array of "total" peers.
630 struct PeerData *peers;
633 * Number of peers in this group.
638 * At what time should we fail the peer startup process?
640 struct GNUNET_TIME_Absolute max_timeout;
643 * How many peers are being started right now?
645 unsigned int starting;
648 * How many peers have already been started?
650 unsigned int started;
653 * Number of possible connections to peers
656 unsigned int max_outstanding_connections;
659 * Number of connects we are waiting on, allows us to rate limit
662 unsigned int outstanding_connects;
665 * How many connects have already been scheduled?
667 unsigned int total_connects_scheduled;
670 * Hostkeys loaded from a file.
677 struct GNUNET_CONFIGURATION_Handle *ret;
678 const struct GNUNET_CONFIGURATION_Handle *orig;
679 const char *hostname;
685 struct ConnectTopologyContext
688 * How many connections are left to create.
690 unsigned int remaining_connections;
693 * How many more connections do we need to schedule?
695 unsigned int remaining_connects_to_schedule;
698 * Handle to group of peers.
700 struct GNUNET_TESTING_PeerGroup *pg;
703 * How long to try this connection before timing out.
705 struct GNUNET_TIME_Relative connect_timeout;
708 * How many times to retry connecting the two peers.
710 unsigned int connect_attempts;
713 * Temp value set for each iteration.
715 //struct PeerData *first;
718 * Notification that all peers are connected.
720 GNUNET_TESTING_NotifyCompletion notify_connections_done;
723 * Closure for notify.
728 struct ConnectContext
731 * Peer to connect second to.
733 struct GNUNET_TESTING_Daemon *first;
736 * Peer to connect first to.
738 struct GNUNET_TESTING_Daemon *second;
741 * Higher level topology connection context.
743 struct ConnectTopologyContext *ct_ctx;
746 * Whether this connection has been accounted for in the schedule_connect call.
751 struct UnblacklistContext
756 struct GNUNET_TESTING_PeerGroup *pg;
759 * uid of the first peer
769 struct GNUNET_TESTING_PeerGroup *pg;
772 * uid of the first peer
777 * Peer data for first peer.
779 struct PeerData *first;
782 * Random percentage to use
787 struct MinimumContext
792 struct GNUNET_TESTING_PeerGroup *pg;
795 * uid of the first peer
800 * Peer data for first peer.
802 struct PeerData *first;
805 * Number of conns per peer
807 unsigned int num_to_add;
810 * Permuted array of all possible connections. Only add the Nth
811 * peer if it's in the Nth position.
813 unsigned int *pg_array;
816 * What number is the current element we are iterating over?
818 unsigned int current;
826 struct GNUNET_TESTING_PeerGroup *pg;
829 * uid of the first peer
834 * uid of the second peer
839 * Peer data for first peer.
841 struct PeerData *first;
844 * Which peer has been chosen as the one to add?
849 * What number is the current element we are iterating over?
851 unsigned int current;
856 * Convert unique ID to hash code.
858 * @param uid unique ID to convert
859 * @param hash set to uid (extended with zeros)
862 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
864 memset (hash, 0, sizeof (GNUNET_HashCode));
865 *((uint32_t *) hash) = uid;
869 * Convert hash code to unique ID.
871 * @param uid unique ID to convert
872 * @param hash set to uid (extended with zeros)
875 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
877 memcpy (uid, hash, sizeof (uint32_t));
882 * Get a topology from a string input.
884 * @param topology where to write the retrieved topology
885 * @param topology_string The string to attempt to
886 * get a configuration value from
887 * @return GNUNET_YES if topology string matched a
888 * known topology, GNUNET_NO if not
891 GNUNET_TESTING_topology_get (enum GNUNET_TESTING_Topology *topology,
892 const char *topology_string)
895 * Strings representing topologies in enum
897 static const char *topology_strings[] = {
899 * A clique (everyone connected to everyone else).
904 * Small-world network (2d torus plus random links).
909 * Small-world network (ring plus random links).
929 * Certain percentage of peers are unable to communicate directly
930 * replicating NAT conditions
935 * Scale free topology.
940 * Straight line topology.
945 * All peers are disconnected.
950 * Read the topology from a file.
958 if (topology_string == NULL)
960 while (topology_strings[curr] != NULL)
962 if (strcasecmp (topology_strings[curr], topology_string) == 0)
969 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
975 * Get connect topology option from string input.
977 * @param topology_option where to write the retrieved topology
978 * @param topology_string The string to attempt to
979 * get a configuration value from
980 * @return GNUNET_YES if string matched a known
981 * topology option, GNUNET_NO if not
984 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption
986 const char *topology_string)
989 * Options for connecting a topology as strings.
991 static const char *topology_option_strings[] = {
993 * Try to connect all peers specified in the topology.
998 * Choose a random subset of connections to create.
1000 "CONNECT_RANDOM_SUBSET",
1003 * Create at least X connections for each peer.
1008 * Using a depth first search, create one connection
1009 * per peer. If any are missed (graph disconnected)
1010 * start over at those peers until all have at least one
1016 * Find the N closest peers to each allowed peer in the
1017 * topology and make sure a connection to those peers
1018 * exists in the connect topology.
1023 * No options specified.
1031 if (topology_string == NULL)
1033 while (NULL != topology_option_strings[curr])
1035 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1037 *topology_option = curr;
1042 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1047 * Function to iterate over options. Copies
1048 * the options to the target configuration,
1049 * updating PORT values as needed.
1051 * @param cls closure
1052 * @param section name of the section
1053 * @param option name of the option
1054 * @param value value of the option
1057 update_config (void *cls,
1058 const char *section, const char *option, const char *value)
1060 struct UpdateContext *ctx = cls;
1064 char *single_variable;
1065 char *per_host_variable;
1066 unsigned long long num_per_host;
1068 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1070 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1073 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1076 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
1080 GNUNET_free (single_variable);
1083 if (0 == strcmp (option, "UNIXPATH"))
1085 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1086 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1088 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1091 GNUNET_snprintf (uval,
1093 "/tmp/test-service-%s-%u", section, ctx->upnum++);
1096 else if ((GNUNET_YES ==
1097 GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1099 &num_per_host)) && (num_per_host > 0))
1102 GNUNET_snprintf (uval,
1104 "/tmp/test-service-%s-%u",
1105 section, ctx->fdnum % num_per_host);
1108 GNUNET_free (single_variable);
1109 GNUNET_free (per_host_variable);
1112 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1114 value = ctx->hostname;
1117 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1122 * Create a new configuration using the given configuration
1123 * as a template; however, each PORT in the existing cfg
1124 * must be renumbered by incrementing "*port". If we run
1125 * out of "*port" numbers, return NULL.
1127 * @param cfg template configuration
1128 * @param port port numbers to use, update to reflect
1129 * port numbers that were used
1130 * @param upnum number to make unix domain socket names unique
1131 * @param hostname hostname of the controlling host, to allow control connections from
1132 * @param fdnum number used to offset the unix domain socket for grouped processes
1133 * (such as statistics or peerinfo, which can be shared among others)
1135 * @return new configuration, NULL on error
1137 static struct GNUNET_CONFIGURATION_Handle *
1138 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
1140 uint32_t * upnum, const char *hostname, uint32_t * fdnum)
1142 struct UpdateContext uc;
1145 char *allowed_hosts;
1151 uc.ret = GNUNET_CONFIGURATION_create ();
1152 uc.hostname = hostname;
1155 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1156 if (uc.nport >= HIGH_PORT)
1159 GNUNET_CONFIGURATION_destroy (uc.ret);
1163 if (GNUNET_CONFIGURATION_get_value_string
1164 (cfg, "testing", "control_host", &control_host) == GNUNET_OK)
1166 if (hostname != NULL)
1167 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1170 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1172 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1174 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH",
1176 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1177 "ACCEPT_FROM", allowed_hosts);
1178 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH",
1180 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1182 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH",
1184 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1185 "ACCEPT_FROM", allowed_hosts);
1186 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1188 GNUNET_free_non_null (control_host);
1189 GNUNET_free (allowed_hosts);
1193 /* arm needs to know to allow connections from the host on which it is running,
1194 * otherwise gnunet-arm is unable to connect to it in some instances */
1195 if (hostname != NULL)
1197 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1198 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1199 "BINDTO", hostname);
1200 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1201 "BINDTO", hostname);
1202 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1204 GNUNET_free (allowed_hosts);
1208 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1209 "BINDTO", "127.0.0.1");
1210 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1211 "BINDTO", "127.0.0.1");
1214 *port = (uint16_t) uc.nport;
1222 * Add entries to the some list
1224 * @param pg the peer group we are working with
1225 * @param first index of the first peer
1226 * @param second index of the second peer
1227 * @param list the peer list to use
1229 * @return the number of connections added (can be 0, 1 or 2)
1233 remove_connections (struct GNUNET_TESTING_PeerGroup *pg,
1234 unsigned int first, unsigned int second,
1235 enum PeerLists list)
1239 struct PeerConnection **first_list;
1240 struct PeerConnection **second_list;
1241 struct PeerConnection *first_iter;
1242 struct PeerConnection *second_iter;
1243 struct PeerConnection **first_tail;
1244 struct PeerConnection **second_tail;
1247 GNUNET_HashCode hash_first;
1248 GNUNET_HashCode hash_second;
1250 hash_from_uid (first, &hash_first);
1251 hash_from_uid (second, &hash_second);
1259 first_list = &pg->peers[first].allowed_peers_head;
1260 second_list = &pg->peers[second].allowed_peers_head;
1261 first_tail = &pg->peers[first].allowed_peers_tail;
1262 second_tail = &pg->peers[second].allowed_peers_tail;
1265 first_list = &pg->peers[first].connect_peers_head;
1266 second_list = &pg->peers[second].connect_peers_head;
1267 first_tail = &pg->peers[first].connect_peers_tail;
1268 second_tail = &pg->peers[second].connect_peers_tail;
1271 first_list = &pg->peers[first].blacklisted_peers_head;
1272 second_list = &pg->peers[second].blacklisted_peers_head;
1273 first_tail = &pg->peers[first].blacklisted_peers_tail;
1274 second_tail = &pg->peers[second].blacklisted_peers_tail;
1277 first_list = &pg->peers[first].connect_peers_working_set_head;
1278 second_list = &pg->peers[second].connect_peers_working_set_head;
1279 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1280 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1287 first_iter = *first_list;
1288 while (first_iter != NULL)
1290 if (first_iter->index == second)
1292 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1293 GNUNET_free(first_iter);
1297 first_iter = first_iter->next;
1300 second_iter = *second_list;
1301 while (second_iter != NULL)
1303 if (second_iter->index == first)
1305 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1306 GNUNET_free(second_iter);
1310 second_iter = second_iter->next;
1314 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1317 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1322 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1325 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1334 * Add entries to the some list
1336 * @param pg the peer group we are working with
1337 * @param first index of the first peer
1338 * @param second index of the second peer
1339 * @param list the list type that we should modify
1341 * @return the number of connections added (can be 0, 1 or 2)
1345 add_connections (struct GNUNET_TESTING_PeerGroup *pg,
1346 unsigned int first, unsigned int second,
1347 enum PeerLists list)
1353 struct PeerConnection **first_list;
1354 struct PeerConnection **second_list;
1355 struct PeerConnection *first_iter;
1356 struct PeerConnection *second_iter;
1357 struct PeerConnection *new_first;
1358 struct PeerConnection *new_second;
1359 struct PeerConnection **first_tail;
1360 struct PeerConnection **second_tail;
1362 GNUNET_HashCode hash_first;
1363 GNUNET_HashCode hash_second;
1365 hash_from_uid (first, &hash_first);
1366 hash_from_uid (second, &hash_second);
1373 first_list = &pg->peers[first].allowed_peers_head;
1374 second_list = &pg->peers[second].allowed_peers_head;
1375 first_tail = &pg->peers[first].allowed_peers_tail;
1376 second_tail = &pg->peers[second].allowed_peers_tail;
1379 first_list = &pg->peers[first].connect_peers_head;
1380 second_list = &pg->peers[second].connect_peers_head;
1381 first_tail = &pg->peers[first].connect_peers_tail;
1382 second_tail = &pg->peers[second].connect_peers_tail;
1385 first_list = &pg->peers[first].blacklisted_peers_head;
1386 second_list = &pg->peers[second].blacklisted_peers_head;
1387 first_tail = &pg->peers[first].blacklisted_peers_tail;
1388 second_tail = &pg->peers[second].blacklisted_peers_tail;
1391 first_list = &pg->peers[first].connect_peers_working_set_head;
1392 second_list = &pg->peers[second].connect_peers_working_set_head;
1393 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1394 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1401 add_first = GNUNET_YES;
1402 add_second = GNUNET_YES;
1404 first_iter = *first_list;
1405 while (first_iter != NULL)
1407 if (first_iter->index == second)
1409 add_first = GNUNET_NO;
1412 first_iter = first_iter->next;
1415 second_iter = *second_list;
1416 while (second_iter != NULL)
1418 if (second_iter->index == first)
1420 add_second = GNUNET_NO;
1423 second_iter = second_iter->next;
1427 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1430 add_first = GNUNET_YES;
1434 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1437 add_second = GNUNET_YES;
1445 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1446 new_first->index = second;
1447 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1449 new_first->next = *first_list;
1450 *first_list = new_first;*/
1452 GNUNET_assert (GNUNET_OK ==
1453 GNUNET_CONTAINER_multihashmap_put (pg->
1455 [first].blacklisted_peers,
1458 peers[second].daemon,
1459 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1461 pg->peers[first].num_connections++;
1468 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1469 new_second->index = first;
1470 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1472 new_second->next = *second_list;
1473 *second_list = new_second;
1476 GNUNET_assert (GNUNET_OK ==
1477 GNUNET_CONTAINER_multihashmap_put (pg->
1479 [second].blacklisted_peers,
1482 peers[first].daemon,
1483 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1485 pg->peers[second].num_connections++;
1494 * Scale free network construction as described in:
1496 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1498 * Start with a network of "one" peer, then progressively add
1499 * peers up to the total number. At each step, iterate over
1500 * all possible peers and connect new peer based on number of
1501 * existing connections of the target peer.
1503 * @param pg the peer group we are dealing with
1504 * @param proc the connection processor to use
1505 * @param list the peer list to use
1507 * @return the number of connections created
1510 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg,
1511 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1514 unsigned int total_connections;
1515 unsigned int outer_count;
1517 unsigned int previous_total_connections;
1521 GNUNET_assert (pg->total > 1);
1523 /* Add a connection between the first two nodes */
1524 total_connections = proc (pg, 0, 1, list);
1526 for (outer_count = 1; outer_count < pg->total; outer_count++)
1528 previous_total_connections = total_connections;
1529 for (i = 0; i < outer_count; i++)
1532 pg->peers[i].num_connections /
1533 (double) previous_total_connections;
1536 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1537 UINT64_MAX)) / ((double) UINT64_MAX);
1539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1540 "Considering connecting peer %d to peer %d\n",
1543 if (random < probability)
1546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1547 "Connecting peer %d to peer %d\n", outer_count, i);
1549 total_connections += proc (pg, outer_count, i, list);
1554 return total_connections;
1558 * Create a topology given a peer group (set of running peers)
1559 * and a connection processor. Creates a small world topology
1560 * according to the rewired ring construction. The basic
1561 * behavior is that a ring topology is created, but with some
1562 * probability instead of connecting a peer to the next
1563 * neighbor in the ring a connection will be created to a peer
1564 * selected uniformly at random. We use the TESTING
1565 * PERCENTAGE option to specify what number of
1566 * connections each peer should have. Default is 2,
1567 * which makes the ring, any given number is multiplied by
1568 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1569 * each peer have on average 2logn connections. The additional
1570 * connections are made at increasing distance around the ring
1571 * from the original peer, or to random peers based on the re-
1572 * wiring probability. The TESTING
1573 * PROBABILITY option is used as the probability that a given
1574 * connection is rewired.
1576 * @param pg the peergroup to create the topology on
1577 * @param proc the connection processor to call to actually set
1578 * up connections between two peers
1579 * @param list the peer list to use
1581 * @return the number of connections that were set up
1585 create_small_world_ring (struct GNUNET_TESTING_PeerGroup *pg,
1586 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1590 unsigned int natLog;
1591 unsigned int randomPeer;
1592 double random, logNModifier, probability;
1593 unsigned int smallWorldConnections;
1598 unsigned int useAnd;
1599 int connect_attempts;
1601 logNModifier = 0.5; /* FIXME: default value? */
1602 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1607 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1608 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1610 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1611 p_string, "LOGNMODIFIER", "TESTING");
1612 GNUNET_free (p_string);
1614 probability = 0.5; /* FIXME: default percentage? */
1615 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1620 if (sscanf (p_string, "%lf", &probability) != 1)
1621 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1623 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1624 p_string, "PERCENTAGE", "TESTING");
1625 GNUNET_free (p_string);
1627 natLog = log (pg->total);
1628 connsPerPeer = ceil (natLog * logNModifier);
1630 if (connsPerPeer % 2 == 1)
1633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."), connsPerPeer);
1635 smallWorldConnections = 0;
1636 connect_attempts = 0;
1637 for (i = 0; i < pg->total; i++)
1640 max = i + connsPerPeer / 2;
1641 min = i - connsPerPeer / 2;
1643 if (max > pg->total - 1)
1645 max = max - pg->total;
1651 min = pg->total - 1 + min;
1655 for (j = 0; j < connsPerPeer / 2; j++)
1659 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1660 UINT64_MAX) / ((double) UINT64_MAX));
1661 if (random < probability)
1663 /* Connect to uniformly selected random peer */
1665 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1667 while ((((randomPeer < max) && (randomPeer > min))
1668 && (useAnd == 0)) || (((randomPeer > min)
1669 || (randomPeer < max))
1673 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1676 smallWorldConnections += proc (pg, i, randomPeer, list);
1680 nodeToConnect = i + j + 1;
1681 if (nodeToConnect > pg->total - 1)
1683 nodeToConnect = nodeToConnect - pg->total;
1685 connect_attempts += proc (pg, i, nodeToConnect, list);
1691 connect_attempts += smallWorldConnections;
1693 return connect_attempts;
1697 * Create a topology given a peer group (set of running peers)
1698 * and a connection processor.
1700 * @param pg the peergroup to create the topology on
1701 * @param proc the connection processor to call to actually set
1702 * up connections between two peers
1703 * @param list the peer list to use
1705 * @return the number of connections that were set up
1709 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg,
1710 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1712 unsigned int outer_count, inner_count;
1713 unsigned int cutoff;
1714 int connect_attempts;
1715 double nat_percentage;
1718 nat_percentage = 0.6; /* FIXME: default percentage? */
1719 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1724 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1725 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1727 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1728 p_string, "PERCENTAGE", "TESTING");
1729 GNUNET_free (p_string);
1734 cutoff = (unsigned int) (nat_percentage * pg->total);
1736 connect_attempts = 0;
1738 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1740 for (inner_count = outer_count + 1; inner_count < pg->total;
1743 if ((outer_count > cutoff) || (inner_count > cutoff))
1746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1747 "Connecting peer %d to peer %d\n",
1748 outer_count, inner_count);
1750 connect_attempts += proc (pg, outer_count, inner_count, list);
1755 return connect_attempts;
1760 * Create a topology given a peer group (set of running peers)
1761 * and a connection processor.
1763 * @param pg the peergroup to create the topology on
1764 * @param proc the connection processor to call to actually set
1765 * up connections between two peers
1766 * @param list the peer list to use
1768 * @return the number of connections that were set up
1772 create_small_world (struct GNUNET_TESTING_PeerGroup *pg,
1773 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1775 unsigned int i, j, k;
1776 unsigned int square;
1779 unsigned int toggle = 1;
1780 unsigned int nodeToConnect;
1781 unsigned int natLog;
1782 unsigned int node1Row;
1783 unsigned int node1Col;
1784 unsigned int node2Row;
1785 unsigned int node2Col;
1786 unsigned int distance;
1787 double probability, random, percentage;
1788 unsigned int smallWorldConnections;
1789 unsigned int small_world_it;
1791 int connect_attempts;
1792 square = floor (sqrt (pg->total));
1796 percentage = 0.5; /* FIXME: default percentage? */
1797 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1802 if (sscanf (p_string, "%lf", &percentage) != 1)
1803 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1805 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1806 p_string, "PERCENTAGE", "TESTING");
1807 GNUNET_free (p_string);
1809 if (percentage < 0.0)
1811 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1813 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1814 "PERCENTAGE", "TESTING", percentage);
1817 probability = 0.5; /* FIXME: default percentage? */
1818 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1823 if (sscanf (p_string, "%lf", &probability) != 1)
1824 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1826 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1827 p_string, "PROBABILITY", "TESTING");
1828 GNUNET_free (p_string);
1830 if (square * square != pg->total)
1832 while (rows * cols < pg->total)
1834 if (toggle % 2 == 0)
1843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1845 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1849 connect_attempts = 0;
1850 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1851 * to the node to its right and above. Once this is over, we'll have our torus!
1852 * Special case for the last node (if the rows and columns are not equal), connect
1853 * to the first in the row to maintain topology.
1855 for (i = 0; i < pg->total; i++)
1857 /* First connect to the node to the right */
1858 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1859 nodeToConnect = i + 1;
1860 else if (i + 1 == pg->total)
1861 nodeToConnect = rows * cols - cols;
1863 nodeToConnect = i - cols + 1;
1865 connect_attempts += proc (pg, i, nodeToConnect, list);
1869 nodeToConnect = (rows * cols) - cols + i;
1870 if (nodeToConnect >= pg->total)
1871 nodeToConnect -= cols;
1874 nodeToConnect = i - cols;
1876 if (nodeToConnect < pg->total)
1877 connect_attempts += proc (pg, i, nodeToConnect, list);
1879 natLog = log (pg->total);
1880 #if VERBOSE_TESTING > 2
1881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1882 _("natural log of %d is %d, will run %d iterations\n"),
1883 pg->total, natLog, (int) (natLog * percentage));
1884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1885 _("Total connections added thus far: %u!\n"), connect_attempts);
1887 smallWorldConnections = 0;
1888 small_world_it = (unsigned int) (natLog * percentage);
1889 if (small_world_it < 1)
1891 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
1892 for (i = 0; i < small_world_it; i++)
1894 for (j = 0; j < pg->total; j++)
1896 /* Determine the row and column of node at position j on the 2d torus */
1897 node1Row = j / cols;
1898 node1Col = j - (node1Row * cols);
1899 for (k = 0; k < pg->total; k++)
1901 /* Determine the row and column of node at position k on the 2d torus */
1902 node2Row = k / cols;
1903 node2Col = k - (node2Row * cols);
1904 /* Simple Cartesian distance */
1906 abs (node1Row - node2Row) + abs (node1Col - node2Col);
1909 /* Calculate probability as 1 over the square of the distance */
1910 probability = 1.0 / (distance * distance);
1911 /* Choose a random value between 0 and 1 */
1914 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1916 ((double) UINT64_MAX);
1917 /* If random < probability, then connect the two nodes */
1918 if (random < probability)
1919 smallWorldConnections += proc (pg, j, k, list);
1925 connect_attempts += smallWorldConnections;
1926 #if VERBOSE_TESTING > 2
1927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1928 _("Total connections added for small world: %d!\n"),
1929 smallWorldConnections);
1931 return connect_attempts;
1935 * Create a topology given a peer group (set of running peers)
1936 * and a connection processor.
1938 * @param pg the peergroup to create the topology on
1939 * @param proc the connection processor to call to actually set
1940 * up connections between two peers
1941 * @param list the peer list to use
1943 * @return the number of connections that were set up
1947 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg,
1948 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1951 unsigned int outer_count;
1952 unsigned int inner_count;
1953 int connect_attempts;
1957 probability = 0.5; /* FIXME: default percentage? */
1958 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1963 if (sscanf (p_string, "%lf", &probability) != 1)
1964 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1966 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1967 p_string, "PROBABILITY", "TESTING");
1968 GNUNET_free (p_string);
1970 connect_attempts = 0;
1971 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1973 for (inner_count = outer_count + 1; inner_count < pg->total;
1978 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1979 UINT64_MAX)) / ((double) UINT64_MAX);
1981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1982 _("rand is %f probability is %f\n"), temp_rand,
1985 if (temp_rand < probability)
1987 connect_attempts += proc (pg, outer_count, inner_count, list);
1992 return connect_attempts;
1996 * Create a topology given a peer group (set of running peers)
1997 * and a connection processor. This particular function creates
1998 * the connections for a 2d-torus, plus additional "closest"
1999 * connections per peer.
2001 * @param pg the peergroup to create the topology on
2002 * @param proc the connection processor to call to actually set
2003 * up connections between two peers
2004 * @param list the peer list to use
2006 * @return the number of connections that were set up
2010 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg,
2011 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2014 unsigned int square;
2017 unsigned int toggle = 1;
2018 unsigned int nodeToConnect;
2019 int connect_attempts;
2021 connect_attempts = 0;
2023 square = floor (sqrt (pg->total));
2027 if (square * square != pg->total)
2029 while (rows * cols < pg->total)
2031 if (toggle % 2 == 0)
2040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2042 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2045 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2046 * to the node to its right and above. Once this is over, we'll have our torus!
2047 * Special case for the last node (if the rows and columns are not equal), connect
2048 * to the first in the row to maintain topology.
2050 for (i = 0; i < pg->total; i++)
2052 /* First connect to the node to the right */
2053 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2054 nodeToConnect = i + 1;
2055 else if (i + 1 == pg->total)
2056 nodeToConnect = rows * cols - cols;
2058 nodeToConnect = i - cols + 1;
2060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2061 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2063 connect_attempts += proc (pg, i, nodeToConnect, list);
2065 /* Second connect to the node immediately above */
2068 nodeToConnect = (rows * cols) - cols + i;
2069 if (nodeToConnect >= pg->total)
2070 nodeToConnect -= cols;
2073 nodeToConnect = i - cols;
2075 if (nodeToConnect < pg->total)
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2079 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2081 connect_attempts += proc (pg, i, nodeToConnect, list);
2086 return connect_attempts;
2091 * Create a topology given a peer group (set of running peers)
2092 * and a connection processor.
2094 * @param pg the peergroup to create the topology on
2095 * @param proc the connection processor to call to actually set
2096 * up connections between two peers
2097 * @param list the peer list to use
2099 * @return the number of connections that were set up
2103 create_clique (struct GNUNET_TESTING_PeerGroup *pg,
2104 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2106 unsigned int outer_count;
2107 unsigned int inner_count;
2108 int connect_attempts;
2110 connect_attempts = 0;
2112 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2114 for (inner_count = outer_count + 1; inner_count < pg->total;
2118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2119 "Connecting peer %d to peer %d\n",
2120 outer_count, inner_count);
2122 connect_attempts += proc (pg, outer_count, inner_count, list);
2126 return connect_attempts;
2131 * Iterator over hash map entries.
2133 * @param cls closure the peer group
2134 * @param key the key stored in the hashmap is the
2135 * index of the peer to connect to
2136 * @param value value in the hash map, handle to the peer daemon
2137 * @return GNUNET_YES if we should continue to
2142 unblacklist_iterator (void *cls,
2143 const GNUNET_HashCode * key,
2146 struct UnblacklistContext *un_ctx = cls;
2147 uint32_t second_pos;
2149 uid_from_hash (key, &second_pos);
2151 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2158 * Create a blacklist topology based on the allowed topology
2159 * which disallows any connections not in the allowed topology
2160 * at the transport level.
2162 * @param pg the peergroup to create the topology on
2163 * @param proc the connection processor to call to allow
2164 * up connections between two peers
2166 * @return the number of connections that were set up
2170 copy_allowed (struct GNUNET_TESTING_PeerGroup *pg,
2171 GNUNET_TESTING_ConnectionProcessor proc)
2173 struct UnblacklistContext un_ctx;
2176 struct PeerConnection *iter;
2180 for (count = 0; count < pg->total - 1; count++)
2182 un_ctx.first_uid = count;
2184 iter = pg->peers[count].allowed_peers_head;
2185 while (iter != NULL)
2187 remove_connections(pg, count, iter->index, BLACKLIST);
2188 //unblacklist_connections(pg, count, iter->index);
2192 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers, &unblacklist_iterator, &un_ctx);
2195 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2200 * Create a topology given a peer group (set of running peers)
2201 * and a connection processor.
2203 * @param pg the peergroup to create the topology on
2204 * @param proc the connection processor to call to actually set
2205 * up connections between two peers
2206 * @param list which list should be modified
2208 * @return the number of connections that were set up
2212 create_line (struct GNUNET_TESTING_PeerGroup *pg,
2213 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2216 int connect_attempts;
2218 connect_attempts = 0;
2220 /* Connect each peer to the next highest numbered peer */
2221 for (count = 0; count < pg->total - 1; count++)
2224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2225 "Connecting peer %d to peer %d\n", count, count + 1);
2227 connect_attempts += proc (pg, count, count + 1, list);
2230 return connect_attempts;
2234 * Create a topology given a peer group (set of running peers)
2235 * and a connection processor.
2237 * @param pg the peergroup to create the topology on
2238 * @param filename the file to read topology information from
2239 * @param proc the connection processor to call to actually set
2240 * up connections between two peers
2241 * @param list the peer list to use
2243 * @return the number of connections that were set up
2247 create_from_file (struct GNUNET_TESTING_PeerGroup *pg,
2249 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2251 int connect_attempts;
2252 unsigned int first_peer_index;
2253 unsigned int second_peer_index;
2254 connect_attempts = 0;
2259 unsigned int total_peers;
2261 enum States curr_state;
2263 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2264 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2266 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2269 "Could not open file `%s' specified for topology!", filename);
2270 return connect_attempts;
2273 data = GNUNET_malloc_large (frstat.st_size);
2274 GNUNET_assert(data != NULL);
2275 if (frstat.st_size !=
2276 GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2279 "Could not read file %s specified for host list, ending test!", filename);
2281 return connect_attempts;
2286 /* First line should contain a single integer, specifying the number of peers */
2287 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2288 curr_state = NUM_PEERS;
2289 while (count < frstat.st_size - 1)
2291 if ((buf[count] == '\n') || (buf[count] == ' '))
2300 if (1 != sscanf(&buf[count], "%u", &total_peers))
2302 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read number of peers from topology file!\n");
2303 GNUNET_free_non_null(data);
2304 return connect_attempts;
2306 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read %u total peers in topology\n", total_peers);
2307 GNUNET_assert(total_peers == pg->total);
2308 curr_state = PEER_INDEX;
2309 while((buf[count] != '\n') && (count < frstat.st_size - 1))
2314 if (1 != sscanf(&buf[count], "%u", &first_peer_index))
2316 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read peer index from topology file!\n");
2317 GNUNET_free_non_null(data);
2318 return connect_attempts;
2320 while((buf[count] != ':') && (count < frstat.st_size - 1))
2323 curr_state = OTHER_PEER_INDEX;
2326 if (1 == sscanf(&buf[count], ":"))
2327 curr_state = OTHER_PEER_INDEX;
2330 case OTHER_PEER_INDEX:
2331 if (1 != sscanf(&buf[count], "%u", &second_peer_index))
2333 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to peer index from topology file!\n");
2334 GNUNET_free_non_null(data);
2335 return connect_attempts;
2337 /* Assume file is written with first peer 1, but array index is 0 */
2338 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index - 1, list);
2339 while((buf[count] != '\n') && (buf[count] != ',') && (count < frstat.st_size - 1))
2341 if (buf[count] == '\n')
2343 curr_state = PEER_INDEX;
2345 else if (buf[count] != ',')
2347 curr_state = OTHER_PEER_INDEX;
2352 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Found bad data in topology file while in state %d!\n", curr_state);
2354 return connect_attempts;
2359 /* Connect each peer to the next highest numbered peer */
2360 for (count = 0; count < pg->total - 1; count++)
2363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2364 "Connecting peer %d to peer %d\n", first_peer_index, second_peer_index);
2366 connect_attempts += proc (pg, first_peer_index, second_peer_index);
2369 return connect_attempts;
2373 * Create a topology given a peer group (set of running peers)
2374 * and a connection processor.
2376 * @param pg the peergroup to create the topology on
2377 * @param proc the connection processor to call to actually set
2378 * up connections between two peers
2379 * @param list the peer list to use
2381 * @return the number of connections that were set up
2385 create_ring (struct GNUNET_TESTING_PeerGroup *pg,
2386 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2389 int connect_attempts;
2391 connect_attempts = 0;
2393 /* Connect each peer to the next highest numbered peer */
2394 for (count = 0; count < pg->total - 1; count++)
2397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2398 "Connecting peer %d to peer %d\n", count, count + 1);
2400 connect_attempts += proc (pg, count, count + 1, list);
2403 /* Connect the last peer to the first peer */
2404 connect_attempts += proc (pg, pg->total - 1, 0, list);
2406 return connect_attempts;
2411 * Iterator for writing friends of a peer to a file.
2413 * @param cls closure, an open writable file handle
2414 * @param key the key the daemon was stored under
2415 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2417 * @return GNUNET_YES to continue iteration
2419 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2420 * with a single file_iterator that takes a closure which contains
2421 * the prefix to write before the peer. Then this could be used
2422 * for blacklisting multiple transports and writing the friend
2423 * file. I'm sure *someone* will complain loudly about other
2424 * things that negate these functions even existing so no point in
2428 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2430 FILE *temp_friend_handle = cls;
2431 struct GNUNET_TESTING_Daemon *peer = value;
2432 struct GNUNET_PeerIdentity *temppeer;
2433 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2435 temppeer = &peer->id;
2436 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2437 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2442 struct BlacklistContext
2445 * The (open) file handle to write to
2447 FILE *temp_file_handle;
2450 * The transport that this peer will be blacklisted on.
2456 * Iterator for writing blacklist data to appropriate files.
2458 * @param cls closure, an open writable file handle
2459 * @param key the key the daemon was stored under
2460 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2462 * @return GNUNET_YES to continue iteration
2465 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2467 struct BlacklistContext *blacklist_ctx = cls;
2468 struct GNUNET_TESTING_Daemon *peer = value;
2469 struct GNUNET_PeerIdentity *temppeer;
2470 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2472 temppeer = &peer->id;
2473 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2474 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2475 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2476 blacklist_ctx->transport, (char *) &peer_enc);
2484 * Create the friend files based on the PeerConnection's
2485 * of each peer in the peer group, and copy the files
2486 * to the appropriate place
2488 * @param pg the peer group we are dealing with
2491 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
2493 FILE *temp_friend_handle;
2494 unsigned int pg_iter;
2495 char *temp_service_path;
2496 struct GNUNET_OS_Process **procarr;
2499 enum GNUNET_OS_ProcessStatusType type;
2500 unsigned long return_code;
2505 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2506 struct PeerConnection *conn_iter;
2508 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2509 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2511 mytemp = GNUNET_DISK_mktemp ("friends");
2512 GNUNET_assert (mytemp != NULL);
2513 temp_friend_handle = fopen (mytemp, "wt");
2514 GNUNET_assert (temp_friend_handle != NULL);
2516 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2517 while (conn_iter != NULL)
2519 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
2520 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2521 conn_iter = conn_iter->next;
2524 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2525 &friend_file_iterator,
2526 temp_friend_handle);
2528 fclose (temp_friend_handle);
2531 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2532 daemon->cfg, "PATHS",
2534 &temp_service_path))
2536 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2538 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2539 "SERVICEHOME", "PATHS");
2540 if (UNLINK (mytemp) != 0)
2541 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2543 GNUNET_free (mytemp);
2547 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2549 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2550 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2555 _("Copying file with command cp %s %s\n"), mytemp, arg);
2560 else /* Remote, scp the file to the correct place */
2562 if (NULL != pg->peers[pg_iter].daemon->username)
2563 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2564 pg->peers[pg_iter].daemon->username,
2565 pg->peers[pg_iter].daemon->hostname,
2568 GNUNET_asprintf (&arg, "%s:%s/friends",
2569 pg->peers[pg_iter].daemon->hostname,
2572 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2577 _("Copying file with command scp %s %s\n"), mytemp,
2582 GNUNET_free (temp_service_path);
2583 GNUNET_free (mytemp);
2587 ret = GNUNET_SYSERR;
2588 while ((count < max_wait) && (ret != GNUNET_OK))
2591 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2595 _("Checking copy status of file %d\n"), pg_iter);
2597 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2599 if (GNUNET_OS_process_status
2600 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2602 ret = GNUNET_SYSERR;
2604 else if ((type != GNUNET_OS_PROCESS_EXITED)
2605 || (return_code != 0))
2607 ret = GNUNET_SYSERR;
2611 GNUNET_OS_process_close (procarr[pg_iter]);
2612 procarr[pg_iter] = NULL;
2614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2615 _("File %d copied\n"), pg_iter);
2621 if (ret == GNUNET_SYSERR)
2623 /* FIXME: why sleep here? -CG */
2629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2630 _("Finished copying all friend files!\n"));
2632 GNUNET_free (procarr);
2638 * Create the blacklist files based on the PeerConnection's
2639 * of each peer in the peer group, and copy the files
2640 * to the appropriate place.
2642 * @param pg the peer group we are dealing with
2643 * @param transports space delimited list of transports to blacklist
2646 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg,
2647 const char *transports)
2649 FILE *temp_file_handle;
2650 unsigned int pg_iter;
2651 char *temp_service_path;
2652 struct GNUNET_OS_Process **procarr;
2655 enum GNUNET_OS_ProcessStatusType type;
2656 unsigned long return_code;
2663 char *temp_transports;
2666 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2667 struct PeerConnection *conn_iter;
2669 static struct BlacklistContext blacklist_ctx;
2672 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2673 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2675 mytemp = GNUNET_DISK_mktemp ("blacklist");
2676 GNUNET_assert (mytemp != NULL);
2677 temp_file_handle = fopen (mytemp, "wt");
2678 GNUNET_assert (temp_file_handle != NULL);
2679 temp_transports = GNUNET_strdup (transports);
2681 blacklist_ctx.temp_file_handle = temp_file_handle;
2683 transport_len = strlen (temp_transports) + 1;
2686 for (i = 0; i < transport_len; i++)
2688 if ((temp_transports[i] == ' ') && (pos == NULL))
2689 continue; /* At start of string (whitespace) */
2690 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2692 temp_transports[i] = '\0';
2694 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
2695 while (conn_iter != NULL)
2697 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
2698 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
2699 conn_iter = conn_iter->next;
2703 blacklist_ctx.transport = pos;
2704 entry_count = GNUNET_CONTAINER_multihashmap_iterate (pg->
2706 [pg_iter].blacklisted_peers,
2707 &blacklist_file_iterator,
2711 } /* At beginning of actual string */
2712 else if (pos == NULL)
2714 pos = &temp_transports[i];
2718 GNUNET_free (temp_transports);
2719 fclose (temp_file_handle);
2722 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2723 daemon->cfg, "PATHS",
2725 &temp_service_path))
2727 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2729 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2730 "SERVICEHOME", "PATHS");
2731 if (UNLINK (mytemp) != 0)
2732 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2734 GNUNET_free (mytemp);
2738 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2740 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2741 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2746 _("Copying file with command cp %s %s\n"), mytemp, arg);
2751 else /* Remote, scp the file to the correct place */
2753 if (NULL != pg->peers[pg_iter].daemon->username)
2754 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
2755 pg->peers[pg_iter].daemon->username,
2756 pg->peers[pg_iter].daemon->hostname,
2759 GNUNET_asprintf (&arg, "%s:%s/blacklist",
2760 pg->peers[pg_iter].daemon->hostname,
2763 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2768 _("Copying file with command scp %s %s\n"), mytemp,
2773 GNUNET_free (temp_service_path);
2774 GNUNET_free (mytemp);
2778 ret = GNUNET_SYSERR;
2779 while ((count < max_wait) && (ret != GNUNET_OK))
2782 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2786 _("Checking copy status of file %d\n"), pg_iter);
2788 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2790 if (GNUNET_OS_process_status
2791 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2793 ret = GNUNET_SYSERR;
2795 else if ((type != GNUNET_OS_PROCESS_EXITED)
2796 || (return_code != 0))
2798 ret = GNUNET_SYSERR;
2802 GNUNET_OS_process_close (procarr[pg_iter]);
2803 procarr[pg_iter] = NULL;
2805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2806 _("File %d copied\n"), pg_iter);
2812 if (ret == GNUNET_SYSERR)
2814 /* FIXME: why sleep here? -CG */
2820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2821 _("Finished copying all blacklist files!\n"));
2823 GNUNET_free (procarr);
2827 /* Forward Declaration */
2829 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2832 * Choose a random peer's next connection to create, and
2833 * call schedule_connect to set up the connect task.
2835 * @param ct_ctx the overall connection context
2837 static void preschedule_connect(struct ConnectTopologyContext *ct_ctx)
2839 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
2840 struct PeerConnection *connection_iter;
2841 struct ConnectContext *connect_context;
2842 uint32_t random_peer;
2844 if (ct_ctx->remaining_connects_to_schedule == 0)
2846 random_peer = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
2847 while (pg->peers[random_peer].connect_peers_head == NULL)
2848 random_peer = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
2850 connection_iter = pg->peers[random_peer].connect_peers_head;
2851 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling connection between %d and %d\n", random_peer, connection_iter->index);
2853 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
2854 connect_context->first = pg->peers[random_peer].daemon;
2855 connect_context->second = pg->peers[connection_iter->index].daemon;
2856 connect_context->ct_ctx = ct_ctx;
2857 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
2858 GNUNET_CONTAINER_DLL_remove(pg->peers[random_peer].connect_peers_head, pg->peers[random_peer].connect_peers_tail, connection_iter);
2859 ct_ctx->remaining_connects_to_schedule--;
2864 * Internal notification of a connection, kept so that we can ensure some connections
2865 * happen instead of flooding all testing daemons with requests to connect.
2868 internal_connect_notify (void *cls,
2869 const struct GNUNET_PeerIdentity *first,
2870 const struct GNUNET_PeerIdentity *second,
2872 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2873 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2874 struct GNUNET_TESTING_Daemon *first_daemon,
2875 struct GNUNET_TESTING_Daemon *second_daemon,
2878 struct ConnectTopologyContext *ct_ctx = cls;
2879 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
2880 pg->outstanding_connects--;
2881 ct_ctx->remaining_connections--;
2882 if (ct_ctx->remaining_connections == 0)
2884 if (ct_ctx->notify_connections_done != NULL)
2885 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
2886 GNUNET_free (ct_ctx);
2889 preschedule_connect(ct_ctx);
2891 if (pg->notify_connection != NULL)
2892 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
2893 first_cfg, second_cfg, first_daemon, second_daemon,
2899 * Either delay a connection (because there are too many outstanding)
2900 * or schedule it for right now.
2902 * @param cls a connection context
2903 * @param tc the task runtime context
2906 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2908 struct ConnectContext *connect_context = cls;
2909 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
2911 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2914 if (pg->outstanding_connects > pg->max_outstanding_connections)
2916 #if VERBOSE_TESTING > 2
2917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2919 ("Delaying connect, we have too many outstanding connections!\n"));
2921 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2922 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
2923 &schedule_connect, connect_context);
2927 #if VERBOSE_TESTING > 2
2928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2929 _("Creating connection, outstanding_connections is %d\n"),
2930 outstanding_connects);
2932 pg->outstanding_connects++;
2933 pg->total_connects_scheduled++;
2934 GNUNET_TESTING_daemons_connect (connect_context->first,
2935 connect_context->second,
2936 connect_context->ct_ctx->connect_timeout,
2937 connect_context->ct_ctx->connect_attempts,
2938 &internal_connect_notify,
2939 connect_context->ct_ctx);
2940 GNUNET_free (connect_context);
2946 * Iterator for actually scheduling connections to be created
2947 * between two peers.
2949 * @param cls closure, a GNUNET_TESTING_Daemon
2950 * @param key the key the second Daemon was stored under
2951 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2953 * @return GNUNET_YES to continue iteration
2956 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2958 struct ConnectTopologyContext *ct_ctx = cls;
2959 struct PeerData *first = ct_ctx->first;
2960 struct GNUNET_TESTING_Daemon *second = value;
2961 struct ConnectContext *connect_context;
2963 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
2964 connect_context->first = first->daemon;
2965 connect_context->second = second;
2966 connect_context->ct_ctx = ct_ctx;
2967 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
2975 * Iterator for copying all entries in the allowed hashmap to the
2978 * @param cls closure, a GNUNET_TESTING_Daemon
2979 * @param key the key the second Daemon was stored under
2980 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2982 * @return GNUNET_YES to continue iteration
2985 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2987 struct PeerData *first = cls;
2989 GNUNET_assert (GNUNET_OK ==
2990 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
2992 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2999 * Make the peers to connect the same as those that are allowed to be
3002 * @param pg the peer group
3005 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
3007 unsigned int pg_iter;
3011 struct PeerConnection *iter;
3015 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3018 iter = pg->peers[pg_iter].allowed_peers_head;
3019 while (iter != NULL)
3021 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating connection between %d and %d\n", pg_iter, iter->index);
3022 total += add_connections(pg, pg_iter, iter->index, CONNECT);
3023 //total += add_actual_connections(pg, pg_iter, iter->index);
3028 GNUNET_CONTAINER_multihashmap_iterate (pg->
3029 peers[pg_iter].allowed_peers,
3030 ©_topology_iterator,
3031 &pg->peers[pg_iter]);
3033 if (GNUNET_SYSERR == ret)
3034 return GNUNET_SYSERR;
3036 total = total + ret;
3044 * Connect the topology as specified by the PeerConnection's
3045 * of each peer in the peer group
3047 * @param pg the peer group we are dealing with
3048 * @param connect_timeout how long try connecting two peers
3049 * @param connect_attempts how many times (max) to attempt
3050 * @param notify_callback callback to notify when finished
3051 * @param notify_cls closure for notify callback
3053 * @return the number of connections that will be attempted
3056 connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3057 struct GNUNET_TIME_Relative connect_timeout,
3058 unsigned int connect_attempts,
3059 GNUNET_TESTING_NotifyCompletion notify_callback,
3062 unsigned int pg_iter;
3064 struct ConnectTopologyContext *ct_ctx;
3066 struct PeerConnection *connection_iter;
3070 ct_ctx = GNUNET_malloc (sizeof (struct ConnectTopologyContext));
3071 ct_ctx->notify_connections_done = notify_callback;
3072 ct_ctx->notify_cls = notify_cls;
3075 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3078 connection_iter = pg->peers[pg_iter].connect_peers_head;
3079 while (connection_iter != NULL)
3081 connection_iter = connection_iter->next;
3086 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3092 GNUNET_free (ct_ctx);
3095 ct_ctx->connect_timeout = connect_timeout;
3096 ct_ctx->connect_attempts = connect_attempts;
3097 ct_ctx->remaining_connections = total;
3098 ct_ctx->remaining_connects_to_schedule = total;
3100 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3102 preschedule_connect(ct_ctx);
3110 * Takes a peer group and creates a topology based on the
3111 * one specified. Creates a topology means generates friend
3112 * files for the peers so they can only connect to those allowed
3113 * by the topology. This will only have an effect once peers
3114 * are started if the FRIENDS_ONLY option is set in the base
3115 * config. Also takes an optional restrict topology which
3116 * disallows connections based on particular transports
3117 * UNLESS they are specified in the restricted topology.
3119 * @param pg the peer group struct representing the running peers
3120 * @param topology which topology to connect the peers in
3121 * @param restrict_topology disallow restrict_transports transport
3122 * connections to peers NOT in this topology
3123 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3124 * @param restrict_transports space delimited list of transports to blacklist
3125 * to create restricted topology
3127 * @return the maximum number of connections were all allowed peers
3128 * connected to each other
3131 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
3132 enum GNUNET_TESTING_Topology topology,
3133 enum GNUNET_TESTING_Topology
3135 const char *restrict_transports)
3139 unsigned int num_connections;
3140 int unblacklisted_connections;
3145 for (i = 0; i < pg->total; i++)
3147 pg->peers[i].allowed_peers =
3148 GNUNET_CONTAINER_multihashmap_create (100);
3149 pg->peers[i].connect_peers =
3150 GNUNET_CONTAINER_multihashmap_create (100);
3151 pg->peers[i].blacklisted_peers =
3152 GNUNET_CONTAINER_multihashmap_create (100);
3153 pg->peers[i].pg = pg;
3159 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3163 num_connections = create_clique (pg, &add_connections, ALLOWED);
3165 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3168 _("Creating small world (ring) topology\n"));
3171 create_small_world_ring (pg, &add_connections, ALLOWED);
3173 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3176 _("Creating small world (2d-torus) topology\n"));
3178 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3180 case GNUNET_TESTING_TOPOLOGY_RING:
3182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3184 num_connections = create_ring (pg, &add_connections, ALLOWED);
3186 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3190 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3192 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3195 _("Creating Erdos-Renyi topology\n"));
3197 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3199 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3203 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3205 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3208 _("Creating Scale Free topology\n"));
3210 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3212 case GNUNET_TESTING_TOPOLOGY_LINE:
3214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3215 _("Creating straight line topology\n"));
3217 num_connections = create_line (pg, &add_connections, ALLOWED);
3219 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3222 _("Creating topology from file!\n"));
3225 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing", "topology_file",
3227 num_connections = create_from_file (pg, filename, &add_connections, ALLOWED);
3230 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3231 num_connections = 0;
3234 case GNUNET_TESTING_TOPOLOGY_NONE:
3236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3238 ("Creating no allowed topology (all peers can connect at core level)\n"));
3240 num_connections = pg->total * pg->total; /* Clique is allowed! */
3243 num_connections = 0;
3248 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
3250 ret = create_and_copy_friend_files (pg);
3251 if (ret != GNUNET_OK)
3254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3255 _("Failed during friend file copying!\n"));
3257 return GNUNET_SYSERR;
3262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3263 _("Friend files created/copied successfully!\n"));
3268 /* Use the create clique method to initially set all connections as blacklisted. */
3269 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
3270 create_clique (pg, &add_connections, BLACKLIST);
3272 unblacklisted_connections = 0;
3273 /* Un-blacklist connections as per the topology specified */
3274 switch (restrict_topology)
3276 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3279 _("Blacklisting all but clique topology\n"));
3281 unblacklisted_connections =
3282 create_clique (pg, &remove_connections, BLACKLIST);
3284 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3287 _("Blacklisting all but small world (ring) topology\n"));
3289 unblacklisted_connections =
3290 create_small_world_ring (pg, &remove_connections, BLACKLIST);
3292 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3296 ("Blacklisting all but small world (2d-torus) topology\n"));
3298 unblacklisted_connections =
3299 create_small_world (pg, &remove_connections, BLACKLIST);
3301 case GNUNET_TESTING_TOPOLOGY_RING:
3303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3304 _("Blacklisting all but ring topology\n"));
3306 unblacklisted_connections = create_ring (pg, &remove_connections, BLACKLIST);
3308 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3311 _("Blacklisting all but 2d torus topology\n"));
3313 unblacklisted_connections =
3314 create_2d_torus (pg, &remove_connections, BLACKLIST);
3316 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3319 _("Blacklisting all but Erdos-Renyi topology\n"));
3321 unblacklisted_connections =
3322 create_erdos_renyi (pg, &remove_connections, BLACKLIST);
3324 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3327 _("Blacklisting all but InterNAT topology\n"));
3329 unblacklisted_connections =
3330 create_nated_internet (pg, &remove_connections, BLACKLIST);
3332 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3335 _("Blacklisting all but Scale Free topology\n"));
3337 unblacklisted_connections =
3338 create_scale_free (pg, &remove_connections, BLACKLIST);
3340 case GNUNET_TESTING_TOPOLOGY_LINE:
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3343 _("Blacklisting all but straight line topology\n"));
3345 unblacklisted_connections = create_line (pg, &remove_connections, BLACKLIST);
3347 case GNUNET_TESTING_TOPOLOGY_NONE: /* Fall through */
3348 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3352 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
3354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3356 ("Creating blacklist topology from allowed\n"));
3357 unblacklisted_connections = copy_allowed (pg, &remove_connections);
3362 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
3364 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n", restrict_transports);
3365 ret = create_and_copy_blacklist_files (pg, restrict_transports);
3366 if (ret != GNUNET_OK)
3369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3370 _("Failed during blacklist file copying!\n"));
3377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3378 _("Blacklist files created/copied successfully!\n"));
3382 return num_connections;
3388 * Iterator for choosing random peers to connect.
3390 * @param cls closure, a RandomContext
3391 * @param key the key the second Daemon was stored under
3392 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3394 * @return GNUNET_YES to continue iteration
3397 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3399 struct RandomContext *random_ctx = cls;
3400 double random_number;
3401 uint32_t second_pos;
3402 GNUNET_HashCode first_hash;
3405 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3406 UINT64_MAX)) / ((double) UINT64_MAX);
3407 if (random_number < random_ctx->percentage)
3409 GNUNET_assert (GNUNET_OK ==
3410 GNUNET_CONTAINER_multihashmap_put (random_ctx->
3411 first->connect_peers_working_set,
3413 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3416 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
3417 uid_from_hash (key, &second_pos);
3418 hash_from_uid (random_ctx->first_uid, &first_hash);
3419 GNUNET_assert (random_ctx->pg->total > second_pos);
3420 GNUNET_assert (GNUNET_YES ==
3421 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
3423 [second_pos].connect_peers,
3433 * Iterator for adding at least X peers to a peers connection set.
3435 * @param cls closure, MinimumContext
3436 * @param key the key the second Daemon was stored under
3437 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3439 * @return GNUNET_YES to continue iteration
3442 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3444 struct MinimumContext *min_ctx = cls;
3445 uint32_t second_pos;
3446 GNUNET_HashCode first_hash;
3449 if (GNUNET_CONTAINER_multihashmap_size
3450 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
3452 for (i = 0; i < min_ctx->num_to_add; i++)
3454 if (min_ctx->pg_array[i] == min_ctx->current)
3456 GNUNET_assert (GNUNET_OK ==
3457 GNUNET_CONTAINER_multihashmap_put
3458 (min_ctx->first->connect_peers_working_set, key,
3460 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3461 uid_from_hash (key, &second_pos);
3462 hash_from_uid (min_ctx->first_uid, &first_hash);
3463 GNUNET_assert (min_ctx->pg->total > second_pos);
3464 GNUNET_assert (GNUNET_OK ==
3465 GNUNET_CONTAINER_multihashmap_put (min_ctx->
3467 [second_pos].connect_peers_working_set,
3471 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3472 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
3473 GNUNET_assert (GNUNET_YES ==
3474 GNUNET_CONTAINER_multihashmap_remove
3475 (min_ctx->pg->peers[second_pos].connect_peers,
3476 &first_hash, min_ctx->first->daemon));
3483 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
3489 * Iterator for adding peers to a connection set based on a depth first search.
3491 * @param cls closure, MinimumContext
3492 * @param key the key the second daemon was stored under
3493 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3495 * @return GNUNET_YES to continue iteration
3498 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3500 struct DFSContext *dfs_ctx = cls;
3501 GNUNET_HashCode first_hash;
3503 if (dfs_ctx->current == dfs_ctx->chosen)
3505 GNUNET_assert (GNUNET_OK ==
3506 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3507 first->connect_peers_working_set,
3509 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3510 uid_from_hash (key, &dfs_ctx->second_uid);
3511 hash_from_uid (dfs_ctx->first_uid, &first_hash);
3512 GNUNET_assert (GNUNET_OK ==
3513 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3515 [dfs_ctx->second_uid].connect_peers_working_set,
3519 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3520 GNUNET_assert (GNUNET_YES ==
3521 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
3523 [dfs_ctx->second_uid].connect_peers,
3527 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
3528 return GNUNET_NO; /* We have found our peer, don't iterate more */
3537 * From the set of connections possible, choose percentage percent of connections
3538 * to actually connect.
3540 * @param pg the peergroup we are dealing with
3541 * @param percentage what percent of total connections to make
3544 choose_random_connections (struct GNUNET_TESTING_PeerGroup *pg,
3547 struct RandomContext random_ctx;
3550 struct PeerConnection *temp_peers;
3551 struct PeerConnection *conn_iter;
3552 double random_number;
3555 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3557 random_ctx.first_uid = pg_iter;
3558 random_ctx.first = &pg->peers[pg_iter];
3559 random_ctx.percentage = percentage;
3563 conn_iter = pg->peers[pg_iter].connect_peers_head;
3564 while (conn_iter != NULL)
3568 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3569 UINT64_MAX)) / ((double) UINT64_MAX);
3570 if (random_number < percentage)
3572 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
3574 conn_iter = conn_iter->next;
3577 pg->peers[pg_iter].connect_peers_working_set =
3578 GNUNET_CONTAINER_multihashmap_create (pg->total);
3579 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3580 &random_connect_iterator,
3582 /* Now remove the old connections */
3583 GNUNET_CONTAINER_multihashmap_destroy (pg->
3584 peers[pg_iter].connect_peers);
3585 /* And replace with the random set */
3586 pg->peers[pg_iter].connect_peers =
3587 pg->peers[pg_iter].connect_peers_working_set;
3591 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3593 conn_iter = pg->peers[pg_iter].connect_peers_head;
3594 while (pg->peers[pg_iter].connect_peers_head != NULL)
3595 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
3597 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
3598 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
3599 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
3600 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
3606 * Count the number of connections in a linked list of connections.
3608 * @param conn_list the connection list to get the count of
3610 * @return the number of elements in the list
3613 count_connections (struct PeerConnection *conn_list)
3615 struct PeerConnection *iter;
3619 while (iter != NULL)
3629 count_workingset_connections (struct GNUNET_TESTING_PeerGroup *pg)
3632 unsigned int pg_iter;
3634 struct PeerConnection *conn_iter;
3638 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3641 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
3642 while (conn_iter != NULL)
3645 conn_iter = conn_iter->next;
3649 GNUNET_CONTAINER_multihashmap_size (pg->
3651 [pg_iter].connect_peers_working_set);
3660 count_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg)
3663 unsigned int pg_iter;
3665 struct PeerConnection *conn_iter;
3669 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3672 conn_iter = pg->peers[pg_iter].allowed_peers_head;
3673 while (conn_iter != NULL)
3676 conn_iter = conn_iter->next;
3680 GNUNET_CONTAINER_multihashmap_size (pg->
3682 [pg_iter].allowed_peers);
3691 * From the set of connections possible, choose at least num connections per
3694 * @param pg the peergroup we are dealing with
3695 * @param num how many connections at least should each peer have (if possible)?
3698 choose_minimum (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3701 struct MinimumContext minimum_ctx;
3703 struct PeerConnection *conn_iter;
3704 unsigned int temp_list_size;
3707 uint32_t random; /* Random list entry to connect peer to */
3712 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3714 temp_list_size = count_connections(pg->peers[pg_iter].connect_peers_head);
3715 if (temp_list_size == 0)
3717 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer %d has 0 connections!?!?\n", pg_iter);
3720 for (i = 0; i < num; i++)
3722 random = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, temp_list_size);
3723 conn_iter = pg->peers[pg_iter].connect_peers_head;
3724 for (count = 0; count < random; count++)
3725 conn_iter = conn_iter->next;
3726 /* We now have a random connection, connect it! */
3727 GNUNET_assert(conn_iter != NULL);
3728 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
3732 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3734 pg->peers[pg_iter].connect_peers_working_set =
3735 GNUNET_CONTAINER_multihashmap_create (num);
3738 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3740 minimum_ctx.first_uid = pg_iter;
3741 minimum_ctx.pg_array =
3742 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
3743 GNUNET_CONTAINER_multihashmap_size
3744 (pg->peers[pg_iter].connect_peers));
3745 minimum_ctx.first = &pg->peers[pg_iter];
3746 minimum_ctx.pg = pg;
3747 minimum_ctx.num_to_add = num;
3748 minimum_ctx.current = 0;
3749 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3750 &minimum_connect_iterator,
3754 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3756 /* Remove the "old" connections */
3757 GNUNET_CONTAINER_multihashmap_destroy (pg->
3758 peers[pg_iter].connect_peers);
3759 /* And replace with the working set */
3760 pg->peers[pg_iter].connect_peers =
3761 pg->peers[pg_iter].connect_peers_working_set;
3764 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3766 while (pg->peers[pg_iter].connect_peers_head != NULL)
3767 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
3769 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
3770 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
3771 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
3772 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
3777 struct FindClosestContext
3780 * The currently known closest peer.
3782 struct GNUNET_TESTING_Daemon *closest;
3785 * The info for the peer we are adding connections for.
3787 struct PeerData *curr_peer;
3790 * The distance (bits) between the current
3791 * peer and the currently known closest.
3793 unsigned int closest_dist;
3796 * The offset of the closest known peer in
3799 unsigned int closest_num;
3803 * Iterator over hash map entries of the allowed
3804 * peer connections. Find the closest, not already
3805 * connected peer and return it.
3807 * @param cls closure (struct FindClosestContext)
3808 * @param key current key code (hash of offset in pg)
3809 * @param value value in the hash map - a GNUNET_TESTING_Daemon
3810 * @return GNUNET_YES if we should continue to
3815 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
3817 struct FindClosestContext *closest_ctx = cls;
3818 struct GNUNET_TESTING_Daemon *daemon = value;
3820 if (((closest_ctx->closest == NULL) ||
3821 (GNUNET_CRYPTO_hash_matching_bits
3822 (&daemon->id.hashPubKey,
3823 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
3824 closest_ctx->closest_dist))
3826 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
3827 curr_peer->connect_peers,
3830 closest_ctx->closest_dist =
3831 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
3832 &closest_ctx->curr_peer->daemon->
3834 closest_ctx->closest = daemon;
3835 uid_from_hash (key, &closest_ctx->closest_num);
3842 * From the set of connections possible, choose at num connections per
3843 * peer based on depth which are closest out of those allowed. Guaranteed
3844 * to add num peers to connect to, provided there are that many peers
3845 * in the underlay topology to connect to.
3847 * @param pg the peergroup we are dealing with
3848 * @param num how many connections at least should each peer have (if possible)?
3849 * @param proc processor to actually add the connections
3850 * @param list the peer list to use
3853 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
3854 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
3859 struct FindClosestContext closest_ctx;
3864 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
3866 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3868 closest_ctx.curr_peer = &pg->peers[pg_iter];
3869 closest_ctx.closest = NULL;
3870 closest_ctx.closest_dist = 0;
3871 closest_ctx.closest_num = 0;
3872 GNUNET_CONTAINER_multihashmap_iterate (pg->
3873 peers[pg_iter].allowed_peers,
3874 &find_closest_peers,
3876 if (closest_ctx.closest != NULL)
3878 GNUNET_assert (closest_ctx.closest_num < pg->total);
3879 proc (pg, pg_iter, closest_ctx.closest_num, list);
3887 * From the set of connections possible, choose at least num connections per
3888 * peer based on depth first traversal of peer connections. If DFS leaves
3889 * peers unconnected, ensure those peers get connections.
3891 * @param pg the peergroup we are dealing with
3892 * @param num how many connections at least should each peer have (if possible)?
3895 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3899 uint32_t starting_peer;
3900 uint32_t least_connections;
3901 uint32_t random_connection;
3903 unsigned int temp_count;
3904 struct PeerConnection *peer_iter;
3906 struct DFSContext dfs_ctx;
3907 GNUNET_HashCode second_hash;
3913 while ((count_workingset_connections (pg) < num * pg->total)
3914 && (count_allowed_connections (pg) > 0))
3916 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3918 least_connections = -1; /* Set to very high number */
3919 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3921 temp_count = count_connections(pg->peers[pg_iter].connect_peers_working_set_head);
3922 if (temp_count < least_connections)
3924 starting_peer = pg_iter;
3925 least_connections = temp_count;
3930 temp_count = count_connections(pg->peers[starting_peer].connect_peers_head);
3931 if (temp_count == 0)
3932 continue; /* FIXME: infinite loop? */
3934 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, temp_count);
3936 peer_iter = pg->peers[starting_peer].connect_peers_head;
3937 while (temp_count < random_connection)
3939 peer_iter = peer_iter->next;
3942 GNUNET_assert(peer_iter != NULL);
3943 add_connections(pg, starting_peer, peer_iter->index, WORKING_SET);
3944 remove_connections(pg, starting_peer, peer_iter->index, CONNECT);
3945 starting_peer = peer_iter->index;
3950 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3952 pg->peers[pg_iter].connect_peers_working_set =
3953 GNUNET_CONTAINER_multihashmap_create (num);
3958 while ((count_workingset_connections (pg) < num * pg->total)
3959 && (count_allowed_connections (pg) > 0))
3961 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3963 least_connections = -1; /* Set to very high number */
3964 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3966 if (GNUNET_CONTAINER_multihashmap_size
3967 (pg->peers[pg_iter].connect_peers_working_set) <
3970 starting_peer = pg_iter;
3972 GNUNET_CONTAINER_multihashmap_size (pg->
3974 [pg_iter].connect_peers_working_set);
3979 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
3985 /* Choose a random peer from the chosen peers set of connections to add */
3987 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3988 GNUNET_CONTAINER_multihashmap_size
3989 (pg->peers[starting_peer].connect_peers));
3990 dfs_ctx.first_uid = starting_peer;
3991 dfs_ctx.first = &pg->peers[starting_peer];
3993 dfs_ctx.current = 0;
3995 GNUNET_CONTAINER_multihashmap_iterate (pg->
3997 [starting_peer].connect_peers,
3998 &dfs_connect_iterator, &dfs_ctx);
3999 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4000 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4001 GNUNET_assert (GNUNET_YES ==
4002 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4003 [starting_peer].connect_peers,
4007 [dfs_ctx.second_uid].daemon));
4008 starting_peer = dfs_ctx.second_uid;
4011 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4013 /* Remove the "old" connections */
4014 GNUNET_CONTAINER_multihashmap_destroy (pg->
4015 peers[pg_iter].connect_peers);
4016 /* And replace with the working set */
4017 pg->peers[pg_iter].connect_peers =
4018 pg->peers[pg_iter].connect_peers_working_set;
4025 * Internal callback for topology information for a particular peer.
4028 internal_topology_callback (void *cls,
4029 const struct GNUNET_PeerIdentity *peer,
4030 const struct GNUNET_TRANSPORT_ATS_Information
4033 struct CoreContext *core_ctx = cls;
4034 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4036 if (peer == NULL) /* Either finished, or something went wrong */
4038 iter_ctx->completed++;
4039 iter_ctx->connected--;
4040 /* One core context allocated per iteration, must free! */
4041 GNUNET_free (core_ctx);
4045 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id,
4049 if (iter_ctx->completed == iter_ctx->total)
4051 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4052 /* Once all are done, free the iteration context */
4053 GNUNET_free (iter_ctx);
4059 * Check running topology iteration tasks, if below max start a new one, otherwise
4060 * schedule for some time in the future.
4063 schedule_get_topology (void *cls,
4064 const struct GNUNET_SCHEDULER_TaskContext *tc)
4066 struct CoreContext *core_context = cls;
4067 struct TopologyIterateContext *topology_context =
4068 (struct TopologyIterateContext *) core_context->iter_context;
4069 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4072 if (topology_context->connected > topology_context->pg->max_outstanding_connections)
4074 #if VERBOSE_TESTING > 2
4075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4077 ("Delaying connect, we have too many outstanding connections!\n"));
4079 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4080 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4081 &schedule_get_topology, core_context);
4085 #if VERBOSE_TESTING > 2
4086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4087 _("Creating connection, outstanding_connections is %d\n"),
4088 outstanding_connects);
4090 topology_context->connected++;
4093 GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4094 &internal_topology_callback,
4097 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4098 internal_topology_callback (core_context, NULL, NULL);
4104 * Iterate over all (running) peers in the peer group, retrieve
4105 * all connections that each currently has.
4108 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
4109 GNUNET_TESTING_NotifyTopology cb, void *cls)
4111 struct TopologyIterateContext *topology_context;
4112 struct CoreContext *core_ctx;
4114 unsigned int total_count;
4116 /* Allocate a single topology iteration context */
4117 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4118 topology_context->topology_cb = cb;
4119 topology_context->cls = cls;
4120 topology_context->pg = pg;
4122 for (i = 0; i < pg->total; i++)
4124 if (pg->peers[i].daemon->running == GNUNET_YES)
4126 /* Allocate one core context per core we need to connect to */
4127 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4128 core_ctx->daemon = pg->peers[i].daemon;
4129 /* Set back pointer to topology iteration context */
4130 core_ctx->iter_context = topology_context;
4131 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4135 if (total_count == 0)
4137 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4138 GNUNET_free (topology_context);
4141 topology_context->total = total_count;
4146 * Callback function to process statistic values.
4147 * This handler is here only really to insert a peer
4148 * identity (or daemon) so the statistics can be uniquely
4149 * tied to a single running peer.
4151 * @param cls closure
4152 * @param subsystem name of subsystem that created the statistic
4153 * @param name the name of the datum
4154 * @param value the current value
4155 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4156 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4159 internal_stats_callback (void *cls,
4160 const char *subsystem,
4161 const char *name, uint64_t value, int is_persistent)
4163 struct StatsCoreContext *core_context = cls;
4164 struct StatsIterateContext *stats_context =
4165 (struct StatsIterateContext *) core_context->iter_context;
4167 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4168 subsystem, name, value, is_persistent);
4172 * Internal continuation call for statistics iteration.
4174 * @param cls closure, the CoreContext for this iteration
4175 * @param success whether or not the statistics iterations
4176 * was canceled or not (we don't care)
4179 internal_stats_cont (void *cls, int success)
4181 struct StatsCoreContext *core_context = cls;
4182 struct StatsIterateContext *stats_context =
4183 (struct StatsIterateContext *) core_context->iter_context;
4185 stats_context->connected--;
4186 stats_context->completed++;
4188 if (stats_context->completed == stats_context->total)
4190 stats_context->cont (stats_context->cls, GNUNET_YES);
4191 GNUNET_free (stats_context);
4194 if (core_context->stats_handle != NULL)
4195 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4197 GNUNET_free (core_context);
4201 * Check running topology iteration tasks, if below max start a new one, otherwise
4202 * schedule for some time in the future.
4205 schedule_get_statistics (void *cls,
4206 const struct GNUNET_SCHEDULER_TaskContext *tc)
4208 struct StatsCoreContext *core_context = cls;
4209 struct StatsIterateContext *stats_context =
4210 (struct StatsIterateContext *) core_context->iter_context;
4212 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4215 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
4217 #if VERBOSE_TESTING > 2
4218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4220 ("Delaying connect, we have too many outstanding connections!\n"));
4222 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4223 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4224 &schedule_get_statistics, core_context);
4228 #if VERBOSE_TESTING > 2
4229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4230 _("Creating connection, outstanding_connections is %d\n"),
4231 outstanding_connects);
4234 stats_context->connected++;
4235 core_context->stats_handle =
4236 GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
4237 if (core_context->stats_handle == NULL)
4239 internal_stats_cont (core_context, GNUNET_NO);
4243 core_context->stats_get_handle =
4244 GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
4245 GNUNET_TIME_relative_get_forever (),
4246 &internal_stats_cont, &internal_stats_callback,
4248 if (core_context->stats_get_handle == NULL)
4249 internal_stats_cont (core_context, GNUNET_NO);
4254 struct DuplicateStats
4257 * Next item in the list
4259 struct DuplicateStats *next;
4262 * Nasty string, concatenation of relevant information.
4264 char *unique_string;
4268 * Check whether the combination of port/host/unix domain socket
4269 * already exists in the list of peers being checked for statistics.
4271 * @param pg the peergroup in question
4272 * @param specific_peer the peer we're concerned with
4273 * @param stats_list the list to return to the caller
4275 * @return GNUNET_YES if the statistics instance has been seen already,
4276 * GNUNET_NO if not (and we may have added it to the list)
4279 stats_check_existing (struct GNUNET_TESTING_PeerGroup *pg,
4280 struct PeerData *specific_peer,
4281 struct DuplicateStats **stats_list)
4283 struct DuplicateStats *pos;
4284 char *unix_domain_socket;
4285 unsigned long long port;
4288 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
4289 "single_statistics_per_host"))
4290 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
4294 GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg, "statistics",
4295 "unixpath", &unix_domain_socket))
4299 GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg, "statistics",
4302 GNUNET_free(unix_domain_socket);
4306 if (specific_peer->daemon->hostname != NULL)
4307 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
4308 unix_domain_socket, port);
4310 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
4314 if (0 == strcmp (to_match, pos->unique_string))
4316 GNUNET_free (unix_domain_socket);
4317 GNUNET_free (to_match);
4322 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
4323 pos->unique_string = to_match;
4324 pos->next = *stats_list;
4326 GNUNET_free (unix_domain_socket);
4331 * Iterate over all (running) peers in the peer group, retrieve
4332 * all statistics from each.
4335 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
4336 GNUNET_STATISTICS_Callback cont,
4337 GNUNET_TESTING_STATISTICS_Iterator proc,
4340 struct StatsIterateContext *stats_context;
4341 struct StatsCoreContext *core_ctx;
4343 unsigned int total_count;
4344 struct DuplicateStats *stats_list;
4345 struct DuplicateStats *pos;
4348 /* Allocate a single stats iteration context */
4349 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
4350 stats_context->cont = cont;
4351 stats_context->proc = proc;
4352 stats_context->cls = cls;
4353 stats_context->pg = pg;
4356 for (i = 0; i < pg->total; i++)
4358 if ((pg->peers[i].daemon->running == GNUNET_YES)
4360 stats_check_existing (pg, &pg->peers[i], &stats_list)))
4362 /* Allocate one core context per core we need to connect to */
4363 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
4364 core_ctx->daemon = pg->peers[i].daemon;
4365 /* Set back pointer to topology iteration context */
4366 core_ctx->iter_context = stats_context;
4367 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
4372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4373 "Retrieving stats from %u total instances.\n", total_count);
4374 stats_context->total = total_count;
4375 if (stats_list != NULL)
4380 GNUNET_free (pos->unique_string);
4381 stats_list = pos->next;
4383 pos = stats_list->next;
4390 * There are many ways to connect peers that are supported by this function.
4391 * To connect peers in the same topology that was created via the
4392 * GNUNET_TESTING_create_topology, the topology variable must be set to
4393 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
4394 * a new instance of that topology will be generated and attempted to be
4395 * connected. This could result in some connections being impossible,
4396 * because some topologies are non-deterministic.
4398 * @param pg the peer group struct representing the running peers
4399 * @param topology which topology to connect the peers in
4400 * @param options options for connecting the topology
4401 * @param option_modifier modifier for options that take a parameter
4402 * @param connect_timeout how long to wait before giving up on connecting
4404 * @param connect_attempts how many times to attempt to connect two peers
4405 * over the connect_timeout duration
4406 * @param notify_callback notification to be called once all connections completed
4407 * @param notify_cls closure for notification callback
4409 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
4412 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
4413 enum GNUNET_TESTING_Topology topology,
4414 enum GNUNET_TESTING_TopologyOption options,
4415 double option_modifier,
4416 struct GNUNET_TIME_Relative connect_timeout,
4417 unsigned int connect_attempts,
4418 GNUNET_TESTING_NotifyCompletion
4419 notify_callback, void *notify_cls)
4423 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4424 #if VERBOSE_TOPOLOGY
4425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4426 _("Creating clique CONNECT topology\n"));
4428 create_clique (pg, &add_connections, CONNECT);
4430 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4431 #if VERBOSE_TOPOLOGY
4432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4433 _("Creating small world (ring) CONNECT topology\n"));
4435 create_small_world_ring (pg, &add_connections, CONNECT);
4437 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4438 #if VERBOSE_TOPOLOGY
4439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4440 _("Creating small world (2d-torus) CONNECT topology\n"));
4442 create_small_world (pg, &add_connections, CONNECT);
4444 case GNUNET_TESTING_TOPOLOGY_RING:
4445 #if VERBOSE_TOPOLOGY
4446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4447 _("Creating ring CONNECT topology\n"));
4449 create_ring (pg, &add_connections, CONNECT);
4451 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4452 #if VERBOSE_TOPOLOGY
4453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4454 _("Creating 2d torus CONNECT topology\n"));
4456 create_2d_torus (pg, &add_connections, CONNECT);
4458 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4459 #if VERBOSE_TOPOLOGY
4460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4461 _("Creating Erdos-Renyi CONNECT topology\n"));
4463 create_erdos_renyi (pg, &add_connections, CONNECT);
4465 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4466 #if VERBOSE_TOPOLOGY
4467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4468 _("Creating InterNAT CONNECT topology\n"));
4470 create_nated_internet (pg, &add_connections, CONNECT);
4472 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4473 #if VERBOSE_TOPOLOGY
4474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4475 _("Creating Scale Free CONNECT topology\n"));
4477 create_scale_free (pg, &add_connections, CONNECT);
4479 case GNUNET_TESTING_TOPOLOGY_LINE:
4480 #if VERBOSE_TOPOLOGY
4481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4482 _("Creating straight line CONNECT topology\n"));
4484 create_line (pg, &add_connections, CONNECT);
4486 case GNUNET_TESTING_TOPOLOGY_NONE:
4487 #if VERBOSE_TOPOLOGY
4488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4489 _("Creating no CONNECT topology\n"));
4491 copy_allowed_topology (pg);
4494 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4496 ("Unknown topology specification, can't connect peers!\n"));
4497 return GNUNET_SYSERR;
4502 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
4503 #if VERBOSE_TOPOLOGY
4504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4506 ("Connecting random subset (%'.2f percent) of possible peers\n"),
4507 100 * option_modifier);
4509 choose_random_connections (pg, option_modifier);
4511 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
4512 #if VERBOSE_TOPOLOGY
4513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4514 _("Connecting a minimum of %u peers each (if possible)\n"),
4515 (unsigned int) option_modifier);
4517 choose_minimum (pg, (unsigned int) option_modifier);
4519 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
4520 #if VERBOSE_TOPOLOGY
4521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4523 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
4524 (unsigned int) option_modifier);
4527 perform_dfs (pg, (int) option_modifier);
4530 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
4531 #if VERBOSE_TOPOLOGY
4532 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4534 ("Finding additional %u closest peers each (if possible)\n"),
4535 (unsigned int) option_modifier);
4538 add_closest (pg, (unsigned int) option_modifier,
4539 &add_connections, CONNECT);
4542 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
4544 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
4550 return connect_topology (pg, connect_timeout, connect_attempts, notify_callback, notify_cls);
4554 * Callback that is called whenever a hostkey is generated
4555 * for a peer. Call the real callback and decrement the
4556 * starting counter for the peergroup.
4558 * @param cls closure
4559 * @param id identifier for the daemon, NULL on error
4560 * @param d handle for the daemon
4561 * @param emsg error message (NULL on success)
4564 internal_hostkey_callback (void *cls,
4565 const struct GNUNET_PeerIdentity *id,
4566 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4568 struct InternalStartContext *internal_context = cls;
4569 internal_context->peer->pg->starting--;
4570 internal_context->peer->pg->started++;
4571 if (internal_context->hostkey_callback != NULL)
4572 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
4574 else if (internal_context->peer->pg->started ==
4575 internal_context->peer->pg->total)
4577 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
4578 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
4583 * Callback that is called whenever a peer has finished starting.
4584 * Call the real callback and decrement the starting counter
4585 * for the peergroup.
4587 * @param cls closure
4588 * @param id identifier for the daemon, NULL on error
4590 * @param d handle for the daemon
4591 * @param emsg error message (NULL on success)
4594 internal_startup_callback (void *cls,
4595 const struct GNUNET_PeerIdentity *id,
4596 const struct GNUNET_CONFIGURATION_Handle *cfg,
4597 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4599 struct InternalStartContext *internal_context = cls;
4600 internal_context->peer->pg->starting--;
4601 if (internal_context->start_cb != NULL)
4602 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
4607 internal_continue_startup (void *cls,
4608 const struct GNUNET_SCHEDULER_TaskContext *tc)
4610 struct InternalStartContext *internal_context = cls;
4612 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4617 if (internal_context->peer->pg->starting < internal_context->peer->pg->max_outstanding_connections)
4619 internal_context->peer->pg->starting++;
4620 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
4624 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4625 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4626 &internal_continue_startup,
4633 * Callback for informing us about a successful
4634 * or unsuccessful churn start call.
4636 * @param cls a ChurnContext
4637 * @param id the peer identity of the started peer
4638 * @param cfg the handle to the configuration of the peer
4639 * @param d handle to the daemon for the peer
4640 * @param emsg NULL on success, non-NULL on failure
4644 churn_start_callback (void *cls,
4645 const struct GNUNET_PeerIdentity *id,
4646 const struct GNUNET_CONFIGURATION_Handle *cfg,
4647 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4649 struct ChurnRestartContext *startup_ctx = cls;
4650 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
4652 unsigned int total_left;
4653 char *error_message;
4655 error_message = NULL;
4658 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4659 "Churn stop callback failed with error `%s'\n", emsg);
4660 churn_ctx->num_failed_start++;
4664 churn_ctx->num_to_start--;
4668 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4669 "Started peer, %d left.\n", churn_ctx->num_to_start);
4673 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
4674 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4676 if (total_left == 0)
4678 if ((churn_ctx->num_failed_stop > 0)
4679 || (churn_ctx->num_failed_start > 0))
4680 GNUNET_asprintf (&error_message,
4681 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4682 churn_ctx->num_failed_start,
4683 churn_ctx->num_failed_stop);
4684 churn_ctx->cb (churn_ctx->cb_cls, error_message);
4685 GNUNET_free_non_null (error_message);
4686 GNUNET_free (churn_ctx);
4687 GNUNET_free (startup_ctx);
4693 schedule_churn_restart (void *cls,
4694 const struct GNUNET_SCHEDULER_TaskContext *tc)
4696 struct PeerRestartContext *peer_restart_ctx = cls;
4697 struct ChurnRestartContext *startup_ctx =
4698 peer_restart_ctx->churn_restart_ctx;
4700 if (startup_ctx->outstanding > startup_ctx->pg->max_outstanding_connections)
4701 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4702 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4703 &schedule_churn_restart, peer_restart_ctx);
4706 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
4707 startup_ctx->timeout,
4708 &churn_start_callback,
4710 GNUNET_free (peer_restart_ctx);
4715 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4717 struct InternalStartContext *internal_context = cls;
4719 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4724 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
4726 internal_context->peer->pg->starting++;
4727 internal_context->peer->daemon =
4728 GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
4729 internal_context->timeout,
4730 internal_context->hostname,
4731 internal_context->username,
4732 internal_context->sshport,
4733 internal_context->hostkey,
4734 &internal_hostkey_callback,
4736 &internal_startup_callback,
4741 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4742 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4743 &internal_start, internal_context);
4748 * Function which continues a peer group starting up
4749 * after successfully generating hostkeys for each peer.
4751 * @param pg the peer group to continue starting
4755 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg)
4760 for (i = 0; i < pg->total; i++)
4762 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
4763 &pg->peers[i].internal_context);
4764 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
4769 * Start count gnunet instances with the same set of transports and
4770 * applications. The port numbers (any option called "PORT") will be
4771 * adjusted to ensure that no two peers running on the same system
4772 * have the same port(s) in their respective configurations.
4774 * @param cfg configuration template to use
4775 * @param total number of daemons to start
4776 * @param max_concurrent_connections for testing, how many peers can
4777 * we connect to simultaneously
4778 * @param timeout total time allowed for peers to start
4779 * @param hostkey_callback function to call on each peers hostkey generation
4780 * if NULL, peers will be started by this call, if non-null,
4781 * GNUNET_TESTING_daemons_continue_startup must be called after
4782 * successful hostkey generation
4783 * @param hostkey_cls closure for hostkey callback
4784 * @param cb function to call on each daemon that was started
4785 * @param cb_cls closure for cb
4786 * @param connect_callback function to call each time two hosts are connected
4787 * @param connect_callback_cls closure for connect_callback
4788 * @param hostnames linked list of host structs to use to start peers on
4789 * (NULL to run on localhost only)
4791 * @return NULL on error, otherwise handle to control peer group
4793 struct GNUNET_TESTING_PeerGroup *
4794 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
4796 unsigned int max_concurrent_connections,
4797 struct GNUNET_TIME_Relative timeout,
4798 GNUNET_TESTING_NotifyHostkeyCreated
4799 hostkey_callback, void *hostkey_cls,
4800 GNUNET_TESTING_NotifyDaemonRunning cb,
4802 GNUNET_TESTING_NotifyConnection
4803 connect_callback, void *connect_callback_cls,
4804 const struct GNUNET_TESTING_Host *hostnames)
4806 struct GNUNET_TESTING_PeerGroup *pg;
4807 const struct GNUNET_TESTING_Host *hostpos;
4813 const char *hostname;
4814 const char *username;
4815 char *baseservicehome;
4816 char *newservicehome;
4818 char *hostkeys_file;
4821 struct GNUNET_DISK_FileHandle *fd;
4822 struct GNUNET_CONFIGURATION_Handle *pcfg;
4824 unsigned int hostcnt;
4831 uint64_t total_hostkeys;
4832 struct GNUNET_OS_Process *proc;
4842 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
4844 pg->notify_connection = connect_callback;
4845 pg->notify_connection_cls = connect_callback_cls;
4847 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
4848 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
4849 pg->max_outstanding_connections = max_concurrent_connections;
4850 if (NULL != hostnames)
4853 hostpos = hostnames;
4854 while (hostpos != NULL)
4856 hostpos = hostpos->next;
4859 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4862 hostpos = hostnames;
4863 while (hostpos != NULL)
4865 pg->hosts[off].minport = LOW_PORT;
4866 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
4867 if (hostpos->username != NULL)
4868 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
4869 pg->hosts[off].sshport = hostpos->port;
4870 hostpos = hostpos->next;
4880 pg->num_hosts = off;
4884 /* skip leading spaces */
4885 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
4888 while ('\0' != *rpos)
4890 if (isspace ((unsigned char) *rpos))
4894 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4896 start = GNUNET_strdup (hostnames);
4898 while ('\0' != *pos)
4900 if (isspace ((unsigned char) *pos))
4903 if (strlen (start) > 0)
4905 pg->hosts[off].minport = LOW_PORT;
4906 pg->hosts[off++].hostname = start;
4912 if (strlen (start) > 0)
4914 pg->hosts[off].minport = LOW_PORT;
4915 pg->hosts[off++].hostname = start;
4919 GNUNET_free (start);
4920 GNUNET_free (pg->hosts);
4924 minport = 0; /* make gcc happy */
4933 /* Create the servicehome directory for each remote peer */
4934 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
4936 for (i = 0; i < pg->num_hosts; i++)
4939 if (NULL != pg->hosts[i].username)
4940 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username, pg->hosts[i].hostname);
4942 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
4943 if (pg->hosts[i].sshport != 0)
4945 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
4946 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
4947 "ssh", "-P", ssh_port_str,
4951 arg, "mkdir -p", baseservicehome, NULL);
4954 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
4955 "ssh", arg, "mkdir -p", baseservicehome, NULL);
4956 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating remote dir with command ssh %s %s %s\n", arg, " mkdir -p ", baseservicehome);
4957 GNUNET_OS_process_wait(proc);
4959 GNUNET_free(baseservicehome);
4961 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "HOSTKEYSFILE",
4964 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
4965 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!\n");
4968 /* Check hostkey file size, read entire thing into memory */
4969 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
4970 GNUNET_DISK_PERM_NONE);
4973 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
4977 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
4980 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Found file size %llu for hostkeys, expect hostkeys to be size %d\n", fs, HOSTKEYFILESIZE);
4982 if (fs % HOSTKEYFILESIZE != 0)
4984 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "File size %llu seems incorrect for hostkeys...\n", fs);
4988 total_hostkeys = fs / HOSTKEYFILESIZE;
4989 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Will read %llu hostkeys from file\n", total_hostkeys);
4990 pg->hostkey_data = GNUNET_malloc_large (fs);
4991 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
4992 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
4995 GNUNET_free(hostkeys_file);
4998 for (off = 0; off < total; off++)
5002 hostname = pg->hosts[off % hostcnt].hostname;
5003 username = pg->hosts[off % hostcnt].username;
5004 sshport = pg->hosts[off % hostcnt].sshport;
5005 pcfg = make_config (cfg,
5006 &pg->hosts[off % hostcnt].minport,
5007 &upnum, hostname, &fdnum);
5014 pcfg = make_config (cfg, &minport, &upnum, hostname, &fdnum);
5019 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5021 ("Could not create configuration for peer number %u on `%s'!\n"),
5022 off, hostname == NULL ? "localhost" : hostname);
5027 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
5030 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
5031 GNUNET_free (baseservicehome);
5035 tmpdir = getenv ("TMPDIR");
5036 tmpdir = tmpdir ? tmpdir : "/tmp";
5037 GNUNET_asprintf (&newservicehome,
5039 tmpdir, "gnunet-testing-test-test", off);
5041 GNUNET_CONFIGURATION_set_value_string (pcfg,
5043 "SERVICEHOME", newservicehome);
5044 GNUNET_free (newservicehome);
5045 pg->peers[off].cfg = pcfg;
5047 /* Can we do this later? */
5048 pg->peers[off].allowed_peers =
5049 GNUNET_CONTAINER_multihashmap_create (total);
5050 pg->peers[off].connect_peers =
5051 GNUNET_CONTAINER_multihashmap_create (total);
5052 pg->peers[off].blacklisted_peers =
5053 GNUNET_CONTAINER_multihashmap_create (total);
5056 pg->peers[off].pg = pg;
5057 pg->peers[off].internal_context.peer = &pg->peers[off];
5058 pg->peers[off].internal_context.timeout = timeout;
5059 pg->peers[off].internal_context.hostname = hostname;
5060 pg->peers[off].internal_context.username = username;
5061 pg->peers[off].internal_context.sshport = sshport;
5062 if (pg->hostkey_data != NULL)
5063 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off * HOSTKEYFILESIZE];
5064 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
5065 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
5066 pg->peers[off].internal_context.start_cb = cb;
5067 pg->peers[off].internal_context.start_cb_cls = cb_cls;
5069 GNUNET_SCHEDULER_add_now (&internal_start,
5070 &pg->peers[off].internal_context);
5077 * Get a daemon by number, so callers don't have to do nasty
5078 * offsetting operation.
5080 struct GNUNET_TESTING_Daemon *
5081 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
5082 unsigned int position)
5084 if (position < pg->total)
5085 return pg->peers[position].daemon;
5091 * Get a daemon by peer identity, so callers can
5092 * retrieve the daemon without knowing it's offset.
5094 * @param pg the peer group to retrieve the daemon from
5095 * @param peer_id the peer identity of the daemon to retrieve
5097 * @return the daemon on success, or NULL if no such peer identity is found
5099 struct GNUNET_TESTING_Daemon *
5100 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
5101 struct GNUNET_PeerIdentity *peer_id)
5105 for (i = 0; i < pg->total; i++)
5108 memcmp (&pg->peers[i].daemon->id, peer_id,
5109 sizeof (struct GNUNET_PeerIdentity)))
5110 return pg->peers[i].daemon;
5117 * Prototype of a function that will be called when a
5118 * particular operation was completed the testing library.
5120 * @param cls closure (a struct RestartContext)
5121 * @param id id of the peer that was restarted
5122 * @param cfg handle to the configuration of the peer
5123 * @param d handle to the daemon that was restarted
5124 * @param emsg NULL on success
5127 restart_callback (void *cls,
5128 const struct GNUNET_PeerIdentity *id,
5129 const struct GNUNET_CONFIGURATION_Handle *cfg,
5130 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5132 struct RestartContext *restart_context = cls;
5136 restart_context->peers_restarted++;
5140 restart_context->peers_restart_failed++;
5143 if (restart_context->peers_restarted == restart_context->peer_group->total)
5145 restart_context->callback (restart_context->callback_cls, NULL);
5146 GNUNET_free (restart_context);
5148 else if (restart_context->peers_restart_failed +
5149 restart_context->peers_restarted ==
5150 restart_context->peer_group->total)
5152 restart_context->callback (restart_context->callback_cls,
5153 "Failed to restart peers!");
5154 GNUNET_free (restart_context);
5160 * Callback for informing us about a successful
5161 * or unsuccessful churn stop call.
5163 * @param cls a ChurnContext
5164 * @param emsg NULL on success, non-NULL on failure
5168 churn_stop_callback (void *cls, const char *emsg)
5170 struct ShutdownContext *shutdown_ctx = cls;
5171 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
5172 unsigned int total_left;
5173 char *error_message;
5175 error_message = NULL;
5176 shutdown_ctx->outstanding--;
5180 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5181 "Churn stop callback failed with error `%s'\n", emsg);
5182 churn_ctx->num_failed_stop++;
5186 churn_ctx->num_to_stop--;
5190 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5191 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
5194 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
5195 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5197 if (total_left == 0)
5199 if ((churn_ctx->num_failed_stop > 0)
5200 || (churn_ctx->num_failed_start > 0))
5202 GNUNET_asprintf (&error_message,
5203 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5204 churn_ctx->num_failed_start,
5205 churn_ctx->num_failed_stop);
5207 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5208 GNUNET_free_non_null (error_message);
5209 GNUNET_free (churn_ctx);
5210 GNUNET_free (shutdown_ctx);
5215 * Count the number of running peers.
5217 * @param pg handle for the peer group
5219 * @return the number of currently running peers in the peer group
5222 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
5225 unsigned int running = 0;
5226 for (i = 0; i < pg->total; i++)
5228 if (pg->peers[i].daemon->running == GNUNET_YES)
5230 GNUNET_assert (running != -1);
5238 * Task to rate limit the number of outstanding peer shutdown
5239 * requests. This is necessary for making sure we don't do
5240 * too many ssh connections at once, but is generally nicer
5241 * to any system as well (graduated task starts, as opposed
5242 * to calling gnunet-arm N times all at once).
5245 schedule_churn_shutdown_task (void *cls,
5246 const struct GNUNET_SCHEDULER_TaskContext *tc)
5248 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5249 struct ShutdownContext *shutdown_ctx;
5250 struct ChurnContext *churn_ctx;
5251 GNUNET_assert (peer_shutdown_ctx != NULL);
5252 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5253 GNUNET_assert (shutdown_ctx != NULL);
5254 churn_ctx = (struct ChurnContext *)shutdown_ctx->cb_cls;
5255 if (shutdown_ctx->outstanding > churn_ctx->pg->max_outstanding_connections)
5256 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5257 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5258 &schedule_churn_shutdown_task,
5262 shutdown_ctx->outstanding++;
5263 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5264 shutdown_ctx->timeout, shutdown_ctx->cb,
5265 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5266 GNUNET_free (peer_shutdown_ctx);
5271 * Simulate churn by stopping some peers (and possibly
5272 * re-starting others if churn is called multiple times). This
5273 * function can only be used to create leave-join churn (peers "never"
5274 * leave for good). First "voff" random peers that are currently
5275 * online will be taken offline; then "von" random peers that are then
5276 * offline will be put back online. No notifications will be
5277 * generated for any of these operations except for the callback upon
5280 * @param pg handle for the peer group
5281 * @param voff number of peers that should go offline
5282 * @param von number of peers that should come back online;
5283 * must be zero on first call (since "testbed_start"
5284 * always starts all of the peers)
5285 * @param timeout how long to wait for operations to finish before
5287 * @param cb function to call at the end
5288 * @param cb_cls closure for cb
5291 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
5294 struct GNUNET_TIME_Relative timeout,
5295 GNUNET_TESTING_NotifyCompletion cb,
5298 struct ChurnContext *churn_ctx;
5299 struct ShutdownContext *shutdown_ctx;
5300 struct PeerShutdownContext *peer_shutdown_ctx;
5301 struct PeerRestartContext *peer_restart_ctx;
5302 struct ChurnRestartContext *churn_startup_ctx;
5304 unsigned int running;
5305 unsigned int stopped;
5306 unsigned int total_running;
5307 unsigned int total_stopped;
5309 unsigned int *running_arr;
5310 unsigned int *stopped_arr;
5311 unsigned int *running_permute;
5312 unsigned int *stopped_permute;
5317 if ((von == 0) && (voff == 0)) /* No peers at all? */
5323 for (i = 0; i < pg->total; i++)
5325 if (pg->peers[i].daemon->running == GNUNET_YES)
5327 GNUNET_assert (running != -1);
5332 GNUNET_assert (stopped != -1);
5339 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5340 "Trying to stop more peers than are currently running!\n");
5341 cb (cb_cls, "Trying to stop more peers than are currently running!");
5347 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5348 "Trying to start more peers than are currently stopped!\n");
5349 cb (cb_cls, "Trying to start more peers than are currently stopped!");
5353 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5357 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
5361 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
5363 running_permute = NULL;
5364 stopped_permute = NULL;
5368 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, running);
5371 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, stopped);
5373 total_running = running;
5374 total_stopped = stopped;
5378 churn_ctx->num_to_start = von;
5379 churn_ctx->num_to_stop = voff;
5381 churn_ctx->cb_cls = cb_cls;
5384 for (i = 0; i < pg->total; i++)
5386 if (pg->peers[i].daemon->running == GNUNET_YES)
5388 GNUNET_assert ((running_arr != NULL) && (total_running > running));
5389 running_arr[running] = i;
5394 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
5395 stopped_arr[stopped] = i;
5400 GNUNET_assert (running >= voff);
5403 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5404 shutdown_ctx->cb = &churn_stop_callback;
5405 shutdown_ctx->cb_cls = churn_ctx;
5406 shutdown_ctx->total_peers = voff;
5407 shutdown_ctx->timeout = timeout;
5410 for (i = 0; i < voff; i++)
5413 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
5414 running_permute[i]);
5416 GNUNET_assert (running_arr != NULL);
5417 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
5418 peer_shutdown_ctx->daemon =
5419 pg->peers[running_arr[running_permute[i]]].daemon;
5420 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
5421 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
5425 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
5427 &churn_stop_callback, churn_ctx,
5428 GNUNET_NO, GNUNET_YES); */
5431 GNUNET_assert (stopped >= von);
5434 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
5435 churn_startup_ctx->churn_ctx = churn_ctx;
5436 churn_startup_ctx->timeout = timeout;
5437 churn_startup_ctx->pg = pg;
5439 for (i = 0; i < von; i++)
5442 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
5443 stopped_permute[i]);
5445 GNUNET_assert (stopped_arr != NULL);
5446 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
5447 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
5448 peer_restart_ctx->daemon =
5449 pg->peers[stopped_arr[stopped_permute[i]]].daemon;
5450 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
5452 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
5453 timeout, &churn_start_callback, churn_ctx); */
5456 GNUNET_free_non_null (running_arr);
5457 GNUNET_free_non_null (stopped_arr);
5458 GNUNET_free_non_null (running_permute);
5459 GNUNET_free_non_null (stopped_permute);
5464 * Restart all peers in the given group.
5466 * @param pg the handle to the peer group
5467 * @param callback function to call on completion (or failure)
5468 * @param callback_cls closure for the callback function
5471 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
5472 GNUNET_TESTING_NotifyCompletion callback,
5475 struct RestartContext *restart_context;
5480 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
5481 restart_context->peer_group = pg;
5482 restart_context->peers_restarted = 0;
5483 restart_context->callback = callback;
5484 restart_context->callback_cls = callback_cls;
5486 for (off = 0; off < pg->total; off++)
5488 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
5489 &restart_callback, restart_context);
5495 * Start or stop an individual peer from the given group.
5497 * @param pg handle to the peer group
5498 * @param offset which peer to start or stop
5499 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
5500 * @param timeout how long to wait for shutdown
5501 * @param cb function to call at the end
5502 * @param cb_cls closure for cb
5505 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
5506 unsigned int offset,
5508 struct GNUNET_TIME_Relative timeout,
5509 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5511 struct ShutdownContext *shutdown_ctx;
5512 struct ChurnRestartContext *startup_ctx;
5513 struct ChurnContext *churn_ctx;
5515 if (GNUNET_NO == desired_status)
5517 if (NULL != pg->peers[offset].daemon)
5519 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5520 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5521 churn_ctx->num_to_start = 0;
5522 churn_ctx->num_to_stop = 1;
5524 churn_ctx->cb_cls = cb_cls;
5525 shutdown_ctx->cb_cls = churn_ctx;
5526 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon,
5527 timeout, &churn_stop_callback,
5528 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5531 else if (GNUNET_YES == desired_status)
5533 if (NULL == pg->peers[offset].daemon)
5535 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
5536 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5537 churn_ctx->num_to_start = 1;
5538 churn_ctx->num_to_stop = 0;
5540 churn_ctx->cb_cls = cb_cls;
5541 startup_ctx->churn_ctx = churn_ctx;
5542 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
5543 timeout, &churn_start_callback,
5553 * Callback for shutting down peers in a peer group.
5555 * @param cls closure (struct ShutdownContext)
5556 * @param emsg NULL on success
5559 internal_shutdown_callback (void *cls, const char *emsg)
5561 struct ShutdownContext *shutdown_ctx = cls;
5563 shutdown_ctx->outstanding--;
5566 shutdown_ctx->peers_down++;
5570 shutdown_ctx->peers_failed++;
5573 if ((shutdown_ctx->cb != NULL)
5574 && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed ==
5575 shutdown_ctx->total_peers))
5577 if (shutdown_ctx->peers_failed > 0)
5578 shutdown_ctx->cb (shutdown_ctx->cb_cls,
5579 "Not all peers successfully shut down!");
5581 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
5582 GNUNET_free (shutdown_ctx);
5588 * Task to rate limit the number of outstanding peer shutdown
5589 * requests. This is necessary for making sure we don't do
5590 * too many ssh connections at once, but is generally nicer
5591 * to any system as well (graduated task starts, as opposed
5592 * to calling gnunet-arm N times all at once).
5595 schedule_shutdown_task (void *cls,
5596 const struct GNUNET_SCHEDULER_TaskContext *tc)
5598 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5599 struct ShutdownContext *shutdown_ctx;
5601 GNUNET_assert (peer_shutdown_ctx != NULL);
5602 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5603 GNUNET_assert (shutdown_ctx != NULL);
5605 if (shutdown_ctx->outstanding > shutdown_ctx->pg->max_outstanding_connections)
5606 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5607 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5608 &schedule_shutdown_task, peer_shutdown_ctx);
5611 shutdown_ctx->outstanding++;
5612 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5613 shutdown_ctx->timeout,
5614 &internal_shutdown_callback, shutdown_ctx,
5615 GNUNET_YES, GNUNET_NO);
5616 GNUNET_free (peer_shutdown_ctx);
5621 * Shutdown all peers started in the given group.
5623 * @param pg handle to the peer group
5624 * @param timeout how long to wait for shutdown
5625 * @param cb callback to notify upon success or failure
5626 * @param cb_cls closure for cb
5629 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
5630 struct GNUNET_TIME_Relative timeout,
5631 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5634 struct ShutdownContext *shutdown_ctx;
5635 struct PeerShutdownContext *peer_shutdown_ctx;
5637 struct PeerConnection *conn_iter;
5638 struct PeerConnection *temp_conn;
5641 GNUNET_assert (pg->total > 0);
5643 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5644 shutdown_ctx->cb = cb;
5645 shutdown_ctx->cb_cls = cb_cls;
5646 shutdown_ctx->total_peers = pg->total;
5647 shutdown_ctx->timeout = timeout;
5648 shutdown_ctx->pg = pg;
5649 /* shtudown_ctx->outstanding = 0; */
5651 for (off = 0; off < pg->total; off++)
5653 GNUNET_assert (NULL != pg->peers[off].daemon);
5654 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
5655 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
5656 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
5657 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
5659 if (NULL != pg->peers[off].cfg)
5660 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
5662 conn_iter = pg->peers[off].allowed_peers_head;
5663 while (conn_iter != NULL)
5665 temp_conn = conn_iter->next;
5666 GNUNET_free(conn_iter);
5667 conn_iter = temp_conn;
5670 conn_iter = pg->peers[off].connect_peers_head;
5671 while (conn_iter != NULL)
5673 temp_conn = conn_iter->next;
5674 GNUNET_free(conn_iter);
5675 conn_iter = temp_conn;
5678 conn_iter = pg->peers[off].blacklisted_peers_head;
5679 while (conn_iter != NULL)
5681 temp_conn = conn_iter->next;
5682 GNUNET_free(conn_iter);
5683 conn_iter = temp_conn;
5686 conn_iter = pg->peers[off].connect_peers_working_set_head;
5687 while (conn_iter != NULL)
5689 temp_conn = conn_iter->next;
5690 GNUNET_free(conn_iter);
5691 conn_iter = temp_conn;
5694 if (pg->peers[off].allowed_peers != NULL)
5695 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
5696 if (pg->peers[off].connect_peers != NULL)
5697 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
5698 if (pg->peers[off].blacklisted_peers != NULL)
5699 GNUNET_CONTAINER_multihashmap_destroy (pg->
5700 peers[off].blacklisted_peers);
5703 GNUNET_free (pg->peers);
5704 GNUNET_free_non_null(pg->hostkey_data);
5705 for (off = 0; off < pg->num_hosts; off++)
5707 GNUNET_free (pg->hosts[off].hostname);
5708 GNUNET_free_non_null (pg->hosts[off].username);
5710 GNUNET_free_non_null (pg->hosts);
5715 /* end of testing_group.c */