2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing_group.c
23 * @brief convenience API for writing testcases for GNUnet
24 * @author Nathan Evans
25 * @author Christian Grothoff
29 #include "gnunet_constants.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_testing_lib.h"
32 #include "gnunet_core_service.h"
34 #define VERBOSE_TESTING GNUNET_NO
36 #define VERBOSE_TOPOLOGY GNUNET_NO
38 #define DEBUG_CHURN GNUNET_NO
40 #define USE_START_HELPER GNUNET_YES
44 /* Before connecting peers, send all of the HELLOs */
45 #define USE_SEND_HELLOS GNUNET_NO
47 #define TOPOLOGY_HACK GNUNET_YES
51 * Lowest port used for GNUnet testing. Should be high enough to not
52 * conflict with other applications running on the hosts but be low
53 * enough to not conflict with client-ports (typically starting around
56 #define LOW_PORT 12000
59 * Highest port used for GNUnet testing. Should be low enough to not
60 * conflict with the port range for "local" ports (client apps; see
61 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
63 #define HIGH_PORT 56000
65 /* Maximum time to delay connect attempt */
66 #define MAX_CONNECT_DELAY 300
69 * Which list of peers do we need to modify?
73 /** Modify allowed peers */
76 /** Modify connect peers */
79 /** Modify blacklist peers */
82 /** Modify workingset peers */
87 * Prototype of a function called whenever two peers would be connected
88 * in a certain topology.
91 (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup * pg,
92 unsigned int first, unsigned int second,
93 enum PeerLists list, unsigned int check);
96 * Context for handling churning a peer group
101 * The peergroup we are dealing with.
103 struct GNUNET_TESTING_PeerGroup *pg;
106 * Callback used to notify of churning finished
108 GNUNET_TESTING_NotifyCompletion cb;
111 * Closure for callback
116 * Number of peers that still need to be started
118 unsigned int num_to_start;
121 * Number of peers that still need to be stopped
123 unsigned int num_to_stop;
126 * Number of peers that failed to start
128 unsigned int num_failed_start;
131 * Number of peers that failed to stop
133 unsigned int num_failed_stop;
136 struct RestartContext
139 * The group of peers being restarted
141 struct GNUNET_TESTING_PeerGroup *peer_group;
144 * How many peers have been restarted thus far
146 unsigned int peers_restarted;
149 * How many peers got an error when restarting
151 unsigned int peers_restart_failed;
154 * The function to call once all peers have been restarted
156 GNUNET_TESTING_NotifyCompletion callback;
159 * Closure for callback function
165 struct SendHelloContext
168 * Global handle to the peer group.
170 struct GNUNET_TESTING_PeerGroup *pg;
173 * The data about this specific peer.
175 struct PeerData *peer;
178 * The next HELLO that needs sent to this peer.
180 struct PeerConnection *peer_pos;
183 * Are we connected to CORE yet?
185 unsigned int core_ready;
188 * How many attempts should we make for failed connections?
190 unsigned int connect_attempts;
193 * Task for scheduling core connect requests to be sent.
195 GNUNET_SCHEDULER_TaskIdentifier core_connect_task;
198 struct ShutdownContext
200 struct GNUNET_TESTING_PeerGroup *pg;
202 * Total peers to wait for
204 unsigned int total_peers;
207 * Number of peers successfully shut down
209 unsigned int peers_down;
212 * Number of peers failed to shut down
214 unsigned int peers_failed;
217 * Number of peers we have started shutting
218 * down. If too many, wait on them.
220 unsigned int outstanding;
223 * Timeout for shutdown.
225 struct GNUNET_TIME_Relative timeout;
228 * Callback to call when all peers either
229 * shutdown or failed to shutdown
231 GNUNET_TESTING_NotifyCompletion cb;
239 * Should we delete all of the files from the peers?
245 * Individual shutdown context for a particular peer.
247 struct PeerShutdownContext
250 * Pointer to the high level shutdown context.
252 struct ShutdownContext *shutdown_ctx;
255 * The daemon handle for the peer to shut down.
257 struct GNUNET_TESTING_Daemon *daemon;
261 * Individual shutdown context for a particular peer.
263 struct PeerRestartContext
266 * Pointer to the high level restart context.
268 struct ChurnRestartContext *churn_restart_ctx;
271 * The daemon handle for the peer to shut down.
273 struct GNUNET_TESTING_Daemon *daemon;
276 struct CreateTopologyContext
280 * Function to call with number of connections
282 GNUNET_TESTING_NotifyConnections cont;
285 * Closure for connection notification
292 /** Waiting to read number of peers */
295 /** Should find next peer index */
298 /** Should find colon */
301 /** Should read other peer index, space, or endline */
306 struct PeerConnection
311 struct PeerConnection *prev;
316 struct PeerConnection *next;
319 * Index of daemon in pg->peers
326 struct InternalStartContext
329 * Pointer to peerdata
331 struct PeerData *peer;
334 * Timeout for peer startup
336 struct GNUNET_TIME_Relative timeout;
339 * Client callback for hostkey notification
341 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
344 * Closure for hostkey_callback
349 * Client callback for peer start notification
351 GNUNET_TESTING_NotifyDaemonRunning start_cb;
359 * Hostname, where to start the peer
361 const char *hostname;
364 * Username to use when connecting to the
367 const char *username;
370 * Pointer to starting memory location of a hostkey
375 * Port to use for ssh.
381 struct ChurnRestartContext
384 * PeerGroup that we are working with.
386 struct GNUNET_TESTING_PeerGroup *pg;
389 * Number of restarts currently in flight.
391 unsigned int outstanding;
394 * Handle to the underlying churn context.
396 struct ChurnContext *churn_ctx;
399 * How long to allow the operation to take.
401 struct GNUNET_TIME_Relative timeout;
404 struct OutstandingSSH
406 struct OutstandingSSH *next;
408 struct OutstandingSSH *prev;
411 * Number of current ssh connections.
413 uint32_t outstanding;
416 * The hostname of this peer.
418 const char *hostname;
422 * Data we keep per peer.
427 * (Initial) configuration of the host.
428 * (initial because clients could change
429 * it and we would not know about those
432 struct GNUNET_CONFIGURATION_Handle *cfg;
435 * Handle for controlling the daemon.
437 struct GNUNET_TESTING_Daemon *daemon;
440 * The peergroup this peer belongs to.
442 struct GNUNET_TESTING_PeerGroup *pg;
446 * Linked list of allowed peer connections.
448 struct PeerConnection *allowed_peers_head;
451 * Linked list of allowed peer connections.
453 struct PeerConnection *allowed_peers_tail;
456 * Linked list of blacklisted peer connections.
458 struct PeerConnection *blacklisted_peers_head;
461 * Linked list of blacklisted peer connections.
463 struct PeerConnection *blacklisted_peers_tail;
466 * Linked list of connect peer connections.
468 struct PeerConnection *connect_peers_head;
471 * Linked list of connect peer connections.
473 struct PeerConnection *connect_peers_tail;
476 * Linked list of connect peer connections.
478 struct PeerConnection *connect_peers_working_set_head;
481 * Linked list of connect peer connections.
483 struct PeerConnection *connect_peers_working_set_tail;
487 * Hash map of allowed peer connections (F2F created topology)
489 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
492 * Hash map of blacklisted peers
494 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
497 * Hash map of peer connections
499 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
502 * Temporary hash map of peer connections
504 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
508 * Temporary variable for topology creation, should be reset before
509 * creating any topology so the count is valid once finished.
514 * Context to keep track of peers being started, to
515 * stagger hostkey generation and peer startup.
517 struct InternalStartContext internal_context;
521 * Linked list of per-host data.
531 * SSH username to use when connecting to this host.
536 * SSH port to use when connecting to this host.
541 * Lowest port that we have not yet used
547 struct TopologyIterateContext
550 * The peergroup we are working with.
552 struct GNUNET_TESTING_PeerGroup *pg;
555 * Callback for notifying of two connected peers.
557 GNUNET_TESTING_NotifyTopology topology_cb;
560 * Closure for topology_cb
565 * Number of peers currently connected to.
567 unsigned int connected;
570 * Number of peers we have finished iterating.
572 unsigned int completed;
575 * Number of peers total.
580 struct StatsIterateContext
583 * The peergroup that we are dealing with.
585 struct GNUNET_TESTING_PeerGroup *pg;
588 * Continuation to call once all stats information has been retrieved.
590 GNUNET_STATISTICS_Callback cont;
593 * Proc function to call on each value received.
595 GNUNET_TESTING_STATISTICS_Iterator proc;
598 * Closure for topology_cb
603 * Number of peers currently connected to.
605 unsigned int connected;
608 * Number of peers we have finished iterating.
610 unsigned int completed;
613 * Number of peers total.
621 struct GNUNET_TESTING_Daemon *daemon;
624 struct StatsCoreContext
627 struct GNUNET_TESTING_Daemon *daemon;
629 * Handle to the statistics service.
631 struct GNUNET_STATISTICS_Handle *stats_handle;
634 * Handle for getting statistics.
636 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
639 struct ConnectTopologyContext
642 * How many connections are left to create.
644 unsigned int remaining_connections;
647 * Handle to group of peers.
649 struct GNUNET_TESTING_PeerGroup *pg;
652 * How long to try this connection before timing out.
654 struct GNUNET_TIME_Relative connect_timeout;
657 * How many times to retry connecting the two peers.
659 unsigned int connect_attempts;
662 * Temp value set for each iteration.
664 //struct PeerData *first;
667 * Notification that all peers are connected.
669 GNUNET_TESTING_NotifyCompletion notify_connections_done;
672 * Closure for notify.
678 * Handle to a group of GNUnet peers.
680 struct GNUNET_TESTING_PeerGroup
683 * Configuration template.
685 const struct GNUNET_CONFIGURATION_Handle *cfg;
688 * Function to call on each started daemon.
690 //GNUNET_TESTING_NotifyDaemonRunning cb;
698 * Function to call on each topology connection created
700 GNUNET_TESTING_NotifyConnection notify_connection;
703 * Callback for notify_connection
705 void *notify_connection_cls;
708 * Array of information about hosts.
710 struct HostData *hosts;
713 * Number of hosts (size of HostData)
715 unsigned int num_hosts;
718 * Array of "total" peers.
720 struct PeerData *peers;
723 * Number of peers in this group.
728 * At what time should we fail the peer startup process?
730 struct GNUNET_TIME_Absolute max_timeout;
733 * How many peers are being started right now?
735 unsigned int starting;
738 * How many peers have already been started?
740 unsigned int started;
743 * Number of possible connections to peers
746 unsigned int max_outstanding_connections;
749 * Number of ssh connections to peers (max).
751 unsigned int max_concurrent_ssh;
754 * Number of connects we are waiting on, allows us to rate limit
757 unsigned int outstanding_connects;
760 * Number of HELLOs we have yet to send.
762 unsigned int remaining_hellos;
765 * How many connects have already been scheduled?
767 unsigned int total_connects_scheduled;
770 * Hostkeys loaded from a file.
775 * Head of DLL to keep track of the number of outstanding
776 * ssh connections per peer.
778 struct OutstandingSSH *ssh_head;
781 * Tail of DLL to keep track of the number of outstanding
782 * ssh connections per peer.
784 struct OutstandingSSH *ssh_tail;
787 * Stop scheduling peers connecting.
789 unsigned int stop_connects;
792 * Connection context for peer group.
794 struct ConnectTopologyContext ct_ctx;
800 * The altered configuration.
802 struct GNUNET_CONFIGURATION_Handle *ret;
805 * The original configuration to alter.
807 const struct GNUNET_CONFIGURATION_Handle *orig;
810 * The hostname that this peer will run on.
812 const char *hostname;
815 * The next possible port to assign.
820 * Unique number for unix domain sockets.
825 * Unique number for this peer/host to offset
826 * things that are grouped by host.
831 struct ConnectContext
834 * Index of peer to connect second to.
836 uint32_t first_index;
839 * Index of peer to connect first to.
841 uint32_t second_index;
844 * Higher level topology connection context.
846 struct ConnectTopologyContext *ct_ctx;
849 * Whether this connection has been accounted for in the schedule_connect call.
854 struct UnblacklistContext
859 struct GNUNET_TESTING_PeerGroup *pg;
862 * uid of the first peer
872 struct GNUNET_TESTING_PeerGroup *pg;
875 * uid of the first peer
880 * Peer data for first peer.
882 struct PeerData *first;
885 * Random percentage to use
890 struct MinimumContext
895 struct GNUNET_TESTING_PeerGroup *pg;
898 * uid of the first peer
903 * Peer data for first peer.
905 struct PeerData *first;
908 * Number of conns per peer
910 unsigned int num_to_add;
913 * Permuted array of all possible connections. Only add the Nth
914 * peer if it's in the Nth position.
916 unsigned int *pg_array;
919 * What number is the current element we are iterating over?
921 unsigned int current;
929 struct GNUNET_TESTING_PeerGroup *pg;
932 * uid of the first peer
937 * uid of the second peer
942 * Peer data for first peer.
944 struct PeerData *first;
947 * Which peer has been chosen as the one to add?
952 * What number is the current element we are iterating over?
954 unsigned int current;
958 * Simple struct to keep track of progress, and print a
959 * nice little percentage meter for long running tasks.
969 unsigned int completed;
973 char *startup_string;
978 * Convert unique ID to hash code.
980 * @param uid unique ID to convert
981 * @param hash set to uid (extended with zeros)
984 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
986 memset (hash, 0, sizeof (GNUNET_HashCode));
987 *((uint32_t *) hash) = uid;
991 * Convert hash code to unique ID.
993 * @param uid unique ID to convert
994 * @param hash set to uid (extended with zeros)
997 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
999 memcpy (uid, hash, sizeof (uint32_t));
1004 static struct GNUNET_CORE_MessageHandler no_handlers[] =
1010 * Create a meter to keep track of the progress of some task.
1012 * @param total the total number of items to complete
1013 * @param start_string a string to prefix the meter with (if printing)
1014 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
1017 * @return the progress meter
1019 static struct ProgressMeter *
1020 create_meter(unsigned int total, char * start_string, int print)
1022 struct ProgressMeter *ret;
1023 ret = GNUNET_malloc(sizeof(struct ProgressMeter));
1026 ret->modnum = total / 4;
1027 if (ret->modnum == 0) /* Divide by zero check */
1029 ret->dotnum = (total / 50) + 1;
1030 if (start_string != NULL)
1031 ret->startup_string = GNUNET_strdup(start_string);
1033 ret->startup_string = GNUNET_strdup("");
1039 * Update progress meter (increment by one).
1041 * @param meter the meter to update and print info for
1043 * @return GNUNET_YES if called the total requested,
1044 * GNUNET_NO if more items expected
1047 update_meter(struct ProgressMeter *meter)
1049 if (meter->print == GNUNET_YES)
1051 if (meter->completed % meter->modnum == 0)
1053 if (meter->completed == 0)
1055 fprintf (stdout, "%sProgress: [0%%", meter->startup_string);
1058 fprintf (stdout, "%d%%", (int) (((float) meter->completed
1059 / meter->total) * 100));
1061 else if (meter->completed % meter->dotnum == 0)
1062 fprintf (stdout, ".");
1064 if (meter->completed + 1 == meter->total)
1065 fprintf (stdout, "%d%%]\n", 100);
1070 if (meter->completed == meter->total)
1072 if (meter->completed > meter->total)
1073 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
1078 * Reset progress meter.
1080 * @param meter the meter to reset
1082 * @return GNUNET_YES if meter reset,
1083 * GNUNET_SYSERR on error
1086 reset_meter(struct ProgressMeter *meter)
1089 return GNUNET_SYSERR;
1091 meter->completed = 0;
1096 * Release resources for meter
1098 * @param meter the meter to free
1101 free_meter(struct ProgressMeter *meter)
1103 GNUNET_free_non_null (meter->startup_string);
1104 GNUNET_free (meter);
1108 * Get a topology from a string input.
1110 * @param topology where to write the retrieved topology
1111 * @param topology_string The string to attempt to
1112 * get a configuration value from
1113 * @return GNUNET_YES if topology string matched a
1114 * known topology, GNUNET_NO if not
1117 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology,
1118 const char *topology_string)
1121 * Strings representing topologies in enum
1123 static const char *topology_strings[] =
1126 * A clique (everyone connected to everyone else).
1131 * Small-world network (2d torus plus random links).
1136 * Small-world network (ring plus random links).
1156 * Certain percentage of peers are unable to communicate directly
1157 * replicating NAT conditions
1162 * Scale free topology.
1167 * Straight line topology.
1172 * All peers are disconnected.
1177 * Read the topology from a file.
1184 if (topology_string == NULL)
1186 while (topology_strings[curr] != NULL)
1188 if (strcasecmp (topology_strings[curr], topology_string) == 0)
1195 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
1200 * Get connect topology option from string input.
1202 * @param topology_option where to write the retrieved topology
1203 * @param topology_string The string to attempt to
1204 * get a configuration value from
1205 * @return GNUNET_YES if string matched a known
1206 * topology option, GNUNET_NO if not
1209 GNUNET_TESTING_topology_option_get(
1210 enum GNUNET_TESTING_TopologyOption *topology_option,
1211 const char *topology_string)
1214 * Options for connecting a topology as strings.
1216 static const char *topology_option_strings[] =
1219 * Try to connect all peers specified in the topology.
1224 * Choose a random subset of connections to create.
1226 "CONNECT_RANDOM_SUBSET",
1229 * Create at least X connections for each peer.
1234 * Using a depth first search, create one connection
1235 * per peer. If any are missed (graph disconnected)
1236 * start over at those peers until all have at least one
1242 * Find the N closest peers to each allowed peer in the
1243 * topology and make sure a connection to those peers
1244 * exists in the connect topology.
1249 * No options specified.
1256 if (topology_string == NULL)
1258 while (NULL != topology_option_strings[curr])
1260 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1262 *topology_option = curr;
1267 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1272 * Function to iterate over options. Copies
1273 * the options to the target configuration,
1274 * updating PORT values as needed.
1276 * @param cls closure
1277 * @param section name of the section
1278 * @param option name of the option
1279 * @param value value of the option
1282 update_config(void *cls, const char *section, const char *option,
1285 struct UpdateContext *ctx = cls;
1289 char *single_variable;
1290 char *per_host_variable;
1291 unsigned long long num_per_host;
1293 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1294 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1296 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1298 if ((ival != 0) && (GNUNET_YES
1299 != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1302 GNUNET_snprintf (cval, sizeof(cval), "%u", ctx->nport++);
1305 else if ((ival != 0) && (GNUNET_YES
1306 == GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1308 && GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1312 GNUNET_snprintf (cval, sizeof(cval), "%u", ival + ctx->fdnum
1317 /* FIXME: REMOVE FOREVER HACK HACK HACK */
1318 if (0 == strcasecmp (section, "transport-tcp"))
1319 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, "ADVERTISED_PORT", value);
1322 if (0 == strcmp (option, "UNIXPATH"))
1324 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig,
1328 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1329 section, ctx->upnum++);
1332 else if ((GNUNET_YES
1333 == GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1336 && (num_per_host > 0))
1339 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1340 section, ctx->fdnum % num_per_host);
1345 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1347 value = ctx->hostname;
1349 GNUNET_free (single_variable);
1350 GNUNET_free (per_host_variable);
1351 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1355 * Create a new configuration using the given configuration
1356 * as a template; however, each PORT in the existing cfg
1357 * must be renumbered by incrementing "*port". If we run
1358 * out of "*port" numbers, return NULL.
1360 * @param cfg template configuration
1361 * @param off the current peer offset
1362 * @param port port numbers to use, update to reflect
1363 * port numbers that were used
1364 * @param upnum number to make unix domain socket names unique
1365 * @param hostname hostname of the controlling host, to allow control connections from
1366 * @param fdnum number used to offset the unix domain socket for grouped processes
1367 * (such as statistics or peerinfo, which can be shared among others)
1369 * @return new configuration, NULL on error
1371 static struct GNUNET_CONFIGURATION_Handle *
1372 make_config(const struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t off,
1373 uint16_t * port, uint32_t * upnum, const char *hostname,
1376 struct UpdateContext uc;
1379 char *allowed_hosts;
1380 unsigned long long skew_variance;
1381 unsigned long long skew_offset;
1387 uc.ret = GNUNET_CONFIGURATION_create ();
1388 uc.hostname = hostname;
1391 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1392 if (uc.nport >= HIGH_PORT)
1395 GNUNET_CONFIGURATION_destroy (uc.ret);
1399 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
1402 && (skew_variance > 0))
1405 skew_offset = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, skew_variance + 1);
1406 GNUNET_CONFIGURATION_set_value_number(uc.ret, "testing", "skew_offset", skew_offset);
1409 if (GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "control_host",
1410 &control_host) == GNUNET_OK)
1412 if (hostname != NULL)
1413 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1416 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1418 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1420 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1421 "ACCEPT_FROM", allowed_hosts);
1422 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1424 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1425 "ACCEPT_FROM", allowed_hosts);
1427 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH", "");
1428 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH",
1430 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH", "");
1431 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1434 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "USE_LOCALADDR",
1436 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "USE_LOCALADDR",
1438 GNUNET_free_non_null (control_host);
1439 GNUNET_free (allowed_hosts);
1442 /* arm needs to know to allow connections from the host on which it is running,
1443 * otherwise gnunet-arm is unable to connect to it in some instances */
1444 if (hostname != NULL)
1446 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1447 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1449 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1451 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "USE_LOCALADDR",
1453 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "USE_LOCALADDR",
1455 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1457 GNUNET_free (allowed_hosts);
1461 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1463 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1465 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "USE_LOCALADDR",
1467 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "USE_LOCALADDR",
1471 *port = (uint16_t) uc.nport;
1479 * Remove entries from the peer connection list
1481 * @param pg the peer group we are working with
1482 * @param first index of the first peer
1483 * @param second index of the second peer
1484 * @param list the peer list to use
1485 * @param check UNUSED
1487 * @return the number of connections added (can be 0, 1 or 2)
1491 remove_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1492 unsigned int second, enum PeerLists list, unsigned int check)
1496 struct PeerConnection **first_list;
1497 struct PeerConnection **second_list;
1498 struct PeerConnection *first_iter;
1499 struct PeerConnection *second_iter;
1500 struct PeerConnection **first_tail;
1501 struct PeerConnection **second_tail;
1504 GNUNET_HashCode hash_first;
1505 GNUNET_HashCode hash_second;
1507 hash_from_uid (first, &hash_first);
1508 hash_from_uid (second, &hash_second);
1516 first_list = &pg->peers[first].allowed_peers_head;
1517 second_list = &pg->peers[second].allowed_peers_head;
1518 first_tail = &pg->peers[first].allowed_peers_tail;
1519 second_tail = &pg->peers[second].allowed_peers_tail;
1522 first_list = &pg->peers[first].connect_peers_head;
1523 second_list = &pg->peers[second].connect_peers_head;
1524 first_tail = &pg->peers[first].connect_peers_tail;
1525 second_tail = &pg->peers[second].connect_peers_tail;
1528 first_list = &pg->peers[first].blacklisted_peers_head;
1529 second_list = &pg->peers[second].blacklisted_peers_head;
1530 first_tail = &pg->peers[first].blacklisted_peers_tail;
1531 second_tail = &pg->peers[second].blacklisted_peers_tail;
1534 first_list = &pg->peers[first].connect_peers_working_set_head;
1535 second_list = &pg->peers[second].connect_peers_working_set_head;
1536 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1537 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1544 first_iter = *first_list;
1545 while (first_iter != NULL)
1547 if (first_iter->index == second)
1549 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1550 GNUNET_free(first_iter);
1554 first_iter = first_iter->next;
1557 second_iter = *second_list;
1558 while (second_iter != NULL)
1560 if (second_iter->index == first)
1562 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1563 GNUNET_free(second_iter);
1567 second_iter = second_iter->next;
1571 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1574 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1579 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1582 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1591 * Add entries to the some list
1593 * @param pg the peer group we are working with
1594 * @param first index of the first peer
1595 * @param second index of the second peer
1596 * @param list the list type that we should modify
1597 * @param check GNUNET_YES to check lists before adding
1598 * GNUNET_NO to force add
1600 * @return the number of connections added (can be 0, 1 or 2)
1604 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1605 unsigned int second, enum PeerLists list, unsigned int check)
1611 struct PeerConnection **first_list;
1612 struct PeerConnection **second_list;
1613 struct PeerConnection *first_iter;
1614 struct PeerConnection *second_iter;
1615 struct PeerConnection *new_first;
1616 struct PeerConnection *new_second;
1617 struct PeerConnection **first_tail;
1618 struct PeerConnection **second_tail;
1623 first_list = &pg->peers[first].allowed_peers_head;
1624 second_list = &pg->peers[second].allowed_peers_head;
1625 first_tail = &pg->peers[first].allowed_peers_tail;
1626 second_tail = &pg->peers[second].allowed_peers_tail;
1629 first_list = &pg->peers[first].connect_peers_head;
1630 second_list = &pg->peers[second].connect_peers_head;
1631 first_tail = &pg->peers[first].connect_peers_tail;
1632 second_tail = &pg->peers[second].connect_peers_tail;
1635 first_list = &pg->peers[first].blacklisted_peers_head;
1636 second_list = &pg->peers[second].blacklisted_peers_head;
1637 first_tail = &pg->peers[first].blacklisted_peers_tail;
1638 second_tail = &pg->peers[second].blacklisted_peers_tail;
1641 first_list = &pg->peers[first].connect_peers_working_set_head;
1642 second_list = &pg->peers[second].connect_peers_working_set_head;
1643 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1644 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1651 add_first = GNUNET_YES;
1652 add_second = GNUNET_YES;
1654 if (check == GNUNET_YES)
1656 first_iter = *first_list;
1657 while (first_iter != NULL)
1659 if (first_iter->index == second)
1661 add_first = GNUNET_NO;
1664 first_iter = first_iter->next;
1667 second_iter = *second_list;
1668 while (second_iter != NULL)
1670 if (second_iter->index == first)
1672 add_second = GNUNET_NO;
1675 second_iter = second_iter->next;
1682 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1683 new_first->index = second;
1684 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1685 pg->peers[first].num_connections++;
1691 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1692 new_second->index = first;
1693 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1694 pg->peers[second].num_connections++;
1702 * Scale free network construction as described in:
1704 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1706 * Start with a network of "one" peer, then progressively add
1707 * peers up to the total number. At each step, iterate over
1708 * all possible peers and connect new peer based on number of
1709 * existing connections of the target peer.
1711 * @param pg the peer group we are dealing with
1712 * @param proc the connection processor to use
1713 * @param list the peer list to use
1715 * @return the number of connections created
1718 create_scale_free(struct GNUNET_TESTING_PeerGroup *pg,
1719 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1722 unsigned int total_connections;
1723 unsigned int outer_count;
1725 unsigned int previous_total_connections;
1729 GNUNET_assert (pg->total > 1);
1731 /* Add a connection between the first two nodes */
1732 total_connections = proc (pg, 0, 1, list, GNUNET_YES);
1734 for (outer_count = 1; outer_count < pg->total; outer_count++)
1736 previous_total_connections = total_connections;
1737 for (i = 0; i < outer_count; i++)
1739 probability = pg->peers[i].num_connections
1740 / (double) previous_total_connections;
1742 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1744 / ((double) UINT64_MAX);
1746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1747 "Considering connecting peer %d to peer %d\n",
1750 if (random < probability)
1753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1754 "Connecting peer %d to peer %d\n", outer_count, i);
1756 total_connections += proc (pg, outer_count, i, list, GNUNET_YES);
1761 return total_connections;
1765 * Create a topology given a peer group (set of running peers)
1766 * and a connection processor. Creates a small world topology
1767 * according to the rewired ring construction. The basic
1768 * behavior is that a ring topology is created, but with some
1769 * probability instead of connecting a peer to the next
1770 * neighbor in the ring a connection will be created to a peer
1771 * selected uniformly at random. We use the TESTING
1772 * PERCENTAGE option to specify what number of
1773 * connections each peer should have. Default is 2,
1774 * which makes the ring, any given number is multiplied by
1775 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1776 * each peer have on average 2logn connections. The additional
1777 * connections are made at increasing distance around the ring
1778 * from the original peer, or to random peers based on the re-
1779 * wiring probability. The TESTING
1780 * PROBABILITY option is used as the probability that a given
1781 * connection is rewired.
1783 * @param pg the peergroup to create the topology on
1784 * @param proc the connection processor to call to actually set
1785 * up connections between two peers
1786 * @param list the peer list to use
1788 * @return the number of connections that were set up
1792 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg,
1793 GNUNET_TESTING_ConnectionProcessor proc,
1794 enum PeerLists list)
1798 unsigned int natLog;
1799 unsigned int randomPeer;
1800 double random, logNModifier, probability;
1801 unsigned int smallWorldConnections;
1806 unsigned int useAnd;
1807 int connect_attempts;
1809 logNModifier = 0.5; /* FIXME: default value? */
1810 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1814 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1816 GNUNET_ERROR_TYPE_WARNING,
1818 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1819 p_string, "LOGNMODIFIER", "TESTING");
1820 GNUNET_free (p_string);
1822 probability = 0.5; /* FIXME: default percentage? */
1823 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1827 if (sscanf (p_string, "%lf", &probability) != 1)
1829 GNUNET_ERROR_TYPE_WARNING,
1831 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1832 p_string, "PERCENTAGE", "TESTING");
1833 GNUNET_free (p_string);
1835 natLog = log (pg->total);
1836 connsPerPeer = ceil (natLog * logNModifier);
1838 if (connsPerPeer % 2 == 1)
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."),
1844 smallWorldConnections = 0;
1845 connect_attempts = 0;
1846 for (i = 0; i < pg->total; i++)
1849 max = i + connsPerPeer / 2;
1850 min = i - connsPerPeer / 2;
1852 if (max > pg->total - 1)
1854 max = max - pg->total;
1860 min = pg->total - 1 + min;
1864 for (j = 0; j < connsPerPeer / 2; j++)
1867 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1869 / ((double) UINT64_MAX));
1870 if (random < probability)
1872 /* Connect to uniformly selected random peer */
1874 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1876 while ((((randomPeer < max) && (randomPeer > min)) && (useAnd
1877 == 0)) || (((randomPeer > min) || (randomPeer < max))
1881 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1884 smallWorldConnections += proc (pg, i, randomPeer, list,
1889 nodeToConnect = i + j + 1;
1890 if (nodeToConnect > pg->total - 1)
1892 nodeToConnect = nodeToConnect - pg->total;
1894 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
1900 connect_attempts += smallWorldConnections;
1902 return connect_attempts;
1906 * Create a topology given a peer group (set of running peers)
1907 * and a connection processor.
1909 * @param pg the peergroup to create the topology on
1910 * @param proc the connection processor to call to actually set
1911 * up connections between two peers
1912 * @param list the peer list to use
1914 * @return the number of connections that were set up
1918 create_nated_internet(struct GNUNET_TESTING_PeerGroup *pg,
1919 GNUNET_TESTING_ConnectionProcessor proc,
1920 enum PeerLists list)
1922 unsigned int outer_count, inner_count;
1923 unsigned int cutoff;
1924 int connect_attempts;
1925 double nat_percentage;
1928 nat_percentage = 0.6; /* FIXME: default percentage? */
1929 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1933 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1935 GNUNET_ERROR_TYPE_WARNING,
1937 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1938 p_string, "PERCENTAGE", "TESTING");
1939 GNUNET_free (p_string);
1942 cutoff = (unsigned int) (nat_percentage * pg->total);
1943 connect_attempts = 0;
1944 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1946 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1948 if ((outer_count > cutoff) || (inner_count > cutoff))
1951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1952 "Connecting peer %d to peer %d\n",
1953 outer_count, inner_count);
1955 connect_attempts += proc (pg, outer_count, inner_count, list,
1960 return connect_attempts;
1965 * Create a topology given a peer group (set of running peers)
1966 * and a connection processor.
1968 * @param pg the peergroup to create the topology on
1969 * @param proc the connection processor to call to actually set
1970 * up connections between two peers
1971 * @param list the peer list to use
1973 * @return the number of connections that were set up
1977 create_nated_internet_copy(struct GNUNET_TESTING_PeerGroup *pg,
1978 GNUNET_TESTING_ConnectionProcessor proc,
1979 enum PeerLists list)
1981 unsigned int outer_count, inner_count;
1982 unsigned int cutoff;
1983 int connect_attempts;
1984 double nat_percentage;
1987 struct ProgressMeter *conn_meter;
1989 nat_percentage = 0.6; /* FIXME: default percentage? */
1990 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1994 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1996 GNUNET_ERROR_TYPE_WARNING,
1998 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1999 p_string, "PERCENTAGE", "TESTING");
2000 GNUNET_free (p_string);
2003 cutoff = (unsigned int) (nat_percentage * pg->total);
2005 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2007 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2009 if ((outer_count > cutoff) || (inner_count > cutoff))
2015 conn_meter = create_meter (count, "NAT COPY", GNUNET_YES);
2016 connect_attempts = 0;
2017 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2019 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2021 if ((outer_count > cutoff) || (inner_count > cutoff))
2024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2025 "Connecting peer %d to peer %d\n",
2026 outer_count, inner_count);
2028 connect_attempts += proc (pg, outer_count, inner_count, list,
2030 add_connections (pg, outer_count, inner_count, ALLOWED, GNUNET_NO);
2031 update_meter (conn_meter);
2035 free_meter (conn_meter);
2037 return connect_attempts;
2042 * Create a topology given a peer group (set of running peers)
2043 * and a connection processor.
2045 * @param pg the peergroup to create the topology on
2046 * @param proc the connection processor to call to actually set
2047 * up connections between two peers
2048 * @param list the peer list to use
2050 * @return the number of connections that were set up
2054 create_small_world(struct GNUNET_TESTING_PeerGroup *pg,
2055 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2057 unsigned int i, j, k;
2058 unsigned int square;
2061 unsigned int toggle = 1;
2062 unsigned int nodeToConnect;
2063 unsigned int natLog;
2064 unsigned int node1Row;
2065 unsigned int node1Col;
2066 unsigned int node2Row;
2067 unsigned int node2Col;
2068 unsigned int distance;
2069 double probability, random, percentage;
2070 unsigned int smallWorldConnections;
2071 unsigned int small_world_it;
2073 int connect_attempts;
2074 square = floor (sqrt (pg->total));
2078 percentage = 0.5; /* FIXME: default percentage? */
2079 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2083 if (sscanf (p_string, "%lf", &percentage) != 1)
2085 GNUNET_ERROR_TYPE_WARNING,
2087 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2088 p_string, "PERCENTAGE", "TESTING");
2089 GNUNET_free (p_string);
2091 if (percentage < 0.0)
2094 GNUNET_ERROR_TYPE_WARNING,
2096 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
2097 "PERCENTAGE", "TESTING", percentage);
2100 probability = 0.5; /* FIXME: default percentage? */
2101 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2105 if (sscanf (p_string, "%lf", &probability) != 1)
2107 GNUNET_ERROR_TYPE_WARNING,
2109 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2110 p_string, "PROBABILITY", "TESTING");
2111 GNUNET_free (p_string);
2113 if (square * square != pg->total)
2115 while (rows * cols < pg->total)
2117 if (toggle % 2 == 0)
2126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2128 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2132 connect_attempts = 0;
2133 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2134 * to the node to its right and above. Once this is over, we'll have our torus!
2135 * Special case for the last node (if the rows and columns are not equal), connect
2136 * to the first in the row to maintain topology.
2138 for (i = 0; i < pg->total; i++)
2140 /* First connect to the node to the right */
2141 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2142 nodeToConnect = i + 1;
2143 else if (i + 1 == pg->total)
2144 nodeToConnect = rows * cols - cols;
2146 nodeToConnect = i - cols + 1;
2148 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2152 nodeToConnect = (rows * cols) - cols + i;
2153 if (nodeToConnect >= pg->total)
2154 nodeToConnect -= cols;
2157 nodeToConnect = i - cols;
2159 if (nodeToConnect < pg->total)
2160 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2162 natLog = log (pg->total);
2163 #if VERBOSE_TESTING > 2
2164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2165 _("natural log of %d is %d, will run %d iterations\n"),
2166 pg->total, natLog, (int) (natLog * percentage));
2167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2168 _("Total connections added thus far: %u!\n"), connect_attempts);
2170 smallWorldConnections = 0;
2171 small_world_it = (unsigned int) (natLog * percentage);
2172 if (small_world_it < 1)
2174 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
2175 for (i = 0; i < small_world_it; i++)
2177 for (j = 0; j < pg->total; j++)
2179 /* Determine the row and column of node at position j on the 2d torus */
2180 node1Row = j / cols;
2181 node1Col = j - (node1Row * cols);
2182 for (k = 0; k < pg->total; k++)
2184 /* Determine the row and column of node at position k on the 2d torus */
2185 node2Row = k / cols;
2186 node2Col = k - (node2Row * cols);
2187 /* Simple Cartesian distance */
2188 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
2191 /* Calculate probability as 1 over the square of the distance */
2192 probability = 1.0 / (distance * distance);
2193 /* Choose a random value between 0 and 1 */
2195 = ((double) GNUNET_CRYPTO_random_u64 (
2196 GNUNET_CRYPTO_QUALITY_WEAK,
2198 / ((double) UINT64_MAX);
2199 /* If random < probability, then connect the two nodes */
2200 if (random < probability)
2201 smallWorldConnections += proc (pg, j, k, list, GNUNET_YES);
2207 connect_attempts += smallWorldConnections;
2208 #if VERBOSE_TESTING > 2
2209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2210 _("Total connections added for small world: %d!\n"),
2211 smallWorldConnections);
2213 return connect_attempts;
2217 * Create a topology given a peer group (set of running peers)
2218 * and a connection processor.
2220 * @param pg the peergroup to create the topology on
2221 * @param proc the connection processor to call to actually set
2222 * up connections between two peers
2223 * @param list the peer list to use
2225 * @return the number of connections that were set up
2229 create_erdos_renyi(struct GNUNET_TESTING_PeerGroup *pg,
2230 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2233 unsigned int outer_count;
2234 unsigned int inner_count;
2235 int connect_attempts;
2239 probability = 0.5; /* FIXME: default percentage? */
2240 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2244 if (sscanf (p_string, "%lf", &probability) != 1)
2246 GNUNET_ERROR_TYPE_WARNING,
2248 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2249 p_string, "PROBABILITY", "TESTING");
2250 GNUNET_free (p_string);
2252 connect_attempts = 0;
2253 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2255 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2258 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2260 / ((double) UINT64_MAX);
2262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2263 _("rand is %f probability is %f\n"), temp_rand,
2266 if (temp_rand < probability)
2268 connect_attempts += proc (pg, outer_count, inner_count, list,
2274 return connect_attempts;
2278 * Create a topology given a peer group (set of running peers)
2279 * and a connection processor. This particular function creates
2280 * the connections for a 2d-torus, plus additional "closest"
2281 * connections per peer.
2283 * @param pg the peergroup to create the topology on
2284 * @param proc the connection processor to call to actually set
2285 * up connections between two peers
2286 * @param list the peer list to use
2288 * @return the number of connections that were set up
2292 create_2d_torus(struct GNUNET_TESTING_PeerGroup *pg,
2293 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2296 unsigned int square;
2299 unsigned int toggle = 1;
2300 unsigned int nodeToConnect;
2301 int connect_attempts;
2303 connect_attempts = 0;
2305 square = floor (sqrt (pg->total));
2309 if (square * square != pg->total)
2311 while (rows * cols < pg->total)
2313 if (toggle % 2 == 0)
2322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2324 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2327 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2328 * to the node to its right and above. Once this is over, we'll have our torus!
2329 * Special case for the last node (if the rows and columns are not equal), connect
2330 * to the first in the row to maintain topology.
2332 for (i = 0; i < pg->total; i++)
2334 /* First connect to the node to the right */
2335 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2336 nodeToConnect = i + 1;
2337 else if (i + 1 == pg->total)
2338 nodeToConnect = rows * cols - cols;
2340 nodeToConnect = i - cols + 1;
2342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2343 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2345 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2347 /* Second connect to the node immediately above */
2350 nodeToConnect = (rows * cols) - cols + i;
2351 if (nodeToConnect >= pg->total)
2352 nodeToConnect -= cols;
2355 nodeToConnect = i - cols;
2357 if (nodeToConnect < pg->total)
2360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2361 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2363 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2368 return connect_attempts;
2372 * Create a topology given a peer group (set of running peers)
2373 * and a connection processor.
2375 * @param pg the peergroup to create the topology on
2376 * @param proc the connection processor to call to actually set
2377 * up connections between two peers
2378 * @param list the peer list to use
2379 * @param check does the connection processor need to check before
2380 * performing an action on the list?
2382 * @return the number of connections that were set up
2386 create_clique(struct GNUNET_TESTING_PeerGroup *pg,
2387 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list,
2390 unsigned int outer_count;
2391 unsigned int inner_count;
2392 int connect_attempts;
2393 struct ProgressMeter *conn_meter;
2394 connect_attempts = 0;
2396 conn_meter = create_meter ((((pg->total * pg->total) + pg->total) / 2)
2397 - pg->total, "Create Clique ", GNUNET_NO);
2398 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2400 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2404 "Connecting peer %d to peer %d\n",
2405 outer_count, inner_count);
2407 connect_attempts += proc (pg, outer_count, inner_count, list, check);
2408 update_meter (conn_meter);
2411 reset_meter (conn_meter);
2412 free_meter (conn_meter);
2413 return connect_attempts;
2418 * Iterator over hash map entries.
2420 * @param cls closure the peer group
2421 * @param key the key stored in the hashmap is the
2422 * index of the peer to connect to
2423 * @param value value in the hash map, handle to the peer daemon
2424 * @return GNUNET_YES if we should continue to
2429 unblacklist_iterator (void *cls,
2430 const GNUNET_HashCode * key,
2433 struct UnblacklistContext *un_ctx = cls;
2434 uint32_t second_pos;
2436 uid_from_hash (key, &second_pos);
2438 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2446 * Create a blacklist topology based on the allowed topology
2447 * which disallows any connections not in the allowed topology
2448 * at the transport level.
2450 * @param pg the peergroup to create the topology on
2451 * @param proc the connection processor to call to allow
2452 * up connections between two peers
2454 * @return the number of connections that were set up
2458 copy_allowed (struct GNUNET_TESTING_PeerGroup *pg,
2459 GNUNET_TESTING_ConnectionProcessor proc)
2463 struct PeerConnection *iter;
2465 struct UnblacklistContext un_ctx;
2470 for (count = 0; count < pg->total - 1; count++)
2473 iter = pg->peers[count].allowed_peers_head;
2474 while (iter != NULL)
2476 remove_connections (pg, count, iter->index, BLACKLIST, GNUNET_YES);
2477 //unblacklist_connections(pg, count, iter->index);
2481 un_ctx.first_uid = count;
2482 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers,
2483 &unblacklist_iterator,
2487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2488 "Unblacklisted %u peers\n",
2495 * Create a topology given a peer group (set of running peers)
2496 * and a connection processor.
2498 * @param pg the peergroup to create the topology on
2499 * @param proc the connection processor to call to actually set
2500 * up connections between two peers
2501 * @param list which list should be modified
2503 * @return the number of connections that were set up
2507 create_line(struct GNUNET_TESTING_PeerGroup *pg,
2508 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2511 unsigned int connect_attempts;
2513 connect_attempts = 0;
2514 /* Connect each peer to the next highest numbered peer */
2515 for (count = 0; count < pg->total - 1; count++)
2518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2519 "Connecting peer %d to peer %d\n",
2522 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2525 return connect_attempts;
2529 * Create a topology given a peer group (set of running peers)
2530 * and a connection processor.
2532 * @param pg the peergroup to create the topology on
2533 * @param filename the file to read topology information from
2534 * @param proc the connection processor to call to actually set
2535 * up connections between two peers
2536 * @param list the peer list to use
2538 * @return the number of connections that were set up
2542 create_from_file(struct GNUNET_TESTING_PeerGroup *pg, char *filename,
2543 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2545 int connect_attempts;
2546 unsigned int first_peer_index;
2547 unsigned int second_peer_index;
2552 unsigned int total_peers;
2553 enum States curr_state;
2555 connect_attempts = 0;
2556 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2557 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2559 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2561 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2562 "Could not open file `%s' specified for topology!", filename);
2563 return connect_attempts;
2566 data = GNUNET_malloc_large (frstat.st_size);
2567 GNUNET_assert(data != NULL);
2568 if (frstat.st_size != GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2571 GNUNET_ERROR_TYPE_ERROR,
2572 "Could not read file %s specified for host list, ending test!",
2575 return connect_attempts;
2580 first_peer_index = 0;
2581 /* First line should contain a single integer, specifying the number of peers */
2582 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2583 curr_state = NUM_PEERS;
2584 while (count < frstat.st_size - 1)
2586 if ((buf[count] == '\n') || (buf[count] == ' '))
2596 total_peers = strtoul(&buf[count], NULL, 10);
2599 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2600 "Failed to read number of peers from topology file!\n");
2602 return connect_attempts;
2604 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2605 "Read %u total peers in topology\n", total_peers);
2606 GNUNET_assert(total_peers == pg->total);
2607 curr_state = PEER_INDEX;
2608 while ((buf[count] != '\n') && (count < frstat.st_size - 1))
2614 first_peer_index = strtoul(&buf[count], NULL, 10);
2617 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2618 "Failed to read peer index from topology file!\n");
2620 return connect_attempts;
2622 while ((buf[count] != ':') && (count < frstat.st_size - 1))
2625 curr_state = OTHER_PEER_INDEX;
2628 if (1 == sscanf (&buf[count], ":"))
2629 curr_state = OTHER_PEER_INDEX;
2632 case OTHER_PEER_INDEX:
2634 second_peer_index = strtoul(&buf[count], NULL, 10);
2637 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2638 "Failed to peer index from topology file!\n");
2640 return connect_attempts;
2642 /* Assume file is written with first peer 1, but array index is 0 */
2643 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index
2644 - 1, list, GNUNET_YES);
2645 while ((buf[count] != '\n') && (buf[count] != ',') && (count
2646 < frstat.st_size - 1))
2648 if (buf[count] == '\n')
2650 curr_state = PEER_INDEX;
2652 else if (buf[count] != ',')
2654 curr_state = OTHER_PEER_INDEX;
2659 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2660 "Found bad data in topology file while in state %d!\n",
2664 return connect_attempts;
2668 return connect_attempts;
2672 * Create a topology given a peer group (set of running peers)
2673 * and a connection processor.
2675 * @param pg the peergroup to create the topology on
2676 * @param proc the connection processor to call to actually set
2677 * up connections between two peers
2678 * @param list the peer list to use
2680 * @return the number of connections that were set up
2684 create_ring(struct GNUNET_TESTING_PeerGroup *pg,
2685 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2688 int connect_attempts;
2690 connect_attempts = 0;
2692 /* Connect each peer to the next highest numbered peer */
2693 for (count = 0; count < pg->total - 1; count++)
2696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2697 "Connecting peer %d to peer %d\n", count, count + 1);
2699 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2702 /* Connect the last peer to the first peer */
2703 connect_attempts += proc (pg, pg->total - 1, 0, list, GNUNET_YES);
2705 return connect_attempts;
2710 * Iterator for writing friends of a peer to a file.
2712 * @param cls closure, an open writable file handle
2713 * @param key the key the daemon was stored under
2714 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2716 * @return GNUNET_YES to continue iteration
2718 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2719 * with a single file_iterator that takes a closure which contains
2720 * the prefix to write before the peer. Then this could be used
2721 * for blacklisting multiple transports and writing the friend
2722 * file. I'm sure *someone* will complain loudly about other
2723 * things that negate these functions even existing so no point in
2727 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2729 FILE *temp_friend_handle = cls;
2730 struct GNUNET_TESTING_Daemon *peer = value;
2731 struct GNUNET_PeerIdentity *temppeer;
2732 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2734 temppeer = &peer->id;
2735 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2736 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2741 struct BlacklistContext
2744 * The (open) file handle to write to
2746 FILE *temp_file_handle;
2749 * The transport that this peer will be blacklisted on.
2755 * Iterator for writing blacklist data to appropriate files.
2757 * @param cls closure, an open writable file handle
2758 * @param key the key the daemon was stored under
2759 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2761 * @return GNUNET_YES to continue iteration
2764 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2766 struct BlacklistContext *blacklist_ctx = cls;
2767 struct GNUNET_TESTING_Daemon *peer = value;
2768 struct GNUNET_PeerIdentity *temppeer;
2769 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2771 temppeer = &peer->id;
2772 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2773 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2774 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2775 blacklist_ctx->transport, (char *) &peer_enc);
2782 * Create the friend files based on the PeerConnection's
2783 * of each peer in the peer group, and copy the files
2784 * to the appropriate place
2786 * @param pg the peer group we are dealing with
2789 create_and_copy_friend_files(struct GNUNET_TESTING_PeerGroup *pg)
2791 FILE *temp_friend_handle;
2792 unsigned int pg_iter;
2793 char *temp_service_path;
2794 struct GNUNET_OS_Process **procarr;
2798 enum GNUNET_OS_ProcessStatusType type;
2799 unsigned long return_code;
2807 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2808 struct PeerConnection *conn_iter;
2810 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2811 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2813 mytemp = GNUNET_DISK_mktemp ("friends");
2814 GNUNET_assert (mytemp != NULL);
2815 temp_friend_handle = fopen (mytemp, "wt");
2816 GNUNET_assert (temp_friend_handle != NULL);
2818 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2819 while (conn_iter != NULL)
2821 GNUNET_CRYPTO_hash_to_enc (
2822 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2824 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2825 conn_iter = conn_iter->next;
2828 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2829 &friend_file_iterator,
2830 temp_friend_handle);
2832 fclose (temp_friend_handle);
2835 != GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2837 "PATHS", "SERVICEHOME",
2838 &temp_service_path))
2840 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2841 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2842 "SERVICEHOME", "PATHS");
2843 if (UNLINK (mytemp) != 0)
2844 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
2847 GNUNET_free (mytemp);
2851 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2853 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2854 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
2856 GNUNET_assert(procarr[pg_iter] != NULL);
2858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2859 "Copying file with command cp %s %s\n",
2863 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2864 GNUNET_OS_process_close (procarr[pg_iter]);
2867 else /* Remote, scp the file to the correct place */
2869 if (NULL != pg->peers[pg_iter].daemon->username)
2870 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2871 pg->peers[pg_iter].daemon->username,
2872 pg->peers[pg_iter].daemon->hostname,
2875 GNUNET_asprintf (&arg, "%s:%s/friends",
2876 pg->peers[pg_iter].daemon->hostname,
2878 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
2880 GNUNET_assert(procarr[pg_iter] != NULL);
2881 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2882 GNUNET_OS_process_close (procarr[pg_iter]);
2883 if (ret != GNUNET_OK)
2885 /* FIXME: free contents of 'procarr' array */
2886 GNUNET_free (procarr);
2887 GNUNET_free (temp_service_path);
2888 GNUNET_free (mytemp);
2892 procarr[pg_iter] = NULL;
2894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2895 "Copying file with command scp %s %s\n",
2901 GNUNET_free (temp_service_path);
2902 GNUNET_free (mytemp);
2907 ret = GNUNET_SYSERR;
2908 while ((count < max_wait) && (ret != GNUNET_OK))
2911 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2915 "Checking copy status of file %d\n",
2918 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2920 if (GNUNET_OS_process_status
2921 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2923 ret = GNUNET_SYSERR;
2925 else if ((type != GNUNET_OS_PROCESS_EXITED)
2926 || (return_code != 0))
2928 ret = GNUNET_SYSERR;
2932 GNUNET_OS_process_close (procarr[pg_iter]);
2933 procarr[pg_iter] = NULL;
2935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2943 if (ret == GNUNET_SYSERR)
2945 /* FIXME: why sleep here? -CG */
2951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2952 _("Finished copying all friend files!\n"));
2955 GNUNET_free (procarr);
2960 * Create the blacklist files based on the PeerConnection's
2961 * of each peer in the peer group, and copy the files
2962 * to the appropriate place.
2964 * @param pg the peer group we are dealing with
2965 * @param transports space delimited list of transports to blacklist
2968 create_and_copy_blacklist_files(struct GNUNET_TESTING_PeerGroup *pg,
2969 const char *transports)
2971 FILE *temp_file_handle;
2972 unsigned int pg_iter;
2973 char *temp_service_path;
2974 struct GNUNET_OS_Process **procarr;
2977 enum GNUNET_OS_ProcessStatusType type;
2978 unsigned long return_code;
2985 char *temp_transports;
2987 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2988 struct PeerConnection *conn_iter;
2990 static struct BlacklistContext blacklist_ctx;
2993 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2994 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2996 mytemp = GNUNET_DISK_mktemp ("blacklist");
2997 GNUNET_assert (mytemp != NULL);
2998 temp_file_handle = fopen (mytemp, "wt");
2999 GNUNET_assert (temp_file_handle != NULL);
3000 temp_transports = GNUNET_strdup (transports);
3002 blacklist_ctx.temp_file_handle = temp_file_handle;
3004 transport_len = strlen (temp_transports) + 1;
3007 for (i = 0; i < transport_len; i++)
3009 if ((temp_transports[i] == ' ') && (pos == NULL))
3010 continue; /* At start of string (whitespace) */
3011 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
3013 temp_transports[i] = '\0';
3015 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
3016 while (conn_iter != NULL)
3018 GNUNET_CRYPTO_hash_to_enc (
3019 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
3021 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
3022 conn_iter = conn_iter->next;
3025 blacklist_ctx.transport = pos;
3026 (void) GNUNET_CONTAINER_multihashmap_iterate (pg->
3028 [pg_iter].blacklisted_peers,
3029 &blacklist_file_iterator,
3033 } /* At beginning of actual string */
3034 else if (pos == NULL)
3036 pos = &temp_transports[i];
3040 GNUNET_free (temp_transports);
3041 fclose (temp_file_handle);
3044 != GNUNET_CONFIGURATION_get_value_string (
3045 pg->peers[pg_iter]. daemon->cfg,
3046 "PATHS", "SERVICEHOME",
3047 &temp_service_path))
3050 GNUNET_ERROR_TYPE_WARNING,
3052 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
3053 "SERVICEHOME", "PATHS");
3054 if (UNLINK (mytemp) != 0)
3055 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
3057 GNUNET_free (mytemp);
3061 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
3063 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
3064 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
3067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3068 _("Copying file with command cp %s %s\n"), mytemp, arg);
3073 else /* Remote, scp the file to the correct place */
3075 if (NULL != pg->peers[pg_iter].daemon->username)
3076 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
3077 pg->peers[pg_iter].daemon->username,
3078 pg->peers[pg_iter].daemon->hostname,
3081 GNUNET_asprintf (&arg, "%s:%s/blacklist",
3082 pg->peers[pg_iter].daemon->hostname,
3084 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
3086 GNUNET_assert(procarr[pg_iter] != NULL);
3087 GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
3090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3091 _("Copying file with command scp %s %s\n"), mytemp,
3096 GNUNET_free (temp_service_path);
3097 GNUNET_free (mytemp);
3101 ret = GNUNET_SYSERR;
3102 while ((count < max_wait) && (ret != GNUNET_OK))
3105 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3109 _("Checking copy status of file %d\n"), pg_iter);
3111 if (procarr[pg_iter] != NULL) /* Check for already completed! */
3113 if (GNUNET_OS_process_status (procarr[pg_iter], &type,
3114 &return_code) != GNUNET_OK)
3116 ret = GNUNET_SYSERR;
3118 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
3120 ret = GNUNET_SYSERR;
3124 GNUNET_OS_process_close (procarr[pg_iter]);
3125 procarr[pg_iter] = NULL;
3127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3128 _("File %d copied\n"), pg_iter);
3134 if (ret == GNUNET_SYSERR)
3136 /* FIXME: why sleep here? -CG */
3142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3143 _("Finished copying all blacklist files!\n"));
3145 GNUNET_free (procarr);
3149 /* Forward Declaration */
3151 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3154 * Choose a random peer's next connection to create, and
3155 * call schedule_connect to set up the connect task.
3157 * @param ct_ctx the overall connection context
3160 preschedule_connect(struct GNUNET_TESTING_PeerGroup *pg)
3162 struct ConnectTopologyContext *ct_ctx = &pg->ct_ctx;
3163 struct PeerConnection *connection_iter;
3164 struct ConnectContext *connect_context;
3165 uint32_t random_peer;
3167 if (ct_ctx->remaining_connections == 0)
3170 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3171 while (pg->peers[random_peer].connect_peers_head == NULL)
3172 random_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3175 connection_iter = pg->peers[random_peer].connect_peers_head;
3176 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3177 connect_context->first_index = random_peer;
3178 connect_context->second_index = connection_iter->index;
3179 connect_context->ct_ctx = ct_ctx;
3180 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3181 GNUNET_CONTAINER_DLL_remove(pg->peers[random_peer].connect_peers_head, pg->peers[random_peer].connect_peers_tail, connection_iter);
3182 GNUNET_free(connection_iter);
3183 ct_ctx->remaining_connections--;
3187 /* Forward declaration */
3188 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3191 * Close connections and free the hello context.
3193 * @param cls the 'struct SendHelloContext *'
3194 * @param tc scheduler context
3197 free_hello_context (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3199 struct SendHelloContext *send_hello_context = cls;
3200 if (send_hello_context->peer->daemon->server != NULL)
3202 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3203 send_hello_context->peer->daemon->server = NULL;
3205 if (send_hello_context->peer->daemon->th != NULL)
3207 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3208 send_hello_context->peer->daemon->th = NULL;
3210 if (send_hello_context->core_connect_task != GNUNET_SCHEDULER_NO_TASK)
3212 GNUNET_SCHEDULER_cancel(send_hello_context->core_connect_task);
3213 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3215 send_hello_context->pg->outstanding_connects--;
3216 GNUNET_free(send_hello_context);
3220 * For peers that haven't yet connected, notify
3221 * the caller that they have failed (timeout).
3223 * @param cls the 'struct SendHelloContext *'
3224 * @param tc scheduler context
3227 notify_remaining_connections_failed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3229 struct SendHelloContext *send_hello_context = cls;
3230 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3231 struct PeerConnection *connection;
3233 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3234 send_hello_context->peer->daemon->server = NULL;
3236 connection = send_hello_context->peer->connect_peers_head;
3238 while (connection != NULL)
3240 if (pg->notify_connection != NULL)
3242 pg->notify_connection(pg->notify_connection_cls,
3243 &send_hello_context->peer->daemon->id,
3244 &pg->peers[connection->index].daemon->id,
3246 send_hello_context->peer->daemon->cfg,
3247 pg->peers[connection->index].daemon->cfg,
3248 send_hello_context->peer->daemon,
3249 pg->peers[connection->index].daemon,
3250 "Peers failed to connect (timeout)");
3252 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3253 GNUNET_free(connection);
3254 connection = connection->next;
3256 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3258 other_peer = &pg->peers[connection->index];
3263 * For peers that haven't yet connected, send
3264 * CORE connect requests.
3266 * @param cls the 'struct SendHelloContext *'
3267 * @param tc scheduler context
3270 send_core_connect_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3272 struct SendHelloContext *send_hello_context = cls;
3273 struct PeerConnection *conn;
3274 GNUNET_assert(send_hello_context->peer->daemon->server != NULL);
3276 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3278 send_hello_context->connect_attempts++;
3279 if (send_hello_context->connect_attempts < send_hello_context->pg->ct_ctx.connect_attempts)
3281 conn = send_hello_context->peer->connect_peers_head;
3282 while (conn != NULL)
3284 GNUNET_CORE_peer_request_connect(send_hello_context->peer->daemon->server,
3285 &send_hello_context->pg->peers[conn->index].daemon->id,
3290 send_hello_context->core_connect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_divide(send_hello_context->pg->ct_ctx.connect_timeout, send_hello_context->pg->ct_ctx.connect_attempts) ,
3291 &send_core_connect_requests,
3292 send_hello_context);
3296 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Timeout before all connections created, marking rest as failed!\n");
3297 GNUNET_SCHEDULER_add_now(¬ify_remaining_connections_failed, send_hello_context);
3303 * Success, connection is up. Signal client our success.
3305 * @param cls our "struct SendHelloContext"
3306 * @param peer identity of the peer that has connected
3307 * @param atsi performance information
3309 * FIXME: remove peers from BOTH lists, call notify twice, should
3310 * double the speed of connections as long as the list iteration
3311 * doesn't take too long!
3314 core_connect_notify (void *cls,
3315 const struct GNUNET_PeerIdentity *peer,
3316 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3318 struct SendHelloContext *send_hello_context = cls;
3319 struct PeerConnection *connection;
3320 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3322 struct PeerData *other_peer;
3325 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3326 "Connected peer %s to peer %s\n",
3327 ctx->d1->shortname, GNUNET_i2s(peer));
3330 if (0 == memcmp(&send_hello_context->peer->daemon->id, peer, sizeof(struct GNUNET_PeerIdentity)))
3333 connection = send_hello_context->peer->connect_peers_head;
3338 while ((connection != NULL) &&
3339 (0 != memcmp(&pg->peers[connection->index].daemon->id, peer, sizeof(struct GNUNET_PeerIdentity))))
3341 connection = connection->next;
3344 if (connection == NULL)
3346 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Connected peer %s to %s, not in list (no problem(?))\n", GNUNET_i2s(peer), send_hello_context->peer->daemon->shortname);
3351 other_peer = &pg->peers[connection->index];
3353 if (pg->notify_connection != NULL)
3355 pg->notify_connection(pg->notify_connection_cls,
3356 &send_hello_context->peer->daemon->id,
3359 send_hello_context->peer->daemon->cfg,
3360 pg->peers[connection->index].daemon->cfg,
3361 send_hello_context->peer->daemon,
3362 pg->peers[connection->index].daemon,
3365 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3366 GNUNET_free(connection);
3370 /* Notify of reverse connection and remove from other peers list of outstanding */
3371 if (other_peer != NULL)
3373 connection = other_peer->connect_peers_head;
3374 while ((connection != NULL) &&
3375 (0 != memcmp(&send_hello_context->peer->daemon->id, &pg->peers[connection->index].daemon->id, sizeof(struct GNUNET_PeerIdentity))))
3377 connection = connection->next;
3379 if (connection != NULL)
3381 if (pg->notify_connection != NULL)
3383 pg->notify_connection(pg->notify_connection_cls,
3385 &send_hello_context->peer->daemon->id,
3387 pg->peers[connection->index].daemon->cfg,
3388 send_hello_context->peer->daemon->cfg,
3389 pg->peers[connection->index].daemon,
3390 send_hello_context->peer->daemon,
3394 GNUNET_CONTAINER_DLL_remove(other_peer->connect_peers_head, other_peer->connect_peers_tail, connection);
3395 GNUNET_free(connection);
3400 if (send_hello_context->peer->connect_peers_head == NULL)
3402 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3407 * Notify of a successful connection to the core service.
3409 * @param cls a struct SendHelloContext *
3410 * @param server handle to the core service
3411 * @param my_identity the peer identity of this peer
3412 * @param publicKey the public key of the peer
3415 core_init (void *cls,
3416 struct GNUNET_CORE_Handle * server,
3417 const struct GNUNET_PeerIdentity *
3420 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
3423 struct SendHelloContext *send_hello_context = cls;
3424 send_hello_context->core_ready = GNUNET_YES;
3428 * Function called once a hello has been sent
3429 * to the transport, move on to the next one
3430 * or go away forever.
3432 * @param cls the 'struct SendHelloContext *'
3433 * @param tc scheduler context
3436 hello_sent_callback (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3438 struct SendHelloContext *send_hello_context = cls;
3439 //unsigned int pg_iter;
3440 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3442 GNUNET_free(send_hello_context);
3446 send_hello_context->pg->remaining_hellos--;
3448 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sent HELLO, have %d remaining!\n", send_hello_context->pg->remaining_hellos);
3450 if (send_hello_context->peer_pos == NULL) /* All HELLOs (for this peer!) have been transmitted! */
3453 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All hellos for this peer sent, disconnecting transport!\n");
3455 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3456 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3457 send_hello_context->peer->daemon->th = NULL;
3459 /*if (send_hello_context->pg->remaining_hellos == 0)
3461 for (pg_iter = 0; pg_iter < send_hello_context->pg->max_outstanding_connections; pg_iter++)
3463 preschedule_connect(&send_hello_context->pg->ct_ctx);
3467 GNUNET_assert (send_hello_context->peer->daemon->server == NULL);
3468 send_hello_context->peer->daemon->server = GNUNET_CORE_connect(send_hello_context->peer->cfg,
3472 &core_connect_notify,
3479 send_hello_context->core_connect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_divide(send_hello_context->pg->ct_ctx.connect_timeout, send_hello_context->pg->ct_ctx.connect_attempts),
3480 &send_core_connect_requests,
3481 send_hello_context);
3484 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3488 * Connect to a peer, give it all the HELLO's of those peers
3489 * we will later ask it to connect to.
3491 * @param ct_ctx the overall connection context
3493 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3495 struct SendHelloContext *send_hello_context = cls;
3496 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3498 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3500 GNUNET_free(send_hello_context);
3504 GNUNET_assert(send_hello_context->peer_pos != NULL); /* All of the HELLO sends to be scheduled have been scheduled! */
3506 if (((send_hello_context->peer->daemon->th == NULL) &&
3507 (pg->outstanding_connects > pg->max_outstanding_connections)) ||
3508 (pg->stop_connects == GNUNET_YES))
3510 #if VERBOSE_TESTING > 2
3511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3513 ("Delaying connect, we have too many outstanding connections!\n"));
3515 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3516 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3517 &schedule_send_hellos, send_hello_context);
3521 #if VERBOSE_TESTING > 2
3522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3523 _("Creating connection, outstanding_connections is %d\n"),
3524 outstanding_connects);
3526 if (send_hello_context->peer->daemon->th == NULL)
3528 pg->outstanding_connects++; /* Actual TRANSPORT, CORE connections! */
3529 send_hello_context->peer->daemon->th
3530 = GNUNET_TRANSPORT_connect (send_hello_context->peer->cfg, NULL,
3531 send_hello_context, NULL, NULL, NULL);
3534 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3535 _("Offering Hello of peer %s to peer %s\n"),
3536 send_hello_context->peer->daemon->shortname, pg->peers[send_hello_context->peer_pos->index].daemon->shortname);
3538 GNUNET_TRANSPORT_offer_hello(send_hello_context->peer->daemon->th,
3539 (const struct GNUNET_MessageHeader *)pg->peers[send_hello_context->peer_pos->index].daemon->hello,
3540 &hello_sent_callback,
3541 send_hello_context);
3542 send_hello_context->peer_pos = send_hello_context->peer_pos->next;
3543 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3549 * Internal notification of a connection, kept so that we can ensure some connections
3550 * happen instead of flooding all testing daemons with requests to connect.
3553 internal_connect_notify(void *cls, const struct GNUNET_PeerIdentity *first,
3554 const struct GNUNET_PeerIdentity *second,
3556 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
3557 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
3558 struct GNUNET_TESTING_Daemon *first_daemon,
3559 struct GNUNET_TESTING_Daemon *second_daemon,
3562 struct ConnectContext *connect_ctx = cls;
3563 struct ConnectTopologyContext *ct_ctx = connect_ctx->ct_ctx;
3564 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
3565 struct PeerConnection *connection;
3567 GNUNET_assert (0 < pg->outstanding_connects);
3568 pg->outstanding_connects--;
3571 * Check whether the inverse connection has been scheduled yet,
3572 * if not, we can remove it from the other peers list and avoid
3573 * even trying to connect them again!
3575 connection = pg->peers[connect_ctx->second_index].connect_peers_head;
3580 while ((connection != NULL) && (0
3581 != memcmp (first, &pg->peers[connection->index].daemon->id,
3582 sizeof(struct GNUNET_PeerIdentity))))
3584 connection = connection->next;
3587 if (connection != NULL) /* Can safely remove! */
3589 GNUNET_assert (0 < ct_ctx->remaining_connections);
3590 ct_ctx->remaining_connections--;
3591 if (pg->notify_connection != NULL) /* Notify of reverse connection */
3592 pg->notify_connection (pg->notify_connection_cls, second, first,
3593 distance, second_cfg, first_cfg, second_daemon,
3594 first_daemon, emsg);
3596 GNUNET_CONTAINER_DLL_remove(pg->peers[connect_ctx->second_index].connect_peers_head, pg->peers[connect_ctx->second_index].connect_peers_tail, connection);
3597 GNUNET_free(connection);
3600 if (ct_ctx->remaining_connections == 0)
3602 if (ct_ctx->notify_connections_done != NULL)
3604 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
3605 ct_ctx->notify_connections_done = NULL;
3609 preschedule_connect (pg);
3611 if (pg->notify_connection != NULL)
3612 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
3613 first_cfg, second_cfg, first_daemon, second_daemon,
3616 GNUNET_free(connect_ctx);
3620 * Either delay a connection (because there are too many outstanding)
3621 * or schedule it for right now.
3623 * @param cls a connection context
3624 * @param tc the task runtime context
3627 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3629 struct ConnectContext *connect_context = cls;
3630 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
3632 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3635 if ((pg->outstanding_connects > pg->max_outstanding_connections)
3636 || (pg->stop_connects == GNUNET_YES))
3639 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3641 ("Delaying connect, we have too many outstanding connections!\n"));
3643 GNUNET_SCHEDULER_add_delayed (
3644 GNUNET_TIME_relative_multiply (
3645 GNUNET_TIME_UNIT_MILLISECONDS,
3647 &schedule_connect, connect_context);
3652 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3653 _("Creating connection, outstanding_connections is %d (max %d)\n"),
3654 pg->outstanding_connects, pg->max_outstanding_connections);
3656 pg->outstanding_connects++;
3657 pg->total_connects_scheduled++;
3658 GNUNET_TESTING_daemons_connect (
3659 pg->peers[connect_context->first_index].daemon,
3660 pg->peers[connect_context->second_index].daemon,
3661 connect_context->ct_ctx->connect_timeout,
3662 connect_context->ct_ctx->connect_attempts,
3668 &internal_connect_notify, connect_context); /* FIXME: free connect context! */
3674 * Iterator for actually scheduling connections to be created
3675 * between two peers.
3677 * @param cls closure, a GNUNET_TESTING_Daemon
3678 * @param key the key the second Daemon was stored under
3679 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3681 * @return GNUNET_YES to continue iteration
3684 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3686 struct ConnectTopologyContext *ct_ctx = cls;
3687 struct PeerData *first = ct_ctx->first;
3688 struct GNUNET_TESTING_Daemon *second = value;
3689 struct ConnectContext *connect_context;
3691 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3692 connect_context->first = first->daemon;
3693 connect_context->second = second;
3694 connect_context->ct_ctx = ct_ctx;
3695 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3703 * Iterator for copying all entries in the allowed hashmap to the
3706 * @param cls closure, a GNUNET_TESTING_Daemon
3707 * @param key the key the second Daemon was stored under
3708 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3710 * @return GNUNET_YES to continue iteration
3713 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3715 struct PeerData *first = cls;
3717 GNUNET_assert (GNUNET_OK ==
3718 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
3720 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3727 * Make the peers to connect the same as those that are allowed to be
3730 * @param pg the peer group
3733 copy_allowed_topology(struct GNUNET_TESTING_PeerGroup *pg)
3735 unsigned int pg_iter;
3739 struct PeerConnection *iter;
3743 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3746 iter = pg->peers[pg_iter].allowed_peers_head;
3747 while (iter != NULL)
3749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3750 "Creating connection between %d and %d\n", pg_iter,
3752 total += add_connections (pg, pg_iter, iter->index, CONNECT,
3754 //total += add_actual_connections(pg, pg_iter, iter->index);
3759 GNUNET_CONTAINER_multihashmap_iterate (pg->
3760 peers[pg_iter].allowed_peers,
3761 ©_topology_iterator,
3762 &pg->peers[pg_iter]);
3764 if (GNUNET_SYSERR == ret)
3765 return GNUNET_SYSERR;
3767 total = total + ret;
3774 * Connect the topology as specified by the PeerConnection's
3775 * of each peer in the peer group
3777 * @param pg the peer group we are dealing with
3778 * @param connect_timeout how long try connecting two peers
3779 * @param connect_attempts how many times (max) to attempt
3780 * @param notify_callback callback to notify when finished
3781 * @param notify_cls closure for notify callback
3783 * @return the number of connections that will be attempted
3786 connect_topology(struct GNUNET_TESTING_PeerGroup *pg,
3787 struct GNUNET_TIME_Relative connect_timeout,
3788 unsigned int connect_attempts,
3789 GNUNET_TESTING_NotifyCompletion notify_callback,
3792 unsigned int pg_iter;
3796 struct PeerConnection *connection_iter;
3799 struct SendHelloContext *send_hello_context;
3803 pg->ct_ctx.notify_connections_done = notify_callback;
3804 pg->ct_ctx.notify_cls = notify_cls;
3807 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3810 connection_iter = pg->peers[pg_iter].connect_peers_head;
3811 while (connection_iter != NULL)
3813 connection_iter = connection_iter->next;
3818 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3825 pg->ct_ctx.connect_timeout = connect_timeout;
3826 pg->ct_ctx.connect_attempts = connect_attempts;
3827 pg->ct_ctx.remaining_connections = total;
3830 /* First give all peers the HELLO's of other peers (connect to first peer's transport service, give HELLO's of other peers, continue...) */
3831 pg->remaining_hellos = total;
3832 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3834 send_hello_context = GNUNET_malloc(sizeof(struct SendHelloContext));
3835 send_hello_context->peer = &pg->peers[pg_iter];
3836 send_hello_context->peer_pos = pg->peers[pg_iter].connect_peers_head;
3837 send_hello_context->pg = pg;
3838 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3841 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3843 preschedule_connect (pg);
3851 * Takes a peer group and creates a topology based on the
3852 * one specified. Creates a topology means generates friend
3853 * files for the peers so they can only connect to those allowed
3854 * by the topology. This will only have an effect once peers
3855 * are started if the FRIENDS_ONLY option is set in the base
3856 * config. Also takes an optional restrict topology which
3857 * disallows connections based on particular transports
3858 * UNLESS they are specified in the restricted topology.
3860 * @param pg the peer group struct representing the running peers
3861 * @param topology which topology to connect the peers in
3862 * @param restrict_topology disallow restrict_transports transport
3863 * connections to peers NOT in this topology
3864 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3865 * @param restrict_transports space delimited list of transports to blacklist
3866 * to create restricted topology
3868 * @return the maximum number of connections were all allowed peers
3869 * connected to each other
3872 GNUNET_TESTING_create_topology(struct GNUNET_TESTING_PeerGroup *pg,
3873 enum GNUNET_TESTING_Topology topology,
3874 enum GNUNET_TESTING_Topology restrict_topology,
3875 const char *restrict_transports)
3879 unsigned int num_connections;
3880 int unblacklisted_connections;
3882 struct PeerConnection *conn_iter;
3883 struct PeerConnection *temp_conn;
3888 for (i = 0; i < pg->total; i++)
3890 pg->peers[i].allowed_peers =
3891 GNUNET_CONTAINER_multihashmap_create (100);
3892 pg->peers[i].connect_peers =
3893 GNUNET_CONTAINER_multihashmap_create (100);
3894 pg->peers[i].blacklisted_peers =
3895 GNUNET_CONTAINER_multihashmap_create (100);
3896 pg->peers[i].pg = pg;
3902 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3906 num_connections = create_clique (pg, &add_connections, ALLOWED, GNUNET_NO);
3908 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3911 _("Creating small world (ring) topology\n"));
3913 num_connections = create_small_world_ring (pg, &add_connections, ALLOWED);
3915 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3918 _("Creating small world (2d-torus) topology\n"));
3920 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3922 case GNUNET_TESTING_TOPOLOGY_RING:
3924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3926 num_connections = create_ring (pg, &add_connections, ALLOWED);
3928 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3932 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3934 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3937 _("Creating Erdos-Renyi topology\n"));
3939 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3941 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3945 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3947 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3950 _("Creating Scale Free topology\n"));
3952 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3954 case GNUNET_TESTING_TOPOLOGY_LINE:
3956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3957 _("Creating straight line topology\n"));
3959 num_connections = create_line (pg, &add_connections, ALLOWED);
3961 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3964 _("Creating topology from file!\n"));
3966 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing",
3969 num_connections = create_from_file (pg, filename, &add_connections,
3974 GNUNET_ERROR_TYPE_WARNING,
3975 "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3976 num_connections = 0;
3979 case GNUNET_TESTING_TOPOLOGY_NONE:
3981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3983 ("Creating no allowed topology (all peers can connect at core level)\n"));
3985 num_connections = pg->total * pg->total; /* Clique is allowed! */
3988 num_connections = 0;
3992 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING",
3995 ret = create_and_copy_friend_files (pg);
3996 if (ret != GNUNET_OK)
3999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4000 _("Failed during friend file copying!\n"));
4002 return GNUNET_SYSERR;
4007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4008 _("Friend files created/copied successfully!\n"));
4013 /* Use the create clique method to initially set all connections as blacklisted. */
4014 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology
4015 != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
4016 create_clique (pg, &add_connections, BLACKLIST, GNUNET_NO);
4018 return num_connections;
4020 unblacklisted_connections = 0;
4021 /* Un-blacklist connections as per the topology specified */
4022 switch (restrict_topology)
4024 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4027 _("Blacklisting all but clique topology\n"));
4029 unblacklisted_connections = create_clique (pg, &remove_connections,
4030 BLACKLIST, GNUNET_NO);
4032 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4035 _("Blacklisting all but small world (ring) topology\n"));
4037 unblacklisted_connections = create_small_world_ring (pg,
4038 &remove_connections,
4041 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4045 ("Blacklisting all but small world (2d-torus) topology\n"));
4047 unblacklisted_connections = create_small_world (pg, &remove_connections,
4050 case GNUNET_TESTING_TOPOLOGY_RING:
4052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4053 _("Blacklisting all but ring topology\n"));
4055 unblacklisted_connections
4056 = create_ring (pg, &remove_connections, BLACKLIST);
4058 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4061 _("Blacklisting all but 2d torus topology\n"));
4063 unblacklisted_connections = create_2d_torus (pg, &remove_connections,
4066 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4069 _("Blacklisting all but Erdos-Renyi topology\n"));
4071 unblacklisted_connections = create_erdos_renyi (pg, &remove_connections,
4074 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4077 _("Blacklisting all but InterNAT topology\n"));
4081 for (off = 0; off < pg->total; off++)
4083 conn_iter = pg->peers[off].allowed_peers_head;
4084 while (conn_iter != NULL)
4086 temp_conn = conn_iter->next;
4087 GNUNET_free(conn_iter);
4088 conn_iter = temp_conn;
4090 pg->peers[off].allowed_peers_head = NULL;
4091 pg->peers[off].allowed_peers_tail = NULL;
4093 conn_iter = pg->peers[off].connect_peers_head;
4094 while (conn_iter != NULL)
4096 temp_conn = conn_iter->next;
4097 GNUNET_free(conn_iter);
4098 conn_iter = temp_conn;
4100 pg->peers[off].connect_peers_head = NULL;
4101 pg->peers[off].connect_peers_tail = NULL;
4103 unblacklisted_connections
4104 = create_nated_internet_copy (pg, &remove_connections, BLACKLIST);
4106 unblacklisted_connections =
4107 create_nated_internet (pg, &remove_connections, BLACKLIST);
4111 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4114 _("Blacklisting all but Scale Free topology\n"));
4116 unblacklisted_connections = create_scale_free (pg, &remove_connections,
4119 case GNUNET_TESTING_TOPOLOGY_LINE:
4121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4122 _("Blacklisting all but straight line topology\n"));
4124 unblacklisted_connections
4125 = create_line (pg, &remove_connections, BLACKLIST);
4130 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
4132 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n",
4133 restrict_transports);
4134 ret = create_and_copy_blacklist_files (pg, restrict_transports);
4135 if (ret != GNUNET_OK)
4138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4139 _("Failed during blacklist file copying!\n"));
4146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4147 _("Blacklist files created/copied successfully!\n"));
4151 return num_connections;
4156 * Iterator for choosing random peers to connect.
4158 * @param cls closure, a RandomContext
4159 * @param key the key the second Daemon was stored under
4160 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4162 * @return GNUNET_YES to continue iteration
4165 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4167 struct RandomContext *random_ctx = cls;
4168 double random_number;
4169 uint32_t second_pos;
4170 GNUNET_HashCode first_hash;
4173 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4174 UINT64_MAX)) / ((double) UINT64_MAX);
4175 if (random_number < random_ctx->percentage)
4177 GNUNET_assert (GNUNET_OK ==
4178 GNUNET_CONTAINER_multihashmap_put (random_ctx->
4179 first->connect_peers_working_set,
4181 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4184 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
4185 uid_from_hash (key, &second_pos);
4186 hash_from_uid (random_ctx->first_uid, &first_hash);
4187 GNUNET_assert (random_ctx->pg->total > second_pos);
4188 GNUNET_assert (GNUNET_YES ==
4189 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
4191 [second_pos].connect_peers,
4200 * Iterator for adding at least X peers to a peers connection set.
4202 * @param cls closure, MinimumContext
4203 * @param key the key the second Daemon was stored under
4204 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4206 * @return GNUNET_YES to continue iteration
4209 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4211 struct MinimumContext *min_ctx = cls;
4212 uint32_t second_pos;
4213 GNUNET_HashCode first_hash;
4216 if (GNUNET_CONTAINER_multihashmap_size
4217 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
4219 for (i = 0; i < min_ctx->num_to_add; i++)
4221 if (min_ctx->pg_array[i] == min_ctx->current)
4223 GNUNET_assert (GNUNET_OK ==
4224 GNUNET_CONTAINER_multihashmap_put
4225 (min_ctx->first->connect_peers_working_set, key,
4227 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4228 uid_from_hash (key, &second_pos);
4229 hash_from_uid (min_ctx->first_uid, &first_hash);
4230 GNUNET_assert (min_ctx->pg->total > second_pos);
4231 GNUNET_assert (GNUNET_OK ==
4232 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4234 [second_pos].connect_peers_working_set,
4238 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4239 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
4240 GNUNET_assert (GNUNET_YES ==
4241 GNUNET_CONTAINER_multihashmap_remove
4242 (min_ctx->pg->peers[second_pos].connect_peers,
4243 &first_hash, min_ctx->first->daemon));
4250 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
4255 * Iterator for adding peers to a connection set based on a depth first search.
4257 * @param cls closure, MinimumContext
4258 * @param key the key the second daemon was stored under
4259 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4261 * @return GNUNET_YES to continue iteration
4264 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4266 struct DFSContext *dfs_ctx = cls;
4267 GNUNET_HashCode first_hash;
4269 if (dfs_ctx->current == dfs_ctx->chosen)
4271 GNUNET_assert (GNUNET_OK ==
4272 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4273 first->connect_peers_working_set,
4275 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4276 uid_from_hash (key, &dfs_ctx->second_uid);
4277 hash_from_uid (dfs_ctx->first_uid, &first_hash);
4278 GNUNET_assert (GNUNET_OK ==
4279 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4281 [dfs_ctx->second_uid].connect_peers_working_set,
4285 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4286 GNUNET_assert (GNUNET_YES ==
4287 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
4289 [dfs_ctx->second_uid].connect_peers,
4293 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
4294 return GNUNET_NO; /* We have found our peer, don't iterate more */
4303 * From the set of connections possible, choose percentage percent of connections
4304 * to actually connect.
4306 * @param pg the peergroup we are dealing with
4307 * @param percentage what percent of total connections to make
4310 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg,
4315 struct PeerConnection *conn_iter;
4316 double random_number;
4318 struct RandomContext random_ctx;
4321 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4324 conn_iter = pg->peers[pg_iter].connect_peers_head;
4325 while (conn_iter != NULL)
4328 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4330 / ((double) UINT64_MAX);
4331 if (random_number < percentage)
4333 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4336 conn_iter = conn_iter->next;
4339 random_ctx.first_uid = pg_iter;
4340 random_ctx.first = &pg->peers[pg_iter];
4341 random_ctx.percentage = percentage;
4343 pg->peers[pg_iter].connect_peers_working_set
4344 = GNUNET_CONTAINER_multihashmap_create (pg->total);
4345 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4346 &random_connect_iterator,
4348 /* Now remove the old connections */
4349 GNUNET_CONTAINER_multihashmap_destroy (pg->
4350 peers[pg_iter].connect_peers);
4351 /* And replace with the random set */
4352 pg->peers[pg_iter].connect_peers
4353 = pg->peers[pg_iter].connect_peers_working_set;
4357 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4359 conn_iter = pg->peers[pg_iter].connect_peers_head;
4360 while (pg->peers[pg_iter].connect_peers_head != NULL)
4361 remove_connections (pg, pg_iter,
4362 pg->peers[pg_iter].connect_peers_head->index,
4363 CONNECT, GNUNET_YES);
4365 pg->peers[pg_iter].connect_peers_head
4366 = pg->peers[pg_iter].connect_peers_working_set_head;
4367 pg->peers[pg_iter].connect_peers_tail
4368 = pg->peers[pg_iter].connect_peers_working_set_tail;
4369 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4370 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4375 * Count the number of connections in a linked list of connections.
4377 * @param conn_list the connection list to get the count of
4379 * @return the number of elements in the list
4382 count_connections(struct PeerConnection *conn_list)
4384 struct PeerConnection *iter;
4388 while (iter != NULL)
4397 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
4400 unsigned int pg_iter;
4402 struct PeerConnection *conn_iter;
4406 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4409 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
4410 while (conn_iter != NULL)
4413 conn_iter = conn_iter->next;
4417 GNUNET_CONTAINER_multihashmap_size (pg->
4419 [pg_iter].connect_peers_working_set);
4427 count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
4430 unsigned int pg_iter;
4432 struct PeerConnection *conn_iter;
4436 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4439 conn_iter = pg->peers[pg_iter].allowed_peers_head;
4440 while (conn_iter != NULL)
4443 conn_iter = conn_iter->next;
4447 GNUNET_CONTAINER_multihashmap_size (pg->
4449 [pg_iter].allowed_peers);
4457 * From the set of connections possible, choose at least num connections per
4460 * @param pg the peergroup we are dealing with
4461 * @param num how many connections at least should each peer have (if possible)?
4464 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4467 struct MinimumContext minimum_ctx;
4469 struct PeerConnection *conn_iter;
4470 unsigned int temp_list_size;
4473 uint32_t random; /* Random list entry to connect peer to */
4478 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4481 = count_connections (pg->peers[pg_iter].connect_peers_head);
4482 if (temp_list_size == 0)
4484 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4485 "Peer %d has 0 connections!?!?\n", pg_iter);
4488 for (i = 0; i < num; i++)
4490 random = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4492 conn_iter = pg->peers[pg_iter].connect_peers_head;
4493 for (count = 0; count < random; count++)
4494 conn_iter = conn_iter->next;
4495 /* We now have a random connection, connect it! */
4496 GNUNET_assert(conn_iter != NULL);
4497 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4502 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4504 pg->peers[pg_iter].connect_peers_working_set =
4505 GNUNET_CONTAINER_multihashmap_create (num);
4508 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4510 minimum_ctx.first_uid = pg_iter;
4511 minimum_ctx.pg_array =
4512 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
4513 GNUNET_CONTAINER_multihashmap_size
4514 (pg->peers[pg_iter].connect_peers));
4515 minimum_ctx.first = &pg->peers[pg_iter];
4516 minimum_ctx.pg = pg;
4517 minimum_ctx.num_to_add = num;
4518 minimum_ctx.current = 0;
4519 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4520 &minimum_connect_iterator,
4524 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4526 /* Remove the "old" connections */
4527 GNUNET_CONTAINER_multihashmap_destroy (pg->
4528 peers[pg_iter].connect_peers);
4529 /* And replace with the working set */
4530 pg->peers[pg_iter].connect_peers =
4531 pg->peers[pg_iter].connect_peers_working_set;
4534 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4536 while (pg->peers[pg_iter].connect_peers_head != NULL)
4538 conn_iter = pg->peers[pg_iter].connect_peers_head;
4539 GNUNET_CONTAINER_DLL_remove(pg->peers[pg_iter].connect_peers_head,
4540 pg->peers[pg_iter].connect_peers_tail,
4542 GNUNET_free(conn_iter);
4543 /*remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT, GNUNET_YES);*/
4546 pg->peers[pg_iter].connect_peers_head
4547 = pg->peers[pg_iter].connect_peers_working_set_head;
4548 pg->peers[pg_iter].connect_peers_tail
4549 = pg->peers[pg_iter].connect_peers_working_set_tail;
4550 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4551 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4556 struct FindClosestContext
4559 * The currently known closest peer.
4561 struct GNUNET_TESTING_Daemon *closest;
4564 * The info for the peer we are adding connections for.
4566 struct PeerData *curr_peer;
4569 * The distance (bits) between the current
4570 * peer and the currently known closest.
4572 unsigned int closest_dist;
4575 * The offset of the closest known peer in
4578 unsigned int closest_num;
4582 * Iterator over hash map entries of the allowed
4583 * peer connections. Find the closest, not already
4584 * connected peer and return it.
4586 * @param cls closure (struct FindClosestContext)
4587 * @param key current key code (hash of offset in pg)
4588 * @param value value in the hash map - a GNUNET_TESTING_Daemon
4589 * @return GNUNET_YES if we should continue to
4594 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
4596 struct FindClosestContext *closest_ctx = cls;
4597 struct GNUNET_TESTING_Daemon *daemon = value;
4599 if (((closest_ctx->closest == NULL) ||
4600 (GNUNET_CRYPTO_hash_matching_bits
4601 (&daemon->id.hashPubKey,
4602 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
4603 closest_ctx->closest_dist))
4605 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
4606 curr_peer->connect_peers,
4609 closest_ctx->closest_dist =
4610 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
4611 &closest_ctx->curr_peer->daemon->
4613 closest_ctx->closest = daemon;
4614 uid_from_hash (key, &closest_ctx->closest_num);
4620 * From the set of connections possible, choose at num connections per
4621 * peer based on depth which are closest out of those allowed. Guaranteed
4622 * to add num peers to connect to, provided there are that many peers
4623 * in the underlay topology to connect to.
4625 * @param pg the peergroup we are dealing with
4626 * @param num how many connections at least should each peer have (if possible)?
4627 * @param proc processor to actually add the connections
4628 * @param list the peer list to use
4631 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
4632 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
4637 struct FindClosestContext closest_ctx;
4642 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
4644 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4646 closest_ctx.curr_peer = &pg->peers[pg_iter];
4647 closest_ctx.closest = NULL;
4648 closest_ctx.closest_dist = 0;
4649 closest_ctx.closest_num = 0;
4650 GNUNET_CONTAINER_multihashmap_iterate (pg->
4651 peers[pg_iter].allowed_peers,
4652 &find_closest_peers,
4654 if (closest_ctx.closest != NULL)
4656 GNUNET_assert (closest_ctx.closest_num < pg->total);
4657 proc (pg, pg_iter, closest_ctx.closest_num, list);
4665 * From the set of connections possible, choose at least num connections per
4666 * peer based on depth first traversal of peer connections. If DFS leaves
4667 * peers unconnected, ensure those peers get connections.
4669 * @param pg the peergroup we are dealing with
4670 * @param num how many connections at least should each peer have (if possible)?
4673 perform_dfs(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4677 uint32_t starting_peer;
4678 uint32_t least_connections;
4679 uint32_t random_connection;
4681 unsigned int temp_count;
4682 struct PeerConnection *peer_iter;
4684 struct DFSContext dfs_ctx;
4685 GNUNET_HashCode second_hash;
4691 while ((count_workingset_connections (pg) < num * pg->total)
4692 && (count_allowed_connections (pg) > 0))
4694 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4696 least_connections = -1; /* Set to very high number */
4697 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4700 = count_connections (
4701 pg->peers[pg_iter].connect_peers_working_set_head);
4702 if (temp_count < least_connections)
4704 starting_peer = pg_iter;
4705 least_connections = temp_count;
4711 = count_connections (pg->peers[starting_peer].connect_peers_head);
4712 if (temp_count == 0)
4713 continue; /* FIXME: infinite loop? */
4715 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4718 peer_iter = pg->peers[starting_peer].connect_peers_head;
4719 while (temp_count < random_connection)
4721 peer_iter = peer_iter->next;
4724 GNUNET_assert(peer_iter != NULL);
4725 add_connections (pg, starting_peer, peer_iter->index, WORKING_SET,
4727 remove_connections (pg, starting_peer, peer_iter->index, CONNECT,
4729 starting_peer = peer_iter->index;
4734 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4736 pg->peers[pg_iter].connect_peers_working_set =
4737 GNUNET_CONTAINER_multihashmap_create (num);
4742 while ((count_workingset_connections (pg) < num * pg->total)
4743 && (count_allowed_connections (pg) > 0))
4745 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4747 least_connections = -1; /* Set to very high number */
4748 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4750 if (GNUNET_CONTAINER_multihashmap_size
4751 (pg->peers[pg_iter].connect_peers_working_set) <
4754 starting_peer = pg_iter;
4756 GNUNET_CONTAINER_multihashmap_size (pg->
4758 [pg_iter].connect_peers_working_set);
4763 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4769 /* Choose a random peer from the chosen peers set of connections to add */
4771 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4772 GNUNET_CONTAINER_multihashmap_size
4773 (pg->peers[starting_peer].connect_peers));
4774 dfs_ctx.first_uid = starting_peer;
4775 dfs_ctx.first = &pg->peers[starting_peer];
4777 dfs_ctx.current = 0;
4779 GNUNET_CONTAINER_multihashmap_iterate (pg->
4781 [starting_peer].connect_peers,
4782 &dfs_connect_iterator, &dfs_ctx);
4783 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4784 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4785 GNUNET_assert (GNUNET_YES ==
4786 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4787 [starting_peer].connect_peers,
4791 [dfs_ctx.second_uid].daemon));
4792 starting_peer = dfs_ctx.second_uid;
4795 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4797 /* Remove the "old" connections */
4798 GNUNET_CONTAINER_multihashmap_destroy (pg->
4799 peers[pg_iter].connect_peers);
4800 /* And replace with the working set */
4801 pg->peers[pg_iter].connect_peers =
4802 pg->peers[pg_iter].connect_peers_working_set;
4808 * Internal callback for topology information for a particular peer.
4811 internal_topology_callback(void *cls, const struct GNUNET_PeerIdentity *peer,
4812 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4814 struct CoreContext *core_ctx = cls;
4815 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4817 if (peer == NULL) /* Either finished, or something went wrong */
4819 iter_ctx->completed++;
4820 iter_ctx->connected--;
4821 /* One core context allocated per iteration, must free! */
4822 GNUNET_free (core_ctx);
4826 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id, peer, NULL);
4829 if (iter_ctx->completed == iter_ctx->total)
4831 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4832 /* Once all are done, free the iteration context */
4833 GNUNET_free (iter_ctx);
4838 * Check running topology iteration tasks, if below max start a new one, otherwise
4839 * schedule for some time in the future.
4842 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4844 struct CoreContext *core_context = cls;
4845 struct TopologyIterateContext *topology_context =
4846 (struct TopologyIterateContext *) core_context->iter_context;
4847 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
4850 if (topology_context->connected
4851 > topology_context->pg->max_outstanding_connections)
4853 #if VERBOSE_TESTING > 2
4854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4856 ("Delaying connect, we have too many outstanding connections!\n"));
4858 GNUNET_SCHEDULER_add_delayed (
4859 GNUNET_TIME_relative_multiply (
4860 GNUNET_TIME_UNIT_MILLISECONDS,
4862 &schedule_get_topology, core_context);
4866 #if VERBOSE_TESTING > 2
4867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4868 _("Creating connection, outstanding_connections is %d\n"),
4869 outstanding_connects);
4871 topology_context->connected++;
4873 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4874 &internal_topology_callback,
4877 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4878 internal_topology_callback (core_context, NULL, NULL);
4884 * Iterate over all (running) peers in the peer group, retrieve
4885 * all connections that each currently has.
4888 GNUNET_TESTING_get_topology(struct GNUNET_TESTING_PeerGroup *pg,
4889 GNUNET_TESTING_NotifyTopology cb, void *cls)
4891 struct TopologyIterateContext *topology_context;
4892 struct CoreContext *core_ctx;
4894 unsigned int total_count;
4896 /* Allocate a single topology iteration context */
4897 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4898 topology_context->topology_cb = cb;
4899 topology_context->cls = cls;
4900 topology_context->pg = pg;
4902 for (i = 0; i < pg->total; i++)
4904 if (pg->peers[i].daemon->running == GNUNET_YES)
4906 /* Allocate one core context per core we need to connect to */
4907 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4908 core_ctx->daemon = pg->peers[i].daemon;
4909 /* Set back pointer to topology iteration context */
4910 core_ctx->iter_context = topology_context;
4911 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4915 if (total_count == 0)
4917 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4918 GNUNET_free (topology_context);
4921 topology_context->total = total_count;
4926 * Callback function to process statistic values.
4927 * This handler is here only really to insert a peer
4928 * identity (or daemon) so the statistics can be uniquely
4929 * tied to a single running peer.
4931 * @param cls closure
4932 * @param subsystem name of subsystem that created the statistic
4933 * @param name the name of the datum
4934 * @param value the current value
4935 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4936 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4939 internal_stats_callback(void *cls, const char *subsystem, const char *name,
4940 uint64_t value, int is_persistent)
4942 struct StatsCoreContext *core_context = cls;
4943 struct StatsIterateContext *stats_context =
4944 (struct StatsIterateContext *) core_context->iter_context;
4946 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4947 subsystem, name, value, is_persistent);
4951 * Internal continuation call for statistics iteration.
4953 * @param cls closure, the CoreContext for this iteration
4954 * @param success whether or not the statistics iterations
4955 * was canceled or not (we don't care)
4958 internal_stats_cont(void *cls, int success)
4960 struct StatsCoreContext *core_context = cls;
4961 struct StatsIterateContext *stats_context =
4962 (struct StatsIterateContext *) core_context->iter_context;
4964 stats_context->connected--;
4965 stats_context->completed++;
4967 if (stats_context->completed == stats_context->total)
4969 stats_context->cont (stats_context->cls, GNUNET_YES);
4970 GNUNET_free (stats_context);
4973 if (core_context->stats_handle != NULL)
4974 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4976 GNUNET_free (core_context);
4980 * Check running topology iteration tasks, if below max start a new one, otherwise
4981 * schedule for some time in the future.
4984 schedule_get_statistics(void *cls,
4985 const struct GNUNET_SCHEDULER_TaskContext *tc)
4987 struct StatsCoreContext *core_context = cls;
4988 struct StatsIterateContext *stats_context =
4989 (struct StatsIterateContext *) core_context->iter_context;
4991 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
4994 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
4996 #if VERBOSE_TESTING > 2
4997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4999 ("Delaying connect, we have too many outstanding connections!\n"));
5001 GNUNET_SCHEDULER_add_delayed (
5002 GNUNET_TIME_relative_multiply (
5003 GNUNET_TIME_UNIT_MILLISECONDS,
5005 &schedule_get_statistics, core_context);
5009 #if VERBOSE_TESTING > 2
5010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5011 _("Creating connection, outstanding_connections is %d\n"),
5012 outstanding_connects);
5015 stats_context->connected++;
5016 core_context->stats_handle
5017 = GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
5018 if (core_context->stats_handle == NULL)
5020 internal_stats_cont (core_context, GNUNET_NO);
5024 core_context->stats_get_handle
5025 = GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
5026 GNUNET_TIME_relative_get_forever (),
5027 &internal_stats_cont,
5028 &internal_stats_callback, core_context);
5029 if (core_context->stats_get_handle == NULL)
5030 internal_stats_cont (core_context, GNUNET_NO);
5035 struct DuplicateStats
5038 * Next item in the list
5040 struct DuplicateStats *next;
5043 * Nasty string, concatenation of relevant information.
5045 char *unique_string;
5049 * Check whether the combination of port/host/unix domain socket
5050 * already exists in the list of peers being checked for statistics.
5052 * @param pg the peergroup in question
5053 * @param specific_peer the peer we're concerned with
5054 * @param stats_list the list to return to the caller
5056 * @return GNUNET_YES if the statistics instance has been seen already,
5057 * GNUNET_NO if not (and we may have added it to the list)
5060 stats_check_existing(struct GNUNET_TESTING_PeerGroup *pg,
5061 struct PeerData *specific_peer,
5062 struct DuplicateStats **stats_list)
5064 struct DuplicateStats *pos;
5065 char *unix_domain_socket;
5066 unsigned long long port;
5069 != GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
5070 "single_statistics_per_host"))
5071 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
5074 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg,
5077 &unix_domain_socket))
5080 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg,
5081 "statistics", "port",
5084 GNUNET_free(unix_domain_socket);
5088 if (specific_peer->daemon->hostname != NULL)
5089 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
5090 unix_domain_socket, port);
5092 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
5096 if (0 == strcmp (to_match, pos->unique_string))
5098 GNUNET_free (unix_domain_socket);
5099 GNUNET_free (to_match);
5104 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
5105 pos->unique_string = to_match;
5106 pos->next = *stats_list;
5108 GNUNET_free (unix_domain_socket);
5113 * Iterate over all (running) peers in the peer group, retrieve
5114 * all statistics from each.
5116 * @param pg the peergroup to iterate statistics of
5117 * @param cont continuation to call once all stats have been retrieved
5118 * @param proc processing function for each statistic from each peer
5119 * @param cls closure to pass to proc
5123 GNUNET_TESTING_get_statistics(struct GNUNET_TESTING_PeerGroup *pg,
5124 GNUNET_STATISTICS_Callback cont,
5125 GNUNET_TESTING_STATISTICS_Iterator proc,
5128 struct StatsIterateContext *stats_context;
5129 struct StatsCoreContext *core_ctx;
5131 unsigned int total_count;
5132 struct DuplicateStats *stats_list;
5133 struct DuplicateStats *pos;
5136 /* Allocate a single stats iteration context */
5137 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
5138 stats_context->cont = cont;
5139 stats_context->proc = proc;
5140 stats_context->cls = cls;
5141 stats_context->pg = pg;
5144 for (i = 0; i < pg->total; i++)
5146 if ((pg->peers[i].daemon->running == GNUNET_YES) && (GNUNET_NO
5147 == stats_check_existing (pg, &pg->peers[i], &stats_list)))
5149 /* Allocate one core context per core we need to connect to */
5150 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
5151 core_ctx->daemon = pg->peers[i].daemon;
5152 /* Set back pointer to topology iteration context */
5153 core_ctx->iter_context = stats_context;
5154 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
5159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5160 "Retrieving stats from %u total instances.\n", total_count);
5161 stats_context->total = total_count;
5162 if (stats_list != NULL)
5167 GNUNET_free (pos->unique_string);
5168 stats_list = pos->next;
5170 pos = stats_list->next;
5177 * Stop the connection process temporarily.
5179 * @param pg the peer group to stop connecting
5182 GNUNET_TESTING_stop_connections(struct GNUNET_TESTING_PeerGroup *pg)
5184 pg->stop_connects = GNUNET_YES;
5188 * Resume the connection process temporarily.
5190 * @param pg the peer group to resume connecting
5193 GNUNET_TESTING_resume_connections(struct GNUNET_TESTING_PeerGroup *pg)
5195 pg->stop_connects = GNUNET_NO;
5199 * There are many ways to connect peers that are supported by this function.
5200 * To connect peers in the same topology that was created via the
5201 * GNUNET_TESTING_create_topology, the topology variable must be set to
5202 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
5203 * a new instance of that topology will be generated and attempted to be
5204 * connected. This could result in some connections being impossible,
5205 * because some topologies are non-deterministic.
5207 * @param pg the peer group struct representing the running peers
5208 * @param topology which topology to connect the peers in
5209 * @param options options for connecting the topology
5210 * @param option_modifier modifier for options that take a parameter
5211 * @param connect_timeout how long to wait before giving up on connecting
5213 * @param connect_attempts how many times to attempt to connect two peers
5214 * over the connect_timeout duration
5215 * @param notify_callback notification to be called once all connections completed
5216 * @param notify_cls closure for notification callback
5218 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
5221 GNUNET_TESTING_connect_topology(
5222 struct GNUNET_TESTING_PeerGroup *pg,
5223 enum GNUNET_TESTING_Topology topology,
5224 enum GNUNET_TESTING_TopologyOption options,
5225 double option_modifier,
5226 struct GNUNET_TIME_Relative connect_timeout,
5227 unsigned int connect_attempts,
5228 GNUNET_TESTING_NotifyCompletion notify_callback,
5233 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
5234 #if VERBOSE_TOPOLOGY
5235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5236 _("Creating clique CONNECT topology\n"));
5238 create_clique (pg, &add_connections, CONNECT, GNUNET_NO);
5240 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
5241 #if VERBOSE_TOPOLOGY
5242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5243 _("Creating small world (ring) CONNECT topology\n"));
5245 create_small_world_ring (pg, &add_connections, CONNECT);
5247 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
5248 #if VERBOSE_TOPOLOGY
5249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5250 _("Creating small world (2d-torus) CONNECT topology\n"));
5252 create_small_world (pg, &add_connections, CONNECT);
5254 case GNUNET_TESTING_TOPOLOGY_RING:
5255 #if VERBOSE_TOPOLOGY
5256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring CONNECT topology\n"));
5258 create_ring (pg, &add_connections, CONNECT);
5260 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
5261 #if VERBOSE_TOPOLOGY
5262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5263 _("Creating 2d torus CONNECT topology\n"));
5265 create_2d_torus (pg, &add_connections, CONNECT);
5267 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
5268 #if VERBOSE_TOPOLOGY
5269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5270 _("Creating Erdos-Renyi CONNECT topology\n"));
5272 create_erdos_renyi (pg, &add_connections, CONNECT);
5274 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
5275 #if VERBOSE_TOPOLOGY
5276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5277 _("Creating InterNAT CONNECT topology\n"));
5279 create_nated_internet (pg, &add_connections, CONNECT);
5281 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
5282 #if VERBOSE_TOPOLOGY
5283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5284 _("Creating Scale Free CONNECT topology\n"));
5286 create_scale_free (pg, &add_connections, CONNECT);
5288 case GNUNET_TESTING_TOPOLOGY_LINE:
5289 #if VERBOSE_TOPOLOGY
5290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5291 _("Creating straight line CONNECT topology\n"));
5293 create_line (pg, &add_connections, CONNECT);
5295 case GNUNET_TESTING_TOPOLOGY_NONE:
5296 #if VERBOSE_TOPOLOGY
5297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating no CONNECT topology\n"));
5299 copy_allowed_topology (pg);
5302 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5303 ("Unknown topology specification, can't connect peers!\n"));
5304 return GNUNET_SYSERR;
5309 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
5310 #if VERBOSE_TOPOLOGY
5311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5312 ("Connecting random subset (%'.2f percent) of possible peers\n"), 100
5315 choose_random_connections (pg, option_modifier);
5317 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
5318 #if VERBOSE_TOPOLOGY
5319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5320 _("Connecting a minimum of %u peers each (if possible)\n"),
5321 (unsigned int) option_modifier);
5323 choose_minimum (pg, (unsigned int) option_modifier);
5325 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
5326 #if VERBOSE_TOPOLOGY
5327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5328 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
5329 (unsigned int) option_modifier);
5332 perform_dfs (pg, (int) option_modifier);
5335 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
5336 #if VERBOSE_TOPOLOGY
5337 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5338 ("Finding additional %u closest peers each (if possible)\n"),
5339 (unsigned int) option_modifier);
5342 add_closest (pg, (unsigned int) option_modifier,
5343 &add_connections, CONNECT);
5346 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
5348 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
5354 return connect_topology (pg, connect_timeout, connect_attempts,
5355 notify_callback, notify_cls);
5359 * Lookup and return the number of SSH connections to a host.
5361 * @param hostname the hostname to lookup in the list
5362 * @param pg the peergroup that the host belongs to
5364 * @return the number of current ssh connections to the host
5367 count_outstanding_at_host(const char *hostname,
5368 struct GNUNET_TESTING_PeerGroup *pg)
5370 struct OutstandingSSH *pos;
5372 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5374 GNUNET_assert(pos != NULL);
5375 return pos->outstanding;
5379 * Increment the number of SSH connections to a host by one.
5381 * @param hostname the hostname to lookup in the list
5382 * @param pg the peergroup that the host belongs to
5386 increment_outstanding_at_host(const char *hostname,
5387 struct GNUNET_TESTING_PeerGroup *pg)
5389 struct OutstandingSSH *pos;
5391 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5393 GNUNET_assert(pos != NULL);
5398 * Decrement the number of SSH connections to a host by one.
5400 * @param hostname the hostname to lookup in the list
5401 * @param pg the peergroup that the host belongs to
5405 decrement_outstanding_at_host(const char *hostname,
5406 struct GNUNET_TESTING_PeerGroup *pg)
5408 struct OutstandingSSH *pos;
5410 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5412 GNUNET_assert(pos != NULL);
5417 * Callback that is called whenever a hostkey is generated
5418 * for a peer. Call the real callback and decrement the
5419 * starting counter for the peergroup.
5421 * @param cls closure
5422 * @param id identifier for the daemon, NULL on error
5423 * @param d handle for the daemon
5424 * @param emsg error message (NULL on success)
5427 internal_hostkey_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5428 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5430 struct InternalStartContext *internal_context = cls;
5431 internal_context->peer->pg->starting--;
5432 internal_context->peer->pg->started++;
5433 if (internal_context->hostname != NULL)
5434 decrement_outstanding_at_host (internal_context->hostname,
5435 internal_context->peer->pg);
5436 if (internal_context->hostkey_callback != NULL)
5437 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
5439 else if (internal_context->peer->pg->started
5440 == internal_context->peer->pg->total)
5442 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
5443 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
5448 * Callback that is called whenever a peer has finished starting.
5449 * Call the real callback and decrement the starting counter
5450 * for the peergroup.
5452 * @param cls closure
5453 * @param id identifier for the daemon, NULL on error
5455 * @param d handle for the daemon
5456 * @param emsg error message (NULL on success)
5459 internal_startup_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5460 const struct GNUNET_CONFIGURATION_Handle *cfg,
5461 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5463 struct InternalStartContext *internal_context = cls;
5464 internal_context->peer->pg->starting--;
5465 if (internal_context->hostname != NULL)
5466 decrement_outstanding_at_host (internal_context->hostname,
5467 internal_context->peer->pg);
5468 if (internal_context->start_cb != NULL)
5469 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
5474 internal_continue_startup(void *cls,
5475 const struct GNUNET_SCHEDULER_TaskContext *tc)
5477 struct InternalStartContext *internal_context = cls;
5479 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5484 if ((internal_context->peer->pg->starting
5485 < internal_context->peer->pg->max_concurrent_ssh)
5486 || ((internal_context->hostname != NULL)
5487 && (count_outstanding_at_host (internal_context->hostname,
5488 internal_context->peer->pg)
5489 < internal_context->peer->pg->max_concurrent_ssh)))
5491 if (internal_context->hostname != NULL)
5492 increment_outstanding_at_host (internal_context->hostname,
5493 internal_context->peer->pg);
5494 internal_context->peer->pg->starting++;
5495 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
5499 GNUNET_SCHEDULER_add_delayed (
5500 GNUNET_TIME_relative_multiply (
5501 GNUNET_TIME_UNIT_MILLISECONDS,
5503 &internal_continue_startup,
5509 * Callback for informing us about a successful
5510 * or unsuccessful churn start call.
5512 * @param cls a ChurnContext
5513 * @param id the peer identity of the started peer
5514 * @param cfg the handle to the configuration of the peer
5515 * @param d handle to the daemon for the peer
5516 * @param emsg NULL on success, non-NULL on failure
5520 churn_start_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5521 const struct GNUNET_CONFIGURATION_Handle *cfg,
5522 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5524 struct ChurnRestartContext *startup_ctx = cls;
5525 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
5527 unsigned int total_left;
5528 char *error_message;
5530 error_message = NULL;
5533 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5534 "Churn stop callback failed with error `%s'\n", emsg);
5535 churn_ctx->num_failed_start++;
5539 churn_ctx->num_to_start--;
5543 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5544 "Started peer, %d left.\n", churn_ctx->num_to_start);
5547 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
5548 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5550 if (total_left == 0)
5552 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
5555 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5556 churn_ctx->num_failed_start,
5557 churn_ctx->num_failed_stop);
5558 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5559 GNUNET_free_non_null (error_message);
5560 GNUNET_free (churn_ctx);
5561 GNUNET_free (startup_ctx);
5566 schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5568 struct PeerRestartContext *peer_restart_ctx = cls;
5569 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
5571 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5572 GNUNET_SCHEDULER_add_delayed (
5573 GNUNET_TIME_relative_multiply (
5574 GNUNET_TIME_UNIT_MILLISECONDS,
5576 &schedule_churn_restart, peer_restart_ctx);
5579 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
5580 startup_ctx->timeout,
5581 &churn_start_callback, startup_ctx);
5582 GNUNET_free (peer_restart_ctx);
5588 internal_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5590 struct InternalStartContext *internal_context = cls;
5592 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5597 if ((internal_context->peer->pg->starting
5598 < internal_context->peer->pg->max_concurrent_ssh)
5599 || ((internal_context->hostname != NULL)
5600 && (count_outstanding_at_host (internal_context->hostname,
5601 internal_context->peer->pg)
5602 < internal_context->peer->pg->max_concurrent_ssh)))
5604 if (internal_context->hostname != NULL)
5605 increment_outstanding_at_host (internal_context->hostname,
5606 internal_context->peer->pg);
5607 internal_context->peer->pg->starting++;
5608 internal_context->peer->daemon
5609 = GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
5610 internal_context->timeout,
5612 internal_context->hostname,
5613 internal_context->username,
5614 internal_context->sshport,
5615 internal_context->hostkey,
5616 &internal_hostkey_callback,
5618 &internal_startup_callback,
5623 GNUNET_SCHEDULER_add_delayed (
5624 GNUNET_TIME_relative_multiply (
5625 GNUNET_TIME_UNIT_MILLISECONDS,
5627 &internal_start, internal_context);
5630 #if USE_START_HELPER
5632 struct PeerStartHelperContext
5634 struct GNUNET_TESTING_PeerGroup *pg;
5636 struct HostData *host;
5638 struct GNUNET_OS_Process *proc;
5642 check_peers_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5644 struct PeerStartHelperContext *helper = cls;
5645 enum GNUNET_OS_ProcessStatusType type;
5648 GNUNET_TESTING_NotifyDaemonRunning cb;
5650 if (GNUNET_NO == GNUNET_OS_process_status (helper->proc, &type, &code)) /* Still running, wait some more! */
5652 GNUNET_SCHEDULER_add_delayed(GNUNET_CONSTANTS_EXEC_WAIT, &check_peers_started, helper);
5656 helper->pg->starting--;
5657 if (helper->pg->starting == 0) /* All peers have finished starting! */
5659 /* Call the peer started callback for each peer, set proper FSM state (?) */
5660 for (i = 0; i < helper->pg->total; i++)
5662 cb = helper->pg->peers[i].daemon->cb;
5663 helper->pg->peers[i].daemon->cb = NULL;
5664 helper->pg->peers[i].daemon->running = GNUNET_YES;
5665 helper->pg->peers[i].daemon->phase = SP_START_DONE;
5668 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
5669 cb (helper->pg->peers[i].daemon->cb_cls,
5670 &helper->pg->peers[i].daemon->id,
5671 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5672 "Failed to execute peerStartHelper.pl, or return code bad!");
5674 cb (helper->pg->peers[i].daemon->cb_cls,
5675 &helper->pg->peers[i].daemon->id,
5676 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5683 GNUNET_OS_process_close(helper->proc);
5687 start_peer_helper (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5689 struct PeerStartHelperContext *helper = cls;
5690 char *baseservicehome;
5693 /* ssh user@host peerStartHelper /path/to/basedirectory */
5694 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (helper->pg->cfg, "PATHS", "SERVICEHOME",
5696 GNUNET_asprintf(&tempdir, "%s/%s/", baseservicehome, helper->host->hostname);
5697 if (NULL != helper->host->username)
5698 GNUNET_asprintf (&arg, "%s@%s", helper->host->username, helper->host->hostname);
5700 GNUNET_asprintf (&arg, "%s", helper->host->hostname);
5702 /* FIXME: Doesn't support ssh_port option! */
5703 helper->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5704 "peerStartHelper.pl", tempdir, NULL);
5705 GNUNET_assert(helper->proc != NULL);
5706 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "starting peers with cmd ssh %s %s %s\n", arg, "peerStartHelper.pl", tempdir);
5707 GNUNET_SCHEDULER_add_now (&check_peers_started, helper);
5708 GNUNET_free (tempdir);
5709 GNUNET_free (baseservicehome);
5715 * Function which continues a peer group starting up
5716 * after successfully generating hostkeys for each peer.
5718 * @param pg the peer group to continue starting
5722 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
5726 #if USE_START_HELPER
5727 if ((pg->num_hosts > 0) && (pg->hostkey_data != NULL))
5729 struct PeerStartHelperContext *helper;
5730 pg->starting = pg->num_hosts;
5731 for (i = 0; i < pg->num_hosts; i++)
5733 helper = GNUNET_malloc(sizeof(struct PeerStartHelperContext));
5735 helper->host = &pg->hosts[i];
5736 GNUNET_SCHEDULER_add_now(&start_peer_helper, helper);
5742 for (i = 0; i < pg->total; i++)
5744 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5745 &pg->peers[i].internal_context);
5750 for (i = 0; i < pg->total; i++)
5752 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5753 &pg->peers[i].internal_context);
5758 #if USE_START_HELPER
5760 call_hostkey_callbacks (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5762 struct GNUNET_TESTING_PeerGroup *pg = cls;
5764 for (i = 0; i < pg->total; i++)
5766 if (pg->peers[i].internal_context.hostkey_callback != NULL)
5767 pg->peers[i].internal_context.hostkey_callback (pg->peers[i].internal_context.hostkey_cls,
5768 &pg->peers[i].daemon->id,
5769 pg->peers[i].daemon,
5773 if (pg->peers[0].internal_context.hostkey_callback == NULL)
5774 GNUNET_TESTING_daemons_continue_startup (pg);
5779 * Start count gnunet instances with the same set of transports and
5780 * applications. The port numbers (any option called "PORT") will be
5781 * adjusted to ensure that no two peers running on the same system
5782 * have the same port(s) in their respective configurations.
5784 * @param cfg configuration template to use
5785 * @param total number of daemons to start
5786 * @param max_concurrent_connections for testing, how many peers can
5787 * we connect to simultaneously
5788 * @param max_concurrent_ssh when starting with ssh, how many ssh
5789 * connections will we allow at once (based on remote hosts allowed!)
5790 * @param timeout total time allowed for peers to start
5791 * @param hostkey_callback function to call on each peers hostkey generation
5792 * if NULL, peers will be started by this call, if non-null,
5793 * GNUNET_TESTING_daemons_continue_startup must be called after
5794 * successful hostkey generation
5795 * @param hostkey_cls closure for hostkey callback
5796 * @param cb function to call on each daemon that was started
5797 * @param cb_cls closure for cb
5798 * @param connect_callback function to call each time two hosts are connected
5799 * @param connect_callback_cls closure for connect_callback
5800 * @param hostnames linked list of host structs to use to start peers on
5801 * (NULL to run on localhost only)
5803 * @return NULL on error, otherwise handle to control peer group
5805 struct GNUNET_TESTING_PeerGroup *
5806 GNUNET_TESTING_daemons_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
5808 unsigned int max_concurrent_connections,
5809 unsigned int max_concurrent_ssh,
5810 struct GNUNET_TIME_Relative timeout,
5811 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
5813 GNUNET_TESTING_NotifyDaemonRunning cb,
5815 GNUNET_TESTING_NotifyConnection connect_callback,
5816 void *connect_callback_cls,
5817 const struct GNUNET_TESTING_Host *hostnames)
5819 struct GNUNET_TESTING_PeerGroup *pg;
5820 const struct GNUNET_TESTING_Host *hostpos;
5821 const char *hostname;
5822 const char *username;
5823 char *baseservicehome;
5824 char *newservicehome;
5826 char *hostkeys_file;
5829 struct GNUNET_DISK_FileHandle *fd;
5830 struct GNUNET_CONFIGURATION_Handle *pcfg;
5832 struct OutstandingSSH *ssh_entry;
5833 unsigned int hostcnt;
5840 uint64_t total_hostkeys;
5841 struct GNUNET_OS_Process *proc;
5852 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
5854 pg->notify_connection = connect_callback;
5855 pg->notify_connection_cls = connect_callback_cls;
5857 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
5858 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
5859 pg->max_outstanding_connections = max_concurrent_connections;
5860 pg->max_concurrent_ssh = max_concurrent_ssh;
5861 if (NULL != hostnames)
5864 hostpos = hostnames;
5865 while (hostpos != NULL)
5867 hostpos = hostpos->next;
5870 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5873 hostpos = hostnames;
5874 while (hostpos != NULL)
5876 pg->hosts[off].minport = LOW_PORT;
5877 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
5878 if (hostpos->username != NULL)
5879 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
5880 pg->hosts[off].sshport = hostpos->port;
5881 hostpos = hostpos->next;
5891 pg->num_hosts = off;
5899 /* Create the servicehome directory for each remote peer */
5900 GNUNET_assert(GNUNET_OK ==
5901 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
5903 for (i = 0; i < pg->num_hosts; i++)
5905 ssh_entry = GNUNET_malloc(sizeof(struct OutstandingSSH));
5906 ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
5907 GNUNET_CONTAINER_DLL_insert(pg->ssh_head, pg->ssh_tail, ssh_entry);
5908 GNUNET_asprintf(&tmpdir, "%s/%s", baseservicehome, pg->hosts[i].hostname);
5909 if (NULL != pg->hosts[i].username)
5910 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username,
5911 pg->hosts[i].hostname);
5913 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
5914 if (pg->hosts[i].sshport != 0)
5916 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
5917 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-P",
5922 arg, "mkdir -p", tmpdir,
5926 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5927 "mkdir -p", tmpdir, NULL);
5928 GNUNET_assert(proc != NULL);
5929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5930 "Creating remote dir with command ssh %s %s %s\n", arg,
5931 " mkdir -p ", tmpdir);
5932 GNUNET_free(tmpdir);
5934 GNUNET_OS_process_wait (proc);
5935 GNUNET_OS_process_close(proc);
5937 GNUNET_free(baseservicehome);
5938 baseservicehome = NULL;
5940 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING",
5944 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
5945 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5946 _("Could not read hostkeys file!\n"));
5949 /* Check hostkey file size, read entire thing into memory */
5950 fd = GNUNET_DISK_file_open (hostkeys_file,
5951 GNUNET_DISK_OPEN_READ,
5952 GNUNET_DISK_PERM_NONE);
5955 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
5958 GNUNET_free (hostkeys_file);
5959 for (i=0;i<pg->num_hosts;i++)
5961 GNUNET_free (pg->hosts[i].hostname);
5962 GNUNET_free_non_null (pg->hosts[i].username);
5964 GNUNET_free (pg->peers);
5965 GNUNET_free (pg->hosts);
5970 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs,
5974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5975 "Found file size %llu for hostkeys, expect hostkeys to be size %d\n",
5976 fs, HOSTKEYFILESIZE);
5978 if (0 != (fs % HOSTKEYFILESIZE))
5980 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5981 "File size %llu seems incorrect for hostkeys...\n",
5986 total_hostkeys = fs / HOSTKEYFILESIZE;
5987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5988 "Will read %llu hostkeys from file\n",
5990 pg->hostkey_data = GNUNET_malloc_large (fs);
5991 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
5993 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
5995 GNUNET_free(hostkeys_file);
5998 for (off = 0; off < total; off++)
6002 hostname = pg->hosts[off % hostcnt].hostname;
6003 username = pg->hosts[off % hostcnt].username;
6004 sshport = pg->hosts[off % hostcnt].sshport;
6005 pcfg = make_config (cfg, off, &pg->hosts[off % hostcnt].minport,
6006 &upnum, hostname, &fdnum);
6013 pcfg = make_config (cfg, off, &minport, &upnum, hostname, &fdnum);
6018 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6019 _("Could not create configuration for peer number %u on `%s'!\n"),
6021 hostname == NULL ? "localhost" : hostname);
6026 == GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS",
6030 if (hostname != NULL)
6031 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", baseservicehome, hostname, off);
6033 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
6034 GNUNET_free (baseservicehome);
6035 baseservicehome = NULL;
6039 tmpdir = getenv ("TMPDIR");
6040 tmpdir = tmpdir ? tmpdir : "/tmp";
6041 if (hostname != NULL)
6042 GNUNET_asprintf (&newservicehome, "%s/%s/%s/%d/", tmpdir, hostname,
6043 "gnunet-testing-test-test", off);
6045 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", tmpdir,
6046 "gnunet-testing-test-test", off);
6048 GNUNET_CONFIGURATION_set_value_string (pcfg, "PATHS", "SERVICEHOME",
6050 GNUNET_free (newservicehome);
6051 pg->peers[off].cfg = pcfg;
6052 pg->peers[off].pg = pg;
6053 pg->peers[off].internal_context.peer = &pg->peers[off];
6054 pg->peers[off].internal_context.timeout = timeout;
6055 pg->peers[off].internal_context.hostname = hostname;
6056 pg->peers[off].internal_context.username = username;
6057 pg->peers[off].internal_context.sshport = sshport;
6058 if (pg->hostkey_data != NULL)
6059 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off
6061 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
6062 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
6063 pg->peers[off].internal_context.start_cb = cb;
6064 pg->peers[off].internal_context.start_cb_cls = cb_cls;
6065 #if !USE_START_HELPER
6066 GNUNET_SCHEDULER_add_now (&internal_start,
6067 &pg->peers[off].internal_context);
6069 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6071 pg->peers[off].daemon
6072 = GNUNET_TESTING_daemon_start (pcfg,
6078 pg->peers[off].internal_context.hostkey,
6079 &internal_hostkey_callback,
6080 &pg->peers[off].internal_context,
6081 &internal_startup_callback,
6082 &pg->peers[off].internal_context);
6084 * At this point, given that we had a hostkeyfile,
6085 * we can call the hostkey callback!
6086 * But first, we should copy (rsync) all of the configs
6087 * and hostkeys to the remote peers. Then let topology
6088 * creation happen, then call the peer start helper processes,
6089 * then set pg->whatever_phase for each peer and let them
6090 * enter the fsm to get the HELLO's for peers and start connecting.
6095 GNUNET_SCHEDULER_add_now (&internal_start,
6096 &pg->peers[off].internal_context);
6102 #if USE_START_HELPER /* Now the peergroup has been set up, hostkeys and configs written to files. */
6103 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6105 for (off = 0; off < hostcnt; off++)
6107 /* FIXME: grab baseservicehome! */
6108 GNUNET_asprintf (&newservicehome,
6111 pg->hosts[off].hostname);
6113 if (NULL != username)
6114 GNUNET_asprintf (&arg,
6117 pg->hosts[off].hostname,
6119 pg->hosts[off].hostname);
6121 GNUNET_asprintf (&arg,
6123 pg->hosts[off].hostname,
6125 pg->hosts[off].hostname);
6127 // FIXME: free--- GNUNET_free (baseservicehome);
6129 /* FIXME: Doesn't support ssh_port option! */
6130 proc = GNUNET_OS_start_process (NULL, NULL,
6132 "rsync", "-r", newservicehome, arg, NULL);
6134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6135 "copying directory with command rsync -r %s %s\n",
6136 newservicehome, arg);
6141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6142 _("Could not start `%s' process to copy configuration directory.\n"),
6146 GNUNET_OS_process_wait (proc);
6147 GNUNET_OS_process_close (proc);
6149 /* Now all the configuration files and hostkeys are copied to the remote host. Call the hostkey callback for each peer! */
6150 GNUNET_SCHEDULER_add_now (&call_hostkey_callbacks, pg);
6157 * Get a daemon by number, so callers don't have to do nasty
6158 * offsetting operation.
6160 struct GNUNET_TESTING_Daemon *
6161 GNUNET_TESTING_daemon_get(struct GNUNET_TESTING_PeerGroup *pg,
6162 unsigned int position)
6164 if (position < pg->total)
6165 return pg->peers[position].daemon;
6170 * Get a daemon by peer identity, so callers can
6171 * retrieve the daemon without knowing it's offset.
6173 * @param pg the peer group to retrieve the daemon from
6174 * @param peer_id the peer identity of the daemon to retrieve
6176 * @return the daemon on success, or NULL if no such peer identity is found
6178 struct GNUNET_TESTING_Daemon *
6179 GNUNET_TESTING_daemon_get_by_id(struct GNUNET_TESTING_PeerGroup *pg,
6180 struct GNUNET_PeerIdentity *peer_id)
6184 for (i = 0; i < pg->total; i++)
6186 if (0 == memcmp (&pg->peers[i].daemon->id, peer_id,
6187 sizeof(struct GNUNET_PeerIdentity)))
6188 return pg->peers[i].daemon;
6194 * Prototype of a function that will be called when a
6195 * particular operation was completed the testing library.
6197 * @param cls closure (a struct RestartContext)
6198 * @param id id of the peer that was restarted
6199 * @param cfg handle to the configuration of the peer
6200 * @param d handle to the daemon that was restarted
6201 * @param emsg NULL on success
6204 restart_callback(void *cls, const struct GNUNET_PeerIdentity *id,
6205 const struct GNUNET_CONFIGURATION_Handle *cfg,
6206 struct GNUNET_TESTING_Daemon *d, const char *emsg)
6208 struct RestartContext *restart_context = cls;
6212 restart_context->peers_restarted++;
6216 restart_context->peers_restart_failed++;
6219 if (restart_context->peers_restarted == restart_context->peer_group->total)
6221 restart_context->callback (restart_context->callback_cls, NULL);
6222 GNUNET_free (restart_context);
6224 else if (restart_context->peers_restart_failed
6225 + restart_context->peers_restarted == restart_context->peer_group->total)
6227 restart_context->callback (restart_context->callback_cls,
6228 "Failed to restart peers!");
6229 GNUNET_free (restart_context);
6235 * Callback for informing us about a successful
6236 * or unsuccessful churn stop call.
6238 * @param cls a ChurnContext
6239 * @param emsg NULL on success, non-NULL on failure
6243 churn_stop_callback(void *cls, const char *emsg)
6245 struct ShutdownContext *shutdown_ctx = cls;
6246 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
6247 unsigned int total_left;
6248 char *error_message;
6250 error_message = NULL;
6251 shutdown_ctx->outstanding--;
6255 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6256 "Churn stop callback failed with error `%s'\n", emsg);
6257 churn_ctx->num_failed_stop++;
6261 churn_ctx->num_to_stop--;
6265 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6266 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
6268 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
6269 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
6271 if (total_left == 0)
6273 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
6277 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
6278 churn_ctx->num_failed_start,
6279 churn_ctx->num_failed_stop);
6281 churn_ctx->cb (churn_ctx->cb_cls, error_message);
6282 GNUNET_free_non_null (error_message);
6283 GNUNET_free (churn_ctx);
6284 GNUNET_free (shutdown_ctx);
6289 * Count the number of running peers.
6291 * @param pg handle for the peer group
6293 * @return the number of currently running peers in the peer group
6296 GNUNET_TESTING_daemons_running(struct GNUNET_TESTING_PeerGroup *pg)
6299 unsigned int running = 0;
6300 for (i = 0; i < pg->total; i++)
6302 if (pg->peers[i].daemon->running == GNUNET_YES)
6304 GNUNET_assert (running != -1);
6312 * Task to rate limit the number of outstanding peer shutdown
6313 * requests. This is necessary for making sure we don't do
6314 * too many ssh connections at once, but is generally nicer
6315 * to any system as well (graduated task starts, as opposed
6316 * to calling gnunet-arm N times all at once).
6319 schedule_churn_shutdown_task(void *cls,
6320 const struct GNUNET_SCHEDULER_TaskContext *tc)
6322 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6323 struct ShutdownContext *shutdown_ctx;
6324 struct ChurnContext *churn_ctx;
6325 GNUNET_assert (peer_shutdown_ctx != NULL);
6326 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6327 GNUNET_assert (shutdown_ctx != NULL);
6328 churn_ctx = (struct ChurnContext *) shutdown_ctx->cb_cls;
6329 if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
6330 GNUNET_SCHEDULER_add_delayed (
6331 GNUNET_TIME_relative_multiply (
6332 GNUNET_TIME_UNIT_MILLISECONDS,
6334 &schedule_churn_shutdown_task,
6338 shutdown_ctx->outstanding++;
6339 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6340 shutdown_ctx->timeout, shutdown_ctx->cb,
6341 shutdown_ctx, GNUNET_NO, GNUNET_YES);
6342 GNUNET_free (peer_shutdown_ctx);
6348 * Simulate churn by stopping some peers (and possibly
6349 * re-starting others if churn is called multiple times). This
6350 * function can only be used to create leave-join churn (peers "never"
6351 * leave for good). First "voff" random peers that are currently
6352 * online will be taken offline; then "von" random peers that are then
6353 * offline will be put back online. No notifications will be
6354 * generated for any of these operations except for the callback upon
6357 * @param pg handle for the peer group
6358 * @param voff number of peers that should go offline
6359 * @param von number of peers that should come back online;
6360 * must be zero on first call (since "testbed_start"
6361 * always starts all of the peers)
6362 * @param timeout how long to wait for operations to finish before
6364 * @param cb function to call at the end
6365 * @param cb_cls closure for cb
6368 GNUNET_TESTING_daemons_churn(struct GNUNET_TESTING_PeerGroup *pg,
6369 unsigned int voff, unsigned int von,
6370 struct GNUNET_TIME_Relative timeout,
6371 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6373 struct ChurnContext *churn_ctx;
6374 struct ShutdownContext *shutdown_ctx;
6375 struct PeerShutdownContext *peer_shutdown_ctx;
6376 struct PeerRestartContext *peer_restart_ctx;
6377 struct ChurnRestartContext *churn_startup_ctx;
6379 unsigned int running;
6380 unsigned int stopped;
6381 unsigned int total_running;
6382 unsigned int total_stopped;
6384 unsigned int *running_arr;
6385 unsigned int *stopped_arr;
6386 unsigned int *running_permute;
6387 unsigned int *stopped_permute;
6389 shutdown_ctx = NULL;
6390 peer_shutdown_ctx = NULL;
6391 peer_restart_ctx = NULL;
6392 churn_startup_ctx = NULL;
6397 if ((von == 0) && (voff == 0)) /* No peers at all? */
6403 for (i = 0; i < pg->total; i++)
6405 if (pg->peers[i].daemon->running == GNUNET_YES)
6407 GNUNET_assert (running != -1);
6412 GNUNET_assert (stopped != -1);
6419 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6420 "Trying to stop more peers than are currently running!\n");
6421 cb (cb_cls, "Trying to stop more peers than are currently running!");
6427 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6428 "Trying to start more peers than are currently stopped!\n");
6429 cb (cb_cls, "Trying to start more peers than are currently stopped!");
6433 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6437 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
6441 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
6443 running_permute = NULL;
6444 stopped_permute = NULL;
6447 running_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6450 stopped_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6453 total_running = running;
6454 total_stopped = stopped;
6458 churn_ctx->num_to_start = von;
6459 churn_ctx->num_to_stop = voff;
6461 churn_ctx->cb_cls = cb_cls;
6464 for (i = 0; i < pg->total; i++)
6466 if (pg->peers[i].daemon->running == GNUNET_YES)
6468 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6469 running_arr[running] = i;
6474 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6475 stopped_arr[stopped] = i;
6480 GNUNET_assert (running >= voff);
6483 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6484 shutdown_ctx->cb = &churn_stop_callback;
6485 shutdown_ctx->cb_cls = churn_ctx;
6486 shutdown_ctx->total_peers = voff;
6487 shutdown_ctx->timeout = timeout;
6490 for (i = 0; i < voff; i++)
6493 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
6494 running_permute[i]);
6496 GNUNET_assert (running_arr != NULL);
6497 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6498 peer_shutdown_ctx->daemon
6499 = pg->peers[running_arr[running_permute[i]]].daemon;
6500 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6501 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
6505 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
6507 &churn_stop_callback, churn_ctx,
6508 GNUNET_NO, GNUNET_YES); */
6511 GNUNET_assert (stopped >= von);
6514 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6515 churn_startup_ctx->churn_ctx = churn_ctx;
6516 churn_startup_ctx->timeout = timeout;
6517 churn_startup_ctx->pg = pg;
6519 for (i = 0; i < von; i++)
6522 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
6523 stopped_permute[i]);
6525 GNUNET_assert (stopped_arr != NULL);
6526 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
6527 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
6528 peer_restart_ctx->daemon
6529 = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
6530 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
6532 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
6533 timeout, &churn_start_callback, churn_ctx); */
6536 GNUNET_free_non_null (running_arr);
6537 GNUNET_free_non_null (stopped_arr);
6538 GNUNET_free_non_null (running_permute);
6539 GNUNET_free_non_null (stopped_permute);
6544 * Restart all peers in the given group.
6546 * @param pg the handle to the peer group
6547 * @param callback function to call on completion (or failure)
6548 * @param callback_cls closure for the callback function
6551 GNUNET_TESTING_daemons_restart(struct GNUNET_TESTING_PeerGroup *pg,
6552 GNUNET_TESTING_NotifyCompletion callback,
6555 struct RestartContext *restart_context;
6560 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
6561 restart_context->peer_group = pg;
6562 restart_context->peers_restarted = 0;
6563 restart_context->callback = callback;
6564 restart_context->callback_cls = callback_cls;
6566 for (off = 0; off < pg->total; off++)
6568 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
6569 &restart_callback, restart_context);
6576 * Start or stop an individual peer from the given group.
6578 * @param pg handle to the peer group
6579 * @param offset which peer to start or stop
6580 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
6581 * @param timeout how long to wait for shutdown
6582 * @param cb function to call at the end
6583 * @param cb_cls closure for cb
6586 GNUNET_TESTING_daemons_vary(struct GNUNET_TESTING_PeerGroup *pg,
6587 unsigned int offset, int desired_status,
6588 struct GNUNET_TIME_Relative timeout,
6589 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6591 struct ShutdownContext *shutdown_ctx;
6592 struct ChurnRestartContext *startup_ctx;
6593 struct ChurnContext *churn_ctx;
6595 if (GNUNET_NO == desired_status)
6597 if (NULL != pg->peers[offset].daemon)
6599 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6600 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6601 churn_ctx->num_to_start = 0;
6602 churn_ctx->num_to_stop = 1;
6604 churn_ctx->cb_cls = cb_cls;
6605 shutdown_ctx->cb_cls = churn_ctx;
6606 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon, timeout,
6607 &churn_stop_callback, shutdown_ctx,
6608 GNUNET_NO, GNUNET_YES);
6611 else if (GNUNET_YES == desired_status)
6613 if (NULL == pg->peers[offset].daemon)
6615 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6616 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6617 churn_ctx->num_to_start = 1;
6618 churn_ctx->num_to_stop = 0;
6620 churn_ctx->cb_cls = cb_cls;
6621 startup_ctx->churn_ctx = churn_ctx;
6622 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
6623 timeout, &churn_start_callback,
6633 * Callback for shutting down peers in a peer group.
6635 * @param cls closure (struct ShutdownContext)
6636 * @param emsg NULL on success
6639 internal_shutdown_callback(void *cls, const char *emsg)
6641 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6642 struct ShutdownContext *shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6644 struct OutstandingSSH *ssh_pos;
6646 shutdown_ctx->outstanding--;
6647 if (peer_shutdown_ctx->daemon->hostname != NULL)
6648 decrement_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6653 shutdown_ctx->peers_down++;
6657 shutdown_ctx->peers_failed++;
6660 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down
6661 + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
6663 if (shutdown_ctx->peers_failed > 0)
6664 shutdown_ctx->cb (shutdown_ctx->cb_cls,
6665 "Not all peers successfully shut down!");
6667 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
6669 GNUNET_free (shutdown_ctx->pg->peers);
6670 GNUNET_free_non_null(shutdown_ctx->pg->hostkey_data);
6671 for (off = 0; off < shutdown_ctx->pg->num_hosts; off++)
6673 GNUNET_free (shutdown_ctx->pg->hosts[off].hostname);
6674 GNUNET_free_non_null (shutdown_ctx->pg->hosts[off].username);
6676 GNUNET_free_non_null (shutdown_ctx->pg->hosts);
6677 while (NULL != (ssh_pos = shutdown_ctx->pg->ssh_head))
6679 GNUNET_CONTAINER_DLL_remove(shutdown_ctx->pg->ssh_head, shutdown_ctx->pg->ssh_tail, ssh_pos);
6680 GNUNET_free(ssh_pos);
6682 GNUNET_free (shutdown_ctx->pg);
6683 GNUNET_free (shutdown_ctx);
6685 GNUNET_free(peer_shutdown_ctx);
6690 * Task to rate limit the number of outstanding peer shutdown
6691 * requests. This is necessary for making sure we don't do
6692 * too many ssh connections at once, but is generally nicer
6693 * to any system as well (graduated task starts, as opposed
6694 * to calling gnunet-arm N times all at once).
6697 schedule_shutdown_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6699 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6700 struct ShutdownContext *shutdown_ctx;
6702 GNUNET_assert (peer_shutdown_ctx != NULL);
6703 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6704 GNUNET_assert (shutdown_ctx != NULL);
6706 if ((shutdown_ctx->outstanding < shutdown_ctx->pg->max_concurrent_ssh)
6707 || ((peer_shutdown_ctx->daemon->hostname != NULL)
6708 && (count_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6710 < shutdown_ctx->pg->max_concurrent_ssh)))
6712 if (peer_shutdown_ctx->daemon->hostname != NULL)
6713 increment_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6715 shutdown_ctx->outstanding++;
6716 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6717 shutdown_ctx->timeout,
6718 &internal_shutdown_callback, peer_shutdown_ctx,
6719 shutdown_ctx->delete_files, GNUNET_NO);
6722 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
6724 &schedule_shutdown_task, peer_shutdown_ctx);
6730 * Shutdown all peers started in the given group.
6732 * @param pg handle to the peer group
6733 * @param timeout how long to wait for shutdown
6734 * @param cb callback to notify upon success or failure
6735 * @param cb_cls closure for cb
6738 GNUNET_TESTING_daemons_stop(struct GNUNET_TESTING_PeerGroup *pg,
6739 struct GNUNET_TIME_Relative timeout,
6740 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6743 struct ShutdownContext *shutdown_ctx;
6744 struct PeerShutdownContext *peer_shutdown_ctx;
6746 struct PeerConnection *conn_iter;
6747 struct PeerConnection *temp_conn;
6750 GNUNET_assert (pg->total > 0);
6752 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6753 shutdown_ctx->delete_files = GNUNET_CONFIGURATION_get_value_yesno (pg->cfg,
6756 shutdown_ctx->cb = cb;
6757 shutdown_ctx->cb_cls = cb_cls;
6758 shutdown_ctx->total_peers = pg->total;
6759 shutdown_ctx->timeout = timeout;
6760 shutdown_ctx->pg = pg;
6761 /* shtudown_ctx->outstanding = 0; */
6763 for (off = 0; off < pg->total; off++)
6765 GNUNET_assert (NULL != pg->peers[off].daemon);
6766 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6767 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
6768 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6769 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
6771 if (NULL != pg->peers[off].cfg)
6773 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
6774 pg->peers[off].cfg = NULL;
6777 conn_iter = pg->peers[off].allowed_peers_head;
6778 while (conn_iter != NULL)
6780 temp_conn = conn_iter->next;
6781 GNUNET_free(conn_iter);
6782 conn_iter = temp_conn;
6785 conn_iter = pg->peers[off].connect_peers_head;
6786 while (conn_iter != NULL)
6788 temp_conn = conn_iter->next;
6789 GNUNET_free(conn_iter);
6790 conn_iter = temp_conn;
6793 conn_iter = pg->peers[off].blacklisted_peers_head;
6794 while (conn_iter != NULL)
6796 temp_conn = conn_iter->next;
6797 GNUNET_free(conn_iter);
6798 conn_iter = temp_conn;
6801 conn_iter = pg->peers[off].connect_peers_working_set_head;
6802 while (conn_iter != NULL)
6804 temp_conn = conn_iter->next;
6805 GNUNET_free(conn_iter);
6806 conn_iter = temp_conn;
6809 if (pg->peers[off].allowed_peers != NULL)
6810 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
6811 if (pg->peers[off].connect_peers != NULL)
6812 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
6813 if (pg->peers[off].blacklisted_peers != NULL)
6814 GNUNET_CONTAINER_multihashmap_destroy (pg->
6815 peers[off].blacklisted_peers);
6820 /* end of testing_group.c */