2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing_group.c
23 * @brief convenience API for writing testcases for GNUnet
24 * @author Nathan Evans
25 * @author Christian Grothoff
29 #include "gnunet_constants.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_testing_lib.h"
32 #include "gnunet_core_service.h"
34 #define VERBOSE_TESTING GNUNET_NO
36 #define VERBOSE_TOPOLOGY GNUNET_NO
38 #define DEBUG_CHURN GNUNET_NO
40 #define USE_START_HELPER GNUNET_YES
44 /* Before connecting peers, send all of the HELLOs */
45 #define USE_SEND_HELLOS GNUNET_NO
47 #define TOPOLOGY_HACK GNUNET_YES
51 * Lowest port used for GNUnet testing. Should be high enough to not
52 * conflict with other applications running on the hosts but be low
53 * enough to not conflict with client-ports (typically starting around
56 #define LOW_PORT 12000
59 * Highest port used for GNUnet testing. Should be low enough to not
60 * conflict with the port range for "local" ports (client apps; see
61 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
63 #define HIGH_PORT 56000
65 /* Maximum time to delay connect attempt */
66 #define MAX_CONNECT_DELAY 300
69 * Which list of peers do we need to modify?
73 /** Modify allowed peers */
76 /** Modify connect peers */
79 /** Modify blacklist peers */
82 /** Modify workingset peers */
87 * Prototype of a function called whenever two peers would be connected
88 * in a certain topology.
91 (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup * pg,
92 unsigned int first, unsigned int second,
93 enum PeerLists list, unsigned int check);
96 * Context for handling churning a peer group
101 * The peergroup we are dealing with.
103 struct GNUNET_TESTING_PeerGroup *pg;
106 * Name of the service to churn on/off, NULL
107 * to churn entire peer.
112 * Callback used to notify of churning finished
114 GNUNET_TESTING_NotifyCompletion cb;
117 * Closure for callback
122 * Number of peers that still need to be started
124 unsigned int num_to_start;
127 * Number of peers that still need to be stopped
129 unsigned int num_to_stop;
132 * Number of peers that failed to start
134 unsigned int num_failed_start;
137 * Number of peers that failed to stop
139 unsigned int num_failed_stop;
142 struct RestartContext
145 * The group of peers being restarted
147 struct GNUNET_TESTING_PeerGroup *peer_group;
150 * How many peers have been restarted thus far
152 unsigned int peers_restarted;
155 * How many peers got an error when restarting
157 unsigned int peers_restart_failed;
160 * The function to call once all peers have been restarted
162 GNUNET_TESTING_NotifyCompletion callback;
165 * Closure for callback function
171 struct SendHelloContext
174 * Global handle to the peer group.
176 struct GNUNET_TESTING_PeerGroup *pg;
179 * The data about this specific peer.
181 struct PeerData *peer;
184 * The next HELLO that needs sent to this peer.
186 struct PeerConnection *peer_pos;
189 * Are we connected to CORE yet?
191 unsigned int core_ready;
194 * How many attempts should we make for failed connections?
196 unsigned int connect_attempts;
199 * Task for scheduling core connect requests to be sent.
201 GNUNET_SCHEDULER_TaskIdentifier core_connect_task;
204 struct ShutdownContext
206 struct GNUNET_TESTING_PeerGroup *pg;
208 * Total peers to wait for
210 unsigned int total_peers;
213 * Number of peers successfully shut down
215 unsigned int peers_down;
218 * Number of peers failed to shut down
220 unsigned int peers_failed;
223 * Number of peers we have started shutting
224 * down. If too many, wait on them.
226 unsigned int outstanding;
229 * Timeout for shutdown.
231 struct GNUNET_TIME_Relative timeout;
234 * Callback to call when all peers either
235 * shutdown or failed to shutdown
237 GNUNET_TESTING_NotifyCompletion cb;
245 * Should we delete all of the files from the peers?
251 * Individual shutdown context for a particular peer.
253 struct PeerShutdownContext
256 * Pointer to the high level shutdown context.
258 struct ShutdownContext *shutdown_ctx;
261 * The daemon handle for the peer to shut down.
263 struct GNUNET_TESTING_Daemon *daemon;
267 * Individual shutdown context for a particular peer.
269 struct PeerRestartContext
272 * Pointer to the high level restart context.
274 struct ChurnRestartContext *churn_restart_ctx;
277 * The daemon handle for the peer to shut down.
279 struct GNUNET_TESTING_Daemon *daemon;
282 struct CreateTopologyContext
286 * Function to call with number of connections
288 GNUNET_TESTING_NotifyConnections cont;
291 * Closure for connection notification
298 /** Waiting to read number of peers */
301 /** Should find next peer index */
304 /** Should find colon */
307 /** Should read other peer index, space, or endline */
312 struct PeerConnection
317 struct PeerConnection *prev;
322 struct PeerConnection *next;
325 * Index of daemon in pg->peers
332 struct InternalStartContext
335 * Pointer to peerdata
337 struct PeerData *peer;
340 * Timeout for peer startup
342 struct GNUNET_TIME_Relative timeout;
345 * Client callback for hostkey notification
347 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
350 * Closure for hostkey_callback
355 * Client callback for peer start notification
357 GNUNET_TESTING_NotifyDaemonRunning start_cb;
365 * Hostname, where to start the peer
367 const char *hostname;
370 * Username to use when connecting to the
373 const char *username;
376 * Pointer to starting memory location of a hostkey
381 * Port to use for ssh.
387 struct ChurnRestartContext
390 * PeerGroup that we are working with.
392 struct GNUNET_TESTING_PeerGroup *pg;
395 * Number of restarts currently in flight.
397 unsigned int outstanding;
400 * Handle to the underlying churn context.
402 struct ChurnContext *churn_ctx;
405 * How long to allow the operation to take.
407 struct GNUNET_TIME_Relative timeout;
410 struct OutstandingSSH
412 struct OutstandingSSH *next;
414 struct OutstandingSSH *prev;
417 * Number of current ssh connections.
419 uint32_t outstanding;
422 * The hostname of this peer.
424 const char *hostname;
428 * Data we keep per peer.
433 * (Initial) configuration of the host.
434 * (initial because clients could change
435 * it and we would not know about those
438 struct GNUNET_CONFIGURATION_Handle *cfg;
441 * Handle for controlling the daemon.
443 struct GNUNET_TESTING_Daemon *daemon;
446 * The peergroup this peer belongs to.
448 struct GNUNET_TESTING_PeerGroup *pg;
452 * Linked list of allowed peer connections.
454 struct PeerConnection *allowed_peers_head;
457 * Linked list of allowed peer connections.
459 struct PeerConnection *allowed_peers_tail;
462 * Linked list of blacklisted peer connections.
464 struct PeerConnection *blacklisted_peers_head;
467 * Linked list of blacklisted peer connections.
469 struct PeerConnection *blacklisted_peers_tail;
472 * Linked list of connect peer connections.
474 struct PeerConnection *connect_peers_head;
477 * Linked list of connect peer connections.
479 struct PeerConnection *connect_peers_tail;
482 * Linked list of connect peer connections.
484 struct PeerConnection *connect_peers_working_set_head;
487 * Linked list of connect peer connections.
489 struct PeerConnection *connect_peers_working_set_tail;
493 * Hash map of allowed peer connections (F2F created topology)
495 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
498 * Hash map of blacklisted peers
500 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
503 * Hash map of peer connections
505 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
508 * Temporary hash map of peer connections
510 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
514 * Temporary variable for topology creation, should be reset before
515 * creating any topology so the count is valid once finished.
520 * Context to keep track of peers being started, to
521 * stagger hostkey generation and peer startup.
523 struct InternalStartContext internal_context;
527 * Linked list of per-host data.
537 * SSH username to use when connecting to this host.
542 * SSH port to use when connecting to this host.
547 * Lowest port that we have not yet used
553 struct TopologyIterateContext
556 * The peergroup we are working with.
558 struct GNUNET_TESTING_PeerGroup *pg;
561 * Callback for notifying of two connected peers.
563 GNUNET_TESTING_NotifyTopology topology_cb;
566 * Closure for topology_cb
571 * Number of peers currently connected to.
573 unsigned int connected;
576 * Number of peers we have finished iterating.
578 unsigned int completed;
581 * Number of peers total.
586 struct StatsIterateContext
589 * The peergroup that we are dealing with.
591 struct GNUNET_TESTING_PeerGroup *pg;
594 * Continuation to call once all stats information has been retrieved.
596 GNUNET_STATISTICS_Callback cont;
599 * Proc function to call on each value received.
601 GNUNET_TESTING_STATISTICS_Iterator proc;
604 * Closure for topology_cb
609 * Number of peers currently connected to.
611 unsigned int connected;
614 * Number of peers we have finished iterating.
616 unsigned int completed;
619 * Number of peers total.
627 struct GNUNET_TESTING_Daemon *daemon;
630 struct StatsCoreContext
633 struct GNUNET_TESTING_Daemon *daemon;
635 * Handle to the statistics service.
637 struct GNUNET_STATISTICS_Handle *stats_handle;
640 * Handle for getting statistics.
642 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
645 struct ConnectTopologyContext
648 * How many connections are left to create.
650 unsigned int remaining_connections;
653 * Handle to group of peers.
655 struct GNUNET_TESTING_PeerGroup *pg;
658 * How long to try this connection before timing out.
660 struct GNUNET_TIME_Relative connect_timeout;
663 * How many times to retry connecting the two peers.
665 unsigned int connect_attempts;
668 * Temp value set for each iteration.
670 //struct PeerData *first;
673 * Notification that all peers are connected.
675 GNUNET_TESTING_NotifyCompletion notify_connections_done;
678 * Closure for notify.
684 * Handle to a group of GNUnet peers.
686 struct GNUNET_TESTING_PeerGroup
689 * Configuration template.
691 const struct GNUNET_CONFIGURATION_Handle *cfg;
694 * Function to call on each started daemon.
696 //GNUNET_TESTING_NotifyDaemonRunning cb;
704 * Function to call on each topology connection created
706 GNUNET_TESTING_NotifyConnection notify_connection;
709 * Callback for notify_connection
711 void *notify_connection_cls;
714 * Array of information about hosts.
716 struct HostData *hosts;
719 * Number of hosts (size of HostData)
721 unsigned int num_hosts;
724 * Array of "total" peers.
726 struct PeerData *peers;
729 * Number of peers in this group.
734 * At what time should we fail the peer startup process?
736 struct GNUNET_TIME_Absolute max_timeout;
739 * How many peers are being started right now?
741 unsigned int starting;
744 * How many peers have already been started?
746 unsigned int started;
749 * Number of possible connections to peers
752 unsigned int max_outstanding_connections;
755 * Number of ssh connections to peers (max).
757 unsigned int max_concurrent_ssh;
760 * Number of connects we are waiting on, allows us to rate limit
763 unsigned int outstanding_connects;
766 * Number of HELLOs we have yet to send.
768 unsigned int remaining_hellos;
771 * How many connects have already been scheduled?
773 unsigned int total_connects_scheduled;
776 * Hostkeys loaded from a file.
781 * Head of DLL to keep track of the number of outstanding
782 * ssh connections per peer.
784 struct OutstandingSSH *ssh_head;
787 * Tail of DLL to keep track of the number of outstanding
788 * ssh connections per peer.
790 struct OutstandingSSH *ssh_tail;
793 * Stop scheduling peers connecting.
795 unsigned int stop_connects;
798 * Connection context for peer group.
800 struct ConnectTopologyContext ct_ctx;
806 * The altered configuration.
808 struct GNUNET_CONFIGURATION_Handle *ret;
811 * The original configuration to alter.
813 const struct GNUNET_CONFIGURATION_Handle *orig;
816 * The hostname that this peer will run on.
818 const char *hostname;
821 * The next possible port to assign.
826 * Unique number for unix domain sockets.
831 * Unique number for this peer/host to offset
832 * things that are grouped by host.
837 struct ConnectContext
840 * Index of peer to connect second to.
842 uint32_t first_index;
845 * Index of peer to connect first to.
847 uint32_t second_index;
850 * Higher level topology connection context.
852 struct ConnectTopologyContext *ct_ctx;
855 * Whether this connection has been accounted for in the schedule_connect call.
860 struct UnblacklistContext
865 struct GNUNET_TESTING_PeerGroup *pg;
868 * uid of the first peer
878 struct GNUNET_TESTING_PeerGroup *pg;
881 * uid of the first peer
886 * Peer data for first peer.
888 struct PeerData *first;
891 * Random percentage to use
896 struct MinimumContext
901 struct GNUNET_TESTING_PeerGroup *pg;
904 * uid of the first peer
909 * Peer data for first peer.
911 struct PeerData *first;
914 * Number of conns per peer
916 unsigned int num_to_add;
919 * Permuted array of all possible connections. Only add the Nth
920 * peer if it's in the Nth position.
922 unsigned int *pg_array;
925 * What number is the current element we are iterating over?
927 unsigned int current;
935 struct GNUNET_TESTING_PeerGroup *pg;
938 * uid of the first peer
943 * uid of the second peer
948 * Peer data for first peer.
950 struct PeerData *first;
953 * Which peer has been chosen as the one to add?
958 * What number is the current element we are iterating over?
960 unsigned int current;
964 * Simple struct to keep track of progress, and print a
965 * nice little percentage meter for long running tasks.
975 unsigned int completed;
979 char *startup_string;
984 * Convert unique ID to hash code.
986 * @param uid unique ID to convert
987 * @param hash set to uid (extended with zeros)
990 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
992 memset (hash, 0, sizeof (GNUNET_HashCode));
993 *((uint32_t *) hash) = uid;
997 * Convert hash code to unique ID.
999 * @param uid unique ID to convert
1000 * @param hash set to uid (extended with zeros)
1003 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
1005 memcpy (uid, hash, sizeof (uint32_t));
1010 static struct GNUNET_CORE_MessageHandler no_handlers[] =
1016 * Create a meter to keep track of the progress of some task.
1018 * @param total the total number of items to complete
1019 * @param start_string a string to prefix the meter with (if printing)
1020 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
1023 * @return the progress meter
1025 static struct ProgressMeter *
1026 create_meter(unsigned int total, char * start_string, int print)
1028 struct ProgressMeter *ret;
1029 ret = GNUNET_malloc(sizeof(struct ProgressMeter));
1032 ret->modnum = total / 4;
1033 if (ret->modnum == 0) /* Divide by zero check */
1035 ret->dotnum = (total / 50) + 1;
1036 if (start_string != NULL)
1037 ret->startup_string = GNUNET_strdup(start_string);
1039 ret->startup_string = GNUNET_strdup("");
1045 * Update progress meter (increment by one).
1047 * @param meter the meter to update and print info for
1049 * @return GNUNET_YES if called the total requested,
1050 * GNUNET_NO if more items expected
1053 update_meter(struct ProgressMeter *meter)
1055 if (meter->print == GNUNET_YES)
1057 if (meter->completed % meter->modnum == 0)
1059 if (meter->completed == 0)
1061 fprintf (stdout, "%sProgress: [0%%", meter->startup_string);
1064 fprintf (stdout, "%d%%", (int) (((float) meter->completed
1065 / meter->total) * 100));
1067 else if (meter->completed % meter->dotnum == 0)
1068 fprintf (stdout, ".");
1070 if (meter->completed + 1 == meter->total)
1071 fprintf (stdout, "%d%%]\n", 100);
1076 if (meter->completed == meter->total)
1078 if (meter->completed > meter->total)
1079 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
1084 * Reset progress meter.
1086 * @param meter the meter to reset
1088 * @return GNUNET_YES if meter reset,
1089 * GNUNET_SYSERR on error
1092 reset_meter(struct ProgressMeter *meter)
1095 return GNUNET_SYSERR;
1097 meter->completed = 0;
1102 * Release resources for meter
1104 * @param meter the meter to free
1107 free_meter(struct ProgressMeter *meter)
1109 GNUNET_free_non_null (meter->startup_string);
1110 GNUNET_free (meter);
1114 * Get a topology from a string input.
1116 * @param topology where to write the retrieved topology
1117 * @param topology_string The string to attempt to
1118 * get a configuration value from
1119 * @return GNUNET_YES if topology string matched a
1120 * known topology, GNUNET_NO if not
1123 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology,
1124 const char *topology_string)
1127 * Strings representing topologies in enum
1129 static const char *topology_strings[] =
1132 * A clique (everyone connected to everyone else).
1137 * Small-world network (2d torus plus random links).
1142 * Small-world network (ring plus random links).
1162 * Certain percentage of peers are unable to communicate directly
1163 * replicating NAT conditions
1168 * Scale free topology.
1173 * Straight line topology.
1178 * All peers are disconnected.
1183 * Read the topology from a file.
1190 if (topology_string == NULL)
1192 while (topology_strings[curr] != NULL)
1194 if (strcasecmp (topology_strings[curr], topology_string) == 0)
1201 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
1206 * Get connect topology option from string input.
1208 * @param topology_option where to write the retrieved topology
1209 * @param topology_string The string to attempt to
1210 * get a configuration value from
1211 * @return GNUNET_YES if string matched a known
1212 * topology option, GNUNET_NO if not
1215 GNUNET_TESTING_topology_option_get(
1216 enum GNUNET_TESTING_TopologyOption *topology_option,
1217 const char *topology_string)
1220 * Options for connecting a topology as strings.
1222 static const char *topology_option_strings[] =
1225 * Try to connect all peers specified in the topology.
1230 * Choose a random subset of connections to create.
1232 "CONNECT_RANDOM_SUBSET",
1235 * Create at least X connections for each peer.
1240 * Using a depth first search, create one connection
1241 * per peer. If any are missed (graph disconnected)
1242 * start over at those peers until all have at least one
1248 * Find the N closest peers to each allowed peer in the
1249 * topology and make sure a connection to those peers
1250 * exists in the connect topology.
1255 * No options specified.
1262 if (topology_string == NULL)
1264 while (NULL != topology_option_strings[curr])
1266 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1268 *topology_option = curr;
1273 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1278 * Function to iterate over options. Copies
1279 * the options to the target configuration,
1280 * updating PORT values as needed.
1282 * @param cls closure
1283 * @param section name of the section
1284 * @param option name of the option
1285 * @param value value of the option
1288 update_config(void *cls, const char *section, const char *option,
1291 struct UpdateContext *ctx = cls;
1295 char *single_variable;
1296 char *per_host_variable;
1297 unsigned long long num_per_host;
1299 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1300 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1302 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1304 if ((ival != 0) && (GNUNET_YES
1305 != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1308 GNUNET_snprintf (cval, sizeof(cval), "%u", ctx->nport++);
1311 else if ((ival != 0) && (GNUNET_YES
1312 == GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1314 && GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1318 GNUNET_snprintf (cval, sizeof(cval), "%u", ival + ctx->fdnum
1323 /* FIXME: REMOVE FOREVER HACK HACK HACK */
1324 if (0 == strcasecmp (section, "transport-tcp"))
1325 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, "ADVERTISED_PORT", value);
1328 if (0 == strcmp (option, "UNIXPATH"))
1330 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig,
1334 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1335 section, ctx->upnum++);
1338 else if ((GNUNET_YES
1339 == GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1342 && (num_per_host > 0))
1345 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1346 section, ctx->fdnum % num_per_host);
1351 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1353 value = ctx->hostname;
1355 GNUNET_free (single_variable);
1356 GNUNET_free (per_host_variable);
1357 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1361 * Create a new configuration using the given configuration
1362 * as a template; however, each PORT in the existing cfg
1363 * must be renumbered by incrementing "*port". If we run
1364 * out of "*port" numbers, return NULL.
1366 * @param cfg template configuration
1367 * @param off the current peer offset
1368 * @param port port numbers to use, update to reflect
1369 * port numbers that were used
1370 * @param upnum number to make unix domain socket names unique
1371 * @param hostname hostname of the controlling host, to allow control connections from
1372 * @param fdnum number used to offset the unix domain socket for grouped processes
1373 * (such as statistics or peerinfo, which can be shared among others)
1375 * @return new configuration, NULL on error
1377 static struct GNUNET_CONFIGURATION_Handle *
1378 make_config(const struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t off,
1379 uint16_t * port, uint32_t * upnum, const char *hostname,
1382 struct UpdateContext uc;
1385 char *allowed_hosts;
1386 unsigned long long skew_variance;
1387 unsigned long long skew_offset;
1388 long long actual_offset;
1394 uc.ret = GNUNET_CONFIGURATION_create ();
1395 uc.hostname = hostname;
1398 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1399 if (uc.nport >= HIGH_PORT)
1402 GNUNET_CONFIGURATION_destroy (uc.ret);
1406 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
1409 && (skew_variance > 0))
1411 skew_offset = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, skew_variance + 1);
1412 actual_offset = skew_offset - GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, skew_variance + 1);
1413 /* Min is -skew_variance, Max is skew_variance */
1414 skew_offset = skew_variance + actual_offset; /* Normal distribution around 0 */
1415 GNUNET_CONFIGURATION_set_value_number(uc.ret, "testing", "skew_offset", skew_offset);
1418 if (GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "control_host",
1419 &control_host) == GNUNET_OK)
1421 if (hostname != NULL)
1422 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1425 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1427 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1430 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nse", "ACCEPT_FROM",
1433 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1434 "ACCEPT_FROM", allowed_hosts);
1435 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1437 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1438 "ACCEPT_FROM", allowed_hosts);
1440 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH", "");
1441 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH",
1443 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH", "");
1444 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1447 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "USE_LOCALADDR",
1449 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "USE_LOCALADDR",
1451 GNUNET_free_non_null (control_host);
1452 GNUNET_free (allowed_hosts);
1455 /* arm needs to know to allow connections from the host on which it is running,
1456 * otherwise gnunet-arm is unable to connect to it in some instances */
1457 if (hostname != NULL)
1459 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1460 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "BINDTO",
1462 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "INTERNAL_ADDRESS",
1464 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "EXTERNAL_ADDRESS",
1466 GNUNET_CONFIGURATION_set_value_string (uc.ret, "disablev6", "BINDTO",
1468 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "USE_LOCALADDR",
1470 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "USE_LOCALADDR",
1472 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1474 GNUNET_free (allowed_hosts);
1478 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "USE_LOCALADDR",
1480 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "USE_LOCALADDR",
1482 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "BINDTO",
1484 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "INTERNAL_ADDRESS",
1486 GNUNET_CONFIGURATION_set_value_string (uc.ret, "nat", "EXTERNAL_ADDRESS",
1488 GNUNET_CONFIGURATION_set_value_string (uc.ret, "disablev6", "BINDTO",
1492 *port = (uint16_t) uc.nport;
1500 * Remove entries from the peer connection list
1502 * @param pg the peer group we are working with
1503 * @param first index of the first peer
1504 * @param second index of the second peer
1505 * @param list the peer list to use
1506 * @param check UNUSED
1508 * @return the number of connections added (can be 0, 1 or 2)
1512 remove_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1513 unsigned int second, enum PeerLists list, unsigned int check)
1517 struct PeerConnection **first_list;
1518 struct PeerConnection **second_list;
1519 struct PeerConnection *first_iter;
1520 struct PeerConnection *second_iter;
1521 struct PeerConnection **first_tail;
1522 struct PeerConnection **second_tail;
1525 GNUNET_HashCode hash_first;
1526 GNUNET_HashCode hash_second;
1528 hash_from_uid (first, &hash_first);
1529 hash_from_uid (second, &hash_second);
1537 first_list = &pg->peers[first].allowed_peers_head;
1538 second_list = &pg->peers[second].allowed_peers_head;
1539 first_tail = &pg->peers[first].allowed_peers_tail;
1540 second_tail = &pg->peers[second].allowed_peers_tail;
1543 first_list = &pg->peers[first].connect_peers_head;
1544 second_list = &pg->peers[second].connect_peers_head;
1545 first_tail = &pg->peers[first].connect_peers_tail;
1546 second_tail = &pg->peers[second].connect_peers_tail;
1549 first_list = &pg->peers[first].blacklisted_peers_head;
1550 second_list = &pg->peers[second].blacklisted_peers_head;
1551 first_tail = &pg->peers[first].blacklisted_peers_tail;
1552 second_tail = &pg->peers[second].blacklisted_peers_tail;
1555 first_list = &pg->peers[first].connect_peers_working_set_head;
1556 second_list = &pg->peers[second].connect_peers_working_set_head;
1557 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1558 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1565 first_iter = *first_list;
1566 while (first_iter != NULL)
1568 if (first_iter->index == second)
1570 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1571 GNUNET_free(first_iter);
1575 first_iter = first_iter->next;
1578 second_iter = *second_list;
1579 while (second_iter != NULL)
1581 if (second_iter->index == first)
1583 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1584 GNUNET_free(second_iter);
1588 second_iter = second_iter->next;
1592 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1595 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1600 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1603 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1612 * Add entries to the some list
1614 * @param pg the peer group we are working with
1615 * @param first index of the first peer
1616 * @param second index of the second peer
1617 * @param list the list type that we should modify
1618 * @param check GNUNET_YES to check lists before adding
1619 * GNUNET_NO to force add
1621 * @return the number of connections added (can be 0, 1 or 2)
1625 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1626 unsigned int second, enum PeerLists list, unsigned int check)
1632 struct PeerConnection **first_list;
1633 struct PeerConnection **second_list;
1634 struct PeerConnection *first_iter;
1635 struct PeerConnection *second_iter;
1636 struct PeerConnection *new_first;
1637 struct PeerConnection *new_second;
1638 struct PeerConnection **first_tail;
1639 struct PeerConnection **second_tail;
1644 first_list = &pg->peers[first].allowed_peers_head;
1645 second_list = &pg->peers[second].allowed_peers_head;
1646 first_tail = &pg->peers[first].allowed_peers_tail;
1647 second_tail = &pg->peers[second].allowed_peers_tail;
1650 first_list = &pg->peers[first].connect_peers_head;
1651 second_list = &pg->peers[second].connect_peers_head;
1652 first_tail = &pg->peers[first].connect_peers_tail;
1653 second_tail = &pg->peers[second].connect_peers_tail;
1656 first_list = &pg->peers[first].blacklisted_peers_head;
1657 second_list = &pg->peers[second].blacklisted_peers_head;
1658 first_tail = &pg->peers[first].blacklisted_peers_tail;
1659 second_tail = &pg->peers[second].blacklisted_peers_tail;
1662 first_list = &pg->peers[first].connect_peers_working_set_head;
1663 second_list = &pg->peers[second].connect_peers_working_set_head;
1664 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1665 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1672 add_first = GNUNET_YES;
1673 add_second = GNUNET_YES;
1675 if (check == GNUNET_YES)
1677 first_iter = *first_list;
1678 while (first_iter != NULL)
1680 if (first_iter->index == second)
1682 add_first = GNUNET_NO;
1685 first_iter = first_iter->next;
1688 second_iter = *second_list;
1689 while (second_iter != NULL)
1691 if (second_iter->index == first)
1693 add_second = GNUNET_NO;
1696 second_iter = second_iter->next;
1703 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1704 new_first->index = second;
1705 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1706 pg->peers[first].num_connections++;
1712 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1713 new_second->index = first;
1714 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1715 pg->peers[second].num_connections++;
1723 * Scale free network construction as described in:
1725 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1727 * Start with a network of "one" peer, then progressively add
1728 * peers up to the total number. At each step, iterate over
1729 * all possible peers and connect new peer based on number of
1730 * existing connections of the target peer.
1732 * @param pg the peer group we are dealing with
1733 * @param proc the connection processor to use
1734 * @param list the peer list to use
1736 * @return the number of connections created
1739 create_scale_free(struct GNUNET_TESTING_PeerGroup *pg,
1740 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1743 unsigned int total_connections;
1744 unsigned int outer_count;
1746 unsigned int previous_total_connections;
1750 GNUNET_assert (pg->total > 1);
1752 /* Add a connection between the first two nodes */
1753 total_connections = proc (pg, 0, 1, list, GNUNET_YES);
1755 for (outer_count = 1; outer_count < pg->total; outer_count++)
1757 previous_total_connections = total_connections;
1758 for (i = 0; i < outer_count; i++)
1760 probability = pg->peers[i].num_connections
1761 / (double) previous_total_connections;
1763 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1765 / ((double) UINT64_MAX);
1767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1768 "Considering connecting peer %d to peer %d\n",
1771 if (random < probability)
1774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1775 "Connecting peer %d to peer %d\n", outer_count, i);
1777 total_connections += proc (pg, outer_count, i, list, GNUNET_YES);
1782 return total_connections;
1786 * Create a topology given a peer group (set of running peers)
1787 * and a connection processor. Creates a small world topology
1788 * according to the rewired ring construction. The basic
1789 * behavior is that a ring topology is created, but with some
1790 * probability instead of connecting a peer to the next
1791 * neighbor in the ring a connection will be created to a peer
1792 * selected uniformly at random. We use the TESTING
1793 * PERCENTAGE option to specify what number of
1794 * connections each peer should have. Default is 2,
1795 * which makes the ring, any given number is multiplied by
1796 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1797 * each peer have on average 2logn connections. The additional
1798 * connections are made at increasing distance around the ring
1799 * from the original peer, or to random peers based on the re-
1800 * wiring probability. The TESTING
1801 * PROBABILITY option is used as the probability that a given
1802 * connection is rewired.
1804 * @param pg the peergroup to create the topology on
1805 * @param proc the connection processor to call to actually set
1806 * up connections between two peers
1807 * @param list the peer list to use
1809 * @return the number of connections that were set up
1813 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg,
1814 GNUNET_TESTING_ConnectionProcessor proc,
1815 enum PeerLists list)
1819 unsigned int natLog;
1820 unsigned int randomPeer;
1821 double random, logNModifier, probability;
1822 unsigned int smallWorldConnections;
1827 unsigned int useAnd;
1828 int connect_attempts;
1830 logNModifier = 0.5; /* FIXME: default value? */
1831 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1835 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1837 GNUNET_ERROR_TYPE_WARNING,
1839 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1840 p_string, "LOGNMODIFIER", "TESTING");
1841 GNUNET_free (p_string);
1843 probability = 0.5; /* FIXME: default percentage? */
1844 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1848 if (sscanf (p_string, "%lf", &probability) != 1)
1850 GNUNET_ERROR_TYPE_WARNING,
1852 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1853 p_string, "PERCENTAGE", "TESTING");
1854 GNUNET_free (p_string);
1856 natLog = log (pg->total);
1857 connsPerPeer = ceil (natLog * logNModifier);
1859 if (connsPerPeer % 2 == 1)
1862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."),
1865 smallWorldConnections = 0;
1866 connect_attempts = 0;
1867 for (i = 0; i < pg->total; i++)
1870 max = i + connsPerPeer / 2;
1871 min = i - connsPerPeer / 2;
1873 if (max > pg->total - 1)
1875 max = max - pg->total;
1881 min = pg->total - 1 + min;
1885 for (j = 0; j < connsPerPeer / 2; j++)
1888 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1890 / ((double) UINT64_MAX));
1891 if (random < probability)
1893 /* Connect to uniformly selected random peer */
1895 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1897 while ((((randomPeer < max) && (randomPeer > min)) && (useAnd
1898 == 0)) || (((randomPeer > min) || (randomPeer < max))
1902 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1905 smallWorldConnections += proc (pg, i, randomPeer, list,
1910 nodeToConnect = i + j + 1;
1911 if (nodeToConnect > pg->total - 1)
1913 nodeToConnect = nodeToConnect - pg->total;
1915 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
1921 connect_attempts += smallWorldConnections;
1923 return connect_attempts;
1927 * Create a topology given a peer group (set of running peers)
1928 * and a connection processor.
1930 * @param pg the peergroup to create the topology on
1931 * @param proc the connection processor to call to actually set
1932 * up connections between two peers
1933 * @param list the peer list to use
1935 * @return the number of connections that were set up
1939 create_nated_internet(struct GNUNET_TESTING_PeerGroup *pg,
1940 GNUNET_TESTING_ConnectionProcessor proc,
1941 enum PeerLists list)
1943 unsigned int outer_count, inner_count;
1944 unsigned int cutoff;
1945 int connect_attempts;
1946 double nat_percentage;
1949 nat_percentage = 0.6; /* FIXME: default percentage? */
1950 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1954 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1956 GNUNET_ERROR_TYPE_WARNING,
1958 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1959 p_string, "PERCENTAGE", "TESTING");
1960 GNUNET_free (p_string);
1963 cutoff = (unsigned int) (nat_percentage * pg->total);
1964 connect_attempts = 0;
1965 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1967 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1969 if ((outer_count > cutoff) || (inner_count > cutoff))
1972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1973 "Connecting peer %d to peer %d\n",
1974 outer_count, inner_count);
1976 connect_attempts += proc (pg, outer_count, inner_count, list,
1981 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_copy(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 struct ProgressMeter *conn_meter;
2010 nat_percentage = 0.6; /* FIXME: default percentage? */
2011 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2015 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
2017 GNUNET_ERROR_TYPE_WARNING,
2019 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2020 p_string, "PERCENTAGE", "TESTING");
2021 GNUNET_free (p_string);
2024 cutoff = (unsigned int) (nat_percentage * pg->total);
2026 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2028 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2030 if ((outer_count > cutoff) || (inner_count > cutoff))
2036 conn_meter = create_meter (count, "NAT COPY", GNUNET_YES);
2037 connect_attempts = 0;
2038 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2040 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2042 if ((outer_count > cutoff) || (inner_count > cutoff))
2045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2046 "Connecting peer %d to peer %d\n",
2047 outer_count, inner_count);
2049 connect_attempts += proc (pg, outer_count, inner_count, list,
2051 add_connections (pg, outer_count, inner_count, ALLOWED, GNUNET_NO);
2052 update_meter (conn_meter);
2056 free_meter (conn_meter);
2058 return connect_attempts;
2063 * Create a topology given a peer group (set of running peers)
2064 * and a connection processor.
2066 * @param pg the peergroup to create the topology on
2067 * @param proc the connection processor to call to actually set
2068 * up connections between two peers
2069 * @param list the peer list to use
2071 * @return the number of connections that were set up
2075 create_small_world(struct GNUNET_TESTING_PeerGroup *pg,
2076 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2078 unsigned int i, j, k;
2079 unsigned int square;
2082 unsigned int toggle = 1;
2083 unsigned int nodeToConnect;
2084 unsigned int natLog;
2085 unsigned int node1Row;
2086 unsigned int node1Col;
2087 unsigned int node2Row;
2088 unsigned int node2Col;
2089 unsigned int distance;
2090 double probability, random, percentage;
2091 unsigned int smallWorldConnections;
2092 unsigned int small_world_it;
2094 int connect_attempts;
2095 square = floor (sqrt (pg->total));
2099 percentage = 0.5; /* FIXME: default percentage? */
2100 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2104 if (sscanf (p_string, "%lf", &percentage) != 1)
2106 GNUNET_ERROR_TYPE_WARNING,
2108 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2109 p_string, "PERCENTAGE", "TESTING");
2110 GNUNET_free (p_string);
2112 if (percentage < 0.0)
2115 GNUNET_ERROR_TYPE_WARNING,
2117 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
2118 "PERCENTAGE", "TESTING", percentage);
2121 probability = 0.5; /* FIXME: default percentage? */
2122 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2126 if (sscanf (p_string, "%lf", &probability) != 1)
2128 GNUNET_ERROR_TYPE_WARNING,
2130 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2131 p_string, "PROBABILITY", "TESTING");
2132 GNUNET_free (p_string);
2134 if (square * square != pg->total)
2136 while (rows * cols < pg->total)
2138 if (toggle % 2 == 0)
2147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2149 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2153 connect_attempts = 0;
2154 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2155 * to the node to its right and above. Once this is over, we'll have our torus!
2156 * Special case for the last node (if the rows and columns are not equal), connect
2157 * to the first in the row to maintain topology.
2159 for (i = 0; i < pg->total; i++)
2161 /* First connect to the node to the right */
2162 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2163 nodeToConnect = i + 1;
2164 else if (i + 1 == pg->total)
2165 nodeToConnect = rows * cols - cols;
2167 nodeToConnect = i - cols + 1;
2169 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2173 nodeToConnect = (rows * cols) - cols + i;
2174 if (nodeToConnect >= pg->total)
2175 nodeToConnect -= cols;
2178 nodeToConnect = i - cols;
2180 if (nodeToConnect < pg->total)
2181 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2183 natLog = log (pg->total);
2184 #if VERBOSE_TESTING > 2
2185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2186 _("natural log of %d is %d, will run %d iterations\n"),
2187 pg->total, natLog, (int) (natLog * percentage));
2188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2189 _("Total connections added thus far: %u!\n"), connect_attempts);
2191 smallWorldConnections = 0;
2192 small_world_it = (unsigned int) (natLog * percentage);
2193 if (small_world_it < 1)
2195 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
2196 for (i = 0; i < small_world_it; i++)
2198 for (j = 0; j < pg->total; j++)
2200 /* Determine the row and column of node at position j on the 2d torus */
2201 node1Row = j / cols;
2202 node1Col = j - (node1Row * cols);
2203 for (k = 0; k < pg->total; k++)
2205 /* Determine the row and column of node at position k on the 2d torus */
2206 node2Row = k / cols;
2207 node2Col = k - (node2Row * cols);
2208 /* Simple Cartesian distance */
2209 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
2212 /* Calculate probability as 1 over the square of the distance */
2213 probability = 1.0 / (distance * distance);
2214 /* Choose a random value between 0 and 1 */
2216 = ((double) GNUNET_CRYPTO_random_u64 (
2217 GNUNET_CRYPTO_QUALITY_WEAK,
2219 / ((double) UINT64_MAX);
2220 /* If random < probability, then connect the two nodes */
2221 if (random < probability)
2222 smallWorldConnections += proc (pg, j, k, list, GNUNET_YES);
2228 connect_attempts += smallWorldConnections;
2229 #if VERBOSE_TESTING > 2
2230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2231 _("Total connections added for small world: %d!\n"),
2232 smallWorldConnections);
2234 return connect_attempts;
2238 * Create a topology given a peer group (set of running peers)
2239 * and a connection processor.
2241 * @param pg the peergroup to create the topology on
2242 * @param proc the connection processor to call to actually set
2243 * up connections between two peers
2244 * @param list the peer list to use
2246 * @return the number of connections that were set up
2250 create_erdos_renyi(struct GNUNET_TESTING_PeerGroup *pg,
2251 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2254 unsigned int outer_count;
2255 unsigned int inner_count;
2256 int connect_attempts;
2260 probability = 0.5; /* FIXME: default percentage? */
2261 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2265 if (sscanf (p_string, "%lf", &probability) != 1)
2267 GNUNET_ERROR_TYPE_WARNING,
2269 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2270 p_string, "PROBABILITY", "TESTING");
2271 GNUNET_free (p_string);
2273 connect_attempts = 0;
2274 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2276 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2279 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2281 / ((double) UINT64_MAX);
2283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2284 _("rand is %f probability is %f\n"), temp_rand,
2287 if (temp_rand < probability)
2289 connect_attempts += proc (pg, outer_count, inner_count, list,
2295 return connect_attempts;
2299 * Create a topology given a peer group (set of running peers)
2300 * and a connection processor. This particular function creates
2301 * the connections for a 2d-torus, plus additional "closest"
2302 * connections per peer.
2304 * @param pg the peergroup to create the topology on
2305 * @param proc the connection processor to call to actually set
2306 * up connections between two peers
2307 * @param list the peer list to use
2309 * @return the number of connections that were set up
2313 create_2d_torus(struct GNUNET_TESTING_PeerGroup *pg,
2314 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2317 unsigned int square;
2320 unsigned int toggle = 1;
2321 unsigned int nodeToConnect;
2322 int connect_attempts;
2324 connect_attempts = 0;
2326 square = floor (sqrt (pg->total));
2330 if (square * square != pg->total)
2332 while (rows * cols < pg->total)
2334 if (toggle % 2 == 0)
2343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2345 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2348 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2349 * to the node to its right and above. Once this is over, we'll have our torus!
2350 * Special case for the last node (if the rows and columns are not equal), connect
2351 * to the first in the row to maintain topology.
2353 for (i = 0; i < pg->total; i++)
2355 /* First connect to the node to the right */
2356 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2357 nodeToConnect = i + 1;
2358 else if (i + 1 == pg->total)
2359 nodeToConnect = rows * cols - cols;
2361 nodeToConnect = i - cols + 1;
2363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2364 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2366 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2368 /* Second connect to the node immediately above */
2371 nodeToConnect = (rows * cols) - cols + i;
2372 if (nodeToConnect >= pg->total)
2373 nodeToConnect -= cols;
2376 nodeToConnect = i - cols;
2378 if (nodeToConnect < pg->total)
2381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2382 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2384 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2389 return connect_attempts;
2393 * Create a topology given a peer group (set of running peers)
2394 * and a connection processor.
2396 * @param pg the peergroup to create the topology on
2397 * @param proc the connection processor to call to actually set
2398 * up connections between two peers
2399 * @param list the peer list to use
2400 * @param check does the connection processor need to check before
2401 * performing an action on the list?
2403 * @return the number of connections that were set up
2407 create_clique(struct GNUNET_TESTING_PeerGroup *pg,
2408 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list,
2411 unsigned int outer_count;
2412 unsigned int inner_count;
2413 int connect_attempts;
2414 struct ProgressMeter *conn_meter;
2415 connect_attempts = 0;
2417 conn_meter = create_meter ((((pg->total * pg->total) + pg->total) / 2)
2418 - pg->total, "Create Clique ", GNUNET_NO);
2419 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2421 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2425 "Connecting peer %d to peer %d\n",
2426 outer_count, inner_count);
2428 connect_attempts += proc (pg, outer_count, inner_count, list, check);
2429 update_meter (conn_meter);
2432 reset_meter (conn_meter);
2433 free_meter (conn_meter);
2434 return connect_attempts;
2439 * Iterator over hash map entries.
2441 * @param cls closure the peer group
2442 * @param key the key stored in the hashmap is the
2443 * index of the peer to connect to
2444 * @param value value in the hash map, handle to the peer daemon
2445 * @return GNUNET_YES if we should continue to
2450 unblacklist_iterator (void *cls,
2451 const GNUNET_HashCode * key,
2454 struct UnblacklistContext *un_ctx = cls;
2455 uint32_t second_pos;
2457 uid_from_hash (key, &second_pos);
2459 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2467 * Create a blacklist topology based on the allowed topology
2468 * which disallows any connections not in the allowed topology
2469 * at the transport level.
2471 * @param pg the peergroup to create the topology on
2472 * @param proc the connection processor to call to allow
2473 * up connections between two peers
2475 * @return the number of connections that were set up
2479 copy_allowed (struct GNUNET_TESTING_PeerGroup *pg,
2480 GNUNET_TESTING_ConnectionProcessor proc)
2484 struct PeerConnection *iter;
2486 struct UnblacklistContext un_ctx;
2491 for (count = 0; count < pg->total - 1; count++)
2494 iter = pg->peers[count].allowed_peers_head;
2495 while (iter != NULL)
2497 remove_connections (pg, count, iter->index, BLACKLIST, GNUNET_YES);
2498 //unblacklist_connections(pg, count, iter->index);
2502 un_ctx.first_uid = count;
2503 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers,
2504 &unblacklist_iterator,
2508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2509 "Unblacklisted %u peers\n",
2516 * Create a topology given a peer group (set of running peers)
2517 * and a connection processor.
2519 * @param pg the peergroup to create the topology on
2520 * @param proc the connection processor to call to actually set
2521 * up connections between two peers
2522 * @param list which list should be modified
2524 * @return the number of connections that were set up
2528 create_line(struct GNUNET_TESTING_PeerGroup *pg,
2529 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2532 unsigned int connect_attempts;
2534 connect_attempts = 0;
2535 /* Connect each peer to the next highest numbered peer */
2536 for (count = 0; count < pg->total - 1; count++)
2539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2540 "Connecting peer %d to peer %d\n",
2543 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2546 return connect_attempts;
2550 * Create a topology given a peer group (set of running peers)
2551 * and a connection processor.
2553 * @param pg the peergroup to create the topology on
2554 * @param filename the file to read topology information from
2555 * @param proc the connection processor to call to actually set
2556 * up connections between two peers
2557 * @param list the peer list to use
2559 * @return the number of connections that were set up
2563 create_from_file(struct GNUNET_TESTING_PeerGroup *pg, char *filename,
2564 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2566 int connect_attempts;
2567 unsigned int first_peer_index;
2568 unsigned int second_peer_index;
2573 unsigned int total_peers;
2574 enum States curr_state;
2576 connect_attempts = 0;
2577 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2578 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2580 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2582 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2583 "Could not open file `%s' specified for topology!", filename);
2584 return connect_attempts;
2587 data = GNUNET_malloc_large (frstat.st_size);
2588 GNUNET_assert(data != NULL);
2589 if (frstat.st_size != GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2592 GNUNET_ERROR_TYPE_ERROR,
2593 "Could not read file %s specified for host list, ending test!",
2596 return connect_attempts;
2601 first_peer_index = 0;
2602 /* First line should contain a single integer, specifying the number of peers */
2603 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2604 curr_state = NUM_PEERS;
2605 while (count < frstat.st_size - 1)
2607 if ((buf[count] == '\n') || (buf[count] == ' '))
2617 total_peers = strtoul(&buf[count], NULL, 10);
2620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2621 "Failed to read number of peers from topology file!\n");
2623 return connect_attempts;
2625 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2626 "Read %u total peers in topology\n", total_peers);
2627 GNUNET_assert(total_peers == pg->total);
2628 curr_state = PEER_INDEX;
2629 while ((buf[count] != '\n') && (count < frstat.st_size - 1))
2635 first_peer_index = strtoul(&buf[count], NULL, 10);
2638 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2639 "Failed to read peer index from topology file!\n");
2641 return connect_attempts;
2643 while ((buf[count] != ':') && (count < frstat.st_size - 1))
2646 curr_state = OTHER_PEER_INDEX;
2649 if (1 == sscanf (&buf[count], ":"))
2650 curr_state = OTHER_PEER_INDEX;
2653 case OTHER_PEER_INDEX:
2655 second_peer_index = strtoul(&buf[count], NULL, 10);
2658 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2659 "Failed to peer index from topology file!\n");
2661 return connect_attempts;
2663 /* Assume file is written with first peer 1, but array index is 0 */
2664 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index
2665 - 1, list, GNUNET_YES);
2666 while ((buf[count] != '\n') && (buf[count] != ',') && (count
2667 < frstat.st_size - 1))
2669 if (buf[count] == '\n')
2671 curr_state = PEER_INDEX;
2673 else if (buf[count] != ',')
2675 curr_state = OTHER_PEER_INDEX;
2680 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2681 "Found bad data in topology file while in state %d!\n",
2685 return connect_attempts;
2689 return connect_attempts;
2693 * Create a topology given a peer group (set of running peers)
2694 * and a connection processor.
2696 * @param pg the peergroup to create the topology on
2697 * @param proc the connection processor to call to actually set
2698 * up connections between two peers
2699 * @param list the peer list to use
2701 * @return the number of connections that were set up
2705 create_ring(struct GNUNET_TESTING_PeerGroup *pg,
2706 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2709 int connect_attempts;
2711 connect_attempts = 0;
2713 /* Connect each peer to the next highest numbered peer */
2714 for (count = 0; count < pg->total - 1; count++)
2717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2718 "Connecting peer %d to peer %d\n", count, count + 1);
2720 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2723 /* Connect the last peer to the first peer */
2724 connect_attempts += proc (pg, pg->total - 1, 0, list, GNUNET_YES);
2726 return connect_attempts;
2731 * Iterator for writing friends of a peer to a file.
2733 * @param cls closure, an open writable file handle
2734 * @param key the key the daemon was stored under
2735 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2737 * @return GNUNET_YES to continue iteration
2739 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2740 * with a single file_iterator that takes a closure which contains
2741 * the prefix to write before the peer. Then this could be used
2742 * for blacklisting multiple transports and writing the friend
2743 * file. I'm sure *someone* will complain loudly about other
2744 * things that negate these functions even existing so no point in
2748 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2750 FILE *temp_friend_handle = cls;
2751 struct GNUNET_TESTING_Daemon *peer = value;
2752 struct GNUNET_PeerIdentity *temppeer;
2753 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2755 temppeer = &peer->id;
2756 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2757 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2762 struct BlacklistContext
2765 * The (open) file handle to write to
2767 FILE *temp_file_handle;
2770 * The transport that this peer will be blacklisted on.
2776 * Iterator for writing blacklist data to appropriate files.
2778 * @param cls closure, an open writable file handle
2779 * @param key the key the daemon was stored under
2780 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2782 * @return GNUNET_YES to continue iteration
2785 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2787 struct BlacklistContext *blacklist_ctx = cls;
2788 struct GNUNET_TESTING_Daemon *peer = value;
2789 struct GNUNET_PeerIdentity *temppeer;
2790 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2792 temppeer = &peer->id;
2793 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2794 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2795 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2796 blacklist_ctx->transport, (char *) &peer_enc);
2803 * Create the friend files based on the PeerConnection's
2804 * of each peer in the peer group, and copy the files
2805 * to the appropriate place
2807 * @param pg the peer group we are dealing with
2810 create_and_copy_friend_files(struct GNUNET_TESTING_PeerGroup *pg)
2812 FILE *temp_friend_handle;
2813 unsigned int pg_iter;
2814 char *temp_service_path;
2815 struct GNUNET_OS_Process **procarr;
2819 enum GNUNET_OS_ProcessStatusType type;
2820 unsigned long return_code;
2828 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2829 struct PeerConnection *conn_iter;
2831 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2832 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2834 mytemp = GNUNET_DISK_mktemp ("friends");
2835 GNUNET_assert (mytemp != NULL);
2836 temp_friend_handle = fopen (mytemp, "wt");
2837 GNUNET_assert (temp_friend_handle != NULL);
2839 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2840 while (conn_iter != NULL)
2842 GNUNET_CRYPTO_hash_to_enc (
2843 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2845 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2846 conn_iter = conn_iter->next;
2849 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2850 &friend_file_iterator,
2851 temp_friend_handle);
2853 fclose (temp_friend_handle);
2856 != GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2858 "PATHS", "SERVICEHOME",
2859 &temp_service_path))
2861 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2862 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2863 "SERVICEHOME", "PATHS");
2864 if (UNLINK (mytemp) != 0)
2865 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
2868 GNUNET_free (mytemp);
2872 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2874 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2875 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
2877 GNUNET_assert(procarr[pg_iter] != NULL);
2879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2880 "Copying file with command cp %s %s\n",
2884 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2885 GNUNET_OS_process_close (procarr[pg_iter]);
2888 else /* Remote, scp the file to the correct place */
2890 if (NULL != pg->peers[pg_iter].daemon->username)
2891 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2892 pg->peers[pg_iter].daemon->username,
2893 pg->peers[pg_iter].daemon->hostname,
2896 GNUNET_asprintf (&arg, "%s:%s/friends",
2897 pg->peers[pg_iter].daemon->hostname,
2899 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
2901 GNUNET_assert(procarr[pg_iter] != NULL);
2902 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2903 GNUNET_OS_process_close (procarr[pg_iter]);
2904 if (ret != GNUNET_OK)
2906 /* FIXME: free contents of 'procarr' array */
2907 GNUNET_free (procarr);
2908 GNUNET_free (temp_service_path);
2909 GNUNET_free (mytemp);
2913 procarr[pg_iter] = NULL;
2915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2916 "Copying file with command scp %s %s\n",
2922 GNUNET_free (temp_service_path);
2923 GNUNET_free (mytemp);
2928 ret = GNUNET_SYSERR;
2929 while ((count < max_wait) && (ret != GNUNET_OK))
2932 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2936 "Checking copy status of file %d\n",
2939 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2941 if (GNUNET_OS_process_status
2942 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2944 ret = GNUNET_SYSERR;
2946 else if ((type != GNUNET_OS_PROCESS_EXITED)
2947 || (return_code != 0))
2949 ret = GNUNET_SYSERR;
2953 GNUNET_OS_process_close (procarr[pg_iter]);
2954 procarr[pg_iter] = NULL;
2956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2964 if (ret == GNUNET_SYSERR)
2966 /* FIXME: why sleep here? -CG */
2972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2973 _("Finished copying all friend files!\n"));
2976 GNUNET_free (procarr);
2981 * Create the blacklist files based on the PeerConnection's
2982 * of each peer in the peer group, and copy the files
2983 * to the appropriate place.
2985 * @param pg the peer group we are dealing with
2986 * @param transports space delimited list of transports to blacklist
2989 create_and_copy_blacklist_files(struct GNUNET_TESTING_PeerGroup *pg,
2990 const char *transports)
2992 FILE *temp_file_handle;
2993 unsigned int pg_iter;
2994 char *temp_service_path;
2995 struct GNUNET_OS_Process **procarr;
2998 enum GNUNET_OS_ProcessStatusType type;
2999 unsigned long return_code;
3006 char *temp_transports;
3008 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
3009 struct PeerConnection *conn_iter;
3011 static struct BlacklistContext blacklist_ctx;
3014 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
3015 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3017 mytemp = GNUNET_DISK_mktemp ("blacklist");
3018 GNUNET_assert (mytemp != NULL);
3019 temp_file_handle = fopen (mytemp, "wt");
3020 GNUNET_assert (temp_file_handle != NULL);
3021 temp_transports = GNUNET_strdup (transports);
3023 blacklist_ctx.temp_file_handle = temp_file_handle;
3025 transport_len = strlen (temp_transports) + 1;
3028 for (i = 0; i < transport_len; i++)
3030 if ((temp_transports[i] == ' ') && (pos == NULL))
3031 continue; /* At start of string (whitespace) */
3032 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
3034 temp_transports[i] = '\0';
3036 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
3037 while (conn_iter != NULL)
3039 GNUNET_CRYPTO_hash_to_enc (
3040 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
3042 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
3043 conn_iter = conn_iter->next;
3046 blacklist_ctx.transport = pos;
3047 (void) GNUNET_CONTAINER_multihashmap_iterate (pg->
3049 [pg_iter].blacklisted_peers,
3050 &blacklist_file_iterator,
3054 } /* At beginning of actual string */
3055 else if (pos == NULL)
3057 pos = &temp_transports[i];
3061 GNUNET_free (temp_transports);
3062 fclose (temp_file_handle);
3065 != GNUNET_CONFIGURATION_get_value_string (
3066 pg->peers[pg_iter]. daemon->cfg,
3067 "PATHS", "SERVICEHOME",
3068 &temp_service_path))
3071 GNUNET_ERROR_TYPE_WARNING,
3073 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
3074 "SERVICEHOME", "PATHS");
3075 if (UNLINK (mytemp) != 0)
3076 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
3078 GNUNET_free (mytemp);
3082 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
3084 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
3085 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
3088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3089 _("Copying file with command cp %s %s\n"), mytemp, arg);
3094 else /* Remote, scp the file to the correct place */
3096 if (NULL != pg->peers[pg_iter].daemon->username)
3097 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
3098 pg->peers[pg_iter].daemon->username,
3099 pg->peers[pg_iter].daemon->hostname,
3102 GNUNET_asprintf (&arg, "%s:%s/blacklist",
3103 pg->peers[pg_iter].daemon->hostname,
3105 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
3107 GNUNET_assert(procarr[pg_iter] != NULL);
3108 GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
3111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3112 _("Copying file with command scp %s %s\n"), mytemp,
3117 GNUNET_free (temp_service_path);
3118 GNUNET_free (mytemp);
3122 ret = GNUNET_SYSERR;
3123 while ((count < max_wait) && (ret != GNUNET_OK))
3126 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3130 _("Checking copy status of file %d\n"), pg_iter);
3132 if (procarr[pg_iter] != NULL) /* Check for already completed! */
3134 if (GNUNET_OS_process_status (procarr[pg_iter], &type,
3135 &return_code) != GNUNET_OK)
3137 ret = GNUNET_SYSERR;
3139 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
3141 ret = GNUNET_SYSERR;
3145 GNUNET_OS_process_close (procarr[pg_iter]);
3146 procarr[pg_iter] = NULL;
3148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3149 _("File %d copied\n"), pg_iter);
3155 if (ret == GNUNET_SYSERR)
3157 /* FIXME: why sleep here? -CG */
3163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3164 _("Finished copying all blacklist files!\n"));
3166 GNUNET_free (procarr);
3170 /* Forward Declaration */
3172 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3175 * Choose a random peer's next connection to create, and
3176 * call schedule_connect to set up the connect task.
3178 * @param ct_ctx the overall connection context
3181 preschedule_connect(struct GNUNET_TESTING_PeerGroup *pg)
3183 struct ConnectTopologyContext *ct_ctx = &pg->ct_ctx;
3184 struct PeerConnection *connection_iter;
3185 struct ConnectContext *connect_context;
3186 uint32_t random_peer;
3188 if (ct_ctx->remaining_connections == 0)
3191 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3192 while (pg->peers[random_peer].connect_peers_head == NULL)
3193 random_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3196 connection_iter = pg->peers[random_peer].connect_peers_head;
3197 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3198 connect_context->first_index = random_peer;
3199 connect_context->second_index = connection_iter->index;
3200 connect_context->ct_ctx = ct_ctx;
3201 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3202 GNUNET_CONTAINER_DLL_remove(pg->peers[random_peer].connect_peers_head, pg->peers[random_peer].connect_peers_tail, connection_iter);
3203 GNUNET_free(connection_iter);
3204 ct_ctx->remaining_connections--;
3208 /* Forward declaration */
3209 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3212 * Close connections and free the hello context.
3214 * @param cls the 'struct SendHelloContext *'
3215 * @param tc scheduler context
3218 free_hello_context (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3220 struct SendHelloContext *send_hello_context = cls;
3221 if (send_hello_context->peer->daemon->server != NULL)
3223 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3224 send_hello_context->peer->daemon->server = NULL;
3226 if (send_hello_context->peer->daemon->th != NULL)
3228 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3229 send_hello_context->peer->daemon->th = NULL;
3231 if (send_hello_context->core_connect_task != GNUNET_SCHEDULER_NO_TASK)
3233 GNUNET_SCHEDULER_cancel(send_hello_context->core_connect_task);
3234 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3236 send_hello_context->pg->outstanding_connects--;
3237 GNUNET_free(send_hello_context);
3241 * For peers that haven't yet connected, notify
3242 * the caller that they have failed (timeout).
3244 * @param cls the 'struct SendHelloContext *'
3245 * @param tc scheduler context
3248 notify_remaining_connections_failed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3250 struct SendHelloContext *send_hello_context = cls;
3251 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3252 struct PeerConnection *connection;
3254 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3255 send_hello_context->peer->daemon->server = NULL;
3257 connection = send_hello_context->peer->connect_peers_head;
3259 while (connection != NULL)
3261 if (pg->notify_connection != NULL)
3263 pg->notify_connection(pg->notify_connection_cls,
3264 &send_hello_context->peer->daemon->id,
3265 &pg->peers[connection->index].daemon->id,
3267 send_hello_context->peer->daemon->cfg,
3268 pg->peers[connection->index].daemon->cfg,
3269 send_hello_context->peer->daemon,
3270 pg->peers[connection->index].daemon,
3271 "Peers failed to connect (timeout)");
3273 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3274 GNUNET_free(connection);
3275 connection = connection->next;
3277 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3279 other_peer = &pg->peers[connection->index];
3284 * For peers that haven't yet connected, send
3285 * CORE connect requests.
3287 * @param cls the 'struct SendHelloContext *'
3288 * @param tc scheduler context
3291 send_core_connect_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3293 struct SendHelloContext *send_hello_context = cls;
3294 struct PeerConnection *conn;
3295 GNUNET_assert(send_hello_context->peer->daemon->server != NULL);
3297 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3299 send_hello_context->connect_attempts++;
3300 if (send_hello_context->connect_attempts < send_hello_context->pg->ct_ctx.connect_attempts)
3302 conn = send_hello_context->peer->connect_peers_head;
3303 while (conn != NULL)
3305 GNUNET_CORE_peer_request_connect(send_hello_context->peer->daemon->server,
3306 &send_hello_context->pg->peers[conn->index].daemon->id,
3311 send_hello_context->core_connect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_divide(send_hello_context->pg->ct_ctx.connect_timeout, send_hello_context->pg->ct_ctx.connect_attempts) ,
3312 &send_core_connect_requests,
3313 send_hello_context);
3317 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Timeout before all connections created, marking rest as failed!\n");
3318 GNUNET_SCHEDULER_add_now(¬ify_remaining_connections_failed, send_hello_context);
3324 * Success, connection is up. Signal client our success.
3326 * @param cls our "struct SendHelloContext"
3327 * @param peer identity of the peer that has connected
3328 * @param atsi performance information
3330 * FIXME: remove peers from BOTH lists, call notify twice, should
3331 * double the speed of connections as long as the list iteration
3332 * doesn't take too long!
3335 core_connect_notify (void *cls,
3336 const struct GNUNET_PeerIdentity *peer,
3337 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3339 struct SendHelloContext *send_hello_context = cls;
3340 struct PeerConnection *connection;
3341 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3343 struct PeerData *other_peer;
3346 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3347 "Connected peer %s to peer %s\n",
3348 ctx->d1->shortname, GNUNET_i2s(peer));
3351 if (0 == memcmp(&send_hello_context->peer->daemon->id, peer, sizeof(struct GNUNET_PeerIdentity)))
3354 connection = send_hello_context->peer->connect_peers_head;
3359 while ((connection != NULL) &&
3360 (0 != memcmp(&pg->peers[connection->index].daemon->id, peer, sizeof(struct GNUNET_PeerIdentity))))
3362 connection = connection->next;
3365 if (connection == NULL)
3367 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Connected peer %s to %s, not in list (no problem(?))\n", GNUNET_i2s(peer), send_hello_context->peer->daemon->shortname);
3372 other_peer = &pg->peers[connection->index];
3374 if (pg->notify_connection != NULL)
3376 pg->notify_connection(pg->notify_connection_cls,
3377 &send_hello_context->peer->daemon->id,
3380 send_hello_context->peer->daemon->cfg,
3381 pg->peers[connection->index].daemon->cfg,
3382 send_hello_context->peer->daemon,
3383 pg->peers[connection->index].daemon,
3386 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3387 GNUNET_free(connection);
3391 /* Notify of reverse connection and remove from other peers list of outstanding */
3392 if (other_peer != NULL)
3394 connection = other_peer->connect_peers_head;
3395 while ((connection != NULL) &&
3396 (0 != memcmp(&send_hello_context->peer->daemon->id, &pg->peers[connection->index].daemon->id, sizeof(struct GNUNET_PeerIdentity))))
3398 connection = connection->next;
3400 if (connection != NULL)
3402 if (pg->notify_connection != NULL)
3404 pg->notify_connection(pg->notify_connection_cls,
3406 &send_hello_context->peer->daemon->id,
3408 pg->peers[connection->index].daemon->cfg,
3409 send_hello_context->peer->daemon->cfg,
3410 pg->peers[connection->index].daemon,
3411 send_hello_context->peer->daemon,
3415 GNUNET_CONTAINER_DLL_remove(other_peer->connect_peers_head, other_peer->connect_peers_tail, connection);
3416 GNUNET_free(connection);
3421 if (send_hello_context->peer->connect_peers_head == NULL)
3423 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3428 * Notify of a successful connection to the core service.
3430 * @param cls a struct SendHelloContext *
3431 * @param server handle to the core service
3432 * @param my_identity the peer identity of this peer
3433 * @param publicKey the public key of the peer
3436 core_init (void *cls,
3437 struct GNUNET_CORE_Handle * server,
3438 const struct GNUNET_PeerIdentity *
3441 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
3444 struct SendHelloContext *send_hello_context = cls;
3445 send_hello_context->core_ready = GNUNET_YES;
3449 * Function called once a hello has been sent
3450 * to the transport, move on to the next one
3451 * or go away forever.
3453 * @param cls the 'struct SendHelloContext *'
3454 * @param tc scheduler context
3457 hello_sent_callback (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3459 struct SendHelloContext *send_hello_context = cls;
3460 //unsigned int pg_iter;
3461 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3463 GNUNET_free(send_hello_context);
3467 send_hello_context->pg->remaining_hellos--;
3469 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sent HELLO, have %d remaining!\n", send_hello_context->pg->remaining_hellos);
3471 if (send_hello_context->peer_pos == NULL) /* All HELLOs (for this peer!) have been transmitted! */
3474 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All hellos for this peer sent, disconnecting transport!\n");
3476 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3477 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3478 send_hello_context->peer->daemon->th = NULL;
3480 /*if (send_hello_context->pg->remaining_hellos == 0)
3482 for (pg_iter = 0; pg_iter < send_hello_context->pg->max_outstanding_connections; pg_iter++)
3484 preschedule_connect(&send_hello_context->pg->ct_ctx);
3488 GNUNET_assert (send_hello_context->peer->daemon->server == NULL);
3489 send_hello_context->peer->daemon->server = GNUNET_CORE_connect(send_hello_context->peer->cfg,
3493 &core_connect_notify,
3500 send_hello_context->core_connect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_divide(send_hello_context->pg->ct_ctx.connect_timeout, send_hello_context->pg->ct_ctx.connect_attempts),
3501 &send_core_connect_requests,
3502 send_hello_context);
3505 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3509 * Connect to a peer, give it all the HELLO's of those peers
3510 * we will later ask it to connect to.
3512 * @param ct_ctx the overall connection context
3514 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3516 struct SendHelloContext *send_hello_context = cls;
3517 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3519 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3521 GNUNET_free(send_hello_context);
3525 GNUNET_assert(send_hello_context->peer_pos != NULL); /* All of the HELLO sends to be scheduled have been scheduled! */
3527 if (((send_hello_context->peer->daemon->th == NULL) &&
3528 (pg->outstanding_connects > pg->max_outstanding_connections)) ||
3529 (pg->stop_connects == GNUNET_YES))
3531 #if VERBOSE_TESTING > 2
3532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3534 ("Delaying connect, we have too many outstanding connections!\n"));
3536 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3537 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3538 &schedule_send_hellos, send_hello_context);
3542 #if VERBOSE_TESTING > 2
3543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3544 _("Creating connection, outstanding_connections is %d\n"),
3545 outstanding_connects);
3547 if (send_hello_context->peer->daemon->th == NULL)
3549 pg->outstanding_connects++; /* Actual TRANSPORT, CORE connections! */
3550 send_hello_context->peer->daemon->th
3551 = GNUNET_TRANSPORT_connect (send_hello_context->peer->cfg, NULL,
3552 send_hello_context, NULL, NULL, NULL);
3555 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3556 _("Offering Hello of peer %s to peer %s\n"),
3557 send_hello_context->peer->daemon->shortname, pg->peers[send_hello_context->peer_pos->index].daemon->shortname);
3559 GNUNET_TRANSPORT_offer_hello(send_hello_context->peer->daemon->th,
3560 (const struct GNUNET_MessageHeader *)pg->peers[send_hello_context->peer_pos->index].daemon->hello,
3561 &hello_sent_callback,
3562 send_hello_context);
3563 send_hello_context->peer_pos = send_hello_context->peer_pos->next;
3564 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3570 * Internal notification of a connection, kept so that we can ensure some connections
3571 * happen instead of flooding all testing daemons with requests to connect.
3574 internal_connect_notify(void *cls, const struct GNUNET_PeerIdentity *first,
3575 const struct GNUNET_PeerIdentity *second,
3577 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
3578 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
3579 struct GNUNET_TESTING_Daemon *first_daemon,
3580 struct GNUNET_TESTING_Daemon *second_daemon,
3583 struct ConnectContext *connect_ctx = cls;
3584 struct ConnectTopologyContext *ct_ctx = connect_ctx->ct_ctx;
3585 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
3586 struct PeerConnection *connection;
3588 GNUNET_assert (0 < pg->outstanding_connects);
3589 pg->outstanding_connects--;
3592 * Check whether the inverse connection has been scheduled yet,
3593 * if not, we can remove it from the other peers list and avoid
3594 * even trying to connect them again!
3596 connection = pg->peers[connect_ctx->second_index].connect_peers_head;
3601 while ((connection != NULL) && (0
3602 != memcmp (first, &pg->peers[connection->index].daemon->id,
3603 sizeof(struct GNUNET_PeerIdentity))))
3605 connection = connection->next;
3608 if (connection != NULL) /* Can safely remove! */
3610 GNUNET_assert (0 < ct_ctx->remaining_connections);
3611 ct_ctx->remaining_connections--;
3612 if (pg->notify_connection != NULL) /* Notify of reverse connection */
3613 pg->notify_connection (pg->notify_connection_cls, second, first,
3614 distance, second_cfg, first_cfg, second_daemon,
3615 first_daemon, emsg);
3617 GNUNET_CONTAINER_DLL_remove(pg->peers[connect_ctx->second_index].connect_peers_head, pg->peers[connect_ctx->second_index].connect_peers_tail, connection);
3618 GNUNET_free(connection);
3621 if (ct_ctx->remaining_connections == 0)
3623 if (ct_ctx->notify_connections_done != NULL)
3625 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
3626 ct_ctx->notify_connections_done = NULL;
3630 preschedule_connect (pg);
3632 if (pg->notify_connection != NULL)
3633 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
3634 first_cfg, second_cfg, first_daemon, second_daemon,
3637 GNUNET_free(connect_ctx);
3641 * Either delay a connection (because there are too many outstanding)
3642 * or schedule it for right now.
3644 * @param cls a connection context
3645 * @param tc the task runtime context
3648 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3650 struct ConnectContext *connect_context = cls;
3651 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
3653 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3656 if ((pg->outstanding_connects > pg->max_outstanding_connections)
3657 || (pg->stop_connects == GNUNET_YES))
3660 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3662 ("Delaying connect, we have too many outstanding connections!\n"));
3664 GNUNET_SCHEDULER_add_delayed (
3665 GNUNET_TIME_relative_multiply (
3666 GNUNET_TIME_UNIT_MILLISECONDS,
3668 &schedule_connect, connect_context);
3673 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3674 _("Creating connection, outstanding_connections is %d (max %d)\n"),
3675 pg->outstanding_connects, pg->max_outstanding_connections);
3677 pg->outstanding_connects++;
3678 pg->total_connects_scheduled++;
3679 GNUNET_TESTING_daemons_connect (
3680 pg->peers[connect_context->first_index].daemon,
3681 pg->peers[connect_context->second_index].daemon,
3682 connect_context->ct_ctx->connect_timeout,
3683 connect_context->ct_ctx->connect_attempts,
3689 &internal_connect_notify, connect_context); /* FIXME: free connect context! */
3695 * Iterator for actually scheduling connections to be created
3696 * between two peers.
3698 * @param cls closure, a GNUNET_TESTING_Daemon
3699 * @param key the key the second Daemon was stored under
3700 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3702 * @return GNUNET_YES to continue iteration
3705 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3707 struct ConnectTopologyContext *ct_ctx = cls;
3708 struct PeerData *first = ct_ctx->first;
3709 struct GNUNET_TESTING_Daemon *second = value;
3710 struct ConnectContext *connect_context;
3712 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3713 connect_context->first = first->daemon;
3714 connect_context->second = second;
3715 connect_context->ct_ctx = ct_ctx;
3716 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3724 * Iterator for copying all entries in the allowed hashmap to the
3727 * @param cls closure, a GNUNET_TESTING_Daemon
3728 * @param key the key the second Daemon was stored under
3729 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3731 * @return GNUNET_YES to continue iteration
3734 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3736 struct PeerData *first = cls;
3738 GNUNET_assert (GNUNET_OK ==
3739 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
3741 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3748 * Make the peers to connect the same as those that are allowed to be
3751 * @param pg the peer group
3754 copy_allowed_topology(struct GNUNET_TESTING_PeerGroup *pg)
3756 unsigned int pg_iter;
3760 struct PeerConnection *iter;
3764 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3767 iter = pg->peers[pg_iter].allowed_peers_head;
3768 while (iter != NULL)
3770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3771 "Creating connection between %d and %d\n", pg_iter,
3773 total += add_connections (pg, pg_iter, iter->index, CONNECT,
3775 //total += add_actual_connections(pg, pg_iter, iter->index);
3780 GNUNET_CONTAINER_multihashmap_iterate (pg->
3781 peers[pg_iter].allowed_peers,
3782 ©_topology_iterator,
3783 &pg->peers[pg_iter]);
3785 if (GNUNET_SYSERR == ret)
3786 return GNUNET_SYSERR;
3788 total = total + ret;
3795 * Connect the topology as specified by the PeerConnection's
3796 * of each peer in the peer group
3798 * @param pg the peer group we are dealing with
3799 * @param connect_timeout how long try connecting two peers
3800 * @param connect_attempts how many times (max) to attempt
3801 * @param notify_callback callback to notify when finished
3802 * @param notify_cls closure for notify callback
3804 * @return the number of connections that will be attempted
3807 connect_topology(struct GNUNET_TESTING_PeerGroup *pg,
3808 struct GNUNET_TIME_Relative connect_timeout,
3809 unsigned int connect_attempts,
3810 GNUNET_TESTING_NotifyCompletion notify_callback,
3813 unsigned int pg_iter;
3817 struct PeerConnection *connection_iter;
3820 struct SendHelloContext *send_hello_context;
3824 pg->ct_ctx.notify_connections_done = notify_callback;
3825 pg->ct_ctx.notify_cls = notify_cls;
3828 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3831 connection_iter = pg->peers[pg_iter].connect_peers_head;
3832 while (connection_iter != NULL)
3834 connection_iter = connection_iter->next;
3839 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3846 pg->ct_ctx.connect_timeout = connect_timeout;
3847 pg->ct_ctx.connect_attempts = connect_attempts;
3848 pg->ct_ctx.remaining_connections = total;
3851 /* First give all peers the HELLO's of other peers (connect to first peer's transport service, give HELLO's of other peers, continue...) */
3852 pg->remaining_hellos = total;
3853 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3855 send_hello_context = GNUNET_malloc(sizeof(struct SendHelloContext));
3856 send_hello_context->peer = &pg->peers[pg_iter];
3857 send_hello_context->peer_pos = pg->peers[pg_iter].connect_peers_head;
3858 send_hello_context->pg = pg;
3859 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3862 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3864 preschedule_connect (pg);
3872 * Takes a peer group and creates a topology based on the
3873 * one specified. Creates a topology means generates friend
3874 * files for the peers so they can only connect to those allowed
3875 * by the topology. This will only have an effect once peers
3876 * are started if the FRIENDS_ONLY option is set in the base
3877 * config. Also takes an optional restrict topology which
3878 * disallows connections based on particular transports
3879 * UNLESS they are specified in the restricted topology.
3881 * @param pg the peer group struct representing the running peers
3882 * @param topology which topology to connect the peers in
3883 * @param restrict_topology disallow restrict_transports transport
3884 * connections to peers NOT in this topology
3885 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3886 * @param restrict_transports space delimited list of transports to blacklist
3887 * to create restricted topology
3889 * @return the maximum number of connections were all allowed peers
3890 * connected to each other
3893 GNUNET_TESTING_create_topology(struct GNUNET_TESTING_PeerGroup *pg,
3894 enum GNUNET_TESTING_Topology topology,
3895 enum GNUNET_TESTING_Topology restrict_topology,
3896 const char *restrict_transports)
3900 unsigned int num_connections;
3901 int unblacklisted_connections;
3903 struct PeerConnection *conn_iter;
3904 struct PeerConnection *temp_conn;
3909 for (i = 0; i < pg->total; i++)
3911 pg->peers[i].allowed_peers =
3912 GNUNET_CONTAINER_multihashmap_create (100);
3913 pg->peers[i].connect_peers =
3914 GNUNET_CONTAINER_multihashmap_create (100);
3915 pg->peers[i].blacklisted_peers =
3916 GNUNET_CONTAINER_multihashmap_create (100);
3917 pg->peers[i].pg = pg;
3923 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3927 num_connections = create_clique (pg, &add_connections, ALLOWED, GNUNET_NO);
3929 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3932 _("Creating small world (ring) topology\n"));
3934 num_connections = create_small_world_ring (pg, &add_connections, ALLOWED);
3936 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3939 _("Creating small world (2d-torus) topology\n"));
3941 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3943 case GNUNET_TESTING_TOPOLOGY_RING:
3945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3947 num_connections = create_ring (pg, &add_connections, ALLOWED);
3949 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3953 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3955 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3958 _("Creating Erdos-Renyi topology\n"));
3960 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3962 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3966 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3968 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3971 _("Creating Scale Free topology\n"));
3973 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3975 case GNUNET_TESTING_TOPOLOGY_LINE:
3977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3978 _("Creating straight line topology\n"));
3980 num_connections = create_line (pg, &add_connections, ALLOWED);
3982 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3985 _("Creating topology from file!\n"));
3987 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing",
3990 num_connections = create_from_file (pg, filename, &add_connections,
3995 GNUNET_ERROR_TYPE_WARNING,
3996 "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3997 num_connections = 0;
4000 case GNUNET_TESTING_TOPOLOGY_NONE:
4002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4004 ("Creating no allowed topology (all peers can connect at core level)\n"));
4006 num_connections = pg->total * pg->total; /* Clique is allowed! */
4009 num_connections = 0;
4013 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING",
4016 ret = create_and_copy_friend_files (pg);
4017 if (ret != GNUNET_OK)
4020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4021 _("Failed during friend file copying!\n"));
4023 return GNUNET_SYSERR;
4028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4029 _("Friend files created/copied successfully!\n"));
4034 /* Use the create clique method to initially set all connections as blacklisted. */
4035 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology
4036 != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
4037 create_clique (pg, &add_connections, BLACKLIST, GNUNET_NO);
4039 return num_connections;
4041 unblacklisted_connections = 0;
4042 /* Un-blacklist connections as per the topology specified */
4043 switch (restrict_topology)
4045 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4048 _("Blacklisting all but clique topology\n"));
4050 unblacklisted_connections = create_clique (pg, &remove_connections,
4051 BLACKLIST, GNUNET_NO);
4053 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4056 _("Blacklisting all but small world (ring) topology\n"));
4058 unblacklisted_connections = create_small_world_ring (pg,
4059 &remove_connections,
4062 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4066 ("Blacklisting all but small world (2d-torus) topology\n"));
4068 unblacklisted_connections = create_small_world (pg, &remove_connections,
4071 case GNUNET_TESTING_TOPOLOGY_RING:
4073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4074 _("Blacklisting all but ring topology\n"));
4076 unblacklisted_connections
4077 = create_ring (pg, &remove_connections, BLACKLIST);
4079 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4082 _("Blacklisting all but 2d torus topology\n"));
4084 unblacklisted_connections = create_2d_torus (pg, &remove_connections,
4087 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4090 _("Blacklisting all but Erdos-Renyi topology\n"));
4092 unblacklisted_connections = create_erdos_renyi (pg, &remove_connections,
4095 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4098 _("Blacklisting all but InterNAT topology\n"));
4102 for (off = 0; off < pg->total; off++)
4104 conn_iter = pg->peers[off].allowed_peers_head;
4105 while (conn_iter != NULL)
4107 temp_conn = conn_iter->next;
4108 GNUNET_free(conn_iter);
4109 conn_iter = temp_conn;
4111 pg->peers[off].allowed_peers_head = NULL;
4112 pg->peers[off].allowed_peers_tail = NULL;
4114 conn_iter = pg->peers[off].connect_peers_head;
4115 while (conn_iter != NULL)
4117 temp_conn = conn_iter->next;
4118 GNUNET_free(conn_iter);
4119 conn_iter = temp_conn;
4121 pg->peers[off].connect_peers_head = NULL;
4122 pg->peers[off].connect_peers_tail = NULL;
4124 unblacklisted_connections
4125 = create_nated_internet_copy (pg, &remove_connections, BLACKLIST);
4127 unblacklisted_connections =
4128 create_nated_internet (pg, &remove_connections, BLACKLIST);
4132 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4135 _("Blacklisting all but Scale Free topology\n"));
4137 unblacklisted_connections = create_scale_free (pg, &remove_connections,
4140 case GNUNET_TESTING_TOPOLOGY_LINE:
4142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4143 _("Blacklisting all but straight line topology\n"));
4145 unblacklisted_connections
4146 = create_line (pg, &remove_connections, BLACKLIST);
4151 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
4153 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n",
4154 restrict_transports);
4155 ret = create_and_copy_blacklist_files (pg, restrict_transports);
4156 if (ret != GNUNET_OK)
4159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4160 _("Failed during blacklist file copying!\n"));
4167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4168 _("Blacklist files created/copied successfully!\n"));
4172 return num_connections;
4177 * Iterator for choosing random peers to connect.
4179 * @param cls closure, a RandomContext
4180 * @param key the key the second Daemon was stored under
4181 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4183 * @return GNUNET_YES to continue iteration
4186 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4188 struct RandomContext *random_ctx = cls;
4189 double random_number;
4190 uint32_t second_pos;
4191 GNUNET_HashCode first_hash;
4194 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4195 UINT64_MAX)) / ((double) UINT64_MAX);
4196 if (random_number < random_ctx->percentage)
4198 GNUNET_assert (GNUNET_OK ==
4199 GNUNET_CONTAINER_multihashmap_put (random_ctx->
4200 first->connect_peers_working_set,
4202 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4205 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
4206 uid_from_hash (key, &second_pos);
4207 hash_from_uid (random_ctx->first_uid, &first_hash);
4208 GNUNET_assert (random_ctx->pg->total > second_pos);
4209 GNUNET_assert (GNUNET_YES ==
4210 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
4212 [second_pos].connect_peers,
4221 * Iterator for adding at least X peers to a peers connection set.
4223 * @param cls closure, MinimumContext
4224 * @param key the key the second Daemon was stored under
4225 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4227 * @return GNUNET_YES to continue iteration
4230 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4232 struct MinimumContext *min_ctx = cls;
4233 uint32_t second_pos;
4234 GNUNET_HashCode first_hash;
4237 if (GNUNET_CONTAINER_multihashmap_size
4238 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
4240 for (i = 0; i < min_ctx->num_to_add; i++)
4242 if (min_ctx->pg_array[i] == min_ctx->current)
4244 GNUNET_assert (GNUNET_OK ==
4245 GNUNET_CONTAINER_multihashmap_put
4246 (min_ctx->first->connect_peers_working_set, key,
4248 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4249 uid_from_hash (key, &second_pos);
4250 hash_from_uid (min_ctx->first_uid, &first_hash);
4251 GNUNET_assert (min_ctx->pg->total > second_pos);
4252 GNUNET_assert (GNUNET_OK ==
4253 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4255 [second_pos].connect_peers_working_set,
4259 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4260 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
4261 GNUNET_assert (GNUNET_YES ==
4262 GNUNET_CONTAINER_multihashmap_remove
4263 (min_ctx->pg->peers[second_pos].connect_peers,
4264 &first_hash, min_ctx->first->daemon));
4271 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
4276 * Iterator for adding peers to a connection set based on a depth first search.
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 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4287 struct DFSContext *dfs_ctx = cls;
4288 GNUNET_HashCode first_hash;
4290 if (dfs_ctx->current == dfs_ctx->chosen)
4292 GNUNET_assert (GNUNET_OK ==
4293 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4294 first->connect_peers_working_set,
4296 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4297 uid_from_hash (key, &dfs_ctx->second_uid);
4298 hash_from_uid (dfs_ctx->first_uid, &first_hash);
4299 GNUNET_assert (GNUNET_OK ==
4300 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4302 [dfs_ctx->second_uid].connect_peers_working_set,
4306 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4307 GNUNET_assert (GNUNET_YES ==
4308 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
4310 [dfs_ctx->second_uid].connect_peers,
4314 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
4315 return GNUNET_NO; /* We have found our peer, don't iterate more */
4324 * From the set of connections possible, choose percentage percent of connections
4325 * to actually connect.
4327 * @param pg the peergroup we are dealing with
4328 * @param percentage what percent of total connections to make
4331 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg,
4336 struct PeerConnection *conn_iter;
4337 double random_number;
4339 struct RandomContext random_ctx;
4342 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4345 conn_iter = pg->peers[pg_iter].connect_peers_head;
4346 while (conn_iter != NULL)
4349 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4351 / ((double) UINT64_MAX);
4352 if (random_number < percentage)
4354 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4357 conn_iter = conn_iter->next;
4360 random_ctx.first_uid = pg_iter;
4361 random_ctx.first = &pg->peers[pg_iter];
4362 random_ctx.percentage = percentage;
4364 pg->peers[pg_iter].connect_peers_working_set
4365 = GNUNET_CONTAINER_multihashmap_create (pg->total);
4366 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4367 &random_connect_iterator,
4369 /* Now remove the old connections */
4370 GNUNET_CONTAINER_multihashmap_destroy (pg->
4371 peers[pg_iter].connect_peers);
4372 /* And replace with the random set */
4373 pg->peers[pg_iter].connect_peers
4374 = pg->peers[pg_iter].connect_peers_working_set;
4378 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4380 conn_iter = pg->peers[pg_iter].connect_peers_head;
4381 while (pg->peers[pg_iter].connect_peers_head != NULL)
4382 remove_connections (pg, pg_iter,
4383 pg->peers[pg_iter].connect_peers_head->index,
4384 CONNECT, GNUNET_YES);
4386 pg->peers[pg_iter].connect_peers_head
4387 = pg->peers[pg_iter].connect_peers_working_set_head;
4388 pg->peers[pg_iter].connect_peers_tail
4389 = pg->peers[pg_iter].connect_peers_working_set_tail;
4390 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4391 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4396 * Count the number of connections in a linked list of connections.
4398 * @param conn_list the connection list to get the count of
4400 * @return the number of elements in the list
4403 count_connections(struct PeerConnection *conn_list)
4405 struct PeerConnection *iter;
4409 while (iter != NULL)
4418 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
4421 unsigned int pg_iter;
4423 struct PeerConnection *conn_iter;
4427 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4430 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
4431 while (conn_iter != NULL)
4434 conn_iter = conn_iter->next;
4438 GNUNET_CONTAINER_multihashmap_size (pg->
4440 [pg_iter].connect_peers_working_set);
4448 count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
4451 unsigned int pg_iter;
4453 struct PeerConnection *conn_iter;
4457 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4460 conn_iter = pg->peers[pg_iter].allowed_peers_head;
4461 while (conn_iter != NULL)
4464 conn_iter = conn_iter->next;
4468 GNUNET_CONTAINER_multihashmap_size (pg->
4470 [pg_iter].allowed_peers);
4478 * From the set of connections possible, choose at least num connections per
4481 * @param pg the peergroup we are dealing with
4482 * @param num how many connections at least should each peer have (if possible)?
4485 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4488 struct MinimumContext minimum_ctx;
4490 struct PeerConnection *conn_iter;
4491 unsigned int temp_list_size;
4494 uint32_t random; /* Random list entry to connect peer to */
4499 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4502 = count_connections (pg->peers[pg_iter].connect_peers_head);
4503 if (temp_list_size == 0)
4505 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4506 "Peer %d has 0 connections!?!?\n", pg_iter);
4509 for (i = 0; i < num; i++)
4511 random = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4513 conn_iter = pg->peers[pg_iter].connect_peers_head;
4514 for (count = 0; count < random; count++)
4515 conn_iter = conn_iter->next;
4516 /* We now have a random connection, connect it! */
4517 GNUNET_assert(conn_iter != NULL);
4518 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4523 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4525 pg->peers[pg_iter].connect_peers_working_set =
4526 GNUNET_CONTAINER_multihashmap_create (num);
4529 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4531 minimum_ctx.first_uid = pg_iter;
4532 minimum_ctx.pg_array =
4533 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
4534 GNUNET_CONTAINER_multihashmap_size
4535 (pg->peers[pg_iter].connect_peers));
4536 minimum_ctx.first = &pg->peers[pg_iter];
4537 minimum_ctx.pg = pg;
4538 minimum_ctx.num_to_add = num;
4539 minimum_ctx.current = 0;
4540 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4541 &minimum_connect_iterator,
4545 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4547 /* Remove the "old" connections */
4548 GNUNET_CONTAINER_multihashmap_destroy (pg->
4549 peers[pg_iter].connect_peers);
4550 /* And replace with the working set */
4551 pg->peers[pg_iter].connect_peers =
4552 pg->peers[pg_iter].connect_peers_working_set;
4555 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4557 while (pg->peers[pg_iter].connect_peers_head != NULL)
4559 conn_iter = pg->peers[pg_iter].connect_peers_head;
4560 GNUNET_CONTAINER_DLL_remove(pg->peers[pg_iter].connect_peers_head,
4561 pg->peers[pg_iter].connect_peers_tail,
4563 GNUNET_free(conn_iter);
4564 /*remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT, GNUNET_YES);*/
4567 pg->peers[pg_iter].connect_peers_head
4568 = pg->peers[pg_iter].connect_peers_working_set_head;
4569 pg->peers[pg_iter].connect_peers_tail
4570 = pg->peers[pg_iter].connect_peers_working_set_tail;
4571 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4572 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4577 struct FindClosestContext
4580 * The currently known closest peer.
4582 struct GNUNET_TESTING_Daemon *closest;
4585 * The info for the peer we are adding connections for.
4587 struct PeerData *curr_peer;
4590 * The distance (bits) between the current
4591 * peer and the currently known closest.
4593 unsigned int closest_dist;
4596 * The offset of the closest known peer in
4599 unsigned int closest_num;
4603 * Iterator over hash map entries of the allowed
4604 * peer connections. Find the closest, not already
4605 * connected peer and return it.
4607 * @param cls closure (struct FindClosestContext)
4608 * @param key current key code (hash of offset in pg)
4609 * @param value value in the hash map - a GNUNET_TESTING_Daemon
4610 * @return GNUNET_YES if we should continue to
4615 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
4617 struct FindClosestContext *closest_ctx = cls;
4618 struct GNUNET_TESTING_Daemon *daemon = value;
4620 if (((closest_ctx->closest == NULL) ||
4621 (GNUNET_CRYPTO_hash_matching_bits
4622 (&daemon->id.hashPubKey,
4623 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
4624 closest_ctx->closest_dist))
4626 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
4627 curr_peer->connect_peers,
4630 closest_ctx->closest_dist =
4631 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
4632 &closest_ctx->curr_peer->daemon->
4634 closest_ctx->closest = daemon;
4635 uid_from_hash (key, &closest_ctx->closest_num);
4641 * From the set of connections possible, choose at num connections per
4642 * peer based on depth which are closest out of those allowed. Guaranteed
4643 * to add num peers to connect to, provided there are that many peers
4644 * in the underlay topology to connect to.
4646 * @param pg the peergroup we are dealing with
4647 * @param num how many connections at least should each peer have (if possible)?
4648 * @param proc processor to actually add the connections
4649 * @param list the peer list to use
4652 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
4653 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
4658 struct FindClosestContext closest_ctx;
4663 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
4665 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4667 closest_ctx.curr_peer = &pg->peers[pg_iter];
4668 closest_ctx.closest = NULL;
4669 closest_ctx.closest_dist = 0;
4670 closest_ctx.closest_num = 0;
4671 GNUNET_CONTAINER_multihashmap_iterate (pg->
4672 peers[pg_iter].allowed_peers,
4673 &find_closest_peers,
4675 if (closest_ctx.closest != NULL)
4677 GNUNET_assert (closest_ctx.closest_num < pg->total);
4678 proc (pg, pg_iter, closest_ctx.closest_num, list);
4686 * From the set of connections possible, choose at least num connections per
4687 * peer based on depth first traversal of peer connections. If DFS leaves
4688 * peers unconnected, ensure those peers get connections.
4690 * @param pg the peergroup we are dealing with
4691 * @param num how many connections at least should each peer have (if possible)?
4694 perform_dfs(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4698 uint32_t starting_peer;
4699 uint32_t least_connections;
4700 uint32_t random_connection;
4702 unsigned int temp_count;
4703 struct PeerConnection *peer_iter;
4705 struct DFSContext dfs_ctx;
4706 GNUNET_HashCode second_hash;
4712 while ((count_workingset_connections (pg) < num * pg->total)
4713 && (count_allowed_connections (pg) > 0))
4715 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4717 least_connections = -1; /* Set to very high number */
4718 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4721 = count_connections (
4722 pg->peers[pg_iter].connect_peers_working_set_head);
4723 if (temp_count < least_connections)
4725 starting_peer = pg_iter;
4726 least_connections = temp_count;
4732 = count_connections (pg->peers[starting_peer].connect_peers_head);
4733 if (temp_count == 0)
4734 continue; /* FIXME: infinite loop? */
4736 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4739 peer_iter = pg->peers[starting_peer].connect_peers_head;
4740 while (temp_count < random_connection)
4742 peer_iter = peer_iter->next;
4745 GNUNET_assert(peer_iter != NULL);
4746 add_connections (pg, starting_peer, peer_iter->index, WORKING_SET,
4748 remove_connections (pg, starting_peer, peer_iter->index, CONNECT,
4750 starting_peer = peer_iter->index;
4755 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4757 pg->peers[pg_iter].connect_peers_working_set =
4758 GNUNET_CONTAINER_multihashmap_create (num);
4763 while ((count_workingset_connections (pg) < num * pg->total)
4764 && (count_allowed_connections (pg) > 0))
4766 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4768 least_connections = -1; /* Set to very high number */
4769 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4771 if (GNUNET_CONTAINER_multihashmap_size
4772 (pg->peers[pg_iter].connect_peers_working_set) <
4775 starting_peer = pg_iter;
4777 GNUNET_CONTAINER_multihashmap_size (pg->
4779 [pg_iter].connect_peers_working_set);
4784 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4790 /* Choose a random peer from the chosen peers set of connections to add */
4792 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4793 GNUNET_CONTAINER_multihashmap_size
4794 (pg->peers[starting_peer].connect_peers));
4795 dfs_ctx.first_uid = starting_peer;
4796 dfs_ctx.first = &pg->peers[starting_peer];
4798 dfs_ctx.current = 0;
4800 GNUNET_CONTAINER_multihashmap_iterate (pg->
4802 [starting_peer].connect_peers,
4803 &dfs_connect_iterator, &dfs_ctx);
4804 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4805 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4806 GNUNET_assert (GNUNET_YES ==
4807 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4808 [starting_peer].connect_peers,
4812 [dfs_ctx.second_uid].daemon));
4813 starting_peer = dfs_ctx.second_uid;
4816 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4818 /* Remove the "old" connections */
4819 GNUNET_CONTAINER_multihashmap_destroy (pg->
4820 peers[pg_iter].connect_peers);
4821 /* And replace with the working set */
4822 pg->peers[pg_iter].connect_peers =
4823 pg->peers[pg_iter].connect_peers_working_set;
4829 * Internal callback for topology information for a particular peer.
4832 internal_topology_callback(void *cls, const struct GNUNET_PeerIdentity *peer,
4833 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4835 struct CoreContext *core_ctx = cls;
4836 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4838 if (peer == NULL) /* Either finished, or something went wrong */
4840 iter_ctx->completed++;
4841 iter_ctx->connected--;
4842 /* One core context allocated per iteration, must free! */
4843 GNUNET_free (core_ctx);
4847 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id, peer, NULL);
4850 if (iter_ctx->completed == iter_ctx->total)
4852 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4853 /* Once all are done, free the iteration context */
4854 GNUNET_free (iter_ctx);
4859 * Check running topology iteration tasks, if below max start a new one, otherwise
4860 * schedule for some time in the future.
4863 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4865 struct CoreContext *core_context = cls;
4866 struct TopologyIterateContext *topology_context =
4867 (struct TopologyIterateContext *) core_context->iter_context;
4868 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
4871 if (topology_context->connected
4872 > topology_context->pg->max_outstanding_connections)
4874 #if VERBOSE_TESTING > 2
4875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4877 ("Delaying connect, we have too many outstanding connections!\n"));
4879 GNUNET_SCHEDULER_add_delayed (
4880 GNUNET_TIME_relative_multiply (
4881 GNUNET_TIME_UNIT_MILLISECONDS,
4883 &schedule_get_topology, core_context);
4887 #if VERBOSE_TESTING > 2
4888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4889 _("Creating connection, outstanding_connections is %d\n"),
4890 outstanding_connects);
4892 topology_context->connected++;
4894 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4895 &internal_topology_callback,
4898 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4899 internal_topology_callback (core_context, NULL, NULL);
4905 * Iterate over all (running) peers in the peer group, retrieve
4906 * all connections that each currently has.
4909 GNUNET_TESTING_get_topology(struct GNUNET_TESTING_PeerGroup *pg,
4910 GNUNET_TESTING_NotifyTopology cb, void *cls)
4912 struct TopologyIterateContext *topology_context;
4913 struct CoreContext *core_ctx;
4915 unsigned int total_count;
4917 /* Allocate a single topology iteration context */
4918 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4919 topology_context->topology_cb = cb;
4920 topology_context->cls = cls;
4921 topology_context->pg = pg;
4923 for (i = 0; i < pg->total; i++)
4925 if (pg->peers[i].daemon->running == GNUNET_YES)
4927 /* Allocate one core context per core we need to connect to */
4928 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4929 core_ctx->daemon = pg->peers[i].daemon;
4930 /* Set back pointer to topology iteration context */
4931 core_ctx->iter_context = topology_context;
4932 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4936 if (total_count == 0)
4938 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4939 GNUNET_free (topology_context);
4942 topology_context->total = total_count;
4947 * Callback function to process statistic values.
4948 * This handler is here only really to insert a peer
4949 * identity (or daemon) so the statistics can be uniquely
4950 * tied to a single running peer.
4952 * @param cls closure
4953 * @param subsystem name of subsystem that created the statistic
4954 * @param name the name of the datum
4955 * @param value the current value
4956 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4957 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4960 internal_stats_callback(void *cls, const char *subsystem, const char *name,
4961 uint64_t value, int is_persistent)
4963 struct StatsCoreContext *core_context = cls;
4964 struct StatsIterateContext *stats_context =
4965 (struct StatsIterateContext *) core_context->iter_context;
4967 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4968 subsystem, name, value, is_persistent);
4972 * Internal continuation call for statistics iteration.
4974 * @param cls closure, the CoreContext for this iteration
4975 * @param success whether or not the statistics iterations
4976 * was canceled or not (we don't care)
4979 internal_stats_cont(void *cls, int success)
4981 struct StatsCoreContext *core_context = cls;
4982 struct StatsIterateContext *stats_context =
4983 (struct StatsIterateContext *) core_context->iter_context;
4985 stats_context->connected--;
4986 stats_context->completed++;
4988 if (stats_context->completed == stats_context->total)
4990 stats_context->cont (stats_context->cls, GNUNET_YES);
4991 GNUNET_free (stats_context);
4994 if (core_context->stats_handle != NULL)
4995 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4997 GNUNET_free (core_context);
5001 * Check running topology iteration tasks, if below max start a new one, otherwise
5002 * schedule for some time in the future.
5005 schedule_get_statistics(void *cls,
5006 const struct GNUNET_SCHEDULER_TaskContext *tc)
5008 struct StatsCoreContext *core_context = cls;
5009 struct StatsIterateContext *stats_context =
5010 (struct StatsIterateContext *) core_context->iter_context;
5012 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5015 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
5017 #if VERBOSE_TESTING > 2
5018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5020 ("Delaying connect, we have too many outstanding connections!\n"));
5022 GNUNET_SCHEDULER_add_delayed (
5023 GNUNET_TIME_relative_multiply (
5024 GNUNET_TIME_UNIT_MILLISECONDS,
5026 &schedule_get_statistics, core_context);
5030 #if VERBOSE_TESTING > 2
5031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5032 _("Creating connection, outstanding_connections is %d\n"),
5033 outstanding_connects);
5036 stats_context->connected++;
5037 core_context->stats_handle
5038 = GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
5039 if (core_context->stats_handle == NULL)
5041 internal_stats_cont (core_context, GNUNET_NO);
5045 core_context->stats_get_handle
5046 = GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
5047 GNUNET_TIME_relative_get_forever (),
5048 &internal_stats_cont,
5049 &internal_stats_callback, core_context);
5050 if (core_context->stats_get_handle == NULL)
5051 internal_stats_cont (core_context, GNUNET_NO);
5056 struct DuplicateStats
5059 * Next item in the list
5061 struct DuplicateStats *next;
5064 * Nasty string, concatenation of relevant information.
5066 char *unique_string;
5070 * Check whether the combination of port/host/unix domain socket
5071 * already exists in the list of peers being checked for statistics.
5073 * @param pg the peergroup in question
5074 * @param specific_peer the peer we're concerned with
5075 * @param stats_list the list to return to the caller
5077 * @return GNUNET_YES if the statistics instance has been seen already,
5078 * GNUNET_NO if not (and we may have added it to the list)
5081 stats_check_existing(struct GNUNET_TESTING_PeerGroup *pg,
5082 struct PeerData *specific_peer,
5083 struct DuplicateStats **stats_list)
5085 struct DuplicateStats *pos;
5086 char *unix_domain_socket;
5087 unsigned long long port;
5090 != GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
5091 "single_statistics_per_host"))
5092 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
5095 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg,
5098 &unix_domain_socket))
5101 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg,
5102 "statistics", "port",
5105 GNUNET_free(unix_domain_socket);
5109 if (specific_peer->daemon->hostname != NULL)
5110 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
5111 unix_domain_socket, port);
5113 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
5117 if (0 == strcmp (to_match, pos->unique_string))
5119 GNUNET_free (unix_domain_socket);
5120 GNUNET_free (to_match);
5125 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
5126 pos->unique_string = to_match;
5127 pos->next = *stats_list;
5129 GNUNET_free (unix_domain_socket);
5134 * Iterate over all (running) peers in the peer group, retrieve
5135 * all statistics from each.
5137 * @param pg the peergroup to iterate statistics of
5138 * @param cont continuation to call once all stats have been retrieved
5139 * @param proc processing function for each statistic from each peer
5140 * @param cls closure to pass to proc
5144 GNUNET_TESTING_get_statistics(struct GNUNET_TESTING_PeerGroup *pg,
5145 GNUNET_STATISTICS_Callback cont,
5146 GNUNET_TESTING_STATISTICS_Iterator proc,
5149 struct StatsIterateContext *stats_context;
5150 struct StatsCoreContext *core_ctx;
5152 unsigned int total_count;
5153 struct DuplicateStats *stats_list;
5154 struct DuplicateStats *pos;
5157 /* Allocate a single stats iteration context */
5158 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
5159 stats_context->cont = cont;
5160 stats_context->proc = proc;
5161 stats_context->cls = cls;
5162 stats_context->pg = pg;
5165 for (i = 0; i < pg->total; i++)
5167 if ((pg->peers[i].daemon->running == GNUNET_YES) && (GNUNET_NO
5168 == stats_check_existing (pg, &pg->peers[i], &stats_list)))
5170 /* Allocate one core context per core we need to connect to */
5171 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
5172 core_ctx->daemon = pg->peers[i].daemon;
5173 /* Set back pointer to topology iteration context */
5174 core_ctx->iter_context = stats_context;
5175 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
5180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5181 "Retrieving stats from %u total instances.\n", total_count);
5182 stats_context->total = total_count;
5183 if (stats_list != NULL)
5188 GNUNET_free (pos->unique_string);
5189 stats_list = pos->next;
5191 pos = stats_list->next;
5198 * Stop the connection process temporarily.
5200 * @param pg the peer group to stop connecting
5203 GNUNET_TESTING_stop_connections(struct GNUNET_TESTING_PeerGroup *pg)
5205 pg->stop_connects = GNUNET_YES;
5209 * Resume the connection process temporarily.
5211 * @param pg the peer group to resume connecting
5214 GNUNET_TESTING_resume_connections(struct GNUNET_TESTING_PeerGroup *pg)
5216 pg->stop_connects = GNUNET_NO;
5220 * There are many ways to connect peers that are supported by this function.
5221 * To connect peers in the same topology that was created via the
5222 * GNUNET_TESTING_create_topology, the topology variable must be set to
5223 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
5224 * a new instance of that topology will be generated and attempted to be
5225 * connected. This could result in some connections being impossible,
5226 * because some topologies are non-deterministic.
5228 * @param pg the peer group struct representing the running peers
5229 * @param topology which topology to connect the peers in
5230 * @param options options for connecting the topology
5231 * @param option_modifier modifier for options that take a parameter
5232 * @param connect_timeout how long to wait before giving up on connecting
5234 * @param connect_attempts how many times to attempt to connect two peers
5235 * over the connect_timeout duration
5236 * @param notify_callback notification to be called once all connections completed
5237 * @param notify_cls closure for notification callback
5239 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
5242 GNUNET_TESTING_connect_topology(
5243 struct GNUNET_TESTING_PeerGroup *pg,
5244 enum GNUNET_TESTING_Topology topology,
5245 enum GNUNET_TESTING_TopologyOption options,
5246 double option_modifier,
5247 struct GNUNET_TIME_Relative connect_timeout,
5248 unsigned int connect_attempts,
5249 GNUNET_TESTING_NotifyCompletion notify_callback,
5254 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
5255 #if VERBOSE_TOPOLOGY
5256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5257 _("Creating clique CONNECT topology\n"));
5259 create_clique (pg, &add_connections, CONNECT, GNUNET_NO);
5261 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
5262 #if VERBOSE_TOPOLOGY
5263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5264 _("Creating small world (ring) CONNECT topology\n"));
5266 create_small_world_ring (pg, &add_connections, CONNECT);
5268 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
5269 #if VERBOSE_TOPOLOGY
5270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5271 _("Creating small world (2d-torus) CONNECT topology\n"));
5273 create_small_world (pg, &add_connections, CONNECT);
5275 case GNUNET_TESTING_TOPOLOGY_RING:
5276 #if VERBOSE_TOPOLOGY
5277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring CONNECT topology\n"));
5279 create_ring (pg, &add_connections, CONNECT);
5281 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
5282 #if VERBOSE_TOPOLOGY
5283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5284 _("Creating 2d torus CONNECT topology\n"));
5286 create_2d_torus (pg, &add_connections, CONNECT);
5288 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
5289 #if VERBOSE_TOPOLOGY
5290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5291 _("Creating Erdos-Renyi CONNECT topology\n"));
5293 create_erdos_renyi (pg, &add_connections, CONNECT);
5295 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
5296 #if VERBOSE_TOPOLOGY
5297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5298 _("Creating InterNAT CONNECT topology\n"));
5300 create_nated_internet (pg, &add_connections, CONNECT);
5302 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
5303 #if VERBOSE_TOPOLOGY
5304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5305 _("Creating Scale Free CONNECT topology\n"));
5307 create_scale_free (pg, &add_connections, CONNECT);
5309 case GNUNET_TESTING_TOPOLOGY_LINE:
5310 #if VERBOSE_TOPOLOGY
5311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5312 _("Creating straight line CONNECT topology\n"));
5314 create_line (pg, &add_connections, CONNECT);
5316 case GNUNET_TESTING_TOPOLOGY_NONE:
5317 #if VERBOSE_TOPOLOGY
5318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating no CONNECT topology\n"));
5320 copy_allowed_topology (pg);
5323 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5324 ("Unknown topology specification, can't connect peers!\n"));
5325 return GNUNET_SYSERR;
5330 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
5331 #if VERBOSE_TOPOLOGY
5332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5333 ("Connecting random subset (%'.2f percent) of possible peers\n"), 100
5336 choose_random_connections (pg, option_modifier);
5338 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
5339 #if VERBOSE_TOPOLOGY
5340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5341 _("Connecting a minimum of %u peers each (if possible)\n"),
5342 (unsigned int) option_modifier);
5344 choose_minimum (pg, (unsigned int) option_modifier);
5346 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
5347 #if VERBOSE_TOPOLOGY
5348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5349 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
5350 (unsigned int) option_modifier);
5353 perform_dfs (pg, (int) option_modifier);
5356 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
5357 #if VERBOSE_TOPOLOGY
5358 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5359 ("Finding additional %u closest peers each (if possible)\n"),
5360 (unsigned int) option_modifier);
5363 add_closest (pg, (unsigned int) option_modifier,
5364 &add_connections, CONNECT);
5367 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
5369 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
5375 return connect_topology (pg, connect_timeout, connect_attempts,
5376 notify_callback, notify_cls);
5380 * Lookup and return the number of SSH connections to a host.
5382 * @param hostname the hostname to lookup in the list
5383 * @param pg the peergroup that the host belongs to
5385 * @return the number of current ssh connections to the host
5388 count_outstanding_at_host(const char *hostname,
5389 struct GNUNET_TESTING_PeerGroup *pg)
5391 struct OutstandingSSH *pos;
5393 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5395 GNUNET_assert(pos != NULL);
5396 return pos->outstanding;
5400 * Increment the number of SSH connections to a host by one.
5402 * @param hostname the hostname to lookup in the list
5403 * @param pg the peergroup that the host belongs to
5407 increment_outstanding_at_host(const char *hostname,
5408 struct GNUNET_TESTING_PeerGroup *pg)
5410 struct OutstandingSSH *pos;
5412 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5414 GNUNET_assert(pos != NULL);
5419 * Decrement the number of SSH connections to a host by one.
5421 * @param hostname the hostname to lookup in the list
5422 * @param pg the peergroup that the host belongs to
5426 decrement_outstanding_at_host(const char *hostname,
5427 struct GNUNET_TESTING_PeerGroup *pg)
5429 struct OutstandingSSH *pos;
5431 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5433 GNUNET_assert(pos != NULL);
5438 * Callback that is called whenever a hostkey is generated
5439 * for a peer. Call the real callback and decrement the
5440 * starting counter for the peergroup.
5442 * @param cls closure
5443 * @param id identifier for the daemon, NULL on error
5444 * @param d handle for the daemon
5445 * @param emsg error message (NULL on success)
5448 internal_hostkey_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5449 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5451 struct InternalStartContext *internal_context = cls;
5452 internal_context->peer->pg->starting--;
5453 internal_context->peer->pg->started++;
5454 if (internal_context->hostname != NULL)
5455 decrement_outstanding_at_host (internal_context->hostname,
5456 internal_context->peer->pg);
5457 if (internal_context->hostkey_callback != NULL)
5458 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
5460 else if (internal_context->peer->pg->started
5461 == internal_context->peer->pg->total)
5463 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
5464 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
5469 * Callback that is called whenever a peer has finished starting.
5470 * Call the real callback and decrement the starting counter
5471 * for the peergroup.
5473 * @param cls closure
5474 * @param id identifier for the daemon, NULL on error
5476 * @param d handle for the daemon
5477 * @param emsg error message (NULL on success)
5480 internal_startup_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5481 const struct GNUNET_CONFIGURATION_Handle *cfg,
5482 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5484 struct InternalStartContext *internal_context = cls;
5485 internal_context->peer->pg->starting--;
5486 if (internal_context->hostname != NULL)
5487 decrement_outstanding_at_host (internal_context->hostname,
5488 internal_context->peer->pg);
5489 if (internal_context->start_cb != NULL)
5490 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
5495 internal_continue_startup(void *cls,
5496 const struct GNUNET_SCHEDULER_TaskContext *tc)
5498 struct InternalStartContext *internal_context = cls;
5500 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5505 if ((internal_context->peer->pg->starting
5506 < internal_context->peer->pg->max_concurrent_ssh)
5507 || ((internal_context->hostname != NULL)
5508 && (count_outstanding_at_host (internal_context->hostname,
5509 internal_context->peer->pg)
5510 < internal_context->peer->pg->max_concurrent_ssh)))
5512 if (internal_context->hostname != NULL)
5513 increment_outstanding_at_host (internal_context->hostname,
5514 internal_context->peer->pg);
5515 internal_context->peer->pg->starting++;
5516 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
5520 GNUNET_SCHEDULER_add_delayed (
5521 GNUNET_TIME_relative_multiply (
5522 GNUNET_TIME_UNIT_MILLISECONDS,
5524 &internal_continue_startup,
5530 * Callback for informing us about a successful
5531 * or unsuccessful churn start call.
5533 * @param cls a ChurnContext
5534 * @param id the peer identity of the started peer
5535 * @param cfg the handle to the configuration of the peer
5536 * @param d handle to the daemon for the peer
5537 * @param emsg NULL on success, non-NULL on failure
5541 churn_start_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5542 const struct GNUNET_CONFIGURATION_Handle *cfg,
5543 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5545 struct ChurnRestartContext *startup_ctx = cls;
5546 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
5548 unsigned int total_left;
5549 char *error_message;
5551 error_message = NULL;
5554 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5555 "Churn stop callback failed with error `%s'\n", emsg);
5556 churn_ctx->num_failed_start++;
5560 churn_ctx->num_to_start--;
5563 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
5564 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5566 if (total_left == 0)
5568 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
5571 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5572 churn_ctx->num_failed_start,
5573 churn_ctx->num_failed_stop);
5574 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5575 GNUNET_free_non_null (error_message);
5576 GNUNET_free (churn_ctx);
5577 GNUNET_free (startup_ctx);
5582 schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5584 struct PeerRestartContext *peer_restart_ctx = cls;
5585 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
5587 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5588 GNUNET_SCHEDULER_add_delayed (
5589 GNUNET_TIME_relative_multiply (
5590 GNUNET_TIME_UNIT_MILLISECONDS,
5592 &schedule_churn_restart, peer_restart_ctx);
5595 if (startup_ctx->churn_ctx->service != NULL)
5596 GNUNET_TESTING_daemon_start_stopped_service (peer_restart_ctx->daemon,
5597 startup_ctx->churn_ctx->service,
5598 startup_ctx->timeout,
5599 &churn_start_callback, startup_ctx);
5601 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
5602 startup_ctx->timeout,
5603 &churn_start_callback, startup_ctx);
5604 GNUNET_free (peer_restart_ctx);
5610 internal_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5612 struct InternalStartContext *internal_context = cls;
5614 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5619 if ((internal_context->peer->pg->starting
5620 < internal_context->peer->pg->max_concurrent_ssh)
5621 || ((internal_context->hostname != NULL)
5622 && (count_outstanding_at_host (internal_context->hostname,
5623 internal_context->peer->pg)
5624 < internal_context->peer->pg->max_concurrent_ssh)))
5626 if (internal_context->hostname != NULL)
5627 increment_outstanding_at_host (internal_context->hostname,
5628 internal_context->peer->pg);
5629 internal_context->peer->pg->starting++;
5630 internal_context->peer->daemon
5631 = GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
5632 internal_context->timeout,
5634 internal_context->hostname,
5635 internal_context->username,
5636 internal_context->sshport,
5637 internal_context->hostkey,
5638 &internal_hostkey_callback,
5640 &internal_startup_callback,
5645 GNUNET_SCHEDULER_add_delayed (
5646 GNUNET_TIME_relative_multiply (
5647 GNUNET_TIME_UNIT_MILLISECONDS,
5649 &internal_start, internal_context);
5652 #if USE_START_HELPER
5654 struct PeerStartHelperContext
5656 struct GNUNET_TESTING_PeerGroup *pg;
5658 struct HostData *host;
5660 struct GNUNET_OS_Process *proc;
5664 check_peers_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5666 struct PeerStartHelperContext *helper = cls;
5667 enum GNUNET_OS_ProcessStatusType type;
5670 GNUNET_TESTING_NotifyDaemonRunning cb;
5672 if (GNUNET_NO == GNUNET_OS_process_status (helper->proc, &type, &code)) /* Still running, wait some more! */
5674 GNUNET_SCHEDULER_add_delayed(GNUNET_CONSTANTS_EXEC_WAIT, &check_peers_started, helper);
5678 helper->pg->starting--;
5679 if (helper->pg->starting == 0) /* All peers have finished starting! */
5681 /* Call the peer started callback for each peer, set proper FSM state (?) */
5682 for (i = 0; i < helper->pg->total; i++)
5684 cb = helper->pg->peers[i].daemon->cb;
5685 helper->pg->peers[i].daemon->cb = NULL;
5686 helper->pg->peers[i].daemon->running = GNUNET_YES;
5687 helper->pg->peers[i].daemon->phase = SP_START_DONE;
5690 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
5691 cb (helper->pg->peers[i].daemon->cb_cls,
5692 &helper->pg->peers[i].daemon->id,
5693 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5694 "Failed to execute peerStartHelper.pl, or return code bad!");
5696 cb (helper->pg->peers[i].daemon->cb_cls,
5697 &helper->pg->peers[i].daemon->id,
5698 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5705 GNUNET_OS_process_close(helper->proc);
5709 start_peer_helper (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5711 struct PeerStartHelperContext *helper = cls;
5712 char *baseservicehome;
5715 /* ssh user@host peerStartHelper /path/to/basedirectory */
5716 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (helper->pg->cfg, "PATHS", "SERVICEHOME",
5718 GNUNET_asprintf(&tempdir, "%s/%s/", baseservicehome, helper->host->hostname);
5719 if (NULL != helper->host->username)
5720 GNUNET_asprintf (&arg, "%s@%s", helper->host->username, helper->host->hostname);
5722 GNUNET_asprintf (&arg, "%s", helper->host->hostname);
5724 /* FIXME: Doesn't support ssh_port option! */
5725 helper->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5726 "peerStartHelper.pl", tempdir, NULL);
5727 GNUNET_assert(helper->proc != NULL);
5728 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "starting peers with cmd ssh %s %s %s\n", arg, "peerStartHelper.pl", tempdir);
5729 GNUNET_SCHEDULER_add_now (&check_peers_started, helper);
5730 GNUNET_free (tempdir);
5731 GNUNET_free (baseservicehome);
5737 * Function which continues a peer group starting up
5738 * after successfully generating hostkeys for each peer.
5740 * @param pg the peer group to continue starting
5744 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
5748 #if USE_START_HELPER
5749 if ((pg->num_hosts > 0) && (pg->hostkey_data != NULL))
5751 struct PeerStartHelperContext *helper;
5752 pg->starting = pg->num_hosts;
5753 for (i = 0; i < pg->num_hosts; i++)
5755 helper = GNUNET_malloc(sizeof(struct PeerStartHelperContext));
5757 helper->host = &pg->hosts[i];
5758 GNUNET_SCHEDULER_add_now(&start_peer_helper, helper);
5764 for (i = 0; i < pg->total; i++)
5766 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5767 &pg->peers[i].internal_context);
5772 for (i = 0; i < pg->total; i++)
5774 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5775 &pg->peers[i].internal_context);
5780 #if USE_START_HELPER
5782 call_hostkey_callbacks (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5784 struct GNUNET_TESTING_PeerGroup *pg = cls;
5786 for (i = 0; i < pg->total; i++)
5788 if (pg->peers[i].internal_context.hostkey_callback != NULL)
5789 pg->peers[i].internal_context.hostkey_callback (pg->peers[i].internal_context.hostkey_cls,
5790 &pg->peers[i].daemon->id,
5791 pg->peers[i].daemon,
5795 if (pg->peers[0].internal_context.hostkey_callback == NULL)
5796 GNUNET_TESTING_daemons_continue_startup (pg);
5801 * Start count gnunet instances with the same set of transports and
5802 * applications. The port numbers (any option called "PORT") will be
5803 * adjusted to ensure that no two peers running on the same system
5804 * have the same port(s) in their respective configurations.
5806 * @param cfg configuration template to use
5807 * @param total number of daemons to start
5808 * @param max_concurrent_connections for testing, how many peers can
5809 * we connect to simultaneously
5810 * @param max_concurrent_ssh when starting with ssh, how many ssh
5811 * connections will we allow at once (based on remote hosts allowed!)
5812 * @param timeout total time allowed for peers to start
5813 * @param hostkey_callback function to call on each peers hostkey generation
5814 * if NULL, peers will be started by this call, if non-null,
5815 * GNUNET_TESTING_daemons_continue_startup must be called after
5816 * successful hostkey generation
5817 * @param hostkey_cls closure for hostkey callback
5818 * @param cb function to call on each daemon that was started
5819 * @param cb_cls closure for cb
5820 * @param connect_callback function to call each time two hosts are connected
5821 * @param connect_callback_cls closure for connect_callback
5822 * @param hostnames linked list of host structs to use to start peers on
5823 * (NULL to run on localhost only)
5825 * @return NULL on error, otherwise handle to control peer group
5827 struct GNUNET_TESTING_PeerGroup *
5828 GNUNET_TESTING_daemons_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
5830 unsigned int max_concurrent_connections,
5831 unsigned int max_concurrent_ssh,
5832 struct GNUNET_TIME_Relative timeout,
5833 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
5835 GNUNET_TESTING_NotifyDaemonRunning cb,
5837 GNUNET_TESTING_NotifyConnection connect_callback,
5838 void *connect_callback_cls,
5839 const struct GNUNET_TESTING_Host *hostnames)
5841 struct GNUNET_TESTING_PeerGroup *pg;
5842 const struct GNUNET_TESTING_Host *hostpos;
5843 const char *hostname;
5844 const char *username;
5845 char *baseservicehome;
5846 char *newservicehome;
5848 char *hostkeys_file;
5851 struct GNUNET_DISK_FileHandle *fd;
5852 struct GNUNET_CONFIGURATION_Handle *pcfg;
5854 struct OutstandingSSH *ssh_entry;
5855 unsigned int hostcnt;
5862 uint64_t total_hostkeys;
5863 struct GNUNET_OS_Process *proc;
5874 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
5876 pg->notify_connection = connect_callback;
5877 pg->notify_connection_cls = connect_callback_cls;
5879 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
5880 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
5881 pg->max_outstanding_connections = max_concurrent_connections;
5882 pg->max_concurrent_ssh = max_concurrent_ssh;
5883 if (NULL != hostnames)
5886 hostpos = hostnames;
5887 while (hostpos != NULL)
5889 hostpos = hostpos->next;
5892 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5895 hostpos = hostnames;
5896 while (hostpos != NULL)
5898 pg->hosts[off].minport = LOW_PORT;
5899 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
5900 if (hostpos->username != NULL)
5901 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
5902 pg->hosts[off].sshport = hostpos->port;
5903 hostpos = hostpos->next;
5913 pg->num_hosts = off;
5921 /* Create the servicehome directory for each remote peer */
5922 GNUNET_assert(GNUNET_OK ==
5923 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
5925 for (i = 0; i < pg->num_hosts; i++)
5927 ssh_entry = GNUNET_malloc(sizeof(struct OutstandingSSH));
5928 ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
5929 GNUNET_CONTAINER_DLL_insert(pg->ssh_head, pg->ssh_tail, ssh_entry);
5930 GNUNET_asprintf(&tmpdir, "%s/%s", baseservicehome, pg->hosts[i].hostname);
5931 if (NULL != pg->hosts[i].username)
5932 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username,
5933 pg->hosts[i].hostname);
5935 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
5936 if (pg->hosts[i].sshport != 0)
5938 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
5939 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-P",
5944 arg, "mkdir -p", tmpdir,
5948 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5949 "mkdir -p", tmpdir, NULL);
5950 GNUNET_assert(proc != NULL);
5951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5952 "Creating remote dir with command ssh %s %s %s\n", arg,
5953 " mkdir -p ", tmpdir);
5954 GNUNET_free(tmpdir);
5956 GNUNET_OS_process_wait (proc);
5957 GNUNET_OS_process_close(proc);
5959 GNUNET_free(baseservicehome);
5960 baseservicehome = NULL;
5962 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING",
5966 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
5967 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5968 _("Could not read hostkeys file!\n"));
5971 /* Check hostkey file size, read entire thing into memory */
5972 fd = GNUNET_DISK_file_open (hostkeys_file,
5973 GNUNET_DISK_OPEN_READ,
5974 GNUNET_DISK_PERM_NONE);
5977 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
5980 GNUNET_free (hostkeys_file);
5981 for (i=0;i<pg->num_hosts;i++)
5983 GNUNET_free (pg->hosts[i].hostname);
5984 GNUNET_free_non_null (pg->hosts[i].username);
5986 GNUNET_free (pg->peers);
5987 GNUNET_free (pg->hosts);
5992 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs,
5996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5997 "Found file size %llu for hostkeys, expect hostkeys to be size %d\n",
5998 fs, HOSTKEYFILESIZE);
6000 if (0 != (fs % HOSTKEYFILESIZE))
6002 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6003 "File size %llu seems incorrect for hostkeys...\n",
6008 total_hostkeys = fs / HOSTKEYFILESIZE;
6009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6010 "Will read %llu hostkeys from file\n",
6012 pg->hostkey_data = GNUNET_malloc_large (fs);
6013 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
6015 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
6017 GNUNET_free(hostkeys_file);
6020 for (off = 0; off < total; off++)
6024 hostname = pg->hosts[off % hostcnt].hostname;
6025 username = pg->hosts[off % hostcnt].username;
6026 sshport = pg->hosts[off % hostcnt].sshport;
6027 pcfg = make_config (cfg, off, &pg->hosts[off % hostcnt].minport,
6028 &upnum, hostname, &fdnum);
6035 pcfg = make_config (cfg, off, &minport, &upnum, hostname, &fdnum);
6040 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6041 _("Could not create configuration for peer number %u on `%s'!\n"),
6043 hostname == NULL ? "localhost" : hostname);
6048 == GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS",
6052 if (hostname != NULL)
6053 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", baseservicehome, hostname, off);
6055 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
6056 GNUNET_free (baseservicehome);
6057 baseservicehome = NULL;
6061 tmpdir = getenv ("TMPDIR");
6062 tmpdir = tmpdir ? tmpdir : "/tmp";
6063 if (hostname != NULL)
6064 GNUNET_asprintf (&newservicehome, "%s/%s/%s/%d/", tmpdir, hostname,
6065 "gnunet-testing-test-test", off);
6067 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", tmpdir,
6068 "gnunet-testing-test-test", off);
6070 GNUNET_CONFIGURATION_set_value_string (pcfg, "PATHS", "SERVICEHOME",
6072 GNUNET_free (newservicehome);
6073 pg->peers[off].cfg = pcfg;
6074 pg->peers[off].pg = pg;
6075 pg->peers[off].internal_context.peer = &pg->peers[off];
6076 pg->peers[off].internal_context.timeout = timeout;
6077 pg->peers[off].internal_context.hostname = hostname;
6078 pg->peers[off].internal_context.username = username;
6079 pg->peers[off].internal_context.sshport = sshport;
6080 if (pg->hostkey_data != NULL)
6081 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off
6083 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
6084 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
6085 pg->peers[off].internal_context.start_cb = cb;
6086 pg->peers[off].internal_context.start_cb_cls = cb_cls;
6087 #if !USE_START_HELPER
6088 GNUNET_SCHEDULER_add_now (&internal_start,
6089 &pg->peers[off].internal_context);
6091 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6093 pg->peers[off].daemon
6094 = GNUNET_TESTING_daemon_start (pcfg,
6100 pg->peers[off].internal_context.hostkey,
6101 &internal_hostkey_callback,
6102 &pg->peers[off].internal_context,
6103 &internal_startup_callback,
6104 &pg->peers[off].internal_context);
6106 * At this point, given that we had a hostkeyfile,
6107 * we can call the hostkey callback!
6108 * But first, we should copy (rsync) all of the configs
6109 * and hostkeys to the remote peers. Then let topology
6110 * creation happen, then call the peer start helper processes,
6111 * then set pg->whatever_phase for each peer and let them
6112 * enter the fsm to get the HELLO's for peers and start connecting.
6117 GNUNET_SCHEDULER_add_now (&internal_start,
6118 &pg->peers[off].internal_context);
6124 #if USE_START_HELPER /* Now the peergroup has been set up, hostkeys and configs written to files. */
6125 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6127 for (off = 0; off < hostcnt; off++)
6132 hostname = pg->hosts[off % hostcnt].hostname;
6133 username = pg->hosts[off % hostcnt].username;
6134 sshport = pg->hosts[off % hostcnt].sshport;
6144 == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
6148 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "baseservice home is %s\n", baseservicehome);
6149 if (hostname != NULL)
6150 GNUNET_asprintf (&newservicehome, "%s/%s/", baseservicehome, hostname);
6152 GNUNET_asprintf (&newservicehome, "%s/", baseservicehome);
6153 GNUNET_free (baseservicehome);
6154 baseservicehome = NULL;
6158 tmpdir = getenv ("TMPDIR");
6159 tmpdir = tmpdir ? tmpdir : "/tmp";
6160 if (hostname != NULL)
6161 GNUNET_asprintf (&newservicehome, "%s/%s/%s/", tmpdir, hostname,
6162 "gnunet-testing-test-test");
6164 GNUNET_asprintf (&newservicehome, "%s/%s/", tmpdir,
6165 "gnunet-testing-test-test", off);
6168 if (NULL != username)
6169 GNUNET_asprintf (&arg,
6172 pg->hosts[off].hostname,
6175 GNUNET_asprintf (&arg,
6177 pg->hosts[off].hostname,
6180 /* FIXME: Doesn't support ssh_port option! */
6181 proc = GNUNET_OS_start_process (NULL, NULL,
6183 "rsync", "-r", newservicehome, arg, NULL);
6185 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6186 "copying directory with command rsync -r %s %s\n",
6187 newservicehome, arg);
6188 GNUNET_free(newservicehome);
6192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6193 _("Could not start `%s' process to copy configuration directory.\n"),
6197 GNUNET_OS_process_wait (proc);
6198 GNUNET_OS_process_close (proc);
6200 /* Now all the configuration files and hostkeys are copied to the remote host. Call the hostkey callback for each peer! */
6201 GNUNET_SCHEDULER_add_now (&call_hostkey_callbacks, pg);
6208 * Get a daemon by number, so callers don't have to do nasty
6209 * offsetting operation.
6211 struct GNUNET_TESTING_Daemon *
6212 GNUNET_TESTING_daemon_get(struct GNUNET_TESTING_PeerGroup *pg,
6213 unsigned int position)
6215 if (position < pg->total)
6216 return pg->peers[position].daemon;
6221 * Get a daemon by peer identity, so callers can
6222 * retrieve the daemon without knowing it's offset.
6224 * @param pg the peer group to retrieve the daemon from
6225 * @param peer_id the peer identity of the daemon to retrieve
6227 * @return the daemon on success, or NULL if no such peer identity is found
6229 struct GNUNET_TESTING_Daemon *
6230 GNUNET_TESTING_daemon_get_by_id(struct GNUNET_TESTING_PeerGroup *pg,
6231 struct GNUNET_PeerIdentity *peer_id)
6235 for (i = 0; i < pg->total; i++)
6237 if (0 == memcmp (&pg->peers[i].daemon->id, peer_id,
6238 sizeof(struct GNUNET_PeerIdentity)))
6239 return pg->peers[i].daemon;
6245 * Prototype of a function that will be called when a
6246 * particular operation was completed the testing library.
6248 * @param cls closure (a struct RestartContext)
6249 * @param id id of the peer that was restarted
6250 * @param cfg handle to the configuration of the peer
6251 * @param d handle to the daemon that was restarted
6252 * @param emsg NULL on success
6255 restart_callback(void *cls, const struct GNUNET_PeerIdentity *id,
6256 const struct GNUNET_CONFIGURATION_Handle *cfg,
6257 struct GNUNET_TESTING_Daemon *d, const char *emsg)
6259 struct RestartContext *restart_context = cls;
6263 restart_context->peers_restarted++;
6267 restart_context->peers_restart_failed++;
6270 if (restart_context->peers_restarted == restart_context->peer_group->total)
6272 restart_context->callback (restart_context->callback_cls, NULL);
6273 GNUNET_free (restart_context);
6275 else if (restart_context->peers_restart_failed
6276 + restart_context->peers_restarted == restart_context->peer_group->total)
6278 restart_context->callback (restart_context->callback_cls,
6279 "Failed to restart peers!");
6280 GNUNET_free (restart_context);
6286 * Callback for informing us about a successful
6287 * or unsuccessful churn stop call.
6289 * @param cls a ChurnContext
6290 * @param emsg NULL on success, non-NULL on failure
6294 churn_stop_callback(void *cls, const char *emsg)
6296 struct ShutdownContext *shutdown_ctx = cls;
6297 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
6298 unsigned int total_left;
6299 char *error_message;
6301 error_message = NULL;
6302 shutdown_ctx->outstanding--;
6306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6307 "Churn stop callback failed with error `%s'\n", emsg);
6308 churn_ctx->num_failed_stop++;
6312 churn_ctx->num_to_stop--;
6315 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
6316 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
6318 if (total_left == 0)
6320 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
6324 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
6325 churn_ctx->num_failed_start,
6326 churn_ctx->num_failed_stop);
6328 churn_ctx->cb (churn_ctx->cb_cls, error_message);
6329 GNUNET_free_non_null (error_message);
6330 GNUNET_free (churn_ctx);
6331 GNUNET_free (shutdown_ctx);
6336 * Count the number of running peers.
6338 * @param pg handle for the peer group
6340 * @return the number of currently running peers in the peer group
6343 GNUNET_TESTING_daemons_running(struct GNUNET_TESTING_PeerGroup *pg)
6346 unsigned int running = 0;
6347 for (i = 0; i < pg->total; i++)
6349 if (pg->peers[i].daemon->running == GNUNET_YES)
6351 GNUNET_assert (running != -1);
6359 * Task to rate limit the number of outstanding peer shutdown
6360 * requests. This is necessary for making sure we don't do
6361 * too many ssh connections at once, but is generally nicer
6362 * to any system as well (graduated task starts, as opposed
6363 * to calling gnunet-arm N times all at once).
6366 schedule_churn_shutdown_task(void *cls,
6367 const struct GNUNET_SCHEDULER_TaskContext *tc)
6369 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6370 struct ShutdownContext *shutdown_ctx;
6371 struct ChurnContext *churn_ctx;
6372 GNUNET_assert (peer_shutdown_ctx != NULL);
6373 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6374 GNUNET_assert (shutdown_ctx != NULL);
6375 churn_ctx = (struct ChurnContext *) shutdown_ctx->cb_cls;
6376 if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
6377 GNUNET_SCHEDULER_add_delayed (
6378 GNUNET_TIME_relative_multiply (
6379 GNUNET_TIME_UNIT_MILLISECONDS,
6381 &schedule_churn_shutdown_task,
6385 shutdown_ctx->outstanding++;
6386 if (churn_ctx->service != NULL)
6387 GNUNET_TESTING_daemon_stop_service (peer_shutdown_ctx->daemon,
6389 shutdown_ctx->timeout, shutdown_ctx->cb,
6392 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6393 shutdown_ctx->timeout, shutdown_ctx->cb,
6394 shutdown_ctx, GNUNET_NO, GNUNET_YES);
6395 GNUNET_free (peer_shutdown_ctx);
6401 * Simulate churn by stopping some peers (and possibly
6402 * re-starting others if churn is called multiple times). This
6403 * function can only be used to create leave-join churn (peers "never"
6404 * leave for good). First "voff" random peers that are currently
6405 * online will be taken offline; then "von" random peers that are then
6406 * offline will be put back online. No notifications will be
6407 * generated for any of these operations except for the callback upon
6410 * @param pg handle for the peer group
6411 * @param service the service to churn off/on, NULL to churn peer
6412 * @param voff number of peers that should go offline
6413 * @param von number of peers that should come back online;
6414 * must be zero on first call (since "testbed_start"
6415 * always starts all of the peers)
6416 * @param timeout how long to wait for operations to finish before
6418 * @param cb function to call at the end
6419 * @param cb_cls closure for cb
6422 GNUNET_TESTING_daemons_churn(struct GNUNET_TESTING_PeerGroup *pg,
6424 unsigned int voff, unsigned int von,
6425 struct GNUNET_TIME_Relative timeout,
6426 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6428 struct ChurnContext *churn_ctx;
6429 struct ShutdownContext *shutdown_ctx;
6430 struct PeerShutdownContext *peer_shutdown_ctx;
6431 struct PeerRestartContext *peer_restart_ctx;
6432 struct ChurnRestartContext *churn_startup_ctx;
6434 unsigned int running;
6435 unsigned int stopped;
6436 unsigned int total_running;
6437 unsigned int total_stopped;
6439 unsigned int *running_arr;
6440 unsigned int *stopped_arr;
6441 unsigned int *running_permute;
6442 unsigned int *stopped_permute;
6445 shutdown_ctx = NULL;
6446 peer_shutdown_ctx = NULL;
6447 peer_restart_ctx = NULL;
6448 churn_startup_ctx = NULL;
6453 if ((von == 0) && (voff == 0)) /* No peers at all? */
6459 for (i = 0; i < pg->total; i++)
6461 if (service == NULL)
6463 if (pg->peers[i].daemon->running == GNUNET_YES)
6465 GNUNET_assert (running != -1);
6470 GNUNET_assert (stopped != -1);
6476 /* FIXME: make churned services a list! */
6477 pos = pg->peers[i].daemon->churned_services;
6478 /* FIXME: while (pos != NULL) */
6482 if (0 == strcasecmp(pos, service))
6488 GNUNET_assert (stopped != -1);
6490 /* FIXME: pos = pos->next; */
6494 GNUNET_assert (running != -1);
6502 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6503 "Trying to stop more peers (%d) than are currently running (%d)!\n", voff, running);
6504 cb (cb_cls, "Trying to stop more peers than are currently running!");
6510 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6511 "Trying to start more peers (%d) than are currently stopped (%d)!\n", von, stopped);
6512 cb (cb_cls, "Trying to start more peers than are currently stopped!");
6516 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6518 if (service != NULL)
6519 churn_ctx->service = GNUNET_strdup(service);
6522 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
6526 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
6528 running_permute = NULL;
6529 stopped_permute = NULL;
6532 running_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6535 stopped_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6538 total_running = running;
6539 total_stopped = stopped;
6543 churn_ctx->num_to_start = von;
6544 churn_ctx->num_to_stop = voff;
6546 churn_ctx->cb_cls = cb_cls;
6549 for (i = 0; i < pg->total; i++)
6551 if (service == NULL)
6553 if (pg->peers[i].daemon->running == GNUNET_YES)
6555 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6556 running_arr[running] = i;
6561 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6562 stopped_arr[stopped] = i;
6568 /* FIXME: make churned services a list! */
6569 pos = pg->peers[i].daemon->churned_services;
6570 /* FIXME: while (pos != NULL) */
6573 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6574 stopped_arr[stopped] = i;
6576 /* FIXME: pos = pos->next; */
6580 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6581 running_arr[running] = i;
6587 GNUNET_assert (running >= voff);
6590 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6591 shutdown_ctx->cb = &churn_stop_callback;
6592 shutdown_ctx->cb_cls = churn_ctx;
6593 shutdown_ctx->total_peers = voff;
6594 shutdown_ctx->timeout = timeout;
6597 for (i = 0; i < voff; i++)
6600 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_arr[running_permute[i]]);
6602 GNUNET_assert (running_arr != NULL);
6603 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6604 peer_shutdown_ctx->daemon
6605 = pg->peers[running_arr[running_permute[i]]].daemon;
6606 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6607 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
6611 GNUNET_assert (stopped >= von);
6614 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6615 churn_startup_ctx->churn_ctx = churn_ctx;
6616 churn_startup_ctx->timeout = timeout;
6617 churn_startup_ctx->pg = pg;
6619 for (i = 0; i < von; i++)
6622 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_arr[stopped_permute[i]]);
6624 GNUNET_assert (stopped_arr != NULL);
6625 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
6626 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
6627 peer_restart_ctx->daemon
6628 = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
6629 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
6632 GNUNET_free_non_null (running_arr);
6633 GNUNET_free_non_null (stopped_arr);
6634 GNUNET_free_non_null (running_permute);
6635 GNUNET_free_non_null (stopped_permute);
6640 * Restart all peers in the given group.
6642 * @param pg the handle to the peer group
6643 * @param callback function to call on completion (or failure)
6644 * @param callback_cls closure for the callback function
6647 GNUNET_TESTING_daemons_restart(struct GNUNET_TESTING_PeerGroup *pg,
6648 GNUNET_TESTING_NotifyCompletion callback,
6651 struct RestartContext *restart_context;
6656 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
6657 restart_context->peer_group = pg;
6658 restart_context->peers_restarted = 0;
6659 restart_context->callback = callback;
6660 restart_context->callback_cls = callback_cls;
6662 for (off = 0; off < pg->total; off++)
6664 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
6665 &restart_callback, restart_context);
6672 * Start or stop an individual peer from the given group.
6674 * @param pg handle to the peer group
6675 * @param offset which peer to start or stop
6676 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
6677 * @param timeout how long to wait for shutdown
6678 * @param cb function to call at the end
6679 * @param cb_cls closure for cb
6682 GNUNET_TESTING_daemons_vary(struct GNUNET_TESTING_PeerGroup *pg,
6683 unsigned int offset, int desired_status,
6684 struct GNUNET_TIME_Relative timeout,
6685 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6687 struct ShutdownContext *shutdown_ctx;
6688 struct ChurnRestartContext *startup_ctx;
6689 struct ChurnContext *churn_ctx;
6691 if (GNUNET_NO == desired_status)
6693 if (NULL != pg->peers[offset].daemon)
6695 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6696 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6697 churn_ctx->num_to_start = 0;
6698 churn_ctx->num_to_stop = 1;
6700 churn_ctx->cb_cls = cb_cls;
6701 shutdown_ctx->cb_cls = churn_ctx;
6702 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon, timeout,
6703 &churn_stop_callback, shutdown_ctx,
6704 GNUNET_NO, GNUNET_YES);
6707 else if (GNUNET_YES == desired_status)
6709 if (NULL == pg->peers[offset].daemon)
6711 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6712 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6713 churn_ctx->num_to_start = 1;
6714 churn_ctx->num_to_stop = 0;
6716 churn_ctx->cb_cls = cb_cls;
6717 startup_ctx->churn_ctx = churn_ctx;
6718 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
6719 timeout, &churn_start_callback,
6729 * Callback for shutting down peers in a peer group.
6731 * @param cls closure (struct ShutdownContext)
6732 * @param emsg NULL on success
6735 internal_shutdown_callback(void *cls, const char *emsg)
6737 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6738 struct ShutdownContext *shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6740 struct OutstandingSSH *ssh_pos;
6742 shutdown_ctx->outstanding--;
6743 if (peer_shutdown_ctx->daemon->hostname != NULL)
6744 decrement_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6749 shutdown_ctx->peers_down++;
6753 shutdown_ctx->peers_failed++;
6756 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down
6757 + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
6759 if (shutdown_ctx->peers_failed > 0)
6760 shutdown_ctx->cb (shutdown_ctx->cb_cls,
6761 "Not all peers successfully shut down!");
6763 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
6765 GNUNET_free (shutdown_ctx->pg->peers);
6766 GNUNET_free_non_null(shutdown_ctx->pg->hostkey_data);
6767 for (off = 0; off < shutdown_ctx->pg->num_hosts; off++)
6769 GNUNET_free (shutdown_ctx->pg->hosts[off].hostname);
6770 GNUNET_free_non_null (shutdown_ctx->pg->hosts[off].username);
6772 GNUNET_free_non_null (shutdown_ctx->pg->hosts);
6773 while (NULL != (ssh_pos = shutdown_ctx->pg->ssh_head))
6775 GNUNET_CONTAINER_DLL_remove(shutdown_ctx->pg->ssh_head, shutdown_ctx->pg->ssh_tail, ssh_pos);
6776 GNUNET_free(ssh_pos);
6778 GNUNET_free (shutdown_ctx->pg);
6779 GNUNET_free (shutdown_ctx);
6781 GNUNET_free(peer_shutdown_ctx);
6786 * Task to rate limit the number of outstanding peer shutdown
6787 * requests. This is necessary for making sure we don't do
6788 * too many ssh connections at once, but is generally nicer
6789 * to any system as well (graduated task starts, as opposed
6790 * to calling gnunet-arm N times all at once).
6793 schedule_shutdown_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6795 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6796 struct ShutdownContext *shutdown_ctx;
6798 GNUNET_assert (peer_shutdown_ctx != NULL);
6799 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6800 GNUNET_assert (shutdown_ctx != NULL);
6802 if ((shutdown_ctx->outstanding < shutdown_ctx->pg->max_concurrent_ssh)
6803 || ((peer_shutdown_ctx->daemon->hostname != NULL)
6804 && (count_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6806 < shutdown_ctx->pg->max_concurrent_ssh)))
6808 if (peer_shutdown_ctx->daemon->hostname != NULL)
6809 increment_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6811 shutdown_ctx->outstanding++;
6812 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6813 shutdown_ctx->timeout,
6814 &internal_shutdown_callback, peer_shutdown_ctx,
6815 shutdown_ctx->delete_files, GNUNET_NO);
6818 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
6820 &schedule_shutdown_task, peer_shutdown_ctx);
6825 * Read a testing hosts file based on a configuration.
6826 * Returns a DLL of hosts (caller must free!) on success
6827 * or NULL on failure.
6829 * @param cfg a configuration with a testing section
6831 * @return DLL of hosts on success, NULL on failure
6833 struct GNUNET_TESTING_Host *
6834 GNUNET_TESTING_hosts_load (const struct GNUNET_CONFIGURATION_Handle *cfg)
6836 struct GNUNET_TESTING_Host *hosts;
6837 struct GNUNET_TESTING_Host *temphost;
6845 /* Check for a hostfile containing user@host:port triples */
6847 != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "hostfile",
6854 if (hostfile != NULL)
6856 if (GNUNET_OK != GNUNET_DISK_file_test (hostfile))
6857 GNUNET_DISK_fn_write (hostfile, NULL, 0, GNUNET_DISK_PERM_USER_READ
6858 | GNUNET_DISK_PERM_USER_WRITE);
6859 if ((0 != STAT (hostfile, &frstat)) || (frstat.st_size == 0))
6861 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6862 "Could not open file specified for host list, ending test!");
6863 GNUNET_free(hostfile);
6867 data = GNUNET_malloc_large (frstat.st_size);
6868 GNUNET_assert(data != NULL);
6870 != GNUNET_DISK_fn_read (hostfile, data, frstat.st_size))
6873 GNUNET_ERROR_TYPE_ERROR,
6874 "Could not read file %s specified for host list, ending test!",
6876 GNUNET_free (hostfile);
6881 GNUNET_free_non_null(hostfile);
6885 while (count < frstat.st_size - 1)
6888 if (((data[count] == '\n')) && (buf != &data[count]))
6891 temphost = GNUNET_malloc(sizeof(struct GNUNET_TESTING_Host));
6892 ret = sscanf (buf, "%a[a-zA-Z0-9_]@%a[a-zA-Z0-9.]:%hd",
6893 &temphost->username, &temphost->hostname,
6898 GNUNET_ERROR_TYPE_DEBUG,
6899 "Successfully read host %s, port %d and user %s from file\n",
6900 temphost->hostname, temphost->port,
6901 temphost->username);
6905 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6906 "Error reading line `%s' in hostfile\n", buf);
6907 GNUNET_free(temphost);
6908 buf = &data[count + 1];
6911 temphost->next = hosts;
6913 buf = &data[count + 1];
6915 else if ((data[count] == '\n') || (data[count] == '\0'))
6916 buf = &data[count + 1];
6919 GNUNET_free_non_null(data);
6925 * Shutdown all peers started in the given group.
6927 * @param pg handle to the peer group
6928 * @param timeout how long to wait for shutdown
6929 * @param cb callback to notify upon success or failure
6930 * @param cb_cls closure for cb
6933 GNUNET_TESTING_daemons_stop(struct GNUNET_TESTING_PeerGroup *pg,
6934 struct GNUNET_TIME_Relative timeout,
6935 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6938 struct ShutdownContext *shutdown_ctx;
6939 struct PeerShutdownContext *peer_shutdown_ctx;
6941 struct PeerConnection *conn_iter;
6942 struct PeerConnection *temp_conn;
6945 GNUNET_assert (pg->total > 0);
6947 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6948 shutdown_ctx->delete_files = GNUNET_CONFIGURATION_get_value_yesno (pg->cfg,
6951 shutdown_ctx->cb = cb;
6952 shutdown_ctx->cb_cls = cb_cls;
6953 shutdown_ctx->total_peers = pg->total;
6954 shutdown_ctx->timeout = timeout;
6955 shutdown_ctx->pg = pg;
6956 /* shtudown_ctx->outstanding = 0; */
6958 for (off = 0; off < pg->total; off++)
6960 GNUNET_assert (NULL != pg->peers[off].daemon);
6961 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6962 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
6963 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6964 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
6966 if (NULL != pg->peers[off].cfg)
6968 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
6969 pg->peers[off].cfg = NULL;
6972 conn_iter = pg->peers[off].allowed_peers_head;
6973 while (conn_iter != NULL)
6975 temp_conn = conn_iter->next;
6976 GNUNET_free(conn_iter);
6977 conn_iter = temp_conn;
6980 conn_iter = pg->peers[off].connect_peers_head;
6981 while (conn_iter != NULL)
6983 temp_conn = conn_iter->next;
6984 GNUNET_free(conn_iter);
6985 conn_iter = temp_conn;
6988 conn_iter = pg->peers[off].blacklisted_peers_head;
6989 while (conn_iter != NULL)
6991 temp_conn = conn_iter->next;
6992 GNUNET_free(conn_iter);
6993 conn_iter = temp_conn;
6996 conn_iter = pg->peers[off].connect_peers_working_set_head;
6997 while (conn_iter != NULL)
6999 temp_conn = conn_iter->next;
7000 GNUNET_free(conn_iter);
7001 conn_iter = temp_conn;
7004 if (pg->peers[off].allowed_peers != NULL)
7005 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
7006 if (pg->peers[off].connect_peers != NULL)
7007 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
7008 if (pg->peers[off].blacklisted_peers != NULL)
7009 GNUNET_CONTAINER_multihashmap_destroy (pg->
7010 peers[off].blacklisted_peers);
7015 /* end of testing_group.c */