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
28 #include "gnunet_constants.h"
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_NO
37 #define DEBUG_CHURN GNUNET_EXTRA_LOGGING
39 #define USE_START_HELPER GNUNET_YES
43 /* Before connecting peers, send all of the HELLOs */
44 #define USE_SEND_HELLOS GNUNET_NO
46 #define TOPOLOGY_HACK GNUNET_YES
50 * Lowest port used for GNUnet testing. Should be high enough to not
51 * conflict with other applications running on the hosts but be low
52 * enough to not conflict with client-ports (typically starting around
55 #define LOW_PORT 12000
58 * Highest port used for GNUnet testing. Should be low enough to not
59 * conflict with the port range for "local" ports (client apps; see
60 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
62 #define HIGH_PORT 56000
64 /* Maximum time to delay connect attempt */
65 #define MAX_CONNECT_DELAY 300
68 * Which list of peers do we need to modify?
72 /** Modify allowed peers */
75 /** Modify connect peers */
78 /** Modify blacklist peers */
81 /** Modify workingset peers */
86 * Prototype of a function called whenever two peers would be connected
87 * in a certain topology.
89 typedef unsigned int (*GNUNET_TESTING_ConnectionProcessor) (struct
90 GNUNET_TESTING_PeerGroup
98 * Context for handling churning a peer group
103 * The peergroup we are dealing with.
105 struct GNUNET_TESTING_PeerGroup *pg;
108 * Name of the service to churn on/off, NULL
109 * to churn entire peer.
114 * Callback used to notify of churning finished
116 GNUNET_TESTING_NotifyCompletion cb;
119 * Closure for callback
124 * Number of peers that still need to be started
126 unsigned int num_to_start;
129 * Number of peers that still need to be stopped
131 unsigned int num_to_stop;
134 * Number of peers that failed to start
136 unsigned int num_failed_start;
139 * Number of peers that failed to stop
141 unsigned int num_failed_stop;
144 struct RestartContext
147 * The group of peers being restarted
149 struct GNUNET_TESTING_PeerGroup *peer_group;
152 * How many peers have been restarted thus far
154 unsigned int peers_restarted;
157 * How many peers got an error when restarting
159 unsigned int peers_restart_failed;
162 * The function to call once all peers have been restarted
164 GNUNET_TESTING_NotifyCompletion callback;
167 * Closure for callback function
173 struct SendHelloContext
176 * Global handle to the peer group.
178 struct GNUNET_TESTING_PeerGroup *pg;
181 * The data about this specific peer.
183 struct PeerData *peer;
186 * The next HELLO that needs sent to this peer.
188 struct PeerConnection *peer_pos;
191 * Are we connected to CORE yet?
193 unsigned int core_ready;
196 * How many attempts should we make for failed connections?
198 unsigned int connect_attempts;
201 * Task for scheduling core connect requests to be sent.
203 GNUNET_SCHEDULER_TaskIdentifier core_connect_task;
206 struct ShutdownContext
208 struct GNUNET_TESTING_PeerGroup *pg;
210 * Total peers to wait for
212 unsigned int total_peers;
215 * Number of peers successfully shut down
217 unsigned int peers_down;
220 * Number of peers failed to shut down
222 unsigned int peers_failed;
225 * Number of peers we have started shutting
226 * down. If too many, wait on them.
228 unsigned int outstanding;
231 * Timeout for shutdown.
233 struct GNUNET_TIME_Relative timeout;
236 * Callback to call when all peers either
237 * shutdown or failed to shutdown
239 GNUNET_TESTING_NotifyCompletion cb;
247 * Should we delete all of the files from the peers?
253 * Individual shutdown context for a particular peer.
255 struct PeerShutdownContext
258 * Pointer to the high level shutdown context.
260 struct ShutdownContext *shutdown_ctx;
263 * The daemon handle for the peer to shut down.
265 struct GNUNET_TESTING_Daemon *daemon;
269 * Individual shutdown context for a particular peer.
271 struct PeerRestartContext
274 * Pointer to the high level restart context.
276 struct ChurnRestartContext *churn_restart_ctx;
279 * The daemon handle for the peer to shut down.
281 struct GNUNET_TESTING_Daemon *daemon;
284 struct ServiceStartContext
286 struct GNUNET_TESTING_PeerGroup *pg;
287 unsigned int remaining;
288 GNUNET_TESTING_NotifyCompletion cb;
289 unsigned int outstanding;
291 struct GNUNET_TIME_Relative timeout;
296 * Individual shutdown context for a particular peer.
298 struct PeerServiceStartContext
301 * Pointer to the high level start context.
303 struct ServiceStartContext *start_ctx;
306 * The daemon handle for the peer to start the service on.
308 struct GNUNET_TESTING_Daemon *daemon;
311 struct CreateTopologyContext
315 * Function to call with number of connections
317 GNUNET_TESTING_NotifyConnections cont;
320 * Closure for connection notification
327 /** Waiting to read number of peers */
330 /** Should find next peer index */
333 /** Should find colon */
336 /** Should read other peer index, space, or endline */
341 struct PeerConnection
346 struct PeerConnection *prev;
351 struct PeerConnection *next;
354 * Index of daemon in pg->peers
361 struct InternalStartContext
364 * Pointer to peerdata
366 struct PeerData *peer;
369 * Timeout for peer startup
371 struct GNUNET_TIME_Relative timeout;
374 * Client callback for hostkey notification
376 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
379 * Closure for hostkey_callback
384 * Client callback for peer start notification
386 GNUNET_TESTING_NotifyDaemonRunning start_cb;
394 * Hostname, where to start the peer
396 const char *hostname;
399 * Username to use when connecting to the
402 const char *username;
405 * Pointer to starting memory location of a hostkey
410 * Port to use for ssh.
416 struct ChurnRestartContext
419 * PeerGroup that we are working with.
421 struct GNUNET_TESTING_PeerGroup *pg;
424 * Number of restarts currently in flight.
426 unsigned int outstanding;
429 * Handle to the underlying churn context.
431 struct ChurnContext *churn_ctx;
434 * How long to allow the operation to take.
436 struct GNUNET_TIME_Relative timeout;
439 struct OutstandingSSH
441 struct OutstandingSSH *next;
443 struct OutstandingSSH *prev;
446 * Number of current ssh connections.
448 uint32_t outstanding;
451 * The hostname of this peer.
453 const char *hostname;
457 * Data we keep per peer.
462 * (Initial) configuration of the host.
463 * (initial because clients could change
464 * it and we would not know about those
467 struct GNUNET_CONFIGURATION_Handle *cfg;
470 * Handle for controlling the daemon.
472 struct GNUNET_TESTING_Daemon *daemon;
475 * The peergroup this peer belongs to.
477 struct GNUNET_TESTING_PeerGroup *pg;
481 * Linked list of allowed peer connections.
483 struct PeerConnection *allowed_peers_head;
486 * Linked list of allowed peer connections.
488 struct PeerConnection *allowed_peers_tail;
491 * Linked list of blacklisted peer connections.
493 struct PeerConnection *blacklisted_peers_head;
496 * Linked list of blacklisted peer connections.
498 struct PeerConnection *blacklisted_peers_tail;
501 * Linked list of connect peer connections.
503 struct PeerConnection *connect_peers_head;
506 * Linked list of connect peer connections.
508 struct PeerConnection *connect_peers_tail;
511 * Linked list of connect peer connections.
513 struct PeerConnection *connect_peers_working_set_head;
516 * Linked list of connect peer connections.
518 struct PeerConnection *connect_peers_working_set_tail;
522 * Hash map of allowed peer connections (F2F created topology)
524 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
527 * Hash map of blacklisted peers
529 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
532 * Hash map of peer connections
534 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
537 * Temporary hash map of peer connections
539 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
543 * Temporary variable for topology creation, should be reset before
544 * creating any topology so the count is valid once finished.
549 * Context to keep track of peers being started, to
550 * stagger hostkey generation and peer startup.
552 struct InternalStartContext internal_context;
555 * Task ID for the queued internal_continue_startup task
557 GNUNET_SCHEDULER_TaskIdentifier startup_task;
562 * Linked list of per-host data.
572 * SSH username to use when connecting to this host.
577 * SSH port to use when connecting to this host.
582 * Lowest port that we have not yet used
588 struct TopologyIterateContext
591 * The peergroup we are working with.
593 struct GNUNET_TESTING_PeerGroup *pg;
596 * Callback for notifying of two connected peers.
598 GNUNET_TESTING_NotifyTopology topology_cb;
601 * Closure for topology_cb
606 * Number of peers currently connected to.
608 unsigned int connected;
611 * Number of peers we have finished iterating.
613 unsigned int completed;
616 * Number of peers total.
621 struct StatsIterateContext
624 * The peergroup that we are dealing with.
626 struct GNUNET_TESTING_PeerGroup *pg;
629 * Continuation to call once all stats information has been retrieved.
631 GNUNET_STATISTICS_Callback cont;
634 * Proc function to call on each value received.
636 GNUNET_TESTING_STATISTICS_Iterator proc;
639 * Closure for topology_cb
644 * Number of peers currently connected to.
646 unsigned int connected;
649 * Number of peers we have finished iterating.
651 unsigned int completed;
654 * Number of peers total.
662 struct GNUNET_TESTING_Daemon *daemon;
665 struct StatsCoreContext
668 struct GNUNET_TESTING_Daemon *daemon;
670 * Handle to the statistics service.
672 struct GNUNET_STATISTICS_Handle *stats_handle;
675 * Handle for getting statistics.
677 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
680 struct ConnectTopologyContext
683 * How many connections are left to create.
685 unsigned int remaining_connections;
688 * Handle to group of peers.
690 struct GNUNET_TESTING_PeerGroup *pg;
693 * How long to try this connection before timing out.
695 struct GNUNET_TIME_Relative connect_timeout;
698 * How many times to retry connecting the two peers.
700 unsigned int connect_attempts;
703 * Temp value set for each iteration.
705 //struct PeerData *first;
708 * Notification that all peers are connected.
710 GNUNET_TESTING_NotifyCompletion notify_connections_done;
713 * Closure for notify.
718 struct ConnectContext;
721 * Handle to a group of GNUnet peers.
723 struct GNUNET_TESTING_PeerGroup
726 * Configuration template.
728 const struct GNUNET_CONFIGURATION_Handle *cfg;
730 struct ConnectContext *cc_head;
732 struct ConnectContext *cc_tail;
735 * Function to call on each started daemon.
737 //GNUNET_TESTING_NotifyDaemonRunning cb;
745 * Function to call on each topology connection created
747 GNUNET_TESTING_NotifyConnection notify_connection;
750 * Callback for notify_connection
752 void *notify_connection_cls;
755 * Array of information about hosts.
757 struct HostData *hosts;
760 * Number of hosts (size of HostData)
762 unsigned int num_hosts;
765 * Array of "total" peers.
767 struct PeerData *peers;
770 * Number of peers in this group.
775 * At what time should we fail the peer startup process?
777 struct GNUNET_TIME_Absolute max_timeout;
780 * How many peers are being started right now?
782 unsigned int starting;
785 * How many peers have already been started?
787 unsigned int started;
790 * Number of possible connections to peers
793 unsigned int max_outstanding_connections;
796 * Number of ssh connections to peers (max).
798 unsigned int max_concurrent_ssh;
801 * Number of connects we are waiting on, allows us to rate limit
804 unsigned int outstanding_connects;
807 * Number of HELLOs we have yet to send.
809 unsigned int remaining_hellos;
812 * How many connects have already been scheduled?
814 unsigned int total_connects_scheduled;
817 * Hostkeys loaded from a file.
822 * Head of DLL to keep track of the number of outstanding
823 * ssh connections per peer.
825 struct OutstandingSSH *ssh_head;
828 * Tail of DLL to keep track of the number of outstanding
829 * ssh connections per peer.
831 struct OutstandingSSH *ssh_tail;
834 * Stop scheduling peers connecting.
836 unsigned int stop_connects;
839 * Connection context for peer group.
841 struct ConnectTopologyContext ct_ctx;
847 * The altered configuration.
849 struct GNUNET_CONFIGURATION_Handle *ret;
852 * The original configuration to alter.
854 const struct GNUNET_CONFIGURATION_Handle *orig;
857 * The hostname that this peer will run on.
859 const char *hostname;
862 * The next possible port to assign.
867 * Unique number for unix domain sockets.
872 * Unique number for this peer/host to offset
873 * things that are grouped by host.
878 struct ConnectContext
881 struct ConnectContext *next;
883 struct ConnectContext *prev;
886 * Index of peer to connect second to.
888 uint32_t first_index;
891 * Index of peer to connect first to.
893 uint32_t second_index;
896 * Task associated with the attempt to connect.
898 GNUNET_SCHEDULER_TaskIdentifier task;
901 * Context in 'testing.c', to cancel connection attempt.
903 struct GNUNET_TESTING_ConnectContext *cc;
906 * Higher level topology connection context.
908 struct ConnectTopologyContext *ct_ctx;
911 * Whether this connection has been accounted for in the schedule_connect call.
916 struct UnblacklistContext
921 struct GNUNET_TESTING_PeerGroup *pg;
924 * uid of the first peer
934 struct GNUNET_TESTING_PeerGroup *pg;
937 * uid of the first peer
942 * Peer data for first peer.
944 struct PeerData *first;
947 * Random percentage to use
952 struct MinimumContext
957 struct GNUNET_TESTING_PeerGroup *pg;
960 * uid of the first peer
965 * Peer data for first peer.
967 struct PeerData *first;
970 * Number of conns per peer
972 unsigned int num_to_add;
975 * Permuted array of all possible connections. Only add the Nth
976 * peer if it's in the Nth position.
978 unsigned int *pg_array;
981 * What number is the current element we are iterating over?
983 unsigned int current;
991 struct GNUNET_TESTING_PeerGroup *pg;
994 * uid of the first peer
999 * uid of the second peer
1001 uint32_t second_uid;
1004 * Peer data for first peer.
1006 struct PeerData *first;
1009 * Which peer has been chosen as the one to add?
1011 unsigned int chosen;
1014 * What number is the current element we are iterating over?
1016 unsigned int current;
1020 * Simple struct to keep track of progress, and print a
1021 * nice little percentage meter for long running tasks.
1023 struct ProgressMeter
1027 unsigned int modnum;
1029 unsigned int dotnum;
1031 unsigned int completed;
1035 char *startup_string;
1040 * Convert unique ID to hash code.
1042 * @param uid unique ID to convert
1043 * @param hash set to uid (extended with zeros)
1046 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
1048 memset (hash, 0, sizeof (GNUNET_HashCode));
1049 *((uint32_t *) hash) = uid;
1053 * Convert hash code to unique ID.
1055 * @param uid unique ID to convert
1056 * @param hash set to uid (extended with zeros)
1059 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
1061 memcpy (uid, hash, sizeof (uint32_t));
1066 static struct GNUNET_CORE_MessageHandler no_handlers[] = {
1072 * Create a meter to keep track of the progress of some task.
1074 * @param total the total number of items to complete
1075 * @param start_string a string to prefix the meter with (if printing)
1076 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
1079 * @return the progress meter
1081 static struct ProgressMeter *
1082 create_meter (unsigned int total, char *start_string, int print)
1084 struct ProgressMeter *ret;
1086 ret = GNUNET_malloc (sizeof (struct ProgressMeter));
1089 ret->modnum = total / 4;
1090 if (ret->modnum == 0) /* Divide by zero check */
1092 ret->dotnum = (total / 50) + 1;
1093 if (start_string != NULL)
1094 ret->startup_string = GNUNET_strdup (start_string);
1096 ret->startup_string = GNUNET_strdup ("");
1102 * Update progress meter (increment by one).
1104 * @param meter the meter to update and print info for
1106 * @return GNUNET_YES if called the total requested,
1107 * GNUNET_NO if more items expected
1110 update_meter (struct ProgressMeter *meter)
1112 if (meter->print == GNUNET_YES)
1114 if (meter->completed % meter->modnum == 0)
1116 if (meter->completed == 0)
1118 FPRINTF (stdout, "%sProgress: [0%%", meter->startup_string);
1121 FPRINTF (stdout, "%d%%",
1122 (int) (((float) meter->completed / meter->total) * 100));
1124 else if (meter->completed % meter->dotnum == 0)
1125 FPRINTF (stdout, "%s", ".");
1127 if (meter->completed + 1 == meter->total)
1128 FPRINTF (stdout, "%d%%]\n", 100);
1133 if (meter->completed == meter->total)
1135 if (meter->completed > meter->total)
1136 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
1141 * Reset progress meter.
1143 * @param meter the meter to reset
1145 * @return GNUNET_YES if meter reset,
1146 * GNUNET_SYSERR on error
1149 reset_meter (struct ProgressMeter *meter)
1152 return GNUNET_SYSERR;
1154 meter->completed = 0;
1159 * Release resources for meter
1161 * @param meter the meter to free
1164 free_meter (struct ProgressMeter *meter)
1166 GNUNET_free_non_null (meter->startup_string);
1167 GNUNET_free (meter);
1171 * Get a topology from a string input.
1173 * @param topology where to write the retrieved topology
1174 * @param topology_string The string to attempt to
1175 * get a configuration value from
1176 * @return GNUNET_YES if topology string matched a
1177 * known topology, GNUNET_NO if not
1180 GNUNET_TESTING_topology_get (enum GNUNET_TESTING_Topology *topology,
1181 const char *topology_string)
1184 * Strings representing topologies in enum
1186 static const char *topology_strings[] = {
1188 * A clique (everyone connected to everyone else).
1193 * Small-world network (2d torus plus random links).
1198 * Small-world network (ring plus random links).
1218 * Certain percentage of peers are unable to communicate directly
1219 * replicating NAT conditions
1224 * Scale free topology.
1229 * Straight line topology.
1234 * All peers are disconnected.
1239 * Read the topology from a file.
1248 if (topology_string == NULL)
1250 while (topology_strings[curr] != NULL)
1252 if (strcasecmp (topology_strings[curr], topology_string) == 0)
1259 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
1264 * Get connect topology option from string input.
1266 * @param topology_option where to write the retrieved topology
1267 * @param topology_string The string to attempt to
1268 * get a configuration value from
1269 * @return GNUNET_YES if string matched a known
1270 * topology option, GNUNET_NO if not
1273 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption
1275 const char *topology_string)
1278 * Options for connecting a topology as strings.
1280 static const char *topology_option_strings[] = {
1282 * Try to connect all peers specified in the topology.
1287 * Choose a random subset of connections to create.
1289 "CONNECT_RANDOM_SUBSET",
1292 * Create at least X connections for each peer.
1297 * Using a depth first search, create one connection
1298 * per peer. If any are missed (graph disconnected)
1299 * start over at those peers until all have at least one
1305 * Find the N closest peers to each allowed peer in the
1306 * topology and make sure a connection to those peers
1307 * exists in the connect topology.
1312 * No options specified.
1320 if (topology_string == NULL)
1322 while (NULL != topology_option_strings[curr])
1324 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1326 *topology_option = curr;
1331 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1336 * Function to iterate over options. Copies
1337 * the options to the target configuration,
1338 * updating PORT values as needed.
1340 * @param cls closure
1341 * @param section name of the section
1342 * @param option name of the option
1343 * @param value value of the option
1346 update_config (void *cls, const char *section, const char *option,
1349 struct UpdateContext *ctx = cls;
1353 char *single_variable;
1354 char *per_host_variable;
1355 unsigned long long num_per_host;
1357 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1358 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1360 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1364 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1367 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
1370 else if ((ival != 0) &&
1372 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1373 single_variable)) &&
1374 GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1378 GNUNET_snprintf (cval, sizeof (cval), "%u",
1379 ival + ctx->fdnum % num_per_host);
1383 /* FIXME: REMOVE FOREVER HACK HACK HACK */
1384 if (0 == strcasecmp (section, "transport-tcp"))
1385 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section,
1386 "ADVERTISED_PORT", value);
1389 if (0 == strcmp (option, "UNIXPATH"))
1392 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1395 GNUNET_snprintf (uval, sizeof (uval), "/tmp/test-service-%s-%u", section,
1399 else if ((GNUNET_YES ==
1400 GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1406 GNUNET_snprintf (uval, sizeof (uval), "/tmp/test-service-%s-%u", section,
1407 ctx->fdnum % num_per_host);
1412 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1414 value = ctx->hostname;
1416 GNUNET_free (single_variable);
1417 GNUNET_free (per_host_variable);
1418 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1422 * Create a new configuration using the given configuration
1423 * as a template; however, each PORT in the existing cfg
1424 * must be renumbered by incrementing "*port". If we run
1425 * out of "*port" numbers, return NULL.
1427 * @param cfg template configuration
1428 * @param off the current peer offset
1429 * @param port port numbers to use, update to reflect
1430 * port numbers that were used
1431 * @param upnum number to make unix domain socket names unique
1432 * @param hostname hostname of the controlling host, to allow control connections from
1433 * @param fdnum number used to offset the unix domain socket for grouped processes
1434 * (such as statistics or peerinfo, which can be shared among others)
1436 * @return new configuration, NULL on error
1438 struct GNUNET_CONFIGURATION_Handle *
1439 GNUNET_TESTING_create_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t off,
1440 uint16_t * port, uint32_t * upnum, const char *hostname,
1443 struct UpdateContext uc;
1446 char *allowed_hosts;
1447 unsigned long long skew_variance;
1448 unsigned long long skew_offset;
1449 long long actual_offset;
1455 uc.ret = GNUNET_CONFIGURATION_create ();
1456 uc.hostname = hostname;
1459 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1460 if (uc.nport >= HIGH_PORT)
1463 GNUNET_CONFIGURATION_destroy (uc.ret);
1468 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "skew_variance",
1470 (skew_variance > 0))
1473 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1476 skew_offset - GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1478 /* Min is -skew_variance, Max is skew_variance */
1479 skew_offset = skew_variance + actual_offset; /* Normal distribution around 0 */
1480 GNUNET_CONFIGURATION_set_value_number (uc.ret, "testing", "skew_offset",
1484 if (GNUNET_CONFIGURATION_get_value_string
1485 (cfg, "testing", "control_host", &control_host) == GNUNET_OK)
1487 if (hostname != NULL)
1488 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1491 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1493 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1496 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nse", "ACCEPT_FROM",
1499 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "ACCEPT_FROM",
1501 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1503 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "ACCEPT_FROM",
1506 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH", "");
1507 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH", "");
1508 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH", "");
1509 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1511 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nse", "UNIXPATH", "");
1513 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1514 "USE_LOCALADDR", "YES");
1515 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1516 "USE_LOCALADDR", "YES");
1517 GNUNET_free_non_null (control_host);
1518 GNUNET_free (allowed_hosts);
1521 /* arm needs to know to allow connections from the host on which it is running,
1522 * otherwise gnunet-arm is unable to connect to it in some instances */
1523 if (hostname != NULL)
1525 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1526 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "BINDTO", hostname);
1527 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "INTERNAL_ADDRESS",
1529 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "EXTERNAL_ADDRESS",
1531 GNUNET_CONFIGURATION_set_value_string (uc.ret, "disablev6", "BINDTO",
1533 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1534 "USE_LOCALADDR", "YES");
1535 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1536 "USE_LOCALADDR", "YES");
1537 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1539 GNUNET_free (allowed_hosts);
1544 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1545 "USE_LOCALADDR", "YES");
1546 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1547 "USE_LOCALADDR", "YES");
1548 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "BINDTO",
1550 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "INTERNAL_ADDRESS",
1552 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "EXTERNAL_ADDRESS",
1554 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "disablev6",
1558 *port = (uint16_t) uc.nport;
1566 * Remove entries from the peer connection list
1568 * @param pg the peer group we are working with
1569 * @param first index of the first peer
1570 * @param second index of the second peer
1571 * @param list the peer list to use
1572 * @param check UNUSED
1574 * @return the number of connections added (can be 0, 1 or 2)
1578 remove_connections (struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1579 unsigned int second, enum PeerLists list,
1585 struct PeerConnection **first_list;
1586 struct PeerConnection **second_list;
1587 struct PeerConnection *first_iter;
1588 struct PeerConnection *second_iter;
1589 struct PeerConnection **first_tail;
1590 struct PeerConnection **second_tail;
1593 GNUNET_HashCode hash_first;
1594 GNUNET_HashCode hash_second;
1596 hash_from_uid (first, &hash_first);
1597 hash_from_uid (second, &hash_second);
1605 first_list = &pg->peers[first].allowed_peers_head;
1606 second_list = &pg->peers[second].allowed_peers_head;
1607 first_tail = &pg->peers[first].allowed_peers_tail;
1608 second_tail = &pg->peers[second].allowed_peers_tail;
1611 first_list = &pg->peers[first].connect_peers_head;
1612 second_list = &pg->peers[second].connect_peers_head;
1613 first_tail = &pg->peers[first].connect_peers_tail;
1614 second_tail = &pg->peers[second].connect_peers_tail;
1617 first_list = &pg->peers[first].blacklisted_peers_head;
1618 second_list = &pg->peers[second].blacklisted_peers_head;
1619 first_tail = &pg->peers[first].blacklisted_peers_tail;
1620 second_tail = &pg->peers[second].blacklisted_peers_tail;
1623 first_list = &pg->peers[first].connect_peers_working_set_head;
1624 second_list = &pg->peers[second].connect_peers_working_set_head;
1625 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1626 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1633 first_iter = *first_list;
1634 while (first_iter != NULL)
1636 if (first_iter->index == second)
1638 GNUNET_CONTAINER_DLL_remove (*first_list, *first_tail, first_iter);
1639 GNUNET_free (first_iter);
1643 first_iter = first_iter->next;
1646 second_iter = *second_list;
1647 while (second_iter != NULL)
1649 if (second_iter->index == first)
1651 GNUNET_CONTAINER_DLL_remove (*second_list, *second_tail, second_iter);
1652 GNUNET_free (second_iter);
1656 second_iter = second_iter->next;
1660 GNUNET_CONTAINER_multihashmap_contains (pg->
1661 peers[first].blacklisted_peers,
1664 GNUNET_CONTAINER_multihashmap_remove_all (pg->
1665 peers[first].blacklisted_peers,
1670 GNUNET_CONTAINER_multihashmap_contains (pg->
1671 peers[second].blacklisted_peers,
1674 GNUNET_CONTAINER_multihashmap_remove_all (pg->
1675 peers[second].blacklisted_peers,
1684 * Add entries to the some list
1686 * @param pg the peer group we are working with
1687 * @param first index of the first peer
1688 * @param second index of the second peer
1689 * @param list the list type that we should modify
1690 * @param check GNUNET_YES to check lists before adding
1691 * GNUNET_NO to force add
1693 * @return the number of connections added (can be 0, 1 or 2)
1697 add_connections (struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1698 unsigned int second, enum PeerLists list, unsigned int check)
1704 struct PeerConnection **first_list;
1705 struct PeerConnection **second_list;
1706 struct PeerConnection *first_iter;
1707 struct PeerConnection *second_iter;
1708 struct PeerConnection *new_first;
1709 struct PeerConnection *new_second;
1710 struct PeerConnection **first_tail;
1711 struct PeerConnection **second_tail;
1716 first_list = &pg->peers[first].allowed_peers_head;
1717 second_list = &pg->peers[second].allowed_peers_head;
1718 first_tail = &pg->peers[first].allowed_peers_tail;
1719 second_tail = &pg->peers[second].allowed_peers_tail;
1722 first_list = &pg->peers[first].connect_peers_head;
1723 second_list = &pg->peers[second].connect_peers_head;
1724 first_tail = &pg->peers[first].connect_peers_tail;
1725 second_tail = &pg->peers[second].connect_peers_tail;
1728 first_list = &pg->peers[first].blacklisted_peers_head;
1729 second_list = &pg->peers[second].blacklisted_peers_head;
1730 first_tail = &pg->peers[first].blacklisted_peers_tail;
1731 second_tail = &pg->peers[second].blacklisted_peers_tail;
1734 first_list = &pg->peers[first].connect_peers_working_set_head;
1735 second_list = &pg->peers[second].connect_peers_working_set_head;
1736 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1737 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1744 add_first = GNUNET_YES;
1745 add_second = GNUNET_YES;
1747 if (check == GNUNET_YES)
1749 first_iter = *first_list;
1750 while (first_iter != NULL)
1752 if (first_iter->index == second)
1754 add_first = GNUNET_NO;
1757 first_iter = first_iter->next;
1760 second_iter = *second_list;
1761 while (second_iter != NULL)
1763 if (second_iter->index == first)
1765 add_second = GNUNET_NO;
1768 second_iter = second_iter->next;
1775 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1776 new_first->index = second;
1777 GNUNET_CONTAINER_DLL_insert (*first_list, *first_tail, new_first);
1778 pg->peers[first].num_connections++;
1784 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1785 new_second->index = first;
1786 GNUNET_CONTAINER_DLL_insert (*second_list, *second_tail, new_second);
1787 pg->peers[second].num_connections++;
1795 * Scale free network construction as described in:
1797 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1799 * Start with a network of "one" peer, then progressively add
1800 * peers up to the total number. At each step, iterate over
1801 * all possible peers and connect new peer based on number of
1802 * existing connections of the target peer.
1804 * @param pg the peer group we are dealing with
1805 * @param proc the connection processor to use
1806 * @param list the peer list to use
1808 * @return the number of connections created
1811 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg,
1812 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1815 unsigned int total_connections;
1816 unsigned int outer_count;
1818 unsigned int previous_total_connections;
1822 GNUNET_assert (pg->total > 1);
1824 /* Add a connection between the first two nodes */
1825 total_connections = proc (pg, 0, 1, list, GNUNET_YES);
1827 for (outer_count = 1; outer_count < pg->total; outer_count++)
1829 previous_total_connections = total_connections;
1830 for (i = 0; i < outer_count; i++)
1833 pg->peers[i].num_connections / (double) previous_total_connections;
1836 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1837 UINT64_MAX)) / ((double) UINT64_MAX);
1839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1840 "Considering connecting peer %d to peer %d\n", outer_count,
1843 if (random < probability)
1846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n",
1849 total_connections += proc (pg, outer_count, i, list, GNUNET_YES);
1854 return total_connections;
1858 * Create a topology given a peer group (set of running peers)
1859 * and a connection processor. Creates a small world topology
1860 * according to the rewired ring construction. The basic
1861 * behavior is that a ring topology is created, but with some
1862 * probability instead of connecting a peer to the next
1863 * neighbor in the ring a connection will be created to a peer
1864 * selected uniformly at random. We use the TESTING
1865 * PERCENTAGE option to specify what number of
1866 * connections each peer should have. Default is 2,
1867 * which makes the ring, any given number is multiplied by
1868 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1869 * each peer have on average 2logn connections. The additional
1870 * connections are made at increasing distance around the ring
1871 * from the original peer, or to random peers based on the re-
1872 * wiring probability. The TESTING
1873 * PROBABILITY option is used as the probability that a given
1874 * connection is rewired.
1876 * @param pg the peergroup to create the topology on
1877 * @param proc the connection processor to call to actually set
1878 * up connections between two peers
1879 * @param list the peer list to use
1881 * @return the number of connections that were set up
1885 create_small_world_ring (struct GNUNET_TESTING_PeerGroup *pg,
1886 GNUNET_TESTING_ConnectionProcessor proc,
1887 enum PeerLists list)
1891 unsigned int natLog;
1892 unsigned int randomPeer;
1893 double random, logNModifier, probability;
1894 unsigned int smallWorldConnections;
1899 unsigned int useAnd;
1900 int connect_attempts;
1902 logNModifier = 0.5; /* FIXME: default value? */
1904 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING", "PERCENTAGE",
1907 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1908 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1910 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1911 p_string, "LOGNMODIFIER", "TESTING");
1912 GNUNET_free (p_string);
1914 probability = 0.5; /* FIXME: default percentage? */
1916 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING", "PROBABILITY",
1919 if (sscanf (p_string, "%lf", &probability) != 1)
1920 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1922 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1923 p_string, "PERCENTAGE", "TESTING");
1924 GNUNET_free (p_string);
1926 natLog = log (pg->total);
1927 connsPerPeer = ceil (natLog * logNModifier);
1929 if (connsPerPeer % 2 == 1)
1932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."),
1935 smallWorldConnections = 0;
1936 connect_attempts = 0;
1937 for (i = 0; i < pg->total; i++)
1940 max = i + connsPerPeer / 2;
1941 min = i - connsPerPeer / 2;
1943 if (max > pg->total - 1)
1945 max = max - pg->total;
1951 min = pg->total - 1 + min;
1955 for (j = 0; j < connsPerPeer / 2; j++)
1959 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1960 UINT64_MAX) / ((double) UINT64_MAX));
1961 if (random < probability)
1963 /* Connect to uniformly selected random peer */
1965 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
1966 while ((((randomPeer < max) && (randomPeer > min)) && (useAnd == 0)) ||
1967 (((randomPeer > min) || (randomPeer < max)) && (useAnd == 1)))
1970 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
1972 smallWorldConnections += proc (pg, i, randomPeer, list, GNUNET_YES);
1976 nodeToConnect = i + j + 1;
1977 if (nodeToConnect > pg->total - 1)
1979 nodeToConnect = nodeToConnect - pg->total;
1981 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
1987 connect_attempts += smallWorldConnections;
1989 return connect_attempts;
1993 * Create a topology given a peer group (set of running peers)
1994 * and a connection processor.
1996 * @param pg the peergroup to create the topology on
1997 * @param proc the connection processor to call to actually set
1998 * up connections between two peers
1999 * @param list the peer list to use
2001 * @return the number of connections that were set up
2005 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg,
2006 GNUNET_TESTING_ConnectionProcessor proc,
2007 enum PeerLists list)
2009 unsigned int outer_count, inner_count;
2010 unsigned int cutoff;
2011 int connect_attempts;
2012 double nat_percentage;
2015 nat_percentage = 0.6; /* FIXME: default percentage? */
2017 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING", "PERCENTAGE",
2020 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
2021 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2023 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2024 p_string, "PERCENTAGE", "TESTING");
2025 GNUNET_free (p_string);
2028 cutoff = (unsigned int) (nat_percentage * pg->total);
2029 connect_attempts = 0;
2030 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2032 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2034 if ((outer_count > cutoff) || (inner_count > cutoff))
2037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n",
2038 outer_count, inner_count);
2041 proc (pg, outer_count, inner_count, list, GNUNET_YES);
2045 return connect_attempts;
2050 * Create a topology given a peer group (set of running peers)
2051 * and a connection processor.
2053 * @param pg the peergroup to create the topology on
2054 * @param proc the connection processor to call to actually set
2055 * up connections between two peers
2056 * @param list the peer list to use
2058 * @return the number of connections that were set up
2062 create_nated_internet_copy (struct GNUNET_TESTING_PeerGroup *pg,
2063 GNUNET_TESTING_ConnectionProcessor proc,
2064 enum PeerLists list)
2066 unsigned int outer_count, inner_count;
2067 unsigned int cutoff;
2068 int connect_attempts;
2069 double nat_percentage;
2072 struct ProgressMeter *conn_meter;
2074 nat_percentage = 0.6; /* FIXME: default percentage? */
2076 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING", "PERCENTAGE",
2079 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
2080 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2082 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2083 p_string, "PERCENTAGE", "TESTING");
2084 GNUNET_free (p_string);
2087 cutoff = (unsigned int) (nat_percentage * pg->total);
2089 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2091 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2093 if ((outer_count > cutoff) || (inner_count > cutoff))
2099 conn_meter = create_meter (count, "NAT COPY", GNUNET_YES);
2100 connect_attempts = 0;
2101 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2103 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2105 if ((outer_count > cutoff) || (inner_count > cutoff))
2108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n",
2109 outer_count, inner_count);
2112 proc (pg, outer_count, inner_count, list, GNUNET_YES);
2113 add_connections (pg, outer_count, inner_count, ALLOWED, GNUNET_NO);
2114 update_meter (conn_meter);
2118 free_meter (conn_meter);
2120 return connect_attempts;
2125 * Create a topology given a peer group (set of running peers)
2126 * and a connection processor.
2128 * @param pg the peergroup to create the topology on
2129 * @param proc the connection processor to call to actually set
2130 * up connections between two peers
2131 * @param list the peer list to use
2133 * @return the number of connections that were set up
2137 create_small_world (struct GNUNET_TESTING_PeerGroup *pg,
2138 GNUNET_TESTING_ConnectionProcessor proc,
2139 enum PeerLists list)
2141 unsigned int i, j, k;
2142 unsigned int square;
2145 unsigned int toggle = 1;
2146 unsigned int nodeToConnect;
2147 unsigned int natLog;
2148 unsigned int node1Row;
2149 unsigned int node1Col;
2150 unsigned int node2Row;
2151 unsigned int node2Col;
2152 unsigned int distance;
2153 double probability, random, percentage;
2154 unsigned int smallWorldConnections;
2155 unsigned int small_world_it;
2157 int connect_attempts;
2159 square = floor (sqrt (pg->total));
2163 percentage = 0.5; /* FIXME: default percentage? */
2165 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING", "PERCENTAGE",
2168 if (sscanf (p_string, "%lf", &percentage) != 1)
2169 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2171 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2172 p_string, "PERCENTAGE", "TESTING");
2173 GNUNET_free (p_string);
2175 if (percentage < 0.0)
2177 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2179 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
2180 "PERCENTAGE", "TESTING", percentage);
2183 probability = 0.5; /* FIXME: default percentage? */
2185 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING", "PROBABILITY",
2188 if (sscanf (p_string, "%lf", &probability) != 1)
2189 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2191 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2192 p_string, "PROBABILITY", "TESTING");
2193 GNUNET_free (p_string);
2195 if (square * square != pg->total)
2197 while (rows * cols < pg->total)
2199 if (toggle % 2 == 0)
2208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2209 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2213 connect_attempts = 0;
2214 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2215 * to the node to its right and above. Once this is over, we'll have our torus!
2216 * Special case for the last node (if the rows and columns are not equal), connect
2217 * to the first in the row to maintain topology.
2219 for (i = 0; i < pg->total; i++)
2221 /* First connect to the node to the right */
2222 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2223 nodeToConnect = i + 1;
2224 else if (i + 1 == pg->total)
2225 nodeToConnect = rows * cols - cols;
2227 nodeToConnect = i - cols + 1;
2229 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2233 nodeToConnect = (rows * cols) - cols + i;
2234 if (nodeToConnect >= pg->total)
2235 nodeToConnect -= cols;
2238 nodeToConnect = i - cols;
2240 if (nodeToConnect < pg->total)
2241 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2243 natLog = log (pg->total);
2244 #if VERBOSE_TESTING > 2
2245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2246 _("natural log of %d is %d, will run %d iterations\n"), pg->total,
2247 natLog, (int) (natLog * percentage));
2248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2249 _("Total connections added thus far: %u!\n"), connect_attempts);
2251 smallWorldConnections = 0;
2252 small_world_it = (unsigned int) (natLog * percentage);
2253 if (small_world_it < 1)
2255 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
2256 for (i = 0; i < small_world_it; i++)
2258 for (j = 0; j < pg->total; j++)
2260 /* Determine the row and column of node at position j on the 2d torus */
2261 node1Row = j / cols;
2262 node1Col = j - (node1Row * cols);
2263 for (k = 0; k < pg->total; k++)
2265 /* Determine the row and column of node at position k on the 2d torus */
2266 node2Row = k / cols;
2267 node2Col = k - (node2Row * cols);
2268 /* Simple Cartesian distance */
2269 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
2272 /* Calculate probability as 1 over the square of the distance */
2273 probability = 1.0 / (distance * distance);
2274 /* Choose a random value between 0 and 1 */
2277 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2278 UINT64_MAX)) / ((double) UINT64_MAX);
2279 /* If random < probability, then connect the two nodes */
2280 if (random < probability)
2281 smallWorldConnections += proc (pg, j, k, list, GNUNET_YES);
2287 connect_attempts += smallWorldConnections;
2288 #if VERBOSE_TESTING > 2
2289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2290 _("Total connections added for small world: %d!\n"),
2291 smallWorldConnections);
2293 return connect_attempts;
2297 * Create a topology given a peer group (set of running peers)
2298 * and a connection processor.
2300 * @param pg the peergroup to create the topology on
2301 * @param proc the connection processor to call to actually set
2302 * up connections between two peers
2303 * @param list the peer list to use
2305 * @return the number of connections that were set up
2309 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg,
2310 GNUNET_TESTING_ConnectionProcessor proc,
2311 enum PeerLists list)
2314 unsigned int outer_count;
2315 unsigned int inner_count;
2316 int connect_attempts;
2320 probability = 0.5; /* FIXME: default percentage? */
2322 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING", "PROBABILITY",
2325 if (sscanf (p_string, "%lf", &probability) != 1)
2326 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2328 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2329 p_string, "PROBABILITY", "TESTING");
2330 GNUNET_free (p_string);
2332 connect_attempts = 0;
2333 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2335 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2339 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2340 UINT64_MAX)) / ((double) UINT64_MAX);
2342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("rand is %f probability is %f\n"),
2343 temp_rand, probability);
2345 if (temp_rand < probability)
2348 proc (pg, outer_count, inner_count, list, GNUNET_YES);
2353 return connect_attempts;
2357 * Create a topology given a peer group (set of running peers)
2358 * and a connection processor. This particular function creates
2359 * the connections for a 2d-torus, plus additional "closest"
2360 * connections per peer.
2362 * @param pg the peergroup to create the topology on
2363 * @param proc the connection processor to call to actually set
2364 * up connections between two peers
2365 * @param list the peer list to use
2367 * @return the number of connections that were set up
2371 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg,
2372 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2375 unsigned int square;
2378 unsigned int toggle = 1;
2379 unsigned int nodeToConnect;
2380 int connect_attempts;
2382 connect_attempts = 0;
2384 square = floor (sqrt (pg->total));
2388 if (square * square != pg->total)
2390 while (rows * cols < pg->total)
2392 if (toggle % 2 == 0)
2401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2402 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2405 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2406 * to the node to its right and above. Once this is over, we'll have our torus!
2407 * Special case for the last node (if the rows and columns are not equal), connect
2408 * to the first in the row to maintain topology.
2410 for (i = 0; i < pg->total; i++)
2412 /* First connect to the node to the right */
2413 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2414 nodeToConnect = i + 1;
2415 else if (i + 1 == pg->total)
2416 nodeToConnect = rows * cols - cols;
2418 nodeToConnect = i - cols + 1;
2420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n", i,
2423 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2425 /* Second connect to the node immediately above */
2428 nodeToConnect = (rows * cols) - cols + i;
2429 if (nodeToConnect >= pg->total)
2430 nodeToConnect -= cols;
2433 nodeToConnect = i - cols;
2435 if (nodeToConnect < pg->total)
2438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n", i,
2441 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2446 return connect_attempts;
2450 * Create a topology given a peer group (set of running peers)
2451 * and a connection processor.
2453 * @param pg the peergroup to create the topology on
2454 * @param proc the connection processor to call to actually set
2455 * up connections between two peers
2456 * @param list the peer list to use
2457 * @param check does the connection processor need to check before
2458 * performing an action on the list?
2460 * @return the number of connections that were set up
2464 create_clique (struct GNUNET_TESTING_PeerGroup *pg,
2465 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list,
2468 unsigned int outer_count;
2469 unsigned int inner_count;
2470 int connect_attempts;
2471 struct ProgressMeter *conn_meter;
2473 connect_attempts = 0;
2476 create_meter ((((pg->total * pg->total) + pg->total) / 2) - pg->total,
2477 "Create Clique ", GNUNET_NO);
2478 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2480 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n",
2484 outer_count, inner_count);
2486 connect_attempts += proc (pg, outer_count, inner_count, list, check);
2487 update_meter (conn_meter);
2490 reset_meter (conn_meter);
2491 free_meter (conn_meter);
2492 return connect_attempts;
2497 * Iterator over hash map entries.
2499 * @param cls closure the peer group
2500 * @param key the key stored in the hashmap is the
2501 * index of the peer to connect to
2502 * @param value value in the hash map, handle to the peer daemon
2503 * @return GNUNET_YES if we should continue to
2508 unblacklist_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2510 struct UnblacklistContext *un_ctx = cls;
2511 uint32_t second_pos;
2513 uid_from_hash (key, &second_pos);
2515 unblacklist_connections (un_ctx->pg, un_ctx->first_uid, second_pos);
2523 * Create a blacklist topology based on the allowed topology
2524 * which disallows any connections not in the allowed topology
2525 * at the transport level.
2527 * @param pg the peergroup to create the topology on
2528 * @param proc the connection processor to call to allow
2529 * up connections between two peers
2531 * @return the number of connections that were set up
2535 copy_allowed (struct GNUNET_TESTING_PeerGroup *pg,
2536 GNUNET_TESTING_ConnectionProcessor proc)
2540 struct PeerConnection *iter;
2543 struct UnblacklistContext un_ctx;
2548 for (count = 0; count < pg->total - 1; count++)
2551 iter = pg->peers[count].allowed_peers_head;
2552 while (iter != NULL)
2554 remove_connections (pg, count, iter->index, BLACKLIST, GNUNET_YES);
2555 //unblacklist_connections(pg, count, iter->index);
2559 un_ctx.first_uid = count;
2561 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[count].allowed_peers,
2562 &unblacklist_iterator, &un_ctx);
2565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2571 * Create a topology given a peer group (set of running peers)
2572 * and a connection processor.
2574 * @param pg the peergroup to create the topology on
2575 * @param proc the connection processor to call to actually set
2576 * up connections between two peers
2577 * @param list which list should be modified
2579 * @return the number of connections that were set up
2583 create_line (struct GNUNET_TESTING_PeerGroup *pg,
2584 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2587 unsigned int connect_attempts;
2589 connect_attempts = 0;
2590 /* Connect each peer to the next highest numbered peer */
2591 for (count = 0; count < pg->total - 1; count++)
2594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n",
2597 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2600 return connect_attempts;
2604 * Create a topology given a peer group (set of running peers)
2605 * and a connection processor.
2607 * @param pg the peergroup to create the topology on
2608 * @param filename the file to read topology information from
2609 * @param proc the connection processor to call to actually set
2610 * up connections between two peers
2611 * @param list the peer list to use
2613 * @return the number of connections that were set up
2617 create_from_file (struct GNUNET_TESTING_PeerGroup *pg, char *filename,
2618 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2620 int connect_attempts;
2621 unsigned int first_peer_index;
2622 unsigned int second_peer_index;
2627 unsigned int total_peers;
2628 enum States curr_state;
2630 connect_attempts = 0;
2631 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2632 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2634 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2636 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2637 "Could not open file `%s' specified for topology!", filename);
2638 return connect_attempts;
2641 data = GNUNET_malloc_large (frstat.st_size);
2642 GNUNET_assert (data != NULL);
2643 if (frstat.st_size != GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2645 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2646 "Could not read file %s specified for host list, ending test!",
2649 return connect_attempts;
2654 first_peer_index = 0;
2655 /* First line should contain a single integer, specifying the number of peers */
2656 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2657 curr_state = NUM_PEERS;
2658 while (count < frstat.st_size - 1)
2660 if ((buf[count] == '\n') || (buf[count] == ' '))
2670 total_peers = strtoul (&buf[count], NULL, 10);
2673 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2674 "Failed to read number of peers from topology file!\n");
2676 return connect_attempts;
2679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u total peers in topology\n",
2682 GNUNET_assert (total_peers == pg->total);
2683 curr_state = PEER_INDEX;
2684 while ((buf[count] != '\n') && (count < frstat.st_size - 1))
2690 first_peer_index = strtoul (&buf[count], NULL, 10);
2693 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2694 "Failed to read peer index from topology file!\n");
2696 return connect_attempts;
2698 while ((buf[count] != ':') && (count < frstat.st_size - 1))
2701 curr_state = OTHER_PEER_INDEX;
2704 if (1 == sscanf (&buf[count], ":"))
2705 curr_state = OTHER_PEER_INDEX;
2708 case OTHER_PEER_INDEX:
2710 second_peer_index = strtoul (&buf[count], NULL, 10);
2713 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2714 "Failed to peer index from topology file!\n");
2716 return connect_attempts;
2718 /* Assume file is written with first peer 1, but array index is 0 */
2720 proc (pg, first_peer_index - 1, second_peer_index - 1, list,
2722 while ((buf[count] != '\n') && (buf[count] != ',') &&
2723 (count < frstat.st_size - 1))
2725 if (buf[count] == '\n')
2727 curr_state = PEER_INDEX;
2729 else if (buf[count] != ',')
2731 curr_state = OTHER_PEER_INDEX;
2736 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2737 "Found bad data in topology file while in state %d!\n",
2741 return connect_attempts;
2745 return connect_attempts;
2749 * Create a topology given a peer group (set of running peers)
2750 * and a connection processor.
2752 * @param pg the peergroup to create the topology on
2753 * @param proc the connection processor to call to actually set
2754 * up connections between two peers
2755 * @param list the peer list to use
2757 * @return the number of connections that were set up
2761 create_ring (struct GNUNET_TESTING_PeerGroup *pg,
2762 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2765 int connect_attempts;
2767 connect_attempts = 0;
2769 /* Connect each peer to the next highest numbered peer */
2770 for (count = 0; count < pg->total - 1; count++)
2773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n",
2776 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2779 /* Connect the last peer to the first peer */
2780 connect_attempts += proc (pg, pg->total - 1, 0, list, GNUNET_YES);
2782 return connect_attempts;
2787 * Iterator for writing friends of a peer to a file.
2789 * @param cls closure, an open writable file handle
2790 * @param key the key the daemon was stored under
2791 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2793 * @return GNUNET_YES to continue iteration
2795 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2796 * with a single file_iterator that takes a closure which contains
2797 * the prefix to write before the peer. Then this could be used
2798 * for blacklisting multiple transports and writing the friend
2799 * file. I'm sure *someone* will complain loudly about other
2800 * things that negate these functions even existing so no point in
2804 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2806 FILE *temp_friend_handle = cls;
2807 struct GNUNET_TESTING_Daemon *peer = value;
2808 struct GNUNET_PeerIdentity *temppeer;
2809 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2811 temppeer = &peer->id;
2812 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2813 FPRINTF (temp_friend_handle, "%s\n", (char *) &peer_enc);
2818 struct BlacklistContext
2821 * The (open) file handle to write to
2823 FILE *temp_file_handle;
2826 * The transport that this peer will be blacklisted on.
2832 * Iterator for writing blacklist data to appropriate files.
2834 * @param cls closure, an open writable file handle
2835 * @param key the key the daemon was stored under
2836 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2838 * @return GNUNET_YES to continue iteration
2841 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2843 struct BlacklistContext *blacklist_ctx = cls;
2844 struct GNUNET_TESTING_Daemon *peer = value;
2845 struct GNUNET_PeerIdentity *temppeer;
2846 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2848 temppeer = &peer->id;
2849 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n",
2851 blacklist_ctx->transport, (char *) &peer_enc);
2852 FPRINTF (blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport,
2853 (char *) &peer_enc);
2860 * Create the friend files based on the PeerConnection's
2861 * of each peer in the peer group, and copy the files
2862 * to the appropriate place
2864 * @param pg the peer group we are dealing with
2867 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
2869 FILE *temp_friend_handle;
2870 unsigned int pg_iter;
2871 char *temp_service_path;
2872 struct GNUNET_OS_Process **procarr;
2877 enum GNUNET_OS_ProcessStatusType type;
2878 unsigned long return_code;
2886 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2887 struct PeerConnection *conn_iter;
2889 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2890 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2892 mytemp = GNUNET_DISK_mktemp ("friends");
2893 GNUNET_assert (mytemp != NULL);
2894 temp_friend_handle = FOPEN (mytemp, "wt");
2895 GNUNET_assert (temp_friend_handle != NULL);
2897 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2898 while (conn_iter != NULL)
2900 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->
2901 id.hashPubKey, &peer_enc);
2902 FPRINTF (temp_friend_handle, "%s\n", (char *) &peer_enc);
2903 conn_iter = conn_iter->next;
2906 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2907 &friend_file_iterator,
2908 temp_friend_handle);
2910 FCLOSE (temp_friend_handle);
2913 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].daemon->cfg,
2914 "PATHS", "SERVICEHOME",
2915 &temp_service_path))
2917 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2919 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2920 "SERVICEHOME", "PATHS");
2921 if (UNLINK (mytemp) != 0)
2922 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
2923 GNUNET_free (mytemp);
2927 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2929 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2931 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "mv", "mv", mytemp, arg, NULL);
2932 GNUNET_assert (procarr[pg_iter] != NULL);
2934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2935 "Copying file with command cp %s %s\n", mytemp, arg);
2937 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2938 GNUNET_OS_process_close (procarr[pg_iter]);
2941 else /* Remote, scp the file to the correct place */
2943 if (NULL != pg->peers[pg_iter].daemon->username)
2944 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2945 pg->peers[pg_iter].daemon->username,
2946 pg->peers[pg_iter].daemon->hostname,
2949 GNUNET_asprintf (&arg, "%s:%s/friends",
2950 pg->peers[pg_iter].daemon->hostname,
2953 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "scp", "scp", mytemp, arg, NULL);
2954 GNUNET_assert (procarr[pg_iter] != NULL);
2955 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2956 GNUNET_OS_process_close (procarr[pg_iter]);
2957 if (ret != GNUNET_OK)
2959 /* FIXME: free contents of 'procarr' array */
2960 GNUNET_free (procarr);
2961 GNUNET_free (temp_service_path);
2962 GNUNET_free (mytemp);
2966 procarr[pg_iter] = NULL;
2968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2969 "Copying file with command scp %s %s\n", mytemp, arg);
2973 GNUNET_free (temp_service_path);
2974 GNUNET_free (mytemp);
2979 ret = GNUNET_SYSERR;
2980 while ((count < max_wait) && (ret != GNUNET_OK))
2983 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking copy status of file %d\n",
2989 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2991 if (GNUNET_OS_process_status (procarr[pg_iter], &type, &return_code) !=
2994 ret = GNUNET_SYSERR;
2996 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
2998 ret = GNUNET_SYSERR;
3002 GNUNET_OS_process_close (procarr[pg_iter]);
3003 procarr[pg_iter] = NULL;
3005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "File %d copied\n", pg_iter);
3011 if (ret == GNUNET_SYSERR)
3013 /* FIXME: why sleep here? -CG */
3019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3020 _("Finished copying all friend files!\n"));
3023 GNUNET_free (procarr);
3028 * Create the blacklist files based on the PeerConnection's
3029 * of each peer in the peer group, and copy the files
3030 * to the appropriate place.
3032 * @param pg the peer group we are dealing with
3033 * @param transports space delimited list of transports to blacklist
3036 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg,
3037 const char *transports)
3039 FILE *temp_file_handle;
3040 unsigned int pg_iter;
3041 char *temp_service_path;
3042 struct GNUNET_OS_Process **procarr;
3045 enum GNUNET_OS_ProcessStatusType type;
3046 unsigned long return_code;
3053 char *temp_transports;
3056 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
3057 struct PeerConnection *conn_iter;
3059 static struct BlacklistContext blacklist_ctx;
3062 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
3063 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3065 mytemp = GNUNET_DISK_mktemp ("blacklist");
3066 GNUNET_assert (mytemp != NULL);
3067 temp_file_handle = FOPEN (mytemp, "wt");
3068 GNUNET_assert (temp_file_handle != NULL);
3069 temp_transports = GNUNET_strdup (transports);
3071 blacklist_ctx.temp_file_handle = temp_file_handle;
3073 transport_len = strlen (temp_transports) + 1;
3076 for (i = 0; i < transport_len; i++)
3078 if ((temp_transports[i] == ' ') && (pos == NULL))
3079 continue; /* At start of string (whitespace) */
3080 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
3082 temp_transports[i] = '\0';
3084 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
3085 while (conn_iter != NULL)
3087 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->
3088 id.hashPubKey, &peer_enc);
3089 FPRINTF (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
3090 conn_iter = conn_iter->next;
3093 blacklist_ctx.transport = pos;
3094 (void) GNUNET_CONTAINER_multihashmap_iterate (pg->
3096 [pg_iter].blacklisted_peers,
3097 &blacklist_file_iterator,
3101 } /* At beginning of actual string */
3102 else if (pos == NULL)
3104 pos = &temp_transports[i];
3108 GNUNET_free (temp_transports);
3109 FCLOSE (temp_file_handle);
3112 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].daemon->cfg,
3113 "PATHS", "SERVICEHOME",
3114 &temp_service_path))
3116 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3118 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
3119 "SERVICEHOME", "PATHS");
3120 if (UNLINK (mytemp) != 0)
3121 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
3122 GNUNET_free (mytemp);
3126 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
3128 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
3130 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "mv", "mv", mytemp, arg, NULL);
3132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3133 _("Copying file with command cp %s %s\n"), mytemp, arg);
3138 else /* Remote, scp the file to the correct place */
3140 if (NULL != pg->peers[pg_iter].daemon->username)
3141 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
3142 pg->peers[pg_iter].daemon->username,
3143 pg->peers[pg_iter].daemon->hostname,
3146 GNUNET_asprintf (&arg, "%s:%s/blacklist",
3147 pg->peers[pg_iter].daemon->hostname,
3150 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "scp", "scp", mytemp, arg, NULL);
3151 GNUNET_assert (procarr[pg_iter] != NULL);
3152 GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
3155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3156 _("Copying file with command scp %s %s\n"), mytemp, arg);
3160 GNUNET_free (temp_service_path);
3161 GNUNET_free (mytemp);
3165 ret = GNUNET_SYSERR;
3166 while ((count < max_wait) && (ret != GNUNET_OK))
3169 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3173 _("Checking copy status of file %d\n"), pg_iter);
3175 if (procarr[pg_iter] != NULL) /* Check for already completed! */
3177 if (GNUNET_OS_process_status (procarr[pg_iter], &type, &return_code) !=
3180 ret = GNUNET_SYSERR;
3182 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
3184 ret = GNUNET_SYSERR;
3188 GNUNET_OS_process_close (procarr[pg_iter]);
3189 procarr[pg_iter] = NULL;
3191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("File %d copied\n"), pg_iter);
3197 if (ret == GNUNET_SYSERR)
3199 /* FIXME: why sleep here? -CG */
3205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3206 _("Finished copying all blacklist files!\n"));
3208 GNUNET_free (procarr);
3212 /* Forward Declaration */
3214 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3217 * Choose a random peer's next connection to create, and
3218 * call schedule_connect to set up the connect task.
3220 * @param pg the peer group to connect
3223 preschedule_connect (struct GNUNET_TESTING_PeerGroup *pg)
3225 struct ConnectTopologyContext *ct_ctx = &pg->ct_ctx;
3226 struct PeerConnection *connection_iter;
3227 struct ConnectContext *connect_context;
3228 uint32_t random_peer;
3230 if (ct_ctx->remaining_connections == 0)
3233 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3234 while (pg->peers[random_peer].connect_peers_head == NULL)
3236 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3238 connection_iter = pg->peers[random_peer].connect_peers_head;
3239 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3240 connect_context->first_index = random_peer;
3241 connect_context->second_index = connection_iter->index;
3242 connect_context->ct_ctx = ct_ctx;
3243 connect_context->task =
3244 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3245 GNUNET_CONTAINER_DLL_insert (pg->cc_head, pg->cc_tail, connect_context);
3246 GNUNET_CONTAINER_DLL_remove (pg->peers[random_peer].connect_peers_head,
3247 pg->peers[random_peer].connect_peers_tail,
3249 GNUNET_free (connection_iter);
3250 ct_ctx->remaining_connections--;
3254 /* Forward declaration */
3256 schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3259 * Close connections and free the hello context.
3261 * @param cls the 'struct SendHelloContext *'
3262 * @param tc scheduler context
3265 free_hello_context (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3267 struct SendHelloContext *send_hello_context = cls;
3269 if (send_hello_context->peer->daemon->server != NULL)
3271 GNUNET_CORE_disconnect (send_hello_context->peer->daemon->server);
3272 send_hello_context->peer->daemon->server = NULL;
3274 if (send_hello_context->peer->daemon->th != NULL)
3276 GNUNET_TRANSPORT_disconnect (send_hello_context->peer->daemon->th);
3277 send_hello_context->peer->daemon->th = NULL;
3279 if (send_hello_context->core_connect_task != GNUNET_SCHEDULER_NO_TASK)
3281 GNUNET_SCHEDULER_cancel (send_hello_context->core_connect_task);
3282 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3284 send_hello_context->pg->outstanding_connects--;
3285 GNUNET_free (send_hello_context);
3289 * For peers that haven't yet connected, notify
3290 * the caller that they have failed (timeout).
3292 * @param cls the 'struct SendHelloContext *'
3293 * @param tc scheduler context
3296 notify_remaining_connections_failed (void *cls,
3297 const struct GNUNET_SCHEDULER_TaskContext
3300 struct SendHelloContext *send_hello_context = cls;
3301 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3302 struct PeerConnection *connection;
3304 GNUNET_CORE_disconnect (send_hello_context->peer->daemon->server);
3305 send_hello_context->peer->daemon->server = NULL;
3307 connection = send_hello_context->peer->connect_peers_head;
3309 while (connection != NULL)
3311 if (pg->notify_connection != NULL)
3313 pg->notify_connection (pg->notify_connection_cls, &send_hello_context->peer->daemon->id, &pg->peers[connection->index].daemon->id, 0, /* FIXME */
3314 send_hello_context->peer->daemon->cfg,
3315 pg->peers[connection->index].daemon->cfg,
3316 send_hello_context->peer->daemon,
3317 pg->peers[connection->index].daemon,
3318 "Peers failed to connect (timeout)");
3320 GNUNET_CONTAINER_DLL_remove (send_hello_context->peer->connect_peers_head,
3321 send_hello_context->peer->connect_peers_tail,
3323 GNUNET_free (connection);
3324 connection = connection->next;
3326 GNUNET_SCHEDULER_add_now (&free_hello_context, send_hello_context);
3328 other_peer = &pg->peers[connection->index];
3333 * For peers that haven't yet connected, send
3334 * CORE connect requests.
3336 * @param cls the 'struct SendHelloContext *'
3337 * @param tc scheduler context
3340 send_core_connect_requests (void *cls,
3341 const struct GNUNET_SCHEDULER_TaskContext *tc)
3343 struct SendHelloContext *send_hello_context = cls;
3344 struct PeerConnection *conn;
3346 GNUNET_assert (send_hello_context->peer->daemon->server != NULL);
3348 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3350 send_hello_context->connect_attempts++;
3351 if (send_hello_context->connect_attempts <
3352 send_hello_context->pg->ct_ctx.connect_attempts)
3354 conn = send_hello_context->peer->connect_peers_head;
3355 while (conn != NULL)
3357 GNUNET_TRANSPORT_try_connect (send_hello_context->peer->daemon->th,
3358 &send_hello_context->pg->peers[conn->
3363 send_hello_context->core_connect_task =
3364 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
3365 (send_hello_context->pg->
3366 ct_ctx.connect_timeout,
3367 send_hello_context->pg->
3368 ct_ctx.connect_attempts),
3369 &send_core_connect_requests,
3370 send_hello_context);
3374 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3375 "Timeout before all connections created, marking rest as failed!\n");
3376 GNUNET_SCHEDULER_add_now (¬ify_remaining_connections_failed,
3377 send_hello_context);
3383 * Success, connection is up. Signal client our success.
3385 * @param cls our "struct SendHelloContext"
3386 * @param peer identity of the peer that has connected
3387 * @param atsi performance information
3389 * FIXME: remove peers from BOTH lists, call notify twice, should
3390 * double the speed of connections as long as the list iteration
3391 * doesn't take too long!
3394 core_connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
3395 const struct GNUNET_ATS_Information *atsi)
3397 struct SendHelloContext *send_hello_context = cls;
3398 struct PeerConnection *connection;
3399 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3402 struct PeerData *other_peer;
3405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected peer %s to peer %s\n",
3406 ctx->d1->shortname, GNUNET_i2s (peer));
3410 memcmp (&send_hello_context->peer->daemon->id, peer,
3411 sizeof (struct GNUNET_PeerIdentity)))
3414 connection = send_hello_context->peer->connect_peers_head;
3419 while ((connection != NULL) &&
3421 memcmp (&pg->peers[connection->index].daemon->id, peer,
3422 sizeof (struct GNUNET_PeerIdentity))))
3424 connection = connection->next;
3427 if (connection == NULL)
3429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3430 "Connected peer %s to %s, not in list (no problem(?))\n",
3431 GNUNET_i2s (peer), send_hello_context->peer->daemon->shortname);
3436 other_peer = &pg->peers[connection->index];
3438 if (pg->notify_connection != NULL)
3440 pg->notify_connection (pg->notify_connection_cls, &send_hello_context->peer->daemon->id, peer, 0, /* FIXME */
3441 send_hello_context->peer->daemon->cfg,
3442 pg->peers[connection->index].daemon->cfg,
3443 send_hello_context->peer->daemon,
3444 pg->peers[connection->index].daemon, NULL);
3446 GNUNET_CONTAINER_DLL_remove (send_hello_context->peer->connect_peers_head,
3447 send_hello_context->peer->connect_peers_tail,
3449 GNUNET_free (connection);
3453 /* Notify of reverse connection and remove from other peers list of outstanding */
3454 if (other_peer != NULL)
3456 connection = other_peer->connect_peers_head;
3457 while ((connection != NULL) &&
3459 memcmp (&send_hello_context->peer->daemon->id,
3460 &pg->peers[connection->index].daemon->id,
3461 sizeof (struct GNUNET_PeerIdentity))))
3463 connection = connection->next;
3465 if (connection != NULL)
3467 if (pg->notify_connection != NULL)
3469 pg->notify_connection (pg->notify_connection_cls, peer, &send_hello_context->peer->daemon->id, 0, /* FIXME */
3470 pg->peers[connection->index].daemon->cfg,
3471 send_hello_context->peer->daemon->cfg,
3472 pg->peers[connection->index].daemon,
3473 send_hello_context->peer->daemon, NULL);
3476 GNUNET_CONTAINER_DLL_remove (other_peer->connect_peers_head,
3477 other_peer->connect_peers_tail, connection);
3478 GNUNET_free (connection);
3483 if (send_hello_context->peer->connect_peers_head == NULL)
3485 GNUNET_SCHEDULER_add_now (&free_hello_context, send_hello_context);
3490 * Notify of a successful connection to the core service.
3492 * @param cls a struct SendHelloContext *
3493 * @param server handle to the core service
3494 * @param my_identity the peer identity of this peer
3497 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3498 struct GNUNET_PeerIdentity *my_identity)
3500 struct SendHelloContext *send_hello_context = cls;
3502 send_hello_context->core_ready = GNUNET_YES;
3506 * Function called once a hello has been sent
3507 * to the transport, move on to the next one
3508 * or go away forever.
3510 * @param cls the 'struct SendHelloContext *'
3511 * @param tc scheduler context
3514 hello_sent_callback (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3516 struct SendHelloContext *send_hello_context = cls;
3518 //unsigned int pg_iter;
3519 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3521 GNUNET_free (send_hello_context);
3525 send_hello_context->pg->remaining_hellos--;
3527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent HELLO, have %d remaining!\n",
3528 send_hello_context->pg->remaining_hellos);
3530 if (send_hello_context->peer_pos == NULL) /* All HELLOs (for this peer!) have been transmitted! */
3533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3534 "All hellos for this peer sent, disconnecting transport!\n");
3536 GNUNET_assert (send_hello_context->peer->daemon->th != NULL);
3537 GNUNET_TRANSPORT_disconnect (send_hello_context->peer->daemon->th);
3538 send_hello_context->peer->daemon->th = NULL;
3539 GNUNET_assert (send_hello_context->peer->daemon->server == NULL);
3540 send_hello_context->peer->daemon->server =
3541 GNUNET_CORE_connect (send_hello_context->peer->cfg, 1,
3542 send_hello_context, &core_init,
3543 &core_connect_notify, NULL, NULL, NULL, GNUNET_NO,
3544 NULL, GNUNET_NO, no_handlers);
3546 send_hello_context->core_connect_task =
3547 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
3548 (send_hello_context->pg->
3549 ct_ctx.connect_timeout,
3550 send_hello_context->pg->
3551 ct_ctx.connect_attempts),
3552 &send_core_connect_requests,
3553 send_hello_context);
3556 GNUNET_SCHEDULER_add_now (&schedule_send_hellos, send_hello_context);
3560 * Connect to a peer, give it all the HELLO's of those peers
3561 * we will later ask it to connect to.
3563 * @param ct_ctx the overall connection context
3566 schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3568 struct SendHelloContext *send_hello_context = cls;
3569 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3571 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3573 GNUNET_free (send_hello_context);
3577 GNUNET_assert (send_hello_context->peer_pos != NULL); /* All of the HELLO sends to be scheduled have been scheduled! */
3579 if (((send_hello_context->peer->daemon->th == NULL) &&
3580 (pg->outstanding_connects > pg->max_outstanding_connections)) ||
3581 (pg->stop_connects == GNUNET_YES))
3583 #if VERBOSE_TESTING > 2
3584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3586 ("Delaying connect, we have too many outstanding connections!\n"));
3588 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3589 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3590 &schedule_send_hellos, send_hello_context);
3594 #if VERBOSE_TESTING > 2
3595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3596 _("Creating connection, outstanding_connections is %d\n"),
3597 outstanding_connects);
3599 if (send_hello_context->peer->daemon->th == NULL)
3601 pg->outstanding_connects++; /* Actual TRANSPORT, CORE connections! */
3602 send_hello_context->peer->daemon->th =
3603 GNUNET_TRANSPORT_connect (send_hello_context->peer->cfg, NULL,
3604 send_hello_context, NULL, NULL, NULL);
3607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3608 _("Offering HELLO of peer %s to peer %s\n"),
3609 send_hello_context->peer->daemon->shortname,
3610 pg->peers[send_hello_context->peer_pos->index].
3613 GNUNET_TRANSPORT_offer_hello (send_hello_context->peer->daemon->th,
3614 (const struct GNUNET_MessageHeader *)
3615 pg->peers[send_hello_context->peer_pos->
3616 index].daemon->hello,
3617 &hello_sent_callback, send_hello_context);
3618 send_hello_context->peer_pos = send_hello_context->peer_pos->next;
3619 GNUNET_assert (send_hello_context->peer->daemon->th != NULL);
3625 * Internal notification of a connection, kept so that we can ensure some connections
3626 * happen instead of flooding all testing daemons with requests to connect.
3629 internal_connect_notify (void *cls, const struct GNUNET_PeerIdentity *first,
3630 const struct GNUNET_PeerIdentity *second,
3632 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
3633 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
3634 struct GNUNET_TESTING_Daemon *first_daemon,
3635 struct GNUNET_TESTING_Daemon *second_daemon,
3638 struct ConnectContext *connect_ctx = cls;
3639 struct ConnectTopologyContext *ct_ctx = connect_ctx->ct_ctx;
3640 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
3641 struct PeerConnection *connection;
3643 GNUNET_assert (NULL != connect_ctx->cc);
3644 connect_ctx->cc = NULL;
3645 GNUNET_assert (0 < pg->outstanding_connects);
3646 pg->outstanding_connects--;
3647 GNUNET_CONTAINER_DLL_remove (pg->cc_head, pg->cc_tail, connect_ctx);
3649 * Check whether the inverse connection has been scheduled yet,
3650 * if not, we can remove it from the other peers list and avoid
3651 * even trying to connect them again!
3653 connection = pg->peers[connect_ctx->second_index].connect_peers_head;
3658 while ((connection != NULL) &&
3660 memcmp (first, &pg->peers[connection->index].daemon->id,
3661 sizeof (struct GNUNET_PeerIdentity))))
3662 connection = connection->next;
3664 if (connection != NULL) /* Can safely remove! */
3666 GNUNET_assert (0 < ct_ctx->remaining_connections);
3667 ct_ctx->remaining_connections--;
3668 if (pg->notify_connection != NULL) /* Notify of reverse connection */
3669 pg->notify_connection (pg->notify_connection_cls, second, first, distance,
3670 second_cfg, first_cfg, second_daemon, first_daemon,
3673 GNUNET_CONTAINER_DLL_remove (pg->
3675 second_index].connect_peers_head,
3676 pg->peers[connect_ctx->
3677 second_index].connect_peers_tail,
3679 GNUNET_free (connection);
3682 if (ct_ctx->remaining_connections == 0)
3684 if (ct_ctx->notify_connections_done != NULL)
3686 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
3687 ct_ctx->notify_connections_done = NULL;
3691 preschedule_connect (pg);
3693 if (pg->notify_connection != NULL)
3694 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
3695 first_cfg, second_cfg, first_daemon, second_daemon,
3697 GNUNET_free (connect_ctx);
3701 * Either delay a connection (because there are too many outstanding)
3702 * or schedule it for right now.
3704 * @param cls a connection context
3705 * @param tc the task runtime context
3708 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3710 struct ConnectContext *connect_context = cls;
3711 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
3713 connect_context->task = GNUNET_SCHEDULER_NO_TASK;
3714 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3717 if ((pg->outstanding_connects > pg->max_outstanding_connections) ||
3718 (pg->stop_connects == GNUNET_YES))
3721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3723 ("Delaying connect, we have too many outstanding connections!\n"));
3725 connect_context->task =
3726 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3727 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3728 &schedule_connect, connect_context);
3732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3734 ("Creating connection, outstanding_connections is %d (max %d)\n"),
3735 pg->outstanding_connects, pg->max_outstanding_connections);
3737 pg->outstanding_connects++;
3738 pg->total_connects_scheduled++;
3739 GNUNET_assert (NULL == connect_context->cc);
3740 connect_context->cc =
3741 GNUNET_TESTING_daemons_connect (pg->
3742 peers[connect_context->
3743 first_index].daemon,
3744 pg->peers[connect_context->
3745 second_index].daemon,
3746 connect_context->ct_ctx->connect_timeout,
3747 connect_context->ct_ctx->connect_attempts,
3753 &internal_connect_notify,
3760 * Iterator for actually scheduling connections to be created
3761 * between two peers.
3763 * @param cls closure, a GNUNET_TESTING_Daemon
3764 * @param key the key the second Daemon was stored under
3765 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3767 * @return GNUNET_YES to continue iteration
3770 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3772 struct ConnectTopologyContext *ct_ctx = cls;
3773 struct PeerData *first = ct_ctx->first;
3774 struct GNUNET_TESTING_Daemon *second = value;
3775 struct ConnectContext *connect_context;
3777 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3778 connect_context->first = first->daemon;
3779 connect_context->second = second;
3780 connect_context->ct_ctx = ct_ctx;
3781 connect_context->task =
3782 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3783 GNUNET_CONTAINER_DLL_insert (ct_ctx->pg->cc_head, ct_ctx->pg->cc_tail,
3791 * Iterator for copying all entries in the allowed hashmap to the
3794 * @param cls closure, a GNUNET_TESTING_Daemon
3795 * @param key the key the second Daemon was stored under
3796 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3798 * @return GNUNET_YES to continue iteration
3801 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3803 struct PeerData *first = cls;
3805 GNUNET_assert (GNUNET_OK ==
3806 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
3808 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3815 * Make the peers to connect the same as those that are allowed to be
3818 * @param pg the peer group
3821 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
3823 unsigned int pg_iter;
3828 struct PeerConnection *iter;
3832 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3835 iter = pg->peers[pg_iter].allowed_peers_head;
3836 while (iter != NULL)
3838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3839 "Creating connection between %d and %d\n", pg_iter,
3841 total += add_connections (pg, pg_iter, iter->index, CONNECT, GNUNET_YES);
3842 //total += add_actual_connections(pg, pg_iter, iter->index);
3847 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
3848 ©_topology_iterator,
3849 &pg->peers[pg_iter]);
3851 if (GNUNET_SYSERR == ret)
3852 return GNUNET_SYSERR;
3854 total = total + ret;
3861 * Connect the topology as specified by the PeerConnection's
3862 * of each peer in the peer group
3864 * @param pg the peer group we are dealing with
3865 * @param connect_timeout how long try connecting two peers
3866 * @param connect_attempts how many times (max) to attempt
3867 * @param notify_callback callback to notify when finished
3868 * @param notify_cls closure for notify callback
3870 * @return the number of connections that will be attempted
3873 connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3874 struct GNUNET_TIME_Relative connect_timeout,
3875 unsigned int connect_attempts,
3876 GNUNET_TESTING_NotifyCompletion notify_callback,
3879 unsigned int pg_iter;
3883 struct PeerConnection *connection_iter;
3886 struct SendHelloContext *send_hello_context;
3890 pg->ct_ctx.notify_connections_done = notify_callback;
3891 pg->ct_ctx.notify_cls = notify_cls;
3894 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3897 connection_iter = pg->peers[pg_iter].connect_peers_head;
3898 while (connection_iter != NULL)
3900 connection_iter = connection_iter->next;
3905 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3912 pg->ct_ctx.connect_timeout = connect_timeout;
3913 pg->ct_ctx.connect_attempts = connect_attempts;
3914 pg->ct_ctx.remaining_connections = total;
3917 /* First give all peers the HELLO's of other peers (connect to first peer's transport service, give HELLO's of other peers, continue...) */
3918 pg->remaining_hellos = total;
3919 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3921 send_hello_context = GNUNET_malloc (sizeof (struct SendHelloContext));
3922 send_hello_context->peer = &pg->peers[pg_iter];
3923 send_hello_context->peer_pos = pg->peers[pg_iter].connect_peers_head;
3924 send_hello_context->pg = pg;
3925 GNUNET_SCHEDULER_add_now (&schedule_send_hellos, send_hello_context);
3928 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3930 preschedule_connect (pg);
3938 * Takes a peer group and creates a topology based on the
3939 * one specified. Creates a topology means generates friend
3940 * files for the peers so they can only connect to those allowed
3941 * by the topology. This will only have an effect once peers
3942 * are started if the FRIENDS_ONLY option is set in the base
3943 * config. Also takes an optional restrict topology which
3944 * disallows connections based on particular transports
3945 * UNLESS they are specified in the restricted topology.
3947 * @param pg the peer group struct representing the running peers
3948 * @param topology which topology to connect the peers in
3949 * @param restrict_topology disallow restrict_transports transport
3950 * connections to peers NOT in this topology
3951 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3952 * @param restrict_transports space delimited list of transports to blacklist
3953 * to create restricted topology
3955 * @return the maximum number of connections were all allowed peers
3956 * connected to each other
3959 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
3960 enum GNUNET_TESTING_Topology topology,
3961 enum GNUNET_TESTING_Topology restrict_topology,
3962 const char *restrict_transports)
3966 unsigned int num_connections;
3967 int unblacklisted_connections;
3969 struct PeerConnection *conn_iter;
3970 struct PeerConnection *temp_conn;
3976 for (i = 0; i < pg->total; i++)
3978 pg->peers[i].allowed_peers = GNUNET_CONTAINER_multihashmap_create (100);
3979 pg->peers[i].connect_peers = GNUNET_CONTAINER_multihashmap_create (100);
3980 pg->peers[i].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create (100);
3981 pg->peers[i].pg = pg;
3987 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3989 num_connections = create_clique (pg, &add_connections, ALLOWED, GNUNET_NO);
3991 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3993 _("Creating small world (ring) topology\n"));
3994 num_connections = create_small_world_ring (pg, &add_connections, ALLOWED);
3996 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3998 _("Creating small world (2d-torus) topology\n"));
3999 num_connections = create_small_world (pg, &add_connections, ALLOWED);
4001 case GNUNET_TESTING_TOPOLOGY_RING:
4002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
4003 num_connections = create_ring (pg, &add_connections, ALLOWED);
4005 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
4007 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
4009 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating Erdos-Renyi topology\n"));
4011 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
4013 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
4015 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
4017 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating Scale Free topology\n"));
4019 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
4021 case GNUNET_TESTING_TOPOLOGY_LINE:
4022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4023 _("Creating straight line topology\n"));
4024 num_connections = create_line (pg, &add_connections, ALLOWED);
4026 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
4027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating topology from file!\n"));
4029 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing",
4030 "topology_file", &filename))
4032 create_from_file (pg, filename, &add_connections, ALLOWED);
4035 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4036 "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
4037 num_connections = 0;
4040 case GNUNET_TESTING_TOPOLOGY_NONE:
4041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4043 ("Creating no allowed topology (all peers can connect at core level)\n"));
4044 num_connections = pg->total * pg->total; /* Clique is allowed! */
4047 num_connections = 0;
4052 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
4054 ret = create_and_copy_friend_files (pg);
4055 if (ret != GNUNET_OK)
4057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4058 _("Failed during friend file copying!\n"));
4059 return GNUNET_SYSERR;
4063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4064 _("Friend files created/copied successfully!\n"));
4068 /* Use the create clique method to initially set all connections as blacklisted. */
4069 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) &&
4070 (restrict_topology != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
4071 create_clique (pg, &add_connections, BLACKLIST, GNUNET_NO);
4073 return num_connections;
4075 unblacklisted_connections = 0;
4076 /* Un-blacklist connections as per the topology specified */
4077 switch (restrict_topology)
4079 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4081 _("Blacklisting all but clique topology\n"));
4082 unblacklisted_connections =
4083 create_clique (pg, &remove_connections, BLACKLIST, GNUNET_NO);
4085 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4087 _("Blacklisting all but small world (ring) topology\n"));
4088 unblacklisted_connections =
4089 create_small_world_ring (pg, &remove_connections, BLACKLIST);
4091 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4093 _("Blacklisting all but small world (2d-torus) topology\n"));
4094 unblacklisted_connections =
4095 create_small_world (pg, &remove_connections, BLACKLIST);
4097 case GNUNET_TESTING_TOPOLOGY_RING:
4098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4099 _("Blacklisting all but ring topology\n"));
4100 unblacklisted_connections =
4101 create_ring (pg, &remove_connections, BLACKLIST);
4103 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4105 _("Blacklisting all but 2d torus topology\n"));
4106 unblacklisted_connections =
4107 create_2d_torus (pg, &remove_connections, BLACKLIST);
4109 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4111 _("Blacklisting all but Erdos-Renyi topology\n"));
4112 unblacklisted_connections =
4113 create_erdos_renyi (pg, &remove_connections, BLACKLIST);
4115 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4117 _("Blacklisting all but InterNAT topology\n"));
4120 for (off = 0; off < pg->total; off++)
4122 conn_iter = pg->peers[off].allowed_peers_head;
4123 while (conn_iter != NULL)
4125 temp_conn = conn_iter->next;
4126 GNUNET_free (conn_iter);
4127 conn_iter = temp_conn;
4129 pg->peers[off].allowed_peers_head = NULL;
4130 pg->peers[off].allowed_peers_tail = NULL;
4132 conn_iter = pg->peers[off].connect_peers_head;
4133 while (conn_iter != NULL)
4135 temp_conn = conn_iter->next;
4136 GNUNET_free (conn_iter);
4137 conn_iter = temp_conn;
4139 pg->peers[off].connect_peers_head = NULL;
4140 pg->peers[off].connect_peers_tail = NULL;
4142 unblacklisted_connections =
4143 create_nated_internet_copy (pg, &remove_connections, BLACKLIST);
4145 unblacklisted_connections =
4146 create_nated_internet (pg, &remove_connections, BLACKLIST);
4150 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4152 _("Blacklisting all but Scale Free topology\n"));
4153 unblacklisted_connections =
4154 create_scale_free (pg, &remove_connections, BLACKLIST);
4156 case GNUNET_TESTING_TOPOLOGY_LINE:
4157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4158 _("Blacklisting all but straight line topology\n"));
4159 unblacklisted_connections =
4160 create_line (pg, &remove_connections, BLACKLIST);
4165 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
4167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating blacklist with `%s'\n",
4168 restrict_transports);
4169 ret = create_and_copy_blacklist_files (pg, restrict_transports);
4170 if (ret != GNUNET_OK)
4172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4173 _("Failed during blacklist file copying!\n"));
4178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4179 _("Blacklist files created/copied successfully!\n"));
4182 return num_connections;
4187 * Iterator for choosing random peers to connect.
4189 * @param cls closure, a RandomContext
4190 * @param key the key the second Daemon was stored under
4191 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4193 * @return GNUNET_YES to continue iteration
4196 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4198 struct RandomContext *random_ctx = cls;
4199 double random_number;
4200 uint32_t second_pos;
4201 GNUNET_HashCode first_hash;
4205 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4206 UINT64_MAX)) / ((double) UINT64_MAX);
4207 if (random_number < random_ctx->percentage)
4209 GNUNET_assert (GNUNET_OK ==
4210 GNUNET_CONTAINER_multihashmap_put (random_ctx->
4211 first->connect_peers_working_set,
4213 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4216 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
4217 uid_from_hash (key, &second_pos);
4218 hash_from_uid (random_ctx->first_uid, &first_hash);
4219 GNUNET_assert (random_ctx->pg->total > second_pos);
4220 GNUNET_assert (GNUNET_YES ==
4221 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
4223 [second_pos].connect_peers,
4232 * Iterator for adding at least X peers to a peers connection set.
4234 * @param cls closure, MinimumContext
4235 * @param key the key the second Daemon was stored under
4236 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4238 * @return GNUNET_YES to continue iteration
4241 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4243 struct MinimumContext *min_ctx = cls;
4244 uint32_t second_pos;
4245 GNUNET_HashCode first_hash;
4248 if (GNUNET_CONTAINER_multihashmap_size
4249 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
4251 for (i = 0; i < min_ctx->num_to_add; i++)
4253 if (min_ctx->pg_array[i] == min_ctx->current)
4255 GNUNET_assert (GNUNET_OK ==
4256 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4257 first->connect_peers_working_set,
4259 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4260 uid_from_hash (key, &second_pos);
4261 hash_from_uid (min_ctx->first_uid, &first_hash);
4262 GNUNET_assert (min_ctx->pg->total > second_pos);
4263 GNUNET_assert (GNUNET_OK ==
4264 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4266 [second_pos].connect_peers_working_set,
4270 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4271 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
4272 GNUNET_assert (GNUNET_YES ==
4273 GNUNET_CONTAINER_multihashmap_remove (min_ctx->
4275 [second_pos].connect_peers,
4285 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
4290 * Iterator for adding peers to a connection set based on a depth first search.
4292 * @param cls closure, MinimumContext
4293 * @param key the key the second daemon was stored under
4294 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4296 * @return GNUNET_YES to continue iteration
4299 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4301 struct DFSContext *dfs_ctx = cls;
4302 GNUNET_HashCode first_hash;
4304 if (dfs_ctx->current == dfs_ctx->chosen)
4306 GNUNET_assert (GNUNET_OK ==
4307 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4308 first->connect_peers_working_set,
4310 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4311 uid_from_hash (key, &dfs_ctx->second_uid);
4312 hash_from_uid (dfs_ctx->first_uid, &first_hash);
4313 GNUNET_assert (GNUNET_OK ==
4314 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4316 second_uid].connect_peers_working_set,
4318 dfs_ctx->first->daemon,
4319 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4320 GNUNET_assert (GNUNET_YES ==
4321 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
4323 [dfs_ctx->second_uid].connect_peers,
4327 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
4328 return GNUNET_NO; /* We have found our peer, don't iterate more */
4337 * From the set of connections possible, choose percentage percent of connections
4338 * to actually connect.
4340 * @param pg the peergroup we are dealing with
4341 * @param percentage what percent of total connections to make
4344 choose_random_connections (struct GNUNET_TESTING_PeerGroup *pg,
4350 struct PeerConnection *conn_iter;
4351 double random_number;
4353 struct RandomContext random_ctx;
4356 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4359 conn_iter = pg->peers[pg_iter].connect_peers_head;
4360 while (conn_iter != NULL)
4364 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4365 UINT64_MAX)) / ((double) UINT64_MAX);
4366 if (random_number < percentage)
4368 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4371 conn_iter = conn_iter->next;
4374 random_ctx.first_uid = pg_iter;
4375 random_ctx.first = &pg->peers[pg_iter];
4376 random_ctx.percentage = percentage;
4378 pg->peers[pg_iter].connect_peers_working_set =
4379 GNUNET_CONTAINER_multihashmap_create (pg->total);
4380 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4381 &random_connect_iterator,
4383 /* Now remove the old connections */
4384 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[pg_iter].connect_peers);
4385 /* And replace with the random set */
4386 pg->peers[pg_iter].connect_peers =
4387 pg->peers[pg_iter].connect_peers_working_set;
4391 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4393 conn_iter = pg->peers[pg_iter].connect_peers_head;
4394 while (pg->peers[pg_iter].connect_peers_head != NULL)
4395 remove_connections (pg, pg_iter,
4396 pg->peers[pg_iter].connect_peers_head->index, CONNECT,
4399 pg->peers[pg_iter].connect_peers_head =
4400 pg->peers[pg_iter].connect_peers_working_set_head;
4401 pg->peers[pg_iter].connect_peers_tail =
4402 pg->peers[pg_iter].connect_peers_working_set_tail;
4403 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4404 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4409 * Count the number of connections in a linked list of connections.
4411 * @param conn_list the connection list to get the count of
4413 * @return the number of elements in the list
4416 count_connections (struct PeerConnection *conn_list)
4418 struct PeerConnection *iter;
4423 while (iter != NULL)
4432 count_workingset_connections (struct GNUNET_TESTING_PeerGroup *pg)
4435 unsigned int pg_iter;
4438 struct PeerConnection *conn_iter;
4442 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4445 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
4446 while (conn_iter != NULL)
4449 conn_iter = conn_iter->next;
4453 GNUNET_CONTAINER_multihashmap_size (pg->
4455 [pg_iter].connect_peers_working_set);
4463 count_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg)
4466 unsigned int pg_iter;
4469 struct PeerConnection *conn_iter;
4473 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4476 conn_iter = pg->peers[pg_iter].allowed_peers_head;
4477 while (conn_iter != NULL)
4480 conn_iter = conn_iter->next;
4484 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].allowed_peers);
4492 * From the set of connections possible, choose at least num connections per
4495 * @param pg the peergroup we are dealing with
4496 * @param num how many connections at least should each peer have (if possible)?
4499 choose_minimum (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4502 struct MinimumContext minimum_ctx;
4504 struct PeerConnection *conn_iter;
4505 unsigned int temp_list_size;
4508 uint32_t random; /* Random list entry to connect peer to */
4513 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4515 temp_list_size = count_connections (pg->peers[pg_iter].connect_peers_head);
4516 if (temp_list_size == 0)
4518 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Peer %d has 0 connections!?!?\n",
4522 for (i = 0; i < num; i++)
4525 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, temp_list_size);
4526 conn_iter = pg->peers[pg_iter].connect_peers_head;
4527 for (count = 0; count < random; count++)
4528 conn_iter = conn_iter->next;
4529 /* We now have a random connection, connect it! */
4530 GNUNET_assert (conn_iter != NULL);
4531 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET, GNUNET_YES);
4535 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4537 pg->peers[pg_iter].connect_peers_working_set =
4538 GNUNET_CONTAINER_multihashmap_create (num);
4541 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4543 minimum_ctx.first_uid = pg_iter;
4544 minimum_ctx.pg_array =
4545 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
4546 GNUNET_CONTAINER_multihashmap_size
4547 (pg->peers[pg_iter].connect_peers));
4548 minimum_ctx.first = &pg->peers[pg_iter];
4549 minimum_ctx.pg = pg;
4550 minimum_ctx.num_to_add = num;
4551 minimum_ctx.current = 0;
4552 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4553 &minimum_connect_iterator,
4557 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4559 /* Remove the "old" connections */
4560 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[pg_iter].connect_peers);
4561 /* And replace with the working set */
4562 pg->peers[pg_iter].connect_peers =
4563 pg->peers[pg_iter].connect_peers_working_set;
4566 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4568 while (pg->peers[pg_iter].connect_peers_head != NULL)
4570 conn_iter = pg->peers[pg_iter].connect_peers_head;
4571 GNUNET_CONTAINER_DLL_remove (pg->peers[pg_iter].connect_peers_head,
4572 pg->peers[pg_iter].connect_peers_tail,
4574 GNUNET_free (conn_iter);
4575 /*remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT, GNUNET_YES); */
4578 pg->peers[pg_iter].connect_peers_head =
4579 pg->peers[pg_iter].connect_peers_working_set_head;
4580 pg->peers[pg_iter].connect_peers_tail =
4581 pg->peers[pg_iter].connect_peers_working_set_tail;
4582 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4583 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4588 struct FindClosestContext
4591 * The currently known closest peer.
4593 struct GNUNET_TESTING_Daemon *closest;
4596 * The info for the peer we are adding connections for.
4598 struct PeerData *curr_peer;
4601 * The distance (bits) between the current
4602 * peer and the currently known closest.
4604 unsigned int closest_dist;
4607 * The offset of the closest known peer in
4610 unsigned int closest_num;
4614 * Iterator over hash map entries of the allowed
4615 * peer connections. Find the closest, not already
4616 * connected peer and return it.
4618 * @param cls closure (struct FindClosestContext)
4619 * @param key current key code (hash of offset in pg)
4620 * @param value value in the hash map - a GNUNET_TESTING_Daemon
4621 * @return GNUNET_YES if we should continue to
4626 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
4628 struct FindClosestContext *closest_ctx = cls;
4629 struct GNUNET_TESTING_Daemon *daemon = value;
4631 if (((closest_ctx->closest == NULL) ||
4632 (GNUNET_CRYPTO_hash_matching_bits
4633 (&daemon->id.hashPubKey,
4634 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
4635 closest_ctx->closest_dist)) &&
4637 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
4638 curr_peer->connect_peers, key)))
4640 closest_ctx->closest_dist =
4641 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
4642 &closest_ctx->curr_peer->daemon->
4644 closest_ctx->closest = daemon;
4645 uid_from_hash (key, &closest_ctx->closest_num);
4651 * From the set of connections possible, choose at num connections per
4652 * peer based on depth which are closest out of those allowed. Guaranteed
4653 * to add num peers to connect to, provided there are that many peers
4654 * in the underlay topology to connect to.
4656 * @param pg the peergroup we are dealing with
4657 * @param num how many connections at least should each peer have (if possible)?
4658 * @param proc processor to actually add the connections
4659 * @param list the peer list to use
4662 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
4663 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
4668 struct FindClosestContext closest_ctx;
4673 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
4675 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4677 closest_ctx.curr_peer = &pg->peers[pg_iter];
4678 closest_ctx.closest = NULL;
4679 closest_ctx.closest_dist = 0;
4680 closest_ctx.closest_num = 0;
4681 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
4682 &find_closest_peers, &closest_ctx);
4683 if (closest_ctx.closest != NULL)
4685 GNUNET_assert (closest_ctx.closest_num < pg->total);
4686 proc (pg, pg_iter, closest_ctx.closest_num, list);
4694 * From the set of connections possible, choose at least num connections per
4695 * peer based on depth first traversal of peer connections. If DFS leaves
4696 * peers unconnected, ensure those peers get connections.
4698 * @param pg the peergroup we are dealing with
4699 * @param num how many connections at least should each peer have (if possible)?
4702 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4706 uint32_t starting_peer;
4707 uint32_t least_connections;
4708 uint32_t random_connection;
4711 unsigned int temp_count;
4712 struct PeerConnection *peer_iter;
4714 struct DFSContext dfs_ctx;
4715 GNUNET_HashCode second_hash;
4721 while ((count_workingset_connections (pg) < num * pg->total) &&
4722 (count_allowed_connections (pg) > 0))
4724 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4726 least_connections = -1; /* Set to very high number */
4727 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4730 count_connections (pg->
4731 peers[pg_iter].connect_peers_working_set_head);
4732 if (temp_count < least_connections)
4734 starting_peer = pg_iter;
4735 least_connections = temp_count;
4741 count_connections (pg->peers[starting_peer].connect_peers_head);
4742 if (temp_count == 0)
4743 continue; /* FIXME: infinite loop? */
4746 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, temp_count);
4748 peer_iter = pg->peers[starting_peer].connect_peers_head;
4749 while (temp_count < random_connection)
4751 peer_iter = peer_iter->next;
4754 GNUNET_assert (peer_iter != NULL);
4755 add_connections (pg, starting_peer, peer_iter->index, WORKING_SET,
4757 remove_connections (pg, starting_peer, peer_iter->index, CONNECT,
4759 starting_peer = peer_iter->index;
4764 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4766 pg->peers[pg_iter].connect_peers_working_set =
4767 GNUNET_CONTAINER_multihashmap_create (num);
4772 while ((count_workingset_connections (pg) < num * pg->total) &&
4773 (count_allowed_connections (pg) > 0))
4775 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4777 least_connections = -1; /* Set to very high number */
4778 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4780 if (GNUNET_CONTAINER_multihashmap_size
4781 (pg->peers[pg_iter].connect_peers_working_set) < least_connections)
4783 starting_peer = pg_iter;
4785 GNUNET_CONTAINER_multihashmap_size (pg->
4787 [pg_iter].connect_peers_working_set);
4792 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4798 /* Choose a random peer from the chosen peers set of connections to add */
4800 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4801 GNUNET_CONTAINER_multihashmap_size (pg->peers
4802 [starting_peer].connect_peers));
4803 dfs_ctx.first_uid = starting_peer;
4804 dfs_ctx.first = &pg->peers[starting_peer];
4806 dfs_ctx.current = 0;
4808 GNUNET_CONTAINER_multihashmap_iterate (pg->
4809 peers[starting_peer].connect_peers,
4810 &dfs_connect_iterator, &dfs_ctx);
4811 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4812 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4813 GNUNET_assert (GNUNET_YES ==
4814 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4815 [starting_peer].connect_peers,
4819 [dfs_ctx.second_uid].daemon));
4820 starting_peer = dfs_ctx.second_uid;
4823 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4825 /* Remove the "old" connections */
4826 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[pg_iter].connect_peers);
4827 /* And replace with the working set */
4828 pg->peers[pg_iter].connect_peers =
4829 pg->peers[pg_iter].connect_peers_working_set;
4835 * Internal callback for topology information for a particular peer.
4838 internal_topology_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
4839 const struct GNUNET_ATS_Information *atsi,
4840 unsigned int atsi_count)
4842 struct CoreContext *core_ctx = cls;
4843 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4845 if (peer == NULL) /* Either finished, or something went wrong */
4847 iter_ctx->completed++;
4848 iter_ctx->connected--;
4849 /* One core context allocated per iteration, must free! */
4850 GNUNET_free (core_ctx);
4854 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id, peer, NULL);
4857 if (iter_ctx->completed == iter_ctx->total)
4859 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4860 /* Once all are done, free the iteration context */
4861 GNUNET_free (iter_ctx);
4866 * Check running topology iteration tasks, if below max start a new one, otherwise
4867 * schedule for some time in the future.
4870 schedule_get_topology (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4872 struct CoreContext *core_context = cls;
4873 struct TopologyIterateContext *topology_context =
4874 (struct TopologyIterateContext *) core_context->iter_context;
4875 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
4878 if (topology_context->connected >
4879 topology_context->pg->max_outstanding_connections)
4881 #if VERBOSE_TESTING > 2
4882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4884 ("Delaying connect, we have too many outstanding connections!\n"));
4886 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4887 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4888 &schedule_get_topology, core_context);
4892 #if VERBOSE_TESTING > 2
4893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4894 _("Creating connection, outstanding_connections is %d\n"),
4895 outstanding_connects);
4897 topology_context->connected++;
4900 GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4901 &internal_topology_callback, core_context))
4903 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4904 internal_topology_callback (core_context, NULL, NULL, 0);
4910 * Iterate over all (running) peers in the peer group, retrieve
4911 * all connections that each currently has.
4914 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
4915 GNUNET_TESTING_NotifyTopology cb, void *cls)
4917 struct TopologyIterateContext *topology_context;
4918 struct CoreContext *core_ctx;
4920 unsigned int total_count;
4922 /* Allocate a single topology iteration context */
4923 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4924 topology_context->topology_cb = cb;
4925 topology_context->cls = cls;
4926 topology_context->pg = pg;
4928 for (i = 0; i < pg->total; i++)
4930 if (pg->peers[i].daemon->running == GNUNET_YES)
4932 /* Allocate one core context per core we need to connect to */
4933 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4934 core_ctx->daemon = pg->peers[i].daemon;
4935 /* Set back pointer to topology iteration context */
4936 core_ctx->iter_context = topology_context;
4937 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4941 if (total_count == 0)
4943 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4944 GNUNET_free (topology_context);
4947 topology_context->total = total_count;
4952 * Callback function to process statistic values.
4953 * This handler is here only really to insert a peer
4954 * identity (or daemon) so the statistics can be uniquely
4955 * tied to a single running peer.
4957 * @param cls closure
4958 * @param subsystem name of subsystem that created the statistic
4959 * @param name the name of the datum
4960 * @param value the current value
4961 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4962 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4965 internal_stats_callback (void *cls, const char *subsystem, const char *name,
4966 uint64_t value, int is_persistent)
4968 struct StatsCoreContext *core_context = cls;
4969 struct StatsIterateContext *stats_context =
4970 (struct StatsIterateContext *) core_context->iter_context;
4972 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4973 subsystem, name, value, is_persistent);
4977 * Internal continuation call for statistics iteration.
4979 * @param cls closure, the CoreContext for this iteration
4980 * @param success whether or not the statistics iterations
4981 * was canceled or not (we don't care)
4984 internal_stats_cont (void *cls, int success)
4986 struct StatsCoreContext *core_context = cls;
4987 struct StatsIterateContext *stats_context =
4988 (struct StatsIterateContext *) core_context->iter_context;
4990 stats_context->connected--;
4991 stats_context->completed++;
4993 if (stats_context->completed == stats_context->total)
4995 stats_context->cont (stats_context->cls, GNUNET_YES);
4996 GNUNET_free (stats_context);
4999 if (core_context->stats_handle != NULL)
5000 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
5002 GNUNET_free (core_context);
5006 * Check running topology iteration tasks, if below max start a new one, otherwise
5007 * schedule for some time in the future.
5010 schedule_get_statistics (void *cls,
5011 const struct GNUNET_SCHEDULER_TaskContext *tc)
5013 struct StatsCoreContext *core_context = cls;
5014 struct StatsIterateContext *stats_context =
5015 (struct StatsIterateContext *) core_context->iter_context;
5017 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5020 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
5022 #if VERBOSE_TESTING > 2
5023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5025 ("Delaying connect, we have too many outstanding connections!\n"));
5027 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5028 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5029 &schedule_get_statistics, core_context);
5033 #if VERBOSE_TESTING > 2
5034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5035 _("Creating connection, outstanding_connections is %d\n"),
5036 outstanding_connects);
5039 stats_context->connected++;
5040 core_context->stats_handle =
5041 GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
5042 if (core_context->stats_handle == NULL)
5044 internal_stats_cont (core_context, GNUNET_NO);
5048 core_context->stats_get_handle =
5049 GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
5050 GNUNET_TIME_relative_get_forever (),
5051 &internal_stats_cont, &internal_stats_callback,
5053 if (core_context->stats_get_handle == NULL)
5054 internal_stats_cont (core_context, GNUNET_NO);
5059 struct DuplicateStats
5062 * Next item in the list
5064 struct DuplicateStats *next;
5067 * Nasty string, concatenation of relevant information.
5069 char *unique_string;
5073 * Check whether the combination of port/host/unix domain socket
5074 * already exists in the list of peers being checked for statistics.
5076 * @param pg the peergroup in question
5077 * @param specific_peer the peer we're concerned with
5078 * @param stats_list the list to return to the caller
5080 * @return GNUNET_YES if the statistics instance has been seen already,
5081 * GNUNET_NO if not (and we may have added it to the list)
5084 stats_check_existing (struct GNUNET_TESTING_PeerGroup *pg,
5085 struct PeerData *specific_peer,
5086 struct DuplicateStats **stats_list)
5088 struct DuplicateStats *pos;
5089 char *unix_domain_socket;
5090 unsigned long long port;
5094 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
5095 "single_statistics_per_host"))
5096 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
5100 GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg, "statistics",
5101 "unixpath", &unix_domain_socket))
5105 GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg, "statistics",
5108 GNUNET_free (unix_domain_socket);
5112 if (specific_peer->daemon->hostname != NULL)
5113 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
5114 unix_domain_socket, port);
5116 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
5120 if (0 == strcmp (to_match, pos->unique_string))
5122 GNUNET_free (unix_domain_socket);
5123 GNUNET_free (to_match);
5128 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
5129 pos->unique_string = to_match;
5130 pos->next = *stats_list;
5132 GNUNET_free (unix_domain_socket);
5137 * Iterate over all (running) peers in the peer group, retrieve
5138 * all statistics from each.
5140 * @param pg the peergroup to iterate statistics of
5141 * @param cont continuation to call once all stats have been retrieved
5142 * @param proc processing function for each statistic from each peer
5143 * @param cls closure to pass to proc
5147 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
5148 GNUNET_STATISTICS_Callback cont,
5149 GNUNET_TESTING_STATISTICS_Iterator proc,
5152 struct StatsIterateContext *stats_context;
5153 struct StatsCoreContext *core_ctx;
5155 unsigned int total_count;
5156 struct DuplicateStats *stats_list;
5157 struct DuplicateStats *pos;
5161 /* Allocate a single stats iteration context */
5162 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
5163 stats_context->cont = cont;
5164 stats_context->proc = proc;
5165 stats_context->cls = cls;
5166 stats_context->pg = pg;
5169 for (i = 0; i < pg->total; i++)
5171 if ((pg->peers[i].daemon->running == GNUNET_YES) &&
5172 (GNUNET_NO == stats_check_existing (pg, &pg->peers[i], &stats_list)))
5174 /* Allocate one core context per core we need to connect to */
5175 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
5176 core_ctx->daemon = pg->peers[i].daemon;
5177 /* Set back pointer to topology iteration context */
5178 core_ctx->iter_context = stats_context;
5179 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
5184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5185 "Retrieving stats from %u total instances.\n", total_count);
5186 if (0 != total_count)
5187 stats_context->total = total_count;
5189 GNUNET_free (stats_context);
5190 if (stats_list != NULL)
5195 GNUNET_free (pos->unique_string);
5196 stats_list = pos->next;
5198 pos = stats_list->next;
5205 * Stop the connection process temporarily.
5207 * @param pg the peer group to stop connecting
5210 GNUNET_TESTING_stop_connections (struct GNUNET_TESTING_PeerGroup *pg)
5212 pg->stop_connects = GNUNET_YES;
5216 * Resume the connection process temporarily.
5218 * @param pg the peer group to resume connecting
5221 GNUNET_TESTING_resume_connections (struct GNUNET_TESTING_PeerGroup *pg)
5223 pg->stop_connects = GNUNET_NO;
5227 * There are many ways to connect peers that are supported by this function.
5228 * To connect peers in the same topology that was created via the
5229 * GNUNET_TESTING_create_topology, the topology variable must be set to
5230 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
5231 * a new instance of that topology will be generated and attempted to be
5232 * connected. This could result in some connections being impossible,
5233 * because some topologies are non-deterministic.
5235 * @param pg the peer group struct representing the running peers
5236 * @param topology which topology to connect the peers in
5237 * @param options options for connecting the topology
5238 * @param option_modifier modifier for options that take a parameter
5239 * @param connect_timeout how long to wait before giving up on connecting
5241 * @param connect_attempts how many times to attempt to connect two peers
5242 * over the connect_timeout duration
5243 * @param notify_callback notification to be called once all connections completed
5244 * @param notify_cls closure for notification callback
5246 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
5249 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
5250 enum GNUNET_TESTING_Topology topology,
5251 enum GNUNET_TESTING_TopologyOption options,
5252 double option_modifier,
5253 struct GNUNET_TIME_Relative connect_timeout,
5254 unsigned int connect_attempts,
5255 GNUNET_TESTING_NotifyCompletion
5256 notify_callback, void *notify_cls)
5260 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
5261 #if VERBOSE_TOPOLOGY
5262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5263 _("Creating clique CONNECT topology\n"));
5265 create_clique (pg, &add_connections, CONNECT, GNUNET_NO);
5267 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
5268 #if VERBOSE_TOPOLOGY
5269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5270 _("Creating small world (ring) CONNECT topology\n"));
5272 create_small_world_ring (pg, &add_connections, CONNECT);
5274 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
5275 #if VERBOSE_TOPOLOGY
5276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5277 _("Creating small world (2d-torus) CONNECT topology\n"));
5279 create_small_world (pg, &add_connections, CONNECT);
5281 case GNUNET_TESTING_TOPOLOGY_RING:
5282 #if VERBOSE_TOPOLOGY
5283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring CONNECT topology\n"));
5285 create_ring (pg, &add_connections, CONNECT);
5287 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
5288 #if VERBOSE_TOPOLOGY
5289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5290 _("Creating 2d torus CONNECT topology\n"));
5292 create_2d_torus (pg, &add_connections, CONNECT);
5294 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
5295 #if VERBOSE_TOPOLOGY
5296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5297 _("Creating Erdos-Renyi CONNECT topology\n"));
5299 create_erdos_renyi (pg, &add_connections, CONNECT);
5301 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
5302 #if VERBOSE_TOPOLOGY
5303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5304 _("Creating InterNAT CONNECT topology\n"));
5306 create_nated_internet (pg, &add_connections, CONNECT);
5308 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
5309 #if VERBOSE_TOPOLOGY
5310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5311 _("Creating Scale Free CONNECT topology\n"));
5313 create_scale_free (pg, &add_connections, CONNECT);
5315 case GNUNET_TESTING_TOPOLOGY_LINE:
5316 #if VERBOSE_TOPOLOGY
5317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5318 _("Creating straight line CONNECT topology\n"));
5320 create_line (pg, &add_connections, CONNECT);
5322 case GNUNET_TESTING_TOPOLOGY_NONE:
5323 #if VERBOSE_TOPOLOGY
5324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating no CONNECT topology\n"));
5326 copy_allowed_topology (pg);
5329 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5330 _("Unknown topology specification, can't connect peers!\n"));
5331 return GNUNET_SYSERR;
5336 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
5337 #if VERBOSE_TOPOLOGY
5338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5340 ("Connecting random subset (%'.2f percent) of possible peers\n"),
5341 100 * option_modifier);
5343 choose_random_connections (pg, option_modifier);
5345 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
5346 #if VERBOSE_TOPOLOGY
5347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5348 _("Connecting a minimum of %u peers each (if possible)\n"),
5349 (unsigned int) option_modifier);
5351 choose_minimum (pg, (unsigned int) option_modifier);
5353 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
5354 #if VERBOSE_TOPOLOGY
5355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5357 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
5358 (unsigned int) option_modifier);
5361 perform_dfs (pg, (int) option_modifier);
5364 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
5365 #if VERBOSE_TOPOLOGY
5366 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5367 _("Finding additional %u closest peers each (if possible)\n"),
5368 (unsigned int) option_modifier);
5371 add_closest (pg, (unsigned int) option_modifier, &add_connections, CONNECT);
5374 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
5376 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
5382 return connect_topology (pg, connect_timeout, connect_attempts,
5383 notify_callback, notify_cls);
5387 * Lookup and return the number of SSH connections to a host.
5389 * @param hostname the hostname to lookup in the list
5390 * @param pg the peergroup that the host belongs to
5392 * @return the number of current ssh connections to the host
5395 count_outstanding_at_host (const char *hostname,
5396 struct GNUNET_TESTING_PeerGroup *pg)
5398 struct OutstandingSSH *pos;
5401 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5403 GNUNET_assert (pos != NULL);
5404 return pos->outstanding;
5408 * Increment the number of SSH connections to a host by one.
5410 * @param hostname the hostname to lookup in the list
5411 * @param pg the peergroup that the host belongs to
5415 increment_outstanding_at_host (const char *hostname,
5416 struct GNUNET_TESTING_PeerGroup *pg)
5418 struct OutstandingSSH *pos;
5421 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5423 GNUNET_assert (pos != NULL);
5428 * Decrement the number of SSH connections to a host by one.
5430 * @param hostname the hostname to lookup in the list
5431 * @param pg the peergroup that the host belongs to
5435 decrement_outstanding_at_host (const char *hostname,
5436 struct GNUNET_TESTING_PeerGroup *pg)
5438 struct OutstandingSSH *pos;
5441 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5443 GNUNET_assert (pos != NULL);
5448 * Callback that is called whenever a hostkey is generated
5449 * for a peer. Call the real callback and decrement the
5450 * starting counter for the peergroup.
5452 * @param cls closure
5453 * @param id identifier for the daemon, NULL on error
5454 * @param d handle for the daemon
5455 * @param emsg error message (NULL on success)
5458 internal_hostkey_callback (void *cls, const struct GNUNET_PeerIdentity *id,
5459 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5461 struct InternalStartContext *internal_context = cls;
5463 internal_context->peer->pg->starting--;
5464 internal_context->peer->pg->started++;
5465 if (internal_context->hostname != NULL)
5466 decrement_outstanding_at_host (internal_context->hostname,
5467 internal_context->peer->pg);
5468 if (internal_context->hostkey_callback != NULL)
5469 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
5471 else if (internal_context->peer->pg->started ==
5472 internal_context->peer->pg->total)
5474 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
5475 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
5480 * Callback that is called whenever a peer has finished starting.
5481 * Call the real callback and decrement the starting counter
5482 * for the peergroup.
5484 * @param cls closure
5485 * @param id identifier for the daemon, NULL on error
5487 * @param d handle for the daemon
5488 * @param emsg error message (NULL on success)
5491 internal_startup_callback (void *cls, const struct GNUNET_PeerIdentity *id,
5492 const struct GNUNET_CONFIGURATION_Handle *cfg,
5493 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5495 struct InternalStartContext *internal_context = cls;
5497 internal_context->peer->pg->starting--;
5498 if (internal_context->hostname != NULL)
5499 decrement_outstanding_at_host (internal_context->hostname,
5500 internal_context->peer->pg);
5501 if (internal_context->start_cb != NULL)
5502 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
5508 * Calls GNUNET_TESTING_daemon_continue_startup to set the daemon's state
5509 * from HOSTKEY_CREATED to TOPOLOGY_SETUP. Makes sure not to saturate a host
5510 * with requests delaying them when needed.
5512 * @param cls closure: internal context of the daemon.
5513 * @param tc TaskContext
5516 internal_continue_startup (void *cls,
5517 const struct GNUNET_SCHEDULER_TaskContext *tc)
5519 struct InternalStartContext *internal_context = cls;
5521 internal_context->peer->startup_task = GNUNET_SCHEDULER_NO_TASK;
5523 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5528 if ((internal_context->peer->pg->starting <
5529 internal_context->peer->pg->max_concurrent_ssh) ||
5530 ((internal_context->hostname != NULL) &&
5531 (count_outstanding_at_host
5532 (internal_context->hostname,
5533 internal_context->peer->pg) <
5534 internal_context->peer->pg->max_concurrent_ssh)))
5536 if (internal_context->hostname != NULL)
5537 increment_outstanding_at_host (internal_context->hostname,
5538 internal_context->peer->pg);
5539 internal_context->peer->pg->starting++;
5540 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
5544 internal_context->peer->startup_task =
5545 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5546 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5547 &internal_continue_startup,
5553 * Callback for informing us about a successful
5554 * or unsuccessful churn start call.
5556 * @param cls a ChurnContext
5557 * @param id the peer identity of the started peer
5558 * @param cfg the handle to the configuration of the peer
5559 * @param d handle to the daemon for the peer
5560 * @param emsg NULL on success, non-NULL on failure
5564 churn_start_callback (void *cls, const struct GNUNET_PeerIdentity *id,
5565 const struct GNUNET_CONFIGURATION_Handle *cfg,
5566 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5568 struct ChurnRestartContext *startup_ctx = cls;
5569 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
5571 unsigned int total_left;
5572 char *error_message;
5574 error_message = NULL;
5577 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5578 "Churn stop callback failed with error `%s'\n", emsg);
5579 churn_ctx->num_failed_start++;
5583 churn_ctx->num_to_start--;
5587 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
5588 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5590 if (total_left == 0)
5592 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
5593 GNUNET_asprintf (&error_message,
5594 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5595 churn_ctx->num_failed_start, churn_ctx->num_failed_stop);
5596 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5597 GNUNET_free_non_null (error_message);
5598 GNUNET_free (churn_ctx);
5599 GNUNET_free (startup_ctx);
5604 schedule_churn_restart (void *cls,
5605 const struct GNUNET_SCHEDULER_TaskContext *tc)
5607 struct PeerRestartContext *peer_restart_ctx = cls;
5608 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
5610 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5611 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5612 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5613 &schedule_churn_restart, peer_restart_ctx);
5616 if (startup_ctx->churn_ctx->service != NULL)
5617 GNUNET_TESTING_daemon_start_stopped_service (peer_restart_ctx->daemon,
5620 startup_ctx->timeout,
5621 &churn_start_callback,
5624 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
5625 startup_ctx->timeout,
5626 &churn_start_callback, startup_ctx);
5627 GNUNET_free (peer_restart_ctx);
5632 * Callback for informing us about a successful
5633 * or unsuccessful churn start call.
5635 * @param cls a struct ServiceStartContext *startup_ctx
5636 * @param id the peer identity of the started peer
5637 * @param cfg the handle to the configuration of the peer
5638 * @param d handle to the daemon for the peer
5639 * @param emsg NULL on success, non-NULL on failure
5643 service_start_callback (void *cls, const struct GNUNET_PeerIdentity *id,
5644 const struct GNUNET_CONFIGURATION_Handle *cfg,
5645 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5647 struct ServiceStartContext *startup_ctx = (struct ServiceStartContext *) cls;
5651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5652 "Service start failed with error `%s'\n", emsg);
5655 startup_ctx->outstanding--;
5656 startup_ctx->remaining--;
5658 if (startup_ctx->remaining == 0)
5660 startup_ctx->cb (startup_ctx->cb_cls, NULL);
5661 GNUNET_free (startup_ctx->service);
5662 GNUNET_free (startup_ctx);
5667 schedule_service_start (void *cls,
5668 const struct GNUNET_SCHEDULER_TaskContext *tc)
5670 struct PeerServiceStartContext *peer_ctx = cls;
5671 struct ServiceStartContext *startup_ctx = peer_ctx->start_ctx;
5673 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5674 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5675 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5676 &schedule_service_start, peer_ctx);
5680 GNUNET_TESTING_daemon_start_service (peer_ctx->daemon, startup_ctx->service,
5681 startup_ctx->timeout,
5682 &service_start_callback, startup_ctx);
5683 GNUNET_free (peer_ctx);
5689 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5691 struct InternalStartContext *internal_context = cls;
5693 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5698 if ((internal_context->peer->pg->starting <
5699 internal_context->peer->pg->max_concurrent_ssh) ||
5700 ((internal_context->hostname != NULL) &&
5701 (count_outstanding_at_host
5702 (internal_context->hostname,
5703 internal_context->peer->pg) <
5704 internal_context->peer->pg->max_concurrent_ssh)))
5706 if (internal_context->hostname != NULL)
5707 increment_outstanding_at_host (internal_context->hostname,
5708 internal_context->peer->pg);
5709 internal_context->peer->pg->starting++;
5710 internal_context->peer->daemon =
5711 GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
5712 internal_context->timeout, GNUNET_NO,
5713 internal_context->hostname,
5714 internal_context->username,
5715 internal_context->sshport,
5716 internal_context->hostkey,
5717 &internal_hostkey_callback,
5719 &internal_startup_callback,
5724 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5725 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5726 &internal_start, internal_context);
5730 #if USE_START_HELPER
5732 struct PeerStartHelperContext
5734 struct GNUNET_TESTING_PeerGroup *pg;
5736 struct HostData *host;
5738 struct GNUNET_OS_Process *proc;
5742 check_peers_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5744 struct PeerStartHelperContext *helper = cls;
5745 enum GNUNET_OS_ProcessStatusType type;
5748 GNUNET_TESTING_NotifyDaemonRunning cb;
5750 if (GNUNET_NO == GNUNET_OS_process_status (helper->proc, &type, &code)) /* Still running, wait some more! */
5752 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
5753 &check_peers_started, helper);
5757 helper->pg->starting--;
5758 if (helper->pg->starting == 0) /* All peers have finished starting! */
5760 /* Call the peer started callback for each peer, set proper FSM state (?) */
5761 for (i = 0; i < helper->pg->total; i++)
5763 cb = helper->pg->peers[i].daemon->cb;
5764 helper->pg->peers[i].daemon->cb = NULL;
5765 helper->pg->peers[i].daemon->running = GNUNET_YES;
5766 helper->pg->peers[i].daemon->phase = SP_START_DONE;
5769 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
5770 cb (helper->pg->peers[i].daemon->cb_cls,
5771 &helper->pg->peers[i].daemon->id,
5772 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5773 "Failed to execute peerStartHelper.pl, or return code bad!");
5775 cb (helper->pg->peers[i].daemon->cb_cls,
5776 &helper->pg->peers[i].daemon->id,
5777 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5784 GNUNET_OS_process_close (helper->proc);
5788 start_peer_helper (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5790 struct PeerStartHelperContext *helper = cls;
5791 char *baseservicehome;
5795 /* ssh user@host peerStartHelper /path/to/basedirectory */
5796 GNUNET_assert (GNUNET_OK ==
5797 GNUNET_CONFIGURATION_get_value_string (helper->pg->cfg,
5798 "PATHS", "SERVICEHOME",
5800 GNUNET_asprintf (&tempdir, "%s/%s/", baseservicehome, helper->host->hostname);
5801 if (NULL != helper->host->username)
5802 GNUNET_asprintf (&arg, "%s@%s", helper->host->username,
5803 helper->host->hostname);
5805 GNUNET_asprintf (&arg, "%s", helper->host->hostname);
5807 /* FIXME: Doesn't support ssh_port option! */
5809 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh", arg,
5810 "peerStartHelper.pl", tempdir, NULL);
5811 GNUNET_assert (helper->proc != NULL);
5813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting peers with cmd ssh %s %s %s\n",
5814 arg, "peerStartHelper.pl", tempdir);
5816 GNUNET_SCHEDULER_add_now (&check_peers_started, helper);
5817 GNUNET_free (tempdir);
5818 GNUNET_free (baseservicehome);
5824 * Function which continues a peer group starting up
5825 * after successfully generating hostkeys for each peer.
5827 * @param pg the peer group to continue starting
5831 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg)
5835 #if USE_START_HELPER
5836 if ((pg->num_hosts > 0) && (pg->hostkey_data != NULL))
5838 struct PeerStartHelperContext *helper;
5840 pg->starting = pg->num_hosts;
5841 for (i = 0; i < pg->num_hosts; i++)
5843 helper = GNUNET_malloc (sizeof (struct PeerStartHelperContext));
5845 helper->host = &pg->hosts[i];
5846 GNUNET_SCHEDULER_add_now (&start_peer_helper, helper);
5852 for (i = 0; i < pg->total; i++)
5854 pg->peers[i].startup_task =
5855 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5856 &pg->peers[i].internal_context);
5861 for (i = 0; i < pg->total; i++)
5863 pg->peers[i].startup_task =
5864 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5865 &pg->peers[i].internal_context);
5870 #if USE_START_HELPER
5872 call_hostkey_callbacks (void *cls,
5873 const struct GNUNET_SCHEDULER_TaskContext *tc)
5875 struct GNUNET_TESTING_PeerGroup *pg = cls;
5878 for (i = 0; i < pg->total; i++)
5880 if (pg->peers[i].internal_context.hostkey_callback != NULL)
5881 pg->peers[i].internal_context.hostkey_callback (pg->peers[i].
5882 internal_context.hostkey_cls,
5883 &pg->peers[i].daemon->id,
5884 pg->peers[i].daemon,
5888 if (pg->peers[0].internal_context.hostkey_callback == NULL)
5889 GNUNET_TESTING_daemons_continue_startup (pg);
5894 * Start count gnunet instances with the same set of transports and
5895 * applications. The port numbers (any option called "PORT") will be
5896 * adjusted to ensure that no two peers running on the same system
5897 * have the same port(s) in their respective configurations.
5899 * @param cfg configuration template to use
5900 * @param total number of daemons to start
5901 * @param max_concurrent_connections for testing, how many peers can
5902 * we connect to simultaneously
5903 * @param max_concurrent_ssh when starting with ssh, how many ssh
5904 * connections will we allow at once (based on remote hosts allowed!)
5905 * @param timeout total time allowed for peers to start
5906 * @param hostkey_callback function to call on each peers hostkey generation
5907 * if NULL, peers will be started by this call, if non-null,
5908 * GNUNET_TESTING_daemons_continue_startup must be called after
5909 * successful hostkey generation
5910 * @param hostkey_cls closure for hostkey callback
5911 * @param cb function to call on each daemon that was started
5912 * @param cb_cls closure for cb
5913 * @param connect_callback function to call each time two hosts are connected
5914 * @param connect_callback_cls closure for connect_callback
5915 * @param hostnames linked list of host structs to use to start peers on
5916 * (NULL to run on localhost only)
5918 * @return NULL on error, otherwise handle to control peer group
5920 struct GNUNET_TESTING_PeerGroup *
5921 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
5923 unsigned int max_concurrent_connections,
5924 unsigned int max_concurrent_ssh,
5925 struct GNUNET_TIME_Relative timeout,
5926 GNUNET_TESTING_NotifyHostkeyCreated
5927 hostkey_callback, void *hostkey_cls,
5928 GNUNET_TESTING_NotifyDaemonRunning cb,
5930 GNUNET_TESTING_NotifyConnection connect_callback,
5931 void *connect_callback_cls,
5932 const struct GNUNET_TESTING_Host *hostnames)
5934 struct GNUNET_TESTING_PeerGroup *pg;
5935 const struct GNUNET_TESTING_Host *hostpos;
5936 const char *hostname;
5937 const char *username;
5938 char *baseservicehome;
5939 char *newservicehome;
5941 char *hostkeys_file;
5944 struct GNUNET_DISK_FileHandle *fd;
5945 struct GNUNET_CONFIGURATION_Handle *pcfg;
5947 struct OutstandingSSH *ssh_entry;
5948 unsigned int hostcnt;
5955 uint64_t total_hostkeys;
5956 struct GNUNET_OS_Process *proc;
5967 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
5969 pg->notify_connection = connect_callback;
5970 pg->notify_connection_cls = connect_callback_cls;
5972 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
5973 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
5974 pg->max_outstanding_connections = max_concurrent_connections;
5975 pg->max_concurrent_ssh = max_concurrent_ssh;
5976 if (NULL != hostnames)
5979 hostpos = hostnames;
5980 while (hostpos != NULL)
5982 hostpos = hostpos->next;
5985 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5988 hostpos = hostnames;
5989 while (hostpos != NULL)
5991 pg->hosts[off].minport = LOW_PORT;
5992 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
5993 if (hostpos->username != NULL)
5994 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
5995 pg->hosts[off].sshport = hostpos->port;
5996 hostpos = hostpos->next;
6006 pg->num_hosts = off;
6014 /* Create the servicehome directory for each remote peer */
6015 GNUNET_assert (GNUNET_OK ==
6016 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
6019 for (i = 0; i < pg->num_hosts; i++)
6021 ssh_entry = GNUNET_malloc (sizeof (struct OutstandingSSH));
6022 ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
6023 GNUNET_CONTAINER_DLL_insert (pg->ssh_head, pg->ssh_tail, ssh_entry);
6024 GNUNET_asprintf (&tmpdir, "%s/%s", baseservicehome, pg->hosts[i].hostname);
6025 if (NULL != pg->hosts[i].username)
6026 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username,
6027 pg->hosts[i].hostname);
6029 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
6030 if (pg->hosts[i].sshport != 0)
6032 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
6034 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh", "-P", ssh_port_str,
6038 arg, "mkdir -p", tmpdir, NULL);
6042 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh", arg, "mkdir -p",
6044 GNUNET_assert (proc != NULL);
6045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6046 "Creating remote dir with command ssh %s %s %s\n", arg,
6047 " mkdir -p ", tmpdir);
6048 GNUNET_free (tmpdir);
6050 GNUNET_OS_process_wait (proc);
6051 GNUNET_OS_process_close (proc);
6053 GNUNET_free (baseservicehome);
6054 baseservicehome = NULL;
6057 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "HOSTKEYSFILE",
6060 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
6061 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6062 _("Could not read hostkeys file!\n"));
6065 /* Check hostkey file size, read entire thing into memory */
6066 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
6067 GNUNET_DISK_PERM_NONE);
6070 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open",
6072 GNUNET_free (hostkeys_file);
6073 for (i = 0; i < pg->num_hosts; i++)
6075 GNUNET_free (pg->hosts[i].hostname);
6076 GNUNET_free_non_null (pg->hosts[i].username);
6078 GNUNET_free (pg->peers);
6079 GNUNET_free (pg->hosts);
6084 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
6087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6088 "Found file size %llu for hostkeys\n", fs);
6090 if (0 != (fs % HOSTKEYFILESIZE))
6092 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6093 "File size %llu seems incorrect for hostkeys...\n", fs);
6097 total_hostkeys = fs / HOSTKEYFILESIZE;
6098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6099 "Will read %llu hostkeys from file\n", total_hostkeys);
6100 pg->hostkey_data = GNUNET_malloc_large (fs);
6101 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
6103 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
6105 GNUNET_free (hostkeys_file);
6108 for (off = 0; off < total; off++)
6112 hostname = pg->hosts[off % hostcnt].hostname;
6113 username = pg->hosts[off % hostcnt].username;
6114 sshport = pg->hosts[off % hostcnt].sshport;
6116 GNUNET_TESTING_create_cfg (cfg, off, &pg->hosts[off % hostcnt].minport, &upnum,
6124 pcfg = GNUNET_TESTING_create_cfg (cfg, off, &minport, &upnum, hostname, &fdnum);
6129 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6131 ("Could not create configuration for peer number %u on `%s'!\n"),
6132 off, hostname == NULL ? "localhost" : hostname);
6137 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
6140 if (hostname != NULL)
6141 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", baseservicehome,
6144 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
6145 GNUNET_free (baseservicehome);
6146 baseservicehome = NULL;
6150 tmpdir = getenv ("TMPDIR");
6151 tmpdir = tmpdir ? tmpdir : "/tmp";
6152 if (hostname != NULL)
6153 GNUNET_asprintf (&newservicehome, "%s/%s/%s/%d/", tmpdir, hostname,
6154 "gnunet-testing-test-test", off);
6156 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", tmpdir,
6157 "gnunet-testing-test-test", off);
6159 GNUNET_CONFIGURATION_set_value_string (pcfg, "PATHS", "SERVICEHOME",
6161 GNUNET_free (newservicehome);
6162 pg->peers[off].cfg = pcfg;
6163 pg->peers[off].pg = pg;
6164 pg->peers[off].internal_context.peer = &pg->peers[off];
6165 pg->peers[off].internal_context.timeout = timeout;
6166 pg->peers[off].internal_context.hostname = hostname;
6167 pg->peers[off].internal_context.username = username;
6168 pg->peers[off].internal_context.sshport = sshport;
6169 if (pg->hostkey_data != NULL)
6170 pg->peers[off].internal_context.hostkey =
6171 &pg->hostkey_data[off * HOSTKEYFILESIZE];
6172 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
6173 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
6174 pg->peers[off].internal_context.start_cb = cb;
6175 pg->peers[off].internal_context.start_cb_cls = cb_cls;
6176 #if !USE_START_HELPER
6177 GNUNET_SCHEDULER_add_now (&internal_start,
6178 &pg->peers[off].internal_context);
6180 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6182 pg->peers[off].daemon =
6183 GNUNET_TESTING_daemon_start (pcfg, timeout, GNUNET_YES, hostname,
6185 pg->peers[off].internal_context.hostkey,
6186 &internal_hostkey_callback,
6187 &pg->peers[off].internal_context,
6188 &internal_startup_callback,
6189 &pg->peers[off].internal_context);
6191 * At this point, given that we had a hostkeyfile,
6192 * we can call the hostkey callback!
6193 * But first, we should copy (rsync) all of the configs
6194 * and hostkeys to the remote peers. Then let topology
6195 * creation happen, then call the peer start helper processes,
6196 * then set pg->whatever_phase for each peer and let them
6197 * enter the fsm to get the HELLO's for peers and start connecting.
6202 GNUNET_SCHEDULER_add_now (&internal_start,
6203 &pg->peers[off].internal_context);
6209 #if USE_START_HELPER /* Now the peergroup has been set up, hostkeys and configs written to files. */
6210 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6212 for (off = 0; off < hostcnt; off++)
6217 hostname = pg->hosts[off % hostcnt].hostname;
6218 username = pg->hosts[off % hostcnt].username;
6219 sshport = pg->hosts[off % hostcnt].sshport;
6229 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
6233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "baseservice home is %s\n",
6236 if (hostname != NULL)
6237 GNUNET_asprintf (&newservicehome, "%s/%s/", baseservicehome,
6240 GNUNET_asprintf (&newservicehome, "%s/", baseservicehome);
6241 GNUNET_free (baseservicehome);
6242 baseservicehome = NULL;
6246 tmpdir = getenv ("TMPDIR");
6247 tmpdir = tmpdir ? tmpdir : "/tmp";
6248 if (hostname != NULL)
6249 GNUNET_asprintf (&newservicehome, "%s/%s/%s/", tmpdir, hostname,
6250 "gnunet-testing-test-test");
6252 GNUNET_asprintf (&newservicehome, "%s/%s/", tmpdir,
6253 "gnunet-testing-test-test", off);
6256 if (NULL != username)
6257 GNUNET_asprintf (&arg, "%s@%s:%s", username, pg->hosts[off].hostname,
6260 GNUNET_asprintf (&arg, "%s:%s", pg->hosts[off].hostname,
6263 /* FIXME: Doesn't support ssh_port option! */
6265 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "rsync", "rsync", "-r",
6266 newservicehome, arg, NULL);
6268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6269 "copying directory with command rsync -r %s %s\n",
6270 newservicehome, arg);
6272 GNUNET_free (newservicehome);
6276 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6278 ("Could not start `%s' process to copy configuration directory.\n"),
6282 GNUNET_OS_process_wait (proc);
6283 GNUNET_OS_process_close (proc);
6285 /* Now all the configuration files and hostkeys are copied to the remote host. Call the hostkey callback for each peer! */
6286 GNUNET_SCHEDULER_add_now (&call_hostkey_callbacks, pg);
6293 * Get a daemon by number, so callers don't have to do nasty
6294 * offsetting operation.
6296 struct GNUNET_TESTING_Daemon *
6297 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
6298 unsigned int position)
6300 if (position < pg->total)
6301 return pg->peers[position].daemon;
6306 * Get a daemon by peer identity, so callers can
6307 * retrieve the daemon without knowing it's offset.
6309 * @param pg the peer group to retrieve the daemon from
6310 * @param peer_id the peer identity of the daemon to retrieve
6312 * @return the daemon on success, or NULL if no such peer identity is found
6314 struct GNUNET_TESTING_Daemon *
6315 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
6316 const struct GNUNET_PeerIdentity *peer_id)
6320 for (i = 0; i < pg->total; i++)
6323 memcmp (&pg->peers[i].daemon->id, peer_id,
6324 sizeof (struct GNUNET_PeerIdentity)))
6325 return pg->peers[i].daemon;
6331 * Prototype of a function that will be called when a
6332 * particular operation was completed the testing library.
6334 * @param cls closure (a struct RestartContext)
6335 * @param id id of the peer that was restarted
6336 * @param cfg handle to the configuration of the peer
6337 * @param d handle to the daemon that was restarted
6338 * @param emsg NULL on success
6341 restart_callback (void *cls, const struct GNUNET_PeerIdentity *id,
6342 const struct GNUNET_CONFIGURATION_Handle *cfg,
6343 struct GNUNET_TESTING_Daemon *d, const char *emsg)
6345 struct RestartContext *restart_context = cls;
6349 restart_context->peers_restarted++;
6353 restart_context->peers_restart_failed++;
6356 if (restart_context->peers_restarted == restart_context->peer_group->total)
6358 restart_context->callback (restart_context->callback_cls, NULL);
6359 GNUNET_free (restart_context);
6361 else if (restart_context->peers_restart_failed +
6362 restart_context->peers_restarted ==
6363 restart_context->peer_group->total)
6365 restart_context->callback (restart_context->callback_cls,
6366 "Failed to restart peers!");
6367 GNUNET_free (restart_context);
6373 * Callback for informing us about a successful
6374 * or unsuccessful churn stop call.
6376 * @param cls a ChurnContext
6377 * @param emsg NULL on success, non-NULL on failure
6381 churn_stop_callback (void *cls, const char *emsg)
6383 struct ShutdownContext *shutdown_ctx = cls;
6384 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
6385 unsigned int total_left;
6386 char *error_message;
6388 error_message = NULL;
6389 shutdown_ctx->outstanding--;
6393 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6394 "Churn stop callback failed with error `%s'\n", emsg);
6395 churn_ctx->num_failed_stop++;
6399 churn_ctx->num_to_stop--;
6403 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
6404 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
6406 if (total_left == 0)
6408 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
6410 GNUNET_asprintf (&error_message,
6411 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
6412 churn_ctx->num_failed_start, churn_ctx->num_failed_stop);
6414 churn_ctx->cb (churn_ctx->cb_cls, error_message);
6415 GNUNET_free_non_null (error_message);
6416 GNUNET_free (churn_ctx);
6417 GNUNET_free (shutdown_ctx);
6422 * Count the number of running peers.
6424 * @param pg handle for the peer group
6426 * @return the number of currently running peers in the peer group
6429 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
6432 unsigned int running = 0;
6434 for (i = 0; i < pg->total; i++)
6436 if (pg->peers[i].daemon->running == GNUNET_YES)
6438 GNUNET_assert (running != -1);
6446 * Task to rate limit the number of outstanding peer shutdown
6447 * requests. This is necessary for making sure we don't do
6448 * too many ssh connections at once, but is generally nicer
6449 * to any system as well (graduated task starts, as opposed
6450 * to calling gnunet-arm N times all at once).
6453 schedule_churn_shutdown_task (void *cls,
6454 const struct GNUNET_SCHEDULER_TaskContext *tc)
6456 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6457 struct ShutdownContext *shutdown_ctx;
6458 struct ChurnContext *churn_ctx;
6460 GNUNET_assert (peer_shutdown_ctx != NULL);
6461 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6462 GNUNET_assert (shutdown_ctx != NULL);
6463 churn_ctx = (struct ChurnContext *) shutdown_ctx->cb_cls;
6464 if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
6465 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
6466 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
6467 &schedule_churn_shutdown_task,
6471 shutdown_ctx->outstanding++;
6472 if (churn_ctx->service != NULL)
6473 GNUNET_TESTING_daemon_stop_service (peer_shutdown_ctx->daemon,
6475 shutdown_ctx->timeout,
6476 shutdown_ctx->cb, shutdown_ctx);
6478 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6479 shutdown_ctx->timeout, shutdown_ctx->cb,
6480 shutdown_ctx, GNUNET_NO, GNUNET_YES);
6481 GNUNET_free (peer_shutdown_ctx);
6487 * Simulate churn by stopping some peers (and possibly
6488 * re-starting others if churn is called multiple times). This
6489 * function can only be used to create leave-join churn (peers "never"
6490 * leave for good). First "voff" random peers that are currently
6491 * online will be taken offline; then "von" random peers that are then
6492 * offline will be put back online. No notifications will be
6493 * generated for any of these operations except for the callback upon
6496 * @param pg handle for the peer group
6497 * @param service the service to churn off/on, NULL to churn peer
6498 * @param voff number of peers that should go offline
6499 * @param von number of peers that should come back online;
6500 * must be zero on first call (since "testbed_start"
6501 * always starts all of the peers)
6502 * @param timeout how long to wait for operations to finish before
6504 * @param cb function to call at the end
6505 * @param cb_cls closure for cb
6508 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
6509 char *service, unsigned int voff,
6511 struct GNUNET_TIME_Relative timeout,
6512 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6514 struct ChurnContext *churn_ctx;
6515 struct ShutdownContext *shutdown_ctx;
6516 struct PeerShutdownContext *peer_shutdown_ctx;
6517 struct PeerRestartContext *peer_restart_ctx;
6518 struct ChurnRestartContext *churn_startup_ctx;
6520 unsigned int running;
6521 unsigned int stopped;
6522 unsigned int total_running;
6523 unsigned int total_stopped;
6525 unsigned int *running_arr;
6526 unsigned int *stopped_arr;
6527 unsigned int *running_permute;
6528 unsigned int *stopped_permute;
6531 shutdown_ctx = NULL;
6532 peer_shutdown_ctx = NULL;
6533 peer_restart_ctx = NULL;
6534 churn_startup_ctx = NULL;
6539 if ((von == 0) && (voff == 0)) /* No peers at all? */
6545 for (i = 0; i < pg->total; i++)
6547 if (service == NULL)
6549 if (pg->peers[i].daemon->running == GNUNET_YES)
6551 GNUNET_assert (running != -1);
6556 GNUNET_assert (stopped != -1);
6562 /* FIXME: make churned services a list! */
6563 pos = pg->peers[i].daemon->churned_services;
6564 /* FIXME: while (pos != NULL) */
6568 if (0 == strcasecmp (pos, service))
6574 GNUNET_assert (stopped != -1);
6576 /* FIXME: pos = pos->next; */
6580 GNUNET_assert (running != -1);
6588 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6589 "Trying to stop more peers (%d) than are currently running (%d)!\n",
6591 cb (cb_cls, "Trying to stop more peers than are currently running!");
6597 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6598 "Trying to start more peers (%d) than are currently stopped (%d)!\n",
6600 cb (cb_cls, "Trying to start more peers than are currently stopped!");
6604 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6606 if (service != NULL)
6607 churn_ctx->service = GNUNET_strdup (service);
6610 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
6614 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
6616 running_permute = NULL;
6617 stopped_permute = NULL;
6621 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, running);
6624 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, stopped);
6626 total_running = running;
6627 total_stopped = stopped;
6631 churn_ctx->num_to_start = von;
6632 churn_ctx->num_to_stop = voff;
6634 churn_ctx->cb_cls = cb_cls;
6637 for (i = 0; i < pg->total; i++)
6639 if (service == NULL)
6641 if (pg->peers[i].daemon->running == GNUNET_YES)
6643 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6644 running_arr[running] = i;
6649 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6650 stopped_arr[stopped] = i;
6656 /* FIXME: make churned services a list! */
6657 pos = pg->peers[i].daemon->churned_services;
6658 /* FIXME: while (pos != NULL) */
6661 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6662 stopped_arr[stopped] = i;
6664 /* FIXME: pos = pos->next; */
6668 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6669 running_arr[running] = i;
6675 GNUNET_assert (running >= voff);
6678 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6679 shutdown_ctx->cb = &churn_stop_callback;
6680 shutdown_ctx->cb_cls = churn_ctx;
6681 shutdown_ctx->total_peers = voff;
6682 shutdown_ctx->timeout = timeout;
6685 for (i = 0; i < voff; i++)
6688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peer %d!\n",
6689 running_arr[running_permute[i]]);
6691 GNUNET_assert (running_arr != NULL);
6692 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6693 peer_shutdown_ctx->daemon =
6694 pg->peers[running_arr[running_permute[i]]].daemon;
6695 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6696 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task, peer_shutdown_ctx);
6699 GNUNET_assert (stopped >= von);
6702 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6703 churn_startup_ctx->churn_ctx = churn_ctx;
6704 churn_startup_ctx->timeout = timeout;
6705 churn_startup_ctx->pg = pg;
6707 for (i = 0; i < von; i++)
6710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting up peer %d!\n",
6711 stopped_arr[stopped_permute[i]]);
6713 GNUNET_assert (stopped_arr != NULL);
6714 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
6715 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
6716 peer_restart_ctx->daemon =
6717 pg->peers[stopped_arr[stopped_permute[i]]].daemon;
6718 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
6721 GNUNET_free_non_null (running_arr);
6722 GNUNET_free_non_null (stopped_arr);
6723 GNUNET_free_non_null (running_permute);
6724 GNUNET_free_non_null (stopped_permute);
6728 * Start a given service for each of the peers in the peer group.
6730 * @param pg handle for the peer group
6731 * @param service the service to start
6732 * @param timeout how long to wait for operations to finish before
6734 * @param cb function to call once finished
6735 * @param cb_cls closure for cb
6739 GNUNET_TESTING_daemons_start_service (struct GNUNET_TESTING_PeerGroup *pg,
6741 struct GNUNET_TIME_Relative timeout,
6742 GNUNET_TESTING_NotifyCompletion cb,
6745 struct ServiceStartContext *start_ctx;
6746 struct PeerServiceStartContext *peer_start_ctx;
6749 GNUNET_assert (service != NULL);
6751 start_ctx = GNUNET_malloc (sizeof (struct ServiceStartContext));
6753 start_ctx->remaining = pg->total;
6755 start_ctx->cb_cls = cb_cls;
6756 start_ctx->service = GNUNET_strdup (service);
6757 start_ctx->timeout = timeout;
6759 for (i = 0; i < pg->total; i++)
6762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting up service %s on peer %d!\n",
6763 service, stopped_arr[stopped_permute[i]]);
6765 peer_start_ctx = GNUNET_malloc (sizeof (struct PeerServiceStartContext));
6766 peer_start_ctx->start_ctx = start_ctx;
6767 peer_start_ctx->daemon = pg->peers[i].daemon;
6768 GNUNET_SCHEDULER_add_now (&schedule_service_start, peer_start_ctx);
6773 * Restart all peers in the given group.
6775 * @param pg the handle to the peer group
6776 * @param callback function to call on completion (or failure)
6777 * @param callback_cls closure for the callback function
6780 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
6781 GNUNET_TESTING_NotifyCompletion callback,
6784 struct RestartContext *restart_context;
6789 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
6790 restart_context->peer_group = pg;
6791 restart_context->peers_restarted = 0;
6792 restart_context->callback = callback;
6793 restart_context->callback_cls = callback_cls;
6795 for (off = 0; off < pg->total; off++)
6797 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback,
6805 * Start or stop an individual peer from the given group.
6807 * @param pg handle to the peer group
6808 * @param offset which peer to start or stop
6809 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
6810 * @param timeout how long to wait for shutdown
6811 * @param cb function to call at the end
6812 * @param cb_cls closure for cb
6815 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
6816 unsigned int offset, int desired_status,
6817 struct GNUNET_TIME_Relative timeout,
6818 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6820 struct ShutdownContext *shutdown_ctx;
6821 struct ChurnRestartContext *startup_ctx;
6822 struct ChurnContext *churn_ctx;
6824 if (GNUNET_NO == desired_status)
6826 if (NULL != pg->peers[offset].daemon)
6828 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6829 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6830 churn_ctx->num_to_start = 0;
6831 churn_ctx->num_to_stop = 1;
6833 churn_ctx->cb_cls = cb_cls;
6834 shutdown_ctx->cb_cls = churn_ctx;
6835 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon, timeout,
6836 &churn_stop_callback, shutdown_ctx, GNUNET_NO,
6840 else if (GNUNET_YES == desired_status)
6842 if (NULL == pg->peers[offset].daemon)
6844 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6845 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6846 churn_ctx->num_to_start = 1;
6847 churn_ctx->num_to_stop = 0;
6849 churn_ctx->cb_cls = cb_cls;
6850 startup_ctx->churn_ctx = churn_ctx;
6851 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon, timeout,
6852 &churn_start_callback, startup_ctx);
6861 * Callback for shutting down peers in a peer group.
6863 * @param cls closure (struct ShutdownContext)
6864 * @param emsg NULL on success
6867 internal_shutdown_callback (void *cls, const char *emsg)
6869 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6870 struct ShutdownContext *shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6873 struct OutstandingSSH *ssh_pos;
6875 shutdown_ctx->outstanding--;
6876 if (peer_shutdown_ctx->daemon->hostname != NULL)
6877 decrement_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6882 shutdown_ctx->peers_down++;
6887 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "internal_shutdown_callback",
6888 "Failed to stop a peer: %s\n", emsg);
6890 shutdown_ctx->peers_failed++;
6893 if ((shutdown_ctx->cb != NULL) &&
6894 (shutdown_ctx->peers_down + shutdown_ctx->peers_failed ==
6895 shutdown_ctx->total_peers))
6897 if (shutdown_ctx->peers_failed > 0)
6898 shutdown_ctx->cb (shutdown_ctx->cb_cls,
6899 "Not all peers successfully shut down!");
6901 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
6903 for (i = 0; i < shutdown_ctx->pg->total; i++)
6905 if (shutdown_ctx->pg->peers[i].startup_task != GNUNET_SCHEDULER_NO_TASK)
6906 GNUNET_SCHEDULER_cancel (shutdown_ctx->pg->peers[i].startup_task);
6908 GNUNET_free (shutdown_ctx->pg->peers);
6909 GNUNET_free_non_null (shutdown_ctx->pg->hostkey_data);
6910 for (off = 0; off < shutdown_ctx->pg->num_hosts; off++)
6912 GNUNET_free (shutdown_ctx->pg->hosts[off].hostname);
6913 GNUNET_free_non_null (shutdown_ctx->pg->hosts[off].username);
6915 GNUNET_free_non_null (shutdown_ctx->pg->hosts);
6916 while (NULL != (ssh_pos = shutdown_ctx->pg->ssh_head))
6918 GNUNET_CONTAINER_DLL_remove (shutdown_ctx->pg->ssh_head,
6919 shutdown_ctx->pg->ssh_tail, ssh_pos);
6920 GNUNET_free (ssh_pos);
6922 GNUNET_free (shutdown_ctx->pg);
6923 GNUNET_free (shutdown_ctx);
6925 GNUNET_free (peer_shutdown_ctx);
6930 * Task to rate limit the number of outstanding peer shutdown
6931 * requests. This is necessary for making sure we don't do
6932 * too many ssh connections at once, but is generally nicer
6933 * to any system as well (graduated task starts, as opposed
6934 * to calling gnunet-arm N times all at once).
6937 schedule_shutdown_task (void *cls,
6938 const struct GNUNET_SCHEDULER_TaskContext *tc)
6940 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6941 struct ShutdownContext *shutdown_ctx;
6943 GNUNET_assert (peer_shutdown_ctx != NULL);
6944 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6945 GNUNET_assert (shutdown_ctx != NULL);
6947 if ((shutdown_ctx->outstanding < shutdown_ctx->pg->max_concurrent_ssh) ||
6948 ((peer_shutdown_ctx->daemon->hostname != NULL) &&
6949 (count_outstanding_at_host
6950 (peer_shutdown_ctx->daemon->hostname,
6951 shutdown_ctx->pg) < shutdown_ctx->pg->max_concurrent_ssh)))
6953 if (peer_shutdown_ctx->daemon->hostname != NULL)
6954 increment_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6956 shutdown_ctx->outstanding++;
6957 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6958 shutdown_ctx->timeout,
6959 &internal_shutdown_callback, peer_shutdown_ctx,
6960 shutdown_ctx->delete_files, GNUNET_NO);
6963 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
6964 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
6965 &schedule_shutdown_task, peer_shutdown_ctx);
6970 * Read a testing hosts file based on a configuration.
6971 * Returns a DLL of hosts (caller must free!) on success
6972 * or NULL on failure.
6974 * @param cfg a configuration with a testing section
6976 * @return DLL of hosts on success, NULL on failure
6978 struct GNUNET_TESTING_Host *
6979 GNUNET_TESTING_hosts_load (const struct GNUNET_CONFIGURATION_Handle *cfg)
6981 struct GNUNET_TESTING_Host *hosts;
6982 struct GNUNET_TESTING_Host *temphost;
6990 /* Check for a hostfile containing user@host:port triples */
6992 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "hostfile",
6999 if (hostfile != NULL)
7001 if (GNUNET_OK != GNUNET_DISK_file_test (hostfile))
7002 GNUNET_DISK_fn_write (hostfile, NULL, 0,
7003 GNUNET_DISK_PERM_USER_READ |
7004 GNUNET_DISK_PERM_USER_WRITE);
7005 if ((0 != STAT (hostfile, &frstat)) || (frstat.st_size == 0))
7007 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7008 "Could not open file specified for host list, ending test!");
7009 GNUNET_free (hostfile);
7013 data = GNUNET_malloc_large (frstat.st_size);
7014 GNUNET_assert (data != NULL);
7015 if (frstat.st_size != GNUNET_DISK_fn_read (hostfile, data, frstat.st_size))
7017 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7018 "Could not read file %s specified for host list, ending test!",
7020 GNUNET_free (hostfile);
7025 GNUNET_free_non_null (hostfile);
7029 while (count < frstat.st_size - 1)
7032 if (((data[count] == '\n')) && (buf != &data[count]))
7035 temphost = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Host));
7037 sscanf (buf, "%a[a-zA-Z0-9_]@%a[a-zA-Z0-9.]:%hd",
7038 &temphost->username, &temphost->hostname, &temphost->port);
7041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7042 "Successfully read host %s, port %d and user %s from file\n",
7043 temphost->hostname, temphost->port, temphost->username);
7047 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7048 "Error reading line `%s' in hostfile\n", buf);
7049 GNUNET_free (temphost);
7050 buf = &data[count + 1];
7053 temphost->next = hosts;
7055 buf = &data[count + 1];
7057 else if ((data[count] == '\n') || (data[count] == '\0'))
7058 buf = &data[count + 1];
7061 GNUNET_free_non_null (data);
7067 * Shutdown all peers started in the given group.
7069 * @param pg handle to the peer group
7070 * @param timeout how long to wait for shutdown
7071 * @param cb callback to notify upon success or failure
7072 * @param cb_cls closure for cb
7075 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
7076 struct GNUNET_TIME_Relative timeout,
7077 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
7080 struct ShutdownContext *shutdown_ctx;
7081 struct PeerShutdownContext *peer_shutdown_ctx;
7084 struct PeerConnection *conn_iter;
7085 struct PeerConnection *temp_conn;
7087 struct ConnectContext *cc;
7089 GNUNET_assert (pg->total > 0);
7090 while (NULL != (cc = pg->cc_head))
7092 GNUNET_CONTAINER_DLL_remove (pg->cc_head, pg->cc_tail, cc);
7093 if (GNUNET_SCHEDULER_NO_TASK != cc->task)
7094 GNUNET_SCHEDULER_cancel (cc->task);
7096 GNUNET_TESTING_daemons_connect_cancel (cc->cc);
7100 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
7101 shutdown_ctx->delete_files =
7102 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "DELETE_FILES");
7103 shutdown_ctx->cb = cb;
7104 shutdown_ctx->cb_cls = cb_cls;
7105 shutdown_ctx->total_peers = pg->total;
7106 shutdown_ctx->timeout = timeout;
7107 shutdown_ctx->pg = pg;
7109 for (off = 0; off < pg->total; off++)
7111 GNUNET_assert (NULL != pg->peers[off].daemon);
7112 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
7113 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
7114 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
7115 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
7117 if (NULL != pg->peers[off].cfg)
7119 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
7120 pg->peers[off].cfg = NULL;
7123 // FIXME Do DLL remove for all pg->peers[off].LIST
7124 conn_iter = pg->peers[off].allowed_peers_head;
7125 while (conn_iter != NULL)
7127 temp_conn = conn_iter->next;
7128 GNUNET_free (conn_iter);
7129 conn_iter = temp_conn;
7131 pg->peers[off].allowed_peers_head = NULL;
7133 conn_iter = pg->peers[off].connect_peers_head;
7134 while (conn_iter != NULL)
7136 temp_conn = conn_iter->next;
7137 GNUNET_free (conn_iter);
7138 conn_iter = temp_conn;
7140 pg->peers[off].connect_peers_head = NULL;
7142 conn_iter = pg->peers[off].blacklisted_peers_head;
7143 while (conn_iter != NULL)
7145 temp_conn = conn_iter->next;
7146 GNUNET_free (conn_iter);
7147 conn_iter = temp_conn;
7149 pg->peers[off].blacklisted_peers_head = NULL;
7151 conn_iter = pg->peers[off].connect_peers_working_set_head;
7152 while (conn_iter != NULL)
7154 temp_conn = conn_iter->next;
7155 GNUNET_free (conn_iter);
7156 conn_iter = temp_conn;
7158 pg->peers[off].connect_peers_working_set_head = NULL;
7160 if (pg->peers[off].allowed_peers != NULL)
7161 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
7162 if (pg->peers[off].connect_peers != NULL)
7163 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
7164 if (pg->peers[off].blacklisted_peers != NULL)
7165 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].blacklisted_peers);
7170 /* end of testing_group.c */