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_NO
35 #define VERBOSE_TOPOLOGY GNUNET_YES
37 #define DEBUG_CHURN GNUNET_NO
41 #define USE_SEND_HELLOS GNUNET_NO
44 * Lowest port used for GNUnet testing. Should be high enough to not
45 * conflict with other applications running on the hosts but be low
46 * enough to not conflict with client-ports (typically starting around
49 #define LOW_PORT 12000
52 * Highest port used for GNUnet testing. Should be low enough to not
53 * conflict with the port range for "local" ports (client apps; see
54 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
56 #define HIGH_PORT 56000
58 /* Maximum time to delay connect attempt */
59 #define MAX_CONNECT_DELAY 300
62 * Which list of peers do we need to modify?
66 /** Modify allowed peers */
69 /** Modify connect peers */
72 /** Modify blacklist peers */
75 /** Modify workingset peers */
80 * Prototype of a function called whenever two peers would be connected
81 * in a certain topology.
83 typedef unsigned int (*GNUNET_TESTING_ConnectionProcessor) (struct
84 GNUNET_TESTING_PeerGroup
94 * Context for handling churning a peer group
99 * The peergroup we are dealing with.
101 struct GNUNET_TESTING_PeerGroup *pg;
104 * Callback used to notify of churning finished
106 GNUNET_TESTING_NotifyCompletion cb;
109 * Closure for callback
114 * Number of peers that still need to be started
116 unsigned int num_to_start;
119 * Number of peers that still need to be stopped
121 unsigned int num_to_stop;
124 * Number of peers that failed to start
126 unsigned int num_failed_start;
129 * Number of peers that failed to stop
131 unsigned int num_failed_stop;
134 struct RestartContext
137 * The group of peers being restarted
139 struct GNUNET_TESTING_PeerGroup *peer_group;
142 * How many peers have been restarted thus far
144 unsigned int peers_restarted;
147 * How many peers got an error when restarting
149 unsigned int peers_restart_failed;
152 * The function to call once all peers have been restarted
154 GNUNET_TESTING_NotifyCompletion callback;
157 * Closure for callback function
164 struct SendHelloContext
167 * Global handle to the peer group.
169 struct GNUNET_TESTING_PeerGroup *pg;
172 * The data about this specific peer.
174 struct PeerData *peer;
177 * The next HELLO that needs sent to this peer.
179 struct PeerConnection *peer_pos;
182 * Are we connected to CORE yet?
184 unsigned int core_ready;
187 * How many attempts should we make for failed connections?
189 unsigned int connect_attempts;
192 * Task for scheduling core connect requests to be sent.
194 GNUNET_SCHEDULER_TaskIdentifier core_connect_task;
198 struct ShutdownContext
200 struct GNUNET_TESTING_PeerGroup *pg;
202 * Total peers to wait for
204 unsigned int total_peers;
207 * Number of peers successfully shut down
209 unsigned int peers_down;
212 * Number of peers failed to shut down
214 unsigned int peers_failed;
217 * Number of peers we have started shutting
218 * down. If too many, wait on them.
220 unsigned int outstanding;
223 * Timeout for shutdown.
225 struct GNUNET_TIME_Relative timeout;
228 * Callback to call when all peers either
229 * shutdown or failed to shutdown
231 GNUNET_TESTING_NotifyCompletion cb;
240 * Individual shutdown context for a particular peer.
242 struct PeerShutdownContext
245 * Pointer to the high level shutdown context.
247 struct ShutdownContext *shutdown_ctx;
250 * The daemon handle for the peer to shut down.
252 struct GNUNET_TESTING_Daemon *daemon;
256 * Individual shutdown context for a particular peer.
258 struct PeerRestartContext
261 * Pointer to the high level restart context.
263 struct ChurnRestartContext *churn_restart_ctx;
266 * The daemon handle for the peer to shut down.
268 struct GNUNET_TESTING_Daemon *daemon;
272 struct CreateTopologyContext
276 * Function to call with number of connections
278 GNUNET_TESTING_NotifyConnections cont;
281 * Closure for connection notification
288 /** Waiting to read number of peers */
291 /** Should find next peer index */
294 /** Should find colon */
297 /** Should read other peer index, space, or endline */
303 struct PeerConnection
308 struct PeerConnection *prev;
313 struct PeerConnection *next;
317 * Index of daemon in pg->peers
324 struct InternalStartContext
327 * Pointer to peerdata
329 struct PeerData *peer;
332 * Timeout for peer startup
334 struct GNUNET_TIME_Relative timeout;
337 * Client callback for hostkey notification
339 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
342 * Closure for hostkey_callback
347 * Client callback for peer start notification
349 GNUNET_TESTING_NotifyDaemonRunning start_cb;
357 * Hostname, where to start the peer
359 const char *hostname;
362 * Username to use when connecting to the
365 const char *username;
368 * Pointer to starting memory location of a hostkey
373 * Port to use for ssh.
379 struct ChurnRestartContext
382 * PeerGroup that we are working with.
384 struct GNUNET_TESTING_PeerGroup *pg;
387 * Number of restarts currently in flight.
389 unsigned int outstanding;
392 * Handle to the underlying churn context.
394 struct ChurnContext *churn_ctx;
397 * How long to allow the operation to take.
399 struct GNUNET_TIME_Relative timeout;
402 struct OutstandingSSH
404 struct OutstandingSSH *next;
406 struct OutstandingSSH *prev;
409 * Number of current ssh connections.
411 uint32_t outstanding;
414 * The hostname of this peer.
416 const char *hostname;
420 * Data we keep per peer.
425 * (Initial) configuration of the host.
426 * (initial because clients could change
427 * it and we would not know about those
430 struct GNUNET_CONFIGURATION_Handle *cfg;
433 * Handle for controlling the daemon.
435 struct GNUNET_TESTING_Daemon *daemon;
438 * The peergroup this peer belongs to.
440 struct GNUNET_TESTING_PeerGroup *pg;
444 * Linked list of allowed peer connections.
446 struct PeerConnection *allowed_peers_head;
449 * Linked list of allowed peer connections.
451 struct PeerConnection *allowed_peers_tail;
454 * Linked list of blacklisted peer connections.
456 struct PeerConnection *blacklisted_peers_head;
459 * Linked list of blacklisted peer connections.
461 struct PeerConnection *blacklisted_peers_tail;
464 * Linked list of connect peer connections.
466 struct PeerConnection *connect_peers_head;
469 * Linked list of connect peer connections.
471 struct PeerConnection *connect_peers_tail;
474 * Linked list of connect peer connections.
476 struct PeerConnection *connect_peers_working_set_head;
479 * Linked list of connect peer connections.
481 struct PeerConnection *connect_peers_working_set_tail;
485 * Hash map of allowed peer connections (F2F created topology)
487 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
490 * Hash map of blacklisted peers
492 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
495 * Hash map of peer connections
497 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
500 * Temporary hash map of peer connections
502 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
506 * Temporary variable for topology creation, should be reset before
507 * creating any topology so the count is valid once finished.
512 * Context to keep track of peers being started, to
513 * stagger hostkey generation and peer startup.
515 struct InternalStartContext internal_context;
520 * Linked list of per-host data.
530 * SSH username to use when connecting to this host.
535 * SSH port to use when connecting to this host.
540 * Lowest port that we have not yet used
546 struct TopologyIterateContext
549 * The peergroup we are working with.
551 struct GNUNET_TESTING_PeerGroup *pg;
554 * Callback for notifying of two connected peers.
556 GNUNET_TESTING_NotifyTopology topology_cb;
559 * Closure for topology_cb
564 * Number of peers currently connected to.
566 unsigned int connected;
569 * Number of peers we have finished iterating.
571 unsigned int completed;
574 * Number of peers total.
579 struct StatsIterateContext
582 * The peergroup that we are dealing with.
584 struct GNUNET_TESTING_PeerGroup *pg;
587 * Continuation to call once all stats information has been retrieved.
589 GNUNET_STATISTICS_Callback cont;
592 * Proc function to call on each value received.
594 GNUNET_TESTING_STATISTICS_Iterator proc;
597 * Closure for topology_cb
602 * Number of peers currently connected to.
604 unsigned int connected;
607 * Number of peers we have finished iterating.
609 unsigned int completed;
612 * Number of peers total.
620 struct GNUNET_TESTING_Daemon *daemon;
623 struct StatsCoreContext
626 struct GNUNET_TESTING_Daemon *daemon;
628 * Handle to the statistics service.
630 struct GNUNET_STATISTICS_Handle *stats_handle;
633 * Handle for getting statistics.
635 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
639 struct ConnectTopologyContext
642 * How many connections are left to create.
644 unsigned int remaining_connections;
647 * Handle to group of peers.
649 struct GNUNET_TESTING_PeerGroup *pg;
652 * How long to try this connection before timing out.
654 struct GNUNET_TIME_Relative connect_timeout;
657 * How many times to retry connecting the two peers.
659 unsigned int connect_attempts;
662 * Temp value set for each iteration.
664 //struct PeerData *first;
667 * Notification that all peers are connected.
669 GNUNET_TESTING_NotifyCompletion notify_connections_done;
672 * Closure for notify.
679 * Handle to a group of GNUnet peers.
681 struct GNUNET_TESTING_PeerGroup
684 * Configuration template.
686 const struct GNUNET_CONFIGURATION_Handle *cfg;
689 * Function to call on each started daemon.
691 //GNUNET_TESTING_NotifyDaemonRunning cb;
699 * Function to call on each topology connection created
701 GNUNET_TESTING_NotifyConnection notify_connection;
704 * Callback for notify_connection
706 void *notify_connection_cls;
709 * Array of information about hosts.
711 struct HostData *hosts;
714 * Number of hosts (size of HostData)
716 unsigned int num_hosts;
719 * Array of "total" peers.
721 struct PeerData *peers;
724 * Number of peers in this group.
729 * At what time should we fail the peer startup process?
731 struct GNUNET_TIME_Absolute max_timeout;
734 * How many peers are being started right now?
736 unsigned int starting;
739 * How many peers have already been started?
741 unsigned int started;
744 * Number of possible connections to peers
747 unsigned int max_outstanding_connections;
750 * Number of ssh connections to peers (max).
752 unsigned int max_concurrent_ssh;
755 * Number of connects we are waiting on, allows us to rate limit
758 unsigned int outstanding_connects;
761 * Number of HELLOs we have yet to send.
763 unsigned int remaining_hellos;
766 * How many connects have already been scheduled?
768 unsigned int total_connects_scheduled;
771 * Hostkeys loaded from a file.
776 * Head of DLL to keep track of the number of outstanding
777 * ssh connections per peer.
779 struct OutstandingSSH *ssh_head;
782 * Tail of DLL to keep track of the number of outstanding
783 * ssh connections per peer.
785 struct OutstandingSSH *ssh_tail;
788 * Stop scheduling peers connecting.
790 unsigned int stop_connects;
793 * Connection context for peer group.
795 struct ConnectTopologyContext ct_ctx;
801 * The altered configuration.
803 struct GNUNET_CONFIGURATION_Handle *ret;
806 * The original configuration to alter.
808 const struct GNUNET_CONFIGURATION_Handle *orig;
811 * The hostname that this peer will run on.
813 const char *hostname;
816 * The next possible port to assign.
821 * Unique number for unix domain sockets.
826 * Unique number for this peer/host to offset
827 * things that are grouped by host.
833 struct ConnectContext
836 * Index of peer to connect second to.
838 uint32_t first_index;
841 * Index of peer to connect first to.
843 uint32_t second_index;
846 * Higher level topology connection context.
848 struct ConnectTopologyContext *ct_ctx;
851 * Whether this connection has been accounted for in the schedule_connect call.
856 struct UnblacklistContext
861 struct GNUNET_TESTING_PeerGroup *pg;
864 * uid of the first peer
874 struct GNUNET_TESTING_PeerGroup *pg;
877 * uid of the first peer
882 * Peer data for first peer.
884 struct PeerData *first;
887 * Random percentage to use
892 struct MinimumContext
897 struct GNUNET_TESTING_PeerGroup *pg;
900 * uid of the first peer
905 * Peer data for first peer.
907 struct PeerData *first;
910 * Number of conns per peer
912 unsigned int num_to_add;
915 * Permuted array of all possible connections. Only add the Nth
916 * peer if it's in the Nth position.
918 unsigned int *pg_array;
921 * What number is the current element we are iterating over?
923 unsigned int current;
931 struct GNUNET_TESTING_PeerGroup *pg;
934 * uid of the first peer
939 * uid of the second peer
944 * Peer data for first peer.
946 struct PeerData *first;
949 * Which peer has been chosen as the one to add?
954 * What number is the current element we are iterating over?
956 unsigned int current;
961 * Convert unique ID to hash code.
963 * @param uid unique ID to convert
964 * @param hash set to uid (extended with zeros)
967 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
969 memset (hash, 0, sizeof (GNUNET_HashCode));
970 *((uint32_t *) hash) = uid;
974 * Convert hash code to unique ID.
976 * @param uid unique ID to convert
977 * @param hash set to uid (extended with zeros)
980 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
982 memcpy (uid, hash, sizeof (uint32_t));
987 static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
991 * Get a topology from a string input.
993 * @param topology where to write the retrieved topology
994 * @param topology_string The string to attempt to
995 * get a configuration value from
996 * @return GNUNET_YES if topology string matched a
997 * known topology, GNUNET_NO if not
1000 GNUNET_TESTING_topology_get (enum GNUNET_TESTING_Topology *topology,
1001 const char *topology_string)
1004 * Strings representing topologies in enum
1006 static const char *topology_strings[] = {
1008 * A clique (everyone connected to everyone else).
1013 * Small-world network (2d torus plus random links).
1018 * Small-world network (ring plus random links).
1038 * Certain percentage of peers are unable to communicate directly
1039 * replicating NAT conditions
1044 * Scale free topology.
1049 * Straight line topology.
1054 * All peers are disconnected.
1059 * Read the topology from a file.
1067 if (topology_string == NULL)
1069 while (topology_strings[curr] != NULL)
1071 if (strcasecmp (topology_strings[curr], topology_string) == 0)
1078 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
1084 * Get connect topology option from string input.
1086 * @param topology_option where to write the retrieved topology
1087 * @param topology_string The string to attempt to
1088 * get a configuration value from
1089 * @return GNUNET_YES if string matched a known
1090 * topology option, GNUNET_NO if not
1093 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption
1095 const char *topology_string)
1098 * Options for connecting a topology as strings.
1100 static const char *topology_option_strings[] = {
1102 * Try to connect all peers specified in the topology.
1107 * Choose a random subset of connections to create.
1109 "CONNECT_RANDOM_SUBSET",
1112 * Create at least X connections for each peer.
1117 * Using a depth first search, create one connection
1118 * per peer. If any are missed (graph disconnected)
1119 * start over at those peers until all have at least one
1125 * Find the N closest peers to each allowed peer in the
1126 * topology and make sure a connection to those peers
1127 * exists in the connect topology.
1132 * No options specified.
1140 if (topology_string == NULL)
1142 while (NULL != topology_option_strings[curr])
1144 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1146 *topology_option = curr;
1151 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1156 * Function to iterate over options. Copies
1157 * the options to the target configuration,
1158 * updating PORT values as needed.
1160 * @param cls closure
1161 * @param section name of the section
1162 * @param option name of the option
1163 * @param value value of the option
1166 update_config (void *cls,
1167 const char *section, const char *option, const char *value)
1169 struct UpdateContext *ctx = cls;
1173 char *single_variable;
1174 char *per_host_variable;
1175 unsigned long long num_per_host;
1177 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1178 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1180 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1184 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1187 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
1190 else if ((ival != 0) &&
1192 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1193 single_variable)) &&
1194 GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1198 GNUNET_snprintf (cval, sizeof (cval), "%u", ival + ctx->fdnum % num_per_host);
1203 if (0 == strcmp (option, "UNIXPATH"))
1206 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1209 GNUNET_snprintf (uval,
1211 "/tmp/test-service-%s-%u", section, ctx->upnum++);
1214 else if ((GNUNET_YES ==
1215 GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1217 &num_per_host)) && (num_per_host > 0))
1220 GNUNET_snprintf (uval,
1222 "/tmp/test-service-%s-%u",
1223 section, ctx->fdnum % num_per_host);
1228 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1230 value = ctx->hostname;
1232 GNUNET_free (single_variable);
1233 GNUNET_free (per_host_variable);
1234 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1239 * Create a new configuration using the given configuration
1240 * as a template; however, each PORT in the existing cfg
1241 * must be renumbered by incrementing "*port". If we run
1242 * out of "*port" numbers, return NULL.
1244 * @param cfg template configuration
1245 * @param off the current peer offset
1246 * @param port port numbers to use, update to reflect
1247 * port numbers that were used
1248 * @param upnum number to make unix domain socket names unique
1249 * @param hostname hostname of the controlling host, to allow control connections from
1250 * @param fdnum number used to offset the unix domain socket for grouped processes
1251 * (such as statistics or peerinfo, which can be shared among others)
1253 * @return new configuration, NULL on error
1255 static struct GNUNET_CONFIGURATION_Handle *
1256 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
1259 uint32_t * upnum, const char *hostname,
1262 struct UpdateContext uc;
1265 char *allowed_hosts;
1271 uc.ret = GNUNET_CONFIGURATION_create ();
1272 uc.hostname = hostname;
1275 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1276 if (uc.nport >= HIGH_PORT)
1279 GNUNET_CONFIGURATION_destroy (uc.ret);
1283 if (GNUNET_CONFIGURATION_get_value_string
1284 (cfg, "testing", "control_host", &control_host) == GNUNET_OK)
1286 if (hostname != NULL)
1287 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1290 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1292 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1294 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1295 "ACCEPT_FROM", allowed_hosts);
1296 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1298 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1299 "ACCEPT_FROM", allowed_hosts);
1301 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH", "");
1302 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH", "");
1303 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH", "");
1304 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH", "");
1306 GNUNET_free_non_null (control_host);
1307 GNUNET_free (allowed_hosts);
1311 /* arm needs to know to allow connections from the host on which it is running,
1312 * otherwise gnunet-arm is unable to connect to it in some instances */
1313 if (hostname != NULL)
1315 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1316 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1317 "BINDTO", hostname);
1318 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1319 "BINDTO", hostname);
1320 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1322 GNUNET_free (allowed_hosts);
1326 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1327 "BINDTO", "127.0.0.1");
1328 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1329 "BINDTO", "127.0.0.1");
1332 *port = (uint16_t) uc.nport;
1340 * Add entries to the some list
1342 * @param pg the peer group we are working with
1343 * @param first index of the first peer
1344 * @param second index of the second peer
1345 * @param list the peer list to use
1347 * @return the number of connections added (can be 0, 1 or 2)
1351 remove_connections (struct GNUNET_TESTING_PeerGroup *pg,
1352 unsigned int first, unsigned int second,
1353 enum PeerLists list)
1357 struct PeerConnection **first_list;
1358 struct PeerConnection **second_list;
1359 struct PeerConnection *first_iter;
1360 struct PeerConnection *second_iter;
1361 struct PeerConnection **first_tail;
1362 struct PeerConnection **second_tail;
1365 GNUNET_HashCode hash_first;
1366 GNUNET_HashCode hash_second;
1368 hash_from_uid (first, &hash_first);
1369 hash_from_uid (second, &hash_second);
1377 first_list = &pg->peers[first].allowed_peers_head;
1378 second_list = &pg->peers[second].allowed_peers_head;
1379 first_tail = &pg->peers[first].allowed_peers_tail;
1380 second_tail = &pg->peers[second].allowed_peers_tail;
1383 first_list = &pg->peers[first].connect_peers_head;
1384 second_list = &pg->peers[second].connect_peers_head;
1385 first_tail = &pg->peers[first].connect_peers_tail;
1386 second_tail = &pg->peers[second].connect_peers_tail;
1389 first_list = &pg->peers[first].blacklisted_peers_head;
1390 second_list = &pg->peers[second].blacklisted_peers_head;
1391 first_tail = &pg->peers[first].blacklisted_peers_tail;
1392 second_tail = &pg->peers[second].blacklisted_peers_tail;
1395 first_list = &pg->peers[first].connect_peers_working_set_head;
1396 second_list = &pg->peers[second].connect_peers_working_set_head;
1397 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1398 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1405 first_iter = *first_list;
1406 while (first_iter != NULL)
1408 if (first_iter->index == second)
1410 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1411 GNUNET_free(first_iter);
1415 first_iter = first_iter->next;
1418 second_iter = *second_list;
1419 while (second_iter != NULL)
1421 if (second_iter->index == first)
1423 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1424 GNUNET_free(second_iter);
1428 second_iter = second_iter->next;
1432 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1435 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1440 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1443 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1452 * Add entries to the some list
1454 * @param pg the peer group we are working with
1455 * @param first index of the first peer
1456 * @param second index of the second peer
1457 * @param list the list type that we should modify
1459 * @return the number of connections added (can be 0, 1 or 2)
1463 add_connections (struct GNUNET_TESTING_PeerGroup *pg,
1464 unsigned int first, unsigned int second,
1465 enum PeerLists list)
1471 struct PeerConnection **first_list;
1472 struct PeerConnection **second_list;
1473 struct PeerConnection *first_iter;
1474 struct PeerConnection *second_iter;
1475 struct PeerConnection *new_first;
1476 struct PeerConnection *new_second;
1477 struct PeerConnection **first_tail;
1478 struct PeerConnection **second_tail;
1480 GNUNET_HashCode hash_first;
1481 GNUNET_HashCode hash_second;
1483 hash_from_uid (first, &hash_first);
1484 hash_from_uid (second, &hash_second);
1491 first_list = &pg->peers[first].allowed_peers_head;
1492 second_list = &pg->peers[second].allowed_peers_head;
1493 first_tail = &pg->peers[first].allowed_peers_tail;
1494 second_tail = &pg->peers[second].allowed_peers_tail;
1497 first_list = &pg->peers[first].connect_peers_head;
1498 second_list = &pg->peers[second].connect_peers_head;
1499 first_tail = &pg->peers[first].connect_peers_tail;
1500 second_tail = &pg->peers[second].connect_peers_tail;
1503 first_list = &pg->peers[first].blacklisted_peers_head;
1504 second_list = &pg->peers[second].blacklisted_peers_head;
1505 first_tail = &pg->peers[first].blacklisted_peers_tail;
1506 second_tail = &pg->peers[second].blacklisted_peers_tail;
1509 first_list = &pg->peers[first].connect_peers_working_set_head;
1510 second_list = &pg->peers[second].connect_peers_working_set_head;
1511 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1512 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1519 add_first = GNUNET_YES;
1520 add_second = GNUNET_YES;
1522 first_iter = *first_list;
1523 while (first_iter != NULL)
1525 if (first_iter->index == second)
1527 add_first = GNUNET_NO;
1530 first_iter = first_iter->next;
1533 second_iter = *second_list;
1534 while (second_iter != NULL)
1536 if (second_iter->index == first)
1538 add_second = GNUNET_NO;
1541 second_iter = second_iter->next;
1545 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1548 add_first = GNUNET_YES;
1552 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1555 add_second = GNUNET_YES;
1563 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1564 new_first->index = second;
1565 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1567 GNUNET_assert (GNUNET_OK ==
1568 GNUNET_CONTAINER_multihashmap_put (pg->
1570 [first].blacklisted_peers,
1573 peers[second].daemon,
1574 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1576 pg->peers[first].num_connections++;
1583 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1584 new_second->index = first;
1585 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1587 GNUNET_assert (GNUNET_OK ==
1588 GNUNET_CONTAINER_multihashmap_put (pg->
1590 [second].blacklisted_peers,
1593 peers[first].daemon,
1594 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1596 pg->peers[second].num_connections++;
1605 * Scale free network construction as described in:
1607 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1609 * Start with a network of "one" peer, then progressively add
1610 * peers up to the total number. At each step, iterate over
1611 * all possible peers and connect new peer based on number of
1612 * existing connections of the target peer.
1614 * @param pg the peer group we are dealing with
1615 * @param proc the connection processor to use
1616 * @param list the peer list to use
1618 * @return the number of connections created
1621 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg,
1622 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1625 unsigned int total_connections;
1626 unsigned int outer_count;
1628 unsigned int previous_total_connections;
1632 GNUNET_assert (pg->total > 1);
1634 /* Add a connection between the first two nodes */
1635 total_connections = proc (pg, 0, 1, list);
1637 for (outer_count = 1; outer_count < pg->total; outer_count++)
1639 previous_total_connections = total_connections;
1640 for (i = 0; i < outer_count; i++)
1643 pg->peers[i].num_connections /
1644 (double) previous_total_connections;
1647 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1648 UINT64_MAX)) / ((double) UINT64_MAX);
1650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1651 "Considering connecting peer %d to peer %d\n",
1654 if (random < probability)
1657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1658 "Connecting peer %d to peer %d\n", outer_count, i);
1660 total_connections += proc (pg, outer_count, i, list);
1665 return total_connections;
1669 * Create a topology given a peer group (set of running peers)
1670 * and a connection processor. Creates a small world topology
1671 * according to the rewired ring construction. The basic
1672 * behavior is that a ring topology is created, but with some
1673 * probability instead of connecting a peer to the next
1674 * neighbor in the ring a connection will be created to a peer
1675 * selected uniformly at random. We use the TESTING
1676 * PERCENTAGE option to specify what number of
1677 * connections each peer should have. Default is 2,
1678 * which makes the ring, any given number is multiplied by
1679 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1680 * each peer have on average 2logn connections. The additional
1681 * connections are made at increasing distance around the ring
1682 * from the original peer, or to random peers based on the re-
1683 * wiring probability. The TESTING
1684 * PROBABILITY option is used as the probability that a given
1685 * connection is rewired.
1687 * @param pg the peergroup to create the topology on
1688 * @param proc the connection processor to call to actually set
1689 * up connections between two peers
1690 * @param list the peer list to use
1692 * @return the number of connections that were set up
1696 create_small_world_ring (struct GNUNET_TESTING_PeerGroup *pg,
1697 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1701 unsigned int natLog;
1702 unsigned int randomPeer;
1703 double random, logNModifier, probability;
1704 unsigned int smallWorldConnections;
1709 unsigned int useAnd;
1710 int connect_attempts;
1712 logNModifier = 0.5; /* FIXME: default value? */
1713 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1718 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1719 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1721 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1722 p_string, "LOGNMODIFIER", "TESTING");
1723 GNUNET_free (p_string);
1725 probability = 0.5; /* FIXME: default percentage? */
1726 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1731 if (sscanf (p_string, "%lf", &probability) != 1)
1732 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1734 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1735 p_string, "PERCENTAGE", "TESTING");
1736 GNUNET_free (p_string);
1738 natLog = log (pg->total);
1739 connsPerPeer = ceil (natLog * logNModifier);
1741 if (connsPerPeer % 2 == 1)
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."), connsPerPeer);
1746 smallWorldConnections = 0;
1747 connect_attempts = 0;
1748 for (i = 0; i < pg->total; i++)
1751 max = i + connsPerPeer / 2;
1752 min = i - connsPerPeer / 2;
1754 if (max > pg->total - 1)
1756 max = max - pg->total;
1762 min = pg->total - 1 + min;
1766 for (j = 0; j < connsPerPeer / 2; j++)
1770 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1771 UINT64_MAX) / ((double) UINT64_MAX));
1772 if (random < probability)
1774 /* Connect to uniformly selected random peer */
1776 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1778 while ((((randomPeer < max) && (randomPeer > min))
1779 && (useAnd == 0)) || (((randomPeer > min)
1780 || (randomPeer < max))
1784 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1787 smallWorldConnections += proc (pg, i, randomPeer, list);
1791 nodeToConnect = i + j + 1;
1792 if (nodeToConnect > pg->total - 1)
1794 nodeToConnect = nodeToConnect - pg->total;
1796 connect_attempts += proc (pg, i, nodeToConnect, list);
1802 connect_attempts += smallWorldConnections;
1804 return connect_attempts;
1808 * Create a topology given a peer group (set of running peers)
1809 * and a connection processor.
1811 * @param pg the peergroup to create the topology on
1812 * @param proc the connection processor to call to actually set
1813 * up connections between two peers
1814 * @param list the peer list to use
1816 * @return the number of connections that were set up
1820 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg,
1821 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1823 unsigned int outer_count, inner_count;
1824 unsigned int cutoff;
1825 int connect_attempts;
1826 double nat_percentage;
1829 nat_percentage = 0.6; /* FIXME: default percentage? */
1830 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1835 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1836 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1838 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1839 p_string, "PERCENTAGE", "TESTING");
1840 GNUNET_free (p_string);
1845 cutoff = (unsigned int) (nat_percentage * pg->total);
1847 connect_attempts = 0;
1849 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1851 for (inner_count = outer_count + 1; inner_count < pg->total;
1854 if ((outer_count > cutoff) || (inner_count > cutoff))
1857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1858 "Connecting peer %d to peer %d\n",
1859 outer_count, inner_count);
1861 connect_attempts += proc (pg, outer_count, inner_count, list);
1866 return connect_attempts;
1871 * Create a topology given a peer group (set of running peers)
1872 * and a connection processor.
1874 * @param pg the peergroup to create the topology on
1875 * @param proc the connection processor to call to actually set
1876 * up connections between two peers
1877 * @param list the peer list to use
1879 * @return the number of connections that were set up
1883 create_small_world (struct GNUNET_TESTING_PeerGroup *pg,
1884 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1886 unsigned int i, j, k;
1887 unsigned int square;
1890 unsigned int toggle = 1;
1891 unsigned int nodeToConnect;
1892 unsigned int natLog;
1893 unsigned int node1Row;
1894 unsigned int node1Col;
1895 unsigned int node2Row;
1896 unsigned int node2Col;
1897 unsigned int distance;
1898 double probability, random, percentage;
1899 unsigned int smallWorldConnections;
1900 unsigned int small_world_it;
1902 int connect_attempts;
1903 square = floor (sqrt (pg->total));
1907 percentage = 0.5; /* FIXME: default percentage? */
1908 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1913 if (sscanf (p_string, "%lf", &percentage) != 1)
1914 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1916 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1917 p_string, "PERCENTAGE", "TESTING");
1918 GNUNET_free (p_string);
1920 if (percentage < 0.0)
1922 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1924 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1925 "PERCENTAGE", "TESTING", percentage);
1928 probability = 0.5; /* FIXME: default percentage? */
1929 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1934 if (sscanf (p_string, "%lf", &probability) != 1)
1935 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1937 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1938 p_string, "PROBABILITY", "TESTING");
1939 GNUNET_free (p_string);
1941 if (square * square != pg->total)
1943 while (rows * cols < pg->total)
1945 if (toggle % 2 == 0)
1954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1956 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1960 connect_attempts = 0;
1961 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1962 * to the node to its right and above. Once this is over, we'll have our torus!
1963 * Special case for the last node (if the rows and columns are not equal), connect
1964 * to the first in the row to maintain topology.
1966 for (i = 0; i < pg->total; i++)
1968 /* First connect to the node to the right */
1969 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1970 nodeToConnect = i + 1;
1971 else if (i + 1 == pg->total)
1972 nodeToConnect = rows * cols - cols;
1974 nodeToConnect = i - cols + 1;
1976 connect_attempts += proc (pg, i, nodeToConnect, list);
1980 nodeToConnect = (rows * cols) - cols + i;
1981 if (nodeToConnect >= pg->total)
1982 nodeToConnect -= cols;
1985 nodeToConnect = i - cols;
1987 if (nodeToConnect < pg->total)
1988 connect_attempts += proc (pg, i, nodeToConnect, list);
1990 natLog = log (pg->total);
1991 #if VERBOSE_TESTING > 2
1992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1993 _("natural log of %d is %d, will run %d iterations\n"),
1994 pg->total, natLog, (int) (natLog * percentage));
1995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1996 _("Total connections added thus far: %u!\n"), connect_attempts);
1998 smallWorldConnections = 0;
1999 small_world_it = (unsigned int) (natLog * percentage);
2000 if (small_world_it < 1)
2002 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
2003 for (i = 0; i < small_world_it; i++)
2005 for (j = 0; j < pg->total; j++)
2007 /* Determine the row and column of node at position j on the 2d torus */
2008 node1Row = j / cols;
2009 node1Col = j - (node1Row * cols);
2010 for (k = 0; k < pg->total; k++)
2012 /* Determine the row and column of node at position k on the 2d torus */
2013 node2Row = k / cols;
2014 node2Col = k - (node2Row * cols);
2015 /* Simple Cartesian distance */
2017 abs (node1Row - node2Row) + abs (node1Col - node2Col);
2020 /* Calculate probability as 1 over the square of the distance */
2021 probability = 1.0 / (distance * distance);
2022 /* Choose a random value between 0 and 1 */
2025 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2027 ((double) UINT64_MAX);
2028 /* If random < probability, then connect the two nodes */
2029 if (random < probability)
2030 smallWorldConnections += proc (pg, j, k, list);
2036 connect_attempts += smallWorldConnections;
2037 #if VERBOSE_TESTING > 2
2038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2039 _("Total connections added for small world: %d!\n"),
2040 smallWorldConnections);
2042 return connect_attempts;
2046 * Create a topology given a peer group (set of running peers)
2047 * and a connection processor.
2049 * @param pg the peergroup to create the topology on
2050 * @param proc the connection processor to call to actually set
2051 * up connections between two peers
2052 * @param list the peer list to use
2054 * @return the number of connections that were set up
2058 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg,
2059 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2062 unsigned int outer_count;
2063 unsigned int inner_count;
2064 int connect_attempts;
2068 probability = 0.5; /* FIXME: default percentage? */
2069 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
2074 if (sscanf (p_string, "%lf", &probability) != 1)
2075 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2077 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2078 p_string, "PROBABILITY", "TESTING");
2079 GNUNET_free (p_string);
2081 connect_attempts = 0;
2082 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2084 for (inner_count = outer_count + 1; inner_count < pg->total;
2089 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2090 UINT64_MAX)) / ((double) UINT64_MAX);
2092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2093 _("rand is %f probability is %f\n"), temp_rand,
2096 if (temp_rand < probability)
2098 connect_attempts += proc (pg, outer_count, inner_count, list);
2103 return connect_attempts;
2107 * Create a topology given a peer group (set of running peers)
2108 * and a connection processor. This particular function creates
2109 * the connections for a 2d-torus, plus additional "closest"
2110 * connections per peer.
2112 * @param pg the peergroup to create the topology on
2113 * @param proc the connection processor to call to actually set
2114 * up connections between two peers
2115 * @param list the peer list to use
2117 * @return the number of connections that were set up
2121 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg,
2122 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2125 unsigned int square;
2128 unsigned int toggle = 1;
2129 unsigned int nodeToConnect;
2130 int connect_attempts;
2132 connect_attempts = 0;
2134 square = floor (sqrt (pg->total));
2138 if (square * square != pg->total)
2140 while (rows * cols < pg->total)
2142 if (toggle % 2 == 0)
2151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2153 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2156 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2157 * to the node to its right and above. Once this is over, we'll have our torus!
2158 * Special case for the last node (if the rows and columns are not equal), connect
2159 * to the first in the row to maintain topology.
2161 for (i = 0; i < pg->total; i++)
2163 /* First connect to the node to the right */
2164 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2165 nodeToConnect = i + 1;
2166 else if (i + 1 == pg->total)
2167 nodeToConnect = rows * cols - cols;
2169 nodeToConnect = i - cols + 1;
2171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2172 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2174 connect_attempts += proc (pg, i, nodeToConnect, list);
2176 /* Second connect to the node immediately above */
2179 nodeToConnect = (rows * cols) - cols + i;
2180 if (nodeToConnect >= pg->total)
2181 nodeToConnect -= cols;
2184 nodeToConnect = i - cols;
2186 if (nodeToConnect < pg->total)
2189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2190 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2192 connect_attempts += proc (pg, i, nodeToConnect, list);
2197 return connect_attempts;
2202 * Create a topology given a peer group (set of running peers)
2203 * and a connection processor.
2205 * @param pg the peergroup to create the topology on
2206 * @param proc the connection processor to call to actually set
2207 * up connections between two peers
2208 * @param list the peer list to use
2210 * @return the number of connections that were set up
2214 create_clique (struct GNUNET_TESTING_PeerGroup *pg,
2215 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2217 unsigned int outer_count;
2218 unsigned int inner_count;
2219 int connect_attempts;
2221 connect_attempts = 0;
2223 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2225 for (inner_count = outer_count + 1; inner_count < pg->total;
2229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2230 "Connecting peer %d to peer %d\n",
2231 outer_count, inner_count);
2233 connect_attempts += proc (pg, outer_count, inner_count, list);
2237 return connect_attempts;
2242 * Iterator over hash map entries.
2244 * @param cls closure the peer group
2245 * @param key the key stored in the hashmap is the
2246 * index of the peer to connect to
2247 * @param value value in the hash map, handle to the peer daemon
2248 * @return GNUNET_YES if we should continue to
2253 unblacklist_iterator (void *cls,
2254 const GNUNET_HashCode * key,
2257 struct UnblacklistContext *un_ctx = cls;
2258 uint32_t second_pos;
2260 uid_from_hash (key, &second_pos);
2262 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2269 * Create a blacklist topology based on the allowed topology
2270 * which disallows any connections not in the allowed topology
2271 * at the transport level.
2273 * @param pg the peergroup to create the topology on
2274 * @param proc the connection processor to call to allow
2275 * up connections between two peers
2277 * @return the number of connections that were set up
2281 copy_allowed (struct GNUNET_TESTING_PeerGroup *pg,
2282 GNUNET_TESTING_ConnectionProcessor proc)
2284 struct UnblacklistContext un_ctx;
2287 struct PeerConnection *iter;
2291 for (count = 0; count < pg->total - 1; count++)
2293 un_ctx.first_uid = count;
2295 iter = pg->peers[count].allowed_peers_head;
2296 while (iter != NULL)
2298 remove_connections(pg, count, iter->index, BLACKLIST);
2299 //unblacklist_connections(pg, count, iter->index);
2303 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers, &unblacklist_iterator, &un_ctx);
2306 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2311 * Create a topology given a peer group (set of running peers)
2312 * and a connection processor.
2314 * @param pg the peergroup to create the topology on
2315 * @param proc the connection processor to call to actually set
2316 * up connections between two peers
2317 * @param list which list should be modified
2319 * @return the number of connections that were set up
2323 create_line (struct GNUNET_TESTING_PeerGroup *pg,
2324 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2327 int connect_attempts;
2329 connect_attempts = 0;
2331 /* Connect each peer to the next highest numbered peer */
2332 for (count = 0; count < pg->total - 1; count++)
2335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2336 "Connecting peer %d to peer %d\n", count, count + 1);
2338 connect_attempts += proc (pg, count, count + 1, list);
2341 return connect_attempts;
2345 * Create a topology given a peer group (set of running peers)
2346 * and a connection processor.
2348 * @param pg the peergroup to create the topology on
2349 * @param filename the file to read topology information from
2350 * @param proc the connection processor to call to actually set
2351 * up connections between two peers
2352 * @param list the peer list to use
2354 * @return the number of connections that were set up
2358 create_from_file (struct GNUNET_TESTING_PeerGroup *pg,
2360 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2362 int connect_attempts;
2363 unsigned int first_peer_index;
2364 unsigned int second_peer_index;
2365 connect_attempts = 0;
2370 unsigned int total_peers;
2372 enum States curr_state;
2374 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2375 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2377 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2379 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2380 "Could not open file `%s' specified for topology!", filename);
2381 return connect_attempts;
2384 data = GNUNET_malloc_large (frstat.st_size);
2385 GNUNET_assert(data != NULL);
2386 if (frstat.st_size !=
2387 GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2390 "Could not read file %s specified for host list, ending test!", filename);
2392 return connect_attempts;
2397 /* First line should contain a single integer, specifying the number of peers */
2398 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2399 curr_state = NUM_PEERS;
2400 while (count < frstat.st_size - 1)
2402 if ((buf[count] == '\n') || (buf[count] == ' '))
2411 if (1 != sscanf(&buf[count], "%u", &total_peers))
2413 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read number of peers from topology file!\n");
2414 GNUNET_free_non_null(data);
2415 return connect_attempts;
2417 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read %u total peers in topology\n", total_peers);
2418 GNUNET_assert(total_peers == pg->total);
2419 curr_state = PEER_INDEX;
2420 while((buf[count] != '\n') && (count < frstat.st_size - 1))
2425 if (1 != sscanf(&buf[count], "%u", &first_peer_index))
2427 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read peer index from topology file!\n");
2428 GNUNET_free_non_null(data);
2429 return connect_attempts;
2431 while((buf[count] != ':') && (count < frstat.st_size - 1))
2434 curr_state = OTHER_PEER_INDEX;
2437 if (1 == sscanf(&buf[count], ":"))
2438 curr_state = OTHER_PEER_INDEX;
2441 case OTHER_PEER_INDEX:
2442 if (1 != sscanf(&buf[count], "%u", &second_peer_index))
2444 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to peer index from topology file!\n");
2445 GNUNET_free_non_null(data);
2446 return connect_attempts;
2448 /* Assume file is written with first peer 1, but array index is 0 */
2449 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index - 1, list);
2450 while((buf[count] != '\n') && (buf[count] != ',') && (count < frstat.st_size - 1))
2452 if (buf[count] == '\n')
2454 curr_state = PEER_INDEX;
2456 else if (buf[count] != ',')
2458 curr_state = OTHER_PEER_INDEX;
2463 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Found bad data in topology file while in state %d!\n", curr_state);
2465 return connect_attempts;
2470 /* Connect each peer to the next highest numbered peer */
2471 for (count = 0; count < pg->total - 1; count++)
2474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2475 "Connecting peer %d to peer %d\n", first_peer_index, second_peer_index);
2477 connect_attempts += proc (pg, first_peer_index, second_peer_index);
2480 return connect_attempts;
2484 * Create a topology given a peer group (set of running peers)
2485 * and a connection processor.
2487 * @param pg the peergroup to create the topology on
2488 * @param proc the connection processor to call to actually set
2489 * up connections between two peers
2490 * @param list the peer list to use
2492 * @return the number of connections that were set up
2496 create_ring (struct GNUNET_TESTING_PeerGroup *pg,
2497 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2500 int connect_attempts;
2502 connect_attempts = 0;
2504 /* Connect each peer to the next highest numbered peer */
2505 for (count = 0; count < pg->total - 1; count++)
2508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2509 "Connecting peer %d to peer %d\n", count, count + 1);
2511 connect_attempts += proc (pg, count, count + 1, list);
2514 /* Connect the last peer to the first peer */
2515 connect_attempts += proc (pg, pg->total - 1, 0, list);
2517 return connect_attempts;
2522 * Iterator for writing friends of a peer to a file.
2524 * @param cls closure, an open writable file handle
2525 * @param key the key the daemon was stored under
2526 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2528 * @return GNUNET_YES to continue iteration
2530 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2531 * with a single file_iterator that takes a closure which contains
2532 * the prefix to write before the peer. Then this could be used
2533 * for blacklisting multiple transports and writing the friend
2534 * file. I'm sure *someone* will complain loudly about other
2535 * things that negate these functions even existing so no point in
2539 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2541 FILE *temp_friend_handle = cls;
2542 struct GNUNET_TESTING_Daemon *peer = value;
2543 struct GNUNET_PeerIdentity *temppeer;
2544 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2546 temppeer = &peer->id;
2547 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2548 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2553 struct BlacklistContext
2556 * The (open) file handle to write to
2558 FILE *temp_file_handle;
2561 * The transport that this peer will be blacklisted on.
2567 * Iterator for writing blacklist data to appropriate files.
2569 * @param cls closure, an open writable file handle
2570 * @param key the key the daemon was stored under
2571 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2573 * @return GNUNET_YES to continue iteration
2576 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2578 struct BlacklistContext *blacklist_ctx = cls;
2579 struct GNUNET_TESTING_Daemon *peer = value;
2580 struct GNUNET_PeerIdentity *temppeer;
2581 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2583 temppeer = &peer->id;
2584 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2585 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2586 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2587 blacklist_ctx->transport, (char *) &peer_enc);
2595 * Create the friend files based on the PeerConnection's
2596 * of each peer in the peer group, and copy the files
2597 * to the appropriate place
2599 * @param pg the peer group we are dealing with
2602 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
2604 FILE *temp_friend_handle;
2605 unsigned int pg_iter;
2606 char *temp_service_path;
2607 struct GNUNET_OS_Process **procarr;
2611 enum GNUNET_OS_ProcessStatusType type;
2612 unsigned long return_code;
2620 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2621 struct PeerConnection *conn_iter;
2623 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2624 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2626 mytemp = GNUNET_DISK_mktemp ("friends");
2627 GNUNET_assert (mytemp != NULL);
2628 temp_friend_handle = fopen (mytemp, "wt");
2629 GNUNET_assert (temp_friend_handle != NULL);
2631 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2632 while (conn_iter != NULL)
2634 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
2635 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2636 conn_iter = conn_iter->next;
2639 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2640 &friend_file_iterator,
2641 temp_friend_handle);
2643 fclose (temp_friend_handle);
2646 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2647 daemon->cfg, "PATHS",
2649 &temp_service_path))
2651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2653 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2654 "SERVICEHOME", "PATHS");
2655 if (UNLINK (mytemp) != 0)
2656 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2658 GNUNET_free (mytemp);
2662 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2664 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2665 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2670 _("Copying file with command cp %s %s\n"), mytemp, arg);
2672 ret = GNUNET_OS_process_wait(procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2673 GNUNET_OS_process_close (procarr[pg_iter]);
2676 else /* Remote, scp the file to the correct place */
2678 if (NULL != pg->peers[pg_iter].daemon->username)
2679 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2680 pg->peers[pg_iter].daemon->username,
2681 pg->peers[pg_iter].daemon->hostname,
2684 GNUNET_asprintf (&arg, "%s:%s/friends",
2685 pg->peers[pg_iter].daemon->hostname,
2688 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2691 ret = GNUNET_OS_process_wait(procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2692 GNUNET_OS_process_close (procarr[pg_iter]);
2693 if (ret != GNUNET_OK)
2695 procarr[pg_iter] = NULL;
2697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2698 _("Copying file with command scp %s %s\n"), mytemp,
2703 GNUNET_free (temp_service_path);
2704 GNUNET_free (mytemp);
2709 ret = GNUNET_SYSERR;
2710 while ((count < max_wait) && (ret != GNUNET_OK))
2713 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2717 _("Checking copy status of file %d\n"), pg_iter);
2719 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2721 if (GNUNET_OS_process_status
2722 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2724 ret = GNUNET_SYSERR;
2726 else if ((type != GNUNET_OS_PROCESS_EXITED)
2727 || (return_code != 0))
2729 ret = GNUNET_SYSERR;
2733 GNUNET_OS_process_close (procarr[pg_iter]);
2734 procarr[pg_iter] = NULL;
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2737 _("File %d copied\n"), pg_iter);
2743 if (ret == GNUNET_SYSERR)
2745 /* FIXME: why sleep here? -CG */
2751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2752 _("Finished copying all friend files!\n"));
2755 GNUNET_free (procarr);
2761 * Create the blacklist files based on the PeerConnection's
2762 * of each peer in the peer group, and copy the files
2763 * to the appropriate place.
2765 * @param pg the peer group we are dealing with
2766 * @param transports space delimited list of transports to blacklist
2769 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg,
2770 const char *transports)
2772 FILE *temp_file_handle;
2773 unsigned int pg_iter;
2774 char *temp_service_path;
2775 struct GNUNET_OS_Process **procarr;
2778 enum GNUNET_OS_ProcessStatusType type;
2779 unsigned long return_code;
2786 char *temp_transports;
2789 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2790 struct PeerConnection *conn_iter;
2792 static struct BlacklistContext blacklist_ctx;
2795 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2796 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2798 mytemp = GNUNET_DISK_mktemp ("blacklist");
2799 GNUNET_assert (mytemp != NULL);
2800 temp_file_handle = fopen (mytemp, "wt");
2801 GNUNET_assert (temp_file_handle != NULL);
2802 temp_transports = GNUNET_strdup (transports);
2804 blacklist_ctx.temp_file_handle = temp_file_handle;
2806 transport_len = strlen (temp_transports) + 1;
2809 for (i = 0; i < transport_len; i++)
2811 if ((temp_transports[i] == ' ') && (pos == NULL))
2812 continue; /* At start of string (whitespace) */
2813 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2815 temp_transports[i] = '\0';
2817 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
2818 while (conn_iter != NULL)
2820 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
2821 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
2822 conn_iter = conn_iter->next;
2826 blacklist_ctx.transport = pos;
2827 entry_count = GNUNET_CONTAINER_multihashmap_iterate (pg->
2829 [pg_iter].blacklisted_peers,
2830 &blacklist_file_iterator,
2834 } /* At beginning of actual string */
2835 else if (pos == NULL)
2837 pos = &temp_transports[i];
2841 GNUNET_free (temp_transports);
2842 fclose (temp_file_handle);
2845 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2846 daemon->cfg, "PATHS",
2848 &temp_service_path))
2850 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2852 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2853 "SERVICEHOME", "PATHS");
2854 if (UNLINK (mytemp) != 0)
2855 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2857 GNUNET_free (mytemp);
2861 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2863 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2864 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2869 _("Copying file with command cp %s %s\n"), mytemp, arg);
2874 else /* Remote, scp the file to the correct place */
2876 if (NULL != pg->peers[pg_iter].daemon->username)
2877 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
2878 pg->peers[pg_iter].daemon->username,
2879 pg->peers[pg_iter].daemon->hostname,
2882 GNUNET_asprintf (&arg, "%s:%s/blacklist",
2883 pg->peers[pg_iter].daemon->hostname,
2886 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2889 GNUNET_OS_process_wait(procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
2892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2893 _("Copying file with command scp %s %s\n"), mytemp,
2898 GNUNET_free (temp_service_path);
2899 GNUNET_free (mytemp);
2903 ret = GNUNET_SYSERR;
2904 while ((count < max_wait) && (ret != GNUNET_OK))
2907 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2911 _("Checking copy status of file %d\n"), pg_iter);
2913 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2915 if (GNUNET_OS_process_status
2916 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2918 ret = GNUNET_SYSERR;
2920 else if ((type != GNUNET_OS_PROCESS_EXITED)
2921 || (return_code != 0))
2923 ret = GNUNET_SYSERR;
2927 GNUNET_OS_process_close (procarr[pg_iter]);
2928 procarr[pg_iter] = NULL;
2930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2931 _("File %d copied\n"), pg_iter);
2937 if (ret == GNUNET_SYSERR)
2939 /* FIXME: why sleep here? -CG */
2945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2946 _("Finished copying all blacklist files!\n"));
2948 GNUNET_free (procarr);
2952 /* Forward Declaration */
2954 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2957 * Choose a random peer's next connection to create, and
2958 * call schedule_connect to set up the connect task.
2960 * @param ct_ctx the overall connection context
2962 static void preschedule_connect(struct GNUNET_TESTING_PeerGroup *pg)
2964 struct ConnectTopologyContext *ct_ctx = &pg->ct_ctx;
2965 struct PeerConnection *connection_iter;
2966 struct ConnectContext *connect_context;
2967 uint32_t random_peer;
2969 if (ct_ctx->remaining_connections == 0)
2971 random_peer = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
2972 while (pg->peers[random_peer].connect_peers_head == NULL)
2973 random_peer = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
2975 connection_iter = pg->peers[random_peer].connect_peers_head;
2976 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
2977 connect_context->first_index = random_peer;
2978 connect_context->second_index = connection_iter->index;
2979 connect_context->ct_ctx = ct_ctx;
2980 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
2981 GNUNET_CONTAINER_DLL_remove(pg->peers[random_peer].connect_peers_head, pg->peers[random_peer].connect_peers_tail, connection_iter);
2982 GNUNET_free(connection_iter);
2983 ct_ctx->remaining_connections--;
2987 /* Forward declaration */
2988 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2992 * Close connections and free the hello context.
2994 * @param cls the 'struct SendHelloContext *'
2995 * @param tc scheduler context
2998 free_hello_context (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3000 struct SendHelloContext *send_hello_context = cls;
3001 if (send_hello_context->peer->daemon->server != NULL)
3003 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3004 send_hello_context->peer->daemon->server = NULL;
3006 if (send_hello_context->peer->daemon->th != NULL)
3008 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3009 send_hello_context->peer->daemon->th = NULL;
3011 if (send_hello_context->core_connect_task != GNUNET_SCHEDULER_NO_TASK)
3013 GNUNET_SCHEDULER_cancel(send_hello_context->core_connect_task);
3014 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3016 send_hello_context->pg->outstanding_connects--;
3017 GNUNET_free(send_hello_context);
3021 * For peers that haven't yet connected, notify
3022 * the caller that they have failed (timeout).
3024 * @param cls the 'struct SendHelloContext *'
3025 * @param tc scheduler context
3028 notify_remaining_connections_failed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3030 struct SendHelloContext *send_hello_context = cls;
3031 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3032 struct PeerConnection *connection;
3034 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3035 send_hello_context->peer->daemon->server = NULL;
3037 connection = send_hello_context->peer->connect_peers_head;
3039 while (connection != NULL)
3041 if (pg->notify_connection != NULL)
3043 pg->notify_connection(pg->notify_connection_cls,
3044 &send_hello_context->peer->daemon->id,
3045 &pg->peers[connection->index].daemon->id,
3047 send_hello_context->peer->daemon->cfg,
3048 pg->peers[connection->index].daemon->cfg,
3049 send_hello_context->peer->daemon,
3050 pg->peers[connection->index].daemon,
3051 "Peers failed to connect (timeout)");
3053 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3054 GNUNET_free(connection);
3055 connection = connection->next;
3057 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3059 other_peer = &pg->peers[connection->index];
3065 * For peers that haven't yet connected, send
3066 * CORE connect requests.
3068 * @param cls the 'struct SendHelloContext *'
3069 * @param tc scheduler context
3072 send_core_connect_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3074 struct SendHelloContext *send_hello_context = cls;
3075 struct PeerConnection *conn;
3076 GNUNET_assert(send_hello_context->peer->daemon->server != NULL);
3078 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3080 send_hello_context->connect_attempts++;
3081 if (send_hello_context->connect_attempts < send_hello_context->pg->ct_ctx.connect_attempts)
3083 conn = send_hello_context->peer->connect_peers_head;
3084 while (conn != NULL)
3086 GNUNET_CORE_peer_request_connect(send_hello_context->peer->daemon->server,
3087 GNUNET_TIME_relative_get_forever(),
3088 &send_hello_context->pg->peers[conn->index].daemon->id,
3093 send_hello_context->core_connect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_divide(send_hello_context->pg->ct_ctx.connect_timeout, send_hello_context->pg->ct_ctx.connect_attempts) ,
3094 &send_core_connect_requests,
3095 send_hello_context);
3099 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Timeout before all connections created, marking rest as failed!\n");
3100 GNUNET_SCHEDULER_add_now(¬ify_remaining_connections_failed, send_hello_context);
3107 * Success, connection is up. Signal client our success.
3109 * @param cls our "struct SendHelloContext"
3110 * @param peer identity of the peer that has connected
3111 * @param atsi performance information
3113 * FIXME: remove peers from BOTH lists, call notify twice, should
3114 * double the speed of connections as long as the list iteration
3115 * doesn't take too long!
3118 core_connect_notify (void *cls,
3119 const struct GNUNET_PeerIdentity *peer,
3120 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3122 struct SendHelloContext *send_hello_context = cls;
3123 struct PeerConnection *connection;
3124 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3126 struct PeerData *other_peer;
3129 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3130 "Connected peer %s to peer %s\n",
3131 ctx->d1->shortname, GNUNET_i2s(peer));
3134 if (0 == memcmp(&send_hello_context->peer->daemon->id, peer, sizeof(struct GNUNET_PeerIdentity)))
3137 connection = send_hello_context->peer->connect_peers_head;
3142 while ((connection != NULL) &&
3143 (0 != memcmp(&pg->peers[connection->index].daemon->id, peer, sizeof(struct GNUNET_PeerIdentity))))
3145 connection = connection->next;
3148 if (connection == NULL)
3150 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Connected peer %s to %s, not in list (no problem(?))\n", GNUNET_i2s(peer), send_hello_context->peer->daemon->shortname);
3155 other_peer = &pg->peers[connection->index];
3157 if (pg->notify_connection != NULL)
3159 pg->notify_connection(pg->notify_connection_cls,
3160 &send_hello_context->peer->daemon->id,
3163 send_hello_context->peer->daemon->cfg,
3164 pg->peers[connection->index].daemon->cfg,
3165 send_hello_context->peer->daemon,
3166 pg->peers[connection->index].daemon,
3169 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3170 GNUNET_free(connection);
3174 /* Notify of reverse connection and remove from other peers list of outstanding */
3175 if (other_peer != NULL)
3177 connection = other_peer->connect_peers_head;
3178 while ((connection != NULL) &&
3179 (0 != memcmp(&send_hello_context->peer->daemon->id, &pg->peers[connection->index].daemon->id, sizeof(struct GNUNET_PeerIdentity))))
3181 connection = connection->next;
3183 if (connection != NULL)
3185 if (pg->notify_connection != NULL)
3187 pg->notify_connection(pg->notify_connection_cls,
3189 &send_hello_context->peer->daemon->id,
3191 pg->peers[connection->index].daemon->cfg,
3192 send_hello_context->peer->daemon->cfg,
3193 pg->peers[connection->index].daemon,
3194 send_hello_context->peer->daemon,
3198 GNUNET_CONTAINER_DLL_remove(other_peer->connect_peers_head, other_peer->connect_peers_tail, connection);
3199 GNUNET_free(connection);
3204 if (send_hello_context->peer->connect_peers_head == NULL)
3206 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3211 * Notify of a successful connection to the core service.
3213 * @param cls a struct SendHelloContext *
3214 * @param server handle to the core service
3215 * @param my_identity the peer identity of this peer
3216 * @param publicKey the public key of the peer
3219 core_init (void *cls,
3220 struct GNUNET_CORE_Handle * server,
3221 const struct GNUNET_PeerIdentity *
3224 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
3227 struct SendHelloContext *send_hello_context = cls;
3228 send_hello_context->core_ready = GNUNET_YES;
3233 * Function called once a hello has been sent
3234 * to the transport, move on to the next one
3235 * or go away forever.
3237 * @param cls the 'struct SendHelloContext *'
3238 * @param tc scheduler context
3241 hello_sent_callback (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3243 struct SendHelloContext *send_hello_context = cls;
3244 //unsigned int pg_iter;
3245 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3247 GNUNET_free(send_hello_context);
3251 send_hello_context->pg->remaining_hellos--;
3253 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sent HELLO, have %d remaining!\n", send_hello_context->pg->remaining_hellos);
3255 if (send_hello_context->peer_pos == NULL) /* All HELLOs (for this peer!) have been transmitted! */
3258 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All hellos for this peer sent, disconnecting transport!\n");
3260 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3261 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3262 send_hello_context->peer->daemon->th = NULL;
3264 /*if (send_hello_context->pg->remaining_hellos == 0)
3266 for (pg_iter = 0; pg_iter < send_hello_context->pg->max_outstanding_connections; pg_iter++)
3268 preschedule_connect(&send_hello_context->pg->ct_ctx);
3272 GNUNET_assert (send_hello_context->peer->daemon->server == NULL);
3273 send_hello_context->peer->daemon->server = GNUNET_CORE_connect(send_hello_context->peer->cfg,
3277 &core_connect_notify,
3284 send_hello_context->core_connect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_divide(send_hello_context->pg->ct_ctx.connect_timeout, send_hello_context->pg->ct_ctx.connect_attempts),
3285 &send_core_connect_requests,
3286 send_hello_context);
3289 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3294 * Connect to a peer, give it all the HELLO's of those peers
3295 * we will later ask it to connect to.
3297 * @param ct_ctx the overall connection context
3299 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3301 struct SendHelloContext *send_hello_context = cls;
3302 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3304 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3306 GNUNET_free(send_hello_context);
3310 GNUNET_assert(send_hello_context->peer_pos != NULL); /* All of the HELLO sends to be scheduled have been scheduled! */
3312 if (((send_hello_context->peer->daemon->th == NULL) &&
3313 (pg->outstanding_connects > pg->max_outstanding_connections)) ||
3314 (pg->stop_connects == GNUNET_YES))
3316 #if VERBOSE_TESTING > 2
3317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3319 ("Delaying connect, we have too many outstanding connections!\n"));
3321 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3322 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3323 &schedule_send_hellos, send_hello_context);
3327 #if VERBOSE_TESTING > 2
3328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3329 _("Creating connection, outstanding_connections is %d\n"),
3330 outstanding_connects);
3332 if (send_hello_context->peer->daemon->th == NULL)
3334 pg->outstanding_connects++; /* Actual TRANSPORT, CORE connections! */
3335 send_hello_context->peer->daemon->th = GNUNET_TRANSPORT_connect(send_hello_context->peer->cfg,
3343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3344 _("Offering Hello of peer %s to peer %s\n"),
3345 send_hello_context->peer->daemon->shortname, pg->peers[send_hello_context->peer_pos->index].daemon->shortname);
3347 GNUNET_TRANSPORT_offer_hello(send_hello_context->peer->daemon->th,
3348 (const struct GNUNET_MessageHeader *)pg->peers[send_hello_context->peer_pos->index].daemon->hello,
3349 &hello_sent_callback,
3350 send_hello_context);
3351 send_hello_context->peer_pos = send_hello_context->peer_pos->next;
3352 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3359 * Internal notification of a connection, kept so that we can ensure some connections
3360 * happen instead of flooding all testing daemons with requests to connect.
3363 internal_connect_notify (void *cls,
3364 const struct GNUNET_PeerIdentity *first,
3365 const struct GNUNET_PeerIdentity *second,
3367 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
3368 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
3369 struct GNUNET_TESTING_Daemon *first_daemon,
3370 struct GNUNET_TESTING_Daemon *second_daemon,
3373 struct ConnectContext *connect_ctx = cls;
3374 struct ConnectTopologyContext *ct_ctx = connect_ctx->ct_ctx;
3375 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
3376 struct PeerConnection *connection;
3377 pg->outstanding_connects--;
3380 * Check whether the inverse connection has been scheduled yet,
3381 * if not, we can remove it from the other peers list and avoid
3382 * even trying to connect them again!
3384 connection = pg->peers[connect_ctx->second_index].connect_peers_head;
3389 while ((connection != NULL) &&
3390 (0 != memcmp(first, &pg->peers[connection->index].daemon->id, sizeof(struct GNUNET_PeerIdentity))))
3392 connection = connection->next;
3395 if (connection != NULL) /* Can safely remove! */
3397 ct_ctx->remaining_connections--;
3398 if (pg->notify_connection != NULL) /* Notify of reverse connection */
3399 pg->notify_connection (pg->notify_connection_cls, second, first, distance,
3400 second_cfg, first_cfg, second_daemon, first_daemon,
3403 GNUNET_CONTAINER_DLL_remove(pg->peers[connect_ctx->second_index].connect_peers_head, pg->peers[connect_ctx->second_index].connect_peers_tail, connection);
3404 GNUNET_free(connection);
3407 if (ct_ctx->remaining_connections == 0)
3409 if (ct_ctx->notify_connections_done != NULL)
3410 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
3413 preschedule_connect(pg);
3415 if (pg->notify_connection != NULL)
3416 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
3417 first_cfg, second_cfg, first_daemon, second_daemon,
3420 GNUNET_free(connect_ctx);
3425 * Either delay a connection (because there are too many outstanding)
3426 * or schedule it for right now.
3428 * @param cls a connection context
3429 * @param tc the task runtime context
3432 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3434 struct ConnectContext *connect_context = cls;
3435 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
3437 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3440 if ((pg->outstanding_connects > pg->max_outstanding_connections) || (pg->stop_connects == GNUNET_YES))
3443 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3445 ("Delaying connect, we have too many outstanding connections!\n"));
3447 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3448 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3449 &schedule_connect, connect_context);
3454 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3455 _("Creating connection, outstanding_connections is %d (max %d)\n"),
3456 pg->outstanding_connects, pg->max_outstanding_connections);
3458 pg->outstanding_connects++;
3459 pg->total_connects_scheduled++;
3460 GNUNET_TESTING_daemons_connect (pg->peers[connect_context->first_index].daemon,
3461 pg->peers[connect_context->second_index].daemon,
3462 connect_context->ct_ctx->connect_timeout,
3463 connect_context->ct_ctx->connect_attempts,
3469 &internal_connect_notify,
3470 connect_context); /* FIXME: free connect context! */
3476 * Iterator for actually scheduling connections to be created
3477 * between two peers.
3479 * @param cls closure, a GNUNET_TESTING_Daemon
3480 * @param key the key the second Daemon was stored under
3481 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3483 * @return GNUNET_YES to continue iteration
3486 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3488 struct ConnectTopologyContext *ct_ctx = cls;
3489 struct PeerData *first = ct_ctx->first;
3490 struct GNUNET_TESTING_Daemon *second = value;
3491 struct ConnectContext *connect_context;
3493 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3494 connect_context->first = first->daemon;
3495 connect_context->second = second;
3496 connect_context->ct_ctx = ct_ctx;
3497 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3505 * Iterator for copying all entries in the allowed hashmap to the
3508 * @param cls closure, a GNUNET_TESTING_Daemon
3509 * @param key the key the second Daemon was stored under
3510 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3512 * @return GNUNET_YES to continue iteration
3515 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3517 struct PeerData *first = cls;
3519 GNUNET_assert (GNUNET_OK ==
3520 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
3522 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3529 * Make the peers to connect the same as those that are allowed to be
3532 * @param pg the peer group
3535 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
3537 unsigned int pg_iter;
3541 struct PeerConnection *iter;
3545 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3548 iter = pg->peers[pg_iter].allowed_peers_head;
3549 while (iter != NULL)
3551 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating connection between %d and %d\n", pg_iter, iter->index);
3552 total += add_connections(pg, pg_iter, iter->index, CONNECT);
3553 //total += add_actual_connections(pg, pg_iter, iter->index);
3558 GNUNET_CONTAINER_multihashmap_iterate (pg->
3559 peers[pg_iter].allowed_peers,
3560 ©_topology_iterator,
3561 &pg->peers[pg_iter]);
3563 if (GNUNET_SYSERR == ret)
3564 return GNUNET_SYSERR;
3566 total = total + ret;
3574 * Connect the topology as specified by the PeerConnection's
3575 * of each peer in the peer group
3577 * @param pg the peer group we are dealing with
3578 * @param connect_timeout how long try connecting two peers
3579 * @param connect_attempts how many times (max) to attempt
3580 * @param notify_callback callback to notify when finished
3581 * @param notify_cls closure for notify callback
3583 * @return the number of connections that will be attempted
3586 connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3587 struct GNUNET_TIME_Relative connect_timeout,
3588 unsigned int connect_attempts,
3589 GNUNET_TESTING_NotifyCompletion notify_callback,
3592 unsigned int pg_iter;
3596 struct PeerConnection *connection_iter;
3599 struct SendHelloContext *send_hello_context
3603 pg->ct_ctx.notify_connections_done = notify_callback;
3604 pg->ct_ctx.notify_cls = notify_cls;
3607 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3610 connection_iter = pg->peers[pg_iter].connect_peers_head;
3611 while (connection_iter != NULL)
3613 connection_iter = connection_iter->next;
3618 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3625 pg->ct_ctx.connect_timeout = connect_timeout;
3626 pg->ct_ctx.connect_attempts = connect_attempts;
3627 pg->ct_ctx.remaining_connections = total;
3630 /* First give all peers the HELLO's of other peers (connect to first peer's transport service, give HELLO's of other peers, continue...) */
3631 pg->remaining_hellos = total;
3632 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3634 send_hello_context = GNUNET_malloc(sizeof(struct SendHelloContext));
3635 send_hello_context->peer = &pg->peers[pg_iter];
3636 send_hello_context->peer_pos = pg->peers[pg_iter].connect_peers_head;
3637 send_hello_context->pg = pg;
3638 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3641 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3643 preschedule_connect(pg);
3652 * Takes a peer group and creates a topology based on the
3653 * one specified. Creates a topology means generates friend
3654 * files for the peers so they can only connect to those allowed
3655 * by the topology. This will only have an effect once peers
3656 * are started if the FRIENDS_ONLY option is set in the base
3657 * config. Also takes an optional restrict topology which
3658 * disallows connections based on particular transports
3659 * UNLESS they are specified in the restricted topology.
3661 * @param pg the peer group struct representing the running peers
3662 * @param topology which topology to connect the peers in
3663 * @param restrict_topology disallow restrict_transports transport
3664 * connections to peers NOT in this topology
3665 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3666 * @param restrict_transports space delimited list of transports to blacklist
3667 * to create restricted topology
3669 * @return the maximum number of connections were all allowed peers
3670 * connected to each other
3673 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
3674 enum GNUNET_TESTING_Topology topology,
3675 enum GNUNET_TESTING_Topology
3677 const char *restrict_transports)
3681 unsigned int num_connections;
3682 int unblacklisted_connections;
3687 for (i = 0; i < pg->total; i++)
3689 pg->peers[i].allowed_peers =
3690 GNUNET_CONTAINER_multihashmap_create (100);
3691 pg->peers[i].connect_peers =
3692 GNUNET_CONTAINER_multihashmap_create (100);
3693 pg->peers[i].blacklisted_peers =
3694 GNUNET_CONTAINER_multihashmap_create (100);
3695 pg->peers[i].pg = pg;
3701 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3705 num_connections = create_clique (pg, &add_connections, ALLOWED);
3707 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3710 _("Creating small world (ring) topology\n"));
3713 create_small_world_ring (pg, &add_connections, ALLOWED);
3715 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3718 _("Creating small world (2d-torus) topology\n"));
3720 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3722 case GNUNET_TESTING_TOPOLOGY_RING:
3724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3726 num_connections = create_ring (pg, &add_connections, ALLOWED);
3728 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3732 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3734 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3737 _("Creating Erdos-Renyi topology\n"));
3739 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3741 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3745 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3747 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3750 _("Creating Scale Free topology\n"));
3752 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3754 case GNUNET_TESTING_TOPOLOGY_LINE:
3756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3757 _("Creating straight line topology\n"));
3759 num_connections = create_line (pg, &add_connections, ALLOWED);
3761 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3764 _("Creating topology from file!\n"));
3767 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing", "topology_file",
3769 num_connections = create_from_file (pg, filename, &add_connections, ALLOWED);
3772 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3773 num_connections = 0;
3776 case GNUNET_TESTING_TOPOLOGY_NONE:
3778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3780 ("Creating no allowed topology (all peers can connect at core level)\n"));
3782 num_connections = pg->total * pg->total; /* Clique is allowed! */
3785 num_connections = 0;
3790 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
3792 ret = create_and_copy_friend_files (pg);
3793 if (ret != GNUNET_OK)
3796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3797 _("Failed during friend file copying!\n"));
3799 return GNUNET_SYSERR;
3804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3805 _("Friend files created/copied successfully!\n"));
3810 /* Use the create clique method to initially set all connections as blacklisted. */
3811 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
3812 create_clique (pg, &add_connections, BLACKLIST);
3814 unblacklisted_connections = 0;
3815 /* Un-blacklist connections as per the topology specified */
3816 switch (restrict_topology)
3818 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3821 _("Blacklisting all but clique topology\n"));
3823 unblacklisted_connections =
3824 create_clique (pg, &remove_connections, BLACKLIST);
3826 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3829 _("Blacklisting all but small world (ring) topology\n"));
3831 unblacklisted_connections =
3832 create_small_world_ring (pg, &remove_connections, BLACKLIST);
3834 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3838 ("Blacklisting all but small world (2d-torus) topology\n"));
3840 unblacklisted_connections =
3841 create_small_world (pg, &remove_connections, BLACKLIST);
3843 case GNUNET_TESTING_TOPOLOGY_RING:
3845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3846 _("Blacklisting all but ring topology\n"));
3848 unblacklisted_connections = create_ring (pg, &remove_connections, BLACKLIST);
3850 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3853 _("Blacklisting all but 2d torus topology\n"));
3855 unblacklisted_connections =
3856 create_2d_torus (pg, &remove_connections, BLACKLIST);
3858 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3861 _("Blacklisting all but Erdos-Renyi topology\n"));
3863 unblacklisted_connections =
3864 create_erdos_renyi (pg, &remove_connections, BLACKLIST);
3866 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3869 _("Blacklisting all but InterNAT topology\n"));
3871 unblacklisted_connections =
3872 create_nated_internet (pg, &remove_connections, BLACKLIST);
3874 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3877 _("Blacklisting all but Scale Free topology\n"));
3879 unblacklisted_connections =
3880 create_scale_free (pg, &remove_connections, BLACKLIST);
3882 case GNUNET_TESTING_TOPOLOGY_LINE:
3884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3885 _("Blacklisting all but straight line topology\n"));
3887 unblacklisted_connections = create_line (pg, &remove_connections, BLACKLIST);
3889 case GNUNET_TESTING_TOPOLOGY_NONE: /* Fall through */
3890 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3894 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
3896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3898 ("Creating blacklist topology from allowed\n"));
3899 unblacklisted_connections = copy_allowed (pg, &remove_connections);
3904 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
3906 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n", restrict_transports);
3907 ret = create_and_copy_blacklist_files (pg, restrict_transports);
3908 if (ret != GNUNET_OK)
3911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3912 _("Failed during blacklist file copying!\n"));
3919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3920 _("Blacklist files created/copied successfully!\n"));
3924 return num_connections;
3930 * Iterator for choosing random peers to connect.
3932 * @param cls closure, a RandomContext
3933 * @param key the key the second Daemon was stored under
3934 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3936 * @return GNUNET_YES to continue iteration
3939 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3941 struct RandomContext *random_ctx = cls;
3942 double random_number;
3943 uint32_t second_pos;
3944 GNUNET_HashCode first_hash;
3947 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
3948 UINT64_MAX)) / ((double) UINT64_MAX);
3949 if (random_number < random_ctx->percentage)
3951 GNUNET_assert (GNUNET_OK ==
3952 GNUNET_CONTAINER_multihashmap_put (random_ctx->
3953 first->connect_peers_working_set,
3955 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3958 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
3959 uid_from_hash (key, &second_pos);
3960 hash_from_uid (random_ctx->first_uid, &first_hash);
3961 GNUNET_assert (random_ctx->pg->total > second_pos);
3962 GNUNET_assert (GNUNET_YES ==
3963 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
3965 [second_pos].connect_peers,
3975 * Iterator for adding at least X peers to a peers connection set.
3977 * @param cls closure, MinimumContext
3978 * @param key the key the second Daemon was stored under
3979 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3981 * @return GNUNET_YES to continue iteration
3984 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3986 struct MinimumContext *min_ctx = cls;
3987 uint32_t second_pos;
3988 GNUNET_HashCode first_hash;
3991 if (GNUNET_CONTAINER_multihashmap_size
3992 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
3994 for (i = 0; i < min_ctx->num_to_add; i++)
3996 if (min_ctx->pg_array[i] == min_ctx->current)
3998 GNUNET_assert (GNUNET_OK ==
3999 GNUNET_CONTAINER_multihashmap_put
4000 (min_ctx->first->connect_peers_working_set, key,
4002 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4003 uid_from_hash (key, &second_pos);
4004 hash_from_uid (min_ctx->first_uid, &first_hash);
4005 GNUNET_assert (min_ctx->pg->total > second_pos);
4006 GNUNET_assert (GNUNET_OK ==
4007 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4009 [second_pos].connect_peers_working_set,
4013 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4014 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
4015 GNUNET_assert (GNUNET_YES ==
4016 GNUNET_CONTAINER_multihashmap_remove
4017 (min_ctx->pg->peers[second_pos].connect_peers,
4018 &first_hash, min_ctx->first->daemon));
4025 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
4031 * Iterator for adding peers to a connection set based on a depth first search.
4033 * @param cls closure, MinimumContext
4034 * @param key the key the second daemon was stored under
4035 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4037 * @return GNUNET_YES to continue iteration
4040 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4042 struct DFSContext *dfs_ctx = cls;
4043 GNUNET_HashCode first_hash;
4045 if (dfs_ctx->current == dfs_ctx->chosen)
4047 GNUNET_assert (GNUNET_OK ==
4048 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4049 first->connect_peers_working_set,
4051 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4052 uid_from_hash (key, &dfs_ctx->second_uid);
4053 hash_from_uid (dfs_ctx->first_uid, &first_hash);
4054 GNUNET_assert (GNUNET_OK ==
4055 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4057 [dfs_ctx->second_uid].connect_peers_working_set,
4061 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4062 GNUNET_assert (GNUNET_YES ==
4063 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
4065 [dfs_ctx->second_uid].connect_peers,
4069 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
4070 return GNUNET_NO; /* We have found our peer, don't iterate more */
4079 * From the set of connections possible, choose percentage percent of connections
4080 * to actually connect.
4082 * @param pg the peergroup we are dealing with
4083 * @param percentage what percent of total connections to make
4086 choose_random_connections (struct GNUNET_TESTING_PeerGroup *pg,
4089 struct RandomContext random_ctx;
4092 struct PeerConnection *temp_peers;
4093 struct PeerConnection *conn_iter;
4094 double random_number;
4097 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4099 random_ctx.first_uid = pg_iter;
4100 random_ctx.first = &pg->peers[pg_iter];
4101 random_ctx.percentage = percentage;
4105 conn_iter = pg->peers[pg_iter].connect_peers_head;
4106 while (conn_iter != NULL)
4110 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4111 UINT64_MAX)) / ((double) UINT64_MAX);
4112 if (random_number < percentage)
4114 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
4116 conn_iter = conn_iter->next;
4119 pg->peers[pg_iter].connect_peers_working_set =
4120 GNUNET_CONTAINER_multihashmap_create (pg->total);
4121 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4122 &random_connect_iterator,
4124 /* Now remove the old connections */
4125 GNUNET_CONTAINER_multihashmap_destroy (pg->
4126 peers[pg_iter].connect_peers);
4127 /* And replace with the random set */
4128 pg->peers[pg_iter].connect_peers =
4129 pg->peers[pg_iter].connect_peers_working_set;
4133 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4135 conn_iter = pg->peers[pg_iter].connect_peers_head;
4136 while (pg->peers[pg_iter].connect_peers_head != NULL)
4137 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
4139 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
4140 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
4141 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4142 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4148 * Count the number of connections in a linked list of connections.
4150 * @param conn_list the connection list to get the count of
4152 * @return the number of elements in the list
4155 count_connections (struct PeerConnection *conn_list)
4157 struct PeerConnection *iter;
4161 while (iter != NULL)
4171 count_workingset_connections (struct GNUNET_TESTING_PeerGroup *pg)
4174 unsigned int pg_iter;
4176 struct PeerConnection *conn_iter;
4180 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4183 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
4184 while (conn_iter != NULL)
4187 conn_iter = conn_iter->next;
4191 GNUNET_CONTAINER_multihashmap_size (pg->
4193 [pg_iter].connect_peers_working_set);
4202 count_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg)
4205 unsigned int pg_iter;
4207 struct PeerConnection *conn_iter;
4211 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4214 conn_iter = pg->peers[pg_iter].allowed_peers_head;
4215 while (conn_iter != NULL)
4218 conn_iter = conn_iter->next;
4222 GNUNET_CONTAINER_multihashmap_size (pg->
4224 [pg_iter].allowed_peers);
4233 * From the set of connections possible, choose at least num connections per
4236 * @param pg the peergroup we are dealing with
4237 * @param num how many connections at least should each peer have (if possible)?
4240 choose_minimum (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4243 struct MinimumContext minimum_ctx;
4245 struct PeerConnection *conn_iter;
4246 unsigned int temp_list_size;
4249 uint32_t random; /* Random list entry to connect peer to */
4254 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4256 temp_list_size = count_connections(pg->peers[pg_iter].connect_peers_head);
4257 if (temp_list_size == 0)
4259 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer %d has 0 connections!?!?\n", pg_iter);
4262 for (i = 0; i < num; i++)
4264 random = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, temp_list_size);
4265 conn_iter = pg->peers[pg_iter].connect_peers_head;
4266 for (count = 0; count < random; count++)
4267 conn_iter = conn_iter->next;
4268 /* We now have a random connection, connect it! */
4269 GNUNET_assert(conn_iter != NULL);
4270 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET);
4274 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4276 pg->peers[pg_iter].connect_peers_working_set =
4277 GNUNET_CONTAINER_multihashmap_create (num);
4280 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4282 minimum_ctx.first_uid = pg_iter;
4283 minimum_ctx.pg_array =
4284 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
4285 GNUNET_CONTAINER_multihashmap_size
4286 (pg->peers[pg_iter].connect_peers));
4287 minimum_ctx.first = &pg->peers[pg_iter];
4288 minimum_ctx.pg = pg;
4289 minimum_ctx.num_to_add = num;
4290 minimum_ctx.current = 0;
4291 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4292 &minimum_connect_iterator,
4296 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4298 /* Remove the "old" connections */
4299 GNUNET_CONTAINER_multihashmap_destroy (pg->
4300 peers[pg_iter].connect_peers);
4301 /* And replace with the working set */
4302 pg->peers[pg_iter].connect_peers =
4303 pg->peers[pg_iter].connect_peers_working_set;
4306 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4308 while (pg->peers[pg_iter].connect_peers_head != NULL)
4309 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT);
4311 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
4312 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
4313 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4314 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4319 struct FindClosestContext
4322 * The currently known closest peer.
4324 struct GNUNET_TESTING_Daemon *closest;
4327 * The info for the peer we are adding connections for.
4329 struct PeerData *curr_peer;
4332 * The distance (bits) between the current
4333 * peer and the currently known closest.
4335 unsigned int closest_dist;
4338 * The offset of the closest known peer in
4341 unsigned int closest_num;
4345 * Iterator over hash map entries of the allowed
4346 * peer connections. Find the closest, not already
4347 * connected peer and return it.
4349 * @param cls closure (struct FindClosestContext)
4350 * @param key current key code (hash of offset in pg)
4351 * @param value value in the hash map - a GNUNET_TESTING_Daemon
4352 * @return GNUNET_YES if we should continue to
4357 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
4359 struct FindClosestContext *closest_ctx = cls;
4360 struct GNUNET_TESTING_Daemon *daemon = value;
4362 if (((closest_ctx->closest == NULL) ||
4363 (GNUNET_CRYPTO_hash_matching_bits
4364 (&daemon->id.hashPubKey,
4365 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
4366 closest_ctx->closest_dist))
4368 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
4369 curr_peer->connect_peers,
4372 closest_ctx->closest_dist =
4373 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
4374 &closest_ctx->curr_peer->daemon->
4376 closest_ctx->closest = daemon;
4377 uid_from_hash (key, &closest_ctx->closest_num);
4384 * From the set of connections possible, choose at num connections per
4385 * peer based on depth which are closest out of those allowed. Guaranteed
4386 * to add num peers to connect to, provided there are that many peers
4387 * in the underlay topology to connect to.
4389 * @param pg the peergroup we are dealing with
4390 * @param num how many connections at least should each peer have (if possible)?
4391 * @param proc processor to actually add the connections
4392 * @param list the peer list to use
4395 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
4396 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
4401 struct FindClosestContext closest_ctx;
4406 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
4408 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4410 closest_ctx.curr_peer = &pg->peers[pg_iter];
4411 closest_ctx.closest = NULL;
4412 closest_ctx.closest_dist = 0;
4413 closest_ctx.closest_num = 0;
4414 GNUNET_CONTAINER_multihashmap_iterate (pg->
4415 peers[pg_iter].allowed_peers,
4416 &find_closest_peers,
4418 if (closest_ctx.closest != NULL)
4420 GNUNET_assert (closest_ctx.closest_num < pg->total);
4421 proc (pg, pg_iter, closest_ctx.closest_num, list);
4429 * From the set of connections possible, choose at least num connections per
4430 * peer based on depth first traversal of peer connections. If DFS leaves
4431 * peers unconnected, ensure those peers get connections.
4433 * @param pg the peergroup we are dealing with
4434 * @param num how many connections at least should each peer have (if possible)?
4437 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4441 uint32_t starting_peer;
4442 uint32_t least_connections;
4443 uint32_t random_connection;
4445 unsigned int temp_count;
4446 struct PeerConnection *peer_iter;
4448 struct DFSContext dfs_ctx;
4449 GNUNET_HashCode second_hash;
4455 while ((count_workingset_connections (pg) < num * pg->total)
4456 && (count_allowed_connections (pg) > 0))
4458 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4460 least_connections = -1; /* Set to very high number */
4461 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4463 temp_count = count_connections(pg->peers[pg_iter].connect_peers_working_set_head);
4464 if (temp_count < least_connections)
4466 starting_peer = pg_iter;
4467 least_connections = temp_count;
4472 temp_count = count_connections(pg->peers[starting_peer].connect_peers_head);
4473 if (temp_count == 0)
4474 continue; /* FIXME: infinite loop? */
4476 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, temp_count);
4478 peer_iter = pg->peers[starting_peer].connect_peers_head;
4479 while (temp_count < random_connection)
4481 peer_iter = peer_iter->next;
4484 GNUNET_assert(peer_iter != NULL);
4485 add_connections(pg, starting_peer, peer_iter->index, WORKING_SET);
4486 remove_connections(pg, starting_peer, peer_iter->index, CONNECT);
4487 starting_peer = peer_iter->index;
4492 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4494 pg->peers[pg_iter].connect_peers_working_set =
4495 GNUNET_CONTAINER_multihashmap_create (num);
4500 while ((count_workingset_connections (pg) < num * pg->total)
4501 && (count_allowed_connections (pg) > 0))
4503 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4505 least_connections = -1; /* Set to very high number */
4506 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4508 if (GNUNET_CONTAINER_multihashmap_size
4509 (pg->peers[pg_iter].connect_peers_working_set) <
4512 starting_peer = pg_iter;
4514 GNUNET_CONTAINER_multihashmap_size (pg->
4516 [pg_iter].connect_peers_working_set);
4521 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4527 /* Choose a random peer from the chosen peers set of connections to add */
4529 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4530 GNUNET_CONTAINER_multihashmap_size
4531 (pg->peers[starting_peer].connect_peers));
4532 dfs_ctx.first_uid = starting_peer;
4533 dfs_ctx.first = &pg->peers[starting_peer];
4535 dfs_ctx.current = 0;
4537 GNUNET_CONTAINER_multihashmap_iterate (pg->
4539 [starting_peer].connect_peers,
4540 &dfs_connect_iterator, &dfs_ctx);
4541 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4542 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4543 GNUNET_assert (GNUNET_YES ==
4544 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4545 [starting_peer].connect_peers,
4549 [dfs_ctx.second_uid].daemon));
4550 starting_peer = dfs_ctx.second_uid;
4553 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4555 /* Remove the "old" connections */
4556 GNUNET_CONTAINER_multihashmap_destroy (pg->
4557 peers[pg_iter].connect_peers);
4558 /* And replace with the working set */
4559 pg->peers[pg_iter].connect_peers =
4560 pg->peers[pg_iter].connect_peers_working_set;
4567 * Internal callback for topology information for a particular peer.
4570 internal_topology_callback (void *cls,
4571 const struct GNUNET_PeerIdentity *peer,
4572 const struct GNUNET_TRANSPORT_ATS_Information
4575 struct CoreContext *core_ctx = cls;
4576 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4578 if (peer == NULL) /* Either finished, or something went wrong */
4580 iter_ctx->completed++;
4581 iter_ctx->connected--;
4582 /* One core context allocated per iteration, must free! */
4583 GNUNET_free (core_ctx);
4587 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id,
4591 if (iter_ctx->completed == iter_ctx->total)
4593 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4594 /* Once all are done, free the iteration context */
4595 GNUNET_free (iter_ctx);
4601 * Check running topology iteration tasks, if below max start a new one, otherwise
4602 * schedule for some time in the future.
4605 schedule_get_topology (void *cls,
4606 const struct GNUNET_SCHEDULER_TaskContext *tc)
4608 struct CoreContext *core_context = cls;
4609 struct TopologyIterateContext *topology_context =
4610 (struct TopologyIterateContext *) core_context->iter_context;
4611 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4614 if (topology_context->connected > topology_context->pg->max_outstanding_connections)
4616 #if VERBOSE_TESTING > 2
4617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4619 ("Delaying connect, we have too many outstanding connections!\n"));
4621 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4622 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4623 &schedule_get_topology, core_context);
4627 #if VERBOSE_TESTING > 2
4628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4629 _("Creating connection, outstanding_connections is %d\n"),
4630 outstanding_connects);
4632 topology_context->connected++;
4635 GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4636 &internal_topology_callback,
4639 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4640 internal_topology_callback (core_context, NULL, NULL);
4646 * Iterate over all (running) peers in the peer group, retrieve
4647 * all connections that each currently has.
4650 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
4651 GNUNET_TESTING_NotifyTopology cb, void *cls)
4653 struct TopologyIterateContext *topology_context;
4654 struct CoreContext *core_ctx;
4656 unsigned int total_count;
4658 /* Allocate a single topology iteration context */
4659 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4660 topology_context->topology_cb = cb;
4661 topology_context->cls = cls;
4662 topology_context->pg = pg;
4664 for (i = 0; i < pg->total; i++)
4666 if (pg->peers[i].daemon->running == GNUNET_YES)
4668 /* Allocate one core context per core we need to connect to */
4669 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4670 core_ctx->daemon = pg->peers[i].daemon;
4671 /* Set back pointer to topology iteration context */
4672 core_ctx->iter_context = topology_context;
4673 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4677 if (total_count == 0)
4679 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4680 GNUNET_free (topology_context);
4683 topology_context->total = total_count;
4688 * Callback function to process statistic values.
4689 * This handler is here only really to insert a peer
4690 * identity (or daemon) so the statistics can be uniquely
4691 * tied to a single running peer.
4693 * @param cls closure
4694 * @param subsystem name of subsystem that created the statistic
4695 * @param name the name of the datum
4696 * @param value the current value
4697 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4698 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4701 internal_stats_callback (void *cls,
4702 const char *subsystem,
4703 const char *name, uint64_t value, int is_persistent)
4705 struct StatsCoreContext *core_context = cls;
4706 struct StatsIterateContext *stats_context =
4707 (struct StatsIterateContext *) core_context->iter_context;
4709 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4710 subsystem, name, value, is_persistent);
4714 * Internal continuation call for statistics iteration.
4716 * @param cls closure, the CoreContext for this iteration
4717 * @param success whether or not the statistics iterations
4718 * was canceled or not (we don't care)
4721 internal_stats_cont (void *cls, int success)
4723 struct StatsCoreContext *core_context = cls;
4724 struct StatsIterateContext *stats_context =
4725 (struct StatsIterateContext *) core_context->iter_context;
4727 stats_context->connected--;
4728 stats_context->completed++;
4730 if (stats_context->completed == stats_context->total)
4732 stats_context->cont (stats_context->cls, GNUNET_YES);
4733 GNUNET_free (stats_context);
4736 if (core_context->stats_handle != NULL)
4737 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4739 GNUNET_free (core_context);
4743 * Check running topology iteration tasks, if below max start a new one, otherwise
4744 * schedule for some time in the future.
4747 schedule_get_statistics (void *cls,
4748 const struct GNUNET_SCHEDULER_TaskContext *tc)
4750 struct StatsCoreContext *core_context = cls;
4751 struct StatsIterateContext *stats_context =
4752 (struct StatsIterateContext *) core_context->iter_context;
4754 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4757 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
4759 #if VERBOSE_TESTING > 2
4760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4762 ("Delaying connect, we have too many outstanding connections!\n"));
4764 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4765 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4766 &schedule_get_statistics, core_context);
4770 #if VERBOSE_TESTING > 2
4771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4772 _("Creating connection, outstanding_connections is %d\n"),
4773 outstanding_connects);
4776 stats_context->connected++;
4777 core_context->stats_handle =
4778 GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
4779 if (core_context->stats_handle == NULL)
4781 internal_stats_cont (core_context, GNUNET_NO);
4785 core_context->stats_get_handle =
4786 GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
4787 GNUNET_TIME_relative_get_forever (),
4788 &internal_stats_cont, &internal_stats_callback,
4790 if (core_context->stats_get_handle == NULL)
4791 internal_stats_cont (core_context, GNUNET_NO);
4796 struct DuplicateStats
4799 * Next item in the list
4801 struct DuplicateStats *next;
4804 * Nasty string, concatenation of relevant information.
4806 char *unique_string;
4810 * Check whether the combination of port/host/unix domain socket
4811 * already exists in the list of peers being checked for statistics.
4813 * @param pg the peergroup in question
4814 * @param specific_peer the peer we're concerned with
4815 * @param stats_list the list to return to the caller
4817 * @return GNUNET_YES if the statistics instance has been seen already,
4818 * GNUNET_NO if not (and we may have added it to the list)
4821 stats_check_existing (struct GNUNET_TESTING_PeerGroup *pg,
4822 struct PeerData *specific_peer,
4823 struct DuplicateStats **stats_list)
4825 struct DuplicateStats *pos;
4826 char *unix_domain_socket;
4827 unsigned long long port;
4830 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
4831 "single_statistics_per_host"))
4832 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
4836 GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg, "statistics",
4837 "unixpath", &unix_domain_socket))
4841 GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg, "statistics",
4844 GNUNET_free(unix_domain_socket);
4848 if (specific_peer->daemon->hostname != NULL)
4849 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
4850 unix_domain_socket, port);
4852 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
4856 if (0 == strcmp (to_match, pos->unique_string))
4858 GNUNET_free (unix_domain_socket);
4859 GNUNET_free (to_match);
4864 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
4865 pos->unique_string = to_match;
4866 pos->next = *stats_list;
4868 GNUNET_free (unix_domain_socket);
4873 * Iterate over all (running) peers in the peer group, retrieve
4874 * all statistics from each.
4877 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
4878 GNUNET_STATISTICS_Callback cont,
4879 GNUNET_TESTING_STATISTICS_Iterator proc,
4882 struct StatsIterateContext *stats_context;
4883 struct StatsCoreContext *core_ctx;
4885 unsigned int total_count;
4886 struct DuplicateStats *stats_list;
4887 struct DuplicateStats *pos;
4890 /* Allocate a single stats iteration context */
4891 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
4892 stats_context->cont = cont;
4893 stats_context->proc = proc;
4894 stats_context->cls = cls;
4895 stats_context->pg = pg;
4898 for (i = 0; i < pg->total; i++)
4900 if ((pg->peers[i].daemon->running == GNUNET_YES)
4902 stats_check_existing (pg, &pg->peers[i], &stats_list)))
4904 /* Allocate one core context per core we need to connect to */
4905 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
4906 core_ctx->daemon = pg->peers[i].daemon;
4907 /* Set back pointer to topology iteration context */
4908 core_ctx->iter_context = stats_context;
4909 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
4914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4915 "Retrieving stats from %u total instances.\n", total_count);
4916 stats_context->total = total_count;
4917 if (stats_list != NULL)
4922 GNUNET_free (pos->unique_string);
4923 stats_list = pos->next;
4925 pos = stats_list->next;
4932 * Stop the connection process temporarily.
4934 * @param pg the peer group to stop connecting
4936 void GNUNET_TESTING_stop_connections(struct GNUNET_TESTING_PeerGroup *pg)
4938 pg->stop_connects = GNUNET_YES;
4942 * Resume the connection process temporarily.
4944 * @param pg the peer group to resume connecting
4946 void GNUNET_TESTING_resume_connections(struct GNUNET_TESTING_PeerGroup *pg)
4948 pg->stop_connects = GNUNET_NO;
4952 * There are many ways to connect peers that are supported by this function.
4953 * To connect peers in the same topology that was created via the
4954 * GNUNET_TESTING_create_topology, the topology variable must be set to
4955 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
4956 * a new instance of that topology will be generated and attempted to be
4957 * connected. This could result in some connections being impossible,
4958 * because some topologies are non-deterministic.
4960 * @param pg the peer group struct representing the running peers
4961 * @param topology which topology to connect the peers in
4962 * @param options options for connecting the topology
4963 * @param option_modifier modifier for options that take a parameter
4964 * @param connect_timeout how long to wait before giving up on connecting
4966 * @param connect_attempts how many times to attempt to connect two peers
4967 * over the connect_timeout duration
4968 * @param notify_callback notification to be called once all connections completed
4969 * @param notify_cls closure for notification callback
4971 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
4974 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
4975 enum GNUNET_TESTING_Topology topology,
4976 enum GNUNET_TESTING_TopologyOption options,
4977 double option_modifier,
4978 struct GNUNET_TIME_Relative connect_timeout,
4979 unsigned int connect_attempts,
4980 GNUNET_TESTING_NotifyCompletion
4981 notify_callback, void *notify_cls)
4985 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4986 #if VERBOSE_TOPOLOGY
4987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4988 _("Creating clique CONNECT topology\n"));
4990 create_clique (pg, &add_connections, CONNECT);
4992 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4993 #if VERBOSE_TOPOLOGY
4994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4995 _("Creating small world (ring) CONNECT topology\n"));
4997 create_small_world_ring (pg, &add_connections, CONNECT);
4999 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
5000 #if VERBOSE_TOPOLOGY
5001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5002 _("Creating small world (2d-torus) CONNECT topology\n"));
5004 create_small_world (pg, &add_connections, CONNECT);
5006 case GNUNET_TESTING_TOPOLOGY_RING:
5007 #if VERBOSE_TOPOLOGY
5008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5009 _("Creating ring CONNECT topology\n"));
5011 create_ring (pg, &add_connections, CONNECT);
5013 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
5014 #if VERBOSE_TOPOLOGY
5015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5016 _("Creating 2d torus CONNECT topology\n"));
5018 create_2d_torus (pg, &add_connections, CONNECT);
5020 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
5021 #if VERBOSE_TOPOLOGY
5022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5023 _("Creating Erdos-Renyi CONNECT topology\n"));
5025 create_erdos_renyi (pg, &add_connections, CONNECT);
5027 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
5028 #if VERBOSE_TOPOLOGY
5029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5030 _("Creating InterNAT CONNECT topology\n"));
5032 create_nated_internet (pg, &add_connections, CONNECT);
5034 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
5035 #if VERBOSE_TOPOLOGY
5036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5037 _("Creating Scale Free CONNECT topology\n"));
5039 create_scale_free (pg, &add_connections, CONNECT);
5041 case GNUNET_TESTING_TOPOLOGY_LINE:
5042 #if VERBOSE_TOPOLOGY
5043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5044 _("Creating straight line CONNECT topology\n"));
5046 create_line (pg, &add_connections, CONNECT);
5048 case GNUNET_TESTING_TOPOLOGY_NONE:
5049 #if VERBOSE_TOPOLOGY
5050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5051 _("Creating no CONNECT topology\n"));
5053 copy_allowed_topology (pg);
5056 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5058 ("Unknown topology specification, can't connect peers!\n"));
5059 return GNUNET_SYSERR;
5064 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
5065 #if VERBOSE_TOPOLOGY
5066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5068 ("Connecting random subset (%'.2f percent) of possible peers\n"),
5069 100 * option_modifier);
5071 choose_random_connections (pg, option_modifier);
5073 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
5074 #if VERBOSE_TOPOLOGY
5075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5076 _("Connecting a minimum of %u peers each (if possible)\n"),
5077 (unsigned int) option_modifier);
5079 choose_minimum (pg, (unsigned int) option_modifier);
5081 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
5082 #if VERBOSE_TOPOLOGY
5083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5085 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
5086 (unsigned int) option_modifier);
5089 perform_dfs (pg, (int) option_modifier);
5092 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
5093 #if VERBOSE_TOPOLOGY
5094 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5096 ("Finding additional %u closest peers each (if possible)\n"),
5097 (unsigned int) option_modifier);
5100 add_closest (pg, (unsigned int) option_modifier,
5101 &add_connections, CONNECT);
5104 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
5106 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
5112 return connect_topology (pg, connect_timeout, connect_attempts, notify_callback, notify_cls);
5116 * Lookup and return the number of SSH connections to a host.
5118 * @param hostname the hostname to lookup in the list
5119 * @param pg the peergroup that the host belongs to
5121 * @return the number of current ssh connections to the host
5124 count_outstanding_at_host(const char *hostname, struct GNUNET_TESTING_PeerGroup *pg)
5126 struct OutstandingSSH *pos;
5128 while ((pos != NULL) && (strcmp(pos->hostname, hostname) != 0))
5130 GNUNET_assert(pos != NULL);
5131 return pos->outstanding;
5136 * Increment the number of SSH connections to a host by one.
5138 * @param hostname the hostname to lookup in the list
5139 * @param pg the peergroup that the host belongs to
5143 increment_outstanding_at_host(const char *hostname, struct GNUNET_TESTING_PeerGroup *pg)
5145 struct OutstandingSSH *pos;
5147 while ((pos != NULL) && (strcmp(pos->hostname, hostname) != 0))
5149 GNUNET_assert(pos != NULL);
5154 * Decrement the number of SSH connections to a host by one.
5156 * @param hostname the hostname to lookup in the list
5157 * @param pg the peergroup that the host belongs to
5161 decrement_outstanding_at_host(const char *hostname, struct GNUNET_TESTING_PeerGroup *pg)
5163 struct OutstandingSSH *pos;
5165 while ((pos != NULL) && (strcmp(pos->hostname, hostname) != 0))
5167 GNUNET_assert(pos != NULL);
5173 * Callback that is called whenever a hostkey is generated
5174 * for a peer. Call the real callback and decrement the
5175 * starting counter for the peergroup.
5177 * @param cls closure
5178 * @param id identifier for the daemon, NULL on error
5179 * @param d handle for the daemon
5180 * @param emsg error message (NULL on success)
5183 internal_hostkey_callback (void *cls,
5184 const struct GNUNET_PeerIdentity *id,
5185 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5187 struct InternalStartContext *internal_context = cls;
5188 internal_context->peer->pg->starting--;
5189 internal_context->peer->pg->started++;
5190 if (internal_context->hostname != NULL)
5191 decrement_outstanding_at_host(internal_context->hostname, internal_context->peer->pg);
5192 if (internal_context->hostkey_callback != NULL)
5193 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
5195 else if (internal_context->peer->pg->started ==
5196 internal_context->peer->pg->total)
5198 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
5199 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
5204 * Callback that is called whenever a peer has finished starting.
5205 * Call the real callback and decrement the starting counter
5206 * for the peergroup.
5208 * @param cls closure
5209 * @param id identifier for the daemon, NULL on error
5211 * @param d handle for the daemon
5212 * @param emsg error message (NULL on success)
5215 internal_startup_callback (void *cls,
5216 const struct GNUNET_PeerIdentity *id,
5217 const struct GNUNET_CONFIGURATION_Handle *cfg,
5218 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5220 struct InternalStartContext *internal_context = cls;
5221 internal_context->peer->pg->starting--;
5222 if (internal_context->hostname != NULL)
5223 decrement_outstanding_at_host(internal_context->hostname, internal_context->peer->pg);
5224 if (internal_context->start_cb != NULL)
5225 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
5230 internal_continue_startup (void *cls,
5231 const struct GNUNET_SCHEDULER_TaskContext *tc)
5233 struct InternalStartContext *internal_context = cls;
5235 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
5240 if ((internal_context->peer->pg->starting < internal_context->peer->pg->max_concurrent_ssh) ||
5241 ((internal_context->hostname != NULL) &&
5242 (count_outstanding_at_host(internal_context->hostname, internal_context->peer->pg) < internal_context->peer->pg->max_concurrent_ssh)))
5244 if (internal_context->hostname != NULL)
5245 increment_outstanding_at_host(internal_context->hostname, internal_context->peer->pg);
5246 internal_context->peer->pg->starting++;
5247 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
5251 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5252 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5253 &internal_continue_startup,
5260 * Callback for informing us about a successful
5261 * or unsuccessful churn start call.
5263 * @param cls a ChurnContext
5264 * @param id the peer identity of the started peer
5265 * @param cfg the handle to the configuration of the peer
5266 * @param d handle to the daemon for the peer
5267 * @param emsg NULL on success, non-NULL on failure
5271 churn_start_callback (void *cls,
5272 const struct GNUNET_PeerIdentity *id,
5273 const struct GNUNET_CONFIGURATION_Handle *cfg,
5274 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5276 struct ChurnRestartContext *startup_ctx = cls;
5277 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
5279 unsigned int total_left;
5280 char *error_message;
5282 error_message = NULL;
5285 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5286 "Churn stop callback failed with error `%s'\n", emsg);
5287 churn_ctx->num_failed_start++;
5291 churn_ctx->num_to_start--;
5295 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5296 "Started peer, %d left.\n", churn_ctx->num_to_start);
5300 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
5301 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5303 if (total_left == 0)
5305 if ((churn_ctx->num_failed_stop > 0)
5306 || (churn_ctx->num_failed_start > 0))
5307 GNUNET_asprintf (&error_message,
5308 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5309 churn_ctx->num_failed_start,
5310 churn_ctx->num_failed_stop);
5311 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5312 GNUNET_free_non_null (error_message);
5313 GNUNET_free (churn_ctx);
5314 GNUNET_free (startup_ctx);
5320 schedule_churn_restart (void *cls,
5321 const struct GNUNET_SCHEDULER_TaskContext *tc)
5323 struct PeerRestartContext *peer_restart_ctx = cls;
5324 struct ChurnRestartContext *startup_ctx =
5325 peer_restart_ctx->churn_restart_ctx;
5327 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5328 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5329 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5330 &schedule_churn_restart, peer_restart_ctx);
5333 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
5334 startup_ctx->timeout,
5335 &churn_start_callback,
5337 GNUNET_free (peer_restart_ctx);
5344 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5346 struct InternalStartContext *internal_context = cls;
5348 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
5353 if ((internal_context->peer->pg->starting < internal_context->peer->pg->max_concurrent_ssh) ||
5354 ((internal_context->hostname != NULL) &&
5355 (count_outstanding_at_host(internal_context->hostname, internal_context->peer->pg) < internal_context->peer->pg->max_concurrent_ssh)))
5357 if (internal_context->hostname != NULL)
5358 increment_outstanding_at_host(internal_context->hostname, internal_context->peer->pg);
5359 internal_context->peer->pg->starting++;
5360 internal_context->peer->daemon =
5361 GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
5362 internal_context->timeout,
5363 internal_context->hostname,
5364 internal_context->username,
5365 internal_context->sshport,
5366 internal_context->hostkey,
5367 &internal_hostkey_callback,
5369 &internal_startup_callback,
5374 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5375 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5376 &internal_start, internal_context);
5381 * Function which continues a peer group starting up
5382 * after successfully generating hostkeys for each peer.
5384 * @param pg the peer group to continue starting
5388 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg)
5393 for (i = 0; i < pg->total; i++)
5395 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5396 &pg->peers[i].internal_context);
5397 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
5402 * Start count gnunet instances with the same set of transports and
5403 * applications. The port numbers (any option called "PORT") will be
5404 * adjusted to ensure that no two peers running on the same system
5405 * have the same port(s) in their respective configurations.
5407 * @param cfg configuration template to use
5408 * @param total number of daemons to start
5409 * @param max_concurrent_connections for testing, how many peers can
5410 * we connect to simultaneously
5411 * @param max_concurrent_ssh when starting with ssh, how many ssh
5412 * connections will we allow at once (based on remote hosts allowed!)
5413 * @param timeout total time allowed for peers to start
5414 * @param hostkey_callback function to call on each peers hostkey generation
5415 * if NULL, peers will be started by this call, if non-null,
5416 * GNUNET_TESTING_daemons_continue_startup must be called after
5417 * successful hostkey generation
5418 * @param hostkey_cls closure for hostkey callback
5419 * @param cb function to call on each daemon that was started
5420 * @param cb_cls closure for cb
5421 * @param connect_callback function to call each time two hosts are connected
5422 * @param connect_callback_cls closure for connect_callback
5423 * @param hostnames linked list of host structs to use to start peers on
5424 * (NULL to run on localhost only)
5426 * @return NULL on error, otherwise handle to control peer group
5428 struct GNUNET_TESTING_PeerGroup *
5429 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
5431 unsigned int max_concurrent_connections,
5432 unsigned int max_concurrent_ssh,
5433 struct GNUNET_TIME_Relative timeout,
5434 GNUNET_TESTING_NotifyHostkeyCreated
5435 hostkey_callback, void *hostkey_cls,
5436 GNUNET_TESTING_NotifyDaemonRunning cb,
5438 GNUNET_TESTING_NotifyConnection
5439 connect_callback, void *connect_callback_cls,
5440 const struct GNUNET_TESTING_Host *hostnames)
5442 struct GNUNET_TESTING_PeerGroup *pg;
5443 const struct GNUNET_TESTING_Host *hostpos;
5449 const char *hostname;
5450 const char *username;
5451 char *baseservicehome;
5452 char *newservicehome;
5454 char *hostkeys_file;
5457 struct GNUNET_DISK_FileHandle *fd;
5458 struct GNUNET_CONFIGURATION_Handle *pcfg;
5460 unsigned int hostcnt;
5467 uint64_t total_hostkeys;
5468 struct GNUNET_OS_Process *proc;
5478 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
5480 pg->notify_connection = connect_callback;
5481 pg->notify_connection_cls = connect_callback_cls;
5483 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
5484 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
5485 pg->max_outstanding_connections = max_concurrent_connections;
5486 pg->max_concurrent_ssh = max_concurrent_ssh;
5487 if (NULL != hostnames)
5490 hostpos = hostnames;
5491 while (hostpos != NULL)
5493 hostpos = hostpos->next;
5496 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5499 hostpos = hostnames;
5500 while (hostpos != NULL)
5502 pg->hosts[off].minport = LOW_PORT;
5503 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
5504 if (hostpos->username != NULL)
5505 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
5506 pg->hosts[off].sshport = hostpos->port;
5507 hostpos = hostpos->next;
5517 pg->num_hosts = off;
5521 /* skip leading spaces */
5522 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
5525 while ('\0' != *rpos)
5527 if (isspace ((unsigned char) *rpos))
5531 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5533 start = GNUNET_strdup (hostnames);
5535 while ('\0' != *pos)
5537 if (isspace ((unsigned char) *pos))
5540 if (strlen (start) > 0)
5542 pg->hosts[off].minport = LOW_PORT;
5543 pg->hosts[off++].hostname = start;
5549 if (strlen (start) > 0)
5551 pg->hosts[off].minport = LOW_PORT;
5552 pg->hosts[off++].hostname = start;
5556 GNUNET_free (start);
5557 GNUNET_free (pg->hosts);
5561 minport = 0; /* make gcc happy */
5570 /* Create the servicehome directory for each remote peer */
5571 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
5573 for (i = 0; i < pg->num_hosts; i++)
5575 struct OutstandingSSH *ssh_entry;
5576 ssh_entry = GNUNET_malloc(sizeof(struct OutstandingSSH));
5577 ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
5578 GNUNET_CONTAINER_DLL_insert(pg->ssh_head, pg->ssh_tail, ssh_entry);
5579 if (NULL != pg->hosts[i].username)
5580 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username, pg->hosts[i].hostname);
5582 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
5583 if (pg->hosts[i].sshport != 0)
5585 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
5586 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
5587 "ssh", "-P", ssh_port_str,
5591 arg, "mkdir -p", baseservicehome, NULL);
5594 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
5595 "ssh", arg, "mkdir -p", baseservicehome, NULL);
5596 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating remote dir with command ssh %s %s %s\n", arg, " mkdir -p ", baseservicehome);
5597 GNUNET_OS_process_wait(proc);
5599 GNUNET_free(baseservicehome);
5601 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "HOSTKEYSFILE",
5604 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
5605 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!\n");
5608 /* Check hostkey file size, read entire thing into memory */
5609 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
5610 GNUNET_DISK_PERM_NONE);
5613 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
5617 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
5620 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Found file size %llu for hostkeys, expect hostkeys to be size %d\n", fs, HOSTKEYFILESIZE);
5622 if (fs % HOSTKEYFILESIZE != 0)
5624 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "File size %llu seems incorrect for hostkeys...\n", fs);
5628 total_hostkeys = fs / HOSTKEYFILESIZE;
5629 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Will read %llu hostkeys from file\n", total_hostkeys);
5630 pg->hostkey_data = GNUNET_malloc_large (fs);
5631 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
5632 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
5635 GNUNET_free(hostkeys_file);
5638 for (off = 0; off < total; off++)
5642 hostname = pg->hosts[off % hostcnt].hostname;
5643 username = pg->hosts[off % hostcnt].username;
5644 sshport = pg->hosts[off % hostcnt].sshport;
5645 pcfg = make_config (cfg,
5647 &pg->hosts[off % hostcnt].minport,
5648 &upnum, hostname, &fdnum);
5655 pcfg = make_config (cfg, off, &minport, &upnum, hostname, &fdnum);
5660 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5662 ("Could not create configuration for peer number %u on `%s'!\n"),
5663 off, hostname == NULL ? "localhost" : hostname);
5668 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
5671 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
5672 GNUNET_free (baseservicehome);
5676 tmpdir = getenv ("TMPDIR");
5677 tmpdir = tmpdir ? tmpdir : "/tmp";
5678 GNUNET_asprintf (&newservicehome,
5680 tmpdir, "gnunet-testing-test-test", off);
5682 GNUNET_CONFIGURATION_set_value_string (pcfg,
5684 "SERVICEHOME", newservicehome);
5685 GNUNET_free (newservicehome);
5686 pg->peers[off].cfg = pcfg;
5688 /* Can we do this later? */
5689 pg->peers[off].allowed_peers =
5690 GNUNET_CONTAINER_multihashmap_create (total);
5691 pg->peers[off].connect_peers =
5692 GNUNET_CONTAINER_multihashmap_create (total);
5693 pg->peers[off].blacklisted_peers =
5694 GNUNET_CONTAINER_multihashmap_create (total);
5697 pg->peers[off].pg = pg;
5698 pg->peers[off].internal_context.peer = &pg->peers[off];
5699 pg->peers[off].internal_context.timeout = timeout;
5700 pg->peers[off].internal_context.hostname = hostname;
5701 pg->peers[off].internal_context.username = username;
5702 pg->peers[off].internal_context.sshport = sshport;
5703 if (pg->hostkey_data != NULL)
5704 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off * HOSTKEYFILESIZE];
5705 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
5706 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
5707 pg->peers[off].internal_context.start_cb = cb;
5708 pg->peers[off].internal_context.start_cb_cls = cb_cls;
5710 GNUNET_SCHEDULER_add_now (&internal_start,
5711 &pg->peers[off].internal_context);
5718 * Get a daemon by number, so callers don't have to do nasty
5719 * offsetting operation.
5721 struct GNUNET_TESTING_Daemon *
5722 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
5723 unsigned int position)
5725 if (position < pg->total)
5726 return pg->peers[position].daemon;
5732 * Get a daemon by peer identity, so callers can
5733 * retrieve the daemon without knowing it's offset.
5735 * @param pg the peer group to retrieve the daemon from
5736 * @param peer_id the peer identity of the daemon to retrieve
5738 * @return the daemon on success, or NULL if no such peer identity is found
5740 struct GNUNET_TESTING_Daemon *
5741 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
5742 struct GNUNET_PeerIdentity *peer_id)
5746 for (i = 0; i < pg->total; i++)
5749 memcmp (&pg->peers[i].daemon->id, peer_id,
5750 sizeof (struct GNUNET_PeerIdentity)))
5751 return pg->peers[i].daemon;
5758 * Prototype of a function that will be called when a
5759 * particular operation was completed the testing library.
5761 * @param cls closure (a struct RestartContext)
5762 * @param id id of the peer that was restarted
5763 * @param cfg handle to the configuration of the peer
5764 * @param d handle to the daemon that was restarted
5765 * @param emsg NULL on success
5768 restart_callback (void *cls,
5769 const struct GNUNET_PeerIdentity *id,
5770 const struct GNUNET_CONFIGURATION_Handle *cfg,
5771 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5773 struct RestartContext *restart_context = cls;
5777 restart_context->peers_restarted++;
5781 restart_context->peers_restart_failed++;
5784 if (restart_context->peers_restarted == restart_context->peer_group->total)
5786 restart_context->callback (restart_context->callback_cls, NULL);
5787 GNUNET_free (restart_context);
5789 else if (restart_context->peers_restart_failed +
5790 restart_context->peers_restarted ==
5791 restart_context->peer_group->total)
5793 restart_context->callback (restart_context->callback_cls,
5794 "Failed to restart peers!");
5795 GNUNET_free (restart_context);
5801 * Callback for informing us about a successful
5802 * or unsuccessful churn stop call.
5804 * @param cls a ChurnContext
5805 * @param emsg NULL on success, non-NULL on failure
5809 churn_stop_callback (void *cls, const char *emsg)
5811 struct ShutdownContext *shutdown_ctx = cls;
5812 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
5813 unsigned int total_left;
5814 char *error_message;
5816 error_message = NULL;
5817 shutdown_ctx->outstanding--;
5821 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5822 "Churn stop callback failed with error `%s'\n", emsg);
5823 churn_ctx->num_failed_stop++;
5827 churn_ctx->num_to_stop--;
5831 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5832 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
5835 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
5836 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5838 if (total_left == 0)
5840 if ((churn_ctx->num_failed_stop > 0)
5841 || (churn_ctx->num_failed_start > 0))
5843 GNUNET_asprintf (&error_message,
5844 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5845 churn_ctx->num_failed_start,
5846 churn_ctx->num_failed_stop);
5848 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5849 GNUNET_free_non_null (error_message);
5850 GNUNET_free (churn_ctx);
5851 GNUNET_free (shutdown_ctx);
5856 * Count the number of running peers.
5858 * @param pg handle for the peer group
5860 * @return the number of currently running peers in the peer group
5863 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
5866 unsigned int running = 0;
5867 for (i = 0; i < pg->total; i++)
5869 if (pg->peers[i].daemon->running == GNUNET_YES)
5871 GNUNET_assert (running != -1);
5879 * Task to rate limit the number of outstanding peer shutdown
5880 * requests. This is necessary for making sure we don't do
5881 * too many ssh connections at once, but is generally nicer
5882 * to any system as well (graduated task starts, as opposed
5883 * to calling gnunet-arm N times all at once).
5886 schedule_churn_shutdown_task (void *cls,
5887 const struct GNUNET_SCHEDULER_TaskContext *tc)
5889 struct PeerShutdownContext *peer_shutdown_ctx = cls;
5890 struct ShutdownContext *shutdown_ctx;
5891 struct ChurnContext *churn_ctx;
5892 GNUNET_assert (peer_shutdown_ctx != NULL);
5893 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
5894 GNUNET_assert (shutdown_ctx != NULL);
5895 churn_ctx = (struct ChurnContext *)shutdown_ctx->cb_cls;
5896 if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
5897 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5898 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5899 &schedule_churn_shutdown_task,
5903 shutdown_ctx->outstanding++;
5904 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
5905 shutdown_ctx->timeout, shutdown_ctx->cb,
5906 shutdown_ctx, GNUNET_NO, GNUNET_YES);
5907 GNUNET_free (peer_shutdown_ctx);
5912 * Simulate churn by stopping some peers (and possibly
5913 * re-starting others if churn is called multiple times). This
5914 * function can only be used to create leave-join churn (peers "never"
5915 * leave for good). First "voff" random peers that are currently
5916 * online will be taken offline; then "von" random peers that are then
5917 * offline will be put back online. No notifications will be
5918 * generated for any of these operations except for the callback upon
5921 * @param pg handle for the peer group
5922 * @param voff number of peers that should go offline
5923 * @param von number of peers that should come back online;
5924 * must be zero on first call (since "testbed_start"
5925 * always starts all of the peers)
5926 * @param timeout how long to wait for operations to finish before
5928 * @param cb function to call at the end
5929 * @param cb_cls closure for cb
5932 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
5935 struct GNUNET_TIME_Relative timeout,
5936 GNUNET_TESTING_NotifyCompletion cb,
5939 struct ChurnContext *churn_ctx;
5940 struct ShutdownContext *shutdown_ctx;
5941 struct PeerShutdownContext *peer_shutdown_ctx;
5942 struct PeerRestartContext *peer_restart_ctx;
5943 struct ChurnRestartContext *churn_startup_ctx;
5945 unsigned int running;
5946 unsigned int stopped;
5947 unsigned int total_running;
5948 unsigned int total_stopped;
5950 unsigned int *running_arr;
5951 unsigned int *stopped_arr;
5952 unsigned int *running_permute;
5953 unsigned int *stopped_permute;
5958 if ((von == 0) && (voff == 0)) /* No peers at all? */
5964 for (i = 0; i < pg->total; i++)
5966 if (pg->peers[i].daemon->running == GNUNET_YES)
5968 GNUNET_assert (running != -1);
5973 GNUNET_assert (stopped != -1);
5980 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5981 "Trying to stop more peers than are currently running!\n");
5982 cb (cb_cls, "Trying to stop more peers than are currently running!");
5988 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5989 "Trying to start more peers than are currently stopped!\n");
5990 cb (cb_cls, "Trying to start more peers than are currently stopped!");
5994 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
5998 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
6002 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
6004 running_permute = NULL;
6005 stopped_permute = NULL;
6009 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, running);
6012 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, stopped);
6014 total_running = running;
6015 total_stopped = stopped;
6019 churn_ctx->num_to_start = von;
6020 churn_ctx->num_to_stop = voff;
6022 churn_ctx->cb_cls = cb_cls;
6025 for (i = 0; i < pg->total; i++)
6027 if (pg->peers[i].daemon->running == GNUNET_YES)
6029 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6030 running_arr[running] = i;
6035 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6036 stopped_arr[stopped] = i;
6041 GNUNET_assert (running >= voff);
6044 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6045 shutdown_ctx->cb = &churn_stop_callback;
6046 shutdown_ctx->cb_cls = churn_ctx;
6047 shutdown_ctx->total_peers = voff;
6048 shutdown_ctx->timeout = timeout;
6051 for (i = 0; i < voff; i++)
6054 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
6055 running_permute[i]);
6057 GNUNET_assert (running_arr != NULL);
6058 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6059 peer_shutdown_ctx->daemon =
6060 pg->peers[running_arr[running_permute[i]]].daemon;
6061 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6062 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
6066 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
6068 &churn_stop_callback, churn_ctx,
6069 GNUNET_NO, GNUNET_YES); */
6072 GNUNET_assert (stopped >= von);
6075 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6076 churn_startup_ctx->churn_ctx = churn_ctx;
6077 churn_startup_ctx->timeout = timeout;
6078 churn_startup_ctx->pg = pg;
6080 for (i = 0; i < von; i++)
6083 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
6084 stopped_permute[i]);
6086 GNUNET_assert (stopped_arr != NULL);
6087 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
6088 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
6089 peer_restart_ctx->daemon =
6090 pg->peers[stopped_arr[stopped_permute[i]]].daemon;
6091 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
6093 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
6094 timeout, &churn_start_callback, churn_ctx); */
6097 GNUNET_free_non_null (running_arr);
6098 GNUNET_free_non_null (stopped_arr);
6099 GNUNET_free_non_null (running_permute);
6100 GNUNET_free_non_null (stopped_permute);
6105 * Restart all peers in the given group.
6107 * @param pg the handle to the peer group
6108 * @param callback function to call on completion (or failure)
6109 * @param callback_cls closure for the callback function
6112 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
6113 GNUNET_TESTING_NotifyCompletion callback,
6116 struct RestartContext *restart_context;
6121 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
6122 restart_context->peer_group = pg;
6123 restart_context->peers_restarted = 0;
6124 restart_context->callback = callback;
6125 restart_context->callback_cls = callback_cls;
6127 for (off = 0; off < pg->total; off++)
6129 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
6130 &restart_callback, restart_context);
6136 * Start or stop an individual peer from the given group.
6138 * @param pg handle to the peer group
6139 * @param offset which peer to start or stop
6140 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
6141 * @param timeout how long to wait for shutdown
6142 * @param cb function to call at the end
6143 * @param cb_cls closure for cb
6146 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
6147 unsigned int offset,
6149 struct GNUNET_TIME_Relative timeout,
6150 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6152 struct ShutdownContext *shutdown_ctx;
6153 struct ChurnRestartContext *startup_ctx;
6154 struct ChurnContext *churn_ctx;
6156 if (GNUNET_NO == desired_status)
6158 if (NULL != pg->peers[offset].daemon)
6160 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6161 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6162 churn_ctx->num_to_start = 0;
6163 churn_ctx->num_to_stop = 1;
6165 churn_ctx->cb_cls = cb_cls;
6166 shutdown_ctx->cb_cls = churn_ctx;
6167 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon,
6168 timeout, &churn_stop_callback,
6169 shutdown_ctx, GNUNET_NO, GNUNET_YES);
6172 else if (GNUNET_YES == desired_status)
6174 if (NULL == pg->peers[offset].daemon)
6176 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6177 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6178 churn_ctx->num_to_start = 1;
6179 churn_ctx->num_to_stop = 0;
6181 churn_ctx->cb_cls = cb_cls;
6182 startup_ctx->churn_ctx = churn_ctx;
6183 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
6184 timeout, &churn_start_callback,
6194 * Callback for shutting down peers in a peer group.
6196 * @param cls closure (struct ShutdownContext)
6197 * @param emsg NULL on success
6200 internal_shutdown_callback (void *cls, const char *emsg)
6202 struct ShutdownContext *shutdown_ctx = cls;
6205 shutdown_ctx->outstanding--;
6208 shutdown_ctx->peers_down++;
6212 shutdown_ctx->peers_failed++;
6215 if ((shutdown_ctx->cb != NULL)
6216 && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed ==
6217 shutdown_ctx->total_peers))
6219 if (shutdown_ctx->peers_failed > 0)
6220 shutdown_ctx->cb (shutdown_ctx->cb_cls,
6221 "Not all peers successfully shut down!");
6223 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
6225 GNUNET_free (shutdown_ctx->pg->peers);
6226 GNUNET_free_non_null(shutdown_ctx->pg->hostkey_data);
6227 for (off = 0; off < shutdown_ctx->pg->num_hosts; off++)
6229 GNUNET_free (shutdown_ctx->pg->hosts[off].hostname);
6230 GNUNET_free_non_null (shutdown_ctx->pg->hosts[off].username);
6232 GNUNET_free_non_null (shutdown_ctx->pg->hosts);
6233 GNUNET_free (shutdown_ctx->pg);
6235 GNUNET_free (shutdown_ctx);
6241 * Task to rate limit the number of outstanding peer shutdown
6242 * requests. This is necessary for making sure we don't do
6243 * too many ssh connections at once, but is generally nicer
6244 * to any system as well (graduated task starts, as opposed
6245 * to calling gnunet-arm N times all at once).
6248 schedule_shutdown_task (void *cls,
6249 const struct GNUNET_SCHEDULER_TaskContext *tc)
6251 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6252 struct ShutdownContext *shutdown_ctx;
6254 GNUNET_assert (peer_shutdown_ctx != NULL);
6255 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6256 GNUNET_assert (shutdown_ctx != NULL);
6258 if (shutdown_ctx->outstanding > shutdown_ctx->pg->max_concurrent_ssh)
6259 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
6260 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
6261 &schedule_shutdown_task, peer_shutdown_ctx);
6264 shutdown_ctx->outstanding++;
6265 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6266 shutdown_ctx->timeout,
6267 &internal_shutdown_callback, shutdown_ctx,
6268 GNUNET_YES, GNUNET_NO);
6269 GNUNET_free (peer_shutdown_ctx);
6274 * Shutdown all peers started in the given group.
6276 * @param pg handle to the peer group
6277 * @param timeout how long to wait for shutdown
6278 * @param cb callback to notify upon success or failure
6279 * @param cb_cls closure for cb
6282 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
6283 struct GNUNET_TIME_Relative timeout,
6284 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6287 struct ShutdownContext *shutdown_ctx;
6288 struct PeerShutdownContext *peer_shutdown_ctx;
6290 struct PeerConnection *conn_iter;
6291 struct PeerConnection *temp_conn;
6294 GNUNET_assert (pg->total > 0);
6296 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6297 shutdown_ctx->cb = cb;
6298 shutdown_ctx->cb_cls = cb_cls;
6299 shutdown_ctx->total_peers = pg->total;
6300 shutdown_ctx->timeout = timeout;
6301 shutdown_ctx->pg = pg;
6302 /* shtudown_ctx->outstanding = 0; */
6304 for (off = 0; off < pg->total; off++)
6306 GNUNET_assert (NULL != pg->peers[off].daemon);
6307 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6308 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
6309 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6310 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
6312 if (NULL != pg->peers[off].cfg)
6313 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
6315 conn_iter = pg->peers[off].allowed_peers_head;
6316 while (conn_iter != NULL)
6318 temp_conn = conn_iter->next;
6319 GNUNET_free(conn_iter);
6320 conn_iter = temp_conn;
6323 conn_iter = pg->peers[off].connect_peers_head;
6324 while (conn_iter != NULL)
6326 temp_conn = conn_iter->next;
6327 GNUNET_free(conn_iter);
6328 conn_iter = temp_conn;
6331 conn_iter = pg->peers[off].blacklisted_peers_head;
6332 while (conn_iter != NULL)
6334 temp_conn = conn_iter->next;
6335 GNUNET_free(conn_iter);
6336 conn_iter = temp_conn;
6339 conn_iter = pg->peers[off].connect_peers_working_set_head;
6340 while (conn_iter != NULL)
6342 temp_conn = conn_iter->next;
6343 GNUNET_free(conn_iter);
6344 conn_iter = temp_conn;
6347 if (pg->peers[off].allowed_peers != NULL)
6348 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
6349 if (pg->peers[off].connect_peers != NULL)
6350 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
6351 if (pg->peers[off].blacklisted_peers != NULL)
6352 GNUNET_CONTAINER_multihashmap_destroy (pg->
6353 peers[off].blacklisted_peers);
6360 /* end of testing_group.c */