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
1313 if (0 == strcmp (option, "UNIXPATH"))
1315 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig,
1319 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1320 section, ctx->upnum++);
1323 else if ((GNUNET_YES
1324 == GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1327 && (num_per_host > 0))
1330 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1331 section, ctx->fdnum % num_per_host);
1336 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1338 value = ctx->hostname;
1340 GNUNET_free (single_variable);
1341 GNUNET_free (per_host_variable);
1342 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1346 * Create a new configuration using the given configuration
1347 * as a template; however, each PORT in the existing cfg
1348 * must be renumbered by incrementing "*port". If we run
1349 * out of "*port" numbers, return NULL.
1351 * @param cfg template configuration
1352 * @param off the current peer offset
1353 * @param port port numbers to use, update to reflect
1354 * port numbers that were used
1355 * @param upnum number to make unix domain socket names unique
1356 * @param hostname hostname of the controlling host, to allow control connections from
1357 * @param fdnum number used to offset the unix domain socket for grouped processes
1358 * (such as statistics or peerinfo, which can be shared among others)
1360 * @return new configuration, NULL on error
1362 static struct GNUNET_CONFIGURATION_Handle *
1363 make_config(const struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t off,
1364 uint16_t * port, uint32_t * upnum, const char *hostname,
1367 struct UpdateContext uc;
1370 char *allowed_hosts;
1376 uc.ret = GNUNET_CONFIGURATION_create ();
1377 uc.hostname = hostname;
1380 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1381 if (uc.nport >= HIGH_PORT)
1384 GNUNET_CONFIGURATION_destroy (uc.ret);
1388 if (GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "control_host",
1389 &control_host) == GNUNET_OK)
1391 if (hostname != NULL)
1392 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1395 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1397 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1399 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1400 "ACCEPT_FROM", allowed_hosts);
1401 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1403 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1404 "ACCEPT_FROM", allowed_hosts);
1406 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH", "");
1407 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH",
1409 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH", "");
1410 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1413 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "USE_LOCALADDR",
1415 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "USE_LOCALADDR",
1417 GNUNET_free_non_null (control_host);
1418 GNUNET_free (allowed_hosts);
1421 /* arm needs to know to allow connections from the host on which it is running,
1422 * otherwise gnunet-arm is unable to connect to it in some instances */
1423 if (hostname != NULL)
1425 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1426 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1428 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1430 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "USE_LOCALADDR",
1432 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "USE_LOCALADDR",
1434 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1436 GNUNET_free (allowed_hosts);
1440 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1442 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1444 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "USE_LOCALADDR",
1446 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "USE_LOCALADDR",
1450 *port = (uint16_t) uc.nport;
1458 * Remove entries from the peer connection list
1460 * @param pg the peer group we are working with
1461 * @param first index of the first peer
1462 * @param second index of the second peer
1463 * @param list the peer list to use
1464 * @param check UNUSED
1466 * @return the number of connections added (can be 0, 1 or 2)
1470 remove_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1471 unsigned int second, enum PeerLists list, unsigned int check)
1475 struct PeerConnection **first_list;
1476 struct PeerConnection **second_list;
1477 struct PeerConnection *first_iter;
1478 struct PeerConnection *second_iter;
1479 struct PeerConnection **first_tail;
1480 struct PeerConnection **second_tail;
1483 GNUNET_HashCode hash_first;
1484 GNUNET_HashCode hash_second;
1486 hash_from_uid (first, &hash_first);
1487 hash_from_uid (second, &hash_second);
1495 first_list = &pg->peers[first].allowed_peers_head;
1496 second_list = &pg->peers[second].allowed_peers_head;
1497 first_tail = &pg->peers[first].allowed_peers_tail;
1498 second_tail = &pg->peers[second].allowed_peers_tail;
1501 first_list = &pg->peers[first].connect_peers_head;
1502 second_list = &pg->peers[second].connect_peers_head;
1503 first_tail = &pg->peers[first].connect_peers_tail;
1504 second_tail = &pg->peers[second].connect_peers_tail;
1507 first_list = &pg->peers[first].blacklisted_peers_head;
1508 second_list = &pg->peers[second].blacklisted_peers_head;
1509 first_tail = &pg->peers[first].blacklisted_peers_tail;
1510 second_tail = &pg->peers[second].blacklisted_peers_tail;
1513 first_list = &pg->peers[first].connect_peers_working_set_head;
1514 second_list = &pg->peers[second].connect_peers_working_set_head;
1515 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1516 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1523 first_iter = *first_list;
1524 while (first_iter != NULL)
1526 if (first_iter->index == second)
1528 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1529 GNUNET_free(first_iter);
1533 first_iter = first_iter->next;
1536 second_iter = *second_list;
1537 while (second_iter != NULL)
1539 if (second_iter->index == first)
1541 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1542 GNUNET_free(second_iter);
1546 second_iter = second_iter->next;
1550 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1553 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1558 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1561 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1570 * Add entries to the some list
1572 * @param pg the peer group we are working with
1573 * @param first index of the first peer
1574 * @param second index of the second peer
1575 * @param list the list type that we should modify
1576 * @param check GNUNET_YES to check lists before adding
1577 * GNUNET_NO to force add
1579 * @return the number of connections added (can be 0, 1 or 2)
1583 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1584 unsigned int second, enum PeerLists list, unsigned int check)
1590 struct PeerConnection **first_list;
1591 struct PeerConnection **second_list;
1592 struct PeerConnection *first_iter;
1593 struct PeerConnection *second_iter;
1594 struct PeerConnection *new_first;
1595 struct PeerConnection *new_second;
1596 struct PeerConnection **first_tail;
1597 struct PeerConnection **second_tail;
1602 first_list = &pg->peers[first].allowed_peers_head;
1603 second_list = &pg->peers[second].allowed_peers_head;
1604 first_tail = &pg->peers[first].allowed_peers_tail;
1605 second_tail = &pg->peers[second].allowed_peers_tail;
1608 first_list = &pg->peers[first].connect_peers_head;
1609 second_list = &pg->peers[second].connect_peers_head;
1610 first_tail = &pg->peers[first].connect_peers_tail;
1611 second_tail = &pg->peers[second].connect_peers_tail;
1614 first_list = &pg->peers[first].blacklisted_peers_head;
1615 second_list = &pg->peers[second].blacklisted_peers_head;
1616 first_tail = &pg->peers[first].blacklisted_peers_tail;
1617 second_tail = &pg->peers[second].blacklisted_peers_tail;
1620 first_list = &pg->peers[first].connect_peers_working_set_head;
1621 second_list = &pg->peers[second].connect_peers_working_set_head;
1622 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1623 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1630 add_first = GNUNET_YES;
1631 add_second = GNUNET_YES;
1633 if (check == GNUNET_YES)
1635 first_iter = *first_list;
1636 while (first_iter != NULL)
1638 if (first_iter->index == second)
1640 add_first = GNUNET_NO;
1643 first_iter = first_iter->next;
1646 second_iter = *second_list;
1647 while (second_iter != NULL)
1649 if (second_iter->index == first)
1651 add_second = GNUNET_NO;
1654 second_iter = second_iter->next;
1661 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1662 new_first->index = second;
1663 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1664 pg->peers[first].num_connections++;
1670 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1671 new_second->index = first;
1672 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1673 pg->peers[second].num_connections++;
1681 * Scale free network construction as described in:
1683 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1685 * Start with a network of "one" peer, then progressively add
1686 * peers up to the total number. At each step, iterate over
1687 * all possible peers and connect new peer based on number of
1688 * existing connections of the target peer.
1690 * @param pg the peer group we are dealing with
1691 * @param proc the connection processor to use
1692 * @param list the peer list to use
1694 * @return the number of connections created
1697 create_scale_free(struct GNUNET_TESTING_PeerGroup *pg,
1698 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1701 unsigned int total_connections;
1702 unsigned int outer_count;
1704 unsigned int previous_total_connections;
1708 GNUNET_assert (pg->total > 1);
1710 /* Add a connection between the first two nodes */
1711 total_connections = proc (pg, 0, 1, list, GNUNET_YES);
1713 for (outer_count = 1; outer_count < pg->total; outer_count++)
1715 previous_total_connections = total_connections;
1716 for (i = 0; i < outer_count; i++)
1718 probability = pg->peers[i].num_connections
1719 / (double) previous_total_connections;
1721 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1723 / ((double) UINT64_MAX);
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1726 "Considering connecting peer %d to peer %d\n",
1729 if (random < probability)
1732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1733 "Connecting peer %d to peer %d\n", outer_count, i);
1735 total_connections += proc (pg, outer_count, i, list, GNUNET_YES);
1740 return total_connections;
1744 * Create a topology given a peer group (set of running peers)
1745 * and a connection processor. Creates a small world topology
1746 * according to the rewired ring construction. The basic
1747 * behavior is that a ring topology is created, but with some
1748 * probability instead of connecting a peer to the next
1749 * neighbor in the ring a connection will be created to a peer
1750 * selected uniformly at random. We use the TESTING
1751 * PERCENTAGE option to specify what number of
1752 * connections each peer should have. Default is 2,
1753 * which makes the ring, any given number is multiplied by
1754 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1755 * each peer have on average 2logn connections. The additional
1756 * connections are made at increasing distance around the ring
1757 * from the original peer, or to random peers based on the re-
1758 * wiring probability. The TESTING
1759 * PROBABILITY option is used as the probability that a given
1760 * connection is rewired.
1762 * @param pg the peergroup to create the topology on
1763 * @param proc the connection processor to call to actually set
1764 * up connections between two peers
1765 * @param list the peer list to use
1767 * @return the number of connections that were set up
1771 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg,
1772 GNUNET_TESTING_ConnectionProcessor proc,
1773 enum PeerLists list)
1777 unsigned int natLog;
1778 unsigned int randomPeer;
1779 double random, logNModifier, probability;
1780 unsigned int smallWorldConnections;
1785 unsigned int useAnd;
1786 int connect_attempts;
1788 logNModifier = 0.5; /* FIXME: default value? */
1789 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1793 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1795 GNUNET_ERROR_TYPE_WARNING,
1797 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1798 p_string, "LOGNMODIFIER", "TESTING");
1799 GNUNET_free (p_string);
1801 probability = 0.5; /* FIXME: default percentage? */
1802 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1806 if (sscanf (p_string, "%lf", &probability) != 1)
1808 GNUNET_ERROR_TYPE_WARNING,
1810 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1811 p_string, "PERCENTAGE", "TESTING");
1812 GNUNET_free (p_string);
1814 natLog = log (pg->total);
1815 connsPerPeer = ceil (natLog * logNModifier);
1817 if (connsPerPeer % 2 == 1)
1820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."),
1823 smallWorldConnections = 0;
1824 connect_attempts = 0;
1825 for (i = 0; i < pg->total; i++)
1828 max = i + connsPerPeer / 2;
1829 min = i - connsPerPeer / 2;
1831 if (max > pg->total - 1)
1833 max = max - pg->total;
1839 min = pg->total - 1 + min;
1843 for (j = 0; j < connsPerPeer / 2; j++)
1846 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1848 / ((double) UINT64_MAX));
1849 if (random < probability)
1851 /* Connect to uniformly selected random peer */
1853 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1855 while ((((randomPeer < max) && (randomPeer > min)) && (useAnd
1856 == 0)) || (((randomPeer > min) || (randomPeer < max))
1860 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1863 smallWorldConnections += proc (pg, i, randomPeer, list,
1868 nodeToConnect = i + j + 1;
1869 if (nodeToConnect > pg->total - 1)
1871 nodeToConnect = nodeToConnect - pg->total;
1873 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
1879 connect_attempts += smallWorldConnections;
1881 return connect_attempts;
1885 * Create a topology given a peer group (set of running peers)
1886 * and a connection processor.
1888 * @param pg the peergroup to create the topology on
1889 * @param proc the connection processor to call to actually set
1890 * up connections between two peers
1891 * @param list the peer list to use
1893 * @return the number of connections that were set up
1897 create_nated_internet(struct GNUNET_TESTING_PeerGroup *pg,
1898 GNUNET_TESTING_ConnectionProcessor proc,
1899 enum PeerLists list)
1901 unsigned int outer_count, inner_count;
1902 unsigned int cutoff;
1903 int connect_attempts;
1904 double nat_percentage;
1907 nat_percentage = 0.6; /* FIXME: default percentage? */
1908 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1912 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1914 GNUNET_ERROR_TYPE_WARNING,
1916 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1917 p_string, "PERCENTAGE", "TESTING");
1918 GNUNET_free (p_string);
1921 cutoff = (unsigned int) (nat_percentage * pg->total);
1922 connect_attempts = 0;
1923 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1925 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1927 if ((outer_count > cutoff) || (inner_count > cutoff))
1930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1931 "Connecting peer %d to peer %d\n",
1932 outer_count, inner_count);
1934 connect_attempts += proc (pg, outer_count, inner_count, list,
1939 return connect_attempts;
1944 * Create a topology given a peer group (set of running peers)
1945 * and a connection processor.
1947 * @param pg the peergroup to create the topology on
1948 * @param proc the connection processor to call to actually set
1949 * up connections between two peers
1950 * @param list the peer list to use
1952 * @return the number of connections that were set up
1956 create_nated_internet_copy(struct GNUNET_TESTING_PeerGroup *pg,
1957 GNUNET_TESTING_ConnectionProcessor proc,
1958 enum PeerLists list)
1960 unsigned int outer_count, inner_count;
1961 unsigned int cutoff;
1962 int connect_attempts;
1963 double nat_percentage;
1966 struct ProgressMeter *conn_meter;
1968 nat_percentage = 0.6; /* FIXME: default percentage? */
1969 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1973 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1975 GNUNET_ERROR_TYPE_WARNING,
1977 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1978 p_string, "PERCENTAGE", "TESTING");
1979 GNUNET_free (p_string);
1982 cutoff = (unsigned int) (nat_percentage * pg->total);
1984 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1986 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1988 if ((outer_count > cutoff) || (inner_count > cutoff))
1994 conn_meter = create_meter (count, "NAT COPY", GNUNET_YES);
1995 connect_attempts = 0;
1996 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1998 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2000 if ((outer_count > cutoff) || (inner_count > cutoff))
2003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2004 "Connecting peer %d to peer %d\n",
2005 outer_count, inner_count);
2007 connect_attempts += proc (pg, outer_count, inner_count, list,
2009 add_connections (pg, outer_count, inner_count, ALLOWED, GNUNET_NO);
2010 update_meter (conn_meter);
2014 free_meter (conn_meter);
2016 return connect_attempts;
2021 * Create a topology given a peer group (set of running peers)
2022 * and a connection processor.
2024 * @param pg the peergroup to create the topology on
2025 * @param proc the connection processor to call to actually set
2026 * up connections between two peers
2027 * @param list the peer list to use
2029 * @return the number of connections that were set up
2033 create_small_world(struct GNUNET_TESTING_PeerGroup *pg,
2034 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2036 unsigned int i, j, k;
2037 unsigned int square;
2040 unsigned int toggle = 1;
2041 unsigned int nodeToConnect;
2042 unsigned int natLog;
2043 unsigned int node1Row;
2044 unsigned int node1Col;
2045 unsigned int node2Row;
2046 unsigned int node2Col;
2047 unsigned int distance;
2048 double probability, random, percentage;
2049 unsigned int smallWorldConnections;
2050 unsigned int small_world_it;
2052 int connect_attempts;
2053 square = floor (sqrt (pg->total));
2057 percentage = 0.5; /* FIXME: default percentage? */
2058 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2062 if (sscanf (p_string, "%lf", &percentage) != 1)
2064 GNUNET_ERROR_TYPE_WARNING,
2066 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2067 p_string, "PERCENTAGE", "TESTING");
2068 GNUNET_free (p_string);
2070 if (percentage < 0.0)
2073 GNUNET_ERROR_TYPE_WARNING,
2075 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
2076 "PERCENTAGE", "TESTING", percentage);
2079 probability = 0.5; /* FIXME: default percentage? */
2080 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2084 if (sscanf (p_string, "%lf", &probability) != 1)
2086 GNUNET_ERROR_TYPE_WARNING,
2088 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2089 p_string, "PROBABILITY", "TESTING");
2090 GNUNET_free (p_string);
2092 if (square * square != pg->total)
2094 while (rows * cols < pg->total)
2096 if (toggle % 2 == 0)
2105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2107 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2111 connect_attempts = 0;
2112 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2113 * to the node to its right and above. Once this is over, we'll have our torus!
2114 * Special case for the last node (if the rows and columns are not equal), connect
2115 * to the first in the row to maintain topology.
2117 for (i = 0; i < pg->total; i++)
2119 /* First connect to the node to the right */
2120 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2121 nodeToConnect = i + 1;
2122 else if (i + 1 == pg->total)
2123 nodeToConnect = rows * cols - cols;
2125 nodeToConnect = i - cols + 1;
2127 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2131 nodeToConnect = (rows * cols) - cols + i;
2132 if (nodeToConnect >= pg->total)
2133 nodeToConnect -= cols;
2136 nodeToConnect = i - cols;
2138 if (nodeToConnect < pg->total)
2139 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2141 natLog = log (pg->total);
2142 #if VERBOSE_TESTING > 2
2143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2144 _("natural log of %d is %d, will run %d iterations\n"),
2145 pg->total, natLog, (int) (natLog * percentage));
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2147 _("Total connections added thus far: %u!\n"), connect_attempts);
2149 smallWorldConnections = 0;
2150 small_world_it = (unsigned int) (natLog * percentage);
2151 if (small_world_it < 1)
2153 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
2154 for (i = 0; i < small_world_it; i++)
2156 for (j = 0; j < pg->total; j++)
2158 /* Determine the row and column of node at position j on the 2d torus */
2159 node1Row = j / cols;
2160 node1Col = j - (node1Row * cols);
2161 for (k = 0; k < pg->total; k++)
2163 /* Determine the row and column of node at position k on the 2d torus */
2164 node2Row = k / cols;
2165 node2Col = k - (node2Row * cols);
2166 /* Simple Cartesian distance */
2167 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
2170 /* Calculate probability as 1 over the square of the distance */
2171 probability = 1.0 / (distance * distance);
2172 /* Choose a random value between 0 and 1 */
2174 = ((double) GNUNET_CRYPTO_random_u64 (
2175 GNUNET_CRYPTO_QUALITY_WEAK,
2177 / ((double) UINT64_MAX);
2178 /* If random < probability, then connect the two nodes */
2179 if (random < probability)
2180 smallWorldConnections += proc (pg, j, k, list, GNUNET_YES);
2186 connect_attempts += smallWorldConnections;
2187 #if VERBOSE_TESTING > 2
2188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2189 _("Total connections added for small world: %d!\n"),
2190 smallWorldConnections);
2192 return connect_attempts;
2196 * Create a topology given a peer group (set of running peers)
2197 * and a connection processor.
2199 * @param pg the peergroup to create the topology on
2200 * @param proc the connection processor to call to actually set
2201 * up connections between two peers
2202 * @param list the peer list to use
2204 * @return the number of connections that were set up
2208 create_erdos_renyi(struct GNUNET_TESTING_PeerGroup *pg,
2209 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2212 unsigned int outer_count;
2213 unsigned int inner_count;
2214 int connect_attempts;
2218 probability = 0.5; /* FIXME: default percentage? */
2219 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2223 if (sscanf (p_string, "%lf", &probability) != 1)
2225 GNUNET_ERROR_TYPE_WARNING,
2227 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2228 p_string, "PROBABILITY", "TESTING");
2229 GNUNET_free (p_string);
2231 connect_attempts = 0;
2232 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2234 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2237 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2239 / ((double) UINT64_MAX);
2241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2242 _("rand is %f probability is %f\n"), temp_rand,
2245 if (temp_rand < probability)
2247 connect_attempts += proc (pg, outer_count, inner_count, list,
2253 return connect_attempts;
2257 * Create a topology given a peer group (set of running peers)
2258 * and a connection processor. This particular function creates
2259 * the connections for a 2d-torus, plus additional "closest"
2260 * connections per peer.
2262 * @param pg the peergroup to create the topology on
2263 * @param proc the connection processor to call to actually set
2264 * up connections between two peers
2265 * @param list the peer list to use
2267 * @return the number of connections that were set up
2271 create_2d_torus(struct GNUNET_TESTING_PeerGroup *pg,
2272 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2275 unsigned int square;
2278 unsigned int toggle = 1;
2279 unsigned int nodeToConnect;
2280 int connect_attempts;
2282 connect_attempts = 0;
2284 square = floor (sqrt (pg->total));
2288 if (square * square != pg->total)
2290 while (rows * cols < pg->total)
2292 if (toggle % 2 == 0)
2301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2303 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2306 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2307 * to the node to its right and above. Once this is over, we'll have our torus!
2308 * Special case for the last node (if the rows and columns are not equal), connect
2309 * to the first in the row to maintain topology.
2311 for (i = 0; i < pg->total; i++)
2313 /* First connect to the node to the right */
2314 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2315 nodeToConnect = i + 1;
2316 else if (i + 1 == pg->total)
2317 nodeToConnect = rows * cols - cols;
2319 nodeToConnect = i - cols + 1;
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2322 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2324 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2326 /* Second connect to the node immediately above */
2329 nodeToConnect = (rows * cols) - cols + i;
2330 if (nodeToConnect >= pg->total)
2331 nodeToConnect -= cols;
2334 nodeToConnect = i - cols;
2336 if (nodeToConnect < pg->total)
2339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2340 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2342 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2347 return connect_attempts;
2351 * Create a topology given a peer group (set of running peers)
2352 * and a connection processor.
2354 * @param pg the peergroup to create the topology on
2355 * @param proc the connection processor to call to actually set
2356 * up connections between two peers
2357 * @param list the peer list to use
2358 * @param check does the connection processor need to check before
2359 * performing an action on the list?
2361 * @return the number of connections that were set up
2365 create_clique(struct GNUNET_TESTING_PeerGroup *pg,
2366 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list,
2369 unsigned int outer_count;
2370 unsigned int inner_count;
2371 int connect_attempts;
2372 struct ProgressMeter *conn_meter;
2373 connect_attempts = 0;
2375 conn_meter = create_meter ((((pg->total * pg->total) + pg->total) / 2)
2376 - pg->total, "Create Clique ", GNUNET_NO);
2377 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2379 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2383 "Connecting peer %d to peer %d\n",
2384 outer_count, inner_count);
2386 connect_attempts += proc (pg, outer_count, inner_count, list, check);
2387 update_meter (conn_meter);
2390 reset_meter (conn_meter);
2391 free_meter (conn_meter);
2392 return connect_attempts;
2397 * Iterator over hash map entries.
2399 * @param cls closure the peer group
2400 * @param key the key stored in the hashmap is the
2401 * index of the peer to connect to
2402 * @param value value in the hash map, handle to the peer daemon
2403 * @return GNUNET_YES if we should continue to
2408 unblacklist_iterator (void *cls,
2409 const GNUNET_HashCode * key,
2412 struct UnblacklistContext *un_ctx = cls;
2413 uint32_t second_pos;
2415 uid_from_hash (key, &second_pos);
2417 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2424 * Create a blacklist topology based on the allowed topology
2425 * which disallows any connections not in the allowed topology
2426 * at the transport level.
2428 * @param pg the peergroup to create the topology on
2429 * @param proc the connection processor to call to allow
2430 * up connections between two peers
2432 * @return the number of connections that were set up
2436 copy_allowed(struct GNUNET_TESTING_PeerGroup *pg,
2437 GNUNET_TESTING_ConnectionProcessor proc)
2441 struct PeerConnection *iter;
2443 struct UnblacklistContext un_ctx;
2448 for (count = 0; count < pg->total - 1; count++)
2451 iter = pg->peers[count].allowed_peers_head;
2452 while (iter != NULL)
2454 remove_connections (pg, count, iter->index, BLACKLIST, GNUNET_YES);
2455 //unblacklist_connections(pg, count, iter->index);
2459 un_ctx.first_uid = count;
2460 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers,
2461 &unblacklist_iterator,
2465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2466 "Unblacklisted %u peers\n",
2472 * Create a topology given a peer group (set of running peers)
2473 * and a connection processor.
2475 * @param pg the peergroup to create the topology on
2476 * @param proc the connection processor to call to actually set
2477 * up connections between two peers
2478 * @param list which list should be modified
2480 * @return the number of connections that were set up
2484 create_line(struct GNUNET_TESTING_PeerGroup *pg,
2485 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2488 unsigned int connect_attempts;
2490 connect_attempts = 0;
2491 /* Connect each peer to the next highest numbered peer */
2492 for (count = 0; count < pg->total - 1; count++)
2495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2496 "Connecting peer %d to peer %d\n",
2499 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2502 return connect_attempts;
2506 * Create a topology given a peer group (set of running peers)
2507 * and a connection processor.
2509 * @param pg the peergroup to create the topology on
2510 * @param filename the file to read topology information from
2511 * @param proc the connection processor to call to actually set
2512 * up connections between two peers
2513 * @param list the peer list to use
2515 * @return the number of connections that were set up
2519 create_from_file(struct GNUNET_TESTING_PeerGroup *pg, char *filename,
2520 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2522 int connect_attempts;
2523 unsigned int first_peer_index;
2524 unsigned int second_peer_index;
2529 unsigned int total_peers;
2530 enum States curr_state;
2532 connect_attempts = 0;
2533 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2534 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2536 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2538 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2539 "Could not open file `%s' specified for topology!", filename);
2540 return connect_attempts;
2543 data = GNUNET_malloc_large (frstat.st_size);
2544 GNUNET_assert(data != NULL);
2545 if (frstat.st_size != GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2548 GNUNET_ERROR_TYPE_ERROR,
2549 "Could not read file %s specified for host list, ending test!",
2552 return connect_attempts;
2557 first_peer_index = 0;
2558 /* First line should contain a single integer, specifying the number of peers */
2559 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2560 curr_state = NUM_PEERS;
2561 while (count < frstat.st_size - 1)
2563 if ((buf[count] == '\n') || (buf[count] == ' '))
2573 total_peers = strtoul(&buf[count], NULL, 10);
2576 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2577 "Failed to read number of peers from topology file!\n");
2579 return connect_attempts;
2581 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2582 "Read %u total peers in topology\n", total_peers);
2583 GNUNET_assert(total_peers == pg->total);
2584 curr_state = PEER_INDEX;
2585 while ((buf[count] != '\n') && (count < frstat.st_size - 1))
2591 first_peer_index = strtoul(&buf[count], NULL, 10);
2594 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2595 "Failed to read peer index from topology file!\n");
2597 return connect_attempts;
2599 while ((buf[count] != ':') && (count < frstat.st_size - 1))
2602 curr_state = OTHER_PEER_INDEX;
2605 if (1 == sscanf (&buf[count], ":"))
2606 curr_state = OTHER_PEER_INDEX;
2609 case OTHER_PEER_INDEX:
2611 second_peer_index = strtoul(&buf[count], NULL, 10);
2614 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2615 "Failed to peer index from topology file!\n");
2617 return connect_attempts;
2619 /* Assume file is written with first peer 1, but array index is 0 */
2620 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index
2621 - 1, list, GNUNET_YES);
2622 while ((buf[count] != '\n') && (buf[count] != ',') && (count
2623 < frstat.st_size - 1))
2625 if (buf[count] == '\n')
2627 curr_state = PEER_INDEX;
2629 else if (buf[count] != ',')
2631 curr_state = OTHER_PEER_INDEX;
2636 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2637 "Found bad data in topology file while in state %d!\n",
2641 return connect_attempts;
2645 return connect_attempts;
2649 * Create a topology given a peer group (set of running peers)
2650 * and a connection processor.
2652 * @param pg the peergroup to create the topology on
2653 * @param proc the connection processor to call to actually set
2654 * up connections between two peers
2655 * @param list the peer list to use
2657 * @return the number of connections that were set up
2661 create_ring(struct GNUNET_TESTING_PeerGroup *pg,
2662 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2665 int connect_attempts;
2667 connect_attempts = 0;
2669 /* Connect each peer to the next highest numbered peer */
2670 for (count = 0; count < pg->total - 1; count++)
2673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2674 "Connecting peer %d to peer %d\n", count, count + 1);
2676 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2679 /* Connect the last peer to the first peer */
2680 connect_attempts += proc (pg, pg->total - 1, 0, list, GNUNET_YES);
2682 return connect_attempts;
2687 * Iterator for writing friends of a peer to a file.
2689 * @param cls closure, an open writable file handle
2690 * @param key the key the daemon was stored under
2691 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2693 * @return GNUNET_YES to continue iteration
2695 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2696 * with a single file_iterator that takes a closure which contains
2697 * the prefix to write before the peer. Then this could be used
2698 * for blacklisting multiple transports and writing the friend
2699 * file. I'm sure *someone* will complain loudly about other
2700 * things that negate these functions even existing so no point in
2704 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2706 FILE *temp_friend_handle = cls;
2707 struct GNUNET_TESTING_Daemon *peer = value;
2708 struct GNUNET_PeerIdentity *temppeer;
2709 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2711 temppeer = &peer->id;
2712 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2713 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2718 struct BlacklistContext
2721 * The (open) file handle to write to
2723 FILE *temp_file_handle;
2726 * The transport that this peer will be blacklisted on.
2732 * Iterator for writing blacklist data to appropriate files.
2734 * @param cls closure, an open writable file handle
2735 * @param key the key the daemon was stored under
2736 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2738 * @return GNUNET_YES to continue iteration
2741 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2743 struct BlacklistContext *blacklist_ctx = cls;
2744 struct GNUNET_TESTING_Daemon *peer = value;
2745 struct GNUNET_PeerIdentity *temppeer;
2746 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2748 temppeer = &peer->id;
2749 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2750 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2751 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2752 blacklist_ctx->transport, (char *) &peer_enc);
2759 * Create the friend files based on the PeerConnection's
2760 * of each peer in the peer group, and copy the files
2761 * to the appropriate place
2763 * @param pg the peer group we are dealing with
2766 create_and_copy_friend_files(struct GNUNET_TESTING_PeerGroup *pg)
2768 FILE *temp_friend_handle;
2769 unsigned int pg_iter;
2770 char *temp_service_path;
2771 struct GNUNET_OS_Process **procarr;
2775 enum GNUNET_OS_ProcessStatusType type;
2776 unsigned long return_code;
2784 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2785 struct PeerConnection *conn_iter;
2787 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2788 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2790 mytemp = GNUNET_DISK_mktemp ("friends");
2791 GNUNET_assert (mytemp != NULL);
2792 temp_friend_handle = fopen (mytemp, "wt");
2793 GNUNET_assert (temp_friend_handle != NULL);
2795 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2796 while (conn_iter != NULL)
2798 GNUNET_CRYPTO_hash_to_enc (
2799 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2801 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2802 conn_iter = conn_iter->next;
2805 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2806 &friend_file_iterator,
2807 temp_friend_handle);
2809 fclose (temp_friend_handle);
2812 != GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2814 "PATHS", "SERVICEHOME",
2815 &temp_service_path))
2817 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2818 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2819 "SERVICEHOME", "PATHS");
2820 if (UNLINK (mytemp) != 0)
2821 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
2824 GNUNET_free (mytemp);
2828 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2830 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2831 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
2833 GNUNET_assert(procarr[pg_iter] != NULL);
2835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2836 "Copying file with command cp %s %s\n",
2840 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2841 GNUNET_OS_process_close (procarr[pg_iter]);
2844 else /* Remote, scp the file to the correct place */
2846 if (NULL != pg->peers[pg_iter].daemon->username)
2847 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2848 pg->peers[pg_iter].daemon->username,
2849 pg->peers[pg_iter].daemon->hostname,
2852 GNUNET_asprintf (&arg, "%s:%s/friends",
2853 pg->peers[pg_iter].daemon->hostname,
2855 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
2857 GNUNET_assert(procarr[pg_iter] != NULL);
2858 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2859 GNUNET_OS_process_close (procarr[pg_iter]);
2860 if (ret != GNUNET_OK)
2862 /* FIXME: free contents of 'procarr' array */
2863 GNUNET_free (procarr);
2866 procarr[pg_iter] = NULL;
2868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2869 "Copying file with command scp %s %s\n",
2875 GNUNET_free (temp_service_path);
2876 GNUNET_free (mytemp);
2881 ret = GNUNET_SYSERR;
2882 while ((count < max_wait) && (ret != GNUNET_OK))
2885 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2889 "Checking copy status of file %d\n",
2892 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2894 if (GNUNET_OS_process_status
2895 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2897 ret = GNUNET_SYSERR;
2899 else if ((type != GNUNET_OS_PROCESS_EXITED)
2900 || (return_code != 0))
2902 ret = GNUNET_SYSERR;
2906 GNUNET_OS_process_close (procarr[pg_iter]);
2907 procarr[pg_iter] = NULL;
2909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2917 if (ret == GNUNET_SYSERR)
2919 /* FIXME: why sleep here? -CG */
2925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2926 _("Finished copying all friend files!\n"));
2929 GNUNET_free (procarr);
2934 * Create the blacklist files based on the PeerConnection's
2935 * of each peer in the peer group, and copy the files
2936 * to the appropriate place.
2938 * @param pg the peer group we are dealing with
2939 * @param transports space delimited list of transports to blacklist
2942 create_and_copy_blacklist_files(struct GNUNET_TESTING_PeerGroup *pg,
2943 const char *transports)
2945 FILE *temp_file_handle;
2946 unsigned int pg_iter;
2947 char *temp_service_path;
2948 struct GNUNET_OS_Process **procarr;
2951 enum GNUNET_OS_ProcessStatusType type;
2952 unsigned long return_code;
2959 char *temp_transports;
2961 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2962 struct PeerConnection *conn_iter;
2964 static struct BlacklistContext blacklist_ctx;
2967 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2968 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2970 mytemp = GNUNET_DISK_mktemp ("blacklist");
2971 GNUNET_assert (mytemp != NULL);
2972 temp_file_handle = fopen (mytemp, "wt");
2973 GNUNET_assert (temp_file_handle != NULL);
2974 temp_transports = GNUNET_strdup (transports);
2976 blacklist_ctx.temp_file_handle = temp_file_handle;
2978 transport_len = strlen (temp_transports) + 1;
2981 for (i = 0; i < transport_len; i++)
2983 if ((temp_transports[i] == ' ') && (pos == NULL))
2984 continue; /* At start of string (whitespace) */
2985 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2987 temp_transports[i] = '\0';
2989 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
2990 while (conn_iter != NULL)
2992 GNUNET_CRYPTO_hash_to_enc (
2993 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2995 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
2996 conn_iter = conn_iter->next;
2999 blacklist_ctx.transport = pos;
3000 (void) GNUNET_CONTAINER_multihashmap_iterate (pg->
3002 [pg_iter].blacklisted_peers,
3003 &blacklist_file_iterator,
3007 } /* At beginning of actual string */
3008 else if (pos == NULL)
3010 pos = &temp_transports[i];
3014 GNUNET_free (temp_transports);
3015 fclose (temp_file_handle);
3018 != GNUNET_CONFIGURATION_get_value_string (
3019 pg->peers[pg_iter]. daemon->cfg,
3020 "PATHS", "SERVICEHOME",
3021 &temp_service_path))
3024 GNUNET_ERROR_TYPE_WARNING,
3026 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
3027 "SERVICEHOME", "PATHS");
3028 if (UNLINK (mytemp) != 0)
3029 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
3031 GNUNET_free (mytemp);
3035 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
3037 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
3038 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
3041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3042 _("Copying file with command cp %s %s\n"), mytemp, arg);
3047 else /* Remote, scp the file to the correct place */
3049 if (NULL != pg->peers[pg_iter].daemon->username)
3050 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
3051 pg->peers[pg_iter].daemon->username,
3052 pg->peers[pg_iter].daemon->hostname,
3055 GNUNET_asprintf (&arg, "%s:%s/blacklist",
3056 pg->peers[pg_iter].daemon->hostname,
3058 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
3060 GNUNET_assert(procarr[pg_iter] != NULL);
3061 GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
3064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3065 _("Copying file with command scp %s %s\n"), mytemp,
3070 GNUNET_free (temp_service_path);
3071 GNUNET_free (mytemp);
3075 ret = GNUNET_SYSERR;
3076 while ((count < max_wait) && (ret != GNUNET_OK))
3079 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3083 _("Checking copy status of file %d\n"), pg_iter);
3085 if (procarr[pg_iter] != NULL) /* Check for already completed! */
3087 if (GNUNET_OS_process_status (procarr[pg_iter], &type,
3088 &return_code) != GNUNET_OK)
3090 ret = GNUNET_SYSERR;
3092 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
3094 ret = GNUNET_SYSERR;
3098 GNUNET_OS_process_close (procarr[pg_iter]);
3099 procarr[pg_iter] = NULL;
3101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3102 _("File %d copied\n"), pg_iter);
3108 if (ret == GNUNET_SYSERR)
3110 /* FIXME: why sleep here? -CG */
3116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3117 _("Finished copying all blacklist files!\n"));
3119 GNUNET_free (procarr);
3123 /* Forward Declaration */
3125 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3128 * Choose a random peer's next connection to create, and
3129 * call schedule_connect to set up the connect task.
3131 * @param ct_ctx the overall connection context
3134 preschedule_connect(struct GNUNET_TESTING_PeerGroup *pg)
3136 struct ConnectTopologyContext *ct_ctx = &pg->ct_ctx;
3137 struct PeerConnection *connection_iter;
3138 struct ConnectContext *connect_context;
3139 uint32_t random_peer;
3141 if (ct_ctx->remaining_connections == 0)
3144 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3145 while (pg->peers[random_peer].connect_peers_head == NULL)
3146 random_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3149 connection_iter = pg->peers[random_peer].connect_peers_head;
3150 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3151 connect_context->first_index = random_peer;
3152 connect_context->second_index = connection_iter->index;
3153 connect_context->ct_ctx = ct_ctx;
3154 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3155 GNUNET_CONTAINER_DLL_remove(pg->peers[random_peer].connect_peers_head, pg->peers[random_peer].connect_peers_tail, connection_iter);
3156 GNUNET_free(connection_iter);
3157 ct_ctx->remaining_connections--;
3161 /* Forward declaration */
3162 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3165 * Close connections and free the hello context.
3167 * @param cls the 'struct SendHelloContext *'
3168 * @param tc scheduler context
3171 free_hello_context (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3173 struct SendHelloContext *send_hello_context = cls;
3174 if (send_hello_context->peer->daemon->server != NULL)
3176 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3177 send_hello_context->peer->daemon->server = NULL;
3179 if (send_hello_context->peer->daemon->th != NULL)
3181 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3182 send_hello_context->peer->daemon->th = NULL;
3184 if (send_hello_context->core_connect_task != GNUNET_SCHEDULER_NO_TASK)
3186 GNUNET_SCHEDULER_cancel(send_hello_context->core_connect_task);
3187 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3189 send_hello_context->pg->outstanding_connects--;
3190 GNUNET_free(send_hello_context);
3194 * For peers that haven't yet connected, notify
3195 * the caller that they have failed (timeout).
3197 * @param cls the 'struct SendHelloContext *'
3198 * @param tc scheduler context
3201 notify_remaining_connections_failed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3203 struct SendHelloContext *send_hello_context = cls;
3204 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3205 struct PeerConnection *connection;
3207 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3208 send_hello_context->peer->daemon->server = NULL;
3210 connection = send_hello_context->peer->connect_peers_head;
3212 while (connection != NULL)
3214 if (pg->notify_connection != NULL)
3216 pg->notify_connection(pg->notify_connection_cls,
3217 &send_hello_context->peer->daemon->id,
3218 &pg->peers[connection->index].daemon->id,
3220 send_hello_context->peer->daemon->cfg,
3221 pg->peers[connection->index].daemon->cfg,
3222 send_hello_context->peer->daemon,
3223 pg->peers[connection->index].daemon,
3224 "Peers failed to connect (timeout)");
3226 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3227 GNUNET_free(connection);
3228 connection = connection->next;
3230 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3232 other_peer = &pg->peers[connection->index];
3237 * For peers that haven't yet connected, send
3238 * CORE connect requests.
3240 * @param cls the 'struct SendHelloContext *'
3241 * @param tc scheduler context
3244 send_core_connect_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3246 struct SendHelloContext *send_hello_context = cls;
3247 struct PeerConnection *conn;
3248 GNUNET_assert(send_hello_context->peer->daemon->server != NULL);
3250 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3252 send_hello_context->connect_attempts++;
3253 if (send_hello_context->connect_attempts < send_hello_context->pg->ct_ctx.connect_attempts)
3255 conn = send_hello_context->peer->connect_peers_head;
3256 while (conn != NULL)
3258 GNUNET_CORE_peer_request_connect(send_hello_context->peer->daemon->server,
3259 &send_hello_context->pg->peers[conn->index].daemon->id,
3264 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) ,
3265 &send_core_connect_requests,
3266 send_hello_context);
3270 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Timeout before all connections created, marking rest as failed!\n");
3271 GNUNET_SCHEDULER_add_now(¬ify_remaining_connections_failed, send_hello_context);
3277 * Success, connection is up. Signal client our success.
3279 * @param cls our "struct SendHelloContext"
3280 * @param peer identity of the peer that has connected
3281 * @param atsi performance information
3283 * FIXME: remove peers from BOTH lists, call notify twice, should
3284 * double the speed of connections as long as the list iteration
3285 * doesn't take too long!
3288 core_connect_notify (void *cls,
3289 const struct GNUNET_PeerIdentity *peer,
3290 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3292 struct SendHelloContext *send_hello_context = cls;
3293 struct PeerConnection *connection;
3294 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3296 struct PeerData *other_peer;
3299 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3300 "Connected peer %s to peer %s\n",
3301 ctx->d1->shortname, GNUNET_i2s(peer));
3304 if (0 == memcmp(&send_hello_context->peer->daemon->id, peer, sizeof(struct GNUNET_PeerIdentity)))
3307 connection = send_hello_context->peer->connect_peers_head;
3312 while ((connection != NULL) &&
3313 (0 != memcmp(&pg->peers[connection->index].daemon->id, peer, sizeof(struct GNUNET_PeerIdentity))))
3315 connection = connection->next;
3318 if (connection == NULL)
3320 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);
3325 other_peer = &pg->peers[connection->index];
3327 if (pg->notify_connection != NULL)
3329 pg->notify_connection(pg->notify_connection_cls,
3330 &send_hello_context->peer->daemon->id,
3333 send_hello_context->peer->daemon->cfg,
3334 pg->peers[connection->index].daemon->cfg,
3335 send_hello_context->peer->daemon,
3336 pg->peers[connection->index].daemon,
3339 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3340 GNUNET_free(connection);
3344 /* Notify of reverse connection and remove from other peers list of outstanding */
3345 if (other_peer != NULL)
3347 connection = other_peer->connect_peers_head;
3348 while ((connection != NULL) &&
3349 (0 != memcmp(&send_hello_context->peer->daemon->id, &pg->peers[connection->index].daemon->id, sizeof(struct GNUNET_PeerIdentity))))
3351 connection = connection->next;
3353 if (connection != NULL)
3355 if (pg->notify_connection != NULL)
3357 pg->notify_connection(pg->notify_connection_cls,
3359 &send_hello_context->peer->daemon->id,
3361 pg->peers[connection->index].daemon->cfg,
3362 send_hello_context->peer->daemon->cfg,
3363 pg->peers[connection->index].daemon,
3364 send_hello_context->peer->daemon,
3368 GNUNET_CONTAINER_DLL_remove(other_peer->connect_peers_head, other_peer->connect_peers_tail, connection);
3369 GNUNET_free(connection);
3374 if (send_hello_context->peer->connect_peers_head == NULL)
3376 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3381 * Notify of a successful connection to the core service.
3383 * @param cls a struct SendHelloContext *
3384 * @param server handle to the core service
3385 * @param my_identity the peer identity of this peer
3386 * @param publicKey the public key of the peer
3389 core_init (void *cls,
3390 struct GNUNET_CORE_Handle * server,
3391 const struct GNUNET_PeerIdentity *
3394 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
3397 struct SendHelloContext *send_hello_context = cls;
3398 send_hello_context->core_ready = GNUNET_YES;
3402 * Function called once a hello has been sent
3403 * to the transport, move on to the next one
3404 * or go away forever.
3406 * @param cls the 'struct SendHelloContext *'
3407 * @param tc scheduler context
3410 hello_sent_callback (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3412 struct SendHelloContext *send_hello_context = cls;
3413 //unsigned int pg_iter;
3414 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3416 GNUNET_free(send_hello_context);
3420 send_hello_context->pg->remaining_hellos--;
3422 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sent HELLO, have %d remaining!\n", send_hello_context->pg->remaining_hellos);
3424 if (send_hello_context->peer_pos == NULL) /* All HELLOs (for this peer!) have been transmitted! */
3427 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All hellos for this peer sent, disconnecting transport!\n");
3429 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3430 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3431 send_hello_context->peer->daemon->th = NULL;
3433 /*if (send_hello_context->pg->remaining_hellos == 0)
3435 for (pg_iter = 0; pg_iter < send_hello_context->pg->max_outstanding_connections; pg_iter++)
3437 preschedule_connect(&send_hello_context->pg->ct_ctx);
3441 GNUNET_assert (send_hello_context->peer->daemon->server == NULL);
3442 send_hello_context->peer->daemon->server = GNUNET_CORE_connect(send_hello_context->peer->cfg,
3446 &core_connect_notify,
3453 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),
3454 &send_core_connect_requests,
3455 send_hello_context);
3458 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3462 * Connect to a peer, give it all the HELLO's of those peers
3463 * we will later ask it to connect to.
3465 * @param ct_ctx the overall connection context
3467 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3469 struct SendHelloContext *send_hello_context = cls;
3470 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3472 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3474 GNUNET_free(send_hello_context);
3478 GNUNET_assert(send_hello_context->peer_pos != NULL); /* All of the HELLO sends to be scheduled have been scheduled! */
3480 if (((send_hello_context->peer->daemon->th == NULL) &&
3481 (pg->outstanding_connects > pg->max_outstanding_connections)) ||
3482 (pg->stop_connects == GNUNET_YES))
3484 #if VERBOSE_TESTING > 2
3485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3487 ("Delaying connect, we have too many outstanding connections!\n"));
3489 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3490 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3491 &schedule_send_hellos, send_hello_context);
3495 #if VERBOSE_TESTING > 2
3496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3497 _("Creating connection, outstanding_connections is %d\n"),
3498 outstanding_connects);
3500 if (send_hello_context->peer->daemon->th == NULL)
3502 pg->outstanding_connects++; /* Actual TRANSPORT, CORE connections! */
3503 send_hello_context->peer->daemon->th
3504 = GNUNET_TRANSPORT_connect (send_hello_context->peer->cfg, NULL,
3505 send_hello_context, NULL, NULL, NULL);
3508 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3509 _("Offering Hello of peer %s to peer %s\n"),
3510 send_hello_context->peer->daemon->shortname, pg->peers[send_hello_context->peer_pos->index].daemon->shortname);
3512 GNUNET_TRANSPORT_offer_hello(send_hello_context->peer->daemon->th,
3513 (const struct GNUNET_MessageHeader *)pg->peers[send_hello_context->peer_pos->index].daemon->hello,
3514 &hello_sent_callback,
3515 send_hello_context);
3516 send_hello_context->peer_pos = send_hello_context->peer_pos->next;
3517 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3523 * Internal notification of a connection, kept so that we can ensure some connections
3524 * happen instead of flooding all testing daemons with requests to connect.
3527 internal_connect_notify(void *cls, const struct GNUNET_PeerIdentity *first,
3528 const struct GNUNET_PeerIdentity *second,
3530 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
3531 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
3532 struct GNUNET_TESTING_Daemon *first_daemon,
3533 struct GNUNET_TESTING_Daemon *second_daemon,
3536 struct ConnectContext *connect_ctx = cls;
3537 struct ConnectTopologyContext *ct_ctx = connect_ctx->ct_ctx;
3538 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
3539 struct PeerConnection *connection;
3541 GNUNET_assert (0 < pg->outstanding_connects);
3542 pg->outstanding_connects--;
3545 * Check whether the inverse connection has been scheduled yet,
3546 * if not, we can remove it from the other peers list and avoid
3547 * even trying to connect them again!
3549 connection = pg->peers[connect_ctx->second_index].connect_peers_head;
3554 while ((connection != NULL) && (0
3555 != memcmp (first, &pg->peers[connection->index].daemon->id,
3556 sizeof(struct GNUNET_PeerIdentity))))
3558 connection = connection->next;
3561 if (connection != NULL) /* Can safely remove! */
3563 GNUNET_assert (0 < ct_ctx->remaining_connections);
3564 ct_ctx->remaining_connections--;
3565 if (pg->notify_connection != NULL) /* Notify of reverse connection */
3566 pg->notify_connection (pg->notify_connection_cls, second, first,
3567 distance, second_cfg, first_cfg, second_daemon,
3568 first_daemon, emsg);
3570 GNUNET_CONTAINER_DLL_remove(pg->peers[connect_ctx->second_index].connect_peers_head, pg->peers[connect_ctx->second_index].connect_peers_tail, connection);
3571 GNUNET_free(connection);
3574 if (ct_ctx->remaining_connections == 0)
3576 if (ct_ctx->notify_connections_done != NULL)
3578 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
3579 ct_ctx->notify_connections_done = NULL;
3583 preschedule_connect (pg);
3585 if (pg->notify_connection != NULL)
3586 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
3587 first_cfg, second_cfg, first_daemon, second_daemon,
3590 GNUNET_free(connect_ctx);
3594 * Either delay a connection (because there are too many outstanding)
3595 * or schedule it for right now.
3597 * @param cls a connection context
3598 * @param tc the task runtime context
3601 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3603 struct ConnectContext *connect_context = cls;
3604 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
3606 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3609 if ((pg->outstanding_connects > pg->max_outstanding_connections)
3610 || (pg->stop_connects == GNUNET_YES))
3613 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3615 ("Delaying connect, we have too many outstanding connections!\n"));
3617 GNUNET_SCHEDULER_add_delayed (
3618 GNUNET_TIME_relative_multiply (
3619 GNUNET_TIME_UNIT_MILLISECONDS,
3621 &schedule_connect, connect_context);
3626 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3627 _("Creating connection, outstanding_connections is %d (max %d)\n"),
3628 pg->outstanding_connects, pg->max_outstanding_connections);
3630 pg->outstanding_connects++;
3631 pg->total_connects_scheduled++;
3632 GNUNET_TESTING_daemons_connect (
3633 pg->peers[connect_context->first_index].daemon,
3634 pg->peers[connect_context->second_index].daemon,
3635 connect_context->ct_ctx->connect_timeout,
3636 connect_context->ct_ctx->connect_attempts,
3642 &internal_connect_notify, connect_context); /* FIXME: free connect context! */
3648 * Iterator for actually scheduling connections to be created
3649 * between two peers.
3651 * @param cls closure, a GNUNET_TESTING_Daemon
3652 * @param key the key the second Daemon was stored under
3653 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3655 * @return GNUNET_YES to continue iteration
3658 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3660 struct ConnectTopologyContext *ct_ctx = cls;
3661 struct PeerData *first = ct_ctx->first;
3662 struct GNUNET_TESTING_Daemon *second = value;
3663 struct ConnectContext *connect_context;
3665 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3666 connect_context->first = first->daemon;
3667 connect_context->second = second;
3668 connect_context->ct_ctx = ct_ctx;
3669 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3677 * Iterator for copying all entries in the allowed hashmap to the
3680 * @param cls closure, a GNUNET_TESTING_Daemon
3681 * @param key the key the second Daemon was stored under
3682 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3684 * @return GNUNET_YES to continue iteration
3687 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3689 struct PeerData *first = cls;
3691 GNUNET_assert (GNUNET_OK ==
3692 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
3694 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3701 * Make the peers to connect the same as those that are allowed to be
3704 * @param pg the peer group
3707 copy_allowed_topology(struct GNUNET_TESTING_PeerGroup *pg)
3709 unsigned int pg_iter;
3713 struct PeerConnection *iter;
3717 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3720 iter = pg->peers[pg_iter].allowed_peers_head;
3721 while (iter != NULL)
3723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3724 "Creating connection between %d and %d\n", pg_iter,
3726 total += add_connections (pg, pg_iter, iter->index, CONNECT,
3728 //total += add_actual_connections(pg, pg_iter, iter->index);
3733 GNUNET_CONTAINER_multihashmap_iterate (pg->
3734 peers[pg_iter].allowed_peers,
3735 ©_topology_iterator,
3736 &pg->peers[pg_iter]);
3738 if (GNUNET_SYSERR == ret)
3739 return GNUNET_SYSERR;
3741 total = total + ret;
3748 * Connect the topology as specified by the PeerConnection's
3749 * of each peer in the peer group
3751 * @param pg the peer group we are dealing with
3752 * @param connect_timeout how long try connecting two peers
3753 * @param connect_attempts how many times (max) to attempt
3754 * @param notify_callback callback to notify when finished
3755 * @param notify_cls closure for notify callback
3757 * @return the number of connections that will be attempted
3760 connect_topology(struct GNUNET_TESTING_PeerGroup *pg,
3761 struct GNUNET_TIME_Relative connect_timeout,
3762 unsigned int connect_attempts,
3763 GNUNET_TESTING_NotifyCompletion notify_callback,
3766 unsigned int pg_iter;
3770 struct PeerConnection *connection_iter;
3773 struct SendHelloContext *send_hello_context;
3777 pg->ct_ctx.notify_connections_done = notify_callback;
3778 pg->ct_ctx.notify_cls = notify_cls;
3781 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3784 connection_iter = pg->peers[pg_iter].connect_peers_head;
3785 while (connection_iter != NULL)
3787 connection_iter = connection_iter->next;
3792 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3799 pg->ct_ctx.connect_timeout = connect_timeout;
3800 pg->ct_ctx.connect_attempts = connect_attempts;
3801 pg->ct_ctx.remaining_connections = total;
3804 /* First give all peers the HELLO's of other peers (connect to first peer's transport service, give HELLO's of other peers, continue...) */
3805 pg->remaining_hellos = total;
3806 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3808 send_hello_context = GNUNET_malloc(sizeof(struct SendHelloContext));
3809 send_hello_context->peer = &pg->peers[pg_iter];
3810 send_hello_context->peer_pos = pg->peers[pg_iter].connect_peers_head;
3811 send_hello_context->pg = pg;
3812 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3815 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3817 preschedule_connect (pg);
3825 * Takes a peer group and creates a topology based on the
3826 * one specified. Creates a topology means generates friend
3827 * files for the peers so they can only connect to those allowed
3828 * by the topology. This will only have an effect once peers
3829 * are started if the FRIENDS_ONLY option is set in the base
3830 * config. Also takes an optional restrict topology which
3831 * disallows connections based on particular transports
3832 * UNLESS they are specified in the restricted topology.
3834 * @param pg the peer group struct representing the running peers
3835 * @param topology which topology to connect the peers in
3836 * @param restrict_topology disallow restrict_transports transport
3837 * connections to peers NOT in this topology
3838 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3839 * @param restrict_transports space delimited list of transports to blacklist
3840 * to create restricted topology
3842 * @return the maximum number of connections were all allowed peers
3843 * connected to each other
3846 GNUNET_TESTING_create_topology(struct GNUNET_TESTING_PeerGroup *pg,
3847 enum GNUNET_TESTING_Topology topology,
3848 enum GNUNET_TESTING_Topology restrict_topology,
3849 const char *restrict_transports)
3853 unsigned int num_connections;
3854 int unblacklisted_connections;
3856 struct PeerConnection *conn_iter;
3857 struct PeerConnection *temp_conn;
3862 for (i = 0; i < pg->total; i++)
3864 pg->peers[i].allowed_peers =
3865 GNUNET_CONTAINER_multihashmap_create (100);
3866 pg->peers[i].connect_peers =
3867 GNUNET_CONTAINER_multihashmap_create (100);
3868 pg->peers[i].blacklisted_peers =
3869 GNUNET_CONTAINER_multihashmap_create (100);
3870 pg->peers[i].pg = pg;
3876 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3880 num_connections = create_clique (pg, &add_connections, ALLOWED, GNUNET_NO);
3882 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3885 _("Creating small world (ring) topology\n"));
3887 num_connections = create_small_world_ring (pg, &add_connections, ALLOWED);
3889 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3892 _("Creating small world (2d-torus) topology\n"));
3894 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3896 case GNUNET_TESTING_TOPOLOGY_RING:
3898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3900 num_connections = create_ring (pg, &add_connections, ALLOWED);
3902 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3906 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3908 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3911 _("Creating Erdos-Renyi topology\n"));
3913 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3915 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3919 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3921 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3924 _("Creating Scale Free topology\n"));
3926 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3928 case GNUNET_TESTING_TOPOLOGY_LINE:
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3931 _("Creating straight line topology\n"));
3933 num_connections = create_line (pg, &add_connections, ALLOWED);
3935 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3938 _("Creating topology from file!\n"));
3940 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing",
3943 num_connections = create_from_file (pg, filename, &add_connections,
3948 GNUNET_ERROR_TYPE_WARNING,
3949 "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3950 num_connections = 0;
3953 case GNUNET_TESTING_TOPOLOGY_NONE:
3955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3957 ("Creating no allowed topology (all peers can connect at core level)\n"));
3959 num_connections = pg->total * pg->total; /* Clique is allowed! */
3962 num_connections = 0;
3966 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING",
3969 ret = create_and_copy_friend_files (pg);
3970 if (ret != GNUNET_OK)
3973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3974 _("Failed during friend file copying!\n"));
3976 return GNUNET_SYSERR;
3981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3982 _("Friend files created/copied successfully!\n"));
3987 /* Use the create clique method to initially set all connections as blacklisted. */
3988 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology
3989 != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
3990 create_clique (pg, &add_connections, BLACKLIST, GNUNET_NO);
3992 return num_connections;
3994 unblacklisted_connections = 0;
3995 /* Un-blacklist connections as per the topology specified */
3996 switch (restrict_topology)
3998 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4001 _("Blacklisting all but clique topology\n"));
4003 unblacklisted_connections = create_clique (pg, &remove_connections,
4004 BLACKLIST, GNUNET_NO);
4006 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4009 _("Blacklisting all but small world (ring) topology\n"));
4011 unblacklisted_connections = create_small_world_ring (pg,
4012 &remove_connections,
4015 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4019 ("Blacklisting all but small world (2d-torus) topology\n"));
4021 unblacklisted_connections = create_small_world (pg, &remove_connections,
4024 case GNUNET_TESTING_TOPOLOGY_RING:
4026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4027 _("Blacklisting all but ring topology\n"));
4029 unblacklisted_connections
4030 = create_ring (pg, &remove_connections, BLACKLIST);
4032 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4035 _("Blacklisting all but 2d torus topology\n"));
4037 unblacklisted_connections = create_2d_torus (pg, &remove_connections,
4040 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4043 _("Blacklisting all but Erdos-Renyi topology\n"));
4045 unblacklisted_connections = create_erdos_renyi (pg, &remove_connections,
4048 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4051 _("Blacklisting all but InterNAT topology\n"));
4055 for (off = 0; off < pg->total; off++)
4057 conn_iter = pg->peers[off].allowed_peers_head;
4058 while (conn_iter != NULL)
4060 temp_conn = conn_iter->next;
4061 GNUNET_free(conn_iter);
4062 conn_iter = temp_conn;
4064 pg->peers[off].allowed_peers_head = NULL;
4065 pg->peers[off].allowed_peers_tail = NULL;
4067 conn_iter = pg->peers[off].connect_peers_head;
4068 while (conn_iter != NULL)
4070 temp_conn = conn_iter->next;
4071 GNUNET_free(conn_iter);
4072 conn_iter = temp_conn;
4074 pg->peers[off].connect_peers_head = NULL;
4075 pg->peers[off].connect_peers_tail = NULL;
4077 unblacklisted_connections
4078 = create_nated_internet_copy (pg, &remove_connections, BLACKLIST);
4080 unblacklisted_connections =
4081 create_nated_internet (pg, &remove_connections, BLACKLIST);
4085 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4088 _("Blacklisting all but Scale Free topology\n"));
4090 unblacklisted_connections = create_scale_free (pg, &remove_connections,
4093 case GNUNET_TESTING_TOPOLOGY_LINE:
4095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4096 _("Blacklisting all but straight line topology\n"));
4098 unblacklisted_connections
4099 = create_line (pg, &remove_connections, BLACKLIST);
4101 case GNUNET_TESTING_TOPOLOGY_NONE: /* Fall through */
4102 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
4104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4106 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
4108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
4109 ("Creating blacklist topology from allowed\n"));
4110 unblacklisted_connections = copy_allowed (pg, &remove_connections);
4115 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
4117 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n",
4118 restrict_transports);
4119 ret = create_and_copy_blacklist_files (pg, restrict_transports);
4120 if (ret != GNUNET_OK)
4123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4124 _("Failed during blacklist file copying!\n"));
4131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4132 _("Blacklist files created/copied successfully!\n"));
4136 return num_connections;
4141 * Iterator for choosing random peers to connect.
4143 * @param cls closure, a RandomContext
4144 * @param key the key the second Daemon was stored under
4145 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4147 * @return GNUNET_YES to continue iteration
4150 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4152 struct RandomContext *random_ctx = cls;
4153 double random_number;
4154 uint32_t second_pos;
4155 GNUNET_HashCode first_hash;
4158 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4159 UINT64_MAX)) / ((double) UINT64_MAX);
4160 if (random_number < random_ctx->percentage)
4162 GNUNET_assert (GNUNET_OK ==
4163 GNUNET_CONTAINER_multihashmap_put (random_ctx->
4164 first->connect_peers_working_set,
4166 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4169 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
4170 uid_from_hash (key, &second_pos);
4171 hash_from_uid (random_ctx->first_uid, &first_hash);
4172 GNUNET_assert (random_ctx->pg->total > second_pos);
4173 GNUNET_assert (GNUNET_YES ==
4174 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
4176 [second_pos].connect_peers,
4185 * Iterator for adding at least X peers to a peers connection set.
4187 * @param cls closure, MinimumContext
4188 * @param key the key the second Daemon was stored under
4189 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4191 * @return GNUNET_YES to continue iteration
4194 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4196 struct MinimumContext *min_ctx = cls;
4197 uint32_t second_pos;
4198 GNUNET_HashCode first_hash;
4201 if (GNUNET_CONTAINER_multihashmap_size
4202 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
4204 for (i = 0; i < min_ctx->num_to_add; i++)
4206 if (min_ctx->pg_array[i] == min_ctx->current)
4208 GNUNET_assert (GNUNET_OK ==
4209 GNUNET_CONTAINER_multihashmap_put
4210 (min_ctx->first->connect_peers_working_set, key,
4212 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4213 uid_from_hash (key, &second_pos);
4214 hash_from_uid (min_ctx->first_uid, &first_hash);
4215 GNUNET_assert (min_ctx->pg->total > second_pos);
4216 GNUNET_assert (GNUNET_OK ==
4217 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4219 [second_pos].connect_peers_working_set,
4223 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4224 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
4225 GNUNET_assert (GNUNET_YES ==
4226 GNUNET_CONTAINER_multihashmap_remove
4227 (min_ctx->pg->peers[second_pos].connect_peers,
4228 &first_hash, min_ctx->first->daemon));
4235 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
4240 * Iterator for adding peers to a connection set based on a depth first search.
4242 * @param cls closure, MinimumContext
4243 * @param key the key the second daemon was stored under
4244 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4246 * @return GNUNET_YES to continue iteration
4249 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4251 struct DFSContext *dfs_ctx = cls;
4252 GNUNET_HashCode first_hash;
4254 if (dfs_ctx->current == dfs_ctx->chosen)
4256 GNUNET_assert (GNUNET_OK ==
4257 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4258 first->connect_peers_working_set,
4260 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4261 uid_from_hash (key, &dfs_ctx->second_uid);
4262 hash_from_uid (dfs_ctx->first_uid, &first_hash);
4263 GNUNET_assert (GNUNET_OK ==
4264 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4266 [dfs_ctx->second_uid].connect_peers_working_set,
4270 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4271 GNUNET_assert (GNUNET_YES ==
4272 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
4274 [dfs_ctx->second_uid].connect_peers,
4278 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
4279 return GNUNET_NO; /* We have found our peer, don't iterate more */
4288 * From the set of connections possible, choose percentage percent of connections
4289 * to actually connect.
4291 * @param pg the peergroup we are dealing with
4292 * @param percentage what percent of total connections to make
4295 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg,
4300 struct PeerConnection *conn_iter;
4301 double random_number;
4303 struct RandomContext random_ctx;
4306 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4309 conn_iter = pg->peers[pg_iter].connect_peers_head;
4310 while (conn_iter != NULL)
4313 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4315 / ((double) UINT64_MAX);
4316 if (random_number < percentage)
4318 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4321 conn_iter = conn_iter->next;
4324 random_ctx.first_uid = pg_iter;
4325 random_ctx.first = &pg->peers[pg_iter];
4326 random_ctx.percentage = percentage;
4328 pg->peers[pg_iter].connect_peers_working_set
4329 = GNUNET_CONTAINER_multihashmap_create (pg->total);
4330 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4331 &random_connect_iterator,
4333 /* Now remove the old connections */
4334 GNUNET_CONTAINER_multihashmap_destroy (pg->
4335 peers[pg_iter].connect_peers);
4336 /* And replace with the random set */
4337 pg->peers[pg_iter].connect_peers
4338 = pg->peers[pg_iter].connect_peers_working_set;
4342 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4344 conn_iter = pg->peers[pg_iter].connect_peers_head;
4345 while (pg->peers[pg_iter].connect_peers_head != NULL)
4346 remove_connections (pg, pg_iter,
4347 pg->peers[pg_iter].connect_peers_head->index,
4348 CONNECT, GNUNET_YES);
4350 pg->peers[pg_iter].connect_peers_head
4351 = pg->peers[pg_iter].connect_peers_working_set_head;
4352 pg->peers[pg_iter].connect_peers_tail
4353 = pg->peers[pg_iter].connect_peers_working_set_tail;
4354 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4355 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4360 * Count the number of connections in a linked list of connections.
4362 * @param conn_list the connection list to get the count of
4364 * @return the number of elements in the list
4367 count_connections(struct PeerConnection *conn_list)
4369 struct PeerConnection *iter;
4373 while (iter != NULL)
4382 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
4385 unsigned int pg_iter;
4387 struct PeerConnection *conn_iter;
4391 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4394 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
4395 while (conn_iter != NULL)
4398 conn_iter = conn_iter->next;
4402 GNUNET_CONTAINER_multihashmap_size (pg->
4404 [pg_iter].connect_peers_working_set);
4412 count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
4415 unsigned int pg_iter;
4417 struct PeerConnection *conn_iter;
4421 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4424 conn_iter = pg->peers[pg_iter].allowed_peers_head;
4425 while (conn_iter != NULL)
4428 conn_iter = conn_iter->next;
4432 GNUNET_CONTAINER_multihashmap_size (pg->
4434 [pg_iter].allowed_peers);
4442 * From the set of connections possible, choose at least num connections per
4445 * @param pg the peergroup we are dealing with
4446 * @param num how many connections at least should each peer have (if possible)?
4449 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4452 struct MinimumContext minimum_ctx;
4454 struct PeerConnection *conn_iter;
4455 unsigned int temp_list_size;
4458 uint32_t random; /* Random list entry to connect peer to */
4463 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4466 = count_connections (pg->peers[pg_iter].connect_peers_head);
4467 if (temp_list_size == 0)
4469 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4470 "Peer %d has 0 connections!?!?\n", pg_iter);
4473 for (i = 0; i < num; i++)
4475 random = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4477 conn_iter = pg->peers[pg_iter].connect_peers_head;
4478 for (count = 0; count < random; count++)
4479 conn_iter = conn_iter->next;
4480 /* We now have a random connection, connect it! */
4481 GNUNET_assert(conn_iter != NULL);
4482 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4487 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4489 pg->peers[pg_iter].connect_peers_working_set =
4490 GNUNET_CONTAINER_multihashmap_create (num);
4493 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4495 minimum_ctx.first_uid = pg_iter;
4496 minimum_ctx.pg_array =
4497 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
4498 GNUNET_CONTAINER_multihashmap_size
4499 (pg->peers[pg_iter].connect_peers));
4500 minimum_ctx.first = &pg->peers[pg_iter];
4501 minimum_ctx.pg = pg;
4502 minimum_ctx.num_to_add = num;
4503 minimum_ctx.current = 0;
4504 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4505 &minimum_connect_iterator,
4509 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4511 /* Remove the "old" connections */
4512 GNUNET_CONTAINER_multihashmap_destroy (pg->
4513 peers[pg_iter].connect_peers);
4514 /* And replace with the working set */
4515 pg->peers[pg_iter].connect_peers =
4516 pg->peers[pg_iter].connect_peers_working_set;
4519 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4521 while (pg->peers[pg_iter].connect_peers_head != NULL)
4523 conn_iter = pg->peers[pg_iter].connect_peers_head;
4524 GNUNET_CONTAINER_DLL_remove(pg->peers[pg_iter].connect_peers_head,
4525 pg->peers[pg_iter].connect_peers_tail,
4527 GNUNET_free(conn_iter);
4528 /*remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT, GNUNET_YES);*/
4531 pg->peers[pg_iter].connect_peers_head
4532 = pg->peers[pg_iter].connect_peers_working_set_head;
4533 pg->peers[pg_iter].connect_peers_tail
4534 = pg->peers[pg_iter].connect_peers_working_set_tail;
4535 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4536 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4541 struct FindClosestContext
4544 * The currently known closest peer.
4546 struct GNUNET_TESTING_Daemon *closest;
4549 * The info for the peer we are adding connections for.
4551 struct PeerData *curr_peer;
4554 * The distance (bits) between the current
4555 * peer and the currently known closest.
4557 unsigned int closest_dist;
4560 * The offset of the closest known peer in
4563 unsigned int closest_num;
4567 * Iterator over hash map entries of the allowed
4568 * peer connections. Find the closest, not already
4569 * connected peer and return it.
4571 * @param cls closure (struct FindClosestContext)
4572 * @param key current key code (hash of offset in pg)
4573 * @param value value in the hash map - a GNUNET_TESTING_Daemon
4574 * @return GNUNET_YES if we should continue to
4579 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
4581 struct FindClosestContext *closest_ctx = cls;
4582 struct GNUNET_TESTING_Daemon *daemon = value;
4584 if (((closest_ctx->closest == NULL) ||
4585 (GNUNET_CRYPTO_hash_matching_bits
4586 (&daemon->id.hashPubKey,
4587 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
4588 closest_ctx->closest_dist))
4590 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
4591 curr_peer->connect_peers,
4594 closest_ctx->closest_dist =
4595 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
4596 &closest_ctx->curr_peer->daemon->
4598 closest_ctx->closest = daemon;
4599 uid_from_hash (key, &closest_ctx->closest_num);
4605 * From the set of connections possible, choose at num connections per
4606 * peer based on depth which are closest out of those allowed. Guaranteed
4607 * to add num peers to connect to, provided there are that many peers
4608 * in the underlay topology to connect to.
4610 * @param pg the peergroup we are dealing with
4611 * @param num how many connections at least should each peer have (if possible)?
4612 * @param proc processor to actually add the connections
4613 * @param list the peer list to use
4616 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
4617 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
4622 struct FindClosestContext closest_ctx;
4627 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
4629 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4631 closest_ctx.curr_peer = &pg->peers[pg_iter];
4632 closest_ctx.closest = NULL;
4633 closest_ctx.closest_dist = 0;
4634 closest_ctx.closest_num = 0;
4635 GNUNET_CONTAINER_multihashmap_iterate (pg->
4636 peers[pg_iter].allowed_peers,
4637 &find_closest_peers,
4639 if (closest_ctx.closest != NULL)
4641 GNUNET_assert (closest_ctx.closest_num < pg->total);
4642 proc (pg, pg_iter, closest_ctx.closest_num, list);
4650 * From the set of connections possible, choose at least num connections per
4651 * peer based on depth first traversal of peer connections. If DFS leaves
4652 * peers unconnected, ensure those peers get connections.
4654 * @param pg the peergroup we are dealing with
4655 * @param num how many connections at least should each peer have (if possible)?
4658 perform_dfs(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4662 uint32_t starting_peer;
4663 uint32_t least_connections;
4664 uint32_t random_connection;
4666 unsigned int temp_count;
4667 struct PeerConnection *peer_iter;
4669 struct DFSContext dfs_ctx;
4670 GNUNET_HashCode second_hash;
4676 while ((count_workingset_connections (pg) < num * pg->total)
4677 && (count_allowed_connections (pg) > 0))
4679 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4681 least_connections = -1; /* Set to very high number */
4682 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4685 = count_connections (
4686 pg->peers[pg_iter].connect_peers_working_set_head);
4687 if (temp_count < least_connections)
4689 starting_peer = pg_iter;
4690 least_connections = temp_count;
4696 = count_connections (pg->peers[starting_peer].connect_peers_head);
4697 if (temp_count == 0)
4698 continue; /* FIXME: infinite loop? */
4700 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4703 peer_iter = pg->peers[starting_peer].connect_peers_head;
4704 while (temp_count < random_connection)
4706 peer_iter = peer_iter->next;
4709 GNUNET_assert(peer_iter != NULL);
4710 add_connections (pg, starting_peer, peer_iter->index, WORKING_SET,
4712 remove_connections (pg, starting_peer, peer_iter->index, CONNECT,
4714 starting_peer = peer_iter->index;
4719 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4721 pg->peers[pg_iter].connect_peers_working_set =
4722 GNUNET_CONTAINER_multihashmap_create (num);
4727 while ((count_workingset_connections (pg) < num * pg->total)
4728 && (count_allowed_connections (pg) > 0))
4730 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4732 least_connections = -1; /* Set to very high number */
4733 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4735 if (GNUNET_CONTAINER_multihashmap_size
4736 (pg->peers[pg_iter].connect_peers_working_set) <
4739 starting_peer = pg_iter;
4741 GNUNET_CONTAINER_multihashmap_size (pg->
4743 [pg_iter].connect_peers_working_set);
4748 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4754 /* Choose a random peer from the chosen peers set of connections to add */
4756 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4757 GNUNET_CONTAINER_multihashmap_size
4758 (pg->peers[starting_peer].connect_peers));
4759 dfs_ctx.first_uid = starting_peer;
4760 dfs_ctx.first = &pg->peers[starting_peer];
4762 dfs_ctx.current = 0;
4764 GNUNET_CONTAINER_multihashmap_iterate (pg->
4766 [starting_peer].connect_peers,
4767 &dfs_connect_iterator, &dfs_ctx);
4768 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4769 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4770 GNUNET_assert (GNUNET_YES ==
4771 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4772 [starting_peer].connect_peers,
4776 [dfs_ctx.second_uid].daemon));
4777 starting_peer = dfs_ctx.second_uid;
4780 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4782 /* Remove the "old" connections */
4783 GNUNET_CONTAINER_multihashmap_destroy (pg->
4784 peers[pg_iter].connect_peers);
4785 /* And replace with the working set */
4786 pg->peers[pg_iter].connect_peers =
4787 pg->peers[pg_iter].connect_peers_working_set;
4793 * Internal callback for topology information for a particular peer.
4796 internal_topology_callback(void *cls, const struct GNUNET_PeerIdentity *peer,
4797 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4799 struct CoreContext *core_ctx = cls;
4800 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4802 if (peer == NULL) /* Either finished, or something went wrong */
4804 iter_ctx->completed++;
4805 iter_ctx->connected--;
4806 /* One core context allocated per iteration, must free! */
4807 GNUNET_free (core_ctx);
4811 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id, peer, NULL);
4814 if (iter_ctx->completed == iter_ctx->total)
4816 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4817 /* Once all are done, free the iteration context */
4818 GNUNET_free (iter_ctx);
4823 * Check running topology iteration tasks, if below max start a new one, otherwise
4824 * schedule for some time in the future.
4827 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4829 struct CoreContext *core_context = cls;
4830 struct TopologyIterateContext *topology_context =
4831 (struct TopologyIterateContext *) core_context->iter_context;
4832 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
4835 if (topology_context->connected
4836 > topology_context->pg->max_outstanding_connections)
4838 #if VERBOSE_TESTING > 2
4839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4841 ("Delaying connect, we have too many outstanding connections!\n"));
4843 GNUNET_SCHEDULER_add_delayed (
4844 GNUNET_TIME_relative_multiply (
4845 GNUNET_TIME_UNIT_MILLISECONDS,
4847 &schedule_get_topology, core_context);
4851 #if VERBOSE_TESTING > 2
4852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4853 _("Creating connection, outstanding_connections is %d\n"),
4854 outstanding_connects);
4856 topology_context->connected++;
4858 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4859 &internal_topology_callback,
4862 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4863 internal_topology_callback (core_context, NULL, NULL);
4869 * Iterate over all (running) peers in the peer group, retrieve
4870 * all connections that each currently has.
4873 GNUNET_TESTING_get_topology(struct GNUNET_TESTING_PeerGroup *pg,
4874 GNUNET_TESTING_NotifyTopology cb, void *cls)
4876 struct TopologyIterateContext *topology_context;
4877 struct CoreContext *core_ctx;
4879 unsigned int total_count;
4881 /* Allocate a single topology iteration context */
4882 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4883 topology_context->topology_cb = cb;
4884 topology_context->cls = cls;
4885 topology_context->pg = pg;
4887 for (i = 0; i < pg->total; i++)
4889 if (pg->peers[i].daemon->running == GNUNET_YES)
4891 /* Allocate one core context per core we need to connect to */
4892 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4893 core_ctx->daemon = pg->peers[i].daemon;
4894 /* Set back pointer to topology iteration context */
4895 core_ctx->iter_context = topology_context;
4896 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4900 if (total_count == 0)
4902 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4903 GNUNET_free (topology_context);
4906 topology_context->total = total_count;
4911 * Callback function to process statistic values.
4912 * This handler is here only really to insert a peer
4913 * identity (or daemon) so the statistics can be uniquely
4914 * tied to a single running peer.
4916 * @param cls closure
4917 * @param subsystem name of subsystem that created the statistic
4918 * @param name the name of the datum
4919 * @param value the current value
4920 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4921 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4924 internal_stats_callback(void *cls, const char *subsystem, const char *name,
4925 uint64_t value, int is_persistent)
4927 struct StatsCoreContext *core_context = cls;
4928 struct StatsIterateContext *stats_context =
4929 (struct StatsIterateContext *) core_context->iter_context;
4931 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4932 subsystem, name, value, is_persistent);
4936 * Internal continuation call for statistics iteration.
4938 * @param cls closure, the CoreContext for this iteration
4939 * @param success whether or not the statistics iterations
4940 * was canceled or not (we don't care)
4943 internal_stats_cont(void *cls, int success)
4945 struct StatsCoreContext *core_context = cls;
4946 struct StatsIterateContext *stats_context =
4947 (struct StatsIterateContext *) core_context->iter_context;
4949 stats_context->connected--;
4950 stats_context->completed++;
4952 if (stats_context->completed == stats_context->total)
4954 stats_context->cont (stats_context->cls, GNUNET_YES);
4955 GNUNET_free (stats_context);
4958 if (core_context->stats_handle != NULL)
4959 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4961 GNUNET_free (core_context);
4965 * Check running topology iteration tasks, if below max start a new one, otherwise
4966 * schedule for some time in the future.
4969 schedule_get_statistics(void *cls,
4970 const struct GNUNET_SCHEDULER_TaskContext *tc)
4972 struct StatsCoreContext *core_context = cls;
4973 struct StatsIterateContext *stats_context =
4974 (struct StatsIterateContext *) core_context->iter_context;
4976 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
4979 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
4981 #if VERBOSE_TESTING > 2
4982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4984 ("Delaying connect, we have too many outstanding connections!\n"));
4986 GNUNET_SCHEDULER_add_delayed (
4987 GNUNET_TIME_relative_multiply (
4988 GNUNET_TIME_UNIT_MILLISECONDS,
4990 &schedule_get_statistics, core_context);
4994 #if VERBOSE_TESTING > 2
4995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4996 _("Creating connection, outstanding_connections is %d\n"),
4997 outstanding_connects);
5000 stats_context->connected++;
5001 core_context->stats_handle
5002 = GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
5003 if (core_context->stats_handle == NULL)
5005 internal_stats_cont (core_context, GNUNET_NO);
5009 core_context->stats_get_handle
5010 = GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
5011 GNUNET_TIME_relative_get_forever (),
5012 &internal_stats_cont,
5013 &internal_stats_callback, core_context);
5014 if (core_context->stats_get_handle == NULL)
5015 internal_stats_cont (core_context, GNUNET_NO);
5020 struct DuplicateStats
5023 * Next item in the list
5025 struct DuplicateStats *next;
5028 * Nasty string, concatenation of relevant information.
5030 char *unique_string;
5034 * Check whether the combination of port/host/unix domain socket
5035 * already exists in the list of peers being checked for statistics.
5037 * @param pg the peergroup in question
5038 * @param specific_peer the peer we're concerned with
5039 * @param stats_list the list to return to the caller
5041 * @return GNUNET_YES if the statistics instance has been seen already,
5042 * GNUNET_NO if not (and we may have added it to the list)
5045 stats_check_existing(struct GNUNET_TESTING_PeerGroup *pg,
5046 struct PeerData *specific_peer,
5047 struct DuplicateStats **stats_list)
5049 struct DuplicateStats *pos;
5050 char *unix_domain_socket;
5051 unsigned long long port;
5054 != GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
5055 "single_statistics_per_host"))
5056 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
5059 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg,
5062 &unix_domain_socket))
5065 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg,
5066 "statistics", "port",
5069 GNUNET_free(unix_domain_socket);
5073 if (specific_peer->daemon->hostname != NULL)
5074 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
5075 unix_domain_socket, port);
5077 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
5081 if (0 == strcmp (to_match, pos->unique_string))
5083 GNUNET_free (unix_domain_socket);
5084 GNUNET_free (to_match);
5089 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
5090 pos->unique_string = to_match;
5091 pos->next = *stats_list;
5093 GNUNET_free (unix_domain_socket);
5098 * Iterate over all (running) peers in the peer group, retrieve
5099 * all statistics from each.
5102 GNUNET_TESTING_get_statistics(struct GNUNET_TESTING_PeerGroup *pg,
5103 GNUNET_STATISTICS_Callback cont,
5104 GNUNET_TESTING_STATISTICS_Iterator proc,
5107 struct StatsIterateContext *stats_context;
5108 struct StatsCoreContext *core_ctx;
5110 unsigned int total_count;
5111 struct DuplicateStats *stats_list;
5112 struct DuplicateStats *pos;
5115 /* Allocate a single stats iteration context */
5116 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
5117 stats_context->cont = cont;
5118 stats_context->proc = proc;
5119 stats_context->cls = cls;
5120 stats_context->pg = pg;
5123 for (i = 0; i < pg->total; i++)
5125 if ((pg->peers[i].daemon->running == GNUNET_YES) && (GNUNET_NO
5126 == stats_check_existing (pg, &pg->peers[i], &stats_list)))
5128 /* Allocate one core context per core we need to connect to */
5129 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
5130 core_ctx->daemon = pg->peers[i].daemon;
5131 /* Set back pointer to topology iteration context */
5132 core_ctx->iter_context = stats_context;
5133 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
5138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5139 "Retrieving stats from %u total instances.\n", total_count);
5140 stats_context->total = total_count;
5141 if (stats_list != NULL)
5146 GNUNET_free (pos->unique_string);
5147 stats_list = pos->next;
5149 pos = stats_list->next;
5156 * Stop the connection process temporarily.
5158 * @param pg the peer group to stop connecting
5161 GNUNET_TESTING_stop_connections(struct GNUNET_TESTING_PeerGroup *pg)
5163 pg->stop_connects = GNUNET_YES;
5167 * Resume the connection process temporarily.
5169 * @param pg the peer group to resume connecting
5172 GNUNET_TESTING_resume_connections(struct GNUNET_TESTING_PeerGroup *pg)
5174 pg->stop_connects = GNUNET_NO;
5178 * There are many ways to connect peers that are supported by this function.
5179 * To connect peers in the same topology that was created via the
5180 * GNUNET_TESTING_create_topology, the topology variable must be set to
5181 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
5182 * a new instance of that topology will be generated and attempted to be
5183 * connected. This could result in some connections being impossible,
5184 * because some topologies are non-deterministic.
5186 * @param pg the peer group struct representing the running peers
5187 * @param topology which topology to connect the peers in
5188 * @param options options for connecting the topology
5189 * @param option_modifier modifier for options that take a parameter
5190 * @param connect_timeout how long to wait before giving up on connecting
5192 * @param connect_attempts how many times to attempt to connect two peers
5193 * over the connect_timeout duration
5194 * @param notify_callback notification to be called once all connections completed
5195 * @param notify_cls closure for notification callback
5197 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
5200 GNUNET_TESTING_connect_topology(
5201 struct GNUNET_TESTING_PeerGroup *pg,
5202 enum GNUNET_TESTING_Topology topology,
5203 enum GNUNET_TESTING_TopologyOption options,
5204 double option_modifier,
5205 struct GNUNET_TIME_Relative connect_timeout,
5206 unsigned int connect_attempts,
5207 GNUNET_TESTING_NotifyCompletion notify_callback,
5212 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
5213 #if VERBOSE_TOPOLOGY
5214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5215 _("Creating clique CONNECT topology\n"));
5217 create_clique (pg, &add_connections, CONNECT, GNUNET_NO);
5219 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
5220 #if VERBOSE_TOPOLOGY
5221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5222 _("Creating small world (ring) CONNECT topology\n"));
5224 create_small_world_ring (pg, &add_connections, CONNECT);
5226 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
5227 #if VERBOSE_TOPOLOGY
5228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5229 _("Creating small world (2d-torus) CONNECT topology\n"));
5231 create_small_world (pg, &add_connections, CONNECT);
5233 case GNUNET_TESTING_TOPOLOGY_RING:
5234 #if VERBOSE_TOPOLOGY
5235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring CONNECT topology\n"));
5237 create_ring (pg, &add_connections, CONNECT);
5239 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
5240 #if VERBOSE_TOPOLOGY
5241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5242 _("Creating 2d torus CONNECT topology\n"));
5244 create_2d_torus (pg, &add_connections, CONNECT);
5246 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
5247 #if VERBOSE_TOPOLOGY
5248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5249 _("Creating Erdos-Renyi CONNECT topology\n"));
5251 create_erdos_renyi (pg, &add_connections, CONNECT);
5253 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
5254 #if VERBOSE_TOPOLOGY
5255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5256 _("Creating InterNAT CONNECT topology\n"));
5258 create_nated_internet (pg, &add_connections, CONNECT);
5260 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
5261 #if VERBOSE_TOPOLOGY
5262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5263 _("Creating Scale Free CONNECT topology\n"));
5265 create_scale_free (pg, &add_connections, CONNECT);
5267 case GNUNET_TESTING_TOPOLOGY_LINE:
5268 #if VERBOSE_TOPOLOGY
5269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5270 _("Creating straight line CONNECT topology\n"));
5272 create_line (pg, &add_connections, CONNECT);
5274 case GNUNET_TESTING_TOPOLOGY_NONE:
5275 #if VERBOSE_TOPOLOGY
5276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating no CONNECT topology\n"));
5278 copy_allowed_topology (pg);
5281 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5282 ("Unknown topology specification, can't connect peers!\n"));
5283 return GNUNET_SYSERR;
5288 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
5289 #if VERBOSE_TOPOLOGY
5290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5291 ("Connecting random subset (%'.2f percent) of possible peers\n"), 100
5294 choose_random_connections (pg, option_modifier);
5296 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
5297 #if VERBOSE_TOPOLOGY
5298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5299 _("Connecting a minimum of %u peers each (if possible)\n"),
5300 (unsigned int) option_modifier);
5302 choose_minimum (pg, (unsigned int) option_modifier);
5304 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
5305 #if VERBOSE_TOPOLOGY
5306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5307 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
5308 (unsigned int) option_modifier);
5311 perform_dfs (pg, (int) option_modifier);
5314 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
5315 #if VERBOSE_TOPOLOGY
5316 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5317 ("Finding additional %u closest peers each (if possible)\n"),
5318 (unsigned int) option_modifier);
5321 add_closest (pg, (unsigned int) option_modifier,
5322 &add_connections, CONNECT);
5325 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
5327 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
5333 return connect_topology (pg, connect_timeout, connect_attempts,
5334 notify_callback, notify_cls);
5338 * Lookup and return the number of SSH connections to a host.
5340 * @param hostname the hostname to lookup in the list
5341 * @param pg the peergroup that the host belongs to
5343 * @return the number of current ssh connections to the host
5346 count_outstanding_at_host(const char *hostname,
5347 struct GNUNET_TESTING_PeerGroup *pg)
5349 struct OutstandingSSH *pos;
5351 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5353 GNUNET_assert(pos != NULL);
5354 return pos->outstanding;
5358 * Increment the number of SSH connections to a host by one.
5360 * @param hostname the hostname to lookup in the list
5361 * @param pg the peergroup that the host belongs to
5365 increment_outstanding_at_host(const char *hostname,
5366 struct GNUNET_TESTING_PeerGroup *pg)
5368 struct OutstandingSSH *pos;
5370 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5372 GNUNET_assert(pos != NULL);
5377 * Decrement the number of SSH connections to a host by one.
5379 * @param hostname the hostname to lookup in the list
5380 * @param pg the peergroup that the host belongs to
5384 decrement_outstanding_at_host(const char *hostname,
5385 struct GNUNET_TESTING_PeerGroup *pg)
5387 struct OutstandingSSH *pos;
5389 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5391 GNUNET_assert(pos != NULL);
5396 * Callback that is called whenever a hostkey is generated
5397 * for a peer. Call the real callback and decrement the
5398 * starting counter for the peergroup.
5400 * @param cls closure
5401 * @param id identifier for the daemon, NULL on error
5402 * @param d handle for the daemon
5403 * @param emsg error message (NULL on success)
5406 internal_hostkey_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5407 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5409 struct InternalStartContext *internal_context = cls;
5410 internal_context->peer->pg->starting--;
5411 internal_context->peer->pg->started++;
5412 if (internal_context->hostname != NULL)
5413 decrement_outstanding_at_host (internal_context->hostname,
5414 internal_context->peer->pg);
5415 if (internal_context->hostkey_callback != NULL)
5416 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
5418 else if (internal_context->peer->pg->started
5419 == internal_context->peer->pg->total)
5421 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
5422 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
5427 * Callback that is called whenever a peer has finished starting.
5428 * Call the real callback and decrement the starting counter
5429 * for the peergroup.
5431 * @param cls closure
5432 * @param id identifier for the daemon, NULL on error
5434 * @param d handle for the daemon
5435 * @param emsg error message (NULL on success)
5438 internal_startup_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5439 const struct GNUNET_CONFIGURATION_Handle *cfg,
5440 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5442 struct InternalStartContext *internal_context = cls;
5443 internal_context->peer->pg->starting--;
5444 if (internal_context->hostname != NULL)
5445 decrement_outstanding_at_host (internal_context->hostname,
5446 internal_context->peer->pg);
5447 if (internal_context->start_cb != NULL)
5448 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
5453 internal_continue_startup(void *cls,
5454 const struct GNUNET_SCHEDULER_TaskContext *tc)
5456 struct InternalStartContext *internal_context = cls;
5458 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5463 if ((internal_context->peer->pg->starting
5464 < internal_context->peer->pg->max_concurrent_ssh)
5465 || ((internal_context->hostname != NULL)
5466 && (count_outstanding_at_host (internal_context->hostname,
5467 internal_context->peer->pg)
5468 < internal_context->peer->pg->max_concurrent_ssh)))
5470 if (internal_context->hostname != NULL)
5471 increment_outstanding_at_host (internal_context->hostname,
5472 internal_context->peer->pg);
5473 internal_context->peer->pg->starting++;
5474 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
5478 GNUNET_SCHEDULER_add_delayed (
5479 GNUNET_TIME_relative_multiply (
5480 GNUNET_TIME_UNIT_MILLISECONDS,
5482 &internal_continue_startup,
5488 * Callback for informing us about a successful
5489 * or unsuccessful churn start call.
5491 * @param cls a ChurnContext
5492 * @param id the peer identity of the started peer
5493 * @param cfg the handle to the configuration of the peer
5494 * @param d handle to the daemon for the peer
5495 * @param emsg NULL on success, non-NULL on failure
5499 churn_start_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5500 const struct GNUNET_CONFIGURATION_Handle *cfg,
5501 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5503 struct ChurnRestartContext *startup_ctx = cls;
5504 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
5506 unsigned int total_left;
5507 char *error_message;
5509 error_message = NULL;
5512 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5513 "Churn stop callback failed with error `%s'\n", emsg);
5514 churn_ctx->num_failed_start++;
5518 churn_ctx->num_to_start--;
5522 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5523 "Started peer, %d left.\n", churn_ctx->num_to_start);
5526 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
5527 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5529 if (total_left == 0)
5531 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
5534 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5535 churn_ctx->num_failed_start,
5536 churn_ctx->num_failed_stop);
5537 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5538 GNUNET_free_non_null (error_message);
5539 GNUNET_free (churn_ctx);
5540 GNUNET_free (startup_ctx);
5545 schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5547 struct PeerRestartContext *peer_restart_ctx = cls;
5548 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
5550 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5551 GNUNET_SCHEDULER_add_delayed (
5552 GNUNET_TIME_relative_multiply (
5553 GNUNET_TIME_UNIT_MILLISECONDS,
5555 &schedule_churn_restart, peer_restart_ctx);
5558 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
5559 startup_ctx->timeout,
5560 &churn_start_callback, startup_ctx);
5561 GNUNET_free (peer_restart_ctx);
5567 internal_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5569 struct InternalStartContext *internal_context = cls;
5571 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5576 if ((internal_context->peer->pg->starting
5577 < internal_context->peer->pg->max_concurrent_ssh)
5578 || ((internal_context->hostname != NULL)
5579 && (count_outstanding_at_host (internal_context->hostname,
5580 internal_context->peer->pg)
5581 < internal_context->peer->pg->max_concurrent_ssh)))
5583 if (internal_context->hostname != NULL)
5584 increment_outstanding_at_host (internal_context->hostname,
5585 internal_context->peer->pg);
5586 internal_context->peer->pg->starting++;
5587 internal_context->peer->daemon
5588 = GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
5589 internal_context->timeout,
5591 internal_context->hostname,
5592 internal_context->username,
5593 internal_context->sshport,
5594 internal_context->hostkey,
5595 &internal_hostkey_callback,
5597 &internal_startup_callback,
5602 GNUNET_SCHEDULER_add_delayed (
5603 GNUNET_TIME_relative_multiply (
5604 GNUNET_TIME_UNIT_MILLISECONDS,
5606 &internal_start, internal_context);
5609 #if USE_START_HELPER
5611 struct PeerStartHelperContext
5613 struct GNUNET_TESTING_PeerGroup *pg;
5615 struct HostData *host;
5617 struct GNUNET_OS_Process *proc;
5621 check_peers_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5623 struct PeerStartHelperContext *helper = cls;
5624 enum GNUNET_OS_ProcessStatusType type;
5627 GNUNET_TESTING_NotifyDaemonRunning cb;
5629 if (GNUNET_NO == GNUNET_OS_process_status (helper->proc, &type, &code)) /* Still running, wait some more! */
5631 GNUNET_SCHEDULER_add_delayed(GNUNET_CONSTANTS_EXEC_WAIT, &check_peers_started, helper);
5635 helper->pg->starting--;
5636 if (helper->pg->starting == 0) /* All peers have finished starting! */
5638 /* Call the peer started callback for each peer, set proper FSM state (?) */
5639 for (i = 0; i < helper->pg->total; i++)
5641 cb = helper->pg->peers[i].daemon->cb;
5642 helper->pg->peers[i].daemon->cb = NULL;
5643 helper->pg->peers[i].daemon->running = GNUNET_YES;
5644 helper->pg->peers[i].daemon->phase = SP_START_DONE;
5647 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
5648 cb (helper->pg->peers[i].daemon->cb_cls,
5649 &helper->pg->peers[i].daemon->id,
5650 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5651 "Failed to execute peerStartHelper.pl, or return code bad!");
5653 cb (helper->pg->peers[i].daemon->cb_cls,
5654 &helper->pg->peers[i].daemon->id,
5655 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5662 GNUNET_OS_process_close(helper->proc);
5666 start_peer_helper (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5668 struct PeerStartHelperContext *helper = cls;
5669 char *baseservicehome;
5672 /* ssh user@host peerStartHelper /path/to/basedirectory */
5673 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (helper->pg->cfg, "PATHS", "SERVICEHOME",
5675 GNUNET_asprintf(&tempdir, "%s/%s/", baseservicehome, helper->host->hostname);
5676 if (NULL != helper->host->username)
5677 GNUNET_asprintf (&arg, "%s@%s", helper->host->username, helper->host->hostname);
5679 GNUNET_asprintf (&arg, "%s", helper->host->hostname);
5681 /* FIXME: Doesn't support ssh_port option! */
5682 helper->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5683 "peerStartHelper.pl", tempdir, NULL);
5684 GNUNET_assert(helper->proc != NULL);
5685 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "starting peers with cmd ssh %s %s %s\n", arg, "peerStartHelper.pl", tempdir);
5686 GNUNET_SCHEDULER_add_now (&check_peers_started, helper);
5687 GNUNET_free (tempdir);
5688 GNUNET_free (baseservicehome);
5694 * Function which continues a peer group starting up
5695 * after successfully generating hostkeys for each peer.
5697 * @param pg the peer group to continue starting
5701 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
5705 #if USE_START_HELPER
5706 if ((pg->num_hosts > 0) && (pg->hostkey_data != NULL))
5708 struct PeerStartHelperContext *helper;
5709 pg->starting = pg->num_hosts;
5710 for (i = 0; i < pg->num_hosts; i++)
5712 helper = GNUNET_malloc(sizeof(struct PeerStartHelperContext));
5714 helper->host = &pg->hosts[i];
5715 GNUNET_SCHEDULER_add_now(&start_peer_helper, helper);
5721 for (i = 0; i < pg->total; i++)
5723 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5724 &pg->peers[i].internal_context);
5729 for (i = 0; i < pg->total; i++)
5731 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5732 &pg->peers[i].internal_context);
5737 #if USE_START_HELPER
5739 call_hostkey_callbacks (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5741 struct GNUNET_TESTING_PeerGroup *pg = cls;
5743 for (i = 0; i < pg->total; i++)
5745 if (pg->peers[i].internal_context.hostkey_callback != NULL)
5746 pg->peers[i].internal_context.hostkey_callback (pg->peers[i].internal_context.hostkey_cls,
5747 &pg->peers[i].daemon->id,
5748 pg->peers[i].daemon,
5752 if (pg->peers[0].internal_context.hostkey_callback == NULL)
5753 GNUNET_TESTING_daemons_continue_startup (pg);
5758 * Start count gnunet instances with the same set of transports and
5759 * applications. The port numbers (any option called "PORT") will be
5760 * adjusted to ensure that no two peers running on the same system
5761 * have the same port(s) in their respective configurations.
5763 * @param cfg configuration template to use
5764 * @param total number of daemons to start
5765 * @param max_concurrent_connections for testing, how many peers can
5766 * we connect to simultaneously
5767 * @param max_concurrent_ssh when starting with ssh, how many ssh
5768 * connections will we allow at once (based on remote hosts allowed!)
5769 * @param timeout total time allowed for peers to start
5770 * @param hostkey_callback function to call on each peers hostkey generation
5771 * if NULL, peers will be started by this call, if non-null,
5772 * GNUNET_TESTING_daemons_continue_startup must be called after
5773 * successful hostkey generation
5774 * @param hostkey_cls closure for hostkey callback
5775 * @param cb function to call on each daemon that was started
5776 * @param cb_cls closure for cb
5777 * @param connect_callback function to call each time two hosts are connected
5778 * @param connect_callback_cls closure for connect_callback
5779 * @param hostnames linked list of host structs to use to start peers on
5780 * (NULL to run on localhost only)
5782 * @return NULL on error, otherwise handle to control peer group
5784 struct GNUNET_TESTING_PeerGroup *
5785 GNUNET_TESTING_daemons_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
5787 unsigned int max_concurrent_connections,
5788 unsigned int max_concurrent_ssh,
5789 struct GNUNET_TIME_Relative timeout,
5790 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
5792 GNUNET_TESTING_NotifyDaemonRunning cb,
5794 GNUNET_TESTING_NotifyConnection connect_callback,
5795 void *connect_callback_cls,
5796 const struct GNUNET_TESTING_Host *hostnames)
5798 struct GNUNET_TESTING_PeerGroup *pg;
5799 const struct GNUNET_TESTING_Host *hostpos;
5800 const char *hostname;
5801 const char *username;
5802 char *baseservicehome;
5803 char *newservicehome;
5805 char *hostkeys_file;
5808 struct GNUNET_DISK_FileHandle *fd;
5809 struct GNUNET_CONFIGURATION_Handle *pcfg;
5811 struct OutstandingSSH *ssh_entry;
5812 unsigned int hostcnt;
5819 uint64_t total_hostkeys;
5820 struct GNUNET_OS_Process *proc;
5831 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
5833 pg->notify_connection = connect_callback;
5834 pg->notify_connection_cls = connect_callback_cls;
5836 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
5837 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
5838 pg->max_outstanding_connections = max_concurrent_connections;
5839 pg->max_concurrent_ssh = max_concurrent_ssh;
5840 if (NULL != hostnames)
5843 hostpos = hostnames;
5844 while (hostpos != NULL)
5846 hostpos = hostpos->next;
5849 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5852 hostpos = hostnames;
5853 while (hostpos != NULL)
5855 pg->hosts[off].minport = LOW_PORT;
5856 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
5857 if (hostpos->username != NULL)
5858 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
5859 pg->hosts[off].sshport = hostpos->port;
5860 hostpos = hostpos->next;
5870 pg->num_hosts = off;
5878 /* Create the servicehome directory for each remote peer */
5879 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
5881 for (i = 0; i < pg->num_hosts; i++)
5883 ssh_entry = GNUNET_malloc(sizeof(struct OutstandingSSH));
5884 ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
5885 GNUNET_CONTAINER_DLL_insert(pg->ssh_head, pg->ssh_tail, ssh_entry);
5886 GNUNET_asprintf(&tmpdir, "%s/%s", baseservicehome, pg->hosts[i].hostname);
5887 if (NULL != pg->hosts[i].username)
5888 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username,
5889 pg->hosts[i].hostname);
5891 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
5892 if (pg->hosts[i].sshport != 0)
5894 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
5895 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-P",
5900 arg, "mkdir -p", tmpdir,
5904 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5905 "mkdir -p", tmpdir, NULL);
5906 GNUNET_assert(proc != NULL);
5907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5908 "Creating remote dir with command ssh %s %s %s\n", arg,
5909 " mkdir -p ", tmpdir);
5910 GNUNET_free(tmpdir);
5912 GNUNET_OS_process_wait (proc);
5913 GNUNET_OS_process_close(proc);
5915 GNUNET_free(baseservicehome);
5917 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING",
5921 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
5922 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5923 _("Could not read hostkeys file!\n"));
5926 /* Check hostkey file size, read entire thing into memory */
5927 fd = GNUNET_DISK_file_open (hostkeys_file,
5928 GNUNET_DISK_OPEN_READ,
5929 GNUNET_DISK_PERM_NONE);
5932 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
5935 GNUNET_free (hostkeys_file);
5936 for (i=0;i<pg->num_hosts;i++)
5938 GNUNET_free (pg->hosts[i].hostname);
5939 GNUNET_free_non_null (pg->hosts[i].username);
5941 GNUNET_free (pg->peers);
5942 GNUNET_free (pg->hosts);
5947 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs,
5951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5952 "Found file size %llu for hostkeys, expect hostkeys to be size %d\n",
5953 fs, HOSTKEYFILESIZE);
5955 if (0 != (fs % HOSTKEYFILESIZE))
5957 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5958 "File size %llu seems incorrect for hostkeys...\n",
5963 total_hostkeys = fs / HOSTKEYFILESIZE;
5964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5965 "Will read %llu hostkeys from file\n",
5967 pg->hostkey_data = GNUNET_malloc_large (fs);
5968 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
5970 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
5972 GNUNET_free(hostkeys_file);
5975 for (off = 0; off < total; off++)
5979 hostname = pg->hosts[off % hostcnt].hostname;
5980 username = pg->hosts[off % hostcnt].username;
5981 sshport = pg->hosts[off % hostcnt].sshport;
5982 pcfg = make_config (cfg, off, &pg->hosts[off % hostcnt].minport,
5983 &upnum, hostname, &fdnum);
5990 pcfg = make_config (cfg, off, &minport, &upnum, hostname, &fdnum);
5995 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5996 ("Could not create configuration for peer number %u on `%s'!\n"),
5997 off, hostname == NULL ? "localhost" : hostname);
6002 == GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS",
6006 if (hostname != NULL)
6007 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", baseservicehome, hostname, off);
6009 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
6010 #if !USE_START_HELPER
6011 GNUNET_free (baseservicehome);
6016 tmpdir = getenv ("TMPDIR");
6017 tmpdir = tmpdir ? tmpdir : "/tmp";
6018 if (hostname != NULL)
6019 GNUNET_asprintf (&newservicehome, "%s/%s/%s/%d/", tmpdir, hostname,
6020 "gnunet-testing-test-test", off);
6022 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", tmpdir,
6023 "gnunet-testing-test-test", off);
6025 GNUNET_CONFIGURATION_set_value_string (pcfg, "PATHS", "SERVICEHOME",
6027 GNUNET_free (newservicehome);
6028 pg->peers[off].cfg = pcfg;
6029 pg->peers[off].pg = pg;
6030 pg->peers[off].internal_context.peer = &pg->peers[off];
6031 pg->peers[off].internal_context.timeout = timeout;
6032 pg->peers[off].internal_context.hostname = hostname;
6033 pg->peers[off].internal_context.username = username;
6034 pg->peers[off].internal_context.sshport = sshport;
6035 if (pg->hostkey_data != NULL)
6036 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off
6038 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
6039 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
6040 pg->peers[off].internal_context.start_cb = cb;
6041 pg->peers[off].internal_context.start_cb_cls = cb_cls;
6042 #if !USE_START_HELPER
6043 GNUNET_SCHEDULER_add_now (&internal_start,
6044 &pg->peers[off].internal_context);
6046 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6048 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (pcfg,
6054 pg->peers[off].internal_context.hostkey,
6055 &internal_hostkey_callback,
6056 &pg->peers[off].internal_context,
6057 &internal_startup_callback,
6058 &pg->peers[off].internal_context);
6060 * At this point, given that we had a hostkeyfile,
6061 * we can call the hostkey callback!
6062 * But first, we should copy (rsync) all of the configs
6063 * and hostkeys to the remote peers. Then let topology
6064 * creation happen, then call the peer start helper processes,
6065 * then set pg->whatever_phase for each peer and let them
6066 * enter the fsm to get the HELLO's for peers and start connecting.
6071 GNUNET_SCHEDULER_add_now (&internal_start,
6072 &pg->peers[off].internal_context);
6078 #if USE_START_HELPER /* Now the peergroup has been set up, hostkeys and configs written to files. */
6079 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6081 for (off = 0; off < hostcnt; off++)
6083 GNUNET_asprintf(&newservicehome, "%s/%s/", baseservicehome, pg->hosts[off].hostname);
6085 if (NULL != username)
6086 GNUNET_asprintf (&arg, "%s@%s:%s/%s", username, pg->hosts[off].hostname, baseservicehome, pg->hosts[off].hostname);
6088 GNUNET_asprintf (&arg, "%s:%s/%s", pg->hosts[off].hostname, baseservicehome, pg->hosts[off].hostname);
6090 /* FIXME: Doesn't support ssh_port option! */
6091 proc = GNUNET_OS_start_process (NULL, NULL, "rsync", "rsync", "-r", newservicehome, arg, NULL);
6093 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "copying directory with command rsync -r %s %s\n", newservicehome, arg);
6099 GNUNET_ERROR_TYPE_ERROR,
6101 ("Could not start `%s' process to copy configuration directory.\n"),
6105 GNUNET_OS_process_wait (proc);
6106 GNUNET_OS_process_close (proc);
6108 /* Now all the configuration files and hostkeys are copied to the remote host. Call the hostkey callback for each peer! */
6109 GNUNET_SCHEDULER_add_now(&call_hostkey_callbacks, pg);
6111 GNUNET_free (baseservicehome);
6117 * Get a daemon by number, so callers don't have to do nasty
6118 * offsetting operation.
6120 struct GNUNET_TESTING_Daemon *
6121 GNUNET_TESTING_daemon_get(struct GNUNET_TESTING_PeerGroup *pg,
6122 unsigned int position)
6124 if (position < pg->total)
6125 return pg->peers[position].daemon;
6130 * Get a daemon by peer identity, so callers can
6131 * retrieve the daemon without knowing it's offset.
6133 * @param pg the peer group to retrieve the daemon from
6134 * @param peer_id the peer identity of the daemon to retrieve
6136 * @return the daemon on success, or NULL if no such peer identity is found
6138 struct GNUNET_TESTING_Daemon *
6139 GNUNET_TESTING_daemon_get_by_id(struct GNUNET_TESTING_PeerGroup *pg,
6140 struct GNUNET_PeerIdentity *peer_id)
6144 for (i = 0; i < pg->total; i++)
6146 if (0 == memcmp (&pg->peers[i].daemon->id, peer_id,
6147 sizeof(struct GNUNET_PeerIdentity)))
6148 return pg->peers[i].daemon;
6154 * Prototype of a function that will be called when a
6155 * particular operation was completed the testing library.
6157 * @param cls closure (a struct RestartContext)
6158 * @param id id of the peer that was restarted
6159 * @param cfg handle to the configuration of the peer
6160 * @param d handle to the daemon that was restarted
6161 * @param emsg NULL on success
6164 restart_callback(void *cls, const struct GNUNET_PeerIdentity *id,
6165 const struct GNUNET_CONFIGURATION_Handle *cfg,
6166 struct GNUNET_TESTING_Daemon *d, const char *emsg)
6168 struct RestartContext *restart_context = cls;
6172 restart_context->peers_restarted++;
6176 restart_context->peers_restart_failed++;
6179 if (restart_context->peers_restarted == restart_context->peer_group->total)
6181 restart_context->callback (restart_context->callback_cls, NULL);
6182 GNUNET_free (restart_context);
6184 else if (restart_context->peers_restart_failed
6185 + restart_context->peers_restarted == restart_context->peer_group->total)
6187 restart_context->callback (restart_context->callback_cls,
6188 "Failed to restart peers!");
6189 GNUNET_free (restart_context);
6195 * Callback for informing us about a successful
6196 * or unsuccessful churn stop call.
6198 * @param cls a ChurnContext
6199 * @param emsg NULL on success, non-NULL on failure
6203 churn_stop_callback(void *cls, const char *emsg)
6205 struct ShutdownContext *shutdown_ctx = cls;
6206 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
6207 unsigned int total_left;
6208 char *error_message;
6210 error_message = NULL;
6211 shutdown_ctx->outstanding--;
6215 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6216 "Churn stop callback failed with error `%s'\n", emsg);
6217 churn_ctx->num_failed_stop++;
6221 churn_ctx->num_to_stop--;
6225 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6226 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
6228 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
6229 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
6231 if (total_left == 0)
6233 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
6237 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
6238 churn_ctx->num_failed_start,
6239 churn_ctx->num_failed_stop);
6241 churn_ctx->cb (churn_ctx->cb_cls, error_message);
6242 GNUNET_free_non_null (error_message);
6243 GNUNET_free (churn_ctx);
6244 GNUNET_free (shutdown_ctx);
6249 * Count the number of running peers.
6251 * @param pg handle for the peer group
6253 * @return the number of currently running peers in the peer group
6256 GNUNET_TESTING_daemons_running(struct GNUNET_TESTING_PeerGroup *pg)
6259 unsigned int running = 0;
6260 for (i = 0; i < pg->total; i++)
6262 if (pg->peers[i].daemon->running == GNUNET_YES)
6264 GNUNET_assert (running != -1);
6272 * Task to rate limit the number of outstanding peer shutdown
6273 * requests. This is necessary for making sure we don't do
6274 * too many ssh connections at once, but is generally nicer
6275 * to any system as well (graduated task starts, as opposed
6276 * to calling gnunet-arm N times all at once).
6279 schedule_churn_shutdown_task(void *cls,
6280 const struct GNUNET_SCHEDULER_TaskContext *tc)
6282 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6283 struct ShutdownContext *shutdown_ctx;
6284 struct ChurnContext *churn_ctx;
6285 GNUNET_assert (peer_shutdown_ctx != NULL);
6286 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6287 GNUNET_assert (shutdown_ctx != NULL);
6288 churn_ctx = (struct ChurnContext *) shutdown_ctx->cb_cls;
6289 if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
6290 GNUNET_SCHEDULER_add_delayed (
6291 GNUNET_TIME_relative_multiply (
6292 GNUNET_TIME_UNIT_MILLISECONDS,
6294 &schedule_churn_shutdown_task,
6298 shutdown_ctx->outstanding++;
6299 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6300 shutdown_ctx->timeout, shutdown_ctx->cb,
6301 shutdown_ctx, GNUNET_NO, GNUNET_YES);
6302 GNUNET_free (peer_shutdown_ctx);
6308 * Simulate churn by stopping some peers (and possibly
6309 * re-starting others if churn is called multiple times). This
6310 * function can only be used to create leave-join churn (peers "never"
6311 * leave for good). First "voff" random peers that are currently
6312 * online will be taken offline; then "von" random peers that are then
6313 * offline will be put back online. No notifications will be
6314 * generated for any of these operations except for the callback upon
6317 * @param pg handle for the peer group
6318 * @param voff number of peers that should go offline
6319 * @param von number of peers that should come back online;
6320 * must be zero on first call (since "testbed_start"
6321 * always starts all of the peers)
6322 * @param timeout how long to wait for operations to finish before
6324 * @param cb function to call at the end
6325 * @param cb_cls closure for cb
6328 GNUNET_TESTING_daemons_churn(struct GNUNET_TESTING_PeerGroup *pg,
6329 unsigned int voff, unsigned int von,
6330 struct GNUNET_TIME_Relative timeout,
6331 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6333 struct ChurnContext *churn_ctx;
6334 struct ShutdownContext *shutdown_ctx;
6335 struct PeerShutdownContext *peer_shutdown_ctx;
6336 struct PeerRestartContext *peer_restart_ctx;
6337 struct ChurnRestartContext *churn_startup_ctx;
6339 unsigned int running;
6340 unsigned int stopped;
6341 unsigned int total_running;
6342 unsigned int total_stopped;
6344 unsigned int *running_arr;
6345 unsigned int *stopped_arr;
6346 unsigned int *running_permute;
6347 unsigned int *stopped_permute;
6349 shutdown_ctx = NULL;
6350 peer_shutdown_ctx = NULL;
6351 peer_restart_ctx = NULL;
6352 churn_startup_ctx = NULL;
6357 if ((von == 0) && (voff == 0)) /* No peers at all? */
6363 for (i = 0; i < pg->total; i++)
6365 if (pg->peers[i].daemon->running == GNUNET_YES)
6367 GNUNET_assert (running != -1);
6372 GNUNET_assert (stopped != -1);
6379 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6380 "Trying to stop more peers than are currently running!\n");
6381 cb (cb_cls, "Trying to stop more peers than are currently running!");
6387 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6388 "Trying to start more peers than are currently stopped!\n");
6389 cb (cb_cls, "Trying to start more peers than are currently stopped!");
6393 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6397 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
6401 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
6403 running_permute = NULL;
6404 stopped_permute = NULL;
6407 running_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6410 stopped_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6413 total_running = running;
6414 total_stopped = stopped;
6418 churn_ctx->num_to_start = von;
6419 churn_ctx->num_to_stop = voff;
6421 churn_ctx->cb_cls = cb_cls;
6424 for (i = 0; i < pg->total; i++)
6426 if (pg->peers[i].daemon->running == GNUNET_YES)
6428 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6429 running_arr[running] = i;
6434 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6435 stopped_arr[stopped] = i;
6440 GNUNET_assert (running >= voff);
6443 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6444 shutdown_ctx->cb = &churn_stop_callback;
6445 shutdown_ctx->cb_cls = churn_ctx;
6446 shutdown_ctx->total_peers = voff;
6447 shutdown_ctx->timeout = timeout;
6450 for (i = 0; i < voff; i++)
6453 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
6454 running_permute[i]);
6456 GNUNET_assert (running_arr != NULL);
6457 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6458 peer_shutdown_ctx->daemon
6459 = pg->peers[running_arr[running_permute[i]]].daemon;
6460 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6461 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
6465 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
6467 &churn_stop_callback, churn_ctx,
6468 GNUNET_NO, GNUNET_YES); */
6471 GNUNET_assert (stopped >= von);
6474 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6475 churn_startup_ctx->churn_ctx = churn_ctx;
6476 churn_startup_ctx->timeout = timeout;
6477 churn_startup_ctx->pg = pg;
6479 for (i = 0; i < von; i++)
6482 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
6483 stopped_permute[i]);
6485 GNUNET_assert (stopped_arr != NULL);
6486 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
6487 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
6488 peer_restart_ctx->daemon
6489 = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
6490 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
6492 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
6493 timeout, &churn_start_callback, churn_ctx); */
6496 GNUNET_free_non_null (running_arr);
6497 GNUNET_free_non_null (stopped_arr);
6498 GNUNET_free_non_null (running_permute);
6499 GNUNET_free_non_null (stopped_permute);
6504 * Restart all peers in the given group.
6506 * @param pg the handle to the peer group
6507 * @param callback function to call on completion (or failure)
6508 * @param callback_cls closure for the callback function
6511 GNUNET_TESTING_daemons_restart(struct GNUNET_TESTING_PeerGroup *pg,
6512 GNUNET_TESTING_NotifyCompletion callback,
6515 struct RestartContext *restart_context;
6520 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
6521 restart_context->peer_group = pg;
6522 restart_context->peers_restarted = 0;
6523 restart_context->callback = callback;
6524 restart_context->callback_cls = callback_cls;
6526 for (off = 0; off < pg->total; off++)
6528 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
6529 &restart_callback, restart_context);
6536 * Start or stop an individual peer from the given group.
6538 * @param pg handle to the peer group
6539 * @param offset which peer to start or stop
6540 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
6541 * @param timeout how long to wait for shutdown
6542 * @param cb function to call at the end
6543 * @param cb_cls closure for cb
6546 GNUNET_TESTING_daemons_vary(struct GNUNET_TESTING_PeerGroup *pg,
6547 unsigned int offset, int desired_status,
6548 struct GNUNET_TIME_Relative timeout,
6549 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6551 struct ShutdownContext *shutdown_ctx;
6552 struct ChurnRestartContext *startup_ctx;
6553 struct ChurnContext *churn_ctx;
6555 if (GNUNET_NO == desired_status)
6557 if (NULL != pg->peers[offset].daemon)
6559 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6560 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6561 churn_ctx->num_to_start = 0;
6562 churn_ctx->num_to_stop = 1;
6564 churn_ctx->cb_cls = cb_cls;
6565 shutdown_ctx->cb_cls = churn_ctx;
6566 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon, timeout,
6567 &churn_stop_callback, shutdown_ctx,
6568 GNUNET_NO, GNUNET_YES);
6571 else if (GNUNET_YES == desired_status)
6573 if (NULL == pg->peers[offset].daemon)
6575 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6576 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6577 churn_ctx->num_to_start = 1;
6578 churn_ctx->num_to_stop = 0;
6580 churn_ctx->cb_cls = cb_cls;
6581 startup_ctx->churn_ctx = churn_ctx;
6582 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
6583 timeout, &churn_start_callback,
6593 * Callback for shutting down peers in a peer group.
6595 * @param cls closure (struct ShutdownContext)
6596 * @param emsg NULL on success
6599 internal_shutdown_callback(void *cls, const char *emsg)
6601 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6602 struct ShutdownContext *shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6604 struct OutstandingSSH *ssh_pos;
6606 shutdown_ctx->outstanding--;
6607 if (peer_shutdown_ctx->daemon->hostname != NULL)
6608 decrement_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6613 shutdown_ctx->peers_down++;
6617 shutdown_ctx->peers_failed++;
6620 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down
6621 + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
6623 if (shutdown_ctx->peers_failed > 0)
6624 shutdown_ctx->cb (shutdown_ctx->cb_cls,
6625 "Not all peers successfully shut down!");
6627 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
6629 GNUNET_free (shutdown_ctx->pg->peers);
6630 GNUNET_free_non_null(shutdown_ctx->pg->hostkey_data);
6631 for (off = 0; off < shutdown_ctx->pg->num_hosts; off++)
6633 GNUNET_free (shutdown_ctx->pg->hosts[off].hostname);
6634 GNUNET_free_non_null (shutdown_ctx->pg->hosts[off].username);
6636 GNUNET_free_non_null (shutdown_ctx->pg->hosts);
6637 while (NULL != (ssh_pos = shutdown_ctx->pg->ssh_head))
6639 GNUNET_CONTAINER_DLL_remove(shutdown_ctx->pg->ssh_head, shutdown_ctx->pg->ssh_tail, ssh_pos);
6640 GNUNET_free(ssh_pos);
6642 GNUNET_free (shutdown_ctx->pg);
6643 GNUNET_free (shutdown_ctx);
6645 GNUNET_free(peer_shutdown_ctx);
6650 * Task to rate limit the number of outstanding peer shutdown
6651 * requests. This is necessary for making sure we don't do
6652 * too many ssh connections at once, but is generally nicer
6653 * to any system as well (graduated task starts, as opposed
6654 * to calling gnunet-arm N times all at once).
6657 schedule_shutdown_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6659 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6660 struct ShutdownContext *shutdown_ctx;
6662 GNUNET_assert (peer_shutdown_ctx != NULL);
6663 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6664 GNUNET_assert (shutdown_ctx != NULL);
6666 if ((shutdown_ctx->outstanding < shutdown_ctx->pg->max_concurrent_ssh)
6667 || ((peer_shutdown_ctx->daemon->hostname != NULL)
6668 && (count_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6670 < shutdown_ctx->pg->max_concurrent_ssh)))
6672 if (peer_shutdown_ctx->daemon->hostname != NULL)
6673 increment_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6675 shutdown_ctx->outstanding++;
6676 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6677 shutdown_ctx->timeout,
6678 &internal_shutdown_callback, peer_shutdown_ctx,
6679 GNUNET_YES, GNUNET_NO);
6682 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
6684 &schedule_shutdown_task, peer_shutdown_ctx);
6690 * Shutdown all peers started in the given group.
6692 * @param pg handle to the peer group
6693 * @param timeout how long to wait for shutdown
6694 * @param cb callback to notify upon success or failure
6695 * @param cb_cls closure for cb
6698 GNUNET_TESTING_daemons_stop(struct GNUNET_TESTING_PeerGroup *pg,
6699 struct GNUNET_TIME_Relative timeout,
6700 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6703 struct ShutdownContext *shutdown_ctx;
6704 struct PeerShutdownContext *peer_shutdown_ctx;
6706 struct PeerConnection *conn_iter;
6707 struct PeerConnection *temp_conn;
6710 GNUNET_assert (pg->total > 0);
6712 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6713 shutdown_ctx->cb = cb;
6714 shutdown_ctx->cb_cls = cb_cls;
6715 shutdown_ctx->total_peers = pg->total;
6716 shutdown_ctx->timeout = timeout;
6717 shutdown_ctx->pg = pg;
6718 /* shtudown_ctx->outstanding = 0; */
6720 for (off = 0; off < pg->total; off++)
6722 GNUNET_assert (NULL != pg->peers[off].daemon);
6723 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6724 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
6725 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6726 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
6728 if (NULL != pg->peers[off].cfg)
6730 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
6731 pg->peers[off].cfg = NULL;
6734 conn_iter = pg->peers[off].allowed_peers_head;
6735 while (conn_iter != NULL)
6737 temp_conn = conn_iter->next;
6738 GNUNET_free(conn_iter);
6739 conn_iter = temp_conn;
6742 conn_iter = pg->peers[off].connect_peers_head;
6743 while (conn_iter != NULL)
6745 temp_conn = conn_iter->next;
6746 GNUNET_free(conn_iter);
6747 conn_iter = temp_conn;
6750 conn_iter = pg->peers[off].blacklisted_peers_head;
6751 while (conn_iter != NULL)
6753 temp_conn = conn_iter->next;
6754 GNUNET_free(conn_iter);
6755 conn_iter = temp_conn;
6758 conn_iter = pg->peers[off].connect_peers_working_set_head;
6759 while (conn_iter != NULL)
6761 temp_conn = conn_iter->next;
6762 GNUNET_free(conn_iter);
6763 conn_iter = temp_conn;
6766 if (pg->peers[off].allowed_peers != NULL)
6767 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
6768 if (pg->peers[off].connect_peers != NULL)
6769 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
6770 if (pg->peers[off].blacklisted_peers != NULL)
6771 GNUNET_CONTAINER_multihashmap_destroy (pg->
6772 peers[off].blacklisted_peers);
6777 /* end of testing_group.c */