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;
3068 struct ConnectContext *connect_context;
3073 ct_ctx = GNUNET_malloc (sizeof (struct ConnectTopologyContext));
3074 ct_ctx->notify_connections_done = notify_callback;
3075 ct_ctx->notify_cls = notify_cls;
3078 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3081 connection_iter = pg->peers[pg_iter].connect_peers_head;
3082 while (connection_iter != NULL)
3084 connection_iter = connection_iter->next;
3089 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3095 GNUNET_free (ct_ctx);
3098 ct_ctx->connect_timeout = connect_timeout;
3099 ct_ctx->connect_attempts = connect_attempts;
3100 ct_ctx->remaining_connections = total;
3101 ct_ctx->remaining_connects_to_schedule = total;
3105 * FIXME: iterating over peer lists in this way means that a single peer will have
3106 * all of its connections scheduled basically at once. This means a single peer
3107 * will have a lot of work to do, and may slow down the connection process. If
3108 * we could choose a connection *randomly* that would work much better.
3110 * Even using the hashmap method we still choose all of a single peers connections.
3112 * Will we incur a serious performance penalty iterating over the lists a bunch of
3113 * times? Probably not compared to the delays in connecting peers. Can't hurt
3116 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3118 preschedule_connect(ct_ctx);
3124 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3127 connection_iter = pg->peers[pg_iter].connect_peers_head;
3128 while (connection_iter != NULL)
3130 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling connect of peer %d to peer %d\n", pg_iter, connection_iter->index);
3131 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3132 connect_context->first = pg->peers[pg_iter].daemon;
3133 connect_context->second = pg->peers[connection_iter->index].daemon;
3134 connect_context->ct_ctx = ct_ctx;
3135 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3136 connection_iter = connection_iter->next;
3141 GNUNET_CONTAINER_multihashmap_iterate (pg->
3142 peers[pg_iter].connect_peers,
3143 &connect_iterator, ct_ctx);
3144 GNUNET_assert (GNUNET_SYSERR != ret && ret >= 0);
3145 total = total + ret;
3154 * Takes a peer group and creates a topology based on the
3155 * one specified. Creates a topology means generates friend
3156 * files for the peers so they can only connect to those allowed
3157 * by the topology. This will only have an effect once peers
3158 * are started if the FRIENDS_ONLY option is set in the base
3159 * config. Also takes an optional restrict topology which
3160 * disallows connections based on particular transports
3161 * UNLESS they are specified in the restricted topology.
3163 * @param pg the peer group struct representing the running peers
3164 * @param topology which topology to connect the peers in
3165 * @param restrict_topology disallow restrict_transports transport
3166 * connections to peers NOT in this topology
3167 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3168 * @param restrict_transports space delimited list of transports to blacklist
3169 * to create restricted topology
3171 * @return the maximum number of connections were all allowed peers
3172 * connected to each other
3175 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
3176 enum GNUNET_TESTING_Topology topology,
3177 enum GNUNET_TESTING_Topology
3179 const char *restrict_transports)
3183 unsigned int num_connections;
3184 int unblacklisted_connections;
3189 for (i = 0; i < pg->total; i++)
3191 pg->peers[i].allowed_peers =
3192 GNUNET_CONTAINER_multihashmap_create (100);
3193 pg->peers[i].connect_peers =
3194 GNUNET_CONTAINER_multihashmap_create (100);
3195 pg->peers[i].blacklisted_peers =
3196 GNUNET_CONTAINER_multihashmap_create (100);
3197 pg->peers[i].pg = pg;
3203 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3207 num_connections = create_clique (pg, &add_connections, ALLOWED);
3209 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3212 _("Creating small world (ring) topology\n"));
3215 create_small_world_ring (pg, &add_connections, ALLOWED);
3217 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3220 _("Creating small world (2d-torus) topology\n"));
3222 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3224 case GNUNET_TESTING_TOPOLOGY_RING:
3226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3228 num_connections = create_ring (pg, &add_connections, ALLOWED);
3230 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3234 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3236 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3239 _("Creating Erdos-Renyi topology\n"));
3241 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3243 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3247 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3249 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3252 _("Creating Scale Free topology\n"));
3254 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3256 case GNUNET_TESTING_TOPOLOGY_LINE:
3258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3259 _("Creating straight line topology\n"));
3261 num_connections = create_line (pg, &add_connections, ALLOWED);
3263 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3266 _("Creating topology from file!\n"));
3269 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing", "topology_file",
3271 num_connections = create_from_file (pg, filename, &add_connections, ALLOWED);
3274 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3275 num_connections = 0;
3278 case GNUNET_TESTING_TOPOLOGY_NONE:
3280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3282 ("Creating no allowed topology (all peers can connect at core level)\n"));
3284 num_connections = pg->total * pg->total; /* Clique is allowed! */
3287 num_connections = 0;
3292 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
3294 ret = create_and_copy_friend_files (pg);
3295 if (ret != GNUNET_OK)
3298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3299 _("Failed during friend file copying!\n"));
3301 return GNUNET_SYSERR;
3306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3307 _("Friend files created/copied successfully!\n"));
3312 /* Use the create clique method to initially set all connections as blacklisted. */
3313 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
3314 create_clique (pg, &add_connections, BLACKLIST);
3316 unblacklisted_connections = 0;
3317 /* Un-blacklist connections as per the topology specified */
3318 switch (restrict_topology)
3320 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3323 _("Blacklisting all but clique topology\n"));
3325 unblacklisted_connections =
3326 create_clique (pg, &remove_connections, BLACKLIST);
3328 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3331 _("Blacklisting all but small world (ring) topology\n"));
3333 unblacklisted_connections =
3334 create_small_world_ring (pg, &remove_connections, BLACKLIST);
3336 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3340 ("Blacklisting all but small world (2d-torus) topology\n"));
3342 unblacklisted_connections =
3343 create_small_world (pg, &remove_connections, BLACKLIST);
3345 case GNUNET_TESTING_TOPOLOGY_RING:
3347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3348 _("Blacklisting all but ring topology\n"));
3350 unblacklisted_connections = create_ring (pg, &remove_connections, BLACKLIST);
3352 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3355 _("Blacklisting all but 2d torus topology\n"));
3357 unblacklisted_connections =
3358 create_2d_torus (pg, &remove_connections, BLACKLIST);
3360 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3363 _("Blacklisting all but Erdos-Renyi topology\n"));
3365 unblacklisted_connections =
3366 create_erdos_renyi (pg, &remove_connections, BLACKLIST);
3368 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3371 _("Blacklisting all but InterNAT topology\n"));
3373 unblacklisted_connections =
3374 create_nated_internet (pg, &remove_connections, BLACKLIST);
3376 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3379 _("Blacklisting all but Scale Free topology\n"));
3381 unblacklisted_connections =
3382 create_scale_free (pg, &remove_connections, BLACKLIST);
3384 case GNUNET_TESTING_TOPOLOGY_LINE:
3386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3387 _("Blacklisting all but straight line topology\n"));
3389 unblacklisted_connections = create_line (pg, &remove_connections, BLACKLIST);
3391 case GNUNET_TESTING_TOPOLOGY_NONE: /* Fall through */
3392 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3396 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
3398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3400 ("Creating blacklist topology from allowed\n"));
3401 unblacklisted_connections = copy_allowed (pg, &remove_connections);
3406 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
3408 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n", restrict_transports);
3409 ret = create_and_copy_blacklist_files (pg, restrict_transports);
3410 if (ret != GNUNET_OK)
3413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3414 _("Failed during blacklist file copying!\n"));
3421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3422 _("Blacklist files created/copied successfully!\n"));
3426 return num_connections;
3432 * Iterator for choosing random peers to connect.
3434 * @param cls closure, a RandomContext
3435 * @param key the key the second Daemon was stored under
3436 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3438 * @return GNUNET_YES to continue iteration
3441 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3443 struct RandomContext *random_ctx = cls;
3444 double random_number;
3445 uint32_t second_pos;
3446 GNUNET_HashCode first_hash;
3449 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3450 UINT64_MAX)) / ((double) UINT64_MAX);
3451 if (random_number < random_ctx->percentage)
3453 GNUNET_assert (GNUNET_OK ==
3454 GNUNET_CONTAINER_multihashmap_put (random_ctx->
3455 first->connect_peers_working_set,
3457 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3460 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
3461 uid_from_hash (key, &second_pos);
3462 hash_from_uid (random_ctx->first_uid, &first_hash);
3463 GNUNET_assert (random_ctx->pg->total > second_pos);
3464 GNUNET_assert (GNUNET_YES ==
3465 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
3467 [second_pos].connect_peers,
3477 * Iterator for adding at least X peers to a peers connection set.
3479 * @param cls closure, MinimumContext
3480 * @param key the key the second Daemon was stored under
3481 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3483 * @return GNUNET_YES to continue iteration
3486 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3488 struct MinimumContext *min_ctx = cls;
3489 uint32_t second_pos;
3490 GNUNET_HashCode first_hash;
3493 if (GNUNET_CONTAINER_multihashmap_size
3494 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
3496 for (i = 0; i < min_ctx->num_to_add; i++)
3498 if (min_ctx->pg_array[i] == min_ctx->current)
3500 GNUNET_assert (GNUNET_OK ==
3501 GNUNET_CONTAINER_multihashmap_put
3502 (min_ctx->first->connect_peers_working_set, key,
3504 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3505 uid_from_hash (key, &second_pos);
3506 hash_from_uid (min_ctx->first_uid, &first_hash);
3507 GNUNET_assert (min_ctx->pg->total > second_pos);
3508 GNUNET_assert (GNUNET_OK ==
3509 GNUNET_CONTAINER_multihashmap_put (min_ctx->
3511 [second_pos].connect_peers_working_set,
3515 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3516 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
3517 GNUNET_assert (GNUNET_YES ==
3518 GNUNET_CONTAINER_multihashmap_remove
3519 (min_ctx->pg->peers[second_pos].connect_peers,
3520 &first_hash, min_ctx->first->daemon));
3527 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
3533 * Iterator for adding peers to a connection set based on a depth first search.
3535 * @param cls closure, MinimumContext
3536 * @param key the key the second daemon was stored under
3537 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3539 * @return GNUNET_YES to continue iteration
3542 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3544 struct DFSContext *dfs_ctx = cls;
3545 GNUNET_HashCode first_hash;
3547 if (dfs_ctx->current == dfs_ctx->chosen)
3549 GNUNET_assert (GNUNET_OK ==
3550 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3551 first->connect_peers_working_set,
3553 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3554 uid_from_hash (key, &dfs_ctx->second_uid);
3555 hash_from_uid (dfs_ctx->first_uid, &first_hash);
3556 GNUNET_assert (GNUNET_OK ==
3557 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3559 [dfs_ctx->second_uid].connect_peers_working_set,
3563 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3564 GNUNET_assert (GNUNET_YES ==
3565 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
3567 [dfs_ctx->second_uid].connect_peers,
3571 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
3572 return GNUNET_NO; /* We have found our peer, don't iterate more */
3581 * From the set of connections possible, choose percentage percent of connections
3582 * to actually connect.
3584 * @param pg the peergroup we are dealing with
3585 * @param percentage what percent of total connections to make
3588 choose_random_connections (struct GNUNET_TESTING_PeerGroup *pg,
3591 struct RandomContext random_ctx;
3594 struct PeerConnection *temp_peers;
3595 struct PeerConnection *conn_iter;
3596 double random_number;
3599 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3601 random_ctx.first_uid = pg_iter;
3602 random_ctx.first = &pg->peers[pg_iter];
3603 random_ctx.percentage = percentage;
3607 conn_iter = pg->peers[pg_iter].connect_peers_head;
3608 while (conn_iter != NULL)
3612 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3613 UINT64_MAX)) / ((double) UINT64_MAX);
3614 if (random_number < percentage)
3616 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
3618 conn_iter = conn_iter->next;
3621 pg->peers[pg_iter].connect_peers_working_set =
3622 GNUNET_CONTAINER_multihashmap_create (pg->total);
3623 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3624 &random_connect_iterator,
3626 /* Now remove the old connections */
3627 GNUNET_CONTAINER_multihashmap_destroy (pg->
3628 peers[pg_iter].connect_peers);
3629 /* And replace with the random set */
3630 pg->peers[pg_iter].connect_peers =
3631 pg->peers[pg_iter].connect_peers_working_set;
3635 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3637 conn_iter = pg->peers[pg_iter].connect_peers_head;
3638 while (pg->peers[pg_iter].connect_peers_head != NULL)
3639 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
3641 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
3642 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
3643 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
3644 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
3650 * Count the number of connections in a linked list of connections.
3652 * @param conn_list the connection list to get the count of
3654 * @return the number of elements in the list
3657 count_connections (struct PeerConnection *conn_list)
3659 struct PeerConnection *iter;
3663 while (iter != NULL)
3673 count_workingset_connections (struct GNUNET_TESTING_PeerGroup *pg)
3676 unsigned int pg_iter;
3678 struct PeerConnection *conn_iter;
3682 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3685 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
3686 while (conn_iter != NULL)
3689 conn_iter = conn_iter->next;
3693 GNUNET_CONTAINER_multihashmap_size (pg->
3695 [pg_iter].connect_peers_working_set);
3704 count_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg)
3707 unsigned int pg_iter;
3709 struct PeerConnection *conn_iter;
3713 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3716 conn_iter = pg->peers[pg_iter].allowed_peers_head;
3717 while (conn_iter != NULL)
3720 conn_iter = conn_iter->next;
3724 GNUNET_CONTAINER_multihashmap_size (pg->
3726 [pg_iter].allowed_peers);
3735 * From the set of connections possible, choose at least num connections per
3738 * @param pg the peergroup we are dealing with
3739 * @param num how many connections at least should each peer have (if possible)?
3742 choose_minimum (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3745 struct MinimumContext minimum_ctx;
3747 struct PeerConnection *conn_iter;
3748 unsigned int temp_list_size;
3751 uint32_t random; /* Random list entry to connect peer to */
3756 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3758 temp_list_size = count_connections(pg->peers[pg_iter].connect_peers_head);
3759 if (temp_list_size == 0)
3761 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer %d has 0 connections!?!?\n", pg_iter);
3764 for (i = 0; i < num; i++)
3766 random = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, temp_list_size);
3767 conn_iter = pg->peers[pg_iter].connect_peers_head;
3768 for (count = 0; count < random; count++)
3769 conn_iter = conn_iter->next;
3770 /* We now have a random connection, connect it! */
3771 GNUNET_assert(conn_iter != NULL);
3772 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
3776 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3778 pg->peers[pg_iter].connect_peers_working_set =
3779 GNUNET_CONTAINER_multihashmap_create (num);
3782 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3784 minimum_ctx.first_uid = pg_iter;
3785 minimum_ctx.pg_array =
3786 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
3787 GNUNET_CONTAINER_multihashmap_size
3788 (pg->peers[pg_iter].connect_peers));
3789 minimum_ctx.first = &pg->peers[pg_iter];
3790 minimum_ctx.pg = pg;
3791 minimum_ctx.num_to_add = num;
3792 minimum_ctx.current = 0;
3793 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3794 &minimum_connect_iterator,
3798 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3800 /* Remove the "old" connections */
3801 GNUNET_CONTAINER_multihashmap_destroy (pg->
3802 peers[pg_iter].connect_peers);
3803 /* And replace with the working set */
3804 pg->peers[pg_iter].connect_peers =
3805 pg->peers[pg_iter].connect_peers_working_set;
3808 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3810 while (pg->peers[pg_iter].connect_peers_head != NULL)
3811 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
3813 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
3814 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
3815 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
3816 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
3821 struct FindClosestContext
3824 * The currently known closest peer.
3826 struct GNUNET_TESTING_Daemon *closest;
3829 * The info for the peer we are adding connections for.
3831 struct PeerData *curr_peer;
3834 * The distance (bits) between the current
3835 * peer and the currently known closest.
3837 unsigned int closest_dist;
3840 * The offset of the closest known peer in
3843 unsigned int closest_num;
3847 * Iterator over hash map entries of the allowed
3848 * peer connections. Find the closest, not already
3849 * connected peer and return it.
3851 * @param cls closure (struct FindClosestContext)
3852 * @param key current key code (hash of offset in pg)
3853 * @param value value in the hash map - a GNUNET_TESTING_Daemon
3854 * @return GNUNET_YES if we should continue to
3859 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
3861 struct FindClosestContext *closest_ctx = cls;
3862 struct GNUNET_TESTING_Daemon *daemon = value;
3864 if (((closest_ctx->closest == NULL) ||
3865 (GNUNET_CRYPTO_hash_matching_bits
3866 (&daemon->id.hashPubKey,
3867 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
3868 closest_ctx->closest_dist))
3870 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
3871 curr_peer->connect_peers,
3874 closest_ctx->closest_dist =
3875 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
3876 &closest_ctx->curr_peer->daemon->
3878 closest_ctx->closest = daemon;
3879 uid_from_hash (key, &closest_ctx->closest_num);
3886 * From the set of connections possible, choose at num connections per
3887 * peer based on depth which are closest out of those allowed. Guaranteed
3888 * to add num peers to connect to, provided there are that many peers
3889 * in the underlay topology to connect to.
3891 * @param pg the peergroup we are dealing with
3892 * @param num how many connections at least should each peer have (if possible)?
3893 * @param proc processor to actually add the connections
3894 * @param list the peer list to use
3897 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
3898 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
3903 struct FindClosestContext closest_ctx;
3908 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
3910 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3912 closest_ctx.curr_peer = &pg->peers[pg_iter];
3913 closest_ctx.closest = NULL;
3914 closest_ctx.closest_dist = 0;
3915 closest_ctx.closest_num = 0;
3916 GNUNET_CONTAINER_multihashmap_iterate (pg->
3917 peers[pg_iter].allowed_peers,
3918 &find_closest_peers,
3920 if (closest_ctx.closest != NULL)
3922 GNUNET_assert (closest_ctx.closest_num < pg->total);
3923 proc (pg, pg_iter, closest_ctx.closest_num, list);
3931 * From the set of connections possible, choose at least num connections per
3932 * peer based on depth first traversal of peer connections. If DFS leaves
3933 * peers unconnected, ensure those peers get connections.
3935 * @param pg the peergroup we are dealing with
3936 * @param num how many connections at least should each peer have (if possible)?
3939 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3943 uint32_t starting_peer;
3944 uint32_t least_connections;
3945 uint32_t random_connection;
3947 unsigned int temp_count;
3948 struct PeerConnection *peer_iter;
3950 struct DFSContext dfs_ctx;
3951 GNUNET_HashCode second_hash;
3957 while ((count_workingset_connections (pg) < num * pg->total)
3958 && (count_allowed_connections (pg) > 0))
3960 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3962 least_connections = -1; /* Set to very high number */
3963 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3965 temp_count = count_connections(pg->peers[pg_iter].connect_peers_working_set_head);
3966 if (temp_count < least_connections)
3968 starting_peer = pg_iter;
3969 least_connections = temp_count;
3974 temp_count = count_connections(pg->peers[starting_peer].connect_peers_head);
3975 if (temp_count == 0)
3976 continue; /* FIXME: infinite loop? */
3978 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, temp_count);
3980 peer_iter = pg->peers[starting_peer].connect_peers_head;
3981 while (temp_count < random_connection)
3983 peer_iter = peer_iter->next;
3986 GNUNET_assert(peer_iter != NULL);
3987 add_connections(pg, starting_peer, peer_iter->index, WORKING_SET);
3988 remove_connections(pg, starting_peer, peer_iter->index, CONNECT);
3989 starting_peer = peer_iter->index;
3994 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3996 pg->peers[pg_iter].connect_peers_working_set =
3997 GNUNET_CONTAINER_multihashmap_create (num);
4002 while ((count_workingset_connections (pg) < num * pg->total)
4003 && (count_allowed_connections (pg) > 0))
4005 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4007 least_connections = -1; /* Set to very high number */
4008 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4010 if (GNUNET_CONTAINER_multihashmap_size
4011 (pg->peers[pg_iter].connect_peers_working_set) <
4014 starting_peer = pg_iter;
4016 GNUNET_CONTAINER_multihashmap_size (pg->
4018 [pg_iter].connect_peers_working_set);
4023 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4029 /* Choose a random peer from the chosen peers set of connections to add */
4031 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4032 GNUNET_CONTAINER_multihashmap_size
4033 (pg->peers[starting_peer].connect_peers));
4034 dfs_ctx.first_uid = starting_peer;
4035 dfs_ctx.first = &pg->peers[starting_peer];
4037 dfs_ctx.current = 0;
4039 GNUNET_CONTAINER_multihashmap_iterate (pg->
4041 [starting_peer].connect_peers,
4042 &dfs_connect_iterator, &dfs_ctx);
4043 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4044 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4045 GNUNET_assert (GNUNET_YES ==
4046 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4047 [starting_peer].connect_peers,
4051 [dfs_ctx.second_uid].daemon));
4052 starting_peer = dfs_ctx.second_uid;
4055 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4057 /* Remove the "old" connections */
4058 GNUNET_CONTAINER_multihashmap_destroy (pg->
4059 peers[pg_iter].connect_peers);
4060 /* And replace with the working set */
4061 pg->peers[pg_iter].connect_peers =
4062 pg->peers[pg_iter].connect_peers_working_set;
4069 * Internal callback for topology information for a particular peer.
4072 internal_topology_callback (void *cls,
4073 const struct GNUNET_PeerIdentity *peer,
4074 const struct GNUNET_TRANSPORT_ATS_Information
4077 struct CoreContext *core_ctx = cls;
4078 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4080 if (peer == NULL) /* Either finished, or something went wrong */
4082 iter_ctx->completed++;
4083 iter_ctx->connected--;
4084 /* One core context allocated per iteration, must free! */
4085 GNUNET_free (core_ctx);
4089 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id,
4093 if (iter_ctx->completed == iter_ctx->total)
4095 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4096 /* Once all are done, free the iteration context */
4097 GNUNET_free (iter_ctx);
4103 * Check running topology iteration tasks, if below max start a new one, otherwise
4104 * schedule for some time in the future.
4107 schedule_get_topology (void *cls,
4108 const struct GNUNET_SCHEDULER_TaskContext *tc)
4110 struct CoreContext *core_context = cls;
4111 struct TopologyIterateContext *topology_context =
4112 (struct TopologyIterateContext *) core_context->iter_context;
4113 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4116 if (topology_context->connected > topology_context->pg->max_outstanding_connections)
4118 #if VERBOSE_TESTING > 2
4119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4121 ("Delaying connect, we have too many outstanding connections!\n"));
4123 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4124 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4125 &schedule_get_topology, core_context);
4129 #if VERBOSE_TESTING > 2
4130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4131 _("Creating connection, outstanding_connections is %d\n"),
4132 outstanding_connects);
4134 topology_context->connected++;
4137 GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4138 &internal_topology_callback,
4141 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4142 internal_topology_callback (core_context, NULL, NULL);
4148 * Iterate over all (running) peers in the peer group, retrieve
4149 * all connections that each currently has.
4152 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
4153 GNUNET_TESTING_NotifyTopology cb, void *cls)
4155 struct TopologyIterateContext *topology_context;
4156 struct CoreContext *core_ctx;
4158 unsigned int total_count;
4160 /* Allocate a single topology iteration context */
4161 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4162 topology_context->topology_cb = cb;
4163 topology_context->cls = cls;
4164 topology_context->pg = pg;
4166 for (i = 0; i < pg->total; i++)
4168 if (pg->peers[i].daemon->running == GNUNET_YES)
4170 /* Allocate one core context per core we need to connect to */
4171 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4172 core_ctx->daemon = pg->peers[i].daemon;
4173 /* Set back pointer to topology iteration context */
4174 core_ctx->iter_context = topology_context;
4175 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4179 if (total_count == 0)
4181 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4182 GNUNET_free (topology_context);
4185 topology_context->total = total_count;
4190 * Callback function to process statistic values.
4191 * This handler is here only really to insert a peer
4192 * identity (or daemon) so the statistics can be uniquely
4193 * tied to a single running peer.
4195 * @param cls closure
4196 * @param subsystem name of subsystem that created the statistic
4197 * @param name the name of the datum
4198 * @param value the current value
4199 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4200 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4203 internal_stats_callback (void *cls,
4204 const char *subsystem,
4205 const char *name, uint64_t value, int is_persistent)
4207 struct StatsCoreContext *core_context = cls;
4208 struct StatsIterateContext *stats_context =
4209 (struct StatsIterateContext *) core_context->iter_context;
4211 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4212 subsystem, name, value, is_persistent);
4216 * Internal continuation call for statistics iteration.
4218 * @param cls closure, the CoreContext for this iteration
4219 * @param success whether or not the statistics iterations
4220 * was canceled or not (we don't care)
4223 internal_stats_cont (void *cls, int success)
4225 struct StatsCoreContext *core_context = cls;
4226 struct StatsIterateContext *stats_context =
4227 (struct StatsIterateContext *) core_context->iter_context;
4229 stats_context->connected--;
4230 stats_context->completed++;
4232 if (stats_context->completed == stats_context->total)
4234 stats_context->cont (stats_context->cls, GNUNET_YES);
4235 GNUNET_free (stats_context);
4238 if (core_context->stats_handle != NULL)
4239 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4241 GNUNET_free (core_context);
4245 * Check running topology iteration tasks, if below max start a new one, otherwise
4246 * schedule for some time in the future.
4249 schedule_get_statistics (void *cls,
4250 const struct GNUNET_SCHEDULER_TaskContext *tc)
4252 struct StatsCoreContext *core_context = cls;
4253 struct StatsIterateContext *stats_context =
4254 (struct StatsIterateContext *) core_context->iter_context;
4256 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4259 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
4261 #if VERBOSE_TESTING > 2
4262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4264 ("Delaying connect, we have too many outstanding connections!\n"));
4266 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4267 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4268 &schedule_get_statistics, core_context);
4272 #if VERBOSE_TESTING > 2
4273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4274 _("Creating connection, outstanding_connections is %d\n"),
4275 outstanding_connects);
4278 stats_context->connected++;
4279 core_context->stats_handle =
4280 GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
4281 if (core_context->stats_handle == NULL)
4283 internal_stats_cont (core_context, GNUNET_NO);
4287 core_context->stats_get_handle =
4288 GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
4289 GNUNET_TIME_relative_get_forever (),
4290 &internal_stats_cont, &internal_stats_callback,
4292 if (core_context->stats_get_handle == NULL)
4293 internal_stats_cont (core_context, GNUNET_NO);
4298 struct DuplicateStats
4301 * Next item in the list
4303 struct DuplicateStats *next;
4306 * Nasty string, concatenation of relevant information.
4308 char *unique_string;
4312 * Check whether the combination of port/host/unix domain socket
4313 * already exists in the list of peers being checked for statistics.
4315 * @param pg the peergroup in question
4316 * @param specific_peer the peer we're concerned with
4317 * @param stats_list the list to return to the caller
4319 * @return GNUNET_YES if the statistics instance has been seen already,
4320 * GNUNET_NO if not (and we may have added it to the list)
4323 stats_check_existing (struct GNUNET_TESTING_PeerGroup *pg,
4324 struct PeerData *specific_peer,
4325 struct DuplicateStats **stats_list)
4327 struct DuplicateStats *pos;
4328 char *unix_domain_socket;
4329 unsigned long long port;
4332 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
4333 "single_statistics_per_host"))
4334 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
4338 GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg, "statistics",
4339 "unixpath", &unix_domain_socket))
4343 GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg, "statistics",
4346 GNUNET_free(unix_domain_socket);
4350 if (specific_peer->daemon->hostname != NULL)
4351 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
4352 unix_domain_socket, port);
4354 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
4358 if (0 == strcmp (to_match, pos->unique_string))
4360 GNUNET_free (unix_domain_socket);
4361 GNUNET_free (to_match);
4366 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
4367 pos->unique_string = to_match;
4368 pos->next = *stats_list;
4370 GNUNET_free (unix_domain_socket);
4375 * Iterate over all (running) peers in the peer group, retrieve
4376 * all statistics from each.
4379 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
4380 GNUNET_STATISTICS_Callback cont,
4381 GNUNET_TESTING_STATISTICS_Iterator proc,
4384 struct StatsIterateContext *stats_context;
4385 struct StatsCoreContext *core_ctx;
4387 unsigned int total_count;
4388 struct DuplicateStats *stats_list;
4389 struct DuplicateStats *pos;
4392 /* Allocate a single stats iteration context */
4393 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
4394 stats_context->cont = cont;
4395 stats_context->proc = proc;
4396 stats_context->cls = cls;
4397 stats_context->pg = pg;
4400 for (i = 0; i < pg->total; i++)
4402 if ((pg->peers[i].daemon->running == GNUNET_YES)
4404 stats_check_existing (pg, &pg->peers[i], &stats_list)))
4406 /* Allocate one core context per core we need to connect to */
4407 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
4408 core_ctx->daemon = pg->peers[i].daemon;
4409 /* Set back pointer to topology iteration context */
4410 core_ctx->iter_context = stats_context;
4411 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
4416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4417 "Retrieving stats from %u total instances.\n", total_count);
4418 stats_context->total = total_count;
4419 if (stats_list != NULL)
4424 GNUNET_free (pos->unique_string);
4425 stats_list = pos->next;
4427 pos = stats_list->next;
4434 * There are many ways to connect peers that are supported by this function.
4435 * To connect peers in the same topology that was created via the
4436 * GNUNET_TESTING_create_topology, the topology variable must be set to
4437 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
4438 * a new instance of that topology will be generated and attempted to be
4439 * connected. This could result in some connections being impossible,
4440 * because some topologies are non-deterministic.
4442 * @param pg the peer group struct representing the running peers
4443 * @param topology which topology to connect the peers in
4444 * @param options options for connecting the topology
4445 * @param option_modifier modifier for options that take a parameter
4446 * @param connect_timeout how long to wait before giving up on connecting
4448 * @param connect_attempts how many times to attempt to connect two peers
4449 * over the connect_timeout duration
4450 * @param notify_callback notification to be called once all connections completed
4451 * @param notify_cls closure for notification callback
4453 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
4456 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
4457 enum GNUNET_TESTING_Topology topology,
4458 enum GNUNET_TESTING_TopologyOption options,
4459 double option_modifier,
4460 struct GNUNET_TIME_Relative connect_timeout,
4461 unsigned int connect_attempts,
4462 GNUNET_TESTING_NotifyCompletion
4463 notify_callback, void *notify_cls)
4467 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4468 #if VERBOSE_TOPOLOGY
4469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4470 _("Creating clique CONNECT topology\n"));
4472 create_clique (pg, &add_connections, CONNECT);
4474 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4475 #if VERBOSE_TOPOLOGY
4476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4477 _("Creating small world (ring) CONNECT topology\n"));
4479 create_small_world_ring (pg, &add_connections, CONNECT);
4481 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4482 #if VERBOSE_TOPOLOGY
4483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4484 _("Creating small world (2d-torus) CONNECT topology\n"));
4486 create_small_world (pg, &add_connections, CONNECT);
4488 case GNUNET_TESTING_TOPOLOGY_RING:
4489 #if VERBOSE_TOPOLOGY
4490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4491 _("Creating ring CONNECT topology\n"));
4493 create_ring (pg, &add_connections, CONNECT);
4495 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4496 #if VERBOSE_TOPOLOGY
4497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4498 _("Creating 2d torus CONNECT topology\n"));
4500 create_2d_torus (pg, &add_connections, CONNECT);
4502 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4503 #if VERBOSE_TOPOLOGY
4504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4505 _("Creating Erdos-Renyi CONNECT topology\n"));
4507 create_erdos_renyi (pg, &add_connections, CONNECT);
4509 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4510 #if VERBOSE_TOPOLOGY
4511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4512 _("Creating InterNAT CONNECT topology\n"));
4514 create_nated_internet (pg, &add_connections, CONNECT);
4516 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4517 #if VERBOSE_TOPOLOGY
4518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4519 _("Creating Scale Free CONNECT topology\n"));
4521 create_scale_free (pg, &add_connections, CONNECT);
4523 case GNUNET_TESTING_TOPOLOGY_LINE:
4524 #if VERBOSE_TOPOLOGY
4525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4526 _("Creating straight line CONNECT topology\n"));
4528 create_line (pg, &add_connections, CONNECT);
4530 case GNUNET_TESTING_TOPOLOGY_NONE:
4531 #if VERBOSE_TOPOLOGY
4532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4533 _("Creating no CONNECT topology\n"));
4535 copy_allowed_topology (pg);
4538 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4540 ("Unknown topology specification, can't connect peers!\n"));
4541 return GNUNET_SYSERR;
4546 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
4547 #if VERBOSE_TOPOLOGY
4548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4550 ("Connecting random subset (%'.2f percent) of possible peers\n"),
4551 100 * option_modifier);
4553 choose_random_connections (pg, option_modifier);
4555 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
4556 #if VERBOSE_TOPOLOGY
4557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4558 _("Connecting a minimum of %u peers each (if possible)\n"),
4559 (unsigned int) option_modifier);
4561 choose_minimum (pg, (unsigned int) option_modifier);
4563 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
4564 #if VERBOSE_TOPOLOGY
4565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4567 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
4568 (unsigned int) option_modifier);
4571 perform_dfs (pg, (int) option_modifier);
4574 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
4575 #if VERBOSE_TOPOLOGY
4576 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4578 ("Finding additional %u closest peers each (if possible)\n"),
4579 (unsigned int) option_modifier);
4582 add_closest (pg, (unsigned int) option_modifier,
4583 &add_connections, CONNECT);
4586 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
4588 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
4594 return connect_topology (pg, connect_timeout, connect_attempts, notify_callback, notify_cls);
4598 * Callback that is called whenever a hostkey is generated
4599 * for a peer. Call the real callback and decrement the
4600 * starting counter for the peergroup.
4602 * @param cls closure
4603 * @param id identifier for the daemon, NULL on error
4604 * @param d handle for the daemon
4605 * @param emsg error message (NULL on success)
4608 internal_hostkey_callback (void *cls,
4609 const struct GNUNET_PeerIdentity *id,
4610 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4612 struct InternalStartContext *internal_context = cls;
4613 internal_context->peer->pg->starting--;
4614 internal_context->peer->pg->started++;
4615 if (internal_context->hostkey_callback != NULL)
4616 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
4618 else if (internal_context->peer->pg->started ==
4619 internal_context->peer->pg->total)
4621 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
4622 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
4627 * Callback that is called whenever a peer has finished starting.
4628 * Call the real callback and decrement the starting counter
4629 * for the peergroup.
4631 * @param cls closure
4632 * @param id identifier for the daemon, NULL on error
4634 * @param d handle for the daemon
4635 * @param emsg error message (NULL on success)
4638 internal_startup_callback (void *cls,
4639 const struct GNUNET_PeerIdentity *id,
4640 const struct GNUNET_CONFIGURATION_Handle *cfg,
4641 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4643 struct InternalStartContext *internal_context = cls;
4644 internal_context->peer->pg->starting--;
4645 if (internal_context->start_cb != NULL)
4646 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
4651 internal_continue_startup (void *cls,
4652 const struct GNUNET_SCHEDULER_TaskContext *tc)
4654 struct InternalStartContext *internal_context = cls;
4656 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4661 if (internal_context->peer->pg->starting < internal_context->peer->pg->max_outstanding_connections)
4663 internal_context->peer->pg->starting++;
4664 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
4668 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4669 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4670 &internal_continue_startup,
4677 * Callback for informing us about a successful
4678 * or unsuccessful churn start call.
4680 * @param cls a ChurnContext
4681 * @param id the peer identity of the started peer
4682 * @param cfg the handle to the configuration of the peer
4683 * @param d handle to the daemon for the peer
4684 * @param emsg NULL on success, non-NULL on failure
4688 churn_start_callback (void *cls,
4689 const struct GNUNET_PeerIdentity *id,
4690 const struct GNUNET_CONFIGURATION_Handle *cfg,
4691 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4693 struct ChurnRestartContext *startup_ctx = cls;
4694 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
4696 unsigned int total_left;
4697 char *error_message;
4699 error_message = NULL;
4702 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4703 "Churn stop callback failed with error `%s'\n", emsg);
4704 churn_ctx->num_failed_start++;
4708 churn_ctx->num_to_start--;
4712 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4713 "Started peer, %d left.\n", churn_ctx->num_to_start);
4717 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
4718 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4720 if (total_left == 0)
4722 if ((churn_ctx->num_failed_stop > 0)
4723 || (churn_ctx->num_failed_start > 0))
4724 GNUNET_asprintf (&error_message,
4725 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4726 churn_ctx->num_failed_start,
4727 churn_ctx->num_failed_stop);
4728 churn_ctx->cb (churn_ctx->cb_cls, error_message);
4729 GNUNET_free_non_null (error_message);
4730 GNUNET_free (churn_ctx);
4731 GNUNET_free (startup_ctx);
4737 schedule_churn_restart (void *cls,
4738 const struct GNUNET_SCHEDULER_TaskContext *tc)
4740 struct PeerRestartContext *peer_restart_ctx = cls;
4741 struct ChurnRestartContext *startup_ctx =
4742 peer_restart_ctx->churn_restart_ctx;
4744 if (startup_ctx->outstanding > startup_ctx->pg->max_outstanding_connections)
4745 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4746 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4747 &schedule_churn_restart, peer_restart_ctx);
4750 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
4751 startup_ctx->timeout,
4752 &churn_start_callback,
4754 GNUNET_free (peer_restart_ctx);
4759 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4761 struct InternalStartContext *internal_context = cls;
4763 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4768 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
4770 internal_context->peer->pg->starting++;
4771 internal_context->peer->daemon =
4772 GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
4773 internal_context->timeout,
4774 internal_context->hostname,
4775 internal_context->username,
4776 internal_context->sshport,
4777 internal_context->hostkey,
4778 &internal_hostkey_callback,
4780 &internal_startup_callback,
4785 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4786 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4787 &internal_start, internal_context);
4792 * Function which continues a peer group starting up
4793 * after successfully generating hostkeys for each peer.
4795 * @param pg the peer group to continue starting
4799 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg)
4804 for (i = 0; i < pg->total; i++)
4806 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
4807 &pg->peers[i].internal_context);
4808 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
4813 * Start count gnunet instances with the same set of transports and
4814 * applications. The port numbers (any option called "PORT") will be
4815 * adjusted to ensure that no two peers running on the same system
4816 * have the same port(s) in their respective configurations.
4818 * @param cfg configuration template to use
4819 * @param total number of daemons to start
4820 * @param max_concurrent_connections for testing, how many peers can
4821 * we connect to simultaneously
4822 * @param timeout total time allowed for peers to start
4823 * @param hostkey_callback function to call on each peers hostkey generation
4824 * if NULL, peers will be started by this call, if non-null,
4825 * GNUNET_TESTING_daemons_continue_startup must be called after
4826 * successful hostkey generation
4827 * @param hostkey_cls closure for hostkey callback
4828 * @param cb function to call on each daemon that was started
4829 * @param cb_cls closure for cb
4830 * @param connect_callback function to call each time two hosts are connected
4831 * @param connect_callback_cls closure for connect_callback
4832 * @param hostnames linked list of host structs to use to start peers on
4833 * (NULL to run on localhost only)
4835 * @return NULL on error, otherwise handle to control peer group
4837 struct GNUNET_TESTING_PeerGroup *
4838 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
4840 unsigned int max_concurrent_connections,
4841 struct GNUNET_TIME_Relative timeout,
4842 GNUNET_TESTING_NotifyHostkeyCreated
4843 hostkey_callback, void *hostkey_cls,
4844 GNUNET_TESTING_NotifyDaemonRunning cb,
4846 GNUNET_TESTING_NotifyConnection
4847 connect_callback, void *connect_callback_cls,
4848 const struct GNUNET_TESTING_Host *hostnames)
4850 struct GNUNET_TESTING_PeerGroup *pg;
4851 const struct GNUNET_TESTING_Host *hostpos;
4857 const char *hostname;
4858 const char *username;
4859 char *baseservicehome;
4860 char *newservicehome;
4862 char *hostkeys_file;
4865 struct GNUNET_DISK_FileHandle *fd;
4866 struct GNUNET_CONFIGURATION_Handle *pcfg;
4868 unsigned int hostcnt;
4875 uint64_t total_hostkeys;
4876 struct GNUNET_OS_Process *proc;
4886 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
4888 pg->notify_connection = connect_callback;
4889 pg->notify_connection_cls = connect_callback_cls;
4891 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
4892 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
4893 pg->max_outstanding_connections = max_concurrent_connections;
4894 if (NULL != hostnames)
4897 hostpos = hostnames;
4898 while (hostpos != NULL)
4900 hostpos = hostpos->next;
4903 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4906 hostpos = hostnames;
4907 while (hostpos != NULL)
4909 pg->hosts[off].minport = LOW_PORT;
4910 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
4911 if (hostpos->username != NULL)
4912 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
4913 pg->hosts[off].sshport = hostpos->port;
4914 hostpos = hostpos->next;
4924 pg->num_hosts = off;
4928 /* skip leading spaces */
4929 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
4932 while ('\0' != *rpos)
4934 if (isspace ((unsigned char) *rpos))
4938 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4940 start = GNUNET_strdup (hostnames);
4942 while ('\0' != *pos)
4944 if (isspace ((unsigned char) *pos))
4947 if (strlen (start) > 0)
4949 pg->hosts[off].minport = LOW_PORT;
4950 pg->hosts[off++].hostname = start;
4956 if (strlen (start) > 0)
4958 pg->hosts[off].minport = LOW_PORT;
4959 pg->hosts[off++].hostname = start;
4963 GNUNET_free (start);
4964 GNUNET_free (pg->hosts);
4968 minport = 0; /* make gcc happy */
4977 /* Create the servicehome directory for each remote peer */
4978 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
4980 for (i = 0; i < pg->num_hosts; i++)
4983 if (NULL != pg->hosts[i].username)
4984 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username, pg->hosts[i].hostname);
4986 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
4987 if (pg->hosts[i].sshport != 0)
4989 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
4990 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
4991 "ssh", "-P", ssh_port_str,
4995 arg, "mkdir -p", baseservicehome, NULL);
4998 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
4999 "ssh", arg, "mkdir -p", baseservicehome, NULL);
5000 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating remote dir with command ssh %s %s %s\n", arg, " mkdir -p ", baseservicehome);
5001 GNUNET_OS_process_wait(proc);
5003 GNUNET_free(baseservicehome);
5005 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "HOSTKEYSFILE",
5008 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
5009 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!\n");
5012 /* Check hostkey file size, read entire thing into memory */
5013 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
5014 GNUNET_DISK_PERM_NONE);
5017 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
5021 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
5024 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Found file size %llu for hostkeys, expect hostkeys to be size %d\n", fs, HOSTKEYFILESIZE);
5026 if (fs % HOSTKEYFILESIZE != 0)
5028 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "File size %llu seems incorrect for hostkeys...\n", fs);
5032 total_hostkeys = fs / HOSTKEYFILESIZE;
5033 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Will read %llu hostkeys from file\n", total_hostkeys);
5034 pg->hostkey_data = GNUNET_malloc_large (fs);
5035 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
5036 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
5039 GNUNET_free(hostkeys_file);
5042 for (off = 0; off < total; off++)
5046 hostname = pg->hosts[off % hostcnt].hostname;
5047 username = pg->hosts[off % hostcnt].username;
5048 sshport = pg->hosts[off % hostcnt].sshport;
5049 pcfg = make_config (cfg,
5050 &pg->hosts[off % hostcnt].minport,
5051 &upnum, hostname, &fdnum);
5058 pcfg = make_config (cfg, &minport, &upnum, hostname, &fdnum);
5063 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5065 ("Could not create configuration for peer number %u on `%s'!\n"),
5066 off, hostname == NULL ? "localhost" : hostname);
5071 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
5074 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
5075 GNUNET_free (baseservicehome);
5079 tmpdir = getenv ("TMPDIR");
5080 tmpdir = tmpdir ? tmpdir : "/tmp";
5081 GNUNET_asprintf (&newservicehome,
5083 tmpdir, "gnunet-testing-test-test", off);
5085 GNUNET_CONFIGURATION_set_value_string (pcfg,
5087 "SERVICEHOME", newservicehome);
5088 GNUNET_free (newservicehome);
5089 pg->peers[off].cfg = pcfg;
5091 /* Can we do this later? */
5092 pg->peers[off].allowed_peers =
5093 GNUNET_CONTAINER_multihashmap_create (total);
5094 pg->peers[off].connect_peers =
5095 GNUNET_CONTAINER_multihashmap_create (total);
5096 pg->peers[off].blacklisted_peers =
5097 GNUNET_CONTAINER_multihashmap_create (total);
5100 pg->peers[off].pg = pg;
5101 pg->peers[off].internal_context.peer = &pg->peers[off];
5102 pg->peers[off].internal_context.timeout = timeout;
5103 pg->peers[off].internal_context.hostname = hostname;
5104 pg->peers[off].internal_context.username = username;
5105 pg->peers[off].internal_context.sshport = sshport;
5106 if (pg->hostkey_data != NULL)
5107 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off * HOSTKEYFILESIZE];
5108 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
5109 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
5110 pg->peers[off].internal_context.start_cb = cb;
5111 pg->peers[off].internal_context.start_cb_cls = cb_cls;
5113 GNUNET_SCHEDULER_add_now (&internal_start,
5114 &pg->peers[off].internal_context);
5121 * Get a daemon by number, so callers don't have to do nasty
5122 * offsetting operation.
5124 struct GNUNET_TESTING_Daemon *
5125 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
5126 unsigned int position)
5128 if (position < pg->total)
5129 return pg->peers[position].daemon;
5135 * Get a daemon by peer identity, so callers can
5136 * retrieve the daemon without knowing it's offset.
5138 * @param pg the peer group to retrieve the daemon from
5139 * @param peer_id the peer identity of the daemon to retrieve
5141 * @return the daemon on success, or NULL if no such peer identity is found
5143 struct GNUNET_TESTING_Daemon *
5144 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
5145 struct GNUNET_PeerIdentity *peer_id)
5149 for (i = 0; i < pg->total; i++)
5152 memcmp (&pg->peers[i].daemon->id, peer_id,
5153 sizeof (struct GNUNET_PeerIdentity)))
5154 return pg->peers[i].daemon;
5161 * Prototype of a function that will be called when a
5162 * particular operation was completed the testing library.
5164 * @param cls closure (a struct RestartContext)
5165 * @param id id of the peer that was restarted
5166 * @param cfg handle to the configuration of the peer
5167 * @param d handle to the daemon that was restarted
5168 * @param emsg NULL on success
5171 restart_callback (void *cls,
5172 const struct GNUNET_PeerIdentity *id,
5173 const struct GNUNET_CONFIGURATION_Handle *cfg,
5174 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5176 struct RestartContext *restart_context = cls;
5180 restart_context->peers_restarted++;
5184 restart_context->peers_restart_failed++;
5187 if (restart_context->peers_restarted == restart_context->peer_group->total)
5189 restart_context->callback (restart_context->callback_cls, NULL);
5190 GNUNET_free (restart_context);
5192 else if (restart_context->peers_restart_failed +
5193 restart_context->peers_restarted ==
5194 restart_context->peer_group->total)
5196 restart_context->callback (restart_context->callback_cls,
5197 "Failed to restart peers!");
5198 GNUNET_free (restart_context);
5204 * Callback for informing us about a successful
5205 * or unsuccessful churn stop call.
5207 * @param cls a ChurnContext
5208 * @param emsg NULL on success, non-NULL on failure
5212 churn_stop_callback (void *cls, const char *emsg)
5214 struct ShutdownContext *shutdown_ctx = cls;
5215 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
5216 unsigned int total_left;
5217 char *error_message;
5219 error_message = NULL;
5220 shutdown_ctx->outstanding--;
5224 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5225 "Churn stop callback failed with error `%s'\n", emsg);
5226 churn_ctx->num_failed_stop++;
5230 churn_ctx->num_to_stop--;
5234 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5235 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
5238 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
5239 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5241 if (total_left == 0)
5243 if ((churn_ctx->num_failed_stop > 0)
5244 || (churn_ctx->num_failed_start > 0))
5246 GNUNET_asprintf (&error_message,
5247 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5248 churn_ctx->num_failed_start,
5249 churn_ctx->num_failed_stop);
5251 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5252 GNUNET_free_non_null (error_message);
5253 GNUNET_free (churn_ctx);
5254 GNUNET_free (shutdown_ctx);
5259 * Count the number of running peers.
5261 * @param pg handle for the peer group
5263 * @return the number of currently running peers in the peer group
5266 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
5269 unsigned int running = 0;
5270 for (i = 0; i < pg->total; i++)
5272 if (pg->peers[i].daemon->running == GNUNET_YES)
5274 GNUNET_assert (running != -1);
5282 * Task to rate limit the number of outstanding peer shutdown
5283 * requests. This is necessary for making sure we don't do
5284 * too many ssh connections at once, but is generally nicer
5285 * to any system as well (graduated task starts, as opposed
5286 * to calling gnunet-arm N times all at once).
5289 schedule_churn_shutdown_task (void *cls,
5290 const struct GNUNET_SCHEDULER_TaskContext *tc)
5292 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5293 struct ShutdownContext *shutdown_ctx;
5294 struct ChurnContext *churn_ctx;
5295 GNUNET_assert (peer_shutdown_ctx != NULL);
5296 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5297 GNUNET_assert (shutdown_ctx != NULL);
5298 churn_ctx = (struct ChurnContext *)shutdown_ctx->cb_cls;
5299 if (shutdown_ctx->outstanding > churn_ctx->pg->max_outstanding_connections)
5300 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5301 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5302 &schedule_churn_shutdown_task,
5306 shutdown_ctx->outstanding++;
5307 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5308 shutdown_ctx->timeout, shutdown_ctx->cb,
5309 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5310 GNUNET_free (peer_shutdown_ctx);
5315 * Simulate churn by stopping some peers (and possibly
5316 * re-starting others if churn is called multiple times). This
5317 * function can only be used to create leave-join churn (peers "never"
5318 * leave for good). First "voff" random peers that are currently
5319 * online will be taken offline; then "von" random peers that are then
5320 * offline will be put back online. No notifications will be
5321 * generated for any of these operations except for the callback upon
5324 * @param pg handle for the peer group
5325 * @param voff number of peers that should go offline
5326 * @param von number of peers that should come back online;
5327 * must be zero on first call (since "testbed_start"
5328 * always starts all of the peers)
5329 * @param timeout how long to wait for operations to finish before
5331 * @param cb function to call at the end
5332 * @param cb_cls closure for cb
5335 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
5338 struct GNUNET_TIME_Relative timeout,
5339 GNUNET_TESTING_NotifyCompletion cb,
5342 struct ChurnContext *churn_ctx;
5343 struct ShutdownContext *shutdown_ctx;
5344 struct PeerShutdownContext *peer_shutdown_ctx;
5345 struct PeerRestartContext *peer_restart_ctx;
5346 struct ChurnRestartContext *churn_startup_ctx;
5348 unsigned int running;
5349 unsigned int stopped;
5350 unsigned int total_running;
5351 unsigned int total_stopped;
5353 unsigned int *running_arr;
5354 unsigned int *stopped_arr;
5355 unsigned int *running_permute;
5356 unsigned int *stopped_permute;
5361 if ((von == 0) && (voff == 0)) /* No peers at all? */
5367 for (i = 0; i < pg->total; i++)
5369 if (pg->peers[i].daemon->running == GNUNET_YES)
5371 GNUNET_assert (running != -1);
5376 GNUNET_assert (stopped != -1);
5383 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5384 "Trying to stop more peers than are currently running!\n");
5385 cb (cb_cls, "Trying to stop more peers than are currently running!");
5391 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5392 "Trying to start more peers than are currently stopped!\n");
5393 cb (cb_cls, "Trying to start more peers than are currently stopped!");
5397 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5401 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
5405 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
5407 running_permute = NULL;
5408 stopped_permute = NULL;
5412 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, running);
5415 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, stopped);
5417 total_running = running;
5418 total_stopped = stopped;
5422 churn_ctx->num_to_start = von;
5423 churn_ctx->num_to_stop = voff;
5425 churn_ctx->cb_cls = cb_cls;
5428 for (i = 0; i < pg->total; i++)
5430 if (pg->peers[i].daemon->running == GNUNET_YES)
5432 GNUNET_assert ((running_arr != NULL) && (total_running > running));
5433 running_arr[running] = i;
5438 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
5439 stopped_arr[stopped] = i;
5444 GNUNET_assert (running >= voff);
5447 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5448 shutdown_ctx->cb = &churn_stop_callback;
5449 shutdown_ctx->cb_cls = churn_ctx;
5450 shutdown_ctx->total_peers = voff;
5451 shutdown_ctx->timeout = timeout;
5454 for (i = 0; i < voff; i++)
5457 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
5458 running_permute[i]);
5460 GNUNET_assert (running_arr != NULL);
5461 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
5462 peer_shutdown_ctx->daemon =
5463 pg->peers[running_arr[running_permute[i]]].daemon;
5464 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
5465 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
5469 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
5471 &churn_stop_callback, churn_ctx,
5472 GNUNET_NO, GNUNET_YES); */
5475 GNUNET_assert (stopped >= von);
5478 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
5479 churn_startup_ctx->churn_ctx = churn_ctx;
5480 churn_startup_ctx->timeout = timeout;
5481 churn_startup_ctx->pg = pg;
5483 for (i = 0; i < von; i++)
5486 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
5487 stopped_permute[i]);
5489 GNUNET_assert (stopped_arr != NULL);
5490 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
5491 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
5492 peer_restart_ctx->daemon =
5493 pg->peers[stopped_arr[stopped_permute[i]]].daemon;
5494 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
5496 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
5497 timeout, &churn_start_callback, churn_ctx); */
5500 GNUNET_free_non_null (running_arr);
5501 GNUNET_free_non_null (stopped_arr);
5502 GNUNET_free_non_null (running_permute);
5503 GNUNET_free_non_null (stopped_permute);
5508 * Restart all peers in the given group.
5510 * @param pg the handle to the peer group
5511 * @param callback function to call on completion (or failure)
5512 * @param callback_cls closure for the callback function
5515 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
5516 GNUNET_TESTING_NotifyCompletion callback,
5519 struct RestartContext *restart_context;
5524 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
5525 restart_context->peer_group = pg;
5526 restart_context->peers_restarted = 0;
5527 restart_context->callback = callback;
5528 restart_context->callback_cls = callback_cls;
5530 for (off = 0; off < pg->total; off++)
5532 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
5533 &restart_callback, restart_context);
5539 * Start or stop an individual peer from the given group.
5541 * @param pg handle to the peer group
5542 * @param offset which peer to start or stop
5543 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
5544 * @param timeout how long to wait for shutdown
5545 * @param cb function to call at the end
5546 * @param cb_cls closure for cb
5549 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
5550 unsigned int offset,
5552 struct GNUNET_TIME_Relative timeout,
5553 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5555 struct ShutdownContext *shutdown_ctx;
5556 struct ChurnRestartContext *startup_ctx;
5557 struct ChurnContext *churn_ctx;
5559 if (GNUNET_NO == desired_status)
5561 if (NULL != pg->peers[offset].daemon)
5563 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5564 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5565 churn_ctx->num_to_start = 0;
5566 churn_ctx->num_to_stop = 1;
5568 churn_ctx->cb_cls = cb_cls;
5569 shutdown_ctx->cb_cls = churn_ctx;
5570 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon,
5571 timeout, &churn_stop_callback,
5572 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5575 else if (GNUNET_YES == desired_status)
5577 if (NULL == pg->peers[offset].daemon)
5579 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
5580 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5581 churn_ctx->num_to_start = 1;
5582 churn_ctx->num_to_stop = 0;
5584 churn_ctx->cb_cls = cb_cls;
5585 startup_ctx->churn_ctx = churn_ctx;
5586 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
5587 timeout, &churn_start_callback,
5597 * Callback for shutting down peers in a peer group.
5599 * @param cls closure (struct ShutdownContext)
5600 * @param emsg NULL on success
5603 internal_shutdown_callback (void *cls, const char *emsg)
5605 struct ShutdownContext *shutdown_ctx = cls;
5607 shutdown_ctx->outstanding--;
5610 shutdown_ctx->peers_down++;
5614 shutdown_ctx->peers_failed++;
5617 if ((shutdown_ctx->cb != NULL)
5618 && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed ==
5619 shutdown_ctx->total_peers))
5621 if (shutdown_ctx->peers_failed > 0)
5622 shutdown_ctx->cb (shutdown_ctx->cb_cls,
5623 "Not all peers successfully shut down!");
5625 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
5626 GNUNET_free (shutdown_ctx);
5632 * Task to rate limit the number of outstanding peer shutdown
5633 * requests. This is necessary for making sure we don't do
5634 * too many ssh connections at once, but is generally nicer
5635 * to any system as well (graduated task starts, as opposed
5636 * to calling gnunet-arm N times all at once).
5639 schedule_shutdown_task (void *cls,
5640 const struct GNUNET_SCHEDULER_TaskContext *tc)
5642 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5643 struct ShutdownContext *shutdown_ctx;
5645 GNUNET_assert (peer_shutdown_ctx != NULL);
5646 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5647 GNUNET_assert (shutdown_ctx != NULL);
5649 if (shutdown_ctx->outstanding > shutdown_ctx->pg->max_outstanding_connections)
5650 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5651 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5652 &schedule_shutdown_task, peer_shutdown_ctx);
5655 shutdown_ctx->outstanding++;
5656 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5657 shutdown_ctx->timeout,
5658 &internal_shutdown_callback, shutdown_ctx,
5659 GNUNET_YES, GNUNET_NO);
5660 GNUNET_free (peer_shutdown_ctx);
5665 * Shutdown all peers started in the given group.
5667 * @param pg handle to the peer group
5668 * @param timeout how long to wait for shutdown
5669 * @param cb callback to notify upon success or failure
5670 * @param cb_cls closure for cb
5673 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
5674 struct GNUNET_TIME_Relative timeout,
5675 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5678 struct ShutdownContext *shutdown_ctx;
5679 struct PeerShutdownContext *peer_shutdown_ctx;
5681 struct PeerConnection *conn_iter;
5682 struct PeerConnection *temp_conn;
5685 GNUNET_assert (pg->total > 0);
5687 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5688 shutdown_ctx->cb = cb;
5689 shutdown_ctx->cb_cls = cb_cls;
5690 shutdown_ctx->total_peers = pg->total;
5691 shutdown_ctx->timeout = timeout;
5692 shutdown_ctx->pg = pg;
5693 /* shtudown_ctx->outstanding = 0; */
5695 for (off = 0; off < pg->total; off++)
5697 GNUNET_assert (NULL != pg->peers[off].daemon);
5698 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
5699 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
5700 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
5701 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
5703 if (NULL != pg->peers[off].cfg)
5704 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
5706 conn_iter = pg->peers[off].allowed_peers_head;
5707 while (conn_iter != NULL)
5709 temp_conn = conn_iter->next;
5710 GNUNET_free(conn_iter);
5711 conn_iter = temp_conn;
5714 conn_iter = pg->peers[off].connect_peers_head;
5715 while (conn_iter != NULL)
5717 temp_conn = conn_iter->next;
5718 GNUNET_free(conn_iter);
5719 conn_iter = temp_conn;
5722 conn_iter = pg->peers[off].blacklisted_peers_head;
5723 while (conn_iter != NULL)
5725 temp_conn = conn_iter->next;
5726 GNUNET_free(conn_iter);
5727 conn_iter = temp_conn;
5730 conn_iter = pg->peers[off].connect_peers_working_set_head;
5731 while (conn_iter != NULL)
5733 temp_conn = conn_iter->next;
5734 GNUNET_free(conn_iter);
5735 conn_iter = temp_conn;
5738 if (pg->peers[off].allowed_peers != NULL)
5739 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
5740 if (pg->peers[off].connect_peers != NULL)
5741 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
5742 if (pg->peers[off].blacklisted_peers != NULL)
5743 GNUNET_CONTAINER_multihashmap_destroy (pg->
5744 peers[off].blacklisted_peers);
5747 GNUNET_free (pg->peers);
5748 GNUNET_free_non_null(pg->hostkey_data);
5749 for (off = 0; off < pg->num_hosts; off++)
5751 GNUNET_free (pg->hosts[off].hostname);
5752 GNUNET_free_non_null (pg->hosts[off].username);
5754 GNUNET_free_non_null (pg->hosts);
5759 /* end of testing_group.c */