2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing_group.c
23 * @brief convenience API for writing testcases for GNUnet
24 * @author Nathan Evans
25 * @author Christian Grothoff
29 #include "gnunet_constants.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_testing_lib.h"
32 #include "gnunet_core_service.h"
34 #define VERBOSE_TESTING GNUNET_NO
36 #define VERBOSE_TOPOLOGY GNUNET_NO
38 #define DEBUG_CHURN GNUNET_NO
40 #define USE_START_HELPER GNUNET_YES
44 /* Before connecting peers, send all of the HELLOs */
45 #define USE_SEND_HELLOS GNUNET_NO
47 #define TOPOLOGY_HACK GNUNET_YES
51 * Lowest port used for GNUnet testing. Should be high enough to not
52 * conflict with other applications running on the hosts but be low
53 * enough to not conflict with client-ports (typically starting around
56 #define LOW_PORT 12000
59 * Highest port used for GNUnet testing. Should be low enough to not
60 * conflict with the port range for "local" ports (client apps; see
61 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
63 #define HIGH_PORT 56000
65 /* Maximum time to delay connect attempt */
66 #define MAX_CONNECT_DELAY 300
69 * Which list of peers do we need to modify?
73 /** Modify allowed peers */
76 /** Modify connect peers */
79 /** Modify blacklist peers */
82 /** Modify workingset peers */
87 * Prototype of a function called whenever two peers would be connected
88 * in a certain topology.
91 (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup * pg,
92 unsigned int first, unsigned int second,
93 enum PeerLists list, unsigned int check);
96 * Context for handling churning a peer group
101 * The peergroup we are dealing with.
103 struct GNUNET_TESTING_PeerGroup *pg;
106 * Name of the service to churn on/off, NULL
107 * to churn entire peer.
112 * Callback used to notify of churning finished
114 GNUNET_TESTING_NotifyCompletion cb;
117 * Closure for callback
122 * Number of peers that still need to be started
124 unsigned int num_to_start;
127 * Number of peers that still need to be stopped
129 unsigned int num_to_stop;
132 * Number of peers that failed to start
134 unsigned int num_failed_start;
137 * Number of peers that failed to stop
139 unsigned int num_failed_stop;
142 struct RestartContext
145 * The group of peers being restarted
147 struct GNUNET_TESTING_PeerGroup *peer_group;
150 * How many peers have been restarted thus far
152 unsigned int peers_restarted;
155 * How many peers got an error when restarting
157 unsigned int peers_restart_failed;
160 * The function to call once all peers have been restarted
162 GNUNET_TESTING_NotifyCompletion callback;
165 * Closure for callback function
171 struct SendHelloContext
174 * Global handle to the peer group.
176 struct GNUNET_TESTING_PeerGroup *pg;
179 * The data about this specific peer.
181 struct PeerData *peer;
184 * The next HELLO that needs sent to this peer.
186 struct PeerConnection *peer_pos;
189 * Are we connected to CORE yet?
191 unsigned int core_ready;
194 * How many attempts should we make for failed connections?
196 unsigned int connect_attempts;
199 * Task for scheduling core connect requests to be sent.
201 GNUNET_SCHEDULER_TaskIdentifier core_connect_task;
204 struct ShutdownContext
206 struct GNUNET_TESTING_PeerGroup *pg;
208 * Total peers to wait for
210 unsigned int total_peers;
213 * Number of peers successfully shut down
215 unsigned int peers_down;
218 * Number of peers failed to shut down
220 unsigned int peers_failed;
223 * Number of peers we have started shutting
224 * down. If too many, wait on them.
226 unsigned int outstanding;
229 * Timeout for shutdown.
231 struct GNUNET_TIME_Relative timeout;
234 * Callback to call when all peers either
235 * shutdown or failed to shutdown
237 GNUNET_TESTING_NotifyCompletion cb;
245 * Should we delete all of the files from the peers?
251 * Individual shutdown context for a particular peer.
253 struct PeerShutdownContext
256 * Pointer to the high level shutdown context.
258 struct ShutdownContext *shutdown_ctx;
261 * The daemon handle for the peer to shut down.
263 struct GNUNET_TESTING_Daemon *daemon;
267 * Individual shutdown context for a particular peer.
269 struct PeerRestartContext
272 * Pointer to the high level restart context.
274 struct ChurnRestartContext *churn_restart_ctx;
277 * The daemon handle for the peer to shut down.
279 struct GNUNET_TESTING_Daemon *daemon;
282 struct CreateTopologyContext
286 * Function to call with number of connections
288 GNUNET_TESTING_NotifyConnections cont;
291 * Closure for connection notification
298 /** Waiting to read number of peers */
301 /** Should find next peer index */
304 /** Should find colon */
307 /** Should read other peer index, space, or endline */
312 struct PeerConnection
317 struct PeerConnection *prev;
322 struct PeerConnection *next;
325 * Index of daemon in pg->peers
332 struct InternalStartContext
335 * Pointer to peerdata
337 struct PeerData *peer;
340 * Timeout for peer startup
342 struct GNUNET_TIME_Relative timeout;
345 * Client callback for hostkey notification
347 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
350 * Closure for hostkey_callback
355 * Client callback for peer start notification
357 GNUNET_TESTING_NotifyDaemonRunning start_cb;
365 * Hostname, where to start the peer
367 const char *hostname;
370 * Username to use when connecting to the
373 const char *username;
376 * Pointer to starting memory location of a hostkey
381 * Port to use for ssh.
387 struct ChurnRestartContext
390 * PeerGroup that we are working with.
392 struct GNUNET_TESTING_PeerGroup *pg;
395 * Number of restarts currently in flight.
397 unsigned int outstanding;
400 * Handle to the underlying churn context.
402 struct ChurnContext *churn_ctx;
405 * How long to allow the operation to take.
407 struct GNUNET_TIME_Relative timeout;
410 struct OutstandingSSH
412 struct OutstandingSSH *next;
414 struct OutstandingSSH *prev;
417 * Number of current ssh connections.
419 uint32_t outstanding;
422 * The hostname of this peer.
424 const char *hostname;
428 * Data we keep per peer.
433 * (Initial) configuration of the host.
434 * (initial because clients could change
435 * it and we would not know about those
438 struct GNUNET_CONFIGURATION_Handle *cfg;
441 * Handle for controlling the daemon.
443 struct GNUNET_TESTING_Daemon *daemon;
446 * The peergroup this peer belongs to.
448 struct GNUNET_TESTING_PeerGroup *pg;
452 * Linked list of allowed peer connections.
454 struct PeerConnection *allowed_peers_head;
457 * Linked list of allowed peer connections.
459 struct PeerConnection *allowed_peers_tail;
462 * Linked list of blacklisted peer connections.
464 struct PeerConnection *blacklisted_peers_head;
467 * Linked list of blacklisted peer connections.
469 struct PeerConnection *blacklisted_peers_tail;
472 * Linked list of connect peer connections.
474 struct PeerConnection *connect_peers_head;
477 * Linked list of connect peer connections.
479 struct PeerConnection *connect_peers_tail;
482 * Linked list of connect peer connections.
484 struct PeerConnection *connect_peers_working_set_head;
487 * Linked list of connect peer connections.
489 struct PeerConnection *connect_peers_working_set_tail;
493 * Hash map of allowed peer connections (F2F created topology)
495 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
498 * Hash map of blacklisted peers
500 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
503 * Hash map of peer connections
505 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
508 * Temporary hash map of peer connections
510 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
514 * Temporary variable for topology creation, should be reset before
515 * creating any topology so the count is valid once finished.
520 * Context to keep track of peers being started, to
521 * stagger hostkey generation and peer startup.
523 struct InternalStartContext internal_context;
527 * Linked list of per-host data.
537 * SSH username to use when connecting to this host.
542 * SSH port to use when connecting to this host.
547 * Lowest port that we have not yet used
553 struct TopologyIterateContext
556 * The peergroup we are working with.
558 struct GNUNET_TESTING_PeerGroup *pg;
561 * Callback for notifying of two connected peers.
563 GNUNET_TESTING_NotifyTopology topology_cb;
566 * Closure for topology_cb
571 * Number of peers currently connected to.
573 unsigned int connected;
576 * Number of peers we have finished iterating.
578 unsigned int completed;
581 * Number of peers total.
586 struct StatsIterateContext
589 * The peergroup that we are dealing with.
591 struct GNUNET_TESTING_PeerGroup *pg;
594 * Continuation to call once all stats information has been retrieved.
596 GNUNET_STATISTICS_Callback cont;
599 * Proc function to call on each value received.
601 GNUNET_TESTING_STATISTICS_Iterator proc;
604 * Closure for topology_cb
609 * Number of peers currently connected to.
611 unsigned int connected;
614 * Number of peers we have finished iterating.
616 unsigned int completed;
619 * Number of peers total.
627 struct GNUNET_TESTING_Daemon *daemon;
630 struct StatsCoreContext
633 struct GNUNET_TESTING_Daemon *daemon;
635 * Handle to the statistics service.
637 struct GNUNET_STATISTICS_Handle *stats_handle;
640 * Handle for getting statistics.
642 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
645 struct ConnectTopologyContext
648 * How many connections are left to create.
650 unsigned int remaining_connections;
653 * Handle to group of peers.
655 struct GNUNET_TESTING_PeerGroup *pg;
658 * How long to try this connection before timing out.
660 struct GNUNET_TIME_Relative connect_timeout;
663 * How many times to retry connecting the two peers.
665 unsigned int connect_attempts;
668 * Temp value set for each iteration.
670 //struct PeerData *first;
673 * Notification that all peers are connected.
675 GNUNET_TESTING_NotifyCompletion notify_connections_done;
678 * Closure for notify.
684 * Handle to a group of GNUnet peers.
686 struct GNUNET_TESTING_PeerGroup
689 * Configuration template.
691 const struct GNUNET_CONFIGURATION_Handle *cfg;
694 * Function to call on each started daemon.
696 //GNUNET_TESTING_NotifyDaemonRunning cb;
704 * Function to call on each topology connection created
706 GNUNET_TESTING_NotifyConnection notify_connection;
709 * Callback for notify_connection
711 void *notify_connection_cls;
714 * Array of information about hosts.
716 struct HostData *hosts;
719 * Number of hosts (size of HostData)
721 unsigned int num_hosts;
724 * Array of "total" peers.
726 struct PeerData *peers;
729 * Number of peers in this group.
734 * At what time should we fail the peer startup process?
736 struct GNUNET_TIME_Absolute max_timeout;
739 * How many peers are being started right now?
741 unsigned int starting;
744 * How many peers have already been started?
746 unsigned int started;
749 * Number of possible connections to peers
752 unsigned int max_outstanding_connections;
755 * Number of ssh connections to peers (max).
757 unsigned int max_concurrent_ssh;
760 * Number of connects we are waiting on, allows us to rate limit
763 unsigned int outstanding_connects;
766 * Number of HELLOs we have yet to send.
768 unsigned int remaining_hellos;
771 * How many connects have already been scheduled?
773 unsigned int total_connects_scheduled;
776 * Hostkeys loaded from a file.
781 * Head of DLL to keep track of the number of outstanding
782 * ssh connections per peer.
784 struct OutstandingSSH *ssh_head;
787 * Tail of DLL to keep track of the number of outstanding
788 * ssh connections per peer.
790 struct OutstandingSSH *ssh_tail;
793 * Stop scheduling peers connecting.
795 unsigned int stop_connects;
798 * Connection context for peer group.
800 struct ConnectTopologyContext ct_ctx;
806 * The altered configuration.
808 struct GNUNET_CONFIGURATION_Handle *ret;
811 * The original configuration to alter.
813 const struct GNUNET_CONFIGURATION_Handle *orig;
816 * The hostname that this peer will run on.
818 const char *hostname;
821 * The next possible port to assign.
826 * Unique number for unix domain sockets.
831 * Unique number for this peer/host to offset
832 * things that are grouped by host.
837 struct ConnectContext
840 * Index of peer to connect second to.
842 uint32_t first_index;
845 * Index of peer to connect first to.
847 uint32_t second_index;
850 * Higher level topology connection context.
852 struct ConnectTopologyContext *ct_ctx;
855 * Whether this connection has been accounted for in the schedule_connect call.
860 struct UnblacklistContext
865 struct GNUNET_TESTING_PeerGroup *pg;
868 * uid of the first peer
878 struct GNUNET_TESTING_PeerGroup *pg;
881 * uid of the first peer
886 * Peer data for first peer.
888 struct PeerData *first;
891 * Random percentage to use
896 struct MinimumContext
901 struct GNUNET_TESTING_PeerGroup *pg;
904 * uid of the first peer
909 * Peer data for first peer.
911 struct PeerData *first;
914 * Number of conns per peer
916 unsigned int num_to_add;
919 * Permuted array of all possible connections. Only add the Nth
920 * peer if it's in the Nth position.
922 unsigned int *pg_array;
925 * What number is the current element we are iterating over?
927 unsigned int current;
935 struct GNUNET_TESTING_PeerGroup *pg;
938 * uid of the first peer
943 * uid of the second peer
948 * Peer data for first peer.
950 struct PeerData *first;
953 * Which peer has been chosen as the one to add?
958 * What number is the current element we are iterating over?
960 unsigned int current;
964 * Simple struct to keep track of progress, and print a
965 * nice little percentage meter for long running tasks.
975 unsigned int completed;
979 char *startup_string;
984 * Convert unique ID to hash code.
986 * @param uid unique ID to convert
987 * @param hash set to uid (extended with zeros)
990 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
992 memset (hash, 0, sizeof (GNUNET_HashCode));
993 *((uint32_t *) hash) = uid;
997 * Convert hash code to unique ID.
999 * @param uid unique ID to convert
1000 * @param hash set to uid (extended with zeros)
1003 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
1005 memcpy (uid, hash, sizeof (uint32_t));
1010 static struct GNUNET_CORE_MessageHandler no_handlers[] =
1016 * Create a meter to keep track of the progress of some task.
1018 * @param total the total number of items to complete
1019 * @param start_string a string to prefix the meter with (if printing)
1020 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
1023 * @return the progress meter
1025 static struct ProgressMeter *
1026 create_meter(unsigned int total, char * start_string, int print)
1028 struct ProgressMeter *ret;
1029 ret = GNUNET_malloc(sizeof(struct ProgressMeter));
1032 ret->modnum = total / 4;
1033 if (ret->modnum == 0) /* Divide by zero check */
1035 ret->dotnum = (total / 50) + 1;
1036 if (start_string != NULL)
1037 ret->startup_string = GNUNET_strdup(start_string);
1039 ret->startup_string = GNUNET_strdup("");
1045 * Update progress meter (increment by one).
1047 * @param meter the meter to update and print info for
1049 * @return GNUNET_YES if called the total requested,
1050 * GNUNET_NO if more items expected
1053 update_meter(struct ProgressMeter *meter)
1055 if (meter->print == GNUNET_YES)
1057 if (meter->completed % meter->modnum == 0)
1059 if (meter->completed == 0)
1061 fprintf (stdout, "%sProgress: [0%%", meter->startup_string);
1064 fprintf (stdout, "%d%%", (int) (((float) meter->completed
1065 / meter->total) * 100));
1067 else if (meter->completed % meter->dotnum == 0)
1068 fprintf (stdout, ".");
1070 if (meter->completed + 1 == meter->total)
1071 fprintf (stdout, "%d%%]\n", 100);
1076 if (meter->completed == meter->total)
1078 if (meter->completed > meter->total)
1079 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
1084 * Reset progress meter.
1086 * @param meter the meter to reset
1088 * @return GNUNET_YES if meter reset,
1089 * GNUNET_SYSERR on error
1092 reset_meter(struct ProgressMeter *meter)
1095 return GNUNET_SYSERR;
1097 meter->completed = 0;
1102 * Release resources for meter
1104 * @param meter the meter to free
1107 free_meter(struct ProgressMeter *meter)
1109 GNUNET_free_non_null (meter->startup_string);
1110 GNUNET_free (meter);
1114 * Get a topology from a string input.
1116 * @param topology where to write the retrieved topology
1117 * @param topology_string The string to attempt to
1118 * get a configuration value from
1119 * @return GNUNET_YES if topology string matched a
1120 * known topology, GNUNET_NO if not
1123 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology,
1124 const char *topology_string)
1127 * Strings representing topologies in enum
1129 static const char *topology_strings[] =
1132 * A clique (everyone connected to everyone else).
1137 * Small-world network (2d torus plus random links).
1142 * Small-world network (ring plus random links).
1162 * Certain percentage of peers are unable to communicate directly
1163 * replicating NAT conditions
1168 * Scale free topology.
1173 * Straight line topology.
1178 * All peers are disconnected.
1183 * Read the topology from a file.
1190 if (topology_string == NULL)
1192 while (topology_strings[curr] != NULL)
1194 if (strcasecmp (topology_strings[curr], topology_string) == 0)
1201 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
1206 * Get connect topology option from string input.
1208 * @param topology_option where to write the retrieved topology
1209 * @param topology_string The string to attempt to
1210 * get a configuration value from
1211 * @return GNUNET_YES if string matched a known
1212 * topology option, GNUNET_NO if not
1215 GNUNET_TESTING_topology_option_get(
1216 enum GNUNET_TESTING_TopologyOption *topology_option,
1217 const char *topology_string)
1220 * Options for connecting a topology as strings.
1222 static const char *topology_option_strings[] =
1225 * Try to connect all peers specified in the topology.
1230 * Choose a random subset of connections to create.
1232 "CONNECT_RANDOM_SUBSET",
1235 * Create at least X connections for each peer.
1240 * Using a depth first search, create one connection
1241 * per peer. If any are missed (graph disconnected)
1242 * start over at those peers until all have at least one
1248 * Find the N closest peers to each allowed peer in the
1249 * topology and make sure a connection to those peers
1250 * exists in the connect topology.
1255 * No options specified.
1262 if (topology_string == NULL)
1264 while (NULL != topology_option_strings[curr])
1266 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1268 *topology_option = curr;
1273 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1278 * Function to iterate over options. Copies
1279 * the options to the target configuration,
1280 * updating PORT values as needed.
1282 * @param cls closure
1283 * @param section name of the section
1284 * @param option name of the option
1285 * @param value value of the option
1288 update_config(void *cls, const char *section, const char *option,
1291 struct UpdateContext *ctx = cls;
1295 char *single_variable;
1296 char *per_host_variable;
1297 unsigned long long num_per_host;
1299 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1300 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1302 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1304 if ((ival != 0) && (GNUNET_YES
1305 != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1308 GNUNET_snprintf (cval, sizeof(cval), "%u", ctx->nport++);
1311 else if ((ival != 0) && (GNUNET_YES
1312 == GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1314 && GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1318 GNUNET_snprintf (cval, sizeof(cval), "%u", ival + ctx->fdnum
1323 /* FIXME: REMOVE FOREVER HACK HACK HACK */
1324 if (0 == strcasecmp (section, "transport-tcp"))
1325 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, "ADVERTISED_PORT", value);
1328 if (0 == strcmp (option, "UNIXPATH"))
1330 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig,
1334 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1335 section, ctx->upnum++);
1338 else if ((GNUNET_YES
1339 == GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1342 && (num_per_host > 0))
1345 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1346 section, ctx->fdnum % num_per_host);
1351 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1353 value = ctx->hostname;
1355 GNUNET_free (single_variable);
1356 GNUNET_free (per_host_variable);
1357 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1361 * Create a new configuration using the given configuration
1362 * as a template; however, each PORT in the existing cfg
1363 * must be renumbered by incrementing "*port". If we run
1364 * out of "*port" numbers, return NULL.
1366 * @param cfg template configuration
1367 * @param off the current peer offset
1368 * @param port port numbers to use, update to reflect
1369 * port numbers that were used
1370 * @param upnum number to make unix domain socket names unique
1371 * @param hostname hostname of the controlling host, to allow control connections from
1372 * @param fdnum number used to offset the unix domain socket for grouped processes
1373 * (such as statistics or peerinfo, which can be shared among others)
1375 * @return new configuration, NULL on error
1377 static struct GNUNET_CONFIGURATION_Handle *
1378 make_config(const struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t off,
1379 uint16_t * port, uint32_t * upnum, const char *hostname,
1382 struct UpdateContext uc;
1385 char *allowed_hosts;
1386 unsigned long long skew_variance;
1387 unsigned long long skew_offset;
1388 long long actual_offset;
1394 uc.ret = GNUNET_CONFIGURATION_create ();
1395 uc.hostname = hostname;
1398 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1399 if (uc.nport >= HIGH_PORT)
1402 GNUNET_CONFIGURATION_destroy (uc.ret);
1406 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
1409 && (skew_variance > 0))
1411 skew_offset = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, skew_variance + 1);
1412 actual_offset = skew_offset - GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, skew_variance + 1);
1413 /* Min is -skew_variance, Max is skew_variance */
1414 skew_offset = skew_variance + actual_offset; /* Normal distribution around 0 */
1415 GNUNET_CONFIGURATION_set_value_number(uc.ret, "testing", "skew_offset", skew_offset);
1418 if (GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "control_host",
1419 &control_host) == GNUNET_OK)
1421 if (hostname != NULL)
1422 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1425 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1427 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1429 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1430 "ACCEPT_FROM", allowed_hosts);
1431 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1433 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1434 "ACCEPT_FROM", allowed_hosts);
1436 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH", "");
1437 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH",
1439 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH", "");
1440 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1443 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "USE_LOCALADDR",
1445 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "USE_LOCALADDR",
1447 GNUNET_free_non_null (control_host);
1448 GNUNET_free (allowed_hosts);
1451 /* arm needs to know to allow connections from the host on which it is running,
1452 * otherwise gnunet-arm is unable to connect to it in some instances */
1453 if (hostname != NULL)
1455 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1456 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1458 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1460 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "USE_LOCALADDR",
1462 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "USE_LOCALADDR",
1464 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1466 GNUNET_free (allowed_hosts);
1470 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1472 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1474 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "USE_LOCALADDR",
1476 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "USE_LOCALADDR",
1480 *port = (uint16_t) uc.nport;
1488 * Remove entries from the peer connection list
1490 * @param pg the peer group we are working with
1491 * @param first index of the first peer
1492 * @param second index of the second peer
1493 * @param list the peer list to use
1494 * @param check UNUSED
1496 * @return the number of connections added (can be 0, 1 or 2)
1500 remove_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1501 unsigned int second, enum PeerLists list, unsigned int check)
1505 struct PeerConnection **first_list;
1506 struct PeerConnection **second_list;
1507 struct PeerConnection *first_iter;
1508 struct PeerConnection *second_iter;
1509 struct PeerConnection **first_tail;
1510 struct PeerConnection **second_tail;
1513 GNUNET_HashCode hash_first;
1514 GNUNET_HashCode hash_second;
1516 hash_from_uid (first, &hash_first);
1517 hash_from_uid (second, &hash_second);
1525 first_list = &pg->peers[first].allowed_peers_head;
1526 second_list = &pg->peers[second].allowed_peers_head;
1527 first_tail = &pg->peers[first].allowed_peers_tail;
1528 second_tail = &pg->peers[second].allowed_peers_tail;
1531 first_list = &pg->peers[first].connect_peers_head;
1532 second_list = &pg->peers[second].connect_peers_head;
1533 first_tail = &pg->peers[first].connect_peers_tail;
1534 second_tail = &pg->peers[second].connect_peers_tail;
1537 first_list = &pg->peers[first].blacklisted_peers_head;
1538 second_list = &pg->peers[second].blacklisted_peers_head;
1539 first_tail = &pg->peers[first].blacklisted_peers_tail;
1540 second_tail = &pg->peers[second].blacklisted_peers_tail;
1543 first_list = &pg->peers[first].connect_peers_working_set_head;
1544 second_list = &pg->peers[second].connect_peers_working_set_head;
1545 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1546 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1553 first_iter = *first_list;
1554 while (first_iter != NULL)
1556 if (first_iter->index == second)
1558 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1559 GNUNET_free(first_iter);
1563 first_iter = first_iter->next;
1566 second_iter = *second_list;
1567 while (second_iter != NULL)
1569 if (second_iter->index == first)
1571 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1572 GNUNET_free(second_iter);
1576 second_iter = second_iter->next;
1580 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1583 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1588 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1591 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1600 * Add entries to the some list
1602 * @param pg the peer group we are working with
1603 * @param first index of the first peer
1604 * @param second index of the second peer
1605 * @param list the list type that we should modify
1606 * @param check GNUNET_YES to check lists before adding
1607 * GNUNET_NO to force add
1609 * @return the number of connections added (can be 0, 1 or 2)
1613 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1614 unsigned int second, enum PeerLists list, unsigned int check)
1620 struct PeerConnection **first_list;
1621 struct PeerConnection **second_list;
1622 struct PeerConnection *first_iter;
1623 struct PeerConnection *second_iter;
1624 struct PeerConnection *new_first;
1625 struct PeerConnection *new_second;
1626 struct PeerConnection **first_tail;
1627 struct PeerConnection **second_tail;
1632 first_list = &pg->peers[first].allowed_peers_head;
1633 second_list = &pg->peers[second].allowed_peers_head;
1634 first_tail = &pg->peers[first].allowed_peers_tail;
1635 second_tail = &pg->peers[second].allowed_peers_tail;
1638 first_list = &pg->peers[first].connect_peers_head;
1639 second_list = &pg->peers[second].connect_peers_head;
1640 first_tail = &pg->peers[first].connect_peers_tail;
1641 second_tail = &pg->peers[second].connect_peers_tail;
1644 first_list = &pg->peers[first].blacklisted_peers_head;
1645 second_list = &pg->peers[second].blacklisted_peers_head;
1646 first_tail = &pg->peers[first].blacklisted_peers_tail;
1647 second_tail = &pg->peers[second].blacklisted_peers_tail;
1650 first_list = &pg->peers[first].connect_peers_working_set_head;
1651 second_list = &pg->peers[second].connect_peers_working_set_head;
1652 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1653 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1660 add_first = GNUNET_YES;
1661 add_second = GNUNET_YES;
1663 if (check == GNUNET_YES)
1665 first_iter = *first_list;
1666 while (first_iter != NULL)
1668 if (first_iter->index == second)
1670 add_first = GNUNET_NO;
1673 first_iter = first_iter->next;
1676 second_iter = *second_list;
1677 while (second_iter != NULL)
1679 if (second_iter->index == first)
1681 add_second = GNUNET_NO;
1684 second_iter = second_iter->next;
1691 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1692 new_first->index = second;
1693 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1694 pg->peers[first].num_connections++;
1700 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1701 new_second->index = first;
1702 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1703 pg->peers[second].num_connections++;
1711 * Scale free network construction as described in:
1713 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1715 * Start with a network of "one" peer, then progressively add
1716 * peers up to the total number. At each step, iterate over
1717 * all possible peers and connect new peer based on number of
1718 * existing connections of the target peer.
1720 * @param pg the peer group we are dealing with
1721 * @param proc the connection processor to use
1722 * @param list the peer list to use
1724 * @return the number of connections created
1727 create_scale_free(struct GNUNET_TESTING_PeerGroup *pg,
1728 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1731 unsigned int total_connections;
1732 unsigned int outer_count;
1734 unsigned int previous_total_connections;
1738 GNUNET_assert (pg->total > 1);
1740 /* Add a connection between the first two nodes */
1741 total_connections = proc (pg, 0, 1, list, GNUNET_YES);
1743 for (outer_count = 1; outer_count < pg->total; outer_count++)
1745 previous_total_connections = total_connections;
1746 for (i = 0; i < outer_count; i++)
1748 probability = pg->peers[i].num_connections
1749 / (double) previous_total_connections;
1751 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1753 / ((double) UINT64_MAX);
1755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1756 "Considering connecting peer %d to peer %d\n",
1759 if (random < probability)
1762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1763 "Connecting peer %d to peer %d\n", outer_count, i);
1765 total_connections += proc (pg, outer_count, i, list, GNUNET_YES);
1770 return total_connections;
1774 * Create a topology given a peer group (set of running peers)
1775 * and a connection processor. Creates a small world topology
1776 * according to the rewired ring construction. The basic
1777 * behavior is that a ring topology is created, but with some
1778 * probability instead of connecting a peer to the next
1779 * neighbor in the ring a connection will be created to a peer
1780 * selected uniformly at random. We use the TESTING
1781 * PERCENTAGE option to specify what number of
1782 * connections each peer should have. Default is 2,
1783 * which makes the ring, any given number is multiplied by
1784 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1785 * each peer have on average 2logn connections. The additional
1786 * connections are made at increasing distance around the ring
1787 * from the original peer, or to random peers based on the re-
1788 * wiring probability. The TESTING
1789 * PROBABILITY option is used as the probability that a given
1790 * connection is rewired.
1792 * @param pg the peergroup to create the topology on
1793 * @param proc the connection processor to call to actually set
1794 * up connections between two peers
1795 * @param list the peer list to use
1797 * @return the number of connections that were set up
1801 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg,
1802 GNUNET_TESTING_ConnectionProcessor proc,
1803 enum PeerLists list)
1807 unsigned int natLog;
1808 unsigned int randomPeer;
1809 double random, logNModifier, probability;
1810 unsigned int smallWorldConnections;
1815 unsigned int useAnd;
1816 int connect_attempts;
1818 logNModifier = 0.5; /* FIXME: default value? */
1819 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1823 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1825 GNUNET_ERROR_TYPE_WARNING,
1827 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1828 p_string, "LOGNMODIFIER", "TESTING");
1829 GNUNET_free (p_string);
1831 probability = 0.5; /* FIXME: default percentage? */
1832 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1836 if (sscanf (p_string, "%lf", &probability) != 1)
1838 GNUNET_ERROR_TYPE_WARNING,
1840 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1841 p_string, "PERCENTAGE", "TESTING");
1842 GNUNET_free (p_string);
1844 natLog = log (pg->total);
1845 connsPerPeer = ceil (natLog * logNModifier);
1847 if (connsPerPeer % 2 == 1)
1850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."),
1853 smallWorldConnections = 0;
1854 connect_attempts = 0;
1855 for (i = 0; i < pg->total; i++)
1858 max = i + connsPerPeer / 2;
1859 min = i - connsPerPeer / 2;
1861 if (max > pg->total - 1)
1863 max = max - pg->total;
1869 min = pg->total - 1 + min;
1873 for (j = 0; j < connsPerPeer / 2; j++)
1876 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1878 / ((double) UINT64_MAX));
1879 if (random < probability)
1881 /* Connect to uniformly selected random peer */
1883 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1885 while ((((randomPeer < max) && (randomPeer > min)) && (useAnd
1886 == 0)) || (((randomPeer > min) || (randomPeer < max))
1890 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1893 smallWorldConnections += proc (pg, i, randomPeer, list,
1898 nodeToConnect = i + j + 1;
1899 if (nodeToConnect > pg->total - 1)
1901 nodeToConnect = nodeToConnect - pg->total;
1903 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
1909 connect_attempts += smallWorldConnections;
1911 return connect_attempts;
1915 * Create a topology given a peer group (set of running peers)
1916 * and a connection processor.
1918 * @param pg the peergroup to create the topology on
1919 * @param proc the connection processor to call to actually set
1920 * up connections between two peers
1921 * @param list the peer list to use
1923 * @return the number of connections that were set up
1927 create_nated_internet(struct GNUNET_TESTING_PeerGroup *pg,
1928 GNUNET_TESTING_ConnectionProcessor proc,
1929 enum PeerLists list)
1931 unsigned int outer_count, inner_count;
1932 unsigned int cutoff;
1933 int connect_attempts;
1934 double nat_percentage;
1937 nat_percentage = 0.6; /* FIXME: default percentage? */
1938 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1942 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1944 GNUNET_ERROR_TYPE_WARNING,
1946 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1947 p_string, "PERCENTAGE", "TESTING");
1948 GNUNET_free (p_string);
1951 cutoff = (unsigned int) (nat_percentage * pg->total);
1952 connect_attempts = 0;
1953 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1955 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1957 if ((outer_count > cutoff) || (inner_count > cutoff))
1960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1961 "Connecting peer %d to peer %d\n",
1962 outer_count, inner_count);
1964 connect_attempts += proc (pg, outer_count, inner_count, list,
1969 return connect_attempts;
1974 * Create a topology given a peer group (set of running peers)
1975 * and a connection processor.
1977 * @param pg the peergroup to create the topology on
1978 * @param proc the connection processor to call to actually set
1979 * up connections between two peers
1980 * @param list the peer list to use
1982 * @return the number of connections that were set up
1986 create_nated_internet_copy(struct GNUNET_TESTING_PeerGroup *pg,
1987 GNUNET_TESTING_ConnectionProcessor proc,
1988 enum PeerLists list)
1990 unsigned int outer_count, inner_count;
1991 unsigned int cutoff;
1992 int connect_attempts;
1993 double nat_percentage;
1996 struct ProgressMeter *conn_meter;
1998 nat_percentage = 0.6; /* FIXME: default percentage? */
1999 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2003 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
2005 GNUNET_ERROR_TYPE_WARNING,
2007 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2008 p_string, "PERCENTAGE", "TESTING");
2009 GNUNET_free (p_string);
2012 cutoff = (unsigned int) (nat_percentage * pg->total);
2014 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2016 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2018 if ((outer_count > cutoff) || (inner_count > cutoff))
2024 conn_meter = create_meter (count, "NAT COPY", GNUNET_YES);
2025 connect_attempts = 0;
2026 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2028 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2030 if ((outer_count > cutoff) || (inner_count > cutoff))
2033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2034 "Connecting peer %d to peer %d\n",
2035 outer_count, inner_count);
2037 connect_attempts += proc (pg, outer_count, inner_count, list,
2039 add_connections (pg, outer_count, inner_count, ALLOWED, GNUNET_NO);
2040 update_meter (conn_meter);
2044 free_meter (conn_meter);
2046 return connect_attempts;
2051 * Create a topology given a peer group (set of running peers)
2052 * and a connection processor.
2054 * @param pg the peergroup to create the topology on
2055 * @param proc the connection processor to call to actually set
2056 * up connections between two peers
2057 * @param list the peer list to use
2059 * @return the number of connections that were set up
2063 create_small_world(struct GNUNET_TESTING_PeerGroup *pg,
2064 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2066 unsigned int i, j, k;
2067 unsigned int square;
2070 unsigned int toggle = 1;
2071 unsigned int nodeToConnect;
2072 unsigned int natLog;
2073 unsigned int node1Row;
2074 unsigned int node1Col;
2075 unsigned int node2Row;
2076 unsigned int node2Col;
2077 unsigned int distance;
2078 double probability, random, percentage;
2079 unsigned int smallWorldConnections;
2080 unsigned int small_world_it;
2082 int connect_attempts;
2083 square = floor (sqrt (pg->total));
2087 percentage = 0.5; /* FIXME: default percentage? */
2088 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2092 if (sscanf (p_string, "%lf", &percentage) != 1)
2094 GNUNET_ERROR_TYPE_WARNING,
2096 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2097 p_string, "PERCENTAGE", "TESTING");
2098 GNUNET_free (p_string);
2100 if (percentage < 0.0)
2103 GNUNET_ERROR_TYPE_WARNING,
2105 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
2106 "PERCENTAGE", "TESTING", percentage);
2109 probability = 0.5; /* FIXME: default percentage? */
2110 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2114 if (sscanf (p_string, "%lf", &probability) != 1)
2116 GNUNET_ERROR_TYPE_WARNING,
2118 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2119 p_string, "PROBABILITY", "TESTING");
2120 GNUNET_free (p_string);
2122 if (square * square != pg->total)
2124 while (rows * cols < pg->total)
2126 if (toggle % 2 == 0)
2135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2137 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2141 connect_attempts = 0;
2142 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2143 * to the node to its right and above. Once this is over, we'll have our torus!
2144 * Special case for the last node (if the rows and columns are not equal), connect
2145 * to the first in the row to maintain topology.
2147 for (i = 0; i < pg->total; i++)
2149 /* First connect to the node to the right */
2150 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2151 nodeToConnect = i + 1;
2152 else if (i + 1 == pg->total)
2153 nodeToConnect = rows * cols - cols;
2155 nodeToConnect = i - cols + 1;
2157 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2161 nodeToConnect = (rows * cols) - cols + i;
2162 if (nodeToConnect >= pg->total)
2163 nodeToConnect -= cols;
2166 nodeToConnect = i - cols;
2168 if (nodeToConnect < pg->total)
2169 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2171 natLog = log (pg->total);
2172 #if VERBOSE_TESTING > 2
2173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2174 _("natural log of %d is %d, will run %d iterations\n"),
2175 pg->total, natLog, (int) (natLog * percentage));
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2177 _("Total connections added thus far: %u!\n"), connect_attempts);
2179 smallWorldConnections = 0;
2180 small_world_it = (unsigned int) (natLog * percentage);
2181 if (small_world_it < 1)
2183 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
2184 for (i = 0; i < small_world_it; i++)
2186 for (j = 0; j < pg->total; j++)
2188 /* Determine the row and column of node at position j on the 2d torus */
2189 node1Row = j / cols;
2190 node1Col = j - (node1Row * cols);
2191 for (k = 0; k < pg->total; k++)
2193 /* Determine the row and column of node at position k on the 2d torus */
2194 node2Row = k / cols;
2195 node2Col = k - (node2Row * cols);
2196 /* Simple Cartesian distance */
2197 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
2200 /* Calculate probability as 1 over the square of the distance */
2201 probability = 1.0 / (distance * distance);
2202 /* Choose a random value between 0 and 1 */
2204 = ((double) GNUNET_CRYPTO_random_u64 (
2205 GNUNET_CRYPTO_QUALITY_WEAK,
2207 / ((double) UINT64_MAX);
2208 /* If random < probability, then connect the two nodes */
2209 if (random < probability)
2210 smallWorldConnections += proc (pg, j, k, list, GNUNET_YES);
2216 connect_attempts += smallWorldConnections;
2217 #if VERBOSE_TESTING > 2
2218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2219 _("Total connections added for small world: %d!\n"),
2220 smallWorldConnections);
2222 return connect_attempts;
2226 * Create a topology given a peer group (set of running peers)
2227 * and a connection processor.
2229 * @param pg the peergroup to create the topology on
2230 * @param proc the connection processor to call to actually set
2231 * up connections between two peers
2232 * @param list the peer list to use
2234 * @return the number of connections that were set up
2238 create_erdos_renyi(struct GNUNET_TESTING_PeerGroup *pg,
2239 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2242 unsigned int outer_count;
2243 unsigned int inner_count;
2244 int connect_attempts;
2248 probability = 0.5; /* FIXME: default percentage? */
2249 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2253 if (sscanf (p_string, "%lf", &probability) != 1)
2255 GNUNET_ERROR_TYPE_WARNING,
2257 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2258 p_string, "PROBABILITY", "TESTING");
2259 GNUNET_free (p_string);
2261 connect_attempts = 0;
2262 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2264 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2267 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2269 / ((double) UINT64_MAX);
2271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2272 _("rand is %f probability is %f\n"), temp_rand,
2275 if (temp_rand < probability)
2277 connect_attempts += proc (pg, outer_count, inner_count, list,
2283 return connect_attempts;
2287 * Create a topology given a peer group (set of running peers)
2288 * and a connection processor. This particular function creates
2289 * the connections for a 2d-torus, plus additional "closest"
2290 * connections per peer.
2292 * @param pg the peergroup to create the topology on
2293 * @param proc the connection processor to call to actually set
2294 * up connections between two peers
2295 * @param list the peer list to use
2297 * @return the number of connections that were set up
2301 create_2d_torus(struct GNUNET_TESTING_PeerGroup *pg,
2302 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2305 unsigned int square;
2308 unsigned int toggle = 1;
2309 unsigned int nodeToConnect;
2310 int connect_attempts;
2312 connect_attempts = 0;
2314 square = floor (sqrt (pg->total));
2318 if (square * square != pg->total)
2320 while (rows * cols < pg->total)
2322 if (toggle % 2 == 0)
2331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2333 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2336 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2337 * to the node to its right and above. Once this is over, we'll have our torus!
2338 * Special case for the last node (if the rows and columns are not equal), connect
2339 * to the first in the row to maintain topology.
2341 for (i = 0; i < pg->total; i++)
2343 /* First connect to the node to the right */
2344 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2345 nodeToConnect = i + 1;
2346 else if (i + 1 == pg->total)
2347 nodeToConnect = rows * cols - cols;
2349 nodeToConnect = i - cols + 1;
2351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2352 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2354 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2356 /* Second connect to the node immediately above */
2359 nodeToConnect = (rows * cols) - cols + i;
2360 if (nodeToConnect >= pg->total)
2361 nodeToConnect -= cols;
2364 nodeToConnect = i - cols;
2366 if (nodeToConnect < pg->total)
2369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2370 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2372 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2377 return connect_attempts;
2381 * Create a topology given a peer group (set of running peers)
2382 * and a connection processor.
2384 * @param pg the peergroup to create the topology on
2385 * @param proc the connection processor to call to actually set
2386 * up connections between two peers
2387 * @param list the peer list to use
2388 * @param check does the connection processor need to check before
2389 * performing an action on the list?
2391 * @return the number of connections that were set up
2395 create_clique(struct GNUNET_TESTING_PeerGroup *pg,
2396 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list,
2399 unsigned int outer_count;
2400 unsigned int inner_count;
2401 int connect_attempts;
2402 struct ProgressMeter *conn_meter;
2403 connect_attempts = 0;
2405 conn_meter = create_meter ((((pg->total * pg->total) + pg->total) / 2)
2406 - pg->total, "Create Clique ", GNUNET_NO);
2407 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2409 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2413 "Connecting peer %d to peer %d\n",
2414 outer_count, inner_count);
2416 connect_attempts += proc (pg, outer_count, inner_count, list, check);
2417 update_meter (conn_meter);
2420 reset_meter (conn_meter);
2421 free_meter (conn_meter);
2422 return connect_attempts;
2427 * Iterator over hash map entries.
2429 * @param cls closure the peer group
2430 * @param key the key stored in the hashmap is the
2431 * index of the peer to connect to
2432 * @param value value in the hash map, handle to the peer daemon
2433 * @return GNUNET_YES if we should continue to
2438 unblacklist_iterator (void *cls,
2439 const GNUNET_HashCode * key,
2442 struct UnblacklistContext *un_ctx = cls;
2443 uint32_t second_pos;
2445 uid_from_hash (key, &second_pos);
2447 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2455 * Create a blacklist topology based on the allowed topology
2456 * which disallows any connections not in the allowed topology
2457 * at the transport level.
2459 * @param pg the peergroup to create the topology on
2460 * @param proc the connection processor to call to allow
2461 * up connections between two peers
2463 * @return the number of connections that were set up
2467 copy_allowed (struct GNUNET_TESTING_PeerGroup *pg,
2468 GNUNET_TESTING_ConnectionProcessor proc)
2472 struct PeerConnection *iter;
2474 struct UnblacklistContext un_ctx;
2479 for (count = 0; count < pg->total - 1; count++)
2482 iter = pg->peers[count].allowed_peers_head;
2483 while (iter != NULL)
2485 remove_connections (pg, count, iter->index, BLACKLIST, GNUNET_YES);
2486 //unblacklist_connections(pg, count, iter->index);
2490 un_ctx.first_uid = count;
2491 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers,
2492 &unblacklist_iterator,
2496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2497 "Unblacklisted %u peers\n",
2504 * Create a topology given a peer group (set of running peers)
2505 * and a connection processor.
2507 * @param pg the peergroup to create the topology on
2508 * @param proc the connection processor to call to actually set
2509 * up connections between two peers
2510 * @param list which list should be modified
2512 * @return the number of connections that were set up
2516 create_line(struct GNUNET_TESTING_PeerGroup *pg,
2517 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2520 unsigned int connect_attempts;
2522 connect_attempts = 0;
2523 /* Connect each peer to the next highest numbered peer */
2524 for (count = 0; count < pg->total - 1; count++)
2527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2528 "Connecting peer %d to peer %d\n",
2531 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2534 return connect_attempts;
2538 * Create a topology given a peer group (set of running peers)
2539 * and a connection processor.
2541 * @param pg the peergroup to create the topology on
2542 * @param filename the file to read topology information from
2543 * @param proc the connection processor to call to actually set
2544 * up connections between two peers
2545 * @param list the peer list to use
2547 * @return the number of connections that were set up
2551 create_from_file(struct GNUNET_TESTING_PeerGroup *pg, char *filename,
2552 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2554 int connect_attempts;
2555 unsigned int first_peer_index;
2556 unsigned int second_peer_index;
2561 unsigned int total_peers;
2562 enum States curr_state;
2564 connect_attempts = 0;
2565 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2566 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2568 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2570 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2571 "Could not open file `%s' specified for topology!", filename);
2572 return connect_attempts;
2575 data = GNUNET_malloc_large (frstat.st_size);
2576 GNUNET_assert(data != NULL);
2577 if (frstat.st_size != GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2580 GNUNET_ERROR_TYPE_ERROR,
2581 "Could not read file %s specified for host list, ending test!",
2584 return connect_attempts;
2589 first_peer_index = 0;
2590 /* First line should contain a single integer, specifying the number of peers */
2591 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2592 curr_state = NUM_PEERS;
2593 while (count < frstat.st_size - 1)
2595 if ((buf[count] == '\n') || (buf[count] == ' '))
2605 total_peers = strtoul(&buf[count], NULL, 10);
2608 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2609 "Failed to read number of peers from topology file!\n");
2611 return connect_attempts;
2613 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2614 "Read %u total peers in topology\n", total_peers);
2615 GNUNET_assert(total_peers == pg->total);
2616 curr_state = PEER_INDEX;
2617 while ((buf[count] != '\n') && (count < frstat.st_size - 1))
2623 first_peer_index = strtoul(&buf[count], NULL, 10);
2626 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2627 "Failed to read peer index from topology file!\n");
2629 return connect_attempts;
2631 while ((buf[count] != ':') && (count < frstat.st_size - 1))
2634 curr_state = OTHER_PEER_INDEX;
2637 if (1 == sscanf (&buf[count], ":"))
2638 curr_state = OTHER_PEER_INDEX;
2641 case OTHER_PEER_INDEX:
2643 second_peer_index = strtoul(&buf[count], NULL, 10);
2646 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2647 "Failed to peer index from topology file!\n");
2649 return connect_attempts;
2651 /* Assume file is written with first peer 1, but array index is 0 */
2652 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index
2653 - 1, list, GNUNET_YES);
2654 while ((buf[count] != '\n') && (buf[count] != ',') && (count
2655 < frstat.st_size - 1))
2657 if (buf[count] == '\n')
2659 curr_state = PEER_INDEX;
2661 else if (buf[count] != ',')
2663 curr_state = OTHER_PEER_INDEX;
2668 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2669 "Found bad data in topology file while in state %d!\n",
2673 return connect_attempts;
2677 return connect_attempts;
2681 * Create a topology given a peer group (set of running peers)
2682 * and a connection processor.
2684 * @param pg the peergroup to create the topology on
2685 * @param proc the connection processor to call to actually set
2686 * up connections between two peers
2687 * @param list the peer list to use
2689 * @return the number of connections that were set up
2693 create_ring(struct GNUNET_TESTING_PeerGroup *pg,
2694 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2697 int connect_attempts;
2699 connect_attempts = 0;
2701 /* Connect each peer to the next highest numbered peer */
2702 for (count = 0; count < pg->total - 1; count++)
2705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2706 "Connecting peer %d to peer %d\n", count, count + 1);
2708 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2711 /* Connect the last peer to the first peer */
2712 connect_attempts += proc (pg, pg->total - 1, 0, list, GNUNET_YES);
2714 return connect_attempts;
2719 * Iterator for writing friends of a peer to a file.
2721 * @param cls closure, an open writable file handle
2722 * @param key the key the daemon was stored under
2723 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2725 * @return GNUNET_YES to continue iteration
2727 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2728 * with a single file_iterator that takes a closure which contains
2729 * the prefix to write before the peer. Then this could be used
2730 * for blacklisting multiple transports and writing the friend
2731 * file. I'm sure *someone* will complain loudly about other
2732 * things that negate these functions even existing so no point in
2736 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2738 FILE *temp_friend_handle = cls;
2739 struct GNUNET_TESTING_Daemon *peer = value;
2740 struct GNUNET_PeerIdentity *temppeer;
2741 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2743 temppeer = &peer->id;
2744 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2745 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2750 struct BlacklistContext
2753 * The (open) file handle to write to
2755 FILE *temp_file_handle;
2758 * The transport that this peer will be blacklisted on.
2764 * Iterator for writing blacklist data to appropriate files.
2766 * @param cls closure, an open writable file handle
2767 * @param key the key the daemon was stored under
2768 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2770 * @return GNUNET_YES to continue iteration
2773 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2775 struct BlacklistContext *blacklist_ctx = cls;
2776 struct GNUNET_TESTING_Daemon *peer = value;
2777 struct GNUNET_PeerIdentity *temppeer;
2778 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2780 temppeer = &peer->id;
2781 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2782 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2783 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2784 blacklist_ctx->transport, (char *) &peer_enc);
2791 * Create the friend files based on the PeerConnection's
2792 * of each peer in the peer group, and copy the files
2793 * to the appropriate place
2795 * @param pg the peer group we are dealing with
2798 create_and_copy_friend_files(struct GNUNET_TESTING_PeerGroup *pg)
2800 FILE *temp_friend_handle;
2801 unsigned int pg_iter;
2802 char *temp_service_path;
2803 struct GNUNET_OS_Process **procarr;
2807 enum GNUNET_OS_ProcessStatusType type;
2808 unsigned long return_code;
2816 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2817 struct PeerConnection *conn_iter;
2819 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2820 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2822 mytemp = GNUNET_DISK_mktemp ("friends");
2823 GNUNET_assert (mytemp != NULL);
2824 temp_friend_handle = fopen (mytemp, "wt");
2825 GNUNET_assert (temp_friend_handle != NULL);
2827 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2828 while (conn_iter != NULL)
2830 GNUNET_CRYPTO_hash_to_enc (
2831 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2833 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2834 conn_iter = conn_iter->next;
2837 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2838 &friend_file_iterator,
2839 temp_friend_handle);
2841 fclose (temp_friend_handle);
2844 != GNUNET_CONFIGURATION_get_value_string (pg->peers[pg_iter].
2846 "PATHS", "SERVICEHOME",
2847 &temp_service_path))
2849 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2850 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2851 "SERVICEHOME", "PATHS");
2852 if (UNLINK (mytemp) != 0)
2853 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
2856 GNUNET_free (mytemp);
2860 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2862 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2863 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
2865 GNUNET_assert(procarr[pg_iter] != NULL);
2867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2868 "Copying file with command cp %s %s\n",
2872 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2873 GNUNET_OS_process_close (procarr[pg_iter]);
2876 else /* Remote, scp the file to the correct place */
2878 if (NULL != pg->peers[pg_iter].daemon->username)
2879 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2880 pg->peers[pg_iter].daemon->username,
2881 pg->peers[pg_iter].daemon->hostname,
2884 GNUNET_asprintf (&arg, "%s:%s/friends",
2885 pg->peers[pg_iter].daemon->hostname,
2887 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
2889 GNUNET_assert(procarr[pg_iter] != NULL);
2890 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2891 GNUNET_OS_process_close (procarr[pg_iter]);
2892 if (ret != GNUNET_OK)
2894 /* FIXME: free contents of 'procarr' array */
2895 GNUNET_free (procarr);
2896 GNUNET_free (temp_service_path);
2897 GNUNET_free (mytemp);
2901 procarr[pg_iter] = NULL;
2903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2904 "Copying file with command scp %s %s\n",
2910 GNUNET_free (temp_service_path);
2911 GNUNET_free (mytemp);
2916 ret = GNUNET_SYSERR;
2917 while ((count < max_wait) && (ret != GNUNET_OK))
2920 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2924 "Checking copy status of file %d\n",
2927 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2929 if (GNUNET_OS_process_status
2930 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2932 ret = GNUNET_SYSERR;
2934 else if ((type != GNUNET_OS_PROCESS_EXITED)
2935 || (return_code != 0))
2937 ret = GNUNET_SYSERR;
2941 GNUNET_OS_process_close (procarr[pg_iter]);
2942 procarr[pg_iter] = NULL;
2944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2952 if (ret == GNUNET_SYSERR)
2954 /* FIXME: why sleep here? -CG */
2960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2961 _("Finished copying all friend files!\n"));
2964 GNUNET_free (procarr);
2969 * Create the blacklist files based on the PeerConnection's
2970 * of each peer in the peer group, and copy the files
2971 * to the appropriate place.
2973 * @param pg the peer group we are dealing with
2974 * @param transports space delimited list of transports to blacklist
2977 create_and_copy_blacklist_files(struct GNUNET_TESTING_PeerGroup *pg,
2978 const char *transports)
2980 FILE *temp_file_handle;
2981 unsigned int pg_iter;
2982 char *temp_service_path;
2983 struct GNUNET_OS_Process **procarr;
2986 enum GNUNET_OS_ProcessStatusType type;
2987 unsigned long return_code;
2994 char *temp_transports;
2996 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2997 struct PeerConnection *conn_iter;
2999 static struct BlacklistContext blacklist_ctx;
3002 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
3003 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3005 mytemp = GNUNET_DISK_mktemp ("blacklist");
3006 GNUNET_assert (mytemp != NULL);
3007 temp_file_handle = fopen (mytemp, "wt");
3008 GNUNET_assert (temp_file_handle != NULL);
3009 temp_transports = GNUNET_strdup (transports);
3011 blacklist_ctx.temp_file_handle = temp_file_handle;
3013 transport_len = strlen (temp_transports) + 1;
3016 for (i = 0; i < transport_len; i++)
3018 if ((temp_transports[i] == ' ') && (pos == NULL))
3019 continue; /* At start of string (whitespace) */
3020 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
3022 temp_transports[i] = '\0';
3024 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
3025 while (conn_iter != NULL)
3027 GNUNET_CRYPTO_hash_to_enc (
3028 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
3030 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
3031 conn_iter = conn_iter->next;
3034 blacklist_ctx.transport = pos;
3035 (void) GNUNET_CONTAINER_multihashmap_iterate (pg->
3037 [pg_iter].blacklisted_peers,
3038 &blacklist_file_iterator,
3042 } /* At beginning of actual string */
3043 else if (pos == NULL)
3045 pos = &temp_transports[i];
3049 GNUNET_free (temp_transports);
3050 fclose (temp_file_handle);
3053 != GNUNET_CONFIGURATION_get_value_string (
3054 pg->peers[pg_iter]. daemon->cfg,
3055 "PATHS", "SERVICEHOME",
3056 &temp_service_path))
3059 GNUNET_ERROR_TYPE_WARNING,
3061 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
3062 "SERVICEHOME", "PATHS");
3063 if (UNLINK (mytemp) != 0)
3064 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
3066 GNUNET_free (mytemp);
3070 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
3072 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
3073 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
3076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3077 _("Copying file with command cp %s %s\n"), mytemp, arg);
3082 else /* Remote, scp the file to the correct place */
3084 if (NULL != pg->peers[pg_iter].daemon->username)
3085 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
3086 pg->peers[pg_iter].daemon->username,
3087 pg->peers[pg_iter].daemon->hostname,
3090 GNUNET_asprintf (&arg, "%s:%s/blacklist",
3091 pg->peers[pg_iter].daemon->hostname,
3093 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
3095 GNUNET_assert(procarr[pg_iter] != NULL);
3096 GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
3099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3100 _("Copying file with command scp %s %s\n"), mytemp,
3105 GNUNET_free (temp_service_path);
3106 GNUNET_free (mytemp);
3110 ret = GNUNET_SYSERR;
3111 while ((count < max_wait) && (ret != GNUNET_OK))
3114 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3118 _("Checking copy status of file %d\n"), pg_iter);
3120 if (procarr[pg_iter] != NULL) /* Check for already completed! */
3122 if (GNUNET_OS_process_status (procarr[pg_iter], &type,
3123 &return_code) != GNUNET_OK)
3125 ret = GNUNET_SYSERR;
3127 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
3129 ret = GNUNET_SYSERR;
3133 GNUNET_OS_process_close (procarr[pg_iter]);
3134 procarr[pg_iter] = NULL;
3136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3137 _("File %d copied\n"), pg_iter);
3143 if (ret == GNUNET_SYSERR)
3145 /* FIXME: why sleep here? -CG */
3151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3152 _("Finished copying all blacklist files!\n"));
3154 GNUNET_free (procarr);
3158 /* Forward Declaration */
3160 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3163 * Choose a random peer's next connection to create, and
3164 * call schedule_connect to set up the connect task.
3166 * @param ct_ctx the overall connection context
3169 preschedule_connect(struct GNUNET_TESTING_PeerGroup *pg)
3171 struct ConnectTopologyContext *ct_ctx = &pg->ct_ctx;
3172 struct PeerConnection *connection_iter;
3173 struct ConnectContext *connect_context;
3174 uint32_t random_peer;
3176 if (ct_ctx->remaining_connections == 0)
3179 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3180 while (pg->peers[random_peer].connect_peers_head == NULL)
3181 random_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3184 connection_iter = pg->peers[random_peer].connect_peers_head;
3185 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3186 connect_context->first_index = random_peer;
3187 connect_context->second_index = connection_iter->index;
3188 connect_context->ct_ctx = ct_ctx;
3189 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3190 GNUNET_CONTAINER_DLL_remove(pg->peers[random_peer].connect_peers_head, pg->peers[random_peer].connect_peers_tail, connection_iter);
3191 GNUNET_free(connection_iter);
3192 ct_ctx->remaining_connections--;
3196 /* Forward declaration */
3197 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3200 * Close connections and free the hello context.
3202 * @param cls the 'struct SendHelloContext *'
3203 * @param tc scheduler context
3206 free_hello_context (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3208 struct SendHelloContext *send_hello_context = cls;
3209 if (send_hello_context->peer->daemon->server != NULL)
3211 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3212 send_hello_context->peer->daemon->server = NULL;
3214 if (send_hello_context->peer->daemon->th != NULL)
3216 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3217 send_hello_context->peer->daemon->th = NULL;
3219 if (send_hello_context->core_connect_task != GNUNET_SCHEDULER_NO_TASK)
3221 GNUNET_SCHEDULER_cancel(send_hello_context->core_connect_task);
3222 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3224 send_hello_context->pg->outstanding_connects--;
3225 GNUNET_free(send_hello_context);
3229 * For peers that haven't yet connected, notify
3230 * the caller that they have failed (timeout).
3232 * @param cls the 'struct SendHelloContext *'
3233 * @param tc scheduler context
3236 notify_remaining_connections_failed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3238 struct SendHelloContext *send_hello_context = cls;
3239 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3240 struct PeerConnection *connection;
3242 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3243 send_hello_context->peer->daemon->server = NULL;
3245 connection = send_hello_context->peer->connect_peers_head;
3247 while (connection != NULL)
3249 if (pg->notify_connection != NULL)
3251 pg->notify_connection(pg->notify_connection_cls,
3252 &send_hello_context->peer->daemon->id,
3253 &pg->peers[connection->index].daemon->id,
3255 send_hello_context->peer->daemon->cfg,
3256 pg->peers[connection->index].daemon->cfg,
3257 send_hello_context->peer->daemon,
3258 pg->peers[connection->index].daemon,
3259 "Peers failed to connect (timeout)");
3261 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3262 GNUNET_free(connection);
3263 connection = connection->next;
3265 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3267 other_peer = &pg->peers[connection->index];
3272 * For peers that haven't yet connected, send
3273 * CORE connect requests.
3275 * @param cls the 'struct SendHelloContext *'
3276 * @param tc scheduler context
3279 send_core_connect_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3281 struct SendHelloContext *send_hello_context = cls;
3282 struct PeerConnection *conn;
3283 GNUNET_assert(send_hello_context->peer->daemon->server != NULL);
3285 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3287 send_hello_context->connect_attempts++;
3288 if (send_hello_context->connect_attempts < send_hello_context->pg->ct_ctx.connect_attempts)
3290 conn = send_hello_context->peer->connect_peers_head;
3291 while (conn != NULL)
3293 GNUNET_CORE_peer_request_connect(send_hello_context->peer->daemon->server,
3294 &send_hello_context->pg->peers[conn->index].daemon->id,
3299 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) ,
3300 &send_core_connect_requests,
3301 send_hello_context);
3305 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Timeout before all connections created, marking rest as failed!\n");
3306 GNUNET_SCHEDULER_add_now(¬ify_remaining_connections_failed, send_hello_context);
3312 * Success, connection is up. Signal client our success.
3314 * @param cls our "struct SendHelloContext"
3315 * @param peer identity of the peer that has connected
3316 * @param atsi performance information
3318 * FIXME: remove peers from BOTH lists, call notify twice, should
3319 * double the speed of connections as long as the list iteration
3320 * doesn't take too long!
3323 core_connect_notify (void *cls,
3324 const struct GNUNET_PeerIdentity *peer,
3325 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3327 struct SendHelloContext *send_hello_context = cls;
3328 struct PeerConnection *connection;
3329 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3331 struct PeerData *other_peer;
3334 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3335 "Connected peer %s to peer %s\n",
3336 ctx->d1->shortname, GNUNET_i2s(peer));
3339 if (0 == memcmp(&send_hello_context->peer->daemon->id, peer, sizeof(struct GNUNET_PeerIdentity)))
3342 connection = send_hello_context->peer->connect_peers_head;
3347 while ((connection != NULL) &&
3348 (0 != memcmp(&pg->peers[connection->index].daemon->id, peer, sizeof(struct GNUNET_PeerIdentity))))
3350 connection = connection->next;
3353 if (connection == NULL)
3355 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);
3360 other_peer = &pg->peers[connection->index];
3362 if (pg->notify_connection != NULL)
3364 pg->notify_connection(pg->notify_connection_cls,
3365 &send_hello_context->peer->daemon->id,
3368 send_hello_context->peer->daemon->cfg,
3369 pg->peers[connection->index].daemon->cfg,
3370 send_hello_context->peer->daemon,
3371 pg->peers[connection->index].daemon,
3374 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3375 GNUNET_free(connection);
3379 /* Notify of reverse connection and remove from other peers list of outstanding */
3380 if (other_peer != NULL)
3382 connection = other_peer->connect_peers_head;
3383 while ((connection != NULL) &&
3384 (0 != memcmp(&send_hello_context->peer->daemon->id, &pg->peers[connection->index].daemon->id, sizeof(struct GNUNET_PeerIdentity))))
3386 connection = connection->next;
3388 if (connection != NULL)
3390 if (pg->notify_connection != NULL)
3392 pg->notify_connection(pg->notify_connection_cls,
3394 &send_hello_context->peer->daemon->id,
3396 pg->peers[connection->index].daemon->cfg,
3397 send_hello_context->peer->daemon->cfg,
3398 pg->peers[connection->index].daemon,
3399 send_hello_context->peer->daemon,
3403 GNUNET_CONTAINER_DLL_remove(other_peer->connect_peers_head, other_peer->connect_peers_tail, connection);
3404 GNUNET_free(connection);
3409 if (send_hello_context->peer->connect_peers_head == NULL)
3411 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3416 * Notify of a successful connection to the core service.
3418 * @param cls a struct SendHelloContext *
3419 * @param server handle to the core service
3420 * @param my_identity the peer identity of this peer
3421 * @param publicKey the public key of the peer
3424 core_init (void *cls,
3425 struct GNUNET_CORE_Handle * server,
3426 const struct GNUNET_PeerIdentity *
3429 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
3432 struct SendHelloContext *send_hello_context = cls;
3433 send_hello_context->core_ready = GNUNET_YES;
3437 * Function called once a hello has been sent
3438 * to the transport, move on to the next one
3439 * or go away forever.
3441 * @param cls the 'struct SendHelloContext *'
3442 * @param tc scheduler context
3445 hello_sent_callback (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3447 struct SendHelloContext *send_hello_context = cls;
3448 //unsigned int pg_iter;
3449 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3451 GNUNET_free(send_hello_context);
3455 send_hello_context->pg->remaining_hellos--;
3457 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sent HELLO, have %d remaining!\n", send_hello_context->pg->remaining_hellos);
3459 if (send_hello_context->peer_pos == NULL) /* All HELLOs (for this peer!) have been transmitted! */
3462 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All hellos for this peer sent, disconnecting transport!\n");
3464 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3465 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3466 send_hello_context->peer->daemon->th = NULL;
3468 /*if (send_hello_context->pg->remaining_hellos == 0)
3470 for (pg_iter = 0; pg_iter < send_hello_context->pg->max_outstanding_connections; pg_iter++)
3472 preschedule_connect(&send_hello_context->pg->ct_ctx);
3476 GNUNET_assert (send_hello_context->peer->daemon->server == NULL);
3477 send_hello_context->peer->daemon->server = GNUNET_CORE_connect(send_hello_context->peer->cfg,
3481 &core_connect_notify,
3488 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),
3489 &send_core_connect_requests,
3490 send_hello_context);
3493 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3497 * Connect to a peer, give it all the HELLO's of those peers
3498 * we will later ask it to connect to.
3500 * @param ct_ctx the overall connection context
3502 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3504 struct SendHelloContext *send_hello_context = cls;
3505 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3507 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3509 GNUNET_free(send_hello_context);
3513 GNUNET_assert(send_hello_context->peer_pos != NULL); /* All of the HELLO sends to be scheduled have been scheduled! */
3515 if (((send_hello_context->peer->daemon->th == NULL) &&
3516 (pg->outstanding_connects > pg->max_outstanding_connections)) ||
3517 (pg->stop_connects == GNUNET_YES))
3519 #if VERBOSE_TESTING > 2
3520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3522 ("Delaying connect, we have too many outstanding connections!\n"));
3524 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3525 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3526 &schedule_send_hellos, send_hello_context);
3530 #if VERBOSE_TESTING > 2
3531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3532 _("Creating connection, outstanding_connections is %d\n"),
3533 outstanding_connects);
3535 if (send_hello_context->peer->daemon->th == NULL)
3537 pg->outstanding_connects++; /* Actual TRANSPORT, CORE connections! */
3538 send_hello_context->peer->daemon->th
3539 = GNUNET_TRANSPORT_connect (send_hello_context->peer->cfg, NULL,
3540 send_hello_context, NULL, NULL, NULL);
3543 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3544 _("Offering Hello of peer %s to peer %s\n"),
3545 send_hello_context->peer->daemon->shortname, pg->peers[send_hello_context->peer_pos->index].daemon->shortname);
3547 GNUNET_TRANSPORT_offer_hello(send_hello_context->peer->daemon->th,
3548 (const struct GNUNET_MessageHeader *)pg->peers[send_hello_context->peer_pos->index].daemon->hello,
3549 &hello_sent_callback,
3550 send_hello_context);
3551 send_hello_context->peer_pos = send_hello_context->peer_pos->next;
3552 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3558 * Internal notification of a connection, kept so that we can ensure some connections
3559 * happen instead of flooding all testing daemons with requests to connect.
3562 internal_connect_notify(void *cls, const struct GNUNET_PeerIdentity *first,
3563 const struct GNUNET_PeerIdentity *second,
3565 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
3566 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
3567 struct GNUNET_TESTING_Daemon *first_daemon,
3568 struct GNUNET_TESTING_Daemon *second_daemon,
3571 struct ConnectContext *connect_ctx = cls;
3572 struct ConnectTopologyContext *ct_ctx = connect_ctx->ct_ctx;
3573 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
3574 struct PeerConnection *connection;
3576 GNUNET_assert (0 < pg->outstanding_connects);
3577 pg->outstanding_connects--;
3580 * Check whether the inverse connection has been scheduled yet,
3581 * if not, we can remove it from the other peers list and avoid
3582 * even trying to connect them again!
3584 connection = pg->peers[connect_ctx->second_index].connect_peers_head;
3589 while ((connection != NULL) && (0
3590 != memcmp (first, &pg->peers[connection->index].daemon->id,
3591 sizeof(struct GNUNET_PeerIdentity))))
3593 connection = connection->next;
3596 if (connection != NULL) /* Can safely remove! */
3598 GNUNET_assert (0 < ct_ctx->remaining_connections);
3599 ct_ctx->remaining_connections--;
3600 if (pg->notify_connection != NULL) /* Notify of reverse connection */
3601 pg->notify_connection (pg->notify_connection_cls, second, first,
3602 distance, second_cfg, first_cfg, second_daemon,
3603 first_daemon, emsg);
3605 GNUNET_CONTAINER_DLL_remove(pg->peers[connect_ctx->second_index].connect_peers_head, pg->peers[connect_ctx->second_index].connect_peers_tail, connection);
3606 GNUNET_free(connection);
3609 if (ct_ctx->remaining_connections == 0)
3611 if (ct_ctx->notify_connections_done != NULL)
3613 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
3614 ct_ctx->notify_connections_done = NULL;
3618 preschedule_connect (pg);
3620 if (pg->notify_connection != NULL)
3621 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
3622 first_cfg, second_cfg, first_daemon, second_daemon,
3625 GNUNET_free(connect_ctx);
3629 * Either delay a connection (because there are too many outstanding)
3630 * or schedule it for right now.
3632 * @param cls a connection context
3633 * @param tc the task runtime context
3636 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3638 struct ConnectContext *connect_context = cls;
3639 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
3641 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3644 if ((pg->outstanding_connects > pg->max_outstanding_connections)
3645 || (pg->stop_connects == GNUNET_YES))
3648 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3650 ("Delaying connect, we have too many outstanding connections!\n"));
3652 GNUNET_SCHEDULER_add_delayed (
3653 GNUNET_TIME_relative_multiply (
3654 GNUNET_TIME_UNIT_MILLISECONDS,
3656 &schedule_connect, connect_context);
3661 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3662 _("Creating connection, outstanding_connections is %d (max %d)\n"),
3663 pg->outstanding_connects, pg->max_outstanding_connections);
3665 pg->outstanding_connects++;
3666 pg->total_connects_scheduled++;
3667 GNUNET_TESTING_daemons_connect (
3668 pg->peers[connect_context->first_index].daemon,
3669 pg->peers[connect_context->second_index].daemon,
3670 connect_context->ct_ctx->connect_timeout,
3671 connect_context->ct_ctx->connect_attempts,
3677 &internal_connect_notify, connect_context); /* FIXME: free connect context! */
3683 * Iterator for actually scheduling connections to be created
3684 * between two peers.
3686 * @param cls closure, a GNUNET_TESTING_Daemon
3687 * @param key the key the second Daemon was stored under
3688 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3690 * @return GNUNET_YES to continue iteration
3693 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3695 struct ConnectTopologyContext *ct_ctx = cls;
3696 struct PeerData *first = ct_ctx->first;
3697 struct GNUNET_TESTING_Daemon *second = value;
3698 struct ConnectContext *connect_context;
3700 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3701 connect_context->first = first->daemon;
3702 connect_context->second = second;
3703 connect_context->ct_ctx = ct_ctx;
3704 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3712 * Iterator for copying all entries in the allowed hashmap to the
3715 * @param cls closure, a GNUNET_TESTING_Daemon
3716 * @param key the key the second Daemon was stored under
3717 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3719 * @return GNUNET_YES to continue iteration
3722 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3724 struct PeerData *first = cls;
3726 GNUNET_assert (GNUNET_OK ==
3727 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
3729 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3736 * Make the peers to connect the same as those that are allowed to be
3739 * @param pg the peer group
3742 copy_allowed_topology(struct GNUNET_TESTING_PeerGroup *pg)
3744 unsigned int pg_iter;
3748 struct PeerConnection *iter;
3752 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3755 iter = pg->peers[pg_iter].allowed_peers_head;
3756 while (iter != NULL)
3758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3759 "Creating connection between %d and %d\n", pg_iter,
3761 total += add_connections (pg, pg_iter, iter->index, CONNECT,
3763 //total += add_actual_connections(pg, pg_iter, iter->index);
3768 GNUNET_CONTAINER_multihashmap_iterate (pg->
3769 peers[pg_iter].allowed_peers,
3770 ©_topology_iterator,
3771 &pg->peers[pg_iter]);
3773 if (GNUNET_SYSERR == ret)
3774 return GNUNET_SYSERR;
3776 total = total + ret;
3783 * Connect the topology as specified by the PeerConnection's
3784 * of each peer in the peer group
3786 * @param pg the peer group we are dealing with
3787 * @param connect_timeout how long try connecting two peers
3788 * @param connect_attempts how many times (max) to attempt
3789 * @param notify_callback callback to notify when finished
3790 * @param notify_cls closure for notify callback
3792 * @return the number of connections that will be attempted
3795 connect_topology(struct GNUNET_TESTING_PeerGroup *pg,
3796 struct GNUNET_TIME_Relative connect_timeout,
3797 unsigned int connect_attempts,
3798 GNUNET_TESTING_NotifyCompletion notify_callback,
3801 unsigned int pg_iter;
3805 struct PeerConnection *connection_iter;
3808 struct SendHelloContext *send_hello_context;
3812 pg->ct_ctx.notify_connections_done = notify_callback;
3813 pg->ct_ctx.notify_cls = notify_cls;
3816 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3819 connection_iter = pg->peers[pg_iter].connect_peers_head;
3820 while (connection_iter != NULL)
3822 connection_iter = connection_iter->next;
3827 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3834 pg->ct_ctx.connect_timeout = connect_timeout;
3835 pg->ct_ctx.connect_attempts = connect_attempts;
3836 pg->ct_ctx.remaining_connections = total;
3839 /* First give all peers the HELLO's of other peers (connect to first peer's transport service, give HELLO's of other peers, continue...) */
3840 pg->remaining_hellos = total;
3841 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3843 send_hello_context = GNUNET_malloc(sizeof(struct SendHelloContext));
3844 send_hello_context->peer = &pg->peers[pg_iter];
3845 send_hello_context->peer_pos = pg->peers[pg_iter].connect_peers_head;
3846 send_hello_context->pg = pg;
3847 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3850 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3852 preschedule_connect (pg);
3860 * Takes a peer group and creates a topology based on the
3861 * one specified. Creates a topology means generates friend
3862 * files for the peers so they can only connect to those allowed
3863 * by the topology. This will only have an effect once peers
3864 * are started if the FRIENDS_ONLY option is set in the base
3865 * config. Also takes an optional restrict topology which
3866 * disallows connections based on particular transports
3867 * UNLESS they are specified in the restricted topology.
3869 * @param pg the peer group struct representing the running peers
3870 * @param topology which topology to connect the peers in
3871 * @param restrict_topology disallow restrict_transports transport
3872 * connections to peers NOT in this topology
3873 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3874 * @param restrict_transports space delimited list of transports to blacklist
3875 * to create restricted topology
3877 * @return the maximum number of connections were all allowed peers
3878 * connected to each other
3881 GNUNET_TESTING_create_topology(struct GNUNET_TESTING_PeerGroup *pg,
3882 enum GNUNET_TESTING_Topology topology,
3883 enum GNUNET_TESTING_Topology restrict_topology,
3884 const char *restrict_transports)
3888 unsigned int num_connections;
3889 int unblacklisted_connections;
3891 struct PeerConnection *conn_iter;
3892 struct PeerConnection *temp_conn;
3897 for (i = 0; i < pg->total; i++)
3899 pg->peers[i].allowed_peers =
3900 GNUNET_CONTAINER_multihashmap_create (100);
3901 pg->peers[i].connect_peers =
3902 GNUNET_CONTAINER_multihashmap_create (100);
3903 pg->peers[i].blacklisted_peers =
3904 GNUNET_CONTAINER_multihashmap_create (100);
3905 pg->peers[i].pg = pg;
3911 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3915 num_connections = create_clique (pg, &add_connections, ALLOWED, GNUNET_NO);
3917 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3920 _("Creating small world (ring) topology\n"));
3922 num_connections = create_small_world_ring (pg, &add_connections, ALLOWED);
3924 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3927 _("Creating small world (2d-torus) topology\n"));
3929 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3931 case GNUNET_TESTING_TOPOLOGY_RING:
3933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3935 num_connections = create_ring (pg, &add_connections, ALLOWED);
3937 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3941 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3943 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3946 _("Creating Erdos-Renyi topology\n"));
3948 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3950 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3954 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3956 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3959 _("Creating Scale Free topology\n"));
3961 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3963 case GNUNET_TESTING_TOPOLOGY_LINE:
3965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3966 _("Creating straight line topology\n"));
3968 num_connections = create_line (pg, &add_connections, ALLOWED);
3970 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3973 _("Creating topology from file!\n"));
3975 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing",
3978 num_connections = create_from_file (pg, filename, &add_connections,
3983 GNUNET_ERROR_TYPE_WARNING,
3984 "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3985 num_connections = 0;
3988 case GNUNET_TESTING_TOPOLOGY_NONE:
3990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3992 ("Creating no allowed topology (all peers can connect at core level)\n"));
3994 num_connections = pg->total * pg->total; /* Clique is allowed! */
3997 num_connections = 0;
4001 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING",
4004 ret = create_and_copy_friend_files (pg);
4005 if (ret != GNUNET_OK)
4008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4009 _("Failed during friend file copying!\n"));
4011 return GNUNET_SYSERR;
4016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4017 _("Friend files created/copied successfully!\n"));
4022 /* Use the create clique method to initially set all connections as blacklisted. */
4023 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology
4024 != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
4025 create_clique (pg, &add_connections, BLACKLIST, GNUNET_NO);
4027 return num_connections;
4029 unblacklisted_connections = 0;
4030 /* Un-blacklist connections as per the topology specified */
4031 switch (restrict_topology)
4033 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4036 _("Blacklisting all but clique topology\n"));
4038 unblacklisted_connections = create_clique (pg, &remove_connections,
4039 BLACKLIST, GNUNET_NO);
4041 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4044 _("Blacklisting all but small world (ring) topology\n"));
4046 unblacklisted_connections = create_small_world_ring (pg,
4047 &remove_connections,
4050 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4054 ("Blacklisting all but small world (2d-torus) topology\n"));
4056 unblacklisted_connections = create_small_world (pg, &remove_connections,
4059 case GNUNET_TESTING_TOPOLOGY_RING:
4061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4062 _("Blacklisting all but ring topology\n"));
4064 unblacklisted_connections
4065 = create_ring (pg, &remove_connections, BLACKLIST);
4067 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4070 _("Blacklisting all but 2d torus topology\n"));
4072 unblacklisted_connections = create_2d_torus (pg, &remove_connections,
4075 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4078 _("Blacklisting all but Erdos-Renyi topology\n"));
4080 unblacklisted_connections = create_erdos_renyi (pg, &remove_connections,
4083 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4086 _("Blacklisting all but InterNAT topology\n"));
4090 for (off = 0; off < pg->total; off++)
4092 conn_iter = pg->peers[off].allowed_peers_head;
4093 while (conn_iter != NULL)
4095 temp_conn = conn_iter->next;
4096 GNUNET_free(conn_iter);
4097 conn_iter = temp_conn;
4099 pg->peers[off].allowed_peers_head = NULL;
4100 pg->peers[off].allowed_peers_tail = NULL;
4102 conn_iter = pg->peers[off].connect_peers_head;
4103 while (conn_iter != NULL)
4105 temp_conn = conn_iter->next;
4106 GNUNET_free(conn_iter);
4107 conn_iter = temp_conn;
4109 pg->peers[off].connect_peers_head = NULL;
4110 pg->peers[off].connect_peers_tail = NULL;
4112 unblacklisted_connections
4113 = create_nated_internet_copy (pg, &remove_connections, BLACKLIST);
4115 unblacklisted_connections =
4116 create_nated_internet (pg, &remove_connections, BLACKLIST);
4120 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4123 _("Blacklisting all but Scale Free topology\n"));
4125 unblacklisted_connections = create_scale_free (pg, &remove_connections,
4128 case GNUNET_TESTING_TOPOLOGY_LINE:
4130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4131 _("Blacklisting all but straight line topology\n"));
4133 unblacklisted_connections
4134 = create_line (pg, &remove_connections, BLACKLIST);
4139 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
4141 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n",
4142 restrict_transports);
4143 ret = create_and_copy_blacklist_files (pg, restrict_transports);
4144 if (ret != GNUNET_OK)
4147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4148 _("Failed during blacklist file copying!\n"));
4155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4156 _("Blacklist files created/copied successfully!\n"));
4160 return num_connections;
4165 * Iterator for choosing random peers to connect.
4167 * @param cls closure, a RandomContext
4168 * @param key the key the second Daemon was stored under
4169 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4171 * @return GNUNET_YES to continue iteration
4174 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4176 struct RandomContext *random_ctx = cls;
4177 double random_number;
4178 uint32_t second_pos;
4179 GNUNET_HashCode first_hash;
4182 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4183 UINT64_MAX)) / ((double) UINT64_MAX);
4184 if (random_number < random_ctx->percentage)
4186 GNUNET_assert (GNUNET_OK ==
4187 GNUNET_CONTAINER_multihashmap_put (random_ctx->
4188 first->connect_peers_working_set,
4190 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4193 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
4194 uid_from_hash (key, &second_pos);
4195 hash_from_uid (random_ctx->first_uid, &first_hash);
4196 GNUNET_assert (random_ctx->pg->total > second_pos);
4197 GNUNET_assert (GNUNET_YES ==
4198 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
4200 [second_pos].connect_peers,
4209 * Iterator for adding at least X peers to a peers connection set.
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 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4220 struct MinimumContext *min_ctx = cls;
4221 uint32_t second_pos;
4222 GNUNET_HashCode first_hash;
4225 if (GNUNET_CONTAINER_multihashmap_size
4226 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
4228 for (i = 0; i < min_ctx->num_to_add; i++)
4230 if (min_ctx->pg_array[i] == min_ctx->current)
4232 GNUNET_assert (GNUNET_OK ==
4233 GNUNET_CONTAINER_multihashmap_put
4234 (min_ctx->first->connect_peers_working_set, key,
4236 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4237 uid_from_hash (key, &second_pos);
4238 hash_from_uid (min_ctx->first_uid, &first_hash);
4239 GNUNET_assert (min_ctx->pg->total > second_pos);
4240 GNUNET_assert (GNUNET_OK ==
4241 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4243 [second_pos].connect_peers_working_set,
4247 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4248 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
4249 GNUNET_assert (GNUNET_YES ==
4250 GNUNET_CONTAINER_multihashmap_remove
4251 (min_ctx->pg->peers[second_pos].connect_peers,
4252 &first_hash, min_ctx->first->daemon));
4259 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
4264 * Iterator for adding peers to a connection set based on a depth first search.
4266 * @param cls closure, MinimumContext
4267 * @param key the key the second daemon was stored under
4268 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4270 * @return GNUNET_YES to continue iteration
4273 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4275 struct DFSContext *dfs_ctx = cls;
4276 GNUNET_HashCode first_hash;
4278 if (dfs_ctx->current == dfs_ctx->chosen)
4280 GNUNET_assert (GNUNET_OK ==
4281 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4282 first->connect_peers_working_set,
4284 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4285 uid_from_hash (key, &dfs_ctx->second_uid);
4286 hash_from_uid (dfs_ctx->first_uid, &first_hash);
4287 GNUNET_assert (GNUNET_OK ==
4288 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4290 [dfs_ctx->second_uid].connect_peers_working_set,
4294 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4295 GNUNET_assert (GNUNET_YES ==
4296 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
4298 [dfs_ctx->second_uid].connect_peers,
4302 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
4303 return GNUNET_NO; /* We have found our peer, don't iterate more */
4312 * From the set of connections possible, choose percentage percent of connections
4313 * to actually connect.
4315 * @param pg the peergroup we are dealing with
4316 * @param percentage what percent of total connections to make
4319 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg,
4324 struct PeerConnection *conn_iter;
4325 double random_number;
4327 struct RandomContext random_ctx;
4330 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4333 conn_iter = pg->peers[pg_iter].connect_peers_head;
4334 while (conn_iter != NULL)
4337 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4339 / ((double) UINT64_MAX);
4340 if (random_number < percentage)
4342 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4345 conn_iter = conn_iter->next;
4348 random_ctx.first_uid = pg_iter;
4349 random_ctx.first = &pg->peers[pg_iter];
4350 random_ctx.percentage = percentage;
4352 pg->peers[pg_iter].connect_peers_working_set
4353 = GNUNET_CONTAINER_multihashmap_create (pg->total);
4354 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4355 &random_connect_iterator,
4357 /* Now remove the old connections */
4358 GNUNET_CONTAINER_multihashmap_destroy (pg->
4359 peers[pg_iter].connect_peers);
4360 /* And replace with the random set */
4361 pg->peers[pg_iter].connect_peers
4362 = pg->peers[pg_iter].connect_peers_working_set;
4366 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4368 conn_iter = pg->peers[pg_iter].connect_peers_head;
4369 while (pg->peers[pg_iter].connect_peers_head != NULL)
4370 remove_connections (pg, pg_iter,
4371 pg->peers[pg_iter].connect_peers_head->index,
4372 CONNECT, GNUNET_YES);
4374 pg->peers[pg_iter].connect_peers_head
4375 = pg->peers[pg_iter].connect_peers_working_set_head;
4376 pg->peers[pg_iter].connect_peers_tail
4377 = pg->peers[pg_iter].connect_peers_working_set_tail;
4378 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4379 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4384 * Count the number of connections in a linked list of connections.
4386 * @param conn_list the connection list to get the count of
4388 * @return the number of elements in the list
4391 count_connections(struct PeerConnection *conn_list)
4393 struct PeerConnection *iter;
4397 while (iter != NULL)
4406 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
4409 unsigned int pg_iter;
4411 struct PeerConnection *conn_iter;
4415 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4418 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
4419 while (conn_iter != NULL)
4422 conn_iter = conn_iter->next;
4426 GNUNET_CONTAINER_multihashmap_size (pg->
4428 [pg_iter].connect_peers_working_set);
4436 count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
4439 unsigned int pg_iter;
4441 struct PeerConnection *conn_iter;
4445 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4448 conn_iter = pg->peers[pg_iter].allowed_peers_head;
4449 while (conn_iter != NULL)
4452 conn_iter = conn_iter->next;
4456 GNUNET_CONTAINER_multihashmap_size (pg->
4458 [pg_iter].allowed_peers);
4466 * From the set of connections possible, choose at least num connections per
4469 * @param pg the peergroup we are dealing with
4470 * @param num how many connections at least should each peer have (if possible)?
4473 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4476 struct MinimumContext minimum_ctx;
4478 struct PeerConnection *conn_iter;
4479 unsigned int temp_list_size;
4482 uint32_t random; /* Random list entry to connect peer to */
4487 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4490 = count_connections (pg->peers[pg_iter].connect_peers_head);
4491 if (temp_list_size == 0)
4493 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4494 "Peer %d has 0 connections!?!?\n", pg_iter);
4497 for (i = 0; i < num; i++)
4499 random = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4501 conn_iter = pg->peers[pg_iter].connect_peers_head;
4502 for (count = 0; count < random; count++)
4503 conn_iter = conn_iter->next;
4504 /* We now have a random connection, connect it! */
4505 GNUNET_assert(conn_iter != NULL);
4506 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4511 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4513 pg->peers[pg_iter].connect_peers_working_set =
4514 GNUNET_CONTAINER_multihashmap_create (num);
4517 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4519 minimum_ctx.first_uid = pg_iter;
4520 minimum_ctx.pg_array =
4521 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
4522 GNUNET_CONTAINER_multihashmap_size
4523 (pg->peers[pg_iter].connect_peers));
4524 minimum_ctx.first = &pg->peers[pg_iter];
4525 minimum_ctx.pg = pg;
4526 minimum_ctx.num_to_add = num;
4527 minimum_ctx.current = 0;
4528 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4529 &minimum_connect_iterator,
4533 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4535 /* Remove the "old" connections */
4536 GNUNET_CONTAINER_multihashmap_destroy (pg->
4537 peers[pg_iter].connect_peers);
4538 /* And replace with the working set */
4539 pg->peers[pg_iter].connect_peers =
4540 pg->peers[pg_iter].connect_peers_working_set;
4543 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4545 while (pg->peers[pg_iter].connect_peers_head != NULL)
4547 conn_iter = pg->peers[pg_iter].connect_peers_head;
4548 GNUNET_CONTAINER_DLL_remove(pg->peers[pg_iter].connect_peers_head,
4549 pg->peers[pg_iter].connect_peers_tail,
4551 GNUNET_free(conn_iter);
4552 /*remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT, GNUNET_YES);*/
4555 pg->peers[pg_iter].connect_peers_head
4556 = pg->peers[pg_iter].connect_peers_working_set_head;
4557 pg->peers[pg_iter].connect_peers_tail
4558 = pg->peers[pg_iter].connect_peers_working_set_tail;
4559 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4560 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4565 struct FindClosestContext
4568 * The currently known closest peer.
4570 struct GNUNET_TESTING_Daemon *closest;
4573 * The info for the peer we are adding connections for.
4575 struct PeerData *curr_peer;
4578 * The distance (bits) between the current
4579 * peer and the currently known closest.
4581 unsigned int closest_dist;
4584 * The offset of the closest known peer in
4587 unsigned int closest_num;
4591 * Iterator over hash map entries of the allowed
4592 * peer connections. Find the closest, not already
4593 * connected peer and return it.
4595 * @param cls closure (struct FindClosestContext)
4596 * @param key current key code (hash of offset in pg)
4597 * @param value value in the hash map - a GNUNET_TESTING_Daemon
4598 * @return GNUNET_YES if we should continue to
4603 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
4605 struct FindClosestContext *closest_ctx = cls;
4606 struct GNUNET_TESTING_Daemon *daemon = value;
4608 if (((closest_ctx->closest == NULL) ||
4609 (GNUNET_CRYPTO_hash_matching_bits
4610 (&daemon->id.hashPubKey,
4611 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
4612 closest_ctx->closest_dist))
4614 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
4615 curr_peer->connect_peers,
4618 closest_ctx->closest_dist =
4619 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
4620 &closest_ctx->curr_peer->daemon->
4622 closest_ctx->closest = daemon;
4623 uid_from_hash (key, &closest_ctx->closest_num);
4629 * From the set of connections possible, choose at num connections per
4630 * peer based on depth which are closest out of those allowed. Guaranteed
4631 * to add num peers to connect to, provided there are that many peers
4632 * in the underlay topology to connect to.
4634 * @param pg the peergroup we are dealing with
4635 * @param num how many connections at least should each peer have (if possible)?
4636 * @param proc processor to actually add the connections
4637 * @param list the peer list to use
4640 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
4641 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
4646 struct FindClosestContext closest_ctx;
4651 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
4653 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4655 closest_ctx.curr_peer = &pg->peers[pg_iter];
4656 closest_ctx.closest = NULL;
4657 closest_ctx.closest_dist = 0;
4658 closest_ctx.closest_num = 0;
4659 GNUNET_CONTAINER_multihashmap_iterate (pg->
4660 peers[pg_iter].allowed_peers,
4661 &find_closest_peers,
4663 if (closest_ctx.closest != NULL)
4665 GNUNET_assert (closest_ctx.closest_num < pg->total);
4666 proc (pg, pg_iter, closest_ctx.closest_num, list);
4674 * From the set of connections possible, choose at least num connections per
4675 * peer based on depth first traversal of peer connections. If DFS leaves
4676 * peers unconnected, ensure those peers get connections.
4678 * @param pg the peergroup we are dealing with
4679 * @param num how many connections at least should each peer have (if possible)?
4682 perform_dfs(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4686 uint32_t starting_peer;
4687 uint32_t least_connections;
4688 uint32_t random_connection;
4690 unsigned int temp_count;
4691 struct PeerConnection *peer_iter;
4693 struct DFSContext dfs_ctx;
4694 GNUNET_HashCode second_hash;
4700 while ((count_workingset_connections (pg) < num * pg->total)
4701 && (count_allowed_connections (pg) > 0))
4703 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4705 least_connections = -1; /* Set to very high number */
4706 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4709 = count_connections (
4710 pg->peers[pg_iter].connect_peers_working_set_head);
4711 if (temp_count < least_connections)
4713 starting_peer = pg_iter;
4714 least_connections = temp_count;
4720 = count_connections (pg->peers[starting_peer].connect_peers_head);
4721 if (temp_count == 0)
4722 continue; /* FIXME: infinite loop? */
4724 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4727 peer_iter = pg->peers[starting_peer].connect_peers_head;
4728 while (temp_count < random_connection)
4730 peer_iter = peer_iter->next;
4733 GNUNET_assert(peer_iter != NULL);
4734 add_connections (pg, starting_peer, peer_iter->index, WORKING_SET,
4736 remove_connections (pg, starting_peer, peer_iter->index, CONNECT,
4738 starting_peer = peer_iter->index;
4743 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4745 pg->peers[pg_iter].connect_peers_working_set =
4746 GNUNET_CONTAINER_multihashmap_create (num);
4751 while ((count_workingset_connections (pg) < num * pg->total)
4752 && (count_allowed_connections (pg) > 0))
4754 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4756 least_connections = -1; /* Set to very high number */
4757 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4759 if (GNUNET_CONTAINER_multihashmap_size
4760 (pg->peers[pg_iter].connect_peers_working_set) <
4763 starting_peer = pg_iter;
4765 GNUNET_CONTAINER_multihashmap_size (pg->
4767 [pg_iter].connect_peers_working_set);
4772 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4778 /* Choose a random peer from the chosen peers set of connections to add */
4780 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4781 GNUNET_CONTAINER_multihashmap_size
4782 (pg->peers[starting_peer].connect_peers));
4783 dfs_ctx.first_uid = starting_peer;
4784 dfs_ctx.first = &pg->peers[starting_peer];
4786 dfs_ctx.current = 0;
4788 GNUNET_CONTAINER_multihashmap_iterate (pg->
4790 [starting_peer].connect_peers,
4791 &dfs_connect_iterator, &dfs_ctx);
4792 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4793 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4794 GNUNET_assert (GNUNET_YES ==
4795 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4796 [starting_peer].connect_peers,
4800 [dfs_ctx.second_uid].daemon));
4801 starting_peer = dfs_ctx.second_uid;
4804 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4806 /* Remove the "old" connections */
4807 GNUNET_CONTAINER_multihashmap_destroy (pg->
4808 peers[pg_iter].connect_peers);
4809 /* And replace with the working set */
4810 pg->peers[pg_iter].connect_peers =
4811 pg->peers[pg_iter].connect_peers_working_set;
4817 * Internal callback for topology information for a particular peer.
4820 internal_topology_callback(void *cls, const struct GNUNET_PeerIdentity *peer,
4821 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4823 struct CoreContext *core_ctx = cls;
4824 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4826 if (peer == NULL) /* Either finished, or something went wrong */
4828 iter_ctx->completed++;
4829 iter_ctx->connected--;
4830 /* One core context allocated per iteration, must free! */
4831 GNUNET_free (core_ctx);
4835 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id, peer, NULL);
4838 if (iter_ctx->completed == iter_ctx->total)
4840 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4841 /* Once all are done, free the iteration context */
4842 GNUNET_free (iter_ctx);
4847 * Check running topology iteration tasks, if below max start a new one, otherwise
4848 * schedule for some time in the future.
4851 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4853 struct CoreContext *core_context = cls;
4854 struct TopologyIterateContext *topology_context =
4855 (struct TopologyIterateContext *) core_context->iter_context;
4856 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
4859 if (topology_context->connected
4860 > topology_context->pg->max_outstanding_connections)
4862 #if VERBOSE_TESTING > 2
4863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4865 ("Delaying connect, we have too many outstanding connections!\n"));
4867 GNUNET_SCHEDULER_add_delayed (
4868 GNUNET_TIME_relative_multiply (
4869 GNUNET_TIME_UNIT_MILLISECONDS,
4871 &schedule_get_topology, core_context);
4875 #if VERBOSE_TESTING > 2
4876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4877 _("Creating connection, outstanding_connections is %d\n"),
4878 outstanding_connects);
4880 topology_context->connected++;
4882 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4883 &internal_topology_callback,
4886 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4887 internal_topology_callback (core_context, NULL, NULL);
4893 * Iterate over all (running) peers in the peer group, retrieve
4894 * all connections that each currently has.
4897 GNUNET_TESTING_get_topology(struct GNUNET_TESTING_PeerGroup *pg,
4898 GNUNET_TESTING_NotifyTopology cb, void *cls)
4900 struct TopologyIterateContext *topology_context;
4901 struct CoreContext *core_ctx;
4903 unsigned int total_count;
4905 /* Allocate a single topology iteration context */
4906 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4907 topology_context->topology_cb = cb;
4908 topology_context->cls = cls;
4909 topology_context->pg = pg;
4911 for (i = 0; i < pg->total; i++)
4913 if (pg->peers[i].daemon->running == GNUNET_YES)
4915 /* Allocate one core context per core we need to connect to */
4916 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4917 core_ctx->daemon = pg->peers[i].daemon;
4918 /* Set back pointer to topology iteration context */
4919 core_ctx->iter_context = topology_context;
4920 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4924 if (total_count == 0)
4926 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4927 GNUNET_free (topology_context);
4930 topology_context->total = total_count;
4935 * Callback function to process statistic values.
4936 * This handler is here only really to insert a peer
4937 * identity (or daemon) so the statistics can be uniquely
4938 * tied to a single running peer.
4940 * @param cls closure
4941 * @param subsystem name of subsystem that created the statistic
4942 * @param name the name of the datum
4943 * @param value the current value
4944 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4945 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4948 internal_stats_callback(void *cls, const char *subsystem, const char *name,
4949 uint64_t value, int is_persistent)
4951 struct StatsCoreContext *core_context = cls;
4952 struct StatsIterateContext *stats_context =
4953 (struct StatsIterateContext *) core_context->iter_context;
4955 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4956 subsystem, name, value, is_persistent);
4960 * Internal continuation call for statistics iteration.
4962 * @param cls closure, the CoreContext for this iteration
4963 * @param success whether or not the statistics iterations
4964 * was canceled or not (we don't care)
4967 internal_stats_cont(void *cls, int success)
4969 struct StatsCoreContext *core_context = cls;
4970 struct StatsIterateContext *stats_context =
4971 (struct StatsIterateContext *) core_context->iter_context;
4973 stats_context->connected--;
4974 stats_context->completed++;
4976 if (stats_context->completed == stats_context->total)
4978 stats_context->cont (stats_context->cls, GNUNET_YES);
4979 GNUNET_free (stats_context);
4982 if (core_context->stats_handle != NULL)
4983 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4985 GNUNET_free (core_context);
4989 * Check running topology iteration tasks, if below max start a new one, otherwise
4990 * schedule for some time in the future.
4993 schedule_get_statistics(void *cls,
4994 const struct GNUNET_SCHEDULER_TaskContext *tc)
4996 struct StatsCoreContext *core_context = cls;
4997 struct StatsIterateContext *stats_context =
4998 (struct StatsIterateContext *) core_context->iter_context;
5000 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5003 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
5005 #if VERBOSE_TESTING > 2
5006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5008 ("Delaying connect, we have too many outstanding connections!\n"));
5010 GNUNET_SCHEDULER_add_delayed (
5011 GNUNET_TIME_relative_multiply (
5012 GNUNET_TIME_UNIT_MILLISECONDS,
5014 &schedule_get_statistics, core_context);
5018 #if VERBOSE_TESTING > 2
5019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5020 _("Creating connection, outstanding_connections is %d\n"),
5021 outstanding_connects);
5024 stats_context->connected++;
5025 core_context->stats_handle
5026 = GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
5027 if (core_context->stats_handle == NULL)
5029 internal_stats_cont (core_context, GNUNET_NO);
5033 core_context->stats_get_handle
5034 = GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
5035 GNUNET_TIME_relative_get_forever (),
5036 &internal_stats_cont,
5037 &internal_stats_callback, core_context);
5038 if (core_context->stats_get_handle == NULL)
5039 internal_stats_cont (core_context, GNUNET_NO);
5044 struct DuplicateStats
5047 * Next item in the list
5049 struct DuplicateStats *next;
5052 * Nasty string, concatenation of relevant information.
5054 char *unique_string;
5058 * Check whether the combination of port/host/unix domain socket
5059 * already exists in the list of peers being checked for statistics.
5061 * @param pg the peergroup in question
5062 * @param specific_peer the peer we're concerned with
5063 * @param stats_list the list to return to the caller
5065 * @return GNUNET_YES if the statistics instance has been seen already,
5066 * GNUNET_NO if not (and we may have added it to the list)
5069 stats_check_existing(struct GNUNET_TESTING_PeerGroup *pg,
5070 struct PeerData *specific_peer,
5071 struct DuplicateStats **stats_list)
5073 struct DuplicateStats *pos;
5074 char *unix_domain_socket;
5075 unsigned long long port;
5078 != GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
5079 "single_statistics_per_host"))
5080 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
5083 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg,
5086 &unix_domain_socket))
5089 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg,
5090 "statistics", "port",
5093 GNUNET_free(unix_domain_socket);
5097 if (specific_peer->daemon->hostname != NULL)
5098 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
5099 unix_domain_socket, port);
5101 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
5105 if (0 == strcmp (to_match, pos->unique_string))
5107 GNUNET_free (unix_domain_socket);
5108 GNUNET_free (to_match);
5113 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
5114 pos->unique_string = to_match;
5115 pos->next = *stats_list;
5117 GNUNET_free (unix_domain_socket);
5122 * Iterate over all (running) peers in the peer group, retrieve
5123 * all statistics from each.
5125 * @param pg the peergroup to iterate statistics of
5126 * @param cont continuation to call once all stats have been retrieved
5127 * @param proc processing function for each statistic from each peer
5128 * @param cls closure to pass to proc
5132 GNUNET_TESTING_get_statistics(struct GNUNET_TESTING_PeerGroup *pg,
5133 GNUNET_STATISTICS_Callback cont,
5134 GNUNET_TESTING_STATISTICS_Iterator proc,
5137 struct StatsIterateContext *stats_context;
5138 struct StatsCoreContext *core_ctx;
5140 unsigned int total_count;
5141 struct DuplicateStats *stats_list;
5142 struct DuplicateStats *pos;
5145 /* Allocate a single stats iteration context */
5146 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
5147 stats_context->cont = cont;
5148 stats_context->proc = proc;
5149 stats_context->cls = cls;
5150 stats_context->pg = pg;
5153 for (i = 0; i < pg->total; i++)
5155 if ((pg->peers[i].daemon->running == GNUNET_YES) && (GNUNET_NO
5156 == stats_check_existing (pg, &pg->peers[i], &stats_list)))
5158 /* Allocate one core context per core we need to connect to */
5159 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
5160 core_ctx->daemon = pg->peers[i].daemon;
5161 /* Set back pointer to topology iteration context */
5162 core_ctx->iter_context = stats_context;
5163 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
5168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5169 "Retrieving stats from %u total instances.\n", total_count);
5170 stats_context->total = total_count;
5171 if (stats_list != NULL)
5176 GNUNET_free (pos->unique_string);
5177 stats_list = pos->next;
5179 pos = stats_list->next;
5186 * Stop the connection process temporarily.
5188 * @param pg the peer group to stop connecting
5191 GNUNET_TESTING_stop_connections(struct GNUNET_TESTING_PeerGroup *pg)
5193 pg->stop_connects = GNUNET_YES;
5197 * Resume the connection process temporarily.
5199 * @param pg the peer group to resume connecting
5202 GNUNET_TESTING_resume_connections(struct GNUNET_TESTING_PeerGroup *pg)
5204 pg->stop_connects = GNUNET_NO;
5208 * There are many ways to connect peers that are supported by this function.
5209 * To connect peers in the same topology that was created via the
5210 * GNUNET_TESTING_create_topology, the topology variable must be set to
5211 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
5212 * a new instance of that topology will be generated and attempted to be
5213 * connected. This could result in some connections being impossible,
5214 * because some topologies are non-deterministic.
5216 * @param pg the peer group struct representing the running peers
5217 * @param topology which topology to connect the peers in
5218 * @param options options for connecting the topology
5219 * @param option_modifier modifier for options that take a parameter
5220 * @param connect_timeout how long to wait before giving up on connecting
5222 * @param connect_attempts how many times to attempt to connect two peers
5223 * over the connect_timeout duration
5224 * @param notify_callback notification to be called once all connections completed
5225 * @param notify_cls closure for notification callback
5227 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
5230 GNUNET_TESTING_connect_topology(
5231 struct GNUNET_TESTING_PeerGroup *pg,
5232 enum GNUNET_TESTING_Topology topology,
5233 enum GNUNET_TESTING_TopologyOption options,
5234 double option_modifier,
5235 struct GNUNET_TIME_Relative connect_timeout,
5236 unsigned int connect_attempts,
5237 GNUNET_TESTING_NotifyCompletion notify_callback,
5242 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
5243 #if VERBOSE_TOPOLOGY
5244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5245 _("Creating clique CONNECT topology\n"));
5247 create_clique (pg, &add_connections, CONNECT, GNUNET_NO);
5249 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
5250 #if VERBOSE_TOPOLOGY
5251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5252 _("Creating small world (ring) CONNECT topology\n"));
5254 create_small_world_ring (pg, &add_connections, CONNECT);
5256 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
5257 #if VERBOSE_TOPOLOGY
5258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5259 _("Creating small world (2d-torus) CONNECT topology\n"));
5261 create_small_world (pg, &add_connections, CONNECT);
5263 case GNUNET_TESTING_TOPOLOGY_RING:
5264 #if VERBOSE_TOPOLOGY
5265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring CONNECT topology\n"));
5267 create_ring (pg, &add_connections, CONNECT);
5269 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
5270 #if VERBOSE_TOPOLOGY
5271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5272 _("Creating 2d torus CONNECT topology\n"));
5274 create_2d_torus (pg, &add_connections, CONNECT);
5276 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
5277 #if VERBOSE_TOPOLOGY
5278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5279 _("Creating Erdos-Renyi CONNECT topology\n"));
5281 create_erdos_renyi (pg, &add_connections, CONNECT);
5283 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
5284 #if VERBOSE_TOPOLOGY
5285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5286 _("Creating InterNAT CONNECT topology\n"));
5288 create_nated_internet (pg, &add_connections, CONNECT);
5290 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
5291 #if VERBOSE_TOPOLOGY
5292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5293 _("Creating Scale Free CONNECT topology\n"));
5295 create_scale_free (pg, &add_connections, CONNECT);
5297 case GNUNET_TESTING_TOPOLOGY_LINE:
5298 #if VERBOSE_TOPOLOGY
5299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5300 _("Creating straight line CONNECT topology\n"));
5302 create_line (pg, &add_connections, CONNECT);
5304 case GNUNET_TESTING_TOPOLOGY_NONE:
5305 #if VERBOSE_TOPOLOGY
5306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating no CONNECT topology\n"));
5308 copy_allowed_topology (pg);
5311 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5312 ("Unknown topology specification, can't connect peers!\n"));
5313 return GNUNET_SYSERR;
5318 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
5319 #if VERBOSE_TOPOLOGY
5320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5321 ("Connecting random subset (%'.2f percent) of possible peers\n"), 100
5324 choose_random_connections (pg, option_modifier);
5326 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
5327 #if VERBOSE_TOPOLOGY
5328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5329 _("Connecting a minimum of %u peers each (if possible)\n"),
5330 (unsigned int) option_modifier);
5332 choose_minimum (pg, (unsigned int) option_modifier);
5334 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
5335 #if VERBOSE_TOPOLOGY
5336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5337 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
5338 (unsigned int) option_modifier);
5341 perform_dfs (pg, (int) option_modifier);
5344 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
5345 #if VERBOSE_TOPOLOGY
5346 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5347 ("Finding additional %u closest peers each (if possible)\n"),
5348 (unsigned int) option_modifier);
5351 add_closest (pg, (unsigned int) option_modifier,
5352 &add_connections, CONNECT);
5355 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
5357 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
5363 return connect_topology (pg, connect_timeout, connect_attempts,
5364 notify_callback, notify_cls);
5368 * Lookup and return the number of SSH connections to a host.
5370 * @param hostname the hostname to lookup in the list
5371 * @param pg the peergroup that the host belongs to
5373 * @return the number of current ssh connections to the host
5376 count_outstanding_at_host(const char *hostname,
5377 struct GNUNET_TESTING_PeerGroup *pg)
5379 struct OutstandingSSH *pos;
5381 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5383 GNUNET_assert(pos != NULL);
5384 return pos->outstanding;
5388 * Increment the number of SSH connections to a host by one.
5390 * @param hostname the hostname to lookup in the list
5391 * @param pg the peergroup that the host belongs to
5395 increment_outstanding_at_host(const char *hostname,
5396 struct GNUNET_TESTING_PeerGroup *pg)
5398 struct OutstandingSSH *pos;
5400 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5402 GNUNET_assert(pos != NULL);
5407 * Decrement the number of SSH connections to a host by one.
5409 * @param hostname the hostname to lookup in the list
5410 * @param pg the peergroup that the host belongs to
5414 decrement_outstanding_at_host(const char *hostname,
5415 struct GNUNET_TESTING_PeerGroup *pg)
5417 struct OutstandingSSH *pos;
5419 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5421 GNUNET_assert(pos != NULL);
5426 * Callback that is called whenever a hostkey is generated
5427 * for a peer. Call the real callback and decrement the
5428 * starting counter for the peergroup.
5430 * @param cls closure
5431 * @param id identifier for the daemon, NULL on error
5432 * @param d handle for the daemon
5433 * @param emsg error message (NULL on success)
5436 internal_hostkey_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5437 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5439 struct InternalStartContext *internal_context = cls;
5440 internal_context->peer->pg->starting--;
5441 internal_context->peer->pg->started++;
5442 if (internal_context->hostname != NULL)
5443 decrement_outstanding_at_host (internal_context->hostname,
5444 internal_context->peer->pg);
5445 if (internal_context->hostkey_callback != NULL)
5446 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
5448 else if (internal_context->peer->pg->started
5449 == internal_context->peer->pg->total)
5451 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
5452 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
5457 * Callback that is called whenever a peer has finished starting.
5458 * Call the real callback and decrement the starting counter
5459 * for the peergroup.
5461 * @param cls closure
5462 * @param id identifier for the daemon, NULL on error
5464 * @param d handle for the daemon
5465 * @param emsg error message (NULL on success)
5468 internal_startup_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5469 const struct GNUNET_CONFIGURATION_Handle *cfg,
5470 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5472 struct InternalStartContext *internal_context = cls;
5473 internal_context->peer->pg->starting--;
5474 if (internal_context->hostname != NULL)
5475 decrement_outstanding_at_host (internal_context->hostname,
5476 internal_context->peer->pg);
5477 if (internal_context->start_cb != NULL)
5478 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
5483 internal_continue_startup(void *cls,
5484 const struct GNUNET_SCHEDULER_TaskContext *tc)
5486 struct InternalStartContext *internal_context = cls;
5488 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5493 if ((internal_context->peer->pg->starting
5494 < internal_context->peer->pg->max_concurrent_ssh)
5495 || ((internal_context->hostname != NULL)
5496 && (count_outstanding_at_host (internal_context->hostname,
5497 internal_context->peer->pg)
5498 < internal_context->peer->pg->max_concurrent_ssh)))
5500 if (internal_context->hostname != NULL)
5501 increment_outstanding_at_host (internal_context->hostname,
5502 internal_context->peer->pg);
5503 internal_context->peer->pg->starting++;
5504 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
5508 GNUNET_SCHEDULER_add_delayed (
5509 GNUNET_TIME_relative_multiply (
5510 GNUNET_TIME_UNIT_MILLISECONDS,
5512 &internal_continue_startup,
5518 * Callback for informing us about a successful
5519 * or unsuccessful churn start call.
5521 * @param cls a ChurnContext
5522 * @param id the peer identity of the started peer
5523 * @param cfg the handle to the configuration of the peer
5524 * @param d handle to the daemon for the peer
5525 * @param emsg NULL on success, non-NULL on failure
5529 churn_start_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5530 const struct GNUNET_CONFIGURATION_Handle *cfg,
5531 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5533 struct ChurnRestartContext *startup_ctx = cls;
5534 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
5536 unsigned int total_left;
5537 char *error_message;
5539 error_message = NULL;
5542 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5543 "Churn stop callback failed with error `%s'\n", emsg);
5544 churn_ctx->num_failed_start++;
5548 churn_ctx->num_to_start--;
5552 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5553 "Started peer, %d left.\n", churn_ctx->num_to_start);
5556 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
5557 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5559 if (total_left == 0)
5561 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
5564 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5565 churn_ctx->num_failed_start,
5566 churn_ctx->num_failed_stop);
5567 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5568 GNUNET_free_non_null (error_message);
5569 GNUNET_free (churn_ctx);
5570 GNUNET_free (startup_ctx);
5575 schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5577 struct PeerRestartContext *peer_restart_ctx = cls;
5578 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
5580 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5581 GNUNET_SCHEDULER_add_delayed (
5582 GNUNET_TIME_relative_multiply (
5583 GNUNET_TIME_UNIT_MILLISECONDS,
5585 &schedule_churn_restart, peer_restart_ctx);
5588 if (startup_ctx->churn_ctx->service != NULL)
5589 GNUNET_TESTING_daemon_start_stopped_service (peer_restart_ctx->daemon,
5590 startup_ctx->churn_ctx->service,
5591 startup_ctx->timeout,
5592 &churn_start_callback, startup_ctx);
5594 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
5595 startup_ctx->timeout,
5596 &churn_start_callback, startup_ctx);
5597 GNUNET_free (peer_restart_ctx);
5603 internal_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5605 struct InternalStartContext *internal_context = cls;
5607 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
5612 if ((internal_context->peer->pg->starting
5613 < internal_context->peer->pg->max_concurrent_ssh)
5614 || ((internal_context->hostname != NULL)
5615 && (count_outstanding_at_host (internal_context->hostname,
5616 internal_context->peer->pg)
5617 < internal_context->peer->pg->max_concurrent_ssh)))
5619 if (internal_context->hostname != NULL)
5620 increment_outstanding_at_host (internal_context->hostname,
5621 internal_context->peer->pg);
5622 internal_context->peer->pg->starting++;
5623 internal_context->peer->daemon
5624 = GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
5625 internal_context->timeout,
5627 internal_context->hostname,
5628 internal_context->username,
5629 internal_context->sshport,
5630 internal_context->hostkey,
5631 &internal_hostkey_callback,
5633 &internal_startup_callback,
5638 GNUNET_SCHEDULER_add_delayed (
5639 GNUNET_TIME_relative_multiply (
5640 GNUNET_TIME_UNIT_MILLISECONDS,
5642 &internal_start, internal_context);
5645 #if USE_START_HELPER
5647 struct PeerStartHelperContext
5649 struct GNUNET_TESTING_PeerGroup *pg;
5651 struct HostData *host;
5653 struct GNUNET_OS_Process *proc;
5657 check_peers_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5659 struct PeerStartHelperContext *helper = cls;
5660 enum GNUNET_OS_ProcessStatusType type;
5663 GNUNET_TESTING_NotifyDaemonRunning cb;
5665 if (GNUNET_NO == GNUNET_OS_process_status (helper->proc, &type, &code)) /* Still running, wait some more! */
5667 GNUNET_SCHEDULER_add_delayed(GNUNET_CONSTANTS_EXEC_WAIT, &check_peers_started, helper);
5671 helper->pg->starting--;
5672 if (helper->pg->starting == 0) /* All peers have finished starting! */
5674 /* Call the peer started callback for each peer, set proper FSM state (?) */
5675 for (i = 0; i < helper->pg->total; i++)
5677 cb = helper->pg->peers[i].daemon->cb;
5678 helper->pg->peers[i].daemon->cb = NULL;
5679 helper->pg->peers[i].daemon->running = GNUNET_YES;
5680 helper->pg->peers[i].daemon->phase = SP_START_DONE;
5683 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
5684 cb (helper->pg->peers[i].daemon->cb_cls,
5685 &helper->pg->peers[i].daemon->id,
5686 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5687 "Failed to execute peerStartHelper.pl, or return code bad!");
5689 cb (helper->pg->peers[i].daemon->cb_cls,
5690 &helper->pg->peers[i].daemon->id,
5691 helper->pg->peers[i].daemon->cfg, helper->pg->peers[i].daemon,
5698 GNUNET_OS_process_close(helper->proc);
5702 start_peer_helper (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5704 struct PeerStartHelperContext *helper = cls;
5705 char *baseservicehome;
5708 /* ssh user@host peerStartHelper /path/to/basedirectory */
5709 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (helper->pg->cfg, "PATHS", "SERVICEHOME",
5711 GNUNET_asprintf(&tempdir, "%s/%s/", baseservicehome, helper->host->hostname);
5712 if (NULL != helper->host->username)
5713 GNUNET_asprintf (&arg, "%s@%s", helper->host->username, helper->host->hostname);
5715 GNUNET_asprintf (&arg, "%s", helper->host->hostname);
5717 /* FIXME: Doesn't support ssh_port option! */
5718 helper->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5719 "peerStartHelper.pl", tempdir, NULL);
5720 GNUNET_assert(helper->proc != NULL);
5721 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "starting peers with cmd ssh %s %s %s\n", arg, "peerStartHelper.pl", tempdir);
5722 GNUNET_SCHEDULER_add_now (&check_peers_started, helper);
5723 GNUNET_free (tempdir);
5724 GNUNET_free (baseservicehome);
5730 * Function which continues a peer group starting up
5731 * after successfully generating hostkeys for each peer.
5733 * @param pg the peer group to continue starting
5737 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
5741 #if USE_START_HELPER
5742 if ((pg->num_hosts > 0) && (pg->hostkey_data != NULL))
5744 struct PeerStartHelperContext *helper;
5745 pg->starting = pg->num_hosts;
5746 for (i = 0; i < pg->num_hosts; i++)
5748 helper = GNUNET_malloc(sizeof(struct PeerStartHelperContext));
5750 helper->host = &pg->hosts[i];
5751 GNUNET_SCHEDULER_add_now(&start_peer_helper, helper);
5757 for (i = 0; i < pg->total; i++)
5759 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5760 &pg->peers[i].internal_context);
5765 for (i = 0; i < pg->total; i++)
5767 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5768 &pg->peers[i].internal_context);
5773 #if USE_START_HELPER
5775 call_hostkey_callbacks (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5777 struct GNUNET_TESTING_PeerGroup *pg = cls;
5779 for (i = 0; i < pg->total; i++)
5781 if (pg->peers[i].internal_context.hostkey_callback != NULL)
5782 pg->peers[i].internal_context.hostkey_callback (pg->peers[i].internal_context.hostkey_cls,
5783 &pg->peers[i].daemon->id,
5784 pg->peers[i].daemon,
5788 if (pg->peers[0].internal_context.hostkey_callback == NULL)
5789 GNUNET_TESTING_daemons_continue_startup (pg);
5794 * Start count gnunet instances with the same set of transports and
5795 * applications. The port numbers (any option called "PORT") will be
5796 * adjusted to ensure that no two peers running on the same system
5797 * have the same port(s) in their respective configurations.
5799 * @param cfg configuration template to use
5800 * @param total number of daemons to start
5801 * @param max_concurrent_connections for testing, how many peers can
5802 * we connect to simultaneously
5803 * @param max_concurrent_ssh when starting with ssh, how many ssh
5804 * connections will we allow at once (based on remote hosts allowed!)
5805 * @param timeout total time allowed for peers to start
5806 * @param hostkey_callback function to call on each peers hostkey generation
5807 * if NULL, peers will be started by this call, if non-null,
5808 * GNUNET_TESTING_daemons_continue_startup must be called after
5809 * successful hostkey generation
5810 * @param hostkey_cls closure for hostkey callback
5811 * @param cb function to call on each daemon that was started
5812 * @param cb_cls closure for cb
5813 * @param connect_callback function to call each time two hosts are connected
5814 * @param connect_callback_cls closure for connect_callback
5815 * @param hostnames linked list of host structs to use to start peers on
5816 * (NULL to run on localhost only)
5818 * @return NULL on error, otherwise handle to control peer group
5820 struct GNUNET_TESTING_PeerGroup *
5821 GNUNET_TESTING_daemons_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
5823 unsigned int max_concurrent_connections,
5824 unsigned int max_concurrent_ssh,
5825 struct GNUNET_TIME_Relative timeout,
5826 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
5828 GNUNET_TESTING_NotifyDaemonRunning cb,
5830 GNUNET_TESTING_NotifyConnection connect_callback,
5831 void *connect_callback_cls,
5832 const struct GNUNET_TESTING_Host *hostnames)
5834 struct GNUNET_TESTING_PeerGroup *pg;
5835 const struct GNUNET_TESTING_Host *hostpos;
5836 const char *hostname;
5837 const char *username;
5838 char *baseservicehome;
5839 char *newservicehome;
5841 char *hostkeys_file;
5844 struct GNUNET_DISK_FileHandle *fd;
5845 struct GNUNET_CONFIGURATION_Handle *pcfg;
5847 struct OutstandingSSH *ssh_entry;
5848 unsigned int hostcnt;
5855 uint64_t total_hostkeys;
5856 struct GNUNET_OS_Process *proc;
5867 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
5869 pg->notify_connection = connect_callback;
5870 pg->notify_connection_cls = connect_callback_cls;
5872 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
5873 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
5874 pg->max_outstanding_connections = max_concurrent_connections;
5875 pg->max_concurrent_ssh = max_concurrent_ssh;
5876 if (NULL != hostnames)
5879 hostpos = hostnames;
5880 while (hostpos != NULL)
5882 hostpos = hostpos->next;
5885 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5888 hostpos = hostnames;
5889 while (hostpos != NULL)
5891 pg->hosts[off].minport = LOW_PORT;
5892 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
5893 if (hostpos->username != NULL)
5894 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
5895 pg->hosts[off].sshport = hostpos->port;
5896 hostpos = hostpos->next;
5906 pg->num_hosts = off;
5914 /* Create the servicehome directory for each remote peer */
5915 GNUNET_assert(GNUNET_OK ==
5916 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
5918 for (i = 0; i < pg->num_hosts; i++)
5920 ssh_entry = GNUNET_malloc(sizeof(struct OutstandingSSH));
5921 ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
5922 GNUNET_CONTAINER_DLL_insert(pg->ssh_head, pg->ssh_tail, ssh_entry);
5923 GNUNET_asprintf(&tmpdir, "%s/%s", baseservicehome, pg->hosts[i].hostname);
5924 if (NULL != pg->hosts[i].username)
5925 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username,
5926 pg->hosts[i].hostname);
5928 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
5929 if (pg->hosts[i].sshport != 0)
5931 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
5932 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-P",
5937 arg, "mkdir -p", tmpdir,
5941 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5942 "mkdir -p", tmpdir, NULL);
5943 GNUNET_assert(proc != NULL);
5944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5945 "Creating remote dir with command ssh %s %s %s\n", arg,
5946 " mkdir -p ", tmpdir);
5947 GNUNET_free(tmpdir);
5949 GNUNET_OS_process_wait (proc);
5950 GNUNET_OS_process_close(proc);
5952 GNUNET_free(baseservicehome);
5953 baseservicehome = NULL;
5955 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING",
5959 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
5960 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5961 _("Could not read hostkeys file!\n"));
5964 /* Check hostkey file size, read entire thing into memory */
5965 fd = GNUNET_DISK_file_open (hostkeys_file,
5966 GNUNET_DISK_OPEN_READ,
5967 GNUNET_DISK_PERM_NONE);
5970 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
5973 GNUNET_free (hostkeys_file);
5974 for (i=0;i<pg->num_hosts;i++)
5976 GNUNET_free (pg->hosts[i].hostname);
5977 GNUNET_free_non_null (pg->hosts[i].username);
5979 GNUNET_free (pg->peers);
5980 GNUNET_free (pg->hosts);
5985 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs,
5989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5990 "Found file size %llu for hostkeys, expect hostkeys to be size %d\n",
5991 fs, HOSTKEYFILESIZE);
5993 if (0 != (fs % HOSTKEYFILESIZE))
5995 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5996 "File size %llu seems incorrect for hostkeys...\n",
6001 total_hostkeys = fs / HOSTKEYFILESIZE;
6002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6003 "Will read %llu hostkeys from file\n",
6005 pg->hostkey_data = GNUNET_malloc_large (fs);
6006 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
6008 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
6010 GNUNET_free(hostkeys_file);
6013 for (off = 0; off < total; off++)
6017 hostname = pg->hosts[off % hostcnt].hostname;
6018 username = pg->hosts[off % hostcnt].username;
6019 sshport = pg->hosts[off % hostcnt].sshport;
6020 pcfg = make_config (cfg, off, &pg->hosts[off % hostcnt].minport,
6021 &upnum, hostname, &fdnum);
6028 pcfg = make_config (cfg, off, &minport, &upnum, hostname, &fdnum);
6033 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6034 _("Could not create configuration for peer number %u on `%s'!\n"),
6036 hostname == NULL ? "localhost" : hostname);
6041 == GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS",
6045 if (hostname != NULL)
6046 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", baseservicehome, hostname, off);
6048 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
6049 GNUNET_free (baseservicehome);
6050 baseservicehome = NULL;
6054 tmpdir = getenv ("TMPDIR");
6055 tmpdir = tmpdir ? tmpdir : "/tmp";
6056 if (hostname != NULL)
6057 GNUNET_asprintf (&newservicehome, "%s/%s/%s/%d/", tmpdir, hostname,
6058 "gnunet-testing-test-test", off);
6060 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", tmpdir,
6061 "gnunet-testing-test-test", off);
6063 GNUNET_CONFIGURATION_set_value_string (pcfg, "PATHS", "SERVICEHOME",
6065 GNUNET_free (newservicehome);
6066 pg->peers[off].cfg = pcfg;
6067 pg->peers[off].pg = pg;
6068 pg->peers[off].internal_context.peer = &pg->peers[off];
6069 pg->peers[off].internal_context.timeout = timeout;
6070 pg->peers[off].internal_context.hostname = hostname;
6071 pg->peers[off].internal_context.username = username;
6072 pg->peers[off].internal_context.sshport = sshport;
6073 if (pg->hostkey_data != NULL)
6074 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off
6076 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
6077 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
6078 pg->peers[off].internal_context.start_cb = cb;
6079 pg->peers[off].internal_context.start_cb_cls = cb_cls;
6080 #if !USE_START_HELPER
6081 GNUNET_SCHEDULER_add_now (&internal_start,
6082 &pg->peers[off].internal_context);
6084 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6086 pg->peers[off].daemon
6087 = GNUNET_TESTING_daemon_start (pcfg,
6093 pg->peers[off].internal_context.hostkey,
6094 &internal_hostkey_callback,
6095 &pg->peers[off].internal_context,
6096 &internal_startup_callback,
6097 &pg->peers[off].internal_context);
6099 * At this point, given that we had a hostkeyfile,
6100 * we can call the hostkey callback!
6101 * But first, we should copy (rsync) all of the configs
6102 * and hostkeys to the remote peers. Then let topology
6103 * creation happen, then call the peer start helper processes,
6104 * then set pg->whatever_phase for each peer and let them
6105 * enter the fsm to get the HELLO's for peers and start connecting.
6110 GNUNET_SCHEDULER_add_now (&internal_start,
6111 &pg->peers[off].internal_context);
6117 #if USE_START_HELPER /* Now the peergroup has been set up, hostkeys and configs written to files. */
6118 if ((pg->hostkey_data != NULL) && (hostcnt > 0))
6120 for (off = 0; off < hostcnt; off++)
6122 /* FIXME: grab baseservicehome! */
6123 GNUNET_asprintf (&newservicehome,
6126 pg->hosts[off].hostname);
6128 if (NULL != username)
6129 GNUNET_asprintf (&arg,
6132 pg->hosts[off].hostname,
6134 pg->hosts[off].hostname);
6136 GNUNET_asprintf (&arg,
6138 pg->hosts[off].hostname,
6140 pg->hosts[off].hostname);
6142 // FIXME: free--- GNUNET_free (baseservicehome);
6144 /* FIXME: Doesn't support ssh_port option! */
6145 proc = GNUNET_OS_start_process (NULL, NULL,
6147 "rsync", "-r", newservicehome, arg, NULL);
6149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6150 "copying directory with command rsync -r %s %s\n",
6151 newservicehome, arg);
6156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6157 _("Could not start `%s' process to copy configuration directory.\n"),
6161 GNUNET_OS_process_wait (proc);
6162 GNUNET_OS_process_close (proc);
6164 /* Now all the configuration files and hostkeys are copied to the remote host. Call the hostkey callback for each peer! */
6165 GNUNET_SCHEDULER_add_now (&call_hostkey_callbacks, pg);
6172 * Get a daemon by number, so callers don't have to do nasty
6173 * offsetting operation.
6175 struct GNUNET_TESTING_Daemon *
6176 GNUNET_TESTING_daemon_get(struct GNUNET_TESTING_PeerGroup *pg,
6177 unsigned int position)
6179 if (position < pg->total)
6180 return pg->peers[position].daemon;
6185 * Get a daemon by peer identity, so callers can
6186 * retrieve the daemon without knowing it's offset.
6188 * @param pg the peer group to retrieve the daemon from
6189 * @param peer_id the peer identity of the daemon to retrieve
6191 * @return the daemon on success, or NULL if no such peer identity is found
6193 struct GNUNET_TESTING_Daemon *
6194 GNUNET_TESTING_daemon_get_by_id(struct GNUNET_TESTING_PeerGroup *pg,
6195 struct GNUNET_PeerIdentity *peer_id)
6199 for (i = 0; i < pg->total; i++)
6201 if (0 == memcmp (&pg->peers[i].daemon->id, peer_id,
6202 sizeof(struct GNUNET_PeerIdentity)))
6203 return pg->peers[i].daemon;
6209 * Prototype of a function that will be called when a
6210 * particular operation was completed the testing library.
6212 * @param cls closure (a struct RestartContext)
6213 * @param id id of the peer that was restarted
6214 * @param cfg handle to the configuration of the peer
6215 * @param d handle to the daemon that was restarted
6216 * @param emsg NULL on success
6219 restart_callback(void *cls, const struct GNUNET_PeerIdentity *id,
6220 const struct GNUNET_CONFIGURATION_Handle *cfg,
6221 struct GNUNET_TESTING_Daemon *d, const char *emsg)
6223 struct RestartContext *restart_context = cls;
6227 restart_context->peers_restarted++;
6231 restart_context->peers_restart_failed++;
6234 if (restart_context->peers_restarted == restart_context->peer_group->total)
6236 restart_context->callback (restart_context->callback_cls, NULL);
6237 GNUNET_free (restart_context);
6239 else if (restart_context->peers_restart_failed
6240 + restart_context->peers_restarted == restart_context->peer_group->total)
6242 restart_context->callback (restart_context->callback_cls,
6243 "Failed to restart peers!");
6244 GNUNET_free (restart_context);
6250 * Callback for informing us about a successful
6251 * or unsuccessful churn stop call.
6253 * @param cls a ChurnContext
6254 * @param emsg NULL on success, non-NULL on failure
6258 churn_stop_callback(void *cls, const char *emsg)
6260 struct ShutdownContext *shutdown_ctx = cls;
6261 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
6262 unsigned int total_left;
6263 char *error_message;
6265 error_message = NULL;
6266 shutdown_ctx->outstanding--;
6270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6271 "Churn stop callback failed with error `%s'\n", emsg);
6272 churn_ctx->num_failed_stop++;
6276 churn_ctx->num_to_stop--;
6280 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6281 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
6283 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
6284 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
6286 if (total_left == 0)
6288 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
6292 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
6293 churn_ctx->num_failed_start,
6294 churn_ctx->num_failed_stop);
6296 churn_ctx->cb (churn_ctx->cb_cls, error_message);
6297 GNUNET_free_non_null (error_message);
6298 GNUNET_free (churn_ctx);
6299 GNUNET_free (shutdown_ctx);
6304 * Count the number of running peers.
6306 * @param pg handle for the peer group
6308 * @return the number of currently running peers in the peer group
6311 GNUNET_TESTING_daemons_running(struct GNUNET_TESTING_PeerGroup *pg)
6314 unsigned int running = 0;
6315 for (i = 0; i < pg->total; i++)
6317 if (pg->peers[i].daemon->running == GNUNET_YES)
6319 GNUNET_assert (running != -1);
6327 * Task to rate limit the number of outstanding peer shutdown
6328 * requests. This is necessary for making sure we don't do
6329 * too many ssh connections at once, but is generally nicer
6330 * to any system as well (graduated task starts, as opposed
6331 * to calling gnunet-arm N times all at once).
6334 schedule_churn_shutdown_task(void *cls,
6335 const struct GNUNET_SCHEDULER_TaskContext *tc)
6337 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6338 struct ShutdownContext *shutdown_ctx;
6339 struct ChurnContext *churn_ctx;
6340 GNUNET_assert (peer_shutdown_ctx != NULL);
6341 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6342 GNUNET_assert (shutdown_ctx != NULL);
6343 churn_ctx = (struct ChurnContext *) shutdown_ctx->cb_cls;
6344 if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
6345 GNUNET_SCHEDULER_add_delayed (
6346 GNUNET_TIME_relative_multiply (
6347 GNUNET_TIME_UNIT_MILLISECONDS,
6349 &schedule_churn_shutdown_task,
6353 shutdown_ctx->outstanding++;
6354 if (churn_ctx->service != NULL)
6355 GNUNET_TESTING_daemon_stop_service (peer_shutdown_ctx->daemon,
6357 shutdown_ctx->timeout, shutdown_ctx->cb,
6360 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6361 shutdown_ctx->timeout, shutdown_ctx->cb,
6362 shutdown_ctx, GNUNET_NO, GNUNET_YES);
6363 GNUNET_free (peer_shutdown_ctx);
6369 * Simulate churn by stopping some peers (and possibly
6370 * re-starting others if churn is called multiple times). This
6371 * function can only be used to create leave-join churn (peers "never"
6372 * leave for good). First "voff" random peers that are currently
6373 * online will be taken offline; then "von" random peers that are then
6374 * offline will be put back online. No notifications will be
6375 * generated for any of these operations except for the callback upon
6378 * @param pg handle for the peer group
6379 * @param service the service to churn off/on, NULL to churn peer
6380 * @param voff number of peers that should go offline
6381 * @param von number of peers that should come back online;
6382 * must be zero on first call (since "testbed_start"
6383 * always starts all of the peers)
6384 * @param timeout how long to wait for operations to finish before
6386 * @param cb function to call at the end
6387 * @param cb_cls closure for cb
6390 GNUNET_TESTING_daemons_churn(struct GNUNET_TESTING_PeerGroup *pg,
6392 unsigned int voff, unsigned int von,
6393 struct GNUNET_TIME_Relative timeout,
6394 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6396 struct ChurnContext *churn_ctx;
6397 struct ShutdownContext *shutdown_ctx;
6398 struct PeerShutdownContext *peer_shutdown_ctx;
6399 struct PeerRestartContext *peer_restart_ctx;
6400 struct ChurnRestartContext *churn_startup_ctx;
6402 unsigned int running;
6403 unsigned int stopped;
6404 unsigned int total_running;
6405 unsigned int total_stopped;
6407 unsigned int *running_arr;
6408 unsigned int *stopped_arr;
6409 unsigned int *running_permute;
6410 unsigned int *stopped_permute;
6413 shutdown_ctx = NULL;
6414 peer_shutdown_ctx = NULL;
6415 peer_restart_ctx = NULL;
6416 churn_startup_ctx = NULL;
6421 if ((von == 0) && (voff == 0)) /* No peers at all? */
6427 for (i = 0; i < pg->total; i++)
6429 if (service == NULL)
6431 if (pg->peers[i].daemon->running == GNUNET_YES)
6433 GNUNET_assert (running != -1);
6438 GNUNET_assert (stopped != -1);
6444 /* FIXME: make churned services a list! */
6445 pos = pg->peers[i].daemon->churned_services;
6446 /* FIXME: while (pos != NULL) */
6449 if (0 == strcasecmp(pos, service))
6451 GNUNET_assert (stopped != -1);
6455 /* FIXME: pos = pos->next; */
6459 GNUNET_assert (running != -1);
6467 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6468 "Trying to stop more peers than are currently running!\n");
6469 cb (cb_cls, "Trying to stop more peers than are currently running!");
6475 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6476 "Trying to start more peers than are currently stopped!\n");
6477 cb (cb_cls, "Trying to start more peers than are currently stopped!");
6481 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6485 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
6489 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
6491 running_permute = NULL;
6492 stopped_permute = NULL;
6495 running_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6498 stopped_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6501 total_running = running;
6502 total_stopped = stopped;
6506 churn_ctx->num_to_start = von;
6507 churn_ctx->num_to_stop = voff;
6509 churn_ctx->cb_cls = cb_cls;
6512 for (i = 0; i < pg->total; i++)
6514 if (service == NULL)
6516 if (pg->peers[i].daemon->running == GNUNET_YES)
6518 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6519 running_arr[running] = i;
6524 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6525 stopped_arr[stopped] = i;
6531 /* FIXME: make churned services a list! */
6532 pos = pg->peers[i].daemon->churned_services;
6533 /* FIXME: while (pos != NULL) */
6536 if (0 == strcasecmp(pos, service))
6538 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6539 stopped_arr[stopped] = i;
6543 /* FIXME: pos = pos->next; */
6547 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6548 running_arr[running] = i;
6554 GNUNET_assert (running >= voff);
6557 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6558 shutdown_ctx->cb = &churn_stop_callback;
6559 shutdown_ctx->cb_cls = churn_ctx;
6560 shutdown_ctx->total_peers = voff;
6561 shutdown_ctx->timeout = timeout;
6564 for (i = 0; i < voff; i++)
6567 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
6568 running_permute[i]);
6570 GNUNET_assert (running_arr != NULL);
6571 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6572 peer_shutdown_ctx->daemon
6573 = pg->peers[running_arr[running_permute[i]]].daemon;
6574 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6575 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
6579 GNUNET_assert (stopped >= von);
6582 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6583 churn_startup_ctx->churn_ctx = churn_ctx;
6584 churn_startup_ctx->timeout = timeout;
6585 churn_startup_ctx->pg = pg;
6587 for (i = 0; i < von; i++)
6590 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
6591 stopped_permute[i]);
6593 GNUNET_assert (stopped_arr != NULL);
6594 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
6595 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
6596 peer_restart_ctx->daemon
6597 = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
6598 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
6601 GNUNET_free_non_null (running_arr);
6602 GNUNET_free_non_null (stopped_arr);
6603 GNUNET_free_non_null (running_permute);
6604 GNUNET_free_non_null (stopped_permute);
6609 * Restart all peers in the given group.
6611 * @param pg the handle to the peer group
6612 * @param callback function to call on completion (or failure)
6613 * @param callback_cls closure for the callback function
6616 GNUNET_TESTING_daemons_restart(struct GNUNET_TESTING_PeerGroup *pg,
6617 GNUNET_TESTING_NotifyCompletion callback,
6620 struct RestartContext *restart_context;
6625 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
6626 restart_context->peer_group = pg;
6627 restart_context->peers_restarted = 0;
6628 restart_context->callback = callback;
6629 restart_context->callback_cls = callback_cls;
6631 for (off = 0; off < pg->total; off++)
6633 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
6634 &restart_callback, restart_context);
6641 * Start or stop an individual peer from the given group.
6643 * @param pg handle to the peer group
6644 * @param offset which peer to start or stop
6645 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
6646 * @param timeout how long to wait for shutdown
6647 * @param cb function to call at the end
6648 * @param cb_cls closure for cb
6651 GNUNET_TESTING_daemons_vary(struct GNUNET_TESTING_PeerGroup *pg,
6652 unsigned int offset, int desired_status,
6653 struct GNUNET_TIME_Relative timeout,
6654 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6656 struct ShutdownContext *shutdown_ctx;
6657 struct ChurnRestartContext *startup_ctx;
6658 struct ChurnContext *churn_ctx;
6660 if (GNUNET_NO == desired_status)
6662 if (NULL != pg->peers[offset].daemon)
6664 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6665 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6666 churn_ctx->num_to_start = 0;
6667 churn_ctx->num_to_stop = 1;
6669 churn_ctx->cb_cls = cb_cls;
6670 shutdown_ctx->cb_cls = churn_ctx;
6671 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon, timeout,
6672 &churn_stop_callback, shutdown_ctx,
6673 GNUNET_NO, GNUNET_YES);
6676 else if (GNUNET_YES == desired_status)
6678 if (NULL == pg->peers[offset].daemon)
6680 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6681 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6682 churn_ctx->num_to_start = 1;
6683 churn_ctx->num_to_stop = 0;
6685 churn_ctx->cb_cls = cb_cls;
6686 startup_ctx->churn_ctx = churn_ctx;
6687 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
6688 timeout, &churn_start_callback,
6698 * Callback for shutting down peers in a peer group.
6700 * @param cls closure (struct ShutdownContext)
6701 * @param emsg NULL on success
6704 internal_shutdown_callback(void *cls, const char *emsg)
6706 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6707 struct ShutdownContext *shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6709 struct OutstandingSSH *ssh_pos;
6711 shutdown_ctx->outstanding--;
6712 if (peer_shutdown_ctx->daemon->hostname != NULL)
6713 decrement_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6718 shutdown_ctx->peers_down++;
6722 shutdown_ctx->peers_failed++;
6725 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down
6726 + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
6728 if (shutdown_ctx->peers_failed > 0)
6729 shutdown_ctx->cb (shutdown_ctx->cb_cls,
6730 "Not all peers successfully shut down!");
6732 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
6734 GNUNET_free (shutdown_ctx->pg->peers);
6735 GNUNET_free_non_null(shutdown_ctx->pg->hostkey_data);
6736 for (off = 0; off < shutdown_ctx->pg->num_hosts; off++)
6738 GNUNET_free (shutdown_ctx->pg->hosts[off].hostname);
6739 GNUNET_free_non_null (shutdown_ctx->pg->hosts[off].username);
6741 GNUNET_free_non_null (shutdown_ctx->pg->hosts);
6742 while (NULL != (ssh_pos = shutdown_ctx->pg->ssh_head))
6744 GNUNET_CONTAINER_DLL_remove(shutdown_ctx->pg->ssh_head, shutdown_ctx->pg->ssh_tail, ssh_pos);
6745 GNUNET_free(ssh_pos);
6747 GNUNET_free (shutdown_ctx->pg);
6748 GNUNET_free (shutdown_ctx);
6750 GNUNET_free(peer_shutdown_ctx);
6755 * Task to rate limit the number of outstanding peer shutdown
6756 * requests. This is necessary for making sure we don't do
6757 * too many ssh connections at once, but is generally nicer
6758 * to any system as well (graduated task starts, as opposed
6759 * to calling gnunet-arm N times all at once).
6762 schedule_shutdown_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6764 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6765 struct ShutdownContext *shutdown_ctx;
6767 GNUNET_assert (peer_shutdown_ctx != NULL);
6768 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6769 GNUNET_assert (shutdown_ctx != NULL);
6771 if ((shutdown_ctx->outstanding < shutdown_ctx->pg->max_concurrent_ssh)
6772 || ((peer_shutdown_ctx->daemon->hostname != NULL)
6773 && (count_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6775 < shutdown_ctx->pg->max_concurrent_ssh)))
6777 if (peer_shutdown_ctx->daemon->hostname != NULL)
6778 increment_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6780 shutdown_ctx->outstanding++;
6781 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6782 shutdown_ctx->timeout,
6783 &internal_shutdown_callback, peer_shutdown_ctx,
6784 shutdown_ctx->delete_files, GNUNET_NO);
6787 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
6789 &schedule_shutdown_task, peer_shutdown_ctx);
6795 * Shutdown all peers started in the given group.
6797 * @param pg handle to the peer group
6798 * @param timeout how long to wait for shutdown
6799 * @param cb callback to notify upon success or failure
6800 * @param cb_cls closure for cb
6803 GNUNET_TESTING_daemons_stop(struct GNUNET_TESTING_PeerGroup *pg,
6804 struct GNUNET_TIME_Relative timeout,
6805 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6808 struct ShutdownContext *shutdown_ctx;
6809 struct PeerShutdownContext *peer_shutdown_ctx;
6811 struct PeerConnection *conn_iter;
6812 struct PeerConnection *temp_conn;
6815 GNUNET_assert (pg->total > 0);
6817 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6818 shutdown_ctx->delete_files = GNUNET_CONFIGURATION_get_value_yesno (pg->cfg,
6821 shutdown_ctx->cb = cb;
6822 shutdown_ctx->cb_cls = cb_cls;
6823 shutdown_ctx->total_peers = pg->total;
6824 shutdown_ctx->timeout = timeout;
6825 shutdown_ctx->pg = pg;
6826 /* shtudown_ctx->outstanding = 0; */
6828 for (off = 0; off < pg->total; off++)
6830 GNUNET_assert (NULL != pg->peers[off].daemon);
6831 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6832 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
6833 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6834 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
6836 if (NULL != pg->peers[off].cfg)
6838 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
6839 pg->peers[off].cfg = NULL;
6842 conn_iter = pg->peers[off].allowed_peers_head;
6843 while (conn_iter != NULL)
6845 temp_conn = conn_iter->next;
6846 GNUNET_free(conn_iter);
6847 conn_iter = temp_conn;
6850 conn_iter = pg->peers[off].connect_peers_head;
6851 while (conn_iter != NULL)
6853 temp_conn = conn_iter->next;
6854 GNUNET_free(conn_iter);
6855 conn_iter = temp_conn;
6858 conn_iter = pg->peers[off].blacklisted_peers_head;
6859 while (conn_iter != NULL)
6861 temp_conn = conn_iter->next;
6862 GNUNET_free(conn_iter);
6863 conn_iter = temp_conn;
6866 conn_iter = pg->peers[off].connect_peers_working_set_head;
6867 while (conn_iter != NULL)
6869 temp_conn = conn_iter->next;
6870 GNUNET_free(conn_iter);
6871 conn_iter = temp_conn;
6874 if (pg->peers[off].allowed_peers != NULL)
6875 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
6876 if (pg->peers[off].connect_peers != NULL)
6877 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
6878 if (pg->peers[off].blacklisted_peers != NULL)
6879 GNUNET_CONTAINER_multihashmap_destroy (pg->
6880 peers[off].blacklisted_peers);
6885 /* end of testing_group.c */