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_YES
37 #define DEBUG_CHURN GNUNET_NO
41 #define USE_SEND_HELLOS GNUNET_NO
43 #define TOPOLOGY_HACK GNUNET_YES
46 * Lowest port used for GNUnet testing. Should be high enough to not
47 * conflict with other applications running on the hosts but be low
48 * enough to not conflict with client-ports (typically starting around
51 #define LOW_PORT 12000
54 * Highest port used for GNUnet testing. Should be low enough to not
55 * conflict with the port range for "local" ports (client apps; see
56 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
58 #define HIGH_PORT 56000
60 /* Maximum time to delay connect attempt */
61 #define MAX_CONNECT_DELAY 300
64 * Which list of peers do we need to modify?
68 /** Modify allowed peers */
71 /** Modify connect peers */
74 /** Modify blacklist peers */
77 /** Modify workingset peers */
82 * Prototype of a function called whenever two peers would be connected
83 * in a certain topology.
85 typedef unsigned int (*GNUNET_TESTING_ConnectionProcessor) (struct
86 GNUNET_TESTING_PeerGroup
97 * Context for handling churning a peer group
102 * The peergroup we are dealing with.
104 struct GNUNET_TESTING_PeerGroup *pg;
107 * Callback used to notify of churning finished
109 GNUNET_TESTING_NotifyCompletion cb;
112 * Closure for callback
117 * Number of peers that still need to be started
119 unsigned int num_to_start;
122 * Number of peers that still need to be stopped
124 unsigned int num_to_stop;
127 * Number of peers that failed to start
129 unsigned int num_failed_start;
132 * Number of peers that failed to stop
134 unsigned int num_failed_stop;
137 struct RestartContext
140 * The group of peers being restarted
142 struct GNUNET_TESTING_PeerGroup *peer_group;
145 * How many peers have been restarted thus far
147 unsigned int peers_restarted;
150 * How many peers got an error when restarting
152 unsigned int peers_restart_failed;
155 * The function to call once all peers have been restarted
157 GNUNET_TESTING_NotifyCompletion callback;
160 * Closure for callback function
167 struct SendHelloContext
170 * Global handle to the peer group.
172 struct GNUNET_TESTING_PeerGroup *pg;
175 * The data about this specific peer.
177 struct PeerData *peer;
180 * The next HELLO that needs sent to this peer.
182 struct PeerConnection *peer_pos;
185 * Are we connected to CORE yet?
187 unsigned int core_ready;
190 * How many attempts should we make for failed connections?
192 unsigned int connect_attempts;
195 * Task for scheduling core connect requests to be sent.
197 GNUNET_SCHEDULER_TaskIdentifier core_connect_task;
201 struct ShutdownContext
203 struct GNUNET_TESTING_PeerGroup *pg;
205 * Total peers to wait for
207 unsigned int total_peers;
210 * Number of peers successfully shut down
212 unsigned int peers_down;
215 * Number of peers failed to shut down
217 unsigned int peers_failed;
220 * Number of peers we have started shutting
221 * down. If too many, wait on them.
223 unsigned int outstanding;
226 * Timeout for shutdown.
228 struct GNUNET_TIME_Relative timeout;
231 * Callback to call when all peers either
232 * shutdown or failed to shutdown
234 GNUNET_TESTING_NotifyCompletion cb;
243 * Individual shutdown context for a particular peer.
245 struct PeerShutdownContext
248 * Pointer to the high level shutdown context.
250 struct ShutdownContext *shutdown_ctx;
253 * The daemon handle for the peer to shut down.
255 struct GNUNET_TESTING_Daemon *daemon;
259 * Individual shutdown context for a particular peer.
261 struct PeerRestartContext
264 * Pointer to the high level restart context.
266 struct ChurnRestartContext *churn_restart_ctx;
269 * The daemon handle for the peer to shut down.
271 struct GNUNET_TESTING_Daemon *daemon;
275 struct CreateTopologyContext
279 * Function to call with number of connections
281 GNUNET_TESTING_NotifyConnections cont;
284 * Closure for connection notification
291 /** Waiting to read number of peers */
294 /** Should find next peer index */
297 /** Should find colon */
300 /** Should read other peer index, space, or endline */
306 struct PeerConnection
311 struct PeerConnection *prev;
316 struct PeerConnection *next;
320 * Index of daemon in pg->peers
327 struct InternalStartContext
330 * Pointer to peerdata
332 struct PeerData *peer;
335 * Timeout for peer startup
337 struct GNUNET_TIME_Relative timeout;
340 * Client callback for hostkey notification
342 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
345 * Closure for hostkey_callback
350 * Client callback for peer start notification
352 GNUNET_TESTING_NotifyDaemonRunning start_cb;
360 * Hostname, where to start the peer
362 const char *hostname;
365 * Username to use when connecting to the
368 const char *username;
371 * Pointer to starting memory location of a hostkey
376 * Port to use for ssh.
382 struct ChurnRestartContext
385 * PeerGroup that we are working with.
387 struct GNUNET_TESTING_PeerGroup *pg;
390 * Number of restarts currently in flight.
392 unsigned int outstanding;
395 * Handle to the underlying churn context.
397 struct ChurnContext *churn_ctx;
400 * How long to allow the operation to take.
402 struct GNUNET_TIME_Relative timeout;
405 struct OutstandingSSH
407 struct OutstandingSSH *next;
409 struct OutstandingSSH *prev;
412 * Number of current ssh connections.
414 uint32_t outstanding;
417 * The hostname of this peer.
419 const char *hostname;
423 * Data we keep per peer.
428 * (Initial) configuration of the host.
429 * (initial because clients could change
430 * it and we would not know about those
433 struct GNUNET_CONFIGURATION_Handle *cfg;
436 * Handle for controlling the daemon.
438 struct GNUNET_TESTING_Daemon *daemon;
441 * The peergroup this peer belongs to.
443 struct GNUNET_TESTING_PeerGroup *pg;
447 * Linked list of allowed peer connections.
449 struct PeerConnection *allowed_peers_head;
452 * Linked list of allowed peer connections.
454 struct PeerConnection *allowed_peers_tail;
457 * Linked list of blacklisted peer connections.
459 struct PeerConnection *blacklisted_peers_head;
462 * Linked list of blacklisted peer connections.
464 struct PeerConnection *blacklisted_peers_tail;
467 * Linked list of connect peer connections.
469 struct PeerConnection *connect_peers_head;
472 * Linked list of connect peer connections.
474 struct PeerConnection *connect_peers_tail;
477 * Linked list of connect peer connections.
479 struct PeerConnection *connect_peers_working_set_head;
482 * Linked list of connect peer connections.
484 struct PeerConnection *connect_peers_working_set_tail;
488 * Hash map of allowed peer connections (F2F created topology)
490 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
493 * Hash map of blacklisted peers
495 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
498 * Hash map of peer connections
500 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
503 * Temporary hash map of peer connections
505 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
509 * Temporary variable for topology creation, should be reset before
510 * creating any topology so the count is valid once finished.
515 * Context to keep track of peers being started, to
516 * stagger hostkey generation and peer startup.
518 struct InternalStartContext internal_context;
523 * Linked list of per-host data.
533 * SSH username to use when connecting to this host.
538 * SSH port to use when connecting to this host.
543 * Lowest port that we have not yet used
549 struct TopologyIterateContext
552 * The peergroup we are working with.
554 struct GNUNET_TESTING_PeerGroup *pg;
557 * Callback for notifying of two connected peers.
559 GNUNET_TESTING_NotifyTopology topology_cb;
562 * Closure for topology_cb
567 * Number of peers currently connected to.
569 unsigned int connected;
572 * Number of peers we have finished iterating.
574 unsigned int completed;
577 * Number of peers total.
582 struct StatsIterateContext
585 * The peergroup that we are dealing with.
587 struct GNUNET_TESTING_PeerGroup *pg;
590 * Continuation to call once all stats information has been retrieved.
592 GNUNET_STATISTICS_Callback cont;
595 * Proc function to call on each value received.
597 GNUNET_TESTING_STATISTICS_Iterator proc;
600 * Closure for topology_cb
605 * Number of peers currently connected to.
607 unsigned int connected;
610 * Number of peers we have finished iterating.
612 unsigned int completed;
615 * Number of peers total.
623 struct GNUNET_TESTING_Daemon *daemon;
626 struct StatsCoreContext
629 struct GNUNET_TESTING_Daemon *daemon;
631 * Handle to the statistics service.
633 struct GNUNET_STATISTICS_Handle *stats_handle;
636 * Handle for getting statistics.
638 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
642 struct ConnectTopologyContext
645 * How many connections are left to create.
647 unsigned int remaining_connections;
650 * Handle to group of peers.
652 struct GNUNET_TESTING_PeerGroup *pg;
655 * How long to try this connection before timing out.
657 struct GNUNET_TIME_Relative connect_timeout;
660 * How many times to retry connecting the two peers.
662 unsigned int connect_attempts;
665 * Temp value set for each iteration.
667 //struct PeerData *first;
670 * Notification that all peers are connected.
672 GNUNET_TESTING_NotifyCompletion notify_connections_done;
675 * Closure for notify.
682 * Handle to a group of GNUnet peers.
684 struct GNUNET_TESTING_PeerGroup
687 * Configuration template.
689 const struct GNUNET_CONFIGURATION_Handle *cfg;
692 * Function to call on each started daemon.
694 //GNUNET_TESTING_NotifyDaemonRunning cb;
702 * Function to call on each topology connection created
704 GNUNET_TESTING_NotifyConnection notify_connection;
707 * Callback for notify_connection
709 void *notify_connection_cls;
712 * Array of information about hosts.
714 struct HostData *hosts;
717 * Number of hosts (size of HostData)
719 unsigned int num_hosts;
722 * Array of "total" peers.
724 struct PeerData *peers;
727 * Number of peers in this group.
732 * At what time should we fail the peer startup process?
734 struct GNUNET_TIME_Absolute max_timeout;
737 * How many peers are being started right now?
739 unsigned int starting;
742 * How many peers have already been started?
744 unsigned int started;
747 * Number of possible connections to peers
750 unsigned int max_outstanding_connections;
753 * Number of ssh connections to peers (max).
755 unsigned int max_concurrent_ssh;
758 * Number of connects we are waiting on, allows us to rate limit
761 unsigned int outstanding_connects;
764 * Number of HELLOs we have yet to send.
766 unsigned int remaining_hellos;
769 * How many connects have already been scheduled?
771 unsigned int total_connects_scheduled;
774 * Hostkeys loaded from a file.
779 * Head of DLL to keep track of the number of outstanding
780 * ssh connections per peer.
782 struct OutstandingSSH *ssh_head;
785 * Tail of DLL to keep track of the number of outstanding
786 * ssh connections per peer.
788 struct OutstandingSSH *ssh_tail;
791 * Stop scheduling peers connecting.
793 unsigned int stop_connects;
796 * Connection context for peer group.
798 struct ConnectTopologyContext ct_ctx;
804 * The altered configuration.
806 struct GNUNET_CONFIGURATION_Handle *ret;
809 * The original configuration to alter.
811 const struct GNUNET_CONFIGURATION_Handle *orig;
814 * The hostname that this peer will run on.
816 const char *hostname;
819 * The next possible port to assign.
824 * Unique number for unix domain sockets.
829 * Unique number for this peer/host to offset
830 * things that are grouped by host.
836 struct ConnectContext
839 * Index of peer to connect second to.
841 uint32_t first_index;
844 * Index of peer to connect first to.
846 uint32_t second_index;
849 * Higher level topology connection context.
851 struct ConnectTopologyContext *ct_ctx;
854 * Whether this connection has been accounted for in the schedule_connect call.
859 struct UnblacklistContext
864 struct GNUNET_TESTING_PeerGroup *pg;
867 * uid of the first peer
877 struct GNUNET_TESTING_PeerGroup *pg;
880 * uid of the first peer
885 * Peer data for first peer.
887 struct PeerData *first;
890 * Random percentage to use
895 struct MinimumContext
900 struct GNUNET_TESTING_PeerGroup *pg;
903 * uid of the first peer
908 * Peer data for first peer.
910 struct PeerData *first;
913 * Number of conns per peer
915 unsigned int num_to_add;
918 * Permuted array of all possible connections. Only add the Nth
919 * peer if it's in the Nth position.
921 unsigned int *pg_array;
924 * What number is the current element we are iterating over?
926 unsigned int current;
934 struct GNUNET_TESTING_PeerGroup *pg;
937 * uid of the first peer
942 * uid of the second peer
947 * Peer data for first peer.
949 struct PeerData *first;
952 * Which peer has been chosen as the one to add?
957 * What number is the current element we are iterating over?
959 unsigned int current;
963 * Simple struct to keep track of progress, and print a
964 * nice little percentage meter for long running tasks.
974 unsigned int completed;
978 char *startup_string;
983 * Convert unique ID to hash code.
985 * @param uid unique ID to convert
986 * @param hash set to uid (extended with zeros)
989 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
991 memset (hash, 0, sizeof (GNUNET_HashCode));
992 *((uint32_t *) hash) = uid;
996 * Convert hash code to unique ID.
998 * @param uid unique ID to convert
999 * @param hash set to uid (extended with zeros)
1002 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
1004 memcpy (uid, hash, sizeof (uint32_t));
1009 static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
1013 * Create a meter to keep track of the progress of some task.
1015 * @param total the total number of items to complete
1016 * @param start_string a string to prefix the meter with (if printing)
1017 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
1020 * @return the progress meter
1022 static struct ProgressMeter *
1023 create_meter(unsigned int total, char * start_string, int print)
1025 struct ProgressMeter *ret;
1026 ret = GNUNET_malloc(sizeof(struct ProgressMeter));
1029 ret->modnum = total / 4;
1030 ret->dotnum = (total / 50) + 1;
1031 if (start_string != NULL)
1032 ret->startup_string = GNUNET_strdup(start_string);
1034 ret->startup_string = GNUNET_strdup("");
1040 * Update progress meter (increment by one).
1042 * @param meter the meter to update and print info for
1044 * @return GNUNET_YES if called the total requested,
1045 * GNUNET_NO if more items expected
1048 update_meter(struct ProgressMeter *meter)
1050 if (meter->print == GNUNET_YES)
1052 if (meter->completed % meter->modnum == 0)
1054 if (meter->completed == 0)
1056 fprintf(stdout, "%sProgress: [0%%", meter->startup_string);
1059 fprintf(stdout, "%d%%", (int) (((float) meter->completed
1060 / meter->total) * 100));
1062 else if (meter->completed % meter->dotnum == 0)
1063 fprintf(stdout, ".");
1065 if (meter->completed + 1 == meter->total)
1066 fprintf(stdout, "%d%%]\n", 100);
1071 if (meter->completed == meter->total)
1073 if (meter->completed > meter->total)
1074 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
1079 * Reset progress meter.
1081 * @param meter the meter to reset
1083 * @return GNUNET_YES if meter reset,
1084 * GNUNET_SYSERR on error
1087 reset_meter(struct ProgressMeter *meter)
1090 return GNUNET_SYSERR;
1092 meter->completed = 0;
1097 * Release resources for meter
1099 * @param meter the meter to free
1102 free_meter(struct ProgressMeter *meter)
1104 GNUNET_free_non_null (meter->startup_string);
1105 GNUNET_free (meter);
1109 * Get a topology from a string input.
1111 * @param topology where to write the retrieved topology
1112 * @param topology_string The string to attempt to
1113 * get a configuration value from
1114 * @return GNUNET_YES if topology string matched a
1115 * known topology, GNUNET_NO if not
1118 GNUNET_TESTING_topology_get (enum GNUNET_TESTING_Topology *topology,
1119 const char *topology_string)
1122 * Strings representing topologies in enum
1124 static const char *topology_strings[] = {
1126 * A clique (everyone connected to everyone else).
1131 * Small-world network (2d torus plus random links).
1136 * Small-world network (ring plus random links).
1156 * Certain percentage of peers are unable to communicate directly
1157 * replicating NAT conditions
1162 * Scale free topology.
1167 * Straight line topology.
1172 * All peers are disconnected.
1177 * Read the topology from a file.
1185 if (topology_string == NULL)
1187 while (topology_strings[curr] != NULL)
1189 if (strcasecmp (topology_strings[curr], topology_string) == 0)
1196 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
1202 * Get connect topology option from string input.
1204 * @param topology_option where to write the retrieved topology
1205 * @param topology_string The string to attempt to
1206 * get a configuration value from
1207 * @return GNUNET_YES if string matched a known
1208 * topology option, GNUNET_NO if not
1211 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption
1213 const char *topology_string)
1216 * Options for connecting a topology as strings.
1218 static const char *topology_option_strings[] = {
1220 * Try to connect all peers specified in the topology.
1225 * Choose a random subset of connections to create.
1227 "CONNECT_RANDOM_SUBSET",
1230 * Create at least X connections for each peer.
1235 * Using a depth first search, create one connection
1236 * per peer. If any are missed (graph disconnected)
1237 * start over at those peers until all have at least one
1243 * Find the N closest peers to each allowed peer in the
1244 * topology and make sure a connection to those peers
1245 * exists in the connect topology.
1250 * No options specified.
1258 if (topology_string == NULL)
1260 while (NULL != topology_option_strings[curr])
1262 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1264 *topology_option = curr;
1269 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1274 * Function to iterate over options. Copies
1275 * the options to the target configuration,
1276 * updating PORT values as needed.
1278 * @param cls closure
1279 * @param section name of the section
1280 * @param option name of the option
1281 * @param value value of the option
1284 update_config (void *cls,
1285 const char *section, const char *option, const char *value)
1287 struct UpdateContext *ctx = cls;
1291 char *single_variable;
1292 char *per_host_variable;
1293 unsigned long long num_per_host;
1295 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1296 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1298 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1302 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1305 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
1308 else if ((ival != 0) &&
1310 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1311 single_variable)) &&
1312 GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1316 GNUNET_snprintf (cval, sizeof (cval), "%u", ival + ctx->fdnum % num_per_host);
1321 if (0 == strcmp (option, "UNIXPATH"))
1324 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1327 GNUNET_snprintf (uval,
1329 "/tmp/test-service-%s-%u", section, ctx->upnum++);
1332 else if ((GNUNET_YES ==
1333 GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1335 &num_per_host)) && (num_per_host > 0))
1338 GNUNET_snprintf (uval,
1340 "/tmp/test-service-%s-%u",
1341 section, ctx->fdnum % num_per_host);
1346 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1348 value = ctx->hostname;
1350 GNUNET_free (single_variable);
1351 GNUNET_free (per_host_variable);
1352 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1357 * Create a new configuration using the given configuration
1358 * as a template; however, each PORT in the existing cfg
1359 * must be renumbered by incrementing "*port". If we run
1360 * out of "*port" numbers, return NULL.
1362 * @param cfg template configuration
1363 * @param off the current peer offset
1364 * @param port port numbers to use, update to reflect
1365 * port numbers that were used
1366 * @param upnum number to make unix domain socket names unique
1367 * @param hostname hostname of the controlling host, to allow control connections from
1368 * @param fdnum number used to offset the unix domain socket for grouped processes
1369 * (such as statistics or peerinfo, which can be shared among others)
1371 * @return new configuration, NULL on error
1373 static struct GNUNET_CONFIGURATION_Handle *
1374 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
1377 uint32_t * upnum, const char *hostname,
1380 struct UpdateContext uc;
1383 char *allowed_hosts;
1389 uc.ret = GNUNET_CONFIGURATION_create ();
1390 uc.hostname = hostname;
1393 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1394 if (uc.nport >= HIGH_PORT)
1397 GNUNET_CONFIGURATION_destroy (uc.ret);
1401 if (GNUNET_CONFIGURATION_get_value_string
1402 (cfg, "testing", "control_host", &control_host) == GNUNET_OK)
1404 if (hostname != NULL)
1405 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1408 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1410 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1412 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1413 "ACCEPT_FROM", allowed_hosts);
1414 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1416 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1417 "ACCEPT_FROM", allowed_hosts);
1419 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH", "");
1420 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH", "");
1421 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH", "");
1422 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH", "");
1424 GNUNET_free_non_null (control_host);
1425 GNUNET_free (allowed_hosts);
1429 /* arm needs to know to allow connections from the host on which it is running,
1430 * otherwise gnunet-arm is unable to connect to it in some instances */
1431 if (hostname != NULL)
1433 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1434 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1435 "BINDTO", hostname);
1436 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1437 "BINDTO", hostname);
1438 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1440 GNUNET_free (allowed_hosts);
1444 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1445 "BINDTO", "127.0.0.1");
1446 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1447 "BINDTO", "127.0.0.1");
1450 *port = (uint16_t) uc.nport;
1458 * Remove entries from the peer connection list
1460 * @param pg the peer group we are working with
1461 * @param first index of the first peer
1462 * @param second index of the second peer
1463 * @param list the peer list to use
1464 * @param check UNUSED
1466 * @return the number of connections added (can be 0, 1 or 2)
1470 remove_connections (struct GNUNET_TESTING_PeerGroup *pg,
1471 unsigned int first, unsigned int second,
1472 enum PeerLists list, unsigned int check)
1476 struct PeerConnection **first_list;
1477 struct PeerConnection **second_list;
1478 struct PeerConnection *first_iter;
1479 struct PeerConnection *second_iter;
1480 struct PeerConnection **first_tail;
1481 struct PeerConnection **second_tail;
1484 GNUNET_HashCode hash_first;
1485 GNUNET_HashCode hash_second;
1487 hash_from_uid (first, &hash_first);
1488 hash_from_uid (second, &hash_second);
1496 first_list = &pg->peers[first].allowed_peers_head;
1497 second_list = &pg->peers[second].allowed_peers_head;
1498 first_tail = &pg->peers[first].allowed_peers_tail;
1499 second_tail = &pg->peers[second].allowed_peers_tail;
1502 first_list = &pg->peers[first].connect_peers_head;
1503 second_list = &pg->peers[second].connect_peers_head;
1504 first_tail = &pg->peers[first].connect_peers_tail;
1505 second_tail = &pg->peers[second].connect_peers_tail;
1508 first_list = &pg->peers[first].blacklisted_peers_head;
1509 second_list = &pg->peers[second].blacklisted_peers_head;
1510 first_tail = &pg->peers[first].blacklisted_peers_tail;
1511 second_tail = &pg->peers[second].blacklisted_peers_tail;
1514 first_list = &pg->peers[first].connect_peers_working_set_head;
1515 second_list = &pg->peers[second].connect_peers_working_set_head;
1516 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1517 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1524 first_iter = *first_list;
1525 while (first_iter != NULL)
1527 if (first_iter->index == second)
1529 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1530 GNUNET_free(first_iter);
1534 first_iter = first_iter->next;
1537 second_iter = *second_list;
1538 while (second_iter != NULL)
1540 if (second_iter->index == first)
1542 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1543 GNUNET_free(second_iter);
1547 second_iter = second_iter->next;
1551 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1554 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1559 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1562 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1571 * Add entries to the some list
1573 * @param pg the peer group we are working with
1574 * @param first index of the first peer
1575 * @param second index of the second peer
1576 * @param list the list type that we should modify
1577 * @param check GNUNET_YES to check lists before adding
1578 * GNUNET_NO to force add
1580 * @return the number of connections added (can be 0, 1 or 2)
1584 add_connections (struct GNUNET_TESTING_PeerGroup *pg,
1585 unsigned int first, unsigned int second,
1586 enum PeerLists list,
1593 struct PeerConnection **first_list;
1594 struct PeerConnection **second_list;
1595 struct PeerConnection *first_iter;
1596 struct PeerConnection *second_iter;
1597 struct PeerConnection *new_first;
1598 struct PeerConnection *new_second;
1599 struct PeerConnection **first_tail;
1600 struct PeerConnection **second_tail;
1602 GNUNET_HashCode hash_first;
1603 GNUNET_HashCode hash_second;
1605 hash_from_uid (first, &hash_first);
1606 hash_from_uid (second, &hash_second);
1613 first_list = &pg->peers[first].allowed_peers_head;
1614 second_list = &pg->peers[second].allowed_peers_head;
1615 first_tail = &pg->peers[first].allowed_peers_tail;
1616 second_tail = &pg->peers[second].allowed_peers_tail;
1619 first_list = &pg->peers[first].connect_peers_head;
1620 second_list = &pg->peers[second].connect_peers_head;
1621 first_tail = &pg->peers[first].connect_peers_tail;
1622 second_tail = &pg->peers[second].connect_peers_tail;
1625 first_list = &pg->peers[first].blacklisted_peers_head;
1626 second_list = &pg->peers[second].blacklisted_peers_head;
1627 first_tail = &pg->peers[first].blacklisted_peers_tail;
1628 second_tail = &pg->peers[second].blacklisted_peers_tail;
1631 first_list = &pg->peers[first].connect_peers_working_set_head;
1632 second_list = &pg->peers[second].connect_peers_working_set_head;
1633 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1634 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1641 add_first = GNUNET_YES;
1642 add_second = GNUNET_YES;
1644 if (check == GNUNET_YES)
1646 first_iter = *first_list;
1647 while (first_iter != NULL)
1649 if (first_iter->index == second)
1651 add_first = GNUNET_NO;
1654 first_iter = first_iter->next;
1657 second_iter = *second_list;
1658 while (second_iter != NULL)
1660 if (second_iter->index == first)
1662 add_second = GNUNET_NO;
1665 second_iter = second_iter->next;
1670 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1673 add_first = GNUNET_YES;
1677 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1680 add_second = GNUNET_YES;
1688 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1689 new_first->index = second;
1690 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1692 GNUNET_assert (GNUNET_OK ==
1693 GNUNET_CONTAINER_multihashmap_put (pg->
1695 [first].blacklisted_peers,
1698 peers[second].daemon,
1699 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1701 pg->peers[first].num_connections++;
1708 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1709 new_second->index = first;
1710 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1712 GNUNET_assert (GNUNET_OK ==
1713 GNUNET_CONTAINER_multihashmap_put (pg->
1715 [second].blacklisted_peers,
1718 peers[first].daemon,
1719 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1721 pg->peers[second].num_connections++;
1730 * Scale free network construction as described in:
1732 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1734 * Start with a network of "one" peer, then progressively add
1735 * peers up to the total number. At each step, iterate over
1736 * all possible peers and connect new peer based on number of
1737 * existing connections of the target peer.
1739 * @param pg the peer group we are dealing with
1740 * @param proc the connection processor to use
1741 * @param list the peer list to use
1743 * @return the number of connections created
1746 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg,
1747 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1750 unsigned int total_connections;
1751 unsigned int outer_count;
1753 unsigned int previous_total_connections;
1757 GNUNET_assert (pg->total > 1);
1759 /* Add a connection between the first two nodes */
1760 total_connections = proc (pg, 0, 1, list, GNUNET_YES);
1762 for (outer_count = 1; outer_count < pg->total; outer_count++)
1764 previous_total_connections = total_connections;
1765 for (i = 0; i < outer_count; i++)
1768 pg->peers[i].num_connections /
1769 (double) previous_total_connections;
1772 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1773 UINT64_MAX)) / ((double) UINT64_MAX);
1775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1776 "Considering connecting peer %d to peer %d\n",
1779 if (random < probability)
1782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1783 "Connecting peer %d to peer %d\n", outer_count, i);
1785 total_connections += proc (pg, outer_count, i, list, GNUNET_YES);
1790 return total_connections;
1794 * Create a topology given a peer group (set of running peers)
1795 * and a connection processor. Creates a small world topology
1796 * according to the rewired ring construction. The basic
1797 * behavior is that a ring topology is created, but with some
1798 * probability instead of connecting a peer to the next
1799 * neighbor in the ring a connection will be created to a peer
1800 * selected uniformly at random. We use the TESTING
1801 * PERCENTAGE option to specify what number of
1802 * connections each peer should have. Default is 2,
1803 * which makes the ring, any given number is multiplied by
1804 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1805 * each peer have on average 2logn connections. The additional
1806 * connections are made at increasing distance around the ring
1807 * from the original peer, or to random peers based on the re-
1808 * wiring probability. The TESTING
1809 * PROBABILITY option is used as the probability that a given
1810 * connection is rewired.
1812 * @param pg the peergroup to create the topology on
1813 * @param proc the connection processor to call to actually set
1814 * up connections between two peers
1815 * @param list the peer list to use
1817 * @return the number of connections that were set up
1821 create_small_world_ring (struct GNUNET_TESTING_PeerGroup *pg,
1822 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1826 unsigned int natLog;
1827 unsigned int randomPeer;
1828 double random, logNModifier, probability;
1829 unsigned int smallWorldConnections;
1834 unsigned int useAnd;
1835 int connect_attempts;
1837 logNModifier = 0.5; /* FIXME: default value? */
1838 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1843 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1844 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1846 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1847 p_string, "LOGNMODIFIER", "TESTING");
1848 GNUNET_free (p_string);
1850 probability = 0.5; /* FIXME: default percentage? */
1851 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1856 if (sscanf (p_string, "%lf", &probability) != 1)
1857 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1859 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1860 p_string, "PERCENTAGE", "TESTING");
1861 GNUNET_free (p_string);
1863 natLog = log (pg->total);
1864 connsPerPeer = ceil (natLog * logNModifier);
1866 if (connsPerPeer % 2 == 1)
1869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."), connsPerPeer);
1871 smallWorldConnections = 0;
1872 connect_attempts = 0;
1873 for (i = 0; i < pg->total; i++)
1876 max = i + connsPerPeer / 2;
1877 min = i - connsPerPeer / 2;
1879 if (max > pg->total - 1)
1881 max = max - pg->total;
1887 min = pg->total - 1 + min;
1891 for (j = 0; j < connsPerPeer / 2; j++)
1895 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1896 UINT64_MAX) / ((double) UINT64_MAX));
1897 if (random < probability)
1899 /* Connect to uniformly selected random peer */
1901 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1903 while ((((randomPeer < max) && (randomPeer > min))
1904 && (useAnd == 0)) || (((randomPeer > min)
1905 || (randomPeer < max))
1909 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1912 smallWorldConnections += proc (pg, i, randomPeer, list, GNUNET_YES);
1916 nodeToConnect = i + j + 1;
1917 if (nodeToConnect > pg->total - 1)
1919 nodeToConnect = nodeToConnect - pg->total;
1921 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
1927 connect_attempts += smallWorldConnections;
1929 return connect_attempts;
1933 * Create a topology given a peer group (set of running peers)
1934 * and a connection processor.
1936 * @param pg the peergroup to create the topology on
1937 * @param proc the connection processor to call to actually set
1938 * up connections between two peers
1939 * @param list the peer list to use
1941 * @return the number of connections that were set up
1945 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg,
1946 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1948 unsigned int outer_count, inner_count;
1949 unsigned int cutoff;
1950 int connect_attempts;
1951 double nat_percentage;
1954 nat_percentage = 0.6; /* FIXME: default percentage? */
1955 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
1960 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1961 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1963 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1964 p_string, "PERCENTAGE", "TESTING");
1965 GNUNET_free (p_string);
1968 cutoff = (unsigned int) (nat_percentage * pg->total);
1969 connect_attempts = 0;
1970 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1972 for (inner_count = outer_count + 1; inner_count < pg->total;
1975 if ((outer_count > cutoff) || (inner_count > cutoff))
1978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1979 "Connecting peer %d to peer %d\n",
1980 outer_count, inner_count);
1982 connect_attempts += proc (pg, outer_count, inner_count, list, GNUNET_YES);
1986 return connect_attempts;
1991 * Create a topology given a peer group (set of running peers)
1992 * and a connection processor.
1994 * @param pg the peergroup to create the topology on
1995 * @param proc the connection processor to call to actually set
1996 * up connections between two peers
1997 * @param list the peer list to use
1999 * @return the number of connections that were set up
2003 create_nated_internet_copy (struct GNUNET_TESTING_PeerGroup *pg,
2004 GNUNET_TESTING_ConnectionProcessor proc,
2005 enum PeerLists list)
2007 unsigned int outer_count, inner_count;
2008 unsigned int cutoff;
2009 int connect_attempts;
2010 double nat_percentage;
2013 struct ProgressMeter *conn_meter;
2015 nat_percentage = 0.6; /* FIXME: default percentage? */
2016 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
2021 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
2022 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2024 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2025 p_string, "PERCENTAGE", "TESTING");
2026 GNUNET_free (p_string);
2029 cutoff = (unsigned int) (nat_percentage * pg->total);
2031 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2033 for (inner_count = outer_count + 1; inner_count < pg->total;
2036 if ((outer_count > cutoff) || (inner_count > cutoff))
2042 conn_meter = create_meter(count, "NAT COPY", GNUNET_YES);
2043 connect_attempts = 0;
2044 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2046 for (inner_count = outer_count + 1; inner_count < pg->total;
2049 if ((outer_count > cutoff) || (inner_count > cutoff))
2052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2053 "Connecting peer %d to peer %d\n",
2054 outer_count, inner_count);
2056 connect_attempts += proc (pg, outer_count, inner_count, list, GNUNET_YES);
2057 add_connections(pg, outer_count, inner_count, ALLOWED, GNUNET_NO);
2058 update_meter(conn_meter);
2062 free_meter(conn_meter);
2064 return connect_attempts;
2069 * Create a topology given a peer group (set of running peers)
2070 * and a connection processor.
2072 * @param pg the peergroup to create the topology on
2073 * @param proc the connection processor to call to actually set
2074 * up connections between two peers
2075 * @param list the peer list to use
2077 * @return the number of connections that were set up
2081 create_small_world (struct GNUNET_TESTING_PeerGroup *pg,
2082 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2084 unsigned int i, j, k;
2085 unsigned int square;
2088 unsigned int toggle = 1;
2089 unsigned int nodeToConnect;
2090 unsigned int natLog;
2091 unsigned int node1Row;
2092 unsigned int node1Col;
2093 unsigned int node2Row;
2094 unsigned int node2Col;
2095 unsigned int distance;
2096 double probability, random, percentage;
2097 unsigned int smallWorldConnections;
2098 unsigned int small_world_it;
2100 int connect_attempts;
2101 square = floor (sqrt (pg->total));
2105 percentage = 0.5; /* FIXME: default percentage? */
2106 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
2111 if (sscanf (p_string, "%lf", &percentage) != 1)
2112 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2114 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2115 p_string, "PERCENTAGE", "TESTING");
2116 GNUNET_free (p_string);
2118 if (percentage < 0.0)
2120 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2122 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
2123 "PERCENTAGE", "TESTING", percentage);
2126 probability = 0.5; /* FIXME: default percentage? */
2127 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
2132 if (sscanf (p_string, "%lf", &probability) != 1)
2133 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2135 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2136 p_string, "PROBABILITY", "TESTING");
2137 GNUNET_free (p_string);
2139 if (square * square != pg->total)
2141 while (rows * cols < pg->total)
2143 if (toggle % 2 == 0)
2152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2154 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2158 connect_attempts = 0;
2159 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2160 * to the node to its right and above. Once this is over, we'll have our torus!
2161 * Special case for the last node (if the rows and columns are not equal), connect
2162 * to the first in the row to maintain topology.
2164 for (i = 0; i < pg->total; i++)
2166 /* First connect to the node to the right */
2167 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2168 nodeToConnect = i + 1;
2169 else if (i + 1 == pg->total)
2170 nodeToConnect = rows * cols - cols;
2172 nodeToConnect = i - cols + 1;
2174 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2178 nodeToConnect = (rows * cols) - cols + i;
2179 if (nodeToConnect >= pg->total)
2180 nodeToConnect -= cols;
2183 nodeToConnect = i - cols;
2185 if (nodeToConnect < pg->total)
2186 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2188 natLog = log (pg->total);
2189 #if VERBOSE_TESTING > 2
2190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2191 _("natural log of %d is %d, will run %d iterations\n"),
2192 pg->total, natLog, (int) (natLog * percentage));
2193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2194 _("Total connections added thus far: %u!\n"), connect_attempts);
2196 smallWorldConnections = 0;
2197 small_world_it = (unsigned int) (natLog * percentage);
2198 if (small_world_it < 1)
2200 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
2201 for (i = 0; i < small_world_it; i++)
2203 for (j = 0; j < pg->total; j++)
2205 /* Determine the row and column of node at position j on the 2d torus */
2206 node1Row = j / cols;
2207 node1Col = j - (node1Row * cols);
2208 for (k = 0; k < pg->total; k++)
2210 /* Determine the row and column of node at position k on the 2d torus */
2211 node2Row = k / cols;
2212 node2Col = k - (node2Row * cols);
2213 /* Simple Cartesian distance */
2215 abs (node1Row - node2Row) + abs (node1Col - node2Col);
2218 /* Calculate probability as 1 over the square of the distance */
2219 probability = 1.0 / (distance * distance);
2220 /* Choose a random value between 0 and 1 */
2223 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2225 ((double) UINT64_MAX);
2226 /* If random < probability, then connect the two nodes */
2227 if (random < probability)
2228 smallWorldConnections += proc (pg, j, k, list, GNUNET_YES);
2234 connect_attempts += smallWorldConnections;
2235 #if VERBOSE_TESTING > 2
2236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2237 _("Total connections added for small world: %d!\n"),
2238 smallWorldConnections);
2240 return connect_attempts;
2244 * Create a topology given a peer group (set of running peers)
2245 * and a connection processor.
2247 * @param pg the peergroup to create the topology on
2248 * @param proc the connection processor to call to actually set
2249 * up connections between two peers
2250 * @param list the peer list to use
2252 * @return the number of connections that were set up
2256 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg,
2257 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2260 unsigned int outer_count;
2261 unsigned int inner_count;
2262 int connect_attempts;
2266 probability = 0.5; /* FIXME: default percentage? */
2267 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg,
2272 if (sscanf (p_string, "%lf", &probability) != 1)
2273 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2275 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2276 p_string, "PROBABILITY", "TESTING");
2277 GNUNET_free (p_string);
2279 connect_attempts = 0;
2280 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2282 for (inner_count = outer_count + 1; inner_count < pg->total;
2287 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2288 UINT64_MAX)) / ((double) UINT64_MAX);
2290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2291 _("rand is %f probability is %f\n"), temp_rand,
2294 if (temp_rand < probability)
2296 connect_attempts += proc (pg, outer_count, inner_count, list, GNUNET_YES);
2301 return connect_attempts;
2305 * Create a topology given a peer group (set of running peers)
2306 * and a connection processor. This particular function creates
2307 * the connections for a 2d-torus, plus additional "closest"
2308 * connections per peer.
2310 * @param pg the peergroup to create the topology on
2311 * @param proc the connection processor to call to actually set
2312 * up connections between two peers
2313 * @param list the peer list to use
2315 * @return the number of connections that were set up
2319 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg,
2320 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2323 unsigned int square;
2326 unsigned int toggle = 1;
2327 unsigned int nodeToConnect;
2328 int connect_attempts;
2330 connect_attempts = 0;
2332 square = floor (sqrt (pg->total));
2336 if (square * square != pg->total)
2338 while (rows * cols < pg->total)
2340 if (toggle % 2 == 0)
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2351 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2354 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2355 * to the node to its right and above. Once this is over, we'll have our torus!
2356 * Special case for the last node (if the rows and columns are not equal), connect
2357 * to the first in the row to maintain topology.
2359 for (i = 0; i < pg->total; i++)
2361 /* First connect to the node to the right */
2362 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2363 nodeToConnect = i + 1;
2364 else if (i + 1 == pg->total)
2365 nodeToConnect = rows * cols - cols;
2367 nodeToConnect = i - cols + 1;
2369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2370 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2372 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2374 /* Second connect to the node immediately above */
2377 nodeToConnect = (rows * cols) - cols + i;
2378 if (nodeToConnect >= pg->total)
2379 nodeToConnect -= cols;
2382 nodeToConnect = i - cols;
2384 if (nodeToConnect < pg->total)
2387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2388 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2390 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2395 return connect_attempts;
2400 * Create a topology given a peer group (set of running peers)
2401 * and a connection processor.
2403 * @param pg the peergroup to create the topology on
2404 * @param proc the connection processor to call to actually set
2405 * up connections between two peers
2406 * @param list the peer list to use
2407 * @param check does the connection processor need to check before
2408 * performing an action on the list?
2410 * @return the number of connections that were set up
2414 create_clique (struct GNUNET_TESTING_PeerGroup *pg,
2415 GNUNET_TESTING_ConnectionProcessor proc,
2416 enum PeerLists list,
2419 unsigned int outer_count;
2420 unsigned int inner_count;
2421 int connect_attempts;
2422 struct ProgressMeter *conn_meter;
2423 connect_attempts = 0;
2425 conn_meter = create_meter((((pg->total * pg->total) + pg->total) / 2) - pg->total, "Create Clique ", GNUNET_YES);
2426 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2428 for (inner_count = outer_count + 1; inner_count < pg->total;
2432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2433 "Connecting peer %d to peer %d\n",
2434 outer_count, inner_count);
2436 connect_attempts += proc (pg, outer_count, inner_count, list, check);
2437 update_meter(conn_meter);
2440 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Meter has %d left\n", conn_meter->total - conn_meter->completed);
2441 reset_meter(conn_meter);
2442 free_meter(conn_meter);
2443 return connect_attempts;
2448 * Iterator over hash map entries.
2450 * @param cls closure the peer group
2451 * @param key the key stored in the hashmap is the
2452 * index of the peer to connect to
2453 * @param value value in the hash map, handle to the peer daemon
2454 * @return GNUNET_YES if we should continue to
2459 unblacklist_iterator (void *cls,
2460 const GNUNET_HashCode * key,
2463 struct UnblacklistContext *un_ctx = cls;
2464 uint32_t second_pos;
2466 uid_from_hash (key, &second_pos);
2468 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2475 * Create a blacklist topology based on the allowed topology
2476 * which disallows any connections not in the allowed topology
2477 * at the transport level.
2479 * @param pg the peergroup to create the topology on
2480 * @param proc the connection processor to call to allow
2481 * up connections between two peers
2483 * @return the number of connections that were set up
2487 copy_allowed (struct GNUNET_TESTING_PeerGroup *pg,
2488 GNUNET_TESTING_ConnectionProcessor proc)
2490 struct UnblacklistContext un_ctx;
2493 struct PeerConnection *iter;
2497 for (count = 0; count < pg->total - 1; count++)
2499 un_ctx.first_uid = count;
2501 iter = pg->peers[count].allowed_peers_head;
2502 while (iter != NULL)
2504 remove_connections(pg, count, iter->index, BLACKLIST, GNUNET_YES);
2505 //unblacklist_connections(pg, count, iter->index);
2509 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers, &unblacklist_iterator, &un_ctx);
2512 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2518 * Create a topology given a peer group (set of running peers)
2519 * and a connection processor.
2521 * @param pg the peergroup to create the topology on
2522 * @param proc the connection processor to call to actually set
2523 * up connections between two peers
2524 * @param list which list should be modified
2526 * @return the number of connections that were set up
2530 create_line (struct GNUNET_TESTING_PeerGroup *pg,
2531 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2534 int connect_attempts;
2536 connect_attempts = 0;
2538 /* Connect each peer to the next highest numbered peer */
2539 for (count = 0; count < pg->total - 1; count++)
2542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2543 "Connecting peer %d to peer %d\n", count, count + 1);
2545 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2548 return connect_attempts;
2552 * Create a topology given a peer group (set of running peers)
2553 * and a connection processor.
2555 * @param pg the peergroup to create the topology on
2556 * @param filename the file to read topology information from
2557 * @param proc the connection processor to call to actually set
2558 * up connections between two peers
2559 * @param list the peer list to use
2561 * @return the number of connections that were set up
2565 create_from_file (struct GNUNET_TESTING_PeerGroup *pg,
2567 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2569 int connect_attempts;
2570 unsigned int first_peer_index;
2571 unsigned int second_peer_index;
2572 connect_attempts = 0;
2577 unsigned int total_peers;
2579 enum States curr_state;
2581 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2582 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2584 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2586 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2587 "Could not open file `%s' specified for topology!", filename);
2588 return connect_attempts;
2591 data = GNUNET_malloc_large (frstat.st_size);
2592 GNUNET_assert(data != NULL);
2593 if (frstat.st_size !=
2594 GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2596 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2597 "Could not read file %s specified for host list, ending test!", filename);
2599 return connect_attempts;
2604 /* First line should contain a single integer, specifying the number of peers */
2605 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2606 curr_state = NUM_PEERS;
2607 while (count < frstat.st_size - 1)
2609 if ((buf[count] == '\n') || (buf[count] == ' '))
2618 if (1 != sscanf(&buf[count], "%u", &total_peers))
2620 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read number of peers from topology file!\n");
2621 GNUNET_free_non_null(data);
2622 return connect_attempts;
2624 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read %u total peers in topology\n", total_peers);
2625 GNUNET_assert(total_peers == pg->total);
2626 curr_state = PEER_INDEX;
2627 while((buf[count] != '\n') && (count < frstat.st_size - 1))
2632 if (1 != sscanf(&buf[count], "%u", &first_peer_index))
2634 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read peer index from topology file!\n");
2635 GNUNET_free_non_null(data);
2636 return connect_attempts;
2638 while((buf[count] != ':') && (count < frstat.st_size - 1))
2641 curr_state = OTHER_PEER_INDEX;
2644 if (1 == sscanf(&buf[count], ":"))
2645 curr_state = OTHER_PEER_INDEX;
2648 case OTHER_PEER_INDEX:
2649 if (1 != sscanf(&buf[count], "%u", &second_peer_index))
2651 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to peer index from topology file!\n");
2652 GNUNET_free_non_null(data);
2653 return connect_attempts;
2655 /* Assume file is written with first peer 1, but array index is 0 */
2656 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index - 1, list, GNUNET_YES);
2657 while((buf[count] != '\n') && (buf[count] != ',') && (count < frstat.st_size - 1))
2659 if (buf[count] == '\n')
2661 curr_state = PEER_INDEX;
2663 else if (buf[count] != ',')
2665 curr_state = OTHER_PEER_INDEX;
2670 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Found bad data in topology file while in state %d!\n", curr_state);
2672 return connect_attempts;
2677 /* Connect each peer to the next highest numbered peer */
2678 for (count = 0; count < pg->total - 1; count++)
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2682 "Connecting peer %d to peer %d\n", first_peer_index, second_peer_index);
2684 connect_attempts += proc (pg, first_peer_index, second_peer_index);
2687 return connect_attempts;
2691 * Create a topology given a peer group (set of running peers)
2692 * and a connection processor.
2694 * @param pg the peergroup to create the topology on
2695 * @param proc the connection processor to call to actually set
2696 * up connections between two peers
2697 * @param list the peer list to use
2699 * @return the number of connections that were set up
2703 create_ring (struct GNUNET_TESTING_PeerGroup *pg,
2704 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2707 int connect_attempts;
2709 connect_attempts = 0;
2711 /* Connect each peer to the next highest numbered peer */
2712 for (count = 0; count < pg->total - 1; count++)
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2716 "Connecting peer %d to peer %d\n", count, count + 1);
2718 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2721 /* Connect the last peer to the first peer */
2722 connect_attempts += proc (pg, pg->total - 1, 0, list, GNUNET_YES);
2724 return connect_attempts;
2729 * Iterator for writing friends of a peer to a file.
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
2737 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2738 * with a single file_iterator that takes a closure which contains
2739 * the prefix to write before the peer. Then this could be used
2740 * for blacklisting multiple transports and writing the friend
2741 * file. I'm sure *someone* will complain loudly about other
2742 * things that negate these functions even existing so no point in
2746 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2748 FILE *temp_friend_handle = cls;
2749 struct GNUNET_TESTING_Daemon *peer = value;
2750 struct GNUNET_PeerIdentity *temppeer;
2751 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2753 temppeer = &peer->id;
2754 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2755 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2760 struct BlacklistContext
2763 * The (open) file handle to write to
2765 FILE *temp_file_handle;
2768 * The transport that this peer will be blacklisted on.
2774 * Iterator for writing blacklist data to appropriate files.
2776 * @param cls closure, an open writable file handle
2777 * @param key the key the daemon was stored under
2778 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2780 * @return GNUNET_YES to continue iteration
2783 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2785 struct BlacklistContext *blacklist_ctx = cls;
2786 struct GNUNET_TESTING_Daemon *peer = value;
2787 struct GNUNET_PeerIdentity *temppeer;
2788 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2790 temppeer = &peer->id;
2791 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2792 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2793 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2794 blacklist_ctx->transport, (char *) &peer_enc);
2802 * Create the friend files based on the PeerConnection's
2803 * of each peer in the peer group, and copy the files
2804 * to the appropriate place
2806 * @param pg the peer group we are dealing with
2809 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
2811 FILE *temp_friend_handle;
2812 unsigned int pg_iter;
2813 char *temp_service_path;
2814 struct GNUNET_OS_Process **procarr;
2818 enum GNUNET_OS_ProcessStatusType type;
2819 unsigned long return_code;
2827 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2828 struct PeerConnection *conn_iter;
2830 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2831 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2833 mytemp = GNUNET_DISK_mktemp ("friends");
2834 GNUNET_assert (mytemp != NULL);
2835 temp_friend_handle = fopen (mytemp, "wt");
2836 GNUNET_assert (temp_friend_handle != NULL);
2838 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2839 while (conn_iter != NULL)
2841 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
2842 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2843 conn_iter = conn_iter->next;
2846 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2847 &friend_file_iterator,
2848 temp_friend_handle);
2850 fclose (temp_friend_handle);
2853 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2854 daemon->cfg, "PATHS",
2856 &temp_service_path))
2858 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2860 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2861 "SERVICEHOME", "PATHS");
2862 if (UNLINK (mytemp) != 0)
2863 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2865 GNUNET_free (mytemp);
2869 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2871 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2872 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2877 _("Copying file with command cp %s %s\n"), mytemp, arg);
2879 ret = GNUNET_OS_process_wait(procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2880 GNUNET_OS_process_close (procarr[pg_iter]);
2883 else /* Remote, scp the file to the correct place */
2885 if (NULL != pg->peers[pg_iter].daemon->username)
2886 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2887 pg->peers[pg_iter].daemon->username,
2888 pg->peers[pg_iter].daemon->hostname,
2891 GNUNET_asprintf (&arg, "%s:%s/friends",
2892 pg->peers[pg_iter].daemon->hostname,
2895 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
2898 ret = GNUNET_OS_process_wait(procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2899 GNUNET_OS_process_close (procarr[pg_iter]);
2900 if (ret != GNUNET_OK)
2902 procarr[pg_iter] = NULL;
2904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2905 _("Copying file with command scp %s %s\n"), mytemp,
2910 GNUNET_free (temp_service_path);
2911 GNUNET_free (mytemp);
2916 ret = GNUNET_SYSERR;
2917 while ((count < max_wait) && (ret != GNUNET_OK))
2920 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2924 _("Checking copy status of file %d\n"), pg_iter);
2926 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2928 if (GNUNET_OS_process_status
2929 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2931 ret = GNUNET_SYSERR;
2933 else if ((type != GNUNET_OS_PROCESS_EXITED)
2934 || (return_code != 0))
2936 ret = GNUNET_SYSERR;
2940 GNUNET_OS_process_close (procarr[pg_iter]);
2941 procarr[pg_iter] = NULL;
2943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2944 _("File %d copied\n"), pg_iter);
2950 if (ret == GNUNET_SYSERR)
2952 /* FIXME: why sleep here? -CG */
2958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2959 _("Finished copying all friend files!\n"));
2962 GNUNET_free (procarr);
2968 * Create the blacklist files based on the PeerConnection's
2969 * of each peer in the peer group, and copy the files
2970 * to the appropriate place.
2972 * @param pg the peer group we are dealing with
2973 * @param transports space delimited list of transports to blacklist
2976 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg,
2977 const char *transports)
2979 FILE *temp_file_handle;
2980 unsigned int pg_iter;
2981 char *temp_service_path;
2982 struct GNUNET_OS_Process **procarr;
2985 enum GNUNET_OS_ProcessStatusType type;
2986 unsigned long return_code;
2993 char *temp_transports;
2996 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2997 struct PeerConnection *conn_iter;
2999 static struct BlacklistContext blacklist_ctx;
3002 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
3003 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3005 mytemp = GNUNET_DISK_mktemp ("blacklist");
3006 GNUNET_assert (mytemp != NULL);
3007 temp_file_handle = fopen (mytemp, "wt");
3008 GNUNET_assert (temp_file_handle != NULL);
3009 temp_transports = GNUNET_strdup (transports);
3011 blacklist_ctx.temp_file_handle = temp_file_handle;
3013 transport_len = strlen (temp_transports) + 1;
3016 for (i = 0; i < transport_len; i++)
3018 if ((temp_transports[i] == ' ') && (pos == NULL))
3019 continue; /* At start of string (whitespace) */
3020 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
3022 temp_transports[i] = '\0';
3024 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
3025 while (conn_iter != NULL)
3027 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->id.hashPubKey, &peer_enc);
3028 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
3029 conn_iter = conn_iter->next;
3033 blacklist_ctx.transport = pos;
3034 entry_count = GNUNET_CONTAINER_multihashmap_iterate (pg->
3036 [pg_iter].blacklisted_peers,
3037 &blacklist_file_iterator,
3041 } /* At beginning of actual string */
3042 else if (pos == NULL)
3044 pos = &temp_transports[i];
3048 GNUNET_free (temp_transports);
3049 fclose (temp_file_handle);
3052 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
3053 daemon->cfg, "PATHS",
3055 &temp_service_path))
3057 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3059 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
3060 "SERVICEHOME", "PATHS");
3061 if (UNLINK (mytemp) != 0)
3062 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
3064 GNUNET_free (mytemp);
3068 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
3070 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
3071 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
3075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3076 _("Copying file with command cp %s %s\n"), mytemp, arg);
3081 else /* Remote, scp the file to the correct place */
3083 if (NULL != pg->peers[pg_iter].daemon->username)
3084 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
3085 pg->peers[pg_iter].daemon->username,
3086 pg->peers[pg_iter].daemon->hostname,
3089 GNUNET_asprintf (&arg, "%s:%s/blacklist",
3090 pg->peers[pg_iter].daemon->hostname,
3093 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg,
3096 GNUNET_OS_process_wait(procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
3099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3100 _("Copying file with command scp %s %s\n"), mytemp,
3105 GNUNET_free (temp_service_path);
3106 GNUNET_free (mytemp);
3110 ret = GNUNET_SYSERR;
3111 while ((count < max_wait) && (ret != GNUNET_OK))
3114 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3118 _("Checking copy status of file %d\n"), pg_iter);
3120 if (procarr[pg_iter] != NULL) /* Check for already completed! */
3122 if (GNUNET_OS_process_status
3123 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
3125 ret = GNUNET_SYSERR;
3127 else if ((type != GNUNET_OS_PROCESS_EXITED)
3128 || (return_code != 0))
3130 ret = GNUNET_SYSERR;
3134 GNUNET_OS_process_close (procarr[pg_iter]);
3135 procarr[pg_iter] = NULL;
3137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3138 _("File %d copied\n"), pg_iter);
3144 if (ret == GNUNET_SYSERR)
3146 /* FIXME: why sleep here? -CG */
3152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3153 _("Finished copying all blacklist files!\n"));
3155 GNUNET_free (procarr);
3159 /* Forward Declaration */
3161 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3164 * Choose a random peer's next connection to create, and
3165 * call schedule_connect to set up the connect task.
3167 * @param ct_ctx the overall connection context
3169 static void preschedule_connect(struct GNUNET_TESTING_PeerGroup *pg)
3171 struct ConnectTopologyContext *ct_ctx = &pg->ct_ctx;
3172 struct PeerConnection *connection_iter;
3173 struct ConnectContext *connect_context;
3174 uint32_t random_peer;
3176 if (ct_ctx->remaining_connections == 0)
3178 random_peer = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3179 while (pg->peers[random_peer].connect_peers_head == NULL)
3180 random_peer = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3182 connection_iter = pg->peers[random_peer].connect_peers_head;
3183 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3184 connect_context->first_index = random_peer;
3185 connect_context->second_index = connection_iter->index;
3186 connect_context->ct_ctx = ct_ctx;
3187 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3188 GNUNET_CONTAINER_DLL_remove(pg->peers[random_peer].connect_peers_head, pg->peers[random_peer].connect_peers_tail, connection_iter);
3189 GNUNET_free(connection_iter);
3190 ct_ctx->remaining_connections--;
3194 /* Forward declaration */
3195 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3199 * Close connections and free the hello context.
3201 * @param cls the 'struct SendHelloContext *'
3202 * @param tc scheduler context
3205 free_hello_context (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3207 struct SendHelloContext *send_hello_context = cls;
3208 if (send_hello_context->peer->daemon->server != NULL)
3210 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3211 send_hello_context->peer->daemon->server = NULL;
3213 if (send_hello_context->peer->daemon->th != NULL)
3215 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3216 send_hello_context->peer->daemon->th = NULL;
3218 if (send_hello_context->core_connect_task != GNUNET_SCHEDULER_NO_TASK)
3220 GNUNET_SCHEDULER_cancel(send_hello_context->core_connect_task);
3221 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3223 send_hello_context->pg->outstanding_connects--;
3224 GNUNET_free(send_hello_context);
3228 * For peers that haven't yet connected, notify
3229 * the caller that they have failed (timeout).
3231 * @param cls the 'struct SendHelloContext *'
3232 * @param tc scheduler context
3235 notify_remaining_connections_failed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3237 struct SendHelloContext *send_hello_context = cls;
3238 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3239 struct PeerConnection *connection;
3241 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3242 send_hello_context->peer->daemon->server = NULL;
3244 connection = send_hello_context->peer->connect_peers_head;
3246 while (connection != NULL)
3248 if (pg->notify_connection != NULL)
3250 pg->notify_connection(pg->notify_connection_cls,
3251 &send_hello_context->peer->daemon->id,
3252 &pg->peers[connection->index].daemon->id,
3254 send_hello_context->peer->daemon->cfg,
3255 pg->peers[connection->index].daemon->cfg,
3256 send_hello_context->peer->daemon,
3257 pg->peers[connection->index].daemon,
3258 "Peers failed to connect (timeout)");
3260 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3261 GNUNET_free(connection);
3262 connection = connection->next;
3264 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3266 other_peer = &pg->peers[connection->index];
3272 * For peers that haven't yet connected, send
3273 * CORE connect requests.
3275 * @param cls the 'struct SendHelloContext *'
3276 * @param tc scheduler context
3279 send_core_connect_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3281 struct SendHelloContext *send_hello_context = cls;
3282 struct PeerConnection *conn;
3283 GNUNET_assert(send_hello_context->peer->daemon->server != NULL);
3285 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3287 send_hello_context->connect_attempts++;
3288 if (send_hello_context->connect_attempts < send_hello_context->pg->ct_ctx.connect_attempts)
3290 conn = send_hello_context->peer->connect_peers_head;
3291 while (conn != NULL)
3293 GNUNET_CORE_peer_request_connect(send_hello_context->peer->daemon->server,
3294 GNUNET_TIME_relative_get_forever(),
3295 &send_hello_context->pg->peers[conn->index].daemon->id,
3300 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) ,
3301 &send_core_connect_requests,
3302 send_hello_context);
3306 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Timeout before all connections created, marking rest as failed!\n");
3307 GNUNET_SCHEDULER_add_now(¬ify_remaining_connections_failed, send_hello_context);
3314 * Success, connection is up. Signal client our success.
3316 * @param cls our "struct SendHelloContext"
3317 * @param peer identity of the peer that has connected
3318 * @param atsi performance information
3320 * FIXME: remove peers from BOTH lists, call notify twice, should
3321 * double the speed of connections as long as the list iteration
3322 * doesn't take too long!
3325 core_connect_notify (void *cls,
3326 const struct GNUNET_PeerIdentity *peer,
3327 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3329 struct SendHelloContext *send_hello_context = cls;
3330 struct PeerConnection *connection;
3331 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3333 struct PeerData *other_peer;
3336 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3337 "Connected peer %s to peer %s\n",
3338 ctx->d1->shortname, GNUNET_i2s(peer));
3341 if (0 == memcmp(&send_hello_context->peer->daemon->id, peer, sizeof(struct GNUNET_PeerIdentity)))
3344 connection = send_hello_context->peer->connect_peers_head;
3349 while ((connection != NULL) &&
3350 (0 != memcmp(&pg->peers[connection->index].daemon->id, peer, sizeof(struct GNUNET_PeerIdentity))))
3352 connection = connection->next;
3355 if (connection == NULL)
3357 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);
3362 other_peer = &pg->peers[connection->index];
3364 if (pg->notify_connection != NULL)
3366 pg->notify_connection(pg->notify_connection_cls,
3367 &send_hello_context->peer->daemon->id,
3370 send_hello_context->peer->daemon->cfg,
3371 pg->peers[connection->index].daemon->cfg,
3372 send_hello_context->peer->daemon,
3373 pg->peers[connection->index].daemon,
3376 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3377 GNUNET_free(connection);
3381 /* Notify of reverse connection and remove from other peers list of outstanding */
3382 if (other_peer != NULL)
3384 connection = other_peer->connect_peers_head;
3385 while ((connection != NULL) &&
3386 (0 != memcmp(&send_hello_context->peer->daemon->id, &pg->peers[connection->index].daemon->id, sizeof(struct GNUNET_PeerIdentity))))
3388 connection = connection->next;
3390 if (connection != NULL)
3392 if (pg->notify_connection != NULL)
3394 pg->notify_connection(pg->notify_connection_cls,
3396 &send_hello_context->peer->daemon->id,
3398 pg->peers[connection->index].daemon->cfg,
3399 send_hello_context->peer->daemon->cfg,
3400 pg->peers[connection->index].daemon,
3401 send_hello_context->peer->daemon,
3405 GNUNET_CONTAINER_DLL_remove(other_peer->connect_peers_head, other_peer->connect_peers_tail, connection);
3406 GNUNET_free(connection);
3411 if (send_hello_context->peer->connect_peers_head == NULL)
3413 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3418 * Notify of a successful connection to the core service.
3420 * @param cls a struct SendHelloContext *
3421 * @param server handle to the core service
3422 * @param my_identity the peer identity of this peer
3423 * @param publicKey the public key of the peer
3426 core_init (void *cls,
3427 struct GNUNET_CORE_Handle * server,
3428 const struct GNUNET_PeerIdentity *
3431 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
3434 struct SendHelloContext *send_hello_context = cls;
3435 send_hello_context->core_ready = GNUNET_YES;
3440 * Function called once a hello has been sent
3441 * to the transport, move on to the next one
3442 * or go away forever.
3444 * @param cls the 'struct SendHelloContext *'
3445 * @param tc scheduler context
3448 hello_sent_callback (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3450 struct SendHelloContext *send_hello_context = cls;
3451 //unsigned int pg_iter;
3452 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3454 GNUNET_free(send_hello_context);
3458 send_hello_context->pg->remaining_hellos--;
3460 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sent HELLO, have %d remaining!\n", send_hello_context->pg->remaining_hellos);
3462 if (send_hello_context->peer_pos == NULL) /* All HELLOs (for this peer!) have been transmitted! */
3465 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All hellos for this peer sent, disconnecting transport!\n");
3467 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3468 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3469 send_hello_context->peer->daemon->th = NULL;
3471 /*if (send_hello_context->pg->remaining_hellos == 0)
3473 for (pg_iter = 0; pg_iter < send_hello_context->pg->max_outstanding_connections; pg_iter++)
3475 preschedule_connect(&send_hello_context->pg->ct_ctx);
3479 GNUNET_assert (send_hello_context->peer->daemon->server == NULL);
3480 send_hello_context->peer->daemon->server = GNUNET_CORE_connect(send_hello_context->peer->cfg,
3484 &core_connect_notify,
3491 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),
3492 &send_core_connect_requests,
3493 send_hello_context);
3496 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3501 * Connect to a peer, give it all the HELLO's of those peers
3502 * we will later ask it to connect to.
3504 * @param ct_ctx the overall connection context
3506 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3508 struct SendHelloContext *send_hello_context = cls;
3509 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3511 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3513 GNUNET_free(send_hello_context);
3517 GNUNET_assert(send_hello_context->peer_pos != NULL); /* All of the HELLO sends to be scheduled have been scheduled! */
3519 if (((send_hello_context->peer->daemon->th == NULL) &&
3520 (pg->outstanding_connects > pg->max_outstanding_connections)) ||
3521 (pg->stop_connects == GNUNET_YES))
3523 #if VERBOSE_TESTING > 2
3524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3526 ("Delaying connect, we have too many outstanding connections!\n"));
3528 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3529 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3530 &schedule_send_hellos, send_hello_context);
3534 #if VERBOSE_TESTING > 2
3535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3536 _("Creating connection, outstanding_connections is %d\n"),
3537 outstanding_connects);
3539 if (send_hello_context->peer->daemon->th == NULL)
3541 pg->outstanding_connects++; /* Actual TRANSPORT, CORE connections! */
3542 send_hello_context->peer->daemon->th = GNUNET_TRANSPORT_connect(send_hello_context->peer->cfg,
3550 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3551 _("Offering Hello of peer %s to peer %s\n"),
3552 send_hello_context->peer->daemon->shortname, pg->peers[send_hello_context->peer_pos->index].daemon->shortname);
3554 GNUNET_TRANSPORT_offer_hello(send_hello_context->peer->daemon->th,
3555 (const struct GNUNET_MessageHeader *)pg->peers[send_hello_context->peer_pos->index].daemon->hello,
3556 &hello_sent_callback,
3557 send_hello_context);
3558 send_hello_context->peer_pos = send_hello_context->peer_pos->next;
3559 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3566 * Internal notification of a connection, kept so that we can ensure some connections
3567 * happen instead of flooding all testing daemons with requests to connect.
3570 internal_connect_notify (void *cls,
3571 const struct GNUNET_PeerIdentity *first,
3572 const struct GNUNET_PeerIdentity *second,
3574 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
3575 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
3576 struct GNUNET_TESTING_Daemon *first_daemon,
3577 struct GNUNET_TESTING_Daemon *second_daemon,
3580 struct ConnectContext *connect_ctx = cls;
3581 struct ConnectTopologyContext *ct_ctx = connect_ctx->ct_ctx;
3582 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
3583 struct PeerConnection *connection;
3584 pg->outstanding_connects--;
3587 * Check whether the inverse connection has been scheduled yet,
3588 * if not, we can remove it from the other peers list and avoid
3589 * even trying to connect them again!
3591 connection = pg->peers[connect_ctx->second_index].connect_peers_head;
3596 while ((connection != NULL) &&
3597 (0 != memcmp(first, &pg->peers[connection->index].daemon->id, sizeof(struct GNUNET_PeerIdentity))))
3599 connection = connection->next;
3602 if (connection != NULL) /* Can safely remove! */
3604 ct_ctx->remaining_connections--;
3605 if (pg->notify_connection != NULL) /* Notify of reverse connection */
3606 pg->notify_connection (pg->notify_connection_cls, second, first, distance,
3607 second_cfg, first_cfg, second_daemon, first_daemon,
3610 GNUNET_CONTAINER_DLL_remove(pg->peers[connect_ctx->second_index].connect_peers_head, pg->peers[connect_ctx->second_index].connect_peers_tail, connection);
3611 GNUNET_free(connection);
3614 if (ct_ctx->remaining_connections == 0)
3616 if (ct_ctx->notify_connections_done != NULL)
3617 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
3620 preschedule_connect(pg);
3622 if (pg->notify_connection != NULL)
3623 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
3624 first_cfg, second_cfg, first_daemon, second_daemon,
3627 GNUNET_free(connect_ctx);
3632 * Either delay a connection (because there are too many outstanding)
3633 * or schedule it for right now.
3635 * @param cls a connection context
3636 * @param tc the task runtime context
3639 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3641 struct ConnectContext *connect_context = cls;
3642 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
3644 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3647 if ((pg->outstanding_connects > pg->max_outstanding_connections) || (pg->stop_connects == GNUNET_YES))
3650 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3652 ("Delaying connect, we have too many outstanding connections!\n"));
3654 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3655 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3656 &schedule_connect, connect_context);
3661 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3662 _("Creating connection, outstanding_connections is %d (max %d)\n"),
3663 pg->outstanding_connects, pg->max_outstanding_connections);
3665 pg->outstanding_connects++;
3666 pg->total_connects_scheduled++;
3667 GNUNET_TESTING_daemons_connect (pg->peers[connect_context->first_index].daemon,
3668 pg->peers[connect_context->second_index].daemon,
3669 connect_context->ct_ctx->connect_timeout,
3670 connect_context->ct_ctx->connect_attempts,
3676 &internal_connect_notify,
3677 connect_context); /* FIXME: free connect context! */
3683 * Iterator for actually scheduling connections to be created
3684 * between two peers.
3686 * @param cls closure, a GNUNET_TESTING_Daemon
3687 * @param key the key the second Daemon was stored under
3688 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3690 * @return GNUNET_YES to continue iteration
3693 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3695 struct ConnectTopologyContext *ct_ctx = cls;
3696 struct PeerData *first = ct_ctx->first;
3697 struct GNUNET_TESTING_Daemon *second = value;
3698 struct ConnectContext *connect_context;
3700 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3701 connect_context->first = first->daemon;
3702 connect_context->second = second;
3703 connect_context->ct_ctx = ct_ctx;
3704 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3712 * Iterator for copying all entries in the allowed hashmap to the
3715 * @param cls closure, a GNUNET_TESTING_Daemon
3716 * @param key the key the second Daemon was stored under
3717 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3719 * @return GNUNET_YES to continue iteration
3722 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3724 struct PeerData *first = cls;
3726 GNUNET_assert (GNUNET_OK ==
3727 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
3729 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3736 * Make the peers to connect the same as those that are allowed to be
3739 * @param pg the peer group
3742 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
3744 unsigned int pg_iter;
3748 struct PeerConnection *iter;
3752 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3755 iter = pg->peers[pg_iter].allowed_peers_head;
3756 while (iter != NULL)
3758 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating connection between %d and %d\n", pg_iter, iter->index);
3759 total += add_connections(pg, pg_iter, iter->index, CONNECT, GNUNET_NO);
3760 //total += add_actual_connections(pg, pg_iter, iter->index);
3765 GNUNET_CONTAINER_multihashmap_iterate (pg->
3766 peers[pg_iter].allowed_peers,
3767 ©_topology_iterator,
3768 &pg->peers[pg_iter]);
3770 if (GNUNET_SYSERR == ret)
3771 return GNUNET_SYSERR;
3773 total = total + ret;
3781 * Connect the topology as specified by the PeerConnection's
3782 * of each peer in the peer group
3784 * @param pg the peer group we are dealing with
3785 * @param connect_timeout how long try connecting two peers
3786 * @param connect_attempts how many times (max) to attempt
3787 * @param notify_callback callback to notify when finished
3788 * @param notify_cls closure for notify callback
3790 * @return the number of connections that will be attempted
3793 connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3794 struct GNUNET_TIME_Relative connect_timeout,
3795 unsigned int connect_attempts,
3796 GNUNET_TESTING_NotifyCompletion notify_callback,
3799 unsigned int pg_iter;
3803 struct PeerConnection *connection_iter;
3806 struct SendHelloContext *send_hello_context
3810 pg->ct_ctx.notify_connections_done = notify_callback;
3811 pg->ct_ctx.notify_cls = notify_cls;
3814 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3817 connection_iter = pg->peers[pg_iter].connect_peers_head;
3818 while (connection_iter != NULL)
3820 connection_iter = connection_iter->next;
3825 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3832 pg->ct_ctx.connect_timeout = connect_timeout;
3833 pg->ct_ctx.connect_attempts = connect_attempts;
3834 pg->ct_ctx.remaining_connections = total;
3837 /* First give all peers the HELLO's of other peers (connect to first peer's transport service, give HELLO's of other peers, continue...) */
3838 pg->remaining_hellos = total;
3839 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3841 send_hello_context = GNUNET_malloc(sizeof(struct SendHelloContext));
3842 send_hello_context->peer = &pg->peers[pg_iter];
3843 send_hello_context->peer_pos = pg->peers[pg_iter].connect_peers_head;
3844 send_hello_context->pg = pg;
3845 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3848 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3850 preschedule_connect(pg);
3859 * Takes a peer group and creates a topology based on the
3860 * one specified. Creates a topology means generates friend
3861 * files for the peers so they can only connect to those allowed
3862 * by the topology. This will only have an effect once peers
3863 * are started if the FRIENDS_ONLY option is set in the base
3864 * config. Also takes an optional restrict topology which
3865 * disallows connections based on particular transports
3866 * UNLESS they are specified in the restricted topology.
3868 * @param pg the peer group struct representing the running peers
3869 * @param topology which topology to connect the peers in
3870 * @param restrict_topology disallow restrict_transports transport
3871 * connections to peers NOT in this topology
3872 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3873 * @param restrict_transports space delimited list of transports to blacklist
3874 * to create restricted topology
3876 * @return the maximum number of connections were all allowed peers
3877 * connected to each other
3880 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
3881 enum GNUNET_TESTING_Topology topology,
3882 enum GNUNET_TESTING_Topology
3884 const char *restrict_transports)
3888 unsigned int num_connections;
3889 int unblacklisted_connections;
3891 struct PeerConnection *conn_iter;
3892 struct PeerConnection *temp_conn;
3897 for (i = 0; i < pg->total; i++)
3899 pg->peers[i].allowed_peers =
3900 GNUNET_CONTAINER_multihashmap_create (100);
3901 pg->peers[i].connect_peers =
3902 GNUNET_CONTAINER_multihashmap_create (100);
3903 pg->peers[i].blacklisted_peers =
3904 GNUNET_CONTAINER_multihashmap_create (100);
3905 pg->peers[i].pg = pg;
3911 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3915 num_connections = create_clique (pg, &add_connections, ALLOWED, GNUNET_NO);
3917 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3920 _("Creating small world (ring) topology\n"));
3923 create_small_world_ring (pg, &add_connections, ALLOWED);
3925 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3928 _("Creating small world (2d-torus) topology\n"));
3930 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3932 case GNUNET_TESTING_TOPOLOGY_RING:
3934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3936 num_connections = create_ring (pg, &add_connections, ALLOWED);
3938 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3942 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3944 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3947 _("Creating Erdos-Renyi topology\n"));
3949 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3951 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3955 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3957 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3960 _("Creating Scale Free topology\n"));
3962 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3964 case GNUNET_TESTING_TOPOLOGY_LINE:
3966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3967 _("Creating straight line topology\n"));
3969 num_connections = create_line (pg, &add_connections, ALLOWED);
3971 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3974 _("Creating topology from file!\n"));
3977 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing", "topology_file",
3979 num_connections = create_from_file (pg, filename, &add_connections, ALLOWED);
3982 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3983 num_connections = 0;
3986 case GNUNET_TESTING_TOPOLOGY_NONE:
3988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3990 ("Creating no allowed topology (all peers can connect at core level)\n"));
3992 num_connections = pg->total * pg->total; /* Clique is allowed! */
3995 num_connections = 0;
4000 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
4002 ret = create_and_copy_friend_files (pg);
4003 if (ret != GNUNET_OK)
4006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4007 _("Failed during friend file copying!\n"));
4009 return GNUNET_SYSERR;
4014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4015 _("Friend files created/copied successfully!\n"));
4020 /* Use the create clique method to initially set all connections as blacklisted. */
4021 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
4022 create_clique (pg, &add_connections, BLACKLIST, GNUNET_NO);
4024 unblacklisted_connections = 0;
4025 /* Un-blacklist connections as per the topology specified */
4026 switch (restrict_topology)
4028 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4031 _("Blacklisting all but clique topology\n"));
4033 unblacklisted_connections =
4034 create_clique (pg, &remove_connections, BLACKLIST, GNUNET_NO);
4036 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4039 _("Blacklisting all but small world (ring) topology\n"));
4041 unblacklisted_connections =
4042 create_small_world_ring (pg, &remove_connections, BLACKLIST);
4044 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4048 ("Blacklisting all but small world (2d-torus) topology\n"));
4050 unblacklisted_connections =
4051 create_small_world (pg, &remove_connections, BLACKLIST);
4053 case GNUNET_TESTING_TOPOLOGY_RING:
4055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4056 _("Blacklisting all but ring topology\n"));
4058 unblacklisted_connections = create_ring (pg, &remove_connections, BLACKLIST);
4060 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4063 _("Blacklisting all but 2d torus topology\n"));
4065 unblacklisted_connections =
4066 create_2d_torus (pg, &remove_connections, BLACKLIST);
4068 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4071 _("Blacklisting all but Erdos-Renyi topology\n"));
4073 unblacklisted_connections =
4074 create_erdos_renyi (pg, &remove_connections, BLACKLIST);
4076 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4079 _("Blacklisting all but InterNAT topology\n"));
4083 for (off = 0; off < pg->total; off++)
4085 conn_iter = pg->peers[off].allowed_peers_head;
4086 while (conn_iter != NULL)
4088 temp_conn = conn_iter->next;
4089 GNUNET_free(conn_iter);
4090 conn_iter = temp_conn;
4092 pg->peers[off].allowed_peers_head = NULL;
4093 pg->peers[off].allowed_peers_tail = NULL;
4095 conn_iter = pg->peers[off].connect_peers_head;
4096 while (conn_iter != NULL)
4098 temp_conn = conn_iter->next;
4099 GNUNET_free(conn_iter);
4100 conn_iter = temp_conn;
4102 pg->peers[off].connect_peers_head = NULL;
4103 pg->peers[off].connect_peers_tail = NULL;
4105 unblacklisted_connections = create_nated_internet_copy(pg, &remove_connections, BLACKLIST);
4107 unblacklisted_connections =
4108 create_nated_internet (pg, &remove_connections, BLACKLIST);
4112 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4115 _("Blacklisting all but Scale Free topology\n"));
4117 unblacklisted_connections =
4118 create_scale_free (pg, &remove_connections, BLACKLIST);
4120 case GNUNET_TESTING_TOPOLOGY_LINE:
4122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4123 _("Blacklisting all but straight line topology\n"));
4125 unblacklisted_connections = create_line (pg, &remove_connections, BLACKLIST);
4127 case GNUNET_TESTING_TOPOLOGY_NONE: /* Fall through */
4128 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
4130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4132 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
4134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4136 ("Creating blacklist topology from allowed\n"));
4137 unblacklisted_connections = copy_allowed (pg, &remove_connections);
4142 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
4144 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n", restrict_transports);
4145 ret = create_and_copy_blacklist_files (pg, restrict_transports);
4146 if (ret != GNUNET_OK)
4149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4150 _("Failed during blacklist file copying!\n"));
4157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4158 _("Blacklist files created/copied successfully!\n"));
4162 return num_connections;
4168 * Iterator for choosing random peers to connect.
4170 * @param cls closure, a RandomContext
4171 * @param key the key the second Daemon was stored under
4172 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4174 * @return GNUNET_YES to continue iteration
4177 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4179 struct RandomContext *random_ctx = cls;
4180 double random_number;
4181 uint32_t second_pos;
4182 GNUNET_HashCode first_hash;
4185 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4186 UINT64_MAX)) / ((double) UINT64_MAX);
4187 if (random_number < random_ctx->percentage)
4189 GNUNET_assert (GNUNET_OK ==
4190 GNUNET_CONTAINER_multihashmap_put (random_ctx->
4191 first->connect_peers_working_set,
4193 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4196 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
4197 uid_from_hash (key, &second_pos);
4198 hash_from_uid (random_ctx->first_uid, &first_hash);
4199 GNUNET_assert (random_ctx->pg->total > second_pos);
4200 GNUNET_assert (GNUNET_YES ==
4201 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
4203 [second_pos].connect_peers,
4213 * Iterator for adding at least X peers to a peers connection set.
4215 * @param cls closure, MinimumContext
4216 * @param key the key the second Daemon was stored under
4217 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4219 * @return GNUNET_YES to continue iteration
4222 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4224 struct MinimumContext *min_ctx = cls;
4225 uint32_t second_pos;
4226 GNUNET_HashCode first_hash;
4229 if (GNUNET_CONTAINER_multihashmap_size
4230 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
4232 for (i = 0; i < min_ctx->num_to_add; i++)
4234 if (min_ctx->pg_array[i] == min_ctx->current)
4236 GNUNET_assert (GNUNET_OK ==
4237 GNUNET_CONTAINER_multihashmap_put
4238 (min_ctx->first->connect_peers_working_set, key,
4240 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4241 uid_from_hash (key, &second_pos);
4242 hash_from_uid (min_ctx->first_uid, &first_hash);
4243 GNUNET_assert (min_ctx->pg->total > second_pos);
4244 GNUNET_assert (GNUNET_OK ==
4245 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4247 [second_pos].connect_peers_working_set,
4251 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4252 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
4253 GNUNET_assert (GNUNET_YES ==
4254 GNUNET_CONTAINER_multihashmap_remove
4255 (min_ctx->pg->peers[second_pos].connect_peers,
4256 &first_hash, min_ctx->first->daemon));
4263 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
4269 * Iterator for adding peers to a connection set based on a depth first search.
4271 * @param cls closure, MinimumContext
4272 * @param key the key the second daemon was stored under
4273 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4275 * @return GNUNET_YES to continue iteration
4278 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4280 struct DFSContext *dfs_ctx = cls;
4281 GNUNET_HashCode first_hash;
4283 if (dfs_ctx->current == dfs_ctx->chosen)
4285 GNUNET_assert (GNUNET_OK ==
4286 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4287 first->connect_peers_working_set,
4289 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4290 uid_from_hash (key, &dfs_ctx->second_uid);
4291 hash_from_uid (dfs_ctx->first_uid, &first_hash);
4292 GNUNET_assert (GNUNET_OK ==
4293 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4295 [dfs_ctx->second_uid].connect_peers_working_set,
4299 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4300 GNUNET_assert (GNUNET_YES ==
4301 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
4303 [dfs_ctx->second_uid].connect_peers,
4307 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
4308 return GNUNET_NO; /* We have found our peer, don't iterate more */
4317 * From the set of connections possible, choose percentage percent of connections
4318 * to actually connect.
4320 * @param pg the peergroup we are dealing with
4321 * @param percentage what percent of total connections to make
4324 choose_random_connections (struct GNUNET_TESTING_PeerGroup *pg,
4327 struct RandomContext random_ctx;
4330 struct PeerConnection *temp_peers;
4331 struct PeerConnection *conn_iter;
4332 double random_number;
4335 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4337 random_ctx.first_uid = pg_iter;
4338 random_ctx.first = &pg->peers[pg_iter];
4339 random_ctx.percentage = percentage;
4343 conn_iter = pg->peers[pg_iter].connect_peers_head;
4344 while (conn_iter != NULL)
4348 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4349 UINT64_MAX)) / ((double) UINT64_MAX);
4350 if (random_number < percentage)
4352 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET, GNUNET_YES);
4354 conn_iter = conn_iter->next;
4357 pg->peers[pg_iter].connect_peers_working_set =
4358 GNUNET_CONTAINER_multihashmap_create (pg->total);
4359 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4360 &random_connect_iterator,
4362 /* Now remove the old connections */
4363 GNUNET_CONTAINER_multihashmap_destroy (pg->
4364 peers[pg_iter].connect_peers);
4365 /* And replace with the random set */
4366 pg->peers[pg_iter].connect_peers =
4367 pg->peers[pg_iter].connect_peers_working_set;
4371 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4373 conn_iter = pg->peers[pg_iter].connect_peers_head;
4374 while (pg->peers[pg_iter].connect_peers_head != NULL)
4375 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT, GNUNET_YES);
4377 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
4378 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
4379 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4380 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4386 * Count the number of connections in a linked list of connections.
4388 * @param conn_list the connection list to get the count of
4390 * @return the number of elements in the list
4393 count_connections (struct PeerConnection *conn_list)
4395 struct PeerConnection *iter;
4399 while (iter != NULL)
4409 count_workingset_connections (struct GNUNET_TESTING_PeerGroup *pg)
4412 unsigned int pg_iter;
4414 struct PeerConnection *conn_iter;
4418 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4421 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
4422 while (conn_iter != NULL)
4425 conn_iter = conn_iter->next;
4429 GNUNET_CONTAINER_multihashmap_size (pg->
4431 [pg_iter].connect_peers_working_set);
4440 count_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg)
4443 unsigned int pg_iter;
4445 struct PeerConnection *conn_iter;
4449 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4452 conn_iter = pg->peers[pg_iter].allowed_peers_head;
4453 while (conn_iter != NULL)
4456 conn_iter = conn_iter->next;
4460 GNUNET_CONTAINER_multihashmap_size (pg->
4462 [pg_iter].allowed_peers);
4471 * From the set of connections possible, choose at least num connections per
4474 * @param pg the peergroup we are dealing with
4475 * @param num how many connections at least should each peer have (if possible)?
4478 choose_minimum (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4481 struct MinimumContext minimum_ctx;
4483 struct PeerConnection *conn_iter;
4484 unsigned int temp_list_size;
4487 uint32_t random; /* Random list entry to connect peer to */
4492 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4494 temp_list_size = count_connections(pg->peers[pg_iter].connect_peers_head);
4495 if (temp_list_size == 0)
4497 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer %d has 0 connections!?!?\n", pg_iter);
4500 for (i = 0; i < num; i++)
4502 random = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, temp_list_size);
4503 conn_iter = pg->peers[pg_iter].connect_peers_head;
4504 for (count = 0; count < random; count++)
4505 conn_iter = conn_iter->next;
4506 /* We now have a random connection, connect it! */
4507 GNUNET_assert(conn_iter != NULL);
4508 add_connections(pg, pg_iter, conn_iter->index, WORKING_SET, GNUNET_YES);
4512 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4514 pg->peers[pg_iter].connect_peers_working_set =
4515 GNUNET_CONTAINER_multihashmap_create (num);
4518 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4520 minimum_ctx.first_uid = pg_iter;
4521 minimum_ctx.pg_array =
4522 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
4523 GNUNET_CONTAINER_multihashmap_size
4524 (pg->peers[pg_iter].connect_peers));
4525 minimum_ctx.first = &pg->peers[pg_iter];
4526 minimum_ctx.pg = pg;
4527 minimum_ctx.num_to_add = num;
4528 minimum_ctx.current = 0;
4529 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4530 &minimum_connect_iterator,
4534 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4536 /* Remove the "old" connections */
4537 GNUNET_CONTAINER_multihashmap_destroy (pg->
4538 peers[pg_iter].connect_peers);
4539 /* And replace with the working set */
4540 pg->peers[pg_iter].connect_peers =
4541 pg->peers[pg_iter].connect_peers_working_set;
4544 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4546 while (pg->peers[pg_iter].connect_peers_head != NULL)
4547 remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT, GNUNET_YES);
4549 pg->peers[pg_iter].connect_peers_head = pg->peers[pg_iter].connect_peers_working_set_head;
4550 pg->peers[pg_iter].connect_peers_tail = pg->peers[pg_iter].connect_peers_working_set_tail;
4551 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4552 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4557 struct FindClosestContext
4560 * The currently known closest peer.
4562 struct GNUNET_TESTING_Daemon *closest;
4565 * The info for the peer we are adding connections for.
4567 struct PeerData *curr_peer;
4570 * The distance (bits) between the current
4571 * peer and the currently known closest.
4573 unsigned int closest_dist;
4576 * The offset of the closest known peer in
4579 unsigned int closest_num;
4583 * Iterator over hash map entries of the allowed
4584 * peer connections. Find the closest, not already
4585 * connected peer and return it.
4587 * @param cls closure (struct FindClosestContext)
4588 * @param key current key code (hash of offset in pg)
4589 * @param value value in the hash map - a GNUNET_TESTING_Daemon
4590 * @return GNUNET_YES if we should continue to
4595 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
4597 struct FindClosestContext *closest_ctx = cls;
4598 struct GNUNET_TESTING_Daemon *daemon = value;
4600 if (((closest_ctx->closest == NULL) ||
4601 (GNUNET_CRYPTO_hash_matching_bits
4602 (&daemon->id.hashPubKey,
4603 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
4604 closest_ctx->closest_dist))
4606 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
4607 curr_peer->connect_peers,
4610 closest_ctx->closest_dist =
4611 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
4612 &closest_ctx->curr_peer->daemon->
4614 closest_ctx->closest = daemon;
4615 uid_from_hash (key, &closest_ctx->closest_num);
4622 * From the set of connections possible, choose at num connections per
4623 * peer based on depth which are closest out of those allowed. Guaranteed
4624 * to add num peers to connect to, provided there are that many peers
4625 * in the underlay topology to connect to.
4627 * @param pg the peergroup we are dealing with
4628 * @param num how many connections at least should each peer have (if possible)?
4629 * @param proc processor to actually add the connections
4630 * @param list the peer list to use
4633 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
4634 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
4639 struct FindClosestContext closest_ctx;
4644 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
4646 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4648 closest_ctx.curr_peer = &pg->peers[pg_iter];
4649 closest_ctx.closest = NULL;
4650 closest_ctx.closest_dist = 0;
4651 closest_ctx.closest_num = 0;
4652 GNUNET_CONTAINER_multihashmap_iterate (pg->
4653 peers[pg_iter].allowed_peers,
4654 &find_closest_peers,
4656 if (closest_ctx.closest != NULL)
4658 GNUNET_assert (closest_ctx.closest_num < pg->total);
4659 proc (pg, pg_iter, closest_ctx.closest_num, list);
4667 * From the set of connections possible, choose at least num connections per
4668 * peer based on depth first traversal of peer connections. If DFS leaves
4669 * peers unconnected, ensure those peers get connections.
4671 * @param pg the peergroup we are dealing with
4672 * @param num how many connections at least should each peer have (if possible)?
4675 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4679 uint32_t starting_peer;
4680 uint32_t least_connections;
4681 uint32_t random_connection;
4683 unsigned int temp_count;
4684 struct PeerConnection *peer_iter;
4686 struct DFSContext dfs_ctx;
4687 GNUNET_HashCode second_hash;
4693 while ((count_workingset_connections (pg) < num * pg->total)
4694 && (count_allowed_connections (pg) > 0))
4696 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4698 least_connections = -1; /* Set to very high number */
4699 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4701 temp_count = count_connections(pg->peers[pg_iter].connect_peers_working_set_head);
4702 if (temp_count < least_connections)
4704 starting_peer = pg_iter;
4705 least_connections = temp_count;
4710 temp_count = count_connections(pg->peers[starting_peer].connect_peers_head);
4711 if (temp_count == 0)
4712 continue; /* FIXME: infinite loop? */
4714 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, temp_count);
4716 peer_iter = pg->peers[starting_peer].connect_peers_head;
4717 while (temp_count < random_connection)
4719 peer_iter = peer_iter->next;
4722 GNUNET_assert(peer_iter != NULL);
4723 add_connections(pg, starting_peer, peer_iter->index, WORKING_SET, GNUNET_NO);
4724 remove_connections(pg, starting_peer, peer_iter->index, CONNECT, GNUNET_YES);
4725 starting_peer = peer_iter->index;
4730 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4732 pg->peers[pg_iter].connect_peers_working_set =
4733 GNUNET_CONTAINER_multihashmap_create (num);
4738 while ((count_workingset_connections (pg) < num * pg->total)
4739 && (count_allowed_connections (pg) > 0))
4741 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4743 least_connections = -1; /* Set to very high number */
4744 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4746 if (GNUNET_CONTAINER_multihashmap_size
4747 (pg->peers[pg_iter].connect_peers_working_set) <
4750 starting_peer = pg_iter;
4752 GNUNET_CONTAINER_multihashmap_size (pg->
4754 [pg_iter].connect_peers_working_set);
4759 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4765 /* Choose a random peer from the chosen peers set of connections to add */
4767 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4768 GNUNET_CONTAINER_multihashmap_size
4769 (pg->peers[starting_peer].connect_peers));
4770 dfs_ctx.first_uid = starting_peer;
4771 dfs_ctx.first = &pg->peers[starting_peer];
4773 dfs_ctx.current = 0;
4775 GNUNET_CONTAINER_multihashmap_iterate (pg->
4777 [starting_peer].connect_peers,
4778 &dfs_connect_iterator, &dfs_ctx);
4779 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4780 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4781 GNUNET_assert (GNUNET_YES ==
4782 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4783 [starting_peer].connect_peers,
4787 [dfs_ctx.second_uid].daemon));
4788 starting_peer = dfs_ctx.second_uid;
4791 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4793 /* Remove the "old" connections */
4794 GNUNET_CONTAINER_multihashmap_destroy (pg->
4795 peers[pg_iter].connect_peers);
4796 /* And replace with the working set */
4797 pg->peers[pg_iter].connect_peers =
4798 pg->peers[pg_iter].connect_peers_working_set;
4805 * Internal callback for topology information for a particular peer.
4808 internal_topology_callback (void *cls,
4809 const struct GNUNET_PeerIdentity *peer,
4810 const struct GNUNET_TRANSPORT_ATS_Information
4813 struct CoreContext *core_ctx = cls;
4814 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4816 if (peer == NULL) /* Either finished, or something went wrong */
4818 iter_ctx->completed++;
4819 iter_ctx->connected--;
4820 /* One core context allocated per iteration, must free! */
4821 GNUNET_free (core_ctx);
4825 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id,
4829 if (iter_ctx->completed == iter_ctx->total)
4831 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4832 /* Once all are done, free the iteration context */
4833 GNUNET_free (iter_ctx);
4839 * Check running topology iteration tasks, if below max start a new one, otherwise
4840 * schedule for some time in the future.
4843 schedule_get_topology (void *cls,
4844 const struct GNUNET_SCHEDULER_TaskContext *tc)
4846 struct CoreContext *core_context = cls;
4847 struct TopologyIterateContext *topology_context =
4848 (struct TopologyIterateContext *) core_context->iter_context;
4849 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4852 if (topology_context->connected > topology_context->pg->max_outstanding_connections)
4854 #if VERBOSE_TESTING > 2
4855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4857 ("Delaying connect, we have too many outstanding connections!\n"));
4859 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4860 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4861 &schedule_get_topology, core_context);
4865 #if VERBOSE_TESTING > 2
4866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4867 _("Creating connection, outstanding_connections is %d\n"),
4868 outstanding_connects);
4870 topology_context->connected++;
4873 GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4874 &internal_topology_callback,
4877 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4878 internal_topology_callback (core_context, NULL, NULL);
4884 * Iterate over all (running) peers in the peer group, retrieve
4885 * all connections that each currently has.
4888 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
4889 GNUNET_TESTING_NotifyTopology cb, void *cls)
4891 struct TopologyIterateContext *topology_context;
4892 struct CoreContext *core_ctx;
4894 unsigned int total_count;
4896 /* Allocate a single topology iteration context */
4897 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4898 topology_context->topology_cb = cb;
4899 topology_context->cls = cls;
4900 topology_context->pg = pg;
4902 for (i = 0; i < pg->total; i++)
4904 if (pg->peers[i].daemon->running == GNUNET_YES)
4906 /* Allocate one core context per core we need to connect to */
4907 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4908 core_ctx->daemon = pg->peers[i].daemon;
4909 /* Set back pointer to topology iteration context */
4910 core_ctx->iter_context = topology_context;
4911 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4915 if (total_count == 0)
4917 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4918 GNUNET_free (topology_context);
4921 topology_context->total = total_count;
4926 * Callback function to process statistic values.
4927 * This handler is here only really to insert a peer
4928 * identity (or daemon) so the statistics can be uniquely
4929 * tied to a single running peer.
4931 * @param cls closure
4932 * @param subsystem name of subsystem that created the statistic
4933 * @param name the name of the datum
4934 * @param value the current value
4935 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4936 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4939 internal_stats_callback (void *cls,
4940 const char *subsystem,
4941 const char *name, uint64_t value, int is_persistent)
4943 struct StatsCoreContext *core_context = cls;
4944 struct StatsIterateContext *stats_context =
4945 (struct StatsIterateContext *) core_context->iter_context;
4947 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4948 subsystem, name, value, is_persistent);
4952 * Internal continuation call for statistics iteration.
4954 * @param cls closure, the CoreContext for this iteration
4955 * @param success whether or not the statistics iterations
4956 * was canceled or not (we don't care)
4959 internal_stats_cont (void *cls, int success)
4961 struct StatsCoreContext *core_context = cls;
4962 struct StatsIterateContext *stats_context =
4963 (struct StatsIterateContext *) core_context->iter_context;
4965 stats_context->connected--;
4966 stats_context->completed++;
4968 if (stats_context->completed == stats_context->total)
4970 stats_context->cont (stats_context->cls, GNUNET_YES);
4971 GNUNET_free (stats_context);
4974 if (core_context->stats_handle != NULL)
4975 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4977 GNUNET_free (core_context);
4981 * Check running topology iteration tasks, if below max start a new one, otherwise
4982 * schedule for some time in the future.
4985 schedule_get_statistics (void *cls,
4986 const struct GNUNET_SCHEDULER_TaskContext *tc)
4988 struct StatsCoreContext *core_context = cls;
4989 struct StatsIterateContext *stats_context =
4990 (struct StatsIterateContext *) core_context->iter_context;
4992 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4995 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
4997 #if VERBOSE_TESTING > 2
4998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5000 ("Delaying connect, we have too many outstanding connections!\n"));
5002 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5003 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5004 &schedule_get_statistics, core_context);
5008 #if VERBOSE_TESTING > 2
5009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5010 _("Creating connection, outstanding_connections is %d\n"),
5011 outstanding_connects);
5014 stats_context->connected++;
5015 core_context->stats_handle =
5016 GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
5017 if (core_context->stats_handle == NULL)
5019 internal_stats_cont (core_context, GNUNET_NO);
5023 core_context->stats_get_handle =
5024 GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
5025 GNUNET_TIME_relative_get_forever (),
5026 &internal_stats_cont, &internal_stats_callback,
5028 if (core_context->stats_get_handle == NULL)
5029 internal_stats_cont (core_context, GNUNET_NO);
5034 struct DuplicateStats
5037 * Next item in the list
5039 struct DuplicateStats *next;
5042 * Nasty string, concatenation of relevant information.
5044 char *unique_string;
5048 * Check whether the combination of port/host/unix domain socket
5049 * already exists in the list of peers being checked for statistics.
5051 * @param pg the peergroup in question
5052 * @param specific_peer the peer we're concerned with
5053 * @param stats_list the list to return to the caller
5055 * @return GNUNET_YES if the statistics instance has been seen already,
5056 * GNUNET_NO if not (and we may have added it to the list)
5059 stats_check_existing (struct GNUNET_TESTING_PeerGroup *pg,
5060 struct PeerData *specific_peer,
5061 struct DuplicateStats **stats_list)
5063 struct DuplicateStats *pos;
5064 char *unix_domain_socket;
5065 unsigned long long port;
5068 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
5069 "single_statistics_per_host"))
5070 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
5074 GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg, "statistics",
5075 "unixpath", &unix_domain_socket))
5079 GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg, "statistics",
5082 GNUNET_free(unix_domain_socket);
5086 if (specific_peer->daemon->hostname != NULL)
5087 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
5088 unix_domain_socket, port);
5090 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
5094 if (0 == strcmp (to_match, pos->unique_string))
5096 GNUNET_free (unix_domain_socket);
5097 GNUNET_free (to_match);
5102 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
5103 pos->unique_string = to_match;
5104 pos->next = *stats_list;
5106 GNUNET_free (unix_domain_socket);
5111 * Iterate over all (running) peers in the peer group, retrieve
5112 * all statistics from each.
5115 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
5116 GNUNET_STATISTICS_Callback cont,
5117 GNUNET_TESTING_STATISTICS_Iterator proc,
5120 struct StatsIterateContext *stats_context;
5121 struct StatsCoreContext *core_ctx;
5123 unsigned int total_count;
5124 struct DuplicateStats *stats_list;
5125 struct DuplicateStats *pos;
5128 /* Allocate a single stats iteration context */
5129 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
5130 stats_context->cont = cont;
5131 stats_context->proc = proc;
5132 stats_context->cls = cls;
5133 stats_context->pg = pg;
5136 for (i = 0; i < pg->total; i++)
5138 if ((pg->peers[i].daemon->running == GNUNET_YES)
5140 stats_check_existing (pg, &pg->peers[i], &stats_list)))
5142 /* Allocate one core context per core we need to connect to */
5143 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
5144 core_ctx->daemon = pg->peers[i].daemon;
5145 /* Set back pointer to topology iteration context */
5146 core_ctx->iter_context = stats_context;
5147 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
5152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5153 "Retrieving stats from %u total instances.\n", total_count);
5154 stats_context->total = total_count;
5155 if (stats_list != NULL)
5160 GNUNET_free (pos->unique_string);
5161 stats_list = pos->next;
5163 pos = stats_list->next;
5170 * Stop the connection process temporarily.
5172 * @param pg the peer group to stop connecting
5174 void GNUNET_TESTING_stop_connections(struct GNUNET_TESTING_PeerGroup *pg)
5176 pg->stop_connects = GNUNET_YES;
5180 * Resume the connection process temporarily.
5182 * @param pg the peer group to resume connecting
5184 void GNUNET_TESTING_resume_connections(struct GNUNET_TESTING_PeerGroup *pg)
5186 pg->stop_connects = GNUNET_NO;
5190 * There are many ways to connect peers that are supported by this function.
5191 * To connect peers in the same topology that was created via the
5192 * GNUNET_TESTING_create_topology, the topology variable must be set to
5193 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
5194 * a new instance of that topology will be generated and attempted to be
5195 * connected. This could result in some connections being impossible,
5196 * because some topologies are non-deterministic.
5198 * @param pg the peer group struct representing the running peers
5199 * @param topology which topology to connect the peers in
5200 * @param options options for connecting the topology
5201 * @param option_modifier modifier for options that take a parameter
5202 * @param connect_timeout how long to wait before giving up on connecting
5204 * @param connect_attempts how many times to attempt to connect two peers
5205 * over the connect_timeout duration
5206 * @param notify_callback notification to be called once all connections completed
5207 * @param notify_cls closure for notification callback
5209 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
5212 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
5213 enum GNUNET_TESTING_Topology topology,
5214 enum GNUNET_TESTING_TopologyOption options,
5215 double option_modifier,
5216 struct GNUNET_TIME_Relative connect_timeout,
5217 unsigned int connect_attempts,
5218 GNUNET_TESTING_NotifyCompletion
5219 notify_callback, void *notify_cls)
5223 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
5224 #if VERBOSE_TOPOLOGY
5225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5226 _("Creating clique CONNECT topology\n"));
5228 create_clique (pg, &add_connections, CONNECT, GNUNET_NO);
5230 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
5231 #if VERBOSE_TOPOLOGY
5232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5233 _("Creating small world (ring) CONNECT topology\n"));
5235 create_small_world_ring (pg, &add_connections, CONNECT);
5237 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
5238 #if VERBOSE_TOPOLOGY
5239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5240 _("Creating small world (2d-torus) CONNECT topology\n"));
5242 create_small_world (pg, &add_connections, CONNECT);
5244 case GNUNET_TESTING_TOPOLOGY_RING:
5245 #if VERBOSE_TOPOLOGY
5246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5247 _("Creating ring CONNECT topology\n"));
5249 create_ring (pg, &add_connections, CONNECT);
5251 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
5252 #if VERBOSE_TOPOLOGY
5253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5254 _("Creating 2d torus CONNECT topology\n"));
5256 create_2d_torus (pg, &add_connections, CONNECT);
5258 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
5259 #if VERBOSE_TOPOLOGY
5260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5261 _("Creating Erdos-Renyi CONNECT topology\n"));
5263 create_erdos_renyi (pg, &add_connections, CONNECT);
5265 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
5266 #if VERBOSE_TOPOLOGY
5267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5268 _("Creating InterNAT CONNECT topology\n"));
5270 create_nated_internet (pg, &add_connections, CONNECT);
5272 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
5273 #if VERBOSE_TOPOLOGY
5274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5275 _("Creating Scale Free CONNECT topology\n"));
5277 create_scale_free (pg, &add_connections, CONNECT);
5279 case GNUNET_TESTING_TOPOLOGY_LINE:
5280 #if VERBOSE_TOPOLOGY
5281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5282 _("Creating straight line CONNECT topology\n"));
5284 create_line (pg, &add_connections, CONNECT);
5286 case GNUNET_TESTING_TOPOLOGY_NONE:
5287 #if VERBOSE_TOPOLOGY
5288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5289 _("Creating no CONNECT topology\n"));
5291 copy_allowed_topology (pg);
5294 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5296 ("Unknown topology specification, can't connect peers!\n"));
5297 return GNUNET_SYSERR;
5302 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
5303 #if VERBOSE_TOPOLOGY
5304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5306 ("Connecting random subset (%'.2f percent) of possible peers\n"),
5307 100 * option_modifier);
5309 choose_random_connections (pg, option_modifier);
5311 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
5312 #if VERBOSE_TOPOLOGY
5313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5314 _("Connecting a minimum of %u peers each (if possible)\n"),
5315 (unsigned int) option_modifier);
5317 choose_minimum (pg, (unsigned int) option_modifier);
5319 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
5320 #if VERBOSE_TOPOLOGY
5321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5323 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
5324 (unsigned int) option_modifier);
5327 perform_dfs (pg, (int) option_modifier);
5330 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
5331 #if VERBOSE_TOPOLOGY
5332 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5334 ("Finding additional %u closest peers each (if possible)\n"),
5335 (unsigned int) option_modifier);
5338 add_closest (pg, (unsigned int) option_modifier,
5339 &add_connections, CONNECT);
5342 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
5344 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
5350 return connect_topology (pg, connect_timeout, connect_attempts, notify_callback, notify_cls);
5354 * Lookup and return the number of SSH connections to a host.
5356 * @param hostname the hostname to lookup in the list
5357 * @param pg the peergroup that the host belongs to
5359 * @return the number of current ssh connections to the host
5362 count_outstanding_at_host(const char *hostname, struct GNUNET_TESTING_PeerGroup *pg)
5364 struct OutstandingSSH *pos;
5366 while ((pos != NULL) && (strcmp(pos->hostname, hostname) != 0))
5368 GNUNET_assert(pos != NULL);
5369 return pos->outstanding;
5374 * Increment the number of SSH connections to a host by one.
5376 * @param hostname the hostname to lookup in the list
5377 * @param pg the peergroup that the host belongs to
5381 increment_outstanding_at_host(const char *hostname, struct GNUNET_TESTING_PeerGroup *pg)
5383 struct OutstandingSSH *pos;
5385 while ((pos != NULL) && (strcmp(pos->hostname, hostname) != 0))
5387 GNUNET_assert(pos != NULL);
5392 * Decrement the number of SSH connections to a host by one.
5394 * @param hostname the hostname to lookup in the list
5395 * @param pg the peergroup that the host belongs to
5399 decrement_outstanding_at_host(const char *hostname, struct GNUNET_TESTING_PeerGroup *pg)
5401 struct OutstandingSSH *pos;
5403 while ((pos != NULL) && (strcmp(pos->hostname, hostname) != 0))
5405 GNUNET_assert(pos != NULL);
5411 * Callback that is called whenever a hostkey is generated
5412 * for a peer. Call the real callback and decrement the
5413 * starting counter for the peergroup.
5415 * @param cls closure
5416 * @param id identifier for the daemon, NULL on error
5417 * @param d handle for the daemon
5418 * @param emsg error message (NULL on success)
5421 internal_hostkey_callback (void *cls,
5422 const struct GNUNET_PeerIdentity *id,
5423 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5425 struct InternalStartContext *internal_context = cls;
5426 internal_context->peer->pg->starting--;
5427 internal_context->peer->pg->started++;
5428 if (internal_context->hostname != NULL)
5429 decrement_outstanding_at_host(internal_context->hostname, internal_context->peer->pg);
5430 if (internal_context->hostkey_callback != NULL)
5431 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
5433 else if (internal_context->peer->pg->started ==
5434 internal_context->peer->pg->total)
5436 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
5437 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
5442 * Callback that is called whenever a peer has finished starting.
5443 * Call the real callback and decrement the starting counter
5444 * for the peergroup.
5446 * @param cls closure
5447 * @param id identifier for the daemon, NULL on error
5449 * @param d handle for the daemon
5450 * @param emsg error message (NULL on success)
5453 internal_startup_callback (void *cls,
5454 const struct GNUNET_PeerIdentity *id,
5455 const struct GNUNET_CONFIGURATION_Handle *cfg,
5456 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5458 struct InternalStartContext *internal_context = cls;
5459 internal_context->peer->pg->starting--;
5460 if (internal_context->hostname != NULL)
5461 decrement_outstanding_at_host(internal_context->hostname, internal_context->peer->pg);
5462 if (internal_context->start_cb != NULL)
5463 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
5468 internal_continue_startup (void *cls,
5469 const struct GNUNET_SCHEDULER_TaskContext *tc)
5471 struct InternalStartContext *internal_context = cls;
5473 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
5478 if ((internal_context->peer->pg->starting < internal_context->peer->pg->max_concurrent_ssh) ||
5479 ((internal_context->hostname != NULL) &&
5480 (count_outstanding_at_host(internal_context->hostname, internal_context->peer->pg) < internal_context->peer->pg->max_concurrent_ssh)))
5482 if (internal_context->hostname != NULL)
5483 increment_outstanding_at_host(internal_context->hostname, internal_context->peer->pg);
5484 internal_context->peer->pg->starting++;
5485 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
5489 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5490 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5491 &internal_continue_startup,
5498 * Callback for informing us about a successful
5499 * or unsuccessful churn start call.
5501 * @param cls a ChurnContext
5502 * @param id the peer identity of the started peer
5503 * @param cfg the handle to the configuration of the peer
5504 * @param d handle to the daemon for the peer
5505 * @param emsg NULL on success, non-NULL on failure
5509 churn_start_callback (void *cls,
5510 const struct GNUNET_PeerIdentity *id,
5511 const struct GNUNET_CONFIGURATION_Handle *cfg,
5512 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5514 struct ChurnRestartContext *startup_ctx = cls;
5515 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
5517 unsigned int total_left;
5518 char *error_message;
5520 error_message = NULL;
5523 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5524 "Churn stop callback failed with error `%s'\n", emsg);
5525 churn_ctx->num_failed_start++;
5529 churn_ctx->num_to_start--;
5533 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5534 "Started peer, %d left.\n", churn_ctx->num_to_start);
5538 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
5539 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5541 if (total_left == 0)
5543 if ((churn_ctx->num_failed_stop > 0)
5544 || (churn_ctx->num_failed_start > 0))
5545 GNUNET_asprintf (&error_message,
5546 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5547 churn_ctx->num_failed_start,
5548 churn_ctx->num_failed_stop);
5549 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5550 GNUNET_free_non_null (error_message);
5551 GNUNET_free (churn_ctx);
5552 GNUNET_free (startup_ctx);
5558 schedule_churn_restart (void *cls,
5559 const struct GNUNET_SCHEDULER_TaskContext *tc)
5561 struct PeerRestartContext *peer_restart_ctx = cls;
5562 struct ChurnRestartContext *startup_ctx =
5563 peer_restart_ctx->churn_restart_ctx;
5565 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5566 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5567 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5568 &schedule_churn_restart, peer_restart_ctx);
5571 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
5572 startup_ctx->timeout,
5573 &churn_start_callback,
5575 GNUNET_free (peer_restart_ctx);
5582 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5584 struct InternalStartContext *internal_context = cls;
5586 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
5591 if ((internal_context->peer->pg->starting < internal_context->peer->pg->max_concurrent_ssh) ||
5592 ((internal_context->hostname != NULL) &&
5593 (count_outstanding_at_host(internal_context->hostname, internal_context->peer->pg) < internal_context->peer->pg->max_concurrent_ssh)))
5595 if (internal_context->hostname != NULL)
5596 increment_outstanding_at_host(internal_context->hostname, internal_context->peer->pg);
5597 internal_context->peer->pg->starting++;
5598 internal_context->peer->daemon =
5599 GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
5600 internal_context->timeout,
5601 internal_context->hostname,
5602 internal_context->username,
5603 internal_context->sshport,
5604 internal_context->hostkey,
5605 &internal_hostkey_callback,
5607 &internal_startup_callback,
5612 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5613 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5614 &internal_start, internal_context);
5619 * Function which continues a peer group starting up
5620 * after successfully generating hostkeys for each peer.
5622 * @param pg the peer group to continue starting
5626 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg)
5631 for (i = 0; i < pg->total; i++)
5633 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5634 &pg->peers[i].internal_context);
5635 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
5640 * Start count gnunet instances with the same set of transports and
5641 * applications. The port numbers (any option called "PORT") will be
5642 * adjusted to ensure that no two peers running on the same system
5643 * have the same port(s) in their respective configurations.
5645 * @param cfg configuration template to use
5646 * @param total number of daemons to start
5647 * @param max_concurrent_connections for testing, how many peers can
5648 * we connect to simultaneously
5649 * @param max_concurrent_ssh when starting with ssh, how many ssh
5650 * connections will we allow at once (based on remote hosts allowed!)
5651 * @param timeout total time allowed for peers to start
5652 * @param hostkey_callback function to call on each peers hostkey generation
5653 * if NULL, peers will be started by this call, if non-null,
5654 * GNUNET_TESTING_daemons_continue_startup must be called after
5655 * successful hostkey generation
5656 * @param hostkey_cls closure for hostkey callback
5657 * @param cb function to call on each daemon that was started
5658 * @param cb_cls closure for cb
5659 * @param connect_callback function to call each time two hosts are connected
5660 * @param connect_callback_cls closure for connect_callback
5661 * @param hostnames linked list of host structs to use to start peers on
5662 * (NULL to run on localhost only)
5664 * @return NULL on error, otherwise handle to control peer group
5666 struct GNUNET_TESTING_PeerGroup *
5667 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
5669 unsigned int max_concurrent_connections,
5670 unsigned int max_concurrent_ssh,
5671 struct GNUNET_TIME_Relative timeout,
5672 GNUNET_TESTING_NotifyHostkeyCreated
5673 hostkey_callback, void *hostkey_cls,
5674 GNUNET_TESTING_NotifyDaemonRunning cb,
5676 GNUNET_TESTING_NotifyConnection
5677 connect_callback, void *connect_callback_cls,
5678 const struct GNUNET_TESTING_Host *hostnames)
5680 struct GNUNET_TESTING_PeerGroup *pg;
5681 const struct GNUNET_TESTING_Host *hostpos;
5687 const char *hostname;
5688 const char *username;
5689 char *baseservicehome;
5690 char *newservicehome;
5692 char *hostkeys_file;
5695 struct GNUNET_DISK_FileHandle *fd;
5696 struct GNUNET_CONFIGURATION_Handle *pcfg;
5698 unsigned int hostcnt;
5705 uint64_t total_hostkeys;
5706 struct GNUNET_OS_Process *proc;
5716 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
5718 pg->notify_connection = connect_callback;
5719 pg->notify_connection_cls = connect_callback_cls;
5721 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
5722 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
5723 pg->max_outstanding_connections = max_concurrent_connections;
5724 pg->max_concurrent_ssh = max_concurrent_ssh;
5725 if (NULL != hostnames)
5728 hostpos = hostnames;
5729 while (hostpos != NULL)
5731 hostpos = hostpos->next;
5734 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5737 hostpos = hostnames;
5738 while (hostpos != NULL)
5740 pg->hosts[off].minport = LOW_PORT;
5741 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
5742 if (hostpos->username != NULL)
5743 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
5744 pg->hosts[off].sshport = hostpos->port;
5745 hostpos = hostpos->next;
5755 pg->num_hosts = off;
5759 /* skip leading spaces */
5760 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
5763 while ('\0' != *rpos)
5765 if (isspace ((unsigned char) *rpos))
5769 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5771 start = GNUNET_strdup (hostnames);
5773 while ('\0' != *pos)
5775 if (isspace ((unsigned char) *pos))
5778 if (strlen (start) > 0)
5780 pg->hosts[off].minport = LOW_PORT;
5781 pg->hosts[off++].hostname = start;
5787 if (strlen (start) > 0)
5789 pg->hosts[off].minport = LOW_PORT;
5790 pg->hosts[off++].hostname = start;
5794 GNUNET_free (start);
5795 GNUNET_free (pg->hosts);
5799 minport = 0; /* make gcc happy */
5808 /* Create the servicehome directory for each remote peer */
5809 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
5811 for (i = 0; i < pg->num_hosts; i++)
5813 struct OutstandingSSH *ssh_entry;
5814 ssh_entry = GNUNET_malloc(sizeof(struct OutstandingSSH));
5815 ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
5816 GNUNET_CONTAINER_DLL_insert(pg->ssh_head, pg->ssh_tail, ssh_entry);
5817 if (NULL != pg->hosts[i].username)
5818 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username, pg->hosts[i].hostname);
5820 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
5821 if (pg->hosts[i].sshport != 0)
5823 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
5824 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
5825 "ssh", "-P", ssh_port_str,
5829 arg, "mkdir -p", baseservicehome, NULL);
5832 proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
5833 "ssh", arg, "mkdir -p", baseservicehome, NULL);
5834 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating remote dir with command ssh %s %s %s\n", arg, " mkdir -p ", baseservicehome);
5835 GNUNET_OS_process_wait(proc);
5837 GNUNET_free(baseservicehome);
5839 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "HOSTKEYSFILE",
5842 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
5843 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!\n");
5846 /* Check hostkey file size, read entire thing into memory */
5847 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
5848 GNUNET_DISK_PERM_NONE);
5851 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
5855 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
5858 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Found file size %llu for hostkeys, expect hostkeys to be size %d\n", fs, HOSTKEYFILESIZE);
5860 if (fs % HOSTKEYFILESIZE != 0)
5862 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "File size %llu seems incorrect for hostkeys...\n", fs);
5866 total_hostkeys = fs / HOSTKEYFILESIZE;
5867 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Will read %llu hostkeys from file\n", total_hostkeys);
5868 pg->hostkey_data = GNUNET_malloc_large (fs);
5869 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
5870 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
5873 GNUNET_free(hostkeys_file);
5876 for (off = 0; off < total; off++)
5880 hostname = pg->hosts[off % hostcnt].hostname;
5881 username = pg->hosts[off % hostcnt].username;
5882 sshport = pg->hosts[off % hostcnt].sshport;
5883 pcfg = make_config (cfg,
5885 &pg->hosts[off % hostcnt].minport,
5886 &upnum, hostname, &fdnum);
5893 pcfg = make_config (cfg, off, &minport, &upnum, hostname, &fdnum);
5898 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5900 ("Could not create configuration for peer number %u on `%s'!\n"),
5901 off, hostname == NULL ? "localhost" : hostname);
5906 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
5909 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
5910 GNUNET_free (baseservicehome);
5914 tmpdir = getenv ("TMPDIR");
5915 tmpdir = tmpdir ? tmpdir : "/tmp";
5916 GNUNET_asprintf (&newservicehome,
5918 tmpdir, "gnunet-testing-test-test", off);
5920 GNUNET_CONFIGURATION_set_value_string (pcfg,
5922 "SERVICEHOME", newservicehome);
5923 GNUNET_free (newservicehome);
5924 pg->peers[off].cfg = pcfg;
5926 /* Can we do this later? */
5927 pg->peers[off].allowed_peers =
5928 GNUNET_CONTAINER_multihashmap_create (total);
5929 pg->peers[off].connect_peers =
5930 GNUNET_CONTAINER_multihashmap_create (total);
5931 pg->peers[off].blacklisted_peers =
5932 GNUNET_CONTAINER_multihashmap_create (total);
5935 pg->peers[off].pg = pg;
5936 pg->peers[off].internal_context.peer = &pg->peers[off];
5937 pg->peers[off].internal_context.timeout = timeout;
5938 pg->peers[off].internal_context.hostname = hostname;
5939 pg->peers[off].internal_context.username = username;
5940 pg->peers[off].internal_context.sshport = sshport;
5941 if (pg->hostkey_data != NULL)
5942 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off * HOSTKEYFILESIZE];
5943 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
5944 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
5945 pg->peers[off].internal_context.start_cb = cb;
5946 pg->peers[off].internal_context.start_cb_cls = cb_cls;
5948 GNUNET_SCHEDULER_add_now (&internal_start,
5949 &pg->peers[off].internal_context);
5956 * Get a daemon by number, so callers don't have to do nasty
5957 * offsetting operation.
5959 struct GNUNET_TESTING_Daemon *
5960 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
5961 unsigned int position)
5963 if (position < pg->total)
5964 return pg->peers[position].daemon;
5970 * Get a daemon by peer identity, so callers can
5971 * retrieve the daemon without knowing it's offset.
5973 * @param pg the peer group to retrieve the daemon from
5974 * @param peer_id the peer identity of the daemon to retrieve
5976 * @return the daemon on success, or NULL if no such peer identity is found
5978 struct GNUNET_TESTING_Daemon *
5979 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
5980 struct GNUNET_PeerIdentity *peer_id)
5984 for (i = 0; i < pg->total; i++)
5987 memcmp (&pg->peers[i].daemon->id, peer_id,
5988 sizeof (struct GNUNET_PeerIdentity)))
5989 return pg->peers[i].daemon;
5996 * Prototype of a function that will be called when a
5997 * particular operation was completed the testing library.
5999 * @param cls closure (a struct RestartContext)
6000 * @param id id of the peer that was restarted
6001 * @param cfg handle to the configuration of the peer
6002 * @param d handle to the daemon that was restarted
6003 * @param emsg NULL on success
6006 restart_callback (void *cls,
6007 const struct GNUNET_PeerIdentity *id,
6008 const struct GNUNET_CONFIGURATION_Handle *cfg,
6009 struct GNUNET_TESTING_Daemon *d, const char *emsg)
6011 struct RestartContext *restart_context = cls;
6015 restart_context->peers_restarted++;
6019 restart_context->peers_restart_failed++;
6022 if (restart_context->peers_restarted == restart_context->peer_group->total)
6024 restart_context->callback (restart_context->callback_cls, NULL);
6025 GNUNET_free (restart_context);
6027 else if (restart_context->peers_restart_failed +
6028 restart_context->peers_restarted ==
6029 restart_context->peer_group->total)
6031 restart_context->callback (restart_context->callback_cls,
6032 "Failed to restart peers!");
6033 GNUNET_free (restart_context);
6039 * Callback for informing us about a successful
6040 * or unsuccessful churn stop call.
6042 * @param cls a ChurnContext
6043 * @param emsg NULL on success, non-NULL on failure
6047 churn_stop_callback (void *cls, const char *emsg)
6049 struct ShutdownContext *shutdown_ctx = cls;
6050 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
6051 unsigned int total_left;
6052 char *error_message;
6054 error_message = NULL;
6055 shutdown_ctx->outstanding--;
6059 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6060 "Churn stop callback failed with error `%s'\n", emsg);
6061 churn_ctx->num_failed_stop++;
6065 churn_ctx->num_to_stop--;
6069 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6070 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
6073 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
6074 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
6076 if (total_left == 0)
6078 if ((churn_ctx->num_failed_stop > 0)
6079 || (churn_ctx->num_failed_start > 0))
6081 GNUNET_asprintf (&error_message,
6082 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
6083 churn_ctx->num_failed_start,
6084 churn_ctx->num_failed_stop);
6086 churn_ctx->cb (churn_ctx->cb_cls, error_message);
6087 GNUNET_free_non_null (error_message);
6088 GNUNET_free (churn_ctx);
6089 GNUNET_free (shutdown_ctx);
6094 * Count the number of running peers.
6096 * @param pg handle for the peer group
6098 * @return the number of currently running peers in the peer group
6101 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
6104 unsigned int running = 0;
6105 for (i = 0; i < pg->total; i++)
6107 if (pg->peers[i].daemon->running == GNUNET_YES)
6109 GNUNET_assert (running != -1);
6117 * Task to rate limit the number of outstanding peer shutdown
6118 * requests. This is necessary for making sure we don't do
6119 * too many ssh connections at once, but is generally nicer
6120 * to any system as well (graduated task starts, as opposed
6121 * to calling gnunet-arm N times all at once).
6124 schedule_churn_shutdown_task (void *cls,
6125 const struct GNUNET_SCHEDULER_TaskContext *tc)
6127 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6128 struct ShutdownContext *shutdown_ctx;
6129 struct ChurnContext *churn_ctx;
6130 GNUNET_assert (peer_shutdown_ctx != NULL);
6131 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6132 GNUNET_assert (shutdown_ctx != NULL);
6133 churn_ctx = (struct ChurnContext *)shutdown_ctx->cb_cls;
6134 if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
6135 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
6136 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
6137 &schedule_churn_shutdown_task,
6141 shutdown_ctx->outstanding++;
6142 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6143 shutdown_ctx->timeout, shutdown_ctx->cb,
6144 shutdown_ctx, GNUNET_NO, GNUNET_YES);
6145 GNUNET_free (peer_shutdown_ctx);
6150 * Simulate churn by stopping some peers (and possibly
6151 * re-starting others if churn is called multiple times). This
6152 * function can only be used to create leave-join churn (peers "never"
6153 * leave for good). First "voff" random peers that are currently
6154 * online will be taken offline; then "von" random peers that are then
6155 * offline will be put back online. No notifications will be
6156 * generated for any of these operations except for the callback upon
6159 * @param pg handle for the peer group
6160 * @param voff number of peers that should go offline
6161 * @param von number of peers that should come back online;
6162 * must be zero on first call (since "testbed_start"
6163 * always starts all of the peers)
6164 * @param timeout how long to wait for operations to finish before
6166 * @param cb function to call at the end
6167 * @param cb_cls closure for cb
6170 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
6173 struct GNUNET_TIME_Relative timeout,
6174 GNUNET_TESTING_NotifyCompletion cb,
6177 struct ChurnContext *churn_ctx;
6178 struct ShutdownContext *shutdown_ctx;
6179 struct PeerShutdownContext *peer_shutdown_ctx;
6180 struct PeerRestartContext *peer_restart_ctx;
6181 struct ChurnRestartContext *churn_startup_ctx;
6183 unsigned int running;
6184 unsigned int stopped;
6185 unsigned int total_running;
6186 unsigned int total_stopped;
6188 unsigned int *running_arr;
6189 unsigned int *stopped_arr;
6190 unsigned int *running_permute;
6191 unsigned int *stopped_permute;
6196 if ((von == 0) && (voff == 0)) /* No peers at all? */
6202 for (i = 0; i < pg->total; i++)
6204 if (pg->peers[i].daemon->running == GNUNET_YES)
6206 GNUNET_assert (running != -1);
6211 GNUNET_assert (stopped != -1);
6218 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6219 "Trying to stop more peers than are currently running!\n");
6220 cb (cb_cls, "Trying to stop more peers than are currently running!");
6226 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6227 "Trying to start more peers than are currently stopped!\n");
6228 cb (cb_cls, "Trying to start more peers than are currently stopped!");
6232 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6236 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
6240 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
6242 running_permute = NULL;
6243 stopped_permute = NULL;
6247 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, running);
6250 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, stopped);
6252 total_running = running;
6253 total_stopped = stopped;
6257 churn_ctx->num_to_start = von;
6258 churn_ctx->num_to_stop = voff;
6260 churn_ctx->cb_cls = cb_cls;
6263 for (i = 0; i < pg->total; i++)
6265 if (pg->peers[i].daemon->running == GNUNET_YES)
6267 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6268 running_arr[running] = i;
6273 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6274 stopped_arr[stopped] = i;
6279 GNUNET_assert (running >= voff);
6282 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6283 shutdown_ctx->cb = &churn_stop_callback;
6284 shutdown_ctx->cb_cls = churn_ctx;
6285 shutdown_ctx->total_peers = voff;
6286 shutdown_ctx->timeout = timeout;
6289 for (i = 0; i < voff; i++)
6292 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
6293 running_permute[i]);
6295 GNUNET_assert (running_arr != NULL);
6296 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6297 peer_shutdown_ctx->daemon =
6298 pg->peers[running_arr[running_permute[i]]].daemon;
6299 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6300 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
6304 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
6306 &churn_stop_callback, churn_ctx,
6307 GNUNET_NO, GNUNET_YES); */
6310 GNUNET_assert (stopped >= von);
6313 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6314 churn_startup_ctx->churn_ctx = churn_ctx;
6315 churn_startup_ctx->timeout = timeout;
6316 churn_startup_ctx->pg = pg;
6318 for (i = 0; i < von; i++)
6321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
6322 stopped_permute[i]);
6324 GNUNET_assert (stopped_arr != NULL);
6325 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
6326 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
6327 peer_restart_ctx->daemon =
6328 pg->peers[stopped_arr[stopped_permute[i]]].daemon;
6329 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
6331 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
6332 timeout, &churn_start_callback, churn_ctx); */
6335 GNUNET_free_non_null (running_arr);
6336 GNUNET_free_non_null (stopped_arr);
6337 GNUNET_free_non_null (running_permute);
6338 GNUNET_free_non_null (stopped_permute);
6343 * Restart all peers in the given group.
6345 * @param pg the handle to the peer group
6346 * @param callback function to call on completion (or failure)
6347 * @param callback_cls closure for the callback function
6350 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
6351 GNUNET_TESTING_NotifyCompletion callback,
6354 struct RestartContext *restart_context;
6359 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
6360 restart_context->peer_group = pg;
6361 restart_context->peers_restarted = 0;
6362 restart_context->callback = callback;
6363 restart_context->callback_cls = callback_cls;
6365 for (off = 0; off < pg->total; off++)
6367 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
6368 &restart_callback, restart_context);
6374 * Start or stop an individual peer from the given group.
6376 * @param pg handle to the peer group
6377 * @param offset which peer to start or stop
6378 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
6379 * @param timeout how long to wait for shutdown
6380 * @param cb function to call at the end
6381 * @param cb_cls closure for cb
6384 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
6385 unsigned int offset,
6387 struct GNUNET_TIME_Relative timeout,
6388 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6390 struct ShutdownContext *shutdown_ctx;
6391 struct ChurnRestartContext *startup_ctx;
6392 struct ChurnContext *churn_ctx;
6394 if (GNUNET_NO == desired_status)
6396 if (NULL != pg->peers[offset].daemon)
6398 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6399 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6400 churn_ctx->num_to_start = 0;
6401 churn_ctx->num_to_stop = 1;
6403 churn_ctx->cb_cls = cb_cls;
6404 shutdown_ctx->cb_cls = churn_ctx;
6405 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon,
6406 timeout, &churn_stop_callback,
6407 shutdown_ctx, GNUNET_NO, GNUNET_YES);
6410 else if (GNUNET_YES == desired_status)
6412 if (NULL == pg->peers[offset].daemon)
6414 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6415 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6416 churn_ctx->num_to_start = 1;
6417 churn_ctx->num_to_stop = 0;
6419 churn_ctx->cb_cls = cb_cls;
6420 startup_ctx->churn_ctx = churn_ctx;
6421 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
6422 timeout, &churn_start_callback,
6432 * Callback for shutting down peers in a peer group.
6434 * @param cls closure (struct ShutdownContext)
6435 * @param emsg NULL on success
6438 internal_shutdown_callback (void *cls, const char *emsg)
6440 struct ShutdownContext *shutdown_ctx = cls;
6443 shutdown_ctx->outstanding--;
6446 shutdown_ctx->peers_down++;
6450 shutdown_ctx->peers_failed++;
6453 if ((shutdown_ctx->cb != NULL)
6454 && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed ==
6455 shutdown_ctx->total_peers))
6457 if (shutdown_ctx->peers_failed > 0)
6458 shutdown_ctx->cb (shutdown_ctx->cb_cls,
6459 "Not all peers successfully shut down!");
6461 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
6463 GNUNET_free (shutdown_ctx->pg->peers);
6464 GNUNET_free_non_null(shutdown_ctx->pg->hostkey_data);
6465 for (off = 0; off < shutdown_ctx->pg->num_hosts; off++)
6467 GNUNET_free (shutdown_ctx->pg->hosts[off].hostname);
6468 GNUNET_free_non_null (shutdown_ctx->pg->hosts[off].username);
6470 GNUNET_free_non_null (shutdown_ctx->pg->hosts);
6471 GNUNET_free (shutdown_ctx->pg);
6473 GNUNET_free (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,
6487 const struct GNUNET_SCHEDULER_TaskContext *tc)
6489 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6490 struct ShutdownContext *shutdown_ctx;
6492 GNUNET_assert (peer_shutdown_ctx != NULL);
6493 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6494 GNUNET_assert (shutdown_ctx != NULL);
6496 if (shutdown_ctx->outstanding > shutdown_ctx->pg->max_concurrent_ssh)
6497 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
6498 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
6499 &schedule_shutdown_task, peer_shutdown_ctx);
6502 shutdown_ctx->outstanding++;
6503 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6504 shutdown_ctx->timeout,
6505 &internal_shutdown_callback, shutdown_ctx,
6506 GNUNET_YES, GNUNET_NO);
6507 GNUNET_free (peer_shutdown_ctx);
6512 * Shutdown all peers started in the given group.
6514 * @param pg handle to the peer group
6515 * @param timeout how long to wait for shutdown
6516 * @param cb callback to notify upon success or failure
6517 * @param cb_cls closure for cb
6520 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
6521 struct GNUNET_TIME_Relative timeout,
6522 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6525 struct ShutdownContext *shutdown_ctx;
6526 struct PeerShutdownContext *peer_shutdown_ctx;
6528 struct PeerConnection *conn_iter;
6529 struct PeerConnection *temp_conn;
6532 GNUNET_assert (pg->total > 0);
6534 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6535 shutdown_ctx->cb = cb;
6536 shutdown_ctx->cb_cls = cb_cls;
6537 shutdown_ctx->total_peers = pg->total;
6538 shutdown_ctx->timeout = timeout;
6539 shutdown_ctx->pg = pg;
6540 /* shtudown_ctx->outstanding = 0; */
6542 for (off = 0; off < pg->total; off++)
6544 GNUNET_assert (NULL != pg->peers[off].daemon);
6545 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6546 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
6547 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6548 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
6550 if (NULL != pg->peers[off].cfg)
6551 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
6553 conn_iter = pg->peers[off].allowed_peers_head;
6554 while (conn_iter != NULL)
6556 temp_conn = conn_iter->next;
6557 GNUNET_free(conn_iter);
6558 conn_iter = temp_conn;
6561 conn_iter = pg->peers[off].connect_peers_head;
6562 while (conn_iter != NULL)
6564 temp_conn = conn_iter->next;
6565 GNUNET_free(conn_iter);
6566 conn_iter = temp_conn;
6569 conn_iter = pg->peers[off].blacklisted_peers_head;
6570 while (conn_iter != NULL)
6572 temp_conn = conn_iter->next;
6573 GNUNET_free(conn_iter);
6574 conn_iter = temp_conn;
6577 conn_iter = pg->peers[off].connect_peers_working_set_head;
6578 while (conn_iter != NULL)
6580 temp_conn = conn_iter->next;
6581 GNUNET_free(conn_iter);
6582 conn_iter = temp_conn;
6585 if (pg->peers[off].allowed_peers != NULL)
6586 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
6587 if (pg->peers[off].connect_peers != NULL)
6588 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
6589 if (pg->peers[off].blacklisted_peers != NULL)
6590 GNUNET_CONTAINER_multihashmap_destroy (pg->
6591 peers[off].blacklisted_peers);
6598 /* end of testing_group.c */