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
28 #include "gnunet_constants.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_core_service.h"
33 #define VERBOSE_TESTING GNUNET_EXTRA_LOGGING
35 #define VERBOSE_TOPOLOGY GNUNET_EXTRA_LOGGING
37 #define DEBUG_CHURN GNUNET_EXTRA_LOGGING
39 #define USE_START_HELPER GNUNET_YES
43 /* Before connecting peers, send all of the HELLOs */
44 #define USE_SEND_HELLOS GNUNET_NO
46 #define TOPOLOGY_HACK GNUNET_YES
50 * Lowest port used for GNUnet testing. Should be high enough to not
51 * conflict with other applications running on the hosts but be low
52 * enough to not conflict with client-ports (typically starting around
55 #define LOW_PORT 12000
58 * Highest port used for GNUnet testing. Should be low enough to not
59 * conflict with the port range for "local" ports (client apps; see
60 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
62 #define HIGH_PORT 56000
64 /* Maximum time to delay connect attempt */
65 #define MAX_CONNECT_DELAY 300
68 * Which list of peers do we need to modify?
72 /** Modify allowed peers */
75 /** Modify connect peers */
78 /** Modify blacklist peers */
81 /** Modify workingset peers */
86 * Prototype of a function called whenever two peers would be connected
87 * in a certain topology.
89 typedef unsigned int (*GNUNET_TESTING_ConnectionProcessor) (struct
90 GNUNET_TESTING_PeerGroup
98 * Context for handling churning a peer group
103 * The peergroup we are dealing with.
105 struct GNUNET_TESTING_PeerGroup *pg;
108 * Name of the service to churn on/off, NULL
109 * to churn entire peer.
114 * Callback used to notify of churning finished
116 GNUNET_TESTING_NotifyCompletion cb;
119 * Closure for callback
124 * Number of peers that still need to be started
126 unsigned int num_to_start;
129 * Number of peers that still need to be stopped
131 unsigned int num_to_stop;
134 * Number of peers that failed to start
136 unsigned int num_failed_start;
139 * Number of peers that failed to stop
141 unsigned int num_failed_stop;
144 struct RestartContext
147 * The group of peers being restarted
149 struct GNUNET_TESTING_PeerGroup *peer_group;
152 * How many peers have been restarted thus far
154 unsigned int peers_restarted;
157 * How many peers got an error when restarting
159 unsigned int peers_restart_failed;
162 * The function to call once all peers have been restarted
164 GNUNET_TESTING_NotifyCompletion callback;
167 * Closure for callback function
173 struct SendHelloContext
176 * Global handle to the peer group.
178 struct GNUNET_TESTING_PeerGroup *pg;
181 * The data about this specific peer.
183 struct PeerData *peer;
186 * The next HELLO that needs sent to this peer.
188 struct PeerConnection *peer_pos;
191 * Are we connected to CORE yet?
193 unsigned int core_ready;
196 * How many attempts should we make for failed connections?
198 unsigned int connect_attempts;
201 * Task for scheduling core connect requests to be sent.
203 GNUNET_SCHEDULER_TaskIdentifier core_connect_task;
206 struct ShutdownContext
208 struct GNUNET_TESTING_PeerGroup *pg;
210 * Total peers to wait for
212 unsigned int total_peers;
215 * Number of peers successfully shut down
217 unsigned int peers_down;
220 * Number of peers failed to shut down
222 unsigned int peers_failed;
225 * Number of peers we have started shutting
226 * down. If too many, wait on them.
228 unsigned int outstanding;
231 * Timeout for shutdown.
233 struct GNUNET_TIME_Relative timeout;
236 * Callback to call when all peers either
237 * shutdown or failed to shutdown
239 GNUNET_TESTING_NotifyCompletion cb;
247 * Should we delete all of the files from the peers?
253 * Individual shutdown context for a particular peer.
255 struct PeerShutdownContext
258 * Pointer to the high level shutdown context.
260 struct ShutdownContext *shutdown_ctx;
263 * The daemon handle for the peer to shut down.
265 struct GNUNET_TESTING_Daemon *daemon;
269 * Individual shutdown context for a particular peer.
271 struct PeerRestartContext
274 * Pointer to the high level restart context.
276 struct ChurnRestartContext *churn_restart_ctx;
279 * The daemon handle for the peer to shut down.
281 struct GNUNET_TESTING_Daemon *daemon;
284 struct ServiceStartContext
286 struct GNUNET_TESTING_PeerGroup *pg;
287 unsigned int remaining;
288 GNUNET_TESTING_NotifyCompletion cb;
289 unsigned int outstanding;
291 struct GNUNET_TIME_Relative timeout;
296 * Individual shutdown context for a particular peer.
298 struct PeerServiceStartContext
301 * Pointer to the high level start context.
303 struct ServiceStartContext *start_ctx;
306 * The daemon handle for the peer to start the service on.
308 struct GNUNET_TESTING_Daemon *daemon;
311 struct CreateTopologyContext
315 * Function to call with number of connections
317 GNUNET_TESTING_NotifyConnections cont;
320 * Closure for connection notification
327 /** Waiting to read number of peers */
330 /** Should find next peer index */
333 /** Should find colon */
336 /** Should read other peer index, space, or endline */
341 struct PeerConnection
346 struct PeerConnection *prev;
351 struct PeerConnection *next;
354 * Index of daemon in pg->peers
361 struct InternalStartContext
364 * Pointer to peerdata
366 struct PeerData *peer;
369 * Timeout for peer startup
371 struct GNUNET_TIME_Relative timeout;
374 * Client callback for hostkey notification
376 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
379 * Closure for hostkey_callback
384 * Client callback for peer start notification
386 GNUNET_TESTING_NotifyDaemonRunning start_cb;
394 * Hostname, where to start the peer
396 const char *hostname;
399 * Username to use when connecting to the
402 const char *username;
405 * Pointer to starting memory location of a hostkey
410 * Port to use for ssh.
416 struct ChurnRestartContext
419 * PeerGroup that we are working with.
421 struct GNUNET_TESTING_PeerGroup *pg;
424 * Number of restarts currently in flight.
426 unsigned int outstanding;
429 * Handle to the underlying churn context.
431 struct ChurnContext *churn_ctx;
434 * How long to allow the operation to take.
436 struct GNUNET_TIME_Relative timeout;
439 struct OutstandingSSH
441 struct OutstandingSSH *next;
443 struct OutstandingSSH *prev;
446 * Number of current ssh connections.
448 uint32_t outstanding;
451 * The hostname of this peer.
453 const char *hostname;
457 * Data we keep per peer.
462 * (Initial) configuration of the host.
463 * (initial because clients could change
464 * it and we would not know about those
467 struct GNUNET_CONFIGURATION_Handle *cfg;
470 * Handle for controlling the daemon.
472 struct GNUNET_TESTING_Daemon *daemon;
475 * The peergroup this peer belongs to.
477 struct GNUNET_TESTING_PeerGroup *pg;
481 * Linked list of allowed peer connections.
483 struct PeerConnection *allowed_peers_head;
486 * Linked list of allowed peer connections.
488 struct PeerConnection *allowed_peers_tail;
491 * Linked list of blacklisted peer connections.
493 struct PeerConnection *blacklisted_peers_head;
496 * Linked list of blacklisted peer connections.
498 struct PeerConnection *blacklisted_peers_tail;
501 * Linked list of connect peer connections.
503 struct PeerConnection *connect_peers_head;
506 * Linked list of connect peer connections.
508 struct PeerConnection *connect_peers_tail;
511 * Linked list of connect peer connections.
513 struct PeerConnection *connect_peers_working_set_head;
516 * Linked list of connect peer connections.
518 struct PeerConnection *connect_peers_working_set_tail;
522 * Hash map of allowed peer connections (F2F created topology)
524 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
527 * Hash map of blacklisted peers
529 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
532 * Hash map of peer connections
534 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
537 * Temporary hash map of peer connections
539 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
543 * Temporary variable for topology creation, should be reset before
544 * creating any topology so the count is valid once finished.
549 * Context to keep track of peers being started, to
550 * stagger hostkey generation and peer startup.
552 struct InternalStartContext internal_context;
556 * Linked list of per-host data.
566 * SSH username to use when connecting to this host.
571 * SSH port to use when connecting to this host.
576 * Lowest port that we have not yet used
582 struct TopologyIterateContext
585 * The peergroup we are working with.
587 struct GNUNET_TESTING_PeerGroup *pg;
590 * Callback for notifying of two connected peers.
592 GNUNET_TESTING_NotifyTopology topology_cb;
595 * Closure for topology_cb
600 * Number of peers currently connected to.
602 unsigned int connected;
605 * Number of peers we have finished iterating.
607 unsigned int completed;
610 * Number of peers total.
615 struct StatsIterateContext
618 * The peergroup that we are dealing with.
620 struct GNUNET_TESTING_PeerGroup *pg;
623 * Continuation to call once all stats information has been retrieved.
625 GNUNET_STATISTICS_Callback cont;
628 * Proc function to call on each value received.
630 GNUNET_TESTING_STATISTICS_Iterator proc;
633 * Closure for topology_cb
638 * Number of peers currently connected to.
640 unsigned int connected;
643 * Number of peers we have finished iterating.
645 unsigned int completed;
648 * Number of peers total.
656 struct GNUNET_TESTING_Daemon *daemon;
659 struct StatsCoreContext
662 struct GNUNET_TESTING_Daemon *daemon;
664 * Handle to the statistics service.
666 struct GNUNET_STATISTICS_Handle *stats_handle;
669 * Handle for getting statistics.
671 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
674 struct ConnectTopologyContext
677 * How many connections are left to create.
679 unsigned int remaining_connections;
682 * Handle to group of peers.
684 struct GNUNET_TESTING_PeerGroup *pg;
687 * How long to try this connection before timing out.
689 struct GNUNET_TIME_Relative connect_timeout;
692 * How many times to retry connecting the two peers.
694 unsigned int connect_attempts;
697 * Temp value set for each iteration.
699 //struct PeerData *first;
702 * Notification that all peers are connected.
704 GNUNET_TESTING_NotifyCompletion notify_connections_done;
707 * Closure for notify.
712 struct ConnectContext;
715 * Handle to a group of GNUnet peers.
717 struct GNUNET_TESTING_PeerGroup
720 * Configuration template.
722 const struct GNUNET_CONFIGURATION_Handle *cfg;
724 struct ConnectContext *cc_head;
726 struct ConnectContext *cc_tail;
729 * Function to call on each started daemon.
731 //GNUNET_TESTING_NotifyDaemonRunning cb;
739 * Function to call on each topology connection created
741 GNUNET_TESTING_NotifyConnection notify_connection;
744 * Callback for notify_connection
746 void *notify_connection_cls;
749 * Array of information about hosts.
751 struct HostData *hosts;
754 * Number of hosts (size of HostData)
756 unsigned int num_hosts;
759 * Array of "total" peers.
761 struct PeerData *peers;
764 * Number of peers in this group.
769 * At what time should we fail the peer startup process?
771 struct GNUNET_TIME_Absolute max_timeout;
774 * How many peers are being started right now?
776 unsigned int starting;
779 * How many peers have already been started?
781 unsigned int started;
784 * Number of possible connections to peers
787 unsigned int max_outstanding_connections;
790 * Number of ssh connections to peers (max).
792 unsigned int max_concurrent_ssh;
795 * Number of connects we are waiting on, allows us to rate limit
798 unsigned int outstanding_connects;
801 * Number of HELLOs we have yet to send.
803 unsigned int remaining_hellos;
806 * How many connects have already been scheduled?
808 unsigned int total_connects_scheduled;
811 * Hostkeys loaded from a file.
816 * Head of DLL to keep track of the number of outstanding
817 * ssh connections per peer.
819 struct OutstandingSSH *ssh_head;
822 * Tail of DLL to keep track of the number of outstanding
823 * ssh connections per peer.
825 struct OutstandingSSH *ssh_tail;
828 * Stop scheduling peers connecting.
830 unsigned int stop_connects;
833 * Connection context for peer group.
835 struct ConnectTopologyContext ct_ctx;
841 * The altered configuration.
843 struct GNUNET_CONFIGURATION_Handle *ret;
846 * The original configuration to alter.
848 const struct GNUNET_CONFIGURATION_Handle *orig;
851 * The hostname that this peer will run on.
853 const char *hostname;
856 * The next possible port to assign.
861 * Unique number for unix domain sockets.
866 * Unique number for this peer/host to offset
867 * things that are grouped by host.
872 struct ConnectContext
875 struct ConnectContext *next;
877 struct ConnectContext *prev;
880 * Index of peer to connect second to.
882 uint32_t first_index;
885 * Index of peer to connect first to.
887 uint32_t second_index;
890 * Task associated with the attempt to connect.
892 GNUNET_SCHEDULER_TaskIdentifier task;
895 * Context in 'testing.c', to cancel connection attempt.
897 struct GNUNET_TESTING_ConnectContext *cc;
900 * Higher level topology connection context.
902 struct ConnectTopologyContext *ct_ctx;
905 * Whether this connection has been accounted for in the schedule_connect call.
910 struct UnblacklistContext
915 struct GNUNET_TESTING_PeerGroup *pg;
918 * uid of the first peer
928 struct GNUNET_TESTING_PeerGroup *pg;
931 * uid of the first peer
936 * Peer data for first peer.
938 struct PeerData *first;
941 * Random percentage to use
946 struct MinimumContext
951 struct GNUNET_TESTING_PeerGroup *pg;
954 * uid of the first peer
959 * Peer data for first peer.
961 struct PeerData *first;
964 * Number of conns per peer
966 unsigned int num_to_add;
969 * Permuted array of all possible connections. Only add the Nth
970 * peer if it's in the Nth position.
972 unsigned int *pg_array;
975 * What number is the current element we are iterating over?
977 unsigned int current;
985 struct GNUNET_TESTING_PeerGroup *pg;
988 * uid of the first peer
993 * uid of the second peer
998 * Peer data for first peer.
1000 struct PeerData *first;
1003 * Which peer has been chosen as the one to add?
1005 unsigned int chosen;
1008 * What number is the current element we are iterating over?
1010 unsigned int current;
1014 * Simple struct to keep track of progress, and print a
1015 * nice little percentage meter for long running tasks.
1017 struct ProgressMeter
1021 unsigned int modnum;
1023 unsigned int dotnum;
1025 unsigned int completed;
1029 char *startup_string;
1034 * Convert unique ID to hash code.
1036 * @param uid unique ID to convert
1037 * @param hash set to uid (extended with zeros)
1040 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
1042 memset (hash, 0, sizeof (GNUNET_HashCode));
1043 *((uint32_t *) hash) = uid;
1047 * Convert hash code to unique ID.
1049 * @param uid unique ID to convert
1050 * @param hash set to uid (extended with zeros)
1053 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
1055 memcpy (uid, hash, sizeof (uint32_t));
1060 static struct GNUNET_CORE_MessageHandler no_handlers[] = {
1066 * Create a meter to keep track of the progress of some task.
1068 * @param total the total number of items to complete
1069 * @param start_string a string to prefix the meter with (if printing)
1070 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
1073 * @return the progress meter
1075 static struct ProgressMeter *
1076 create_meter (unsigned int total, char *start_string, int print)
1078 struct ProgressMeter *ret;
1080 ret = GNUNET_malloc (sizeof (struct ProgressMeter));
1083 ret->modnum = total / 4;
1084 if (ret->modnum == 0) /* Divide by zero check */
1086 ret->dotnum = (total / 50) + 1;
1087 if (start_string != NULL)
1088 ret->startup_string = GNUNET_strdup (start_string);
1090 ret->startup_string = GNUNET_strdup ("");
1096 * Update progress meter (increment by one).
1098 * @param meter the meter to update and print info for
1100 * @return GNUNET_YES if called the total requested,
1101 * GNUNET_NO if more items expected
1104 update_meter (struct ProgressMeter *meter)
1106 if (meter->print == GNUNET_YES)
1108 if (meter->completed % meter->modnum == 0)
1110 if (meter->completed == 0)
1112 fprintf (stdout, "%sProgress: [0%%", meter->startup_string);
1115 fprintf (stdout, "%d%%",
1116 (int) (((float) meter->completed / meter->total) * 100));
1118 else if (meter->completed % meter->dotnum == 0)
1119 fprintf (stdout, ".");
1121 if (meter->completed + 1 == meter->total)
1122 fprintf (stdout, "%d%%]\n", 100);
1127 if (meter->completed == meter->total)
1129 if (meter->completed > meter->total)
1130 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
1135 * Reset progress meter.
1137 * @param meter the meter to reset
1139 * @return GNUNET_YES if meter reset,
1140 * GNUNET_SYSERR on error
1143 reset_meter (struct ProgressMeter *meter)
1146 return GNUNET_SYSERR;
1148 meter->completed = 0;
1153 * Release resources for meter
1155 * @param meter the meter to free
1158 free_meter (struct ProgressMeter *meter)
1160 GNUNET_free_non_null (meter->startup_string);
1161 GNUNET_free (meter);
1165 * Get a topology from a string input.
1167 * @param topology where to write the retrieved topology
1168 * @param topology_string The string to attempt to
1169 * get a configuration value from
1170 * @return GNUNET_YES if topology string matched a
1171 * known topology, GNUNET_NO if not
1174 GNUNET_TESTING_topology_get (enum GNUNET_TESTING_Topology *topology,
1175 const char *topology_string)
1178 * Strings representing topologies in enum
1180 static const char *topology_strings[] = {
1182 * A clique (everyone connected to everyone else).
1187 * Small-world network (2d torus plus random links).
1192 * Small-world network (ring plus random links).
1212 * Certain percentage of peers are unable to communicate directly
1213 * replicating NAT conditions
1218 * Scale free topology.
1223 * Straight line topology.
1228 * All peers are disconnected.
1233 * Read the topology from a file.
1242 if (topology_string == NULL)
1244 while (topology_strings[curr] != NULL)
1246 if (strcasecmp (topology_strings[curr], topology_string) == 0)
1253 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
1258 * Get connect topology option from string input.
1260 * @param topology_option where to write the retrieved topology
1261 * @param topology_string The string to attempt to
1262 * get a configuration value from
1263 * @return GNUNET_YES if string matched a known
1264 * topology option, GNUNET_NO if not
1267 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption
1269 const char *topology_string)
1272 * Options for connecting a topology as strings.
1274 static const char *topology_option_strings[] = {
1276 * Try to connect all peers specified in the topology.
1281 * Choose a random subset of connections to create.
1283 "CONNECT_RANDOM_SUBSET",
1286 * Create at least X connections for each peer.
1291 * Using a depth first search, create one connection
1292 * per peer. If any are missed (graph disconnected)
1293 * start over at those peers until all have at least one
1299 * Find the N closest peers to each allowed peer in the
1300 * topology and make sure a connection to those peers
1301 * exists in the connect topology.
1306 * No options specified.
1314 if (topology_string == NULL)
1316 while (NULL != topology_option_strings[curr])
1318 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1320 *topology_option = curr;
1325 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1330 * Function to iterate over options. Copies
1331 * the options to the target configuration,
1332 * updating PORT values as needed.
1334 * @param cls closure
1335 * @param section name of the section
1336 * @param option name of the option
1337 * @param value value of the option
1340 update_config (void *cls, const char *section, const char *option,
1343 struct UpdateContext *ctx = cls;
1347 char *single_variable;
1348 char *per_host_variable;
1349 unsigned long long num_per_host;
1351 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1352 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1354 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1358 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1361 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
1364 else if ((ival != 0) &&
1366 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1367 single_variable)) &&
1368 GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1372 GNUNET_snprintf (cval, sizeof (cval), "%u",
1373 ival + ctx->fdnum % num_per_host);
1377 /* FIXME: REMOVE FOREVER HACK HACK HACK */
1378 if (0 == strcasecmp (section, "transport-tcp"))
1379 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section,
1380 "ADVERTISED_PORT", value);
1383 if (0 == strcmp (option, "UNIXPATH"))
1386 GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1389 GNUNET_snprintf (uval, sizeof (uval), "/tmp/test-service-%s-%u", section,
1393 else if ((GNUNET_YES ==
1394 GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1400 GNUNET_snprintf (uval, sizeof (uval), "/tmp/test-service-%s-%u", section,
1401 ctx->fdnum % num_per_host);
1406 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1408 value = ctx->hostname;
1410 GNUNET_free (single_variable);
1411 GNUNET_free (per_host_variable);
1412 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1416 * Create a new configuration using the given configuration
1417 * as a template; however, each PORT in the existing cfg
1418 * must be renumbered by incrementing "*port". If we run
1419 * out of "*port" numbers, return NULL.
1421 * @param cfg template configuration
1422 * @param off the current peer offset
1423 * @param port port numbers to use, update to reflect
1424 * port numbers that were used
1425 * @param upnum number to make unix domain socket names unique
1426 * @param hostname hostname of the controlling host, to allow control connections from
1427 * @param fdnum number used to offset the unix domain socket for grouped processes
1428 * (such as statistics or peerinfo, which can be shared among others)
1430 * @return new configuration, NULL on error
1432 static struct GNUNET_CONFIGURATION_Handle *
1433 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t off,
1434 uint16_t * port, uint32_t * upnum, const char *hostname,
1437 struct UpdateContext uc;
1440 char *allowed_hosts;
1441 unsigned long long skew_variance;
1442 unsigned long long skew_offset;
1443 long long actual_offset;
1449 uc.ret = GNUNET_CONFIGURATION_create ();
1450 uc.hostname = hostname;
1453 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1454 if (uc.nport >= HIGH_PORT)
1457 GNUNET_CONFIGURATION_destroy (uc.ret);
1462 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "skew_variance",
1464 (skew_variance > 0))
1467 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1470 skew_offset - GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1472 /* Min is -skew_variance, Max is skew_variance */
1473 skew_offset = skew_variance + actual_offset; /* Normal distribution around 0 */
1474 GNUNET_CONFIGURATION_set_value_number (uc.ret, "testing", "skew_offset",
1478 if (GNUNET_CONFIGURATION_get_value_string
1479 (cfg, "testing", "control_host", &control_host) == GNUNET_OK)
1481 if (hostname != NULL)
1482 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1485 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1487 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1490 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nse", "ACCEPT_FROM",
1493 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "ACCEPT_FROM",
1495 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1497 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "ACCEPT_FROM",
1500 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH", "");
1501 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH", "");
1502 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH", "");
1503 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1505 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nse", "UNIXPATH", "");
1507 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1508 "USE_LOCALADDR", "YES");
1509 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1510 "USE_LOCALADDR", "YES");
1511 GNUNET_free_non_null (control_host);
1512 GNUNET_free (allowed_hosts);
1515 /* arm needs to know to allow connections from the host on which it is running,
1516 * otherwise gnunet-arm is unable to connect to it in some instances */
1517 if (hostname != NULL)
1519 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1520 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "BINDTO", hostname);
1521 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "INTERNAL_ADDRESS",
1523 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "EXTERNAL_ADDRESS",
1525 GNUNET_CONFIGURATION_set_value_string (uc.ret, "disablev6", "BINDTO",
1527 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1528 "USE_LOCALADDR", "YES");
1529 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1530 "USE_LOCALADDR", "YES");
1531 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1533 GNUNET_free (allowed_hosts);
1537 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp",
1538 "USE_LOCALADDR", "YES");
1539 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp",
1540 "USE_LOCALADDR", "YES");
1541 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "BINDTO",
1543 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "INTERNAL_ADDRESS",
1545 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "EXTERNAL_ADDRESS",
1547 GNUNET_CONFIGURATION_set_value_string (uc.ret, "disablev6", "BINDTO",
1551 *port = (uint16_t) uc.nport;
1559 * Remove entries from the peer connection list
1561 * @param pg the peer group we are working with
1562 * @param first index of the first peer
1563 * @param second index of the second peer
1564 * @param list the peer list to use
1565 * @param check UNUSED
1567 * @return the number of connections added (can be 0, 1 or 2)
1571 remove_connections (struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1572 unsigned int second, enum PeerLists list,
1578 struct PeerConnection **first_list;
1579 struct PeerConnection **second_list;
1580 struct PeerConnection *first_iter;
1581 struct PeerConnection *second_iter;
1582 struct PeerConnection **first_tail;
1583 struct PeerConnection **second_tail;
1586 GNUNET_HashCode hash_first;
1587 GNUNET_HashCode hash_second;
1589 hash_from_uid (first, &hash_first);
1590 hash_from_uid (second, &hash_second);
1598 first_list = &pg->peers[first].allowed_peers_head;
1599 second_list = &pg->peers[second].allowed_peers_head;
1600 first_tail = &pg->peers[first].allowed_peers_tail;
1601 second_tail = &pg->peers[second].allowed_peers_tail;
1604 first_list = &pg->peers[first].connect_peers_head;
1605 second_list = &pg->peers[second].connect_peers_head;
1606 first_tail = &pg->peers[first].connect_peers_tail;
1607 second_tail = &pg->peers[second].connect_peers_tail;
1610 first_list = &pg->peers[first].blacklisted_peers_head;
1611 second_list = &pg->peers[second].blacklisted_peers_head;
1612 first_tail = &pg->peers[first].blacklisted_peers_tail;
1613 second_tail = &pg->peers[second].blacklisted_peers_tail;
1616 first_list = &pg->peers[first].connect_peers_working_set_head;
1617 second_list = &pg->peers[second].connect_peers_working_set_head;
1618 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1619 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1626 first_iter = *first_list;
1627 while (first_iter != NULL)
1629 if (first_iter->index == second)
1631 GNUNET_CONTAINER_DLL_remove (*first_list, *first_tail, first_iter);
1632 GNUNET_free (first_iter);
1636 first_iter = first_iter->next;
1639 second_iter = *second_list;
1640 while (second_iter != NULL)
1642 if (second_iter->index == first)
1644 GNUNET_CONTAINER_DLL_remove (*second_list, *second_tail, second_iter);
1645 GNUNET_free (second_iter);
1649 second_iter = second_iter->next;
1653 GNUNET_CONTAINER_multihashmap_contains (pg->
1654 peers[first].blacklisted_peers,
1657 GNUNET_CONTAINER_multihashmap_remove_all (pg->
1658 peers[first].blacklisted_peers,
1663 GNUNET_CONTAINER_multihashmap_contains (pg->
1664 peers[second].blacklisted_peers,
1667 GNUNET_CONTAINER_multihashmap_remove_all (pg->
1668 peers[second].blacklisted_peers,
1677 * Add entries to the some list
1679 * @param pg the peer group we are working with
1680 * @param first index of the first peer
1681 * @param second index of the second peer
1682 * @param list the list type that we should modify
1683 * @param check GNUNET_YES to check lists before adding
1684 * GNUNET_NO to force add
1686 * @return the number of connections added (can be 0, 1 or 2)
1690 add_connections (struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1691 unsigned int second, enum PeerLists list, unsigned int check)
1697 struct PeerConnection **first_list;
1698 struct PeerConnection **second_list;
1699 struct PeerConnection *first_iter;
1700 struct PeerConnection *second_iter;
1701 struct PeerConnection *new_first;
1702 struct PeerConnection *new_second;
1703 struct PeerConnection **first_tail;
1704 struct PeerConnection **second_tail;
1709 first_list = &pg->peers[first].allowed_peers_head;
1710 second_list = &pg->peers[second].allowed_peers_head;
1711 first_tail = &pg->peers[first].allowed_peers_tail;
1712 second_tail = &pg->peers[second].allowed_peers_tail;
1715 first_list = &pg->peers[first].connect_peers_head;
1716 second_list = &pg->peers[second].connect_peers_head;
1717 first_tail = &pg->peers[first].connect_peers_tail;
1718 second_tail = &pg->peers[second].connect_peers_tail;
1721 first_list = &pg->peers[first].blacklisted_peers_head;
1722 second_list = &pg->peers[second].blacklisted_peers_head;
1723 first_tail = &pg->peers[first].blacklisted_peers_tail;
1724 second_tail = &pg->peers[second].blacklisted_peers_tail;
1727 first_list = &pg->peers[first].connect_peers_working_set_head;
1728 second_list = &pg->peers[second].connect_peers_working_set_head;
1729 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1730 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1737 add_first = GNUNET_YES;
1738 add_second = GNUNET_YES;
1740 if (check == GNUNET_YES)
1742 first_iter = *first_list;
1743 while (first_iter != NULL)
1745 if (first_iter->index == second)
1747 add_first = GNUNET_NO;
1750 first_iter = first_iter->next;
1753 second_iter = *second_list;
1754 while (second_iter != NULL)
1756 if (second_iter->index == first)
1758 add_second = GNUNET_NO;
1761 second_iter = second_iter->next;
1768 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1769 new_first->index = second;
1770 GNUNET_CONTAINER_DLL_insert (*first_list, *first_tail, new_first);
1771 pg->peers[first].num_connections++;
1777 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1778 new_second->index = first;
1779 GNUNET_CONTAINER_DLL_insert (*second_list, *second_tail, new_second);
1780 pg->peers[second].num_connections++;
1788 * Scale free network construction as described in:
1790 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1792 * Start with a network of "one" peer, then progressively add
1793 * peers up to the total number. At each step, iterate over
1794 * all possible peers and connect new peer based on number of
1795 * existing connections of the target peer.
1797 * @param pg the peer group we are dealing with
1798 * @param proc the connection processor to use
1799 * @param list the peer list to use
1801 * @return the number of connections created
1804 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg,
1805 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1808 unsigned int total_connections;
1809 unsigned int outer_count;
1811 unsigned int previous_total_connections;
1815 GNUNET_assert (pg->total > 1);
1817 /* Add a connection between the first two nodes */
1818 total_connections = proc (pg, 0, 1, list, GNUNET_YES);
1820 for (outer_count = 1; outer_count < pg->total; outer_count++)
1822 previous_total_connections = total_connections;
1823 for (i = 0; i < outer_count; i++)
1826 pg->peers[i].num_connections / (double) previous_total_connections;
1829 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1830 UINT64_MAX)) / ((double) UINT64_MAX);
1832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1833 "Considering connecting peer %d to peer %d\n", outer_count,
1836 if (random < probability)
1839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n",
1842 total_connections += proc (pg, outer_count, i, list, GNUNET_YES);
1847 return total_connections;
1851 * Create a topology given a peer group (set of running peers)
1852 * and a connection processor. Creates a small world topology
1853 * according to the rewired ring construction. The basic
1854 * behavior is that a ring topology is created, but with some
1855 * probability instead of connecting a peer to the next
1856 * neighbor in the ring a connection will be created to a peer
1857 * selected uniformly at random. We use the TESTING
1858 * PERCENTAGE option to specify what number of
1859 * connections each peer should have. Default is 2,
1860 * which makes the ring, any given number is multiplied by
1861 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1862 * each peer have on average 2logn connections. The additional
1863 * connections are made at increasing distance around the ring
1864 * from the original peer, or to random peers based on the re-
1865 * wiring probability. The TESTING
1866 * PROBABILITY option is used as the probability that a given
1867 * connection is rewired.
1869 * @param pg the peergroup to create the topology on
1870 * @param proc the connection processor to call to actually set
1871 * up connections between two peers
1872 * @param list the peer list to use
1874 * @return the number of connections that were set up
1878 create_small_world_ring (struct GNUNET_TESTING_PeerGroup *pg,
1879 GNUNET_TESTING_ConnectionProcessor proc,
1880 enum PeerLists list)
1884 unsigned int natLog;
1885 unsigned int randomPeer;
1886 double random, logNModifier, probability;
1887 unsigned int smallWorldConnections;
1892 unsigned int useAnd;
1893 int connect_attempts;
1895 logNModifier = 0.5; /* FIXME: default value? */
1897 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING", "PERCENTAGE",
1900 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1901 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1903 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1904 p_string, "LOGNMODIFIER", "TESTING");
1905 GNUNET_free (p_string);
1907 probability = 0.5; /* FIXME: default percentage? */
1909 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING", "PROBABILITY",
1912 if (sscanf (p_string, "%lf", &probability) != 1)
1913 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1915 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1916 p_string, "PERCENTAGE", "TESTING");
1917 GNUNET_free (p_string);
1919 natLog = log (pg->total);
1920 connsPerPeer = ceil (natLog * logNModifier);
1922 if (connsPerPeer % 2 == 1)
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."),
1928 smallWorldConnections = 0;
1929 connect_attempts = 0;
1930 for (i = 0; i < pg->total; i++)
1933 max = i + connsPerPeer / 2;
1934 min = i - connsPerPeer / 2;
1936 if (max > pg->total - 1)
1938 max = max - pg->total;
1944 min = pg->total - 1 + min;
1948 for (j = 0; j < connsPerPeer / 2; j++)
1952 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1953 UINT64_MAX) / ((double) UINT64_MAX));
1954 if (random < probability)
1956 /* Connect to uniformly selected random peer */
1958 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
1959 while ((((randomPeer < max) && (randomPeer > min)) && (useAnd == 0)) ||
1960 (((randomPeer > min) || (randomPeer < max)) && (useAnd == 1)))
1963 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
1965 smallWorldConnections += proc (pg, i, randomPeer, list, GNUNET_YES);
1969 nodeToConnect = i + j + 1;
1970 if (nodeToConnect > pg->total - 1)
1972 nodeToConnect = nodeToConnect - pg->total;
1974 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
1980 connect_attempts += smallWorldConnections;
1982 return connect_attempts;
1986 * Create a topology given a peer group (set of running peers)
1987 * and a connection processor.
1989 * @param pg the peergroup to create the topology on
1990 * @param proc the connection processor to call to actually set
1991 * up connections between two peers
1992 * @param list the peer list to use
1994 * @return the number of connections that were set up
1998 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg,
1999 GNUNET_TESTING_ConnectionProcessor proc,
2000 enum PeerLists list)
2002 unsigned int outer_count, inner_count;
2003 unsigned int cutoff;
2004 int connect_attempts;
2005 double nat_percentage;
2008 nat_percentage = 0.6; /* FIXME: default percentage? */
2010 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING", "PERCENTAGE",
2013 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
2014 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2016 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2017 p_string, "PERCENTAGE", "TESTING");
2018 GNUNET_free (p_string);
2021 cutoff = (unsigned int) (nat_percentage * pg->total);
2022 connect_attempts = 0;
2023 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2025 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2027 if ((outer_count > cutoff) || (inner_count > cutoff))
2030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n",
2031 outer_count, inner_count);
2034 proc (pg, outer_count, inner_count, list, GNUNET_YES);
2038 return connect_attempts;
2043 * Create a topology given a peer group (set of running peers)
2044 * and a connection processor.
2046 * @param pg the peergroup to create the topology on
2047 * @param proc the connection processor to call to actually set
2048 * up connections between two peers
2049 * @param list the peer list to use
2051 * @return the number of connections that were set up
2055 create_nated_internet_copy (struct GNUNET_TESTING_PeerGroup *pg,
2056 GNUNET_TESTING_ConnectionProcessor proc,
2057 enum PeerLists list)
2059 unsigned int outer_count, inner_count;
2060 unsigned int cutoff;
2061 int connect_attempts;
2062 double nat_percentage;
2065 struct ProgressMeter *conn_meter;
2067 nat_percentage = 0.6; /* FIXME: default percentage? */
2069 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING", "PERCENTAGE",
2072 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
2073 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2075 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2076 p_string, "PERCENTAGE", "TESTING");
2077 GNUNET_free (p_string);
2080 cutoff = (unsigned int) (nat_percentage * pg->total);
2082 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2084 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2086 if ((outer_count > cutoff) || (inner_count > cutoff))
2092 conn_meter = create_meter (count, "NAT COPY", GNUNET_YES);
2093 connect_attempts = 0;
2094 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2096 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2098 if ((outer_count > cutoff) || (inner_count > cutoff))
2101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n",
2102 outer_count, inner_count);
2105 proc (pg, outer_count, inner_count, list, GNUNET_YES);
2106 add_connections (pg, outer_count, inner_count, ALLOWED, GNUNET_NO);
2107 update_meter (conn_meter);
2111 free_meter (conn_meter);
2113 return connect_attempts;
2118 * Create a topology given a peer group (set of running peers)
2119 * and a connection processor.
2121 * @param pg the peergroup to create the topology on
2122 * @param proc the connection processor to call to actually set
2123 * up connections between two peers
2124 * @param list the peer list to use
2126 * @return the number of connections that were set up
2130 create_small_world (struct GNUNET_TESTING_PeerGroup *pg,
2131 GNUNET_TESTING_ConnectionProcessor proc,
2132 enum PeerLists list)
2134 unsigned int i, j, k;
2135 unsigned int square;
2138 unsigned int toggle = 1;
2139 unsigned int nodeToConnect;
2140 unsigned int natLog;
2141 unsigned int node1Row;
2142 unsigned int node1Col;
2143 unsigned int node2Row;
2144 unsigned int node2Col;
2145 unsigned int distance;
2146 double probability, random, percentage;
2147 unsigned int smallWorldConnections;
2148 unsigned int small_world_it;
2150 int connect_attempts;
2152 square = floor (sqrt (pg->total));
2156 percentage = 0.5; /* FIXME: default percentage? */
2158 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING", "PERCENTAGE",
2161 if (sscanf (p_string, "%lf", &percentage) != 1)
2162 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2164 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2165 p_string, "PERCENTAGE", "TESTING");
2166 GNUNET_free (p_string);
2168 if (percentage < 0.0)
2170 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2172 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
2173 "PERCENTAGE", "TESTING", percentage);
2176 probability = 0.5; /* FIXME: default percentage? */
2178 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING", "PROBABILITY",
2181 if (sscanf (p_string, "%lf", &probability) != 1)
2182 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2184 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2185 p_string, "PROBABILITY", "TESTING");
2186 GNUNET_free (p_string);
2188 if (square * square != pg->total)
2190 while (rows * cols < pg->total)
2192 if (toggle % 2 == 0)
2201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2202 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2206 connect_attempts = 0;
2207 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2208 * to the node to its right and above. Once this is over, we'll have our torus!
2209 * Special case for the last node (if the rows and columns are not equal), connect
2210 * to the first in the row to maintain topology.
2212 for (i = 0; i < pg->total; i++)
2214 /* First connect to the node to the right */
2215 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2216 nodeToConnect = i + 1;
2217 else if (i + 1 == pg->total)
2218 nodeToConnect = rows * cols - cols;
2220 nodeToConnect = i - cols + 1;
2222 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2226 nodeToConnect = (rows * cols) - cols + i;
2227 if (nodeToConnect >= pg->total)
2228 nodeToConnect -= cols;
2231 nodeToConnect = i - cols;
2233 if (nodeToConnect < pg->total)
2234 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2236 natLog = log (pg->total);
2237 #if VERBOSE_TESTING > 2
2238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2239 _("natural log of %d is %d, will run %d iterations\n"), pg->total,
2240 natLog, (int) (natLog * percentage));
2241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2242 _("Total connections added thus far: %u!\n"), connect_attempts);
2244 smallWorldConnections = 0;
2245 small_world_it = (unsigned int) (natLog * percentage);
2246 if (small_world_it < 1)
2248 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
2249 for (i = 0; i < small_world_it; i++)
2251 for (j = 0; j < pg->total; j++)
2253 /* Determine the row and column of node at position j on the 2d torus */
2254 node1Row = j / cols;
2255 node1Col = j - (node1Row * cols);
2256 for (k = 0; k < pg->total; k++)
2258 /* Determine the row and column of node at position k on the 2d torus */
2259 node2Row = k / cols;
2260 node2Col = k - (node2Row * cols);
2261 /* Simple Cartesian distance */
2262 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
2265 /* Calculate probability as 1 over the square of the distance */
2266 probability = 1.0 / (distance * distance);
2267 /* Choose a random value between 0 and 1 */
2270 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2271 UINT64_MAX)) / ((double) UINT64_MAX);
2272 /* If random < probability, then connect the two nodes */
2273 if (random < probability)
2274 smallWorldConnections += proc (pg, j, k, list, GNUNET_YES);
2280 connect_attempts += smallWorldConnections;
2281 #if VERBOSE_TESTING > 2
2282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2283 _("Total connections added for small world: %d!\n"),
2284 smallWorldConnections);
2286 return connect_attempts;
2290 * Create a topology given a peer group (set of running peers)
2291 * and a connection processor.
2293 * @param pg the peergroup to create the topology on
2294 * @param proc the connection processor to call to actually set
2295 * up connections between two peers
2296 * @param list the peer list to use
2298 * @return the number of connections that were set up
2302 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg,
2303 GNUNET_TESTING_ConnectionProcessor proc,
2304 enum PeerLists list)
2307 unsigned int outer_count;
2308 unsigned int inner_count;
2309 int connect_attempts;
2313 probability = 0.5; /* FIXME: default percentage? */
2315 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING", "PROBABILITY",
2318 if (sscanf (p_string, "%lf", &probability) != 1)
2319 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2321 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2322 p_string, "PROBABILITY", "TESTING");
2323 GNUNET_free (p_string);
2325 connect_attempts = 0;
2326 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2328 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2332 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2333 UINT64_MAX)) / ((double) UINT64_MAX);
2335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("rand is %f probability is %f\n"),
2336 temp_rand, probability);
2338 if (temp_rand < probability)
2341 proc (pg, outer_count, inner_count, list, GNUNET_YES);
2346 return connect_attempts;
2350 * Create a topology given a peer group (set of running peers)
2351 * and a connection processor. This particular function creates
2352 * the connections for a 2d-torus, plus additional "closest"
2353 * connections per peer.
2355 * @param pg the peergroup to create the topology on
2356 * @param proc the connection processor to call to actually set
2357 * up connections between two peers
2358 * @param list the peer list to use
2360 * @return the number of connections that were set up
2364 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg,
2365 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2368 unsigned int square;
2371 unsigned int toggle = 1;
2372 unsigned int nodeToConnect;
2373 int connect_attempts;
2375 connect_attempts = 0;
2377 square = floor (sqrt (pg->total));
2381 if (square * square != pg->total)
2383 while (rows * cols < pg->total)
2385 if (toggle % 2 == 0)
2394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2395 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2398 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2399 * to the node to its right and above. Once this is over, we'll have our torus!
2400 * Special case for the last node (if the rows and columns are not equal), connect
2401 * to the first in the row to maintain topology.
2403 for (i = 0; i < pg->total; i++)
2405 /* First connect to the node to the right */
2406 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2407 nodeToConnect = i + 1;
2408 else if (i + 1 == pg->total)
2409 nodeToConnect = rows * cols - cols;
2411 nodeToConnect = i - cols + 1;
2413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n", i,
2416 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2418 /* Second connect to the node immediately above */
2421 nodeToConnect = (rows * cols) - cols + i;
2422 if (nodeToConnect >= pg->total)
2423 nodeToConnect -= cols;
2426 nodeToConnect = i - cols;
2428 if (nodeToConnect < pg->total)
2431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n", i,
2434 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2439 return connect_attempts;
2443 * Create a topology given a peer group (set of running peers)
2444 * and a connection processor.
2446 * @param pg the peergroup to create the topology on
2447 * @param proc the connection processor to call to actually set
2448 * up connections between two peers
2449 * @param list the peer list to use
2450 * @param check does the connection processor need to check before
2451 * performing an action on the list?
2453 * @return the number of connections that were set up
2457 create_clique (struct GNUNET_TESTING_PeerGroup *pg,
2458 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list,
2461 unsigned int outer_count;
2462 unsigned int inner_count;
2463 int connect_attempts;
2464 struct ProgressMeter *conn_meter;
2466 connect_attempts = 0;
2469 create_meter ((((pg->total * pg->total) + pg->total) / 2) - pg->total,
2470 "Create Clique ", GNUNET_NO);
2471 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2473 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n",
2477 outer_count, inner_count);
2479 connect_attempts += proc (pg, outer_count, inner_count, list, check);
2480 update_meter (conn_meter);
2483 reset_meter (conn_meter);
2484 free_meter (conn_meter);
2485 return connect_attempts;
2490 * Iterator over hash map entries.
2492 * @param cls closure the peer group
2493 * @param key the key stored in the hashmap is the
2494 * index of the peer to connect to
2495 * @param value value in the hash map, handle to the peer daemon
2496 * @return GNUNET_YES if we should continue to
2501 unblacklist_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2503 struct UnblacklistContext *un_ctx = cls;
2504 uint32_t second_pos;
2506 uid_from_hash (key, &second_pos);
2508 unblacklist_connections (un_ctx->pg, un_ctx->first_uid, second_pos);
2516 * Create a blacklist topology based on the allowed topology
2517 * which disallows any connections not in the allowed topology
2518 * at the transport level.
2520 * @param pg the peergroup to create the topology on
2521 * @param proc the connection processor to call to allow
2522 * up connections between two peers
2524 * @return the number of connections that were set up
2528 copy_allowed (struct GNUNET_TESTING_PeerGroup *pg,
2529 GNUNET_TESTING_ConnectionProcessor proc)
2533 struct PeerConnection *iter;
2536 struct UnblacklistContext un_ctx;
2541 for (count = 0; count < pg->total - 1; count++)
2544 iter = pg->peers[count].allowed_peers_head;
2545 while (iter != NULL)
2547 remove_connections (pg, count, iter->index, BLACKLIST, GNUNET_YES);
2548 //unblacklist_connections(pg, count, iter->index);
2552 un_ctx.first_uid = count;
2554 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[count].allowed_peers,
2555 &unblacklist_iterator, &un_ctx);
2558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2564 * Create a topology given a peer group (set of running peers)
2565 * and a connection processor.
2567 * @param pg the peergroup to create the topology on
2568 * @param proc the connection processor to call to actually set
2569 * up connections between two peers
2570 * @param list which list should be modified
2572 * @return the number of connections that were set up
2576 create_line (struct GNUNET_TESTING_PeerGroup *pg,
2577 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2580 unsigned int connect_attempts;
2582 connect_attempts = 0;
2583 /* Connect each peer to the next highest numbered peer */
2584 for (count = 0; count < pg->total - 1; count++)
2587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n",
2590 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2593 return connect_attempts;
2597 * Create a topology given a peer group (set of running peers)
2598 * and a connection processor.
2600 * @param pg the peergroup to create the topology on
2601 * @param filename the file to read topology information from
2602 * @param proc the connection processor to call to actually set
2603 * up connections between two peers
2604 * @param list the peer list to use
2606 * @return the number of connections that were set up
2610 create_from_file (struct GNUNET_TESTING_PeerGroup *pg, char *filename,
2611 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2613 int connect_attempts;
2614 unsigned int first_peer_index;
2615 unsigned int second_peer_index;
2620 unsigned int total_peers;
2621 enum States curr_state;
2623 connect_attempts = 0;
2624 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2625 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2627 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2629 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2630 "Could not open file `%s' specified for topology!", filename);
2631 return connect_attempts;
2634 data = GNUNET_malloc_large (frstat.st_size);
2635 GNUNET_assert (data != NULL);
2636 if (frstat.st_size != GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2638 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2639 "Could not read file %s specified for host list, ending test!",
2642 return connect_attempts;
2647 first_peer_index = 0;
2648 /* First line should contain a single integer, specifying the number of peers */
2649 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2650 curr_state = NUM_PEERS;
2651 while (count < frstat.st_size - 1)
2653 if ((buf[count] == '\n') || (buf[count] == ' '))
2663 total_peers = strtoul (&buf[count], NULL, 10);
2666 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2667 "Failed to read number of peers from topology file!\n");
2669 return connect_attempts;
2672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2673 "Found %u total peers in topology\n", total_peers);
2675 GNUNET_assert (total_peers == pg->total);
2676 curr_state = PEER_INDEX;
2677 while ((buf[count] != '\n') && (count < frstat.st_size - 1))
2683 first_peer_index = strtoul (&buf[count], NULL, 10);
2686 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2687 "Failed to read peer index from topology file!\n");
2689 return connect_attempts;
2691 while ((buf[count] != ':') && (count < frstat.st_size - 1))
2694 curr_state = OTHER_PEER_INDEX;
2697 if (1 == sscanf (&buf[count], ":"))
2698 curr_state = OTHER_PEER_INDEX;
2701 case OTHER_PEER_INDEX:
2703 second_peer_index = strtoul (&buf[count], NULL, 10);
2706 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2707 "Failed to peer index from topology file!\n");
2709 return connect_attempts;
2711 /* Assume file is written with first peer 1, but array index is 0 */
2713 proc (pg, first_peer_index - 1, second_peer_index - 1, list,
2715 while ((buf[count] != '\n') && (buf[count] != ',') &&
2716 (count < frstat.st_size - 1))
2718 if (buf[count] == '\n')
2720 curr_state = PEER_INDEX;
2722 else if (buf[count] != ',')
2724 curr_state = OTHER_PEER_INDEX;
2729 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2730 "Found bad data in topology file while in state %d!\n",
2734 return connect_attempts;
2738 return connect_attempts;
2742 * Create a topology given a peer group (set of running peers)
2743 * and a connection processor.
2745 * @param pg the peergroup to create the topology on
2746 * @param proc the connection processor to call to actually set
2747 * up connections between two peers
2748 * @param list the peer list to use
2750 * @return the number of connections that were set up
2754 create_ring (struct GNUNET_TESTING_PeerGroup *pg,
2755 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2758 int connect_attempts;
2760 connect_attempts = 0;
2762 /* Connect each peer to the next highest numbered peer */
2763 for (count = 0; count < pg->total - 1; count++)
2766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer %d to peer %d\n",
2769 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2772 /* Connect the last peer to the first peer */
2773 connect_attempts += proc (pg, pg->total - 1, 0, list, GNUNET_YES);
2775 return connect_attempts;
2780 * Iterator for writing friends of a peer to a file.
2782 * @param cls closure, an open writable file handle
2783 * @param key the key the daemon was stored under
2784 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2786 * @return GNUNET_YES to continue iteration
2788 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2789 * with a single file_iterator that takes a closure which contains
2790 * the prefix to write before the peer. Then this could be used
2791 * for blacklisting multiple transports and writing the friend
2792 * file. I'm sure *someone* will complain loudly about other
2793 * things that negate these functions even existing so no point in
2797 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2799 FILE *temp_friend_handle = cls;
2800 struct GNUNET_TESTING_Daemon *peer = value;
2801 struct GNUNET_PeerIdentity *temppeer;
2802 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2804 temppeer = &peer->id;
2805 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2806 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2811 struct BlacklistContext
2814 * The (open) file handle to write to
2816 FILE *temp_file_handle;
2819 * The transport that this peer will be blacklisted on.
2825 * Iterator for writing blacklist data to appropriate files.
2827 * @param cls closure, an open writable file handle
2828 * @param key the key the daemon was stored under
2829 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2831 * @return GNUNET_YES to continue iteration
2834 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2836 struct BlacklistContext *blacklist_ctx = cls;
2837 struct GNUNET_TESTING_Daemon *peer = value;
2838 struct GNUNET_PeerIdentity *temppeer;
2839 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2841 temppeer = &peer->id;
2842 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n",
2844 blacklist_ctx->transport, (char *) &peer_enc);
2845 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport,
2846 (char *) &peer_enc);
2853 * Create the friend files based on the PeerConnection's
2854 * of each peer in the peer group, and copy the files
2855 * to the appropriate place
2857 * @param pg the peer group we are dealing with
2860 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
2862 FILE *temp_friend_handle;
2863 unsigned int pg_iter;
2864 char *temp_service_path;
2865 struct GNUNET_OS_Process **procarr;
2870 enum GNUNET_OS_ProcessStatusType type;
2871 unsigned long return_code;
2879 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2880 struct PeerConnection *conn_iter;
2882 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2883 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2885 mytemp = GNUNET_DISK_mktemp ("friends");
2886 GNUNET_assert (mytemp != NULL);
2887 temp_friend_handle = fopen (mytemp, "wt");
2888 GNUNET_assert (temp_friend_handle != NULL);
2890 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2891 while (conn_iter != NULL)
2893 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->
2894 id.hashPubKey, &peer_enc);
2895 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2896 conn_iter = conn_iter->next;
2899 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2900 &friend_file_iterator,
2901 temp_friend_handle);
2903 fclose (temp_friend_handle);
2906 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].daemon->cfg,
2907 "PATHS", "SERVICEHOME",
2908 &temp_service_path))
2910 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2912 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2913 "SERVICEHOME", "PATHS");
2914 if (UNLINK (mytemp) != 0)
2915 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
2916 GNUNET_free (mytemp);
2920 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2922 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2924 GNUNET_OS_start_process (NULL, NULL, "mv", "mv", mytemp, arg, NULL);
2925 GNUNET_assert (procarr[pg_iter] != NULL);
2927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2928 "Copying file with command cp %s %s\n", mytemp, arg);
2930 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2931 GNUNET_OS_process_close (procarr[pg_iter]);
2934 else /* Remote, scp the file to the correct place */
2936 if (NULL != pg->peers[pg_iter].daemon->username)
2937 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2938 pg->peers[pg_iter].daemon->username,
2939 pg->peers[pg_iter].daemon->hostname,
2942 GNUNET_asprintf (&arg, "%s:%s/friends",
2943 pg->peers[pg_iter].daemon->hostname,
2946 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg, NULL);
2947 GNUNET_assert (procarr[pg_iter] != NULL);
2948 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2949 GNUNET_OS_process_close (procarr[pg_iter]);
2950 if (ret != GNUNET_OK)
2952 /* FIXME: free contents of 'procarr' array */
2953 GNUNET_free (procarr);
2954 GNUNET_free (temp_service_path);
2955 GNUNET_free (mytemp);
2959 procarr[pg_iter] = NULL;
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2962 "Copying file with command scp %s %s\n", mytemp, arg);
2966 GNUNET_free (temp_service_path);
2967 GNUNET_free (mytemp);
2972 ret = GNUNET_SYSERR;
2973 while ((count < max_wait) && (ret != GNUNET_OK))
2976 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking copy status of file %d\n",
2982 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2984 if (GNUNET_OS_process_status (procarr[pg_iter], &type, &return_code) !=
2987 ret = GNUNET_SYSERR;
2989 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
2991 ret = GNUNET_SYSERR;
2995 GNUNET_OS_process_close (procarr[pg_iter]);
2996 procarr[pg_iter] = NULL;
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "File %d copied\n", pg_iter);
3004 if (ret == GNUNET_SYSERR)
3006 /* FIXME: why sleep here? -CG */
3012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3013 _("Finished copying all friend files!\n"));
3016 GNUNET_free (procarr);
3021 * Create the blacklist files based on the PeerConnection's
3022 * of each peer in the peer group, and copy the files
3023 * to the appropriate place.
3025 * @param pg the peer group we are dealing with
3026 * @param transports space delimited list of transports to blacklist
3029 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg,
3030 const char *transports)
3032 FILE *temp_file_handle;
3033 unsigned int pg_iter;
3034 char *temp_service_path;
3035 struct GNUNET_OS_Process **procarr;
3038 enum GNUNET_OS_ProcessStatusType type;
3039 unsigned long return_code;
3046 char *temp_transports;
3049 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
3050 struct PeerConnection *conn_iter;
3052 static struct BlacklistContext blacklist_ctx;
3055 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
3056 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3058 mytemp = GNUNET_DISK_mktemp ("blacklist");
3059 GNUNET_assert (mytemp != NULL);
3060 temp_file_handle = fopen (mytemp, "wt");
3061 GNUNET_assert (temp_file_handle != NULL);
3062 temp_transports = GNUNET_strdup (transports);
3064 blacklist_ctx.temp_file_handle = temp_file_handle;
3066 transport_len = strlen (temp_transports) + 1;
3069 for (i = 0; i < transport_len; i++)
3071 if ((temp_transports[i] == ' ') && (pos == NULL))
3072 continue; /* At start of string (whitespace) */
3073 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
3075 temp_transports[i] = '\0';
3077 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
3078 while (conn_iter != NULL)
3080 GNUNET_CRYPTO_hash_to_enc (&pg->peers[conn_iter->index].daemon->
3081 id.hashPubKey, &peer_enc);
3082 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
3083 conn_iter = conn_iter->next;
3086 blacklist_ctx.transport = pos;
3087 (void) GNUNET_CONTAINER_multihashmap_iterate (pg->
3089 [pg_iter].blacklisted_peers,
3090 &blacklist_file_iterator,
3094 } /* At beginning of actual string */
3095 else if (pos == NULL)
3097 pos = &temp_transports[i];
3101 GNUNET_free (temp_transports);
3102 fclose (temp_file_handle);
3105 GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].daemon->cfg,
3106 "PATHS", "SERVICEHOME",
3107 &temp_service_path))
3109 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3111 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
3112 "SERVICEHOME", "PATHS");
3113 if (UNLINK (mytemp) != 0)
3114 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
3115 GNUNET_free (mytemp);
3119 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
3121 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
3123 GNUNET_OS_start_process (NULL, NULL, "mv", "mv", mytemp, arg, NULL);
3125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3126 _("Copying file with command cp %s %s\n"), mytemp, arg);
3131 else /* Remote, scp the file to the correct place */
3133 if (NULL != pg->peers[pg_iter].daemon->username)
3134 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
3135 pg->peers[pg_iter].daemon->username,
3136 pg->peers[pg_iter].daemon->hostname,
3139 GNUNET_asprintf (&arg, "%s:%s/blacklist",
3140 pg->peers[pg_iter].daemon->hostname,
3143 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", mytemp, arg, NULL);
3144 GNUNET_assert (procarr[pg_iter] != NULL);
3145 GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
3148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3149 _("Copying file with command scp %s %s\n"), mytemp, arg);
3153 GNUNET_free (temp_service_path);
3154 GNUNET_free (mytemp);
3158 ret = GNUNET_SYSERR;
3159 while ((count < max_wait) && (ret != GNUNET_OK))
3162 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3166 _("Checking copy status of file %d\n"), pg_iter);
3168 if (procarr[pg_iter] != NULL) /* Check for already completed! */
3170 if (GNUNET_OS_process_status (procarr[pg_iter], &type, &return_code) !=
3173 ret = GNUNET_SYSERR;
3175 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
3177 ret = GNUNET_SYSERR;
3181 GNUNET_OS_process_close (procarr[pg_iter]);
3182 procarr[pg_iter] = NULL;
3184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("File %d copied\n"), pg_iter);
3190 if (ret == GNUNET_SYSERR)
3192 /* FIXME: why sleep here? -CG */
3198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3199 _("Finished copying all blacklist files!\n"));
3201 GNUNET_free (procarr);
3205 /* Forward Declaration */
3207 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3210 * Choose a random peer's next connection to create, and
3211 * call schedule_connect to set up the connect task.
3213 * @param pg the peer group to connect
3216 preschedule_connect (struct GNUNET_TESTING_PeerGroup *pg)
3218 struct ConnectTopologyContext *ct_ctx = &pg->ct_ctx;
3219 struct PeerConnection *connection_iter;
3220 struct ConnectContext *connect_context;
3221 uint32_t random_peer;
3223 if (ct_ctx->remaining_connections == 0)
3226 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3227 while (pg->peers[random_peer].connect_peers_head == NULL)
3229 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3231 connection_iter = pg->peers[random_peer].connect_peers_head;
3232 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3233 connect_context->first_index = random_peer;
3234 connect_context->second_index = connection_iter->index;
3235 connect_context->ct_ctx = ct_ctx;
3236 connect_context->task =
3237 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3238 GNUNET_CONTAINER_DLL_insert (pg->cc_head, pg->cc_tail, connect_context);
3239 GNUNET_CONTAINER_DLL_remove (pg->peers[random_peer].connect_peers_head,
3240 pg->peers[random_peer].connect_peers_tail,
3242 GNUNET_free (connection_iter);
3243 ct_ctx->remaining_connections--;
3247 /* Forward declaration */
3249 schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3252 * Close connections and free the hello context.
3254 * @param cls the 'struct SendHelloContext *'
3255 * @param tc scheduler context
3258 free_hello_context (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3260 struct SendHelloContext *send_hello_context = cls;
3262 if (send_hello_context->peer->daemon->server != NULL)
3264 GNUNET_CORE_disconnect (send_hello_context->peer->daemon->server);
3265 send_hello_context->peer->daemon->server = NULL;
3267 if (send_hello_context->peer->daemon->th != NULL)
3269 GNUNET_TRANSPORT_disconnect (send_hello_context->peer->daemon->th);
3270 send_hello_context->peer->daemon->th = NULL;
3272 if (send_hello_context->core_connect_task != GNUNET_SCHEDULER_NO_TASK)
3274 GNUNET_SCHEDULER_cancel (send_hello_context->core_connect_task);
3275 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3277 send_hello_context->pg->outstanding_connects--;
3278 GNUNET_free (send_hello_context);
3282 * For peers that haven't yet connected, notify
3283 * the caller that they have failed (timeout).
3285 * @param cls the 'struct SendHelloContext *'
3286 * @param tc scheduler context
3289 notify_remaining_connections_failed (void *cls,
3290 const struct GNUNET_SCHEDULER_TaskContext
3293 struct SendHelloContext *send_hello_context = cls;
3294 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3295 struct PeerConnection *connection;
3297 GNUNET_CORE_disconnect (send_hello_context->peer->daemon->server);
3298 send_hello_context->peer->daemon->server = NULL;
3300 connection = send_hello_context->peer->connect_peers_head;
3302 while (connection != NULL)
3304 if (pg->notify_connection != NULL)
3306 pg->notify_connection (pg->notify_connection_cls, &send_hello_context->peer->daemon->id, &pg->peers[connection->index].daemon->id, 0, /* FIXME */
3307 send_hello_context->peer->daemon->cfg,
3308 pg->peers[connection->index].daemon->cfg,
3309 send_hello_context->peer->daemon,
3310 pg->peers[connection->index].daemon,
3311 "Peers failed to connect (timeout)");
3313 GNUNET_CONTAINER_DLL_remove (send_hello_context->peer->connect_peers_head,
3314 send_hello_context->peer->connect_peers_tail,
3316 GNUNET_free (connection);
3317 connection = connection->next;
3319 GNUNET_SCHEDULER_add_now (&free_hello_context, send_hello_context);
3321 other_peer = &pg->peers[connection->index];
3326 * For peers that haven't yet connected, send
3327 * CORE connect requests.
3329 * @param cls the 'struct SendHelloContext *'
3330 * @param tc scheduler context
3333 send_core_connect_requests (void *cls,
3334 const struct GNUNET_SCHEDULER_TaskContext *tc)
3336 struct SendHelloContext *send_hello_context = cls;
3337 struct PeerConnection *conn;
3339 GNUNET_assert (send_hello_context->peer->daemon->server != NULL);
3341 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3343 send_hello_context->connect_attempts++;
3344 if (send_hello_context->connect_attempts <
3345 send_hello_context->pg->ct_ctx.connect_attempts)
3347 conn = send_hello_context->peer->connect_peers_head;
3348 while (conn != NULL)
3350 GNUNET_CORE_peer_request_connect (send_hello_context->peer->
3352 &send_hello_context->pg->
3353 peers[conn->index].daemon->id, NULL,
3357 send_hello_context->core_connect_task =
3358 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
3359 (send_hello_context->pg->
3360 ct_ctx.connect_timeout,
3361 send_hello_context->pg->
3362 ct_ctx.connect_attempts),
3363 &send_core_connect_requests,
3364 send_hello_context);
3368 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3369 "Timeout before all connections created, marking rest as failed!\n");
3370 GNUNET_SCHEDULER_add_now (¬ify_remaining_connections_failed,
3371 send_hello_context);
3377 * Success, connection is up. Signal client our success.
3379 * @param cls our "struct SendHelloContext"
3380 * @param peer identity of the peer that has connected
3381 * @param atsi performance information
3383 * FIXME: remove peers from BOTH lists, call notify twice, should
3384 * double the speed of connections as long as the list iteration
3385 * doesn't take too long!
3388 core_connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
3389 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3391 struct SendHelloContext *send_hello_context = cls;
3392 struct PeerConnection *connection;
3393 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3396 struct PeerData *other_peer;
3399 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Connected peer %s to peer %s\n",
3400 ctx->d1->shortname, GNUNET_i2s (peer));
3404 memcmp (&send_hello_context->peer->daemon->id, peer,
3405 sizeof (struct GNUNET_PeerIdentity)))
3408 connection = send_hello_context->peer->connect_peers_head;
3413 while ((connection != NULL) &&
3415 memcmp (&pg->peers[connection->index].daemon->id, peer,
3416 sizeof (struct GNUNET_PeerIdentity))))
3418 connection = connection->next;
3421 if (connection == NULL)
3423 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3424 "Connected peer %s to %s, not in list (no problem(?))\n",
3425 GNUNET_i2s (peer), send_hello_context->peer->daemon->shortname);
3430 other_peer = &pg->peers[connection->index];
3432 if (pg->notify_connection != NULL)
3434 pg->notify_connection (pg->notify_connection_cls, &send_hello_context->peer->daemon->id, peer, 0, /* FIXME */
3435 send_hello_context->peer->daemon->cfg,
3436 pg->peers[connection->index].daemon->cfg,
3437 send_hello_context->peer->daemon,
3438 pg->peers[connection->index].daemon, NULL);
3440 GNUNET_CONTAINER_DLL_remove (send_hello_context->peer->connect_peers_head,
3441 send_hello_context->peer->connect_peers_tail,
3443 GNUNET_free (connection);
3447 /* Notify of reverse connection and remove from other peers list of outstanding */
3448 if (other_peer != NULL)
3450 connection = other_peer->connect_peers_head;
3451 while ((connection != NULL) &&
3453 memcmp (&send_hello_context->peer->daemon->id,
3454 &pg->peers[connection->index].daemon->id,
3455 sizeof (struct GNUNET_PeerIdentity))))
3457 connection = connection->next;
3459 if (connection != NULL)
3461 if (pg->notify_connection != NULL)
3463 pg->notify_connection (pg->notify_connection_cls, peer, &send_hello_context->peer->daemon->id, 0, /* FIXME */
3464 pg->peers[connection->index].daemon->cfg,
3465 send_hello_context->peer->daemon->cfg,
3466 pg->peers[connection->index].daemon,
3467 send_hello_context->peer->daemon, NULL);
3470 GNUNET_CONTAINER_DLL_remove (other_peer->connect_peers_head,
3471 other_peer->connect_peers_tail, connection);
3472 GNUNET_free (connection);
3477 if (send_hello_context->peer->connect_peers_head == NULL)
3479 GNUNET_SCHEDULER_add_now (&free_hello_context, send_hello_context);
3484 * Notify of a successful connection to the core service.
3486 * @param cls a struct SendHelloContext *
3487 * @param server handle to the core service
3488 * @param my_identity the peer identity of this peer
3489 * @param publicKey the public key of the peer
3492 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3493 const struct GNUNET_PeerIdentity *my_identity,
3494 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
3496 struct SendHelloContext *send_hello_context = cls;
3498 send_hello_context->core_ready = GNUNET_YES;
3502 * Function called once a hello has been sent
3503 * to the transport, move on to the next one
3504 * or go away forever.
3506 * @param cls the 'struct SendHelloContext *'
3507 * @param tc scheduler context
3510 hello_sent_callback (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3512 struct SendHelloContext *send_hello_context = cls;
3514 //unsigned int pg_iter;
3515 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3517 GNUNET_free (send_hello_context);
3521 send_hello_context->pg->remaining_hellos--;
3523 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Sent HELLO, have %d remaining!\n",
3524 send_hello_context->pg->remaining_hellos);
3526 if (send_hello_context->peer_pos == NULL) /* All HELLOs (for this peer!) have been transmitted! */
3529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3530 "All hellos for this peer sent, disconnecting transport!\n");
3532 GNUNET_assert (send_hello_context->peer->daemon->th != NULL);
3533 GNUNET_TRANSPORT_disconnect (send_hello_context->peer->daemon->th);
3534 send_hello_context->peer->daemon->th = NULL;
3535 GNUNET_assert (send_hello_context->peer->daemon->server == NULL);
3536 send_hello_context->peer->daemon->server =
3537 GNUNET_CORE_connect (send_hello_context->peer->cfg, 1,
3538 send_hello_context, &core_init,
3539 &core_connect_notify, NULL, NULL, NULL, GNUNET_NO,
3540 NULL, GNUNET_NO, no_handlers);
3542 send_hello_context->core_connect_task =
3543 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
3544 (send_hello_context->pg->
3545 ct_ctx.connect_timeout,
3546 send_hello_context->pg->
3547 ct_ctx.connect_attempts),
3548 &send_core_connect_requests,
3549 send_hello_context);
3552 GNUNET_SCHEDULER_add_now (&schedule_send_hellos, send_hello_context);
3556 * Connect to a peer, give it all the HELLO's of those peers
3557 * we will later ask it to connect to.
3559 * @param ct_ctx the overall connection context
3562 schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3564 struct SendHelloContext *send_hello_context = cls;
3565 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3567 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3569 GNUNET_free (send_hello_context);
3573 GNUNET_assert (send_hello_context->peer_pos != NULL); /* All of the HELLO sends to be scheduled have been scheduled! */
3575 if (((send_hello_context->peer->daemon->th == NULL) &&
3576 (pg->outstanding_connects > pg->max_outstanding_connections)) ||
3577 (pg->stop_connects == GNUNET_YES))
3579 #if VERBOSE_TESTING > 2
3580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3582 ("Delaying connect, we have too many outstanding connections!\n"));
3584 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3585 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3586 &schedule_send_hellos, send_hello_context);
3590 #if VERBOSE_TESTING > 2
3591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3592 _("Creating connection, outstanding_connections is %d\n"),
3593 outstanding_connects);
3595 if (send_hello_context->peer->daemon->th == NULL)
3597 pg->outstanding_connects++; /* Actual TRANSPORT, CORE connections! */
3598 send_hello_context->peer->daemon->th =
3599 GNUNET_TRANSPORT_connect (send_hello_context->peer->cfg, NULL,
3600 send_hello_context, NULL, NULL, NULL);
3603 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3604 _("Offering Hello of peer %s to peer %s\n"),
3605 send_hello_context->peer->daemon->shortname,
3606 pg->peers[send_hello_context->peer_pos->index].
3609 GNUNET_TRANSPORT_offer_hello (send_hello_context->peer->daemon->th,
3610 (const struct GNUNET_MessageHeader *)
3611 pg->peers[send_hello_context->peer_pos->
3612 index].daemon->hello,
3613 &hello_sent_callback, send_hello_context);
3614 send_hello_context->peer_pos = send_hello_context->peer_pos->next;
3615 GNUNET_assert (send_hello_context->peer->daemon->th != NULL);
3621 * Internal notification of a connection, kept so that we can ensure some connections
3622 * happen instead of flooding all testing daemons with requests to connect.
3625 internal_connect_notify (void *cls, const struct GNUNET_PeerIdentity *first,
3626 const struct GNUNET_PeerIdentity *second,
3628 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
3629 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
3630 struct GNUNET_TESTING_Daemon *first_daemon,
3631 struct GNUNET_TESTING_Daemon *second_daemon,
3634 struct ConnectContext *connect_ctx = cls;
3635 struct ConnectTopologyContext *ct_ctx = connect_ctx->ct_ctx;
3636 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
3637 struct PeerConnection *connection;
3639 GNUNET_assert (NULL != connect_ctx->cc);
3640 connect_ctx->cc = NULL;
3641 GNUNET_assert (0 < pg->outstanding_connects);
3642 pg->outstanding_connects--;
3643 GNUNET_CONTAINER_DLL_remove (pg->cc_head, pg->cc_tail, connect_ctx);
3645 * Check whether the inverse connection has been scheduled yet,
3646 * if not, we can remove it from the other peers list and avoid
3647 * even trying to connect them again!
3649 connection = pg->peers[connect_ctx->second_index].connect_peers_head;
3654 while ((connection != NULL) &&
3656 memcmp (first, &pg->peers[connection->index].daemon->id,
3657 sizeof (struct GNUNET_PeerIdentity))))
3658 connection = connection->next;
3660 if (connection != NULL) /* Can safely remove! */
3662 GNUNET_assert (0 < ct_ctx->remaining_connections);
3663 ct_ctx->remaining_connections--;
3664 if (pg->notify_connection != NULL) /* Notify of reverse connection */
3665 pg->notify_connection (pg->notify_connection_cls, second, first, distance,
3666 second_cfg, first_cfg, second_daemon, first_daemon,
3669 GNUNET_CONTAINER_DLL_remove (pg->
3671 second_index].connect_peers_head,
3672 pg->peers[connect_ctx->
3673 second_index].connect_peers_tail,
3675 GNUNET_free (connection);
3678 if (ct_ctx->remaining_connections == 0)
3680 if (ct_ctx->notify_connections_done != NULL)
3682 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
3683 ct_ctx->notify_connections_done = NULL;
3687 preschedule_connect (pg);
3689 if (pg->notify_connection != NULL)
3690 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
3691 first_cfg, second_cfg, first_daemon, second_daemon,
3693 GNUNET_free (connect_ctx);
3697 * Either delay a connection (because there are too many outstanding)
3698 * or schedule it for right now.
3700 * @param cls a connection context
3701 * @param tc the task runtime context
3704 schedule_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3706 struct ConnectContext *connect_context = cls;
3707 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
3709 connect_context->task = GNUNET_SCHEDULER_NO_TASK;
3710 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3713 if ((pg->outstanding_connects > pg->max_outstanding_connections) ||
3714 (pg->stop_connects == GNUNET_YES))
3717 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3719 ("Delaying connect, we have too many outstanding connections!\n"));
3721 connect_context->task =
3722 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3723 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3724 &schedule_connect, connect_context);
3728 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3730 ("Creating connection, outstanding_connections is %d (max %d)\n"),
3731 pg->outstanding_connects, pg->max_outstanding_connections);
3733 pg->outstanding_connects++;
3734 pg->total_connects_scheduled++;
3735 GNUNET_assert (NULL == connect_context->cc);
3736 connect_context->cc =
3737 GNUNET_TESTING_daemons_connect (pg->
3738 peers[connect_context->
3739 first_index].daemon,
3740 pg->peers[connect_context->
3741 second_index].daemon,
3742 connect_context->ct_ctx->connect_timeout,
3743 connect_context->ct_ctx->connect_attempts,
3749 &internal_connect_notify,
3756 * Iterator for actually scheduling connections to be created
3757 * between two peers.
3759 * @param cls closure, a GNUNET_TESTING_Daemon
3760 * @param key the key the second Daemon was stored under
3761 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3763 * @return GNUNET_YES to continue iteration
3766 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3768 struct ConnectTopologyContext *ct_ctx = cls;
3769 struct PeerData *first = ct_ctx->first;
3770 struct GNUNET_TESTING_Daemon *second = value;
3771 struct ConnectContext *connect_context;
3773 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3774 connect_context->first = first->daemon;
3775 connect_context->second = second;
3776 connect_context->ct_ctx = ct_ctx;
3777 connect_context->task =
3778 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3779 GNUNET_CONTAINER_DLL_insert (ct_ctx->pg->cc_head, ct_ctx->pg->cc_tail,
3787 * Iterator for copying all entries in the allowed hashmap to the
3790 * @param cls closure, a GNUNET_TESTING_Daemon
3791 * @param key the key the second Daemon was stored under
3792 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3794 * @return GNUNET_YES to continue iteration
3797 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3799 struct PeerData *first = cls;
3801 GNUNET_assert (GNUNET_OK ==
3802 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
3804 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3811 * Make the peers to connect the same as those that are allowed to be
3814 * @param pg the peer group
3817 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
3819 unsigned int pg_iter;
3824 struct PeerConnection *iter;
3828 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3831 iter = pg->peers[pg_iter].allowed_peers_head;
3832 while (iter != NULL)
3834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3835 "Creating connection between %d and %d\n", pg_iter,
3837 total += add_connections (pg, pg_iter, iter->index, CONNECT, GNUNET_YES);
3838 //total += add_actual_connections(pg, pg_iter, iter->index);
3843 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
3844 ©_topology_iterator,
3845 &pg->peers[pg_iter]);
3847 if (GNUNET_SYSERR == ret)
3848 return GNUNET_SYSERR;
3850 total = total + ret;
3857 * Connect the topology as specified by the PeerConnection's
3858 * of each peer in the peer group
3860 * @param pg the peer group we are dealing with
3861 * @param connect_timeout how long try connecting two peers
3862 * @param connect_attempts how many times (max) to attempt
3863 * @param notify_callback callback to notify when finished
3864 * @param notify_cls closure for notify callback
3866 * @return the number of connections that will be attempted
3869 connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3870 struct GNUNET_TIME_Relative connect_timeout,
3871 unsigned int connect_attempts,
3872 GNUNET_TESTING_NotifyCompletion notify_callback,
3875 unsigned int pg_iter;
3879 struct PeerConnection *connection_iter;
3882 struct SendHelloContext *send_hello_context;
3886 pg->ct_ctx.notify_connections_done = notify_callback;
3887 pg->ct_ctx.notify_cls = notify_cls;
3890 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3893 connection_iter = pg->peers[pg_iter].connect_peers_head;
3894 while (connection_iter != NULL)
3896 connection_iter = connection_iter->next;
3901 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3908 pg->ct_ctx.connect_timeout = connect_timeout;
3909 pg->ct_ctx.connect_attempts = connect_attempts;
3910 pg->ct_ctx.remaining_connections = total;
3913 /* First give all peers the HELLO's of other peers (connect to first peer's transport service, give HELLO's of other peers, continue...) */
3914 pg->remaining_hellos = total;
3915 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3917 send_hello_context = GNUNET_malloc (sizeof (struct SendHelloContext));
3918 send_hello_context->peer = &pg->peers[pg_iter];
3919 send_hello_context->peer_pos = pg->peers[pg_iter].connect_peers_head;
3920 send_hello_context->pg = pg;
3921 GNUNET_SCHEDULER_add_now (&schedule_send_hellos, send_hello_context);
3924 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3926 preschedule_connect (pg);
3934 * Takes a peer group and creates a topology based on the
3935 * one specified. Creates a topology means generates friend
3936 * files for the peers so they can only connect to those allowed
3937 * by the topology. This will only have an effect once peers
3938 * are started if the FRIENDS_ONLY option is set in the base
3939 * config. Also takes an optional restrict topology which
3940 * disallows connections based on particular transports
3941 * UNLESS they are specified in the restricted topology.
3943 * @param pg the peer group struct representing the running peers
3944 * @param topology which topology to connect the peers in
3945 * @param restrict_topology disallow restrict_transports transport
3946 * connections to peers NOT in this topology
3947 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3948 * @param restrict_transports space delimited list of transports to blacklist
3949 * to create restricted topology
3951 * @return the maximum number of connections were all allowed peers
3952 * connected to each other
3955 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
3956 enum GNUNET_TESTING_Topology topology,
3957 enum GNUNET_TESTING_Topology restrict_topology,
3958 const char *restrict_transports)
3962 unsigned int num_connections;
3963 int unblacklisted_connections;
3965 struct PeerConnection *conn_iter;
3966 struct PeerConnection *temp_conn;
3972 for (i = 0; i < pg->total; i++)
3974 pg->peers[i].allowed_peers = GNUNET_CONTAINER_multihashmap_create (100);
3975 pg->peers[i].connect_peers = GNUNET_CONTAINER_multihashmap_create (100);
3976 pg->peers[i].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create (100);
3977 pg->peers[i].pg = pg;
3983 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3987 num_connections = create_clique (pg, &add_connections, ALLOWED, GNUNET_NO);
3989 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3992 _("Creating small world (ring) topology\n"));
3994 num_connections = create_small_world_ring (pg, &add_connections, ALLOWED);
3996 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3999 _("Creating small world (2d-torus) topology\n"));
4001 num_connections = create_small_world (pg, &add_connections, ALLOWED);
4003 case GNUNET_TESTING_TOPOLOGY_RING:
4005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
4007 num_connections = create_ring (pg, &add_connections, ALLOWED);
4009 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
4013 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
4015 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating Erdos-Renyi topology\n"));
4019 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
4021 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
4025 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
4027 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating Scale Free topology\n"));
4031 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
4033 case GNUNET_TESTING_TOPOLOGY_LINE:
4035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4036 _("Creating straight line topology\n"));
4038 num_connections = create_line (pg, &add_connections, ALLOWED);
4040 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
4042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating topology from file!\n"));
4045 GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing",
4046 "topology_file", &filename))
4048 create_from_file (pg, filename, &add_connections, ALLOWED);
4051 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4052 "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
4053 num_connections = 0;
4056 case GNUNET_TESTING_TOPOLOGY_NONE:
4058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4060 ("Creating no allowed topology (all peers can connect at core level)\n"));
4062 num_connections = pg->total * pg->total; /* Clique is allowed! */
4065 num_connections = 0;
4070 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
4072 ret = create_and_copy_friend_files (pg);
4073 if (ret != GNUNET_OK)
4076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4077 _("Failed during friend file copying!\n"));
4079 return GNUNET_SYSERR;
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4085 _("Friend files created/copied successfully!\n"));
4090 /* Use the create clique method to initially set all connections as blacklisted. */
4091 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) &&
4092 (restrict_topology != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
4093 create_clique (pg, &add_connections, BLACKLIST, GNUNET_NO);
4095 return num_connections;
4097 unblacklisted_connections = 0;
4098 /* Un-blacklist connections as per the topology specified */
4099 switch (restrict_topology)
4101 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4104 _("Blacklisting all but clique topology\n"));
4106 unblacklisted_connections =
4107 create_clique (pg, &remove_connections, BLACKLIST, GNUNET_NO);
4109 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4112 _("Blacklisting all but small world (ring) topology\n"));
4114 unblacklisted_connections =
4115 create_small_world_ring (pg, &remove_connections, BLACKLIST);
4117 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4120 _("Blacklisting all but small world (2d-torus) topology\n"));
4122 unblacklisted_connections =
4123 create_small_world (pg, &remove_connections, BLACKLIST);
4125 case GNUNET_TESTING_TOPOLOGY_RING:
4127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4128 _("Blacklisting all but ring topology\n"));
4130 unblacklisted_connections =
4131 create_ring (pg, &remove_connections, BLACKLIST);
4133 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4136 _("Blacklisting all but 2d torus topology\n"));
4138 unblacklisted_connections =
4139 create_2d_torus (pg, &remove_connections, BLACKLIST);
4141 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4144 _("Blacklisting all but Erdos-Renyi topology\n"));
4146 unblacklisted_connections =
4147 create_erdos_renyi (pg, &remove_connections, BLACKLIST);
4149 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4152 _("Blacklisting all but InterNAT topology\n"));
4156 for (off = 0; off < pg->total; off++)
4158 conn_iter = pg->peers[off].allowed_peers_head;
4159 while (conn_iter != NULL)
4161 temp_conn = conn_iter->next;
4162 GNUNET_free (conn_iter);
4163 conn_iter = temp_conn;
4165 pg->peers[off].allowed_peers_head = NULL;
4166 pg->peers[off].allowed_peers_tail = NULL;
4168 conn_iter = pg->peers[off].connect_peers_head;
4169 while (conn_iter != NULL)
4171 temp_conn = conn_iter->next;
4172 GNUNET_free (conn_iter);
4173 conn_iter = temp_conn;
4175 pg->peers[off].connect_peers_head = NULL;
4176 pg->peers[off].connect_peers_tail = NULL;
4178 unblacklisted_connections =
4179 create_nated_internet_copy (pg, &remove_connections, BLACKLIST);
4181 unblacklisted_connections =
4182 create_nated_internet (pg, &remove_connections, BLACKLIST);
4186 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4189 _("Blacklisting all but Scale Free topology\n"));
4191 unblacklisted_connections =
4192 create_scale_free (pg, &remove_connections, BLACKLIST);
4194 case GNUNET_TESTING_TOPOLOGY_LINE:
4196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4197 _("Blacklisting all but straight line topology\n"));
4199 unblacklisted_connections =
4200 create_line (pg, &remove_connections, BLACKLIST);
4205 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
4207 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n",
4208 restrict_transports);
4209 ret = create_and_copy_blacklist_files (pg, restrict_transports);
4210 if (ret != GNUNET_OK)
4213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4214 _("Failed during blacklist file copying!\n"));
4221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4222 _("Blacklist files created/copied successfully!\n"));
4226 return num_connections;
4231 * Iterator for choosing random peers to connect.
4233 * @param cls closure, a RandomContext
4234 * @param key the key the second Daemon was stored under
4235 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4237 * @return GNUNET_YES to continue iteration
4240 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4242 struct RandomContext *random_ctx = cls;
4243 double random_number;
4244 uint32_t second_pos;
4245 GNUNET_HashCode first_hash;
4249 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4250 UINT64_MAX)) / ((double) UINT64_MAX);
4251 if (random_number < random_ctx->percentage)
4253 GNUNET_assert (GNUNET_OK ==
4254 GNUNET_CONTAINER_multihashmap_put (random_ctx->
4255 first->connect_peers_working_set,
4257 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4260 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
4261 uid_from_hash (key, &second_pos);
4262 hash_from_uid (random_ctx->first_uid, &first_hash);
4263 GNUNET_assert (random_ctx->pg->total > second_pos);
4264 GNUNET_assert (GNUNET_YES ==
4265 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
4267 [second_pos].connect_peers,
4276 * Iterator for adding at least X peers to a peers connection set.
4278 * @param cls closure, MinimumContext
4279 * @param key the key the second Daemon was stored under
4280 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4282 * @return GNUNET_YES to continue iteration
4285 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4287 struct MinimumContext *min_ctx = cls;
4288 uint32_t second_pos;
4289 GNUNET_HashCode first_hash;
4292 if (GNUNET_CONTAINER_multihashmap_size
4293 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
4295 for (i = 0; i < min_ctx->num_to_add; i++)
4297 if (min_ctx->pg_array[i] == min_ctx->current)
4299 GNUNET_assert (GNUNET_OK ==
4300 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4301 first->connect_peers_working_set,
4303 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4304 uid_from_hash (key, &second_pos);
4305 hash_from_uid (min_ctx->first_uid, &first_hash);
4306 GNUNET_assert (min_ctx->pg->total > second_pos);
4307 GNUNET_assert (GNUNET_OK ==
4308 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4310 [second_pos].connect_peers_working_set,
4314 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4315 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
4316 GNUNET_assert (GNUNET_YES ==
4317 GNUNET_CONTAINER_multihashmap_remove (min_ctx->
4319 [second_pos].connect_peers,
4329 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
4334 * Iterator for adding peers to a connection set based on a depth first search.
4336 * @param cls closure, MinimumContext
4337 * @param key the key the second daemon was stored under
4338 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4340 * @return GNUNET_YES to continue iteration
4343 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4345 struct DFSContext *dfs_ctx = cls;
4346 GNUNET_HashCode first_hash;
4348 if (dfs_ctx->current == dfs_ctx->chosen)
4350 GNUNET_assert (GNUNET_OK ==
4351 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4352 first->connect_peers_working_set,
4354 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4355 uid_from_hash (key, &dfs_ctx->second_uid);
4356 hash_from_uid (dfs_ctx->first_uid, &first_hash);
4357 GNUNET_assert (GNUNET_OK ==
4358 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4360 second_uid].connect_peers_working_set,
4362 dfs_ctx->first->daemon,
4363 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4364 GNUNET_assert (GNUNET_YES ==
4365 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
4367 [dfs_ctx->second_uid].connect_peers,
4371 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
4372 return GNUNET_NO; /* We have found our peer, don't iterate more */
4381 * From the set of connections possible, choose percentage percent of connections
4382 * to actually connect.
4384 * @param pg the peergroup we are dealing with
4385 * @param percentage what percent of total connections to make
4388 choose_random_connections (struct GNUNET_TESTING_PeerGroup *pg,
4394 struct PeerConnection *conn_iter;
4395 double random_number;
4397 struct RandomContext random_ctx;
4400 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4403 conn_iter = pg->peers[pg_iter].connect_peers_head;
4404 while (conn_iter != NULL)
4408 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4409 UINT64_MAX)) / ((double) UINT64_MAX);
4410 if (random_number < percentage)
4412 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4415 conn_iter = conn_iter->next;
4418 random_ctx.first_uid = pg_iter;
4419 random_ctx.first = &pg->peers[pg_iter];
4420 random_ctx.percentage = percentage;
4422 pg->peers[pg_iter].connect_peers_working_set =
4423 GNUNET_CONTAINER_multihashmap_create (pg->total);
4424 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4425 &random_connect_iterator,
4427 /* Now remove the old connections */
4428 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[pg_iter].connect_peers);
4429 /* And replace with the random set */
4430 pg->peers[pg_iter].connect_peers =
4431 pg->peers[pg_iter].connect_peers_working_set;
4435 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4437 conn_iter = pg->peers[pg_iter].connect_peers_head;
4438 while (pg->peers[pg_iter].connect_peers_head != NULL)
4439 remove_connections (pg, pg_iter,
4440 pg->peers[pg_iter].connect_peers_head->index, CONNECT,
4443 pg->peers[pg_iter].connect_peers_head =
4444 pg->peers[pg_iter].connect_peers_working_set_head;
4445 pg->peers[pg_iter].connect_peers_tail =
4446 pg->peers[pg_iter].connect_peers_working_set_tail;
4447 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4448 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4453 * Count the number of connections in a linked list of connections.
4455 * @param conn_list the connection list to get the count of
4457 * @return the number of elements in the list
4460 count_connections (struct PeerConnection *conn_list)
4462 struct PeerConnection *iter;
4467 while (iter != NULL)
4476 count_workingset_connections (struct GNUNET_TESTING_PeerGroup *pg)
4479 unsigned int pg_iter;
4482 struct PeerConnection *conn_iter;
4486 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4489 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
4490 while (conn_iter != NULL)
4493 conn_iter = conn_iter->next;
4497 GNUNET_CONTAINER_multihashmap_size (pg->
4499 [pg_iter].connect_peers_working_set);
4507 count_allowed_connections (struct GNUNET_TESTING_PeerGroup *pg)
4510 unsigned int pg_iter;
4513 struct PeerConnection *conn_iter;
4517 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4520 conn_iter = pg->peers[pg_iter].allowed_peers_head;
4521 while (conn_iter != NULL)
4524 conn_iter = conn_iter->next;
4528 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].allowed_peers);
4536 * From the set of connections possible, choose at least num connections per
4539 * @param pg the peergroup we are dealing with
4540 * @param num how many connections at least should each peer have (if possible)?
4543 choose_minimum (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4546 struct MinimumContext minimum_ctx;
4548 struct PeerConnection *conn_iter;
4549 unsigned int temp_list_size;
4552 uint32_t random; /* Random list entry to connect peer to */
4557 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4559 temp_list_size = count_connections (pg->peers[pg_iter].connect_peers_head);
4560 if (temp_list_size == 0)
4562 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Peer %d has 0 connections!?!?\n",
4566 for (i = 0; i < num; i++)
4569 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, temp_list_size);
4570 conn_iter = pg->peers[pg_iter].connect_peers_head;
4571 for (count = 0; count < random; count++)
4572 conn_iter = conn_iter->next;
4573 /* We now have a random connection, connect it! */
4574 GNUNET_assert (conn_iter != NULL);
4575 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET, GNUNET_YES);
4579 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4581 pg->peers[pg_iter].connect_peers_working_set =
4582 GNUNET_CONTAINER_multihashmap_create (num);
4585 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4587 minimum_ctx.first_uid = pg_iter;
4588 minimum_ctx.pg_array =
4589 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
4590 GNUNET_CONTAINER_multihashmap_size
4591 (pg->peers[pg_iter].connect_peers));
4592 minimum_ctx.first = &pg->peers[pg_iter];
4593 minimum_ctx.pg = pg;
4594 minimum_ctx.num_to_add = num;
4595 minimum_ctx.current = 0;
4596 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4597 &minimum_connect_iterator,
4601 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4603 /* Remove the "old" connections */
4604 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[pg_iter].connect_peers);
4605 /* And replace with the working set */
4606 pg->peers[pg_iter].connect_peers =
4607 pg->peers[pg_iter].connect_peers_working_set;
4610 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4612 while (pg->peers[pg_iter].connect_peers_head != NULL)
4614 conn_iter = pg->peers[pg_iter].connect_peers_head;
4615 GNUNET_CONTAINER_DLL_remove (pg->peers[pg_iter].connect_peers_head,
4616 pg->peers[pg_iter].connect_peers_tail,
4618 GNUNET_free (conn_iter);
4619 /*remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT, GNUNET_YES); */
4622 pg->peers[pg_iter].connect_peers_head =
4623 pg->peers[pg_iter].connect_peers_working_set_head;
4624 pg->peers[pg_iter].connect_peers_tail =
4625 pg->peers[pg_iter].connect_peers_working_set_tail;
4626 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4627 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4632 struct FindClosestContext
4635 * The currently known closest peer.
4637 struct GNUNET_TESTING_Daemon *closest;
4640 * The info for the peer we are adding connections for.
4642 struct PeerData *curr_peer;
4645 * The distance (bits) between the current
4646 * peer and the currently known closest.
4648 unsigned int closest_dist;
4651 * The offset of the closest known peer in
4654 unsigned int closest_num;
4658 * Iterator over hash map entries of the allowed
4659 * peer connections. Find the closest, not already
4660 * connected peer and return it.
4662 * @param cls closure (struct FindClosestContext)
4663 * @param key current key code (hash of offset in pg)
4664 * @param value value in the hash map - a GNUNET_TESTING_Daemon
4665 * @return GNUNET_YES if we should continue to
4670 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
4672 struct FindClosestContext *closest_ctx = cls;
4673 struct GNUNET_TESTING_Daemon *daemon = value;
4675 if (((closest_ctx->closest == NULL) ||
4676 (GNUNET_CRYPTO_hash_matching_bits
4677 (&daemon->id.hashPubKey,
4678 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
4679 closest_ctx->closest_dist)) &&
4681 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
4682 curr_peer->connect_peers, key)))
4684 closest_ctx->closest_dist =
4685 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
4686 &closest_ctx->curr_peer->daemon->
4688 closest_ctx->closest = daemon;
4689 uid_from_hash (key, &closest_ctx->closest_num);
4695 * From the set of connections possible, choose at num connections per
4696 * peer based on depth which are closest out of those allowed. Guaranteed
4697 * to add num peers to connect to, provided there are that many peers
4698 * in the underlay topology to connect to.
4700 * @param pg the peergroup we are dealing with
4701 * @param num how many connections at least should each peer have (if possible)?
4702 * @param proc processor to actually add the connections
4703 * @param list the peer list to use
4706 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
4707 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
4712 struct FindClosestContext closest_ctx;
4717 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
4719 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4721 closest_ctx.curr_peer = &pg->peers[pg_iter];
4722 closest_ctx.closest = NULL;
4723 closest_ctx.closest_dist = 0;
4724 closest_ctx.closest_num = 0;
4725 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
4726 &find_closest_peers, &closest_ctx);
4727 if (closest_ctx.closest != NULL)
4729 GNUNET_assert (closest_ctx.closest_num < pg->total);
4730 proc (pg, pg_iter, closest_ctx.closest_num, list);
4738 * From the set of connections possible, choose at least num connections per
4739 * peer based on depth first traversal of peer connections. If DFS leaves
4740 * peers unconnected, ensure those peers get connections.
4742 * @param pg the peergroup we are dealing with
4743 * @param num how many connections at least should each peer have (if possible)?
4746 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4750 uint32_t starting_peer;
4751 uint32_t least_connections;
4752 uint32_t random_connection;
4755 unsigned int temp_count;
4756 struct PeerConnection *peer_iter;
4758 struct DFSContext dfs_ctx;
4759 GNUNET_HashCode second_hash;
4765 while ((count_workingset_connections (pg) < num * pg->total) &&
4766 (count_allowed_connections (pg) > 0))
4768 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4770 least_connections = -1; /* Set to very high number */
4771 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4774 count_connections (pg->
4775 peers[pg_iter].connect_peers_working_set_head);
4776 if (temp_count < least_connections)
4778 starting_peer = pg_iter;
4779 least_connections = temp_count;
4785 count_connections (pg->peers[starting_peer].connect_peers_head);
4786 if (temp_count == 0)
4787 continue; /* FIXME: infinite loop? */
4790 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, temp_count);
4792 peer_iter = pg->peers[starting_peer].connect_peers_head;
4793 while (temp_count < random_connection)
4795 peer_iter = peer_iter->next;
4798 GNUNET_assert (peer_iter != NULL);
4799 add_connections (pg, starting_peer, peer_iter->index, WORKING_SET,
4801 remove_connections (pg, starting_peer, peer_iter->index, CONNECT,
4803 starting_peer = peer_iter->index;
4808 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4810 pg->peers[pg_iter].connect_peers_working_set =
4811 GNUNET_CONTAINER_multihashmap_create (num);
4816 while ((count_workingset_connections (pg) < num * pg->total) &&
4817 (count_allowed_connections (pg) > 0))
4819 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4821 least_connections = -1; /* Set to very high number */
4822 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4824 if (GNUNET_CONTAINER_multihashmap_size
4825 (pg->peers[pg_iter].connect_peers_working_set) < least_connections)
4827 starting_peer = pg_iter;
4829 GNUNET_CONTAINER_multihashmap_size (pg->
4831 [pg_iter].connect_peers_working_set);
4836 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4842 /* Choose a random peer from the chosen peers set of connections to add */
4844 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4845 GNUNET_CONTAINER_multihashmap_size (pg->peers
4846 [starting_peer].connect_peers));
4847 dfs_ctx.first_uid = starting_peer;
4848 dfs_ctx.first = &pg->peers[starting_peer];
4850 dfs_ctx.current = 0;
4852 GNUNET_CONTAINER_multihashmap_iterate (pg->
4853 peers[starting_peer].connect_peers,
4854 &dfs_connect_iterator, &dfs_ctx);
4855 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4856 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4857 GNUNET_assert (GNUNET_YES ==
4858 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4859 [starting_peer].connect_peers,
4863 [dfs_ctx.second_uid].daemon));
4864 starting_peer = dfs_ctx.second_uid;
4867 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4869 /* Remove the "old" connections */
4870 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[pg_iter].connect_peers);
4871 /* And replace with the working set */
4872 pg->peers[pg_iter].connect_peers =
4873 pg->peers[pg_iter].connect_peers_working_set;
4879 * Internal callback for topology information for a particular peer.
4882 internal_topology_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
4883 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4885 struct CoreContext *core_ctx = cls;
4886 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4888 if (peer == NULL) /* Either finished, or something went wrong */
4890 iter_ctx->completed++;
4891 iter_ctx->connected--;
4892 /* One core context allocated per iteration, must free! */
4893 GNUNET_free (core_ctx);
4897 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id, peer, NULL);
4900 if (iter_ctx->completed == iter_ctx->total)
4902 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4903 /* Once all are done, free the iteration context */
4904 GNUNET_free (iter_ctx);
4909 * Check running topology iteration tasks, if below max start a new one, otherwise
4910 * schedule for some time in the future.
4913 schedule_get_topology (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4915 struct CoreContext *core_context = cls;
4916 struct TopologyIterateContext *topology_context =
4917 (struct TopologyIterateContext *) core_context->iter_context;
4918 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
4921 if (topology_context->connected >
4922 topology_context->pg->max_outstanding_connections)
4924 #if VERBOSE_TESTING > 2
4925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4927 ("Delaying connect, we have too many outstanding connections!\n"));
4929 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4930 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
4931 &schedule_get_topology, core_context);
4935 #if VERBOSE_TESTING > 2
4936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4937 _("Creating connection, outstanding_connections is %d\n"),
4938 outstanding_connects);
4940 topology_context->connected++;
4943 GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4944 &internal_topology_callback, core_context))
4946 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4947 internal_topology_callback (core_context, NULL, NULL);
4953 * Iterate over all (running) peers in the peer group, retrieve
4954 * all connections that each currently has.
4957 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
4958 GNUNET_TESTING_NotifyTopology cb, void *cls)
4960 struct TopologyIterateContext *topology_context;
4961 struct CoreContext *core_ctx;
4963 unsigned int total_count;
4965 /* Allocate a single topology iteration context */
4966 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4967 topology_context->topology_cb = cb;
4968 topology_context->cls = cls;
4969 topology_context->pg = pg;
4971 for (i = 0; i < pg->total; i++)
4973 if (pg->peers[i].daemon->running == GNUNET_YES)
4975 /* Allocate one core context per core we need to connect to */
4976 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4977 core_ctx->daemon = pg->peers[i].daemon;
4978 /* Set back pointer to topology iteration context */
4979 core_ctx->iter_context = topology_context;
4980 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4984 if (total_count == 0)
4986 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4987 GNUNET_free (topology_context);
4990 topology_context->total = total_count;
4995 * Callback function to process statistic values.
4996 * This handler is here only really to insert a peer
4997 * identity (or daemon) so the statistics can be uniquely
4998 * tied to a single running peer.
5000 * @param cls closure
5001 * @param subsystem name of subsystem that created the statistic
5002 * @param name the name of the datum
5003 * @param value the current value
5004 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
5005 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
5008 internal_stats_callback (void *cls, const char *subsystem, const char *name,
5009 uint64_t value, int is_persistent)
5011 struct StatsCoreContext *core_context = cls;
5012 struct StatsIterateContext *stats_context =
5013 (struct StatsIterateContext *) core_context->iter_context;
5015 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
5016 subsystem, name, value, is_persistent);
5020 * Internal continuation call for statistics iteration.
5022 * @param cls closure, the CoreContext for this iteration
5023 * @param success whether or not the statistics iterations
5024 * was canceled or not (we don't care)
5027 internal_stats_cont (void *cls, int success)
5029 struct StatsCoreContext *core_context = cls;
5030 struct StatsIterateContext *stats_context =
5031 (struct StatsIterateContext *) core_context->iter_context;
5033 stats_context->connected--;
5034 stats_context->completed++;
5036 if (stats_context->completed == stats_context->total)
5038 stats_context->cont (stats_context->cls, GNUNET_YES);
5039 GNUNET_free (stats_context);
5042 if (core_context->stats_handle != NULL)
5043 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
5045 GNUNET_free (core_context);
5049 * Check running topology iteration tasks, if below max start a new one, otherwise
5050 * schedule for some time in the future.
5053 schedule_get_statistics (void *cls,
5054 const struct GNUNET_SCHEDULER_TaskContext *tc)
5056 struct StatsCoreContext *core_context = cls;
5057 struct StatsIterateContext *stats_context =
5058 (struct StatsIterateContext *) core_context->iter_context;
5060 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5063 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
5065 #if VERBOSE_TESTING > 2
5066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5068 ("Delaying connect, we have too many outstanding connections!\n"));
5070 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5071 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5072 &schedule_get_statistics, core_context);
5076 #if VERBOSE_TESTING > 2
5077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5078 _("Creating connection, outstanding_connections is %d\n"),
5079 outstanding_connects);
5082 stats_context->connected++;
5083 core_context->stats_handle =
5084 GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
5085 if (core_context->stats_handle == NULL)
5087 internal_stats_cont (core_context, GNUNET_NO);
5091 core_context->stats_get_handle =
5092 GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
5093 GNUNET_TIME_relative_get_forever (),
5094 &internal_stats_cont, &internal_stats_callback,
5096 if (core_context->stats_get_handle == NULL)
5097 internal_stats_cont (core_context, GNUNET_NO);
5102 struct DuplicateStats
5105 * Next item in the list
5107 struct DuplicateStats *next;
5110 * Nasty string, concatenation of relevant information.
5112 char *unique_string;
5116 * Check whether the combination of port/host/unix domain socket
5117 * already exists in the list of peers being checked for statistics.
5119 * @param pg the peergroup in question
5120 * @param specific_peer the peer we're concerned with
5121 * @param stats_list the list to return to the caller
5123 * @return GNUNET_YES if the statistics instance has been seen already,
5124 * GNUNET_NO if not (and we may have added it to the list)
5127 stats_check_existing (struct GNUNET_TESTING_PeerGroup *pg,
5128 struct PeerData *specific_peer,
5129 struct DuplicateStats **stats_list)
5131 struct DuplicateStats *pos;
5132 char *unix_domain_socket;
5133 unsigned long long port;
5137 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
5138 "single_statistics_per_host"))
5139 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
5143 GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg, "statistics",
5144 "unixpath", &unix_domain_socket))
5148 GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg, "statistics",
5151 GNUNET_free (unix_domain_socket);
5155 if (specific_peer->daemon->hostname != NULL)
5156 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
5157 unix_domain_socket, port);
5159 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
5163 if (0 == strcmp (to_match, pos->unique_string))
5165 GNUNET_free (unix_domain_socket);
5166 GNUNET_free (to_match);
5171 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
5172 pos->unique_string = to_match;
5173 pos->next = *stats_list;
5175 GNUNET_free (unix_domain_socket);
5180 * Iterate over all (running) peers in the peer group, retrieve
5181 * all statistics from each.
5183 * @param pg the peergroup to iterate statistics of
5184 * @param cont continuation to call once all stats have been retrieved
5185 * @param proc processing function for each statistic from each peer
5186 * @param cls closure to pass to proc
5190 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
5191 GNUNET_STATISTICS_Callback cont,
5192 GNUNET_TESTING_STATISTICS_Iterator proc,
5195 struct StatsIterateContext *stats_context;
5196 struct StatsCoreContext *core_ctx;
5198 unsigned int total_count;
5199 struct DuplicateStats *stats_list;
5200 struct DuplicateStats *pos;
5204 /* Allocate a single stats iteration context */
5205 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
5206 stats_context->cont = cont;
5207 stats_context->proc = proc;
5208 stats_context->cls = cls;
5209 stats_context->pg = pg;
5212 for (i = 0; i < pg->total; i++)
5214 if ((pg->peers[i].daemon->running == GNUNET_YES) &&
5215 (GNUNET_NO == stats_check_existing (pg, &pg->peers[i], &stats_list)))
5217 /* Allocate one core context per core we need to connect to */
5218 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
5219 core_ctx->daemon = pg->peers[i].daemon;
5220 /* Set back pointer to topology iteration context */
5221 core_ctx->iter_context = stats_context;
5222 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
5227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5228 "Retrieving stats from %u total instances.\n", total_count);
5229 stats_context->total = total_count;
5230 if (stats_list != NULL)
5235 GNUNET_free (pos->unique_string);
5236 stats_list = pos->next;
5238 pos = stats_list->next;
5245 * Stop the connection process temporarily.
5247 * @param pg the peer group to stop connecting
5250 GNUNET_TESTING_stop_connections (struct GNUNET_TESTING_PeerGroup *pg)
5252 pg->stop_connects = GNUNET_YES;
5256 * Resume the connection process temporarily.
5258 * @param pg the peer group to resume connecting
5261 GNUNET_TESTING_resume_connections (struct GNUNET_TESTING_PeerGroup *pg)
5263 pg->stop_connects = GNUNET_NO;
5267 * There are many ways to connect peers that are supported by this function.
5268 * To connect peers in the same topology that was created via the
5269 * GNUNET_TESTING_create_topology, the topology variable must be set to
5270 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
5271 * a new instance of that topology will be generated and attempted to be
5272 * connected. This could result in some connections being impossible,
5273 * because some topologies are non-deterministic.
5275 * @param pg the peer group struct representing the running peers
5276 * @param topology which topology to connect the peers in
5277 * @param options options for connecting the topology
5278 * @param option_modifier modifier for options that take a parameter
5279 * @param connect_timeout how long to wait before giving up on connecting
5281 * @param connect_attempts how many times to attempt to connect two peers
5282 * over the connect_timeout duration
5283 * @param notify_callback notification to be called once all connections completed
5284 * @param notify_cls closure for notification callback
5286 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
5289 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
5290 enum GNUNET_TESTING_Topology topology,
5291 enum GNUNET_TESTING_TopologyOption options,
5292 double option_modifier,
5293 struct GNUNET_TIME_Relative connect_timeout,
5294 unsigned int connect_attempts,
5295 GNUNET_TESTING_NotifyCompletion
5296 notify_callback, void *notify_cls)
5300 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
5301 #if VERBOSE_TOPOLOGY
5302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5303 _("Creating clique CONNECT topology\n"));
5305 create_clique (pg, &add_connections, CONNECT, GNUNET_NO);
5307 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
5308 #if VERBOSE_TOPOLOGY
5309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5310 _("Creating small world (ring) CONNECT topology\n"));
5312 create_small_world_ring (pg, &add_connections, CONNECT);
5314 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
5315 #if VERBOSE_TOPOLOGY
5316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5317 _("Creating small world (2d-torus) CONNECT topology\n"));
5319 create_small_world (pg, &add_connections, CONNECT);
5321 case GNUNET_TESTING_TOPOLOGY_RING:
5322 #if VERBOSE_TOPOLOGY
5323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring CONNECT topology\n"));
5325 create_ring (pg, &add_connections, CONNECT);
5327 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
5328 #if VERBOSE_TOPOLOGY
5329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5330 _("Creating 2d torus CONNECT topology\n"));
5332 create_2d_torus (pg, &add_connections, CONNECT);
5334 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
5335 #if VERBOSE_TOPOLOGY
5336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5337 _("Creating Erdos-Renyi CONNECT topology\n"));
5339 create_erdos_renyi (pg, &add_connections, CONNECT);
5341 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
5342 #if VERBOSE_TOPOLOGY
5343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5344 _("Creating InterNAT CONNECT topology\n"));
5346 create_nated_internet (pg, &add_connections, CONNECT);
5348 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
5349 #if VERBOSE_TOPOLOGY
5350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5351 _("Creating Scale Free CONNECT topology\n"));
5353 create_scale_free (pg, &add_connections, CONNECT);
5355 case GNUNET_TESTING_TOPOLOGY_LINE:
5356 #if VERBOSE_TOPOLOGY
5357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5358 _("Creating straight line CONNECT topology\n"));
5360 create_line (pg, &add_connections, CONNECT);
5362 case GNUNET_TESTING_TOPOLOGY_NONE:
5363 #if VERBOSE_TOPOLOGY
5364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating no CONNECT topology\n"));
5366 copy_allowed_topology (pg);
5369 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5370 _("Unknown topology specification, can't connect peers!\n"));
5371 return GNUNET_SYSERR;
5376 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
5377 #if VERBOSE_TOPOLOGY
5378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5380 ("Connecting random subset (%'.2f percent) of possible peers\n"),
5381 100 * option_modifier);
5383 choose_random_connections (pg, option_modifier);
5385 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
5386 #if VERBOSE_TOPOLOGY
5387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5388 _("Connecting a minimum of %u peers each (if possible)\n"),
5389 (unsigned int) option_modifier);
5391 choose_minimum (pg, (unsigned int) option_modifier);
5393 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
5394 #if VERBOSE_TOPOLOGY
5395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5397 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
5398 (unsigned int) option_modifier);
5401 perform_dfs (pg, (int) option_modifier);
5404 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
5405 #if VERBOSE_TOPOLOGY
5406 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5407 _("Finding additional %u closest peers each (if possible)\n"),
5408 (unsigned int) option_modifier);
5411 add_closest (pg, (unsigned int) option_modifier, &add_connections, CONNECT);
5414 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
5416 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
5422 return connect_topology (pg, connect_timeout, connect_attempts,
5423 notify_callback, notify_cls);
5427 * Lookup and return the number of SSH connections to a host.
5429 * @param hostname the hostname to lookup in the list
5430 * @param pg the peergroup that the host belongs to
5432 * @return the number of current ssh connections to the host
5435 count_outstanding_at_host (const char *hostname,
5436 struct GNUNET_TESTING_PeerGroup *pg)
5438 struct OutstandingSSH *pos;
5441 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5443 GNUNET_assert (pos != NULL);
5444 return pos->outstanding;
5448 * Increment the number of SSH connections to a host by one.
5450 * @param hostname the hostname to lookup in the list
5451 * @param pg the peergroup that the host belongs to
5455 increment_outstanding_at_host (const char *hostname,
5456 struct GNUNET_TESTING_PeerGroup *pg)
5458 struct OutstandingSSH *pos;
5461 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5463 GNUNET_assert (pos != NULL);
5468 * Decrement the number of SSH connections to a host by one.
5470 * @param hostname the hostname to lookup in the list
5471 * @param pg the peergroup that the host belongs to
5475 decrement_outstanding_at_host (const char *hostname,
5476 struct GNUNET_TESTING_PeerGroup *pg)
5478 struct OutstandingSSH *pos;
5481 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5483 GNUNET_assert (pos != NULL);
5488 * Callback that is called whenever a hostkey is generated
5489 * for a peer. Call the real callback and decrement the
5490 * starting counter for the peergroup.
5492 * @param cls closure
5493 * @param id identifier for the daemon, NULL on error
5494 * @param d handle for the daemon
5495 * @param emsg error message (NULL on success)
5498 internal_hostkey_callback (void *cls, const struct GNUNET_PeerIdentity *id,
5499 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5501 struct InternalStartContext *internal_context = cls;
5503 internal_context->peer->pg->starting--;
5504 internal_context->peer->pg->started++;
5505 if (internal_context->hostname != NULL)
5506 decrement_outstanding_at_host (internal_context->hostname,
5507 internal_context->peer->pg);
5508 if (internal_context->hostkey_callback != NULL)
5509 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
5511 else if (internal_context->peer->pg->started ==
5512 internal_context->peer->pg->total)
5514 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
5515 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
5520 * Callback that is called whenever a peer has finished starting.
5521 * Call the real callback and decrement the starting counter
5522 * for the peergroup.
5524 * @param cls closure
5525 * @param id identifier for the daemon, NULL on error
5527 * @param d handle for the daemon
5528 * @param emsg error message (NULL on success)
5531 internal_startup_callback (void *cls, const struct GNUNET_PeerIdentity *id,
5532 const struct GNUNET_CONFIGURATION_Handle *cfg,
5533 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5535 struct InternalStartContext *internal_context = cls;
5537 internal_context->peer->pg->starting--;
5538 if (internal_context->hostname != NULL)
5539 decrement_outstanding_at_host (internal_context->hostname,
5540 internal_context->peer->pg);
5541 if (internal_context->start_cb != NULL)
5542 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
5547 internal_continue_startup (void *cls,
5548 const struct GNUNET_SCHEDULER_TaskContext *tc)
5550 struct InternalStartContext *internal_context = cls;
5552 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5557 if ((internal_context->peer->pg->starting <
5558 internal_context->peer->pg->max_concurrent_ssh) ||
5559 ((internal_context->hostname != NULL) &&
5560 (count_outstanding_at_host
5561 (internal_context->hostname,
5562 internal_context->peer->pg) <
5563 internal_context->peer->pg->max_concurrent_ssh)))
5565 if (internal_context->hostname != NULL)
5566 increment_outstanding_at_host (internal_context->hostname,
5567 internal_context->peer->pg);
5568 internal_context->peer->pg->starting++;
5569 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
5573 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5574 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5575 &internal_continue_startup, internal_context);
5580 * Callback for informing us about a successful
5581 * or unsuccessful churn start call.
5583 * @param cls a ChurnContext
5584 * @param id the peer identity of the started peer
5585 * @param cfg the handle to the configuration of the peer
5586 * @param d handle to the daemon for the peer
5587 * @param emsg NULL on success, non-NULL on failure
5591 churn_start_callback (void *cls, const struct GNUNET_PeerIdentity *id,
5592 const struct GNUNET_CONFIGURATION_Handle *cfg,
5593 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5595 struct ChurnRestartContext *startup_ctx = cls;
5596 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
5598 unsigned int total_left;
5599 char *error_message;
5601 error_message = NULL;
5604 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5605 "Churn stop callback failed with error `%s'\n", emsg);
5606 churn_ctx->num_failed_start++;
5610 churn_ctx->num_to_start--;
5614 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
5615 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5617 if (total_left == 0)
5619 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
5620 GNUNET_asprintf (&error_message,
5621 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5622 churn_ctx->num_failed_start, churn_ctx->num_failed_stop);
5623 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5624 GNUNET_free_non_null (error_message);
5625 GNUNET_free (churn_ctx);
5626 GNUNET_free (startup_ctx);
5631 schedule_churn_restart (void *cls,
5632 const struct GNUNET_SCHEDULER_TaskContext *tc)
5634 struct PeerRestartContext *peer_restart_ctx = cls;
5635 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
5637 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5638 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5639 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5640 &schedule_churn_restart, peer_restart_ctx);
5643 if (startup_ctx->churn_ctx->service != NULL)
5644 GNUNET_TESTING_daemon_start_stopped_service (peer_restart_ctx->daemon,
5647 startup_ctx->timeout,
5648 &churn_start_callback,
5651 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
5652 startup_ctx->timeout,
5653 &churn_start_callback, startup_ctx);
5654 GNUNET_free (peer_restart_ctx);
5659 * Callback for informing us about a successful
5660 * or unsuccessful churn start call.
5662 * @param cls a struct ServiceStartContext *startup_ctx
5663 * @param id the peer identity of the started peer
5664 * @param cfg the handle to the configuration of the peer
5665 * @param d handle to the daemon for the peer
5666 * @param emsg NULL on success, non-NULL on failure
5670 service_start_callback (void *cls, const struct GNUNET_PeerIdentity *id,
5671 const struct GNUNET_CONFIGURATION_Handle *cfg,
5672 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5674 struct ServiceStartContext *startup_ctx = (struct ServiceStartContext *) cls;
5678 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5679 "Service start failed with error `%s'\n", emsg);
5682 startup_ctx->outstanding--;
5683 startup_ctx->remaining--;
5685 if (startup_ctx->remaining == 0)
5687 startup_ctx->cb (startup_ctx->cb_cls, NULL);
5688 GNUNET_free (startup_ctx->service);
5689 GNUNET_free (startup_ctx);
5694 schedule_service_start (void *cls,
5695 const struct GNUNET_SCHEDULER_TaskContext *tc)
5697 struct PeerServiceStartContext *peer_ctx = cls;
5698 struct ServiceStartContext *startup_ctx = peer_ctx->start_ctx;
5700 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5701 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5702 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5703 &schedule_service_start, peer_ctx);
5707 GNUNET_TESTING_daemon_start_service (peer_ctx->daemon, startup_ctx->service,
5708 startup_ctx->timeout,
5709 &service_start_callback, startup_ctx);
5710 GNUNET_free (peer_ctx);
5716 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5718 struct InternalStartContext *internal_context = cls;
5720 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5725 if ((internal_context->peer->pg->starting <
5726 internal_context->peer->pg->max_concurrent_ssh) ||
5727 ((internal_context->hostname != NULL) &&
5728 (count_outstanding_at_host
5729 (internal_context->hostname,
5730 internal_context->peer->pg) <
5731 internal_context->peer->pg->max_concurrent_ssh)))
5733 if (internal_context->hostname != NULL)
5734 increment_outstanding_at_host (internal_context->hostname,
5735 internal_context->peer->pg);
5736 internal_context->peer->pg->starting++;
5737 internal_context->peer->daemon =
5738 GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
5739 internal_context->timeout, GNUNET_NO,
5740 internal_context->hostname,
5741 internal_context->username,
5742 internal_context->sshport,
5743 internal_context->hostkey,
5744 &internal_hostkey_callback,
5746 &internal_startup_callback,
5751 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
5752 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
5753 &internal_start, internal_context);
5757 #if USE_START_HELPER
5759 struct PeerStartHelperContext
5761 struct GNUNET_TESTING_PeerGroup *pg;
5763 struct HostData *host;
5765 struct GNUNET_OS_Process *proc;
5769 check_peers_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5771 struct PeerStartHelperContext *helper = cls;
5772 enum GNUNET_OS_ProcessStatusType type;
5775 GNUNET_TESTING_NotifyDaemonRunning cb;
5777 if (GNUNET_NO == GNUNET_OS_process_status (helper->proc, &type, &code)) /* Still running, wait some more! */
5779 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
5780 &check_peers_started, helper);
5784 helper->pg->starting--;
5785 if (helper->pg->starting == 0) /* All peers have finished starting! */
5787 /* Call the peer started callback for each peer, set proper FSM state (?) */
5788 for (i = 0; i < helper->pg->total; i++)
5790 cb = helper->pg->peers[i].daemon->cb;
5791 helper->pg->peers[i].daemon->cb = NULL;
5792 helper->pg->peers[i].daemon->running = GNUNET_YES;
5793 helper->pg->peers[i].daemon->phase = SP_START_DONE;
5796 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
5797 cb (helper->pg->peers[i].daemon->cb_cls,
5798 &helper->pg->peers[i].daemon->id,
5799 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5800 "Failed to execute peerStartHelper.pl, or return code bad!");
5802 cb (helper->pg->peers[i].daemon->cb_cls,
5803 &helper->pg->peers[i].daemon->id,
5804 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5811 GNUNET_OS_process_close (helper->proc);
5815 start_peer_helper (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5817 struct PeerStartHelperContext *helper = cls;
5818 char *baseservicehome;
5822 /* ssh user@host peerStartHelper /path/to/basedirectory */
5823 GNUNET_assert (GNUNET_OK ==
5824 GNUNET_CONFIGURATION_get_value_string (helper->pg->cfg,
5825 "PATHS", "SERVICEHOME",
5827 GNUNET_asprintf (&tempdir, "%s/%s/", baseservicehome, helper->host->hostname);
5828 if (NULL != helper->host->username)
5829 GNUNET_asprintf (&arg, "%s@%s", helper->host->username,
5830 helper->host->hostname);
5832 GNUNET_asprintf (&arg, "%s", helper->host->hostname);
5834 /* FIXME: Doesn't support ssh_port option! */
5836 GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5837 "peerStartHelper.pl", tempdir, NULL);
5838 GNUNET_assert (helper->proc != NULL);
5839 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5840 "starting peers with cmd ssh %s %s %s\n", arg,
5841 "peerStartHelper.pl", tempdir);
5842 GNUNET_SCHEDULER_add_now (&check_peers_started, helper);
5843 GNUNET_free (tempdir);
5844 GNUNET_free (baseservicehome);
5850 * Function which continues a peer group starting up
5851 * after successfully generating hostkeys for each peer.
5853 * @param pg the peer group to continue starting
5857 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg)
5861 #if USE_START_HELPER
5862 if ((pg->num_hosts > 0) && (pg->hostkey_data != NULL))
5864 struct PeerStartHelperContext *helper;
5866 pg->starting = pg->num_hosts;
5867 for (i = 0; i < pg->num_hosts; i++)
5869 helper = GNUNET_malloc (sizeof (struct PeerStartHelperContext));
5871 helper->host = &pg->hosts[i];
5872 GNUNET_SCHEDULER_add_now (&start_peer_helper, helper);
5878 for (i = 0; i < pg->total; i++)
5880 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5881 &pg->peers[i].internal_context);
5886 for (i = 0; i < pg->total; i++)
5888 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5889 &pg->peers[i].internal_context);
5894 #if USE_START_HELPER
5896 call_hostkey_callbacks (void *cls,
5897 const struct GNUNET_SCHEDULER_TaskContext *tc)
5899 struct GNUNET_TESTING_PeerGroup *pg = cls;
5902 for (i = 0; i < pg->total; i++)
5904 if (pg->peers[i].internal_context.hostkey_callback != NULL)
5905 pg->peers[i].internal_context.hostkey_callback (pg->peers[i].
5906 internal_context.hostkey_cls,
5907 &pg->peers[i].daemon->id,
5908 pg->peers[i].daemon,
5912 if (pg->peers[0].internal_context.hostkey_callback == NULL)
5913 GNUNET_TESTING_daemons_continue_startup (pg);
5918 * Start count gnunet instances with the same set of transports and
5919 * applications. The port numbers (any option called "PORT") will be
5920 * adjusted to ensure that no two peers running on the same system
5921 * have the same port(s) in their respective configurations.
5923 * @param cfg configuration template to use
5924 * @param total number of daemons to start
5925 * @param max_concurrent_connections for testing, how many peers can
5926 * we connect to simultaneously
5927 * @param max_concurrent_ssh when starting with ssh, how many ssh
5928 * connections will we allow at once (based on remote hosts allowed!)
5929 * @param timeout total time allowed for peers to start
5930 * @param hostkey_callback function to call on each peers hostkey generation
5931 * if NULL, peers will be started by this call, if non-null,
5932 * GNUNET_TESTING_daemons_continue_startup must be called after
5933 * successful hostkey generation
5934 * @param hostkey_cls closure for hostkey callback
5935 * @param cb function to call on each daemon that was started
5936 * @param cb_cls closure for cb
5937 * @param connect_callback function to call each time two hosts are connected
5938 * @param connect_callback_cls closure for connect_callback
5939 * @param hostnames linked list of host structs to use to start peers on
5940 * (NULL to run on localhost only)
5942 * @return NULL on error, otherwise handle to control peer group
5944 struct GNUNET_TESTING_PeerGroup *
5945 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
5947 unsigned int max_concurrent_connections,
5948 unsigned int max_concurrent_ssh,
5949 struct GNUNET_TIME_Relative timeout,
5950 GNUNET_TESTING_NotifyHostkeyCreated
5951 hostkey_callback, void *hostkey_cls,
5952 GNUNET_TESTING_NotifyDaemonRunning cb,
5954 GNUNET_TESTING_NotifyConnection connect_callback,
5955 void *connect_callback_cls,
5956 const struct GNUNET_TESTING_Host *hostnames)
5958 struct GNUNET_TESTING_PeerGroup *pg;
5959 const struct GNUNET_TESTING_Host *hostpos;
5960 const char *hostname;
5961 const char *username;
5962 char *baseservicehome;
5963 char *newservicehome;
5965 char *hostkeys_file;
5968 struct GNUNET_DISK_FileHandle *fd;
5969 struct GNUNET_CONFIGURATION_Handle *pcfg;
5971 struct OutstandingSSH *ssh_entry;
5972 unsigned int hostcnt;
5979 uint64_t total_hostkeys;
5980 struct GNUNET_OS_Process *proc;
5991 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
5993 pg->notify_connection = connect_callback;
5994 pg->notify_connection_cls = connect_callback_cls;
5996 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
5997 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
5998 pg->max_outstanding_connections = max_concurrent_connections;
5999 pg->max_concurrent_ssh = max_concurrent_ssh;
6000 if (NULL != hostnames)
6003 hostpos = hostnames;
6004 while (hostpos != NULL)
6006 hostpos = hostpos->next;
6009 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
6012 hostpos = hostnames;
6013 while (hostpos != NULL)
6015 pg->hosts[off].minport = LOW_PORT;
6016 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
6017 if (hostpos->username != NULL)
6018 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
6019 pg->hosts[off].sshport = hostpos->port;
6020 hostpos = hostpos->next;
6030 pg->num_hosts = off;
6038 /* Create the servicehome directory for each remote peer */
6039 GNUNET_assert (GNUNET_OK ==
6040 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
6043 for (i = 0; i < pg->num_hosts; i++)
6045 ssh_entry = GNUNET_malloc (sizeof (struct OutstandingSSH));
6046 ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
6047 GNUNET_CONTAINER_DLL_insert (pg->ssh_head, pg->ssh_tail, ssh_entry);
6048 GNUNET_asprintf (&tmpdir, "%s/%s", baseservicehome, pg->hosts[i].hostname);
6049 if (NULL != pg->hosts[i].username)
6050 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username,
6051 pg->hosts[i].hostname);
6053 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
6054 if (pg->hosts[i].sshport != 0)
6056 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
6058 GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-P", ssh_port_str,
6062 arg, "mkdir -p", tmpdir, NULL);
6066 GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg, "mkdir -p",
6068 GNUNET_assert (proc != NULL);
6069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6070 "Creating remote dir with command ssh %s %s %s\n", arg,
6071 " mkdir -p ", tmpdir);
6072 GNUNET_free (tmpdir);
6074 GNUNET_OS_process_wait (proc);
6075 GNUNET_OS_process_close (proc);
6077 GNUNET_free (baseservicehome);
6078 baseservicehome = NULL;
6081 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "HOSTKEYSFILE",
6084 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
6085 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6086 _("Could not read hostkeys file!\n"));
6089 /* Check hostkey file size, read entire thing into memory */
6090 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
6091 GNUNET_DISK_PERM_NONE);
6094 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open",
6096 GNUNET_free (hostkeys_file);
6097 for (i = 0; i < pg->num_hosts; i++)
6099 GNUNET_free (pg->hosts[i].hostname);
6100 GNUNET_free_non_null (pg->hosts[i].username);
6102 GNUNET_free (pg->peers);
6103 GNUNET_free (pg->hosts);
6108 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
6111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6112 "Found file size %llu for hostkeys, expect hostkeys to be size %d\n",
6113 fs, HOSTKEYFILESIZE);
6115 if (0 != (fs % HOSTKEYFILESIZE))
6117 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6118 "File size %llu seems incorrect for hostkeys...\n", fs);
6122 total_hostkeys = fs / HOSTKEYFILESIZE;
6123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6124 "Will read %llu hostkeys from file\n", total_hostkeys);
6125 pg->hostkey_data = GNUNET_malloc_large (fs);
6126 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
6128 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
6130 GNUNET_free (hostkeys_file);
6133 for (off = 0; off < total; off++)
6137 hostname = pg->hosts[off % hostcnt].hostname;
6138 username = pg->hosts[off % hostcnt].username;
6139 sshport = pg->hosts[off % hostcnt].sshport;
6141 make_config (cfg, off, &pg->hosts[off % hostcnt].minport, &upnum,
6149 pcfg = make_config (cfg, off, &minport, &upnum, hostname, &fdnum);
6154 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6156 ("Could not create configuration for peer number %u on `%s'!\n"),
6157 off, hostname == NULL ? "localhost" : hostname);
6162 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
6165 if (hostname != NULL)
6166 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", baseservicehome,
6169 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
6170 GNUNET_free (baseservicehome);
6171 baseservicehome = NULL;
6175 tmpdir = getenv ("TMPDIR");
6176 tmpdir = tmpdir ? tmpdir : "/tmp";
6177 if (hostname != NULL)
6178 GNUNET_asprintf (&newservicehome, "%s/%s/%s/%d/", tmpdir, hostname,
6179 "gnunet-testing-test-test", off);
6181 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", tmpdir,
6182 "gnunet-testing-test-test", off);
6184 GNUNET_CONFIGURATION_set_value_string (pcfg, "PATHS", "SERVICEHOME",
6186 GNUNET_free (newservicehome);
6187 pg->peers[off].cfg = pcfg;
6188 pg->peers[off].pg = pg;
6189 pg->peers[off].internal_context.peer = &pg->peers[off];
6190 pg->peers[off].internal_context.timeout = timeout;
6191 pg->peers[off].internal_context.hostname = hostname;
6192 pg->peers[off].internal_context.username = username;
6193 pg->peers[off].internal_context.sshport = sshport;
6194 if (pg->hostkey_data != NULL)
6195 pg->peers[off].internal_context.hostkey =
6196 &pg->hostkey_data[off * HOSTKEYFILESIZE];
6197 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
6198 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
6199 pg->peers[off].internal_context.start_cb = cb;
6200 pg->peers[off].internal_context.start_cb_cls = cb_cls;
6201 #if !USE_START_HELPER
6202 GNUNET_SCHEDULER_add_now (&internal_start,
6203 &pg->peers[off].internal_context);
6205 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6207 pg->peers[off].daemon =
6208 GNUNET_TESTING_daemon_start (pcfg, timeout, GNUNET_YES, hostname,
6210 pg->peers[off].internal_context.hostkey,
6211 &internal_hostkey_callback,
6212 &pg->peers[off].internal_context,
6213 &internal_startup_callback,
6214 &pg->peers[off].internal_context);
6216 * At this point, given that we had a hostkeyfile,
6217 * we can call the hostkey callback!
6218 * But first, we should copy (rsync) all of the configs
6219 * and hostkeys to the remote peers. Then let topology
6220 * creation happen, then call the peer start helper processes,
6221 * then set pg->whatever_phase for each peer and let them
6222 * enter the fsm to get the HELLO's for peers and start connecting.
6227 GNUNET_SCHEDULER_add_now (&internal_start,
6228 &pg->peers[off].internal_context);
6234 #if USE_START_HELPER /* Now the peergroup has been set up, hostkeys and configs written to files. */
6235 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6237 for (off = 0; off < hostcnt; off++)
6242 hostname = pg->hosts[off % hostcnt].hostname;
6243 username = pg->hosts[off % hostcnt].username;
6244 sshport = pg->hosts[off % hostcnt].sshport;
6254 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
6257 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "baseservice home is %s\n",
6259 if (hostname != NULL)
6260 GNUNET_asprintf (&newservicehome, "%s/%s/", baseservicehome,
6263 GNUNET_asprintf (&newservicehome, "%s/", baseservicehome);
6264 GNUNET_free (baseservicehome);
6265 baseservicehome = NULL;
6269 tmpdir = getenv ("TMPDIR");
6270 tmpdir = tmpdir ? tmpdir : "/tmp";
6271 if (hostname != NULL)
6272 GNUNET_asprintf (&newservicehome, "%s/%s/%s/", tmpdir, hostname,
6273 "gnunet-testing-test-test");
6275 GNUNET_asprintf (&newservicehome, "%s/%s/", tmpdir,
6276 "gnunet-testing-test-test", off);
6279 if (NULL != username)
6280 GNUNET_asprintf (&arg, "%s@%s:%s", username, pg->hosts[off].hostname,
6283 GNUNET_asprintf (&arg, "%s:%s", pg->hosts[off].hostname,
6286 /* FIXME: Doesn't support ssh_port option! */
6288 GNUNET_OS_start_process (NULL, NULL, "rsync", "rsync", "-r",
6289 newservicehome, arg, NULL);
6291 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6292 "copying directory with command rsync -r %s %s\n",
6293 newservicehome, arg);
6294 GNUNET_free (newservicehome);
6298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6300 ("Could not start `%s' process to copy configuration directory.\n"),
6304 GNUNET_OS_process_wait (proc);
6305 GNUNET_OS_process_close (proc);
6307 /* Now all the configuration files and hostkeys are copied to the remote host. Call the hostkey callback for each peer! */
6308 GNUNET_SCHEDULER_add_now (&call_hostkey_callbacks, pg);
6315 * Get a daemon by number, so callers don't have to do nasty
6316 * offsetting operation.
6318 struct GNUNET_TESTING_Daemon *
6319 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
6320 unsigned int position)
6322 if (position < pg->total)
6323 return pg->peers[position].daemon;
6328 * Get a daemon by peer identity, so callers can
6329 * retrieve the daemon without knowing it's offset.
6331 * @param pg the peer group to retrieve the daemon from
6332 * @param peer_id the peer identity of the daemon to retrieve
6334 * @return the daemon on success, or NULL if no such peer identity is found
6336 struct GNUNET_TESTING_Daemon *
6337 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
6338 const struct GNUNET_PeerIdentity *peer_id)
6342 for (i = 0; i < pg->total; i++)
6345 memcmp (&pg->peers[i].daemon->id, peer_id,
6346 sizeof (struct GNUNET_PeerIdentity)))
6347 return pg->peers[i].daemon;
6353 * Prototype of a function that will be called when a
6354 * particular operation was completed the testing library.
6356 * @param cls closure (a struct RestartContext)
6357 * @param id id of the peer that was restarted
6358 * @param cfg handle to the configuration of the peer
6359 * @param d handle to the daemon that was restarted
6360 * @param emsg NULL on success
6363 restart_callback (void *cls, const struct GNUNET_PeerIdentity *id,
6364 const struct GNUNET_CONFIGURATION_Handle *cfg,
6365 struct GNUNET_TESTING_Daemon *d, const char *emsg)
6367 struct RestartContext *restart_context = cls;
6371 restart_context->peers_restarted++;
6375 restart_context->peers_restart_failed++;
6378 if (restart_context->peers_restarted == restart_context->peer_group->total)
6380 restart_context->callback (restart_context->callback_cls, NULL);
6381 GNUNET_free (restart_context);
6383 else if (restart_context->peers_restart_failed +
6384 restart_context->peers_restarted ==
6385 restart_context->peer_group->total)
6387 restart_context->callback (restart_context->callback_cls,
6388 "Failed to restart peers!");
6389 GNUNET_free (restart_context);
6395 * Callback for informing us about a successful
6396 * or unsuccessful churn stop call.
6398 * @param cls a ChurnContext
6399 * @param emsg NULL on success, non-NULL on failure
6403 churn_stop_callback (void *cls, const char *emsg)
6405 struct ShutdownContext *shutdown_ctx = cls;
6406 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
6407 unsigned int total_left;
6408 char *error_message;
6410 error_message = NULL;
6411 shutdown_ctx->outstanding--;
6415 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6416 "Churn stop callback failed with error `%s'\n", emsg);
6417 churn_ctx->num_failed_stop++;
6421 churn_ctx->num_to_stop--;
6425 (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) +
6426 (churn_ctx->num_to_start - churn_ctx->num_failed_start);
6428 if (total_left == 0)
6430 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
6432 GNUNET_asprintf (&error_message,
6433 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
6434 churn_ctx->num_failed_start, churn_ctx->num_failed_stop);
6436 churn_ctx->cb (churn_ctx->cb_cls, error_message);
6437 GNUNET_free_non_null (error_message);
6438 GNUNET_free (churn_ctx);
6439 GNUNET_free (shutdown_ctx);
6444 * Count the number of running peers.
6446 * @param pg handle for the peer group
6448 * @return the number of currently running peers in the peer group
6451 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
6454 unsigned int running = 0;
6456 for (i = 0; i < pg->total; i++)
6458 if (pg->peers[i].daemon->running == GNUNET_YES)
6460 GNUNET_assert (running != -1);
6468 * Task to rate limit the number of outstanding peer shutdown
6469 * requests. This is necessary for making sure we don't do
6470 * too many ssh connections at once, but is generally nicer
6471 * to any system as well (graduated task starts, as opposed
6472 * to calling gnunet-arm N times all at once).
6475 schedule_churn_shutdown_task (void *cls,
6476 const struct GNUNET_SCHEDULER_TaskContext *tc)
6478 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6479 struct ShutdownContext *shutdown_ctx;
6480 struct ChurnContext *churn_ctx;
6482 GNUNET_assert (peer_shutdown_ctx != NULL);
6483 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6484 GNUNET_assert (shutdown_ctx != NULL);
6485 churn_ctx = (struct ChurnContext *) shutdown_ctx->cb_cls;
6486 if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
6487 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
6488 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
6489 &schedule_churn_shutdown_task,
6493 shutdown_ctx->outstanding++;
6494 if (churn_ctx->service != NULL)
6495 GNUNET_TESTING_daemon_stop_service (peer_shutdown_ctx->daemon,
6497 shutdown_ctx->timeout,
6498 shutdown_ctx->cb, shutdown_ctx);
6500 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6501 shutdown_ctx->timeout, shutdown_ctx->cb,
6502 shutdown_ctx, GNUNET_NO, GNUNET_YES);
6503 GNUNET_free (peer_shutdown_ctx);
6509 * Simulate churn by stopping some peers (and possibly
6510 * re-starting others if churn is called multiple times). This
6511 * function can only be used to create leave-join churn (peers "never"
6512 * leave for good). First "voff" random peers that are currently
6513 * online will be taken offline; then "von" random peers that are then
6514 * offline will be put back online. No notifications will be
6515 * generated for any of these operations except for the callback upon
6518 * @param pg handle for the peer group
6519 * @param service the service to churn off/on, NULL to churn peer
6520 * @param voff number of peers that should go offline
6521 * @param von number of peers that should come back online;
6522 * must be zero on first call (since "testbed_start"
6523 * always starts all of the peers)
6524 * @param timeout how long to wait for operations to finish before
6526 * @param cb function to call at the end
6527 * @param cb_cls closure for cb
6530 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
6531 char *service, unsigned int voff,
6533 struct GNUNET_TIME_Relative timeout,
6534 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6536 struct ChurnContext *churn_ctx;
6537 struct ShutdownContext *shutdown_ctx;
6538 struct PeerShutdownContext *peer_shutdown_ctx;
6539 struct PeerRestartContext *peer_restart_ctx;
6540 struct ChurnRestartContext *churn_startup_ctx;
6542 unsigned int running;
6543 unsigned int stopped;
6544 unsigned int total_running;
6545 unsigned int total_stopped;
6547 unsigned int *running_arr;
6548 unsigned int *stopped_arr;
6549 unsigned int *running_permute;
6550 unsigned int *stopped_permute;
6553 shutdown_ctx = NULL;
6554 peer_shutdown_ctx = NULL;
6555 peer_restart_ctx = NULL;
6556 churn_startup_ctx = NULL;
6561 if ((von == 0) && (voff == 0)) /* No peers at all? */
6567 for (i = 0; i < pg->total; i++)
6569 if (service == NULL)
6571 if (pg->peers[i].daemon->running == GNUNET_YES)
6573 GNUNET_assert (running != -1);
6578 GNUNET_assert (stopped != -1);
6584 /* FIXME: make churned services a list! */
6585 pos = pg->peers[i].daemon->churned_services;
6586 /* FIXME: while (pos != NULL) */
6590 if (0 == strcasecmp (pos, service))
6596 GNUNET_assert (stopped != -1);
6598 /* FIXME: pos = pos->next; */
6602 GNUNET_assert (running != -1);
6610 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6611 "Trying to stop more peers (%d) than are currently running (%d)!\n",
6613 cb (cb_cls, "Trying to stop more peers than are currently running!");
6619 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6620 "Trying to start more peers (%d) than are currently stopped (%d)!\n",
6622 cb (cb_cls, "Trying to start more peers than are currently stopped!");
6626 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6628 if (service != NULL)
6629 churn_ctx->service = GNUNET_strdup (service);
6632 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
6636 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
6638 running_permute = NULL;
6639 stopped_permute = NULL;
6643 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, running);
6646 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, stopped);
6648 total_running = running;
6649 total_stopped = stopped;
6653 churn_ctx->num_to_start = von;
6654 churn_ctx->num_to_stop = voff;
6656 churn_ctx->cb_cls = cb_cls;
6659 for (i = 0; i < pg->total; i++)
6661 if (service == NULL)
6663 if (pg->peers[i].daemon->running == GNUNET_YES)
6665 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6666 running_arr[running] = i;
6671 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6672 stopped_arr[stopped] = i;
6678 /* FIXME: make churned services a list! */
6679 pos = pg->peers[i].daemon->churned_services;
6680 /* FIXME: while (pos != NULL) */
6683 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6684 stopped_arr[stopped] = i;
6686 /* FIXME: pos = pos->next; */
6690 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6691 running_arr[running] = i;
6697 GNUNET_assert (running >= voff);
6700 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6701 shutdown_ctx->cb = &churn_stop_callback;
6702 shutdown_ctx->cb_cls = churn_ctx;
6703 shutdown_ctx->total_peers = voff;
6704 shutdown_ctx->timeout = timeout;
6707 for (i = 0; i < voff; i++)
6710 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
6711 running_arr[running_permute[i]]);
6713 GNUNET_assert (running_arr != NULL);
6714 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6715 peer_shutdown_ctx->daemon =
6716 pg->peers[running_arr[running_permute[i]]].daemon;
6717 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6718 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task, peer_shutdown_ctx);
6721 GNUNET_assert (stopped >= von);
6724 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6725 churn_startup_ctx->churn_ctx = churn_ctx;
6726 churn_startup_ctx->timeout = timeout;
6727 churn_startup_ctx->pg = pg;
6729 for (i = 0; i < von; i++)
6732 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
6733 stopped_arr[stopped_permute[i]]);
6735 GNUNET_assert (stopped_arr != NULL);
6736 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
6737 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
6738 peer_restart_ctx->daemon =
6739 pg->peers[stopped_arr[stopped_permute[i]]].daemon;
6740 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
6743 GNUNET_free_non_null (running_arr);
6744 GNUNET_free_non_null (stopped_arr);
6745 GNUNET_free_non_null (running_permute);
6746 GNUNET_free_non_null (stopped_permute);
6750 * Start a given service for each of the peers in the peer group.
6752 * @param pg handle for the peer group
6753 * @param service the service to start
6754 * @param timeout how long to wait for operations to finish before
6756 * @param cb function to call once finished
6757 * @param cb_cls closure for cb
6761 GNUNET_TESTING_daemons_start_service (struct GNUNET_TESTING_PeerGroup *pg,
6763 struct GNUNET_TIME_Relative timeout,
6764 GNUNET_TESTING_NotifyCompletion cb,
6767 struct ServiceStartContext *start_ctx;
6768 struct PeerServiceStartContext *peer_start_ctx;
6771 GNUNET_assert (service != NULL);
6773 start_ctx = GNUNET_malloc (sizeof (struct ServiceStartContext));
6775 start_ctx->remaining = pg->total;
6777 start_ctx->cb_cls = cb_cls;
6778 start_ctx->service = GNUNET_strdup (service);
6779 start_ctx->timeout = timeout;
6781 for (i = 0; i < pg->total; i++)
6784 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6785 "Starting up service %s on peer %d!\n", service,
6786 stopped_arr[stopped_permute[i]]);
6788 peer_start_ctx = GNUNET_malloc (sizeof (struct PeerServiceStartContext));
6789 peer_start_ctx->start_ctx = start_ctx;
6790 peer_start_ctx->daemon = pg->peers[i].daemon;
6791 GNUNET_SCHEDULER_add_now (&schedule_service_start, peer_start_ctx);
6796 * Restart all peers in the given group.
6798 * @param pg the handle to the peer group
6799 * @param callback function to call on completion (or failure)
6800 * @param callback_cls closure for the callback function
6803 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
6804 GNUNET_TESTING_NotifyCompletion callback,
6807 struct RestartContext *restart_context;
6812 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
6813 restart_context->peer_group = pg;
6814 restart_context->peers_restarted = 0;
6815 restart_context->callback = callback;
6816 restart_context->callback_cls = callback_cls;
6818 for (off = 0; off < pg->total; off++)
6820 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback,
6828 * Start or stop an individual peer from the given group.
6830 * @param pg handle to the peer group
6831 * @param offset which peer to start or stop
6832 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
6833 * @param timeout how long to wait for shutdown
6834 * @param cb function to call at the end
6835 * @param cb_cls closure for cb
6838 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
6839 unsigned int offset, int desired_status,
6840 struct GNUNET_TIME_Relative timeout,
6841 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6843 struct ShutdownContext *shutdown_ctx;
6844 struct ChurnRestartContext *startup_ctx;
6845 struct ChurnContext *churn_ctx;
6847 if (GNUNET_NO == desired_status)
6849 if (NULL != pg->peers[offset].daemon)
6851 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6852 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6853 churn_ctx->num_to_start = 0;
6854 churn_ctx->num_to_stop = 1;
6856 churn_ctx->cb_cls = cb_cls;
6857 shutdown_ctx->cb_cls = churn_ctx;
6858 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon, timeout,
6859 &churn_stop_callback, shutdown_ctx, GNUNET_NO,
6863 else if (GNUNET_YES == desired_status)
6865 if (NULL == pg->peers[offset].daemon)
6867 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6868 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6869 churn_ctx->num_to_start = 1;
6870 churn_ctx->num_to_stop = 0;
6872 churn_ctx->cb_cls = cb_cls;
6873 startup_ctx->churn_ctx = churn_ctx;
6874 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon, timeout,
6875 &churn_start_callback, startup_ctx);
6884 * Callback for shutting down peers in a peer group.
6886 * @param cls closure (struct ShutdownContext)
6887 * @param emsg NULL on success
6890 internal_shutdown_callback (void *cls, const char *emsg)
6892 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6893 struct ShutdownContext *shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6895 struct OutstandingSSH *ssh_pos;
6897 shutdown_ctx->outstanding--;
6898 if (peer_shutdown_ctx->daemon->hostname != NULL)
6899 decrement_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6904 shutdown_ctx->peers_down++;
6909 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "internal_shutdown_callback",
6910 "Failed to stop a peer: %s\n", emsg);
6912 shutdown_ctx->peers_failed++;
6915 if ((shutdown_ctx->cb != NULL) &&
6916 (shutdown_ctx->peers_down + shutdown_ctx->peers_failed ==
6917 shutdown_ctx->total_peers))
6919 if (shutdown_ctx->peers_failed > 0)
6920 shutdown_ctx->cb (shutdown_ctx->cb_cls,
6921 "Not all peers successfully shut down!");
6923 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
6925 GNUNET_free (shutdown_ctx->pg->peers);
6926 GNUNET_free_non_null (shutdown_ctx->pg->hostkey_data);
6927 for (off = 0; off < shutdown_ctx->pg->num_hosts; off++)
6929 GNUNET_free (shutdown_ctx->pg->hosts[off].hostname);
6930 GNUNET_free_non_null (shutdown_ctx->pg->hosts[off].username);
6932 GNUNET_free_non_null (shutdown_ctx->pg->hosts);
6933 while (NULL != (ssh_pos = shutdown_ctx->pg->ssh_head))
6935 GNUNET_CONTAINER_DLL_remove (shutdown_ctx->pg->ssh_head,
6936 shutdown_ctx->pg->ssh_tail, ssh_pos);
6937 GNUNET_free (ssh_pos);
6939 GNUNET_free (shutdown_ctx->pg);
6940 GNUNET_free (shutdown_ctx);
6942 GNUNET_free (peer_shutdown_ctx);
6947 * Task to rate limit the number of outstanding peer shutdown
6948 * requests. This is necessary for making sure we don't do
6949 * too many ssh connections at once, but is generally nicer
6950 * to any system as well (graduated task starts, as opposed
6951 * to calling gnunet-arm N times all at once).
6954 schedule_shutdown_task (void *cls,
6955 const struct GNUNET_SCHEDULER_TaskContext *tc)
6957 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6958 struct ShutdownContext *shutdown_ctx;
6960 GNUNET_assert (peer_shutdown_ctx != NULL);
6961 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6962 GNUNET_assert (shutdown_ctx != NULL);
6964 if ((shutdown_ctx->outstanding < shutdown_ctx->pg->max_concurrent_ssh) ||
6965 ((peer_shutdown_ctx->daemon->hostname != NULL) &&
6966 (count_outstanding_at_host
6967 (peer_shutdown_ctx->daemon->hostname,
6968 shutdown_ctx->pg) < shutdown_ctx->pg->max_concurrent_ssh)))
6970 if (peer_shutdown_ctx->daemon->hostname != NULL)
6971 increment_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6973 shutdown_ctx->outstanding++;
6974 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6975 shutdown_ctx->timeout,
6976 &internal_shutdown_callback, peer_shutdown_ctx,
6977 shutdown_ctx->delete_files, GNUNET_NO);
6980 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
6981 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
6982 &schedule_shutdown_task, peer_shutdown_ctx);
6987 * Read a testing hosts file based on a configuration.
6988 * Returns a DLL of hosts (caller must free!) on success
6989 * or NULL on failure.
6991 * @param cfg a configuration with a testing section
6993 * @return DLL of hosts on success, NULL on failure
6995 struct GNUNET_TESTING_Host *
6996 GNUNET_TESTING_hosts_load (const struct GNUNET_CONFIGURATION_Handle *cfg)
6998 struct GNUNET_TESTING_Host *hosts;
6999 struct GNUNET_TESTING_Host *temphost;
7007 /* Check for a hostfile containing user@host:port triples */
7009 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "hostfile",
7016 if (hostfile != NULL)
7018 if (GNUNET_OK != GNUNET_DISK_file_test (hostfile))
7019 GNUNET_DISK_fn_write (hostfile, NULL, 0,
7020 GNUNET_DISK_PERM_USER_READ |
7021 GNUNET_DISK_PERM_USER_WRITE);
7022 if ((0 != STAT (hostfile, &frstat)) || (frstat.st_size == 0))
7024 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7025 "Could not open file specified for host list, ending test!");
7026 GNUNET_free (hostfile);
7030 data = GNUNET_malloc_large (frstat.st_size);
7031 GNUNET_assert (data != NULL);
7032 if (frstat.st_size != GNUNET_DISK_fn_read (hostfile, data, frstat.st_size))
7034 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7035 "Could not read file %s specified for host list, ending test!",
7037 GNUNET_free (hostfile);
7042 GNUNET_free_non_null (hostfile);
7046 while (count < frstat.st_size - 1)
7049 if (((data[count] == '\n')) && (buf != &data[count]))
7052 temphost = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Host));
7054 sscanf (buf, "%a[a-zA-Z0-9_]@%a[a-zA-Z0-9.]:%hd",
7055 &temphost->username, &temphost->hostname, &temphost->port);
7058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7059 "Successfully read host %s, port %d and user %s from file\n",
7060 temphost->hostname, temphost->port, temphost->username);
7064 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7065 "Error reading line `%s' in hostfile\n", buf);
7066 GNUNET_free (temphost);
7067 buf = &data[count + 1];
7070 temphost->next = hosts;
7072 buf = &data[count + 1];
7074 else if ((data[count] == '\n') || (data[count] == '\0'))
7075 buf = &data[count + 1];
7078 GNUNET_free_non_null (data);
7084 * Shutdown all peers started in the given group.
7086 * @param pg handle to the peer group
7087 * @param timeout how long to wait for shutdown
7088 * @param cb callback to notify upon success or failure
7089 * @param cb_cls closure for cb
7092 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
7093 struct GNUNET_TIME_Relative timeout,
7094 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
7097 struct ShutdownContext *shutdown_ctx;
7098 struct PeerShutdownContext *peer_shutdown_ctx;
7101 struct PeerConnection *conn_iter;
7102 struct PeerConnection *temp_conn;
7104 struct ConnectContext *cc;
7106 GNUNET_assert (pg->total > 0);
7107 while (NULL != (cc = pg->cc_head))
7109 GNUNET_CONTAINER_DLL_remove (pg->cc_head, pg->cc_tail, cc);
7110 if (GNUNET_SCHEDULER_NO_TASK != cc->task)
7111 GNUNET_SCHEDULER_cancel (cc->task);
7113 GNUNET_TESTING_daemons_connect_cancel (cc->cc);
7117 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
7118 shutdown_ctx->delete_files =
7119 GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "DELETE_FILES");
7120 shutdown_ctx->cb = cb;
7121 shutdown_ctx->cb_cls = cb_cls;
7122 shutdown_ctx->total_peers = pg->total;
7123 shutdown_ctx->timeout = timeout;
7124 shutdown_ctx->pg = pg;
7126 for (off = 0; off < pg->total; off++)
7128 GNUNET_assert (NULL != pg->peers[off].daemon);
7129 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
7130 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
7131 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
7132 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
7134 if (NULL != pg->peers[off].cfg)
7136 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
7137 pg->peers[off].cfg = NULL;
7140 conn_iter = pg->peers[off].allowed_peers_head;
7141 while (conn_iter != NULL)
7143 temp_conn = conn_iter->next;
7144 GNUNET_free (conn_iter);
7145 conn_iter = temp_conn;
7148 conn_iter = pg->peers[off].connect_peers_head;
7149 while (conn_iter != NULL)
7151 temp_conn = conn_iter->next;
7152 GNUNET_free (conn_iter);
7153 conn_iter = temp_conn;
7156 conn_iter = pg->peers[off].blacklisted_peers_head;
7157 while (conn_iter != NULL)
7159 temp_conn = conn_iter->next;
7160 GNUNET_free (conn_iter);
7161 conn_iter = temp_conn;
7164 conn_iter = pg->peers[off].connect_peers_working_set_head;
7165 while (conn_iter != NULL)
7167 temp_conn = conn_iter->next;
7168 GNUNET_free (conn_iter);
7169 conn_iter = temp_conn;
7172 if (pg->peers[off].allowed_peers != NULL)
7173 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
7174 if (pg->peers[off].connect_peers != NULL)
7175 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
7176 if (pg->peers[off].blacklisted_peers != NULL)
7177 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].blacklisted_peers);
7182 /* end of testing_group.c */