2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing_group.c
23 * @brief convenience API for writing testcases for GNUnet
24 * @author Nathan Evans
25 * @author Christian Grothoff
29 #include "gnunet_arm_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_core_service.h"
33 #define VERBOSE_TESTING GNUNET_NO
35 #define VERBOSE_TOPOLOGY GNUNET_NO
37 #define DEBUG_CHURN GNUNET_NO
39 #define USE_START_HELPER GNUNET_YES
43 /* Before connecting peers, send all of the HELLOs */
44 #define USE_SEND_HELLOS GNUNET_NO
46 #define TOPOLOGY_HACK GNUNET_YES
50 * Lowest port used for GNUnet testing. Should be high enough to not
51 * conflict with other applications running on the hosts but be low
52 * enough to not conflict with client-ports (typically starting around
55 #define LOW_PORT 12000
58 * Highest port used for GNUnet testing. Should be low enough to not
59 * conflict with the port range for "local" ports (client apps; see
60 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
62 #define HIGH_PORT 56000
64 /* Maximum time to delay connect attempt */
65 #define MAX_CONNECT_DELAY 300
68 * Which list of peers do we need to modify?
72 /** Modify allowed peers */
75 /** Modify connect peers */
78 /** Modify blacklist peers */
81 /** Modify workingset peers */
86 * Prototype of a function called whenever two peers would be connected
87 * in a certain topology.
90 (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup * pg,
91 unsigned int first, unsigned int second,
92 enum PeerLists list, unsigned int check);
95 * Context for handling churning a peer group
100 * The peergroup we are dealing with.
102 struct GNUNET_TESTING_PeerGroup *pg;
105 * Callback used to notify of churning finished
107 GNUNET_TESTING_NotifyCompletion cb;
110 * Closure for callback
115 * Number of peers that still need to be started
117 unsigned int num_to_start;
120 * Number of peers that still need to be stopped
122 unsigned int num_to_stop;
125 * Number of peers that failed to start
127 unsigned int num_failed_start;
130 * Number of peers that failed to stop
132 unsigned int num_failed_stop;
135 struct RestartContext
138 * The group of peers being restarted
140 struct GNUNET_TESTING_PeerGroup *peer_group;
143 * How many peers have been restarted thus far
145 unsigned int peers_restarted;
148 * How many peers got an error when restarting
150 unsigned int peers_restart_failed;
153 * The function to call once all peers have been restarted
155 GNUNET_TESTING_NotifyCompletion callback;
158 * Closure for callback function
164 struct SendHelloContext
167 * Global handle to the peer group.
169 struct GNUNET_TESTING_PeerGroup *pg;
172 * The data about this specific peer.
174 struct PeerData *peer;
177 * The next HELLO that needs sent to this peer.
179 struct PeerConnection *peer_pos;
182 * Are we connected to CORE yet?
184 unsigned int core_ready;
187 * How many attempts should we make for failed connections?
189 unsigned int connect_attempts;
192 * Task for scheduling core connect requests to be sent.
194 GNUNET_SCHEDULER_TaskIdentifier core_connect_task;
197 struct ShutdownContext
199 struct GNUNET_TESTING_PeerGroup *pg;
201 * Total peers to wait for
203 unsigned int total_peers;
206 * Number of peers successfully shut down
208 unsigned int peers_down;
211 * Number of peers failed to shut down
213 unsigned int peers_failed;
216 * Number of peers we have started shutting
217 * down. If too many, wait on them.
219 unsigned int outstanding;
222 * Timeout for shutdown.
224 struct GNUNET_TIME_Relative timeout;
227 * Callback to call when all peers either
228 * shutdown or failed to shutdown
230 GNUNET_TESTING_NotifyCompletion cb;
239 * Individual shutdown context for a particular peer.
241 struct PeerShutdownContext
244 * Pointer to the high level shutdown context.
246 struct ShutdownContext *shutdown_ctx;
249 * The daemon handle for the peer to shut down.
251 struct GNUNET_TESTING_Daemon *daemon;
255 * Individual shutdown context for a particular peer.
257 struct PeerRestartContext
260 * Pointer to the high level restart context.
262 struct ChurnRestartContext *churn_restart_ctx;
265 * The daemon handle for the peer to shut down.
267 struct GNUNET_TESTING_Daemon *daemon;
270 struct CreateTopologyContext
274 * Function to call with number of connections
276 GNUNET_TESTING_NotifyConnections cont;
279 * Closure for connection notification
286 /** Waiting to read number of peers */
289 /** Should find next peer index */
292 /** Should find colon */
295 /** Should read other peer index, space, or endline */
300 struct PeerConnection
305 struct PeerConnection *prev;
310 struct PeerConnection *next;
313 * Index of daemon in pg->peers
320 struct InternalStartContext
323 * Pointer to peerdata
325 struct PeerData *peer;
328 * Timeout for peer startup
330 struct GNUNET_TIME_Relative timeout;
333 * Client callback for hostkey notification
335 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
338 * Closure for hostkey_callback
343 * Client callback for peer start notification
345 GNUNET_TESTING_NotifyDaemonRunning start_cb;
353 * Hostname, where to start the peer
355 const char *hostname;
358 * Username to use when connecting to the
361 const char *username;
364 * Pointer to starting memory location of a hostkey
369 * Port to use for ssh.
375 struct ChurnRestartContext
378 * PeerGroup that we are working with.
380 struct GNUNET_TESTING_PeerGroup *pg;
383 * Number of restarts currently in flight.
385 unsigned int outstanding;
388 * Handle to the underlying churn context.
390 struct ChurnContext *churn_ctx;
393 * How long to allow the operation to take.
395 struct GNUNET_TIME_Relative timeout;
398 struct OutstandingSSH
400 struct OutstandingSSH *next;
402 struct OutstandingSSH *prev;
405 * Number of current ssh connections.
407 uint32_t outstanding;
410 * The hostname of this peer.
412 const char *hostname;
416 * Data we keep per peer.
421 * (Initial) configuration of the host.
422 * (initial because clients could change
423 * it and we would not know about those
426 struct GNUNET_CONFIGURATION_Handle *cfg;
429 * Handle for controlling the daemon.
431 struct GNUNET_TESTING_Daemon *daemon;
434 * The peergroup this peer belongs to.
436 struct GNUNET_TESTING_PeerGroup *pg;
440 * Linked list of allowed peer connections.
442 struct PeerConnection *allowed_peers_head;
445 * Linked list of allowed peer connections.
447 struct PeerConnection *allowed_peers_tail;
450 * Linked list of blacklisted peer connections.
452 struct PeerConnection *blacklisted_peers_head;
455 * Linked list of blacklisted peer connections.
457 struct PeerConnection *blacklisted_peers_tail;
460 * Linked list of connect peer connections.
462 struct PeerConnection *connect_peers_head;
465 * Linked list of connect peer connections.
467 struct PeerConnection *connect_peers_tail;
470 * Linked list of connect peer connections.
472 struct PeerConnection *connect_peers_working_set_head;
475 * Linked list of connect peer connections.
477 struct PeerConnection *connect_peers_working_set_tail;
481 * Hash map of allowed peer connections (F2F created topology)
483 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
486 * Hash map of blacklisted peers
488 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
491 * Hash map of peer connections
493 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
496 * Temporary hash map of peer connections
498 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
502 * Temporary variable for topology creation, should be reset before
503 * creating any topology so the count is valid once finished.
508 * Context to keep track of peers being started, to
509 * stagger hostkey generation and peer startup.
511 struct InternalStartContext internal_context;
515 * Linked list of per-host data.
525 * SSH username to use when connecting to this host.
530 * SSH port to use when connecting to this host.
535 * Lowest port that we have not yet used
541 struct TopologyIterateContext
544 * The peergroup we are working with.
546 struct GNUNET_TESTING_PeerGroup *pg;
549 * Callback for notifying of two connected peers.
551 GNUNET_TESTING_NotifyTopology topology_cb;
554 * Closure for topology_cb
559 * Number of peers currently connected to.
561 unsigned int connected;
564 * Number of peers we have finished iterating.
566 unsigned int completed;
569 * Number of peers total.
574 struct StatsIterateContext
577 * The peergroup that we are dealing with.
579 struct GNUNET_TESTING_PeerGroup *pg;
582 * Continuation to call once all stats information has been retrieved.
584 GNUNET_STATISTICS_Callback cont;
587 * Proc function to call on each value received.
589 GNUNET_TESTING_STATISTICS_Iterator proc;
592 * Closure for topology_cb
597 * Number of peers currently connected to.
599 unsigned int connected;
602 * Number of peers we have finished iterating.
604 unsigned int completed;
607 * Number of peers total.
615 struct GNUNET_TESTING_Daemon *daemon;
618 struct StatsCoreContext
621 struct GNUNET_TESTING_Daemon *daemon;
623 * Handle to the statistics service.
625 struct GNUNET_STATISTICS_Handle *stats_handle;
628 * Handle for getting statistics.
630 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
633 struct ConnectTopologyContext
636 * How many connections are left to create.
638 unsigned int remaining_connections;
641 * Handle to group of peers.
643 struct GNUNET_TESTING_PeerGroup *pg;
646 * How long to try this connection before timing out.
648 struct GNUNET_TIME_Relative connect_timeout;
651 * How many times to retry connecting the two peers.
653 unsigned int connect_attempts;
656 * Temp value set for each iteration.
658 //struct PeerData *first;
661 * Notification that all peers are connected.
663 GNUNET_TESTING_NotifyCompletion notify_connections_done;
666 * Closure for notify.
672 * Handle to a group of GNUnet peers.
674 struct GNUNET_TESTING_PeerGroup
677 * Configuration template.
679 const struct GNUNET_CONFIGURATION_Handle *cfg;
682 * Function to call on each started daemon.
684 //GNUNET_TESTING_NotifyDaemonRunning cb;
692 * Function to call on each topology connection created
694 GNUNET_TESTING_NotifyConnection notify_connection;
697 * Callback for notify_connection
699 void *notify_connection_cls;
702 * Array of information about hosts.
704 struct HostData *hosts;
707 * Number of hosts (size of HostData)
709 unsigned int num_hosts;
712 * Array of "total" peers.
714 struct PeerData *peers;
717 * Number of peers in this group.
722 * At what time should we fail the peer startup process?
724 struct GNUNET_TIME_Absolute max_timeout;
727 * How many peers are being started right now?
729 unsigned int starting;
732 * How many peers have already been started?
734 unsigned int started;
737 * Number of possible connections to peers
740 unsigned int max_outstanding_connections;
743 * Number of ssh connections to peers (max).
745 unsigned int max_concurrent_ssh;
748 * Number of connects we are waiting on, allows us to rate limit
751 unsigned int outstanding_connects;
754 * Number of HELLOs we have yet to send.
756 unsigned int remaining_hellos;
759 * How many connects have already been scheduled?
761 unsigned int total_connects_scheduled;
764 * Hostkeys loaded from a file.
769 * Head of DLL to keep track of the number of outstanding
770 * ssh connections per peer.
772 struct OutstandingSSH *ssh_head;
775 * Tail of DLL to keep track of the number of outstanding
776 * ssh connections per peer.
778 struct OutstandingSSH *ssh_tail;
781 * Stop scheduling peers connecting.
783 unsigned int stop_connects;
786 * Connection context for peer group.
788 struct ConnectTopologyContext ct_ctx;
794 * The altered configuration.
796 struct GNUNET_CONFIGURATION_Handle *ret;
799 * The original configuration to alter.
801 const struct GNUNET_CONFIGURATION_Handle *orig;
804 * The hostname that this peer will run on.
806 const char *hostname;
809 * The next possible port to assign.
814 * Unique number for unix domain sockets.
819 * Unique number for this peer/host to offset
820 * things that are grouped by host.
825 struct ConnectContext
828 * Index of peer to connect second to.
830 uint32_t first_index;
833 * Index of peer to connect first to.
835 uint32_t second_index;
838 * Higher level topology connection context.
840 struct ConnectTopologyContext *ct_ctx;
843 * Whether this connection has been accounted for in the schedule_connect call.
848 struct UnblacklistContext
853 struct GNUNET_TESTING_PeerGroup *pg;
856 * uid of the first peer
866 struct GNUNET_TESTING_PeerGroup *pg;
869 * uid of the first peer
874 * Peer data for first peer.
876 struct PeerData *first;
879 * Random percentage to use
884 struct MinimumContext
889 struct GNUNET_TESTING_PeerGroup *pg;
892 * uid of the first peer
897 * Peer data for first peer.
899 struct PeerData *first;
902 * Number of conns per peer
904 unsigned int num_to_add;
907 * Permuted array of all possible connections. Only add the Nth
908 * peer if it's in the Nth position.
910 unsigned int *pg_array;
913 * What number is the current element we are iterating over?
915 unsigned int current;
923 struct GNUNET_TESTING_PeerGroup *pg;
926 * uid of the first peer
931 * uid of the second peer
936 * Peer data for first peer.
938 struct PeerData *first;
941 * Which peer has been chosen as the one to add?
946 * What number is the current element we are iterating over?
948 unsigned int current;
952 * Simple struct to keep track of progress, and print a
953 * nice little percentage meter for long running tasks.
963 unsigned int completed;
967 char *startup_string;
972 * Convert unique ID to hash code.
974 * @param uid unique ID to convert
975 * @param hash set to uid (extended with zeros)
978 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
980 memset (hash, 0, sizeof (GNUNET_HashCode));
981 *((uint32_t *) hash) = uid;
985 * Convert hash code to unique ID.
987 * @param uid unique ID to convert
988 * @param hash set to uid (extended with zeros)
991 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
993 memcpy (uid, hash, sizeof (uint32_t));
998 static struct GNUNET_CORE_MessageHandler no_handlers[] =
1004 * Create a meter to keep track of the progress of some task.
1006 * @param total the total number of items to complete
1007 * @param start_string a string to prefix the meter with (if printing)
1008 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
1011 * @return the progress meter
1013 static struct ProgressMeter *
1014 create_meter(unsigned int total, char * start_string, int print)
1016 struct ProgressMeter *ret;
1017 ret = GNUNET_malloc(sizeof(struct ProgressMeter));
1020 ret->modnum = total / 4;
1021 if (ret->modnum == 0) /* Divide by zero check */
1023 ret->dotnum = (total / 50) + 1;
1024 if (start_string != NULL)
1025 ret->startup_string = GNUNET_strdup(start_string);
1027 ret->startup_string = GNUNET_strdup("");
1033 * Update progress meter (increment by one).
1035 * @param meter the meter to update and print info for
1037 * @return GNUNET_YES if called the total requested,
1038 * GNUNET_NO if more items expected
1041 update_meter(struct ProgressMeter *meter)
1043 if (meter->print == GNUNET_YES)
1045 if (meter->completed % meter->modnum == 0)
1047 if (meter->completed == 0)
1049 fprintf (stdout, "%sProgress: [0%%", meter->startup_string);
1052 fprintf (stdout, "%d%%", (int) (((float) meter->completed
1053 / meter->total) * 100));
1055 else if (meter->completed % meter->dotnum == 0)
1056 fprintf (stdout, ".");
1058 if (meter->completed + 1 == meter->total)
1059 fprintf (stdout, "%d%%]\n", 100);
1064 if (meter->completed == meter->total)
1066 if (meter->completed > meter->total)
1067 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
1072 * Reset progress meter.
1074 * @param meter the meter to reset
1076 * @return GNUNET_YES if meter reset,
1077 * GNUNET_SYSERR on error
1080 reset_meter(struct ProgressMeter *meter)
1083 return GNUNET_SYSERR;
1085 meter->completed = 0;
1090 * Release resources for meter
1092 * @param meter the meter to free
1095 free_meter(struct ProgressMeter *meter)
1097 GNUNET_free_non_null (meter->startup_string);
1098 GNUNET_free (meter);
1102 * Get a topology from a string input.
1104 * @param topology where to write the retrieved topology
1105 * @param topology_string The string to attempt to
1106 * get a configuration value from
1107 * @return GNUNET_YES if topology string matched a
1108 * known topology, GNUNET_NO if not
1111 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology,
1112 const char *topology_string)
1115 * Strings representing topologies in enum
1117 static const char *topology_strings[] =
1120 * A clique (everyone connected to everyone else).
1125 * Small-world network (2d torus plus random links).
1130 * Small-world network (ring plus random links).
1150 * Certain percentage of peers are unable to communicate directly
1151 * replicating NAT conditions
1156 * Scale free topology.
1161 * Straight line topology.
1166 * All peers are disconnected.
1171 * Read the topology from a file.
1178 if (topology_string == NULL)
1180 while (topology_strings[curr] != NULL)
1182 if (strcasecmp (topology_strings[curr], topology_string) == 0)
1189 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
1194 * Get connect topology option from string input.
1196 * @param topology_option where to write the retrieved topology
1197 * @param topology_string The string to attempt to
1198 * get a configuration value from
1199 * @return GNUNET_YES if string matched a known
1200 * topology option, GNUNET_NO if not
1203 GNUNET_TESTING_topology_option_get(
1204 enum GNUNET_TESTING_TopologyOption *topology_option,
1205 const char *topology_string)
1208 * Options for connecting a topology as strings.
1210 static const char *topology_option_strings[] =
1213 * Try to connect all peers specified in the topology.
1218 * Choose a random subset of connections to create.
1220 "CONNECT_RANDOM_SUBSET",
1223 * Create at least X connections for each peer.
1228 * Using a depth first search, create one connection
1229 * per peer. If any are missed (graph disconnected)
1230 * start over at those peers until all have at least one
1236 * Find the N closest peers to each allowed peer in the
1237 * topology and make sure a connection to those peers
1238 * exists in the connect topology.
1243 * No options specified.
1250 if (topology_string == NULL)
1252 while (NULL != topology_option_strings[curr])
1254 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1256 *topology_option = curr;
1261 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1266 * Function to iterate over options. Copies
1267 * the options to the target configuration,
1268 * updating PORT values as needed.
1270 * @param cls closure
1271 * @param section name of the section
1272 * @param option name of the option
1273 * @param value value of the option
1276 update_config(void *cls, const char *section, const char *option,
1279 struct UpdateContext *ctx = cls;
1283 char *single_variable;
1284 char *per_host_variable;
1285 unsigned long long num_per_host;
1287 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1288 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1290 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1292 if ((ival != 0) && (GNUNET_YES
1293 != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1296 GNUNET_snprintf (cval, sizeof(cval), "%u", ctx->nport++);
1299 else if ((ival != 0) && (GNUNET_YES
1300 == GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1302 && GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1306 GNUNET_snprintf (cval, sizeof(cval), "%u", ival + ctx->fdnum
1312 if (0 == strcmp (option, "UNIXPATH"))
1314 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig,
1318 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1319 section, ctx->upnum++);
1322 else if ((GNUNET_YES
1323 == GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1326 && (num_per_host > 0))
1329 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1330 section, ctx->fdnum % num_per_host);
1335 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1337 value = ctx->hostname;
1339 GNUNET_free (single_variable);
1340 GNUNET_free (per_host_variable);
1341 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1345 * Create a new configuration using the given configuration
1346 * as a template; however, each PORT in the existing cfg
1347 * must be renumbered by incrementing "*port". If we run
1348 * out of "*port" numbers, return NULL.
1350 * @param cfg template configuration
1351 * @param off the current peer offset
1352 * @param port port numbers to use, update to reflect
1353 * port numbers that were used
1354 * @param upnum number to make unix domain socket names unique
1355 * @param hostname hostname of the controlling host, to allow control connections from
1356 * @param fdnum number used to offset the unix domain socket for grouped processes
1357 * (such as statistics or peerinfo, which can be shared among others)
1359 * @return new configuration, NULL on error
1361 static struct GNUNET_CONFIGURATION_Handle *
1362 make_config(const struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t off,
1363 uint16_t * port, uint32_t * upnum, const char *hostname,
1366 struct UpdateContext uc;
1369 char *allowed_hosts;
1375 uc.ret = GNUNET_CONFIGURATION_create ();
1376 uc.hostname = hostname;
1379 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1380 if (uc.nport >= HIGH_PORT)
1383 GNUNET_CONFIGURATION_destroy (uc.ret);
1387 if (GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "control_host",
1388 &control_host) == GNUNET_OK)
1390 if (hostname != NULL)
1391 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1394 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1396 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1398 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1399 "ACCEPT_FROM", allowed_hosts);
1400 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1402 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1403 "ACCEPT_FROM", allowed_hosts);
1405 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH", "");
1406 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH",
1408 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH", "");
1409 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1412 GNUNET_free_non_null (control_host);
1413 GNUNET_free (allowed_hosts);
1416 /* arm needs to know to allow connections from the host on which it is running,
1417 * otherwise gnunet-arm is unable to connect to it in some instances */
1418 if (hostname != NULL)
1420 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1421 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1423 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1425 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1427 GNUNET_free (allowed_hosts);
1431 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1433 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1437 *port = (uint16_t) uc.nport;
1445 * Remove entries from the peer connection list
1447 * @param pg the peer group we are working with
1448 * @param first index of the first peer
1449 * @param second index of the second peer
1450 * @param list the peer list to use
1451 * @param check UNUSED
1453 * @return the number of connections added (can be 0, 1 or 2)
1457 remove_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1458 unsigned int second, enum PeerLists list, unsigned int check)
1462 struct PeerConnection **first_list;
1463 struct PeerConnection **second_list;
1464 struct PeerConnection *first_iter;
1465 struct PeerConnection *second_iter;
1466 struct PeerConnection **first_tail;
1467 struct PeerConnection **second_tail;
1470 GNUNET_HashCode hash_first;
1471 GNUNET_HashCode hash_second;
1473 hash_from_uid (first, &hash_first);
1474 hash_from_uid (second, &hash_second);
1482 first_list = &pg->peers[first].allowed_peers_head;
1483 second_list = &pg->peers[second].allowed_peers_head;
1484 first_tail = &pg->peers[first].allowed_peers_tail;
1485 second_tail = &pg->peers[second].allowed_peers_tail;
1488 first_list = &pg->peers[first].connect_peers_head;
1489 second_list = &pg->peers[second].connect_peers_head;
1490 first_tail = &pg->peers[first].connect_peers_tail;
1491 second_tail = &pg->peers[second].connect_peers_tail;
1494 first_list = &pg->peers[first].blacklisted_peers_head;
1495 second_list = &pg->peers[second].blacklisted_peers_head;
1496 first_tail = &pg->peers[first].blacklisted_peers_tail;
1497 second_tail = &pg->peers[second].blacklisted_peers_tail;
1500 first_list = &pg->peers[first].connect_peers_working_set_head;
1501 second_list = &pg->peers[second].connect_peers_working_set_head;
1502 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1503 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1510 first_iter = *first_list;
1511 while (first_iter != NULL)
1513 if (first_iter->index == second)
1515 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1516 GNUNET_free(first_iter);
1520 first_iter = first_iter->next;
1523 second_iter = *second_list;
1524 while (second_iter != NULL)
1526 if (second_iter->index == first)
1528 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1529 GNUNET_free(second_iter);
1533 second_iter = second_iter->next;
1537 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1540 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1545 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1548 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1557 * Add entries to the some list
1559 * @param pg the peer group we are working with
1560 * @param first index of the first peer
1561 * @param second index of the second peer
1562 * @param list the list type that we should modify
1563 * @param check GNUNET_YES to check lists before adding
1564 * GNUNET_NO to force add
1566 * @return the number of connections added (can be 0, 1 or 2)
1570 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1571 unsigned int second, enum PeerLists list, unsigned int check)
1577 struct PeerConnection **first_list;
1578 struct PeerConnection **second_list;
1579 struct PeerConnection *first_iter;
1580 struct PeerConnection *second_iter;
1581 struct PeerConnection *new_first;
1582 struct PeerConnection *new_second;
1583 struct PeerConnection **first_tail;
1584 struct PeerConnection **second_tail;
1589 first_list = &pg->peers[first].allowed_peers_head;
1590 second_list = &pg->peers[second].allowed_peers_head;
1591 first_tail = &pg->peers[first].allowed_peers_tail;
1592 second_tail = &pg->peers[second].allowed_peers_tail;
1595 first_list = &pg->peers[first].connect_peers_head;
1596 second_list = &pg->peers[second].connect_peers_head;
1597 first_tail = &pg->peers[first].connect_peers_tail;
1598 second_tail = &pg->peers[second].connect_peers_tail;
1601 first_list = &pg->peers[first].blacklisted_peers_head;
1602 second_list = &pg->peers[second].blacklisted_peers_head;
1603 first_tail = &pg->peers[first].blacklisted_peers_tail;
1604 second_tail = &pg->peers[second].blacklisted_peers_tail;
1607 first_list = &pg->peers[first].connect_peers_working_set_head;
1608 second_list = &pg->peers[second].connect_peers_working_set_head;
1609 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1610 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1617 add_first = GNUNET_YES;
1618 add_second = GNUNET_YES;
1620 if (check == GNUNET_YES)
1622 first_iter = *first_list;
1623 while (first_iter != NULL)
1625 if (first_iter->index == second)
1627 add_first = GNUNET_NO;
1630 first_iter = first_iter->next;
1633 second_iter = *second_list;
1634 while (second_iter != NULL)
1636 if (second_iter->index == first)
1638 add_second = GNUNET_NO;
1641 second_iter = second_iter->next;
1648 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1649 new_first->index = second;
1650 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1651 pg->peers[first].num_connections++;
1657 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1658 new_second->index = first;
1659 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1660 pg->peers[second].num_connections++;
1668 * Scale free network construction as described in:
1670 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1672 * Start with a network of "one" peer, then progressively add
1673 * peers up to the total number. At each step, iterate over
1674 * all possible peers and connect new peer based on number of
1675 * existing connections of the target peer.
1677 * @param pg the peer group we are dealing with
1678 * @param proc the connection processor to use
1679 * @param list the peer list to use
1681 * @return the number of connections created
1684 create_scale_free(struct GNUNET_TESTING_PeerGroup *pg,
1685 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1688 unsigned int total_connections;
1689 unsigned int outer_count;
1691 unsigned int previous_total_connections;
1695 GNUNET_assert (pg->total > 1);
1697 /* Add a connection between the first two nodes */
1698 total_connections = proc (pg, 0, 1, list, GNUNET_YES);
1700 for (outer_count = 1; outer_count < pg->total; outer_count++)
1702 previous_total_connections = total_connections;
1703 for (i = 0; i < outer_count; i++)
1705 probability = pg->peers[i].num_connections
1706 / (double) previous_total_connections;
1708 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1710 / ((double) UINT64_MAX);
1712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1713 "Considering connecting peer %d to peer %d\n",
1716 if (random < probability)
1719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1720 "Connecting peer %d to peer %d\n", outer_count, i);
1722 total_connections += proc (pg, outer_count, i, list, GNUNET_YES);
1727 return total_connections;
1731 * Create a topology given a peer group (set of running peers)
1732 * and a connection processor. Creates a small world topology
1733 * according to the rewired ring construction. The basic
1734 * behavior is that a ring topology is created, but with some
1735 * probability instead of connecting a peer to the next
1736 * neighbor in the ring a connection will be created to a peer
1737 * selected uniformly at random. We use the TESTING
1738 * PERCENTAGE option to specify what number of
1739 * connections each peer should have. Default is 2,
1740 * which makes the ring, any given number is multiplied by
1741 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1742 * each peer have on average 2logn connections. The additional
1743 * connections are made at increasing distance around the ring
1744 * from the original peer, or to random peers based on the re-
1745 * wiring probability. The TESTING
1746 * PROBABILITY option is used as the probability that a given
1747 * connection is rewired.
1749 * @param pg the peergroup to create the topology on
1750 * @param proc the connection processor to call to actually set
1751 * up connections between two peers
1752 * @param list the peer list to use
1754 * @return the number of connections that were set up
1758 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg,
1759 GNUNET_TESTING_ConnectionProcessor proc,
1760 enum PeerLists list)
1764 unsigned int natLog;
1765 unsigned int randomPeer;
1766 double random, logNModifier, probability;
1767 unsigned int smallWorldConnections;
1772 unsigned int useAnd;
1773 int connect_attempts;
1775 logNModifier = 0.5; /* FIXME: default value? */
1776 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1780 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1782 GNUNET_ERROR_TYPE_WARNING,
1784 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1785 p_string, "LOGNMODIFIER", "TESTING");
1786 GNUNET_free (p_string);
1788 probability = 0.5; /* FIXME: default percentage? */
1789 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1793 if (sscanf (p_string, "%lf", &probability) != 1)
1795 GNUNET_ERROR_TYPE_WARNING,
1797 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1798 p_string, "PERCENTAGE", "TESTING");
1799 GNUNET_free (p_string);
1801 natLog = log (pg->total);
1802 connsPerPeer = ceil (natLog * logNModifier);
1804 if (connsPerPeer % 2 == 1)
1807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."),
1810 smallWorldConnections = 0;
1811 connect_attempts = 0;
1812 for (i = 0; i < pg->total; i++)
1815 max = i + connsPerPeer / 2;
1816 min = i - connsPerPeer / 2;
1818 if (max > pg->total - 1)
1820 max = max - pg->total;
1826 min = pg->total - 1 + min;
1830 for (j = 0; j < connsPerPeer / 2; j++)
1833 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1835 / ((double) UINT64_MAX));
1836 if (random < probability)
1838 /* Connect to uniformly selected random peer */
1840 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1842 while ((((randomPeer < max) && (randomPeer > min)) && (useAnd
1843 == 0)) || (((randomPeer > min) || (randomPeer < max))
1847 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1850 smallWorldConnections += proc (pg, i, randomPeer, list,
1855 nodeToConnect = i + j + 1;
1856 if (nodeToConnect > pg->total - 1)
1858 nodeToConnect = nodeToConnect - pg->total;
1860 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
1866 connect_attempts += smallWorldConnections;
1868 return connect_attempts;
1872 * Create a topology given a peer group (set of running peers)
1873 * and a connection processor.
1875 * @param pg the peergroup to create the topology on
1876 * @param proc the connection processor to call to actually set
1877 * up connections between two peers
1878 * @param list the peer list to use
1880 * @return the number of connections that were set up
1884 create_nated_internet(struct GNUNET_TESTING_PeerGroup *pg,
1885 GNUNET_TESTING_ConnectionProcessor proc,
1886 enum PeerLists list)
1888 unsigned int outer_count, inner_count;
1889 unsigned int cutoff;
1890 int connect_attempts;
1891 double nat_percentage;
1894 nat_percentage = 0.6; /* FIXME: default percentage? */
1895 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1899 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1901 GNUNET_ERROR_TYPE_WARNING,
1903 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1904 p_string, "PERCENTAGE", "TESTING");
1905 GNUNET_free (p_string);
1908 cutoff = (unsigned int) (nat_percentage * pg->total);
1909 connect_attempts = 0;
1910 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1912 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1914 if ((outer_count > cutoff) || (inner_count > cutoff))
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1918 "Connecting peer %d to peer %d\n",
1919 outer_count, inner_count);
1921 connect_attempts += proc (pg, outer_count, inner_count, list,
1926 return connect_attempts;
1931 * Create a topology given a peer group (set of running peers)
1932 * and a connection processor.
1934 * @param pg the peergroup to create the topology on
1935 * @param proc the connection processor to call to actually set
1936 * up connections between two peers
1937 * @param list the peer list to use
1939 * @return the number of connections that were set up
1943 create_nated_internet_copy(struct GNUNET_TESTING_PeerGroup *pg,
1944 GNUNET_TESTING_ConnectionProcessor proc,
1945 enum PeerLists list)
1947 unsigned int outer_count, inner_count;
1948 unsigned int cutoff;
1949 int connect_attempts;
1950 double nat_percentage;
1953 struct ProgressMeter *conn_meter;
1955 nat_percentage = 0.6; /* FIXME: default percentage? */
1956 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1960 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1962 GNUNET_ERROR_TYPE_WARNING,
1964 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1965 p_string, "PERCENTAGE", "TESTING");
1966 GNUNET_free (p_string);
1969 cutoff = (unsigned int) (nat_percentage * pg->total);
1971 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1973 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1975 if ((outer_count > cutoff) || (inner_count > cutoff))
1981 conn_meter = create_meter (count, "NAT COPY", GNUNET_YES);
1982 connect_attempts = 0;
1983 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1985 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1987 if ((outer_count > cutoff) || (inner_count > cutoff))
1990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1991 "Connecting peer %d to peer %d\n",
1992 outer_count, inner_count);
1994 connect_attempts += proc (pg, outer_count, inner_count, list,
1996 add_connections (pg, outer_count, inner_count, ALLOWED, GNUNET_NO);
1997 update_meter (conn_meter);
2001 free_meter (conn_meter);
2003 return connect_attempts;
2008 * Create a topology given a peer group (set of running peers)
2009 * and a connection processor.
2011 * @param pg the peergroup to create the topology on
2012 * @param proc the connection processor to call to actually set
2013 * up connections between two peers
2014 * @param list the peer list to use
2016 * @return the number of connections that were set up
2020 create_small_world(struct GNUNET_TESTING_PeerGroup *pg,
2021 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2023 unsigned int i, j, k;
2024 unsigned int square;
2027 unsigned int toggle = 1;
2028 unsigned int nodeToConnect;
2029 unsigned int natLog;
2030 unsigned int node1Row;
2031 unsigned int node1Col;
2032 unsigned int node2Row;
2033 unsigned int node2Col;
2034 unsigned int distance;
2035 double probability, random, percentage;
2036 unsigned int smallWorldConnections;
2037 unsigned int small_world_it;
2039 int connect_attempts;
2040 square = floor (sqrt (pg->total));
2044 percentage = 0.5; /* FIXME: default percentage? */
2045 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2049 if (sscanf (p_string, "%lf", &percentage) != 1)
2051 GNUNET_ERROR_TYPE_WARNING,
2053 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2054 p_string, "PERCENTAGE", "TESTING");
2055 GNUNET_free (p_string);
2057 if (percentage < 0.0)
2060 GNUNET_ERROR_TYPE_WARNING,
2062 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
2063 "PERCENTAGE", "TESTING", percentage);
2066 probability = 0.5; /* FIXME: default percentage? */
2067 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2071 if (sscanf (p_string, "%lf", &probability) != 1)
2073 GNUNET_ERROR_TYPE_WARNING,
2075 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2076 p_string, "PROBABILITY", "TESTING");
2077 GNUNET_free (p_string);
2079 if (square * square != pg->total)
2081 while (rows * cols < pg->total)
2083 if (toggle % 2 == 0)
2092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2094 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2098 connect_attempts = 0;
2099 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2100 * to the node to its right and above. Once this is over, we'll have our torus!
2101 * Special case for the last node (if the rows and columns are not equal), connect
2102 * to the first in the row to maintain topology.
2104 for (i = 0; i < pg->total; i++)
2106 /* First connect to the node to the right */
2107 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2108 nodeToConnect = i + 1;
2109 else if (i + 1 == pg->total)
2110 nodeToConnect = rows * cols - cols;
2112 nodeToConnect = i - cols + 1;
2114 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2118 nodeToConnect = (rows * cols) - cols + i;
2119 if (nodeToConnect >= pg->total)
2120 nodeToConnect -= cols;
2123 nodeToConnect = i - cols;
2125 if (nodeToConnect < pg->total)
2126 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2128 natLog = log (pg->total);
2129 #if VERBOSE_TESTING > 2
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2131 _("natural log of %d is %d, will run %d iterations\n"),
2132 pg->total, natLog, (int) (natLog * percentage));
2133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2134 _("Total connections added thus far: %u!\n"), connect_attempts);
2136 smallWorldConnections = 0;
2137 small_world_it = (unsigned int) (natLog * percentage);
2138 if (small_world_it < 1)
2140 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
2141 for (i = 0; i < small_world_it; i++)
2143 for (j = 0; j < pg->total; j++)
2145 /* Determine the row and column of node at position j on the 2d torus */
2146 node1Row = j / cols;
2147 node1Col = j - (node1Row * cols);
2148 for (k = 0; k < pg->total; k++)
2150 /* Determine the row and column of node at position k on the 2d torus */
2151 node2Row = k / cols;
2152 node2Col = k - (node2Row * cols);
2153 /* Simple Cartesian distance */
2154 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
2157 /* Calculate probability as 1 over the square of the distance */
2158 probability = 1.0 / (distance * distance);
2159 /* Choose a random value between 0 and 1 */
2161 = ((double) GNUNET_CRYPTO_random_u64 (
2162 GNUNET_CRYPTO_QUALITY_WEAK,
2164 / ((double) UINT64_MAX);
2165 /* If random < probability, then connect the two nodes */
2166 if (random < probability)
2167 smallWorldConnections += proc (pg, j, k, list, GNUNET_YES);
2173 connect_attempts += smallWorldConnections;
2174 #if VERBOSE_TESTING > 2
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2176 _("Total connections added for small world: %d!\n"),
2177 smallWorldConnections);
2179 return connect_attempts;
2183 * Create a topology given a peer group (set of running peers)
2184 * and a connection processor.
2186 * @param pg the peergroup to create the topology on
2187 * @param proc the connection processor to call to actually set
2188 * up connections between two peers
2189 * @param list the peer list to use
2191 * @return the number of connections that were set up
2195 create_erdos_renyi(struct GNUNET_TESTING_PeerGroup *pg,
2196 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2199 unsigned int outer_count;
2200 unsigned int inner_count;
2201 int connect_attempts;
2205 probability = 0.5; /* FIXME: default percentage? */
2206 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2210 if (sscanf (p_string, "%lf", &probability) != 1)
2212 GNUNET_ERROR_TYPE_WARNING,
2214 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2215 p_string, "PROBABILITY", "TESTING");
2216 GNUNET_free (p_string);
2218 connect_attempts = 0;
2219 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2221 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2224 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2226 / ((double) UINT64_MAX);
2228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2229 _("rand is %f probability is %f\n"), temp_rand,
2232 if (temp_rand < probability)
2234 connect_attempts += proc (pg, outer_count, inner_count, list,
2240 return connect_attempts;
2244 * Create a topology given a peer group (set of running peers)
2245 * and a connection processor. This particular function creates
2246 * the connections for a 2d-torus, plus additional "closest"
2247 * connections per peer.
2249 * @param pg the peergroup to create the topology on
2250 * @param proc the connection processor to call to actually set
2251 * up connections between two peers
2252 * @param list the peer list to use
2254 * @return the number of connections that were set up
2258 create_2d_torus(struct GNUNET_TESTING_PeerGroup *pg,
2259 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2262 unsigned int square;
2265 unsigned int toggle = 1;
2266 unsigned int nodeToConnect;
2267 int connect_attempts;
2269 connect_attempts = 0;
2271 square = floor (sqrt (pg->total));
2275 if (square * square != pg->total)
2277 while (rows * cols < pg->total)
2279 if (toggle % 2 == 0)
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2290 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2293 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2294 * to the node to its right and above. Once this is over, we'll have our torus!
2295 * Special case for the last node (if the rows and columns are not equal), connect
2296 * to the first in the row to maintain topology.
2298 for (i = 0; i < pg->total; i++)
2300 /* First connect to the node to the right */
2301 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2302 nodeToConnect = i + 1;
2303 else if (i + 1 == pg->total)
2304 nodeToConnect = rows * cols - cols;
2306 nodeToConnect = i - cols + 1;
2308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2309 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2311 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2313 /* Second connect to the node immediately above */
2316 nodeToConnect = (rows * cols) - cols + i;
2317 if (nodeToConnect >= pg->total)
2318 nodeToConnect -= cols;
2321 nodeToConnect = i - cols;
2323 if (nodeToConnect < pg->total)
2326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2327 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2329 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2334 return connect_attempts;
2338 * Create a topology given a peer group (set of running peers)
2339 * and a connection processor.
2341 * @param pg the peergroup to create the topology on
2342 * @param proc the connection processor to call to actually set
2343 * up connections between two peers
2344 * @param list the peer list to use
2345 * @param check does the connection processor need to check before
2346 * performing an action on the list?
2348 * @return the number of connections that were set up
2352 create_clique(struct GNUNET_TESTING_PeerGroup *pg,
2353 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list,
2356 unsigned int outer_count;
2357 unsigned int inner_count;
2358 int connect_attempts;
2359 struct ProgressMeter *conn_meter;
2360 connect_attempts = 0;
2362 conn_meter = create_meter ((((pg->total * pg->total) + pg->total) / 2)
2363 - pg->total, "Create Clique ", GNUNET_YES);
2364 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2366 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2370 "Connecting peer %d to peer %d\n",
2371 outer_count, inner_count);
2373 connect_attempts += proc (pg, outer_count, inner_count, list, check);
2374 update_meter (conn_meter);
2377 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Meter has %d left\n",
2378 conn_meter->total - conn_meter->completed);
2379 reset_meter (conn_meter);
2380 free_meter (conn_meter);
2381 return connect_attempts;
2386 * Iterator over hash map entries.
2388 * @param cls closure the peer group
2389 * @param key the key stored in the hashmap is the
2390 * index of the peer to connect to
2391 * @param value value in the hash map, handle to the peer daemon
2392 * @return GNUNET_YES if we should continue to
2397 unblacklist_iterator (void *cls,
2398 const GNUNET_HashCode * key,
2401 struct UnblacklistContext *un_ctx = cls;
2402 uint32_t second_pos;
2404 uid_from_hash (key, &second_pos);
2406 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2413 * Create a blacklist topology based on the allowed topology
2414 * which disallows any connections not in the allowed topology
2415 * at the transport level.
2417 * @param pg the peergroup to create the topology on
2418 * @param proc the connection processor to call to allow
2419 * up connections between two peers
2421 * @return the number of connections that were set up
2425 copy_allowed(struct GNUNET_TESTING_PeerGroup *pg,
2426 GNUNET_TESTING_ConnectionProcessor proc)
2428 struct UnblacklistContext un_ctx;
2431 struct PeerConnection *iter;
2435 for (count = 0; count < pg->total - 1; count++)
2437 un_ctx.first_uid = count;
2439 iter = pg->peers[count].allowed_peers_head;
2440 while (iter != NULL)
2442 remove_connections (pg, count, iter->index, BLACKLIST, GNUNET_YES);
2443 //unblacklist_connections(pg, count, iter->index);
2447 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers, &unblacklist_iterator, &un_ctx);
2450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2455 * Create a topology given a peer group (set of running peers)
2456 * and a connection processor.
2458 * @param pg the peergroup to create the topology on
2459 * @param proc the connection processor to call to actually set
2460 * up connections between two peers
2461 * @param list which list should be modified
2463 * @return the number of connections that were set up
2467 create_line(struct GNUNET_TESTING_PeerGroup *pg,
2468 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2471 int connect_attempts;
2473 connect_attempts = 0;
2475 /* Connect each peer to the next highest numbered peer */
2476 for (count = 0; count < pg->total - 1; count++)
2479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2480 "Connecting peer %d to peer %d\n", count, count + 1);
2482 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2485 return connect_attempts;
2489 * Create a topology given a peer group (set of running peers)
2490 * and a connection processor.
2492 * @param pg the peergroup to create the topology on
2493 * @param filename the file to read topology information from
2494 * @param proc the connection processor to call to actually set
2495 * up connections between two peers
2496 * @param list the peer list to use
2498 * @return the number of connections that were set up
2502 create_from_file(struct GNUNET_TESTING_PeerGroup *pg, char *filename,
2503 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2505 int connect_attempts;
2506 unsigned int first_peer_index;
2507 unsigned int second_peer_index;
2508 connect_attempts = 0;
2513 unsigned int total_peers;
2515 enum States curr_state;
2517 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2518 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2520 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2522 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2523 "Could not open file `%s' specified for topology!", filename);
2524 return connect_attempts;
2527 data = GNUNET_malloc_large (frstat.st_size);
2528 GNUNET_assert(data != NULL);
2529 if (frstat.st_size != GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2532 GNUNET_ERROR_TYPE_ERROR,
2533 "Could not read file %s specified for host list, ending test!",
2536 return connect_attempts;
2541 first_peer_index = 0;
2542 /* First line should contain a single integer, specifying the number of peers */
2543 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2544 curr_state = NUM_PEERS;
2545 while (count < frstat.st_size - 1)
2547 if ((buf[count] == '\n') || (buf[count] == ' '))
2557 total_peers = strtoul(&buf[count], NULL, 10);
2560 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2561 "Failed to read number of peers from topology file!\n");
2562 GNUNET_free_non_null(data);
2563 return connect_attempts;
2565 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2566 "Read %u total peers in topology\n", total_peers);
2567 GNUNET_assert(total_peers == pg->total);
2568 curr_state = PEER_INDEX;
2569 while ((buf[count] != '\n') && (count < frstat.st_size - 1))
2575 first_peer_index = strtoul(&buf[count], NULL, 10);
2578 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2579 "Failed to read peer index from topology file!\n");
2580 GNUNET_free_non_null(data);
2581 return connect_attempts;
2583 while ((buf[count] != ':') && (count < frstat.st_size - 1))
2586 curr_state = OTHER_PEER_INDEX;
2589 if (1 == sscanf (&buf[count], ":"))
2590 curr_state = OTHER_PEER_INDEX;
2593 case OTHER_PEER_INDEX:
2595 second_peer_index = strtoul(&buf[count], NULL, 10);
2598 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2599 "Failed to peer index from topology file!\n");
2600 GNUNET_free_non_null(data);
2601 return connect_attempts;
2603 /* Assume file is written with first peer 1, but array index is 0 */
2604 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index
2605 - 1, list, GNUNET_YES);
2606 while ((buf[count] != '\n') && (buf[count] != ',') && (count
2607 < frstat.st_size - 1))
2609 if (buf[count] == '\n')
2611 curr_state = PEER_INDEX;
2613 else if (buf[count] != ',')
2615 curr_state = OTHER_PEER_INDEX;
2620 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2621 "Found bad data in topology file while in state %d!\n",
2624 return connect_attempts;
2628 return connect_attempts;
2632 * Create a topology given a peer group (set of running peers)
2633 * and a connection processor.
2635 * @param pg the peergroup to create the topology on
2636 * @param proc the connection processor to call to actually set
2637 * up connections between two peers
2638 * @param list the peer list to use
2640 * @return the number of connections that were set up
2644 create_ring(struct GNUNET_TESTING_PeerGroup *pg,
2645 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2648 int connect_attempts;
2650 connect_attempts = 0;
2652 /* Connect each peer to the next highest numbered peer */
2653 for (count = 0; count < pg->total - 1; count++)
2656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2657 "Connecting peer %d to peer %d\n", count, count + 1);
2659 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2662 /* Connect the last peer to the first peer */
2663 connect_attempts += proc (pg, pg->total - 1, 0, list, GNUNET_YES);
2665 return connect_attempts;
2670 * Iterator for writing friends of a peer to a file.
2672 * @param cls closure, an open writable file handle
2673 * @param key the key the daemon was stored under
2674 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2676 * @return GNUNET_YES to continue iteration
2678 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2679 * with a single file_iterator that takes a closure which contains
2680 * the prefix to write before the peer. Then this could be used
2681 * for blacklisting multiple transports and writing the friend
2682 * file. I'm sure *someone* will complain loudly about other
2683 * things that negate these functions even existing so no point in
2687 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2689 FILE *temp_friend_handle = cls;
2690 struct GNUNET_TESTING_Daemon *peer = value;
2691 struct GNUNET_PeerIdentity *temppeer;
2692 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2694 temppeer = &peer->id;
2695 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2696 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2701 struct BlacklistContext
2704 * The (open) file handle to write to
2706 FILE *temp_file_handle;
2709 * The transport that this peer will be blacklisted on.
2715 * Iterator for writing blacklist data to appropriate files.
2717 * @param cls closure, an open writable file handle
2718 * @param key the key the daemon was stored under
2719 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2721 * @return GNUNET_YES to continue iteration
2724 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2726 struct BlacklistContext *blacklist_ctx = cls;
2727 struct GNUNET_TESTING_Daemon *peer = value;
2728 struct GNUNET_PeerIdentity *temppeer;
2729 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2731 temppeer = &peer->id;
2732 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2733 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2734 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2735 blacklist_ctx->transport, (char *) &peer_enc);
2742 * Create the friend files based on the PeerConnection's
2743 * of each peer in the peer group, and copy the files
2744 * to the appropriate place
2746 * @param pg the peer group we are dealing with
2749 create_and_copy_friend_files(struct GNUNET_TESTING_PeerGroup *pg)
2751 FILE *temp_friend_handle;
2752 unsigned int pg_iter;
2753 char *temp_service_path;
2754 struct GNUNET_OS_Process **procarr;
2758 enum GNUNET_OS_ProcessStatusType type;
2759 unsigned long return_code;
2767 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2768 struct PeerConnection *conn_iter;
2770 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2771 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2773 mytemp = GNUNET_DISK_mktemp ("friends");
2774 GNUNET_assert (mytemp != NULL);
2775 temp_friend_handle = fopen (mytemp, "wt");
2776 GNUNET_assert (temp_friend_handle != NULL);
2778 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2779 while (conn_iter != NULL)
2781 GNUNET_CRYPTO_hash_to_enc (
2782 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2784 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2785 conn_iter = conn_iter->next;
2788 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2789 &friend_file_iterator,
2790 temp_friend_handle);
2792 fclose (temp_friend_handle);
2795 != GNUNET_CONFIGURATION_get_value_string (
2796 pg->peers[pg_iter]. daemon->cfg,
2797 "PATHS", "SERVICEHOME",
2798 &temp_service_path))
2801 GNUNET_ERROR_TYPE_WARNING,
2803 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2804 "SERVICEHOME", "PATHS");
2805 if (UNLINK (mytemp) != 0)
2806 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2808 GNUNET_free (mytemp);
2812 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2814 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2815 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
2818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2819 _("Copying file with command cp %s %s\n"), mytemp, arg);
2821 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2822 GNUNET_OS_process_close (procarr[pg_iter]);
2825 else /* Remote, scp the file to the correct place */
2827 if (NULL != pg->peers[pg_iter].daemon->username)
2828 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2829 pg->peers[pg_iter].daemon->username,
2830 pg->peers[pg_iter].daemon->hostname,
2833 GNUNET_asprintf (&arg, "%s:%s/friends",
2834 pg->peers[pg_iter].daemon->hostname,
2836 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
2839 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2840 GNUNET_OS_process_close (procarr[pg_iter]);
2841 if (ret != GNUNET_OK)
2843 procarr[pg_iter] = NULL;
2845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2846 _("Copying file with command scp %s %s\n"), mytemp,
2851 GNUNET_free (temp_service_path);
2852 GNUNET_free (mytemp);
2857 ret = GNUNET_SYSERR;
2858 while ((count < max_wait) && (ret != GNUNET_OK))
2861 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2865 _("Checking copy status of file %d\n"), pg_iter);
2867 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2869 if (GNUNET_OS_process_status
2870 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2872 ret = GNUNET_SYSERR;
2874 else if ((type != GNUNET_OS_PROCESS_EXITED)
2875 || (return_code != 0))
2877 ret = GNUNET_SYSERR;
2881 GNUNET_OS_process_close (procarr[pg_iter]);
2882 procarr[pg_iter] = NULL;
2884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2885 _("File %d copied\n"), pg_iter);
2891 if (ret == GNUNET_SYSERR)
2893 /* FIXME: why sleep here? -CG */
2899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2900 _("Finished copying all friend files!\n"));
2903 GNUNET_free (procarr);
2908 * Create the blacklist files based on the PeerConnection's
2909 * of each peer in the peer group, and copy the files
2910 * to the appropriate place.
2912 * @param pg the peer group we are dealing with
2913 * @param transports space delimited list of transports to blacklist
2916 create_and_copy_blacklist_files(struct GNUNET_TESTING_PeerGroup *pg,
2917 const char *transports)
2919 FILE *temp_file_handle;
2920 unsigned int pg_iter;
2921 char *temp_service_path;
2922 struct GNUNET_OS_Process **procarr;
2925 enum GNUNET_OS_ProcessStatusType type;
2926 unsigned long return_code;
2933 char *temp_transports;
2936 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2937 struct PeerConnection *conn_iter;
2939 static struct BlacklistContext blacklist_ctx;
2942 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2943 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2945 mytemp = GNUNET_DISK_mktemp ("blacklist");
2946 GNUNET_assert (mytemp != NULL);
2947 temp_file_handle = fopen (mytemp, "wt");
2948 GNUNET_assert (temp_file_handle != NULL);
2949 temp_transports = GNUNET_strdup (transports);
2951 blacklist_ctx.temp_file_handle = temp_file_handle;
2953 transport_len = strlen (temp_transports) + 1;
2956 for (i = 0; i < transport_len; i++)
2958 if ((temp_transports[i] == ' ') && (pos == NULL))
2959 continue; /* At start of string (whitespace) */
2960 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2962 temp_transports[i] = '\0';
2964 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
2965 while (conn_iter != NULL)
2967 GNUNET_CRYPTO_hash_to_enc (
2968 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2970 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
2971 conn_iter = conn_iter->next;
2975 blacklist_ctx.transport = pos;
2976 entry_count = GNUNET_CONTAINER_multihashmap_iterate (pg->
2978 [pg_iter].blacklisted_peers,
2979 &blacklist_file_iterator,
2983 } /* At beginning of actual string */
2984 else if (pos == NULL)
2986 pos = &temp_transports[i];
2990 GNUNET_free (temp_transports);
2991 fclose (temp_file_handle);
2994 != GNUNET_CONFIGURATION_get_value_string (
2995 pg->peers[pg_iter]. daemon->cfg,
2996 "PATHS", "SERVICEHOME",
2997 &temp_service_path))
3000 GNUNET_ERROR_TYPE_WARNING,
3002 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
3003 "SERVICEHOME", "PATHS");
3004 if (UNLINK (mytemp) != 0)
3005 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
3007 GNUNET_free (mytemp);
3011 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
3013 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
3014 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
3017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3018 _("Copying file with command cp %s %s\n"), mytemp, arg);
3023 else /* Remote, scp the file to the correct place */
3025 if (NULL != pg->peers[pg_iter].daemon->username)
3026 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
3027 pg->peers[pg_iter].daemon->username,
3028 pg->peers[pg_iter].daemon->hostname,
3031 GNUNET_asprintf (&arg, "%s:%s/blacklist",
3032 pg->peers[pg_iter].daemon->hostname,
3034 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
3037 GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
3040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3041 _("Copying file with command scp %s %s\n"), mytemp,
3046 GNUNET_free (temp_service_path);
3047 GNUNET_free (mytemp);
3051 ret = GNUNET_SYSERR;
3052 while ((count < max_wait) && (ret != GNUNET_OK))
3055 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3059 _("Checking copy status of file %d\n"), pg_iter);
3061 if (procarr[pg_iter] != NULL) /* Check for already completed! */
3063 if (GNUNET_OS_process_status (procarr[pg_iter], &type,
3064 &return_code) != GNUNET_OK)
3066 ret = GNUNET_SYSERR;
3068 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
3070 ret = GNUNET_SYSERR;
3074 GNUNET_OS_process_close (procarr[pg_iter]);
3075 procarr[pg_iter] = NULL;
3077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3078 _("File %d copied\n"), pg_iter);
3084 if (ret == GNUNET_SYSERR)
3086 /* FIXME: why sleep here? -CG */
3092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3093 _("Finished copying all blacklist files!\n"));
3095 GNUNET_free (procarr);
3099 /* Forward Declaration */
3101 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3104 * Choose a random peer's next connection to create, and
3105 * call schedule_connect to set up the connect task.
3107 * @param ct_ctx the overall connection context
3110 preschedule_connect(struct GNUNET_TESTING_PeerGroup *pg)
3112 struct ConnectTopologyContext *ct_ctx = &pg->ct_ctx;
3113 struct PeerConnection *connection_iter;
3114 struct ConnectContext *connect_context;
3115 uint32_t random_peer;
3117 if (ct_ctx->remaining_connections == 0)
3120 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3121 while (pg->peers[random_peer].connect_peers_head == NULL)
3122 random_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3125 connection_iter = pg->peers[random_peer].connect_peers_head;
3126 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3127 connect_context->first_index = random_peer;
3128 connect_context->second_index = connection_iter->index;
3129 connect_context->ct_ctx = ct_ctx;
3130 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3131 GNUNET_CONTAINER_DLL_remove(pg->peers[random_peer].connect_peers_head, pg->peers[random_peer].connect_peers_tail, connection_iter);
3132 GNUNET_free(connection_iter);
3133 ct_ctx->remaining_connections--;
3137 /* Forward declaration */
3138 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3141 * Close connections and free the hello context.
3143 * @param cls the 'struct SendHelloContext *'
3144 * @param tc scheduler context
3147 free_hello_context (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3149 struct SendHelloContext *send_hello_context = cls;
3150 if (send_hello_context->peer->daemon->server != NULL)
3152 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3153 send_hello_context->peer->daemon->server = NULL;
3155 if (send_hello_context->peer->daemon->th != NULL)
3157 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3158 send_hello_context->peer->daemon->th = NULL;
3160 if (send_hello_context->core_connect_task != GNUNET_SCHEDULER_NO_TASK)
3162 GNUNET_SCHEDULER_cancel(send_hello_context->core_connect_task);
3163 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3165 send_hello_context->pg->outstanding_connects--;
3166 GNUNET_free(send_hello_context);
3170 * For peers that haven't yet connected, notify
3171 * the caller that they have failed (timeout).
3173 * @param cls the 'struct SendHelloContext *'
3174 * @param tc scheduler context
3177 notify_remaining_connections_failed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3179 struct SendHelloContext *send_hello_context = cls;
3180 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3181 struct PeerConnection *connection;
3183 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3184 send_hello_context->peer->daemon->server = NULL;
3186 connection = send_hello_context->peer->connect_peers_head;
3188 while (connection != NULL)
3190 if (pg->notify_connection != NULL)
3192 pg->notify_connection(pg->notify_connection_cls,
3193 &send_hello_context->peer->daemon->id,
3194 &pg->peers[connection->index].daemon->id,
3196 send_hello_context->peer->daemon->cfg,
3197 pg->peers[connection->index].daemon->cfg,
3198 send_hello_context->peer->daemon,
3199 pg->peers[connection->index].daemon,
3200 "Peers failed to connect (timeout)");
3202 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3203 GNUNET_free(connection);
3204 connection = connection->next;
3206 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3208 other_peer = &pg->peers[connection->index];
3213 * For peers that haven't yet connected, send
3214 * CORE connect requests.
3216 * @param cls the 'struct SendHelloContext *'
3217 * @param tc scheduler context
3220 send_core_connect_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3222 struct SendHelloContext *send_hello_context = cls;
3223 struct PeerConnection *conn;
3224 GNUNET_assert(send_hello_context->peer->daemon->server != NULL);
3226 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3228 send_hello_context->connect_attempts++;
3229 if (send_hello_context->connect_attempts < send_hello_context->pg->ct_ctx.connect_attempts)
3231 conn = send_hello_context->peer->connect_peers_head;
3232 while (conn != NULL)
3234 GNUNET_CORE_peer_request_connect(send_hello_context->peer->daemon->server,
3235 GNUNET_TIME_relative_get_forever(),
3236 &send_hello_context->pg->peers[conn->index].daemon->id,
3241 send_hello_context->core_connect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_divide(send_hello_context->pg->ct_ctx.connect_timeout, send_hello_context->pg->ct_ctx.connect_attempts) ,
3242 &send_core_connect_requests,
3243 send_hello_context);
3247 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Timeout before all connections created, marking rest as failed!\n");
3248 GNUNET_SCHEDULER_add_now(¬ify_remaining_connections_failed, send_hello_context);
3254 * Success, connection is up. Signal client our success.
3256 * @param cls our "struct SendHelloContext"
3257 * @param peer identity of the peer that has connected
3258 * @param atsi performance information
3260 * FIXME: remove peers from BOTH lists, call notify twice, should
3261 * double the speed of connections as long as the list iteration
3262 * doesn't take too long!
3265 core_connect_notify (void *cls,
3266 const struct GNUNET_PeerIdentity *peer,
3267 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3269 struct SendHelloContext *send_hello_context = cls;
3270 struct PeerConnection *connection;
3271 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3273 struct PeerData *other_peer;
3276 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3277 "Connected peer %s to peer %s\n",
3278 ctx->d1->shortname, GNUNET_i2s(peer));
3281 if (0 == memcmp(&send_hello_context->peer->daemon->id, peer, sizeof(struct GNUNET_PeerIdentity)))
3284 connection = send_hello_context->peer->connect_peers_head;
3289 while ((connection != NULL) &&
3290 (0 != memcmp(&pg->peers[connection->index].daemon->id, peer, sizeof(struct GNUNET_PeerIdentity))))
3292 connection = connection->next;
3295 if (connection == NULL)
3297 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Connected peer %s to %s, not in list (no problem(?))\n", GNUNET_i2s(peer), send_hello_context->peer->daemon->shortname);
3302 other_peer = &pg->peers[connection->index];
3304 if (pg->notify_connection != NULL)
3306 pg->notify_connection(pg->notify_connection_cls,
3307 &send_hello_context->peer->daemon->id,
3310 send_hello_context->peer->daemon->cfg,
3311 pg->peers[connection->index].daemon->cfg,
3312 send_hello_context->peer->daemon,
3313 pg->peers[connection->index].daemon,
3316 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3317 GNUNET_free(connection);
3321 /* Notify of reverse connection and remove from other peers list of outstanding */
3322 if (other_peer != NULL)
3324 connection = other_peer->connect_peers_head;
3325 while ((connection != NULL) &&
3326 (0 != memcmp(&send_hello_context->peer->daemon->id, &pg->peers[connection->index].daemon->id, sizeof(struct GNUNET_PeerIdentity))))
3328 connection = connection->next;
3330 if (connection != NULL)
3332 if (pg->notify_connection != NULL)
3334 pg->notify_connection(pg->notify_connection_cls,
3336 &send_hello_context->peer->daemon->id,
3338 pg->peers[connection->index].daemon->cfg,
3339 send_hello_context->peer->daemon->cfg,
3340 pg->peers[connection->index].daemon,
3341 send_hello_context->peer->daemon,
3345 GNUNET_CONTAINER_DLL_remove(other_peer->connect_peers_head, other_peer->connect_peers_tail, connection);
3346 GNUNET_free(connection);
3351 if (send_hello_context->peer->connect_peers_head == NULL)
3353 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3358 * Notify of a successful connection to the core service.
3360 * @param cls a struct SendHelloContext *
3361 * @param server handle to the core service
3362 * @param my_identity the peer identity of this peer
3363 * @param publicKey the public key of the peer
3366 core_init (void *cls,
3367 struct GNUNET_CORE_Handle * server,
3368 const struct GNUNET_PeerIdentity *
3371 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
3374 struct SendHelloContext *send_hello_context = cls;
3375 send_hello_context->core_ready = GNUNET_YES;
3379 * Function called once a hello has been sent
3380 * to the transport, move on to the next one
3381 * or go away forever.
3383 * @param cls the 'struct SendHelloContext *'
3384 * @param tc scheduler context
3387 hello_sent_callback (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3389 struct SendHelloContext *send_hello_context = cls;
3390 //unsigned int pg_iter;
3391 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3393 GNUNET_free(send_hello_context);
3397 send_hello_context->pg->remaining_hellos--;
3399 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sent HELLO, have %d remaining!\n", send_hello_context->pg->remaining_hellos);
3401 if (send_hello_context->peer_pos == NULL) /* All HELLOs (for this peer!) have been transmitted! */
3404 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All hellos for this peer sent, disconnecting transport!\n");
3406 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3407 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3408 send_hello_context->peer->daemon->th = NULL;
3410 /*if (send_hello_context->pg->remaining_hellos == 0)
3412 for (pg_iter = 0; pg_iter < send_hello_context->pg->max_outstanding_connections; pg_iter++)
3414 preschedule_connect(&send_hello_context->pg->ct_ctx);
3418 GNUNET_assert (send_hello_context->peer->daemon->server == NULL);
3419 send_hello_context->peer->daemon->server = GNUNET_CORE_connect(send_hello_context->peer->cfg,
3423 &core_connect_notify,
3430 send_hello_context->core_connect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_divide(send_hello_context->pg->ct_ctx.connect_timeout, send_hello_context->pg->ct_ctx.connect_attempts),
3431 &send_core_connect_requests,
3432 send_hello_context);
3435 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3439 * Connect to a peer, give it all the HELLO's of those peers
3440 * we will later ask it to connect to.
3442 * @param ct_ctx the overall connection context
3444 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3446 struct SendHelloContext *send_hello_context = cls;
3447 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3449 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3451 GNUNET_free(send_hello_context);
3455 GNUNET_assert(send_hello_context->peer_pos != NULL); /* All of the HELLO sends to be scheduled have been scheduled! */
3457 if (((send_hello_context->peer->daemon->th == NULL) &&
3458 (pg->outstanding_connects > pg->max_outstanding_connections)) ||
3459 (pg->stop_connects == GNUNET_YES))
3461 #if VERBOSE_TESTING > 2
3462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3464 ("Delaying connect, we have too many outstanding connections!\n"));
3466 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3467 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3468 &schedule_send_hellos, send_hello_context);
3472 #if VERBOSE_TESTING > 2
3473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3474 _("Creating connection, outstanding_connections is %d\n"),
3475 outstanding_connects);
3477 if (send_hello_context->peer->daemon->th == NULL)
3479 pg->outstanding_connects++; /* Actual TRANSPORT, CORE connections! */
3480 send_hello_context->peer->daemon->th
3481 = GNUNET_TRANSPORT_connect (send_hello_context->peer->cfg, NULL,
3482 send_hello_context, NULL, NULL, NULL);
3485 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3486 _("Offering Hello of peer %s to peer %s\n"),
3487 send_hello_context->peer->daemon->shortname, pg->peers[send_hello_context->peer_pos->index].daemon->shortname);
3489 GNUNET_TRANSPORT_offer_hello(send_hello_context->peer->daemon->th,
3490 (const struct GNUNET_MessageHeader *)pg->peers[send_hello_context->peer_pos->index].daemon->hello,
3491 &hello_sent_callback,
3492 send_hello_context);
3493 send_hello_context->peer_pos = send_hello_context->peer_pos->next;
3494 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3500 * Internal notification of a connection, kept so that we can ensure some connections
3501 * happen instead of flooding all testing daemons with requests to connect.
3504 internal_connect_notify(void *cls, const struct GNUNET_PeerIdentity *first,
3505 const struct GNUNET_PeerIdentity *second,
3507 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
3508 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
3509 struct GNUNET_TESTING_Daemon *first_daemon,
3510 struct GNUNET_TESTING_Daemon *second_daemon,
3513 struct ConnectContext *connect_ctx = cls;
3514 struct ConnectTopologyContext *ct_ctx = connect_ctx->ct_ctx;
3515 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
3516 struct PeerConnection *connection;
3517 pg->outstanding_connects--;
3520 * Check whether the inverse connection has been scheduled yet,
3521 * if not, we can remove it from the other peers list and avoid
3522 * even trying to connect them again!
3524 connection = pg->peers[connect_ctx->second_index].connect_peers_head;
3529 while ((connection != NULL) && (0
3530 != memcmp (first, &pg->peers[connection->index].daemon->id,
3531 sizeof(struct GNUNET_PeerIdentity))))
3533 connection = connection->next;
3536 if (connection != NULL) /* Can safely remove! */
3538 ct_ctx->remaining_connections--;
3539 if (pg->notify_connection != NULL) /* Notify of reverse connection */
3540 pg->notify_connection (pg->notify_connection_cls, second, first,
3541 distance, second_cfg, first_cfg, second_daemon,
3542 first_daemon, emsg);
3544 GNUNET_CONTAINER_DLL_remove(pg->peers[connect_ctx->second_index].connect_peers_head, pg->peers[connect_ctx->second_index].connect_peers_tail, connection);
3545 GNUNET_free(connection);
3548 if (ct_ctx->remaining_connections == 0)
3550 if (ct_ctx->notify_connections_done != NULL)
3551 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
3554 preschedule_connect (pg);
3556 if (pg->notify_connection != NULL)
3557 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
3558 first_cfg, second_cfg, first_daemon, second_daemon,
3561 GNUNET_free(connect_ctx);
3565 * Either delay a connection (because there are too many outstanding)
3566 * or schedule it for right now.
3568 * @param cls a connection context
3569 * @param tc the task runtime context
3572 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3574 struct ConnectContext *connect_context = cls;
3575 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
3577 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3580 if ((pg->outstanding_connects > pg->max_outstanding_connections)
3581 || (pg->stop_connects == GNUNET_YES))
3584 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3586 ("Delaying connect, we have too many outstanding connections!\n"));
3588 GNUNET_SCHEDULER_add_delayed (
3589 GNUNET_TIME_relative_multiply (
3590 GNUNET_TIME_UNIT_MILLISECONDS,
3592 &schedule_connect, connect_context);
3597 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3598 _("Creating connection, outstanding_connections is %d (max %d)\n"),
3599 pg->outstanding_connects, pg->max_outstanding_connections);
3601 pg->outstanding_connects++;
3602 pg->total_connects_scheduled++;
3603 GNUNET_TESTING_daemons_connect (
3604 pg->peers[connect_context->first_index].daemon,
3605 pg->peers[connect_context->second_index].daemon,
3606 connect_context->ct_ctx->connect_timeout,
3607 connect_context->ct_ctx->connect_attempts,
3613 &internal_connect_notify, connect_context); /* FIXME: free connect context! */
3619 * Iterator for actually scheduling connections to be created
3620 * between two peers.
3622 * @param cls closure, a GNUNET_TESTING_Daemon
3623 * @param key the key the second Daemon was stored under
3624 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3626 * @return GNUNET_YES to continue iteration
3629 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3631 struct ConnectTopologyContext *ct_ctx = cls;
3632 struct PeerData *first = ct_ctx->first;
3633 struct GNUNET_TESTING_Daemon *second = value;
3634 struct ConnectContext *connect_context;
3636 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3637 connect_context->first = first->daemon;
3638 connect_context->second = second;
3639 connect_context->ct_ctx = ct_ctx;
3640 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3648 * Iterator for copying all entries in the allowed hashmap to the
3651 * @param cls closure, a GNUNET_TESTING_Daemon
3652 * @param key the key the second Daemon was stored under
3653 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3655 * @return GNUNET_YES to continue iteration
3658 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3660 struct PeerData *first = cls;
3662 GNUNET_assert (GNUNET_OK ==
3663 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
3665 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3672 * Make the peers to connect the same as those that are allowed to be
3675 * @param pg the peer group
3678 copy_allowed_topology(struct GNUNET_TESTING_PeerGroup *pg)
3680 unsigned int pg_iter;
3684 struct PeerConnection *iter;
3688 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3691 iter = pg->peers[pg_iter].allowed_peers_head;
3692 while (iter != NULL)
3694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3695 "Creating connection between %d and %d\n", pg_iter,
3697 total += add_connections (pg, pg_iter, iter->index, CONNECT,
3699 //total += add_actual_connections(pg, pg_iter, iter->index);
3704 GNUNET_CONTAINER_multihashmap_iterate (pg->
3705 peers[pg_iter].allowed_peers,
3706 ©_topology_iterator,
3707 &pg->peers[pg_iter]);
3709 if (GNUNET_SYSERR == ret)
3710 return GNUNET_SYSERR;
3712 total = total + ret;
3719 * Connect the topology as specified by the PeerConnection's
3720 * of each peer in the peer group
3722 * @param pg the peer group we are dealing with
3723 * @param connect_timeout how long try connecting two peers
3724 * @param connect_attempts how many times (max) to attempt
3725 * @param notify_callback callback to notify when finished
3726 * @param notify_cls closure for notify callback
3728 * @return the number of connections that will be attempted
3731 connect_topology(struct GNUNET_TESTING_PeerGroup *pg,
3732 struct GNUNET_TIME_Relative connect_timeout,
3733 unsigned int connect_attempts,
3734 GNUNET_TESTING_NotifyCompletion notify_callback,
3737 unsigned int pg_iter;
3741 struct PeerConnection *connection_iter;
3744 struct SendHelloContext *send_hello_context;
3748 pg->ct_ctx.notify_connections_done = notify_callback;
3749 pg->ct_ctx.notify_cls = notify_cls;
3752 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3755 connection_iter = pg->peers[pg_iter].connect_peers_head;
3756 while (connection_iter != NULL)
3758 connection_iter = connection_iter->next;
3763 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3770 pg->ct_ctx.connect_timeout = connect_timeout;
3771 pg->ct_ctx.connect_attempts = connect_attempts;
3772 pg->ct_ctx.remaining_connections = total;
3775 /* First give all peers the HELLO's of other peers (connect to first peer's transport service, give HELLO's of other peers, continue...) */
3776 pg->remaining_hellos = total;
3777 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3779 send_hello_context = GNUNET_malloc(sizeof(struct SendHelloContext));
3780 send_hello_context->peer = &pg->peers[pg_iter];
3781 send_hello_context->peer_pos = pg->peers[pg_iter].connect_peers_head;
3782 send_hello_context->pg = pg;
3783 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3786 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3788 preschedule_connect (pg);
3796 * Takes a peer group and creates a topology based on the
3797 * one specified. Creates a topology means generates friend
3798 * files for the peers so they can only connect to those allowed
3799 * by the topology. This will only have an effect once peers
3800 * are started if the FRIENDS_ONLY option is set in the base
3801 * config. Also takes an optional restrict topology which
3802 * disallows connections based on particular transports
3803 * UNLESS they are specified in the restricted topology.
3805 * @param pg the peer group struct representing the running peers
3806 * @param topology which topology to connect the peers in
3807 * @param restrict_topology disallow restrict_transports transport
3808 * connections to peers NOT in this topology
3809 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3810 * @param restrict_transports space delimited list of transports to blacklist
3811 * to create restricted topology
3813 * @return the maximum number of connections were all allowed peers
3814 * connected to each other
3817 GNUNET_TESTING_create_topology(struct GNUNET_TESTING_PeerGroup *pg,
3818 enum GNUNET_TESTING_Topology topology,
3819 enum GNUNET_TESTING_Topology restrict_topology,
3820 const char *restrict_transports)
3824 unsigned int num_connections;
3825 int unblacklisted_connections;
3827 struct PeerConnection *conn_iter;
3828 struct PeerConnection *temp_conn;
3833 for (i = 0; i < pg->total; i++)
3835 pg->peers[i].allowed_peers =
3836 GNUNET_CONTAINER_multihashmap_create (100);
3837 pg->peers[i].connect_peers =
3838 GNUNET_CONTAINER_multihashmap_create (100);
3839 pg->peers[i].blacklisted_peers =
3840 GNUNET_CONTAINER_multihashmap_create (100);
3841 pg->peers[i].pg = pg;
3847 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3851 num_connections = create_clique (pg, &add_connections, ALLOWED, GNUNET_NO);
3853 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3856 _("Creating small world (ring) topology\n"));
3858 num_connections = create_small_world_ring (pg, &add_connections, ALLOWED);
3860 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3863 _("Creating small world (2d-torus) topology\n"));
3865 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3867 case GNUNET_TESTING_TOPOLOGY_RING:
3869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3871 num_connections = create_ring (pg, &add_connections, ALLOWED);
3873 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3877 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3879 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3882 _("Creating Erdos-Renyi topology\n"));
3884 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3886 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3890 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3892 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3895 _("Creating Scale Free topology\n"));
3897 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3899 case GNUNET_TESTING_TOPOLOGY_LINE:
3901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3902 _("Creating straight line topology\n"));
3904 num_connections = create_line (pg, &add_connections, ALLOWED);
3906 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3909 _("Creating topology from file!\n"));
3911 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing",
3914 num_connections = create_from_file (pg, filename, &add_connections,
3919 GNUNET_ERROR_TYPE_WARNING,
3920 "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3921 num_connections = 0;
3924 case GNUNET_TESTING_TOPOLOGY_NONE:
3926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3928 ("Creating no allowed topology (all peers can connect at core level)\n"));
3930 num_connections = pg->total * pg->total; /* Clique is allowed! */
3933 num_connections = 0;
3937 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING",
3940 ret = create_and_copy_friend_files (pg);
3941 if (ret != GNUNET_OK)
3944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3945 _("Failed during friend file copying!\n"));
3947 return GNUNET_SYSERR;
3952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3953 _("Friend files created/copied successfully!\n"));
3958 /* Use the create clique method to initially set all connections as blacklisted. */
3959 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology
3960 != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
3961 create_clique (pg, &add_connections, BLACKLIST, GNUNET_NO);
3963 unblacklisted_connections = 0;
3964 /* Un-blacklist connections as per the topology specified */
3965 switch (restrict_topology)
3967 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3970 _("Blacklisting all but clique topology\n"));
3972 unblacklisted_connections = create_clique (pg, &remove_connections,
3973 BLACKLIST, GNUNET_NO);
3975 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3978 _("Blacklisting all but small world (ring) topology\n"));
3980 unblacklisted_connections = create_small_world_ring (pg,
3981 &remove_connections,
3984 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3988 ("Blacklisting all but small world (2d-torus) topology\n"));
3990 unblacklisted_connections = create_small_world (pg, &remove_connections,
3993 case GNUNET_TESTING_TOPOLOGY_RING:
3995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3996 _("Blacklisting all but ring topology\n"));
3998 unblacklisted_connections
3999 = create_ring (pg, &remove_connections, BLACKLIST);
4001 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4004 _("Blacklisting all but 2d torus topology\n"));
4006 unblacklisted_connections = create_2d_torus (pg, &remove_connections,
4009 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4012 _("Blacklisting all but Erdos-Renyi topology\n"));
4014 unblacklisted_connections = create_erdos_renyi (pg, &remove_connections,
4017 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4020 _("Blacklisting all but InterNAT topology\n"));
4024 for (off = 0; off < pg->total; off++)
4026 conn_iter = pg->peers[off].allowed_peers_head;
4027 while (conn_iter != NULL)
4029 temp_conn = conn_iter->next;
4030 GNUNET_free(conn_iter);
4031 conn_iter = temp_conn;
4033 pg->peers[off].allowed_peers_head = NULL;
4034 pg->peers[off].allowed_peers_tail = NULL;
4036 conn_iter = pg->peers[off].connect_peers_head;
4037 while (conn_iter != NULL)
4039 temp_conn = conn_iter->next;
4040 GNUNET_free(conn_iter);
4041 conn_iter = temp_conn;
4043 pg->peers[off].connect_peers_head = NULL;
4044 pg->peers[off].connect_peers_tail = NULL;
4046 unblacklisted_connections
4047 = create_nated_internet_copy (pg, &remove_connections, BLACKLIST);
4049 unblacklisted_connections =
4050 create_nated_internet (pg, &remove_connections, BLACKLIST);
4054 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4057 _("Blacklisting all but Scale Free topology\n"));
4059 unblacklisted_connections = create_scale_free (pg, &remove_connections,
4062 case GNUNET_TESTING_TOPOLOGY_LINE:
4064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4065 _("Blacklisting all but straight line topology\n"));
4067 unblacklisted_connections
4068 = create_line (pg, &remove_connections, BLACKLIST);
4070 case GNUNET_TESTING_TOPOLOGY_NONE: /* Fall through */
4071 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
4073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4075 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
4077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
4078 ("Creating blacklist topology from allowed\n"));
4079 unblacklisted_connections = copy_allowed (pg, &remove_connections);
4084 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
4086 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n",
4087 restrict_transports);
4088 ret = create_and_copy_blacklist_files (pg, restrict_transports);
4089 if (ret != GNUNET_OK)
4092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4093 _("Failed during blacklist file copying!\n"));
4100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4101 _("Blacklist files created/copied successfully!\n"));
4105 return num_connections;
4110 * Iterator for choosing random peers to connect.
4112 * @param cls closure, a RandomContext
4113 * @param key the key the second Daemon was stored under
4114 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4116 * @return GNUNET_YES to continue iteration
4119 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4121 struct RandomContext *random_ctx = cls;
4122 double random_number;
4123 uint32_t second_pos;
4124 GNUNET_HashCode first_hash;
4127 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4128 UINT64_MAX)) / ((double) UINT64_MAX);
4129 if (random_number < random_ctx->percentage)
4131 GNUNET_assert (GNUNET_OK ==
4132 GNUNET_CONTAINER_multihashmap_put (random_ctx->
4133 first->connect_peers_working_set,
4135 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4138 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
4139 uid_from_hash (key, &second_pos);
4140 hash_from_uid (random_ctx->first_uid, &first_hash);
4141 GNUNET_assert (random_ctx->pg->total > second_pos);
4142 GNUNET_assert (GNUNET_YES ==
4143 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
4145 [second_pos].connect_peers,
4154 * Iterator for adding at least X peers to a peers connection set.
4156 * @param cls closure, MinimumContext
4157 * @param key the key the second Daemon was stored under
4158 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4160 * @return GNUNET_YES to continue iteration
4163 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4165 struct MinimumContext *min_ctx = cls;
4166 uint32_t second_pos;
4167 GNUNET_HashCode first_hash;
4170 if (GNUNET_CONTAINER_multihashmap_size
4171 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
4173 for (i = 0; i < min_ctx->num_to_add; i++)
4175 if (min_ctx->pg_array[i] == min_ctx->current)
4177 GNUNET_assert (GNUNET_OK ==
4178 GNUNET_CONTAINER_multihashmap_put
4179 (min_ctx->first->connect_peers_working_set, key,
4181 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4182 uid_from_hash (key, &second_pos);
4183 hash_from_uid (min_ctx->first_uid, &first_hash);
4184 GNUNET_assert (min_ctx->pg->total > second_pos);
4185 GNUNET_assert (GNUNET_OK ==
4186 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4188 [second_pos].connect_peers_working_set,
4192 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4193 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
4194 GNUNET_assert (GNUNET_YES ==
4195 GNUNET_CONTAINER_multihashmap_remove
4196 (min_ctx->pg->peers[second_pos].connect_peers,
4197 &first_hash, min_ctx->first->daemon));
4204 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
4209 * Iterator for adding peers to a connection set based on a depth first search.
4211 * @param cls closure, MinimumContext
4212 * @param key the key the second daemon was stored under
4213 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4215 * @return GNUNET_YES to continue iteration
4218 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4220 struct DFSContext *dfs_ctx = cls;
4221 GNUNET_HashCode first_hash;
4223 if (dfs_ctx->current == dfs_ctx->chosen)
4225 GNUNET_assert (GNUNET_OK ==
4226 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4227 first->connect_peers_working_set,
4229 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4230 uid_from_hash (key, &dfs_ctx->second_uid);
4231 hash_from_uid (dfs_ctx->first_uid, &first_hash);
4232 GNUNET_assert (GNUNET_OK ==
4233 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4235 [dfs_ctx->second_uid].connect_peers_working_set,
4239 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4240 GNUNET_assert (GNUNET_YES ==
4241 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
4243 [dfs_ctx->second_uid].connect_peers,
4247 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
4248 return GNUNET_NO; /* We have found our peer, don't iterate more */
4257 * From the set of connections possible, choose percentage percent of connections
4258 * to actually connect.
4260 * @param pg the peergroup we are dealing with
4261 * @param percentage what percent of total connections to make
4264 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg,
4267 struct RandomContext random_ctx;
4270 struct PeerConnection *temp_peers;
4271 struct PeerConnection *conn_iter;
4272 double random_number;
4275 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4277 random_ctx.first_uid = pg_iter;
4278 random_ctx.first = &pg->peers[pg_iter];
4279 random_ctx.percentage = percentage;
4283 conn_iter = pg->peers[pg_iter].connect_peers_head;
4284 while (conn_iter != NULL)
4287 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4289 / ((double) UINT64_MAX);
4290 if (random_number < percentage)
4292 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4295 conn_iter = conn_iter->next;
4298 pg->peers[pg_iter].connect_peers_working_set =
4299 GNUNET_CONTAINER_multihashmap_create (pg->total);
4300 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4301 &random_connect_iterator,
4303 /* Now remove the old connections */
4304 GNUNET_CONTAINER_multihashmap_destroy (pg->
4305 peers[pg_iter].connect_peers);
4306 /* And replace with the random set */
4307 pg->peers[pg_iter].connect_peers =
4308 pg->peers[pg_iter].connect_peers_working_set;
4312 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4314 conn_iter = pg->peers[pg_iter].connect_peers_head;
4315 while (pg->peers[pg_iter].connect_peers_head != NULL)
4316 remove_connections (pg, pg_iter,
4317 pg->peers[pg_iter].connect_peers_head->index,
4318 CONNECT, GNUNET_YES);
4320 pg->peers[pg_iter].connect_peers_head
4321 = pg->peers[pg_iter].connect_peers_working_set_head;
4322 pg->peers[pg_iter].connect_peers_tail
4323 = pg->peers[pg_iter].connect_peers_working_set_tail;
4324 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4325 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4330 * Count the number of connections in a linked list of connections.
4332 * @param conn_list the connection list to get the count of
4334 * @return the number of elements in the list
4337 count_connections(struct PeerConnection *conn_list)
4339 struct PeerConnection *iter;
4343 while (iter != NULL)
4352 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
4355 unsigned int pg_iter;
4357 struct PeerConnection *conn_iter;
4361 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4364 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
4365 while (conn_iter != NULL)
4368 conn_iter = conn_iter->next;
4372 GNUNET_CONTAINER_multihashmap_size (pg->
4374 [pg_iter].connect_peers_working_set);
4382 count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
4385 unsigned int pg_iter;
4387 struct PeerConnection *conn_iter;
4391 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4394 conn_iter = pg->peers[pg_iter].allowed_peers_head;
4395 while (conn_iter != NULL)
4398 conn_iter = conn_iter->next;
4402 GNUNET_CONTAINER_multihashmap_size (pg->
4404 [pg_iter].allowed_peers);
4412 * From the set of connections possible, choose at least num connections per
4415 * @param pg the peergroup we are dealing with
4416 * @param num how many connections at least should each peer have (if possible)?
4419 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4422 struct MinimumContext minimum_ctx;
4424 struct PeerConnection *conn_iter;
4425 unsigned int temp_list_size;
4428 uint32_t random; /* Random list entry to connect peer to */
4433 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4436 = count_connections (pg->peers[pg_iter].connect_peers_head);
4437 if (temp_list_size == 0)
4439 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4440 "Peer %d has 0 connections!?!?\n", pg_iter);
4443 for (i = 0; i < num; i++)
4445 random = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4447 conn_iter = pg->peers[pg_iter].connect_peers_head;
4448 for (count = 0; count < random; count++)
4449 conn_iter = conn_iter->next;
4450 /* We now have a random connection, connect it! */
4451 GNUNET_assert(conn_iter != NULL);
4452 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4457 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4459 pg->peers[pg_iter].connect_peers_working_set =
4460 GNUNET_CONTAINER_multihashmap_create (num);
4463 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4465 minimum_ctx.first_uid = pg_iter;
4466 minimum_ctx.pg_array =
4467 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
4468 GNUNET_CONTAINER_multihashmap_size
4469 (pg->peers[pg_iter].connect_peers));
4470 minimum_ctx.first = &pg->peers[pg_iter];
4471 minimum_ctx.pg = pg;
4472 minimum_ctx.num_to_add = num;
4473 minimum_ctx.current = 0;
4474 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4475 &minimum_connect_iterator,
4479 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4481 /* Remove the "old" connections */
4482 GNUNET_CONTAINER_multihashmap_destroy (pg->
4483 peers[pg_iter].connect_peers);
4484 /* And replace with the working set */
4485 pg->peers[pg_iter].connect_peers =
4486 pg->peers[pg_iter].connect_peers_working_set;
4489 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4491 while (pg->peers[pg_iter].connect_peers_head != NULL)
4493 conn_iter = pg->peers[pg_iter].connect_peers_head;
4494 GNUNET_CONTAINER_DLL_remove(pg->peers[pg_iter].connect_peers_head,
4495 pg->peers[pg_iter].connect_peers_tail,
4497 GNUNET_free(conn_iter);
4498 /*remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT, GNUNET_YES);*/
4501 pg->peers[pg_iter].connect_peers_head
4502 = pg->peers[pg_iter].connect_peers_working_set_head;
4503 pg->peers[pg_iter].connect_peers_tail
4504 = pg->peers[pg_iter].connect_peers_working_set_tail;
4505 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4506 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4511 struct FindClosestContext
4514 * The currently known closest peer.
4516 struct GNUNET_TESTING_Daemon *closest;
4519 * The info for the peer we are adding connections for.
4521 struct PeerData *curr_peer;
4524 * The distance (bits) between the current
4525 * peer and the currently known closest.
4527 unsigned int closest_dist;
4530 * The offset of the closest known peer in
4533 unsigned int closest_num;
4537 * Iterator over hash map entries of the allowed
4538 * peer connections. Find the closest, not already
4539 * connected peer and return it.
4541 * @param cls closure (struct FindClosestContext)
4542 * @param key current key code (hash of offset in pg)
4543 * @param value value in the hash map - a GNUNET_TESTING_Daemon
4544 * @return GNUNET_YES if we should continue to
4549 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
4551 struct FindClosestContext *closest_ctx = cls;
4552 struct GNUNET_TESTING_Daemon *daemon = value;
4554 if (((closest_ctx->closest == NULL) ||
4555 (GNUNET_CRYPTO_hash_matching_bits
4556 (&daemon->id.hashPubKey,
4557 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
4558 closest_ctx->closest_dist))
4560 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
4561 curr_peer->connect_peers,
4564 closest_ctx->closest_dist =
4565 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
4566 &closest_ctx->curr_peer->daemon->
4568 closest_ctx->closest = daemon;
4569 uid_from_hash (key, &closest_ctx->closest_num);
4575 * From the set of connections possible, choose at num connections per
4576 * peer based on depth which are closest out of those allowed. Guaranteed
4577 * to add num peers to connect to, provided there are that many peers
4578 * in the underlay topology to connect to.
4580 * @param pg the peergroup we are dealing with
4581 * @param num how many connections at least should each peer have (if possible)?
4582 * @param proc processor to actually add the connections
4583 * @param list the peer list to use
4586 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
4587 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
4592 struct FindClosestContext closest_ctx;
4597 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
4599 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4601 closest_ctx.curr_peer = &pg->peers[pg_iter];
4602 closest_ctx.closest = NULL;
4603 closest_ctx.closest_dist = 0;
4604 closest_ctx.closest_num = 0;
4605 GNUNET_CONTAINER_multihashmap_iterate (pg->
4606 peers[pg_iter].allowed_peers,
4607 &find_closest_peers,
4609 if (closest_ctx.closest != NULL)
4611 GNUNET_assert (closest_ctx.closest_num < pg->total);
4612 proc (pg, pg_iter, closest_ctx.closest_num, list);
4620 * From the set of connections possible, choose at least num connections per
4621 * peer based on depth first traversal of peer connections. If DFS leaves
4622 * peers unconnected, ensure those peers get connections.
4624 * @param pg the peergroup we are dealing with
4625 * @param num how many connections at least should each peer have (if possible)?
4628 perform_dfs(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4632 uint32_t starting_peer;
4633 uint32_t least_connections;
4634 uint32_t random_connection;
4636 unsigned int temp_count;
4637 struct PeerConnection *peer_iter;
4639 struct DFSContext dfs_ctx;
4640 GNUNET_HashCode second_hash;
4646 while ((count_workingset_connections (pg) < num * pg->total)
4647 && (count_allowed_connections (pg) > 0))
4649 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4651 least_connections = -1; /* Set to very high number */
4652 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4655 = count_connections (
4656 pg->peers[pg_iter].connect_peers_working_set_head);
4657 if (temp_count < least_connections)
4659 starting_peer = pg_iter;
4660 least_connections = temp_count;
4666 = count_connections (pg->peers[starting_peer].connect_peers_head);
4667 if (temp_count == 0)
4668 continue; /* FIXME: infinite loop? */
4670 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4673 peer_iter = pg->peers[starting_peer].connect_peers_head;
4674 while (temp_count < random_connection)
4676 peer_iter = peer_iter->next;
4679 GNUNET_assert(peer_iter != NULL);
4680 add_connections (pg, starting_peer, peer_iter->index, WORKING_SET,
4682 remove_connections (pg, starting_peer, peer_iter->index, CONNECT,
4684 starting_peer = peer_iter->index;
4689 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4691 pg->peers[pg_iter].connect_peers_working_set =
4692 GNUNET_CONTAINER_multihashmap_create (num);
4697 while ((count_workingset_connections (pg) < num * pg->total)
4698 && (count_allowed_connections (pg) > 0))
4700 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4702 least_connections = -1; /* Set to very high number */
4703 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4705 if (GNUNET_CONTAINER_multihashmap_size
4706 (pg->peers[pg_iter].connect_peers_working_set) <
4709 starting_peer = pg_iter;
4711 GNUNET_CONTAINER_multihashmap_size (pg->
4713 [pg_iter].connect_peers_working_set);
4718 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4724 /* Choose a random peer from the chosen peers set of connections to add */
4726 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4727 GNUNET_CONTAINER_multihashmap_size
4728 (pg->peers[starting_peer].connect_peers));
4729 dfs_ctx.first_uid = starting_peer;
4730 dfs_ctx.first = &pg->peers[starting_peer];
4732 dfs_ctx.current = 0;
4734 GNUNET_CONTAINER_multihashmap_iterate (pg->
4736 [starting_peer].connect_peers,
4737 &dfs_connect_iterator, &dfs_ctx);
4738 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4739 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4740 GNUNET_assert (GNUNET_YES ==
4741 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4742 [starting_peer].connect_peers,
4746 [dfs_ctx.second_uid].daemon));
4747 starting_peer = dfs_ctx.second_uid;
4750 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4752 /* Remove the "old" connections */
4753 GNUNET_CONTAINER_multihashmap_destroy (pg->
4754 peers[pg_iter].connect_peers);
4755 /* And replace with the working set */
4756 pg->peers[pg_iter].connect_peers =
4757 pg->peers[pg_iter].connect_peers_working_set;
4763 * Internal callback for topology information for a particular peer.
4766 internal_topology_callback(void *cls, const struct GNUNET_PeerIdentity *peer,
4767 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4769 struct CoreContext *core_ctx = cls;
4770 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4772 if (peer == NULL) /* Either finished, or something went wrong */
4774 iter_ctx->completed++;
4775 iter_ctx->connected--;
4776 /* One core context allocated per iteration, must free! */
4777 GNUNET_free (core_ctx);
4781 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id, peer, NULL);
4784 if (iter_ctx->completed == iter_ctx->total)
4786 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4787 /* Once all are done, free the iteration context */
4788 GNUNET_free (iter_ctx);
4793 * Check running topology iteration tasks, if below max start a new one, otherwise
4794 * schedule for some time in the future.
4797 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4799 struct CoreContext *core_context = cls;
4800 struct TopologyIterateContext *topology_context =
4801 (struct TopologyIterateContext *) core_context->iter_context;
4802 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4805 if (topology_context->connected
4806 > topology_context->pg->max_outstanding_connections)
4808 #if VERBOSE_TESTING > 2
4809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4811 ("Delaying connect, we have too many outstanding connections!\n"));
4813 GNUNET_SCHEDULER_add_delayed (
4814 GNUNET_TIME_relative_multiply (
4815 GNUNET_TIME_UNIT_MILLISECONDS,
4817 &schedule_get_topology, core_context);
4821 #if VERBOSE_TESTING > 2
4822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4823 _("Creating connection, outstanding_connections is %d\n"),
4824 outstanding_connects);
4826 topology_context->connected++;
4828 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4829 &internal_topology_callback,
4832 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4833 internal_topology_callback (core_context, NULL, NULL);
4839 * Iterate over all (running) peers in the peer group, retrieve
4840 * all connections that each currently has.
4843 GNUNET_TESTING_get_topology(struct GNUNET_TESTING_PeerGroup *pg,
4844 GNUNET_TESTING_NotifyTopology cb, void *cls)
4846 struct TopologyIterateContext *topology_context;
4847 struct CoreContext *core_ctx;
4849 unsigned int total_count;
4851 /* Allocate a single topology iteration context */
4852 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4853 topology_context->topology_cb = cb;
4854 topology_context->cls = cls;
4855 topology_context->pg = pg;
4857 for (i = 0; i < pg->total; i++)
4859 if (pg->peers[i].daemon->running == GNUNET_YES)
4861 /* Allocate one core context per core we need to connect to */
4862 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4863 core_ctx->daemon = pg->peers[i].daemon;
4864 /* Set back pointer to topology iteration context */
4865 core_ctx->iter_context = topology_context;
4866 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4870 if (total_count == 0)
4872 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4873 GNUNET_free (topology_context);
4876 topology_context->total = total_count;
4881 * Callback function to process statistic values.
4882 * This handler is here only really to insert a peer
4883 * identity (or daemon) so the statistics can be uniquely
4884 * tied to a single running peer.
4886 * @param cls closure
4887 * @param subsystem name of subsystem that created the statistic
4888 * @param name the name of the datum
4889 * @param value the current value
4890 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4891 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4894 internal_stats_callback(void *cls, const char *subsystem, const char *name,
4895 uint64_t value, int is_persistent)
4897 struct StatsCoreContext *core_context = cls;
4898 struct StatsIterateContext *stats_context =
4899 (struct StatsIterateContext *) core_context->iter_context;
4901 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4902 subsystem, name, value, is_persistent);
4906 * Internal continuation call for statistics iteration.
4908 * @param cls closure, the CoreContext for this iteration
4909 * @param success whether or not the statistics iterations
4910 * was canceled or not (we don't care)
4913 internal_stats_cont(void *cls, int success)
4915 struct StatsCoreContext *core_context = cls;
4916 struct StatsIterateContext *stats_context =
4917 (struct StatsIterateContext *) core_context->iter_context;
4919 stats_context->connected--;
4920 stats_context->completed++;
4922 if (stats_context->completed == stats_context->total)
4924 stats_context->cont (stats_context->cls, GNUNET_YES);
4925 GNUNET_free (stats_context);
4928 if (core_context->stats_handle != NULL)
4929 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4931 GNUNET_free (core_context);
4935 * Check running topology iteration tasks, if below max start a new one, otherwise
4936 * schedule for some time in the future.
4939 schedule_get_statistics(void *cls,
4940 const struct GNUNET_SCHEDULER_TaskContext *tc)
4942 struct StatsCoreContext *core_context = cls;
4943 struct StatsIterateContext *stats_context =
4944 (struct StatsIterateContext *) core_context->iter_context;
4946 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4949 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
4951 #if VERBOSE_TESTING > 2
4952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4954 ("Delaying connect, we have too many outstanding connections!\n"));
4956 GNUNET_SCHEDULER_add_delayed (
4957 GNUNET_TIME_relative_multiply (
4958 GNUNET_TIME_UNIT_MILLISECONDS,
4960 &schedule_get_statistics, core_context);
4964 #if VERBOSE_TESTING > 2
4965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4966 _("Creating connection, outstanding_connections is %d\n"),
4967 outstanding_connects);
4970 stats_context->connected++;
4971 core_context->stats_handle
4972 = GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
4973 if (core_context->stats_handle == NULL)
4975 internal_stats_cont (core_context, GNUNET_NO);
4979 core_context->stats_get_handle
4980 = GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
4981 GNUNET_TIME_relative_get_forever (),
4982 &internal_stats_cont,
4983 &internal_stats_callback, core_context);
4984 if (core_context->stats_get_handle == NULL)
4985 internal_stats_cont (core_context, GNUNET_NO);
4990 struct DuplicateStats
4993 * Next item in the list
4995 struct DuplicateStats *next;
4998 * Nasty string, concatenation of relevant information.
5000 char *unique_string;
5004 * Check whether the combination of port/host/unix domain socket
5005 * already exists in the list of peers being checked for statistics.
5007 * @param pg the peergroup in question
5008 * @param specific_peer the peer we're concerned with
5009 * @param stats_list the list to return to the caller
5011 * @return GNUNET_YES if the statistics instance has been seen already,
5012 * GNUNET_NO if not (and we may have added it to the list)
5015 stats_check_existing(struct GNUNET_TESTING_PeerGroup *pg,
5016 struct PeerData *specific_peer,
5017 struct DuplicateStats **stats_list)
5019 struct DuplicateStats *pos;
5020 char *unix_domain_socket;
5021 unsigned long long port;
5024 != GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
5025 "single_statistics_per_host"))
5026 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
5029 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg,
5032 &unix_domain_socket))
5035 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg,
5036 "statistics", "port",
5039 GNUNET_free(unix_domain_socket);
5043 if (specific_peer->daemon->hostname != NULL)
5044 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
5045 unix_domain_socket, port);
5047 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
5051 if (0 == strcmp (to_match, pos->unique_string))
5053 GNUNET_free (unix_domain_socket);
5054 GNUNET_free (to_match);
5059 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
5060 pos->unique_string = to_match;
5061 pos->next = *stats_list;
5063 GNUNET_free (unix_domain_socket);
5068 * Iterate over all (running) peers in the peer group, retrieve
5069 * all statistics from each.
5072 GNUNET_TESTING_get_statistics(struct GNUNET_TESTING_PeerGroup *pg,
5073 GNUNET_STATISTICS_Callback cont,
5074 GNUNET_TESTING_STATISTICS_Iterator proc,
5077 struct StatsIterateContext *stats_context;
5078 struct StatsCoreContext *core_ctx;
5080 unsigned int total_count;
5081 struct DuplicateStats *stats_list;
5082 struct DuplicateStats *pos;
5085 /* Allocate a single stats iteration context */
5086 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
5087 stats_context->cont = cont;
5088 stats_context->proc = proc;
5089 stats_context->cls = cls;
5090 stats_context->pg = pg;
5093 for (i = 0; i < pg->total; i++)
5095 if ((pg->peers[i].daemon->running == GNUNET_YES) && (GNUNET_NO
5096 == stats_check_existing (pg, &pg->peers[i], &stats_list)))
5098 /* Allocate one core context per core we need to connect to */
5099 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
5100 core_ctx->daemon = pg->peers[i].daemon;
5101 /* Set back pointer to topology iteration context */
5102 core_ctx->iter_context = stats_context;
5103 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
5108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5109 "Retrieving stats from %u total instances.\n", total_count);
5110 stats_context->total = total_count;
5111 if (stats_list != NULL)
5116 GNUNET_free (pos->unique_string);
5117 stats_list = pos->next;
5119 pos = stats_list->next;
5126 * Stop the connection process temporarily.
5128 * @param pg the peer group to stop connecting
5131 GNUNET_TESTING_stop_connections(struct GNUNET_TESTING_PeerGroup *pg)
5133 pg->stop_connects = GNUNET_YES;
5137 * Resume the connection process temporarily.
5139 * @param pg the peer group to resume connecting
5142 GNUNET_TESTING_resume_connections(struct GNUNET_TESTING_PeerGroup *pg)
5144 pg->stop_connects = GNUNET_NO;
5148 * There are many ways to connect peers that are supported by this function.
5149 * To connect peers in the same topology that was created via the
5150 * GNUNET_TESTING_create_topology, the topology variable must be set to
5151 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
5152 * a new instance of that topology will be generated and attempted to be
5153 * connected. This could result in some connections being impossible,
5154 * because some topologies are non-deterministic.
5156 * @param pg the peer group struct representing the running peers
5157 * @param topology which topology to connect the peers in
5158 * @param options options for connecting the topology
5159 * @param option_modifier modifier for options that take a parameter
5160 * @param connect_timeout how long to wait before giving up on connecting
5162 * @param connect_attempts how many times to attempt to connect two peers
5163 * over the connect_timeout duration
5164 * @param notify_callback notification to be called once all connections completed
5165 * @param notify_cls closure for notification callback
5167 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
5170 GNUNET_TESTING_connect_topology(
5171 struct GNUNET_TESTING_PeerGroup *pg,
5172 enum GNUNET_TESTING_Topology topology,
5173 enum GNUNET_TESTING_TopologyOption options,
5174 double option_modifier,
5175 struct GNUNET_TIME_Relative connect_timeout,
5176 unsigned int connect_attempts,
5177 GNUNET_TESTING_NotifyCompletion notify_callback,
5182 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
5183 #if VERBOSE_TOPOLOGY
5184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5185 _("Creating clique CONNECT topology\n"));
5187 create_clique (pg, &add_connections, CONNECT, GNUNET_NO);
5189 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
5190 #if VERBOSE_TOPOLOGY
5191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5192 _("Creating small world (ring) CONNECT topology\n"));
5194 create_small_world_ring (pg, &add_connections, CONNECT);
5196 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
5197 #if VERBOSE_TOPOLOGY
5198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5199 _("Creating small world (2d-torus) CONNECT topology\n"));
5201 create_small_world (pg, &add_connections, CONNECT);
5203 case GNUNET_TESTING_TOPOLOGY_RING:
5204 #if VERBOSE_TOPOLOGY
5205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring CONNECT topology\n"));
5207 create_ring (pg, &add_connections, CONNECT);
5209 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
5210 #if VERBOSE_TOPOLOGY
5211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5212 _("Creating 2d torus CONNECT topology\n"));
5214 create_2d_torus (pg, &add_connections, CONNECT);
5216 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
5217 #if VERBOSE_TOPOLOGY
5218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5219 _("Creating Erdos-Renyi CONNECT topology\n"));
5221 create_erdos_renyi (pg, &add_connections, CONNECT);
5223 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
5224 #if VERBOSE_TOPOLOGY
5225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5226 _("Creating InterNAT CONNECT topology\n"));
5228 create_nated_internet (pg, &add_connections, CONNECT);
5230 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
5231 #if VERBOSE_TOPOLOGY
5232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5233 _("Creating Scale Free CONNECT topology\n"));
5235 create_scale_free (pg, &add_connections, CONNECT);
5237 case GNUNET_TESTING_TOPOLOGY_LINE:
5238 #if VERBOSE_TOPOLOGY
5239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5240 _("Creating straight line CONNECT topology\n"));
5242 create_line (pg, &add_connections, CONNECT);
5244 case GNUNET_TESTING_TOPOLOGY_NONE:
5245 #if VERBOSE_TOPOLOGY
5246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating no CONNECT topology\n"));
5248 copy_allowed_topology (pg);
5251 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5252 ("Unknown topology specification, can't connect peers!\n"));
5253 return GNUNET_SYSERR;
5258 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
5259 #if VERBOSE_TOPOLOGY
5260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5261 ("Connecting random subset (%'.2f percent) of possible peers\n"), 100
5264 choose_random_connections (pg, option_modifier);
5266 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
5267 #if VERBOSE_TOPOLOGY
5268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5269 _("Connecting a minimum of %u peers each (if possible)\n"),
5270 (unsigned int) option_modifier);
5272 choose_minimum (pg, (unsigned int) option_modifier);
5274 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
5275 #if VERBOSE_TOPOLOGY
5276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5277 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
5278 (unsigned int) option_modifier);
5281 perform_dfs (pg, (int) option_modifier);
5284 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
5285 #if VERBOSE_TOPOLOGY
5286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5287 ("Finding additional %u closest peers each (if possible)\n"),
5288 (unsigned int) option_modifier);
5291 add_closest (pg, (unsigned int) option_modifier,
5292 &add_connections, CONNECT);
5295 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
5297 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
5303 return connect_topology (pg, connect_timeout, connect_attempts,
5304 notify_callback, notify_cls);
5308 * Lookup and return the number of SSH connections to a host.
5310 * @param hostname the hostname to lookup in the list
5311 * @param pg the peergroup that the host belongs to
5313 * @return the number of current ssh connections to the host
5316 count_outstanding_at_host(const char *hostname,
5317 struct GNUNET_TESTING_PeerGroup *pg)
5319 struct OutstandingSSH *pos;
5321 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5323 GNUNET_assert(pos != NULL);
5324 return pos->outstanding;
5328 * Increment the number of SSH connections to a host by one.
5330 * @param hostname the hostname to lookup in the list
5331 * @param pg the peergroup that the host belongs to
5335 increment_outstanding_at_host(const char *hostname,
5336 struct GNUNET_TESTING_PeerGroup *pg)
5338 struct OutstandingSSH *pos;
5340 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5342 GNUNET_assert(pos != NULL);
5347 * Decrement the number of SSH connections to a host by one.
5349 * @param hostname the hostname to lookup in the list
5350 * @param pg the peergroup that the host belongs to
5354 decrement_outstanding_at_host(const char *hostname,
5355 struct GNUNET_TESTING_PeerGroup *pg)
5357 struct OutstandingSSH *pos;
5359 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5361 GNUNET_assert(pos != NULL);
5366 * Callback that is called whenever a hostkey is generated
5367 * for a peer. Call the real callback and decrement the
5368 * starting counter for the peergroup.
5370 * @param cls closure
5371 * @param id identifier for the daemon, NULL on error
5372 * @param d handle for the daemon
5373 * @param emsg error message (NULL on success)
5376 internal_hostkey_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5377 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5379 struct InternalStartContext *internal_context = cls;
5380 internal_context->peer->pg->starting--;
5381 internal_context->peer->pg->started++;
5382 if (internal_context->hostname != NULL)
5383 decrement_outstanding_at_host (internal_context->hostname,
5384 internal_context->peer->pg);
5385 if (internal_context->hostkey_callback != NULL)
5386 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
5388 else if (internal_context->peer->pg->started
5389 == internal_context->peer->pg->total)
5391 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
5392 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
5397 * Callback that is called whenever a peer has finished starting.
5398 * Call the real callback and decrement the starting counter
5399 * for the peergroup.
5401 * @param cls closure
5402 * @param id identifier for the daemon, NULL on error
5404 * @param d handle for the daemon
5405 * @param emsg error message (NULL on success)
5408 internal_startup_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5409 const struct GNUNET_CONFIGURATION_Handle *cfg,
5410 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5412 struct InternalStartContext *internal_context = cls;
5413 internal_context->peer->pg->starting--;
5414 if (internal_context->hostname != NULL)
5415 decrement_outstanding_at_host (internal_context->hostname,
5416 internal_context->peer->pg);
5417 if (internal_context->start_cb != NULL)
5418 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
5423 internal_continue_startup(void *cls,
5424 const struct GNUNET_SCHEDULER_TaskContext *tc)
5426 struct InternalStartContext *internal_context = cls;
5428 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
5433 if ((internal_context->peer->pg->starting
5434 < internal_context->peer->pg->max_concurrent_ssh)
5435 || ((internal_context->hostname != NULL)
5436 && (count_outstanding_at_host (internal_context->hostname,
5437 internal_context->peer->pg)
5438 < internal_context->peer->pg->max_concurrent_ssh)))
5440 if (internal_context->hostname != NULL)
5441 increment_outstanding_at_host (internal_context->hostname,
5442 internal_context->peer->pg);
5443 internal_context->peer->pg->starting++;
5444 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
5448 GNUNET_SCHEDULER_add_delayed (
5449 GNUNET_TIME_relative_multiply (
5450 GNUNET_TIME_UNIT_MILLISECONDS,
5452 &internal_continue_startup,
5458 * Callback for informing us about a successful
5459 * or unsuccessful churn start call.
5461 * @param cls a ChurnContext
5462 * @param id the peer identity of the started peer
5463 * @param cfg the handle to the configuration of the peer
5464 * @param d handle to the daemon for the peer
5465 * @param emsg NULL on success, non-NULL on failure
5469 churn_start_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5470 const struct GNUNET_CONFIGURATION_Handle *cfg,
5471 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5473 struct ChurnRestartContext *startup_ctx = cls;
5474 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
5476 unsigned int total_left;
5477 char *error_message;
5479 error_message = NULL;
5482 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5483 "Churn stop callback failed with error `%s'\n", emsg);
5484 churn_ctx->num_failed_start++;
5488 churn_ctx->num_to_start--;
5492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5493 "Started peer, %d left.\n", churn_ctx->num_to_start);
5496 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
5497 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5499 if (total_left == 0)
5501 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
5504 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5505 churn_ctx->num_failed_start,
5506 churn_ctx->num_failed_stop);
5507 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5508 GNUNET_free_non_null (error_message);
5509 GNUNET_free (churn_ctx);
5510 GNUNET_free (startup_ctx);
5515 schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5517 struct PeerRestartContext *peer_restart_ctx = cls;
5518 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
5520 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5521 GNUNET_SCHEDULER_add_delayed (
5522 GNUNET_TIME_relative_multiply (
5523 GNUNET_TIME_UNIT_MILLISECONDS,
5525 &schedule_churn_restart, peer_restart_ctx);
5528 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
5529 startup_ctx->timeout,
5530 &churn_start_callback, startup_ctx);
5531 GNUNET_free (peer_restart_ctx);
5537 internal_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5539 struct InternalStartContext *internal_context = cls;
5541 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
5546 if ((internal_context->peer->pg->starting
5547 < internal_context->peer->pg->max_concurrent_ssh)
5548 || ((internal_context->hostname != NULL)
5549 && (count_outstanding_at_host (internal_context->hostname,
5550 internal_context->peer->pg)
5551 < internal_context->peer->pg->max_concurrent_ssh)))
5553 if (internal_context->hostname != NULL)
5554 increment_outstanding_at_host (internal_context->hostname,
5555 internal_context->peer->pg);
5556 internal_context->peer->pg->starting++;
5557 internal_context->peer->daemon
5558 = GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
5559 internal_context->timeout,
5561 internal_context->hostname,
5562 internal_context->username,
5563 internal_context->sshport,
5564 internal_context->hostkey,
5565 &internal_hostkey_callback,
5567 &internal_startup_callback,
5572 GNUNET_SCHEDULER_add_delayed (
5573 GNUNET_TIME_relative_multiply (
5574 GNUNET_TIME_UNIT_MILLISECONDS,
5576 &internal_start, internal_context);
5579 #if USE_START_HELPER
5581 struct PeerStartHelperContext
5583 struct GNUNET_TESTING_PeerGroup *pg;
5585 struct HostData *host;
5589 start_peer_helper (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5591 struct PeerStartHelperContext *helper = cls;
5592 char *baseservicehome;
5594 struct GNUNET_OS_Process *proc;
5597 enum GNUNET_OS_ProcessStatusType type;
5599 GNUNET_TESTING_NotifyDaemonRunning cb;
5600 /* ssh user@host peerStartHelper /path/to/basedirectory */
5602 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (helper->pg->cfg, "PATHS", "SERVICEHOME",
5604 GNUNET_asprintf(&tempdir, "%s/%s/", baseservicehome, helper->host->hostname);
5605 if (NULL != helper->host->username)
5606 GNUNET_asprintf (&arg, "%s@%s", helper->host->username, helper->host->hostname);
5608 GNUNET_asprintf (&arg, "%s", helper->host->hostname);
5610 /* FIXME: Doesn't support ssh_port option! */
5611 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5612 "peerStartHelper.pl", tempdir, NULL);
5614 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "starting peers with cmd ssh %s %s %s\n", arg, "peerStartHelper.pl", tempdir);
5616 GNUNET_OS_process_wait (proc);
5617 if (GNUNET_OK != GNUNET_OS_process_status (proc, &type, &code))
5619 GNUNET_OS_process_close(proc);
5620 GNUNET_free (tempdir);
5621 GNUNET_free (baseservicehome);
5624 helper->pg->starting--;
5625 if (helper->pg->starting == 0) /* All peers have finished starting! */
5627 /* Call the peer started callback for each peer, set proper FSM state (?) */
5628 for (i = 0; i < helper->pg->total; i++)
5630 cb = helper->pg->peers[i].daemon->cb;
5631 helper->pg->peers[i].daemon->cb = NULL;
5635 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
5637 cb (helper->pg->peers[i].daemon->cb_cls,
5638 &helper->pg->peers[i].daemon->id,
5639 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5640 "Failed to execute peerStartHelper.pl, or return code bad!");
5643 cb (helper->pg->peers[i].daemon->cb_cls,
5644 &helper->pg->peers[i].daemon->id,
5645 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5657 * Function which continues a peer group starting up
5658 * after successfully generating hostkeys for each peer.
5660 * @param pg the peer group to continue starting
5664 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
5668 #if USE_START_HELPER
5669 if ((pg->num_hosts > 0) && (pg->hostkey_data != NULL))
5671 struct PeerStartHelperContext *helper;
5672 pg->starting = pg->num_hosts;
5673 for (i = 0; i < pg->num_hosts; i++)
5675 helper = GNUNET_malloc(sizeof(struct PeerStartHelperContext));
5677 helper->host = &pg->hosts[i];
5678 GNUNET_SCHEDULER_add_now(&start_peer_helper, helper);
5684 for (i = 0; i < pg->total; i++)
5686 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5687 &pg->peers[i].internal_context);
5692 for (i = 0; i < pg->total; i++)
5694 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5695 &pg->peers[i].internal_context);
5700 #if USE_START_HELPER
5702 call_hostkey_callbacks (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5704 struct GNUNET_TESTING_PeerGroup *pg = cls;
5706 for (i = 0; i < pg->total; i++)
5708 if (pg->peers[i].internal_context.hostkey_callback != NULL)
5709 pg->peers[i].internal_context.hostkey_callback (pg->peers[i].internal_context.hostkey_cls,
5710 &pg->peers[i].daemon->id,
5711 pg->peers[i].daemon,
5715 if (pg->peers[0].internal_context.hostkey_callback == NULL)
5716 GNUNET_TESTING_daemons_continue_startup (pg);
5721 * Start count gnunet instances with the same set of transports and
5722 * applications. The port numbers (any option called "PORT") will be
5723 * adjusted to ensure that no two peers running on the same system
5724 * have the same port(s) in their respective configurations.
5726 * @param cfg configuration template to use
5727 * @param total number of daemons to start
5728 * @param max_concurrent_connections for testing, how many peers can
5729 * we connect to simultaneously
5730 * @param max_concurrent_ssh when starting with ssh, how many ssh
5731 * connections will we allow at once (based on remote hosts allowed!)
5732 * @param timeout total time allowed for peers to start
5733 * @param hostkey_callback function to call on each peers hostkey generation
5734 * if NULL, peers will be started by this call, if non-null,
5735 * GNUNET_TESTING_daemons_continue_startup must be called after
5736 * successful hostkey generation
5737 * @param hostkey_cls closure for hostkey callback
5738 * @param cb function to call on each daemon that was started
5739 * @param cb_cls closure for cb
5740 * @param connect_callback function to call each time two hosts are connected
5741 * @param connect_callback_cls closure for connect_callback
5742 * @param hostnames linked list of host structs to use to start peers on
5743 * (NULL to run on localhost only)
5745 * @return NULL on error, otherwise handle to control peer group
5747 struct GNUNET_TESTING_PeerGroup *
5748 GNUNET_TESTING_daemons_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
5750 unsigned int max_concurrent_connections,
5751 unsigned int max_concurrent_ssh,
5752 struct GNUNET_TIME_Relative timeout,
5753 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
5755 GNUNET_TESTING_NotifyDaemonRunning cb,
5757 GNUNET_TESTING_NotifyConnection connect_callback,
5758 void *connect_callback_cls,
5759 const struct GNUNET_TESTING_Host *hostnames)
5761 struct GNUNET_TESTING_PeerGroup *pg;
5762 const struct GNUNET_TESTING_Host *hostpos;
5763 const char *hostname;
5764 const char *username;
5765 char *baseservicehome;
5766 char *newservicehome;
5768 char *hostkeys_file;
5771 struct GNUNET_DISK_FileHandle *fd;
5772 struct GNUNET_CONFIGURATION_Handle *pcfg;
5774 struct OutstandingSSH *ssh_entry;
5775 unsigned int hostcnt;
5782 uint64_t total_hostkeys;
5783 struct GNUNET_OS_Process *proc;
5793 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
5795 pg->notify_connection = connect_callback;
5796 pg->notify_connection_cls = connect_callback_cls;
5798 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
5799 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
5800 pg->max_outstanding_connections = max_concurrent_connections;
5801 pg->max_concurrent_ssh = max_concurrent_ssh;
5802 if (NULL != hostnames)
5805 hostpos = hostnames;
5806 while (hostpos != NULL)
5808 hostpos = hostpos->next;
5811 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5814 hostpos = hostnames;
5815 while (hostpos != NULL)
5817 pg->hosts[off].minport = LOW_PORT;
5818 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
5819 if (hostpos->username != NULL)
5820 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
5821 pg->hosts[off].sshport = hostpos->port;
5822 hostpos = hostpos->next;
5832 pg->num_hosts = off;
5840 /* Create the servicehome directory for each remote peer */
5841 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
5843 for (i = 0; i < pg->num_hosts; i++)
5845 ssh_entry = GNUNET_malloc(sizeof(struct OutstandingSSH));
5846 ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
5847 GNUNET_CONTAINER_DLL_insert(pg->ssh_head, pg->ssh_tail, ssh_entry);
5848 GNUNET_asprintf(&tmpdir, "%s/%s", baseservicehome, pg->hosts[i].hostname);
5849 if (NULL != pg->hosts[i].username)
5850 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username,
5851 pg->hosts[i].hostname);
5853 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
5854 if (pg->hosts[i].sshport != 0)
5856 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
5857 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-P",
5862 arg, "mkdir -p", tmpdir,
5866 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5867 "mkdir -p", tmpdir, NULL);
5868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5869 "Creating remote dir with command ssh %s %s %s\n", arg,
5870 " mkdir -p ", tmpdir);
5871 GNUNET_free(tmpdir);
5873 GNUNET_OS_process_wait (proc);
5874 GNUNET_OS_process_close(proc);
5876 GNUNET_free(baseservicehome);
5878 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING",
5882 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
5883 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!\n");
5886 /* Check hostkey file size, read entire thing into memory */
5887 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
5888 GNUNET_DISK_PERM_NONE);
5891 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
5895 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs,
5900 GNUNET_ERROR_TYPE_WARNING,
5901 "Found file size %llu for hostkeys, expect hostkeys to be size %d\n",
5902 fs, HOSTKEYFILESIZE);
5904 if (fs % HOSTKEYFILESIZE != 0)
5906 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5907 "File size %llu seems incorrect for hostkeys...\n",
5912 total_hostkeys = fs / HOSTKEYFILESIZE;
5913 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5914 "Will read %llu hostkeys from file\n", total_hostkeys);
5915 pg->hostkey_data = GNUNET_malloc_large (fs);
5916 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
5917 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
5920 GNUNET_free(hostkeys_file);
5923 for (off = 0; off < total; off++)
5927 hostname = pg->hosts[off % hostcnt].hostname;
5928 username = pg->hosts[off % hostcnt].username;
5929 sshport = pg->hosts[off % hostcnt].sshport;
5930 pcfg = make_config (cfg, off, &pg->hosts[off % hostcnt].minport,
5931 &upnum, hostname, &fdnum);
5938 pcfg = make_config (cfg, off, &minport, &upnum, hostname, &fdnum);
5943 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5944 ("Could not create configuration for peer number %u on `%s'!\n"),
5945 off, hostname == NULL ? "localhost" : hostname);
5950 == GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS",
5954 if (hostname != NULL)
5955 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", baseservicehome, hostname, off);
5957 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
5958 #if !USE_START_HELPER
5959 GNUNET_free (baseservicehome);
5964 tmpdir = getenv ("TMPDIR");
5965 tmpdir = tmpdir ? tmpdir : "/tmp";
5966 if (hostname != NULL)
5967 GNUNET_asprintf (&newservicehome, "%s/%s/%s/%d/", tmpdir, hostname,
5968 "gnunet-testing-test-test", off);
5970 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", tmpdir,
5971 "gnunet-testing-test-test", off);
5973 GNUNET_CONFIGURATION_set_value_string (pcfg, "PATHS", "SERVICEHOME",
5975 GNUNET_free (newservicehome);
5976 pg->peers[off].cfg = pcfg;
5977 pg->peers[off].pg = pg;
5978 pg->peers[off].internal_context.peer = &pg->peers[off];
5979 pg->peers[off].internal_context.timeout = timeout;
5980 pg->peers[off].internal_context.hostname = hostname;
5981 pg->peers[off].internal_context.username = username;
5982 pg->peers[off].internal_context.sshport = sshport;
5983 if (pg->hostkey_data != NULL)
5984 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off
5986 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
5987 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
5988 pg->peers[off].internal_context.start_cb = cb;
5989 pg->peers[off].internal_context.start_cb_cls = cb_cls;
5990 #if !USE_START_HELPER
5991 GNUNET_SCHEDULER_add_now (&internal_start,
5992 &pg->peers[off].internal_context);
5994 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
5996 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Have hostkey data and running on remote hosts!\n");
5997 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (pcfg,
6003 pg->peers[off].internal_context.hostkey,
6004 &internal_hostkey_callback,
6005 &pg->peers[off].internal_context,
6006 &internal_startup_callback,
6007 &pg->peers[off].internal_context);
6009 * At this point, given that we had a hostkeyfile,
6010 * we can call the hostkey callback!
6011 * But first, we should copy (rsync) all of the configs
6012 * and hostkeys to the remote peers. Then let topology
6013 * creation happen, then call the peer start helper processes,
6014 * then set pg->whatever_phase for each peer and let them
6015 * enter the fsm to get the HELLO's for peers and start connecting.
6020 GNUNET_SCHEDULER_add_now (&internal_start,
6021 &pg->peers[off].internal_context);
6027 #if USE_START_HELPER /* Now the peergroup has been set up, hostkeys and configs written to files. */
6028 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6030 for (off = 0; off < hostcnt; off++)
6032 GNUNET_asprintf(&newservicehome, "%s/%s/", baseservicehome, pg->hosts[off].hostname);
6034 if (NULL != username)
6035 GNUNET_asprintf (&arg, "%s@%s:%s", username, pg->hosts[off].hostname, newservicehome);
6037 GNUNET_asprintf (&arg, "%s:%s", pg->hosts[off].hostname, newservicehome);
6039 /* FIXME: Doesn't support ssh_port option! */
6040 proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
6044 newservicehome, arg, NULL);
6046 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "copying directory with command scp -r %s %s\n", newservicehome, arg);
6053 GNUNET_ERROR_TYPE_ERROR,
6055 ("Could not start `%s' process to copy configuration directory.\n"),
6059 GNUNET_OS_process_wait (proc);
6060 GNUNET_OS_process_close (proc);
6062 /* Now all the configuration files and hostkeys are copied to the remote host. Call the hostkey callback for each peer! */
6063 GNUNET_SCHEDULER_add_now(&call_hostkey_callbacks, pg);
6065 GNUNET_free (baseservicehome);
6071 * Get a daemon by number, so callers don't have to do nasty
6072 * offsetting operation.
6074 struct GNUNET_TESTING_Daemon *
6075 GNUNET_TESTING_daemon_get(struct GNUNET_TESTING_PeerGroup *pg,
6076 unsigned int position)
6078 if (position < pg->total)
6079 return pg->peers[position].daemon;
6085 * Get a daemon by peer identity, so callers can
6086 * retrieve the daemon without knowing it's offset.
6088 * @param pg the peer group to retrieve the daemon from
6089 * @param peer_id the peer identity of the daemon to retrieve
6091 * @return the daemon on success, or NULL if no such peer identity is found
6093 struct GNUNET_TESTING_Daemon *
6094 GNUNET_TESTING_daemon_get_by_id(struct GNUNET_TESTING_PeerGroup *pg,
6095 struct GNUNET_PeerIdentity *peer_id)
6099 for (i = 0; i < pg->total; i++)
6101 if (0 == memcmp (&pg->peers[i].daemon->id, peer_id,
6102 sizeof(struct GNUNET_PeerIdentity)))
6103 return pg->peers[i].daemon;
6110 * Prototype of a function that will be called when a
6111 * particular operation was completed the testing library.
6113 * @param cls closure (a struct RestartContext)
6114 * @param id id of the peer that was restarted
6115 * @param cfg handle to the configuration of the peer
6116 * @param d handle to the daemon that was restarted
6117 * @param emsg NULL on success
6120 restart_callback(void *cls, const struct GNUNET_PeerIdentity *id,
6121 const struct GNUNET_CONFIGURATION_Handle *cfg,
6122 struct GNUNET_TESTING_Daemon *d, const char *emsg)
6124 struct RestartContext *restart_context = cls;
6128 restart_context->peers_restarted++;
6132 restart_context->peers_restart_failed++;
6135 if (restart_context->peers_restarted == restart_context->peer_group->total)
6137 restart_context->callback (restart_context->callback_cls, NULL);
6138 GNUNET_free (restart_context);
6140 else if (restart_context->peers_restart_failed
6141 + restart_context->peers_restarted == restart_context->peer_group->total)
6143 restart_context->callback (restart_context->callback_cls,
6144 "Failed to restart peers!");
6145 GNUNET_free (restart_context);
6151 * Callback for informing us about a successful
6152 * or unsuccessful churn stop call.
6154 * @param cls a ChurnContext
6155 * @param emsg NULL on success, non-NULL on failure
6159 churn_stop_callback(void *cls, const char *emsg)
6161 struct ShutdownContext *shutdown_ctx = cls;
6162 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
6163 unsigned int total_left;
6164 char *error_message;
6166 error_message = NULL;
6167 shutdown_ctx->outstanding--;
6171 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6172 "Churn stop callback failed with error `%s'\n", emsg);
6173 churn_ctx->num_failed_stop++;
6177 churn_ctx->num_to_stop--;
6181 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6182 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
6184 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
6185 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
6187 if (total_left == 0)
6189 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
6193 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
6194 churn_ctx->num_failed_start,
6195 churn_ctx->num_failed_stop);
6197 churn_ctx->cb (churn_ctx->cb_cls, error_message);
6198 GNUNET_free_non_null (error_message);
6199 GNUNET_free (churn_ctx);
6200 GNUNET_free (shutdown_ctx);
6205 * Count the number of running peers.
6207 * @param pg handle for the peer group
6209 * @return the number of currently running peers in the peer group
6212 GNUNET_TESTING_daemons_running(struct GNUNET_TESTING_PeerGroup *pg)
6215 unsigned int running = 0;
6216 for (i = 0; i < pg->total; i++)
6218 if (pg->peers[i].daemon->running == GNUNET_YES)
6220 GNUNET_assert (running != -1);
6228 * Task to rate limit the number of outstanding peer shutdown
6229 * requests. This is necessary for making sure we don't do
6230 * too many ssh connections at once, but is generally nicer
6231 * to any system as well (graduated task starts, as opposed
6232 * to calling gnunet-arm N times all at once).
6235 schedule_churn_shutdown_task(void *cls,
6236 const struct GNUNET_SCHEDULER_TaskContext *tc)
6238 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6239 struct ShutdownContext *shutdown_ctx;
6240 struct ChurnContext *churn_ctx;
6241 GNUNET_assert (peer_shutdown_ctx != NULL);
6242 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6243 GNUNET_assert (shutdown_ctx != NULL);
6244 churn_ctx = (struct ChurnContext *) shutdown_ctx->cb_cls;
6245 if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
6246 GNUNET_SCHEDULER_add_delayed (
6247 GNUNET_TIME_relative_multiply (
6248 GNUNET_TIME_UNIT_MILLISECONDS,
6250 &schedule_churn_shutdown_task,
6254 shutdown_ctx->outstanding++;
6255 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6256 shutdown_ctx->timeout, shutdown_ctx->cb,
6257 shutdown_ctx, GNUNET_NO, GNUNET_YES);
6258 GNUNET_free (peer_shutdown_ctx);
6263 * Simulate churn by stopping some peers (and possibly
6264 * re-starting others if churn is called multiple times). This
6265 * function can only be used to create leave-join churn (peers "never"
6266 * leave for good). First "voff" random peers that are currently
6267 * online will be taken offline; then "von" random peers that are then
6268 * offline will be put back online. No notifications will be
6269 * generated for any of these operations except for the callback upon
6272 * @param pg handle for the peer group
6273 * @param voff number of peers that should go offline
6274 * @param von number of peers that should come back online;
6275 * must be zero on first call (since "testbed_start"
6276 * always starts all of the peers)
6277 * @param timeout how long to wait for operations to finish before
6279 * @param cb function to call at the end
6280 * @param cb_cls closure for cb
6283 GNUNET_TESTING_daemons_churn(struct GNUNET_TESTING_PeerGroup *pg,
6284 unsigned int voff, unsigned int von,
6285 struct GNUNET_TIME_Relative timeout,
6286 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6288 struct ChurnContext *churn_ctx;
6289 struct ShutdownContext *shutdown_ctx;
6290 struct PeerShutdownContext *peer_shutdown_ctx;
6291 struct PeerRestartContext *peer_restart_ctx;
6292 struct ChurnRestartContext *churn_startup_ctx;
6294 unsigned int running;
6295 unsigned int stopped;
6296 unsigned int total_running;
6297 unsigned int total_stopped;
6299 unsigned int *running_arr;
6300 unsigned int *stopped_arr;
6301 unsigned int *running_permute;
6302 unsigned int *stopped_permute;
6307 if ((von == 0) && (voff == 0)) /* No peers at all? */
6313 for (i = 0; i < pg->total; i++)
6315 if (pg->peers[i].daemon->running == GNUNET_YES)
6317 GNUNET_assert (running != -1);
6322 GNUNET_assert (stopped != -1);
6329 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6330 "Trying to stop more peers than are currently running!\n");
6331 cb (cb_cls, "Trying to stop more peers than are currently running!");
6337 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6338 "Trying to start more peers than are currently stopped!\n");
6339 cb (cb_cls, "Trying to start more peers than are currently stopped!");
6343 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6347 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
6351 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
6353 running_permute = NULL;
6354 stopped_permute = NULL;
6357 running_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6360 stopped_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6363 total_running = running;
6364 total_stopped = stopped;
6368 churn_ctx->num_to_start = von;
6369 churn_ctx->num_to_stop = voff;
6371 churn_ctx->cb_cls = cb_cls;
6374 for (i = 0; i < pg->total; i++)
6376 if (pg->peers[i].daemon->running == GNUNET_YES)
6378 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6379 running_arr[running] = i;
6384 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6385 stopped_arr[stopped] = i;
6390 GNUNET_assert (running >= voff);
6393 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6394 shutdown_ctx->cb = &churn_stop_callback;
6395 shutdown_ctx->cb_cls = churn_ctx;
6396 shutdown_ctx->total_peers = voff;
6397 shutdown_ctx->timeout = timeout;
6400 for (i = 0; i < voff; i++)
6403 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
6404 running_permute[i]);
6406 GNUNET_assert (running_arr != NULL);
6407 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6408 peer_shutdown_ctx->daemon
6409 = pg->peers[running_arr[running_permute[i]]].daemon;
6410 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6411 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
6415 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
6417 &churn_stop_callback, churn_ctx,
6418 GNUNET_NO, GNUNET_YES); */
6421 GNUNET_assert (stopped >= von);
6424 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6425 churn_startup_ctx->churn_ctx = churn_ctx;
6426 churn_startup_ctx->timeout = timeout;
6427 churn_startup_ctx->pg = pg;
6429 for (i = 0; i < von; i++)
6432 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
6433 stopped_permute[i]);
6435 GNUNET_assert (stopped_arr != NULL);
6436 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
6437 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
6438 peer_restart_ctx->daemon
6439 = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
6440 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
6442 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
6443 timeout, &churn_start_callback, churn_ctx); */
6446 GNUNET_free_non_null (running_arr);
6447 GNUNET_free_non_null (stopped_arr);
6448 GNUNET_free_non_null (running_permute);
6449 GNUNET_free_non_null (stopped_permute);
6453 * Restart all peers in the given group.
6455 * @param pg the handle to the peer group
6456 * @param callback function to call on completion (or failure)
6457 * @param callback_cls closure for the callback function
6460 GNUNET_TESTING_daemons_restart(struct GNUNET_TESTING_PeerGroup *pg,
6461 GNUNET_TESTING_NotifyCompletion callback,
6464 struct RestartContext *restart_context;
6469 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
6470 restart_context->peer_group = pg;
6471 restart_context->peers_restarted = 0;
6472 restart_context->callback = callback;
6473 restart_context->callback_cls = callback_cls;
6475 for (off = 0; off < pg->total; off++)
6477 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
6478 &restart_callback, restart_context);
6484 * Start or stop an individual peer from the given group.
6486 * @param pg handle to the peer group
6487 * @param offset which peer to start or stop
6488 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
6489 * @param timeout how long to wait for shutdown
6490 * @param cb function to call at the end
6491 * @param cb_cls closure for cb
6494 GNUNET_TESTING_daemons_vary(struct GNUNET_TESTING_PeerGroup *pg,
6495 unsigned int offset, int desired_status,
6496 struct GNUNET_TIME_Relative timeout,
6497 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6499 struct ShutdownContext *shutdown_ctx;
6500 struct ChurnRestartContext *startup_ctx;
6501 struct ChurnContext *churn_ctx;
6503 if (GNUNET_NO == desired_status)
6505 if (NULL != pg->peers[offset].daemon)
6507 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6508 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6509 churn_ctx->num_to_start = 0;
6510 churn_ctx->num_to_stop = 1;
6512 churn_ctx->cb_cls = cb_cls;
6513 shutdown_ctx->cb_cls = churn_ctx;
6514 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon, timeout,
6515 &churn_stop_callback, shutdown_ctx,
6516 GNUNET_NO, GNUNET_YES);
6519 else if (GNUNET_YES == desired_status)
6521 if (NULL == pg->peers[offset].daemon)
6523 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6524 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6525 churn_ctx->num_to_start = 1;
6526 churn_ctx->num_to_stop = 0;
6528 churn_ctx->cb_cls = cb_cls;
6529 startup_ctx->churn_ctx = churn_ctx;
6530 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
6531 timeout, &churn_start_callback,
6540 * Callback for shutting down peers in a peer group.
6542 * @param cls closure (struct ShutdownContext)
6543 * @param emsg NULL on success
6546 internal_shutdown_callback(void *cls, const char *emsg)
6548 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6549 struct ShutdownContext *shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6551 struct OutstandingSSH *ssh_pos;
6553 shutdown_ctx->outstanding--;
6554 if (peer_shutdown_ctx->daemon->hostname != NULL)
6555 decrement_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6560 shutdown_ctx->peers_down++;
6564 shutdown_ctx->peers_failed++;
6567 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down
6568 + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
6570 if (shutdown_ctx->peers_failed > 0)
6571 shutdown_ctx->cb (shutdown_ctx->cb_cls,
6572 "Not all peers successfully shut down!");
6574 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
6576 GNUNET_free (shutdown_ctx->pg->peers);
6577 GNUNET_free_non_null(shutdown_ctx->pg->hostkey_data);
6578 for (off = 0; off < shutdown_ctx->pg->num_hosts; off++)
6580 GNUNET_free (shutdown_ctx->pg->hosts[off].hostname);
6581 GNUNET_free_non_null (shutdown_ctx->pg->hosts[off].username);
6583 GNUNET_free_non_null (shutdown_ctx->pg->hosts);
6584 while (NULL != (ssh_pos = shutdown_ctx->pg->ssh_head))
6586 GNUNET_CONTAINER_DLL_remove(shutdown_ctx->pg->ssh_head, shutdown_ctx->pg->ssh_tail, ssh_pos);
6587 GNUNET_free(ssh_pos);
6589 GNUNET_free (shutdown_ctx->pg);
6590 GNUNET_free (shutdown_ctx);
6592 GNUNET_free(peer_shutdown_ctx);
6596 * Task to rate limit the number of outstanding peer shutdown
6597 * requests. This is necessary for making sure we don't do
6598 * too many ssh connections at once, but is generally nicer
6599 * to any system as well (graduated task starts, as opposed
6600 * to calling gnunet-arm N times all at once).
6603 schedule_shutdown_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6605 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6606 struct ShutdownContext *shutdown_ctx;
6608 GNUNET_assert (peer_shutdown_ctx != NULL);
6609 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6610 GNUNET_assert (shutdown_ctx != NULL);
6612 if ((shutdown_ctx->outstanding < shutdown_ctx->pg->max_concurrent_ssh)
6613 || ((peer_shutdown_ctx->daemon->hostname != NULL)
6614 && (count_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6616 < shutdown_ctx->pg->max_concurrent_ssh)))
6618 if (peer_shutdown_ctx->daemon->hostname != NULL)
6619 increment_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6621 shutdown_ctx->outstanding++;
6622 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6623 shutdown_ctx->timeout,
6624 &internal_shutdown_callback, peer_shutdown_ctx,
6625 GNUNET_YES, GNUNET_NO);
6628 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
6630 &schedule_shutdown_task, peer_shutdown_ctx);
6635 * Shutdown all peers started in the given group.
6637 * @param pg handle to the peer group
6638 * @param timeout how long to wait for shutdown
6639 * @param cb callback to notify upon success or failure
6640 * @param cb_cls closure for cb
6643 GNUNET_TESTING_daemons_stop(struct GNUNET_TESTING_PeerGroup *pg,
6644 struct GNUNET_TIME_Relative timeout,
6645 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6648 struct ShutdownContext *shutdown_ctx;
6649 struct PeerShutdownContext *peer_shutdown_ctx;
6651 struct PeerConnection *conn_iter;
6652 struct PeerConnection *temp_conn;
6655 GNUNET_assert (pg->total > 0);
6657 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6658 shutdown_ctx->cb = cb;
6659 shutdown_ctx->cb_cls = cb_cls;
6660 shutdown_ctx->total_peers = pg->total;
6661 shutdown_ctx->timeout = timeout;
6662 shutdown_ctx->pg = pg;
6663 /* shtudown_ctx->outstanding = 0; */
6665 for (off = 0; off < pg->total; off++)
6667 GNUNET_assert (NULL != pg->peers[off].daemon);
6668 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6669 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
6670 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6671 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
6673 if (NULL != pg->peers[off].cfg)
6674 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
6676 conn_iter = pg->peers[off].allowed_peers_head;
6677 while (conn_iter != NULL)
6679 temp_conn = conn_iter->next;
6680 GNUNET_free(conn_iter);
6681 conn_iter = temp_conn;
6684 conn_iter = pg->peers[off].connect_peers_head;
6685 while (conn_iter != NULL)
6687 temp_conn = conn_iter->next;
6688 GNUNET_free(conn_iter);
6689 conn_iter = temp_conn;
6692 conn_iter = pg->peers[off].blacklisted_peers_head;
6693 while (conn_iter != NULL)
6695 temp_conn = conn_iter->next;
6696 GNUNET_free(conn_iter);
6697 conn_iter = temp_conn;
6700 conn_iter = pg->peers[off].connect_peers_working_set_head;
6701 while (conn_iter != NULL)
6703 temp_conn = conn_iter->next;
6704 GNUNET_free(conn_iter);
6705 conn_iter = temp_conn;
6708 if (pg->peers[off].allowed_peers != NULL)
6709 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
6710 if (pg->peers[off].connect_peers != NULL)
6711 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
6712 if (pg->peers[off].blacklisted_peers != NULL)
6713 GNUNET_CONTAINER_multihashmap_destroy (pg->
6714 peers[off].blacklisted_peers);
6719 /* end of testing_group.c */