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_arm_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_core_service.h"
33 #define VERBOSE_TESTING GNUNET_NO
35 #define VERBOSE_TOPOLOGY GNUNET_NO
37 #define DEBUG_CHURN GNUNET_NO
41 #define USE_SEND_HELLOS GNUNET_NO
43 #define TOPOLOGY_HACK GNUNET_YES
45 #define AVOID_CONN_MALLOC GNUNET_NO
48 * Lowest port used for GNUnet testing. Should be high enough to not
49 * conflict with other applications running on the hosts but be low
50 * enough to not conflict with client-ports (typically starting around
53 #define LOW_PORT 12000
56 * Highest port used for GNUnet testing. Should be low enough to not
57 * conflict with the port range for "local" ports (client apps; see
58 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
60 #define HIGH_PORT 56000
62 /* Maximum time to delay connect attempt */
63 #define MAX_CONNECT_DELAY 300
66 * Which list of peers do we need to modify?
70 /** Modify allowed peers */
73 /** Modify connect peers */
76 /** Modify blacklist peers */
79 /** Modify workingset peers */
84 * Prototype of a function called whenever two peers would be connected
85 * in a certain topology.
88 (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup * pg,
89 unsigned int first, unsigned int second,
90 enum PeerLists list, unsigned int check);
93 * Context for handling churning a peer group
98 * The peergroup we are dealing with.
100 struct GNUNET_TESTING_PeerGroup *pg;
103 * Callback used to notify of churning finished
105 GNUNET_TESTING_NotifyCompletion cb;
108 * Closure for callback
113 * Number of peers that still need to be started
115 unsigned int num_to_start;
118 * Number of peers that still need to be stopped
120 unsigned int num_to_stop;
123 * Number of peers that failed to start
125 unsigned int num_failed_start;
128 * Number of peers that failed to stop
130 unsigned int num_failed_stop;
133 struct RestartContext
136 * The group of peers being restarted
138 struct GNUNET_TESTING_PeerGroup *peer_group;
141 * How many peers have been restarted thus far
143 unsigned int peers_restarted;
146 * How many peers got an error when restarting
148 unsigned int peers_restart_failed;
151 * The function to call once all peers have been restarted
153 GNUNET_TESTING_NotifyCompletion callback;
156 * Closure for callback function
162 struct SendHelloContext
165 * Global handle to the peer group.
167 struct GNUNET_TESTING_PeerGroup *pg;
170 * The data about this specific peer.
172 struct PeerData *peer;
175 * The next HELLO that needs sent to this peer.
177 struct PeerConnection *peer_pos;
180 * Are we connected to CORE yet?
182 unsigned int core_ready;
185 * How many attempts should we make for failed connections?
187 unsigned int connect_attempts;
190 * Task for scheduling core connect requests to be sent.
192 GNUNET_SCHEDULER_TaskIdentifier core_connect_task;
195 struct ShutdownContext
197 struct GNUNET_TESTING_PeerGroup *pg;
199 * Total peers to wait for
201 unsigned int total_peers;
204 * Number of peers successfully shut down
206 unsigned int peers_down;
209 * Number of peers failed to shut down
211 unsigned int peers_failed;
214 * Number of peers we have started shutting
215 * down. If too many, wait on them.
217 unsigned int outstanding;
220 * Timeout for shutdown.
222 struct GNUNET_TIME_Relative timeout;
225 * Callback to call when all peers either
226 * shutdown or failed to shutdown
228 GNUNET_TESTING_NotifyCompletion cb;
237 * Individual shutdown context for a particular peer.
239 struct PeerShutdownContext
242 * Pointer to the high level shutdown context.
244 struct ShutdownContext *shutdown_ctx;
247 * The daemon handle for the peer to shut down.
249 struct GNUNET_TESTING_Daemon *daemon;
253 * Individual shutdown context for a particular peer.
255 struct PeerRestartContext
258 * Pointer to the high level restart context.
260 struct ChurnRestartContext *churn_restart_ctx;
263 * The daemon handle for the peer to shut down.
265 struct GNUNET_TESTING_Daemon *daemon;
268 struct CreateTopologyContext
272 * Function to call with number of connections
274 GNUNET_TESTING_NotifyConnections cont;
277 * Closure for connection notification
284 /** Waiting to read number of peers */
287 /** Should find next peer index */
290 /** Should find colon */
293 /** Should read other peer index, space, or endline */
298 struct PeerConnection
303 struct PeerConnection *prev;
308 struct PeerConnection *next;
311 * Index of daemon in pg->peers
318 struct InternalStartContext
321 * Pointer to peerdata
323 struct PeerData *peer;
326 * Timeout for peer startup
328 struct GNUNET_TIME_Relative timeout;
331 * Client callback for hostkey notification
333 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
336 * Closure for hostkey_callback
341 * Client callback for peer start notification
343 GNUNET_TESTING_NotifyDaemonRunning start_cb;
351 * Hostname, where to start the peer
353 const char *hostname;
356 * Username to use when connecting to the
359 const char *username;
362 * Pointer to starting memory location of a hostkey
367 * Port to use for ssh.
373 struct ChurnRestartContext
376 * PeerGroup that we are working with.
378 struct GNUNET_TESTING_PeerGroup *pg;
381 * Number of restarts currently in flight.
383 unsigned int outstanding;
386 * Handle to the underlying churn context.
388 struct ChurnContext *churn_ctx;
391 * How long to allow the operation to take.
393 struct GNUNET_TIME_Relative timeout;
396 struct OutstandingSSH
398 struct OutstandingSSH *next;
400 struct OutstandingSSH *prev;
403 * Number of current ssh connections.
405 uint32_t outstanding;
408 * The hostname of this peer.
410 const char *hostname;
414 * Data we keep per peer.
419 * (Initial) configuration of the host.
420 * (initial because clients could change
421 * it and we would not know about those
424 struct GNUNET_CONFIGURATION_Handle *cfg;
427 * Handle for controlling the daemon.
429 struct GNUNET_TESTING_Daemon *daemon;
432 * The peergroup this peer belongs to.
434 struct GNUNET_TESTING_PeerGroup *pg;
438 * Linked list of allowed peer connections.
440 struct PeerConnection *allowed_peers_head;
443 * Linked list of allowed peer connections.
445 struct PeerConnection *allowed_peers_tail;
448 * Linked list of blacklisted peer connections.
450 struct PeerConnection *blacklisted_peers_head;
453 * Linked list of blacklisted peer connections.
455 struct PeerConnection *blacklisted_peers_tail;
458 * Linked list of connect peer connections.
460 struct PeerConnection *connect_peers_head;
463 * Linked list of connect peer connections.
465 struct PeerConnection *connect_peers_tail;
468 * Linked list of connect peer connections.
470 struct PeerConnection *connect_peers_working_set_head;
473 * Linked list of connect peer connections.
475 struct PeerConnection *connect_peers_working_set_tail;
479 * Hash map of allowed peer connections (F2F created topology)
481 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
484 * Hash map of blacklisted peers
486 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
489 * Hash map of peer connections
491 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
494 * Temporary hash map of peer connections
496 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
500 * Temporary variable for topology creation, should be reset before
501 * creating any topology so the count is valid once finished.
506 * Context to keep track of peers being started, to
507 * stagger hostkey generation and peer startup.
509 struct InternalStartContext internal_context;
513 * Linked list of per-host data.
523 * SSH username to use when connecting to this host.
528 * SSH port to use when connecting to this host.
533 * Lowest port that we have not yet used
539 struct TopologyIterateContext
542 * The peergroup we are working with.
544 struct GNUNET_TESTING_PeerGroup *pg;
547 * Callback for notifying of two connected peers.
549 GNUNET_TESTING_NotifyTopology topology_cb;
552 * Closure for topology_cb
557 * Number of peers currently connected to.
559 unsigned int connected;
562 * Number of peers we have finished iterating.
564 unsigned int completed;
567 * Number of peers total.
572 struct StatsIterateContext
575 * The peergroup that we are dealing with.
577 struct GNUNET_TESTING_PeerGroup *pg;
580 * Continuation to call once all stats information has been retrieved.
582 GNUNET_STATISTICS_Callback cont;
585 * Proc function to call on each value received.
587 GNUNET_TESTING_STATISTICS_Iterator proc;
590 * Closure for topology_cb
595 * Number of peers currently connected to.
597 unsigned int connected;
600 * Number of peers we have finished iterating.
602 unsigned int completed;
605 * Number of peers total.
613 struct GNUNET_TESTING_Daemon *daemon;
616 struct StatsCoreContext
619 struct GNUNET_TESTING_Daemon *daemon;
621 * Handle to the statistics service.
623 struct GNUNET_STATISTICS_Handle *stats_handle;
626 * Handle for getting statistics.
628 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
631 struct ConnectTopologyContext
634 * How many connections are left to create.
636 unsigned int remaining_connections;
639 * Handle to group of peers.
641 struct GNUNET_TESTING_PeerGroup *pg;
644 * How long to try this connection before timing out.
646 struct GNUNET_TIME_Relative connect_timeout;
649 * How many times to retry connecting the two peers.
651 unsigned int connect_attempts;
654 * Temp value set for each iteration.
656 //struct PeerData *first;
659 * Notification that all peers are connected.
661 GNUNET_TESTING_NotifyCompletion notify_connections_done;
664 * Closure for notify.
670 * Handle to a group of GNUnet peers.
672 struct GNUNET_TESTING_PeerGroup
675 * Configuration template.
677 const struct GNUNET_CONFIGURATION_Handle *cfg;
680 * Function to call on each started daemon.
682 //GNUNET_TESTING_NotifyDaemonRunning cb;
690 * Function to call on each topology connection created
692 GNUNET_TESTING_NotifyConnection notify_connection;
695 * Callback for notify_connection
697 void *notify_connection_cls;
700 * Array of information about hosts.
702 struct HostData *hosts;
705 * Number of hosts (size of HostData)
707 unsigned int num_hosts;
710 * Array of "total" peers.
712 struct PeerData *peers;
715 * Number of peers in this group.
720 * At what time should we fail the peer startup process?
722 struct GNUNET_TIME_Absolute max_timeout;
725 * How many peers are being started right now?
727 unsigned int starting;
730 * How many peers have already been started?
732 unsigned int started;
735 * Number of possible connections to peers
738 unsigned int max_outstanding_connections;
741 * Number of ssh connections to peers (max).
743 unsigned int max_concurrent_ssh;
746 * Number of connects we are waiting on, allows us to rate limit
749 unsigned int outstanding_connects;
752 * Number of HELLOs we have yet to send.
754 unsigned int remaining_hellos;
757 * How many connects have already been scheduled?
759 unsigned int total_connects_scheduled;
762 * Hostkeys loaded from a file.
767 * Head of DLL to keep track of the number of outstanding
768 * ssh connections per peer.
770 struct OutstandingSSH *ssh_head;
773 * Tail of DLL to keep track of the number of outstanding
774 * ssh connections per peer.
776 struct OutstandingSSH *ssh_tail;
779 * Stop scheduling peers connecting.
781 unsigned int stop_connects;
784 * Connection context for peer group.
786 struct ConnectTopologyContext ct_ctx;
788 #if AVOID_CONN_MALLOC
789 struct PeerConnection working_peer_connections[200000];
791 unsigned int current_peer_connection;
798 * The altered configuration.
800 struct GNUNET_CONFIGURATION_Handle *ret;
803 * The original configuration to alter.
805 const struct GNUNET_CONFIGURATION_Handle *orig;
808 * The hostname that this peer will run on.
810 const char *hostname;
813 * The next possible port to assign.
818 * Unique number for unix domain sockets.
823 * Unique number for this peer/host to offset
824 * things that are grouped by host.
829 struct ConnectContext
832 * Index of peer to connect second to.
834 uint32_t first_index;
837 * Index of peer to connect first to.
839 uint32_t second_index;
842 * Higher level topology connection context.
844 struct ConnectTopologyContext *ct_ctx;
847 * Whether this connection has been accounted for in the schedule_connect call.
852 struct UnblacklistContext
857 struct GNUNET_TESTING_PeerGroup *pg;
860 * uid of the first peer
870 struct GNUNET_TESTING_PeerGroup *pg;
873 * uid of the first peer
878 * Peer data for first peer.
880 struct PeerData *first;
883 * Random percentage to use
888 struct MinimumContext
893 struct GNUNET_TESTING_PeerGroup *pg;
896 * uid of the first peer
901 * Peer data for first peer.
903 struct PeerData *first;
906 * Number of conns per peer
908 unsigned int num_to_add;
911 * Permuted array of all possible connections. Only add the Nth
912 * peer if it's in the Nth position.
914 unsigned int *pg_array;
917 * What number is the current element we are iterating over?
919 unsigned int current;
927 struct GNUNET_TESTING_PeerGroup *pg;
930 * uid of the first peer
935 * uid of the second peer
940 * Peer data for first peer.
942 struct PeerData *first;
945 * Which peer has been chosen as the one to add?
950 * What number is the current element we are iterating over?
952 unsigned int current;
956 * Simple struct to keep track of progress, and print a
957 * nice little percentage meter for long running tasks.
967 unsigned int completed;
971 char *startup_string;
976 * Convert unique ID to hash code.
978 * @param uid unique ID to convert
979 * @param hash set to uid (extended with zeros)
982 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
984 memset (hash, 0, sizeof (GNUNET_HashCode));
985 *((uint32_t *) hash) = uid;
989 * Convert hash code to unique ID.
991 * @param uid unique ID to convert
992 * @param hash set to uid (extended with zeros)
995 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
997 memcpy (uid, hash, sizeof (uint32_t));
1002 static struct GNUNET_CORE_MessageHandler no_handlers[] =
1008 * Create a meter to keep track of the progress of some task.
1010 * @param total the total number of items to complete
1011 * @param start_string a string to prefix the meter with (if printing)
1012 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
1015 * @return the progress meter
1017 static struct ProgressMeter *
1018 create_meter(unsigned int total, char * start_string, int print)
1020 struct ProgressMeter *ret;
1021 ret = GNUNET_malloc(sizeof(struct ProgressMeter));
1024 ret->modnum = total / 4;
1025 if (ret->modnum == 0) /* Divide by zero check */
1027 ret->dotnum = (total / 50) + 1;
1028 if (start_string != NULL)
1029 ret->startup_string = GNUNET_strdup(start_string);
1031 ret->startup_string = GNUNET_strdup("");
1037 * Update progress meter (increment by one).
1039 * @param meter the meter to update and print info for
1041 * @return GNUNET_YES if called the total requested,
1042 * GNUNET_NO if more items expected
1045 update_meter(struct ProgressMeter *meter)
1047 if (meter->print == GNUNET_YES)
1049 if (meter->completed % meter->modnum == 0)
1051 if (meter->completed == 0)
1053 fprintf (stdout, "%sProgress: [0%%", meter->startup_string);
1056 fprintf (stdout, "%d%%", (int) (((float) meter->completed
1057 / meter->total) * 100));
1059 else if (meter->completed % meter->dotnum == 0)
1060 fprintf (stdout, ".");
1062 if (meter->completed + 1 == meter->total)
1063 fprintf (stdout, "%d%%]\n", 100);
1068 if (meter->completed == meter->total)
1070 if (meter->completed > meter->total)
1071 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
1076 * Reset progress meter.
1078 * @param meter the meter to reset
1080 * @return GNUNET_YES if meter reset,
1081 * GNUNET_SYSERR on error
1084 reset_meter(struct ProgressMeter *meter)
1087 return GNUNET_SYSERR;
1089 meter->completed = 0;
1094 * Release resources for meter
1096 * @param meter the meter to free
1099 free_meter(struct ProgressMeter *meter)
1101 GNUNET_free_non_null (meter->startup_string);
1102 GNUNET_free (meter);
1106 * Get a topology from a string input.
1108 * @param topology where to write the retrieved topology
1109 * @param topology_string The string to attempt to
1110 * get a configuration value from
1111 * @return GNUNET_YES if topology string matched a
1112 * known topology, GNUNET_NO if not
1115 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology,
1116 const char *topology_string)
1119 * Strings representing topologies in enum
1121 static const char *topology_strings[] =
1124 * A clique (everyone connected to everyone else).
1129 * Small-world network (2d torus plus random links).
1134 * Small-world network (ring plus random links).
1154 * Certain percentage of peers are unable to communicate directly
1155 * replicating NAT conditions
1160 * Scale free topology.
1165 * Straight line topology.
1170 * All peers are disconnected.
1175 * Read the topology from a file.
1182 if (topology_string == NULL)
1184 while (topology_strings[curr] != NULL)
1186 if (strcasecmp (topology_strings[curr], topology_string) == 0)
1193 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
1198 * Get connect topology option from string input.
1200 * @param topology_option where to write the retrieved topology
1201 * @param topology_string The string to attempt to
1202 * get a configuration value from
1203 * @return GNUNET_YES if string matched a known
1204 * topology option, GNUNET_NO if not
1207 GNUNET_TESTING_topology_option_get(
1208 enum GNUNET_TESTING_TopologyOption *topology_option,
1209 const char *topology_string)
1212 * Options for connecting a topology as strings.
1214 static const char *topology_option_strings[] =
1217 * Try to connect all peers specified in the topology.
1222 * Choose a random subset of connections to create.
1224 "CONNECT_RANDOM_SUBSET",
1227 * Create at least X connections for each peer.
1232 * Using a depth first search, create one connection
1233 * per peer. If any are missed (graph disconnected)
1234 * start over at those peers until all have at least one
1240 * Find the N closest peers to each allowed peer in the
1241 * topology and make sure a connection to those peers
1242 * exists in the connect topology.
1247 * No options specified.
1254 if (topology_string == NULL)
1256 while (NULL != topology_option_strings[curr])
1258 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1260 *topology_option = curr;
1265 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1270 * Function to iterate over options. Copies
1271 * the options to the target configuration,
1272 * updating PORT values as needed.
1274 * @param cls closure
1275 * @param section name of the section
1276 * @param option name of the option
1277 * @param value value of the option
1280 update_config(void *cls, const char *section, const char *option,
1283 struct UpdateContext *ctx = cls;
1287 char *single_variable;
1288 char *per_host_variable;
1289 unsigned long long num_per_host;
1291 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1292 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1294 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1296 if ((ival != 0) && (GNUNET_YES
1297 != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1300 GNUNET_snprintf (cval, sizeof(cval), "%u", ctx->nport++);
1303 else if ((ival != 0) && (GNUNET_YES
1304 == GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1306 && GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1310 GNUNET_snprintf (cval, sizeof(cval), "%u", ival + ctx->fdnum
1316 if (0 == strcmp (option, "UNIXPATH"))
1318 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig,
1322 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1323 section, ctx->upnum++);
1326 else if ((GNUNET_YES
1327 == GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1330 && (num_per_host > 0))
1333 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1334 section, ctx->fdnum % num_per_host);
1339 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1341 value = ctx->hostname;
1343 GNUNET_free (single_variable);
1344 GNUNET_free (per_host_variable);
1345 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1349 * Create a new configuration using the given configuration
1350 * as a template; however, each PORT in the existing cfg
1351 * must be renumbered by incrementing "*port". If we run
1352 * out of "*port" numbers, return NULL.
1354 * @param cfg template configuration
1355 * @param off the current peer offset
1356 * @param port port numbers to use, update to reflect
1357 * port numbers that were used
1358 * @param upnum number to make unix domain socket names unique
1359 * @param hostname hostname of the controlling host, to allow control connections from
1360 * @param fdnum number used to offset the unix domain socket for grouped processes
1361 * (such as statistics or peerinfo, which can be shared among others)
1363 * @return new configuration, NULL on error
1365 static struct GNUNET_CONFIGURATION_Handle *
1366 make_config(const struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t off,
1367 uint16_t * port, uint32_t * upnum, const char *hostname,
1370 struct UpdateContext uc;
1373 char *allowed_hosts;
1379 uc.ret = GNUNET_CONFIGURATION_create ();
1380 uc.hostname = hostname;
1383 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1384 if (uc.nport >= HIGH_PORT)
1387 GNUNET_CONFIGURATION_destroy (uc.ret);
1391 if (GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "control_host",
1392 &control_host) == GNUNET_OK)
1394 if (hostname != NULL)
1395 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1398 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1400 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1402 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1403 "ACCEPT_FROM", allowed_hosts);
1404 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1406 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1407 "ACCEPT_FROM", allowed_hosts);
1409 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH", "");
1410 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH",
1412 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH", "");
1413 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1416 GNUNET_free_non_null (control_host);
1417 GNUNET_free (allowed_hosts);
1420 /* arm needs to know to allow connections from the host on which it is running,
1421 * otherwise gnunet-arm is unable to connect to it in some instances */
1422 if (hostname != NULL)
1424 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1425 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1427 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1429 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1431 GNUNET_free (allowed_hosts);
1435 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1437 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1441 *port = (uint16_t) uc.nport;
1449 * Remove entries from the peer connection list
1451 * @param pg the peer group we are working with
1452 * @param first index of the first peer
1453 * @param second index of the second peer
1454 * @param list the peer list to use
1455 * @param check UNUSED
1457 * @return the number of connections added (can be 0, 1 or 2)
1461 remove_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1462 unsigned int second, enum PeerLists list, unsigned int check)
1466 struct PeerConnection **first_list;
1467 struct PeerConnection **second_list;
1468 struct PeerConnection *first_iter;
1469 struct PeerConnection *second_iter;
1470 struct PeerConnection **first_tail;
1471 struct PeerConnection **second_tail;
1474 GNUNET_HashCode hash_first;
1475 GNUNET_HashCode hash_second;
1477 hash_from_uid (first, &hash_first);
1478 hash_from_uid (second, &hash_second);
1486 first_list = &pg->peers[first].allowed_peers_head;
1487 second_list = &pg->peers[second].allowed_peers_head;
1488 first_tail = &pg->peers[first].allowed_peers_tail;
1489 second_tail = &pg->peers[second].allowed_peers_tail;
1492 first_list = &pg->peers[first].connect_peers_head;
1493 second_list = &pg->peers[second].connect_peers_head;
1494 first_tail = &pg->peers[first].connect_peers_tail;
1495 second_tail = &pg->peers[second].connect_peers_tail;
1498 first_list = &pg->peers[first].blacklisted_peers_head;
1499 second_list = &pg->peers[second].blacklisted_peers_head;
1500 first_tail = &pg->peers[first].blacklisted_peers_tail;
1501 second_tail = &pg->peers[second].blacklisted_peers_tail;
1504 first_list = &pg->peers[first].connect_peers_working_set_head;
1505 second_list = &pg->peers[second].connect_peers_working_set_head;
1506 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1507 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1514 first_iter = *first_list;
1515 while (first_iter != NULL)
1517 if (first_iter->index == second)
1519 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1520 GNUNET_free(first_iter);
1524 first_iter = first_iter->next;
1527 second_iter = *second_list;
1528 while (second_iter != NULL)
1530 if (second_iter->index == first)
1532 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1533 GNUNET_free(second_iter);
1537 second_iter = second_iter->next;
1541 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1544 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1549 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1552 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1561 * Add entries to the some list
1563 * @param pg the peer group we are working with
1564 * @param first index of the first peer
1565 * @param second index of the second peer
1566 * @param list the list type that we should modify
1567 * @param check GNUNET_YES to check lists before adding
1568 * GNUNET_NO to force add
1570 * @return the number of connections added (can be 0, 1 or 2)
1574 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1575 unsigned int second, enum PeerLists list, unsigned int check)
1581 struct PeerConnection **first_list;
1582 struct PeerConnection **second_list;
1583 struct PeerConnection *first_iter;
1584 struct PeerConnection *second_iter;
1585 struct PeerConnection *new_first;
1586 struct PeerConnection *new_second;
1587 struct PeerConnection **first_tail;
1588 struct PeerConnection **second_tail;
1593 first_list = &pg->peers[first].allowed_peers_head;
1594 second_list = &pg->peers[second].allowed_peers_head;
1595 first_tail = &pg->peers[first].allowed_peers_tail;
1596 second_tail = &pg->peers[second].allowed_peers_tail;
1599 first_list = &pg->peers[first].connect_peers_head;
1600 second_list = &pg->peers[second].connect_peers_head;
1601 first_tail = &pg->peers[first].connect_peers_tail;
1602 second_tail = &pg->peers[second].connect_peers_tail;
1605 first_list = &pg->peers[first].blacklisted_peers_head;
1606 second_list = &pg->peers[second].blacklisted_peers_head;
1607 first_tail = &pg->peers[first].blacklisted_peers_tail;
1608 second_tail = &pg->peers[second].blacklisted_peers_tail;
1611 first_list = &pg->peers[first].connect_peers_working_set_head;
1612 second_list = &pg->peers[second].connect_peers_working_set_head;
1613 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1614 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1621 add_first = GNUNET_YES;
1622 add_second = GNUNET_YES;
1624 if (check == GNUNET_YES)
1626 first_iter = *first_list;
1627 while (first_iter != NULL)
1629 if (first_iter->index == second)
1631 add_first = GNUNET_NO;
1634 first_iter = first_iter->next;
1637 second_iter = *second_list;
1638 while (second_iter != NULL)
1640 if (second_iter->index == first)
1642 add_second = GNUNET_NO;
1645 second_iter = second_iter->next;
1652 #if AVOID_CONN_MALLOC
1653 new_first = &pg->working_peer_connections[pg->current_peer_connection];
1654 pg->current_peer_connection++;
1656 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1658 new_first->index = second;
1659 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1660 pg->peers[first].num_connections++;
1666 #if AVOID_CONN_MALLOC
1667 new_second = &pg->working_peer_connections[pg->current_peer_connection];
1668 pg->current_peer_connection++;
1670 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1672 new_second->index = first;
1673 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1674 pg->peers[second].num_connections++;
1682 * Scale free network construction as described in:
1684 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1686 * Start with a network of "one" peer, then progressively add
1687 * peers up to the total number. At each step, iterate over
1688 * all possible peers and connect new peer based on number of
1689 * existing connections of the target peer.
1691 * @param pg the peer group we are dealing with
1692 * @param proc the connection processor to use
1693 * @param list the peer list to use
1695 * @return the number of connections created
1698 create_scale_free(struct GNUNET_TESTING_PeerGroup *pg,
1699 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1702 unsigned int total_connections;
1703 unsigned int outer_count;
1705 unsigned int previous_total_connections;
1709 GNUNET_assert (pg->total > 1);
1711 /* Add a connection between the first two nodes */
1712 total_connections = proc (pg, 0, 1, list, GNUNET_YES);
1714 for (outer_count = 1; outer_count < pg->total; outer_count++)
1716 previous_total_connections = total_connections;
1717 for (i = 0; i < outer_count; i++)
1719 probability = pg->peers[i].num_connections
1720 / (double) previous_total_connections;
1722 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1724 / ((double) UINT64_MAX);
1726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1727 "Considering connecting peer %d to peer %d\n",
1730 if (random < probability)
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1734 "Connecting peer %d to peer %d\n", outer_count, i);
1736 total_connections += proc (pg, outer_count, i, list, GNUNET_YES);
1741 return total_connections;
1745 * Create a topology given a peer group (set of running peers)
1746 * and a connection processor. Creates a small world topology
1747 * according to the rewired ring construction. The basic
1748 * behavior is that a ring topology is created, but with some
1749 * probability instead of connecting a peer to the next
1750 * neighbor in the ring a connection will be created to a peer
1751 * selected uniformly at random. We use the TESTING
1752 * PERCENTAGE option to specify what number of
1753 * connections each peer should have. Default is 2,
1754 * which makes the ring, any given number is multiplied by
1755 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1756 * each peer have on average 2logn connections. The additional
1757 * connections are made at increasing distance around the ring
1758 * from the original peer, or to random peers based on the re-
1759 * wiring probability. The TESTING
1760 * PROBABILITY option is used as the probability that a given
1761 * connection is rewired.
1763 * @param pg the peergroup to create the topology on
1764 * @param proc the connection processor to call to actually set
1765 * up connections between two peers
1766 * @param list the peer list to use
1768 * @return the number of connections that were set up
1772 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg,
1773 GNUNET_TESTING_ConnectionProcessor proc,
1774 enum PeerLists list)
1778 unsigned int natLog;
1779 unsigned int randomPeer;
1780 double random, logNModifier, probability;
1781 unsigned int smallWorldConnections;
1786 unsigned int useAnd;
1787 int connect_attempts;
1789 logNModifier = 0.5; /* FIXME: default value? */
1790 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1794 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1796 GNUNET_ERROR_TYPE_WARNING,
1798 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1799 p_string, "LOGNMODIFIER", "TESTING");
1800 GNUNET_free (p_string);
1802 probability = 0.5; /* FIXME: default percentage? */
1803 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1807 if (sscanf (p_string, "%lf", &probability) != 1)
1809 GNUNET_ERROR_TYPE_WARNING,
1811 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1812 p_string, "PERCENTAGE", "TESTING");
1813 GNUNET_free (p_string);
1815 natLog = log (pg->total);
1816 connsPerPeer = ceil (natLog * logNModifier);
1818 if (connsPerPeer % 2 == 1)
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."),
1824 smallWorldConnections = 0;
1825 connect_attempts = 0;
1826 for (i = 0; i < pg->total; i++)
1829 max = i + connsPerPeer / 2;
1830 min = i - connsPerPeer / 2;
1832 if (max > pg->total - 1)
1834 max = max - pg->total;
1840 min = pg->total - 1 + min;
1844 for (j = 0; j < connsPerPeer / 2; j++)
1847 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1849 / ((double) UINT64_MAX));
1850 if (random < probability)
1852 /* Connect to uniformly selected random peer */
1854 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1856 while ((((randomPeer < max) && (randomPeer > min)) && (useAnd
1857 == 0)) || (((randomPeer > min) || (randomPeer < max))
1861 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1864 smallWorldConnections += proc (pg, i, randomPeer, list,
1869 nodeToConnect = i + j + 1;
1870 if (nodeToConnect > pg->total - 1)
1872 nodeToConnect = nodeToConnect - pg->total;
1874 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
1880 connect_attempts += smallWorldConnections;
1882 return connect_attempts;
1886 * Create a topology given a peer group (set of running peers)
1887 * and a connection processor.
1889 * @param pg the peergroup to create the topology on
1890 * @param proc the connection processor to call to actually set
1891 * up connections between two peers
1892 * @param list the peer list to use
1894 * @return the number of connections that were set up
1898 create_nated_internet(struct GNUNET_TESTING_PeerGroup *pg,
1899 GNUNET_TESTING_ConnectionProcessor proc,
1900 enum PeerLists list)
1902 unsigned int outer_count, inner_count;
1903 unsigned int cutoff;
1904 int connect_attempts;
1905 double nat_percentage;
1908 nat_percentage = 0.6; /* FIXME: default percentage? */
1909 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1913 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1915 GNUNET_ERROR_TYPE_WARNING,
1917 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1918 p_string, "PERCENTAGE", "TESTING");
1919 GNUNET_free (p_string);
1922 cutoff = (unsigned int) (nat_percentage * pg->total);
1923 connect_attempts = 0;
1924 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1926 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1928 if ((outer_count > cutoff) || (inner_count > cutoff))
1931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1932 "Connecting peer %d to peer %d\n",
1933 outer_count, inner_count);
1935 connect_attempts += proc (pg, outer_count, inner_count, list,
1940 return connect_attempts;
1945 * Create a topology given a peer group (set of running peers)
1946 * and a connection processor.
1948 * @param pg the peergroup to create the topology on
1949 * @param proc the connection processor to call to actually set
1950 * up connections between two peers
1951 * @param list the peer list to use
1953 * @return the number of connections that were set up
1957 create_nated_internet_copy(struct GNUNET_TESTING_PeerGroup *pg,
1958 GNUNET_TESTING_ConnectionProcessor proc,
1959 enum PeerLists list)
1961 unsigned int outer_count, inner_count;
1962 unsigned int cutoff;
1963 int connect_attempts;
1964 double nat_percentage;
1967 struct ProgressMeter *conn_meter;
1969 nat_percentage = 0.6; /* FIXME: default percentage? */
1970 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1974 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1976 GNUNET_ERROR_TYPE_WARNING,
1978 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1979 p_string, "PERCENTAGE", "TESTING");
1980 GNUNET_free (p_string);
1983 cutoff = (unsigned int) (nat_percentage * pg->total);
1985 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1987 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1989 if ((outer_count > cutoff) || (inner_count > cutoff))
1995 conn_meter = create_meter (count, "NAT COPY", GNUNET_YES);
1996 connect_attempts = 0;
1997 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1999 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2001 if ((outer_count > cutoff) || (inner_count > cutoff))
2004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2005 "Connecting peer %d to peer %d\n",
2006 outer_count, inner_count);
2008 connect_attempts += proc (pg, outer_count, inner_count, list,
2010 add_connections (pg, outer_count, inner_count, ALLOWED, GNUNET_NO);
2011 update_meter (conn_meter);
2015 free_meter (conn_meter);
2017 return connect_attempts;
2022 * Create a topology given a peer group (set of running peers)
2023 * and a connection processor.
2025 * @param pg the peergroup to create the topology on
2026 * @param proc the connection processor to call to actually set
2027 * up connections between two peers
2028 * @param list the peer list to use
2030 * @return the number of connections that were set up
2034 create_small_world(struct GNUNET_TESTING_PeerGroup *pg,
2035 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2037 unsigned int i, j, k;
2038 unsigned int square;
2041 unsigned int toggle = 1;
2042 unsigned int nodeToConnect;
2043 unsigned int natLog;
2044 unsigned int node1Row;
2045 unsigned int node1Col;
2046 unsigned int node2Row;
2047 unsigned int node2Col;
2048 unsigned int distance;
2049 double probability, random, percentage;
2050 unsigned int smallWorldConnections;
2051 unsigned int small_world_it;
2053 int connect_attempts;
2054 square = floor (sqrt (pg->total));
2058 percentage = 0.5; /* FIXME: default percentage? */
2059 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2063 if (sscanf (p_string, "%lf", &percentage) != 1)
2065 GNUNET_ERROR_TYPE_WARNING,
2067 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2068 p_string, "PERCENTAGE", "TESTING");
2069 GNUNET_free (p_string);
2071 if (percentage < 0.0)
2074 GNUNET_ERROR_TYPE_WARNING,
2076 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
2077 "PERCENTAGE", "TESTING", percentage);
2080 probability = 0.5; /* FIXME: default percentage? */
2081 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2085 if (sscanf (p_string, "%lf", &probability) != 1)
2087 GNUNET_ERROR_TYPE_WARNING,
2089 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2090 p_string, "PROBABILITY", "TESTING");
2091 GNUNET_free (p_string);
2093 if (square * square != pg->total)
2095 while (rows * cols < pg->total)
2097 if (toggle % 2 == 0)
2106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2108 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2112 connect_attempts = 0;
2113 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2114 * to the node to its right and above. Once this is over, we'll have our torus!
2115 * Special case for the last node (if the rows and columns are not equal), connect
2116 * to the first in the row to maintain topology.
2118 for (i = 0; i < pg->total; i++)
2120 /* First connect to the node to the right */
2121 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2122 nodeToConnect = i + 1;
2123 else if (i + 1 == pg->total)
2124 nodeToConnect = rows * cols - cols;
2126 nodeToConnect = i - cols + 1;
2128 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2132 nodeToConnect = (rows * cols) - cols + i;
2133 if (nodeToConnect >= pg->total)
2134 nodeToConnect -= cols;
2137 nodeToConnect = i - cols;
2139 if (nodeToConnect < pg->total)
2140 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2142 natLog = log (pg->total);
2143 #if VERBOSE_TESTING > 2
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2145 _("natural log of %d is %d, will run %d iterations\n"),
2146 pg->total, natLog, (int) (natLog * percentage));
2147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2148 _("Total connections added thus far: %u!\n"), connect_attempts);
2150 smallWorldConnections = 0;
2151 small_world_it = (unsigned int) (natLog * percentage);
2152 if (small_world_it < 1)
2154 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
2155 for (i = 0; i < small_world_it; i++)
2157 for (j = 0; j < pg->total; j++)
2159 /* Determine the row and column of node at position j on the 2d torus */
2160 node1Row = j / cols;
2161 node1Col = j - (node1Row * cols);
2162 for (k = 0; k < pg->total; k++)
2164 /* Determine the row and column of node at position k on the 2d torus */
2165 node2Row = k / cols;
2166 node2Col = k - (node2Row * cols);
2167 /* Simple Cartesian distance */
2168 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
2171 /* Calculate probability as 1 over the square of the distance */
2172 probability = 1.0 / (distance * distance);
2173 /* Choose a random value between 0 and 1 */
2175 = ((double) GNUNET_CRYPTO_random_u64 (
2176 GNUNET_CRYPTO_QUALITY_WEAK,
2178 / ((double) UINT64_MAX);
2179 /* If random < probability, then connect the two nodes */
2180 if (random < probability)
2181 smallWorldConnections += proc (pg, j, k, list, GNUNET_YES);
2187 connect_attempts += smallWorldConnections;
2188 #if VERBOSE_TESTING > 2
2189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2190 _("Total connections added for small world: %d!\n"),
2191 smallWorldConnections);
2193 return connect_attempts;
2197 * Create a topology given a peer group (set of running peers)
2198 * and a connection processor.
2200 * @param pg the peergroup to create the topology on
2201 * @param proc the connection processor to call to actually set
2202 * up connections between two peers
2203 * @param list the peer list to use
2205 * @return the number of connections that were set up
2209 create_erdos_renyi(struct GNUNET_TESTING_PeerGroup *pg,
2210 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2213 unsigned int outer_count;
2214 unsigned int inner_count;
2215 int connect_attempts;
2219 probability = 0.5; /* FIXME: default percentage? */
2220 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2224 if (sscanf (p_string, "%lf", &probability) != 1)
2226 GNUNET_ERROR_TYPE_WARNING,
2228 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2229 p_string, "PROBABILITY", "TESTING");
2230 GNUNET_free (p_string);
2232 connect_attempts = 0;
2233 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2235 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2238 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2240 / ((double) UINT64_MAX);
2242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2243 _("rand is %f probability is %f\n"), temp_rand,
2246 if (temp_rand < probability)
2248 connect_attempts += proc (pg, outer_count, inner_count, list,
2254 return connect_attempts;
2258 * Create a topology given a peer group (set of running peers)
2259 * and a connection processor. This particular function creates
2260 * the connections for a 2d-torus, plus additional "closest"
2261 * connections per peer.
2263 * @param pg the peergroup to create the topology on
2264 * @param proc the connection processor to call to actually set
2265 * up connections between two peers
2266 * @param list the peer list to use
2268 * @return the number of connections that were set up
2272 create_2d_torus(struct GNUNET_TESTING_PeerGroup *pg,
2273 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2276 unsigned int square;
2279 unsigned int toggle = 1;
2280 unsigned int nodeToConnect;
2281 int connect_attempts;
2283 connect_attempts = 0;
2285 square = floor (sqrt (pg->total));
2289 if (square * square != pg->total)
2291 while (rows * cols < pg->total)
2293 if (toggle % 2 == 0)
2302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2304 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2307 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2308 * to the node to its right and above. Once this is over, we'll have our torus!
2309 * Special case for the last node (if the rows and columns are not equal), connect
2310 * to the first in the row to maintain topology.
2312 for (i = 0; i < pg->total; i++)
2314 /* First connect to the node to the right */
2315 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2316 nodeToConnect = i + 1;
2317 else if (i + 1 == pg->total)
2318 nodeToConnect = rows * cols - cols;
2320 nodeToConnect = i - cols + 1;
2322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2323 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2325 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2327 /* Second connect to the node immediately above */
2330 nodeToConnect = (rows * cols) - cols + i;
2331 if (nodeToConnect >= pg->total)
2332 nodeToConnect -= cols;
2335 nodeToConnect = i - cols;
2337 if (nodeToConnect < pg->total)
2340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2341 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2343 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2348 return connect_attempts;
2352 * Create a topology given a peer group (set of running peers)
2353 * and a connection processor.
2355 * @param pg the peergroup to create the topology on
2356 * @param proc the connection processor to call to actually set
2357 * up connections between two peers
2358 * @param list the peer list to use
2359 * @param check does the connection processor need to check before
2360 * performing an action on the list?
2362 * @return the number of connections that were set up
2366 create_clique(struct GNUNET_TESTING_PeerGroup *pg,
2367 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list,
2370 unsigned int outer_count;
2371 unsigned int inner_count;
2372 int connect_attempts;
2373 struct ProgressMeter *conn_meter;
2374 connect_attempts = 0;
2376 conn_meter = create_meter ((((pg->total * pg->total) + pg->total) / 2)
2377 - pg->total, "Create Clique ", GNUNET_YES);
2378 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2380 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2384 "Connecting peer %d to peer %d\n",
2385 outer_count, inner_count);
2387 connect_attempts += proc (pg, outer_count, inner_count, list, check);
2388 update_meter (conn_meter);
2391 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Meter has %d left\n",
2392 conn_meter->total - conn_meter->completed);
2393 reset_meter (conn_meter);
2394 free_meter (conn_meter);
2395 return connect_attempts;
2400 * Iterator over hash map entries.
2402 * @param cls closure the peer group
2403 * @param key the key stored in the hashmap is the
2404 * index of the peer to connect to
2405 * @param value value in the hash map, handle to the peer daemon
2406 * @return GNUNET_YES if we should continue to
2411 unblacklist_iterator (void *cls,
2412 const GNUNET_HashCode * key,
2415 struct UnblacklistContext *un_ctx = cls;
2416 uint32_t second_pos;
2418 uid_from_hash (key, &second_pos);
2420 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2427 * Create a blacklist topology based on the allowed topology
2428 * which disallows any connections not in the allowed topology
2429 * at the transport level.
2431 * @param pg the peergroup to create the topology on
2432 * @param proc the connection processor to call to allow
2433 * up connections between two peers
2435 * @return the number of connections that were set up
2439 copy_allowed(struct GNUNET_TESTING_PeerGroup *pg,
2440 GNUNET_TESTING_ConnectionProcessor proc)
2442 struct UnblacklistContext un_ctx;
2445 struct PeerConnection *iter;
2449 for (count = 0; count < pg->total - 1; count++)
2451 un_ctx.first_uid = count;
2453 iter = pg->peers[count].allowed_peers_head;
2454 while (iter != NULL)
2456 remove_connections (pg, count, iter->index, BLACKLIST, GNUNET_YES);
2457 //unblacklist_connections(pg, count, iter->index);
2461 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers, &unblacklist_iterator, &un_ctx);
2464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2469 * Create a topology given a peer group (set of running peers)
2470 * and a connection processor.
2472 * @param pg the peergroup to create the topology on
2473 * @param proc the connection processor to call to actually set
2474 * up connections between two peers
2475 * @param list which list should be modified
2477 * @return the number of connections that were set up
2481 create_line(struct GNUNET_TESTING_PeerGroup *pg,
2482 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2485 int connect_attempts;
2487 connect_attempts = 0;
2489 /* Connect each peer to the next highest numbered peer */
2490 for (count = 0; count < pg->total - 1; count++)
2493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2494 "Connecting peer %d to peer %d\n", count, count + 1);
2496 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2499 return connect_attempts;
2503 * Create a topology given a peer group (set of running peers)
2504 * and a connection processor.
2506 * @param pg the peergroup to create the topology on
2507 * @param filename the file to read topology information from
2508 * @param proc the connection processor to call to actually set
2509 * up connections between two peers
2510 * @param list the peer list to use
2512 * @return the number of connections that were set up
2516 create_from_file(struct GNUNET_TESTING_PeerGroup *pg, char *filename,
2517 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2519 int connect_attempts;
2520 unsigned int first_peer_index;
2521 unsigned int second_peer_index;
2522 connect_attempts = 0;
2527 unsigned int total_peers;
2529 enum States curr_state;
2531 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2532 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2534 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2536 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2537 "Could not open file `%s' specified for topology!", filename);
2538 return connect_attempts;
2541 data = GNUNET_malloc_large (frstat.st_size);
2542 GNUNET_assert(data != NULL);
2543 if (frstat.st_size != GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2546 GNUNET_ERROR_TYPE_ERROR,
2547 "Could not read file %s specified for host list, ending test!",
2550 return connect_attempts;
2555 first_peer_index = 0;
2556 /* First line should contain a single integer, specifying the number of peers */
2557 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2558 curr_state = NUM_PEERS;
2559 while (count < frstat.st_size - 1)
2561 if ((buf[count] == '\n') || (buf[count] == ' '))
2571 total_peers = strtoul(&buf[count], NULL, 10);
2574 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2575 "Failed to read number of peers from topology file!\n");
2576 GNUNET_free_non_null(data);
2577 return connect_attempts;
2579 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2580 "Read %u total peers in topology\n", total_peers);
2581 GNUNET_assert(total_peers == pg->total);
2582 curr_state = PEER_INDEX;
2583 while ((buf[count] != '\n') && (count < frstat.st_size - 1))
2589 first_peer_index = strtoul(&buf[count], NULL, 10);
2592 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2593 "Failed to read peer index from topology file!\n");
2594 GNUNET_free_non_null(data);
2595 return connect_attempts;
2597 while ((buf[count] != ':') && (count < frstat.st_size - 1))
2600 curr_state = OTHER_PEER_INDEX;
2603 if (1 == sscanf (&buf[count], ":"))
2604 curr_state = OTHER_PEER_INDEX;
2607 case OTHER_PEER_INDEX:
2609 second_peer_index = strtoul(&buf[count], NULL, 10);
2612 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2613 "Failed to peer index from topology file!\n");
2614 GNUNET_free_non_null(data);
2615 return connect_attempts;
2617 /* Assume file is written with first peer 1, but array index is 0 */
2618 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index
2619 - 1, list, GNUNET_YES);
2620 while ((buf[count] != '\n') && (buf[count] != ',') && (count
2621 < frstat.st_size - 1))
2623 if (buf[count] == '\n')
2625 curr_state = PEER_INDEX;
2627 else if (buf[count] != ',')
2629 curr_state = OTHER_PEER_INDEX;
2634 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2635 "Found bad data in topology file while in state %d!\n",
2638 return connect_attempts;
2642 return connect_attempts;
2646 * Create a topology given a peer group (set of running peers)
2647 * and a connection processor.
2649 * @param pg the peergroup to create the topology on
2650 * @param proc the connection processor to call to actually set
2651 * up connections between two peers
2652 * @param list the peer list to use
2654 * @return the number of connections that were set up
2658 create_ring(struct GNUNET_TESTING_PeerGroup *pg,
2659 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2662 int connect_attempts;
2664 connect_attempts = 0;
2666 /* Connect each peer to the next highest numbered peer */
2667 for (count = 0; count < pg->total - 1; count++)
2670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2671 "Connecting peer %d to peer %d\n", count, count + 1);
2673 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2676 /* Connect the last peer to the first peer */
2677 connect_attempts += proc (pg, pg->total - 1, 0, list, GNUNET_YES);
2679 return connect_attempts;
2684 * Iterator for writing friends of a peer to a file.
2686 * @param cls closure, an open writable file handle
2687 * @param key the key the daemon was stored under
2688 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2690 * @return GNUNET_YES to continue iteration
2692 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2693 * with a single file_iterator that takes a closure which contains
2694 * the prefix to write before the peer. Then this could be used
2695 * for blacklisting multiple transports and writing the friend
2696 * file. I'm sure *someone* will complain loudly about other
2697 * things that negate these functions even existing so no point in
2701 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2703 FILE *temp_friend_handle = cls;
2704 struct GNUNET_TESTING_Daemon *peer = value;
2705 struct GNUNET_PeerIdentity *temppeer;
2706 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2708 temppeer = &peer->id;
2709 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2710 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2715 struct BlacklistContext
2718 * The (open) file handle to write to
2720 FILE *temp_file_handle;
2723 * The transport that this peer will be blacklisted on.
2729 * Iterator for writing blacklist data to appropriate files.
2731 * @param cls closure, an open writable file handle
2732 * @param key the key the daemon was stored under
2733 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2735 * @return GNUNET_YES to continue iteration
2738 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2740 struct BlacklistContext *blacklist_ctx = cls;
2741 struct GNUNET_TESTING_Daemon *peer = value;
2742 struct GNUNET_PeerIdentity *temppeer;
2743 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2745 temppeer = &peer->id;
2746 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2747 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2748 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2749 blacklist_ctx->transport, (char *) &peer_enc);
2756 * Create the friend files based on the PeerConnection's
2757 * of each peer in the peer group, and copy the files
2758 * to the appropriate place
2760 * @param pg the peer group we are dealing with
2763 create_and_copy_friend_files(struct GNUNET_TESTING_PeerGroup *pg)
2765 FILE *temp_friend_handle;
2766 unsigned int pg_iter;
2767 char *temp_service_path;
2768 struct GNUNET_OS_Process **procarr;
2772 enum GNUNET_OS_ProcessStatusType type;
2773 unsigned long return_code;
2781 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2782 struct PeerConnection *conn_iter;
2784 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2785 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2787 mytemp = GNUNET_DISK_mktemp ("friends");
2788 GNUNET_assert (mytemp != NULL);
2789 temp_friend_handle = fopen (mytemp, "wt");
2790 GNUNET_assert (temp_friend_handle != NULL);
2792 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2793 while (conn_iter != NULL)
2795 GNUNET_CRYPTO_hash_to_enc (
2796 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2798 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2799 conn_iter = conn_iter->next;
2802 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2803 &friend_file_iterator,
2804 temp_friend_handle);
2806 fclose (temp_friend_handle);
2809 != GNUNET_CONFIGURATION_get_value_string (
2810 pg->peers[pg_iter]. daemon->cfg,
2811 "PATHS", "SERVICEHOME",
2812 &temp_service_path))
2815 GNUNET_ERROR_TYPE_WARNING,
2817 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2818 "SERVICEHOME", "PATHS");
2819 if (UNLINK (mytemp) != 0)
2820 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2822 GNUNET_free (mytemp);
2826 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2828 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2829 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
2832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2833 _("Copying file with command cp %s %s\n"), mytemp, arg);
2835 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2836 GNUNET_OS_process_close (procarr[pg_iter]);
2839 else /* Remote, scp the file to the correct place */
2841 if (NULL != pg->peers[pg_iter].daemon->username)
2842 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2843 pg->peers[pg_iter].daemon->username,
2844 pg->peers[pg_iter].daemon->hostname,
2847 GNUNET_asprintf (&arg, "%s:%s/friends",
2848 pg->peers[pg_iter].daemon->hostname,
2850 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
2853 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2854 GNUNET_OS_process_close (procarr[pg_iter]);
2855 if (ret != GNUNET_OK)
2857 procarr[pg_iter] = NULL;
2859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2860 _("Copying file with command scp %s %s\n"), mytemp,
2865 GNUNET_free (temp_service_path);
2866 GNUNET_free (mytemp);
2871 ret = GNUNET_SYSERR;
2872 while ((count < max_wait) && (ret != GNUNET_OK))
2875 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2879 _("Checking copy status of file %d\n"), pg_iter);
2881 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2883 if (GNUNET_OS_process_status
2884 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2886 ret = GNUNET_SYSERR;
2888 else if ((type != GNUNET_OS_PROCESS_EXITED)
2889 || (return_code != 0))
2891 ret = GNUNET_SYSERR;
2895 GNUNET_OS_process_close (procarr[pg_iter]);
2896 procarr[pg_iter] = NULL;
2898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2899 _("File %d copied\n"), pg_iter);
2905 if (ret == GNUNET_SYSERR)
2907 /* FIXME: why sleep here? -CG */
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2914 _("Finished copying all friend files!\n"));
2917 GNUNET_free (procarr);
2922 * Create the blacklist files based on the PeerConnection's
2923 * of each peer in the peer group, and copy the files
2924 * to the appropriate place.
2926 * @param pg the peer group we are dealing with
2927 * @param transports space delimited list of transports to blacklist
2930 create_and_copy_blacklist_files(struct GNUNET_TESTING_PeerGroup *pg,
2931 const char *transports)
2933 FILE *temp_file_handle;
2934 unsigned int pg_iter;
2935 char *temp_service_path;
2936 struct GNUNET_OS_Process **procarr;
2939 enum GNUNET_OS_ProcessStatusType type;
2940 unsigned long return_code;
2947 char *temp_transports;
2950 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2951 struct PeerConnection *conn_iter;
2953 static struct BlacklistContext blacklist_ctx;
2956 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2957 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2959 mytemp = GNUNET_DISK_mktemp ("blacklist");
2960 GNUNET_assert (mytemp != NULL);
2961 temp_file_handle = fopen (mytemp, "wt");
2962 GNUNET_assert (temp_file_handle != NULL);
2963 temp_transports = GNUNET_strdup (transports);
2965 blacklist_ctx.temp_file_handle = temp_file_handle;
2967 transport_len = strlen (temp_transports) + 1;
2970 for (i = 0; i < transport_len; i++)
2972 if ((temp_transports[i] == ' ') && (pos == NULL))
2973 continue; /* At start of string (whitespace) */
2974 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2976 temp_transports[i] = '\0';
2978 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
2979 while (conn_iter != NULL)
2981 GNUNET_CRYPTO_hash_to_enc (
2982 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2984 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
2985 conn_iter = conn_iter->next;
2989 blacklist_ctx.transport = pos;
2990 entry_count = GNUNET_CONTAINER_multihashmap_iterate (pg->
2992 [pg_iter].blacklisted_peers,
2993 &blacklist_file_iterator,
2997 } /* At beginning of actual string */
2998 else if (pos == NULL)
3000 pos = &temp_transports[i];
3004 GNUNET_free (temp_transports);
3005 fclose (temp_file_handle);
3008 != GNUNET_CONFIGURATION_get_value_string (
3009 pg->peers[pg_iter]. daemon->cfg,
3010 "PATHS", "SERVICEHOME",
3011 &temp_service_path))
3014 GNUNET_ERROR_TYPE_WARNING,
3016 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
3017 "SERVICEHOME", "PATHS");
3018 if (UNLINK (mytemp) != 0)
3019 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
3021 GNUNET_free (mytemp);
3025 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
3027 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
3028 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
3031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3032 _("Copying file with command cp %s %s\n"), mytemp, arg);
3037 else /* Remote, scp the file to the correct place */
3039 if (NULL != pg->peers[pg_iter].daemon->username)
3040 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
3041 pg->peers[pg_iter].daemon->username,
3042 pg->peers[pg_iter].daemon->hostname,
3045 GNUNET_asprintf (&arg, "%s:%s/blacklist",
3046 pg->peers[pg_iter].daemon->hostname,
3048 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
3051 GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
3054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3055 _("Copying file with command scp %s %s\n"), mytemp,
3060 GNUNET_free (temp_service_path);
3061 GNUNET_free (mytemp);
3065 ret = GNUNET_SYSERR;
3066 while ((count < max_wait) && (ret != GNUNET_OK))
3069 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3073 _("Checking copy status of file %d\n"), pg_iter);
3075 if (procarr[pg_iter] != NULL) /* Check for already completed! */
3077 if (GNUNET_OS_process_status (procarr[pg_iter], &type,
3078 &return_code) != GNUNET_OK)
3080 ret = GNUNET_SYSERR;
3082 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
3084 ret = GNUNET_SYSERR;
3088 GNUNET_OS_process_close (procarr[pg_iter]);
3089 procarr[pg_iter] = NULL;
3091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3092 _("File %d copied\n"), pg_iter);
3098 if (ret == GNUNET_SYSERR)
3100 /* FIXME: why sleep here? -CG */
3106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3107 _("Finished copying all blacklist files!\n"));
3109 GNUNET_free (procarr);
3113 /* Forward Declaration */
3115 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3118 * Choose a random peer's next connection to create, and
3119 * call schedule_connect to set up the connect task.
3121 * @param ct_ctx the overall connection context
3124 preschedule_connect(struct GNUNET_TESTING_PeerGroup *pg)
3126 struct ConnectTopologyContext *ct_ctx = &pg->ct_ctx;
3127 struct PeerConnection *connection_iter;
3128 struct ConnectContext *connect_context;
3129 uint32_t random_peer;
3131 if (ct_ctx->remaining_connections == 0)
3134 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3135 while (pg->peers[random_peer].connect_peers_head == NULL)
3136 random_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3139 connection_iter = pg->peers[random_peer].connect_peers_head;
3140 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3141 connect_context->first_index = random_peer;
3142 connect_context->second_index = connection_iter->index;
3143 connect_context->ct_ctx = ct_ctx;
3144 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3145 GNUNET_CONTAINER_DLL_remove(pg->peers[random_peer].connect_peers_head, pg->peers[random_peer].connect_peers_tail, connection_iter);
3146 GNUNET_free(connection_iter);
3147 ct_ctx->remaining_connections--;
3151 /* Forward declaration */
3152 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3155 * Close connections and free the hello context.
3157 * @param cls the 'struct SendHelloContext *'
3158 * @param tc scheduler context
3161 free_hello_context (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3163 struct SendHelloContext *send_hello_context = cls;
3164 if (send_hello_context->peer->daemon->server != NULL)
3166 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3167 send_hello_context->peer->daemon->server = NULL;
3169 if (send_hello_context->peer->daemon->th != NULL)
3171 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3172 send_hello_context->peer->daemon->th = NULL;
3174 if (send_hello_context->core_connect_task != GNUNET_SCHEDULER_NO_TASK)
3176 GNUNET_SCHEDULER_cancel(send_hello_context->core_connect_task);
3177 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3179 send_hello_context->pg->outstanding_connects--;
3180 GNUNET_free(send_hello_context);
3184 * For peers that haven't yet connected, notify
3185 * the caller that they have failed (timeout).
3187 * @param cls the 'struct SendHelloContext *'
3188 * @param tc scheduler context
3191 notify_remaining_connections_failed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3193 struct SendHelloContext *send_hello_context = cls;
3194 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3195 struct PeerConnection *connection;
3197 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3198 send_hello_context->peer->daemon->server = NULL;
3200 connection = send_hello_context->peer->connect_peers_head;
3202 while (connection != NULL)
3204 if (pg->notify_connection != NULL)
3206 pg->notify_connection(pg->notify_connection_cls,
3207 &send_hello_context->peer->daemon->id,
3208 &pg->peers[connection->index].daemon->id,
3210 send_hello_context->peer->daemon->cfg,
3211 pg->peers[connection->index].daemon->cfg,
3212 send_hello_context->peer->daemon,
3213 pg->peers[connection->index].daemon,
3214 "Peers failed to connect (timeout)");
3216 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3217 GNUNET_free(connection);
3218 connection = connection->next;
3220 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3222 other_peer = &pg->peers[connection->index];
3227 * For peers that haven't yet connected, send
3228 * CORE connect requests.
3230 * @param cls the 'struct SendHelloContext *'
3231 * @param tc scheduler context
3234 send_core_connect_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3236 struct SendHelloContext *send_hello_context = cls;
3237 struct PeerConnection *conn;
3238 GNUNET_assert(send_hello_context->peer->daemon->server != NULL);
3240 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3242 send_hello_context->connect_attempts++;
3243 if (send_hello_context->connect_attempts < send_hello_context->pg->ct_ctx.connect_attempts)
3245 conn = send_hello_context->peer->connect_peers_head;
3246 while (conn != NULL)
3248 GNUNET_CORE_peer_request_connect(send_hello_context->peer->daemon->server,
3249 GNUNET_TIME_relative_get_forever(),
3250 &send_hello_context->pg->peers[conn->index].daemon->id,
3255 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) ,
3256 &send_core_connect_requests,
3257 send_hello_context);
3261 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Timeout before all connections created, marking rest as failed!\n");
3262 GNUNET_SCHEDULER_add_now(¬ify_remaining_connections_failed, send_hello_context);
3268 * Success, connection is up. Signal client our success.
3270 * @param cls our "struct SendHelloContext"
3271 * @param peer identity of the peer that has connected
3272 * @param atsi performance information
3274 * FIXME: remove peers from BOTH lists, call notify twice, should
3275 * double the speed of connections as long as the list iteration
3276 * doesn't take too long!
3279 core_connect_notify (void *cls,
3280 const struct GNUNET_PeerIdentity *peer,
3281 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3283 struct SendHelloContext *send_hello_context = cls;
3284 struct PeerConnection *connection;
3285 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3287 struct PeerData *other_peer;
3290 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3291 "Connected peer %s to peer %s\n",
3292 ctx->d1->shortname, GNUNET_i2s(peer));
3295 if (0 == memcmp(&send_hello_context->peer->daemon->id, peer, sizeof(struct GNUNET_PeerIdentity)))
3298 connection = send_hello_context->peer->connect_peers_head;
3303 while ((connection != NULL) &&
3304 (0 != memcmp(&pg->peers[connection->index].daemon->id, peer, sizeof(struct GNUNET_PeerIdentity))))
3306 connection = connection->next;
3309 if (connection == NULL)
3311 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);
3316 other_peer = &pg->peers[connection->index];
3318 if (pg->notify_connection != NULL)
3320 pg->notify_connection(pg->notify_connection_cls,
3321 &send_hello_context->peer->daemon->id,
3324 send_hello_context->peer->daemon->cfg,
3325 pg->peers[connection->index].daemon->cfg,
3326 send_hello_context->peer->daemon,
3327 pg->peers[connection->index].daemon,
3330 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3331 GNUNET_free(connection);
3335 /* Notify of reverse connection and remove from other peers list of outstanding */
3336 if (other_peer != NULL)
3338 connection = other_peer->connect_peers_head;
3339 while ((connection != NULL) &&
3340 (0 != memcmp(&send_hello_context->peer->daemon->id, &pg->peers[connection->index].daemon->id, sizeof(struct GNUNET_PeerIdentity))))
3342 connection = connection->next;
3344 if (connection != NULL)
3346 if (pg->notify_connection != NULL)
3348 pg->notify_connection(pg->notify_connection_cls,
3350 &send_hello_context->peer->daemon->id,
3352 pg->peers[connection->index].daemon->cfg,
3353 send_hello_context->peer->daemon->cfg,
3354 pg->peers[connection->index].daemon,
3355 send_hello_context->peer->daemon,
3359 GNUNET_CONTAINER_DLL_remove(other_peer->connect_peers_head, other_peer->connect_peers_tail, connection);
3360 GNUNET_free(connection);
3365 if (send_hello_context->peer->connect_peers_head == NULL)
3367 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3372 * Notify of a successful connection to the core service.
3374 * @param cls a struct SendHelloContext *
3375 * @param server handle to the core service
3376 * @param my_identity the peer identity of this peer
3377 * @param publicKey the public key of the peer
3380 core_init (void *cls,
3381 struct GNUNET_CORE_Handle * server,
3382 const struct GNUNET_PeerIdentity *
3385 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
3388 struct SendHelloContext *send_hello_context = cls;
3389 send_hello_context->core_ready = GNUNET_YES;
3393 * Function called once a hello has been sent
3394 * to the transport, move on to the next one
3395 * or go away forever.
3397 * @param cls the 'struct SendHelloContext *'
3398 * @param tc scheduler context
3401 hello_sent_callback (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3403 struct SendHelloContext *send_hello_context = cls;
3404 //unsigned int pg_iter;
3405 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3407 GNUNET_free(send_hello_context);
3411 send_hello_context->pg->remaining_hellos--;
3413 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sent HELLO, have %d remaining!\n", send_hello_context->pg->remaining_hellos);
3415 if (send_hello_context->peer_pos == NULL) /* All HELLOs (for this peer!) have been transmitted! */
3418 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All hellos for this peer sent, disconnecting transport!\n");
3420 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3421 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3422 send_hello_context->peer->daemon->th = NULL;
3424 /*if (send_hello_context->pg->remaining_hellos == 0)
3426 for (pg_iter = 0; pg_iter < send_hello_context->pg->max_outstanding_connections; pg_iter++)
3428 preschedule_connect(&send_hello_context->pg->ct_ctx);
3432 GNUNET_assert (send_hello_context->peer->daemon->server == NULL);
3433 send_hello_context->peer->daemon->server = GNUNET_CORE_connect(send_hello_context->peer->cfg,
3437 &core_connect_notify,
3444 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),
3445 &send_core_connect_requests,
3446 send_hello_context);
3449 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3453 * Connect to a peer, give it all the HELLO's of those peers
3454 * we will later ask it to connect to.
3456 * @param ct_ctx the overall connection context
3458 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3460 struct SendHelloContext *send_hello_context = cls;
3461 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3463 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3465 GNUNET_free(send_hello_context);
3469 GNUNET_assert(send_hello_context->peer_pos != NULL); /* All of the HELLO sends to be scheduled have been scheduled! */
3471 if (((send_hello_context->peer->daemon->th == NULL) &&
3472 (pg->outstanding_connects > pg->max_outstanding_connections)) ||
3473 (pg->stop_connects == GNUNET_YES))
3475 #if VERBOSE_TESTING > 2
3476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3478 ("Delaying connect, we have too many outstanding connections!\n"));
3480 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3481 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3482 &schedule_send_hellos, send_hello_context);
3486 #if VERBOSE_TESTING > 2
3487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3488 _("Creating connection, outstanding_connections is %d\n"),
3489 outstanding_connects);
3491 if (send_hello_context->peer->daemon->th == NULL)
3493 pg->outstanding_connects++; /* Actual TRANSPORT, CORE connections! */
3494 send_hello_context->peer->daemon->th = GNUNET_TRANSPORT_connect(send_hello_context->peer->cfg,
3502 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3503 _("Offering Hello of peer %s to peer %s\n"),
3504 send_hello_context->peer->daemon->shortname, pg->peers[send_hello_context->peer_pos->index].daemon->shortname);
3506 GNUNET_TRANSPORT_offer_hello(send_hello_context->peer->daemon->th,
3507 (const struct GNUNET_MessageHeader *)pg->peers[send_hello_context->peer_pos->index].daemon->hello,
3508 &hello_sent_callback,
3509 send_hello_context);
3510 send_hello_context->peer_pos = send_hello_context->peer_pos->next;
3511 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3517 * Internal notification of a connection, kept so that we can ensure some connections
3518 * happen instead of flooding all testing daemons with requests to connect.
3521 internal_connect_notify(void *cls, const struct GNUNET_PeerIdentity *first,
3522 const struct GNUNET_PeerIdentity *second,
3524 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
3525 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
3526 struct GNUNET_TESTING_Daemon *first_daemon,
3527 struct GNUNET_TESTING_Daemon *second_daemon,
3530 struct ConnectContext *connect_ctx = cls;
3531 struct ConnectTopologyContext *ct_ctx = connect_ctx->ct_ctx;
3532 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
3533 struct PeerConnection *connection;
3534 pg->outstanding_connects--;
3537 * Check whether the inverse connection has been scheduled yet,
3538 * if not, we can remove it from the other peers list and avoid
3539 * even trying to connect them again!
3541 connection = pg->peers[connect_ctx->second_index].connect_peers_head;
3546 while ((connection != NULL) && (0
3547 != memcmp (first, &pg->peers[connection->index].daemon->id,
3548 sizeof(struct GNUNET_PeerIdentity))))
3550 connection = connection->next;
3553 if (connection != NULL) /* Can safely remove! */
3555 ct_ctx->remaining_connections--;
3556 if (pg->notify_connection != NULL) /* Notify of reverse connection */
3557 pg->notify_connection (pg->notify_connection_cls, second, first,
3558 distance, second_cfg, first_cfg, second_daemon,
3559 first_daemon, emsg);
3561 GNUNET_CONTAINER_DLL_remove(pg->peers[connect_ctx->second_index].connect_peers_head, pg->peers[connect_ctx->second_index].connect_peers_tail, connection);
3562 GNUNET_free(connection);
3565 if (ct_ctx->remaining_connections == 0)
3567 if (ct_ctx->notify_connections_done != NULL)
3568 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
3571 preschedule_connect (pg);
3573 if (pg->notify_connection != NULL)
3574 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
3575 first_cfg, second_cfg, first_daemon, second_daemon,
3578 GNUNET_free(connect_ctx);
3582 * Either delay a connection (because there are too many outstanding)
3583 * or schedule it for right now.
3585 * @param cls a connection context
3586 * @param tc the task runtime context
3589 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3591 struct ConnectContext *connect_context = cls;
3592 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
3594 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3597 if ((pg->outstanding_connects > pg->max_outstanding_connections)
3598 || (pg->stop_connects == GNUNET_YES))
3601 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3603 ("Delaying connect, we have too many outstanding connections!\n"));
3605 GNUNET_SCHEDULER_add_delayed (
3606 GNUNET_TIME_relative_multiply (
3607 GNUNET_TIME_UNIT_MILLISECONDS,
3609 &schedule_connect, connect_context);
3614 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3615 _("Creating connection, outstanding_connections is %d (max %d)\n"),
3616 pg->outstanding_connects, pg->max_outstanding_connections);
3618 pg->outstanding_connects++;
3619 pg->total_connects_scheduled++;
3620 GNUNET_TESTING_daemons_connect (
3621 pg->peers[connect_context->first_index].daemon,
3622 pg->peers[connect_context->second_index].daemon,
3623 connect_context->ct_ctx->connect_timeout,
3624 connect_context->ct_ctx->connect_attempts,
3630 &internal_connect_notify, connect_context); /* FIXME: free connect context! */
3636 * Iterator for actually scheduling connections to be created
3637 * between two peers.
3639 * @param cls closure, a GNUNET_TESTING_Daemon
3640 * @param key the key the second Daemon was stored under
3641 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3643 * @return GNUNET_YES to continue iteration
3646 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3648 struct ConnectTopologyContext *ct_ctx = cls;
3649 struct PeerData *first = ct_ctx->first;
3650 struct GNUNET_TESTING_Daemon *second = value;
3651 struct ConnectContext *connect_context;
3653 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3654 connect_context->first = first->daemon;
3655 connect_context->second = second;
3656 connect_context->ct_ctx = ct_ctx;
3657 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3665 * Iterator for copying all entries in the allowed hashmap to the
3668 * @param cls closure, a GNUNET_TESTING_Daemon
3669 * @param key the key the second Daemon was stored under
3670 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3672 * @return GNUNET_YES to continue iteration
3675 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3677 struct PeerData *first = cls;
3679 GNUNET_assert (GNUNET_OK ==
3680 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
3682 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3689 * Make the peers to connect the same as those that are allowed to be
3692 * @param pg the peer group
3695 copy_allowed_topology(struct GNUNET_TESTING_PeerGroup *pg)
3697 unsigned int pg_iter;
3701 struct PeerConnection *iter;
3705 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3708 iter = pg->peers[pg_iter].allowed_peers_head;
3709 while (iter != NULL)
3711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3712 "Creating connection between %d and %d\n", pg_iter,
3714 total += add_connections (pg, pg_iter, iter->index, CONNECT,
3716 //total += add_actual_connections(pg, pg_iter, iter->index);
3721 GNUNET_CONTAINER_multihashmap_iterate (pg->
3722 peers[pg_iter].allowed_peers,
3723 ©_topology_iterator,
3724 &pg->peers[pg_iter]);
3726 if (GNUNET_SYSERR == ret)
3727 return GNUNET_SYSERR;
3729 total = total + ret;
3736 * Connect the topology as specified by the PeerConnection's
3737 * of each peer in the peer group
3739 * @param pg the peer group we are dealing with
3740 * @param connect_timeout how long try connecting two peers
3741 * @param connect_attempts how many times (max) to attempt
3742 * @param notify_callback callback to notify when finished
3743 * @param notify_cls closure for notify callback
3745 * @return the number of connections that will be attempted
3748 connect_topology(struct GNUNET_TESTING_PeerGroup *pg,
3749 struct GNUNET_TIME_Relative connect_timeout,
3750 unsigned int connect_attempts,
3751 GNUNET_TESTING_NotifyCompletion notify_callback,
3754 unsigned int pg_iter;
3758 struct PeerConnection *connection_iter;
3761 struct SendHelloContext *send_hello_context
3765 pg->ct_ctx.notify_connections_done = notify_callback;
3766 pg->ct_ctx.notify_cls = notify_cls;
3769 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3772 connection_iter = pg->peers[pg_iter].connect_peers_head;
3773 while (connection_iter != NULL)
3775 connection_iter = connection_iter->next;
3780 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3787 pg->ct_ctx.connect_timeout = connect_timeout;
3788 pg->ct_ctx.connect_attempts = connect_attempts;
3789 pg->ct_ctx.remaining_connections = total;
3792 /* First give all peers the HELLO's of other peers (connect to first peer's transport service, give HELLO's of other peers, continue...) */
3793 pg->remaining_hellos = total;
3794 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3796 send_hello_context = GNUNET_malloc(sizeof(struct SendHelloContext));
3797 send_hello_context->peer = &pg->peers[pg_iter];
3798 send_hello_context->peer_pos = pg->peers[pg_iter].connect_peers_head;
3799 send_hello_context->pg = pg;
3800 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3803 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3805 preschedule_connect (pg);
3813 * Takes a peer group and creates a topology based on the
3814 * one specified. Creates a topology means generates friend
3815 * files for the peers so they can only connect to those allowed
3816 * by the topology. This will only have an effect once peers
3817 * are started if the FRIENDS_ONLY option is set in the base
3818 * config. Also takes an optional restrict topology which
3819 * disallows connections based on particular transports
3820 * UNLESS they are specified in the restricted topology.
3822 * @param pg the peer group struct representing the running peers
3823 * @param topology which topology to connect the peers in
3824 * @param restrict_topology disallow restrict_transports transport
3825 * connections to peers NOT in this topology
3826 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3827 * @param restrict_transports space delimited list of transports to blacklist
3828 * to create restricted topology
3830 * @return the maximum number of connections were all allowed peers
3831 * connected to each other
3834 GNUNET_TESTING_create_topology(struct GNUNET_TESTING_PeerGroup *pg,
3835 enum GNUNET_TESTING_Topology topology,
3836 enum GNUNET_TESTING_Topology restrict_topology,
3837 const char *restrict_transports)
3841 unsigned int num_connections;
3842 int unblacklisted_connections;
3844 struct PeerConnection *conn_iter;
3845 struct PeerConnection *temp_conn;
3850 for (i = 0; i < pg->total; i++)
3852 pg->peers[i].allowed_peers =
3853 GNUNET_CONTAINER_multihashmap_create (100);
3854 pg->peers[i].connect_peers =
3855 GNUNET_CONTAINER_multihashmap_create (100);
3856 pg->peers[i].blacklisted_peers =
3857 GNUNET_CONTAINER_multihashmap_create (100);
3858 pg->peers[i].pg = pg;
3864 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3868 num_connections = create_clique (pg, &add_connections, ALLOWED, GNUNET_NO);
3870 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3873 _("Creating small world (ring) topology\n"));
3875 num_connections = create_small_world_ring (pg, &add_connections, ALLOWED);
3877 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3880 _("Creating small world (2d-torus) topology\n"));
3882 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3884 case GNUNET_TESTING_TOPOLOGY_RING:
3886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3888 num_connections = create_ring (pg, &add_connections, ALLOWED);
3890 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3894 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3896 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3899 _("Creating Erdos-Renyi topology\n"));
3901 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3903 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3907 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3909 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3912 _("Creating Scale Free topology\n"));
3914 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3916 case GNUNET_TESTING_TOPOLOGY_LINE:
3918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3919 _("Creating straight line topology\n"));
3921 num_connections = create_line (pg, &add_connections, ALLOWED);
3923 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3926 _("Creating topology from file!\n"));
3928 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing",
3931 num_connections = create_from_file (pg, filename, &add_connections,
3936 GNUNET_ERROR_TYPE_WARNING,
3937 "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3938 num_connections = 0;
3941 case GNUNET_TESTING_TOPOLOGY_NONE:
3943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3945 ("Creating no allowed topology (all peers can connect at core level)\n"));
3947 num_connections = pg->total * pg->total; /* Clique is allowed! */
3950 num_connections = 0;
3954 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING",
3957 ret = create_and_copy_friend_files (pg);
3958 if (ret != GNUNET_OK)
3961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3962 _("Failed during friend file copying!\n"));
3964 return GNUNET_SYSERR;
3969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3970 _("Friend files created/copied successfully!\n"));
3975 /* Use the create clique method to initially set all connections as blacklisted. */
3976 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology
3977 != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
3978 create_clique (pg, &add_connections, BLACKLIST, GNUNET_NO);
3980 unblacklisted_connections = 0;
3981 /* Un-blacklist connections as per the topology specified */
3982 switch (restrict_topology)
3984 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3987 _("Blacklisting all but clique topology\n"));
3989 unblacklisted_connections = create_clique (pg, &remove_connections,
3990 BLACKLIST, GNUNET_NO);
3992 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3995 _("Blacklisting all but small world (ring) topology\n"));
3997 unblacklisted_connections = create_small_world_ring (pg,
3998 &remove_connections,
4001 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4005 ("Blacklisting all but small world (2d-torus) topology\n"));
4007 unblacklisted_connections = create_small_world (pg, &remove_connections,
4010 case GNUNET_TESTING_TOPOLOGY_RING:
4012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4013 _("Blacklisting all but ring topology\n"));
4015 unblacklisted_connections
4016 = create_ring (pg, &remove_connections, BLACKLIST);
4018 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4021 _("Blacklisting all but 2d torus topology\n"));
4023 unblacklisted_connections = create_2d_torus (pg, &remove_connections,
4026 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4029 _("Blacklisting all but Erdos-Renyi topology\n"));
4031 unblacklisted_connections = create_erdos_renyi (pg, &remove_connections,
4034 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4037 _("Blacklisting all but InterNAT topology\n"));
4041 for (off = 0; off < pg->total; off++)
4043 conn_iter = pg->peers[off].allowed_peers_head;
4044 while (conn_iter != NULL)
4046 temp_conn = conn_iter->next;
4047 GNUNET_free(conn_iter);
4048 conn_iter = temp_conn;
4050 pg->peers[off].allowed_peers_head = NULL;
4051 pg->peers[off].allowed_peers_tail = NULL;
4053 conn_iter = pg->peers[off].connect_peers_head;
4054 while (conn_iter != NULL)
4056 temp_conn = conn_iter->next;
4057 GNUNET_free(conn_iter);
4058 conn_iter = temp_conn;
4060 pg->peers[off].connect_peers_head = NULL;
4061 pg->peers[off].connect_peers_tail = NULL;
4063 unblacklisted_connections
4064 = create_nated_internet_copy (pg, &remove_connections, BLACKLIST);
4066 unblacklisted_connections =
4067 create_nated_internet (pg, &remove_connections, BLACKLIST);
4071 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4074 _("Blacklisting all but Scale Free topology\n"));
4076 unblacklisted_connections = create_scale_free (pg, &remove_connections,
4079 case GNUNET_TESTING_TOPOLOGY_LINE:
4081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4082 _("Blacklisting all but straight line topology\n"));
4084 unblacklisted_connections
4085 = create_line (pg, &remove_connections, BLACKLIST);
4087 case GNUNET_TESTING_TOPOLOGY_NONE: /* Fall through */
4088 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
4090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4092 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
4094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
4095 ("Creating blacklist topology from allowed\n"));
4096 unblacklisted_connections = copy_allowed (pg, &remove_connections);
4101 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
4103 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n",
4104 restrict_transports);
4105 ret = create_and_copy_blacklist_files (pg, restrict_transports);
4106 if (ret != GNUNET_OK)
4109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4110 _("Failed during blacklist file copying!\n"));
4117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4118 _("Blacklist files created/copied successfully!\n"));
4122 return num_connections;
4127 * Iterator for choosing random peers to connect.
4129 * @param cls closure, a RandomContext
4130 * @param key the key the second Daemon was stored under
4131 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4133 * @return GNUNET_YES to continue iteration
4136 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4138 struct RandomContext *random_ctx = cls;
4139 double random_number;
4140 uint32_t second_pos;
4141 GNUNET_HashCode first_hash;
4144 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4145 UINT64_MAX)) / ((double) UINT64_MAX);
4146 if (random_number < random_ctx->percentage)
4148 GNUNET_assert (GNUNET_OK ==
4149 GNUNET_CONTAINER_multihashmap_put (random_ctx->
4150 first->connect_peers_working_set,
4152 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4155 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
4156 uid_from_hash (key, &second_pos);
4157 hash_from_uid (random_ctx->first_uid, &first_hash);
4158 GNUNET_assert (random_ctx->pg->total > second_pos);
4159 GNUNET_assert (GNUNET_YES ==
4160 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
4162 [second_pos].connect_peers,
4171 * Iterator for adding at least X peers to a peers connection set.
4173 * @param cls closure, MinimumContext
4174 * @param key the key the second Daemon was stored under
4175 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4177 * @return GNUNET_YES to continue iteration
4180 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4182 struct MinimumContext *min_ctx = cls;
4183 uint32_t second_pos;
4184 GNUNET_HashCode first_hash;
4187 if (GNUNET_CONTAINER_multihashmap_size
4188 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
4190 for (i = 0; i < min_ctx->num_to_add; i++)
4192 if (min_ctx->pg_array[i] == min_ctx->current)
4194 GNUNET_assert (GNUNET_OK ==
4195 GNUNET_CONTAINER_multihashmap_put
4196 (min_ctx->first->connect_peers_working_set, key,
4198 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4199 uid_from_hash (key, &second_pos);
4200 hash_from_uid (min_ctx->first_uid, &first_hash);
4201 GNUNET_assert (min_ctx->pg->total > second_pos);
4202 GNUNET_assert (GNUNET_OK ==
4203 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4205 [second_pos].connect_peers_working_set,
4209 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4210 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
4211 GNUNET_assert (GNUNET_YES ==
4212 GNUNET_CONTAINER_multihashmap_remove
4213 (min_ctx->pg->peers[second_pos].connect_peers,
4214 &first_hash, min_ctx->first->daemon));
4221 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
4226 * Iterator for adding peers to a connection set based on a depth first search.
4228 * @param cls closure, MinimumContext
4229 * @param key the key the second daemon was stored under
4230 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4232 * @return GNUNET_YES to continue iteration
4235 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4237 struct DFSContext *dfs_ctx = cls;
4238 GNUNET_HashCode first_hash;
4240 if (dfs_ctx->current == dfs_ctx->chosen)
4242 GNUNET_assert (GNUNET_OK ==
4243 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4244 first->connect_peers_working_set,
4246 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4247 uid_from_hash (key, &dfs_ctx->second_uid);
4248 hash_from_uid (dfs_ctx->first_uid, &first_hash);
4249 GNUNET_assert (GNUNET_OK ==
4250 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4252 [dfs_ctx->second_uid].connect_peers_working_set,
4256 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4257 GNUNET_assert (GNUNET_YES ==
4258 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
4260 [dfs_ctx->second_uid].connect_peers,
4264 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
4265 return GNUNET_NO; /* We have found our peer, don't iterate more */
4274 * From the set of connections possible, choose percentage percent of connections
4275 * to actually connect.
4277 * @param pg the peergroup we are dealing with
4278 * @param percentage what percent of total connections to make
4281 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg,
4284 struct RandomContext random_ctx;
4287 struct PeerConnection *temp_peers;
4288 struct PeerConnection *conn_iter;
4289 double random_number;
4292 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4294 random_ctx.first_uid = pg_iter;
4295 random_ctx.first = &pg->peers[pg_iter];
4296 random_ctx.percentage = percentage;
4300 conn_iter = pg->peers[pg_iter].connect_peers_head;
4301 while (conn_iter != NULL)
4304 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4306 / ((double) UINT64_MAX);
4307 if (random_number < percentage)
4309 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4312 conn_iter = conn_iter->next;
4315 pg->peers[pg_iter].connect_peers_working_set =
4316 GNUNET_CONTAINER_multihashmap_create (pg->total);
4317 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4318 &random_connect_iterator,
4320 /* Now remove the old connections */
4321 GNUNET_CONTAINER_multihashmap_destroy (pg->
4322 peers[pg_iter].connect_peers);
4323 /* And replace with the random set */
4324 pg->peers[pg_iter].connect_peers =
4325 pg->peers[pg_iter].connect_peers_working_set;
4329 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4331 conn_iter = pg->peers[pg_iter].connect_peers_head;
4332 while (pg->peers[pg_iter].connect_peers_head != NULL)
4333 remove_connections (pg, pg_iter,
4334 pg->peers[pg_iter].connect_peers_head->index,
4335 CONNECT, GNUNET_YES);
4337 pg->peers[pg_iter].connect_peers_head
4338 = pg->peers[pg_iter].connect_peers_working_set_head;
4339 pg->peers[pg_iter].connect_peers_tail
4340 = pg->peers[pg_iter].connect_peers_working_set_tail;
4341 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4342 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4347 * Count the number of connections in a linked list of connections.
4349 * @param conn_list the connection list to get the count of
4351 * @return the number of elements in the list
4354 count_connections(struct PeerConnection *conn_list)
4356 struct PeerConnection *iter;
4360 while (iter != NULL)
4369 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
4372 unsigned int pg_iter;
4374 struct PeerConnection *conn_iter;
4378 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4381 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
4382 while (conn_iter != NULL)
4385 conn_iter = conn_iter->next;
4389 GNUNET_CONTAINER_multihashmap_size (pg->
4391 [pg_iter].connect_peers_working_set);
4399 count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
4402 unsigned int pg_iter;
4404 struct PeerConnection *conn_iter;
4408 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4411 conn_iter = pg->peers[pg_iter].allowed_peers_head;
4412 while (conn_iter != NULL)
4415 conn_iter = conn_iter->next;
4419 GNUNET_CONTAINER_multihashmap_size (pg->
4421 [pg_iter].allowed_peers);
4429 * From the set of connections possible, choose at least num connections per
4432 * @param pg the peergroup we are dealing with
4433 * @param num how many connections at least should each peer have (if possible)?
4436 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4439 struct MinimumContext minimum_ctx;
4441 struct PeerConnection *conn_iter;
4442 unsigned int temp_list_size;
4445 uint32_t random; /* Random list entry to connect peer to */
4450 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4453 = count_connections (pg->peers[pg_iter].connect_peers_head);
4454 if (temp_list_size == 0)
4456 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4457 "Peer %d has 0 connections!?!?\n", pg_iter);
4460 for (i = 0; i < num; i++)
4462 random = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4464 conn_iter = pg->peers[pg_iter].connect_peers_head;
4465 for (count = 0; count < random; count++)
4466 conn_iter = conn_iter->next;
4467 /* We now have a random connection, connect it! */
4468 GNUNET_assert(conn_iter != NULL);
4469 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4474 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4476 pg->peers[pg_iter].connect_peers_working_set =
4477 GNUNET_CONTAINER_multihashmap_create (num);
4480 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4482 minimum_ctx.first_uid = pg_iter;
4483 minimum_ctx.pg_array =
4484 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
4485 GNUNET_CONTAINER_multihashmap_size
4486 (pg->peers[pg_iter].connect_peers));
4487 minimum_ctx.first = &pg->peers[pg_iter];
4488 minimum_ctx.pg = pg;
4489 minimum_ctx.num_to_add = num;
4490 minimum_ctx.current = 0;
4491 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4492 &minimum_connect_iterator,
4496 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4498 /* Remove the "old" connections */
4499 GNUNET_CONTAINER_multihashmap_destroy (pg->
4500 peers[pg_iter].connect_peers);
4501 /* And replace with the working set */
4502 pg->peers[pg_iter].connect_peers =
4503 pg->peers[pg_iter].connect_peers_working_set;
4506 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4508 while (pg->peers[pg_iter].connect_peers_head != NULL)
4510 conn_iter = pg->peers[pg_iter].connect_peers_head;
4511 GNUNET_CONTAINER_DLL_remove(pg->peers[pg_iter].connect_peers_head,
4512 pg->peers[pg_iter].connect_peers_tail,
4514 GNUNET_free(conn_iter);
4515 /*remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT, GNUNET_YES);*/
4518 pg->peers[pg_iter].connect_peers_head
4519 = pg->peers[pg_iter].connect_peers_working_set_head;
4520 pg->peers[pg_iter].connect_peers_tail
4521 = pg->peers[pg_iter].connect_peers_working_set_tail;
4522 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4523 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4528 struct FindClosestContext
4531 * The currently known closest peer.
4533 struct GNUNET_TESTING_Daemon *closest;
4536 * The info for the peer we are adding connections for.
4538 struct PeerData *curr_peer;
4541 * The distance (bits) between the current
4542 * peer and the currently known closest.
4544 unsigned int closest_dist;
4547 * The offset of the closest known peer in
4550 unsigned int closest_num;
4554 * Iterator over hash map entries of the allowed
4555 * peer connections. Find the closest, not already
4556 * connected peer and return it.
4558 * @param cls closure (struct FindClosestContext)
4559 * @param key current key code (hash of offset in pg)
4560 * @param value value in the hash map - a GNUNET_TESTING_Daemon
4561 * @return GNUNET_YES if we should continue to
4566 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
4568 struct FindClosestContext *closest_ctx = cls;
4569 struct GNUNET_TESTING_Daemon *daemon = value;
4571 if (((closest_ctx->closest == NULL) ||
4572 (GNUNET_CRYPTO_hash_matching_bits
4573 (&daemon->id.hashPubKey,
4574 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
4575 closest_ctx->closest_dist))
4577 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
4578 curr_peer->connect_peers,
4581 closest_ctx->closest_dist =
4582 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
4583 &closest_ctx->curr_peer->daemon->
4585 closest_ctx->closest = daemon;
4586 uid_from_hash (key, &closest_ctx->closest_num);
4592 * From the set of connections possible, choose at num connections per
4593 * peer based on depth which are closest out of those allowed. Guaranteed
4594 * to add num peers to connect to, provided there are that many peers
4595 * in the underlay topology to connect to.
4597 * @param pg the peergroup we are dealing with
4598 * @param num how many connections at least should each peer have (if possible)?
4599 * @param proc processor to actually add the connections
4600 * @param list the peer list to use
4603 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
4604 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
4609 struct FindClosestContext closest_ctx;
4614 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
4616 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4618 closest_ctx.curr_peer = &pg->peers[pg_iter];
4619 closest_ctx.closest = NULL;
4620 closest_ctx.closest_dist = 0;
4621 closest_ctx.closest_num = 0;
4622 GNUNET_CONTAINER_multihashmap_iterate (pg->
4623 peers[pg_iter].allowed_peers,
4624 &find_closest_peers,
4626 if (closest_ctx.closest != NULL)
4628 GNUNET_assert (closest_ctx.closest_num < pg->total);
4629 proc (pg, pg_iter, closest_ctx.closest_num, list);
4637 * From the set of connections possible, choose at least num connections per
4638 * peer based on depth first traversal of peer connections. If DFS leaves
4639 * peers unconnected, ensure those peers get connections.
4641 * @param pg the peergroup we are dealing with
4642 * @param num how many connections at least should each peer have (if possible)?
4645 perform_dfs(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4649 uint32_t starting_peer;
4650 uint32_t least_connections;
4651 uint32_t random_connection;
4653 unsigned int temp_count;
4654 struct PeerConnection *peer_iter;
4656 struct DFSContext dfs_ctx;
4657 GNUNET_HashCode second_hash;
4663 while ((count_workingset_connections (pg) < num * pg->total)
4664 && (count_allowed_connections (pg) > 0))
4666 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4668 least_connections = -1; /* Set to very high number */
4669 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4672 = count_connections (
4673 pg->peers[pg_iter].connect_peers_working_set_head);
4674 if (temp_count < least_connections)
4676 starting_peer = pg_iter;
4677 least_connections = temp_count;
4683 = count_connections (pg->peers[starting_peer].connect_peers_head);
4684 if (temp_count == 0)
4685 continue; /* FIXME: infinite loop? */
4687 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4690 peer_iter = pg->peers[starting_peer].connect_peers_head;
4691 while (temp_count < random_connection)
4693 peer_iter = peer_iter->next;
4696 GNUNET_assert(peer_iter != NULL);
4697 add_connections (pg, starting_peer, peer_iter->index, WORKING_SET,
4699 remove_connections (pg, starting_peer, peer_iter->index, CONNECT,
4701 starting_peer = peer_iter->index;
4706 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4708 pg->peers[pg_iter].connect_peers_working_set =
4709 GNUNET_CONTAINER_multihashmap_create (num);
4714 while ((count_workingset_connections (pg) < num * pg->total)
4715 && (count_allowed_connections (pg) > 0))
4717 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4719 least_connections = -1; /* Set to very high number */
4720 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4722 if (GNUNET_CONTAINER_multihashmap_size
4723 (pg->peers[pg_iter].connect_peers_working_set) <
4726 starting_peer = pg_iter;
4728 GNUNET_CONTAINER_multihashmap_size (pg->
4730 [pg_iter].connect_peers_working_set);
4735 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4741 /* Choose a random peer from the chosen peers set of connections to add */
4743 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4744 GNUNET_CONTAINER_multihashmap_size
4745 (pg->peers[starting_peer].connect_peers));
4746 dfs_ctx.first_uid = starting_peer;
4747 dfs_ctx.first = &pg->peers[starting_peer];
4749 dfs_ctx.current = 0;
4751 GNUNET_CONTAINER_multihashmap_iterate (pg->
4753 [starting_peer].connect_peers,
4754 &dfs_connect_iterator, &dfs_ctx);
4755 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4756 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4757 GNUNET_assert (GNUNET_YES ==
4758 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4759 [starting_peer].connect_peers,
4763 [dfs_ctx.second_uid].daemon));
4764 starting_peer = dfs_ctx.second_uid;
4767 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4769 /* Remove the "old" connections */
4770 GNUNET_CONTAINER_multihashmap_destroy (pg->
4771 peers[pg_iter].connect_peers);
4772 /* And replace with the working set */
4773 pg->peers[pg_iter].connect_peers =
4774 pg->peers[pg_iter].connect_peers_working_set;
4780 * Internal callback for topology information for a particular peer.
4783 internal_topology_callback(void *cls, const struct GNUNET_PeerIdentity *peer,
4784 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4786 struct CoreContext *core_ctx = cls;
4787 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4789 if (peer == NULL) /* Either finished, or something went wrong */
4791 iter_ctx->completed++;
4792 iter_ctx->connected--;
4793 /* One core context allocated per iteration, must free! */
4794 GNUNET_free (core_ctx);
4798 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id, peer, NULL);
4801 if (iter_ctx->completed == iter_ctx->total)
4803 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4804 /* Once all are done, free the iteration context */
4805 GNUNET_free (iter_ctx);
4810 * Check running topology iteration tasks, if below max start a new one, otherwise
4811 * schedule for some time in the future.
4814 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4816 struct CoreContext *core_context = cls;
4817 struct TopologyIterateContext *topology_context =
4818 (struct TopologyIterateContext *) core_context->iter_context;
4819 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4822 if (topology_context->connected
4823 > topology_context->pg->max_outstanding_connections)
4825 #if VERBOSE_TESTING > 2
4826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4828 ("Delaying connect, we have too many outstanding connections!\n"));
4830 GNUNET_SCHEDULER_add_delayed (
4831 GNUNET_TIME_relative_multiply (
4832 GNUNET_TIME_UNIT_MILLISECONDS,
4834 &schedule_get_topology, core_context);
4838 #if VERBOSE_TESTING > 2
4839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4840 _("Creating connection, outstanding_connections is %d\n"),
4841 outstanding_connects);
4843 topology_context->connected++;
4845 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4846 &internal_topology_callback,
4849 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4850 internal_topology_callback (core_context, NULL, NULL);
4856 * Iterate over all (running) peers in the peer group, retrieve
4857 * all connections that each currently has.
4860 GNUNET_TESTING_get_topology(struct GNUNET_TESTING_PeerGroup *pg,
4861 GNUNET_TESTING_NotifyTopology cb, void *cls)
4863 struct TopologyIterateContext *topology_context;
4864 struct CoreContext *core_ctx;
4866 unsigned int total_count;
4868 /* Allocate a single topology iteration context */
4869 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4870 topology_context->topology_cb = cb;
4871 topology_context->cls = cls;
4872 topology_context->pg = pg;
4874 for (i = 0; i < pg->total; i++)
4876 if (pg->peers[i].daemon->running == GNUNET_YES)
4878 /* Allocate one core context per core we need to connect to */
4879 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4880 core_ctx->daemon = pg->peers[i].daemon;
4881 /* Set back pointer to topology iteration context */
4882 core_ctx->iter_context = topology_context;
4883 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4887 if (total_count == 0)
4889 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4890 GNUNET_free (topology_context);
4893 topology_context->total = total_count;
4898 * Callback function to process statistic values.
4899 * This handler is here only really to insert a peer
4900 * identity (or daemon) so the statistics can be uniquely
4901 * tied to a single running peer.
4903 * @param cls closure
4904 * @param subsystem name of subsystem that created the statistic
4905 * @param name the name of the datum
4906 * @param value the current value
4907 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4908 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4911 internal_stats_callback(void *cls, const char *subsystem, const char *name,
4912 uint64_t value, int is_persistent)
4914 struct StatsCoreContext *core_context = cls;
4915 struct StatsIterateContext *stats_context =
4916 (struct StatsIterateContext *) core_context->iter_context;
4918 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4919 subsystem, name, value, is_persistent);
4923 * Internal continuation call for statistics iteration.
4925 * @param cls closure, the CoreContext for this iteration
4926 * @param success whether or not the statistics iterations
4927 * was canceled or not (we don't care)
4930 internal_stats_cont(void *cls, int success)
4932 struct StatsCoreContext *core_context = cls;
4933 struct StatsIterateContext *stats_context =
4934 (struct StatsIterateContext *) core_context->iter_context;
4936 stats_context->connected--;
4937 stats_context->completed++;
4939 if (stats_context->completed == stats_context->total)
4941 stats_context->cont (stats_context->cls, GNUNET_YES);
4942 GNUNET_free (stats_context);
4945 if (core_context->stats_handle != NULL)
4946 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4948 GNUNET_free (core_context);
4952 * Check running topology iteration tasks, if below max start a new one, otherwise
4953 * schedule for some time in the future.
4956 schedule_get_statistics(void *cls,
4957 const struct GNUNET_SCHEDULER_TaskContext *tc)
4959 struct StatsCoreContext *core_context = cls;
4960 struct StatsIterateContext *stats_context =
4961 (struct StatsIterateContext *) core_context->iter_context;
4963 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4966 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
4968 #if VERBOSE_TESTING > 2
4969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4971 ("Delaying connect, we have too many outstanding connections!\n"));
4973 GNUNET_SCHEDULER_add_delayed (
4974 GNUNET_TIME_relative_multiply (
4975 GNUNET_TIME_UNIT_MILLISECONDS,
4977 &schedule_get_statistics, core_context);
4981 #if VERBOSE_TESTING > 2
4982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4983 _("Creating connection, outstanding_connections is %d\n"),
4984 outstanding_connects);
4987 stats_context->connected++;
4988 core_context->stats_handle
4989 = GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
4990 if (core_context->stats_handle == NULL)
4992 internal_stats_cont (core_context, GNUNET_NO);
4996 core_context->stats_get_handle
4997 = GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
4998 GNUNET_TIME_relative_get_forever (),
4999 &internal_stats_cont,
5000 &internal_stats_callback, core_context);
5001 if (core_context->stats_get_handle == NULL)
5002 internal_stats_cont (core_context, GNUNET_NO);
5007 struct DuplicateStats
5010 * Next item in the list
5012 struct DuplicateStats *next;
5015 * Nasty string, concatenation of relevant information.
5017 char *unique_string;
5021 * Check whether the combination of port/host/unix domain socket
5022 * already exists in the list of peers being checked for statistics.
5024 * @param pg the peergroup in question
5025 * @param specific_peer the peer we're concerned with
5026 * @param stats_list the list to return to the caller
5028 * @return GNUNET_YES if the statistics instance has been seen already,
5029 * GNUNET_NO if not (and we may have added it to the list)
5032 stats_check_existing(struct GNUNET_TESTING_PeerGroup *pg,
5033 struct PeerData *specific_peer,
5034 struct DuplicateStats **stats_list)
5036 struct DuplicateStats *pos;
5037 char *unix_domain_socket;
5038 unsigned long long port;
5041 != GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
5042 "single_statistics_per_host"))
5043 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
5046 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg,
5049 &unix_domain_socket))
5052 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg,
5053 "statistics", "port",
5056 GNUNET_free(unix_domain_socket);
5060 if (specific_peer->daemon->hostname != NULL)
5061 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
5062 unix_domain_socket, port);
5064 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
5068 if (0 == strcmp (to_match, pos->unique_string))
5070 GNUNET_free (unix_domain_socket);
5071 GNUNET_free (to_match);
5076 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
5077 pos->unique_string = to_match;
5078 pos->next = *stats_list;
5080 GNUNET_free (unix_domain_socket);
5085 * Iterate over all (running) peers in the peer group, retrieve
5086 * all statistics from each.
5089 GNUNET_TESTING_get_statistics(struct GNUNET_TESTING_PeerGroup *pg,
5090 GNUNET_STATISTICS_Callback cont,
5091 GNUNET_TESTING_STATISTICS_Iterator proc,
5094 struct StatsIterateContext *stats_context;
5095 struct StatsCoreContext *core_ctx;
5097 unsigned int total_count;
5098 struct DuplicateStats *stats_list;
5099 struct DuplicateStats *pos;
5102 /* Allocate a single stats iteration context */
5103 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
5104 stats_context->cont = cont;
5105 stats_context->proc = proc;
5106 stats_context->cls = cls;
5107 stats_context->pg = pg;
5110 for (i = 0; i < pg->total; i++)
5112 if ((pg->peers[i].daemon->running == GNUNET_YES) && (GNUNET_NO
5113 == stats_check_existing (pg, &pg->peers[i], &stats_list)))
5115 /* Allocate one core context per core we need to connect to */
5116 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
5117 core_ctx->daemon = pg->peers[i].daemon;
5118 /* Set back pointer to topology iteration context */
5119 core_ctx->iter_context = stats_context;
5120 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
5125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5126 "Retrieving stats from %u total instances.\n", total_count);
5127 stats_context->total = total_count;
5128 if (stats_list != NULL)
5133 GNUNET_free (pos->unique_string);
5134 stats_list = pos->next;
5136 pos = stats_list->next;
5143 * Stop the connection process temporarily.
5145 * @param pg the peer group to stop connecting
5148 GNUNET_TESTING_stop_connections(struct GNUNET_TESTING_PeerGroup *pg)
5150 pg->stop_connects = GNUNET_YES;
5154 * Resume the connection process temporarily.
5156 * @param pg the peer group to resume connecting
5159 GNUNET_TESTING_resume_connections(struct GNUNET_TESTING_PeerGroup *pg)
5161 pg->stop_connects = GNUNET_NO;
5165 * There are many ways to connect peers that are supported by this function.
5166 * To connect peers in the same topology that was created via the
5167 * GNUNET_TESTING_create_topology, the topology variable must be set to
5168 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
5169 * a new instance of that topology will be generated and attempted to be
5170 * connected. This could result in some connections being impossible,
5171 * because some topologies are non-deterministic.
5173 * @param pg the peer group struct representing the running peers
5174 * @param topology which topology to connect the peers in
5175 * @param options options for connecting the topology
5176 * @param option_modifier modifier for options that take a parameter
5177 * @param connect_timeout how long to wait before giving up on connecting
5179 * @param connect_attempts how many times to attempt to connect two peers
5180 * over the connect_timeout duration
5181 * @param notify_callback notification to be called once all connections completed
5182 * @param notify_cls closure for notification callback
5184 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
5187 GNUNET_TESTING_connect_topology(
5188 struct GNUNET_TESTING_PeerGroup *pg,
5189 enum GNUNET_TESTING_Topology topology,
5190 enum GNUNET_TESTING_TopologyOption options,
5191 double option_modifier,
5192 struct GNUNET_TIME_Relative connect_timeout,
5193 unsigned int connect_attempts,
5194 GNUNET_TESTING_NotifyCompletion notify_callback,
5199 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
5200 #if VERBOSE_TOPOLOGY
5201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5202 _("Creating clique CONNECT topology\n"));
5204 create_clique (pg, &add_connections, CONNECT, GNUNET_NO);
5206 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
5207 #if VERBOSE_TOPOLOGY
5208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5209 _("Creating small world (ring) CONNECT topology\n"));
5211 create_small_world_ring (pg, &add_connections, CONNECT);
5213 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
5214 #if VERBOSE_TOPOLOGY
5215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5216 _("Creating small world (2d-torus) CONNECT topology\n"));
5218 create_small_world (pg, &add_connections, CONNECT);
5220 case GNUNET_TESTING_TOPOLOGY_RING:
5221 #if VERBOSE_TOPOLOGY
5222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring CONNECT topology\n"));
5224 create_ring (pg, &add_connections, CONNECT);
5226 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
5227 #if VERBOSE_TOPOLOGY
5228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5229 _("Creating 2d torus CONNECT topology\n"));
5231 create_2d_torus (pg, &add_connections, CONNECT);
5233 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
5234 #if VERBOSE_TOPOLOGY
5235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5236 _("Creating Erdos-Renyi CONNECT topology\n"));
5238 create_erdos_renyi (pg, &add_connections, CONNECT);
5240 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
5241 #if VERBOSE_TOPOLOGY
5242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5243 _("Creating InterNAT CONNECT topology\n"));
5245 create_nated_internet (pg, &add_connections, CONNECT);
5247 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
5248 #if VERBOSE_TOPOLOGY
5249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5250 _("Creating Scale Free CONNECT topology\n"));
5252 create_scale_free (pg, &add_connections, CONNECT);
5254 case GNUNET_TESTING_TOPOLOGY_LINE:
5255 #if VERBOSE_TOPOLOGY
5256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5257 _("Creating straight line CONNECT topology\n"));
5259 create_line (pg, &add_connections, CONNECT);
5261 case GNUNET_TESTING_TOPOLOGY_NONE:
5262 #if VERBOSE_TOPOLOGY
5263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating no CONNECT topology\n"));
5265 copy_allowed_topology (pg);
5268 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5269 ("Unknown topology specification, can't connect peers!\n"));
5270 return GNUNET_SYSERR;
5275 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
5276 #if VERBOSE_TOPOLOGY
5277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5278 ("Connecting random subset (%'.2f percent) of possible peers\n"), 100
5281 choose_random_connections (pg, option_modifier);
5283 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
5284 #if VERBOSE_TOPOLOGY
5285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5286 _("Connecting a minimum of %u peers each (if possible)\n"),
5287 (unsigned int) option_modifier);
5289 choose_minimum (pg, (unsigned int) option_modifier);
5291 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
5292 #if VERBOSE_TOPOLOGY
5293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5294 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
5295 (unsigned int) option_modifier);
5298 perform_dfs (pg, (int) option_modifier);
5301 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
5302 #if VERBOSE_TOPOLOGY
5303 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5304 ("Finding additional %u closest peers each (if possible)\n"),
5305 (unsigned int) option_modifier);
5308 add_closest (pg, (unsigned int) option_modifier,
5309 &add_connections, CONNECT);
5312 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
5314 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
5320 return connect_topology (pg, connect_timeout, connect_attempts,
5321 notify_callback, notify_cls);
5325 * Lookup and return the number of SSH connections to a host.
5327 * @param hostname the hostname to lookup in the list
5328 * @param pg the peergroup that the host belongs to
5330 * @return the number of current ssh connections to the host
5333 count_outstanding_at_host(const char *hostname,
5334 struct GNUNET_TESTING_PeerGroup *pg)
5336 struct OutstandingSSH *pos;
5338 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5340 GNUNET_assert(pos != NULL);
5341 return pos->outstanding;
5345 * Increment the number of SSH connections to a host by one.
5347 * @param hostname the hostname to lookup in the list
5348 * @param pg the peergroup that the host belongs to
5352 increment_outstanding_at_host(const char *hostname,
5353 struct GNUNET_TESTING_PeerGroup *pg)
5355 struct OutstandingSSH *pos;
5357 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5359 GNUNET_assert(pos != NULL);
5364 * Decrement the number of SSH connections to a host by one.
5366 * @param hostname the hostname to lookup in the list
5367 * @param pg the peergroup that the host belongs to
5371 decrement_outstanding_at_host(const char *hostname,
5372 struct GNUNET_TESTING_PeerGroup *pg)
5374 struct OutstandingSSH *pos;
5376 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5378 GNUNET_assert(pos != NULL);
5383 * Callback that is called whenever a hostkey is generated
5384 * for a peer. Call the real callback and decrement the
5385 * starting counter for the peergroup.
5387 * @param cls closure
5388 * @param id identifier for the daemon, NULL on error
5389 * @param d handle for the daemon
5390 * @param emsg error message (NULL on success)
5393 internal_hostkey_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5394 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5396 struct InternalStartContext *internal_context = cls;
5397 internal_context->peer->pg->starting--;
5398 internal_context->peer->pg->started++;
5399 if (internal_context->hostname != NULL)
5400 decrement_outstanding_at_host (internal_context->hostname,
5401 internal_context->peer->pg);
5402 if (internal_context->hostkey_callback != NULL)
5403 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
5405 else if (internal_context->peer->pg->started
5406 == internal_context->peer->pg->total)
5408 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
5409 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
5414 * Callback that is called whenever a peer has finished starting.
5415 * Call the real callback and decrement the starting counter
5416 * for the peergroup.
5418 * @param cls closure
5419 * @param id identifier for the daemon, NULL on error
5421 * @param d handle for the daemon
5422 * @param emsg error message (NULL on success)
5425 internal_startup_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5426 const struct GNUNET_CONFIGURATION_Handle *cfg,
5427 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5429 struct InternalStartContext *internal_context = cls;
5430 internal_context->peer->pg->starting--;
5431 if (internal_context->hostname != NULL)
5432 decrement_outstanding_at_host (internal_context->hostname,
5433 internal_context->peer->pg);
5434 if (internal_context->start_cb != NULL)
5435 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
5440 internal_continue_startup(void *cls,
5441 const struct GNUNET_SCHEDULER_TaskContext *tc)
5443 struct InternalStartContext *internal_context = cls;
5445 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
5450 if ((internal_context->peer->pg->starting
5451 < internal_context->peer->pg->max_concurrent_ssh)
5452 || ((internal_context->hostname != NULL)
5453 && (count_outstanding_at_host (internal_context->hostname,
5454 internal_context->peer->pg)
5455 < internal_context->peer->pg->max_concurrent_ssh)))
5457 if (internal_context->hostname != NULL)
5458 increment_outstanding_at_host (internal_context->hostname,
5459 internal_context->peer->pg);
5460 internal_context->peer->pg->starting++;
5461 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
5465 GNUNET_SCHEDULER_add_delayed (
5466 GNUNET_TIME_relative_multiply (
5467 GNUNET_TIME_UNIT_MILLISECONDS,
5469 &internal_continue_startup,
5475 * Callback for informing us about a successful
5476 * or unsuccessful churn start call.
5478 * @param cls a ChurnContext
5479 * @param id the peer identity of the started peer
5480 * @param cfg the handle to the configuration of the peer
5481 * @param d handle to the daemon for the peer
5482 * @param emsg NULL on success, non-NULL on failure
5486 churn_start_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5487 const struct GNUNET_CONFIGURATION_Handle *cfg,
5488 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5490 struct ChurnRestartContext *startup_ctx = cls;
5491 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
5493 unsigned int total_left;
5494 char *error_message;
5496 error_message = NULL;
5499 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5500 "Churn stop callback failed with error `%s'\n", emsg);
5501 churn_ctx->num_failed_start++;
5505 churn_ctx->num_to_start--;
5509 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5510 "Started peer, %d left.\n", churn_ctx->num_to_start);
5513 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
5514 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5516 if (total_left == 0)
5518 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
5521 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5522 churn_ctx->num_failed_start,
5523 churn_ctx->num_failed_stop);
5524 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5525 GNUNET_free_non_null (error_message);
5526 GNUNET_free (churn_ctx);
5527 GNUNET_free (startup_ctx);
5532 schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5534 struct PeerRestartContext *peer_restart_ctx = cls;
5535 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
5537 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5538 GNUNET_SCHEDULER_add_delayed (
5539 GNUNET_TIME_relative_multiply (
5540 GNUNET_TIME_UNIT_MILLISECONDS,
5542 &schedule_churn_restart, peer_restart_ctx);
5545 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
5546 startup_ctx->timeout,
5547 &churn_start_callback, startup_ctx);
5548 GNUNET_free (peer_restart_ctx);
5553 internal_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5555 struct InternalStartContext *internal_context = cls;
5557 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
5562 if ((internal_context->peer->pg->starting
5563 < internal_context->peer->pg->max_concurrent_ssh)
5564 || ((internal_context->hostname != NULL)
5565 && (count_outstanding_at_host (internal_context->hostname,
5566 internal_context->peer->pg)
5567 < internal_context->peer->pg->max_concurrent_ssh)))
5569 if (internal_context->hostname != NULL)
5570 increment_outstanding_at_host (internal_context->hostname,
5571 internal_context->peer->pg);
5572 internal_context->peer->pg->starting++;
5573 internal_context->peer->daemon
5574 = GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
5575 internal_context->timeout,
5576 internal_context->hostname,
5577 internal_context->username,
5578 internal_context->sshport,
5579 internal_context->hostkey,
5580 &internal_hostkey_callback,
5582 &internal_startup_callback,
5587 GNUNET_SCHEDULER_add_delayed (
5588 GNUNET_TIME_relative_multiply (
5589 GNUNET_TIME_UNIT_MILLISECONDS,
5591 &internal_start, internal_context);
5596 * Function which continues a peer group starting up
5597 * after successfully generating hostkeys for each peer.
5599 * @param pg the peer group to continue starting
5603 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
5608 for (i = 0; i < pg->total; i++)
5610 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5611 &pg->peers[i].internal_context);
5612 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
5617 * Start count gnunet instances with the same set of transports and
5618 * applications. The port numbers (any option called "PORT") will be
5619 * adjusted to ensure that no two peers running on the same system
5620 * have the same port(s) in their respective configurations.
5622 * @param cfg configuration template to use
5623 * @param total number of daemons to start
5624 * @param max_concurrent_connections for testing, how many peers can
5625 * we connect to simultaneously
5626 * @param max_concurrent_ssh when starting with ssh, how many ssh
5627 * connections will we allow at once (based on remote hosts allowed!)
5628 * @param timeout total time allowed for peers to start
5629 * @param hostkey_callback function to call on each peers hostkey generation
5630 * if NULL, peers will be started by this call, if non-null,
5631 * GNUNET_TESTING_daemons_continue_startup must be called after
5632 * successful hostkey generation
5633 * @param hostkey_cls closure for hostkey callback
5634 * @param cb function to call on each daemon that was started
5635 * @param cb_cls closure for cb
5636 * @param connect_callback function to call each time two hosts are connected
5637 * @param connect_callback_cls closure for connect_callback
5638 * @param hostnames linked list of host structs to use to start peers on
5639 * (NULL to run on localhost only)
5641 * @return NULL on error, otherwise handle to control peer group
5643 struct GNUNET_TESTING_PeerGroup *
5644 GNUNET_TESTING_daemons_start(
5645 const struct GNUNET_CONFIGURATION_Handle *cfg,
5647 unsigned int max_concurrent_connections,
5648 unsigned int max_concurrent_ssh,
5649 struct GNUNET_TIME_Relative timeout,
5650 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
5652 GNUNET_TESTING_NotifyDaemonRunning cb,
5654 GNUNET_TESTING_NotifyConnection connect_callback,
5655 void *connect_callback_cls,
5656 const struct GNUNET_TESTING_Host *hostnames)
5658 struct GNUNET_TESTING_PeerGroup *pg;
5659 const struct GNUNET_TESTING_Host *hostpos;
5665 const char *hostname;
5666 const char *username;
5667 char *baseservicehome;
5668 char *newservicehome;
5670 char *hostkeys_file;
5673 struct GNUNET_DISK_FileHandle *fd;
5674 struct GNUNET_CONFIGURATION_Handle *pcfg;
5676 struct OutstandingSSH *ssh_entry;
5677 unsigned int hostcnt;
5684 uint64_t total_hostkeys;
5685 struct GNUNET_OS_Process *proc;
5695 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
5697 pg->notify_connection = connect_callback;
5698 pg->notify_connection_cls = connect_callback_cls;
5700 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
5701 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
5702 pg->max_outstanding_connections = max_concurrent_connections;
5703 pg->max_concurrent_ssh = max_concurrent_ssh;
5704 if (NULL != hostnames)
5707 hostpos = hostnames;
5708 while (hostpos != NULL)
5710 hostpos = hostpos->next;
5713 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5716 hostpos = hostnames;
5717 while (hostpos != NULL)
5719 pg->hosts[off].minport = LOW_PORT;
5720 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
5721 if (hostpos->username != NULL)
5722 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
5723 pg->hosts[off].sshport = hostpos->port;
5724 hostpos = hostpos->next;
5734 pg->num_hosts = off;
5738 /* skip leading spaces */
5739 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
5742 while ('\0' != *rpos)
5744 if (isspace ((unsigned char) *rpos))
5748 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5750 start = GNUNET_strdup (hostnames);
5752 while ('\0' != *pos)
5754 if (isspace ((unsigned char) *pos))
5757 if (strlen (start) > 0)
5759 pg->hosts[off].minport = LOW_PORT;
5760 pg->hosts[off++].hostname = start;
5766 if (strlen (start) > 0)
5768 pg->hosts[off].minport = LOW_PORT;
5769 pg->hosts[off++].hostname = start;
5773 GNUNET_free (start);
5774 GNUNET_free (pg->hosts);
5778 minport = 0; /* make gcc happy */
5787 /* Create the servicehome directory for each remote peer */
5788 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
5790 for (i = 0; i < pg->num_hosts; i++)
5792 ssh_entry = GNUNET_malloc(sizeof(struct OutstandingSSH));
5793 ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
5794 GNUNET_CONTAINER_DLL_insert(pg->ssh_head, pg->ssh_tail, ssh_entry);
5795 GNUNET_asprintf(&tmpdir, "%s/%s", baseservicehome, pg->hosts[i].hostname);
5796 if (NULL != pg->hosts[i].username)
5797 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username,
5798 pg->hosts[i].hostname);
5800 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
5801 if (pg->hosts[i].sshport != 0)
5803 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
5804 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-P",
5809 arg, "mkdir -p", tmpdir,
5813 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5814 "mkdir -p", tmpdir, NULL);
5815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5816 "Creating remote dir with command ssh %s %s %s\n", arg,
5817 " mkdir -p ", tmpdir);
5818 GNUNET_free(tmpdir);
5820 GNUNET_OS_process_wait (proc);
5821 GNUNET_OS_process_close(proc);
5823 GNUNET_free(baseservicehome);
5825 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING",
5829 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
5830 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!\n");
5833 /* Check hostkey file size, read entire thing into memory */
5834 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
5835 GNUNET_DISK_PERM_NONE);
5838 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
5842 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs,
5847 GNUNET_ERROR_TYPE_WARNING,
5848 "Found file size %llu for hostkeys, expect hostkeys to be size %d\n",
5849 fs, HOSTKEYFILESIZE);
5851 if (fs % HOSTKEYFILESIZE != 0)
5853 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5854 "File size %llu seems incorrect for hostkeys...\n",
5859 total_hostkeys = fs / HOSTKEYFILESIZE;
5860 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5861 "Will read %llu hostkeys from file\n", total_hostkeys);
5862 pg->hostkey_data = GNUNET_malloc_large (fs);
5863 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
5864 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
5867 GNUNET_free(hostkeys_file);
5870 for (off = 0; off < total; off++)
5874 hostname = pg->hosts[off % hostcnt].hostname;
5875 username = pg->hosts[off % hostcnt].username;
5876 sshport = pg->hosts[off % hostcnt].sshport;
5877 pcfg = make_config (cfg, off, &pg->hosts[off % hostcnt].minport,
5878 &upnum, hostname, &fdnum);
5885 pcfg = make_config (cfg, off, &minport, &upnum, hostname, &fdnum);
5890 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5891 ("Could not create configuration for peer number %u on `%s'!\n"),
5892 off, hostname == NULL ? "localhost" : hostname);
5897 == GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS",
5901 if (hostname != NULL)
5902 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", baseservicehome, hostname, off);
5904 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
5905 GNUNET_free (baseservicehome);
5909 tmpdir = getenv ("TMPDIR");
5910 tmpdir = tmpdir ? tmpdir : "/tmp";
5911 if (hostname != NULL)
5912 GNUNET_asprintf (&newservicehome, "%s/%s/%s/%d/", tmpdir, hostname,
5913 "gnunet-testing-test-test", off);
5915 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", tmpdir,
5916 "gnunet-testing-test-test", off);
5918 GNUNET_CONFIGURATION_set_value_string (pcfg, "PATHS", "SERVICEHOME",
5920 GNUNET_free (newservicehome);
5921 pg->peers[off].cfg = pcfg;
5923 /* Can we do this later? */
5924 pg->peers[off].allowed_peers =
5925 GNUNET_CONTAINER_multihashmap_create (total);
5926 pg->peers[off].connect_peers =
5927 GNUNET_CONTAINER_multihashmap_create (total);
5928 pg->peers[off].blacklisted_peers =
5929 GNUNET_CONTAINER_multihashmap_create (total);
5932 pg->peers[off].pg = pg;
5933 pg->peers[off].internal_context.peer = &pg->peers[off];
5934 pg->peers[off].internal_context.timeout = timeout;
5935 pg->peers[off].internal_context.hostname = hostname;
5936 pg->peers[off].internal_context.username = username;
5937 pg->peers[off].internal_context.sshport = sshport;
5938 if (pg->hostkey_data != NULL)
5939 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off
5941 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
5942 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
5943 pg->peers[off].internal_context.start_cb = cb;
5944 pg->peers[off].internal_context.start_cb_cls = cb_cls;
5946 GNUNET_SCHEDULER_add_now (&internal_start,
5947 &pg->peers[off].internal_context);
5954 * Get a daemon by number, so callers don't have to do nasty
5955 * offsetting operation.
5957 struct GNUNET_TESTING_Daemon *
5958 GNUNET_TESTING_daemon_get(struct GNUNET_TESTING_PeerGroup *pg,
5959 unsigned int position)
5961 if (position < pg->total)
5962 return pg->peers[position].daemon;
5968 * Get a daemon by peer identity, so callers can
5969 * retrieve the daemon without knowing it's offset.
5971 * @param pg the peer group to retrieve the daemon from
5972 * @param peer_id the peer identity of the daemon to retrieve
5974 * @return the daemon on success, or NULL if no such peer identity is found
5976 struct GNUNET_TESTING_Daemon *
5977 GNUNET_TESTING_daemon_get_by_id(struct GNUNET_TESTING_PeerGroup *pg,
5978 struct GNUNET_PeerIdentity *peer_id)
5982 for (i = 0; i < pg->total; i++)
5984 if (0 == memcmp (&pg->peers[i].daemon->id, peer_id,
5985 sizeof(struct GNUNET_PeerIdentity)))
5986 return pg->peers[i].daemon;
5993 * Prototype of a function that will be called when a
5994 * particular operation was completed the testing library.
5996 * @param cls closure (a struct RestartContext)
5997 * @param id id of the peer that was restarted
5998 * @param cfg handle to the configuration of the peer
5999 * @param d handle to the daemon that was restarted
6000 * @param emsg NULL on success
6003 restart_callback(void *cls, const struct GNUNET_PeerIdentity *id,
6004 const struct GNUNET_CONFIGURATION_Handle *cfg,
6005 struct GNUNET_TESTING_Daemon *d, const char *emsg)
6007 struct RestartContext *restart_context = cls;
6011 restart_context->peers_restarted++;
6015 restart_context->peers_restart_failed++;
6018 if (restart_context->peers_restarted == restart_context->peer_group->total)
6020 restart_context->callback (restart_context->callback_cls, NULL);
6021 GNUNET_free (restart_context);
6023 else if (restart_context->peers_restart_failed
6024 + restart_context->peers_restarted == restart_context->peer_group->total)
6026 restart_context->callback (restart_context->callback_cls,
6027 "Failed to restart peers!");
6028 GNUNET_free (restart_context);
6034 * Callback for informing us about a successful
6035 * or unsuccessful churn stop call.
6037 * @param cls a ChurnContext
6038 * @param emsg NULL on success, non-NULL on failure
6042 churn_stop_callback(void *cls, const char *emsg)
6044 struct ShutdownContext *shutdown_ctx = cls;
6045 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
6046 unsigned int total_left;
6047 char *error_message;
6049 error_message = NULL;
6050 shutdown_ctx->outstanding--;
6054 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6055 "Churn stop callback failed with error `%s'\n", emsg);
6056 churn_ctx->num_failed_stop++;
6060 churn_ctx->num_to_stop--;
6064 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6065 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
6067 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
6068 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
6070 if (total_left == 0)
6072 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
6076 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
6077 churn_ctx->num_failed_start,
6078 churn_ctx->num_failed_stop);
6080 churn_ctx->cb (churn_ctx->cb_cls, error_message);
6081 GNUNET_free_non_null (error_message);
6082 GNUNET_free (churn_ctx);
6083 GNUNET_free (shutdown_ctx);
6088 * Count the number of running peers.
6090 * @param pg handle for the peer group
6092 * @return the number of currently running peers in the peer group
6095 GNUNET_TESTING_daemons_running(struct GNUNET_TESTING_PeerGroup *pg)
6098 unsigned int running = 0;
6099 for (i = 0; i < pg->total; i++)
6101 if (pg->peers[i].daemon->running == GNUNET_YES)
6103 GNUNET_assert (running != -1);
6111 * Task to rate limit the number of outstanding peer shutdown
6112 * requests. This is necessary for making sure we don't do
6113 * too many ssh connections at once, but is generally nicer
6114 * to any system as well (graduated task starts, as opposed
6115 * to calling gnunet-arm N times all at once).
6118 schedule_churn_shutdown_task(void *cls,
6119 const struct GNUNET_SCHEDULER_TaskContext *tc)
6121 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6122 struct ShutdownContext *shutdown_ctx;
6123 struct ChurnContext *churn_ctx;
6124 GNUNET_assert (peer_shutdown_ctx != NULL);
6125 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6126 GNUNET_assert (shutdown_ctx != NULL);
6127 churn_ctx = (struct ChurnContext *) shutdown_ctx->cb_cls;
6128 if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
6129 GNUNET_SCHEDULER_add_delayed (
6130 GNUNET_TIME_relative_multiply (
6131 GNUNET_TIME_UNIT_MILLISECONDS,
6133 &schedule_churn_shutdown_task,
6137 shutdown_ctx->outstanding++;
6138 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6139 shutdown_ctx->timeout, shutdown_ctx->cb,
6140 shutdown_ctx, GNUNET_NO, GNUNET_YES);
6141 GNUNET_free (peer_shutdown_ctx);
6146 * Simulate churn by stopping some peers (and possibly
6147 * re-starting others if churn is called multiple times). This
6148 * function can only be used to create leave-join churn (peers "never"
6149 * leave for good). First "voff" random peers that are currently
6150 * online will be taken offline; then "von" random peers that are then
6151 * offline will be put back online. No notifications will be
6152 * generated for any of these operations except for the callback upon
6155 * @param pg handle for the peer group
6156 * @param voff number of peers that should go offline
6157 * @param von number of peers that should come back online;
6158 * must be zero on first call (since "testbed_start"
6159 * always starts all of the peers)
6160 * @param timeout how long to wait for operations to finish before
6162 * @param cb function to call at the end
6163 * @param cb_cls closure for cb
6166 GNUNET_TESTING_daemons_churn(struct GNUNET_TESTING_PeerGroup *pg,
6167 unsigned int voff, unsigned int von,
6168 struct GNUNET_TIME_Relative timeout,
6169 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6171 struct ChurnContext *churn_ctx;
6172 struct ShutdownContext *shutdown_ctx;
6173 struct PeerShutdownContext *peer_shutdown_ctx;
6174 struct PeerRestartContext *peer_restart_ctx;
6175 struct ChurnRestartContext *churn_startup_ctx;
6177 unsigned int running;
6178 unsigned int stopped;
6179 unsigned int total_running;
6180 unsigned int total_stopped;
6182 unsigned int *running_arr;
6183 unsigned int *stopped_arr;
6184 unsigned int *running_permute;
6185 unsigned int *stopped_permute;
6190 if ((von == 0) && (voff == 0)) /* No peers at all? */
6196 for (i = 0; i < pg->total; i++)
6198 if (pg->peers[i].daemon->running == GNUNET_YES)
6200 GNUNET_assert (running != -1);
6205 GNUNET_assert (stopped != -1);
6212 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6213 "Trying to stop more peers than are currently running!\n");
6214 cb (cb_cls, "Trying to stop more peers than are currently running!");
6220 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6221 "Trying to start more peers than are currently stopped!\n");
6222 cb (cb_cls, "Trying to start more peers than are currently stopped!");
6226 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6230 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
6234 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
6236 running_permute = NULL;
6237 stopped_permute = NULL;
6240 running_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6243 stopped_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6246 total_running = running;
6247 total_stopped = stopped;
6251 churn_ctx->num_to_start = von;
6252 churn_ctx->num_to_stop = voff;
6254 churn_ctx->cb_cls = cb_cls;
6257 for (i = 0; i < pg->total; i++)
6259 if (pg->peers[i].daemon->running == GNUNET_YES)
6261 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6262 running_arr[running] = i;
6267 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6268 stopped_arr[stopped] = i;
6273 GNUNET_assert (running >= voff);
6276 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6277 shutdown_ctx->cb = &churn_stop_callback;
6278 shutdown_ctx->cb_cls = churn_ctx;
6279 shutdown_ctx->total_peers = voff;
6280 shutdown_ctx->timeout = timeout;
6283 for (i = 0; i < voff; i++)
6286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
6287 running_permute[i]);
6289 GNUNET_assert (running_arr != NULL);
6290 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6291 peer_shutdown_ctx->daemon
6292 = pg->peers[running_arr[running_permute[i]]].daemon;
6293 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6294 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
6298 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
6300 &churn_stop_callback, churn_ctx,
6301 GNUNET_NO, GNUNET_YES); */
6304 GNUNET_assert (stopped >= von);
6307 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6308 churn_startup_ctx->churn_ctx = churn_ctx;
6309 churn_startup_ctx->timeout = timeout;
6310 churn_startup_ctx->pg = pg;
6312 for (i = 0; i < von; i++)
6315 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
6316 stopped_permute[i]);
6318 GNUNET_assert (stopped_arr != NULL);
6319 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
6320 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
6321 peer_restart_ctx->daemon
6322 = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
6323 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
6325 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
6326 timeout, &churn_start_callback, churn_ctx); */
6329 GNUNET_free_non_null (running_arr);
6330 GNUNET_free_non_null (stopped_arr);
6331 GNUNET_free_non_null (running_permute);
6332 GNUNET_free_non_null (stopped_permute);
6336 * Restart all peers in the given group.
6338 * @param pg the handle to the peer group
6339 * @param callback function to call on completion (or failure)
6340 * @param callback_cls closure for the callback function
6343 GNUNET_TESTING_daemons_restart(struct GNUNET_TESTING_PeerGroup *pg,
6344 GNUNET_TESTING_NotifyCompletion callback,
6347 struct RestartContext *restart_context;
6352 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
6353 restart_context->peer_group = pg;
6354 restart_context->peers_restarted = 0;
6355 restart_context->callback = callback;
6356 restart_context->callback_cls = callback_cls;
6358 for (off = 0; off < pg->total; off++)
6360 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
6361 &restart_callback, restart_context);
6367 * Start or stop an individual peer from the given group.
6369 * @param pg handle to the peer group
6370 * @param offset which peer to start or stop
6371 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
6372 * @param timeout how long to wait for shutdown
6373 * @param cb function to call at the end
6374 * @param cb_cls closure for cb
6377 GNUNET_TESTING_daemons_vary(struct GNUNET_TESTING_PeerGroup *pg,
6378 unsigned int offset, int desired_status,
6379 struct GNUNET_TIME_Relative timeout,
6380 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6382 struct ShutdownContext *shutdown_ctx;
6383 struct ChurnRestartContext *startup_ctx;
6384 struct ChurnContext *churn_ctx;
6386 if (GNUNET_NO == desired_status)
6388 if (NULL != pg->peers[offset].daemon)
6390 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6391 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6392 churn_ctx->num_to_start = 0;
6393 churn_ctx->num_to_stop = 1;
6395 churn_ctx->cb_cls = cb_cls;
6396 shutdown_ctx->cb_cls = churn_ctx;
6397 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon, timeout,
6398 &churn_stop_callback, shutdown_ctx,
6399 GNUNET_NO, GNUNET_YES);
6402 else if (GNUNET_YES == desired_status)
6404 if (NULL == pg->peers[offset].daemon)
6406 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6407 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6408 churn_ctx->num_to_start = 1;
6409 churn_ctx->num_to_stop = 0;
6411 churn_ctx->cb_cls = cb_cls;
6412 startup_ctx->churn_ctx = churn_ctx;
6413 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
6414 timeout, &churn_start_callback,
6423 * Callback for shutting down peers in a peer group.
6425 * @param cls closure (struct ShutdownContext)
6426 * @param emsg NULL on success
6429 internal_shutdown_callback(void *cls, const char *emsg)
6431 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6432 struct ShutdownContext *shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6434 struct OutstandingSSH *ssh_pos;
6436 shutdown_ctx->outstanding--;
6437 if (peer_shutdown_ctx->daemon->hostname != NULL)
6438 decrement_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6443 shutdown_ctx->peers_down++;
6447 shutdown_ctx->peers_failed++;
6450 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down
6451 + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
6453 if (shutdown_ctx->peers_failed > 0)
6454 shutdown_ctx->cb (shutdown_ctx->cb_cls,
6455 "Not all peers successfully shut down!");
6457 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
6459 GNUNET_free (shutdown_ctx->pg->peers);
6460 GNUNET_free_non_null(shutdown_ctx->pg->hostkey_data);
6461 for (off = 0; off < shutdown_ctx->pg->num_hosts; off++)
6463 GNUNET_free (shutdown_ctx->pg->hosts[off].hostname);
6464 GNUNET_free_non_null (shutdown_ctx->pg->hosts[off].username);
6466 GNUNET_free_non_null (shutdown_ctx->pg->hosts);
6467 while (NULL != (ssh_pos = shutdown_ctx->pg->ssh_head))
6469 GNUNET_CONTAINER_DLL_remove(shutdown_ctx->pg->ssh_head, shutdown_ctx->pg->ssh_tail, ssh_pos);
6470 GNUNET_free(ssh_pos);
6472 GNUNET_free (shutdown_ctx->pg);
6473 GNUNET_free (shutdown_ctx);
6475 GNUNET_free(peer_shutdown_ctx);
6479 * Task to rate limit the number of outstanding peer shutdown
6480 * requests. This is necessary for making sure we don't do
6481 * too many ssh connections at once, but is generally nicer
6482 * to any system as well (graduated task starts, as opposed
6483 * to calling gnunet-arm N times all at once).
6486 schedule_shutdown_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6488 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6489 struct ShutdownContext *shutdown_ctx;
6491 GNUNET_assert (peer_shutdown_ctx != NULL);
6492 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6493 GNUNET_assert (shutdown_ctx != NULL);
6495 if ((shutdown_ctx->outstanding < shutdown_ctx->pg->max_concurrent_ssh)
6496 || ((peer_shutdown_ctx->daemon->hostname != NULL)
6497 && (count_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6499 < shutdown_ctx->pg->max_concurrent_ssh)))
6501 if (peer_shutdown_ctx->daemon->hostname != NULL)
6502 increment_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6504 shutdown_ctx->outstanding++;
6505 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6506 shutdown_ctx->timeout,
6507 &internal_shutdown_callback, peer_shutdown_ctx,
6508 GNUNET_YES, GNUNET_NO);
6511 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
6513 &schedule_shutdown_task, peer_shutdown_ctx);
6518 * Shutdown all peers started in the given group.
6520 * @param pg handle to the peer group
6521 * @param timeout how long to wait for shutdown
6522 * @param cb callback to notify upon success or failure
6523 * @param cb_cls closure for cb
6526 GNUNET_TESTING_daemons_stop(struct GNUNET_TESTING_PeerGroup *pg,
6527 struct GNUNET_TIME_Relative timeout,
6528 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6531 struct ShutdownContext *shutdown_ctx;
6532 struct PeerShutdownContext *peer_shutdown_ctx;
6534 struct PeerConnection *conn_iter;
6535 struct PeerConnection *temp_conn;
6538 GNUNET_assert (pg->total > 0);
6540 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6541 shutdown_ctx->cb = cb;
6542 shutdown_ctx->cb_cls = cb_cls;
6543 shutdown_ctx->total_peers = pg->total;
6544 shutdown_ctx->timeout = timeout;
6545 shutdown_ctx->pg = pg;
6546 /* shtudown_ctx->outstanding = 0; */
6548 for (off = 0; off < pg->total; off++)
6550 GNUNET_assert (NULL != pg->peers[off].daemon);
6551 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6552 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
6553 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6554 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
6556 if (NULL != pg->peers[off].cfg)
6557 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
6559 conn_iter = pg->peers[off].allowed_peers_head;
6560 while (conn_iter != NULL)
6562 temp_conn = conn_iter->next;
6563 GNUNET_free(conn_iter);
6564 conn_iter = temp_conn;
6567 conn_iter = pg->peers[off].connect_peers_head;
6568 while (conn_iter != NULL)
6570 temp_conn = conn_iter->next;
6571 GNUNET_free(conn_iter);
6572 conn_iter = temp_conn;
6575 conn_iter = pg->peers[off].blacklisted_peers_head;
6576 while (conn_iter != NULL)
6578 temp_conn = conn_iter->next;
6579 GNUNET_free(conn_iter);
6580 conn_iter = temp_conn;
6583 conn_iter = pg->peers[off].connect_peers_working_set_head;
6584 while (conn_iter != NULL)
6586 temp_conn = conn_iter->next;
6587 GNUNET_free(conn_iter);
6588 conn_iter = temp_conn;
6591 if (pg->peers[off].allowed_peers != NULL)
6592 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
6593 if (pg->peers[off].connect_peers != NULL)
6594 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
6595 if (pg->peers[off].blacklisted_peers != NULL)
6596 GNUNET_CONTAINER_multihashmap_destroy (pg->
6597 peers[off].blacklisted_peers);
6602 /* end of testing_group.c */