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
60 * Which list of peers do we need to modify?
64 /** Modify allowed peers */
67 /** Modify connect peers */
70 /** Modify blacklist peers */
73 /** Modify workingset peers */
78 * Prototype of a function called whenever two peers would be connected
79 * in a certain topology.
81 typedef unsigned int (*GNUNET_TESTING_ConnectionProcessor) (struct
82 GNUNET_TESTING_PeerGroup
92 * Context for handling churning a peer group
97 * The peergroup we are dealing with.
99 struct GNUNET_TESTING_PeerGroup *pg;
102 * Callback used to notify of churning finished
104 GNUNET_TESTING_NotifyCompletion cb;
107 * Closure for callback
112 * Number of peers that still need to be started
114 unsigned int num_to_start;
117 * Number of peers that still need to be stopped
119 unsigned int num_to_stop;
122 * Number of peers that failed to start
124 unsigned int num_failed_start;
127 * Number of peers that failed to stop
129 unsigned int num_failed_stop;
132 struct RestartContext
135 * The group of peers being restarted
137 struct GNUNET_TESTING_PeerGroup *peer_group;
140 * How many peers have been restarted thus far
142 unsigned int peers_restarted;
145 * How many peers got an error when restarting
147 unsigned int peers_restart_failed;
150 * The function to call once all peers have been restarted
152 GNUNET_TESTING_NotifyCompletion callback;
155 * Closure for callback function
162 struct ShutdownContext
164 struct GNUNET_TESTING_PeerGroup *pg;
166 * Total peers to wait for
168 unsigned int total_peers;
171 * Number of peers successfully shut down
173 unsigned int peers_down;
176 * Number of peers failed to shut down
178 unsigned int peers_failed;
181 * Number of peers we have started shutting
182 * down. If too many, wait on them.
184 unsigned int outstanding;
187 * Timeout for shutdown.
189 struct GNUNET_TIME_Relative timeout;
192 * Callback to call when all peers either
193 * shutdown or failed to shutdown
195 GNUNET_TESTING_NotifyCompletion cb;
204 * Individual shutdown context for a particular peer.
206 struct PeerShutdownContext
209 * Pointer to the high level shutdown context.
211 struct ShutdownContext *shutdown_ctx;
214 * The daemon handle for the peer to shut down.
216 struct GNUNET_TESTING_Daemon *daemon;
220 * Individual shutdown context for a particular peer.
222 struct PeerRestartContext
225 * Pointer to the high level restart context.
227 struct ChurnRestartContext *churn_restart_ctx;
230 * The daemon handle for the peer to shut down.
232 struct GNUNET_TESTING_Daemon *daemon;
236 struct CreateTopologyContext
240 * Function to call with number of connections
242 GNUNET_TESTING_NotifyConnections cont;
245 * Closure for connection notification
252 /** Waiting to read number of peers */
255 /** Should find next peer index */
258 /** Should find colon */
261 /** Should read other peer index, space, or endline */
267 struct PeerConnection
272 struct PeerConnection *prev;
277 struct PeerConnection *next;
281 * Index of daemon in pg->peers
288 struct InternalStartContext
291 * Pointer to peerdata
293 struct PeerData *peer;
296 * Timeout for peer startup
298 struct GNUNET_TIME_Relative timeout;
301 * Client callback for hostkey notification
303 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
306 * Closure for hostkey_callback
311 * Client callback for peer start notification
313 GNUNET_TESTING_NotifyDaemonRunning start_cb;
321 * Hostname, where to start the peer
323 const char *hostname;
326 * Username to use when connecting to the
329 const char *username;
332 * Pointer to starting memory location of a hostkey
337 * Port to use for ssh.
343 struct ChurnRestartContext
346 * PeerGroup that we are working with.
348 struct GNUNET_TESTING_PeerGroup *pg;
351 * Number of restarts currently in flight.
353 unsigned int outstanding;
356 * Handle to the underlying churn context.
358 struct ChurnContext *churn_ctx;
361 * How long to allow the operation to take.
363 struct GNUNET_TIME_Relative timeout;
366 struct OutstandingSSH
368 struct OutstandingSSH *next;
370 struct OutstandingSSH *prev;
373 * Number of current ssh connections.
375 uint32_t outstanding;
378 * The hostname of this peer.
380 const char *hostname;
384 * Data we keep per peer.
389 * (Initial) configuration of the host.
390 * (initial because clients could change
391 * it and we would not know about those
394 struct GNUNET_CONFIGURATION_Handle *cfg;
397 * Handle for controlling the daemon.
399 struct GNUNET_TESTING_Daemon *daemon;
402 * The peergroup this peer belongs to.
404 struct GNUNET_TESTING_PeerGroup *pg;
408 * Linked list of allowed peer connections.
410 struct PeerConnection *allowed_peers_head;
413 * Linked list of allowed peer connections.
415 struct PeerConnection *allowed_peers_tail;
418 * Linked list of blacklisted peer connections.
420 struct PeerConnection *blacklisted_peers_head;
423 * Linked list of blacklisted peer connections.
425 struct PeerConnection *blacklisted_peers_tail;
428 * Linked list of connect peer connections.
430 struct PeerConnection *connect_peers_head;
433 * Linked list of connect peer connections.
435 struct PeerConnection *connect_peers_tail;
438 * Linked list of connect peer connections.
440 struct PeerConnection *connect_peers_working_set_head;
443 * Linked list of connect peer connections.
445 struct PeerConnection *connect_peers_working_set_tail;
449 * Hash map of allowed peer connections (F2F created topology)
451 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
454 * Hash map of blacklisted peers
456 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
459 * Hash map of peer connections
461 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
464 * Temporary hash map of peer connections
466 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
470 * Temporary variable for topology creation, should be reset before
471 * creating any topology so the count is valid once finished.
476 * Context to keep track of peers being started, to
477 * stagger hostkey generation and peer startup.
479 struct InternalStartContext internal_context;
484 * Linked list of per-host data.
494 * SSH username to use when connecting to this host.
499 * SSH port to use when connecting to this host.
504 * Lowest port that we have not yet used
510 struct TopologyIterateContext
513 * The peergroup we are working with.
515 struct GNUNET_TESTING_PeerGroup *pg;
518 * Callback for notifying of two connected peers.
520 GNUNET_TESTING_NotifyTopology topology_cb;
523 * Closure for topology_cb
528 * Number of peers currently connected to.
530 unsigned int connected;
533 * Number of peers we have finished iterating.
535 unsigned int completed;
538 * Number of peers total.
543 struct StatsIterateContext
546 * The peergroup that we are dealing with.
548 struct GNUNET_TESTING_PeerGroup *pg;
551 * Continuation to call once all stats information has been retrieved.
553 GNUNET_STATISTICS_Callback cont;
556 * Proc function to call on each value received.
558 GNUNET_TESTING_STATISTICS_Iterator proc;
561 * Closure for topology_cb
566 * Number of peers currently connected to.
568 unsigned int connected;
571 * Number of peers we have finished iterating.
573 unsigned int completed;
576 * Number of peers total.
584 struct GNUNET_TESTING_Daemon *daemon;
587 struct StatsCoreContext
590 struct GNUNET_TESTING_Daemon *daemon;
592 * Handle to the statistics service.
594 struct GNUNET_STATISTICS_Handle *stats_handle;
597 * Handle for getting statistics.
599 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
603 * Handle to a group of GNUnet peers.
605 struct GNUNET_TESTING_PeerGroup
608 * Configuration template.
610 const struct GNUNET_CONFIGURATION_Handle *cfg;
613 * Function to call on each started daemon.
615 //GNUNET_TESTING_NotifyDaemonRunning cb;
623 * Function to call on each topology connection created
625 GNUNET_TESTING_NotifyConnection notify_connection;
628 * Callback for notify_connection
630 void *notify_connection_cls;
633 * Array of information about hosts.
635 struct HostData *hosts;
638 * Number of hosts (size of HostData)
640 unsigned int num_hosts;
643 * Array of "total" peers.
645 struct PeerData *peers;
648 * Number of peers in this group.
653 * At what time should we fail the peer startup process?
655 struct GNUNET_TIME_Absolute max_timeout;
658 * How many peers are being started right now?
660 unsigned int starting;
663 * How many peers have already been started?
665 unsigned int started;
668 * Number of possible connections to peers
671 unsigned int max_outstanding_connections;
674 * Number of ssh connections to peers (max).
676 unsigned int max_concurrent_ssh;
679 * Number of connects we are waiting on, allows us to rate limit
682 unsigned int outstanding_connects;
685 * How many connects have already been scheduled?
687 unsigned int total_connects_scheduled;
690 * Hostkeys loaded from a file.
695 * Head of DLL to keep track of the number of outstanding
696 * ssh connections per peer.
698 struct OutstandingSSH *ssh_head;
701 * Tail of DLL to keep track of the number of outstanding
702 * ssh connections per peer.
704 struct OutstandingSSH *ssh_tail;
709 struct GNUNET_CONFIGURATION_Handle *ret;
710 const struct GNUNET_CONFIGURATION_Handle *orig;
711 const char *hostname;
717 struct ConnectTopologyContext
720 * How many connections are left to create.
722 unsigned int remaining_connections;
725 * How many more connections do we need to schedule?
727 unsigned int remaining_connects_to_schedule;
730 * Handle to group of peers.
732 struct GNUNET_TESTING_PeerGroup *pg;
735 * How long to try this connection before timing out.
737 struct GNUNET_TIME_Relative connect_timeout;
740 * How many times to retry connecting the two peers.
742 unsigned int connect_attempts;
745 * Temp value set for each iteration.
747 //struct PeerData *first;
750 * Notification that all peers are connected.
752 GNUNET_TESTING_NotifyCompletion notify_connections_done;
755 * Closure for notify.
760 struct ConnectContext
763 * Peer to connect second to.
765 struct GNUNET_TESTING_Daemon *first;
768 * Peer to connect first to.
770 struct GNUNET_TESTING_Daemon *second;
773 * Higher level topology connection context.
775 struct ConnectTopologyContext *ct_ctx;
778 * Whether this connection has been accounted for in the schedule_connect call.
783 struct UnblacklistContext
788 struct GNUNET_TESTING_PeerGroup *pg;
791 * uid of the first peer
801 struct GNUNET_TESTING_PeerGroup *pg;
804 * uid of the first peer
809 * Peer data for first peer.
811 struct PeerData *first;
814 * Random percentage to use
819 struct MinimumContext
824 struct GNUNET_TESTING_PeerGroup *pg;
827 * uid of the first peer
832 * Peer data for first peer.
834 struct PeerData *first;
837 * Number of conns per peer
839 unsigned int num_to_add;
842 * Permuted array of all possible connections. Only add the Nth
843 * peer if it's in the Nth position.
845 unsigned int *pg_array;
848 * What number is the current element we are iterating over?
850 unsigned int current;
858 struct GNUNET_TESTING_PeerGroup *pg;
861 * uid of the first peer
866 * uid of the second peer
871 * Peer data for first peer.
873 struct PeerData *first;
876 * Which peer has been chosen as the one to add?
881 * What number is the current element we are iterating over?
883 unsigned int current;
888 * Convert unique ID to hash code.
890 * @param uid unique ID to convert
891 * @param hash set to uid (extended with zeros)
894 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
896 memset (hash, 0, sizeof (GNUNET_HashCode));
897 *((uint32_t *) hash) = uid;
901 * Convert hash code to unique ID.
903 * @param uid unique ID to convert
904 * @param hash set to uid (extended with zeros)
907 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
909 memcpy (uid, hash, sizeof (uint32_t));
914 * Get a topology from a string input.
916 * @param topology where to write the retrieved topology
917 * @param topology_string The string to attempt to
918 * get a configuration value from
919 * @return GNUNET_YES if topology string matched a
920 * known topology, GNUNET_NO if not
923 GNUNET_TESTING_topology_get (enum GNUNET_TESTING_Topology *topology,
924 const char *topology_string)
927 * Strings representing topologies in enum
929 static const char *topology_strings[] = {
931 * A clique (everyone connected to everyone else).
936 * Small-world network (2d torus plus random links).
941 * Small-world network (ring plus random links).
961 * Certain percentage of peers are unable to communicate directly
962 * replicating NAT conditions
967 * Scale free topology.
972 * Straight line topology.
977 * All peers are disconnected.
982 * Read the topology from a file.
990 if (topology_string == NULL)
992 while (topology_strings[curr] != NULL)
994 if (strcasecmp (topology_strings[curr], topology_string) == 0)
1001 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
1007 * Get connect topology option from string input.
1009 * @param topology_option where to write the retrieved topology
1010 * @param topology_string The string to attempt to
1011 * get a configuration value from
1012 * @return GNUNET_YES if string matched a known
1013 * topology option, GNUNET_NO if not
1016 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption
1018 const char *topology_string)
1021 * Options for connecting a topology as strings.
1023 static const char *topology_option_strings[] = {
1025 * Try to connect all peers specified in the topology.
1030 * Choose a random subset of connections to create.
1032 "CONNECT_RANDOM_SUBSET",
1035 * Create at least X connections for each peer.
1040 * Using a depth first search, create one connection
1041 * per peer. If any are missed (graph disconnected)
1042 * start over at those peers until all have at least one
1048 * Find the N closest peers to each allowed peer in the
1049 * topology and make sure a connection to those peers
1050 * exists in the connect topology.
1055 * No options specified.
1063 if (topology_string == NULL)
1065 while (NULL != topology_option_strings[curr])
1067 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1069 *topology_option = curr;
1074 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1079 * Function to iterate over options. Copies
1080 * the options to the target configuration,
1081 * updating PORT values as needed.
1083 * @param cls closure
1084 * @param section name of the section
1085 * @param option name of the option
1086 * @param value value of the option
1089 update_config (void *cls,
1090 const char *section, const char *option, const char *value)
1092 struct UpdateContext *ctx = cls;
1096 char *single_variable;
1097 char *per_host_variable;
1098 unsigned long long num_per_host;
1100 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1102 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1105 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1108 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
1112 GNUNET_free (single_variable);
1115 if (0 == strcmp (option, "UNIXPATH"))
1117 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1118 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1120 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1123 GNUNET_snprintf (uval,
1125 "/tmp/test-service-%s-%u", section, ctx->upnum++);
1128 else if ((GNUNET_YES ==
1129 GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1131 &num_per_host)) && (num_per_host > 0))
1134 GNUNET_snprintf (uval,
1136 "/tmp/test-service-%s-%u",
1137 section, ctx->fdnum % num_per_host);
1140 GNUNET_free (single_variable);
1141 GNUNET_free (per_host_variable);
1144 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1146 value = ctx->hostname;
1149 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1154 * Create a new configuration using the given configuration
1155 * as a template; however, each PORT in the existing cfg
1156 * must be renumbered by incrementing "*port". If we run
1157 * out of "*port" numbers, return NULL.
1159 * @param cfg template configuration
1160 * @param off the current peer offset
1161 * @param port port numbers to use, update to reflect
1162 * port numbers that were used
1163 * @param upnum number to make unix domain socket names unique
1164 * @param hostname hostname of the controlling host, to allow control connections from
1165 * @param fdnum number used to offset the unix domain socket for grouped processes
1166 * (such as statistics or peerinfo, which can be shared among others)
1168 * @return new configuration, NULL on error
1170 static struct GNUNET_CONFIGURATION_Handle *
1171 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
1174 uint32_t * upnum, const char *hostname, uint32_t * fdnum)
1176 struct UpdateContext uc;
1179 char *allowed_hosts;
1180 unsigned long long temp_port;
1186 uc.ret = GNUNET_CONFIGURATION_create ();
1187 uc.hostname = hostname;
1190 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1191 if (uc.nport >= HIGH_PORT)
1194 GNUNET_CONFIGURATION_destroy (uc.ret);
1198 if (GNUNET_CONFIGURATION_get_value_string
1199 (cfg, "testing", "control_host", &control_host) == GNUNET_OK)
1201 if (hostname != NULL)
1202 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1205 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1207 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1209 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1210 "ACCEPT_FROM", allowed_hosts);
1211 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1213 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1214 "ACCEPT_FROM", allowed_hosts);
1216 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH", "");
1217 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH", "");
1218 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH", "");
1219 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH", "");
1222 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(uc.orig, "statistics", "port", &temp_port) &&
1225 GNUNET_CONFIGURATION_get_value_yesno (uc.orig, "testing",
1226 "single_statistics_per_host")))
1228 GNUNET_CONFIGURATION_set_value_number (uc.ret, "statistics", "port", temp_port + off);
1231 GNUNET_free_non_null (control_host);
1232 GNUNET_free (allowed_hosts);
1236 /* arm needs to know to allow connections from the host on which it is running,
1237 * otherwise gnunet-arm is unable to connect to it in some instances */
1238 if (hostname != NULL)
1240 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1241 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1242 "BINDTO", hostname);
1243 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1244 "BINDTO", hostname);
1245 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1247 GNUNET_free (allowed_hosts);
1251 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1252 "BINDTO", "127.0.0.1");
1253 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1254 "BINDTO", "127.0.0.1");
1257 *port = (uint16_t) uc.nport;
1265 * Add entries to the some list
1267 * @param pg the peer group we are working with
1268 * @param first index of the first peer
1269 * @param second index of the second peer
1270 * @param list the peer list to use
1272 * @return the number of connections added (can be 0, 1 or 2)
1276 remove_connections (struct GNUNET_TESTING_PeerGroup *pg,
1277 unsigned int first, unsigned int second,
1278 enum PeerLists list)
1282 struct PeerConnection **first_list;
1283 struct PeerConnection **second_list;
1284 struct PeerConnection *first_iter;
1285 struct PeerConnection *second_iter;
1286 struct PeerConnection **first_tail;
1287 struct PeerConnection **second_tail;
1290 GNUNET_HashCode hash_first;
1291 GNUNET_HashCode hash_second;
1293 hash_from_uid (first, &hash_first);
1294 hash_from_uid (second, &hash_second);
1302 first_list = &pg->peers[first].allowed_peers_head;
1303 second_list = &pg->peers[second].allowed_peers_head;
1304 first_tail = &pg->peers[first].allowed_peers_tail;
1305 second_tail = &pg->peers[second].allowed_peers_tail;
1308 first_list = &pg->peers[first].connect_peers_head;
1309 second_list = &pg->peers[second].connect_peers_head;
1310 first_tail = &pg->peers[first].connect_peers_tail;
1311 second_tail = &pg->peers[second].connect_peers_tail;
1314 first_list = &pg->peers[first].blacklisted_peers_head;
1315 second_list = &pg->peers[second].blacklisted_peers_head;
1316 first_tail = &pg->peers[first].blacklisted_peers_tail;
1317 second_tail = &pg->peers[second].blacklisted_peers_tail;
1320 first_list = &pg->peers[first].connect_peers_working_set_head;
1321 second_list = &pg->peers[second].connect_peers_working_set_head;
1322 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1323 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1330 first_iter = *first_list;
1331 while (first_iter != NULL)
1333 if (first_iter->index == second)
1335 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1336 GNUNET_free(first_iter);
1340 first_iter = first_iter->next;
1343 second_iter = *second_list;
1344 while (second_iter != NULL)
1346 if (second_iter->index == first)
1348 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1349 GNUNET_free(second_iter);
1353 second_iter = second_iter->next;
1357 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1360 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1365 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1368 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1377 * Add entries to the some list
1379 * @param pg the peer group we are working with
1380 * @param first index of the first peer
1381 * @param second index of the second peer
1382 * @param list the list type that we should modify
1384 * @return the number of connections added (can be 0, 1 or 2)
1388 add_connections (struct GNUNET_TESTING_PeerGroup *pg,
1389 unsigned int first, unsigned int second,
1390 enum PeerLists list)
1396 struct PeerConnection **first_list;
1397 struct PeerConnection **second_list;
1398 struct PeerConnection *first_iter;
1399 struct PeerConnection *second_iter;
1400 struct PeerConnection *new_first;
1401 struct PeerConnection *new_second;
1402 struct PeerConnection **first_tail;
1403 struct PeerConnection **second_tail;
1405 GNUNET_HashCode hash_first;
1406 GNUNET_HashCode hash_second;
1408 hash_from_uid (first, &hash_first);
1409 hash_from_uid (second, &hash_second);
1416 first_list = &pg->peers[first].allowed_peers_head;
1417 second_list = &pg->peers[second].allowed_peers_head;
1418 first_tail = &pg->peers[first].allowed_peers_tail;
1419 second_tail = &pg->peers[second].allowed_peers_tail;
1422 first_list = &pg->peers[first].connect_peers_head;
1423 second_list = &pg->peers[second].connect_peers_head;
1424 first_tail = &pg->peers[first].connect_peers_tail;
1425 second_tail = &pg->peers[second].connect_peers_tail;
1428 first_list = &pg->peers[first].blacklisted_peers_head;
1429 second_list = &pg->peers[second].blacklisted_peers_head;
1430 first_tail = &pg->peers[first].blacklisted_peers_tail;
1431 second_tail = &pg->peers[second].blacklisted_peers_tail;
1434 first_list = &pg->peers[first].connect_peers_working_set_head;
1435 second_list = &pg->peers[second].connect_peers_working_set_head;
1436 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1437 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1444 add_first = GNUNET_YES;
1445 add_second = GNUNET_YES;
1447 first_iter = *first_list;
1448 while (first_iter != NULL)
1450 if (first_iter->index == second)
1452 add_first = GNUNET_NO;
1455 first_iter = first_iter->next;
1458 second_iter = *second_list;
1459 while (second_iter != NULL)
1461 if (second_iter->index == first)
1463 add_second = GNUNET_NO;
1466 second_iter = second_iter->next;
1470 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1473 add_first = GNUNET_YES;
1477 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1480 add_second = GNUNET_YES;
1488 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1489 new_first->index = second;
1490 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1492 new_first->next = *first_list;
1493 *first_list = new_first;*/
1495 GNUNET_assert (GNUNET_OK ==
1496 GNUNET_CONTAINER_multihashmap_put (pg->
1498 [first].blacklisted_peers,
1501 peers[second].daemon,
1502 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1504 pg->peers[first].num_connections++;
1511 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1512 new_second->index = first;
1513 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1515 new_second->next = *second_list;
1516 *second_list = new_second;
1519 GNUNET_assert (GNUNET_OK ==
1520 GNUNET_CONTAINER_multihashmap_put (pg->
1522 [second].blacklisted_peers,
1525 peers[first].daemon,
1526 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1528 pg->peers[second].num_connections++;
1537 * Scale free network construction as described in:
1539 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1541 * Start with a network of "one" peer, then progressively add
1542 * peers up to the total number. At each step, iterate over
1543 * all possible peers and connect new peer based on number of
1544 * existing connections of the target peer.
1546 * @param pg the peer group we are dealing with
1547 * @param proc the connection processor to use
1548 * @param list the peer list to use
1550 * @return the number of connections created
1553 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg,
1554 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1557 unsigned int total_connections;
1558 unsigned int outer_count;
1560 unsigned int previous_total_connections;
1564 GNUNET_assert (pg->total > 1);
1566 /* Add a connection between the first two nodes */
1567 total_connections = proc (pg, 0, 1, list);
1569 for (outer_count = 1; outer_count < pg->total; outer_count++)
1571 previous_total_connections = total_connections;
1572 for (i = 0; i < outer_count; i++)
1575 pg->peers[i].num_connections /
1576 (double) previous_total_connections;
1579 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1580 UINT64_MAX)) / ((double) UINT64_MAX);
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1583 "Considering connecting peer %d to peer %d\n",
1586 if (random < probability)
1589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1590 "Connecting peer %d to peer %d\n", outer_count, i);
1592 total_connections += proc (pg, outer_count, i, list);
1597 return total_connections;
1601 * Create a topology given a peer group (set of running peers)
1602 * and a connection processor. Creates a small world topology
1603 * according to the rewired ring construction. The basic
1604 * behavior is that a ring topology is created, but with some
1605 * probability instead of connecting a peer to the next
1606 * neighbor in the ring a connection will be created to a peer
1607 * selected uniformly at random. We use the TESTING
1608 * PERCENTAGE option to specify what number of
1609 * connections each peer should have. Default is 2,
1610 * which makes the ring, any given number is multiplied by
1611 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1612 * each peer have on average 2logn connections. The additional
1613 * connections are made at increasing distance around the ring
1614 * from the original peer, or to random peers based on the re-
1615 * wiring probability. The TESTING
1616 * PROBABILITY option is used as the probability that a given
1617 * connection is rewired.
1619 * @param pg the peergroup to create the topology on
1620 * @param proc the connection processor to call to actually set
1621 * up connections between two peers
1622 * @param list the peer list to use
1624 * @return the number of connections that were set up
1628 create_small_world_ring (struct GNUNET_TESTING_PeerGroup *pg,
1629 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1633 unsigned int natLog;
1634 unsigned int randomPeer;
1635 double random, logNModifier, probability;
1636 unsigned int smallWorldConnections;
1641 unsigned int useAnd;
1642 int connect_attempts;
1644 logNModifier = 0.5; /* FIXME: default value? */
1645 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1650 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1653 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1654 p_string, "LOGNMODIFIER", "TESTING");
1655 GNUNET_free (p_string);
1657 probability = 0.5; /* FIXME: default percentage? */
1658 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1663 if (sscanf (p_string, "%lf", &probability) != 1)
1664 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1666 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1667 p_string, "PERCENTAGE", "TESTING");
1668 GNUNET_free (p_string);
1670 natLog = log (pg->total);
1671 connsPerPeer = ceil (natLog * logNModifier);
1673 if (connsPerPeer % 2 == 1)
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."), connsPerPeer);
1678 smallWorldConnections = 0;
1679 connect_attempts = 0;
1680 for (i = 0; i < pg->total; i++)
1683 max = i + connsPerPeer / 2;
1684 min = i - connsPerPeer / 2;
1686 if (max > pg->total - 1)
1688 max = max - pg->total;
1694 min = pg->total - 1 + min;
1698 for (j = 0; j < connsPerPeer / 2; j++)
1702 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1703 UINT64_MAX) / ((double) UINT64_MAX));
1704 if (random < probability)
1706 /* Connect to uniformly selected random peer */
1708 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1710 while ((((randomPeer < max) && (randomPeer > min))
1711 && (useAnd == 0)) || (((randomPeer > min)
1712 || (randomPeer < max))
1716 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1719 smallWorldConnections += proc (pg, i, randomPeer, list);
1723 nodeToConnect = i + j + 1;
1724 if (nodeToConnect > pg->total - 1)
1726 nodeToConnect = nodeToConnect - pg->total;
1728 connect_attempts += proc (pg, i, nodeToConnect, list);
1734 connect_attempts += smallWorldConnections;
1736 return connect_attempts;
1740 * Create a topology given a peer group (set of running peers)
1741 * and a connection processor.
1743 * @param pg the peergroup to create the topology on
1744 * @param proc the connection processor to call to actually set
1745 * up connections between two peers
1746 * @param list the peer list to use
1748 * @return the number of connections that were set up
1752 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg,
1753 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1755 unsigned int outer_count, inner_count;
1756 unsigned int cutoff;
1757 int connect_attempts;
1758 double nat_percentage;
1761 nat_percentage = 0.6; /* FIXME: default percentage? */
1762 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1767 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1768 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1770 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1771 p_string, "PERCENTAGE", "TESTING");
1772 GNUNET_free (p_string);
1777 cutoff = (unsigned int) (nat_percentage * pg->total);
1779 connect_attempts = 0;
1781 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1783 for (inner_count = outer_count + 1; inner_count < pg->total;
1786 if ((outer_count > cutoff) || (inner_count > cutoff))
1789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1790 "Connecting peer %d to peer %d\n",
1791 outer_count, inner_count);
1793 connect_attempts += proc (pg, outer_count, inner_count, list);
1798 return connect_attempts;
1803 * Create a topology given a peer group (set of running peers)
1804 * and a connection processor.
1806 * @param pg the peergroup to create the topology on
1807 * @param proc the connection processor to call to actually set
1808 * up connections between two peers
1809 * @param list the peer list to use
1811 * @return the number of connections that were set up
1815 create_small_world (struct GNUNET_TESTING_PeerGroup *pg,
1816 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1818 unsigned int i, j, k;
1819 unsigned int square;
1822 unsigned int toggle = 1;
1823 unsigned int nodeToConnect;
1824 unsigned int natLog;
1825 unsigned int node1Row;
1826 unsigned int node1Col;
1827 unsigned int node2Row;
1828 unsigned int node2Col;
1829 unsigned int distance;
1830 double probability, random, percentage;
1831 unsigned int smallWorldConnections;
1832 unsigned int small_world_it;
1834 int connect_attempts;
1835 square = floor (sqrt (pg->total));
1839 percentage = 0.5; /* FIXME: default percentage? */
1840 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1845 if (sscanf (p_string, "%lf", &percentage) != 1)
1846 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1848 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1849 p_string, "PERCENTAGE", "TESTING");
1850 GNUNET_free (p_string);
1852 if (percentage < 0.0)
1854 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1856 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1857 "PERCENTAGE", "TESTING", percentage);
1860 probability = 0.5; /* FIXME: default percentage? */
1861 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1866 if (sscanf (p_string, "%lf", &probability) != 1)
1867 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1869 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1870 p_string, "PROBABILITY", "TESTING");
1871 GNUNET_free (p_string);
1873 if (square * square != pg->total)
1875 while (rows * cols < pg->total)
1877 if (toggle % 2 == 0)
1886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1888 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1892 connect_attempts = 0;
1893 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1894 * to the node to its right and above. Once this is over, we'll have our torus!
1895 * Special case for the last node (if the rows and columns are not equal), connect
1896 * to the first in the row to maintain topology.
1898 for (i = 0; i < pg->total; i++)
1900 /* First connect to the node to the right */
1901 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1902 nodeToConnect = i + 1;
1903 else if (i + 1 == pg->total)
1904 nodeToConnect = rows * cols - cols;
1906 nodeToConnect = i - cols + 1;
1908 connect_attempts += proc (pg, i, nodeToConnect, list);
1912 nodeToConnect = (rows * cols) - cols + i;
1913 if (nodeToConnect >= pg->total)
1914 nodeToConnect -= cols;
1917 nodeToConnect = i - cols;
1919 if (nodeToConnect < pg->total)
1920 connect_attempts += proc (pg, i, nodeToConnect, list);
1922 natLog = log (pg->total);
1923 #if VERBOSE_TESTING > 2
1924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1925 _("natural log of %d is %d, will run %d iterations\n"),
1926 pg->total, natLog, (int) (natLog * percentage));
1927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1928 _("Total connections added thus far: %u!\n"), connect_attempts);
1930 smallWorldConnections = 0;
1931 small_world_it = (unsigned int) (natLog * percentage);
1932 if (small_world_it < 1)
1934 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
1935 for (i = 0; i < small_world_it; i++)
1937 for (j = 0; j < pg->total; j++)
1939 /* Determine the row and column of node at position j on the 2d torus */
1940 node1Row = j / cols;
1941 node1Col = j - (node1Row * cols);
1942 for (k = 0; k < pg->total; k++)
1944 /* Determine the row and column of node at position k on the 2d torus */
1945 node2Row = k / cols;
1946 node2Col = k - (node2Row * cols);
1947 /* Simple Cartesian distance */
1949 abs (node1Row - node2Row) + abs (node1Col - node2Col);
1952 /* Calculate probability as 1 over the square of the distance */
1953 probability = 1.0 / (distance * distance);
1954 /* Choose a random value between 0 and 1 */
1957 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1959 ((double) UINT64_MAX);
1960 /* If random < probability, then connect the two nodes */
1961 if (random < probability)
1962 smallWorldConnections += proc (pg, j, k, list);
1968 connect_attempts += smallWorldConnections;
1969 #if VERBOSE_TESTING > 2
1970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1971 _("Total connections added for small world: %d!\n"),
1972 smallWorldConnections);
1974 return connect_attempts;
1978 * Create a topology given a peer group (set of running peers)
1979 * and a connection processor.
1981 * @param pg the peergroup to create the topology on
1982 * @param proc the connection processor to call to actually set
1983 * up connections between two peers
1984 * @param list the peer list to use
1986 * @return the number of connections that were set up
1990 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg,
1991 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1994 unsigned int outer_count;
1995 unsigned int inner_count;
1996 int connect_attempts;
2000 probability = 0.5; /* FIXME: default percentage? */
2001 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
2006 if (sscanf (p_string, "%lf", &probability) != 1)
2007 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2009 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2010 p_string, "PROBABILITY", "TESTING");
2011 GNUNET_free (p_string);
2013 connect_attempts = 0;
2014 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2016 for (inner_count = outer_count + 1; inner_count < pg->total;
2021 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2022 UINT64_MAX)) / ((double) UINT64_MAX);
2024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2025 _("rand is %f probability is %f\n"), temp_rand,
2028 if (temp_rand < probability)
2030 connect_attempts += proc (pg, outer_count, inner_count, list);
2035 return connect_attempts;
2039 * Create a topology given a peer group (set of running peers)
2040 * and a connection processor. This particular function creates
2041 * the connections for a 2d-torus, plus additional "closest"
2042 * connections per peer.
2044 * @param pg the peergroup to create the topology on
2045 * @param proc the connection processor to call to actually set
2046 * up connections between two peers
2047 * @param list the peer list to use
2049 * @return the number of connections that were set up
2053 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg,
2054 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2057 unsigned int square;
2060 unsigned int toggle = 1;
2061 unsigned int nodeToConnect;
2062 int connect_attempts;
2064 connect_attempts = 0;
2066 square = floor (sqrt (pg->total));
2070 if (square * square != pg->total)
2072 while (rows * cols < pg->total)
2074 if (toggle % 2 == 0)
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2085 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2088 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2089 * to the node to its right and above. Once this is over, we'll have our torus!
2090 * Special case for the last node (if the rows and columns are not equal), connect
2091 * to the first in the row to maintain topology.
2093 for (i = 0; i < pg->total; i++)
2095 /* First connect to the node to the right */
2096 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2097 nodeToConnect = i + 1;
2098 else if (i + 1 == pg->total)
2099 nodeToConnect = rows * cols - cols;
2101 nodeToConnect = i - cols + 1;
2103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2104 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2106 connect_attempts += proc (pg, i, nodeToConnect, list);
2108 /* Second connect to the node immediately above */
2111 nodeToConnect = (rows * cols) - cols + i;
2112 if (nodeToConnect >= pg->total)
2113 nodeToConnect -= cols;
2116 nodeToConnect = i - cols;
2118 if (nodeToConnect < pg->total)
2121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2122 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2124 connect_attempts += proc (pg, i, nodeToConnect, list);
2129 return connect_attempts;
2134 * Create a topology given a peer group (set of running peers)
2135 * and a connection processor.
2137 * @param pg the peergroup to create the topology on
2138 * @param proc the connection processor to call to actually set
2139 * up connections between two peers
2140 * @param list the peer list to use
2142 * @return the number of connections that were set up
2146 create_clique (struct GNUNET_TESTING_PeerGroup *pg,
2147 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2149 unsigned int outer_count;
2150 unsigned int inner_count;
2151 int connect_attempts;
2153 connect_attempts = 0;
2155 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2157 for (inner_count = outer_count + 1; inner_count < pg->total;
2161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2162 "Connecting peer %d to peer %d\n",
2163 outer_count, inner_count);
2165 connect_attempts += proc (pg, outer_count, inner_count, list);
2169 return connect_attempts;
2174 * Iterator over hash map entries.
2176 * @param cls closure the peer group
2177 * @param key the key stored in the hashmap is the
2178 * index of the peer to connect to
2179 * @param value value in the hash map, handle to the peer daemon
2180 * @return GNUNET_YES if we should continue to
2185 unblacklist_iterator (void *cls,
2186 const GNUNET_HashCode * key,
2189 struct UnblacklistContext *un_ctx = cls;
2190 uint32_t second_pos;
2192 uid_from_hash (key, &second_pos);
2194 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2201 * Create a blacklist topology based on the allowed topology
2202 * which disallows any connections not in the allowed topology
2203 * at the transport level.
2205 * @param pg the peergroup to create the topology on
2206 * @param proc the connection processor to call to allow
2207 * up connections between two peers
2209 * @return the number of connections that were set up
2213 copy_allowed (struct GNUNET_TESTING_PeerGroup *pg,
2214 GNUNET_TESTING_ConnectionProcessor proc)
2216 struct UnblacklistContext un_ctx;
2219 struct PeerConnection *iter;
2223 for (count = 0; count < pg->total - 1; count++)
2225 un_ctx.first_uid = count;
2227 iter = pg->peers[count].allowed_peers_head;
2228 while (iter != NULL)
2230 remove_connections(pg, count, iter->index, BLACKLIST);
2231 //unblacklist_connections(pg, count, iter->index);
2235 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers, &unblacklist_iterator, &un_ctx);
2238 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2243 * Create a topology given a peer group (set of running peers)
2244 * and a connection processor.
2246 * @param pg the peergroup to create the topology on
2247 * @param proc the connection processor to call to actually set
2248 * up connections between two peers
2249 * @param list which list should be modified
2251 * @return the number of connections that were set up
2255 create_line (struct GNUNET_TESTING_PeerGroup *pg,
2256 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2259 int connect_attempts;
2261 connect_attempts = 0;
2263 /* Connect each peer to the next highest numbered peer */
2264 for (count = 0; count < pg->total - 1; count++)
2267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2268 "Connecting peer %d to peer %d\n", count, count + 1);
2270 connect_attempts += proc (pg, count, count + 1, list);
2273 return connect_attempts;
2277 * Create a topology given a peer group (set of running peers)
2278 * and a connection processor.
2280 * @param pg the peergroup to create the topology on
2281 * @param filename the file to read topology information from
2282 * @param proc the connection processor to call to actually set
2283 * up connections between two peers
2284 * @param list the peer list to use
2286 * @return the number of connections that were set up
2290 create_from_file (struct GNUNET_TESTING_PeerGroup *pg,
2292 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2294 int connect_attempts;
2295 unsigned int first_peer_index;
2296 unsigned int second_peer_index;
2297 connect_attempts = 0;
2302 unsigned int total_peers;
2304 enum States curr_state;
2306 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2307 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2309 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2312 "Could not open file `%s' specified for topology!", filename);
2313 return connect_attempts;
2316 data = GNUNET_malloc_large (frstat.st_size);
2317 GNUNET_assert(data != NULL);
2318 if (frstat.st_size !=
2319 GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2322 "Could not read file %s specified for host list, ending test!", filename);
2324 return connect_attempts;
2329 /* First line should contain a single integer, specifying the number of peers */
2330 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2331 curr_state = NUM_PEERS;
2332 while (count < frstat.st_size - 1)
2334 if ((buf[count] == '\n') || (buf[count] == ' '))
2343 if (1 != sscanf(&buf[count], "%u", &total_peers))
2345 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read number of peers from topology file!\n");
2346 GNUNET_free_non_null(data);
2347 return connect_attempts;
2349 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read %u total peers in topology\n", total_peers);
2350 GNUNET_assert(total_peers == pg->total);
2351 curr_state = PEER_INDEX;
2352 while((buf[count] != '\n') && (count < frstat.st_size - 1))
2357 if (1 != sscanf(&buf[count], "%u", &first_peer_index))
2359 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read peer index from topology file!\n");
2360 GNUNET_free_non_null(data);
2361 return connect_attempts;
2363 while((buf[count] != ':') && (count < frstat.st_size - 1))
2366 curr_state = OTHER_PEER_INDEX;
2369 if (1 == sscanf(&buf[count], ":"))
2370 curr_state = OTHER_PEER_INDEX;
2373 case OTHER_PEER_INDEX:
2374 if (1 != sscanf(&buf[count], "%u", &second_peer_index))
2376 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to peer index from topology file!\n");
2377 GNUNET_free_non_null(data);
2378 return connect_attempts;
2380 /* Assume file is written with first peer 1, but array index is 0 */
2381 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index - 1, list);
2382 while((buf[count] != '\n') && (buf[count] != ',') && (count < frstat.st_size - 1))
2384 if (buf[count] == '\n')
2386 curr_state = PEER_INDEX;
2388 else if (buf[count] != ',')
2390 curr_state = OTHER_PEER_INDEX;
2395 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Found bad data in topology file while in state %d!\n", curr_state);
2397 return connect_attempts;
2402 /* Connect each peer to the next highest numbered peer */
2403 for (count = 0; count < pg->total - 1; count++)
2406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2407 "Connecting peer %d to peer %d\n", first_peer_index, second_peer_index);
2409 connect_attempts += proc (pg, first_peer_index, second_peer_index);
2412 return connect_attempts;
2416 * Create a topology given a peer group (set of running peers)
2417 * and a connection processor.
2419 * @param pg the peergroup to create the topology on
2420 * @param proc the connection processor to call to actually set
2421 * up connections between two peers
2422 * @param list the peer list to use
2424 * @return the number of connections that were set up
2428 create_ring (struct GNUNET_TESTING_PeerGroup *pg,
2429 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2432 int connect_attempts;
2434 connect_attempts = 0;
2436 /* Connect each peer to the next highest numbered peer */
2437 for (count = 0; count < pg->total - 1; count++)
2440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2441 "Connecting peer %d to peer %d\n", count, count + 1);
2443 connect_attempts += proc (pg, count, count + 1, list);
2446 /* Connect the last peer to the first peer */
2447 connect_attempts += proc (pg, pg->total - 1, 0, list);
2449 return connect_attempts;
2454 * Iterator for writing friends of a peer to a file.
2456 * @param cls closure, an open writable file handle
2457 * @param key the key the daemon was stored under
2458 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2460 * @return GNUNET_YES to continue iteration
2462 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2463 * with a single file_iterator that takes a closure which contains
2464 * the prefix to write before the peer. Then this could be used
2465 * for blacklisting multiple transports and writing the friend
2466 * file. I'm sure *someone* will complain loudly about other
2467 * things that negate these functions even existing so no point in
2471 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2473 FILE *temp_friend_handle = cls;
2474 struct GNUNET_TESTING_Daemon *peer = value;
2475 struct GNUNET_PeerIdentity *temppeer;
2476 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2478 temppeer = &peer->id;
2479 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2480 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2485 struct BlacklistContext
2488 * The (open) file handle to write to
2490 FILE *temp_file_handle;
2493 * The transport that this peer will be blacklisted on.
2499 * Iterator for writing blacklist data to appropriate files.
2501 * @param cls closure, an open writable file handle
2502 * @param key the key the daemon was stored under
2503 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2505 * @return GNUNET_YES to continue iteration
2508 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2510 struct BlacklistContext *blacklist_ctx = cls;
2511 struct GNUNET_TESTING_Daemon *peer = value;
2512 struct GNUNET_PeerIdentity *temppeer;
2513 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2515 temppeer = &peer->id;
2516 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2517 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2518 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2519 blacklist_ctx->transport, (char *) &peer_enc);
2527 * Create the friend files based on the PeerConnection's
2528 * of each peer in the peer group, and copy the files
2529 * to the appropriate place
2531 * @param pg the peer group we are dealing with
2534 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
2536 FILE *temp_friend_handle;
2537 unsigned int pg_iter;
2538 char *temp_service_path;
2539 struct GNUNET_OS_Process **procarr;
2542 enum GNUNET_OS_ProcessStatusType type;
2543 unsigned long return_code;
2548 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2549 struct PeerConnection *conn_iter;
2551 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2552 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2554 mytemp = GNUNET_DISK_mktemp ("friends");
2555 GNUNET_assert (mytemp != NULL);
2556 temp_friend_handle = fopen (mytemp, "wt");
2557 GNUNET_assert (temp_friend_handle != NULL);
2559 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2560 while (conn_iter != NULL)
2562 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
2563 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2564 conn_iter = conn_iter->next;
2567 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2568 &friend_file_iterator,
2569 temp_friend_handle);
2571 fclose (temp_friend_handle);
2574 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2575 daemon->cfg, "PATHS",
2577 &temp_service_path))
2579 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2581 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2582 "SERVICEHOME", "PATHS");
2583 if (UNLINK (mytemp) != 0)
2584 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2586 GNUNET_free (mytemp);
2590 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2592 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2593 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2598 _("Copying file with command cp %s %s\n"), mytemp, arg);
2603 else /* Remote, scp the file to the correct place */
2605 if (NULL != pg->peers[pg_iter].daemon->username)
2606 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2607 pg->peers[pg_iter].daemon->username,
2608 pg->peers[pg_iter].daemon->hostname,
2611 GNUNET_asprintf (&arg, "%s:%s/friends",
2612 pg->peers[pg_iter].daemon->hostname,
2615 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2620 _("Copying file with command scp %s %s\n"), mytemp,
2625 GNUNET_free (temp_service_path);
2626 GNUNET_free (mytemp);
2630 ret = GNUNET_SYSERR;
2631 while ((count < max_wait) && (ret != GNUNET_OK))
2634 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2638 _("Checking copy status of file %d\n"), pg_iter);
2640 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2642 if (GNUNET_OS_process_status
2643 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2645 ret = GNUNET_SYSERR;
2647 else if ((type != GNUNET_OS_PROCESS_EXITED)
2648 || (return_code != 0))
2650 ret = GNUNET_SYSERR;
2654 GNUNET_OS_process_close (procarr[pg_iter]);
2655 procarr[pg_iter] = NULL;
2657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2658 _("File %d copied\n"), pg_iter);
2664 if (ret == GNUNET_SYSERR)
2666 /* FIXME: why sleep here? -CG */
2672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2673 _("Finished copying all friend files!\n"));
2675 GNUNET_free (procarr);
2681 * Create the blacklist files based on the PeerConnection's
2682 * of each peer in the peer group, and copy the files
2683 * to the appropriate place.
2685 * @param pg the peer group we are dealing with
2686 * @param transports space delimited list of transports to blacklist
2689 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg,
2690 const char *transports)
2692 FILE *temp_file_handle;
2693 unsigned int pg_iter;
2694 char *temp_service_path;
2695 struct GNUNET_OS_Process **procarr;
2698 enum GNUNET_OS_ProcessStatusType type;
2699 unsigned long return_code;
2706 char *temp_transports;
2709 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2710 struct PeerConnection *conn_iter;
2712 static struct BlacklistContext blacklist_ctx;
2715 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2716 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2718 mytemp = GNUNET_DISK_mktemp ("blacklist");
2719 GNUNET_assert (mytemp != NULL);
2720 temp_file_handle = fopen (mytemp, "wt");
2721 GNUNET_assert (temp_file_handle != NULL);
2722 temp_transports = GNUNET_strdup (transports);
2724 blacklist_ctx.temp_file_handle = temp_file_handle;
2726 transport_len = strlen (temp_transports) + 1;
2729 for (i = 0; i < transport_len; i++)
2731 if ((temp_transports[i] == ' ') && (pos == NULL))
2732 continue; /* At start of string (whitespace) */
2733 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2735 temp_transports[i] = '\0';
2737 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
2738 while (conn_iter != NULL)
2740 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
2741 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
2742 conn_iter = conn_iter->next;
2746 blacklist_ctx.transport = pos;
2747 entry_count = GNUNET_CONTAINER_multihashmap_iterate (pg->
2749 [pg_iter].blacklisted_peers,
2750 &blacklist_file_iterator,
2754 } /* At beginning of actual string */
2755 else if (pos == NULL)
2757 pos = &temp_transports[i];
2761 GNUNET_free (temp_transports);
2762 fclose (temp_file_handle);
2765 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2766 daemon->cfg, "PATHS",
2768 &temp_service_path))
2770 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2772 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2773 "SERVICEHOME", "PATHS");
2774 if (UNLINK (mytemp) != 0)
2775 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2777 GNUNET_free (mytemp);
2781 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2783 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2784 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2789 _("Copying file with command cp %s %s\n"), mytemp, arg);
2794 else /* Remote, scp the file to the correct place */
2796 if (NULL != pg->peers[pg_iter].daemon->username)
2797 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
2798 pg->peers[pg_iter].daemon->username,
2799 pg->peers[pg_iter].daemon->hostname,
2802 GNUNET_asprintf (&arg, "%s:%s/blacklist",
2803 pg->peers[pg_iter].daemon->hostname,
2806 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2809 GNUNET_OS_process_wait(procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2813 _("Copying file with command scp %s %s\n"), mytemp,
2818 GNUNET_free (temp_service_path);
2819 GNUNET_free (mytemp);
2823 ret = GNUNET_SYSERR;
2824 while ((count < max_wait) && (ret != GNUNET_OK))
2827 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2831 _("Checking copy status of file %d\n"), pg_iter);
2833 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2835 if (GNUNET_OS_process_status
2836 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2838 ret = GNUNET_SYSERR;
2840 else if ((type != GNUNET_OS_PROCESS_EXITED)
2841 || (return_code != 0))
2843 ret = GNUNET_SYSERR;
2847 GNUNET_OS_process_close (procarr[pg_iter]);
2848 procarr[pg_iter] = NULL;
2850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2851 _("File %d copied\n"), pg_iter);
2857 if (ret == GNUNET_SYSERR)
2859 /* FIXME: why sleep here? -CG */
2865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2866 _("Finished copying all blacklist files!\n"));
2868 GNUNET_free (procarr);
2872 /* Forward Declaration */
2874 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2877 * Choose a random peer's next connection to create, and
2878 * call schedule_connect to set up the connect task.
2880 * @param ct_ctx the overall connection context
2882 static void preschedule_connect(struct ConnectTopologyContext *ct_ctx)
2884 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
2885 struct PeerConnection *connection_iter;
2886 struct ConnectContext *connect_context;
2887 uint32_t random_peer;
2889 if (ct_ctx->remaining_connects_to_schedule == 0)
2891 random_peer = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
2892 while (pg->peers[random_peer].connect_peers_head == NULL)
2893 random_peer = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
2895 connection_iter = pg->peers[random_peer].connect_peers_head;
2896 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling connection between %d and %d\n", random_peer, connection_iter->index);
2898 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
2899 connect_context->first = pg->peers[random_peer].daemon;
2900 connect_context->second = pg->peers[connection_iter->index].daemon;
2901 connect_context->ct_ctx = ct_ctx;
2902 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
2903 GNUNET_CONTAINER_DLL_remove(pg->peers[random_peer].connect_peers_head, pg->peers[random_peer].connect_peers_tail, connection_iter);
2904 ct_ctx->remaining_connects_to_schedule--;
2909 * Internal notification of a connection, kept so that we can ensure some connections
2910 * happen instead of flooding all testing daemons with requests to connect.
2913 internal_connect_notify (void *cls,
2914 const struct GNUNET_PeerIdentity *first,
2915 const struct GNUNET_PeerIdentity *second,
2917 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2918 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2919 struct GNUNET_TESTING_Daemon *first_daemon,
2920 struct GNUNET_TESTING_Daemon *second_daemon,
2923 struct ConnectTopologyContext *ct_ctx = cls;
2924 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
2925 pg->outstanding_connects--;
2926 ct_ctx->remaining_connections--;
2927 if (ct_ctx->remaining_connections == 0)
2929 if (ct_ctx->notify_connections_done != NULL)
2930 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
2931 GNUNET_free (ct_ctx);
2934 preschedule_connect(ct_ctx);
2936 if (pg->notify_connection != NULL)
2937 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
2938 first_cfg, second_cfg, first_daemon, second_daemon,
2944 * Either delay a connection (because there are too many outstanding)
2945 * or schedule it for right now.
2947 * @param cls a connection context
2948 * @param tc the task runtime context
2951 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2953 struct ConnectContext *connect_context = cls;
2954 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
2956 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2959 if (pg->outstanding_connects > pg->max_outstanding_connections)
2961 #if VERBOSE_TESTING > 2
2962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2964 ("Delaying connect, we have too many outstanding connections!\n"));
2966 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2967 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
2968 &schedule_connect, connect_context);
2972 #if VERBOSE_TESTING > 2
2973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2974 _("Creating connection, outstanding_connections is %d\n"),
2975 outstanding_connects);
2977 pg->outstanding_connects++;
2978 pg->total_connects_scheduled++;
2979 GNUNET_TESTING_daemons_connect (connect_context->first,
2980 connect_context->second,
2981 connect_context->ct_ctx->connect_timeout,
2982 connect_context->ct_ctx->connect_attempts,
2983 &internal_connect_notify,
2984 connect_context->ct_ctx);
2985 GNUNET_free (connect_context);
2991 * Iterator for actually scheduling connections to be created
2992 * between two peers.
2994 * @param cls closure, a GNUNET_TESTING_Daemon
2995 * @param key the key the second Daemon was stored under
2996 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2998 * @return GNUNET_YES to continue iteration
3001 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3003 struct ConnectTopologyContext *ct_ctx = cls;
3004 struct PeerData *first = ct_ctx->first;
3005 struct GNUNET_TESTING_Daemon *second = value;
3006 struct ConnectContext *connect_context;
3008 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3009 connect_context->first = first->daemon;
3010 connect_context->second = second;
3011 connect_context->ct_ctx = ct_ctx;
3012 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3020 * Iterator for copying all entries in the allowed hashmap to the
3023 * @param cls closure, a GNUNET_TESTING_Daemon
3024 * @param key the key the second Daemon was stored under
3025 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3027 * @return GNUNET_YES to continue iteration
3030 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3032 struct PeerData *first = cls;
3034 GNUNET_assert (GNUNET_OK ==
3035 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
3037 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3044 * Make the peers to connect the same as those that are allowed to be
3047 * @param pg the peer group
3050 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
3052 unsigned int pg_iter;
3056 struct PeerConnection *iter;
3060 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3063 iter = pg->peers[pg_iter].allowed_peers_head;
3064 while (iter != NULL)
3066 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating connection between %d and %d\n", pg_iter, iter->index);
3067 total += add_connections(pg, pg_iter, iter->index, CONNECT);
3068 //total += add_actual_connections(pg, pg_iter, iter->index);
3073 GNUNET_CONTAINER_multihashmap_iterate (pg->
3074 peers[pg_iter].allowed_peers,
3075 ©_topology_iterator,
3076 &pg->peers[pg_iter]);
3078 if (GNUNET_SYSERR == ret)
3079 return GNUNET_SYSERR;
3081 total = total + ret;
3089 * Connect the topology as specified by the PeerConnection's
3090 * of each peer in the peer group
3092 * @param pg the peer group we are dealing with
3093 * @param connect_timeout how long try connecting two peers
3094 * @param connect_attempts how many times (max) to attempt
3095 * @param notify_callback callback to notify when finished
3096 * @param notify_cls closure for notify callback
3098 * @return the number of connections that will be attempted
3101 connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3102 struct GNUNET_TIME_Relative connect_timeout,
3103 unsigned int connect_attempts,
3104 GNUNET_TESTING_NotifyCompletion notify_callback,
3107 unsigned int pg_iter;
3109 struct ConnectTopologyContext *ct_ctx;
3111 struct PeerConnection *connection_iter;
3115 ct_ctx = GNUNET_malloc (sizeof (struct ConnectTopologyContext));
3116 ct_ctx->notify_connections_done = notify_callback;
3117 ct_ctx->notify_cls = notify_cls;
3120 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3123 connection_iter = pg->peers[pg_iter].connect_peers_head;
3124 while (connection_iter != NULL)
3126 connection_iter = connection_iter->next;
3131 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3137 GNUNET_free (ct_ctx);
3140 ct_ctx->connect_timeout = connect_timeout;
3141 ct_ctx->connect_attempts = connect_attempts;
3142 ct_ctx->remaining_connections = total;
3143 ct_ctx->remaining_connects_to_schedule = total;
3145 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3147 preschedule_connect(ct_ctx);
3155 * Takes a peer group and creates a topology based on the
3156 * one specified. Creates a topology means generates friend
3157 * files for the peers so they can only connect to those allowed
3158 * by the topology. This will only have an effect once peers
3159 * are started if the FRIENDS_ONLY option is set in the base
3160 * config. Also takes an optional restrict topology which
3161 * disallows connections based on particular transports
3162 * UNLESS they are specified in the restricted topology.
3164 * @param pg the peer group struct representing the running peers
3165 * @param topology which topology to connect the peers in
3166 * @param restrict_topology disallow restrict_transports transport
3167 * connections to peers NOT in this topology
3168 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3169 * @param restrict_transports space delimited list of transports to blacklist
3170 * to create restricted topology
3172 * @return the maximum number of connections were all allowed peers
3173 * connected to each other
3176 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
3177 enum GNUNET_TESTING_Topology topology,
3178 enum GNUNET_TESTING_Topology
3180 const char *restrict_transports)
3184 unsigned int num_connections;
3185 int unblacklisted_connections;
3190 for (i = 0; i < pg->total; i++)
3192 pg->peers[i].allowed_peers =
3193 GNUNET_CONTAINER_multihashmap_create (100);
3194 pg->peers[i].connect_peers =
3195 GNUNET_CONTAINER_multihashmap_create (100);
3196 pg->peers[i].blacklisted_peers =
3197 GNUNET_CONTAINER_multihashmap_create (100);
3198 pg->peers[i].pg = pg;
3204 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3208 num_connections = create_clique (pg, &add_connections, ALLOWED);
3210 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3213 _("Creating small world (ring) topology\n"));
3216 create_small_world_ring (pg, &add_connections, ALLOWED);
3218 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3221 _("Creating small world (2d-torus) topology\n"));
3223 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3225 case GNUNET_TESTING_TOPOLOGY_RING:
3227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3229 num_connections = create_ring (pg, &add_connections, ALLOWED);
3231 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3235 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3237 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3240 _("Creating Erdos-Renyi topology\n"));
3242 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3244 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3248 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3250 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3253 _("Creating Scale Free topology\n"));
3255 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3257 case GNUNET_TESTING_TOPOLOGY_LINE:
3259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3260 _("Creating straight line topology\n"));
3262 num_connections = create_line (pg, &add_connections, ALLOWED);
3264 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3267 _("Creating topology from file!\n"));
3270 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing", "topology_file",
3272 num_connections = create_from_file (pg, filename, &add_connections, ALLOWED);
3275 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3276 num_connections = 0;
3279 case GNUNET_TESTING_TOPOLOGY_NONE:
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3283 ("Creating no allowed topology (all peers can connect at core level)\n"));
3285 num_connections = pg->total * pg->total; /* Clique is allowed! */
3288 num_connections = 0;
3293 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
3295 ret = create_and_copy_friend_files (pg);
3296 if (ret != GNUNET_OK)
3299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3300 _("Failed during friend file copying!\n"));
3302 return GNUNET_SYSERR;
3307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3308 _("Friend files created/copied successfully!\n"));
3313 /* Use the create clique method to initially set all connections as blacklisted. */
3314 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
3315 create_clique (pg, &add_connections, BLACKLIST);
3317 unblacklisted_connections = 0;
3318 /* Un-blacklist connections as per the topology specified */
3319 switch (restrict_topology)
3321 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3324 _("Blacklisting all but clique topology\n"));
3326 unblacklisted_connections =
3327 create_clique (pg, &remove_connections, BLACKLIST);
3329 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3332 _("Blacklisting all but small world (ring) topology\n"));
3334 unblacklisted_connections =
3335 create_small_world_ring (pg, &remove_connections, BLACKLIST);
3337 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3341 ("Blacklisting all but small world (2d-torus) topology\n"));
3343 unblacklisted_connections =
3344 create_small_world (pg, &remove_connections, BLACKLIST);
3346 case GNUNET_TESTING_TOPOLOGY_RING:
3348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3349 _("Blacklisting all but ring topology\n"));
3351 unblacklisted_connections = create_ring (pg, &remove_connections, BLACKLIST);
3353 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3356 _("Blacklisting all but 2d torus topology\n"));
3358 unblacklisted_connections =
3359 create_2d_torus (pg, &remove_connections, BLACKLIST);
3361 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3364 _("Blacklisting all but Erdos-Renyi topology\n"));
3366 unblacklisted_connections =
3367 create_erdos_renyi (pg, &remove_connections, BLACKLIST);
3369 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3372 _("Blacklisting all but InterNAT topology\n"));
3374 unblacklisted_connections =
3375 create_nated_internet (pg, &remove_connections, BLACKLIST);
3377 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3380 _("Blacklisting all but Scale Free topology\n"));
3382 unblacklisted_connections =
3383 create_scale_free (pg, &remove_connections, BLACKLIST);
3385 case GNUNET_TESTING_TOPOLOGY_LINE:
3387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3388 _("Blacklisting all but straight line topology\n"));
3390 unblacklisted_connections = create_line (pg, &remove_connections, BLACKLIST);
3392 case GNUNET_TESTING_TOPOLOGY_NONE: /* Fall through */
3393 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3397 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
3399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3401 ("Creating blacklist topology from allowed\n"));
3402 unblacklisted_connections = copy_allowed (pg, &remove_connections);
3407 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
3409 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n", restrict_transports);
3410 ret = create_and_copy_blacklist_files (pg, restrict_transports);
3411 if (ret != GNUNET_OK)
3414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3415 _("Failed during blacklist file copying!\n"));
3422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3423 _("Blacklist files created/copied successfully!\n"));
3427 return num_connections;
3433 * Iterator for choosing random peers to connect.
3435 * @param cls closure, a RandomContext
3436 * @param key the key the second Daemon was stored under
3437 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3439 * @return GNUNET_YES to continue iteration
3442 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3444 struct RandomContext *random_ctx = cls;
3445 double random_number;
3446 uint32_t second_pos;
3447 GNUNET_HashCode first_hash;
3450 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3451 UINT64_MAX)) / ((double) UINT64_MAX);
3452 if (random_number < random_ctx->percentage)
3454 GNUNET_assert (GNUNET_OK ==
3455 GNUNET_CONTAINER_multihashmap_put (random_ctx->
3456 first->connect_peers_working_set,
3458 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3461 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
3462 uid_from_hash (key, &second_pos);
3463 hash_from_uid (random_ctx->first_uid, &first_hash);
3464 GNUNET_assert (random_ctx->pg->total > second_pos);
3465 GNUNET_assert (GNUNET_YES ==
3466 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
3468 [second_pos].connect_peers,
3478 * Iterator for adding at least X peers to a peers connection set.
3480 * @param cls closure, MinimumContext
3481 * @param key the key the second Daemon was stored under
3482 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3484 * @return GNUNET_YES to continue iteration
3487 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3489 struct MinimumContext *min_ctx = cls;
3490 uint32_t second_pos;
3491 GNUNET_HashCode first_hash;
3494 if (GNUNET_CONTAINER_multihashmap_size
3495 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
3497 for (i = 0; i < min_ctx->num_to_add; i++)
3499 if (min_ctx->pg_array[i] == min_ctx->current)
3501 GNUNET_assert (GNUNET_OK ==
3502 GNUNET_CONTAINER_multihashmap_put
3503 (min_ctx->first->connect_peers_working_set, key,
3505 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3506 uid_from_hash (key, &second_pos);
3507 hash_from_uid (min_ctx->first_uid, &first_hash);
3508 GNUNET_assert (min_ctx->pg->total > second_pos);
3509 GNUNET_assert (GNUNET_OK ==
3510 GNUNET_CONTAINER_multihashmap_put (min_ctx->
3512 [second_pos].connect_peers_working_set,
3516 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3517 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
3518 GNUNET_assert (GNUNET_YES ==
3519 GNUNET_CONTAINER_multihashmap_remove
3520 (min_ctx->pg->peers[second_pos].connect_peers,
3521 &first_hash, min_ctx->first->daemon));
3528 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
3534 * Iterator for adding peers to a connection set based on a depth first search.
3536 * @param cls closure, MinimumContext
3537 * @param key the key the second daemon was stored under
3538 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3540 * @return GNUNET_YES to continue iteration
3543 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3545 struct DFSContext *dfs_ctx = cls;
3546 GNUNET_HashCode first_hash;
3548 if (dfs_ctx->current == dfs_ctx->chosen)
3550 GNUNET_assert (GNUNET_OK ==
3551 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3552 first->connect_peers_working_set,
3554 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3555 uid_from_hash (key, &dfs_ctx->second_uid);
3556 hash_from_uid (dfs_ctx->first_uid, &first_hash);
3557 GNUNET_assert (GNUNET_OK ==
3558 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
3560 [dfs_ctx->second_uid].connect_peers_working_set,
3564 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3565 GNUNET_assert (GNUNET_YES ==
3566 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
3568 [dfs_ctx->second_uid].connect_peers,
3572 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
3573 return GNUNET_NO; /* We have found our peer, don't iterate more */
3582 * From the set of connections possible, choose percentage percent of connections
3583 * to actually connect.
3585 * @param pg the peergroup we are dealing with
3586 * @param percentage what percent of total connections to make
3589 choose_random_connections (struct GNUNET_TESTING_PeerGroup *pg,
3592 struct RandomContext random_ctx;
3595 struct PeerConnection *temp_peers;
3596 struct PeerConnection *conn_iter;
3597 double random_number;
3600 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3602 random_ctx.first_uid = pg_iter;
3603 random_ctx.first = &pg->peers[pg_iter];
3604 random_ctx.percentage = percentage;
3608 conn_iter = pg->peers[pg_iter].connect_peers_head;
3609 while (conn_iter != NULL)
3613 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3614 UINT64_MAX)) / ((double) UINT64_MAX);
3615 if (random_number < percentage)
3617 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
3619 conn_iter = conn_iter->next;
3622 pg->peers[pg_iter].connect_peers_working_set =
3623 GNUNET_CONTAINER_multihashmap_create (pg->total);
3624 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3625 &random_connect_iterator,
3627 /* Now remove the old connections */
3628 GNUNET_CONTAINER_multihashmap_destroy (pg->
3629 peers[pg_iter].connect_peers);
3630 /* And replace with the random set */
3631 pg->peers[pg_iter].connect_peers =
3632 pg->peers[pg_iter].connect_peers_working_set;
3636 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3638 conn_iter = pg->peers[pg_iter].connect_peers_head;
3639 while (pg->peers[pg_iter].connect_peers_head != NULL)
3640 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
3642 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
3643 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
3644 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
3645 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
3651 * Count the number of connections in a linked list of connections.
3653 * @param conn_list the connection list to get the count of
3655 * @return the number of elements in the list
3658 count_connections (struct PeerConnection *conn_list)
3660 struct PeerConnection *iter;
3664 while (iter != NULL)
3674 count_workingset_connections (struct GNUNET_TESTING_PeerGroup *pg)
3677 unsigned int pg_iter;
3679 struct PeerConnection *conn_iter;
3683 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3686 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
3687 while (conn_iter != NULL)
3690 conn_iter = conn_iter->next;
3694 GNUNET_CONTAINER_multihashmap_size (pg->
3696 [pg_iter].connect_peers_working_set);
3705 count_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg)
3708 unsigned int pg_iter;
3710 struct PeerConnection *conn_iter;
3714 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3717 conn_iter = pg->peers[pg_iter].allowed_peers_head;
3718 while (conn_iter != NULL)
3721 conn_iter = conn_iter->next;
3725 GNUNET_CONTAINER_multihashmap_size (pg->
3727 [pg_iter].allowed_peers);
3736 * From the set of connections possible, choose at least num connections per
3739 * @param pg the peergroup we are dealing with
3740 * @param num how many connections at least should each peer have (if possible)?
3743 choose_minimum (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3746 struct MinimumContext minimum_ctx;
3748 struct PeerConnection *conn_iter;
3749 unsigned int temp_list_size;
3752 uint32_t random; /* Random list entry to connect peer to */
3757 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3759 temp_list_size = count_connections(pg->peers[pg_iter].connect_peers_head);
3760 if (temp_list_size == 0)
3762 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer %d has 0 connections!?!?\n", pg_iter);
3765 for (i = 0; i < num; i++)
3767 random = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, temp_list_size);
3768 conn_iter = pg->peers[pg_iter].connect_peers_head;
3769 for (count = 0; count < random; count++)
3770 conn_iter = conn_iter->next;
3771 /* We now have a random connection, connect it! */
3772 GNUNET_assert(conn_iter != NULL);
3773 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
3777 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3779 pg->peers[pg_iter].connect_peers_working_set =
3780 GNUNET_CONTAINER_multihashmap_create (num);
3783 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3785 minimum_ctx.first_uid = pg_iter;
3786 minimum_ctx.pg_array =
3787 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
3788 GNUNET_CONTAINER_multihashmap_size
3789 (pg->peers[pg_iter].connect_peers));
3790 minimum_ctx.first = &pg->peers[pg_iter];
3791 minimum_ctx.pg = pg;
3792 minimum_ctx.num_to_add = num;
3793 minimum_ctx.current = 0;
3794 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
3795 &minimum_connect_iterator,
3799 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3801 /* Remove the "old" connections */
3802 GNUNET_CONTAINER_multihashmap_destroy (pg->
3803 peers[pg_iter].connect_peers);
3804 /* And replace with the working set */
3805 pg->peers[pg_iter].connect_peers =
3806 pg->peers[pg_iter].connect_peers_working_set;
3809 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3811 while (pg->peers[pg_iter].connect_peers_head != NULL)
3812 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
3814 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
3815 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
3816 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
3817 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
3822 struct FindClosestContext
3825 * The currently known closest peer.
3827 struct GNUNET_TESTING_Daemon *closest;
3830 * The info for the peer we are adding connections for.
3832 struct PeerData *curr_peer;
3835 * The distance (bits) between the current
3836 * peer and the currently known closest.
3838 unsigned int closest_dist;
3841 * The offset of the closest known peer in
3844 unsigned int closest_num;
3848 * Iterator over hash map entries of the allowed
3849 * peer connections. Find the closest, not already
3850 * connected peer and return it.
3852 * @param cls closure (struct FindClosestContext)
3853 * @param key current key code (hash of offset in pg)
3854 * @param value value in the hash map - a GNUNET_TESTING_Daemon
3855 * @return GNUNET_YES if we should continue to
3860 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
3862 struct FindClosestContext *closest_ctx = cls;
3863 struct GNUNET_TESTING_Daemon *daemon = value;
3865 if (((closest_ctx->closest == NULL) ||
3866 (GNUNET_CRYPTO_hash_matching_bits
3867 (&daemon->id.hashPubKey,
3868 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
3869 closest_ctx->closest_dist))
3871 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
3872 curr_peer->connect_peers,
3875 closest_ctx->closest_dist =
3876 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
3877 &closest_ctx->curr_peer->daemon->
3879 closest_ctx->closest = daemon;
3880 uid_from_hash (key, &closest_ctx->closest_num);
3887 * From the set of connections possible, choose at num connections per
3888 * peer based on depth which are closest out of those allowed. Guaranteed
3889 * to add num peers to connect to, provided there are that many peers
3890 * in the underlay topology to connect to.
3892 * @param pg the peergroup we are dealing with
3893 * @param num how many connections at least should each peer have (if possible)?
3894 * @param proc processor to actually add the connections
3895 * @param list the peer list to use
3898 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
3899 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
3904 struct FindClosestContext closest_ctx;
3909 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
3911 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3913 closest_ctx.curr_peer = &pg->peers[pg_iter];
3914 closest_ctx.closest = NULL;
3915 closest_ctx.closest_dist = 0;
3916 closest_ctx.closest_num = 0;
3917 GNUNET_CONTAINER_multihashmap_iterate (pg->
3918 peers[pg_iter].allowed_peers,
3919 &find_closest_peers,
3921 if (closest_ctx.closest != NULL)
3923 GNUNET_assert (closest_ctx.closest_num < pg->total);
3924 proc (pg, pg_iter, closest_ctx.closest_num, list);
3932 * From the set of connections possible, choose at least num connections per
3933 * peer based on depth first traversal of peer connections. If DFS leaves
3934 * peers unconnected, ensure those peers get connections.
3936 * @param pg the peergroup we are dealing with
3937 * @param num how many connections at least should each peer have (if possible)?
3940 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3944 uint32_t starting_peer;
3945 uint32_t least_connections;
3946 uint32_t random_connection;
3948 unsigned int temp_count;
3949 struct PeerConnection *peer_iter;
3951 struct DFSContext dfs_ctx;
3952 GNUNET_HashCode second_hash;
3958 while ((count_workingset_connections (pg) < num * pg->total)
3959 && (count_allowed_connections (pg) > 0))
3961 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3963 least_connections = -1; /* Set to very high number */
3964 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3966 temp_count = count_connections(pg->peers[pg_iter].connect_peers_working_set_head);
3967 if (temp_count < least_connections)
3969 starting_peer = pg_iter;
3970 least_connections = temp_count;
3975 temp_count = count_connections(pg->peers[starting_peer].connect_peers_head);
3976 if (temp_count == 0)
3977 continue; /* FIXME: infinite loop? */
3979 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, temp_count);
3981 peer_iter = pg->peers[starting_peer].connect_peers_head;
3982 while (temp_count < random_connection)
3984 peer_iter = peer_iter->next;
3987 GNUNET_assert(peer_iter != NULL);
3988 add_connections(pg, starting_peer, peer_iter->index, WORKING_SET);
3989 remove_connections(pg, starting_peer, peer_iter->index, CONNECT);
3990 starting_peer = peer_iter->index;
3995 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3997 pg->peers[pg_iter].connect_peers_working_set =
3998 GNUNET_CONTAINER_multihashmap_create (num);
4003 while ((count_workingset_connections (pg) < num * pg->total)
4004 && (count_allowed_connections (pg) > 0))
4006 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4008 least_connections = -1; /* Set to very high number */
4009 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4011 if (GNUNET_CONTAINER_multihashmap_size
4012 (pg->peers[pg_iter].connect_peers_working_set) <
4015 starting_peer = pg_iter;
4017 GNUNET_CONTAINER_multihashmap_size (pg->
4019 [pg_iter].connect_peers_working_set);
4024 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4030 /* Choose a random peer from the chosen peers set of connections to add */
4032 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4033 GNUNET_CONTAINER_multihashmap_size
4034 (pg->peers[starting_peer].connect_peers));
4035 dfs_ctx.first_uid = starting_peer;
4036 dfs_ctx.first = &pg->peers[starting_peer];
4038 dfs_ctx.current = 0;
4040 GNUNET_CONTAINER_multihashmap_iterate (pg->
4042 [starting_peer].connect_peers,
4043 &dfs_connect_iterator, &dfs_ctx);
4044 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4045 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4046 GNUNET_assert (GNUNET_YES ==
4047 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4048 [starting_peer].connect_peers,
4052 [dfs_ctx.second_uid].daemon));
4053 starting_peer = dfs_ctx.second_uid;
4056 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4058 /* Remove the "old" connections */
4059 GNUNET_CONTAINER_multihashmap_destroy (pg->
4060 peers[pg_iter].connect_peers);
4061 /* And replace with the working set */
4062 pg->peers[pg_iter].connect_peers =
4063 pg->peers[pg_iter].connect_peers_working_set;
4070 * Internal callback for topology information for a particular peer.
4073 internal_topology_callback (void *cls,
4074 const struct GNUNET_PeerIdentity *peer,
4075 const struct GNUNET_TRANSPORT_ATS_Information
4078 struct CoreContext *core_ctx = cls;
4079 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4081 if (peer == NULL) /* Either finished, or something went wrong */
4083 iter_ctx->completed++;
4084 iter_ctx->connected--;
4085 /* One core context allocated per iteration, must free! */
4086 GNUNET_free (core_ctx);
4090 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id,
4094 if (iter_ctx->completed == iter_ctx->total)
4096 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4097 /* Once all are done, free the iteration context */
4098 GNUNET_free (iter_ctx);
4104 * Check running topology iteration tasks, if below max start a new one, otherwise
4105 * schedule for some time in the future.
4108 schedule_get_topology (void *cls,
4109 const struct GNUNET_SCHEDULER_TaskContext *tc)
4111 struct CoreContext *core_context = cls;
4112 struct TopologyIterateContext *topology_context =
4113 (struct TopologyIterateContext *) core_context->iter_context;
4114 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4117 if (topology_context->connected > topology_context->pg->max_outstanding_connections)
4119 #if VERBOSE_TESTING > 2
4120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4122 ("Delaying connect, we have too many outstanding connections!\n"));
4124 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4125 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4126 &schedule_get_topology, core_context);
4130 #if VERBOSE_TESTING > 2
4131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4132 _("Creating connection, outstanding_connections is %d\n"),
4133 outstanding_connects);
4135 topology_context->connected++;
4138 GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4139 &internal_topology_callback,
4142 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4143 internal_topology_callback (core_context, NULL, NULL);
4149 * Iterate over all (running) peers in the peer group, retrieve
4150 * all connections that each currently has.
4153 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
4154 GNUNET_TESTING_NotifyTopology cb, void *cls)
4156 struct TopologyIterateContext *topology_context;
4157 struct CoreContext *core_ctx;
4159 unsigned int total_count;
4161 /* Allocate a single topology iteration context */
4162 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4163 topology_context->topology_cb = cb;
4164 topology_context->cls = cls;
4165 topology_context->pg = pg;
4167 for (i = 0; i < pg->total; i++)
4169 if (pg->peers[i].daemon->running == GNUNET_YES)
4171 /* Allocate one core context per core we need to connect to */
4172 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4173 core_ctx->daemon = pg->peers[i].daemon;
4174 /* Set back pointer to topology iteration context */
4175 core_ctx->iter_context = topology_context;
4176 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4180 if (total_count == 0)
4182 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4183 GNUNET_free (topology_context);
4186 topology_context->total = total_count;
4191 * Callback function to process statistic values.
4192 * This handler is here only really to insert a peer
4193 * identity (or daemon) so the statistics can be uniquely
4194 * tied to a single running peer.
4196 * @param cls closure
4197 * @param subsystem name of subsystem that created the statistic
4198 * @param name the name of the datum
4199 * @param value the current value
4200 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4201 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4204 internal_stats_callback (void *cls,
4205 const char *subsystem,
4206 const char *name, uint64_t value, int is_persistent)
4208 struct StatsCoreContext *core_context = cls;
4209 struct StatsIterateContext *stats_context =
4210 (struct StatsIterateContext *) core_context->iter_context;
4212 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4213 subsystem, name, value, is_persistent);
4217 * Internal continuation call for statistics iteration.
4219 * @param cls closure, the CoreContext for this iteration
4220 * @param success whether or not the statistics iterations
4221 * was canceled or not (we don't care)
4224 internal_stats_cont (void *cls, int success)
4226 struct StatsCoreContext *core_context = cls;
4227 struct StatsIterateContext *stats_context =
4228 (struct StatsIterateContext *) core_context->iter_context;
4230 stats_context->connected--;
4231 stats_context->completed++;
4233 if (stats_context->completed == stats_context->total)
4235 stats_context->cont (stats_context->cls, GNUNET_YES);
4236 GNUNET_free (stats_context);
4239 if (core_context->stats_handle != NULL)
4240 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4242 GNUNET_free (core_context);
4246 * Check running topology iteration tasks, if below max start a new one, otherwise
4247 * schedule for some time in the future.
4250 schedule_get_statistics (void *cls,
4251 const struct GNUNET_SCHEDULER_TaskContext *tc)
4253 struct StatsCoreContext *core_context = cls;
4254 struct StatsIterateContext *stats_context =
4255 (struct StatsIterateContext *) core_context->iter_context;
4257 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4260 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
4262 #if VERBOSE_TESTING > 2
4263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4265 ("Delaying connect, we have too many outstanding connections!\n"));
4267 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4268 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4269 &schedule_get_statistics, core_context);
4273 #if VERBOSE_TESTING > 2
4274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4275 _("Creating connection, outstanding_connections is %d\n"),
4276 outstanding_connects);
4279 stats_context->connected++;
4280 core_context->stats_handle =
4281 GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
4282 if (core_context->stats_handle == NULL)
4284 internal_stats_cont (core_context, GNUNET_NO);
4288 core_context->stats_get_handle =
4289 GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
4290 GNUNET_TIME_relative_get_forever (),
4291 &internal_stats_cont, &internal_stats_callback,
4293 if (core_context->stats_get_handle == NULL)
4294 internal_stats_cont (core_context, GNUNET_NO);
4299 struct DuplicateStats
4302 * Next item in the list
4304 struct DuplicateStats *next;
4307 * Nasty string, concatenation of relevant information.
4309 char *unique_string;
4313 * Check whether the combination of port/host/unix domain socket
4314 * already exists in the list of peers being checked for statistics.
4316 * @param pg the peergroup in question
4317 * @param specific_peer the peer we're concerned with
4318 * @param stats_list the list to return to the caller
4320 * @return GNUNET_YES if the statistics instance has been seen already,
4321 * GNUNET_NO if not (and we may have added it to the list)
4324 stats_check_existing (struct GNUNET_TESTING_PeerGroup *pg,
4325 struct PeerData *specific_peer,
4326 struct DuplicateStats **stats_list)
4328 struct DuplicateStats *pos;
4329 char *unix_domain_socket;
4330 unsigned long long port;
4333 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
4334 "single_statistics_per_host"))
4335 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
4339 GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg, "statistics",
4340 "unixpath", &unix_domain_socket))
4344 GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg, "statistics",
4347 GNUNET_free(unix_domain_socket);
4351 if (specific_peer->daemon->hostname != NULL)
4352 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
4353 unix_domain_socket, port);
4355 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
4359 if (0 == strcmp (to_match, pos->unique_string))
4361 GNUNET_free (unix_domain_socket);
4362 GNUNET_free (to_match);
4367 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
4368 pos->unique_string = to_match;
4369 pos->next = *stats_list;
4371 GNUNET_free (unix_domain_socket);
4376 * Iterate over all (running) peers in the peer group, retrieve
4377 * all statistics from each.
4380 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
4381 GNUNET_STATISTICS_Callback cont,
4382 GNUNET_TESTING_STATISTICS_Iterator proc,
4385 struct StatsIterateContext *stats_context;
4386 struct StatsCoreContext *core_ctx;
4388 unsigned int total_count;
4389 struct DuplicateStats *stats_list;
4390 struct DuplicateStats *pos;
4393 /* Allocate a single stats iteration context */
4394 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
4395 stats_context->cont = cont;
4396 stats_context->proc = proc;
4397 stats_context->cls = cls;
4398 stats_context->pg = pg;
4401 for (i = 0; i < pg->total; i++)
4403 if ((pg->peers[i].daemon->running == GNUNET_YES)
4405 stats_check_existing (pg, &pg->peers[i], &stats_list)))
4407 /* Allocate one core context per core we need to connect to */
4408 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
4409 core_ctx->daemon = pg->peers[i].daemon;
4410 /* Set back pointer to topology iteration context */
4411 core_ctx->iter_context = stats_context;
4412 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
4417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4418 "Retrieving stats from %u total instances.\n", total_count);
4419 stats_context->total = total_count;
4420 if (stats_list != NULL)
4425 GNUNET_free (pos->unique_string);
4426 stats_list = pos->next;
4428 pos = stats_list->next;
4435 * There are many ways to connect peers that are supported by this function.
4436 * To connect peers in the same topology that was created via the
4437 * GNUNET_TESTING_create_topology, the topology variable must be set to
4438 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
4439 * a new instance of that topology will be generated and attempted to be
4440 * connected. This could result in some connections being impossible,
4441 * because some topologies are non-deterministic.
4443 * @param pg the peer group struct representing the running peers
4444 * @param topology which topology to connect the peers in
4445 * @param options options for connecting the topology
4446 * @param option_modifier modifier for options that take a parameter
4447 * @param connect_timeout how long to wait before giving up on connecting
4449 * @param connect_attempts how many times to attempt to connect two peers
4450 * over the connect_timeout duration
4451 * @param notify_callback notification to be called once all connections completed
4452 * @param notify_cls closure for notification callback
4454 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
4457 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
4458 enum GNUNET_TESTING_Topology topology,
4459 enum GNUNET_TESTING_TopologyOption options,
4460 double option_modifier,
4461 struct GNUNET_TIME_Relative connect_timeout,
4462 unsigned int connect_attempts,
4463 GNUNET_TESTING_NotifyCompletion
4464 notify_callback, void *notify_cls)
4468 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4469 #if VERBOSE_TOPOLOGY
4470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4471 _("Creating clique CONNECT topology\n"));
4473 create_clique (pg, &add_connections, CONNECT);
4475 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4476 #if VERBOSE_TOPOLOGY
4477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4478 _("Creating small world (ring) CONNECT topology\n"));
4480 create_small_world_ring (pg, &add_connections, CONNECT);
4482 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4483 #if VERBOSE_TOPOLOGY
4484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4485 _("Creating small world (2d-torus) CONNECT topology\n"));
4487 create_small_world (pg, &add_connections, CONNECT);
4489 case GNUNET_TESTING_TOPOLOGY_RING:
4490 #if VERBOSE_TOPOLOGY
4491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4492 _("Creating ring CONNECT topology\n"));
4494 create_ring (pg, &add_connections, CONNECT);
4496 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4497 #if VERBOSE_TOPOLOGY
4498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4499 _("Creating 2d torus CONNECT topology\n"));
4501 create_2d_torus (pg, &add_connections, CONNECT);
4503 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4504 #if VERBOSE_TOPOLOGY
4505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4506 _("Creating Erdos-Renyi CONNECT topology\n"));
4508 create_erdos_renyi (pg, &add_connections, CONNECT);
4510 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4511 #if VERBOSE_TOPOLOGY
4512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4513 _("Creating InterNAT CONNECT topology\n"));
4515 create_nated_internet (pg, &add_connections, CONNECT);
4517 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4518 #if VERBOSE_TOPOLOGY
4519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4520 _("Creating Scale Free CONNECT topology\n"));
4522 create_scale_free (pg, &add_connections, CONNECT);
4524 case GNUNET_TESTING_TOPOLOGY_LINE:
4525 #if VERBOSE_TOPOLOGY
4526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4527 _("Creating straight line CONNECT topology\n"));
4529 create_line (pg, &add_connections, CONNECT);
4531 case GNUNET_TESTING_TOPOLOGY_NONE:
4532 #if VERBOSE_TOPOLOGY
4533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4534 _("Creating no CONNECT topology\n"));
4536 copy_allowed_topology (pg);
4539 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4541 ("Unknown topology specification, can't connect peers!\n"));
4542 return GNUNET_SYSERR;
4547 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
4548 #if VERBOSE_TOPOLOGY
4549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4551 ("Connecting random subset (%'.2f percent) of possible peers\n"),
4552 100 * option_modifier);
4554 choose_random_connections (pg, option_modifier);
4556 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
4557 #if VERBOSE_TOPOLOGY
4558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4559 _("Connecting a minimum of %u peers each (if possible)\n"),
4560 (unsigned int) option_modifier);
4562 choose_minimum (pg, (unsigned int) option_modifier);
4564 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
4565 #if VERBOSE_TOPOLOGY
4566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4568 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
4569 (unsigned int) option_modifier);
4572 perform_dfs (pg, (int) option_modifier);
4575 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
4576 #if VERBOSE_TOPOLOGY
4577 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4579 ("Finding additional %u closest peers each (if possible)\n"),
4580 (unsigned int) option_modifier);
4583 add_closest (pg, (unsigned int) option_modifier,
4584 &add_connections, CONNECT);
4587 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
4589 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
4595 return connect_topology (pg, connect_timeout, connect_attempts, notify_callback, notify_cls);
4599 * Lookup and return the number of SSH connections to a host.
4601 * @param hostname the hostname to lookup in the list
4602 * @param pg the peergroup that the host belongs to
4604 * @return the number of current ssh connections to the host
4607 count_outstanding_at_host(const char *hostname, struct GNUNET_TESTING_PeerGroup *pg)
4609 struct OutstandingSSH *pos;
4611 while ((pos != NULL) && (strcmp(pos->hostname, hostname) != 0))
4613 GNUNET_assert(pos != NULL);
4614 return pos->outstanding;
4619 * Increment the number of SSH connections to a host by one.
4621 * @param hostname the hostname to lookup in the list
4622 * @param pg the peergroup that the host belongs to
4626 increment_outstanding_at_host(const char *hostname, struct GNUNET_TESTING_PeerGroup *pg)
4628 struct OutstandingSSH *pos;
4630 while ((pos != NULL) && (strcmp(pos->hostname, hostname) != 0))
4632 GNUNET_assert(pos != NULL);
4637 * Decrement the number of SSH connections to a host by one.
4639 * @param hostname the hostname to lookup in the list
4640 * @param pg the peergroup that the host belongs to
4644 decrement_outstanding_at_host(const char *hostname, struct GNUNET_TESTING_PeerGroup *pg)
4646 struct OutstandingSSH *pos;
4648 while ((pos != NULL) && (strcmp(pos->hostname, hostname) != 0))
4650 GNUNET_assert(pos != NULL);
4656 * Callback that is called whenever a hostkey is generated
4657 * for a peer. Call the real callback and decrement the
4658 * starting counter for the peergroup.
4660 * @param cls closure
4661 * @param id identifier for the daemon, NULL on error
4662 * @param d handle for the daemon
4663 * @param emsg error message (NULL on success)
4666 internal_hostkey_callback (void *cls,
4667 const struct GNUNET_PeerIdentity *id,
4668 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4670 struct InternalStartContext *internal_context = cls;
4671 internal_context->peer->pg->starting--;
4672 internal_context->peer->pg->started++;
4673 if (internal_context->hostname != NULL)
4674 decrement_outstanding_at_host(internal_context->hostname, internal_context->peer->pg);
4675 if (internal_context->hostkey_callback != NULL)
4676 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
4678 else if (internal_context->peer->pg->started ==
4679 internal_context->peer->pg->total)
4681 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
4682 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
4687 * Callback that is called whenever a peer has finished starting.
4688 * Call the real callback and decrement the starting counter
4689 * for the peergroup.
4691 * @param cls closure
4692 * @param id identifier for the daemon, NULL on error
4694 * @param d handle for the daemon
4695 * @param emsg error message (NULL on success)
4698 internal_startup_callback (void *cls,
4699 const struct GNUNET_PeerIdentity *id,
4700 const struct GNUNET_CONFIGURATION_Handle *cfg,
4701 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4703 struct InternalStartContext *internal_context = cls;
4704 internal_context->peer->pg->starting--;
4705 if (internal_context->start_cb != NULL)
4706 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
4711 internal_continue_startup (void *cls,
4712 const struct GNUNET_SCHEDULER_TaskContext *tc)
4714 struct InternalStartContext *internal_context = cls;
4716 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4721 if ((internal_context->peer->pg->starting < internal_context->peer->pg->max_concurrent_ssh) ||
4722 ((internal_context->hostname != NULL) &&
4723 (count_outstanding_at_host(internal_context->hostname, internal_context->peer->pg) < internal_context->peer->pg->max_concurrent_ssh)))
4725 if (internal_context->hostname != NULL)
4726 increment_outstanding_at_host(internal_context->hostname, internal_context->peer->pg);
4727 internal_context->peer->pg->starting++;
4728 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
4732 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4733 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4734 &internal_continue_startup,
4741 * Callback for informing us about a successful
4742 * or unsuccessful churn start call.
4744 * @param cls a ChurnContext
4745 * @param id the peer identity of the started peer
4746 * @param cfg the handle to the configuration of the peer
4747 * @param d handle to the daemon for the peer
4748 * @param emsg NULL on success, non-NULL on failure
4752 churn_start_callback (void *cls,
4753 const struct GNUNET_PeerIdentity *id,
4754 const struct GNUNET_CONFIGURATION_Handle *cfg,
4755 struct GNUNET_TESTING_Daemon *d, const char *emsg)
4757 struct ChurnRestartContext *startup_ctx = cls;
4758 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
4760 unsigned int total_left;
4761 char *error_message;
4763 error_message = NULL;
4766 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4767 "Churn stop callback failed with error `%s'\n", emsg);
4768 churn_ctx->num_failed_start++;
4772 churn_ctx->num_to_start--;
4776 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4777 "Started peer, %d left.\n", churn_ctx->num_to_start);
4781 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
4782 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4784 if (total_left == 0)
4786 if ((churn_ctx->num_failed_stop > 0)
4787 || (churn_ctx->num_failed_start > 0))
4788 GNUNET_asprintf (&error_message,
4789 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4790 churn_ctx->num_failed_start,
4791 churn_ctx->num_failed_stop);
4792 churn_ctx->cb (churn_ctx->cb_cls, error_message);
4793 GNUNET_free_non_null (error_message);
4794 GNUNET_free (churn_ctx);
4795 GNUNET_free (startup_ctx);
4801 schedule_churn_restart (void *cls,
4802 const struct GNUNET_SCHEDULER_TaskContext *tc)
4804 struct PeerRestartContext *peer_restart_ctx = cls;
4805 struct ChurnRestartContext *startup_ctx =
4806 peer_restart_ctx->churn_restart_ctx;
4808 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
4809 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4810 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4811 &schedule_churn_restart, peer_restart_ctx);
4814 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
4815 startup_ctx->timeout,
4816 &churn_start_callback,
4818 GNUNET_free (peer_restart_ctx);
4825 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4827 struct InternalStartContext *internal_context = cls;
4829 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4834 if ((internal_context->peer->pg->starting < internal_context->peer->pg->max_concurrent_ssh) ||
4835 ((internal_context->hostname != NULL) &&
4836 (count_outstanding_at_host(internal_context->hostname, internal_context->peer->pg) < internal_context->peer->pg->max_concurrent_ssh)))
4838 if (internal_context->hostname != NULL)
4839 increment_outstanding_at_host(internal_context->hostname, internal_context->peer->pg);
4840 internal_context->peer->pg->starting++;
4841 internal_context->peer->daemon =
4842 GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
4843 internal_context->timeout,
4844 internal_context->hostname,
4845 internal_context->username,
4846 internal_context->sshport,
4847 internal_context->hostkey,
4848 &internal_hostkey_callback,
4850 &internal_startup_callback,
4855 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4856 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4857 &internal_start, internal_context);
4862 * Function which continues a peer group starting up
4863 * after successfully generating hostkeys for each peer.
4865 * @param pg the peer group to continue starting
4869 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg)
4874 for (i = 0; i < pg->total; i++)
4876 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
4877 &pg->peers[i].internal_context);
4878 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
4883 * Start count gnunet instances with the same set of transports and
4884 * applications. The port numbers (any option called "PORT") will be
4885 * adjusted to ensure that no two peers running on the same system
4886 * have the same port(s) in their respective configurations.
4888 * @param cfg configuration template to use
4889 * @param total number of daemons to start
4890 * @param max_concurrent_connections for testing, how many peers can
4891 * we connect to simultaneously
4892 * @param max_concurrent_ssh when starting with ssh, how many ssh
4893 * connections will we allow at once (based on remote hosts allowed!)
4894 * @param timeout total time allowed for peers to start
4895 * @param hostkey_callback function to call on each peers hostkey generation
4896 * if NULL, peers will be started by this call, if non-null,
4897 * GNUNET_TESTING_daemons_continue_startup must be called after
4898 * successful hostkey generation
4899 * @param hostkey_cls closure for hostkey callback
4900 * @param cb function to call on each daemon that was started
4901 * @param cb_cls closure for cb
4902 * @param connect_callback function to call each time two hosts are connected
4903 * @param connect_callback_cls closure for connect_callback
4904 * @param hostnames linked list of host structs to use to start peers on
4905 * (NULL to run on localhost only)
4907 * @return NULL on error, otherwise handle to control peer group
4909 struct GNUNET_TESTING_PeerGroup *
4910 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
4912 unsigned int max_concurrent_connections,
4913 unsigned int max_concurrent_ssh,
4914 struct GNUNET_TIME_Relative timeout,
4915 GNUNET_TESTING_NotifyHostkeyCreated
4916 hostkey_callback, void *hostkey_cls,
4917 GNUNET_TESTING_NotifyDaemonRunning cb,
4919 GNUNET_TESTING_NotifyConnection
4920 connect_callback, void *connect_callback_cls,
4921 const struct GNUNET_TESTING_Host *hostnames)
4923 struct GNUNET_TESTING_PeerGroup *pg;
4924 const struct GNUNET_TESTING_Host *hostpos;
4930 const char *hostname;
4931 const char *username;
4932 char *baseservicehome;
4933 char *newservicehome;
4935 char *hostkeys_file;
4938 struct GNUNET_DISK_FileHandle *fd;
4939 struct GNUNET_CONFIGURATION_Handle *pcfg;
4941 unsigned int hostcnt;
4948 uint64_t total_hostkeys;
4949 struct GNUNET_OS_Process *proc;
4959 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
4961 pg->notify_connection = connect_callback;
4962 pg->notify_connection_cls = connect_callback_cls;
4964 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
4965 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
4966 pg->max_outstanding_connections = max_concurrent_connections;
4967 pg->max_concurrent_ssh = max_concurrent_ssh;
4968 if (NULL != hostnames)
4971 hostpos = hostnames;
4972 while (hostpos != NULL)
4974 hostpos = hostpos->next;
4977 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
4980 hostpos = hostnames;
4981 while (hostpos != NULL)
4983 pg->hosts[off].minport = LOW_PORT;
4984 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
4985 if (hostpos->username != NULL)
4986 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
4987 pg->hosts[off].sshport = hostpos->port;
4988 hostpos = hostpos->next;
4998 pg->num_hosts = off;
5002 /* skip leading spaces */
5003 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
5006 while ('\0' != *rpos)
5008 if (isspace ((unsigned char) *rpos))
5012 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5014 start = GNUNET_strdup (hostnames);
5016 while ('\0' != *pos)
5018 if (isspace ((unsigned char) *pos))
5021 if (strlen (start) > 0)
5023 pg->hosts[off].minport = LOW_PORT;
5024 pg->hosts[off++].hostname = start;
5030 if (strlen (start) > 0)
5032 pg->hosts[off].minport = LOW_PORT;
5033 pg->hosts[off++].hostname = start;
5037 GNUNET_free (start);
5038 GNUNET_free (pg->hosts);
5042 minport = 0; /* make gcc happy */
5051 /* Create the servicehome directory for each remote peer */
5052 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
5054 for (i = 0; i < pg->num_hosts; i++)
5056 struct OutstandingSSH *ssh_entry;
5057 ssh_entry = GNUNET_malloc(sizeof(struct OutstandingSSH));
5058 ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
5059 GNUNET_CONTAINER_DLL_insert(pg->ssh_head, pg->ssh_tail, ssh_entry);
5060 if (NULL != pg->hosts[i].username)
5061 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username, pg->hosts[i].hostname);
5063 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
5064 if (pg->hosts[i].sshport != 0)
5066 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
5067 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
5068 "ssh", "-P", ssh_port_str,
5072 arg, "mkdir -p", baseservicehome, NULL);
5075 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
5076 "ssh", arg, "mkdir -p", baseservicehome, NULL);
5077 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating remote dir with command ssh %s %s %s\n", arg, " mkdir -p ", baseservicehome);
5078 GNUNET_OS_process_wait(proc);
5080 GNUNET_free(baseservicehome);
5082 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "HOSTKEYSFILE",
5085 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
5086 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!\n");
5089 /* Check hostkey file size, read entire thing into memory */
5090 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
5091 GNUNET_DISK_PERM_NONE);
5094 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
5098 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
5101 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Found file size %llu for hostkeys, expect hostkeys to be size %d\n", fs, HOSTKEYFILESIZE);
5103 if (fs % HOSTKEYFILESIZE != 0)
5105 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "File size %llu seems incorrect for hostkeys...\n", fs);
5109 total_hostkeys = fs / HOSTKEYFILESIZE;
5110 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Will read %llu hostkeys from file\n", total_hostkeys);
5111 pg->hostkey_data = GNUNET_malloc_large (fs);
5112 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
5113 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
5116 GNUNET_free(hostkeys_file);
5119 for (off = 0; off < total; off++)
5123 hostname = pg->hosts[off % hostcnt].hostname;
5124 username = pg->hosts[off % hostcnt].username;
5125 sshport = pg->hosts[off % hostcnt].sshport;
5126 pcfg = make_config (cfg,
5128 &pg->hosts[off % hostcnt].minport,
5129 &upnum, hostname, &fdnum);
5136 pcfg = make_config (cfg, off, &minport, &upnum, hostname, &fdnum);
5141 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5143 ("Could not create configuration for peer number %u on `%s'!\n"),
5144 off, hostname == NULL ? "localhost" : hostname);
5149 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
5152 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
5153 GNUNET_free (baseservicehome);
5157 tmpdir = getenv ("TMPDIR");
5158 tmpdir = tmpdir ? tmpdir : "/tmp";
5159 GNUNET_asprintf (&newservicehome,
5161 tmpdir, "gnunet-testing-test-test", off);
5163 GNUNET_CONFIGURATION_set_value_string (pcfg,
5165 "SERVICEHOME", newservicehome);
5166 GNUNET_free (newservicehome);
5167 pg->peers[off].cfg = pcfg;
5169 /* Can we do this later? */
5170 pg->peers[off].allowed_peers =
5171 GNUNET_CONTAINER_multihashmap_create (total);
5172 pg->peers[off].connect_peers =
5173 GNUNET_CONTAINER_multihashmap_create (total);
5174 pg->peers[off].blacklisted_peers =
5175 GNUNET_CONTAINER_multihashmap_create (total);
5178 pg->peers[off].pg = pg;
5179 pg->peers[off].internal_context.peer = &pg->peers[off];
5180 pg->peers[off].internal_context.timeout = timeout;
5181 pg->peers[off].internal_context.hostname = hostname;
5182 pg->peers[off].internal_context.username = username;
5183 pg->peers[off].internal_context.sshport = sshport;
5184 if (pg->hostkey_data != NULL)
5185 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off * HOSTKEYFILESIZE];
5186 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
5187 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
5188 pg->peers[off].internal_context.start_cb = cb;
5189 pg->peers[off].internal_context.start_cb_cls = cb_cls;
5191 GNUNET_SCHEDULER_add_now (&internal_start,
5192 &pg->peers[off].internal_context);
5199 * Get a daemon by number, so callers don't have to do nasty
5200 * offsetting operation.
5202 struct GNUNET_TESTING_Daemon *
5203 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
5204 unsigned int position)
5206 if (position < pg->total)
5207 return pg->peers[position].daemon;
5213 * Get a daemon by peer identity, so callers can
5214 * retrieve the daemon without knowing it's offset.
5216 * @param pg the peer group to retrieve the daemon from
5217 * @param peer_id the peer identity of the daemon to retrieve
5219 * @return the daemon on success, or NULL if no such peer identity is found
5221 struct GNUNET_TESTING_Daemon *
5222 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
5223 struct GNUNET_PeerIdentity *peer_id)
5227 for (i = 0; i < pg->total; i++)
5230 memcmp (&pg->peers[i].daemon->id, peer_id,
5231 sizeof (struct GNUNET_PeerIdentity)))
5232 return pg->peers[i].daemon;
5239 * Prototype of a function that will be called when a
5240 * particular operation was completed the testing library.
5242 * @param cls closure (a struct RestartContext)
5243 * @param id id of the peer that was restarted
5244 * @param cfg handle to the configuration of the peer
5245 * @param d handle to the daemon that was restarted
5246 * @param emsg NULL on success
5249 restart_callback (void *cls,
5250 const struct GNUNET_PeerIdentity *id,
5251 const struct GNUNET_CONFIGURATION_Handle *cfg,
5252 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5254 struct RestartContext *restart_context = cls;
5258 restart_context->peers_restarted++;
5262 restart_context->peers_restart_failed++;
5265 if (restart_context->peers_restarted == restart_context->peer_group->total)
5267 restart_context->callback (restart_context->callback_cls, NULL);
5268 GNUNET_free (restart_context);
5270 else if (restart_context->peers_restart_failed +
5271 restart_context->peers_restarted ==
5272 restart_context->peer_group->total)
5274 restart_context->callback (restart_context->callback_cls,
5275 "Failed to restart peers!");
5276 GNUNET_free (restart_context);
5282 * Callback for informing us about a successful
5283 * or unsuccessful churn stop call.
5285 * @param cls a ChurnContext
5286 * @param emsg NULL on success, non-NULL on failure
5290 churn_stop_callback (void *cls, const char *emsg)
5292 struct ShutdownContext *shutdown_ctx = cls;
5293 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
5294 unsigned int total_left;
5295 char *error_message;
5297 error_message = NULL;
5298 shutdown_ctx->outstanding--;
5302 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5303 "Churn stop callback failed with error `%s'\n", emsg);
5304 churn_ctx->num_failed_stop++;
5308 churn_ctx->num_to_stop--;
5312 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5313 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
5316 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
5317 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5319 if (total_left == 0)
5321 if ((churn_ctx->num_failed_stop > 0)
5322 || (churn_ctx->num_failed_start > 0))
5324 GNUNET_asprintf (&error_message,
5325 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5326 churn_ctx->num_failed_start,
5327 churn_ctx->num_failed_stop);
5329 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5330 GNUNET_free_non_null (error_message);
5331 GNUNET_free (churn_ctx);
5332 GNUNET_free (shutdown_ctx);
5337 * Count the number of running peers.
5339 * @param pg handle for the peer group
5341 * @return the number of currently running peers in the peer group
5344 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
5347 unsigned int running = 0;
5348 for (i = 0; i < pg->total; i++)
5350 if (pg->peers[i].daemon->running == GNUNET_YES)
5352 GNUNET_assert (running != -1);
5360 * Task to rate limit the number of outstanding peer shutdown
5361 * requests. This is necessary for making sure we don't do
5362 * too many ssh connections at once, but is generally nicer
5363 * to any system as well (graduated task starts, as opposed
5364 * to calling gnunet-arm N times all at once).
5367 schedule_churn_shutdown_task (void *cls,
5368 const struct GNUNET_SCHEDULER_TaskContext *tc)
5370 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5371 struct ShutdownContext *shutdown_ctx;
5372 struct ChurnContext *churn_ctx;
5373 GNUNET_assert (peer_shutdown_ctx != NULL);
5374 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5375 GNUNET_assert (shutdown_ctx != NULL);
5376 churn_ctx = (struct ChurnContext *)shutdown_ctx->cb_cls;
5377 if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
5378 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5379 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5380 &schedule_churn_shutdown_task,
5384 shutdown_ctx->outstanding++;
5385 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5386 shutdown_ctx->timeout, shutdown_ctx->cb,
5387 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5388 GNUNET_free (peer_shutdown_ctx);
5393 * Simulate churn by stopping some peers (and possibly
5394 * re-starting others if churn is called multiple times). This
5395 * function can only be used to create leave-join churn (peers "never"
5396 * leave for good). First "voff" random peers that are currently
5397 * online will be taken offline; then "von" random peers that are then
5398 * offline will be put back online. No notifications will be
5399 * generated for any of these operations except for the callback upon
5402 * @param pg handle for the peer group
5403 * @param voff number of peers that should go offline
5404 * @param von number of peers that should come back online;
5405 * must be zero on first call (since "testbed_start"
5406 * always starts all of the peers)
5407 * @param timeout how long to wait for operations to finish before
5409 * @param cb function to call at the end
5410 * @param cb_cls closure for cb
5413 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
5416 struct GNUNET_TIME_Relative timeout,
5417 GNUNET_TESTING_NotifyCompletion cb,
5420 struct ChurnContext *churn_ctx;
5421 struct ShutdownContext *shutdown_ctx;
5422 struct PeerShutdownContext *peer_shutdown_ctx;
5423 struct PeerRestartContext *peer_restart_ctx;
5424 struct ChurnRestartContext *churn_startup_ctx;
5426 unsigned int running;
5427 unsigned int stopped;
5428 unsigned int total_running;
5429 unsigned int total_stopped;
5431 unsigned int *running_arr;
5432 unsigned int *stopped_arr;
5433 unsigned int *running_permute;
5434 unsigned int *stopped_permute;
5439 if ((von == 0) && (voff == 0)) /* No peers at all? */
5445 for (i = 0; i < pg->total; i++)
5447 if (pg->peers[i].daemon->running == GNUNET_YES)
5449 GNUNET_assert (running != -1);
5454 GNUNET_assert (stopped != -1);
5461 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5462 "Trying to stop more peers than are currently running!\n");
5463 cb (cb_cls, "Trying to stop more peers than are currently running!");
5469 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5470 "Trying to start more peers than are currently stopped!\n");
5471 cb (cb_cls, "Trying to start more peers than are currently stopped!");
5475 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5479 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
5483 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
5485 running_permute = NULL;
5486 stopped_permute = NULL;
5490 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, running);
5493 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, stopped);
5495 total_running = running;
5496 total_stopped = stopped;
5500 churn_ctx->num_to_start = von;
5501 churn_ctx->num_to_stop = voff;
5503 churn_ctx->cb_cls = cb_cls;
5506 for (i = 0; i < pg->total; i++)
5508 if (pg->peers[i].daemon->running == GNUNET_YES)
5510 GNUNET_assert ((running_arr != NULL) && (total_running > running));
5511 running_arr[running] = i;
5516 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
5517 stopped_arr[stopped] = i;
5522 GNUNET_assert (running >= voff);
5525 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5526 shutdown_ctx->cb = &churn_stop_callback;
5527 shutdown_ctx->cb_cls = churn_ctx;
5528 shutdown_ctx->total_peers = voff;
5529 shutdown_ctx->timeout = timeout;
5532 for (i = 0; i < voff; i++)
5535 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
5536 running_permute[i]);
5538 GNUNET_assert (running_arr != NULL);
5539 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
5540 peer_shutdown_ctx->daemon =
5541 pg->peers[running_arr[running_permute[i]]].daemon;
5542 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
5543 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
5547 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
5549 &churn_stop_callback, churn_ctx,
5550 GNUNET_NO, GNUNET_YES); */
5553 GNUNET_assert (stopped >= von);
5556 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
5557 churn_startup_ctx->churn_ctx = churn_ctx;
5558 churn_startup_ctx->timeout = timeout;
5559 churn_startup_ctx->pg = pg;
5561 for (i = 0; i < von; i++)
5564 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
5565 stopped_permute[i]);
5567 GNUNET_assert (stopped_arr != NULL);
5568 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
5569 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
5570 peer_restart_ctx->daemon =
5571 pg->peers[stopped_arr[stopped_permute[i]]].daemon;
5572 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
5574 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
5575 timeout, &churn_start_callback, churn_ctx); */
5578 GNUNET_free_non_null (running_arr);
5579 GNUNET_free_non_null (stopped_arr);
5580 GNUNET_free_non_null (running_permute);
5581 GNUNET_free_non_null (stopped_permute);
5586 * Restart all peers in the given group.
5588 * @param pg the handle to the peer group
5589 * @param callback function to call on completion (or failure)
5590 * @param callback_cls closure for the callback function
5593 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
5594 GNUNET_TESTING_NotifyCompletion callback,
5597 struct RestartContext *restart_context;
5602 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
5603 restart_context->peer_group = pg;
5604 restart_context->peers_restarted = 0;
5605 restart_context->callback = callback;
5606 restart_context->callback_cls = callback_cls;
5608 for (off = 0; off < pg->total; off++)
5610 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
5611 &restart_callback, restart_context);
5617 * Start or stop an individual peer from the given group.
5619 * @param pg handle to the peer group
5620 * @param offset which peer to start or stop
5621 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
5622 * @param timeout how long to wait for shutdown
5623 * @param cb function to call at the end
5624 * @param cb_cls closure for cb
5627 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
5628 unsigned int offset,
5630 struct GNUNET_TIME_Relative timeout,
5631 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5633 struct ShutdownContext *shutdown_ctx;
5634 struct ChurnRestartContext *startup_ctx;
5635 struct ChurnContext *churn_ctx;
5637 if (GNUNET_NO == desired_status)
5639 if (NULL != pg->peers[offset].daemon)
5641 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5642 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5643 churn_ctx->num_to_start = 0;
5644 churn_ctx->num_to_stop = 1;
5646 churn_ctx->cb_cls = cb_cls;
5647 shutdown_ctx->cb_cls = churn_ctx;
5648 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon,
5649 timeout, &churn_stop_callback,
5650 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5653 else if (GNUNET_YES == desired_status)
5655 if (NULL == pg->peers[offset].daemon)
5657 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
5658 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5659 churn_ctx->num_to_start = 1;
5660 churn_ctx->num_to_stop = 0;
5662 churn_ctx->cb_cls = cb_cls;
5663 startup_ctx->churn_ctx = churn_ctx;
5664 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
5665 timeout, &churn_start_callback,
5675 * Callback for shutting down peers in a peer group.
5677 * @param cls closure (struct ShutdownContext)
5678 * @param emsg NULL on success
5681 internal_shutdown_callback (void *cls, const char *emsg)
5683 struct ShutdownContext *shutdown_ctx = cls;
5685 shutdown_ctx->outstanding--;
5688 shutdown_ctx->peers_down++;
5692 shutdown_ctx->peers_failed++;
5695 if ((shutdown_ctx->cb != NULL)
5696 && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed ==
5697 shutdown_ctx->total_peers))
5699 if (shutdown_ctx->peers_failed > 0)
5700 shutdown_ctx->cb (shutdown_ctx->cb_cls,
5701 "Not all peers successfully shut down!");
5703 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
5704 GNUNET_free (shutdown_ctx);
5710 * Task to rate limit the number of outstanding peer shutdown
5711 * requests. This is necessary for making sure we don't do
5712 * too many ssh connections at once, but is generally nicer
5713 * to any system as well (graduated task starts, as opposed
5714 * to calling gnunet-arm N times all at once).
5717 schedule_shutdown_task (void *cls,
5718 const struct GNUNET_SCHEDULER_TaskContext *tc)
5720 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5721 struct ShutdownContext *shutdown_ctx;
5723 GNUNET_assert (peer_shutdown_ctx != NULL);
5724 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5725 GNUNET_assert (shutdown_ctx != NULL);
5727 if (shutdown_ctx->outstanding > shutdown_ctx->pg->max_concurrent_ssh)
5728 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5729 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5730 &schedule_shutdown_task, peer_shutdown_ctx);
5733 shutdown_ctx->outstanding++;
5734 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5735 shutdown_ctx->timeout,
5736 &internal_shutdown_callback, shutdown_ctx,
5737 GNUNET_YES, GNUNET_NO);
5738 GNUNET_free (peer_shutdown_ctx);
5743 * Shutdown all peers started in the given group.
5745 * @param pg handle to the peer group
5746 * @param timeout how long to wait for shutdown
5747 * @param cb callback to notify upon success or failure
5748 * @param cb_cls closure for cb
5751 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
5752 struct GNUNET_TIME_Relative timeout,
5753 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
5756 struct ShutdownContext *shutdown_ctx;
5757 struct PeerShutdownContext *peer_shutdown_ctx;
5759 struct PeerConnection *conn_iter;
5760 struct PeerConnection *temp_conn;
5763 GNUNET_assert (pg->total > 0);
5765 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
5766 shutdown_ctx->cb = cb;
5767 shutdown_ctx->cb_cls = cb_cls;
5768 shutdown_ctx->total_peers = pg->total;
5769 shutdown_ctx->timeout = timeout;
5770 shutdown_ctx->pg = pg;
5771 /* shtudown_ctx->outstanding = 0; */
5773 for (off = 0; off < pg->total; off++)
5775 GNUNET_assert (NULL != pg->peers[off].daemon);
5776 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
5777 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
5778 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
5779 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
5781 if (NULL != pg->peers[off].cfg)
5782 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
5784 conn_iter = pg->peers[off].allowed_peers_head;
5785 while (conn_iter != NULL)
5787 temp_conn = conn_iter->next;
5788 GNUNET_free(conn_iter);
5789 conn_iter = temp_conn;
5792 conn_iter = pg->peers[off].connect_peers_head;
5793 while (conn_iter != NULL)
5795 temp_conn = conn_iter->next;
5796 GNUNET_free(conn_iter);
5797 conn_iter = temp_conn;
5800 conn_iter = pg->peers[off].blacklisted_peers_head;
5801 while (conn_iter != NULL)
5803 temp_conn = conn_iter->next;
5804 GNUNET_free(conn_iter);
5805 conn_iter = temp_conn;
5808 conn_iter = pg->peers[off].connect_peers_working_set_head;
5809 while (conn_iter != NULL)
5811 temp_conn = conn_iter->next;
5812 GNUNET_free(conn_iter);
5813 conn_iter = temp_conn;
5816 if (pg->peers[off].allowed_peers != NULL)
5817 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
5818 if (pg->peers[off].connect_peers != NULL)
5819 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
5820 if (pg->peers[off].blacklisted_peers != NULL)
5821 GNUNET_CONTAINER_multihashmap_destroy (pg->
5822 peers[off].blacklisted_peers);
5825 GNUNET_free (pg->peers);
5826 GNUNET_free_non_null(pg->hostkey_data);
5827 for (off = 0; off < pg->num_hosts; off++)
5829 GNUNET_free (pg->hosts[off].hostname);
5830 GNUNET_free_non_null (pg->hosts[off].username);
5832 GNUNET_free_non_null (pg->hosts);
5837 /* end of testing_group.c */