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_free_non_null (control_host);
1414 GNUNET_free (allowed_hosts);
1417 /* arm needs to know to allow connections from the host on which it is running,
1418 * otherwise gnunet-arm is unable to connect to it in some instances */
1419 if (hostname != NULL)
1421 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1422 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1424 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1426 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1428 GNUNET_free (allowed_hosts);
1432 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1434 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1438 *port = (uint16_t) uc.nport;
1446 * Remove entries from the peer connection list
1448 * @param pg the peer group we are working with
1449 * @param first index of the first peer
1450 * @param second index of the second peer
1451 * @param list the peer list to use
1452 * @param check UNUSED
1454 * @return the number of connections added (can be 0, 1 or 2)
1458 remove_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1459 unsigned int second, enum PeerLists list, unsigned int check)
1463 struct PeerConnection **first_list;
1464 struct PeerConnection **second_list;
1465 struct PeerConnection *first_iter;
1466 struct PeerConnection *second_iter;
1467 struct PeerConnection **first_tail;
1468 struct PeerConnection **second_tail;
1471 GNUNET_HashCode hash_first;
1472 GNUNET_HashCode hash_second;
1474 hash_from_uid (first, &hash_first);
1475 hash_from_uid (second, &hash_second);
1483 first_list = &pg->peers[first].allowed_peers_head;
1484 second_list = &pg->peers[second].allowed_peers_head;
1485 first_tail = &pg->peers[first].allowed_peers_tail;
1486 second_tail = &pg->peers[second].allowed_peers_tail;
1489 first_list = &pg->peers[first].connect_peers_head;
1490 second_list = &pg->peers[second].connect_peers_head;
1491 first_tail = &pg->peers[first].connect_peers_tail;
1492 second_tail = &pg->peers[second].connect_peers_tail;
1495 first_list = &pg->peers[first].blacklisted_peers_head;
1496 second_list = &pg->peers[second].blacklisted_peers_head;
1497 first_tail = &pg->peers[first].blacklisted_peers_tail;
1498 second_tail = &pg->peers[second].blacklisted_peers_tail;
1501 first_list = &pg->peers[first].connect_peers_working_set_head;
1502 second_list = &pg->peers[second].connect_peers_working_set_head;
1503 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1504 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1511 first_iter = *first_list;
1512 while (first_iter != NULL)
1514 if (first_iter->index == second)
1516 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1517 GNUNET_free(first_iter);
1521 first_iter = first_iter->next;
1524 second_iter = *second_list;
1525 while (second_iter != NULL)
1527 if (second_iter->index == first)
1529 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1530 GNUNET_free(second_iter);
1534 second_iter = second_iter->next;
1538 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1541 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1546 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1549 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1558 * Add entries to the some list
1560 * @param pg the peer group we are working with
1561 * @param first index of the first peer
1562 * @param second index of the second peer
1563 * @param list the list type that we should modify
1564 * @param check GNUNET_YES to check lists before adding
1565 * GNUNET_NO to force add
1567 * @return the number of connections added (can be 0, 1 or 2)
1571 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1572 unsigned int second, enum PeerLists list, unsigned int check)
1578 struct PeerConnection **first_list;
1579 struct PeerConnection **second_list;
1580 struct PeerConnection *first_iter;
1581 struct PeerConnection *second_iter;
1582 struct PeerConnection *new_first;
1583 struct PeerConnection *new_second;
1584 struct PeerConnection **first_tail;
1585 struct PeerConnection **second_tail;
1590 first_list = &pg->peers[first].allowed_peers_head;
1591 second_list = &pg->peers[second].allowed_peers_head;
1592 first_tail = &pg->peers[first].allowed_peers_tail;
1593 second_tail = &pg->peers[second].allowed_peers_tail;
1596 first_list = &pg->peers[first].connect_peers_head;
1597 second_list = &pg->peers[second].connect_peers_head;
1598 first_tail = &pg->peers[first].connect_peers_tail;
1599 second_tail = &pg->peers[second].connect_peers_tail;
1602 first_list = &pg->peers[first].blacklisted_peers_head;
1603 second_list = &pg->peers[second].blacklisted_peers_head;
1604 first_tail = &pg->peers[first].blacklisted_peers_tail;
1605 second_tail = &pg->peers[second].blacklisted_peers_tail;
1608 first_list = &pg->peers[first].connect_peers_working_set_head;
1609 second_list = &pg->peers[second].connect_peers_working_set_head;
1610 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1611 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1618 add_first = GNUNET_YES;
1619 add_second = GNUNET_YES;
1621 if (check == GNUNET_YES)
1623 first_iter = *first_list;
1624 while (first_iter != NULL)
1626 if (first_iter->index == second)
1628 add_first = GNUNET_NO;
1631 first_iter = first_iter->next;
1634 second_iter = *second_list;
1635 while (second_iter != NULL)
1637 if (second_iter->index == first)
1639 add_second = GNUNET_NO;
1642 second_iter = second_iter->next;
1649 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1650 new_first->index = second;
1651 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1652 pg->peers[first].num_connections++;
1658 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1659 new_second->index = first;
1660 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1661 pg->peers[second].num_connections++;
1669 * Scale free network construction as described in:
1671 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1673 * Start with a network of "one" peer, then progressively add
1674 * peers up to the total number. At each step, iterate over
1675 * all possible peers and connect new peer based on number of
1676 * existing connections of the target peer.
1678 * @param pg the peer group we are dealing with
1679 * @param proc the connection processor to use
1680 * @param list the peer list to use
1682 * @return the number of connections created
1685 create_scale_free(struct GNUNET_TESTING_PeerGroup *pg,
1686 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1689 unsigned int total_connections;
1690 unsigned int outer_count;
1692 unsigned int previous_total_connections;
1696 GNUNET_assert (pg->total > 1);
1698 /* Add a connection between the first two nodes */
1699 total_connections = proc (pg, 0, 1, list, GNUNET_YES);
1701 for (outer_count = 1; outer_count < pg->total; outer_count++)
1703 previous_total_connections = total_connections;
1704 for (i = 0; i < outer_count; i++)
1706 probability = pg->peers[i].num_connections
1707 / (double) previous_total_connections;
1709 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1711 / ((double) UINT64_MAX);
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1714 "Considering connecting peer %d to peer %d\n",
1717 if (random < probability)
1720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1721 "Connecting peer %d to peer %d\n", outer_count, i);
1723 total_connections += proc (pg, outer_count, i, list, GNUNET_YES);
1728 return total_connections;
1732 * Create a topology given a peer group (set of running peers)
1733 * and a connection processor. Creates a small world topology
1734 * according to the rewired ring construction. The basic
1735 * behavior is that a ring topology is created, but with some
1736 * probability instead of connecting a peer to the next
1737 * neighbor in the ring a connection will be created to a peer
1738 * selected uniformly at random. We use the TESTING
1739 * PERCENTAGE option to specify what number of
1740 * connections each peer should have. Default is 2,
1741 * which makes the ring, any given number is multiplied by
1742 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1743 * each peer have on average 2logn connections. The additional
1744 * connections are made at increasing distance around the ring
1745 * from the original peer, or to random peers based on the re-
1746 * wiring probability. The TESTING
1747 * PROBABILITY option is used as the probability that a given
1748 * connection is rewired.
1750 * @param pg the peergroup to create the topology on
1751 * @param proc the connection processor to call to actually set
1752 * up connections between two peers
1753 * @param list the peer list to use
1755 * @return the number of connections that were set up
1759 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg,
1760 GNUNET_TESTING_ConnectionProcessor proc,
1761 enum PeerLists list)
1765 unsigned int natLog;
1766 unsigned int randomPeer;
1767 double random, logNModifier, probability;
1768 unsigned int smallWorldConnections;
1773 unsigned int useAnd;
1774 int connect_attempts;
1776 logNModifier = 0.5; /* FIXME: default value? */
1777 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1781 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1783 GNUNET_ERROR_TYPE_WARNING,
1785 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1786 p_string, "LOGNMODIFIER", "TESTING");
1787 GNUNET_free (p_string);
1789 probability = 0.5; /* FIXME: default percentage? */
1790 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1794 if (sscanf (p_string, "%lf", &probability) != 1)
1796 GNUNET_ERROR_TYPE_WARNING,
1798 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1799 p_string, "PERCENTAGE", "TESTING");
1800 GNUNET_free (p_string);
1802 natLog = log (pg->total);
1803 connsPerPeer = ceil (natLog * logNModifier);
1805 if (connsPerPeer % 2 == 1)
1808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."),
1811 smallWorldConnections = 0;
1812 connect_attempts = 0;
1813 for (i = 0; i < pg->total; i++)
1816 max = i + connsPerPeer / 2;
1817 min = i - connsPerPeer / 2;
1819 if (max > pg->total - 1)
1821 max = max - pg->total;
1827 min = pg->total - 1 + min;
1831 for (j = 0; j < connsPerPeer / 2; j++)
1834 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1836 / ((double) UINT64_MAX));
1837 if (random < probability)
1839 /* Connect to uniformly selected random peer */
1841 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1843 while ((((randomPeer < max) && (randomPeer > min)) && (useAnd
1844 == 0)) || (((randomPeer > min) || (randomPeer < max))
1848 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1851 smallWorldConnections += proc (pg, i, randomPeer, list,
1856 nodeToConnect = i + j + 1;
1857 if (nodeToConnect > pg->total - 1)
1859 nodeToConnect = nodeToConnect - pg->total;
1861 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
1867 connect_attempts += smallWorldConnections;
1869 return connect_attempts;
1873 * Create a topology given a peer group (set of running peers)
1874 * and a connection processor.
1876 * @param pg the peergroup to create the topology on
1877 * @param proc the connection processor to call to actually set
1878 * up connections between two peers
1879 * @param list the peer list to use
1881 * @return the number of connections that were set up
1885 create_nated_internet(struct GNUNET_TESTING_PeerGroup *pg,
1886 GNUNET_TESTING_ConnectionProcessor proc,
1887 enum PeerLists list)
1889 unsigned int outer_count, inner_count;
1890 unsigned int cutoff;
1891 int connect_attempts;
1892 double nat_percentage;
1895 nat_percentage = 0.6; /* FIXME: default percentage? */
1896 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1900 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1902 GNUNET_ERROR_TYPE_WARNING,
1904 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1905 p_string, "PERCENTAGE", "TESTING");
1906 GNUNET_free (p_string);
1909 cutoff = (unsigned int) (nat_percentage * pg->total);
1910 connect_attempts = 0;
1911 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1913 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1915 if ((outer_count > cutoff) || (inner_count > cutoff))
1918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1919 "Connecting peer %d to peer %d\n",
1920 outer_count, inner_count);
1922 connect_attempts += proc (pg, outer_count, inner_count, list,
1927 return connect_attempts;
1932 * Create a topology given a peer group (set of running peers)
1933 * and a connection processor.
1935 * @param pg the peergroup to create the topology on
1936 * @param proc the connection processor to call to actually set
1937 * up connections between two peers
1938 * @param list the peer list to use
1940 * @return the number of connections that were set up
1944 create_nated_internet_copy(struct GNUNET_TESTING_PeerGroup *pg,
1945 GNUNET_TESTING_ConnectionProcessor proc,
1946 enum PeerLists list)
1948 unsigned int outer_count, inner_count;
1949 unsigned int cutoff;
1950 int connect_attempts;
1951 double nat_percentage;
1954 struct ProgressMeter *conn_meter;
1956 nat_percentage = 0.6; /* FIXME: default percentage? */
1957 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1961 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1963 GNUNET_ERROR_TYPE_WARNING,
1965 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1966 p_string, "PERCENTAGE", "TESTING");
1967 GNUNET_free (p_string);
1970 cutoff = (unsigned int) (nat_percentage * pg->total);
1972 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1974 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1976 if ((outer_count > cutoff) || (inner_count > cutoff))
1982 conn_meter = create_meter (count, "NAT COPY", GNUNET_YES);
1983 connect_attempts = 0;
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))
1991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1992 "Connecting peer %d to peer %d\n",
1993 outer_count, inner_count);
1995 connect_attempts += proc (pg, outer_count, inner_count, list,
1997 add_connections (pg, outer_count, inner_count, ALLOWED, GNUNET_NO);
1998 update_meter (conn_meter);
2002 free_meter (conn_meter);
2004 return connect_attempts;
2009 * Create a topology given a peer group (set of running peers)
2010 * and a connection processor.
2012 * @param pg the peergroup to create the topology on
2013 * @param proc the connection processor to call to actually set
2014 * up connections between two peers
2015 * @param list the peer list to use
2017 * @return the number of connections that were set up
2021 create_small_world(struct GNUNET_TESTING_PeerGroup *pg,
2022 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2024 unsigned int i, j, k;
2025 unsigned int square;
2028 unsigned int toggle = 1;
2029 unsigned int nodeToConnect;
2030 unsigned int natLog;
2031 unsigned int node1Row;
2032 unsigned int node1Col;
2033 unsigned int node2Row;
2034 unsigned int node2Col;
2035 unsigned int distance;
2036 double probability, random, percentage;
2037 unsigned int smallWorldConnections;
2038 unsigned int small_world_it;
2040 int connect_attempts;
2041 square = floor (sqrt (pg->total));
2045 percentage = 0.5; /* FIXME: default percentage? */
2046 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2050 if (sscanf (p_string, "%lf", &percentage) != 1)
2052 GNUNET_ERROR_TYPE_WARNING,
2054 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2055 p_string, "PERCENTAGE", "TESTING");
2056 GNUNET_free (p_string);
2058 if (percentage < 0.0)
2061 GNUNET_ERROR_TYPE_WARNING,
2063 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
2064 "PERCENTAGE", "TESTING", percentage);
2067 probability = 0.5; /* FIXME: default percentage? */
2068 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2072 if (sscanf (p_string, "%lf", &probability) != 1)
2074 GNUNET_ERROR_TYPE_WARNING,
2076 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2077 p_string, "PROBABILITY", "TESTING");
2078 GNUNET_free (p_string);
2080 if (square * square != pg->total)
2082 while (rows * cols < pg->total)
2084 if (toggle % 2 == 0)
2093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2095 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2099 connect_attempts = 0;
2100 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2101 * to the node to its right and above. Once this is over, we'll have our torus!
2102 * Special case for the last node (if the rows and columns are not equal), connect
2103 * to the first in the row to maintain topology.
2105 for (i = 0; i < pg->total; i++)
2107 /* First connect to the node to the right */
2108 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2109 nodeToConnect = i + 1;
2110 else if (i + 1 == pg->total)
2111 nodeToConnect = rows * cols - cols;
2113 nodeToConnect = i - cols + 1;
2115 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2119 nodeToConnect = (rows * cols) - cols + i;
2120 if (nodeToConnect >= pg->total)
2121 nodeToConnect -= cols;
2124 nodeToConnect = i - cols;
2126 if (nodeToConnect < pg->total)
2127 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2129 natLog = log (pg->total);
2130 #if VERBOSE_TESTING > 2
2131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2132 _("natural log of %d is %d, will run %d iterations\n"),
2133 pg->total, natLog, (int) (natLog * percentage));
2134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2135 _("Total connections added thus far: %u!\n"), connect_attempts);
2137 smallWorldConnections = 0;
2138 small_world_it = (unsigned int) (natLog * percentage);
2139 if (small_world_it < 1)
2141 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
2142 for (i = 0; i < small_world_it; i++)
2144 for (j = 0; j < pg->total; j++)
2146 /* Determine the row and column of node at position j on the 2d torus */
2147 node1Row = j / cols;
2148 node1Col = j - (node1Row * cols);
2149 for (k = 0; k < pg->total; k++)
2151 /* Determine the row and column of node at position k on the 2d torus */
2152 node2Row = k / cols;
2153 node2Col = k - (node2Row * cols);
2154 /* Simple Cartesian distance */
2155 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
2158 /* Calculate probability as 1 over the square of the distance */
2159 probability = 1.0 / (distance * distance);
2160 /* Choose a random value between 0 and 1 */
2162 = ((double) GNUNET_CRYPTO_random_u64 (
2163 GNUNET_CRYPTO_QUALITY_WEAK,
2165 / ((double) UINT64_MAX);
2166 /* If random < probability, then connect the two nodes */
2167 if (random < probability)
2168 smallWorldConnections += proc (pg, j, k, list, GNUNET_YES);
2174 connect_attempts += smallWorldConnections;
2175 #if VERBOSE_TESTING > 2
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2177 _("Total connections added for small world: %d!\n"),
2178 smallWorldConnections);
2180 return connect_attempts;
2184 * Create a topology given a peer group (set of running peers)
2185 * and a connection processor.
2187 * @param pg the peergroup to create the topology on
2188 * @param proc the connection processor to call to actually set
2189 * up connections between two peers
2190 * @param list the peer list to use
2192 * @return the number of connections that were set up
2196 create_erdos_renyi(struct GNUNET_TESTING_PeerGroup *pg,
2197 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2200 unsigned int outer_count;
2201 unsigned int inner_count;
2202 int connect_attempts;
2206 probability = 0.5; /* FIXME: default percentage? */
2207 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2211 if (sscanf (p_string, "%lf", &probability) != 1)
2213 GNUNET_ERROR_TYPE_WARNING,
2215 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2216 p_string, "PROBABILITY", "TESTING");
2217 GNUNET_free (p_string);
2219 connect_attempts = 0;
2220 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2222 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2225 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2227 / ((double) UINT64_MAX);
2229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2230 _("rand is %f probability is %f\n"), temp_rand,
2233 if (temp_rand < probability)
2235 connect_attempts += proc (pg, outer_count, inner_count, list,
2241 return connect_attempts;
2245 * Create a topology given a peer group (set of running peers)
2246 * and a connection processor. This particular function creates
2247 * the connections for a 2d-torus, plus additional "closest"
2248 * connections per peer.
2250 * @param pg the peergroup to create the topology on
2251 * @param proc the connection processor to call to actually set
2252 * up connections between two peers
2253 * @param list the peer list to use
2255 * @return the number of connections that were set up
2259 create_2d_torus(struct GNUNET_TESTING_PeerGroup *pg,
2260 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2263 unsigned int square;
2266 unsigned int toggle = 1;
2267 unsigned int nodeToConnect;
2268 int connect_attempts;
2270 connect_attempts = 0;
2272 square = floor (sqrt (pg->total));
2276 if (square * square != pg->total)
2278 while (rows * cols < pg->total)
2280 if (toggle % 2 == 0)
2289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2291 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2294 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2295 * to the node to its right and above. Once this is over, we'll have our torus!
2296 * Special case for the last node (if the rows and columns are not equal), connect
2297 * to the first in the row to maintain topology.
2299 for (i = 0; i < pg->total; i++)
2301 /* First connect to the node to the right */
2302 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2303 nodeToConnect = i + 1;
2304 else if (i + 1 == pg->total)
2305 nodeToConnect = rows * cols - cols;
2307 nodeToConnect = i - cols + 1;
2309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2310 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2312 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2314 /* Second connect to the node immediately above */
2317 nodeToConnect = (rows * cols) - cols + i;
2318 if (nodeToConnect >= pg->total)
2319 nodeToConnect -= cols;
2322 nodeToConnect = i - cols;
2324 if (nodeToConnect < pg->total)
2327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2328 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2330 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2335 return connect_attempts;
2339 * Create a topology given a peer group (set of running peers)
2340 * and a connection processor.
2342 * @param pg the peergroup to create the topology on
2343 * @param proc the connection processor to call to actually set
2344 * up connections between two peers
2345 * @param list the peer list to use
2346 * @param check does the connection processor need to check before
2347 * performing an action on the list?
2349 * @return the number of connections that were set up
2353 create_clique(struct GNUNET_TESTING_PeerGroup *pg,
2354 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list,
2357 unsigned int outer_count;
2358 unsigned int inner_count;
2359 int connect_attempts;
2360 struct ProgressMeter *conn_meter;
2361 connect_attempts = 0;
2363 conn_meter = create_meter ((((pg->total * pg->total) + pg->total) / 2)
2364 - pg->total, "Create Clique ", GNUNET_NO);
2365 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2367 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2371 "Connecting peer %d to peer %d\n",
2372 outer_count, inner_count);
2374 connect_attempts += proc (pg, outer_count, inner_count, list, check);
2375 update_meter (conn_meter);
2378 reset_meter (conn_meter);
2379 free_meter (conn_meter);
2380 return connect_attempts;
2385 * Iterator over hash map entries.
2387 * @param cls closure the peer group
2388 * @param key the key stored in the hashmap is the
2389 * index of the peer to connect to
2390 * @param value value in the hash map, handle to the peer daemon
2391 * @return GNUNET_YES if we should continue to
2396 unblacklist_iterator (void *cls,
2397 const GNUNET_HashCode * key,
2400 struct UnblacklistContext *un_ctx = cls;
2401 uint32_t second_pos;
2403 uid_from_hash (key, &second_pos);
2405 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2412 * Create a blacklist topology based on the allowed topology
2413 * which disallows any connections not in the allowed topology
2414 * at the transport level.
2416 * @param pg the peergroup to create the topology on
2417 * @param proc the connection processor to call to allow
2418 * up connections between two peers
2420 * @return the number of connections that were set up
2424 copy_allowed(struct GNUNET_TESTING_PeerGroup *pg,
2425 GNUNET_TESTING_ConnectionProcessor proc)
2427 struct UnblacklistContext un_ctx;
2430 struct PeerConnection *iter;
2434 for (count = 0; count < pg->total - 1; count++)
2436 un_ctx.first_uid = count;
2438 iter = pg->peers[count].allowed_peers_head;
2439 while (iter != NULL)
2441 remove_connections (pg, count, iter->index, BLACKLIST, GNUNET_YES);
2442 //unblacklist_connections(pg, count, iter->index);
2446 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers, &unblacklist_iterator, &un_ctx);
2449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2454 * Create a topology given a peer group (set of running peers)
2455 * and a connection processor.
2457 * @param pg the peergroup to create the topology on
2458 * @param proc the connection processor to call to actually set
2459 * up connections between two peers
2460 * @param list which list should be modified
2462 * @return the number of connections that were set up
2466 create_line(struct GNUNET_TESTING_PeerGroup *pg,
2467 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2470 int connect_attempts;
2472 connect_attempts = 0;
2474 /* Connect each peer to the next highest numbered peer */
2475 for (count = 0; count < pg->total - 1; count++)
2478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2479 "Connecting peer %d to peer %d\n", count, count + 1);
2481 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2484 return connect_attempts;
2488 * Create a topology given a peer group (set of running peers)
2489 * and a connection processor.
2491 * @param pg the peergroup to create the topology on
2492 * @param filename the file to read topology information from
2493 * @param proc the connection processor to call to actually set
2494 * up connections between two peers
2495 * @param list the peer list to use
2497 * @return the number of connections that were set up
2501 create_from_file(struct GNUNET_TESTING_PeerGroup *pg, char *filename,
2502 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2504 int connect_attempts;
2505 unsigned int first_peer_index;
2506 unsigned int second_peer_index;
2507 connect_attempts = 0;
2512 unsigned int total_peers;
2514 enum States curr_state;
2516 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2517 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2519 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2521 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2522 "Could not open file `%s' specified for topology!", filename);
2523 return connect_attempts;
2526 data = GNUNET_malloc_large (frstat.st_size);
2527 GNUNET_assert(data != NULL);
2528 if (frstat.st_size != GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2531 GNUNET_ERROR_TYPE_ERROR,
2532 "Could not read file %s specified for host list, ending test!",
2535 return connect_attempts;
2540 first_peer_index = 0;
2541 /* First line should contain a single integer, specifying the number of peers */
2542 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2543 curr_state = NUM_PEERS;
2544 while (count < frstat.st_size - 1)
2546 if ((buf[count] == '\n') || (buf[count] == ' '))
2556 total_peers = strtoul(&buf[count], NULL, 10);
2559 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2560 "Failed to read number of peers from topology file!\n");
2561 GNUNET_free_non_null(data);
2562 return connect_attempts;
2564 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2565 "Read %u total peers in topology\n", total_peers);
2566 GNUNET_assert(total_peers == pg->total);
2567 curr_state = PEER_INDEX;
2568 while ((buf[count] != '\n') && (count < frstat.st_size - 1))
2574 first_peer_index = strtoul(&buf[count], NULL, 10);
2577 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2578 "Failed to read peer index from topology file!\n");
2579 GNUNET_free_non_null(data);
2580 return connect_attempts;
2582 while ((buf[count] != ':') && (count < frstat.st_size - 1))
2585 curr_state = OTHER_PEER_INDEX;
2588 if (1 == sscanf (&buf[count], ":"))
2589 curr_state = OTHER_PEER_INDEX;
2592 case OTHER_PEER_INDEX:
2594 second_peer_index = strtoul(&buf[count], NULL, 10);
2597 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2598 "Failed to peer index from topology file!\n");
2599 GNUNET_free_non_null(data);
2600 return connect_attempts;
2602 /* Assume file is written with first peer 1, but array index is 0 */
2603 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index
2604 - 1, list, GNUNET_YES);
2605 while ((buf[count] != '\n') && (buf[count] != ',') && (count
2606 < frstat.st_size - 1))
2608 if (buf[count] == '\n')
2610 curr_state = PEER_INDEX;
2612 else if (buf[count] != ',')
2614 curr_state = OTHER_PEER_INDEX;
2619 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2620 "Found bad data in topology file while in state %d!\n",
2623 return connect_attempts;
2627 return connect_attempts;
2631 * Create a topology given a peer group (set of running peers)
2632 * and a connection processor.
2634 * @param pg the peergroup to create the topology on
2635 * @param proc the connection processor to call to actually set
2636 * up connections between two peers
2637 * @param list the peer list to use
2639 * @return the number of connections that were set up
2643 create_ring(struct GNUNET_TESTING_PeerGroup *pg,
2644 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2647 int connect_attempts;
2649 connect_attempts = 0;
2651 /* Connect each peer to the next highest numbered peer */
2652 for (count = 0; count < pg->total - 1; count++)
2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2656 "Connecting peer %d to peer %d\n", count, count + 1);
2658 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2661 /* Connect the last peer to the first peer */
2662 connect_attempts += proc (pg, pg->total - 1, 0, list, GNUNET_YES);
2664 return connect_attempts;
2669 * Iterator for writing friends of a peer to a file.
2671 * @param cls closure, an open writable file handle
2672 * @param key the key the daemon was stored under
2673 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2675 * @return GNUNET_YES to continue iteration
2677 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2678 * with a single file_iterator that takes a closure which contains
2679 * the prefix to write before the peer. Then this could be used
2680 * for blacklisting multiple transports and writing the friend
2681 * file. I'm sure *someone* will complain loudly about other
2682 * things that negate these functions even existing so no point in
2686 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2688 FILE *temp_friend_handle = cls;
2689 struct GNUNET_TESTING_Daemon *peer = value;
2690 struct GNUNET_PeerIdentity *temppeer;
2691 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2693 temppeer = &peer->id;
2694 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2695 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2700 struct BlacklistContext
2703 * The (open) file handle to write to
2705 FILE *temp_file_handle;
2708 * The transport that this peer will be blacklisted on.
2714 * Iterator for writing blacklist data to appropriate files.
2716 * @param cls closure, an open writable file handle
2717 * @param key the key the daemon was stored under
2718 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2720 * @return GNUNET_YES to continue iteration
2723 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2725 struct BlacklistContext *blacklist_ctx = cls;
2726 struct GNUNET_TESTING_Daemon *peer = value;
2727 struct GNUNET_PeerIdentity *temppeer;
2728 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2730 temppeer = &peer->id;
2731 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2732 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2733 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2734 blacklist_ctx->transport, (char *) &peer_enc);
2741 * Create the friend files based on the PeerConnection's
2742 * of each peer in the peer group, and copy the files
2743 * to the appropriate place
2745 * @param pg the peer group we are dealing with
2748 create_and_copy_friend_files(struct GNUNET_TESTING_PeerGroup *pg)
2750 FILE *temp_friend_handle;
2751 unsigned int pg_iter;
2752 char *temp_service_path;
2753 struct GNUNET_OS_Process **procarr;
2757 enum GNUNET_OS_ProcessStatusType type;
2758 unsigned long return_code;
2766 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2767 struct PeerConnection *conn_iter;
2769 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2770 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2772 mytemp = GNUNET_DISK_mktemp ("friends");
2773 GNUNET_assert (mytemp != NULL);
2774 temp_friend_handle = fopen (mytemp, "wt");
2775 GNUNET_assert (temp_friend_handle != NULL);
2777 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2778 while (conn_iter != NULL)
2780 GNUNET_CRYPTO_hash_to_enc (
2781 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2783 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2784 conn_iter = conn_iter->next;
2787 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2788 &friend_file_iterator,
2789 temp_friend_handle);
2791 fclose (temp_friend_handle);
2794 != GNUNET_CONFIGURATION_get_value_string (
2795 pg->peers[pg_iter]. daemon->cfg,
2796 "PATHS", "SERVICEHOME",
2797 &temp_service_path))
2800 GNUNET_ERROR_TYPE_WARNING,
2802 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2803 "SERVICEHOME", "PATHS");
2804 if (UNLINK (mytemp) != 0)
2805 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2807 GNUNET_free (mytemp);
2811 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2813 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2814 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
2816 GNUNET_assert(procarr[pg_iter] != NULL);
2818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2819 _("Copying file with command cp %s %s\n"), mytemp, arg);
2821 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2822 GNUNET_OS_process_close (procarr[pg_iter]);
2825 else /* Remote, scp the file to the correct place */
2827 if (NULL != pg->peers[pg_iter].daemon->username)
2828 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2829 pg->peers[pg_iter].daemon->username,
2830 pg->peers[pg_iter].daemon->hostname,
2833 GNUNET_asprintf (&arg, "%s:%s/friends",
2834 pg->peers[pg_iter].daemon->hostname,
2836 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
2838 GNUNET_assert(procarr[pg_iter] != NULL);
2839 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2840 GNUNET_OS_process_close (procarr[pg_iter]);
2841 if (ret != GNUNET_OK)
2843 procarr[pg_iter] = NULL;
2845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2846 _("Copying file with command scp %s %s\n"), mytemp,
2851 GNUNET_free (temp_service_path);
2852 GNUNET_free (mytemp);
2857 ret = GNUNET_SYSERR;
2858 while ((count < max_wait) && (ret != GNUNET_OK))
2861 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2865 _("Checking copy status of file %d\n"), pg_iter);
2867 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2869 if (GNUNET_OS_process_status
2870 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2872 ret = GNUNET_SYSERR;
2874 else if ((type != GNUNET_OS_PROCESS_EXITED)
2875 || (return_code != 0))
2877 ret = GNUNET_SYSERR;
2881 GNUNET_OS_process_close (procarr[pg_iter]);
2882 procarr[pg_iter] = NULL;
2884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2885 _("File %d copied\n"), pg_iter);
2891 if (ret == GNUNET_SYSERR)
2893 /* FIXME: why sleep here? -CG */
2899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2900 _("Finished copying all friend files!\n"));
2903 GNUNET_free (procarr);
2908 * Create the blacklist files based on the PeerConnection's
2909 * of each peer in the peer group, and copy the files
2910 * to the appropriate place.
2912 * @param pg the peer group we are dealing with
2913 * @param transports space delimited list of transports to blacklist
2916 create_and_copy_blacklist_files(struct GNUNET_TESTING_PeerGroup *pg,
2917 const char *transports)
2919 FILE *temp_file_handle;
2920 unsigned int pg_iter;
2921 char *temp_service_path;
2922 struct GNUNET_OS_Process **procarr;
2925 enum GNUNET_OS_ProcessStatusType type;
2926 unsigned long return_code;
2933 char *temp_transports;
2936 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2937 struct PeerConnection *conn_iter;
2939 static struct BlacklistContext blacklist_ctx;
2942 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2943 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2945 mytemp = GNUNET_DISK_mktemp ("blacklist");
2946 GNUNET_assert (mytemp != NULL);
2947 temp_file_handle = fopen (mytemp, "wt");
2948 GNUNET_assert (temp_file_handle != NULL);
2949 temp_transports = GNUNET_strdup (transports);
2951 blacklist_ctx.temp_file_handle = temp_file_handle;
2953 transport_len = strlen (temp_transports) + 1;
2956 for (i = 0; i < transport_len; i++)
2958 if ((temp_transports[i] == ' ') && (pos == NULL))
2959 continue; /* At start of string (whitespace) */
2960 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2962 temp_transports[i] = '\0';
2964 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
2965 while (conn_iter != NULL)
2967 GNUNET_CRYPTO_hash_to_enc (
2968 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2970 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
2971 conn_iter = conn_iter->next;
2975 blacklist_ctx.transport = pos;
2976 entry_count = GNUNET_CONTAINER_multihashmap_iterate (pg->
2978 [pg_iter].blacklisted_peers,
2979 &blacklist_file_iterator,
2983 } /* At beginning of actual string */
2984 else if (pos == NULL)
2986 pos = &temp_transports[i];
2990 GNUNET_free (temp_transports);
2991 fclose (temp_file_handle);
2994 != GNUNET_CONFIGURATION_get_value_string (
2995 pg->peers[pg_iter]. daemon->cfg,
2996 "PATHS", "SERVICEHOME",
2997 &temp_service_path))
3000 GNUNET_ERROR_TYPE_WARNING,
3002 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
3003 "SERVICEHOME", "PATHS");
3004 if (UNLINK (mytemp) != 0)
3005 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
3007 GNUNET_free (mytemp);
3011 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
3013 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
3014 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
3017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3018 _("Copying file with command cp %s %s\n"), mytemp, arg);
3023 else /* Remote, scp the file to the correct place */
3025 if (NULL != pg->peers[pg_iter].daemon->username)
3026 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
3027 pg->peers[pg_iter].daemon->username,
3028 pg->peers[pg_iter].daemon->hostname,
3031 GNUNET_asprintf (&arg, "%s:%s/blacklist",
3032 pg->peers[pg_iter].daemon->hostname,
3034 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
3036 GNUNET_assert(procarr[pg_iter] != NULL);
3037 GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
3040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3041 _("Copying file with command scp %s %s\n"), mytemp,
3046 GNUNET_free (temp_service_path);
3047 GNUNET_free (mytemp);
3051 ret = GNUNET_SYSERR;
3052 while ((count < max_wait) && (ret != GNUNET_OK))
3055 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3059 _("Checking copy status of file %d\n"), pg_iter);
3061 if (procarr[pg_iter] != NULL) /* Check for already completed! */
3063 if (GNUNET_OS_process_status (procarr[pg_iter], &type,
3064 &return_code) != GNUNET_OK)
3066 ret = GNUNET_SYSERR;
3068 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
3070 ret = GNUNET_SYSERR;
3074 GNUNET_OS_process_close (procarr[pg_iter]);
3075 procarr[pg_iter] = NULL;
3077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3078 _("File %d copied\n"), pg_iter);
3084 if (ret == GNUNET_SYSERR)
3086 /* FIXME: why sleep here? -CG */
3092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3093 _("Finished copying all blacklist files!\n"));
3095 GNUNET_free (procarr);
3099 /* Forward Declaration */
3101 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3104 * Choose a random peer's next connection to create, and
3105 * call schedule_connect to set up the connect task.
3107 * @param ct_ctx the overall connection context
3110 preschedule_connect(struct GNUNET_TESTING_PeerGroup *pg)
3112 struct ConnectTopologyContext *ct_ctx = &pg->ct_ctx;
3113 struct PeerConnection *connection_iter;
3114 struct ConnectContext *connect_context;
3115 uint32_t random_peer;
3117 if (ct_ctx->remaining_connections == 0)
3120 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3121 while (pg->peers[random_peer].connect_peers_head == NULL)
3122 random_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3125 connection_iter = pg->peers[random_peer].connect_peers_head;
3126 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3127 connect_context->first_index = random_peer;
3128 connect_context->second_index = connection_iter->index;
3129 connect_context->ct_ctx = ct_ctx;
3130 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3131 GNUNET_CONTAINER_DLL_remove(pg->peers[random_peer].connect_peers_head, pg->peers[random_peer].connect_peers_tail, connection_iter);
3132 GNUNET_free(connection_iter);
3133 ct_ctx->remaining_connections--;
3137 /* Forward declaration */
3138 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3141 * Close connections and free the hello context.
3143 * @param cls the 'struct SendHelloContext *'
3144 * @param tc scheduler context
3147 free_hello_context (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3149 struct SendHelloContext *send_hello_context = cls;
3150 if (send_hello_context->peer->daemon->server != NULL)
3152 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3153 send_hello_context->peer->daemon->server = NULL;
3155 if (send_hello_context->peer->daemon->th != NULL)
3157 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3158 send_hello_context->peer->daemon->th = NULL;
3160 if (send_hello_context->core_connect_task != GNUNET_SCHEDULER_NO_TASK)
3162 GNUNET_SCHEDULER_cancel(send_hello_context->core_connect_task);
3163 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3165 send_hello_context->pg->outstanding_connects--;
3166 GNUNET_free(send_hello_context);
3170 * For peers that haven't yet connected, notify
3171 * the caller that they have failed (timeout).
3173 * @param cls the 'struct SendHelloContext *'
3174 * @param tc scheduler context
3177 notify_remaining_connections_failed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3179 struct SendHelloContext *send_hello_context = cls;
3180 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3181 struct PeerConnection *connection;
3183 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3184 send_hello_context->peer->daemon->server = NULL;
3186 connection = send_hello_context->peer->connect_peers_head;
3188 while (connection != NULL)
3190 if (pg->notify_connection != NULL)
3192 pg->notify_connection(pg->notify_connection_cls,
3193 &send_hello_context->peer->daemon->id,
3194 &pg->peers[connection->index].daemon->id,
3196 send_hello_context->peer->daemon->cfg,
3197 pg->peers[connection->index].daemon->cfg,
3198 send_hello_context->peer->daemon,
3199 pg->peers[connection->index].daemon,
3200 "Peers failed to connect (timeout)");
3202 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3203 GNUNET_free(connection);
3204 connection = connection->next;
3206 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3208 other_peer = &pg->peers[connection->index];
3213 * For peers that haven't yet connected, send
3214 * CORE connect requests.
3216 * @param cls the 'struct SendHelloContext *'
3217 * @param tc scheduler context
3220 send_core_connect_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3222 struct SendHelloContext *send_hello_context = cls;
3223 struct PeerConnection *conn;
3224 GNUNET_assert(send_hello_context->peer->daemon->server != NULL);
3226 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3228 send_hello_context->connect_attempts++;
3229 if (send_hello_context->connect_attempts < send_hello_context->pg->ct_ctx.connect_attempts)
3231 conn = send_hello_context->peer->connect_peers_head;
3232 while (conn != NULL)
3234 GNUNET_CORE_peer_request_connect(send_hello_context->peer->daemon->server,
3235 GNUNET_TIME_relative_get_forever(),
3236 &send_hello_context->pg->peers[conn->index].daemon->id,
3241 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) ,
3242 &send_core_connect_requests,
3243 send_hello_context);
3247 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Timeout before all connections created, marking rest as failed!\n");
3248 GNUNET_SCHEDULER_add_now(¬ify_remaining_connections_failed, send_hello_context);
3254 * Success, connection is up. Signal client our success.
3256 * @param cls our "struct SendHelloContext"
3257 * @param peer identity of the peer that has connected
3258 * @param atsi performance information
3260 * FIXME: remove peers from BOTH lists, call notify twice, should
3261 * double the speed of connections as long as the list iteration
3262 * doesn't take too long!
3265 core_connect_notify (void *cls,
3266 const struct GNUNET_PeerIdentity *peer,
3267 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3269 struct SendHelloContext *send_hello_context = cls;
3270 struct PeerConnection *connection;
3271 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3273 struct PeerData *other_peer;
3276 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3277 "Connected peer %s to peer %s\n",
3278 ctx->d1->shortname, GNUNET_i2s(peer));
3281 if (0 == memcmp(&send_hello_context->peer->daemon->id, peer, sizeof(struct GNUNET_PeerIdentity)))
3284 connection = send_hello_context->peer->connect_peers_head;
3289 while ((connection != NULL) &&
3290 (0 != memcmp(&pg->peers[connection->index].daemon->id, peer, sizeof(struct GNUNET_PeerIdentity))))
3292 connection = connection->next;
3295 if (connection == NULL)
3297 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);
3302 other_peer = &pg->peers[connection->index];
3304 if (pg->notify_connection != NULL)
3306 pg->notify_connection(pg->notify_connection_cls,
3307 &send_hello_context->peer->daemon->id,
3310 send_hello_context->peer->daemon->cfg,
3311 pg->peers[connection->index].daemon->cfg,
3312 send_hello_context->peer->daemon,
3313 pg->peers[connection->index].daemon,
3316 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3317 GNUNET_free(connection);
3321 /* Notify of reverse connection and remove from other peers list of outstanding */
3322 if (other_peer != NULL)
3324 connection = other_peer->connect_peers_head;
3325 while ((connection != NULL) &&
3326 (0 != memcmp(&send_hello_context->peer->daemon->id, &pg->peers[connection->index].daemon->id, sizeof(struct GNUNET_PeerIdentity))))
3328 connection = connection->next;
3330 if (connection != NULL)
3332 if (pg->notify_connection != NULL)
3334 pg->notify_connection(pg->notify_connection_cls,
3336 &send_hello_context->peer->daemon->id,
3338 pg->peers[connection->index].daemon->cfg,
3339 send_hello_context->peer->daemon->cfg,
3340 pg->peers[connection->index].daemon,
3341 send_hello_context->peer->daemon,
3345 GNUNET_CONTAINER_DLL_remove(other_peer->connect_peers_head, other_peer->connect_peers_tail, connection);
3346 GNUNET_free(connection);
3351 if (send_hello_context->peer->connect_peers_head == NULL)
3353 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3358 * Notify of a successful connection to the core service.
3360 * @param cls a struct SendHelloContext *
3361 * @param server handle to the core service
3362 * @param my_identity the peer identity of this peer
3363 * @param publicKey the public key of the peer
3366 core_init (void *cls,
3367 struct GNUNET_CORE_Handle * server,
3368 const struct GNUNET_PeerIdentity *
3371 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
3374 struct SendHelloContext *send_hello_context = cls;
3375 send_hello_context->core_ready = GNUNET_YES;
3379 * Function called once a hello has been sent
3380 * to the transport, move on to the next one
3381 * or go away forever.
3383 * @param cls the 'struct SendHelloContext *'
3384 * @param tc scheduler context
3387 hello_sent_callback (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3389 struct SendHelloContext *send_hello_context = cls;
3390 //unsigned int pg_iter;
3391 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3393 GNUNET_free(send_hello_context);
3397 send_hello_context->pg->remaining_hellos--;
3399 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sent HELLO, have %d remaining!\n", send_hello_context->pg->remaining_hellos);
3401 if (send_hello_context->peer_pos == NULL) /* All HELLOs (for this peer!) have been transmitted! */
3404 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All hellos for this peer sent, disconnecting transport!\n");
3406 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3407 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3408 send_hello_context->peer->daemon->th = NULL;
3410 /*if (send_hello_context->pg->remaining_hellos == 0)
3412 for (pg_iter = 0; pg_iter < send_hello_context->pg->max_outstanding_connections; pg_iter++)
3414 preschedule_connect(&send_hello_context->pg->ct_ctx);
3418 GNUNET_assert (send_hello_context->peer->daemon->server == NULL);
3419 send_hello_context->peer->daemon->server = GNUNET_CORE_connect(send_hello_context->peer->cfg,
3423 &core_connect_notify,
3430 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),
3431 &send_core_connect_requests,
3432 send_hello_context);
3435 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3439 * Connect to a peer, give it all the HELLO's of those peers
3440 * we will later ask it to connect to.
3442 * @param ct_ctx the overall connection context
3444 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3446 struct SendHelloContext *send_hello_context = cls;
3447 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3449 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3451 GNUNET_free(send_hello_context);
3455 GNUNET_assert(send_hello_context->peer_pos != NULL); /* All of the HELLO sends to be scheduled have been scheduled! */
3457 if (((send_hello_context->peer->daemon->th == NULL) &&
3458 (pg->outstanding_connects > pg->max_outstanding_connections)) ||
3459 (pg->stop_connects == GNUNET_YES))
3461 #if VERBOSE_TESTING > 2
3462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3464 ("Delaying connect, we have too many outstanding connections!\n"));
3466 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3467 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3468 &schedule_send_hellos, send_hello_context);
3472 #if VERBOSE_TESTING > 2
3473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3474 _("Creating connection, outstanding_connections is %d\n"),
3475 outstanding_connects);
3477 if (send_hello_context->peer->daemon->th == NULL)
3479 pg->outstanding_connects++; /* Actual TRANSPORT, CORE connections! */
3480 send_hello_context->peer->daemon->th
3481 = GNUNET_TRANSPORT_connect (send_hello_context->peer->cfg, NULL,
3482 send_hello_context, NULL, NULL, NULL);
3485 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3486 _("Offering Hello of peer %s to peer %s\n"),
3487 send_hello_context->peer->daemon->shortname, pg->peers[send_hello_context->peer_pos->index].daemon->shortname);
3489 GNUNET_TRANSPORT_offer_hello(send_hello_context->peer->daemon->th,
3490 (const struct GNUNET_MessageHeader *)pg->peers[send_hello_context->peer_pos->index].daemon->hello,
3491 &hello_sent_callback,
3492 send_hello_context);
3493 send_hello_context->peer_pos = send_hello_context->peer_pos->next;
3494 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3500 * Internal notification of a connection, kept so that we can ensure some connections
3501 * happen instead of flooding all testing daemons with requests to connect.
3504 internal_connect_notify(void *cls, const struct GNUNET_PeerIdentity *first,
3505 const struct GNUNET_PeerIdentity *second,
3507 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
3508 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
3509 struct GNUNET_TESTING_Daemon *first_daemon,
3510 struct GNUNET_TESTING_Daemon *second_daemon,
3513 struct ConnectContext *connect_ctx = cls;
3514 struct ConnectTopologyContext *ct_ctx = connect_ctx->ct_ctx;
3515 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
3516 struct PeerConnection *connection;
3517 pg->outstanding_connects--;
3520 * Check whether the inverse connection has been scheduled yet,
3521 * if not, we can remove it from the other peers list and avoid
3522 * even trying to connect them again!
3524 connection = pg->peers[connect_ctx->second_index].connect_peers_head;
3529 while ((connection != NULL) && (0
3530 != memcmp (first, &pg->peers[connection->index].daemon->id,
3531 sizeof(struct GNUNET_PeerIdentity))))
3533 connection = connection->next;
3536 if (connection != NULL) /* Can safely remove! */
3538 ct_ctx->remaining_connections--;
3539 if (pg->notify_connection != NULL) /* Notify of reverse connection */
3540 pg->notify_connection (pg->notify_connection_cls, second, first,
3541 distance, second_cfg, first_cfg, second_daemon,
3542 first_daemon, emsg);
3544 GNUNET_CONTAINER_DLL_remove(pg->peers[connect_ctx->second_index].connect_peers_head, pg->peers[connect_ctx->second_index].connect_peers_tail, connection);
3545 GNUNET_free(connection);
3548 if (ct_ctx->remaining_connections == 0)
3550 if (ct_ctx->notify_connections_done != NULL)
3551 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
3554 preschedule_connect (pg);
3556 if (pg->notify_connection != NULL)
3557 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
3558 first_cfg, second_cfg, first_daemon, second_daemon,
3561 GNUNET_free(connect_ctx);
3565 * Either delay a connection (because there are too many outstanding)
3566 * or schedule it for right now.
3568 * @param cls a connection context
3569 * @param tc the task runtime context
3572 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3574 struct ConnectContext *connect_context = cls;
3575 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
3577 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3580 if ((pg->outstanding_connects > pg->max_outstanding_connections)
3581 || (pg->stop_connects == GNUNET_YES))
3584 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3586 ("Delaying connect, we have too many outstanding connections!\n"));
3588 GNUNET_SCHEDULER_add_delayed (
3589 GNUNET_TIME_relative_multiply (
3590 GNUNET_TIME_UNIT_MILLISECONDS,
3592 &schedule_connect, connect_context);
3597 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3598 _("Creating connection, outstanding_connections is %d (max %d)\n"),
3599 pg->outstanding_connects, pg->max_outstanding_connections);
3601 pg->outstanding_connects++;
3602 pg->total_connects_scheduled++;
3603 GNUNET_TESTING_daemons_connect (
3604 pg->peers[connect_context->first_index].daemon,
3605 pg->peers[connect_context->second_index].daemon,
3606 connect_context->ct_ctx->connect_timeout,
3607 connect_context->ct_ctx->connect_attempts,
3613 &internal_connect_notify, connect_context); /* FIXME: free connect context! */
3619 * Iterator for actually scheduling connections to be created
3620 * between two peers.
3622 * @param cls closure, a GNUNET_TESTING_Daemon
3623 * @param key the key the second Daemon was stored under
3624 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3626 * @return GNUNET_YES to continue iteration
3629 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3631 struct ConnectTopologyContext *ct_ctx = cls;
3632 struct PeerData *first = ct_ctx->first;
3633 struct GNUNET_TESTING_Daemon *second = value;
3634 struct ConnectContext *connect_context;
3636 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3637 connect_context->first = first->daemon;
3638 connect_context->second = second;
3639 connect_context->ct_ctx = ct_ctx;
3640 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3648 * Iterator for copying all entries in the allowed hashmap to the
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 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3660 struct PeerData *first = cls;
3662 GNUNET_assert (GNUNET_OK ==
3663 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
3665 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3672 * Make the peers to connect the same as those that are allowed to be
3675 * @param pg the peer group
3678 copy_allowed_topology(struct GNUNET_TESTING_PeerGroup *pg)
3680 unsigned int pg_iter;
3684 struct PeerConnection *iter;
3688 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3691 iter = pg->peers[pg_iter].allowed_peers_head;
3692 while (iter != NULL)
3694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3695 "Creating connection between %d and %d\n", pg_iter,
3697 total += add_connections (pg, pg_iter, iter->index, CONNECT,
3699 //total += add_actual_connections(pg, pg_iter, iter->index);
3704 GNUNET_CONTAINER_multihashmap_iterate (pg->
3705 peers[pg_iter].allowed_peers,
3706 ©_topology_iterator,
3707 &pg->peers[pg_iter]);
3709 if (GNUNET_SYSERR == ret)
3710 return GNUNET_SYSERR;
3712 total = total + ret;
3719 * Connect the topology as specified by the PeerConnection's
3720 * of each peer in the peer group
3722 * @param pg the peer group we are dealing with
3723 * @param connect_timeout how long try connecting two peers
3724 * @param connect_attempts how many times (max) to attempt
3725 * @param notify_callback callback to notify when finished
3726 * @param notify_cls closure for notify callback
3728 * @return the number of connections that will be attempted
3731 connect_topology(struct GNUNET_TESTING_PeerGroup *pg,
3732 struct GNUNET_TIME_Relative connect_timeout,
3733 unsigned int connect_attempts,
3734 GNUNET_TESTING_NotifyCompletion notify_callback,
3737 unsigned int pg_iter;
3741 struct PeerConnection *connection_iter;
3744 struct SendHelloContext *send_hello_context;
3748 pg->ct_ctx.notify_connections_done = notify_callback;
3749 pg->ct_ctx.notify_cls = notify_cls;
3752 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3755 connection_iter = pg->peers[pg_iter].connect_peers_head;
3756 while (connection_iter != NULL)
3758 connection_iter = connection_iter->next;
3763 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3770 pg->ct_ctx.connect_timeout = connect_timeout;
3771 pg->ct_ctx.connect_attempts = connect_attempts;
3772 pg->ct_ctx.remaining_connections = total;
3775 /* First give all peers the HELLO's of other peers (connect to first peer's transport service, give HELLO's of other peers, continue...) */
3776 pg->remaining_hellos = total;
3777 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3779 send_hello_context = GNUNET_malloc(sizeof(struct SendHelloContext));
3780 send_hello_context->peer = &pg->peers[pg_iter];
3781 send_hello_context->peer_pos = pg->peers[pg_iter].connect_peers_head;
3782 send_hello_context->pg = pg;
3783 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3786 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3788 preschedule_connect (pg);
3796 * Takes a peer group and creates a topology based on the
3797 * one specified. Creates a topology means generates friend
3798 * files for the peers so they can only connect to those allowed
3799 * by the topology. This will only have an effect once peers
3800 * are started if the FRIENDS_ONLY option is set in the base
3801 * config. Also takes an optional restrict topology which
3802 * disallows connections based on particular transports
3803 * UNLESS they are specified in the restricted topology.
3805 * @param pg the peer group struct representing the running peers
3806 * @param topology which topology to connect the peers in
3807 * @param restrict_topology disallow restrict_transports transport
3808 * connections to peers NOT in this topology
3809 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3810 * @param restrict_transports space delimited list of transports to blacklist
3811 * to create restricted topology
3813 * @return the maximum number of connections were all allowed peers
3814 * connected to each other
3817 GNUNET_TESTING_create_topology(struct GNUNET_TESTING_PeerGroup *pg,
3818 enum GNUNET_TESTING_Topology topology,
3819 enum GNUNET_TESTING_Topology restrict_topology,
3820 const char *restrict_transports)
3824 unsigned int num_connections;
3825 int unblacklisted_connections;
3827 struct PeerConnection *conn_iter;
3828 struct PeerConnection *temp_conn;
3833 for (i = 0; i < pg->total; i++)
3835 pg->peers[i].allowed_peers =
3836 GNUNET_CONTAINER_multihashmap_create (100);
3837 pg->peers[i].connect_peers =
3838 GNUNET_CONTAINER_multihashmap_create (100);
3839 pg->peers[i].blacklisted_peers =
3840 GNUNET_CONTAINER_multihashmap_create (100);
3841 pg->peers[i].pg = pg;
3847 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3851 num_connections = create_clique (pg, &add_connections, ALLOWED, GNUNET_NO);
3853 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3856 _("Creating small world (ring) topology\n"));
3858 num_connections = create_small_world_ring (pg, &add_connections, ALLOWED);
3860 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3863 _("Creating small world (2d-torus) topology\n"));
3865 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3867 case GNUNET_TESTING_TOPOLOGY_RING:
3869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3871 num_connections = create_ring (pg, &add_connections, ALLOWED);
3873 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3877 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3879 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3882 _("Creating Erdos-Renyi topology\n"));
3884 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3886 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3890 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3892 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3895 _("Creating Scale Free topology\n"));
3897 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3899 case GNUNET_TESTING_TOPOLOGY_LINE:
3901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3902 _("Creating straight line topology\n"));
3904 num_connections = create_line (pg, &add_connections, ALLOWED);
3906 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3909 _("Creating topology from file!\n"));
3911 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing",
3914 num_connections = create_from_file (pg, filename, &add_connections,
3919 GNUNET_ERROR_TYPE_WARNING,
3920 "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3921 num_connections = 0;
3924 case GNUNET_TESTING_TOPOLOGY_NONE:
3926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3928 ("Creating no allowed topology (all peers can connect at core level)\n"));
3930 num_connections = pg->total * pg->total; /* Clique is allowed! */
3933 num_connections = 0;
3937 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING",
3940 ret = create_and_copy_friend_files (pg);
3941 if (ret != GNUNET_OK)
3944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3945 _("Failed during friend file copying!\n"));
3947 return GNUNET_SYSERR;
3952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3953 _("Friend files created/copied successfully!\n"));
3958 /* Use the create clique method to initially set all connections as blacklisted. */
3959 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology
3960 != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
3961 create_clique (pg, &add_connections, BLACKLIST, GNUNET_NO);
3963 return num_connections;
3965 unblacklisted_connections = 0;
3966 /* Un-blacklist connections as per the topology specified */
3967 switch (restrict_topology)
3969 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3972 _("Blacklisting all but clique topology\n"));
3974 unblacklisted_connections = create_clique (pg, &remove_connections,
3975 BLACKLIST, GNUNET_NO);
3977 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3980 _("Blacklisting all but small world (ring) topology\n"));
3982 unblacklisted_connections = create_small_world_ring (pg,
3983 &remove_connections,
3986 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3990 ("Blacklisting all but small world (2d-torus) topology\n"));
3992 unblacklisted_connections = create_small_world (pg, &remove_connections,
3995 case GNUNET_TESTING_TOPOLOGY_RING:
3997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3998 _("Blacklisting all but ring topology\n"));
4000 unblacklisted_connections
4001 = create_ring (pg, &remove_connections, BLACKLIST);
4003 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4006 _("Blacklisting all but 2d torus topology\n"));
4008 unblacklisted_connections = create_2d_torus (pg, &remove_connections,
4011 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4014 _("Blacklisting all but Erdos-Renyi topology\n"));
4016 unblacklisted_connections = create_erdos_renyi (pg, &remove_connections,
4019 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4022 _("Blacklisting all but InterNAT topology\n"));
4026 for (off = 0; off < pg->total; off++)
4028 conn_iter = pg->peers[off].allowed_peers_head;
4029 while (conn_iter != NULL)
4031 temp_conn = conn_iter->next;
4032 GNUNET_free(conn_iter);
4033 conn_iter = temp_conn;
4035 pg->peers[off].allowed_peers_head = NULL;
4036 pg->peers[off].allowed_peers_tail = NULL;
4038 conn_iter = pg->peers[off].connect_peers_head;
4039 while (conn_iter != NULL)
4041 temp_conn = conn_iter->next;
4042 GNUNET_free(conn_iter);
4043 conn_iter = temp_conn;
4045 pg->peers[off].connect_peers_head = NULL;
4046 pg->peers[off].connect_peers_tail = NULL;
4048 unblacklisted_connections
4049 = create_nated_internet_copy (pg, &remove_connections, BLACKLIST);
4051 unblacklisted_connections =
4052 create_nated_internet (pg, &remove_connections, BLACKLIST);
4056 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4059 _("Blacklisting all but Scale Free topology\n"));
4061 unblacklisted_connections = create_scale_free (pg, &remove_connections,
4064 case GNUNET_TESTING_TOPOLOGY_LINE:
4066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4067 _("Blacklisting all but straight line topology\n"));
4069 unblacklisted_connections
4070 = create_line (pg, &remove_connections, BLACKLIST);
4072 case GNUNET_TESTING_TOPOLOGY_NONE: /* Fall through */
4073 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
4075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4077 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
4079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
4080 ("Creating blacklist topology from allowed\n"));
4081 unblacklisted_connections = copy_allowed (pg, &remove_connections);
4086 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
4088 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n",
4089 restrict_transports);
4090 ret = create_and_copy_blacklist_files (pg, restrict_transports);
4091 if (ret != GNUNET_OK)
4094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4095 _("Failed during blacklist file copying!\n"));
4102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4103 _("Blacklist files created/copied successfully!\n"));
4107 return num_connections;
4112 * Iterator for choosing random peers to connect.
4114 * @param cls closure, a RandomContext
4115 * @param key the key the second Daemon was stored under
4116 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4118 * @return GNUNET_YES to continue iteration
4121 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4123 struct RandomContext *random_ctx = cls;
4124 double random_number;
4125 uint32_t second_pos;
4126 GNUNET_HashCode first_hash;
4129 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4130 UINT64_MAX)) / ((double) UINT64_MAX);
4131 if (random_number < random_ctx->percentage)
4133 GNUNET_assert (GNUNET_OK ==
4134 GNUNET_CONTAINER_multihashmap_put (random_ctx->
4135 first->connect_peers_working_set,
4137 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4140 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
4141 uid_from_hash (key, &second_pos);
4142 hash_from_uid (random_ctx->first_uid, &first_hash);
4143 GNUNET_assert (random_ctx->pg->total > second_pos);
4144 GNUNET_assert (GNUNET_YES ==
4145 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
4147 [second_pos].connect_peers,
4156 * Iterator for adding at least X peers to a peers connection set.
4158 * @param cls closure, MinimumContext
4159 * @param key the key the second Daemon was stored under
4160 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4162 * @return GNUNET_YES to continue iteration
4165 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4167 struct MinimumContext *min_ctx = cls;
4168 uint32_t second_pos;
4169 GNUNET_HashCode first_hash;
4172 if (GNUNET_CONTAINER_multihashmap_size
4173 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
4175 for (i = 0; i < min_ctx->num_to_add; i++)
4177 if (min_ctx->pg_array[i] == min_ctx->current)
4179 GNUNET_assert (GNUNET_OK ==
4180 GNUNET_CONTAINER_multihashmap_put
4181 (min_ctx->first->connect_peers_working_set, key,
4183 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4184 uid_from_hash (key, &second_pos);
4185 hash_from_uid (min_ctx->first_uid, &first_hash);
4186 GNUNET_assert (min_ctx->pg->total > second_pos);
4187 GNUNET_assert (GNUNET_OK ==
4188 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4190 [second_pos].connect_peers_working_set,
4194 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4195 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
4196 GNUNET_assert (GNUNET_YES ==
4197 GNUNET_CONTAINER_multihashmap_remove
4198 (min_ctx->pg->peers[second_pos].connect_peers,
4199 &first_hash, min_ctx->first->daemon));
4206 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
4211 * Iterator for adding peers to a connection set based on a depth first search.
4213 * @param cls closure, MinimumContext
4214 * @param key the key the second daemon was stored under
4215 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4217 * @return GNUNET_YES to continue iteration
4220 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4222 struct DFSContext *dfs_ctx = cls;
4223 GNUNET_HashCode first_hash;
4225 if (dfs_ctx->current == dfs_ctx->chosen)
4227 GNUNET_assert (GNUNET_OK ==
4228 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4229 first->connect_peers_working_set,
4231 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4232 uid_from_hash (key, &dfs_ctx->second_uid);
4233 hash_from_uid (dfs_ctx->first_uid, &first_hash);
4234 GNUNET_assert (GNUNET_OK ==
4235 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4237 [dfs_ctx->second_uid].connect_peers_working_set,
4241 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4242 GNUNET_assert (GNUNET_YES ==
4243 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
4245 [dfs_ctx->second_uid].connect_peers,
4249 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
4250 return GNUNET_NO; /* We have found our peer, don't iterate more */
4259 * From the set of connections possible, choose percentage percent of connections
4260 * to actually connect.
4262 * @param pg the peergroup we are dealing with
4263 * @param percentage what percent of total connections to make
4266 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg,
4269 struct RandomContext random_ctx;
4272 struct PeerConnection *temp_peers;
4273 struct PeerConnection *conn_iter;
4274 double random_number;
4277 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4279 random_ctx.first_uid = pg_iter;
4280 random_ctx.first = &pg->peers[pg_iter];
4281 random_ctx.percentage = percentage;
4285 conn_iter = pg->peers[pg_iter].connect_peers_head;
4286 while (conn_iter != NULL)
4289 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4291 / ((double) UINT64_MAX);
4292 if (random_number < percentage)
4294 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4297 conn_iter = conn_iter->next;
4300 pg->peers[pg_iter].connect_peers_working_set =
4301 GNUNET_CONTAINER_multihashmap_create (pg->total);
4302 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4303 &random_connect_iterator,
4305 /* Now remove the old connections */
4306 GNUNET_CONTAINER_multihashmap_destroy (pg->
4307 peers[pg_iter].connect_peers);
4308 /* And replace with the random set */
4309 pg->peers[pg_iter].connect_peers =
4310 pg->peers[pg_iter].connect_peers_working_set;
4314 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4316 conn_iter = pg->peers[pg_iter].connect_peers_head;
4317 while (pg->peers[pg_iter].connect_peers_head != NULL)
4318 remove_connections (pg, pg_iter,
4319 pg->peers[pg_iter].connect_peers_head->index,
4320 CONNECT, GNUNET_YES);
4322 pg->peers[pg_iter].connect_peers_head
4323 = pg->peers[pg_iter].connect_peers_working_set_head;
4324 pg->peers[pg_iter].connect_peers_tail
4325 = pg->peers[pg_iter].connect_peers_working_set_tail;
4326 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4327 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4332 * Count the number of connections in a linked list of connections.
4334 * @param conn_list the connection list to get the count of
4336 * @return the number of elements in the list
4339 count_connections(struct PeerConnection *conn_list)
4341 struct PeerConnection *iter;
4345 while (iter != NULL)
4354 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
4357 unsigned int pg_iter;
4359 struct PeerConnection *conn_iter;
4363 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4366 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
4367 while (conn_iter != NULL)
4370 conn_iter = conn_iter->next;
4374 GNUNET_CONTAINER_multihashmap_size (pg->
4376 [pg_iter].connect_peers_working_set);
4384 count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
4387 unsigned int pg_iter;
4389 struct PeerConnection *conn_iter;
4393 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4396 conn_iter = pg->peers[pg_iter].allowed_peers_head;
4397 while (conn_iter != NULL)
4400 conn_iter = conn_iter->next;
4404 GNUNET_CONTAINER_multihashmap_size (pg->
4406 [pg_iter].allowed_peers);
4414 * From the set of connections possible, choose at least num connections per
4417 * @param pg the peergroup we are dealing with
4418 * @param num how many connections at least should each peer have (if possible)?
4421 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4424 struct MinimumContext minimum_ctx;
4426 struct PeerConnection *conn_iter;
4427 unsigned int temp_list_size;
4430 uint32_t random; /* Random list entry to connect peer to */
4435 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4438 = count_connections (pg->peers[pg_iter].connect_peers_head);
4439 if (temp_list_size == 0)
4441 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4442 "Peer %d has 0 connections!?!?\n", pg_iter);
4445 for (i = 0; i < num; i++)
4447 random = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4449 conn_iter = pg->peers[pg_iter].connect_peers_head;
4450 for (count = 0; count < random; count++)
4451 conn_iter = conn_iter->next;
4452 /* We now have a random connection, connect it! */
4453 GNUNET_assert(conn_iter != NULL);
4454 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4459 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4461 pg->peers[pg_iter].connect_peers_working_set =
4462 GNUNET_CONTAINER_multihashmap_create (num);
4465 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4467 minimum_ctx.first_uid = pg_iter;
4468 minimum_ctx.pg_array =
4469 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
4470 GNUNET_CONTAINER_multihashmap_size
4471 (pg->peers[pg_iter].connect_peers));
4472 minimum_ctx.first = &pg->peers[pg_iter];
4473 minimum_ctx.pg = pg;
4474 minimum_ctx.num_to_add = num;
4475 minimum_ctx.current = 0;
4476 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4477 &minimum_connect_iterator,
4481 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4483 /* Remove the "old" connections */
4484 GNUNET_CONTAINER_multihashmap_destroy (pg->
4485 peers[pg_iter].connect_peers);
4486 /* And replace with the working set */
4487 pg->peers[pg_iter].connect_peers =
4488 pg->peers[pg_iter].connect_peers_working_set;
4491 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4493 while (pg->peers[pg_iter].connect_peers_head != NULL)
4495 conn_iter = pg->peers[pg_iter].connect_peers_head;
4496 GNUNET_CONTAINER_DLL_remove(pg->peers[pg_iter].connect_peers_head,
4497 pg->peers[pg_iter].connect_peers_tail,
4499 GNUNET_free(conn_iter);
4500 /*remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT, GNUNET_YES);*/
4503 pg->peers[pg_iter].connect_peers_head
4504 = pg->peers[pg_iter].connect_peers_working_set_head;
4505 pg->peers[pg_iter].connect_peers_tail
4506 = pg->peers[pg_iter].connect_peers_working_set_tail;
4507 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4508 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4513 struct FindClosestContext
4516 * The currently known closest peer.
4518 struct GNUNET_TESTING_Daemon *closest;
4521 * The info for the peer we are adding connections for.
4523 struct PeerData *curr_peer;
4526 * The distance (bits) between the current
4527 * peer and the currently known closest.
4529 unsigned int closest_dist;
4532 * The offset of the closest known peer in
4535 unsigned int closest_num;
4539 * Iterator over hash map entries of the allowed
4540 * peer connections. Find the closest, not already
4541 * connected peer and return it.
4543 * @param cls closure (struct FindClosestContext)
4544 * @param key current key code (hash of offset in pg)
4545 * @param value value in the hash map - a GNUNET_TESTING_Daemon
4546 * @return GNUNET_YES if we should continue to
4551 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
4553 struct FindClosestContext *closest_ctx = cls;
4554 struct GNUNET_TESTING_Daemon *daemon = value;
4556 if (((closest_ctx->closest == NULL) ||
4557 (GNUNET_CRYPTO_hash_matching_bits
4558 (&daemon->id.hashPubKey,
4559 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
4560 closest_ctx->closest_dist))
4562 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
4563 curr_peer->connect_peers,
4566 closest_ctx->closest_dist =
4567 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
4568 &closest_ctx->curr_peer->daemon->
4570 closest_ctx->closest = daemon;
4571 uid_from_hash (key, &closest_ctx->closest_num);
4577 * From the set of connections possible, choose at num connections per
4578 * peer based on depth which are closest out of those allowed. Guaranteed
4579 * to add num peers to connect to, provided there are that many peers
4580 * in the underlay topology to connect to.
4582 * @param pg the peergroup we are dealing with
4583 * @param num how many connections at least should each peer have (if possible)?
4584 * @param proc processor to actually add the connections
4585 * @param list the peer list to use
4588 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
4589 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
4594 struct FindClosestContext closest_ctx;
4599 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
4601 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4603 closest_ctx.curr_peer = &pg->peers[pg_iter];
4604 closest_ctx.closest = NULL;
4605 closest_ctx.closest_dist = 0;
4606 closest_ctx.closest_num = 0;
4607 GNUNET_CONTAINER_multihashmap_iterate (pg->
4608 peers[pg_iter].allowed_peers,
4609 &find_closest_peers,
4611 if (closest_ctx.closest != NULL)
4613 GNUNET_assert (closest_ctx.closest_num < pg->total);
4614 proc (pg, pg_iter, closest_ctx.closest_num, list);
4622 * From the set of connections possible, choose at least num connections per
4623 * peer based on depth first traversal of peer connections. If DFS leaves
4624 * peers unconnected, ensure those peers get connections.
4626 * @param pg the peergroup we are dealing with
4627 * @param num how many connections at least should each peer have (if possible)?
4630 perform_dfs(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4634 uint32_t starting_peer;
4635 uint32_t least_connections;
4636 uint32_t random_connection;
4638 unsigned int temp_count;
4639 struct PeerConnection *peer_iter;
4641 struct DFSContext dfs_ctx;
4642 GNUNET_HashCode second_hash;
4648 while ((count_workingset_connections (pg) < num * pg->total)
4649 && (count_allowed_connections (pg) > 0))
4651 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4653 least_connections = -1; /* Set to very high number */
4654 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4657 = count_connections (
4658 pg->peers[pg_iter].connect_peers_working_set_head);
4659 if (temp_count < least_connections)
4661 starting_peer = pg_iter;
4662 least_connections = temp_count;
4668 = count_connections (pg->peers[starting_peer].connect_peers_head);
4669 if (temp_count == 0)
4670 continue; /* FIXME: infinite loop? */
4672 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4675 peer_iter = pg->peers[starting_peer].connect_peers_head;
4676 while (temp_count < random_connection)
4678 peer_iter = peer_iter->next;
4681 GNUNET_assert(peer_iter != NULL);
4682 add_connections (pg, starting_peer, peer_iter->index, WORKING_SET,
4684 remove_connections (pg, starting_peer, peer_iter->index, CONNECT,
4686 starting_peer = peer_iter->index;
4691 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4693 pg->peers[pg_iter].connect_peers_working_set =
4694 GNUNET_CONTAINER_multihashmap_create (num);
4699 while ((count_workingset_connections (pg) < num * pg->total)
4700 && (count_allowed_connections (pg) > 0))
4702 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4704 least_connections = -1; /* Set to very high number */
4705 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4707 if (GNUNET_CONTAINER_multihashmap_size
4708 (pg->peers[pg_iter].connect_peers_working_set) <
4711 starting_peer = pg_iter;
4713 GNUNET_CONTAINER_multihashmap_size (pg->
4715 [pg_iter].connect_peers_working_set);
4720 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4726 /* Choose a random peer from the chosen peers set of connections to add */
4728 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4729 GNUNET_CONTAINER_multihashmap_size
4730 (pg->peers[starting_peer].connect_peers));
4731 dfs_ctx.first_uid = starting_peer;
4732 dfs_ctx.first = &pg->peers[starting_peer];
4734 dfs_ctx.current = 0;
4736 GNUNET_CONTAINER_multihashmap_iterate (pg->
4738 [starting_peer].connect_peers,
4739 &dfs_connect_iterator, &dfs_ctx);
4740 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4741 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4742 GNUNET_assert (GNUNET_YES ==
4743 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4744 [starting_peer].connect_peers,
4748 [dfs_ctx.second_uid].daemon));
4749 starting_peer = dfs_ctx.second_uid;
4752 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4754 /* Remove the "old" connections */
4755 GNUNET_CONTAINER_multihashmap_destroy (pg->
4756 peers[pg_iter].connect_peers);
4757 /* And replace with the working set */
4758 pg->peers[pg_iter].connect_peers =
4759 pg->peers[pg_iter].connect_peers_working_set;
4765 * Internal callback for topology information for a particular peer.
4768 internal_topology_callback(void *cls, const struct GNUNET_PeerIdentity *peer,
4769 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4771 struct CoreContext *core_ctx = cls;
4772 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4774 if (peer == NULL) /* Either finished, or something went wrong */
4776 iter_ctx->completed++;
4777 iter_ctx->connected--;
4778 /* One core context allocated per iteration, must free! */
4779 GNUNET_free (core_ctx);
4783 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id, peer, NULL);
4786 if (iter_ctx->completed == iter_ctx->total)
4788 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4789 /* Once all are done, free the iteration context */
4790 GNUNET_free (iter_ctx);
4795 * Check running topology iteration tasks, if below max start a new one, otherwise
4796 * schedule for some time in the future.
4799 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4801 struct CoreContext *core_context = cls;
4802 struct TopologyIterateContext *topology_context =
4803 (struct TopologyIterateContext *) core_context->iter_context;
4804 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4807 if (topology_context->connected
4808 > topology_context->pg->max_outstanding_connections)
4810 #if VERBOSE_TESTING > 2
4811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4813 ("Delaying connect, we have too many outstanding connections!\n"));
4815 GNUNET_SCHEDULER_add_delayed (
4816 GNUNET_TIME_relative_multiply (
4817 GNUNET_TIME_UNIT_MILLISECONDS,
4819 &schedule_get_topology, core_context);
4823 #if VERBOSE_TESTING > 2
4824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4825 _("Creating connection, outstanding_connections is %d\n"),
4826 outstanding_connects);
4828 topology_context->connected++;
4830 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4831 &internal_topology_callback,
4834 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4835 internal_topology_callback (core_context, NULL, NULL);
4841 * Iterate over all (running) peers in the peer group, retrieve
4842 * all connections that each currently has.
4845 GNUNET_TESTING_get_topology(struct GNUNET_TESTING_PeerGroup *pg,
4846 GNUNET_TESTING_NotifyTopology cb, void *cls)
4848 struct TopologyIterateContext *topology_context;
4849 struct CoreContext *core_ctx;
4851 unsigned int total_count;
4853 /* Allocate a single topology iteration context */
4854 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4855 topology_context->topology_cb = cb;
4856 topology_context->cls = cls;
4857 topology_context->pg = pg;
4859 for (i = 0; i < pg->total; i++)
4861 if (pg->peers[i].daemon->running == GNUNET_YES)
4863 /* Allocate one core context per core we need to connect to */
4864 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4865 core_ctx->daemon = pg->peers[i].daemon;
4866 /* Set back pointer to topology iteration context */
4867 core_ctx->iter_context = topology_context;
4868 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4872 if (total_count == 0)
4874 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4875 GNUNET_free (topology_context);
4878 topology_context->total = total_count;
4883 * Callback function to process statistic values.
4884 * This handler is here only really to insert a peer
4885 * identity (or daemon) so the statistics can be uniquely
4886 * tied to a single running peer.
4888 * @param cls closure
4889 * @param subsystem name of subsystem that created the statistic
4890 * @param name the name of the datum
4891 * @param value the current value
4892 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4893 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4896 internal_stats_callback(void *cls, const char *subsystem, const char *name,
4897 uint64_t value, int is_persistent)
4899 struct StatsCoreContext *core_context = cls;
4900 struct StatsIterateContext *stats_context =
4901 (struct StatsIterateContext *) core_context->iter_context;
4903 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4904 subsystem, name, value, is_persistent);
4908 * Internal continuation call for statistics iteration.
4910 * @param cls closure, the CoreContext for this iteration
4911 * @param success whether or not the statistics iterations
4912 * was canceled or not (we don't care)
4915 internal_stats_cont(void *cls, int success)
4917 struct StatsCoreContext *core_context = cls;
4918 struct StatsIterateContext *stats_context =
4919 (struct StatsIterateContext *) core_context->iter_context;
4921 stats_context->connected--;
4922 stats_context->completed++;
4924 if (stats_context->completed == stats_context->total)
4926 stats_context->cont (stats_context->cls, GNUNET_YES);
4927 GNUNET_free (stats_context);
4930 if (core_context->stats_handle != NULL)
4931 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4933 GNUNET_free (core_context);
4937 * Check running topology iteration tasks, if below max start a new one, otherwise
4938 * schedule for some time in the future.
4941 schedule_get_statistics(void *cls,
4942 const struct GNUNET_SCHEDULER_TaskContext *tc)
4944 struct StatsCoreContext *core_context = cls;
4945 struct StatsIterateContext *stats_context =
4946 (struct StatsIterateContext *) core_context->iter_context;
4948 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4951 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
4953 #if VERBOSE_TESTING > 2
4954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4956 ("Delaying connect, we have too many outstanding connections!\n"));
4958 GNUNET_SCHEDULER_add_delayed (
4959 GNUNET_TIME_relative_multiply (
4960 GNUNET_TIME_UNIT_MILLISECONDS,
4962 &schedule_get_statistics, core_context);
4966 #if VERBOSE_TESTING > 2
4967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4968 _("Creating connection, outstanding_connections is %d\n"),
4969 outstanding_connects);
4972 stats_context->connected++;
4973 core_context->stats_handle
4974 = GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
4975 if (core_context->stats_handle == NULL)
4977 internal_stats_cont (core_context, GNUNET_NO);
4981 core_context->stats_get_handle
4982 = GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
4983 GNUNET_TIME_relative_get_forever (),
4984 &internal_stats_cont,
4985 &internal_stats_callback, core_context);
4986 if (core_context->stats_get_handle == NULL)
4987 internal_stats_cont (core_context, GNUNET_NO);
4992 struct DuplicateStats
4995 * Next item in the list
4997 struct DuplicateStats *next;
5000 * Nasty string, concatenation of relevant information.
5002 char *unique_string;
5006 * Check whether the combination of port/host/unix domain socket
5007 * already exists in the list of peers being checked for statistics.
5009 * @param pg the peergroup in question
5010 * @param specific_peer the peer we're concerned with
5011 * @param stats_list the list to return to the caller
5013 * @return GNUNET_YES if the statistics instance has been seen already,
5014 * GNUNET_NO if not (and we may have added it to the list)
5017 stats_check_existing(struct GNUNET_TESTING_PeerGroup *pg,
5018 struct PeerData *specific_peer,
5019 struct DuplicateStats **stats_list)
5021 struct DuplicateStats *pos;
5022 char *unix_domain_socket;
5023 unsigned long long port;
5026 != GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
5027 "single_statistics_per_host"))
5028 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
5031 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg,
5034 &unix_domain_socket))
5037 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg,
5038 "statistics", "port",
5041 GNUNET_free(unix_domain_socket);
5045 if (specific_peer->daemon->hostname != NULL)
5046 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
5047 unix_domain_socket, port);
5049 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
5053 if (0 == strcmp (to_match, pos->unique_string))
5055 GNUNET_free (unix_domain_socket);
5056 GNUNET_free (to_match);
5061 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
5062 pos->unique_string = to_match;
5063 pos->next = *stats_list;
5065 GNUNET_free (unix_domain_socket);
5070 * Iterate over all (running) peers in the peer group, retrieve
5071 * all statistics from each.
5074 GNUNET_TESTING_get_statistics(struct GNUNET_TESTING_PeerGroup *pg,
5075 GNUNET_STATISTICS_Callback cont,
5076 GNUNET_TESTING_STATISTICS_Iterator proc,
5079 struct StatsIterateContext *stats_context;
5080 struct StatsCoreContext *core_ctx;
5082 unsigned int total_count;
5083 struct DuplicateStats *stats_list;
5084 struct DuplicateStats *pos;
5087 /* Allocate a single stats iteration context */
5088 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
5089 stats_context->cont = cont;
5090 stats_context->proc = proc;
5091 stats_context->cls = cls;
5092 stats_context->pg = pg;
5095 for (i = 0; i < pg->total; i++)
5097 if ((pg->peers[i].daemon->running == GNUNET_YES) && (GNUNET_NO
5098 == stats_check_existing (pg, &pg->peers[i], &stats_list)))
5100 /* Allocate one core context per core we need to connect to */
5101 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
5102 core_ctx->daemon = pg->peers[i].daemon;
5103 /* Set back pointer to topology iteration context */
5104 core_ctx->iter_context = stats_context;
5105 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
5110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5111 "Retrieving stats from %u total instances.\n", total_count);
5112 stats_context->total = total_count;
5113 if (stats_list != NULL)
5118 GNUNET_free (pos->unique_string);
5119 stats_list = pos->next;
5121 pos = stats_list->next;
5128 * Stop the connection process temporarily.
5130 * @param pg the peer group to stop connecting
5133 GNUNET_TESTING_stop_connections(struct GNUNET_TESTING_PeerGroup *pg)
5135 pg->stop_connects = GNUNET_YES;
5139 * Resume the connection process temporarily.
5141 * @param pg the peer group to resume connecting
5144 GNUNET_TESTING_resume_connections(struct GNUNET_TESTING_PeerGroup *pg)
5146 pg->stop_connects = GNUNET_NO;
5150 * There are many ways to connect peers that are supported by this function.
5151 * To connect peers in the same topology that was created via the
5152 * GNUNET_TESTING_create_topology, the topology variable must be set to
5153 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
5154 * a new instance of that topology will be generated and attempted to be
5155 * connected. This could result in some connections being impossible,
5156 * because some topologies are non-deterministic.
5158 * @param pg the peer group struct representing the running peers
5159 * @param topology which topology to connect the peers in
5160 * @param options options for connecting the topology
5161 * @param option_modifier modifier for options that take a parameter
5162 * @param connect_timeout how long to wait before giving up on connecting
5164 * @param connect_attempts how many times to attempt to connect two peers
5165 * over the connect_timeout duration
5166 * @param notify_callback notification to be called once all connections completed
5167 * @param notify_cls closure for notification callback
5169 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
5172 GNUNET_TESTING_connect_topology(
5173 struct GNUNET_TESTING_PeerGroup *pg,
5174 enum GNUNET_TESTING_Topology topology,
5175 enum GNUNET_TESTING_TopologyOption options,
5176 double option_modifier,
5177 struct GNUNET_TIME_Relative connect_timeout,
5178 unsigned int connect_attempts,
5179 GNUNET_TESTING_NotifyCompletion notify_callback,
5184 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
5185 #if VERBOSE_TOPOLOGY
5186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5187 _("Creating clique CONNECT topology\n"));
5189 create_clique (pg, &add_connections, CONNECT, GNUNET_NO);
5191 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
5192 #if VERBOSE_TOPOLOGY
5193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5194 _("Creating small world (ring) CONNECT topology\n"));
5196 create_small_world_ring (pg, &add_connections, CONNECT);
5198 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
5199 #if VERBOSE_TOPOLOGY
5200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5201 _("Creating small world (2d-torus) CONNECT topology\n"));
5203 create_small_world (pg, &add_connections, CONNECT);
5205 case GNUNET_TESTING_TOPOLOGY_RING:
5206 #if VERBOSE_TOPOLOGY
5207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring CONNECT topology\n"));
5209 create_ring (pg, &add_connections, CONNECT);
5211 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
5212 #if VERBOSE_TOPOLOGY
5213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5214 _("Creating 2d torus CONNECT topology\n"));
5216 create_2d_torus (pg, &add_connections, CONNECT);
5218 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
5219 #if VERBOSE_TOPOLOGY
5220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5221 _("Creating Erdos-Renyi CONNECT topology\n"));
5223 create_erdos_renyi (pg, &add_connections, CONNECT);
5225 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
5226 #if VERBOSE_TOPOLOGY
5227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5228 _("Creating InterNAT CONNECT topology\n"));
5230 create_nated_internet (pg, &add_connections, CONNECT);
5232 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
5233 #if VERBOSE_TOPOLOGY
5234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5235 _("Creating Scale Free CONNECT topology\n"));
5237 create_scale_free (pg, &add_connections, CONNECT);
5239 case GNUNET_TESTING_TOPOLOGY_LINE:
5240 #if VERBOSE_TOPOLOGY
5241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5242 _("Creating straight line CONNECT topology\n"));
5244 create_line (pg, &add_connections, CONNECT);
5246 case GNUNET_TESTING_TOPOLOGY_NONE:
5247 #if VERBOSE_TOPOLOGY
5248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating no CONNECT topology\n"));
5250 copy_allowed_topology (pg);
5253 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5254 ("Unknown topology specification, can't connect peers!\n"));
5255 return GNUNET_SYSERR;
5260 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
5261 #if VERBOSE_TOPOLOGY
5262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5263 ("Connecting random subset (%'.2f percent) of possible peers\n"), 100
5266 choose_random_connections (pg, option_modifier);
5268 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
5269 #if VERBOSE_TOPOLOGY
5270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5271 _("Connecting a minimum of %u peers each (if possible)\n"),
5272 (unsigned int) option_modifier);
5274 choose_minimum (pg, (unsigned int) option_modifier);
5276 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
5277 #if VERBOSE_TOPOLOGY
5278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5279 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
5280 (unsigned int) option_modifier);
5283 perform_dfs (pg, (int) option_modifier);
5286 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
5287 #if VERBOSE_TOPOLOGY
5288 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5289 ("Finding additional %u closest peers each (if possible)\n"),
5290 (unsigned int) option_modifier);
5293 add_closest (pg, (unsigned int) option_modifier,
5294 &add_connections, CONNECT);
5297 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
5299 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
5305 return connect_topology (pg, connect_timeout, connect_attempts,
5306 notify_callback, notify_cls);
5310 * Lookup and return the number of SSH connections to a host.
5312 * @param hostname the hostname to lookup in the list
5313 * @param pg the peergroup that the host belongs to
5315 * @return the number of current ssh connections to the host
5318 count_outstanding_at_host(const char *hostname,
5319 struct GNUNET_TESTING_PeerGroup *pg)
5321 struct OutstandingSSH *pos;
5323 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5325 GNUNET_assert(pos != NULL);
5326 return pos->outstanding;
5330 * Increment the number of SSH connections to a host by one.
5332 * @param hostname the hostname to lookup in the list
5333 * @param pg the peergroup that the host belongs to
5337 increment_outstanding_at_host(const char *hostname,
5338 struct GNUNET_TESTING_PeerGroup *pg)
5340 struct OutstandingSSH *pos;
5342 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5344 GNUNET_assert(pos != NULL);
5349 * Decrement the number of SSH connections to a host by one.
5351 * @param hostname the hostname to lookup in the list
5352 * @param pg the peergroup that the host belongs to
5356 decrement_outstanding_at_host(const char *hostname,
5357 struct GNUNET_TESTING_PeerGroup *pg)
5359 struct OutstandingSSH *pos;
5361 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5363 GNUNET_assert(pos != NULL);
5368 * Callback that is called whenever a hostkey is generated
5369 * for a peer. Call the real callback and decrement the
5370 * starting counter for the peergroup.
5372 * @param cls closure
5373 * @param id identifier for the daemon, NULL on error
5374 * @param d handle for the daemon
5375 * @param emsg error message (NULL on success)
5378 internal_hostkey_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5379 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5381 struct InternalStartContext *internal_context = cls;
5382 internal_context->peer->pg->starting--;
5383 internal_context->peer->pg->started++;
5384 if (internal_context->hostname != NULL)
5385 decrement_outstanding_at_host (internal_context->hostname,
5386 internal_context->peer->pg);
5387 if (internal_context->hostkey_callback != NULL)
5388 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
5390 else if (internal_context->peer->pg->started
5391 == internal_context->peer->pg->total)
5393 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
5394 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
5399 * Callback that is called whenever a peer has finished starting.
5400 * Call the real callback and decrement the starting counter
5401 * for the peergroup.
5403 * @param cls closure
5404 * @param id identifier for the daemon, NULL on error
5406 * @param d handle for the daemon
5407 * @param emsg error message (NULL on success)
5410 internal_startup_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5411 const struct GNUNET_CONFIGURATION_Handle *cfg,
5412 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5414 struct InternalStartContext *internal_context = cls;
5415 internal_context->peer->pg->starting--;
5416 if (internal_context->hostname != NULL)
5417 decrement_outstanding_at_host (internal_context->hostname,
5418 internal_context->peer->pg);
5419 if (internal_context->start_cb != NULL)
5420 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
5425 internal_continue_startup(void *cls,
5426 const struct GNUNET_SCHEDULER_TaskContext *tc)
5428 struct InternalStartContext *internal_context = cls;
5430 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
5435 if ((internal_context->peer->pg->starting
5436 < internal_context->peer->pg->max_concurrent_ssh)
5437 || ((internal_context->hostname != NULL)
5438 && (count_outstanding_at_host (internal_context->hostname,
5439 internal_context->peer->pg)
5440 < internal_context->peer->pg->max_concurrent_ssh)))
5442 if (internal_context->hostname != NULL)
5443 increment_outstanding_at_host (internal_context->hostname,
5444 internal_context->peer->pg);
5445 internal_context->peer->pg->starting++;
5446 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
5450 GNUNET_SCHEDULER_add_delayed (
5451 GNUNET_TIME_relative_multiply (
5452 GNUNET_TIME_UNIT_MILLISECONDS,
5454 &internal_continue_startup,
5460 * Callback for informing us about a successful
5461 * or unsuccessful churn start call.
5463 * @param cls a ChurnContext
5464 * @param id the peer identity of the started peer
5465 * @param cfg the handle to the configuration of the peer
5466 * @param d handle to the daemon for the peer
5467 * @param emsg NULL on success, non-NULL on failure
5471 churn_start_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5472 const struct GNUNET_CONFIGURATION_Handle *cfg,
5473 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5475 struct ChurnRestartContext *startup_ctx = cls;
5476 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
5478 unsigned int total_left;
5479 char *error_message;
5481 error_message = NULL;
5484 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5485 "Churn stop callback failed with error `%s'\n", emsg);
5486 churn_ctx->num_failed_start++;
5490 churn_ctx->num_to_start--;
5494 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5495 "Started peer, %d left.\n", churn_ctx->num_to_start);
5498 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
5499 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5501 if (total_left == 0)
5503 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
5506 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5507 churn_ctx->num_failed_start,
5508 churn_ctx->num_failed_stop);
5509 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5510 GNUNET_free_non_null (error_message);
5511 GNUNET_free (churn_ctx);
5512 GNUNET_free (startup_ctx);
5517 schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5519 struct PeerRestartContext *peer_restart_ctx = cls;
5520 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
5522 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5523 GNUNET_SCHEDULER_add_delayed (
5524 GNUNET_TIME_relative_multiply (
5525 GNUNET_TIME_UNIT_MILLISECONDS,
5527 &schedule_churn_restart, peer_restart_ctx);
5530 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
5531 startup_ctx->timeout,
5532 &churn_start_callback, startup_ctx);
5533 GNUNET_free (peer_restart_ctx);
5539 internal_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5541 struct InternalStartContext *internal_context = cls;
5543 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
5548 if ((internal_context->peer->pg->starting
5549 < internal_context->peer->pg->max_concurrent_ssh)
5550 || ((internal_context->hostname != NULL)
5551 && (count_outstanding_at_host (internal_context->hostname,
5552 internal_context->peer->pg)
5553 < internal_context->peer->pg->max_concurrent_ssh)))
5555 if (internal_context->hostname != NULL)
5556 increment_outstanding_at_host (internal_context->hostname,
5557 internal_context->peer->pg);
5558 internal_context->peer->pg->starting++;
5559 internal_context->peer->daemon
5560 = GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
5561 internal_context->timeout,
5563 internal_context->hostname,
5564 internal_context->username,
5565 internal_context->sshport,
5566 internal_context->hostkey,
5567 &internal_hostkey_callback,
5569 &internal_startup_callback,
5574 GNUNET_SCHEDULER_add_delayed (
5575 GNUNET_TIME_relative_multiply (
5576 GNUNET_TIME_UNIT_MILLISECONDS,
5578 &internal_start, internal_context);
5581 #if USE_START_HELPER
5583 struct PeerStartHelperContext
5585 struct GNUNET_TESTING_PeerGroup *pg;
5587 struct HostData *host;
5589 struct GNUNET_OS_Process *proc;
5593 check_peers_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5595 struct PeerStartHelperContext *helper = cls;
5596 enum GNUNET_OS_ProcessStatusType type;
5599 GNUNET_TESTING_NotifyDaemonRunning cb;
5601 if (GNUNET_NO == GNUNET_OS_process_status (helper->proc, &type, &code)) /* Still running, wait some more! */
5603 GNUNET_SCHEDULER_add_delayed(GNUNET_CONSTANTS_EXEC_WAIT, &check_peers_started, helper);
5607 helper->pg->starting--;
5608 if (helper->pg->starting == 0) /* All peers have finished starting! */
5610 /* Call the peer started callback for each peer, set proper FSM state (?) */
5611 for (i = 0; i < helper->pg->total; i++)
5613 cb = helper->pg->peers[i].daemon->cb;
5614 helper->pg->peers[i].daemon->cb = NULL;
5615 helper->pg->peers[i].daemon->running = GNUNET_YES;
5616 helper->pg->peers[i].daemon->phase = SP_START_DONE;
5619 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
5620 cb (helper->pg->peers[i].daemon->cb_cls,
5621 &helper->pg->peers[i].daemon->id,
5622 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5623 "Failed to execute peerStartHelper.pl, or return code bad!");
5625 cb (helper->pg->peers[i].daemon->cb_cls,
5626 &helper->pg->peers[i].daemon->id,
5627 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5634 GNUNET_OS_process_close(helper->proc);
5638 start_peer_helper (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5640 struct PeerStartHelperContext *helper = cls;
5641 char *baseservicehome;
5644 /* ssh user@host peerStartHelper /path/to/basedirectory */
5645 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (helper->pg->cfg, "PATHS", "SERVICEHOME",
5647 GNUNET_asprintf(&tempdir, "%s/%s/", baseservicehome, helper->host->hostname);
5648 if (NULL != helper->host->username)
5649 GNUNET_asprintf (&arg, "%s@%s", helper->host->username, helper->host->hostname);
5651 GNUNET_asprintf (&arg, "%s", helper->host->hostname);
5653 /* FIXME: Doesn't support ssh_port option! */
5654 helper->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5655 "peerStartHelper.pl", tempdir, NULL);
5656 GNUNET_assert(helper->proc != NULL);
5657 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "starting peers with cmd ssh %s %s %s\n", arg, "peerStartHelper.pl", tempdir);
5658 GNUNET_SCHEDULER_add_now (&check_peers_started, helper);
5659 GNUNET_free (tempdir);
5660 GNUNET_free (baseservicehome);
5666 * Function which continues a peer group starting up
5667 * after successfully generating hostkeys for each peer.
5669 * @param pg the peer group to continue starting
5673 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
5677 #if USE_START_HELPER
5678 if ((pg->num_hosts > 0) && (pg->hostkey_data != NULL))
5680 struct PeerStartHelperContext *helper;
5681 pg->starting = pg->num_hosts;
5682 for (i = 0; i < pg->num_hosts; i++)
5684 helper = GNUNET_malloc(sizeof(struct PeerStartHelperContext));
5686 helper->host = &pg->hosts[i];
5687 GNUNET_SCHEDULER_add_now(&start_peer_helper, helper);
5693 for (i = 0; i < pg->total; i++)
5695 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5696 &pg->peers[i].internal_context);
5701 for (i = 0; i < pg->total; i++)
5703 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5704 &pg->peers[i].internal_context);
5709 #if USE_START_HELPER
5711 call_hostkey_callbacks (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5713 struct GNUNET_TESTING_PeerGroup *pg = cls;
5715 for (i = 0; i < pg->total; i++)
5717 if (pg->peers[i].internal_context.hostkey_callback != NULL)
5718 pg->peers[i].internal_context.hostkey_callback (pg->peers[i].internal_context.hostkey_cls,
5719 &pg->peers[i].daemon->id,
5720 pg->peers[i].daemon,
5724 if (pg->peers[0].internal_context.hostkey_callback == NULL)
5725 GNUNET_TESTING_daemons_continue_startup (pg);
5730 * Start count gnunet instances with the same set of transports and
5731 * applications. The port numbers (any option called "PORT") will be
5732 * adjusted to ensure that no two peers running on the same system
5733 * have the same port(s) in their respective configurations.
5735 * @param cfg configuration template to use
5736 * @param total number of daemons to start
5737 * @param max_concurrent_connections for testing, how many peers can
5738 * we connect to simultaneously
5739 * @param max_concurrent_ssh when starting with ssh, how many ssh
5740 * connections will we allow at once (based on remote hosts allowed!)
5741 * @param timeout total time allowed for peers to start
5742 * @param hostkey_callback function to call on each peers hostkey generation
5743 * if NULL, peers will be started by this call, if non-null,
5744 * GNUNET_TESTING_daemons_continue_startup must be called after
5745 * successful hostkey generation
5746 * @param hostkey_cls closure for hostkey callback
5747 * @param cb function to call on each daemon that was started
5748 * @param cb_cls closure for cb
5749 * @param connect_callback function to call each time two hosts are connected
5750 * @param connect_callback_cls closure for connect_callback
5751 * @param hostnames linked list of host structs to use to start peers on
5752 * (NULL to run on localhost only)
5754 * @return NULL on error, otherwise handle to control peer group
5756 struct GNUNET_TESTING_PeerGroup *
5757 GNUNET_TESTING_daemons_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
5759 unsigned int max_concurrent_connections,
5760 unsigned int max_concurrent_ssh,
5761 struct GNUNET_TIME_Relative timeout,
5762 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
5764 GNUNET_TESTING_NotifyDaemonRunning cb,
5766 GNUNET_TESTING_NotifyConnection connect_callback,
5767 void *connect_callback_cls,
5768 const struct GNUNET_TESTING_Host *hostnames)
5770 struct GNUNET_TESTING_PeerGroup *pg;
5771 const struct GNUNET_TESTING_Host *hostpos;
5772 const char *hostname;
5773 const char *username;
5774 char *baseservicehome;
5775 char *newservicehome;
5777 char *hostkeys_file;
5780 struct GNUNET_DISK_FileHandle *fd;
5781 struct GNUNET_CONFIGURATION_Handle *pcfg;
5783 struct OutstandingSSH *ssh_entry;
5784 unsigned int hostcnt;
5791 uint64_t total_hostkeys;
5792 struct GNUNET_OS_Process *proc;
5803 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
5805 pg->notify_connection = connect_callback;
5806 pg->notify_connection_cls = connect_callback_cls;
5808 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
5809 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
5810 pg->max_outstanding_connections = max_concurrent_connections;
5811 pg->max_concurrent_ssh = max_concurrent_ssh;
5812 if (NULL != hostnames)
5815 hostpos = hostnames;
5816 while (hostpos != NULL)
5818 hostpos = hostpos->next;
5821 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5824 hostpos = hostnames;
5825 while (hostpos != NULL)
5827 pg->hosts[off].minport = LOW_PORT;
5828 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
5829 if (hostpos->username != NULL)
5830 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
5831 pg->hosts[off].sshport = hostpos->port;
5832 hostpos = hostpos->next;
5842 pg->num_hosts = off;
5850 /* Create the servicehome directory for each remote peer */
5851 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
5853 for (i = 0; i < pg->num_hosts; i++)
5855 ssh_entry = GNUNET_malloc(sizeof(struct OutstandingSSH));
5856 ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
5857 GNUNET_CONTAINER_DLL_insert(pg->ssh_head, pg->ssh_tail, ssh_entry);
5858 GNUNET_asprintf(&tmpdir, "%s/%s", baseservicehome, pg->hosts[i].hostname);
5859 if (NULL != pg->hosts[i].username)
5860 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username,
5861 pg->hosts[i].hostname);
5863 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
5864 if (pg->hosts[i].sshport != 0)
5866 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
5867 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-P",
5872 arg, "mkdir -p", tmpdir,
5876 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5877 "mkdir -p", tmpdir, NULL);
5878 GNUNET_assert(proc != NULL);
5879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5880 "Creating remote dir with command ssh %s %s %s\n", arg,
5881 " mkdir -p ", tmpdir);
5882 GNUNET_free(tmpdir);
5884 GNUNET_OS_process_wait (proc);
5885 GNUNET_OS_process_close(proc);
5887 GNUNET_free(baseservicehome);
5889 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING",
5893 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
5894 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!\n");
5897 /* Check hostkey file size, read entire thing into memory */
5898 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
5899 GNUNET_DISK_PERM_NONE);
5902 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
5906 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs,
5911 GNUNET_ERROR_TYPE_DEBUG,
5912 "Found file size %llu for hostkeys, expect hostkeys to be size %d\n",
5913 fs, HOSTKEYFILESIZE);
5915 if (fs % HOSTKEYFILESIZE != 0)
5917 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5918 "File size %llu seems incorrect for hostkeys...\n",
5923 total_hostkeys = fs / HOSTKEYFILESIZE;
5924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5925 "Will read %llu hostkeys from file\n", total_hostkeys);
5926 pg->hostkey_data = GNUNET_malloc_large (fs);
5927 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
5928 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
5931 GNUNET_free(hostkeys_file);
5934 for (off = 0; off < total; off++)
5938 hostname = pg->hosts[off % hostcnt].hostname;
5939 username = pg->hosts[off % hostcnt].username;
5940 sshport = pg->hosts[off % hostcnt].sshport;
5941 pcfg = make_config (cfg, off, &pg->hosts[off % hostcnt].minport,
5942 &upnum, hostname, &fdnum);
5949 pcfg = make_config (cfg, off, &minport, &upnum, hostname, &fdnum);
5954 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5955 ("Could not create configuration for peer number %u on `%s'!\n"),
5956 off, hostname == NULL ? "localhost" : hostname);
5961 == GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS",
5965 if (hostname != NULL)
5966 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", baseservicehome, hostname, off);
5968 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
5969 #if !USE_START_HELPER
5970 GNUNET_free (baseservicehome);
5975 tmpdir = getenv ("TMPDIR");
5976 tmpdir = tmpdir ? tmpdir : "/tmp";
5977 if (hostname != NULL)
5978 GNUNET_asprintf (&newservicehome, "%s/%s/%s/%d/", tmpdir, hostname,
5979 "gnunet-testing-test-test", off);
5981 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", tmpdir,
5982 "gnunet-testing-test-test", off);
5984 GNUNET_CONFIGURATION_set_value_string (pcfg, "PATHS", "SERVICEHOME",
5986 GNUNET_free (newservicehome);
5987 pg->peers[off].cfg = pcfg;
5988 pg->peers[off].pg = pg;
5989 pg->peers[off].internal_context.peer = &pg->peers[off];
5990 pg->peers[off].internal_context.timeout = timeout;
5991 pg->peers[off].internal_context.hostname = hostname;
5992 pg->peers[off].internal_context.username = username;
5993 pg->peers[off].internal_context.sshport = sshport;
5994 if (pg->hostkey_data != NULL)
5995 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off
5997 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
5998 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
5999 pg->peers[off].internal_context.start_cb = cb;
6000 pg->peers[off].internal_context.start_cb_cls = cb_cls;
6001 #if !USE_START_HELPER
6002 GNUNET_SCHEDULER_add_now (&internal_start,
6003 &pg->peers[off].internal_context);
6005 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6007 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (pcfg,
6013 pg->peers[off].internal_context.hostkey,
6014 &internal_hostkey_callback,
6015 &pg->peers[off].internal_context,
6016 &internal_startup_callback,
6017 &pg->peers[off].internal_context);
6019 * At this point, given that we had a hostkeyfile,
6020 * we can call the hostkey callback!
6021 * But first, we should copy (rsync) all of the configs
6022 * and hostkeys to the remote peers. Then let topology
6023 * creation happen, then call the peer start helper processes,
6024 * then set pg->whatever_phase for each peer and let them
6025 * enter the fsm to get the HELLO's for peers and start connecting.
6030 GNUNET_SCHEDULER_add_now (&internal_start,
6031 &pg->peers[off].internal_context);
6037 #if USE_START_HELPER /* Now the peergroup has been set up, hostkeys and configs written to files. */
6038 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6040 for (off = 0; off < hostcnt; off++)
6042 GNUNET_asprintf(&newservicehome, "%s/%s/", baseservicehome, pg->hosts[off].hostname);
6044 if (NULL != username)
6045 GNUNET_asprintf (&arg, "%s@%s:%s/%s", username, pg->hosts[off].hostname, baseservicehome, pg->hosts[off].hostname);
6047 GNUNET_asprintf (&arg, "%s:%s/%s", pg->hosts[off].hostname, baseservicehome, pg->hosts[off].hostname);
6049 /* FIXME: Doesn't support ssh_port option! */
6050 proc = GNUNET_OS_start_process (NULL, NULL, "rsync", "rsync", "-r", newservicehome, arg, NULL);
6052 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "copying directory with command rsync -r %s %s\n", newservicehome, arg);
6058 GNUNET_ERROR_TYPE_ERROR,
6060 ("Could not start `%s' process to copy configuration directory.\n"),
6064 GNUNET_OS_process_wait (proc);
6065 GNUNET_OS_process_close (proc);
6067 /* Now all the configuration files and hostkeys are copied to the remote host. Call the hostkey callback for each peer! */
6068 GNUNET_SCHEDULER_add_now(&call_hostkey_callbacks, pg);
6070 GNUNET_free (baseservicehome);
6076 * Get a daemon by number, so callers don't have to do nasty
6077 * offsetting operation.
6079 struct GNUNET_TESTING_Daemon *
6080 GNUNET_TESTING_daemon_get(struct GNUNET_TESTING_PeerGroup *pg,
6081 unsigned int position)
6083 if (position < pg->total)
6084 return pg->peers[position].daemon;
6090 * Get a daemon by peer identity, so callers can
6091 * retrieve the daemon without knowing it's offset.
6093 * @param pg the peer group to retrieve the daemon from
6094 * @param peer_id the peer identity of the daemon to retrieve
6096 * @return the daemon on success, or NULL if no such peer identity is found
6098 struct GNUNET_TESTING_Daemon *
6099 GNUNET_TESTING_daemon_get_by_id(struct GNUNET_TESTING_PeerGroup *pg,
6100 struct GNUNET_PeerIdentity *peer_id)
6104 for (i = 0; i < pg->total; i++)
6106 if (0 == memcmp (&pg->peers[i].daemon->id, peer_id,
6107 sizeof(struct GNUNET_PeerIdentity)))
6108 return pg->peers[i].daemon;
6115 * Prototype of a function that will be called when a
6116 * particular operation was completed the testing library.
6118 * @param cls closure (a struct RestartContext)
6119 * @param id id of the peer that was restarted
6120 * @param cfg handle to the configuration of the peer
6121 * @param d handle to the daemon that was restarted
6122 * @param emsg NULL on success
6125 restart_callback(void *cls, const struct GNUNET_PeerIdentity *id,
6126 const struct GNUNET_CONFIGURATION_Handle *cfg,
6127 struct GNUNET_TESTING_Daemon *d, const char *emsg)
6129 struct RestartContext *restart_context = cls;
6133 restart_context->peers_restarted++;
6137 restart_context->peers_restart_failed++;
6140 if (restart_context->peers_restarted == restart_context->peer_group->total)
6142 restart_context->callback (restart_context->callback_cls, NULL);
6143 GNUNET_free (restart_context);
6145 else if (restart_context->peers_restart_failed
6146 + restart_context->peers_restarted == restart_context->peer_group->total)
6148 restart_context->callback (restart_context->callback_cls,
6149 "Failed to restart peers!");
6150 GNUNET_free (restart_context);
6156 * Callback for informing us about a successful
6157 * or unsuccessful churn stop call.
6159 * @param cls a ChurnContext
6160 * @param emsg NULL on success, non-NULL on failure
6164 churn_stop_callback(void *cls, const char *emsg)
6166 struct ShutdownContext *shutdown_ctx = cls;
6167 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
6168 unsigned int total_left;
6169 char *error_message;
6171 error_message = NULL;
6172 shutdown_ctx->outstanding--;
6176 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6177 "Churn stop callback failed with error `%s'\n", emsg);
6178 churn_ctx->num_failed_stop++;
6182 churn_ctx->num_to_stop--;
6186 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6187 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
6189 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
6190 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
6192 if (total_left == 0)
6194 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
6198 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
6199 churn_ctx->num_failed_start,
6200 churn_ctx->num_failed_stop);
6202 churn_ctx->cb (churn_ctx->cb_cls, error_message);
6203 GNUNET_free_non_null (error_message);
6204 GNUNET_free (churn_ctx);
6205 GNUNET_free (shutdown_ctx);
6210 * Count the number of running peers.
6212 * @param pg handle for the peer group
6214 * @return the number of currently running peers in the peer group
6217 GNUNET_TESTING_daemons_running(struct GNUNET_TESTING_PeerGroup *pg)
6220 unsigned int running = 0;
6221 for (i = 0; i < pg->total; i++)
6223 if (pg->peers[i].daemon->running == GNUNET_YES)
6225 GNUNET_assert (running != -1);
6233 * Task to rate limit the number of outstanding peer shutdown
6234 * requests. This is necessary for making sure we don't do
6235 * too many ssh connections at once, but is generally nicer
6236 * to any system as well (graduated task starts, as opposed
6237 * to calling gnunet-arm N times all at once).
6240 schedule_churn_shutdown_task(void *cls,
6241 const struct GNUNET_SCHEDULER_TaskContext *tc)
6243 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6244 struct ShutdownContext *shutdown_ctx;
6245 struct ChurnContext *churn_ctx;
6246 GNUNET_assert (peer_shutdown_ctx != NULL);
6247 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6248 GNUNET_assert (shutdown_ctx != NULL);
6249 churn_ctx = (struct ChurnContext *) shutdown_ctx->cb_cls;
6250 if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
6251 GNUNET_SCHEDULER_add_delayed (
6252 GNUNET_TIME_relative_multiply (
6253 GNUNET_TIME_UNIT_MILLISECONDS,
6255 &schedule_churn_shutdown_task,
6259 shutdown_ctx->outstanding++;
6260 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6261 shutdown_ctx->timeout, shutdown_ctx->cb,
6262 shutdown_ctx, GNUNET_NO, GNUNET_YES);
6263 GNUNET_free (peer_shutdown_ctx);
6268 * Simulate churn by stopping some peers (and possibly
6269 * re-starting others if churn is called multiple times). This
6270 * function can only be used to create leave-join churn (peers "never"
6271 * leave for good). First "voff" random peers that are currently
6272 * online will be taken offline; then "von" random peers that are then
6273 * offline will be put back online. No notifications will be
6274 * generated for any of these operations except for the callback upon
6277 * @param pg handle for the peer group
6278 * @param voff number of peers that should go offline
6279 * @param von number of peers that should come back online;
6280 * must be zero on first call (since "testbed_start"
6281 * always starts all of the peers)
6282 * @param timeout how long to wait for operations to finish before
6284 * @param cb function to call at the end
6285 * @param cb_cls closure for cb
6288 GNUNET_TESTING_daemons_churn(struct GNUNET_TESTING_PeerGroup *pg,
6289 unsigned int voff, unsigned int von,
6290 struct GNUNET_TIME_Relative timeout,
6291 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6293 struct ChurnContext *churn_ctx;
6294 struct ShutdownContext *shutdown_ctx;
6295 struct PeerShutdownContext *peer_shutdown_ctx;
6296 struct PeerRestartContext *peer_restart_ctx;
6297 struct ChurnRestartContext *churn_startup_ctx;
6299 unsigned int running;
6300 unsigned int stopped;
6301 unsigned int total_running;
6302 unsigned int total_stopped;
6304 unsigned int *running_arr;
6305 unsigned int *stopped_arr;
6306 unsigned int *running_permute;
6307 unsigned int *stopped_permute;
6309 shutdown_ctx = NULL;
6310 peer_shutdown_ctx = NULL;
6311 peer_restart_ctx = NULL;
6312 churn_startup_ctx = NULL;
6317 if ((von == 0) && (voff == 0)) /* No peers at all? */
6323 for (i = 0; i < pg->total; i++)
6325 if (pg->peers[i].daemon->running == GNUNET_YES)
6327 GNUNET_assert (running != -1);
6332 GNUNET_assert (stopped != -1);
6339 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6340 "Trying to stop more peers than are currently running!\n");
6341 cb (cb_cls, "Trying to stop more peers than are currently running!");
6347 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6348 "Trying to start more peers than are currently stopped!\n");
6349 cb (cb_cls, "Trying to start more peers than are currently stopped!");
6353 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6357 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
6361 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
6363 running_permute = NULL;
6364 stopped_permute = NULL;
6367 running_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6370 stopped_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6373 total_running = running;
6374 total_stopped = stopped;
6378 churn_ctx->num_to_start = von;
6379 churn_ctx->num_to_stop = voff;
6381 churn_ctx->cb_cls = cb_cls;
6384 for (i = 0; i < pg->total; i++)
6386 if (pg->peers[i].daemon->running == GNUNET_YES)
6388 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6389 running_arr[running] = i;
6394 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6395 stopped_arr[stopped] = i;
6400 GNUNET_assert (running >= voff);
6403 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6404 shutdown_ctx->cb = &churn_stop_callback;
6405 shutdown_ctx->cb_cls = churn_ctx;
6406 shutdown_ctx->total_peers = voff;
6407 shutdown_ctx->timeout = timeout;
6410 for (i = 0; i < voff; i++)
6413 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
6414 running_permute[i]);
6416 GNUNET_assert (running_arr != NULL);
6417 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6418 peer_shutdown_ctx->daemon
6419 = pg->peers[running_arr[running_permute[i]]].daemon;
6420 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6421 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
6425 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
6427 &churn_stop_callback, churn_ctx,
6428 GNUNET_NO, GNUNET_YES); */
6431 GNUNET_assert (stopped >= von);
6434 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6435 churn_startup_ctx->churn_ctx = churn_ctx;
6436 churn_startup_ctx->timeout = timeout;
6437 churn_startup_ctx->pg = pg;
6439 for (i = 0; i < von; i++)
6442 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
6443 stopped_permute[i]);
6445 GNUNET_assert (stopped_arr != NULL);
6446 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
6447 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
6448 peer_restart_ctx->daemon
6449 = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
6450 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
6452 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
6453 timeout, &churn_start_callback, churn_ctx); */
6456 GNUNET_free_non_null (running_arr);
6457 GNUNET_free_non_null (stopped_arr);
6458 GNUNET_free_non_null (running_permute);
6459 GNUNET_free_non_null (stopped_permute);
6463 * Restart all peers in the given group.
6465 * @param pg the handle to the peer group
6466 * @param callback function to call on completion (or failure)
6467 * @param callback_cls closure for the callback function
6470 GNUNET_TESTING_daemons_restart(struct GNUNET_TESTING_PeerGroup *pg,
6471 GNUNET_TESTING_NotifyCompletion callback,
6474 struct RestartContext *restart_context;
6479 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
6480 restart_context->peer_group = pg;
6481 restart_context->peers_restarted = 0;
6482 restart_context->callback = callback;
6483 restart_context->callback_cls = callback_cls;
6485 for (off = 0; off < pg->total; off++)
6487 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
6488 &restart_callback, restart_context);
6494 * Start or stop an individual peer from the given group.
6496 * @param pg handle to the peer group
6497 * @param offset which peer to start or stop
6498 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
6499 * @param timeout how long to wait for shutdown
6500 * @param cb function to call at the end
6501 * @param cb_cls closure for cb
6504 GNUNET_TESTING_daemons_vary(struct GNUNET_TESTING_PeerGroup *pg,
6505 unsigned int offset, int desired_status,
6506 struct GNUNET_TIME_Relative timeout,
6507 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6509 struct ShutdownContext *shutdown_ctx;
6510 struct ChurnRestartContext *startup_ctx;
6511 struct ChurnContext *churn_ctx;
6513 if (GNUNET_NO == desired_status)
6515 if (NULL != pg->peers[offset].daemon)
6517 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6518 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6519 churn_ctx->num_to_start = 0;
6520 churn_ctx->num_to_stop = 1;
6522 churn_ctx->cb_cls = cb_cls;
6523 shutdown_ctx->cb_cls = churn_ctx;
6524 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon, timeout,
6525 &churn_stop_callback, shutdown_ctx,
6526 GNUNET_NO, GNUNET_YES);
6529 else if (GNUNET_YES == desired_status)
6531 if (NULL == pg->peers[offset].daemon)
6533 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6534 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6535 churn_ctx->num_to_start = 1;
6536 churn_ctx->num_to_stop = 0;
6538 churn_ctx->cb_cls = cb_cls;
6539 startup_ctx->churn_ctx = churn_ctx;
6540 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
6541 timeout, &churn_start_callback,
6550 * Callback for shutting down peers in a peer group.
6552 * @param cls closure (struct ShutdownContext)
6553 * @param emsg NULL on success
6556 internal_shutdown_callback(void *cls, const char *emsg)
6558 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6559 struct ShutdownContext *shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6561 struct OutstandingSSH *ssh_pos;
6563 shutdown_ctx->outstanding--;
6564 if (peer_shutdown_ctx->daemon->hostname != NULL)
6565 decrement_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6570 shutdown_ctx->peers_down++;
6574 shutdown_ctx->peers_failed++;
6577 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down
6578 + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
6580 if (shutdown_ctx->peers_failed > 0)
6581 shutdown_ctx->cb (shutdown_ctx->cb_cls,
6582 "Not all peers successfully shut down!");
6584 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
6586 GNUNET_free (shutdown_ctx->pg->peers);
6587 GNUNET_free_non_null(shutdown_ctx->pg->hostkey_data);
6588 for (off = 0; off < shutdown_ctx->pg->num_hosts; off++)
6590 GNUNET_free (shutdown_ctx->pg->hosts[off].hostname);
6591 GNUNET_free_non_null (shutdown_ctx->pg->hosts[off].username);
6593 GNUNET_free_non_null (shutdown_ctx->pg->hosts);
6594 while (NULL != (ssh_pos = shutdown_ctx->pg->ssh_head))
6596 GNUNET_CONTAINER_DLL_remove(shutdown_ctx->pg->ssh_head, shutdown_ctx->pg->ssh_tail, ssh_pos);
6597 GNUNET_free(ssh_pos);
6599 GNUNET_free (shutdown_ctx->pg);
6600 GNUNET_free (shutdown_ctx);
6602 GNUNET_free(peer_shutdown_ctx);
6606 * Task to rate limit the number of outstanding peer shutdown
6607 * requests. This is necessary for making sure we don't do
6608 * too many ssh connections at once, but is generally nicer
6609 * to any system as well (graduated task starts, as opposed
6610 * to calling gnunet-arm N times all at once).
6613 schedule_shutdown_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6615 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6616 struct ShutdownContext *shutdown_ctx;
6618 GNUNET_assert (peer_shutdown_ctx != NULL);
6619 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6620 GNUNET_assert (shutdown_ctx != NULL);
6622 if ((shutdown_ctx->outstanding < shutdown_ctx->pg->max_concurrent_ssh)
6623 || ((peer_shutdown_ctx->daemon->hostname != NULL)
6624 && (count_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6626 < shutdown_ctx->pg->max_concurrent_ssh)))
6628 if (peer_shutdown_ctx->daemon->hostname != NULL)
6629 increment_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6631 shutdown_ctx->outstanding++;
6632 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6633 shutdown_ctx->timeout,
6634 &internal_shutdown_callback, peer_shutdown_ctx,
6635 GNUNET_YES, GNUNET_NO);
6638 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
6640 &schedule_shutdown_task, peer_shutdown_ctx);
6645 * Shutdown all peers started in the given group.
6647 * @param pg handle to the peer group
6648 * @param timeout how long to wait for shutdown
6649 * @param cb callback to notify upon success or failure
6650 * @param cb_cls closure for cb
6653 GNUNET_TESTING_daemons_stop(struct GNUNET_TESTING_PeerGroup *pg,
6654 struct GNUNET_TIME_Relative timeout,
6655 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6658 struct ShutdownContext *shutdown_ctx;
6659 struct PeerShutdownContext *peer_shutdown_ctx;
6661 struct PeerConnection *conn_iter;
6662 struct PeerConnection *temp_conn;
6665 GNUNET_assert (pg->total > 0);
6667 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6668 shutdown_ctx->cb = cb;
6669 shutdown_ctx->cb_cls = cb_cls;
6670 shutdown_ctx->total_peers = pg->total;
6671 shutdown_ctx->timeout = timeout;
6672 shutdown_ctx->pg = pg;
6673 /* shtudown_ctx->outstanding = 0; */
6675 for (off = 0; off < pg->total; off++)
6677 GNUNET_assert (NULL != pg->peers[off].daemon);
6678 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6679 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
6680 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6681 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
6683 if (NULL != pg->peers[off].cfg)
6685 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
6686 pg->peers[off].cfg = NULL;
6689 conn_iter = pg->peers[off].allowed_peers_head;
6690 while (conn_iter != NULL)
6692 temp_conn = conn_iter->next;
6693 GNUNET_free(conn_iter);
6694 conn_iter = temp_conn;
6697 conn_iter = pg->peers[off].connect_peers_head;
6698 while (conn_iter != NULL)
6700 temp_conn = conn_iter->next;
6701 GNUNET_free(conn_iter);
6702 conn_iter = temp_conn;
6705 conn_iter = pg->peers[off].blacklisted_peers_head;
6706 while (conn_iter != NULL)
6708 temp_conn = conn_iter->next;
6709 GNUNET_free(conn_iter);
6710 conn_iter = temp_conn;
6713 conn_iter = pg->peers[off].connect_peers_working_set_head;
6714 while (conn_iter != NULL)
6716 temp_conn = conn_iter->next;
6717 GNUNET_free(conn_iter);
6718 conn_iter = temp_conn;
6721 if (pg->peers[off].allowed_peers != NULL)
6722 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
6723 if (pg->peers[off].connect_peers != NULL)
6724 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
6725 if (pg->peers[off].blacklisted_peers != NULL)
6726 GNUNET_CONTAINER_multihashmap_destroy (pg->
6727 peers[off].blacklisted_peers);
6732 /* end of testing_group.c */