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;
240 * Individual shutdown context for a particular peer.
242 struct PeerShutdownContext
245 * Pointer to the high level shutdown context.
247 struct ShutdownContext *shutdown_ctx;
250 * The daemon handle for the peer to shut down.
252 struct GNUNET_TESTING_Daemon *daemon;
256 * Individual shutdown context for a particular peer.
258 struct PeerRestartContext
261 * Pointer to the high level restart context.
263 struct ChurnRestartContext *churn_restart_ctx;
266 * The daemon handle for the peer to shut down.
268 struct GNUNET_TESTING_Daemon *daemon;
271 struct CreateTopologyContext
275 * Function to call with number of connections
277 GNUNET_TESTING_NotifyConnections cont;
280 * Closure for connection notification
287 /** Waiting to read number of peers */
290 /** Should find next peer index */
293 /** Should find colon */
296 /** Should read other peer index, space, or endline */
301 struct PeerConnection
306 struct PeerConnection *prev;
311 struct PeerConnection *next;
314 * Index of daemon in pg->peers
321 struct InternalStartContext
324 * Pointer to peerdata
326 struct PeerData *peer;
329 * Timeout for peer startup
331 struct GNUNET_TIME_Relative timeout;
334 * Client callback for hostkey notification
336 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
339 * Closure for hostkey_callback
344 * Client callback for peer start notification
346 GNUNET_TESTING_NotifyDaemonRunning start_cb;
354 * Hostname, where to start the peer
356 const char *hostname;
359 * Username to use when connecting to the
362 const char *username;
365 * Pointer to starting memory location of a hostkey
370 * Port to use for ssh.
376 struct ChurnRestartContext
379 * PeerGroup that we are working with.
381 struct GNUNET_TESTING_PeerGroup *pg;
384 * Number of restarts currently in flight.
386 unsigned int outstanding;
389 * Handle to the underlying churn context.
391 struct ChurnContext *churn_ctx;
394 * How long to allow the operation to take.
396 struct GNUNET_TIME_Relative timeout;
399 struct OutstandingSSH
401 struct OutstandingSSH *next;
403 struct OutstandingSSH *prev;
406 * Number of current ssh connections.
408 uint32_t outstanding;
411 * The hostname of this peer.
413 const char *hostname;
417 * Data we keep per peer.
422 * (Initial) configuration of the host.
423 * (initial because clients could change
424 * it and we would not know about those
427 struct GNUNET_CONFIGURATION_Handle *cfg;
430 * Handle for controlling the daemon.
432 struct GNUNET_TESTING_Daemon *daemon;
435 * The peergroup this peer belongs to.
437 struct GNUNET_TESTING_PeerGroup *pg;
441 * Linked list of allowed peer connections.
443 struct PeerConnection *allowed_peers_head;
446 * Linked list of allowed peer connections.
448 struct PeerConnection *allowed_peers_tail;
451 * Linked list of blacklisted peer connections.
453 struct PeerConnection *blacklisted_peers_head;
456 * Linked list of blacklisted peer connections.
458 struct PeerConnection *blacklisted_peers_tail;
461 * Linked list of connect peer connections.
463 struct PeerConnection *connect_peers_head;
466 * Linked list of connect peer connections.
468 struct PeerConnection *connect_peers_tail;
471 * Linked list of connect peer connections.
473 struct PeerConnection *connect_peers_working_set_head;
476 * Linked list of connect peer connections.
478 struct PeerConnection *connect_peers_working_set_tail;
482 * Hash map of allowed peer connections (F2F created topology)
484 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
487 * Hash map of blacklisted peers
489 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
492 * Hash map of peer connections
494 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
497 * Temporary hash map of peer connections
499 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
503 * Temporary variable for topology creation, should be reset before
504 * creating any topology so the count is valid once finished.
509 * Context to keep track of peers being started, to
510 * stagger hostkey generation and peer startup.
512 struct InternalStartContext internal_context;
516 * Linked list of per-host data.
526 * SSH username to use when connecting to this host.
531 * SSH port to use when connecting to this host.
536 * Lowest port that we have not yet used
542 struct TopologyIterateContext
545 * The peergroup we are working with.
547 struct GNUNET_TESTING_PeerGroup *pg;
550 * Callback for notifying of two connected peers.
552 GNUNET_TESTING_NotifyTopology topology_cb;
555 * Closure for topology_cb
560 * Number of peers currently connected to.
562 unsigned int connected;
565 * Number of peers we have finished iterating.
567 unsigned int completed;
570 * Number of peers total.
575 struct StatsIterateContext
578 * The peergroup that we are dealing with.
580 struct GNUNET_TESTING_PeerGroup *pg;
583 * Continuation to call once all stats information has been retrieved.
585 GNUNET_STATISTICS_Callback cont;
588 * Proc function to call on each value received.
590 GNUNET_TESTING_STATISTICS_Iterator proc;
593 * Closure for topology_cb
598 * Number of peers currently connected to.
600 unsigned int connected;
603 * Number of peers we have finished iterating.
605 unsigned int completed;
608 * Number of peers total.
616 struct GNUNET_TESTING_Daemon *daemon;
619 struct StatsCoreContext
622 struct GNUNET_TESTING_Daemon *daemon;
624 * Handle to the statistics service.
626 struct GNUNET_STATISTICS_Handle *stats_handle;
629 * Handle for getting statistics.
631 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
634 struct ConnectTopologyContext
637 * How many connections are left to create.
639 unsigned int remaining_connections;
642 * Handle to group of peers.
644 struct GNUNET_TESTING_PeerGroup *pg;
647 * How long to try this connection before timing out.
649 struct GNUNET_TIME_Relative connect_timeout;
652 * How many times to retry connecting the two peers.
654 unsigned int connect_attempts;
657 * Temp value set for each iteration.
659 //struct PeerData *first;
662 * Notification that all peers are connected.
664 GNUNET_TESTING_NotifyCompletion notify_connections_done;
667 * Closure for notify.
673 * Handle to a group of GNUnet peers.
675 struct GNUNET_TESTING_PeerGroup
678 * Configuration template.
680 const struct GNUNET_CONFIGURATION_Handle *cfg;
683 * Function to call on each started daemon.
685 //GNUNET_TESTING_NotifyDaemonRunning cb;
693 * Function to call on each topology connection created
695 GNUNET_TESTING_NotifyConnection notify_connection;
698 * Callback for notify_connection
700 void *notify_connection_cls;
703 * Array of information about hosts.
705 struct HostData *hosts;
708 * Number of hosts (size of HostData)
710 unsigned int num_hosts;
713 * Array of "total" peers.
715 struct PeerData *peers;
718 * Number of peers in this group.
723 * At what time should we fail the peer startup process?
725 struct GNUNET_TIME_Absolute max_timeout;
728 * How many peers are being started right now?
730 unsigned int starting;
733 * How many peers have already been started?
735 unsigned int started;
738 * Number of possible connections to peers
741 unsigned int max_outstanding_connections;
744 * Number of ssh connections to peers (max).
746 unsigned int max_concurrent_ssh;
749 * Number of connects we are waiting on, allows us to rate limit
752 unsigned int outstanding_connects;
755 * Number of HELLOs we have yet to send.
757 unsigned int remaining_hellos;
760 * How many connects have already been scheduled?
762 unsigned int total_connects_scheduled;
765 * Hostkeys loaded from a file.
770 * Head of DLL to keep track of the number of outstanding
771 * ssh connections per peer.
773 struct OutstandingSSH *ssh_head;
776 * Tail of DLL to keep track of the number of outstanding
777 * ssh connections per peer.
779 struct OutstandingSSH *ssh_tail;
782 * Stop scheduling peers connecting.
784 unsigned int stop_connects;
787 * Connection context for peer group.
789 struct ConnectTopologyContext ct_ctx;
795 * The altered configuration.
797 struct GNUNET_CONFIGURATION_Handle *ret;
800 * The original configuration to alter.
802 const struct GNUNET_CONFIGURATION_Handle *orig;
805 * The hostname that this peer will run on.
807 const char *hostname;
810 * The next possible port to assign.
815 * Unique number for unix domain sockets.
820 * Unique number for this peer/host to offset
821 * things that are grouped by host.
826 struct ConnectContext
829 * Index of peer to connect second to.
831 uint32_t first_index;
834 * Index of peer to connect first to.
836 uint32_t second_index;
839 * Higher level topology connection context.
841 struct ConnectTopologyContext *ct_ctx;
844 * Whether this connection has been accounted for in the schedule_connect call.
849 struct UnblacklistContext
854 struct GNUNET_TESTING_PeerGroup *pg;
857 * uid of the first peer
867 struct GNUNET_TESTING_PeerGroup *pg;
870 * uid of the first peer
875 * Peer data for first peer.
877 struct PeerData *first;
880 * Random percentage to use
885 struct MinimumContext
890 struct GNUNET_TESTING_PeerGroup *pg;
893 * uid of the first peer
898 * Peer data for first peer.
900 struct PeerData *first;
903 * Number of conns per peer
905 unsigned int num_to_add;
908 * Permuted array of all possible connections. Only add the Nth
909 * peer if it's in the Nth position.
911 unsigned int *pg_array;
914 * What number is the current element we are iterating over?
916 unsigned int current;
924 struct GNUNET_TESTING_PeerGroup *pg;
927 * uid of the first peer
932 * uid of the second peer
937 * Peer data for first peer.
939 struct PeerData *first;
942 * Which peer has been chosen as the one to add?
947 * What number is the current element we are iterating over?
949 unsigned int current;
953 * Simple struct to keep track of progress, and print a
954 * nice little percentage meter for long running tasks.
964 unsigned int completed;
968 char *startup_string;
973 * Convert unique ID to hash code.
975 * @param uid unique ID to convert
976 * @param hash set to uid (extended with zeros)
979 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
981 memset (hash, 0, sizeof (GNUNET_HashCode));
982 *((uint32_t *) hash) = uid;
986 * Convert hash code to unique ID.
988 * @param uid unique ID to convert
989 * @param hash set to uid (extended with zeros)
992 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
994 memcpy (uid, hash, sizeof (uint32_t));
999 static struct GNUNET_CORE_MessageHandler no_handlers[] =
1005 * Create a meter to keep track of the progress of some task.
1007 * @param total the total number of items to complete
1008 * @param start_string a string to prefix the meter with (if printing)
1009 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
1012 * @return the progress meter
1014 static struct ProgressMeter *
1015 create_meter(unsigned int total, char * start_string, int print)
1017 struct ProgressMeter *ret;
1018 ret = GNUNET_malloc(sizeof(struct ProgressMeter));
1021 ret->modnum = total / 4;
1022 if (ret->modnum == 0) /* Divide by zero check */
1024 ret->dotnum = (total / 50) + 1;
1025 if (start_string != NULL)
1026 ret->startup_string = GNUNET_strdup(start_string);
1028 ret->startup_string = GNUNET_strdup("");
1034 * Update progress meter (increment by one).
1036 * @param meter the meter to update and print info for
1038 * @return GNUNET_YES if called the total requested,
1039 * GNUNET_NO if more items expected
1042 update_meter(struct ProgressMeter *meter)
1044 if (meter->print == GNUNET_YES)
1046 if (meter->completed % meter->modnum == 0)
1048 if (meter->completed == 0)
1050 fprintf (stdout, "%sProgress: [0%%", meter->startup_string);
1053 fprintf (stdout, "%d%%", (int) (((float) meter->completed
1054 / meter->total) * 100));
1056 else if (meter->completed % meter->dotnum == 0)
1057 fprintf (stdout, ".");
1059 if (meter->completed + 1 == meter->total)
1060 fprintf (stdout, "%d%%]\n", 100);
1065 if (meter->completed == meter->total)
1067 if (meter->completed > meter->total)
1068 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
1073 * Reset progress meter.
1075 * @param meter the meter to reset
1077 * @return GNUNET_YES if meter reset,
1078 * GNUNET_SYSERR on error
1081 reset_meter(struct ProgressMeter *meter)
1084 return GNUNET_SYSERR;
1086 meter->completed = 0;
1091 * Release resources for meter
1093 * @param meter the meter to free
1096 free_meter(struct ProgressMeter *meter)
1098 GNUNET_free_non_null (meter->startup_string);
1099 GNUNET_free (meter);
1103 * Get a topology from a string input.
1105 * @param topology where to write the retrieved topology
1106 * @param topology_string The string to attempt to
1107 * get a configuration value from
1108 * @return GNUNET_YES if topology string matched a
1109 * known topology, GNUNET_NO if not
1112 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology,
1113 const char *topology_string)
1116 * Strings representing topologies in enum
1118 static const char *topology_strings[] =
1121 * A clique (everyone connected to everyone else).
1126 * Small-world network (2d torus plus random links).
1131 * Small-world network (ring plus random links).
1151 * Certain percentage of peers are unable to communicate directly
1152 * replicating NAT conditions
1157 * Scale free topology.
1162 * Straight line topology.
1167 * All peers are disconnected.
1172 * Read the topology from a file.
1179 if (topology_string == NULL)
1181 while (topology_strings[curr] != NULL)
1183 if (strcasecmp (topology_strings[curr], topology_string) == 0)
1190 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
1195 * Get connect topology option from string input.
1197 * @param topology_option where to write the retrieved topology
1198 * @param topology_string The string to attempt to
1199 * get a configuration value from
1200 * @return GNUNET_YES if string matched a known
1201 * topology option, GNUNET_NO if not
1204 GNUNET_TESTING_topology_option_get(
1205 enum GNUNET_TESTING_TopologyOption *topology_option,
1206 const char *topology_string)
1209 * Options for connecting a topology as strings.
1211 static const char *topology_option_strings[] =
1214 * Try to connect all peers specified in the topology.
1219 * Choose a random subset of connections to create.
1221 "CONNECT_RANDOM_SUBSET",
1224 * Create at least X connections for each peer.
1229 * Using a depth first search, create one connection
1230 * per peer. If any are missed (graph disconnected)
1231 * start over at those peers until all have at least one
1237 * Find the N closest peers to each allowed peer in the
1238 * topology and make sure a connection to those peers
1239 * exists in the connect topology.
1244 * No options specified.
1251 if (topology_string == NULL)
1253 while (NULL != topology_option_strings[curr])
1255 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1257 *topology_option = curr;
1262 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1267 * Function to iterate over options. Copies
1268 * the options to the target configuration,
1269 * updating PORT values as needed.
1271 * @param cls closure
1272 * @param section name of the section
1273 * @param option name of the option
1274 * @param value value of the option
1277 update_config(void *cls, const char *section, const char *option,
1280 struct UpdateContext *ctx = cls;
1284 char *single_variable;
1285 char *per_host_variable;
1286 unsigned long long num_per_host;
1288 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1289 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1291 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1293 if ((ival != 0) && (GNUNET_YES
1294 != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1297 GNUNET_snprintf (cval, sizeof(cval), "%u", ctx->nport++);
1300 else if ((ival != 0) && (GNUNET_YES
1301 == GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1303 && GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1307 GNUNET_snprintf (cval, sizeof(cval), "%u", ival + ctx->fdnum
1312 /* FIXME: REMOVE FOREVER HACK HACK HACK */
1313 if (0 == strcasecmp (section, "transport-tcp"))
1314 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, "ADVERTISED_PORT", value);
1317 if (0 == strcmp (option, "UNIXPATH"))
1319 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig,
1323 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1324 section, ctx->upnum++);
1327 else if ((GNUNET_YES
1328 == GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1331 && (num_per_host > 0))
1334 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1335 section, ctx->fdnum % num_per_host);
1340 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1342 value = ctx->hostname;
1344 GNUNET_free (single_variable);
1345 GNUNET_free (per_host_variable);
1346 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1350 * Create a new configuration using the given configuration
1351 * as a template; however, each PORT in the existing cfg
1352 * must be renumbered by incrementing "*port". If we run
1353 * out of "*port" numbers, return NULL.
1355 * @param cfg template configuration
1356 * @param off the current peer offset
1357 * @param port port numbers to use, update to reflect
1358 * port numbers that were used
1359 * @param upnum number to make unix domain socket names unique
1360 * @param hostname hostname of the controlling host, to allow control connections from
1361 * @param fdnum number used to offset the unix domain socket for grouped processes
1362 * (such as statistics or peerinfo, which can be shared among others)
1364 * @return new configuration, NULL on error
1366 static struct GNUNET_CONFIGURATION_Handle *
1367 make_config(const struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t off,
1368 uint16_t * port, uint32_t * upnum, const char *hostname,
1371 struct UpdateContext uc;
1374 char *allowed_hosts;
1380 uc.ret = GNUNET_CONFIGURATION_create ();
1381 uc.hostname = hostname;
1384 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1385 if (uc.nport >= HIGH_PORT)
1388 GNUNET_CONFIGURATION_destroy (uc.ret);
1392 if (GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "control_host",
1393 &control_host) == GNUNET_OK)
1395 if (hostname != NULL)
1396 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1399 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1401 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1403 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1404 "ACCEPT_FROM", allowed_hosts);
1405 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1407 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1408 "ACCEPT_FROM", allowed_hosts);
1410 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH", "");
1411 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH",
1413 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH", "");
1414 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1417 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "USE_LOCALADDR",
1419 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "USE_LOCALADDR",
1421 GNUNET_free_non_null (control_host);
1422 GNUNET_free (allowed_hosts);
1425 /* arm needs to know to allow connections from the host on which it is running,
1426 * otherwise gnunet-arm is unable to connect to it in some instances */
1427 if (hostname != NULL)
1429 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1430 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1432 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
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_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1440 GNUNET_free (allowed_hosts);
1444 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1446 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1448 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "USE_LOCALADDR",
1450 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "USE_LOCALADDR",
1454 *port = (uint16_t) uc.nport;
1462 * Remove entries from the peer connection list
1464 * @param pg the peer group we are working with
1465 * @param first index of the first peer
1466 * @param second index of the second peer
1467 * @param list the peer list to use
1468 * @param check UNUSED
1470 * @return the number of connections added (can be 0, 1 or 2)
1474 remove_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1475 unsigned int second, enum PeerLists list, unsigned int check)
1479 struct PeerConnection **first_list;
1480 struct PeerConnection **second_list;
1481 struct PeerConnection *first_iter;
1482 struct PeerConnection *second_iter;
1483 struct PeerConnection **first_tail;
1484 struct PeerConnection **second_tail;
1487 GNUNET_HashCode hash_first;
1488 GNUNET_HashCode hash_second;
1490 hash_from_uid (first, &hash_first);
1491 hash_from_uid (second, &hash_second);
1499 first_list = &pg->peers[first].allowed_peers_head;
1500 second_list = &pg->peers[second].allowed_peers_head;
1501 first_tail = &pg->peers[first].allowed_peers_tail;
1502 second_tail = &pg->peers[second].allowed_peers_tail;
1505 first_list = &pg->peers[first].connect_peers_head;
1506 second_list = &pg->peers[second].connect_peers_head;
1507 first_tail = &pg->peers[first].connect_peers_tail;
1508 second_tail = &pg->peers[second].connect_peers_tail;
1511 first_list = &pg->peers[first].blacklisted_peers_head;
1512 second_list = &pg->peers[second].blacklisted_peers_head;
1513 first_tail = &pg->peers[first].blacklisted_peers_tail;
1514 second_tail = &pg->peers[second].blacklisted_peers_tail;
1517 first_list = &pg->peers[first].connect_peers_working_set_head;
1518 second_list = &pg->peers[second].connect_peers_working_set_head;
1519 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1520 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1527 first_iter = *first_list;
1528 while (first_iter != NULL)
1530 if (first_iter->index == second)
1532 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1533 GNUNET_free(first_iter);
1537 first_iter = first_iter->next;
1540 second_iter = *second_list;
1541 while (second_iter != NULL)
1543 if (second_iter->index == first)
1545 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1546 GNUNET_free(second_iter);
1550 second_iter = second_iter->next;
1554 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1557 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1562 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1565 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1574 * Add entries to the some list
1576 * @param pg the peer group we are working with
1577 * @param first index of the first peer
1578 * @param second index of the second peer
1579 * @param list the list type that we should modify
1580 * @param check GNUNET_YES to check lists before adding
1581 * GNUNET_NO to force add
1583 * @return the number of connections added (can be 0, 1 or 2)
1587 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1588 unsigned int second, enum PeerLists list, unsigned int check)
1594 struct PeerConnection **first_list;
1595 struct PeerConnection **second_list;
1596 struct PeerConnection *first_iter;
1597 struct PeerConnection *second_iter;
1598 struct PeerConnection *new_first;
1599 struct PeerConnection *new_second;
1600 struct PeerConnection **first_tail;
1601 struct PeerConnection **second_tail;
1606 first_list = &pg->peers[first].allowed_peers_head;
1607 second_list = &pg->peers[second].allowed_peers_head;
1608 first_tail = &pg->peers[first].allowed_peers_tail;
1609 second_tail = &pg->peers[second].allowed_peers_tail;
1612 first_list = &pg->peers[first].connect_peers_head;
1613 second_list = &pg->peers[second].connect_peers_head;
1614 first_tail = &pg->peers[first].connect_peers_tail;
1615 second_tail = &pg->peers[second].connect_peers_tail;
1618 first_list = &pg->peers[first].blacklisted_peers_head;
1619 second_list = &pg->peers[second].blacklisted_peers_head;
1620 first_tail = &pg->peers[first].blacklisted_peers_tail;
1621 second_tail = &pg->peers[second].blacklisted_peers_tail;
1624 first_list = &pg->peers[first].connect_peers_working_set_head;
1625 second_list = &pg->peers[second].connect_peers_working_set_head;
1626 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1627 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1634 add_first = GNUNET_YES;
1635 add_second = GNUNET_YES;
1637 if (check == GNUNET_YES)
1639 first_iter = *first_list;
1640 while (first_iter != NULL)
1642 if (first_iter->index == second)
1644 add_first = GNUNET_NO;
1647 first_iter = first_iter->next;
1650 second_iter = *second_list;
1651 while (second_iter != NULL)
1653 if (second_iter->index == first)
1655 add_second = GNUNET_NO;
1658 second_iter = second_iter->next;
1665 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1666 new_first->index = second;
1667 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1668 pg->peers[first].num_connections++;
1674 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1675 new_second->index = first;
1676 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1677 pg->peers[second].num_connections++;
1685 * Scale free network construction as described in:
1687 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1689 * Start with a network of "one" peer, then progressively add
1690 * peers up to the total number. At each step, iterate over
1691 * all possible peers and connect new peer based on number of
1692 * existing connections of the target peer.
1694 * @param pg the peer group we are dealing with
1695 * @param proc the connection processor to use
1696 * @param list the peer list to use
1698 * @return the number of connections created
1701 create_scale_free(struct GNUNET_TESTING_PeerGroup *pg,
1702 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1705 unsigned int total_connections;
1706 unsigned int outer_count;
1708 unsigned int previous_total_connections;
1712 GNUNET_assert (pg->total > 1);
1714 /* Add a connection between the first two nodes */
1715 total_connections = proc (pg, 0, 1, list, GNUNET_YES);
1717 for (outer_count = 1; outer_count < pg->total; outer_count++)
1719 previous_total_connections = total_connections;
1720 for (i = 0; i < outer_count; i++)
1722 probability = pg->peers[i].num_connections
1723 / (double) previous_total_connections;
1725 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1727 / ((double) UINT64_MAX);
1729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1730 "Considering connecting peer %d to peer %d\n",
1733 if (random < probability)
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1737 "Connecting peer %d to peer %d\n", outer_count, i);
1739 total_connections += proc (pg, outer_count, i, list, GNUNET_YES);
1744 return total_connections;
1748 * Create a topology given a peer group (set of running peers)
1749 * and a connection processor. Creates a small world topology
1750 * according to the rewired ring construction. The basic
1751 * behavior is that a ring topology is created, but with some
1752 * probability instead of connecting a peer to the next
1753 * neighbor in the ring a connection will be created to a peer
1754 * selected uniformly at random. We use the TESTING
1755 * PERCENTAGE option to specify what number of
1756 * connections each peer should have. Default is 2,
1757 * which makes the ring, any given number is multiplied by
1758 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1759 * each peer have on average 2logn connections. The additional
1760 * connections are made at increasing distance around the ring
1761 * from the original peer, or to random peers based on the re-
1762 * wiring probability. The TESTING
1763 * PROBABILITY option is used as the probability that a given
1764 * connection is rewired.
1766 * @param pg the peergroup to create the topology on
1767 * @param proc the connection processor to call to actually set
1768 * up connections between two peers
1769 * @param list the peer list to use
1771 * @return the number of connections that were set up
1775 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg,
1776 GNUNET_TESTING_ConnectionProcessor proc,
1777 enum PeerLists list)
1781 unsigned int natLog;
1782 unsigned int randomPeer;
1783 double random, logNModifier, probability;
1784 unsigned int smallWorldConnections;
1789 unsigned int useAnd;
1790 int connect_attempts;
1792 logNModifier = 0.5; /* FIXME: default value? */
1793 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1797 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1799 GNUNET_ERROR_TYPE_WARNING,
1801 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1802 p_string, "LOGNMODIFIER", "TESTING");
1803 GNUNET_free (p_string);
1805 probability = 0.5; /* FIXME: default percentage? */
1806 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1810 if (sscanf (p_string, "%lf", &probability) != 1)
1812 GNUNET_ERROR_TYPE_WARNING,
1814 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1815 p_string, "PERCENTAGE", "TESTING");
1816 GNUNET_free (p_string);
1818 natLog = log (pg->total);
1819 connsPerPeer = ceil (natLog * logNModifier);
1821 if (connsPerPeer % 2 == 1)
1824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."),
1827 smallWorldConnections = 0;
1828 connect_attempts = 0;
1829 for (i = 0; i < pg->total; i++)
1832 max = i + connsPerPeer / 2;
1833 min = i - connsPerPeer / 2;
1835 if (max > pg->total - 1)
1837 max = max - pg->total;
1843 min = pg->total - 1 + min;
1847 for (j = 0; j < connsPerPeer / 2; j++)
1850 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1852 / ((double) UINT64_MAX));
1853 if (random < probability)
1855 /* Connect to uniformly selected random peer */
1857 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1859 while ((((randomPeer < max) && (randomPeer > min)) && (useAnd
1860 == 0)) || (((randomPeer > min) || (randomPeer < max))
1864 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1867 smallWorldConnections += proc (pg, i, randomPeer, list,
1872 nodeToConnect = i + j + 1;
1873 if (nodeToConnect > pg->total - 1)
1875 nodeToConnect = nodeToConnect - pg->total;
1877 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
1883 connect_attempts += smallWorldConnections;
1885 return connect_attempts;
1889 * Create a topology given a peer group (set of running peers)
1890 * and a connection processor.
1892 * @param pg the peergroup to create the topology on
1893 * @param proc the connection processor to call to actually set
1894 * up connections between two peers
1895 * @param list the peer list to use
1897 * @return the number of connections that were set up
1901 create_nated_internet(struct GNUNET_TESTING_PeerGroup *pg,
1902 GNUNET_TESTING_ConnectionProcessor proc,
1903 enum PeerLists list)
1905 unsigned int outer_count, inner_count;
1906 unsigned int cutoff;
1907 int connect_attempts;
1908 double nat_percentage;
1911 nat_percentage = 0.6; /* FIXME: default percentage? */
1912 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1916 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1918 GNUNET_ERROR_TYPE_WARNING,
1920 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1921 p_string, "PERCENTAGE", "TESTING");
1922 GNUNET_free (p_string);
1925 cutoff = (unsigned int) (nat_percentage * pg->total);
1926 connect_attempts = 0;
1927 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1929 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1931 if ((outer_count > cutoff) || (inner_count > cutoff))
1934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1935 "Connecting peer %d to peer %d\n",
1936 outer_count, inner_count);
1938 connect_attempts += proc (pg, outer_count, inner_count, list,
1943 return connect_attempts;
1948 * Create a topology given a peer group (set of running peers)
1949 * and a connection processor.
1951 * @param pg the peergroup to create the topology on
1952 * @param proc the connection processor to call to actually set
1953 * up connections between two peers
1954 * @param list the peer list to use
1956 * @return the number of connections that were set up
1960 create_nated_internet_copy(struct GNUNET_TESTING_PeerGroup *pg,
1961 GNUNET_TESTING_ConnectionProcessor proc,
1962 enum PeerLists list)
1964 unsigned int outer_count, inner_count;
1965 unsigned int cutoff;
1966 int connect_attempts;
1967 double nat_percentage;
1970 struct ProgressMeter *conn_meter;
1972 nat_percentage = 0.6; /* FIXME: default percentage? */
1973 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1977 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1979 GNUNET_ERROR_TYPE_WARNING,
1981 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1982 p_string, "PERCENTAGE", "TESTING");
1983 GNUNET_free (p_string);
1986 cutoff = (unsigned int) (nat_percentage * pg->total);
1988 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1990 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1992 if ((outer_count > cutoff) || (inner_count > cutoff))
1998 conn_meter = create_meter (count, "NAT COPY", GNUNET_YES);
1999 connect_attempts = 0;
2000 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2002 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2004 if ((outer_count > cutoff) || (inner_count > cutoff))
2007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2008 "Connecting peer %d to peer %d\n",
2009 outer_count, inner_count);
2011 connect_attempts += proc (pg, outer_count, inner_count, list,
2013 add_connections (pg, outer_count, inner_count, ALLOWED, GNUNET_NO);
2014 update_meter (conn_meter);
2018 free_meter (conn_meter);
2020 return connect_attempts;
2025 * Create a topology given a peer group (set of running peers)
2026 * and a connection processor.
2028 * @param pg the peergroup to create the topology on
2029 * @param proc the connection processor to call to actually set
2030 * up connections between two peers
2031 * @param list the peer list to use
2033 * @return the number of connections that were set up
2037 create_small_world(struct GNUNET_TESTING_PeerGroup *pg,
2038 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2040 unsigned int i, j, k;
2041 unsigned int square;
2044 unsigned int toggle = 1;
2045 unsigned int nodeToConnect;
2046 unsigned int natLog;
2047 unsigned int node1Row;
2048 unsigned int node1Col;
2049 unsigned int node2Row;
2050 unsigned int node2Col;
2051 unsigned int distance;
2052 double probability, random, percentage;
2053 unsigned int smallWorldConnections;
2054 unsigned int small_world_it;
2056 int connect_attempts;
2057 square = floor (sqrt (pg->total));
2061 percentage = 0.5; /* FIXME: default percentage? */
2062 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2066 if (sscanf (p_string, "%lf", &percentage) != 1)
2068 GNUNET_ERROR_TYPE_WARNING,
2070 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2071 p_string, "PERCENTAGE", "TESTING");
2072 GNUNET_free (p_string);
2074 if (percentage < 0.0)
2077 GNUNET_ERROR_TYPE_WARNING,
2079 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
2080 "PERCENTAGE", "TESTING", percentage);
2083 probability = 0.5; /* FIXME: default percentage? */
2084 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2088 if (sscanf (p_string, "%lf", &probability) != 1)
2090 GNUNET_ERROR_TYPE_WARNING,
2092 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2093 p_string, "PROBABILITY", "TESTING");
2094 GNUNET_free (p_string);
2096 if (square * square != pg->total)
2098 while (rows * cols < pg->total)
2100 if (toggle % 2 == 0)
2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2111 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2115 connect_attempts = 0;
2116 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2117 * to the node to its right and above. Once this is over, we'll have our torus!
2118 * Special case for the last node (if the rows and columns are not equal), connect
2119 * to the first in the row to maintain topology.
2121 for (i = 0; i < pg->total; i++)
2123 /* First connect to the node to the right */
2124 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2125 nodeToConnect = i + 1;
2126 else if (i + 1 == pg->total)
2127 nodeToConnect = rows * cols - cols;
2129 nodeToConnect = i - cols + 1;
2131 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2135 nodeToConnect = (rows * cols) - cols + i;
2136 if (nodeToConnect >= pg->total)
2137 nodeToConnect -= cols;
2140 nodeToConnect = i - cols;
2142 if (nodeToConnect < pg->total)
2143 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2145 natLog = log (pg->total);
2146 #if VERBOSE_TESTING > 2
2147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2148 _("natural log of %d is %d, will run %d iterations\n"),
2149 pg->total, natLog, (int) (natLog * percentage));
2150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2151 _("Total connections added thus far: %u!\n"), connect_attempts);
2153 smallWorldConnections = 0;
2154 small_world_it = (unsigned int) (natLog * percentage);
2155 if (small_world_it < 1)
2157 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
2158 for (i = 0; i < small_world_it; i++)
2160 for (j = 0; j < pg->total; j++)
2162 /* Determine the row and column of node at position j on the 2d torus */
2163 node1Row = j / cols;
2164 node1Col = j - (node1Row * cols);
2165 for (k = 0; k < pg->total; k++)
2167 /* Determine the row and column of node at position k on the 2d torus */
2168 node2Row = k / cols;
2169 node2Col = k - (node2Row * cols);
2170 /* Simple Cartesian distance */
2171 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
2174 /* Calculate probability as 1 over the square of the distance */
2175 probability = 1.0 / (distance * distance);
2176 /* Choose a random value between 0 and 1 */
2178 = ((double) GNUNET_CRYPTO_random_u64 (
2179 GNUNET_CRYPTO_QUALITY_WEAK,
2181 / ((double) UINT64_MAX);
2182 /* If random < probability, then connect the two nodes */
2183 if (random < probability)
2184 smallWorldConnections += proc (pg, j, k, list, GNUNET_YES);
2190 connect_attempts += smallWorldConnections;
2191 #if VERBOSE_TESTING > 2
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2193 _("Total connections added for small world: %d!\n"),
2194 smallWorldConnections);
2196 return connect_attempts;
2200 * Create a topology given a peer group (set of running peers)
2201 * and a connection processor.
2203 * @param pg the peergroup to create the topology on
2204 * @param proc the connection processor to call to actually set
2205 * up connections between two peers
2206 * @param list the peer list to use
2208 * @return the number of connections that were set up
2212 create_erdos_renyi(struct GNUNET_TESTING_PeerGroup *pg,
2213 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2216 unsigned int outer_count;
2217 unsigned int inner_count;
2218 int connect_attempts;
2222 probability = 0.5; /* FIXME: default percentage? */
2223 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2227 if (sscanf (p_string, "%lf", &probability) != 1)
2229 GNUNET_ERROR_TYPE_WARNING,
2231 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2232 p_string, "PROBABILITY", "TESTING");
2233 GNUNET_free (p_string);
2235 connect_attempts = 0;
2236 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2238 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2241 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2243 / ((double) UINT64_MAX);
2245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2246 _("rand is %f probability is %f\n"), temp_rand,
2249 if (temp_rand < probability)
2251 connect_attempts += proc (pg, outer_count, inner_count, list,
2257 return connect_attempts;
2261 * Create a topology given a peer group (set of running peers)
2262 * and a connection processor. This particular function creates
2263 * the connections for a 2d-torus, plus additional "closest"
2264 * connections per peer.
2266 * @param pg the peergroup to create the topology on
2267 * @param proc the connection processor to call to actually set
2268 * up connections between two peers
2269 * @param list the peer list to use
2271 * @return the number of connections that were set up
2275 create_2d_torus(struct GNUNET_TESTING_PeerGroup *pg,
2276 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2279 unsigned int square;
2282 unsigned int toggle = 1;
2283 unsigned int nodeToConnect;
2284 int connect_attempts;
2286 connect_attempts = 0;
2288 square = floor (sqrt (pg->total));
2292 if (square * square != pg->total)
2294 while (rows * cols < pg->total)
2296 if (toggle % 2 == 0)
2305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2307 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2310 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2311 * to the node to its right and above. Once this is over, we'll have our torus!
2312 * Special case for the last node (if the rows and columns are not equal), connect
2313 * to the first in the row to maintain topology.
2315 for (i = 0; i < pg->total; i++)
2317 /* First connect to the node to the right */
2318 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2319 nodeToConnect = i + 1;
2320 else if (i + 1 == pg->total)
2321 nodeToConnect = rows * cols - cols;
2323 nodeToConnect = i - cols + 1;
2325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2326 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2328 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2330 /* Second connect to the node immediately above */
2333 nodeToConnect = (rows * cols) - cols + i;
2334 if (nodeToConnect >= pg->total)
2335 nodeToConnect -= cols;
2338 nodeToConnect = i - cols;
2340 if (nodeToConnect < pg->total)
2343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2344 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2346 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2351 return connect_attempts;
2355 * Create a topology given a peer group (set of running peers)
2356 * and a connection processor.
2358 * @param pg the peergroup to create the topology on
2359 * @param proc the connection processor to call to actually set
2360 * up connections between two peers
2361 * @param list the peer list to use
2362 * @param check does the connection processor need to check before
2363 * performing an action on the list?
2365 * @return the number of connections that were set up
2369 create_clique(struct GNUNET_TESTING_PeerGroup *pg,
2370 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list,
2373 unsigned int outer_count;
2374 unsigned int inner_count;
2375 int connect_attempts;
2376 struct ProgressMeter *conn_meter;
2377 connect_attempts = 0;
2379 conn_meter = create_meter ((((pg->total * pg->total) + pg->total) / 2)
2380 - pg->total, "Create Clique ", GNUNET_NO);
2381 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2383 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2387 "Connecting peer %d to peer %d\n",
2388 outer_count, inner_count);
2390 connect_attempts += proc (pg, outer_count, inner_count, list, check);
2391 update_meter (conn_meter);
2394 reset_meter (conn_meter);
2395 free_meter (conn_meter);
2396 return connect_attempts;
2401 * Iterator over hash map entries.
2403 * @param cls closure the peer group
2404 * @param key the key stored in the hashmap is the
2405 * index of the peer to connect to
2406 * @param value value in the hash map, handle to the peer daemon
2407 * @return GNUNET_YES if we should continue to
2412 unblacklist_iterator (void *cls,
2413 const GNUNET_HashCode * key,
2416 struct UnblacklistContext *un_ctx = cls;
2417 uint32_t second_pos;
2419 uid_from_hash (key, &second_pos);
2421 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2428 * Create a blacklist topology based on the allowed topology
2429 * which disallows any connections not in the allowed topology
2430 * at the transport level.
2432 * @param pg the peergroup to create the topology on
2433 * @param proc the connection processor to call to allow
2434 * up connections between two peers
2436 * @return the number of connections that were set up
2440 copy_allowed(struct GNUNET_TESTING_PeerGroup *pg,
2441 GNUNET_TESTING_ConnectionProcessor proc)
2445 struct PeerConnection *iter;
2447 struct UnblacklistContext un_ctx;
2452 for (count = 0; count < pg->total - 1; count++)
2455 iter = pg->peers[count].allowed_peers_head;
2456 while (iter != NULL)
2458 remove_connections (pg, count, iter->index, BLACKLIST, GNUNET_YES);
2459 //unblacklist_connections(pg, count, iter->index);
2463 un_ctx.first_uid = count;
2464 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers,
2465 &unblacklist_iterator,
2469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2470 "Unblacklisted %u peers\n",
2476 * Create a topology given a peer group (set of running peers)
2477 * and a connection processor.
2479 * @param pg the peergroup to create the topology on
2480 * @param proc the connection processor to call to actually set
2481 * up connections between two peers
2482 * @param list which list should be modified
2484 * @return the number of connections that were set up
2488 create_line(struct GNUNET_TESTING_PeerGroup *pg,
2489 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2492 unsigned int connect_attempts;
2494 connect_attempts = 0;
2495 /* Connect each peer to the next highest numbered peer */
2496 for (count = 0; count < pg->total - 1; count++)
2499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2500 "Connecting peer %d to peer %d\n",
2503 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2506 return connect_attempts;
2510 * Create a topology given a peer group (set of running peers)
2511 * and a connection processor.
2513 * @param pg the peergroup to create the topology on
2514 * @param filename the file to read topology information from
2515 * @param proc the connection processor to call to actually set
2516 * up connections between two peers
2517 * @param list the peer list to use
2519 * @return the number of connections that were set up
2523 create_from_file(struct GNUNET_TESTING_PeerGroup *pg, char *filename,
2524 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2526 int connect_attempts;
2527 unsigned int first_peer_index;
2528 unsigned int second_peer_index;
2533 unsigned int total_peers;
2534 enum States curr_state;
2536 connect_attempts = 0;
2537 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2538 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2540 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2542 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2543 "Could not open file `%s' specified for topology!", filename);
2544 return connect_attempts;
2547 data = GNUNET_malloc_large (frstat.st_size);
2548 GNUNET_assert(data != NULL);
2549 if (frstat.st_size != GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2552 GNUNET_ERROR_TYPE_ERROR,
2553 "Could not read file %s specified for host list, ending test!",
2556 return connect_attempts;
2561 first_peer_index = 0;
2562 /* First line should contain a single integer, specifying the number of peers */
2563 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2564 curr_state = NUM_PEERS;
2565 while (count < frstat.st_size - 1)
2567 if ((buf[count] == '\n') || (buf[count] == ' '))
2577 total_peers = strtoul(&buf[count], NULL, 10);
2580 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2581 "Failed to read number of peers from topology file!\n");
2583 return connect_attempts;
2585 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2586 "Read %u total peers in topology\n", total_peers);
2587 GNUNET_assert(total_peers == pg->total);
2588 curr_state = PEER_INDEX;
2589 while ((buf[count] != '\n') && (count < frstat.st_size - 1))
2595 first_peer_index = strtoul(&buf[count], NULL, 10);
2598 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2599 "Failed to read peer index from topology file!\n");
2601 return connect_attempts;
2603 while ((buf[count] != ':') && (count < frstat.st_size - 1))
2606 curr_state = OTHER_PEER_INDEX;
2609 if (1 == sscanf (&buf[count], ":"))
2610 curr_state = OTHER_PEER_INDEX;
2613 case OTHER_PEER_INDEX:
2615 second_peer_index = strtoul(&buf[count], NULL, 10);
2618 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2619 "Failed to peer index from topology file!\n");
2621 return connect_attempts;
2623 /* Assume file is written with first peer 1, but array index is 0 */
2624 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index
2625 - 1, list, GNUNET_YES);
2626 while ((buf[count] != '\n') && (buf[count] != ',') && (count
2627 < frstat.st_size - 1))
2629 if (buf[count] == '\n')
2631 curr_state = PEER_INDEX;
2633 else if (buf[count] != ',')
2635 curr_state = OTHER_PEER_INDEX;
2640 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2641 "Found bad data in topology file while in state %d!\n",
2645 return connect_attempts;
2649 return connect_attempts;
2653 * Create a topology given a peer group (set of running peers)
2654 * and a connection processor.
2656 * @param pg the peergroup to create the topology on
2657 * @param proc the connection processor to call to actually set
2658 * up connections between two peers
2659 * @param list the peer list to use
2661 * @return the number of connections that were set up
2665 create_ring(struct GNUNET_TESTING_PeerGroup *pg,
2666 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2669 int connect_attempts;
2671 connect_attempts = 0;
2673 /* Connect each peer to the next highest numbered peer */
2674 for (count = 0; count < pg->total - 1; count++)
2677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2678 "Connecting peer %d to peer %d\n", count, count + 1);
2680 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2683 /* Connect the last peer to the first peer */
2684 connect_attempts += proc (pg, pg->total - 1, 0, list, GNUNET_YES);
2686 return connect_attempts;
2691 * Iterator for writing friends of a peer to a file.
2693 * @param cls closure, an open writable file handle
2694 * @param key the key the daemon was stored under
2695 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2697 * @return GNUNET_YES to continue iteration
2699 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2700 * with a single file_iterator that takes a closure which contains
2701 * the prefix to write before the peer. Then this could be used
2702 * for blacklisting multiple transports and writing the friend
2703 * file. I'm sure *someone* will complain loudly about other
2704 * things that negate these functions even existing so no point in
2708 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2710 FILE *temp_friend_handle = cls;
2711 struct GNUNET_TESTING_Daemon *peer = value;
2712 struct GNUNET_PeerIdentity *temppeer;
2713 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2715 temppeer = &peer->id;
2716 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2717 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2722 struct BlacklistContext
2725 * The (open) file handle to write to
2727 FILE *temp_file_handle;
2730 * The transport that this peer will be blacklisted on.
2736 * Iterator for writing blacklist data to appropriate files.
2738 * @param cls closure, an open writable file handle
2739 * @param key the key the daemon was stored under
2740 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2742 * @return GNUNET_YES to continue iteration
2745 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2747 struct BlacklistContext *blacklist_ctx = cls;
2748 struct GNUNET_TESTING_Daemon *peer = value;
2749 struct GNUNET_PeerIdentity *temppeer;
2750 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2752 temppeer = &peer->id;
2753 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2754 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2755 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2756 blacklist_ctx->transport, (char *) &peer_enc);
2763 * Create the friend files based on the PeerConnection's
2764 * of each peer in the peer group, and copy the files
2765 * to the appropriate place
2767 * @param pg the peer group we are dealing with
2770 create_and_copy_friend_files(struct GNUNET_TESTING_PeerGroup *pg)
2772 FILE *temp_friend_handle;
2773 unsigned int pg_iter;
2774 char *temp_service_path;
2775 struct GNUNET_OS_Process **procarr;
2779 enum GNUNET_OS_ProcessStatusType type;
2780 unsigned long return_code;
2788 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2789 struct PeerConnection *conn_iter;
2791 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2792 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2794 mytemp = GNUNET_DISK_mktemp ("friends");
2795 GNUNET_assert (mytemp != NULL);
2796 temp_friend_handle = fopen (mytemp, "wt");
2797 GNUNET_assert (temp_friend_handle != NULL);
2799 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2800 while (conn_iter != NULL)
2802 GNUNET_CRYPTO_hash_to_enc (
2803 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2805 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2806 conn_iter = conn_iter->next;
2809 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2810 &friend_file_iterator,
2811 temp_friend_handle);
2813 fclose (temp_friend_handle);
2816 != GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2818 "PATHS", "SERVICEHOME",
2819 &temp_service_path))
2821 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2822 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2823 "SERVICEHOME", "PATHS");
2824 if (UNLINK (mytemp) != 0)
2825 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
2828 GNUNET_free (mytemp);
2832 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2834 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2835 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
2837 GNUNET_assert(procarr[pg_iter] != NULL);
2839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2840 "Copying file with command cp %s %s\n",
2844 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2845 GNUNET_OS_process_close (procarr[pg_iter]);
2848 else /* Remote, scp the file to the correct place */
2850 if (NULL != pg->peers[pg_iter].daemon->username)
2851 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2852 pg->peers[pg_iter].daemon->username,
2853 pg->peers[pg_iter].daemon->hostname,
2856 GNUNET_asprintf (&arg, "%s:%s/friends",
2857 pg->peers[pg_iter].daemon->hostname,
2859 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
2861 GNUNET_assert(procarr[pg_iter] != NULL);
2862 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2863 GNUNET_OS_process_close (procarr[pg_iter]);
2864 if (ret != GNUNET_OK)
2866 /* FIXME: free contents of 'procarr' array */
2867 GNUNET_free (procarr);
2868 GNUNET_free (temp_service_path);
2869 GNUNET_free (mytemp);
2873 procarr[pg_iter] = NULL;
2875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2876 "Copying file with command scp %s %s\n",
2882 GNUNET_free (temp_service_path);
2883 GNUNET_free (mytemp);
2888 ret = GNUNET_SYSERR;
2889 while ((count < max_wait) && (ret != GNUNET_OK))
2892 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2896 "Checking copy status of file %d\n",
2899 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2901 if (GNUNET_OS_process_status
2902 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2904 ret = GNUNET_SYSERR;
2906 else if ((type != GNUNET_OS_PROCESS_EXITED)
2907 || (return_code != 0))
2909 ret = GNUNET_SYSERR;
2913 GNUNET_OS_process_close (procarr[pg_iter]);
2914 procarr[pg_iter] = NULL;
2916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2924 if (ret == GNUNET_SYSERR)
2926 /* FIXME: why sleep here? -CG */
2932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2933 _("Finished copying all friend files!\n"));
2936 GNUNET_free (procarr);
2941 * Create the blacklist files based on the PeerConnection's
2942 * of each peer in the peer group, and copy the files
2943 * to the appropriate place.
2945 * @param pg the peer group we are dealing with
2946 * @param transports space delimited list of transports to blacklist
2949 create_and_copy_blacklist_files(struct GNUNET_TESTING_PeerGroup *pg,
2950 const char *transports)
2952 FILE *temp_file_handle;
2953 unsigned int pg_iter;
2954 char *temp_service_path;
2955 struct GNUNET_OS_Process **procarr;
2958 enum GNUNET_OS_ProcessStatusType type;
2959 unsigned long return_code;
2966 char *temp_transports;
2968 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2969 struct PeerConnection *conn_iter;
2971 static struct BlacklistContext blacklist_ctx;
2974 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2975 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2977 mytemp = GNUNET_DISK_mktemp ("blacklist");
2978 GNUNET_assert (mytemp != NULL);
2979 temp_file_handle = fopen (mytemp, "wt");
2980 GNUNET_assert (temp_file_handle != NULL);
2981 temp_transports = GNUNET_strdup (transports);
2983 blacklist_ctx.temp_file_handle = temp_file_handle;
2985 transport_len = strlen (temp_transports) + 1;
2988 for (i = 0; i < transport_len; i++)
2990 if ((temp_transports[i] == ' ') && (pos == NULL))
2991 continue; /* At start of string (whitespace) */
2992 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2994 temp_transports[i] = '\0';
2996 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
2997 while (conn_iter != NULL)
2999 GNUNET_CRYPTO_hash_to_enc (
3000 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
3002 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
3003 conn_iter = conn_iter->next;
3006 blacklist_ctx.transport = pos;
3007 (void) GNUNET_CONTAINER_multihashmap_iterate (pg->
3009 [pg_iter].blacklisted_peers,
3010 &blacklist_file_iterator,
3014 } /* At beginning of actual string */
3015 else if (pos == NULL)
3017 pos = &temp_transports[i];
3021 GNUNET_free (temp_transports);
3022 fclose (temp_file_handle);
3025 != GNUNET_CONFIGURATION_get_value_string (
3026 pg->peers[pg_iter]. daemon->cfg,
3027 "PATHS", "SERVICEHOME",
3028 &temp_service_path))
3031 GNUNET_ERROR_TYPE_WARNING,
3033 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
3034 "SERVICEHOME", "PATHS");
3035 if (UNLINK (mytemp) != 0)
3036 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
3038 GNUNET_free (mytemp);
3042 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
3044 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
3045 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
3048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3049 _("Copying file with command cp %s %s\n"), mytemp, arg);
3054 else /* Remote, scp the file to the correct place */
3056 if (NULL != pg->peers[pg_iter].daemon->username)
3057 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
3058 pg->peers[pg_iter].daemon->username,
3059 pg->peers[pg_iter].daemon->hostname,
3062 GNUNET_asprintf (&arg, "%s:%s/blacklist",
3063 pg->peers[pg_iter].daemon->hostname,
3065 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
3067 GNUNET_assert(procarr[pg_iter] != NULL);
3068 GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
3071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3072 _("Copying file with command scp %s %s\n"), mytemp,
3077 GNUNET_free (temp_service_path);
3078 GNUNET_free (mytemp);
3082 ret = GNUNET_SYSERR;
3083 while ((count < max_wait) && (ret != GNUNET_OK))
3086 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3090 _("Checking copy status of file %d\n"), pg_iter);
3092 if (procarr[pg_iter] != NULL) /* Check for already completed! */
3094 if (GNUNET_OS_process_status (procarr[pg_iter], &type,
3095 &return_code) != GNUNET_OK)
3097 ret = GNUNET_SYSERR;
3099 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
3101 ret = GNUNET_SYSERR;
3105 GNUNET_OS_process_close (procarr[pg_iter]);
3106 procarr[pg_iter] = NULL;
3108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3109 _("File %d copied\n"), pg_iter);
3115 if (ret == GNUNET_SYSERR)
3117 /* FIXME: why sleep here? -CG */
3123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3124 _("Finished copying all blacklist files!\n"));
3126 GNUNET_free (procarr);
3130 /* Forward Declaration */
3132 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3135 * Choose a random peer's next connection to create, and
3136 * call schedule_connect to set up the connect task.
3138 * @param ct_ctx the overall connection context
3141 preschedule_connect(struct GNUNET_TESTING_PeerGroup *pg)
3143 struct ConnectTopologyContext *ct_ctx = &pg->ct_ctx;
3144 struct PeerConnection *connection_iter;
3145 struct ConnectContext *connect_context;
3146 uint32_t random_peer;
3148 if (ct_ctx->remaining_connections == 0)
3151 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3152 while (pg->peers[random_peer].connect_peers_head == NULL)
3153 random_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3156 connection_iter = pg->peers[random_peer].connect_peers_head;
3157 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3158 connect_context->first_index = random_peer;
3159 connect_context->second_index = connection_iter->index;
3160 connect_context->ct_ctx = ct_ctx;
3161 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3162 GNUNET_CONTAINER_DLL_remove(pg->peers[random_peer].connect_peers_head, pg->peers[random_peer].connect_peers_tail, connection_iter);
3163 GNUNET_free(connection_iter);
3164 ct_ctx->remaining_connections--;
3168 /* Forward declaration */
3169 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3172 * Close connections and free the hello context.
3174 * @param cls the 'struct SendHelloContext *'
3175 * @param tc scheduler context
3178 free_hello_context (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3180 struct SendHelloContext *send_hello_context = cls;
3181 if (send_hello_context->peer->daemon->server != NULL)
3183 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3184 send_hello_context->peer->daemon->server = NULL;
3186 if (send_hello_context->peer->daemon->th != NULL)
3188 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3189 send_hello_context->peer->daemon->th = NULL;
3191 if (send_hello_context->core_connect_task != GNUNET_SCHEDULER_NO_TASK)
3193 GNUNET_SCHEDULER_cancel(send_hello_context->core_connect_task);
3194 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3196 send_hello_context->pg->outstanding_connects--;
3197 GNUNET_free(send_hello_context);
3201 * For peers that haven't yet connected, notify
3202 * the caller that they have failed (timeout).
3204 * @param cls the 'struct SendHelloContext *'
3205 * @param tc scheduler context
3208 notify_remaining_connections_failed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3210 struct SendHelloContext *send_hello_context = cls;
3211 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3212 struct PeerConnection *connection;
3214 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3215 send_hello_context->peer->daemon->server = NULL;
3217 connection = send_hello_context->peer->connect_peers_head;
3219 while (connection != NULL)
3221 if (pg->notify_connection != NULL)
3223 pg->notify_connection(pg->notify_connection_cls,
3224 &send_hello_context->peer->daemon->id,
3225 &pg->peers[connection->index].daemon->id,
3227 send_hello_context->peer->daemon->cfg,
3228 pg->peers[connection->index].daemon->cfg,
3229 send_hello_context->peer->daemon,
3230 pg->peers[connection->index].daemon,
3231 "Peers failed to connect (timeout)");
3233 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3234 GNUNET_free(connection);
3235 connection = connection->next;
3237 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3239 other_peer = &pg->peers[connection->index];
3244 * For peers that haven't yet connected, send
3245 * CORE connect requests.
3247 * @param cls the 'struct SendHelloContext *'
3248 * @param tc scheduler context
3251 send_core_connect_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3253 struct SendHelloContext *send_hello_context = cls;
3254 struct PeerConnection *conn;
3255 GNUNET_assert(send_hello_context->peer->daemon->server != NULL);
3257 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3259 send_hello_context->connect_attempts++;
3260 if (send_hello_context->connect_attempts < send_hello_context->pg->ct_ctx.connect_attempts)
3262 conn = send_hello_context->peer->connect_peers_head;
3263 while (conn != NULL)
3265 GNUNET_CORE_peer_request_connect(send_hello_context->peer->daemon->server,
3266 &send_hello_context->pg->peers[conn->index].daemon->id,
3271 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) ,
3272 &send_core_connect_requests,
3273 send_hello_context);
3277 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Timeout before all connections created, marking rest as failed!\n");
3278 GNUNET_SCHEDULER_add_now(¬ify_remaining_connections_failed, send_hello_context);
3284 * Success, connection is up. Signal client our success.
3286 * @param cls our "struct SendHelloContext"
3287 * @param peer identity of the peer that has connected
3288 * @param atsi performance information
3290 * FIXME: remove peers from BOTH lists, call notify twice, should
3291 * double the speed of connections as long as the list iteration
3292 * doesn't take too long!
3295 core_connect_notify (void *cls,
3296 const struct GNUNET_PeerIdentity *peer,
3297 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3299 struct SendHelloContext *send_hello_context = cls;
3300 struct PeerConnection *connection;
3301 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3303 struct PeerData *other_peer;
3306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3307 "Connected peer %s to peer %s\n",
3308 ctx->d1->shortname, GNUNET_i2s(peer));
3311 if (0 == memcmp(&send_hello_context->peer->daemon->id, peer, sizeof(struct GNUNET_PeerIdentity)))
3314 connection = send_hello_context->peer->connect_peers_head;
3319 while ((connection != NULL) &&
3320 (0 != memcmp(&pg->peers[connection->index].daemon->id, peer, sizeof(struct GNUNET_PeerIdentity))))
3322 connection = connection->next;
3325 if (connection == NULL)
3327 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);
3332 other_peer = &pg->peers[connection->index];
3334 if (pg->notify_connection != NULL)
3336 pg->notify_connection(pg->notify_connection_cls,
3337 &send_hello_context->peer->daemon->id,
3340 send_hello_context->peer->daemon->cfg,
3341 pg->peers[connection->index].daemon->cfg,
3342 send_hello_context->peer->daemon,
3343 pg->peers[connection->index].daemon,
3346 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3347 GNUNET_free(connection);
3351 /* Notify of reverse connection and remove from other peers list of outstanding */
3352 if (other_peer != NULL)
3354 connection = other_peer->connect_peers_head;
3355 while ((connection != NULL) &&
3356 (0 != memcmp(&send_hello_context->peer->daemon->id, &pg->peers[connection->index].daemon->id, sizeof(struct GNUNET_PeerIdentity))))
3358 connection = connection->next;
3360 if (connection != NULL)
3362 if (pg->notify_connection != NULL)
3364 pg->notify_connection(pg->notify_connection_cls,
3366 &send_hello_context->peer->daemon->id,
3368 pg->peers[connection->index].daemon->cfg,
3369 send_hello_context->peer->daemon->cfg,
3370 pg->peers[connection->index].daemon,
3371 send_hello_context->peer->daemon,
3375 GNUNET_CONTAINER_DLL_remove(other_peer->connect_peers_head, other_peer->connect_peers_tail, connection);
3376 GNUNET_free(connection);
3381 if (send_hello_context->peer->connect_peers_head == NULL)
3383 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3388 * Notify of a successful connection to the core service.
3390 * @param cls a struct SendHelloContext *
3391 * @param server handle to the core service
3392 * @param my_identity the peer identity of this peer
3393 * @param publicKey the public key of the peer
3396 core_init (void *cls,
3397 struct GNUNET_CORE_Handle * server,
3398 const struct GNUNET_PeerIdentity *
3401 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
3404 struct SendHelloContext *send_hello_context = cls;
3405 send_hello_context->core_ready = GNUNET_YES;
3409 * Function called once a hello has been sent
3410 * to the transport, move on to the next one
3411 * or go away forever.
3413 * @param cls the 'struct SendHelloContext *'
3414 * @param tc scheduler context
3417 hello_sent_callback (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3419 struct SendHelloContext *send_hello_context = cls;
3420 //unsigned int pg_iter;
3421 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3423 GNUNET_free(send_hello_context);
3427 send_hello_context->pg->remaining_hellos--;
3429 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sent HELLO, have %d remaining!\n", send_hello_context->pg->remaining_hellos);
3431 if (send_hello_context->peer_pos == NULL) /* All HELLOs (for this peer!) have been transmitted! */
3434 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All hellos for this peer sent, disconnecting transport!\n");
3436 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3437 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3438 send_hello_context->peer->daemon->th = NULL;
3440 /*if (send_hello_context->pg->remaining_hellos == 0)
3442 for (pg_iter = 0; pg_iter < send_hello_context->pg->max_outstanding_connections; pg_iter++)
3444 preschedule_connect(&send_hello_context->pg->ct_ctx);
3448 GNUNET_assert (send_hello_context->peer->daemon->server == NULL);
3449 send_hello_context->peer->daemon->server = GNUNET_CORE_connect(send_hello_context->peer->cfg,
3453 &core_connect_notify,
3460 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),
3461 &send_core_connect_requests,
3462 send_hello_context);
3465 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3469 * Connect to a peer, give it all the HELLO's of those peers
3470 * we will later ask it to connect to.
3472 * @param ct_ctx the overall connection context
3474 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3476 struct SendHelloContext *send_hello_context = cls;
3477 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3479 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3481 GNUNET_free(send_hello_context);
3485 GNUNET_assert(send_hello_context->peer_pos != NULL); /* All of the HELLO sends to be scheduled have been scheduled! */
3487 if (((send_hello_context->peer->daemon->th == NULL) &&
3488 (pg->outstanding_connects > pg->max_outstanding_connections)) ||
3489 (pg->stop_connects == GNUNET_YES))
3491 #if VERBOSE_TESTING > 2
3492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3494 ("Delaying connect, we have too many outstanding connections!\n"));
3496 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3497 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3498 &schedule_send_hellos, send_hello_context);
3502 #if VERBOSE_TESTING > 2
3503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3504 _("Creating connection, outstanding_connections is %d\n"),
3505 outstanding_connects);
3507 if (send_hello_context->peer->daemon->th == NULL)
3509 pg->outstanding_connects++; /* Actual TRANSPORT, CORE connections! */
3510 send_hello_context->peer->daemon->th
3511 = GNUNET_TRANSPORT_connect (send_hello_context->peer->cfg, NULL,
3512 send_hello_context, NULL, NULL, NULL);
3515 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3516 _("Offering Hello of peer %s to peer %s\n"),
3517 send_hello_context->peer->daemon->shortname, pg->peers[send_hello_context->peer_pos->index].daemon->shortname);
3519 GNUNET_TRANSPORT_offer_hello(send_hello_context->peer->daemon->th,
3520 (const struct GNUNET_MessageHeader *)pg->peers[send_hello_context->peer_pos->index].daemon->hello,
3521 &hello_sent_callback,
3522 send_hello_context);
3523 send_hello_context->peer_pos = send_hello_context->peer_pos->next;
3524 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3530 * Internal notification of a connection, kept so that we can ensure some connections
3531 * happen instead of flooding all testing daemons with requests to connect.
3534 internal_connect_notify(void *cls, const struct GNUNET_PeerIdentity *first,
3535 const struct GNUNET_PeerIdentity *second,
3537 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
3538 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
3539 struct GNUNET_TESTING_Daemon *first_daemon,
3540 struct GNUNET_TESTING_Daemon *second_daemon,
3543 struct ConnectContext *connect_ctx = cls;
3544 struct ConnectTopologyContext *ct_ctx = connect_ctx->ct_ctx;
3545 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
3546 struct PeerConnection *connection;
3548 GNUNET_assert (0 < pg->outstanding_connects);
3549 pg->outstanding_connects--;
3552 * Check whether the inverse connection has been scheduled yet,
3553 * if not, we can remove it from the other peers list and avoid
3554 * even trying to connect them again!
3556 connection = pg->peers[connect_ctx->second_index].connect_peers_head;
3561 while ((connection != NULL) && (0
3562 != memcmp (first, &pg->peers[connection->index].daemon->id,
3563 sizeof(struct GNUNET_PeerIdentity))))
3565 connection = connection->next;
3568 if (connection != NULL) /* Can safely remove! */
3570 GNUNET_assert (0 < ct_ctx->remaining_connections);
3571 ct_ctx->remaining_connections--;
3572 if (pg->notify_connection != NULL) /* Notify of reverse connection */
3573 pg->notify_connection (pg->notify_connection_cls, second, first,
3574 distance, second_cfg, first_cfg, second_daemon,
3575 first_daemon, emsg);
3577 GNUNET_CONTAINER_DLL_remove(pg->peers[connect_ctx->second_index].connect_peers_head, pg->peers[connect_ctx->second_index].connect_peers_tail, connection);
3578 GNUNET_free(connection);
3581 if (ct_ctx->remaining_connections == 0)
3583 if (ct_ctx->notify_connections_done != NULL)
3585 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
3586 ct_ctx->notify_connections_done = NULL;
3590 preschedule_connect (pg);
3592 if (pg->notify_connection != NULL)
3593 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
3594 first_cfg, second_cfg, first_daemon, second_daemon,
3597 GNUNET_free(connect_ctx);
3601 * Either delay a connection (because there are too many outstanding)
3602 * or schedule it for right now.
3604 * @param cls a connection context
3605 * @param tc the task runtime context
3608 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3610 struct ConnectContext *connect_context = cls;
3611 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
3613 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3616 if ((pg->outstanding_connects > pg->max_outstanding_connections)
3617 || (pg->stop_connects == GNUNET_YES))
3620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3622 ("Delaying connect, we have too many outstanding connections!\n"));
3624 GNUNET_SCHEDULER_add_delayed (
3625 GNUNET_TIME_relative_multiply (
3626 GNUNET_TIME_UNIT_MILLISECONDS,
3628 &schedule_connect, connect_context);
3633 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3634 _("Creating connection, outstanding_connections is %d (max %d)\n"),
3635 pg->outstanding_connects, pg->max_outstanding_connections);
3637 pg->outstanding_connects++;
3638 pg->total_connects_scheduled++;
3639 GNUNET_TESTING_daemons_connect (
3640 pg->peers[connect_context->first_index].daemon,
3641 pg->peers[connect_context->second_index].daemon,
3642 connect_context->ct_ctx->connect_timeout,
3643 connect_context->ct_ctx->connect_attempts,
3649 &internal_connect_notify, connect_context); /* FIXME: free connect context! */
3655 * Iterator for actually scheduling connections to be created
3656 * between two peers.
3658 * @param cls closure, a GNUNET_TESTING_Daemon
3659 * @param key the key the second Daemon was stored under
3660 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3662 * @return GNUNET_YES to continue iteration
3665 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3667 struct ConnectTopologyContext *ct_ctx = cls;
3668 struct PeerData *first = ct_ctx->first;
3669 struct GNUNET_TESTING_Daemon *second = value;
3670 struct ConnectContext *connect_context;
3672 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3673 connect_context->first = first->daemon;
3674 connect_context->second = second;
3675 connect_context->ct_ctx = ct_ctx;
3676 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3684 * Iterator for copying all entries in the allowed hashmap to the
3687 * @param cls closure, a GNUNET_TESTING_Daemon
3688 * @param key the key the second Daemon was stored under
3689 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3691 * @return GNUNET_YES to continue iteration
3694 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3696 struct PeerData *first = cls;
3698 GNUNET_assert (GNUNET_OK ==
3699 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
3701 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3708 * Make the peers to connect the same as those that are allowed to be
3711 * @param pg the peer group
3714 copy_allowed_topology(struct GNUNET_TESTING_PeerGroup *pg)
3716 unsigned int pg_iter;
3720 struct PeerConnection *iter;
3724 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3727 iter = pg->peers[pg_iter].allowed_peers_head;
3728 while (iter != NULL)
3730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3731 "Creating connection between %d and %d\n", pg_iter,
3733 total += add_connections (pg, pg_iter, iter->index, CONNECT,
3735 //total += add_actual_connections(pg, pg_iter, iter->index);
3740 GNUNET_CONTAINER_multihashmap_iterate (pg->
3741 peers[pg_iter].allowed_peers,
3742 ©_topology_iterator,
3743 &pg->peers[pg_iter]);
3745 if (GNUNET_SYSERR == ret)
3746 return GNUNET_SYSERR;
3748 total = total + ret;
3755 * Connect the topology as specified by the PeerConnection's
3756 * of each peer in the peer group
3758 * @param pg the peer group we are dealing with
3759 * @param connect_timeout how long try connecting two peers
3760 * @param connect_attempts how many times (max) to attempt
3761 * @param notify_callback callback to notify when finished
3762 * @param notify_cls closure for notify callback
3764 * @return the number of connections that will be attempted
3767 connect_topology(struct GNUNET_TESTING_PeerGroup *pg,
3768 struct GNUNET_TIME_Relative connect_timeout,
3769 unsigned int connect_attempts,
3770 GNUNET_TESTING_NotifyCompletion notify_callback,
3773 unsigned int pg_iter;
3777 struct PeerConnection *connection_iter;
3780 struct SendHelloContext *send_hello_context;
3784 pg->ct_ctx.notify_connections_done = notify_callback;
3785 pg->ct_ctx.notify_cls = notify_cls;
3788 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3791 connection_iter = pg->peers[pg_iter].connect_peers_head;
3792 while (connection_iter != NULL)
3794 connection_iter = connection_iter->next;
3799 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3806 pg->ct_ctx.connect_timeout = connect_timeout;
3807 pg->ct_ctx.connect_attempts = connect_attempts;
3808 pg->ct_ctx.remaining_connections = total;
3811 /* First give all peers the HELLO's of other peers (connect to first peer's transport service, give HELLO's of other peers, continue...) */
3812 pg->remaining_hellos = total;
3813 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3815 send_hello_context = GNUNET_malloc(sizeof(struct SendHelloContext));
3816 send_hello_context->peer = &pg->peers[pg_iter];
3817 send_hello_context->peer_pos = pg->peers[pg_iter].connect_peers_head;
3818 send_hello_context->pg = pg;
3819 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3822 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3824 preschedule_connect (pg);
3832 * Takes a peer group and creates a topology based on the
3833 * one specified. Creates a topology means generates friend
3834 * files for the peers so they can only connect to those allowed
3835 * by the topology. This will only have an effect once peers
3836 * are started if the FRIENDS_ONLY option is set in the base
3837 * config. Also takes an optional restrict topology which
3838 * disallows connections based on particular transports
3839 * UNLESS they are specified in the restricted topology.
3841 * @param pg the peer group struct representing the running peers
3842 * @param topology which topology to connect the peers in
3843 * @param restrict_topology disallow restrict_transports transport
3844 * connections to peers NOT in this topology
3845 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3846 * @param restrict_transports space delimited list of transports to blacklist
3847 * to create restricted topology
3849 * @return the maximum number of connections were all allowed peers
3850 * connected to each other
3853 GNUNET_TESTING_create_topology(struct GNUNET_TESTING_PeerGroup *pg,
3854 enum GNUNET_TESTING_Topology topology,
3855 enum GNUNET_TESTING_Topology restrict_topology,
3856 const char *restrict_transports)
3860 unsigned int num_connections;
3861 int unblacklisted_connections;
3863 struct PeerConnection *conn_iter;
3864 struct PeerConnection *temp_conn;
3869 for (i = 0; i < pg->total; i++)
3871 pg->peers[i].allowed_peers =
3872 GNUNET_CONTAINER_multihashmap_create (100);
3873 pg->peers[i].connect_peers =
3874 GNUNET_CONTAINER_multihashmap_create (100);
3875 pg->peers[i].blacklisted_peers =
3876 GNUNET_CONTAINER_multihashmap_create (100);
3877 pg->peers[i].pg = pg;
3883 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3887 num_connections = create_clique (pg, &add_connections, ALLOWED, GNUNET_NO);
3889 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3892 _("Creating small world (ring) topology\n"));
3894 num_connections = create_small_world_ring (pg, &add_connections, ALLOWED);
3896 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3899 _("Creating small world (2d-torus) topology\n"));
3901 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3903 case GNUNET_TESTING_TOPOLOGY_RING:
3905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3907 num_connections = create_ring (pg, &add_connections, ALLOWED);
3909 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3913 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3915 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3918 _("Creating Erdos-Renyi topology\n"));
3920 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3922 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3926 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3928 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3931 _("Creating Scale Free topology\n"));
3933 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3935 case GNUNET_TESTING_TOPOLOGY_LINE:
3937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3938 _("Creating straight line topology\n"));
3940 num_connections = create_line (pg, &add_connections, ALLOWED);
3942 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3945 _("Creating topology from file!\n"));
3947 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing",
3950 num_connections = create_from_file (pg, filename, &add_connections,
3955 GNUNET_ERROR_TYPE_WARNING,
3956 "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3957 num_connections = 0;
3960 case GNUNET_TESTING_TOPOLOGY_NONE:
3962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3964 ("Creating no allowed topology (all peers can connect at core level)\n"));
3966 num_connections = pg->total * pg->total; /* Clique is allowed! */
3969 num_connections = 0;
3973 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING",
3976 ret = create_and_copy_friend_files (pg);
3977 if (ret != GNUNET_OK)
3980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3981 _("Failed during friend file copying!\n"));
3983 return GNUNET_SYSERR;
3988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3989 _("Friend files created/copied successfully!\n"));
3994 /* Use the create clique method to initially set all connections as blacklisted. */
3995 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology
3996 != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
3997 create_clique (pg, &add_connections, BLACKLIST, GNUNET_NO);
3999 return num_connections;
4001 unblacklisted_connections = 0;
4002 /* Un-blacklist connections as per the topology specified */
4003 switch (restrict_topology)
4005 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4008 _("Blacklisting all but clique topology\n"));
4010 unblacklisted_connections = create_clique (pg, &remove_connections,
4011 BLACKLIST, GNUNET_NO);
4013 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4016 _("Blacklisting all but small world (ring) topology\n"));
4018 unblacklisted_connections = create_small_world_ring (pg,
4019 &remove_connections,
4022 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4026 ("Blacklisting all but small world (2d-torus) topology\n"));
4028 unblacklisted_connections = create_small_world (pg, &remove_connections,
4031 case GNUNET_TESTING_TOPOLOGY_RING:
4033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4034 _("Blacklisting all but ring topology\n"));
4036 unblacklisted_connections
4037 = create_ring (pg, &remove_connections, BLACKLIST);
4039 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4042 _("Blacklisting all but 2d torus topology\n"));
4044 unblacklisted_connections = create_2d_torus (pg, &remove_connections,
4047 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4050 _("Blacklisting all but Erdos-Renyi topology\n"));
4052 unblacklisted_connections = create_erdos_renyi (pg, &remove_connections,
4055 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4058 _("Blacklisting all but InterNAT topology\n"));
4062 for (off = 0; off < pg->total; off++)
4064 conn_iter = pg->peers[off].allowed_peers_head;
4065 while (conn_iter != NULL)
4067 temp_conn = conn_iter->next;
4068 GNUNET_free(conn_iter);
4069 conn_iter = temp_conn;
4071 pg->peers[off].allowed_peers_head = NULL;
4072 pg->peers[off].allowed_peers_tail = NULL;
4074 conn_iter = pg->peers[off].connect_peers_head;
4075 while (conn_iter != NULL)
4077 temp_conn = conn_iter->next;
4078 GNUNET_free(conn_iter);
4079 conn_iter = temp_conn;
4081 pg->peers[off].connect_peers_head = NULL;
4082 pg->peers[off].connect_peers_tail = NULL;
4084 unblacklisted_connections
4085 = create_nated_internet_copy (pg, &remove_connections, BLACKLIST);
4087 unblacklisted_connections =
4088 create_nated_internet (pg, &remove_connections, BLACKLIST);
4092 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4095 _("Blacklisting all but Scale Free topology\n"));
4097 unblacklisted_connections = create_scale_free (pg, &remove_connections,
4100 case GNUNET_TESTING_TOPOLOGY_LINE:
4102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4103 _("Blacklisting all but straight line topology\n"));
4105 unblacklisted_connections
4106 = create_line (pg, &remove_connections, BLACKLIST);
4111 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
4113 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n",
4114 restrict_transports);
4115 ret = create_and_copy_blacklist_files (pg, restrict_transports);
4116 if (ret != GNUNET_OK)
4119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4120 _("Failed during blacklist file copying!\n"));
4127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4128 _("Blacklist files created/copied successfully!\n"));
4132 return num_connections;
4137 * Iterator for choosing random peers to connect.
4139 * @param cls closure, a RandomContext
4140 * @param key the key the second Daemon was stored under
4141 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4143 * @return GNUNET_YES to continue iteration
4146 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4148 struct RandomContext *random_ctx = cls;
4149 double random_number;
4150 uint32_t second_pos;
4151 GNUNET_HashCode first_hash;
4154 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4155 UINT64_MAX)) / ((double) UINT64_MAX);
4156 if (random_number < random_ctx->percentage)
4158 GNUNET_assert (GNUNET_OK ==
4159 GNUNET_CONTAINER_multihashmap_put (random_ctx->
4160 first->connect_peers_working_set,
4162 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4165 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
4166 uid_from_hash (key, &second_pos);
4167 hash_from_uid (random_ctx->first_uid, &first_hash);
4168 GNUNET_assert (random_ctx->pg->total > second_pos);
4169 GNUNET_assert (GNUNET_YES ==
4170 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
4172 [second_pos].connect_peers,
4181 * Iterator for adding at least X peers to a peers connection set.
4183 * @param cls closure, MinimumContext
4184 * @param key the key the second Daemon was stored under
4185 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4187 * @return GNUNET_YES to continue iteration
4190 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4192 struct MinimumContext *min_ctx = cls;
4193 uint32_t second_pos;
4194 GNUNET_HashCode first_hash;
4197 if (GNUNET_CONTAINER_multihashmap_size
4198 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
4200 for (i = 0; i < min_ctx->num_to_add; i++)
4202 if (min_ctx->pg_array[i] == min_ctx->current)
4204 GNUNET_assert (GNUNET_OK ==
4205 GNUNET_CONTAINER_multihashmap_put
4206 (min_ctx->first->connect_peers_working_set, key,
4208 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4209 uid_from_hash (key, &second_pos);
4210 hash_from_uid (min_ctx->first_uid, &first_hash);
4211 GNUNET_assert (min_ctx->pg->total > second_pos);
4212 GNUNET_assert (GNUNET_OK ==
4213 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4215 [second_pos].connect_peers_working_set,
4219 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4220 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
4221 GNUNET_assert (GNUNET_YES ==
4222 GNUNET_CONTAINER_multihashmap_remove
4223 (min_ctx->pg->peers[second_pos].connect_peers,
4224 &first_hash, min_ctx->first->daemon));
4231 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
4236 * Iterator for adding peers to a connection set based on a depth first search.
4238 * @param cls closure, MinimumContext
4239 * @param key the key the second daemon was stored under
4240 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4242 * @return GNUNET_YES to continue iteration
4245 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4247 struct DFSContext *dfs_ctx = cls;
4248 GNUNET_HashCode first_hash;
4250 if (dfs_ctx->current == dfs_ctx->chosen)
4252 GNUNET_assert (GNUNET_OK ==
4253 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4254 first->connect_peers_working_set,
4256 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4257 uid_from_hash (key, &dfs_ctx->second_uid);
4258 hash_from_uid (dfs_ctx->first_uid, &first_hash);
4259 GNUNET_assert (GNUNET_OK ==
4260 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4262 [dfs_ctx->second_uid].connect_peers_working_set,
4266 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4267 GNUNET_assert (GNUNET_YES ==
4268 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
4270 [dfs_ctx->second_uid].connect_peers,
4274 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
4275 return GNUNET_NO; /* We have found our peer, don't iterate more */
4284 * From the set of connections possible, choose percentage percent of connections
4285 * to actually connect.
4287 * @param pg the peergroup we are dealing with
4288 * @param percentage what percent of total connections to make
4291 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg,
4296 struct PeerConnection *conn_iter;
4297 double random_number;
4299 struct RandomContext random_ctx;
4302 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4305 conn_iter = pg->peers[pg_iter].connect_peers_head;
4306 while (conn_iter != NULL)
4309 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4311 / ((double) UINT64_MAX);
4312 if (random_number < percentage)
4314 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4317 conn_iter = conn_iter->next;
4320 random_ctx.first_uid = pg_iter;
4321 random_ctx.first = &pg->peers[pg_iter];
4322 random_ctx.percentage = percentage;
4324 pg->peers[pg_iter].connect_peers_working_set
4325 = GNUNET_CONTAINER_multihashmap_create (pg->total);
4326 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4327 &random_connect_iterator,
4329 /* Now remove the old connections */
4330 GNUNET_CONTAINER_multihashmap_destroy (pg->
4331 peers[pg_iter].connect_peers);
4332 /* And replace with the random set */
4333 pg->peers[pg_iter].connect_peers
4334 = pg->peers[pg_iter].connect_peers_working_set;
4338 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4340 conn_iter = pg->peers[pg_iter].connect_peers_head;
4341 while (pg->peers[pg_iter].connect_peers_head != NULL)
4342 remove_connections (pg, pg_iter,
4343 pg->peers[pg_iter].connect_peers_head->index,
4344 CONNECT, GNUNET_YES);
4346 pg->peers[pg_iter].connect_peers_head
4347 = pg->peers[pg_iter].connect_peers_working_set_head;
4348 pg->peers[pg_iter].connect_peers_tail
4349 = pg->peers[pg_iter].connect_peers_working_set_tail;
4350 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4351 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4356 * Count the number of connections in a linked list of connections.
4358 * @param conn_list the connection list to get the count of
4360 * @return the number of elements in the list
4363 count_connections(struct PeerConnection *conn_list)
4365 struct PeerConnection *iter;
4369 while (iter != NULL)
4378 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
4381 unsigned int pg_iter;
4383 struct PeerConnection *conn_iter;
4387 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4390 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
4391 while (conn_iter != NULL)
4394 conn_iter = conn_iter->next;
4398 GNUNET_CONTAINER_multihashmap_size (pg->
4400 [pg_iter].connect_peers_working_set);
4408 count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
4411 unsigned int pg_iter;
4413 struct PeerConnection *conn_iter;
4417 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4420 conn_iter = pg->peers[pg_iter].allowed_peers_head;
4421 while (conn_iter != NULL)
4424 conn_iter = conn_iter->next;
4428 GNUNET_CONTAINER_multihashmap_size (pg->
4430 [pg_iter].allowed_peers);
4438 * From the set of connections possible, choose at least num connections per
4441 * @param pg the peergroup we are dealing with
4442 * @param num how many connections at least should each peer have (if possible)?
4445 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4448 struct MinimumContext minimum_ctx;
4450 struct PeerConnection *conn_iter;
4451 unsigned int temp_list_size;
4454 uint32_t random; /* Random list entry to connect peer to */
4459 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4462 = count_connections (pg->peers[pg_iter].connect_peers_head);
4463 if (temp_list_size == 0)
4465 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4466 "Peer %d has 0 connections!?!?\n", pg_iter);
4469 for (i = 0; i < num; i++)
4471 random = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4473 conn_iter = pg->peers[pg_iter].connect_peers_head;
4474 for (count = 0; count < random; count++)
4475 conn_iter = conn_iter->next;
4476 /* We now have a random connection, connect it! */
4477 GNUNET_assert(conn_iter != NULL);
4478 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4483 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4485 pg->peers[pg_iter].connect_peers_working_set =
4486 GNUNET_CONTAINER_multihashmap_create (num);
4489 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4491 minimum_ctx.first_uid = pg_iter;
4492 minimum_ctx.pg_array =
4493 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
4494 GNUNET_CONTAINER_multihashmap_size
4495 (pg->peers[pg_iter].connect_peers));
4496 minimum_ctx.first = &pg->peers[pg_iter];
4497 minimum_ctx.pg = pg;
4498 minimum_ctx.num_to_add = num;
4499 minimum_ctx.current = 0;
4500 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4501 &minimum_connect_iterator,
4505 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4507 /* Remove the "old" connections */
4508 GNUNET_CONTAINER_multihashmap_destroy (pg->
4509 peers[pg_iter].connect_peers);
4510 /* And replace with the working set */
4511 pg->peers[pg_iter].connect_peers =
4512 pg->peers[pg_iter].connect_peers_working_set;
4515 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4517 while (pg->peers[pg_iter].connect_peers_head != NULL)
4519 conn_iter = pg->peers[pg_iter].connect_peers_head;
4520 GNUNET_CONTAINER_DLL_remove(pg->peers[pg_iter].connect_peers_head,
4521 pg->peers[pg_iter].connect_peers_tail,
4523 GNUNET_free(conn_iter);
4524 /*remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT, GNUNET_YES);*/
4527 pg->peers[pg_iter].connect_peers_head
4528 = pg->peers[pg_iter].connect_peers_working_set_head;
4529 pg->peers[pg_iter].connect_peers_tail
4530 = pg->peers[pg_iter].connect_peers_working_set_tail;
4531 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4532 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4537 struct FindClosestContext
4540 * The currently known closest peer.
4542 struct GNUNET_TESTING_Daemon *closest;
4545 * The info for the peer we are adding connections for.
4547 struct PeerData *curr_peer;
4550 * The distance (bits) between the current
4551 * peer and the currently known closest.
4553 unsigned int closest_dist;
4556 * The offset of the closest known peer in
4559 unsigned int closest_num;
4563 * Iterator over hash map entries of the allowed
4564 * peer connections. Find the closest, not already
4565 * connected peer and return it.
4567 * @param cls closure (struct FindClosestContext)
4568 * @param key current key code (hash of offset in pg)
4569 * @param value value in the hash map - a GNUNET_TESTING_Daemon
4570 * @return GNUNET_YES if we should continue to
4575 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
4577 struct FindClosestContext *closest_ctx = cls;
4578 struct GNUNET_TESTING_Daemon *daemon = value;
4580 if (((closest_ctx->closest == NULL) ||
4581 (GNUNET_CRYPTO_hash_matching_bits
4582 (&daemon->id.hashPubKey,
4583 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
4584 closest_ctx->closest_dist))
4586 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
4587 curr_peer->connect_peers,
4590 closest_ctx->closest_dist =
4591 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
4592 &closest_ctx->curr_peer->daemon->
4594 closest_ctx->closest = daemon;
4595 uid_from_hash (key, &closest_ctx->closest_num);
4601 * From the set of connections possible, choose at num connections per
4602 * peer based on depth which are closest out of those allowed. Guaranteed
4603 * to add num peers to connect to, provided there are that many peers
4604 * in the underlay topology to connect to.
4606 * @param pg the peergroup we are dealing with
4607 * @param num how many connections at least should each peer have (if possible)?
4608 * @param proc processor to actually add the connections
4609 * @param list the peer list to use
4612 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
4613 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
4618 struct FindClosestContext closest_ctx;
4623 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
4625 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4627 closest_ctx.curr_peer = &pg->peers[pg_iter];
4628 closest_ctx.closest = NULL;
4629 closest_ctx.closest_dist = 0;
4630 closest_ctx.closest_num = 0;
4631 GNUNET_CONTAINER_multihashmap_iterate (pg->
4632 peers[pg_iter].allowed_peers,
4633 &find_closest_peers,
4635 if (closest_ctx.closest != NULL)
4637 GNUNET_assert (closest_ctx.closest_num < pg->total);
4638 proc (pg, pg_iter, closest_ctx.closest_num, list);
4646 * From the set of connections possible, choose at least num connections per
4647 * peer based on depth first traversal of peer connections. If DFS leaves
4648 * peers unconnected, ensure those peers get connections.
4650 * @param pg the peergroup we are dealing with
4651 * @param num how many connections at least should each peer have (if possible)?
4654 perform_dfs(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4658 uint32_t starting_peer;
4659 uint32_t least_connections;
4660 uint32_t random_connection;
4662 unsigned int temp_count;
4663 struct PeerConnection *peer_iter;
4665 struct DFSContext dfs_ctx;
4666 GNUNET_HashCode second_hash;
4672 while ((count_workingset_connections (pg) < num * pg->total)
4673 && (count_allowed_connections (pg) > 0))
4675 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4677 least_connections = -1; /* Set to very high number */
4678 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4681 = count_connections (
4682 pg->peers[pg_iter].connect_peers_working_set_head);
4683 if (temp_count < least_connections)
4685 starting_peer = pg_iter;
4686 least_connections = temp_count;
4692 = count_connections (pg->peers[starting_peer].connect_peers_head);
4693 if (temp_count == 0)
4694 continue; /* FIXME: infinite loop? */
4696 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4699 peer_iter = pg->peers[starting_peer].connect_peers_head;
4700 while (temp_count < random_connection)
4702 peer_iter = peer_iter->next;
4705 GNUNET_assert(peer_iter != NULL);
4706 add_connections (pg, starting_peer, peer_iter->index, WORKING_SET,
4708 remove_connections (pg, starting_peer, peer_iter->index, CONNECT,
4710 starting_peer = peer_iter->index;
4715 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4717 pg->peers[pg_iter].connect_peers_working_set =
4718 GNUNET_CONTAINER_multihashmap_create (num);
4723 while ((count_workingset_connections (pg) < num * pg->total)
4724 && (count_allowed_connections (pg) > 0))
4726 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4728 least_connections = -1; /* Set to very high number */
4729 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4731 if (GNUNET_CONTAINER_multihashmap_size
4732 (pg->peers[pg_iter].connect_peers_working_set) <
4735 starting_peer = pg_iter;
4737 GNUNET_CONTAINER_multihashmap_size (pg->
4739 [pg_iter].connect_peers_working_set);
4744 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4750 /* Choose a random peer from the chosen peers set of connections to add */
4752 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4753 GNUNET_CONTAINER_multihashmap_size
4754 (pg->peers[starting_peer].connect_peers));
4755 dfs_ctx.first_uid = starting_peer;
4756 dfs_ctx.first = &pg->peers[starting_peer];
4758 dfs_ctx.current = 0;
4760 GNUNET_CONTAINER_multihashmap_iterate (pg->
4762 [starting_peer].connect_peers,
4763 &dfs_connect_iterator, &dfs_ctx);
4764 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4765 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4766 GNUNET_assert (GNUNET_YES ==
4767 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4768 [starting_peer].connect_peers,
4772 [dfs_ctx.second_uid].daemon));
4773 starting_peer = dfs_ctx.second_uid;
4776 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4778 /* Remove the "old" connections */
4779 GNUNET_CONTAINER_multihashmap_destroy (pg->
4780 peers[pg_iter].connect_peers);
4781 /* And replace with the working set */
4782 pg->peers[pg_iter].connect_peers =
4783 pg->peers[pg_iter].connect_peers_working_set;
4789 * Internal callback for topology information for a particular peer.
4792 internal_topology_callback(void *cls, const struct GNUNET_PeerIdentity *peer,
4793 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4795 struct CoreContext *core_ctx = cls;
4796 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4798 if (peer == NULL) /* Either finished, or something went wrong */
4800 iter_ctx->completed++;
4801 iter_ctx->connected--;
4802 /* One core context allocated per iteration, must free! */
4803 GNUNET_free (core_ctx);
4807 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id, peer, NULL);
4810 if (iter_ctx->completed == iter_ctx->total)
4812 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4813 /* Once all are done, free the iteration context */
4814 GNUNET_free (iter_ctx);
4819 * Check running topology iteration tasks, if below max start a new one, otherwise
4820 * schedule for some time in the future.
4823 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4825 struct CoreContext *core_context = cls;
4826 struct TopologyIterateContext *topology_context =
4827 (struct TopologyIterateContext *) core_context->iter_context;
4828 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
4831 if (topology_context->connected
4832 > topology_context->pg->max_outstanding_connections)
4834 #if VERBOSE_TESTING > 2
4835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4837 ("Delaying connect, we have too many outstanding connections!\n"));
4839 GNUNET_SCHEDULER_add_delayed (
4840 GNUNET_TIME_relative_multiply (
4841 GNUNET_TIME_UNIT_MILLISECONDS,
4843 &schedule_get_topology, core_context);
4847 #if VERBOSE_TESTING > 2
4848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4849 _("Creating connection, outstanding_connections is %d\n"),
4850 outstanding_connects);
4852 topology_context->connected++;
4854 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4855 &internal_topology_callback,
4858 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4859 internal_topology_callback (core_context, NULL, NULL);
4865 * Iterate over all (running) peers in the peer group, retrieve
4866 * all connections that each currently has.
4869 GNUNET_TESTING_get_topology(struct GNUNET_TESTING_PeerGroup *pg,
4870 GNUNET_TESTING_NotifyTopology cb, void *cls)
4872 struct TopologyIterateContext *topology_context;
4873 struct CoreContext *core_ctx;
4875 unsigned int total_count;
4877 /* Allocate a single topology iteration context */
4878 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4879 topology_context->topology_cb = cb;
4880 topology_context->cls = cls;
4881 topology_context->pg = pg;
4883 for (i = 0; i < pg->total; i++)
4885 if (pg->peers[i].daemon->running == GNUNET_YES)
4887 /* Allocate one core context per core we need to connect to */
4888 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4889 core_ctx->daemon = pg->peers[i].daemon;
4890 /* Set back pointer to topology iteration context */
4891 core_ctx->iter_context = topology_context;
4892 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4896 if (total_count == 0)
4898 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4899 GNUNET_free (topology_context);
4902 topology_context->total = total_count;
4907 * Callback function to process statistic values.
4908 * This handler is here only really to insert a peer
4909 * identity (or daemon) so the statistics can be uniquely
4910 * tied to a single running peer.
4912 * @param cls closure
4913 * @param subsystem name of subsystem that created the statistic
4914 * @param name the name of the datum
4915 * @param value the current value
4916 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4917 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4920 internal_stats_callback(void *cls, const char *subsystem, const char *name,
4921 uint64_t value, int is_persistent)
4923 struct StatsCoreContext *core_context = cls;
4924 struct StatsIterateContext *stats_context =
4925 (struct StatsIterateContext *) core_context->iter_context;
4927 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4928 subsystem, name, value, is_persistent);
4932 * Internal continuation call for statistics iteration.
4934 * @param cls closure, the CoreContext for this iteration
4935 * @param success whether or not the statistics iterations
4936 * was canceled or not (we don't care)
4939 internal_stats_cont(void *cls, int success)
4941 struct StatsCoreContext *core_context = cls;
4942 struct StatsIterateContext *stats_context =
4943 (struct StatsIterateContext *) core_context->iter_context;
4945 stats_context->connected--;
4946 stats_context->completed++;
4948 if (stats_context->completed == stats_context->total)
4950 stats_context->cont (stats_context->cls, GNUNET_YES);
4951 GNUNET_free (stats_context);
4954 if (core_context->stats_handle != NULL)
4955 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4957 GNUNET_free (core_context);
4961 * Check running topology iteration tasks, if below max start a new one, otherwise
4962 * schedule for some time in the future.
4965 schedule_get_statistics(void *cls,
4966 const struct GNUNET_SCHEDULER_TaskContext *tc)
4968 struct StatsCoreContext *core_context = cls;
4969 struct StatsIterateContext *stats_context =
4970 (struct StatsIterateContext *) core_context->iter_context;
4972 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
4975 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
4977 #if VERBOSE_TESTING > 2
4978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4980 ("Delaying connect, we have too many outstanding connections!\n"));
4982 GNUNET_SCHEDULER_add_delayed (
4983 GNUNET_TIME_relative_multiply (
4984 GNUNET_TIME_UNIT_MILLISECONDS,
4986 &schedule_get_statistics, core_context);
4990 #if VERBOSE_TESTING > 2
4991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4992 _("Creating connection, outstanding_connections is %d\n"),
4993 outstanding_connects);
4996 stats_context->connected++;
4997 core_context->stats_handle
4998 = GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
4999 if (core_context->stats_handle == NULL)
5001 internal_stats_cont (core_context, GNUNET_NO);
5005 core_context->stats_get_handle
5006 = GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
5007 GNUNET_TIME_relative_get_forever (),
5008 &internal_stats_cont,
5009 &internal_stats_callback, core_context);
5010 if (core_context->stats_get_handle == NULL)
5011 internal_stats_cont (core_context, GNUNET_NO);
5016 struct DuplicateStats
5019 * Next item in the list
5021 struct DuplicateStats *next;
5024 * Nasty string, concatenation of relevant information.
5026 char *unique_string;
5030 * Check whether the combination of port/host/unix domain socket
5031 * already exists in the list of peers being checked for statistics.
5033 * @param pg the peergroup in question
5034 * @param specific_peer the peer we're concerned with
5035 * @param stats_list the list to return to the caller
5037 * @return GNUNET_YES if the statistics instance has been seen already,
5038 * GNUNET_NO if not (and we may have added it to the list)
5041 stats_check_existing(struct GNUNET_TESTING_PeerGroup *pg,
5042 struct PeerData *specific_peer,
5043 struct DuplicateStats **stats_list)
5045 struct DuplicateStats *pos;
5046 char *unix_domain_socket;
5047 unsigned long long port;
5050 != GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
5051 "single_statistics_per_host"))
5052 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
5055 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg,
5058 &unix_domain_socket))
5061 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg,
5062 "statistics", "port",
5065 GNUNET_free(unix_domain_socket);
5069 if (specific_peer->daemon->hostname != NULL)
5070 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
5071 unix_domain_socket, port);
5073 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
5077 if (0 == strcmp (to_match, pos->unique_string))
5079 GNUNET_free (unix_domain_socket);
5080 GNUNET_free (to_match);
5085 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
5086 pos->unique_string = to_match;
5087 pos->next = *stats_list;
5089 GNUNET_free (unix_domain_socket);
5094 * Iterate over all (running) peers in the peer group, retrieve
5095 * all statistics from each.
5098 GNUNET_TESTING_get_statistics(struct GNUNET_TESTING_PeerGroup *pg,
5099 GNUNET_STATISTICS_Callback cont,
5100 GNUNET_TESTING_STATISTICS_Iterator proc,
5103 struct StatsIterateContext *stats_context;
5104 struct StatsCoreContext *core_ctx;
5106 unsigned int total_count;
5107 struct DuplicateStats *stats_list;
5108 struct DuplicateStats *pos;
5111 /* Allocate a single stats iteration context */
5112 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
5113 stats_context->cont = cont;
5114 stats_context->proc = proc;
5115 stats_context->cls = cls;
5116 stats_context->pg = pg;
5119 for (i = 0; i < pg->total; i++)
5121 if ((pg->peers[i].daemon->running == GNUNET_YES) && (GNUNET_NO
5122 == stats_check_existing (pg, &pg->peers[i], &stats_list)))
5124 /* Allocate one core context per core we need to connect to */
5125 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
5126 core_ctx->daemon = pg->peers[i].daemon;
5127 /* Set back pointer to topology iteration context */
5128 core_ctx->iter_context = stats_context;
5129 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
5134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5135 "Retrieving stats from %u total instances.\n", total_count);
5136 stats_context->total = total_count;
5137 if (stats_list != NULL)
5142 GNUNET_free (pos->unique_string);
5143 stats_list = pos->next;
5145 pos = stats_list->next;
5152 * Stop the connection process temporarily.
5154 * @param pg the peer group to stop connecting
5157 GNUNET_TESTING_stop_connections(struct GNUNET_TESTING_PeerGroup *pg)
5159 pg->stop_connects = GNUNET_YES;
5163 * Resume the connection process temporarily.
5165 * @param pg the peer group to resume connecting
5168 GNUNET_TESTING_resume_connections(struct GNUNET_TESTING_PeerGroup *pg)
5170 pg->stop_connects = GNUNET_NO;
5174 * There are many ways to connect peers that are supported by this function.
5175 * To connect peers in the same topology that was created via the
5176 * GNUNET_TESTING_create_topology, the topology variable must be set to
5177 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
5178 * a new instance of that topology will be generated and attempted to be
5179 * connected. This could result in some connections being impossible,
5180 * because some topologies are non-deterministic.
5182 * @param pg the peer group struct representing the running peers
5183 * @param topology which topology to connect the peers in
5184 * @param options options for connecting the topology
5185 * @param option_modifier modifier for options that take a parameter
5186 * @param connect_timeout how long to wait before giving up on connecting
5188 * @param connect_attempts how many times to attempt to connect two peers
5189 * over the connect_timeout duration
5190 * @param notify_callback notification to be called once all connections completed
5191 * @param notify_cls closure for notification callback
5193 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
5196 GNUNET_TESTING_connect_topology(
5197 struct GNUNET_TESTING_PeerGroup *pg,
5198 enum GNUNET_TESTING_Topology topology,
5199 enum GNUNET_TESTING_TopologyOption options,
5200 double option_modifier,
5201 struct GNUNET_TIME_Relative connect_timeout,
5202 unsigned int connect_attempts,
5203 GNUNET_TESTING_NotifyCompletion notify_callback,
5208 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
5209 #if VERBOSE_TOPOLOGY
5210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5211 _("Creating clique CONNECT topology\n"));
5213 create_clique (pg, &add_connections, CONNECT, GNUNET_NO);
5215 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
5216 #if VERBOSE_TOPOLOGY
5217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5218 _("Creating small world (ring) CONNECT topology\n"));
5220 create_small_world_ring (pg, &add_connections, CONNECT);
5222 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
5223 #if VERBOSE_TOPOLOGY
5224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5225 _("Creating small world (2d-torus) CONNECT topology\n"));
5227 create_small_world (pg, &add_connections, CONNECT);
5229 case GNUNET_TESTING_TOPOLOGY_RING:
5230 #if VERBOSE_TOPOLOGY
5231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring CONNECT topology\n"));
5233 create_ring (pg, &add_connections, CONNECT);
5235 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
5236 #if VERBOSE_TOPOLOGY
5237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5238 _("Creating 2d torus CONNECT topology\n"));
5240 create_2d_torus (pg, &add_connections, CONNECT);
5242 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
5243 #if VERBOSE_TOPOLOGY
5244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5245 _("Creating Erdos-Renyi CONNECT topology\n"));
5247 create_erdos_renyi (pg, &add_connections, CONNECT);
5249 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
5250 #if VERBOSE_TOPOLOGY
5251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5252 _("Creating InterNAT CONNECT topology\n"));
5254 create_nated_internet (pg, &add_connections, CONNECT);
5256 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
5257 #if VERBOSE_TOPOLOGY
5258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5259 _("Creating Scale Free CONNECT topology\n"));
5261 create_scale_free (pg, &add_connections, CONNECT);
5263 case GNUNET_TESTING_TOPOLOGY_LINE:
5264 #if VERBOSE_TOPOLOGY
5265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5266 _("Creating straight line CONNECT topology\n"));
5268 create_line (pg, &add_connections, CONNECT);
5270 case GNUNET_TESTING_TOPOLOGY_NONE:
5271 #if VERBOSE_TOPOLOGY
5272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating no CONNECT topology\n"));
5274 copy_allowed_topology (pg);
5277 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5278 ("Unknown topology specification, can't connect peers!\n"));
5279 return GNUNET_SYSERR;
5284 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
5285 #if VERBOSE_TOPOLOGY
5286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5287 ("Connecting random subset (%'.2f percent) of possible peers\n"), 100
5290 choose_random_connections (pg, option_modifier);
5292 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
5293 #if VERBOSE_TOPOLOGY
5294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5295 _("Connecting a minimum of %u peers each (if possible)\n"),
5296 (unsigned int) option_modifier);
5298 choose_minimum (pg, (unsigned int) option_modifier);
5300 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
5301 #if VERBOSE_TOPOLOGY
5302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5303 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
5304 (unsigned int) option_modifier);
5307 perform_dfs (pg, (int) option_modifier);
5310 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
5311 #if VERBOSE_TOPOLOGY
5312 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5313 ("Finding additional %u closest peers each (if possible)\n"),
5314 (unsigned int) option_modifier);
5317 add_closest (pg, (unsigned int) option_modifier,
5318 &add_connections, CONNECT);
5321 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
5323 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
5329 return connect_topology (pg, connect_timeout, connect_attempts,
5330 notify_callback, notify_cls);
5334 * Lookup and return the number of SSH connections to a host.
5336 * @param hostname the hostname to lookup in the list
5337 * @param pg the peergroup that the host belongs to
5339 * @return the number of current ssh connections to the host
5342 count_outstanding_at_host(const char *hostname,
5343 struct GNUNET_TESTING_PeerGroup *pg)
5345 struct OutstandingSSH *pos;
5347 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5349 GNUNET_assert(pos != NULL);
5350 return pos->outstanding;
5354 * Increment the number of SSH connections to a host by one.
5356 * @param hostname the hostname to lookup in the list
5357 * @param pg the peergroup that the host belongs to
5361 increment_outstanding_at_host(const char *hostname,
5362 struct GNUNET_TESTING_PeerGroup *pg)
5364 struct OutstandingSSH *pos;
5366 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5368 GNUNET_assert(pos != NULL);
5373 * Decrement the number of SSH connections to a host by one.
5375 * @param hostname the hostname to lookup in the list
5376 * @param pg the peergroup that the host belongs to
5380 decrement_outstanding_at_host(const char *hostname,
5381 struct GNUNET_TESTING_PeerGroup *pg)
5383 struct OutstandingSSH *pos;
5385 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5387 GNUNET_assert(pos != NULL);
5392 * Callback that is called whenever a hostkey is generated
5393 * for a peer. Call the real callback and decrement the
5394 * starting counter for the peergroup.
5396 * @param cls closure
5397 * @param id identifier for the daemon, NULL on error
5398 * @param d handle for the daemon
5399 * @param emsg error message (NULL on success)
5402 internal_hostkey_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5403 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5405 struct InternalStartContext *internal_context = cls;
5406 internal_context->peer->pg->starting--;
5407 internal_context->peer->pg->started++;
5408 if (internal_context->hostname != NULL)
5409 decrement_outstanding_at_host (internal_context->hostname,
5410 internal_context->peer->pg);
5411 if (internal_context->hostkey_callback != NULL)
5412 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
5414 else if (internal_context->peer->pg->started
5415 == internal_context->peer->pg->total)
5417 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
5418 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
5423 * Callback that is called whenever a peer has finished starting.
5424 * Call the real callback and decrement the starting counter
5425 * for the peergroup.
5427 * @param cls closure
5428 * @param id identifier for the daemon, NULL on error
5430 * @param d handle for the daemon
5431 * @param emsg error message (NULL on success)
5434 internal_startup_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5435 const struct GNUNET_CONFIGURATION_Handle *cfg,
5436 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5438 struct InternalStartContext *internal_context = cls;
5439 internal_context->peer->pg->starting--;
5440 if (internal_context->hostname != NULL)
5441 decrement_outstanding_at_host (internal_context->hostname,
5442 internal_context->peer->pg);
5443 if (internal_context->start_cb != NULL)
5444 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
5449 internal_continue_startup(void *cls,
5450 const struct GNUNET_SCHEDULER_TaskContext *tc)
5452 struct InternalStartContext *internal_context = cls;
5454 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5459 if ((internal_context->peer->pg->starting
5460 < internal_context->peer->pg->max_concurrent_ssh)
5461 || ((internal_context->hostname != NULL)
5462 && (count_outstanding_at_host (internal_context->hostname,
5463 internal_context->peer->pg)
5464 < internal_context->peer->pg->max_concurrent_ssh)))
5466 if (internal_context->hostname != NULL)
5467 increment_outstanding_at_host (internal_context->hostname,
5468 internal_context->peer->pg);
5469 internal_context->peer->pg->starting++;
5470 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
5474 GNUNET_SCHEDULER_add_delayed (
5475 GNUNET_TIME_relative_multiply (
5476 GNUNET_TIME_UNIT_MILLISECONDS,
5478 &internal_continue_startup,
5484 * Callback for informing us about a successful
5485 * or unsuccessful churn start call.
5487 * @param cls a ChurnContext
5488 * @param id the peer identity of the started peer
5489 * @param cfg the handle to the configuration of the peer
5490 * @param d handle to the daemon for the peer
5491 * @param emsg NULL on success, non-NULL on failure
5495 churn_start_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5496 const struct GNUNET_CONFIGURATION_Handle *cfg,
5497 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5499 struct ChurnRestartContext *startup_ctx = cls;
5500 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
5502 unsigned int total_left;
5503 char *error_message;
5505 error_message = NULL;
5508 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5509 "Churn stop callback failed with error `%s'\n", emsg);
5510 churn_ctx->num_failed_start++;
5514 churn_ctx->num_to_start--;
5518 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5519 "Started peer, %d left.\n", churn_ctx->num_to_start);
5522 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
5523 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5525 if (total_left == 0)
5527 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
5530 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5531 churn_ctx->num_failed_start,
5532 churn_ctx->num_failed_stop);
5533 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5534 GNUNET_free_non_null (error_message);
5535 GNUNET_free (churn_ctx);
5536 GNUNET_free (startup_ctx);
5541 schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5543 struct PeerRestartContext *peer_restart_ctx = cls;
5544 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
5546 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5547 GNUNET_SCHEDULER_add_delayed (
5548 GNUNET_TIME_relative_multiply (
5549 GNUNET_TIME_UNIT_MILLISECONDS,
5551 &schedule_churn_restart, peer_restart_ctx);
5554 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
5555 startup_ctx->timeout,
5556 &churn_start_callback, startup_ctx);
5557 GNUNET_free (peer_restart_ctx);
5563 internal_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5565 struct InternalStartContext *internal_context = cls;
5567 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5572 if ((internal_context->peer->pg->starting
5573 < internal_context->peer->pg->max_concurrent_ssh)
5574 || ((internal_context->hostname != NULL)
5575 && (count_outstanding_at_host (internal_context->hostname,
5576 internal_context->peer->pg)
5577 < internal_context->peer->pg->max_concurrent_ssh)))
5579 if (internal_context->hostname != NULL)
5580 increment_outstanding_at_host (internal_context->hostname,
5581 internal_context->peer->pg);
5582 internal_context->peer->pg->starting++;
5583 internal_context->peer->daemon
5584 = GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
5585 internal_context->timeout,
5587 internal_context->hostname,
5588 internal_context->username,
5589 internal_context->sshport,
5590 internal_context->hostkey,
5591 &internal_hostkey_callback,
5593 &internal_startup_callback,
5598 GNUNET_SCHEDULER_add_delayed (
5599 GNUNET_TIME_relative_multiply (
5600 GNUNET_TIME_UNIT_MILLISECONDS,
5602 &internal_start, internal_context);
5605 #if USE_START_HELPER
5607 struct PeerStartHelperContext
5609 struct GNUNET_TESTING_PeerGroup *pg;
5611 struct HostData *host;
5613 struct GNUNET_OS_Process *proc;
5617 check_peers_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5619 struct PeerStartHelperContext *helper = cls;
5620 enum GNUNET_OS_ProcessStatusType type;
5623 GNUNET_TESTING_NotifyDaemonRunning cb;
5625 if (GNUNET_NO == GNUNET_OS_process_status (helper->proc, &type, &code)) /* Still running, wait some more! */
5627 GNUNET_SCHEDULER_add_delayed(GNUNET_CONSTANTS_EXEC_WAIT, &check_peers_started, helper);
5631 helper->pg->starting--;
5632 if (helper->pg->starting == 0) /* All peers have finished starting! */
5634 /* Call the peer started callback for each peer, set proper FSM state (?) */
5635 for (i = 0; i < helper->pg->total; i++)
5637 cb = helper->pg->peers[i].daemon->cb;
5638 helper->pg->peers[i].daemon->cb = NULL;
5639 helper->pg->peers[i].daemon->running = GNUNET_YES;
5640 helper->pg->peers[i].daemon->phase = SP_START_DONE;
5643 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
5644 cb (helper->pg->peers[i].daemon->cb_cls,
5645 &helper->pg->peers[i].daemon->id,
5646 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5647 "Failed to execute peerStartHelper.pl, or return code bad!");
5649 cb (helper->pg->peers[i].daemon->cb_cls,
5650 &helper->pg->peers[i].daemon->id,
5651 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5658 GNUNET_OS_process_close(helper->proc);
5662 start_peer_helper (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5664 struct PeerStartHelperContext *helper = cls;
5665 char *baseservicehome;
5668 /* ssh user@host peerStartHelper /path/to/basedirectory */
5669 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (helper->pg->cfg, "PATHS", "SERVICEHOME",
5671 GNUNET_asprintf(&tempdir, "%s/%s/", baseservicehome, helper->host->hostname);
5672 if (NULL != helper->host->username)
5673 GNUNET_asprintf (&arg, "%s@%s", helper->host->username, helper->host->hostname);
5675 GNUNET_asprintf (&arg, "%s", helper->host->hostname);
5677 /* FIXME: Doesn't support ssh_port option! */
5678 helper->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5679 "peerStartHelper.pl", tempdir, NULL);
5680 GNUNET_assert(helper->proc != NULL);
5681 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "starting peers with cmd ssh %s %s %s\n", arg, "peerStartHelper.pl", tempdir);
5682 GNUNET_SCHEDULER_add_now (&check_peers_started, helper);
5683 GNUNET_free (tempdir);
5684 GNUNET_free (baseservicehome);
5690 * Function which continues a peer group starting up
5691 * after successfully generating hostkeys for each peer.
5693 * @param pg the peer group to continue starting
5697 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
5701 #if USE_START_HELPER
5702 if ((pg->num_hosts > 0) && (pg->hostkey_data != NULL))
5704 struct PeerStartHelperContext *helper;
5705 pg->starting = pg->num_hosts;
5706 for (i = 0; i < pg->num_hosts; i++)
5708 helper = GNUNET_malloc(sizeof(struct PeerStartHelperContext));
5710 helper->host = &pg->hosts[i];
5711 GNUNET_SCHEDULER_add_now(&start_peer_helper, helper);
5717 for (i = 0; i < pg->total; i++)
5719 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5720 &pg->peers[i].internal_context);
5725 for (i = 0; i < pg->total; i++)
5727 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5728 &pg->peers[i].internal_context);
5733 #if USE_START_HELPER
5735 call_hostkey_callbacks (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5737 struct GNUNET_TESTING_PeerGroup *pg = cls;
5739 for (i = 0; i < pg->total; i++)
5741 if (pg->peers[i].internal_context.hostkey_callback != NULL)
5742 pg->peers[i].internal_context.hostkey_callback (pg->peers[i].internal_context.hostkey_cls,
5743 &pg->peers[i].daemon->id,
5744 pg->peers[i].daemon,
5748 if (pg->peers[0].internal_context.hostkey_callback == NULL)
5749 GNUNET_TESTING_daemons_continue_startup (pg);
5754 * Start count gnunet instances with the same set of transports and
5755 * applications. The port numbers (any option called "PORT") will be
5756 * adjusted to ensure that no two peers running on the same system
5757 * have the same port(s) in their respective configurations.
5759 * @param cfg configuration template to use
5760 * @param total number of daemons to start
5761 * @param max_concurrent_connections for testing, how many peers can
5762 * we connect to simultaneously
5763 * @param max_concurrent_ssh when starting with ssh, how many ssh
5764 * connections will we allow at once (based on remote hosts allowed!)
5765 * @param timeout total time allowed for peers to start
5766 * @param hostkey_callback function to call on each peers hostkey generation
5767 * if NULL, peers will be started by this call, if non-null,
5768 * GNUNET_TESTING_daemons_continue_startup must be called after
5769 * successful hostkey generation
5770 * @param hostkey_cls closure for hostkey callback
5771 * @param cb function to call on each daemon that was started
5772 * @param cb_cls closure for cb
5773 * @param connect_callback function to call each time two hosts are connected
5774 * @param connect_callback_cls closure for connect_callback
5775 * @param hostnames linked list of host structs to use to start peers on
5776 * (NULL to run on localhost only)
5778 * @return NULL on error, otherwise handle to control peer group
5780 struct GNUNET_TESTING_PeerGroup *
5781 GNUNET_TESTING_daemons_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
5783 unsigned int max_concurrent_connections,
5784 unsigned int max_concurrent_ssh,
5785 struct GNUNET_TIME_Relative timeout,
5786 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
5788 GNUNET_TESTING_NotifyDaemonRunning cb,
5790 GNUNET_TESTING_NotifyConnection connect_callback,
5791 void *connect_callback_cls,
5792 const struct GNUNET_TESTING_Host *hostnames)
5794 struct GNUNET_TESTING_PeerGroup *pg;
5795 const struct GNUNET_TESTING_Host *hostpos;
5796 const char *hostname;
5797 const char *username;
5798 char *baseservicehome;
5799 char *newservicehome;
5801 char *hostkeys_file;
5804 struct GNUNET_DISK_FileHandle *fd;
5805 struct GNUNET_CONFIGURATION_Handle *pcfg;
5807 struct OutstandingSSH *ssh_entry;
5808 unsigned int hostcnt;
5815 uint64_t total_hostkeys;
5816 struct GNUNET_OS_Process *proc;
5827 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
5829 pg->notify_connection = connect_callback;
5830 pg->notify_connection_cls = connect_callback_cls;
5832 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
5833 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
5834 pg->max_outstanding_connections = max_concurrent_connections;
5835 pg->max_concurrent_ssh = max_concurrent_ssh;
5836 if (NULL != hostnames)
5839 hostpos = hostnames;
5840 while (hostpos != NULL)
5842 hostpos = hostpos->next;
5845 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5848 hostpos = hostnames;
5849 while (hostpos != NULL)
5851 pg->hosts[off].minport = LOW_PORT;
5852 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
5853 if (hostpos->username != NULL)
5854 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
5855 pg->hosts[off].sshport = hostpos->port;
5856 hostpos = hostpos->next;
5866 pg->num_hosts = off;
5874 /* Create the servicehome directory for each remote peer */
5875 GNUNET_assert(GNUNET_OK ==
5876 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
5878 for (i = 0; i < pg->num_hosts; i++)
5880 ssh_entry = GNUNET_malloc(sizeof(struct OutstandingSSH));
5881 ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
5882 GNUNET_CONTAINER_DLL_insert(pg->ssh_head, pg->ssh_tail, ssh_entry);
5883 GNUNET_asprintf(&tmpdir, "%s/%s", baseservicehome, pg->hosts[i].hostname);
5884 if (NULL != pg->hosts[i].username)
5885 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username,
5886 pg->hosts[i].hostname);
5888 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
5889 if (pg->hosts[i].sshport != 0)
5891 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
5892 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-P",
5897 arg, "mkdir -p", tmpdir,
5901 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5902 "mkdir -p", tmpdir, NULL);
5903 GNUNET_assert(proc != NULL);
5904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5905 "Creating remote dir with command ssh %s %s %s\n", arg,
5906 " mkdir -p ", tmpdir);
5907 GNUNET_free(tmpdir);
5909 GNUNET_OS_process_wait (proc);
5910 GNUNET_OS_process_close(proc);
5912 GNUNET_free(baseservicehome);
5913 baseservicehome = NULL;
5915 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING",
5919 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
5920 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5921 _("Could not read hostkeys file!\n"));
5924 /* Check hostkey file size, read entire thing into memory */
5925 fd = GNUNET_DISK_file_open (hostkeys_file,
5926 GNUNET_DISK_OPEN_READ,
5927 GNUNET_DISK_PERM_NONE);
5930 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
5933 GNUNET_free (hostkeys_file);
5934 for (i=0;i<pg->num_hosts;i++)
5936 GNUNET_free (pg->hosts[i].hostname);
5937 GNUNET_free_non_null (pg->hosts[i].username);
5939 GNUNET_free (pg->peers);
5940 GNUNET_free (pg->hosts);
5945 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs,
5949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5950 "Found file size %llu for hostkeys, expect hostkeys to be size %d\n",
5951 fs, HOSTKEYFILESIZE);
5953 if (0 != (fs % HOSTKEYFILESIZE))
5955 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5956 "File size %llu seems incorrect for hostkeys...\n",
5961 total_hostkeys = fs / HOSTKEYFILESIZE;
5962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5963 "Will read %llu hostkeys from file\n",
5965 pg->hostkey_data = GNUNET_malloc_large (fs);
5966 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
5968 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
5970 GNUNET_free(hostkeys_file);
5973 for (off = 0; off < total; off++)
5977 hostname = pg->hosts[off % hostcnt].hostname;
5978 username = pg->hosts[off % hostcnt].username;
5979 sshport = pg->hosts[off % hostcnt].sshport;
5980 pcfg = make_config (cfg, off, &pg->hosts[off % hostcnt].minport,
5981 &upnum, hostname, &fdnum);
5988 pcfg = make_config (cfg, off, &minport, &upnum, hostname, &fdnum);
5993 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5994 _("Could not create configuration for peer number %u on `%s'!\n"),
5996 hostname == NULL ? "localhost" : hostname);
6001 == GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS",
6005 if (hostname != NULL)
6006 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", baseservicehome, hostname, off);
6008 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
6009 GNUNET_free (baseservicehome);
6010 baseservicehome = NULL;
6014 tmpdir = getenv ("TMPDIR");
6015 tmpdir = tmpdir ? tmpdir : "/tmp";
6016 if (hostname != NULL)
6017 GNUNET_asprintf (&newservicehome, "%s/%s/%s/%d/", tmpdir, hostname,
6018 "gnunet-testing-test-test", off);
6020 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", tmpdir,
6021 "gnunet-testing-test-test", off);
6023 GNUNET_CONFIGURATION_set_value_string (pcfg, "PATHS", "SERVICEHOME",
6025 GNUNET_free (newservicehome);
6026 pg->peers[off].cfg = pcfg;
6027 pg->peers[off].pg = pg;
6028 pg->peers[off].internal_context.peer = &pg->peers[off];
6029 pg->peers[off].internal_context.timeout = timeout;
6030 pg->peers[off].internal_context.hostname = hostname;
6031 pg->peers[off].internal_context.username = username;
6032 pg->peers[off].internal_context.sshport = sshport;
6033 if (pg->hostkey_data != NULL)
6034 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off
6036 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
6037 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
6038 pg->peers[off].internal_context.start_cb = cb;
6039 pg->peers[off].internal_context.start_cb_cls = cb_cls;
6040 #if !USE_START_HELPER
6041 GNUNET_SCHEDULER_add_now (&internal_start,
6042 &pg->peers[off].internal_context);
6044 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6046 pg->peers[off].daemon
6047 = GNUNET_TESTING_daemon_start (pcfg,
6053 pg->peers[off].internal_context.hostkey,
6054 &internal_hostkey_callback,
6055 &pg->peers[off].internal_context,
6056 &internal_startup_callback,
6057 &pg->peers[off].internal_context);
6059 * At this point, given that we had a hostkeyfile,
6060 * we can call the hostkey callback!
6061 * But first, we should copy (rsync) all of the configs
6062 * and hostkeys to the remote peers. Then let topology
6063 * creation happen, then call the peer start helper processes,
6064 * then set pg->whatever_phase for each peer and let them
6065 * enter the fsm to get the HELLO's for peers and start connecting.
6070 GNUNET_SCHEDULER_add_now (&internal_start,
6071 &pg->peers[off].internal_context);
6077 #if USE_START_HELPER /* Now the peergroup has been set up, hostkeys and configs written to files. */
6078 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6080 for (off = 0; off < hostcnt; off++)
6082 /* FIXME: grab baseservicehome! */
6083 GNUNET_asprintf (&newservicehome,
6086 pg->hosts[off].hostname);
6088 if (NULL != username)
6089 GNUNET_asprintf (&arg,
6092 pg->hosts[off].hostname,
6094 pg->hosts[off].hostname);
6096 GNUNET_asprintf (&arg,
6098 pg->hosts[off].hostname,
6100 pg->hosts[off].hostname);
6102 // FIXME: free--- GNUNET_free (baseservicehome);
6104 /* FIXME: Doesn't support ssh_port option! */
6105 proc = GNUNET_OS_start_process (NULL, NULL,
6107 "rsync", "-r", newservicehome, arg, NULL);
6109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6110 "copying directory with command rsync -r %s %s\n",
6111 newservicehome, arg);
6116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6117 _("Could not start `%s' process to copy configuration directory.\n"),
6121 GNUNET_OS_process_wait (proc);
6122 GNUNET_OS_process_close (proc);
6124 /* Now all the configuration files and hostkeys are copied to the remote host. Call the hostkey callback for each peer! */
6125 GNUNET_SCHEDULER_add_now (&call_hostkey_callbacks, pg);
6132 * Get a daemon by number, so callers don't have to do nasty
6133 * offsetting operation.
6135 struct GNUNET_TESTING_Daemon *
6136 GNUNET_TESTING_daemon_get(struct GNUNET_TESTING_PeerGroup *pg,
6137 unsigned int position)
6139 if (position < pg->total)
6140 return pg->peers[position].daemon;
6145 * Get a daemon by peer identity, so callers can
6146 * retrieve the daemon without knowing it's offset.
6148 * @param pg the peer group to retrieve the daemon from
6149 * @param peer_id the peer identity of the daemon to retrieve
6151 * @return the daemon on success, or NULL if no such peer identity is found
6153 struct GNUNET_TESTING_Daemon *
6154 GNUNET_TESTING_daemon_get_by_id(struct GNUNET_TESTING_PeerGroup *pg,
6155 struct GNUNET_PeerIdentity *peer_id)
6159 for (i = 0; i < pg->total; i++)
6161 if (0 == memcmp (&pg->peers[i].daemon->id, peer_id,
6162 sizeof(struct GNUNET_PeerIdentity)))
6163 return pg->peers[i].daemon;
6169 * Prototype of a function that will be called when a
6170 * particular operation was completed the testing library.
6172 * @param cls closure (a struct RestartContext)
6173 * @param id id of the peer that was restarted
6174 * @param cfg handle to the configuration of the peer
6175 * @param d handle to the daemon that was restarted
6176 * @param emsg NULL on success
6179 restart_callback(void *cls, const struct GNUNET_PeerIdentity *id,
6180 const struct GNUNET_CONFIGURATION_Handle *cfg,
6181 struct GNUNET_TESTING_Daemon *d, const char *emsg)
6183 struct RestartContext *restart_context = cls;
6187 restart_context->peers_restarted++;
6191 restart_context->peers_restart_failed++;
6194 if (restart_context->peers_restarted == restart_context->peer_group->total)
6196 restart_context->callback (restart_context->callback_cls, NULL);
6197 GNUNET_free (restart_context);
6199 else if (restart_context->peers_restart_failed
6200 + restart_context->peers_restarted == restart_context->peer_group->total)
6202 restart_context->callback (restart_context->callback_cls,
6203 "Failed to restart peers!");
6204 GNUNET_free (restart_context);
6210 * Callback for informing us about a successful
6211 * or unsuccessful churn stop call.
6213 * @param cls a ChurnContext
6214 * @param emsg NULL on success, non-NULL on failure
6218 churn_stop_callback(void *cls, const char *emsg)
6220 struct ShutdownContext *shutdown_ctx = cls;
6221 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
6222 unsigned int total_left;
6223 char *error_message;
6225 error_message = NULL;
6226 shutdown_ctx->outstanding--;
6230 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6231 "Churn stop callback failed with error `%s'\n", emsg);
6232 churn_ctx->num_failed_stop++;
6236 churn_ctx->num_to_stop--;
6240 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6241 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
6243 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
6244 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
6246 if (total_left == 0)
6248 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
6252 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
6253 churn_ctx->num_failed_start,
6254 churn_ctx->num_failed_stop);
6256 churn_ctx->cb (churn_ctx->cb_cls, error_message);
6257 GNUNET_free_non_null (error_message);
6258 GNUNET_free (churn_ctx);
6259 GNUNET_free (shutdown_ctx);
6264 * Count the number of running peers.
6266 * @param pg handle for the peer group
6268 * @return the number of currently running peers in the peer group
6271 GNUNET_TESTING_daemons_running(struct GNUNET_TESTING_PeerGroup *pg)
6274 unsigned int running = 0;
6275 for (i = 0; i < pg->total; i++)
6277 if (pg->peers[i].daemon->running == GNUNET_YES)
6279 GNUNET_assert (running != -1);
6287 * Task to rate limit the number of outstanding peer shutdown
6288 * requests. This is necessary for making sure we don't do
6289 * too many ssh connections at once, but is generally nicer
6290 * to any system as well (graduated task starts, as opposed
6291 * to calling gnunet-arm N times all at once).
6294 schedule_churn_shutdown_task(void *cls,
6295 const struct GNUNET_SCHEDULER_TaskContext *tc)
6297 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6298 struct ShutdownContext *shutdown_ctx;
6299 struct ChurnContext *churn_ctx;
6300 GNUNET_assert (peer_shutdown_ctx != NULL);
6301 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6302 GNUNET_assert (shutdown_ctx != NULL);
6303 churn_ctx = (struct ChurnContext *) shutdown_ctx->cb_cls;
6304 if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
6305 GNUNET_SCHEDULER_add_delayed (
6306 GNUNET_TIME_relative_multiply (
6307 GNUNET_TIME_UNIT_MILLISECONDS,
6309 &schedule_churn_shutdown_task,
6313 shutdown_ctx->outstanding++;
6314 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6315 shutdown_ctx->timeout, shutdown_ctx->cb,
6316 shutdown_ctx, GNUNET_NO, GNUNET_YES);
6317 GNUNET_free (peer_shutdown_ctx);
6323 * Simulate churn by stopping some peers (and possibly
6324 * re-starting others if churn is called multiple times). This
6325 * function can only be used to create leave-join churn (peers "never"
6326 * leave for good). First "voff" random peers that are currently
6327 * online will be taken offline; then "von" random peers that are then
6328 * offline will be put back online. No notifications will be
6329 * generated for any of these operations except for the callback upon
6332 * @param pg handle for the peer group
6333 * @param voff number of peers that should go offline
6334 * @param von number of peers that should come back online;
6335 * must be zero on first call (since "testbed_start"
6336 * always starts all of the peers)
6337 * @param timeout how long to wait for operations to finish before
6339 * @param cb function to call at the end
6340 * @param cb_cls closure for cb
6343 GNUNET_TESTING_daemons_churn(struct GNUNET_TESTING_PeerGroup *pg,
6344 unsigned int voff, unsigned int von,
6345 struct GNUNET_TIME_Relative timeout,
6346 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6348 struct ChurnContext *churn_ctx;
6349 struct ShutdownContext *shutdown_ctx;
6350 struct PeerShutdownContext *peer_shutdown_ctx;
6351 struct PeerRestartContext *peer_restart_ctx;
6352 struct ChurnRestartContext *churn_startup_ctx;
6354 unsigned int running;
6355 unsigned int stopped;
6356 unsigned int total_running;
6357 unsigned int total_stopped;
6359 unsigned int *running_arr;
6360 unsigned int *stopped_arr;
6361 unsigned int *running_permute;
6362 unsigned int *stopped_permute;
6364 shutdown_ctx = NULL;
6365 peer_shutdown_ctx = NULL;
6366 peer_restart_ctx = NULL;
6367 churn_startup_ctx = NULL;
6372 if ((von == 0) && (voff == 0)) /* No peers at all? */
6378 for (i = 0; i < pg->total; i++)
6380 if (pg->peers[i].daemon->running == GNUNET_YES)
6382 GNUNET_assert (running != -1);
6387 GNUNET_assert (stopped != -1);
6394 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6395 "Trying to stop more peers than are currently running!\n");
6396 cb (cb_cls, "Trying to stop more peers than are currently running!");
6402 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6403 "Trying to start more peers than are currently stopped!\n");
6404 cb (cb_cls, "Trying to start more peers than are currently stopped!");
6408 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6412 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
6416 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
6418 running_permute = NULL;
6419 stopped_permute = NULL;
6422 running_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6425 stopped_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6428 total_running = running;
6429 total_stopped = stopped;
6433 churn_ctx->num_to_start = von;
6434 churn_ctx->num_to_stop = voff;
6436 churn_ctx->cb_cls = cb_cls;
6439 for (i = 0; i < pg->total; i++)
6441 if (pg->peers[i].daemon->running == GNUNET_YES)
6443 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6444 running_arr[running] = i;
6449 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6450 stopped_arr[stopped] = i;
6455 GNUNET_assert (running >= voff);
6458 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6459 shutdown_ctx->cb = &churn_stop_callback;
6460 shutdown_ctx->cb_cls = churn_ctx;
6461 shutdown_ctx->total_peers = voff;
6462 shutdown_ctx->timeout = timeout;
6465 for (i = 0; i < voff; i++)
6468 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
6469 running_permute[i]);
6471 GNUNET_assert (running_arr != NULL);
6472 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6473 peer_shutdown_ctx->daemon
6474 = pg->peers[running_arr[running_permute[i]]].daemon;
6475 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6476 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
6480 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
6482 &churn_stop_callback, churn_ctx,
6483 GNUNET_NO, GNUNET_YES); */
6486 GNUNET_assert (stopped >= von);
6489 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6490 churn_startup_ctx->churn_ctx = churn_ctx;
6491 churn_startup_ctx->timeout = timeout;
6492 churn_startup_ctx->pg = pg;
6494 for (i = 0; i < von; i++)
6497 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
6498 stopped_permute[i]);
6500 GNUNET_assert (stopped_arr != NULL);
6501 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
6502 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
6503 peer_restart_ctx->daemon
6504 = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
6505 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
6507 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
6508 timeout, &churn_start_callback, churn_ctx); */
6511 GNUNET_free_non_null (running_arr);
6512 GNUNET_free_non_null (stopped_arr);
6513 GNUNET_free_non_null (running_permute);
6514 GNUNET_free_non_null (stopped_permute);
6519 * Restart all peers in the given group.
6521 * @param pg the handle to the peer group
6522 * @param callback function to call on completion (or failure)
6523 * @param callback_cls closure for the callback function
6526 GNUNET_TESTING_daemons_restart(struct GNUNET_TESTING_PeerGroup *pg,
6527 GNUNET_TESTING_NotifyCompletion callback,
6530 struct RestartContext *restart_context;
6535 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
6536 restart_context->peer_group = pg;
6537 restart_context->peers_restarted = 0;
6538 restart_context->callback = callback;
6539 restart_context->callback_cls = callback_cls;
6541 for (off = 0; off < pg->total; off++)
6543 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
6544 &restart_callback, restart_context);
6551 * Start or stop an individual peer from the given group.
6553 * @param pg handle to the peer group
6554 * @param offset which peer to start or stop
6555 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
6556 * @param timeout how long to wait for shutdown
6557 * @param cb function to call at the end
6558 * @param cb_cls closure for cb
6561 GNUNET_TESTING_daemons_vary(struct GNUNET_TESTING_PeerGroup *pg,
6562 unsigned int offset, int desired_status,
6563 struct GNUNET_TIME_Relative timeout,
6564 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6566 struct ShutdownContext *shutdown_ctx;
6567 struct ChurnRestartContext *startup_ctx;
6568 struct ChurnContext *churn_ctx;
6570 if (GNUNET_NO == desired_status)
6572 if (NULL != pg->peers[offset].daemon)
6574 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6575 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6576 churn_ctx->num_to_start = 0;
6577 churn_ctx->num_to_stop = 1;
6579 churn_ctx->cb_cls = cb_cls;
6580 shutdown_ctx->cb_cls = churn_ctx;
6581 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon, timeout,
6582 &churn_stop_callback, shutdown_ctx,
6583 GNUNET_NO, GNUNET_YES);
6586 else if (GNUNET_YES == desired_status)
6588 if (NULL == pg->peers[offset].daemon)
6590 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6591 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6592 churn_ctx->num_to_start = 1;
6593 churn_ctx->num_to_stop = 0;
6595 churn_ctx->cb_cls = cb_cls;
6596 startup_ctx->churn_ctx = churn_ctx;
6597 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
6598 timeout, &churn_start_callback,
6608 * Callback for shutting down peers in a peer group.
6610 * @param cls closure (struct ShutdownContext)
6611 * @param emsg NULL on success
6614 internal_shutdown_callback(void *cls, const char *emsg)
6616 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6617 struct ShutdownContext *shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6619 struct OutstandingSSH *ssh_pos;
6621 shutdown_ctx->outstanding--;
6622 if (peer_shutdown_ctx->daemon->hostname != NULL)
6623 decrement_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6628 shutdown_ctx->peers_down++;
6632 shutdown_ctx->peers_failed++;
6635 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down
6636 + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
6638 if (shutdown_ctx->peers_failed > 0)
6639 shutdown_ctx->cb (shutdown_ctx->cb_cls,
6640 "Not all peers successfully shut down!");
6642 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
6644 GNUNET_free (shutdown_ctx->pg->peers);
6645 GNUNET_free_non_null(shutdown_ctx->pg->hostkey_data);
6646 for (off = 0; off < shutdown_ctx->pg->num_hosts; off++)
6648 GNUNET_free (shutdown_ctx->pg->hosts[off].hostname);
6649 GNUNET_free_non_null (shutdown_ctx->pg->hosts[off].username);
6651 GNUNET_free_non_null (shutdown_ctx->pg->hosts);
6652 while (NULL != (ssh_pos = shutdown_ctx->pg->ssh_head))
6654 GNUNET_CONTAINER_DLL_remove(shutdown_ctx->pg->ssh_head, shutdown_ctx->pg->ssh_tail, ssh_pos);
6655 GNUNET_free(ssh_pos);
6657 GNUNET_free (shutdown_ctx->pg);
6658 GNUNET_free (shutdown_ctx);
6660 GNUNET_free(peer_shutdown_ctx);
6665 * Task to rate limit the number of outstanding peer shutdown
6666 * requests. This is necessary for making sure we don't do
6667 * too many ssh connections at once, but is generally nicer
6668 * to any system as well (graduated task starts, as opposed
6669 * to calling gnunet-arm N times all at once).
6672 schedule_shutdown_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6674 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6675 struct ShutdownContext *shutdown_ctx;
6677 GNUNET_assert (peer_shutdown_ctx != NULL);
6678 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6679 GNUNET_assert (shutdown_ctx != NULL);
6681 if ((shutdown_ctx->outstanding < shutdown_ctx->pg->max_concurrent_ssh)
6682 || ((peer_shutdown_ctx->daemon->hostname != NULL)
6683 && (count_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6685 < shutdown_ctx->pg->max_concurrent_ssh)))
6687 if (peer_shutdown_ctx->daemon->hostname != NULL)
6688 increment_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6690 shutdown_ctx->outstanding++;
6691 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6692 shutdown_ctx->timeout,
6693 &internal_shutdown_callback, peer_shutdown_ctx,
6694 GNUNET_YES, GNUNET_NO);
6697 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
6699 &schedule_shutdown_task, peer_shutdown_ctx);
6705 * Shutdown all peers started in the given group.
6707 * @param pg handle to the peer group
6708 * @param timeout how long to wait for shutdown
6709 * @param cb callback to notify upon success or failure
6710 * @param cb_cls closure for cb
6713 GNUNET_TESTING_daemons_stop(struct GNUNET_TESTING_PeerGroup *pg,
6714 struct GNUNET_TIME_Relative timeout,
6715 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6718 struct ShutdownContext *shutdown_ctx;
6719 struct PeerShutdownContext *peer_shutdown_ctx;
6721 struct PeerConnection *conn_iter;
6722 struct PeerConnection *temp_conn;
6725 GNUNET_assert (pg->total > 0);
6727 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6728 shutdown_ctx->cb = cb;
6729 shutdown_ctx->cb_cls = cb_cls;
6730 shutdown_ctx->total_peers = pg->total;
6731 shutdown_ctx->timeout = timeout;
6732 shutdown_ctx->pg = pg;
6733 /* shtudown_ctx->outstanding = 0; */
6735 for (off = 0; off < pg->total; off++)
6737 GNUNET_assert (NULL != pg->peers[off].daemon);
6738 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6739 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
6740 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6741 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
6743 if (NULL != pg->peers[off].cfg)
6745 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
6746 pg->peers[off].cfg = NULL;
6749 conn_iter = pg->peers[off].allowed_peers_head;
6750 while (conn_iter != NULL)
6752 temp_conn = conn_iter->next;
6753 GNUNET_free(conn_iter);
6754 conn_iter = temp_conn;
6757 conn_iter = pg->peers[off].connect_peers_head;
6758 while (conn_iter != NULL)
6760 temp_conn = conn_iter->next;
6761 GNUNET_free(conn_iter);
6762 conn_iter = temp_conn;
6765 conn_iter = pg->peers[off].blacklisted_peers_head;
6766 while (conn_iter != NULL)
6768 temp_conn = conn_iter->next;
6769 GNUNET_free(conn_iter);
6770 conn_iter = temp_conn;
6773 conn_iter = pg->peers[off].connect_peers_working_set_head;
6774 while (conn_iter != NULL)
6776 temp_conn = conn_iter->next;
6777 GNUNET_free(conn_iter);
6778 conn_iter = temp_conn;
6781 if (pg->peers[off].allowed_peers != NULL)
6782 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
6783 if (pg->peers[off].connect_peers != NULL)
6784 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
6785 if (pg->peers[off].blacklisted_peers != NULL)
6786 GNUNET_CONTAINER_multihashmap_destroy (pg->
6787 peers[off].blacklisted_peers);
6792 /* end of testing_group.c */