2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing_group.c
23 * @brief convenience API for writing testcases for GNUnet
24 * @author Nathan Evans
25 * @author Christian Grothoff
29 #include "gnunet_arm_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_core_service.h"
33 #define VERBOSE_TESTING GNUNET_NO
35 #define VERBOSE_TOPOLOGY GNUNET_NO
37 #define DEBUG_CHURN GNUNET_NO
41 #define USE_SEND_HELLOS GNUNET_NO
43 #define TOPOLOGY_HACK GNUNET_YES
46 * Lowest port used for GNUnet testing. Should be high enough to not
47 * conflict with other applications running on the hosts but be low
48 * enough to not conflict with client-ports (typically starting around
51 #define LOW_PORT 12000
54 * Highest port used for GNUnet testing. Should be low enough to not
55 * conflict with the port range for "local" ports (client apps; see
56 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
58 #define HIGH_PORT 56000
60 /* Maximum time to delay connect attempt */
61 #define MAX_CONNECT_DELAY 300
64 * Which list of peers do we need to modify?
68 /** Modify allowed peers */
71 /** Modify connect peers */
74 /** Modify blacklist peers */
77 /** Modify workingset peers */
82 * Prototype of a function called whenever two peers would be connected
83 * in a certain topology.
86 (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup * pg,
87 unsigned int first, unsigned int second,
88 enum PeerLists list, unsigned int check);
91 * Context for handling churning a peer group
96 * The peergroup we are dealing with.
98 struct GNUNET_TESTING_PeerGroup *pg;
101 * Callback used to notify of churning finished
103 GNUNET_TESTING_NotifyCompletion cb;
106 * Closure for callback
111 * Number of peers that still need to be started
113 unsigned int num_to_start;
116 * Number of peers that still need to be stopped
118 unsigned int num_to_stop;
121 * Number of peers that failed to start
123 unsigned int num_failed_start;
126 * Number of peers that failed to stop
128 unsigned int num_failed_stop;
131 struct RestartContext
134 * The group of peers being restarted
136 struct GNUNET_TESTING_PeerGroup *peer_group;
139 * How many peers have been restarted thus far
141 unsigned int peers_restarted;
144 * How many peers got an error when restarting
146 unsigned int peers_restart_failed;
149 * The function to call once all peers have been restarted
151 GNUNET_TESTING_NotifyCompletion callback;
154 * Closure for callback function
160 struct SendHelloContext
163 * Global handle to the peer group.
165 struct GNUNET_TESTING_PeerGroup *pg;
168 * The data about this specific peer.
170 struct PeerData *peer;
173 * The next HELLO that needs sent to this peer.
175 struct PeerConnection *peer_pos;
178 * Are we connected to CORE yet?
180 unsigned int core_ready;
183 * How many attempts should we make for failed connections?
185 unsigned int connect_attempts;
188 * Task for scheduling core connect requests to be sent.
190 GNUNET_SCHEDULER_TaskIdentifier core_connect_task;
193 struct ShutdownContext
195 struct GNUNET_TESTING_PeerGroup *pg;
197 * Total peers to wait for
199 unsigned int total_peers;
202 * Number of peers successfully shut down
204 unsigned int peers_down;
207 * Number of peers failed to shut down
209 unsigned int peers_failed;
212 * Number of peers we have started shutting
213 * down. If too many, wait on them.
215 unsigned int outstanding;
218 * Timeout for shutdown.
220 struct GNUNET_TIME_Relative timeout;
223 * Callback to call when all peers either
224 * shutdown or failed to shutdown
226 GNUNET_TESTING_NotifyCompletion cb;
235 * Individual shutdown context for a particular peer.
237 struct PeerShutdownContext
240 * Pointer to the high level shutdown context.
242 struct ShutdownContext *shutdown_ctx;
245 * The daemon handle for the peer to shut down.
247 struct GNUNET_TESTING_Daemon *daemon;
251 * Individual shutdown context for a particular peer.
253 struct PeerRestartContext
256 * Pointer to the high level restart context.
258 struct ChurnRestartContext *churn_restart_ctx;
261 * The daemon handle for the peer to shut down.
263 struct GNUNET_TESTING_Daemon *daemon;
266 struct CreateTopologyContext
270 * Function to call with number of connections
272 GNUNET_TESTING_NotifyConnections cont;
275 * Closure for connection notification
282 /** Waiting to read number of peers */
285 /** Should find next peer index */
288 /** Should find colon */
291 /** Should read other peer index, space, or endline */
296 struct PeerConnection
301 struct PeerConnection *prev;
306 struct PeerConnection *next;
309 * Index of daemon in pg->peers
316 struct InternalStartContext
319 * Pointer to peerdata
321 struct PeerData *peer;
324 * Timeout for peer startup
326 struct GNUNET_TIME_Relative timeout;
329 * Client callback for hostkey notification
331 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
334 * Closure for hostkey_callback
339 * Client callback for peer start notification
341 GNUNET_TESTING_NotifyDaemonRunning start_cb;
349 * Hostname, where to start the peer
351 const char *hostname;
354 * Username to use when connecting to the
357 const char *username;
360 * Pointer to starting memory location of a hostkey
365 * Port to use for ssh.
371 struct ChurnRestartContext
374 * PeerGroup that we are working with.
376 struct GNUNET_TESTING_PeerGroup *pg;
379 * Number of restarts currently in flight.
381 unsigned int outstanding;
384 * Handle to the underlying churn context.
386 struct ChurnContext *churn_ctx;
389 * How long to allow the operation to take.
391 struct GNUNET_TIME_Relative timeout;
394 struct OutstandingSSH
396 struct OutstandingSSH *next;
398 struct OutstandingSSH *prev;
401 * Number of current ssh connections.
403 uint32_t outstanding;
406 * The hostname of this peer.
408 const char *hostname;
412 * Data we keep per peer.
417 * (Initial) configuration of the host.
418 * (initial because clients could change
419 * it and we would not know about those
422 struct GNUNET_CONFIGURATION_Handle *cfg;
425 * Handle for controlling the daemon.
427 struct GNUNET_TESTING_Daemon *daemon;
430 * The peergroup this peer belongs to.
432 struct GNUNET_TESTING_PeerGroup *pg;
436 * Linked list of allowed peer connections.
438 struct PeerConnection *allowed_peers_head;
441 * Linked list of allowed peer connections.
443 struct PeerConnection *allowed_peers_tail;
446 * Linked list of blacklisted peer connections.
448 struct PeerConnection *blacklisted_peers_head;
451 * Linked list of blacklisted peer connections.
453 struct PeerConnection *blacklisted_peers_tail;
456 * Linked list of connect peer connections.
458 struct PeerConnection *connect_peers_head;
461 * Linked list of connect peer connections.
463 struct PeerConnection *connect_peers_tail;
466 * Linked list of connect peer connections.
468 struct PeerConnection *connect_peers_working_set_head;
471 * Linked list of connect peer connections.
473 struct PeerConnection *connect_peers_working_set_tail;
477 * Hash map of allowed peer connections (F2F created topology)
479 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
482 * Hash map of blacklisted peers
484 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
487 * Hash map of peer connections
489 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
492 * Temporary hash map of peer connections
494 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
498 * Temporary variable for topology creation, should be reset before
499 * creating any topology so the count is valid once finished.
504 * Context to keep track of peers being started, to
505 * stagger hostkey generation and peer startup.
507 struct InternalStartContext internal_context;
511 * Linked list of per-host data.
521 * SSH username to use when connecting to this host.
526 * SSH port to use when connecting to this host.
531 * Lowest port that we have not yet used
537 struct TopologyIterateContext
540 * The peergroup we are working with.
542 struct GNUNET_TESTING_PeerGroup *pg;
545 * Callback for notifying of two connected peers.
547 GNUNET_TESTING_NotifyTopology topology_cb;
550 * Closure for topology_cb
555 * Number of peers currently connected to.
557 unsigned int connected;
560 * Number of peers we have finished iterating.
562 unsigned int completed;
565 * Number of peers total.
570 struct StatsIterateContext
573 * The peergroup that we are dealing with.
575 struct GNUNET_TESTING_PeerGroup *pg;
578 * Continuation to call once all stats information has been retrieved.
580 GNUNET_STATISTICS_Callback cont;
583 * Proc function to call on each value received.
585 GNUNET_TESTING_STATISTICS_Iterator proc;
588 * Closure for topology_cb
593 * Number of peers currently connected to.
595 unsigned int connected;
598 * Number of peers we have finished iterating.
600 unsigned int completed;
603 * Number of peers total.
611 struct GNUNET_TESTING_Daemon *daemon;
614 struct StatsCoreContext
617 struct GNUNET_TESTING_Daemon *daemon;
619 * Handle to the statistics service.
621 struct GNUNET_STATISTICS_Handle *stats_handle;
624 * Handle for getting statistics.
626 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
629 struct ConnectTopologyContext
632 * How many connections are left to create.
634 unsigned int remaining_connections;
637 * Handle to group of peers.
639 struct GNUNET_TESTING_PeerGroup *pg;
642 * How long to try this connection before timing out.
644 struct GNUNET_TIME_Relative connect_timeout;
647 * How many times to retry connecting the two peers.
649 unsigned int connect_attempts;
652 * Temp value set for each iteration.
654 //struct PeerData *first;
657 * Notification that all peers are connected.
659 GNUNET_TESTING_NotifyCompletion notify_connections_done;
662 * Closure for notify.
668 * Handle to a group of GNUnet peers.
670 struct GNUNET_TESTING_PeerGroup
673 * Configuration template.
675 const struct GNUNET_CONFIGURATION_Handle *cfg;
678 * Function to call on each started daemon.
680 //GNUNET_TESTING_NotifyDaemonRunning cb;
688 * Function to call on each topology connection created
690 GNUNET_TESTING_NotifyConnection notify_connection;
693 * Callback for notify_connection
695 void *notify_connection_cls;
698 * Array of information about hosts.
700 struct HostData *hosts;
703 * Number of hosts (size of HostData)
705 unsigned int num_hosts;
708 * Array of "total" peers.
710 struct PeerData *peers;
713 * Number of peers in this group.
718 * At what time should we fail the peer startup process?
720 struct GNUNET_TIME_Absolute max_timeout;
723 * How many peers are being started right now?
725 unsigned int starting;
728 * How many peers have already been started?
730 unsigned int started;
733 * Number of possible connections to peers
736 unsigned int max_outstanding_connections;
739 * Number of ssh connections to peers (max).
741 unsigned int max_concurrent_ssh;
744 * Number of connects we are waiting on, allows us to rate limit
747 unsigned int outstanding_connects;
750 * Number of HELLOs we have yet to send.
752 unsigned int remaining_hellos;
755 * How many connects have already been scheduled?
757 unsigned int total_connects_scheduled;
760 * Hostkeys loaded from a file.
765 * Head of DLL to keep track of the number of outstanding
766 * ssh connections per peer.
768 struct OutstandingSSH *ssh_head;
771 * Tail of DLL to keep track of the number of outstanding
772 * ssh connections per peer.
774 struct OutstandingSSH *ssh_tail;
777 * Stop scheduling peers connecting.
779 unsigned int stop_connects;
782 * Connection context for peer group.
784 struct ConnectTopologyContext ct_ctx;
790 * The altered configuration.
792 struct GNUNET_CONFIGURATION_Handle *ret;
795 * The original configuration to alter.
797 const struct GNUNET_CONFIGURATION_Handle *orig;
800 * The hostname that this peer will run on.
802 const char *hostname;
805 * The next possible port to assign.
810 * Unique number for unix domain sockets.
815 * Unique number for this peer/host to offset
816 * things that are grouped by host.
821 struct ConnectContext
824 * Index of peer to connect second to.
826 uint32_t first_index;
829 * Index of peer to connect first to.
831 uint32_t second_index;
834 * Higher level topology connection context.
836 struct ConnectTopologyContext *ct_ctx;
839 * Whether this connection has been accounted for in the schedule_connect call.
844 struct UnblacklistContext
849 struct GNUNET_TESTING_PeerGroup *pg;
852 * uid of the first peer
862 struct GNUNET_TESTING_PeerGroup *pg;
865 * uid of the first peer
870 * Peer data for first peer.
872 struct PeerData *first;
875 * Random percentage to use
880 struct MinimumContext
885 struct GNUNET_TESTING_PeerGroup *pg;
888 * uid of the first peer
893 * Peer data for first peer.
895 struct PeerData *first;
898 * Number of conns per peer
900 unsigned int num_to_add;
903 * Permuted array of all possible connections. Only add the Nth
904 * peer if it's in the Nth position.
906 unsigned int *pg_array;
909 * What number is the current element we are iterating over?
911 unsigned int current;
919 struct GNUNET_TESTING_PeerGroup *pg;
922 * uid of the first peer
927 * uid of the second peer
932 * Peer data for first peer.
934 struct PeerData *first;
937 * Which peer has been chosen as the one to add?
942 * What number is the current element we are iterating over?
944 unsigned int current;
948 * Simple struct to keep track of progress, and print a
949 * nice little percentage meter for long running tasks.
959 unsigned int completed;
963 char *startup_string;
968 * Convert unique ID to hash code.
970 * @param uid unique ID to convert
971 * @param hash set to uid (extended with zeros)
974 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
976 memset (hash, 0, sizeof (GNUNET_HashCode));
977 *((uint32_t *) hash) = uid;
981 * Convert hash code to unique ID.
983 * @param uid unique ID to convert
984 * @param hash set to uid (extended with zeros)
987 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
989 memcpy (uid, hash, sizeof (uint32_t));
994 static struct GNUNET_CORE_MessageHandler no_handlers[] =
1000 * Create a meter to keep track of the progress of some task.
1002 * @param total the total number of items to complete
1003 * @param start_string a string to prefix the meter with (if printing)
1004 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
1007 * @return the progress meter
1009 static struct ProgressMeter *
1010 create_meter(unsigned int total, char * start_string, int print)
1012 struct ProgressMeter *ret;
1013 ret = GNUNET_malloc(sizeof(struct ProgressMeter));
1016 ret->modnum = total / 4;
1017 if (ret->modnum == 0) /* Divide by zero check */
1019 ret->dotnum = (total / 50) + 1;
1020 if (start_string != NULL)
1021 ret->startup_string = GNUNET_strdup(start_string);
1023 ret->startup_string = GNUNET_strdup("");
1029 * Update progress meter (increment by one).
1031 * @param meter the meter to update and print info for
1033 * @return GNUNET_YES if called the total requested,
1034 * GNUNET_NO if more items expected
1037 update_meter(struct ProgressMeter *meter)
1039 if (meter->print == GNUNET_YES)
1041 if (meter->completed % meter->modnum == 0)
1043 if (meter->completed == 0)
1045 fprintf (stdout, "%sProgress: [0%%", meter->startup_string);
1048 fprintf (stdout, "%d%%", (int) (((float) meter->completed
1049 / meter->total) * 100));
1051 else if (meter->completed % meter->dotnum == 0)
1052 fprintf (stdout, ".");
1054 if (meter->completed + 1 == meter->total)
1055 fprintf (stdout, "%d%%]\n", 100);
1060 if (meter->completed == meter->total)
1062 if (meter->completed > meter->total)
1063 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
1068 * Reset progress meter.
1070 * @param meter the meter to reset
1072 * @return GNUNET_YES if meter reset,
1073 * GNUNET_SYSERR on error
1076 reset_meter(struct ProgressMeter *meter)
1079 return GNUNET_SYSERR;
1081 meter->completed = 0;
1086 * Release resources for meter
1088 * @param meter the meter to free
1091 free_meter(struct ProgressMeter *meter)
1093 GNUNET_free_non_null (meter->startup_string);
1094 GNUNET_free (meter);
1098 * Get a topology from a string input.
1100 * @param topology where to write the retrieved topology
1101 * @param topology_string The string to attempt to
1102 * get a configuration value from
1103 * @return GNUNET_YES if topology string matched a
1104 * known topology, GNUNET_NO if not
1107 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology,
1108 const char *topology_string)
1111 * Strings representing topologies in enum
1113 static const char *topology_strings[] =
1116 * A clique (everyone connected to everyone else).
1121 * Small-world network (2d torus plus random links).
1126 * Small-world network (ring plus random links).
1146 * Certain percentage of peers are unable to communicate directly
1147 * replicating NAT conditions
1152 * Scale free topology.
1157 * Straight line topology.
1162 * All peers are disconnected.
1167 * Read the topology from a file.
1174 if (topology_string == NULL)
1176 while (topology_strings[curr] != NULL)
1178 if (strcasecmp (topology_strings[curr], topology_string) == 0)
1185 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
1190 * Get connect topology option from string input.
1192 * @param topology_option where to write the retrieved topology
1193 * @param topology_string The string to attempt to
1194 * get a configuration value from
1195 * @return GNUNET_YES if string matched a known
1196 * topology option, GNUNET_NO if not
1199 GNUNET_TESTING_topology_option_get(
1200 enum GNUNET_TESTING_TopologyOption *topology_option,
1201 const char *topology_string)
1204 * Options for connecting a topology as strings.
1206 static const char *topology_option_strings[] =
1209 * Try to connect all peers specified in the topology.
1214 * Choose a random subset of connections to create.
1216 "CONNECT_RANDOM_SUBSET",
1219 * Create at least X connections for each peer.
1224 * Using a depth first search, create one connection
1225 * per peer. If any are missed (graph disconnected)
1226 * start over at those peers until all have at least one
1232 * Find the N closest peers to each allowed peer in the
1233 * topology and make sure a connection to those peers
1234 * exists in the connect topology.
1239 * No options specified.
1246 if (topology_string == NULL)
1248 while (NULL != topology_option_strings[curr])
1250 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1252 *topology_option = curr;
1257 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1262 * Function to iterate over options. Copies
1263 * the options to the target configuration,
1264 * updating PORT values as needed.
1266 * @param cls closure
1267 * @param section name of the section
1268 * @param option name of the option
1269 * @param value value of the option
1272 update_config(void *cls, const char *section, const char *option,
1275 struct UpdateContext *ctx = cls;
1279 char *single_variable;
1280 char *per_host_variable;
1281 unsigned long long num_per_host;
1283 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1284 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1286 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1288 if ((ival != 0) && (GNUNET_YES
1289 != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1292 GNUNET_snprintf (cval, sizeof(cval), "%u", ctx->nport++);
1295 else if ((ival != 0) && (GNUNET_YES
1296 == GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1298 && GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1302 GNUNET_snprintf (cval, sizeof(cval), "%u", ival + ctx->fdnum
1308 if (0 == strcmp (option, "UNIXPATH"))
1310 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig,
1314 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1315 section, ctx->upnum++);
1318 else if ((GNUNET_YES
1319 == GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1322 && (num_per_host > 0))
1325 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1326 section, ctx->fdnum % num_per_host);
1331 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1333 value = ctx->hostname;
1335 GNUNET_free (single_variable);
1336 GNUNET_free (per_host_variable);
1337 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1341 * Create a new configuration using the given configuration
1342 * as a template; however, each PORT in the existing cfg
1343 * must be renumbered by incrementing "*port". If we run
1344 * out of "*port" numbers, return NULL.
1346 * @param cfg template configuration
1347 * @param off the current peer offset
1348 * @param port port numbers to use, update to reflect
1349 * port numbers that were used
1350 * @param upnum number to make unix domain socket names unique
1351 * @param hostname hostname of the controlling host, to allow control connections from
1352 * @param fdnum number used to offset the unix domain socket for grouped processes
1353 * (such as statistics or peerinfo, which can be shared among others)
1355 * @return new configuration, NULL on error
1357 static struct GNUNET_CONFIGURATION_Handle *
1358 make_config(const struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t off,
1359 uint16_t * port, uint32_t * upnum, const char *hostname,
1362 struct UpdateContext uc;
1365 char *allowed_hosts;
1371 uc.ret = GNUNET_CONFIGURATION_create ();
1372 uc.hostname = hostname;
1375 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1376 if (uc.nport >= HIGH_PORT)
1379 GNUNET_CONFIGURATION_destroy (uc.ret);
1383 if (GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "control_host",
1384 &control_host) == GNUNET_OK)
1386 if (hostname != NULL)
1387 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1390 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1392 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1394 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1395 "ACCEPT_FROM", allowed_hosts);
1396 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1398 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1399 "ACCEPT_FROM", allowed_hosts);
1401 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH", "");
1402 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH",
1404 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH", "");
1405 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1408 GNUNET_free_non_null (control_host);
1409 GNUNET_free (allowed_hosts);
1412 /* arm needs to know to allow connections from the host on which it is running,
1413 * otherwise gnunet-arm is unable to connect to it in some instances */
1414 if (hostname != NULL)
1416 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1417 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1419 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1421 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1423 GNUNET_free (allowed_hosts);
1427 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1429 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1433 *port = (uint16_t) uc.nport;
1441 * Remove entries from the peer connection list
1443 * @param pg the peer group we are working with
1444 * @param first index of the first peer
1445 * @param second index of the second peer
1446 * @param list the peer list to use
1447 * @param check UNUSED
1449 * @return the number of connections added (can be 0, 1 or 2)
1453 remove_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1454 unsigned int second, enum PeerLists list, unsigned int check)
1458 struct PeerConnection **first_list;
1459 struct PeerConnection **second_list;
1460 struct PeerConnection *first_iter;
1461 struct PeerConnection *second_iter;
1462 struct PeerConnection **first_tail;
1463 struct PeerConnection **second_tail;
1466 GNUNET_HashCode hash_first;
1467 GNUNET_HashCode hash_second;
1469 hash_from_uid (first, &hash_first);
1470 hash_from_uid (second, &hash_second);
1478 first_list = &pg->peers[first].allowed_peers_head;
1479 second_list = &pg->peers[second].allowed_peers_head;
1480 first_tail = &pg->peers[first].allowed_peers_tail;
1481 second_tail = &pg->peers[second].allowed_peers_tail;
1484 first_list = &pg->peers[first].connect_peers_head;
1485 second_list = &pg->peers[second].connect_peers_head;
1486 first_tail = &pg->peers[first].connect_peers_tail;
1487 second_tail = &pg->peers[second].connect_peers_tail;
1490 first_list = &pg->peers[first].blacklisted_peers_head;
1491 second_list = &pg->peers[second].blacklisted_peers_head;
1492 first_tail = &pg->peers[first].blacklisted_peers_tail;
1493 second_tail = &pg->peers[second].blacklisted_peers_tail;
1496 first_list = &pg->peers[first].connect_peers_working_set_head;
1497 second_list = &pg->peers[second].connect_peers_working_set_head;
1498 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1499 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1506 first_iter = *first_list;
1507 while (first_iter != NULL)
1509 if (first_iter->index == second)
1511 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1512 GNUNET_free(first_iter);
1516 first_iter = first_iter->next;
1519 second_iter = *second_list;
1520 while (second_iter != NULL)
1522 if (second_iter->index == first)
1524 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1525 GNUNET_free(second_iter);
1529 second_iter = second_iter->next;
1533 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1536 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1541 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1544 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1553 * Add entries to the some list
1555 * @param pg the peer group we are working with
1556 * @param first index of the first peer
1557 * @param second index of the second peer
1558 * @param list the list type that we should modify
1559 * @param check GNUNET_YES to check lists before adding
1560 * GNUNET_NO to force add
1562 * @return the number of connections added (can be 0, 1 or 2)
1566 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1567 unsigned int second, enum PeerLists list, unsigned int check)
1573 struct PeerConnection **first_list;
1574 struct PeerConnection **second_list;
1575 struct PeerConnection *first_iter;
1576 struct PeerConnection *second_iter;
1577 struct PeerConnection *new_first;
1578 struct PeerConnection *new_second;
1579 struct PeerConnection **first_tail;
1580 struct PeerConnection **second_tail;
1582 GNUNET_HashCode hash_first;
1583 GNUNET_HashCode hash_second;
1585 hash_from_uid (first, &hash_first);
1586 hash_from_uid (second, &hash_second);
1593 first_list = &pg->peers[first].allowed_peers_head;
1594 second_list = &pg->peers[second].allowed_peers_head;
1595 first_tail = &pg->peers[first].allowed_peers_tail;
1596 second_tail = &pg->peers[second].allowed_peers_tail;
1599 first_list = &pg->peers[first].connect_peers_head;
1600 second_list = &pg->peers[second].connect_peers_head;
1601 first_tail = &pg->peers[first].connect_peers_tail;
1602 second_tail = &pg->peers[second].connect_peers_tail;
1605 first_list = &pg->peers[first].blacklisted_peers_head;
1606 second_list = &pg->peers[second].blacklisted_peers_head;
1607 first_tail = &pg->peers[first].blacklisted_peers_tail;
1608 second_tail = &pg->peers[second].blacklisted_peers_tail;
1611 first_list = &pg->peers[first].connect_peers_working_set_head;
1612 second_list = &pg->peers[second].connect_peers_working_set_head;
1613 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1614 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1621 add_first = GNUNET_YES;
1622 add_second = GNUNET_YES;
1624 if (check == GNUNET_YES)
1626 first_iter = *first_list;
1627 while (first_iter != NULL)
1629 if (first_iter->index == second)
1631 add_first = GNUNET_NO;
1634 first_iter = first_iter->next;
1637 second_iter = *second_list;
1638 while (second_iter != NULL)
1640 if (second_iter->index == first)
1642 add_second = GNUNET_NO;
1645 second_iter = second_iter->next;
1650 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1653 add_first = GNUNET_YES;
1657 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1660 add_second = GNUNET_YES;
1668 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1669 new_first->index = second;
1670 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1672 GNUNET_assert (GNUNET_OK ==
1673 GNUNET_CONTAINER_multihashmap_put (pg->
1675 [first].blacklisted_peers,
1678 peers[second].daemon,
1679 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1681 pg->peers[first].num_connections++;
1688 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1689 new_second->index = first;
1690 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1692 GNUNET_assert (GNUNET_OK ==
1693 GNUNET_CONTAINER_multihashmap_put (pg->
1695 [second].blacklisted_peers,
1698 peers[first].daemon,
1699 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1701 pg->peers[second].num_connections++;
1709 * Scale free network construction as described in:
1711 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1713 * Start with a network of "one" peer, then progressively add
1714 * peers up to the total number. At each step, iterate over
1715 * all possible peers and connect new peer based on number of
1716 * existing connections of the target peer.
1718 * @param pg the peer group we are dealing with
1719 * @param proc the connection processor to use
1720 * @param list the peer list to use
1722 * @return the number of connections created
1725 create_scale_free(struct GNUNET_TESTING_PeerGroup *pg,
1726 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1729 unsigned int total_connections;
1730 unsigned int outer_count;
1732 unsigned int previous_total_connections;
1736 GNUNET_assert (pg->total > 1);
1738 /* Add a connection between the first two nodes */
1739 total_connections = proc (pg, 0, 1, list, GNUNET_YES);
1741 for (outer_count = 1; outer_count < pg->total; outer_count++)
1743 previous_total_connections = total_connections;
1744 for (i = 0; i < outer_count; i++)
1746 probability = pg->peers[i].num_connections
1747 / (double) previous_total_connections;
1749 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1751 / ((double) UINT64_MAX);
1753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1754 "Considering connecting peer %d to peer %d\n",
1757 if (random < probability)
1760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1761 "Connecting peer %d to peer %d\n", outer_count, i);
1763 total_connections += proc (pg, outer_count, i, list, GNUNET_YES);
1768 return total_connections;
1772 * Create a topology given a peer group (set of running peers)
1773 * and a connection processor. Creates a small world topology
1774 * according to the rewired ring construction. The basic
1775 * behavior is that a ring topology is created, but with some
1776 * probability instead of connecting a peer to the next
1777 * neighbor in the ring a connection will be created to a peer
1778 * selected uniformly at random. We use the TESTING
1779 * PERCENTAGE option to specify what number of
1780 * connections each peer should have. Default is 2,
1781 * which makes the ring, any given number is multiplied by
1782 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1783 * each peer have on average 2logn connections. The additional
1784 * connections are made at increasing distance around the ring
1785 * from the original peer, or to random peers based on the re-
1786 * wiring probability. The TESTING
1787 * PROBABILITY option is used as the probability that a given
1788 * connection is rewired.
1790 * @param pg the peergroup to create the topology on
1791 * @param proc the connection processor to call to actually set
1792 * up connections between two peers
1793 * @param list the peer list to use
1795 * @return the number of connections that were set up
1799 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg,
1800 GNUNET_TESTING_ConnectionProcessor proc,
1801 enum PeerLists list)
1805 unsigned int natLog;
1806 unsigned int randomPeer;
1807 double random, logNModifier, probability;
1808 unsigned int smallWorldConnections;
1813 unsigned int useAnd;
1814 int connect_attempts;
1816 logNModifier = 0.5; /* FIXME: default value? */
1817 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1821 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1823 GNUNET_ERROR_TYPE_WARNING,
1825 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1826 p_string, "LOGNMODIFIER", "TESTING");
1827 GNUNET_free (p_string);
1829 probability = 0.5; /* FIXME: default percentage? */
1830 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1834 if (sscanf (p_string, "%lf", &probability) != 1)
1836 GNUNET_ERROR_TYPE_WARNING,
1838 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1839 p_string, "PERCENTAGE", "TESTING");
1840 GNUNET_free (p_string);
1842 natLog = log (pg->total);
1843 connsPerPeer = ceil (natLog * logNModifier);
1845 if (connsPerPeer % 2 == 1)
1848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."),
1851 smallWorldConnections = 0;
1852 connect_attempts = 0;
1853 for (i = 0; i < pg->total; i++)
1856 max = i + connsPerPeer / 2;
1857 min = i - connsPerPeer / 2;
1859 if (max > pg->total - 1)
1861 max = max - pg->total;
1867 min = pg->total - 1 + min;
1871 for (j = 0; j < connsPerPeer / 2; j++)
1874 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1876 / ((double) UINT64_MAX));
1877 if (random < probability)
1879 /* Connect to uniformly selected random peer */
1881 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1883 while ((((randomPeer < max) && (randomPeer > min)) && (useAnd
1884 == 0)) || (((randomPeer > min) || (randomPeer < max))
1888 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1891 smallWorldConnections += proc (pg, i, randomPeer, list,
1896 nodeToConnect = i + j + 1;
1897 if (nodeToConnect > pg->total - 1)
1899 nodeToConnect = nodeToConnect - pg->total;
1901 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
1907 connect_attempts += smallWorldConnections;
1909 return connect_attempts;
1913 * Create a topology given a peer group (set of running peers)
1914 * and a connection processor.
1916 * @param pg the peergroup to create the topology on
1917 * @param proc the connection processor to call to actually set
1918 * up connections between two peers
1919 * @param list the peer list to use
1921 * @return the number of connections that were set up
1925 create_nated_internet(struct GNUNET_TESTING_PeerGroup *pg,
1926 GNUNET_TESTING_ConnectionProcessor proc,
1927 enum PeerLists list)
1929 unsigned int outer_count, inner_count;
1930 unsigned int cutoff;
1931 int connect_attempts;
1932 double nat_percentage;
1935 nat_percentage = 0.6; /* FIXME: default percentage? */
1936 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1940 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1942 GNUNET_ERROR_TYPE_WARNING,
1944 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1945 p_string, "PERCENTAGE", "TESTING");
1946 GNUNET_free (p_string);
1949 cutoff = (unsigned int) (nat_percentage * pg->total);
1950 connect_attempts = 0;
1951 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1953 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1955 if ((outer_count > cutoff) || (inner_count > cutoff))
1958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1959 "Connecting peer %d to peer %d\n",
1960 outer_count, inner_count);
1962 connect_attempts += proc (pg, outer_count, inner_count, list,
1967 return connect_attempts;
1972 * Create a topology given a peer group (set of running peers)
1973 * and a connection processor.
1975 * @param pg the peergroup to create the topology on
1976 * @param proc the connection processor to call to actually set
1977 * up connections between two peers
1978 * @param list the peer list to use
1980 * @return the number of connections that were set up
1984 create_nated_internet_copy(struct GNUNET_TESTING_PeerGroup *pg,
1985 GNUNET_TESTING_ConnectionProcessor proc,
1986 enum PeerLists list)
1988 unsigned int outer_count, inner_count;
1989 unsigned int cutoff;
1990 int connect_attempts;
1991 double nat_percentage;
1994 struct ProgressMeter *conn_meter;
1996 nat_percentage = 0.6; /* FIXME: default percentage? */
1997 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2001 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
2003 GNUNET_ERROR_TYPE_WARNING,
2005 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2006 p_string, "PERCENTAGE", "TESTING");
2007 GNUNET_free (p_string);
2010 cutoff = (unsigned int) (nat_percentage * pg->total);
2012 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2014 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2016 if ((outer_count > cutoff) || (inner_count > cutoff))
2022 conn_meter = create_meter (count, "NAT COPY", GNUNET_YES);
2023 connect_attempts = 0;
2024 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2026 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2028 if ((outer_count > cutoff) || (inner_count > cutoff))
2031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2032 "Connecting peer %d to peer %d\n",
2033 outer_count, inner_count);
2035 connect_attempts += proc (pg, outer_count, inner_count, list,
2037 add_connections (pg, outer_count, inner_count, ALLOWED, GNUNET_NO);
2038 update_meter (conn_meter);
2042 free_meter (conn_meter);
2044 return connect_attempts;
2049 * Create a topology given a peer group (set of running peers)
2050 * and a connection processor.
2052 * @param pg the peergroup to create the topology on
2053 * @param proc the connection processor to call to actually set
2054 * up connections between two peers
2055 * @param list the peer list to use
2057 * @return the number of connections that were set up
2061 create_small_world(struct GNUNET_TESTING_PeerGroup *pg,
2062 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2064 unsigned int i, j, k;
2065 unsigned int square;
2068 unsigned int toggle = 1;
2069 unsigned int nodeToConnect;
2070 unsigned int natLog;
2071 unsigned int node1Row;
2072 unsigned int node1Col;
2073 unsigned int node2Row;
2074 unsigned int node2Col;
2075 unsigned int distance;
2076 double probability, random, percentage;
2077 unsigned int smallWorldConnections;
2078 unsigned int small_world_it;
2080 int connect_attempts;
2081 square = floor (sqrt (pg->total));
2085 percentage = 0.5; /* FIXME: default percentage? */
2086 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2090 if (sscanf (p_string, "%lf", &percentage) != 1)
2092 GNUNET_ERROR_TYPE_WARNING,
2094 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2095 p_string, "PERCENTAGE", "TESTING");
2096 GNUNET_free (p_string);
2098 if (percentage < 0.0)
2101 GNUNET_ERROR_TYPE_WARNING,
2103 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
2104 "PERCENTAGE", "TESTING", percentage);
2107 probability = 0.5; /* FIXME: default percentage? */
2108 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2112 if (sscanf (p_string, "%lf", &probability) != 1)
2114 GNUNET_ERROR_TYPE_WARNING,
2116 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2117 p_string, "PROBABILITY", "TESTING");
2118 GNUNET_free (p_string);
2120 if (square * square != pg->total)
2122 while (rows * cols < pg->total)
2124 if (toggle % 2 == 0)
2133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2135 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2139 connect_attempts = 0;
2140 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2141 * to the node to its right and above. Once this is over, we'll have our torus!
2142 * Special case for the last node (if the rows and columns are not equal), connect
2143 * to the first in the row to maintain topology.
2145 for (i = 0; i < pg->total; i++)
2147 /* First connect to the node to the right */
2148 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2149 nodeToConnect = i + 1;
2150 else if (i + 1 == pg->total)
2151 nodeToConnect = rows * cols - cols;
2153 nodeToConnect = i - cols + 1;
2155 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2159 nodeToConnect = (rows * cols) - cols + i;
2160 if (nodeToConnect >= pg->total)
2161 nodeToConnect -= cols;
2164 nodeToConnect = i - cols;
2166 if (nodeToConnect < pg->total)
2167 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2169 natLog = log (pg->total);
2170 #if VERBOSE_TESTING > 2
2171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2172 _("natural log of %d is %d, will run %d iterations\n"),
2173 pg->total, natLog, (int) (natLog * percentage));
2174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2175 _("Total connections added thus far: %u!\n"), connect_attempts);
2177 smallWorldConnections = 0;
2178 small_world_it = (unsigned int) (natLog * percentage);
2179 if (small_world_it < 1)
2181 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
2182 for (i = 0; i < small_world_it; i++)
2184 for (j = 0; j < pg->total; j++)
2186 /* Determine the row and column of node at position j on the 2d torus */
2187 node1Row = j / cols;
2188 node1Col = j - (node1Row * cols);
2189 for (k = 0; k < pg->total; k++)
2191 /* Determine the row and column of node at position k on the 2d torus */
2192 node2Row = k / cols;
2193 node2Col = k - (node2Row * cols);
2194 /* Simple Cartesian distance */
2195 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
2198 /* Calculate probability as 1 over the square of the distance */
2199 probability = 1.0 / (distance * distance);
2200 /* Choose a random value between 0 and 1 */
2202 = ((double) GNUNET_CRYPTO_random_u64 (
2203 GNUNET_CRYPTO_QUALITY_WEAK,
2205 / ((double) UINT64_MAX);
2206 /* If random < probability, then connect the two nodes */
2207 if (random < probability)
2208 smallWorldConnections += proc (pg, j, k, list, GNUNET_YES);
2214 connect_attempts += smallWorldConnections;
2215 #if VERBOSE_TESTING > 2
2216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2217 _("Total connections added for small world: %d!\n"),
2218 smallWorldConnections);
2220 return connect_attempts;
2224 * Create a topology given a peer group (set of running peers)
2225 * and a connection processor.
2227 * @param pg the peergroup to create the topology on
2228 * @param proc the connection processor to call to actually set
2229 * up connections between two peers
2230 * @param list the peer list to use
2232 * @return the number of connections that were set up
2236 create_erdos_renyi(struct GNUNET_TESTING_PeerGroup *pg,
2237 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2240 unsigned int outer_count;
2241 unsigned int inner_count;
2242 int connect_attempts;
2246 probability = 0.5; /* FIXME: default percentage? */
2247 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2251 if (sscanf (p_string, "%lf", &probability) != 1)
2253 GNUNET_ERROR_TYPE_WARNING,
2255 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2256 p_string, "PROBABILITY", "TESTING");
2257 GNUNET_free (p_string);
2259 connect_attempts = 0;
2260 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2262 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2265 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2267 / ((double) UINT64_MAX);
2269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2270 _("rand is %f probability is %f\n"), temp_rand,
2273 if (temp_rand < probability)
2275 connect_attempts += proc (pg, outer_count, inner_count, list,
2281 return connect_attempts;
2285 * Create a topology given a peer group (set of running peers)
2286 * and a connection processor. This particular function creates
2287 * the connections for a 2d-torus, plus additional "closest"
2288 * connections per peer.
2290 * @param pg the peergroup to create the topology on
2291 * @param proc the connection processor to call to actually set
2292 * up connections between two peers
2293 * @param list the peer list to use
2295 * @return the number of connections that were set up
2299 create_2d_torus(struct GNUNET_TESTING_PeerGroup *pg,
2300 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2303 unsigned int square;
2306 unsigned int toggle = 1;
2307 unsigned int nodeToConnect;
2308 int connect_attempts;
2310 connect_attempts = 0;
2312 square = floor (sqrt (pg->total));
2316 if (square * square != pg->total)
2318 while (rows * cols < pg->total)
2320 if (toggle % 2 == 0)
2329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2331 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2334 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2335 * to the node to its right and above. Once this is over, we'll have our torus!
2336 * Special case for the last node (if the rows and columns are not equal), connect
2337 * to the first in the row to maintain topology.
2339 for (i = 0; i < pg->total; i++)
2341 /* First connect to the node to the right */
2342 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2343 nodeToConnect = i + 1;
2344 else if (i + 1 == pg->total)
2345 nodeToConnect = rows * cols - cols;
2347 nodeToConnect = i - cols + 1;
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2350 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2352 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2354 /* Second connect to the node immediately above */
2357 nodeToConnect = (rows * cols) - cols + i;
2358 if (nodeToConnect >= pg->total)
2359 nodeToConnect -= cols;
2362 nodeToConnect = i - cols;
2364 if (nodeToConnect < pg->total)
2367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2368 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2370 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2375 return connect_attempts;
2379 * Create a topology given a peer group (set of running peers)
2380 * and a connection processor.
2382 * @param pg the peergroup to create the topology on
2383 * @param proc the connection processor to call to actually set
2384 * up connections between two peers
2385 * @param list the peer list to use
2386 * @param check does the connection processor need to check before
2387 * performing an action on the list?
2389 * @return the number of connections that were set up
2393 create_clique(struct GNUNET_TESTING_PeerGroup *pg,
2394 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list,
2397 unsigned int outer_count;
2398 unsigned int inner_count;
2399 int connect_attempts;
2400 struct ProgressMeter *conn_meter;
2401 connect_attempts = 0;
2403 conn_meter = create_meter ((((pg->total * pg->total) + pg->total) / 2)
2404 - pg->total, "Create Clique ", GNUNET_YES);
2405 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2407 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2411 "Connecting peer %d to peer %d\n",
2412 outer_count, inner_count);
2414 connect_attempts += proc (pg, outer_count, inner_count, list, check);
2415 update_meter (conn_meter);
2418 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Meter has %d left\n",
2419 conn_meter->total - conn_meter->completed);
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);
2454 * Create a blacklist topology based on the allowed topology
2455 * which disallows any connections not in the allowed topology
2456 * at the transport level.
2458 * @param pg the peergroup to create the topology on
2459 * @param proc the connection processor to call to allow
2460 * up connections between two peers
2462 * @return the number of connections that were set up
2466 copy_allowed(struct GNUNET_TESTING_PeerGroup *pg,
2467 GNUNET_TESTING_ConnectionProcessor proc)
2469 struct UnblacklistContext un_ctx;
2472 struct PeerConnection *iter;
2476 for (count = 0; count < pg->total - 1; count++)
2478 un_ctx.first_uid = count;
2480 iter = pg->peers[count].allowed_peers_head;
2481 while (iter != NULL)
2483 remove_connections (pg, count, iter->index, BLACKLIST, GNUNET_YES);
2484 //unblacklist_connections(pg, count, iter->index);
2488 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers, &unblacklist_iterator, &un_ctx);
2491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2496 * Create a topology given a peer group (set of running peers)
2497 * and a connection processor.
2499 * @param pg the peergroup to create the topology on
2500 * @param proc the connection processor to call to actually set
2501 * up connections between two peers
2502 * @param list which list should be modified
2504 * @return the number of connections that were set up
2508 create_line(struct GNUNET_TESTING_PeerGroup *pg,
2509 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2512 int connect_attempts;
2514 connect_attempts = 0;
2516 /* Connect each peer to the next highest numbered peer */
2517 for (count = 0; count < pg->total - 1; count++)
2520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2521 "Connecting peer %d to peer %d\n", count, count + 1);
2523 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2526 return connect_attempts;
2530 * Create a topology given a peer group (set of running peers)
2531 * and a connection processor.
2533 * @param pg the peergroup to create the topology on
2534 * @param filename the file to read topology information from
2535 * @param proc the connection processor to call to actually set
2536 * up connections between two peers
2537 * @param list the peer list to use
2539 * @return the number of connections that were set up
2543 create_from_file(struct GNUNET_TESTING_PeerGroup *pg, char *filename,
2544 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2546 int connect_attempts;
2547 unsigned int first_peer_index;
2548 unsigned int second_peer_index;
2549 connect_attempts = 0;
2554 unsigned int total_peers;
2556 enum States curr_state;
2558 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2559 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2561 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2563 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2564 "Could not open file `%s' specified for topology!", filename);
2565 return connect_attempts;
2568 data = GNUNET_malloc_large (frstat.st_size);
2569 GNUNET_assert(data != NULL);
2570 if (frstat.st_size != GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2573 GNUNET_ERROR_TYPE_ERROR,
2574 "Could not read file %s specified for host list, ending test!",
2577 return connect_attempts;
2582 /* First line should contain a single integer, specifying the number of peers */
2583 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2584 curr_state = NUM_PEERS;
2585 while (count < frstat.st_size - 1)
2587 if ((buf[count] == '\n') || (buf[count] == ' '))
2596 if (1 != sscanf (&buf[count], "%u", &total_peers))
2598 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2599 "Failed to read number of peers from topology file!\n");
2600 GNUNET_free_non_null(data);
2601 return connect_attempts;
2603 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2604 "Read %u total peers in topology\n", total_peers);
2605 GNUNET_assert(total_peers == pg->total);
2606 curr_state = PEER_INDEX;
2607 while ((buf[count] != '\n') && (count < frstat.st_size - 1))
2612 if (1 != sscanf (&buf[count], "%u", &first_peer_index))
2614 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2615 "Failed to read peer index from topology file!\n");
2616 GNUNET_free_non_null(data);
2617 return connect_attempts;
2619 while ((buf[count] != ':') && (count < frstat.st_size - 1))
2622 curr_state = OTHER_PEER_INDEX;
2625 if (1 == sscanf (&buf[count], ":"))
2626 curr_state = OTHER_PEER_INDEX;
2629 case OTHER_PEER_INDEX:
2630 if (1 != sscanf (&buf[count], "%u", &second_peer_index))
2632 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2633 "Failed to peer index from topology file!\n");
2634 GNUNET_free_non_null(data);
2635 return connect_attempts;
2637 /* Assume file is written with first peer 1, but array index is 0 */
2638 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index
2639 - 1, list, GNUNET_YES);
2640 while ((buf[count] != '\n') && (buf[count] != ',') && (count
2641 < frstat.st_size - 1))
2643 if (buf[count] == '\n')
2645 curr_state = PEER_INDEX;
2647 else if (buf[count] != ',')
2649 curr_state = OTHER_PEER_INDEX;
2654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2655 "Found bad data in topology file while in state %d!\n",
2658 return connect_attempts;
2663 /* Connect each peer to the next highest numbered peer */
2664 for (count = 0; count < pg->total - 1; count++)
2667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2668 "Connecting peer %d to peer %d\n", first_peer_index, second_peer_index);
2670 connect_attempts += proc (pg, first_peer_index, second_peer_index);
2673 return connect_attempts;
2677 * Create a topology given a peer group (set of running peers)
2678 * and a connection processor.
2680 * @param pg the peergroup to create the topology on
2681 * @param proc the connection processor to call to actually set
2682 * up connections between two peers
2683 * @param list the peer list to use
2685 * @return the number of connections that were set up
2689 create_ring(struct GNUNET_TESTING_PeerGroup *pg,
2690 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2693 int connect_attempts;
2695 connect_attempts = 0;
2697 /* Connect each peer to the next highest numbered peer */
2698 for (count = 0; count < pg->total - 1; count++)
2701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2702 "Connecting peer %d to peer %d\n", count, count + 1);
2704 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2707 /* Connect the last peer to the first peer */
2708 connect_attempts += proc (pg, pg->total - 1, 0, list, GNUNET_YES);
2710 return connect_attempts;
2715 * Iterator for writing friends of a peer to a file.
2717 * @param cls closure, an open writable file handle
2718 * @param key the key the daemon was stored under
2719 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2721 * @return GNUNET_YES to continue iteration
2723 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2724 * with a single file_iterator that takes a closure which contains
2725 * the prefix to write before the peer. Then this could be used
2726 * for blacklisting multiple transports and writing the friend
2727 * file. I'm sure *someone* will complain loudly about other
2728 * things that negate these functions even existing so no point in
2732 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2734 FILE *temp_friend_handle = cls;
2735 struct GNUNET_TESTING_Daemon *peer = value;
2736 struct GNUNET_PeerIdentity *temppeer;
2737 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2739 temppeer = &peer->id;
2740 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2741 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2746 struct BlacklistContext
2749 * The (open) file handle to write to
2751 FILE *temp_file_handle;
2754 * The transport that this peer will be blacklisted on.
2760 * Iterator for writing blacklist data to appropriate files.
2762 * @param cls closure, an open writable file handle
2763 * @param key the key the daemon was stored under
2764 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2766 * @return GNUNET_YES to continue iteration
2769 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2771 struct BlacklistContext *blacklist_ctx = cls;
2772 struct GNUNET_TESTING_Daemon *peer = value;
2773 struct GNUNET_PeerIdentity *temppeer;
2774 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2776 temppeer = &peer->id;
2777 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2778 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2779 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2780 blacklist_ctx->transport, (char *) &peer_enc);
2787 * Create the friend files based on the PeerConnection's
2788 * of each peer in the peer group, and copy the files
2789 * to the appropriate place
2791 * @param pg the peer group we are dealing with
2794 create_and_copy_friend_files(struct GNUNET_TESTING_PeerGroup *pg)
2796 FILE *temp_friend_handle;
2797 unsigned int pg_iter;
2798 char *temp_service_path;
2799 struct GNUNET_OS_Process **procarr;
2803 enum GNUNET_OS_ProcessStatusType type;
2804 unsigned long return_code;
2812 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2813 struct PeerConnection *conn_iter;
2815 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2816 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2818 mytemp = GNUNET_DISK_mktemp ("friends");
2819 GNUNET_assert (mytemp != NULL);
2820 temp_friend_handle = fopen (mytemp, "wt");
2821 GNUNET_assert (temp_friend_handle != NULL);
2823 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2824 while (conn_iter != NULL)
2826 GNUNET_CRYPTO_hash_to_enc (
2827 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2829 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2830 conn_iter = conn_iter->next;
2833 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2834 &friend_file_iterator,
2835 temp_friend_handle);
2837 fclose (temp_friend_handle);
2840 != GNUNET_CONFIGURATION_get_value_string (
2841 pg->peers[pg_iter]. daemon->cfg,
2842 "PATHS", "SERVICEHOME",
2843 &temp_service_path))
2846 GNUNET_ERROR_TYPE_WARNING,
2848 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2849 "SERVICEHOME", "PATHS");
2850 if (UNLINK (mytemp) != 0)
2851 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2853 GNUNET_free (mytemp);
2857 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2859 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2860 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
2863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2864 _("Copying file with command cp %s %s\n"), mytemp, arg);
2866 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2867 GNUNET_OS_process_close (procarr[pg_iter]);
2870 else /* Remote, scp the file to the correct place */
2872 if (NULL != pg->peers[pg_iter].daemon->username)
2873 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2874 pg->peers[pg_iter].daemon->username,
2875 pg->peers[pg_iter].daemon->hostname,
2878 GNUNET_asprintf (&arg, "%s:%s/friends",
2879 pg->peers[pg_iter].daemon->hostname,
2881 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
2884 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2885 GNUNET_OS_process_close (procarr[pg_iter]);
2886 if (ret != GNUNET_OK)
2888 procarr[pg_iter] = NULL;
2890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2891 _("Copying file with command scp %s %s\n"), mytemp,
2896 GNUNET_free (temp_service_path);
2897 GNUNET_free (mytemp);
2902 ret = GNUNET_SYSERR;
2903 while ((count < max_wait) && (ret != GNUNET_OK))
2906 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2910 _("Checking copy status of file %d\n"), pg_iter);
2912 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2914 if (GNUNET_OS_process_status
2915 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2917 ret = GNUNET_SYSERR;
2919 else if ((type != GNUNET_OS_PROCESS_EXITED)
2920 || (return_code != 0))
2922 ret = GNUNET_SYSERR;
2926 GNUNET_OS_process_close (procarr[pg_iter]);
2927 procarr[pg_iter] = NULL;
2929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2930 _("File %d copied\n"), pg_iter);
2936 if (ret == GNUNET_SYSERR)
2938 /* FIXME: why sleep here? -CG */
2944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2945 _("Finished copying all friend files!\n"));
2948 GNUNET_free (procarr);
2953 * Create the blacklist files based on the PeerConnection's
2954 * of each peer in the peer group, and copy the files
2955 * to the appropriate place.
2957 * @param pg the peer group we are dealing with
2958 * @param transports space delimited list of transports to blacklist
2961 create_and_copy_blacklist_files(struct GNUNET_TESTING_PeerGroup *pg,
2962 const char *transports)
2964 FILE *temp_file_handle;
2965 unsigned int pg_iter;
2966 char *temp_service_path;
2967 struct GNUNET_OS_Process **procarr;
2970 enum GNUNET_OS_ProcessStatusType type;
2971 unsigned long return_code;
2978 char *temp_transports;
2981 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2982 struct PeerConnection *conn_iter;
2984 static struct BlacklistContext blacklist_ctx;
2987 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2988 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2990 mytemp = GNUNET_DISK_mktemp ("blacklist");
2991 GNUNET_assert (mytemp != NULL);
2992 temp_file_handle = fopen (mytemp, "wt");
2993 GNUNET_assert (temp_file_handle != NULL);
2994 temp_transports = GNUNET_strdup (transports);
2996 blacklist_ctx.temp_file_handle = temp_file_handle;
2998 transport_len = strlen (temp_transports) + 1;
3001 for (i = 0; i < transport_len; i++)
3003 if ((temp_transports[i] == ' ') && (pos == NULL))
3004 continue; /* At start of string (whitespace) */
3005 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
3007 temp_transports[i] = '\0';
3009 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
3010 while (conn_iter != NULL)
3012 GNUNET_CRYPTO_hash_to_enc (
3013 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
3015 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
3016 conn_iter = conn_iter->next;
3020 blacklist_ctx.transport = pos;
3021 entry_count = GNUNET_CONTAINER_multihashmap_iterate (pg->
3023 [pg_iter].blacklisted_peers,
3024 &blacklist_file_iterator,
3028 } /* At beginning of actual string */
3029 else if (pos == NULL)
3031 pos = &temp_transports[i];
3035 GNUNET_free (temp_transports);
3036 fclose (temp_file_handle);
3039 != GNUNET_CONFIGURATION_get_value_string (
3040 pg->peers[pg_iter]. daemon->cfg,
3041 "PATHS", "SERVICEHOME",
3042 &temp_service_path))
3045 GNUNET_ERROR_TYPE_WARNING,
3047 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
3048 "SERVICEHOME", "PATHS");
3049 if (UNLINK (mytemp) != 0)
3050 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
3052 GNUNET_free (mytemp);
3056 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
3058 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
3059 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
3062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3063 _("Copying file with command cp %s %s\n"), mytemp, arg);
3068 else /* Remote, scp the file to the correct place */
3070 if (NULL != pg->peers[pg_iter].daemon->username)
3071 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
3072 pg->peers[pg_iter].daemon->username,
3073 pg->peers[pg_iter].daemon->hostname,
3076 GNUNET_asprintf (&arg, "%s:%s/blacklist",
3077 pg->peers[pg_iter].daemon->hostname,
3079 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
3082 GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
3085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3086 _("Copying file with command scp %s %s\n"), mytemp,
3091 GNUNET_free (temp_service_path);
3092 GNUNET_free (mytemp);
3096 ret = GNUNET_SYSERR;
3097 while ((count < max_wait) && (ret != GNUNET_OK))
3100 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3104 _("Checking copy status of file %d\n"), pg_iter);
3106 if (procarr[pg_iter] != NULL) /* Check for already completed! */
3108 if (GNUNET_OS_process_status (procarr[pg_iter], &type,
3109 &return_code) != GNUNET_OK)
3111 ret = GNUNET_SYSERR;
3113 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
3115 ret = GNUNET_SYSERR;
3119 GNUNET_OS_process_close (procarr[pg_iter]);
3120 procarr[pg_iter] = NULL;
3122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3123 _("File %d copied\n"), pg_iter);
3129 if (ret == GNUNET_SYSERR)
3131 /* FIXME: why sleep here? -CG */
3137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3138 _("Finished copying all blacklist files!\n"));
3140 GNUNET_free (procarr);
3144 /* Forward Declaration */
3146 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3149 * Choose a random peer's next connection to create, and
3150 * call schedule_connect to set up the connect task.
3152 * @param ct_ctx the overall connection context
3155 preschedule_connect(struct GNUNET_TESTING_PeerGroup *pg)
3157 struct ConnectTopologyContext *ct_ctx = &pg->ct_ctx;
3158 struct PeerConnection *connection_iter;
3159 struct ConnectContext *connect_context;
3160 uint32_t random_peer;
3162 if (ct_ctx->remaining_connections == 0)
3165 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3166 while (pg->peers[random_peer].connect_peers_head == NULL)
3167 random_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3170 connection_iter = pg->peers[random_peer].connect_peers_head;
3171 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3172 connect_context->first_index = random_peer;
3173 connect_context->second_index = connection_iter->index;
3174 connect_context->ct_ctx = ct_ctx;
3175 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3176 GNUNET_CONTAINER_DLL_remove(pg->peers[random_peer].connect_peers_head, pg->peers[random_peer].connect_peers_tail, connection_iter);
3177 GNUNET_free(connection_iter);
3178 ct_ctx->remaining_connections--;
3182 /* Forward declaration */
3183 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3186 * Close connections and free the hello context.
3188 * @param cls the 'struct SendHelloContext *'
3189 * @param tc scheduler context
3192 free_hello_context (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3194 struct SendHelloContext *send_hello_context = cls;
3195 if (send_hello_context->peer->daemon->server != NULL)
3197 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3198 send_hello_context->peer->daemon->server = NULL;
3200 if (send_hello_context->peer->daemon->th != NULL)
3202 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3203 send_hello_context->peer->daemon->th = NULL;
3205 if (send_hello_context->core_connect_task != GNUNET_SCHEDULER_NO_TASK)
3207 GNUNET_SCHEDULER_cancel(send_hello_context->core_connect_task);
3208 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3210 send_hello_context->pg->outstanding_connects--;
3211 GNUNET_free(send_hello_context);
3215 * For peers that haven't yet connected, notify
3216 * the caller that they have failed (timeout).
3218 * @param cls the 'struct SendHelloContext *'
3219 * @param tc scheduler context
3222 notify_remaining_connections_failed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3224 struct SendHelloContext *send_hello_context = cls;
3225 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3226 struct PeerConnection *connection;
3228 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3229 send_hello_context->peer->daemon->server = NULL;
3231 connection = send_hello_context->peer->connect_peers_head;
3233 while (connection != NULL)
3235 if (pg->notify_connection != NULL)
3237 pg->notify_connection(pg->notify_connection_cls,
3238 &send_hello_context->peer->daemon->id,
3239 &pg->peers[connection->index].daemon->id,
3241 send_hello_context->peer->daemon->cfg,
3242 pg->peers[connection->index].daemon->cfg,
3243 send_hello_context->peer->daemon,
3244 pg->peers[connection->index].daemon,
3245 "Peers failed to connect (timeout)");
3247 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3248 GNUNET_free(connection);
3249 connection = connection->next;
3251 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3253 other_peer = &pg->peers[connection->index];
3258 * For peers that haven't yet connected, send
3259 * CORE connect requests.
3261 * @param cls the 'struct SendHelloContext *'
3262 * @param tc scheduler context
3265 send_core_connect_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3267 struct SendHelloContext *send_hello_context = cls;
3268 struct PeerConnection *conn;
3269 GNUNET_assert(send_hello_context->peer->daemon->server != NULL);
3271 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3273 send_hello_context->connect_attempts++;
3274 if (send_hello_context->connect_attempts < send_hello_context->pg->ct_ctx.connect_attempts)
3276 conn = send_hello_context->peer->connect_peers_head;
3277 while (conn != NULL)
3279 GNUNET_CORE_peer_request_connect(send_hello_context->peer->daemon->server,
3280 GNUNET_TIME_relative_get_forever(),
3281 &send_hello_context->pg->peers[conn->index].daemon->id,
3286 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) ,
3287 &send_core_connect_requests,
3288 send_hello_context);
3292 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Timeout before all connections created, marking rest as failed!\n");
3293 GNUNET_SCHEDULER_add_now(¬ify_remaining_connections_failed, send_hello_context);
3299 * Success, connection is up. Signal client our success.
3301 * @param cls our "struct SendHelloContext"
3302 * @param peer identity of the peer that has connected
3303 * @param atsi performance information
3305 * FIXME: remove peers from BOTH lists, call notify twice, should
3306 * double the speed of connections as long as the list iteration
3307 * doesn't take too long!
3310 core_connect_notify (void *cls,
3311 const struct GNUNET_PeerIdentity *peer,
3312 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3314 struct SendHelloContext *send_hello_context = cls;
3315 struct PeerConnection *connection;
3316 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3318 struct PeerData *other_peer;
3321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3322 "Connected peer %s to peer %s\n",
3323 ctx->d1->shortname, GNUNET_i2s(peer));
3326 if (0 == memcmp(&send_hello_context->peer->daemon->id, peer, sizeof(struct GNUNET_PeerIdentity)))
3329 connection = send_hello_context->peer->connect_peers_head;
3334 while ((connection != NULL) &&
3335 (0 != memcmp(&pg->peers[connection->index].daemon->id, peer, sizeof(struct GNUNET_PeerIdentity))))
3337 connection = connection->next;
3340 if (connection == NULL)
3342 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);
3347 other_peer = &pg->peers[connection->index];
3349 if (pg->notify_connection != NULL)
3351 pg->notify_connection(pg->notify_connection_cls,
3352 &send_hello_context->peer->daemon->id,
3355 send_hello_context->peer->daemon->cfg,
3356 pg->peers[connection->index].daemon->cfg,
3357 send_hello_context->peer->daemon,
3358 pg->peers[connection->index].daemon,
3361 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3362 GNUNET_free(connection);
3366 /* Notify of reverse connection and remove from other peers list of outstanding */
3367 if (other_peer != NULL)
3369 connection = other_peer->connect_peers_head;
3370 while ((connection != NULL) &&
3371 (0 != memcmp(&send_hello_context->peer->daemon->id, &pg->peers[connection->index].daemon->id, sizeof(struct GNUNET_PeerIdentity))))
3373 connection = connection->next;
3375 if (connection != NULL)
3377 if (pg->notify_connection != NULL)
3379 pg->notify_connection(pg->notify_connection_cls,
3381 &send_hello_context->peer->daemon->id,
3383 pg->peers[connection->index].daemon->cfg,
3384 send_hello_context->peer->daemon->cfg,
3385 pg->peers[connection->index].daemon,
3386 send_hello_context->peer->daemon,
3390 GNUNET_CONTAINER_DLL_remove(other_peer->connect_peers_head, other_peer->connect_peers_tail, connection);
3391 GNUNET_free(connection);
3396 if (send_hello_context->peer->connect_peers_head == NULL)
3398 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3403 * Notify of a successful connection to the core service.
3405 * @param cls a struct SendHelloContext *
3406 * @param server handle to the core service
3407 * @param my_identity the peer identity of this peer
3408 * @param publicKey the public key of the peer
3411 core_init (void *cls,
3412 struct GNUNET_CORE_Handle * server,
3413 const struct GNUNET_PeerIdentity *
3416 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
3419 struct SendHelloContext *send_hello_context = cls;
3420 send_hello_context->core_ready = GNUNET_YES;
3424 * Function called once a hello has been sent
3425 * to the transport, move on to the next one
3426 * or go away forever.
3428 * @param cls the 'struct SendHelloContext *'
3429 * @param tc scheduler context
3432 hello_sent_callback (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3434 struct SendHelloContext *send_hello_context = cls;
3435 //unsigned int pg_iter;
3436 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3438 GNUNET_free(send_hello_context);
3442 send_hello_context->pg->remaining_hellos--;
3444 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sent HELLO, have %d remaining!\n", send_hello_context->pg->remaining_hellos);
3446 if (send_hello_context->peer_pos == NULL) /* All HELLOs (for this peer!) have been transmitted! */
3449 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All hellos for this peer sent, disconnecting transport!\n");
3451 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3452 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3453 send_hello_context->peer->daemon->th = NULL;
3455 /*if (send_hello_context->pg->remaining_hellos == 0)
3457 for (pg_iter = 0; pg_iter < send_hello_context->pg->max_outstanding_connections; pg_iter++)
3459 preschedule_connect(&send_hello_context->pg->ct_ctx);
3463 GNUNET_assert (send_hello_context->peer->daemon->server == NULL);
3464 send_hello_context->peer->daemon->server = GNUNET_CORE_connect(send_hello_context->peer->cfg,
3468 &core_connect_notify,
3475 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),
3476 &send_core_connect_requests,
3477 send_hello_context);
3480 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3484 * Connect to a peer, give it all the HELLO's of those peers
3485 * we will later ask it to connect to.
3487 * @param ct_ctx the overall connection context
3489 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3491 struct SendHelloContext *send_hello_context = cls;
3492 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3494 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3496 GNUNET_free(send_hello_context);
3500 GNUNET_assert(send_hello_context->peer_pos != NULL); /* All of the HELLO sends to be scheduled have been scheduled! */
3502 if (((send_hello_context->peer->daemon->th == NULL) &&
3503 (pg->outstanding_connects > pg->max_outstanding_connections)) ||
3504 (pg->stop_connects == GNUNET_YES))
3506 #if VERBOSE_TESTING > 2
3507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3509 ("Delaying connect, we have too many outstanding connections!\n"));
3511 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3512 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3513 &schedule_send_hellos, send_hello_context);
3517 #if VERBOSE_TESTING > 2
3518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3519 _("Creating connection, outstanding_connections is %d\n"),
3520 outstanding_connects);
3522 if (send_hello_context->peer->daemon->th == NULL)
3524 pg->outstanding_connects++; /* Actual TRANSPORT, CORE connections! */
3525 send_hello_context->peer->daemon->th = GNUNET_TRANSPORT_connect(send_hello_context->peer->cfg,
3533 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3534 _("Offering Hello of peer %s to peer %s\n"),
3535 send_hello_context->peer->daemon->shortname, pg->peers[send_hello_context->peer_pos->index].daemon->shortname);
3537 GNUNET_TRANSPORT_offer_hello(send_hello_context->peer->daemon->th,
3538 (const struct GNUNET_MessageHeader *)pg->peers[send_hello_context->peer_pos->index].daemon->hello,
3539 &hello_sent_callback,
3540 send_hello_context);
3541 send_hello_context->peer_pos = send_hello_context->peer_pos->next;
3542 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3548 * Internal notification of a connection, kept so that we can ensure some connections
3549 * happen instead of flooding all testing daemons with requests to connect.
3552 internal_connect_notify(void *cls, const struct GNUNET_PeerIdentity *first,
3553 const struct GNUNET_PeerIdentity *second,
3555 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
3556 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
3557 struct GNUNET_TESTING_Daemon *first_daemon,
3558 struct GNUNET_TESTING_Daemon *second_daemon,
3561 struct ConnectContext *connect_ctx = cls;
3562 struct ConnectTopologyContext *ct_ctx = connect_ctx->ct_ctx;
3563 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
3564 struct PeerConnection *connection;
3565 pg->outstanding_connects--;
3568 * Check whether the inverse connection has been scheduled yet,
3569 * if not, we can remove it from the other peers list and avoid
3570 * even trying to connect them again!
3572 connection = pg->peers[connect_ctx->second_index].connect_peers_head;
3577 while ((connection != NULL) && (0
3578 != memcmp (first, &pg->peers[connection->index].daemon->id,
3579 sizeof(struct GNUNET_PeerIdentity))))
3581 connection = connection->next;
3584 if (connection != NULL) /* Can safely remove! */
3586 ct_ctx->remaining_connections--;
3587 if (pg->notify_connection != NULL) /* Notify of reverse connection */
3588 pg->notify_connection (pg->notify_connection_cls, second, first,
3589 distance, second_cfg, first_cfg, second_daemon,
3590 first_daemon, emsg);
3592 GNUNET_CONTAINER_DLL_remove(pg->peers[connect_ctx->second_index].connect_peers_head, pg->peers[connect_ctx->second_index].connect_peers_tail, connection);
3593 GNUNET_free(connection);
3596 if (ct_ctx->remaining_connections == 0)
3598 if (ct_ctx->notify_connections_done != NULL)
3599 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
3602 preschedule_connect (pg);
3604 if (pg->notify_connection != NULL)
3605 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
3606 first_cfg, second_cfg, first_daemon, second_daemon,
3609 GNUNET_free(connect_ctx);
3613 * Either delay a connection (because there are too many outstanding)
3614 * or schedule it for right now.
3616 * @param cls a connection context
3617 * @param tc the task runtime context
3620 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3622 struct ConnectContext *connect_context = cls;
3623 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
3625 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3628 if ((pg->outstanding_connects > pg->max_outstanding_connections)
3629 || (pg->stop_connects == GNUNET_YES))
3632 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3634 ("Delaying connect, we have too many outstanding connections!\n"));
3636 GNUNET_SCHEDULER_add_delayed (
3637 GNUNET_TIME_relative_multiply (
3638 GNUNET_TIME_UNIT_MILLISECONDS,
3640 &schedule_connect, connect_context);
3645 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3646 _("Creating connection, outstanding_connections is %d (max %d)\n"),
3647 pg->outstanding_connects, pg->max_outstanding_connections);
3649 pg->outstanding_connects++;
3650 pg->total_connects_scheduled++;
3651 GNUNET_TESTING_daemons_connect (
3652 pg->peers[connect_context->first_index].daemon,
3653 pg->peers[connect_context->second_index].daemon,
3654 connect_context->ct_ctx->connect_timeout,
3655 connect_context->ct_ctx->connect_attempts,
3661 &internal_connect_notify, connect_context); /* FIXME: free connect context! */
3667 * Iterator for actually scheduling connections to be created
3668 * between two peers.
3670 * @param cls closure, a GNUNET_TESTING_Daemon
3671 * @param key the key the second Daemon was stored under
3672 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3674 * @return GNUNET_YES to continue iteration
3677 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3679 struct ConnectTopologyContext *ct_ctx = cls;
3680 struct PeerData *first = ct_ctx->first;
3681 struct GNUNET_TESTING_Daemon *second = value;
3682 struct ConnectContext *connect_context;
3684 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3685 connect_context->first = first->daemon;
3686 connect_context->second = second;
3687 connect_context->ct_ctx = ct_ctx;
3688 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3696 * Iterator for copying all entries in the allowed hashmap to the
3699 * @param cls closure, a GNUNET_TESTING_Daemon
3700 * @param key the key the second Daemon was stored under
3701 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3703 * @return GNUNET_YES to continue iteration
3706 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3708 struct PeerData *first = cls;
3710 GNUNET_assert (GNUNET_OK ==
3711 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
3713 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3720 * Make the peers to connect the same as those that are allowed to be
3723 * @param pg the peer group
3726 copy_allowed_topology(struct GNUNET_TESTING_PeerGroup *pg)
3728 unsigned int pg_iter;
3732 struct PeerConnection *iter;
3736 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3739 iter = pg->peers[pg_iter].allowed_peers_head;
3740 while (iter != NULL)
3742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3743 "Creating connection between %d and %d\n", pg_iter,
3745 total += add_connections (pg, pg_iter, iter->index, CONNECT,
3747 //total += add_actual_connections(pg, pg_iter, iter->index);
3752 GNUNET_CONTAINER_multihashmap_iterate (pg->
3753 peers[pg_iter].allowed_peers,
3754 ©_topology_iterator,
3755 &pg->peers[pg_iter]);
3757 if (GNUNET_SYSERR == ret)
3758 return GNUNET_SYSERR;
3760 total = total + ret;
3767 * Connect the topology as specified by the PeerConnection's
3768 * of each peer in the peer group
3770 * @param pg the peer group we are dealing with
3771 * @param connect_timeout how long try connecting two peers
3772 * @param connect_attempts how many times (max) to attempt
3773 * @param notify_callback callback to notify when finished
3774 * @param notify_cls closure for notify callback
3776 * @return the number of connections that will be attempted
3779 connect_topology(struct GNUNET_TESTING_PeerGroup *pg,
3780 struct GNUNET_TIME_Relative connect_timeout,
3781 unsigned int connect_attempts,
3782 GNUNET_TESTING_NotifyCompletion notify_callback,
3785 unsigned int pg_iter;
3789 struct PeerConnection *connection_iter;
3792 struct SendHelloContext *send_hello_context
3796 pg->ct_ctx.notify_connections_done = notify_callback;
3797 pg->ct_ctx.notify_cls = notify_cls;
3800 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3803 connection_iter = pg->peers[pg_iter].connect_peers_head;
3804 while (connection_iter != NULL)
3806 connection_iter = connection_iter->next;
3811 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3818 pg->ct_ctx.connect_timeout = connect_timeout;
3819 pg->ct_ctx.connect_attempts = connect_attempts;
3820 pg->ct_ctx.remaining_connections = total;
3823 /* First give all peers the HELLO's of other peers (connect to first peer's transport service, give HELLO's of other peers, continue...) */
3824 pg->remaining_hellos = total;
3825 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3827 send_hello_context = GNUNET_malloc(sizeof(struct SendHelloContext));
3828 send_hello_context->peer = &pg->peers[pg_iter];
3829 send_hello_context->peer_pos = pg->peers[pg_iter].connect_peers_head;
3830 send_hello_context->pg = pg;
3831 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3834 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3836 preschedule_connect (pg);
3844 * Takes a peer group and creates a topology based on the
3845 * one specified. Creates a topology means generates friend
3846 * files for the peers so they can only connect to those allowed
3847 * by the topology. This will only have an effect once peers
3848 * are started if the FRIENDS_ONLY option is set in the base
3849 * config. Also takes an optional restrict topology which
3850 * disallows connections based on particular transports
3851 * UNLESS they are specified in the restricted topology.
3853 * @param pg the peer group struct representing the running peers
3854 * @param topology which topology to connect the peers in
3855 * @param restrict_topology disallow restrict_transports transport
3856 * connections to peers NOT in this topology
3857 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3858 * @param restrict_transports space delimited list of transports to blacklist
3859 * to create restricted topology
3861 * @return the maximum number of connections were all allowed peers
3862 * connected to each other
3865 GNUNET_TESTING_create_topology(struct GNUNET_TESTING_PeerGroup *pg,
3866 enum GNUNET_TESTING_Topology topology,
3867 enum GNUNET_TESTING_Topology restrict_topology,
3868 const char *restrict_transports)
3872 unsigned int num_connections;
3873 int unblacklisted_connections;
3875 struct PeerConnection *conn_iter;
3876 struct PeerConnection *temp_conn;
3881 for (i = 0; i < pg->total; i++)
3883 pg->peers[i].allowed_peers =
3884 GNUNET_CONTAINER_multihashmap_create (100);
3885 pg->peers[i].connect_peers =
3886 GNUNET_CONTAINER_multihashmap_create (100);
3887 pg->peers[i].blacklisted_peers =
3888 GNUNET_CONTAINER_multihashmap_create (100);
3889 pg->peers[i].pg = pg;
3895 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3899 num_connections = create_clique (pg, &add_connections, ALLOWED, GNUNET_NO);
3901 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3904 _("Creating small world (ring) topology\n"));
3906 num_connections = create_small_world_ring (pg, &add_connections, ALLOWED);
3908 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3911 _("Creating small world (2d-torus) topology\n"));
3913 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3915 case GNUNET_TESTING_TOPOLOGY_RING:
3917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3919 num_connections = create_ring (pg, &add_connections, ALLOWED);
3921 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3925 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3927 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3930 _("Creating Erdos-Renyi topology\n"));
3932 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3934 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3938 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3940 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3943 _("Creating Scale Free topology\n"));
3945 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3947 case GNUNET_TESTING_TOPOLOGY_LINE:
3949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3950 _("Creating straight line topology\n"));
3952 num_connections = create_line (pg, &add_connections, ALLOWED);
3954 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3957 _("Creating topology from file!\n"));
3959 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing",
3962 num_connections = create_from_file (pg, filename, &add_connections,
3967 GNUNET_ERROR_TYPE_WARNING,
3968 "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3969 num_connections = 0;
3972 case GNUNET_TESTING_TOPOLOGY_NONE:
3974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3976 ("Creating no allowed topology (all peers can connect at core level)\n"));
3978 num_connections = pg->total * pg->total; /* Clique is allowed! */
3981 num_connections = 0;
3985 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING",
3988 ret = create_and_copy_friend_files (pg);
3989 if (ret != GNUNET_OK)
3992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3993 _("Failed during friend file copying!\n"));
3995 return GNUNET_SYSERR;
4000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4001 _("Friend files created/copied successfully!\n"));
4006 /* Use the create clique method to initially set all connections as blacklisted. */
4007 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology
4008 != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
4009 create_clique (pg, &add_connections, BLACKLIST, GNUNET_NO);
4011 unblacklisted_connections = 0;
4012 /* Un-blacklist connections as per the topology specified */
4013 switch (restrict_topology)
4015 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
4017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4018 _("Blacklisting all but clique topology\n"));
4020 unblacklisted_connections = create_clique (pg, &remove_connections,
4021 BLACKLIST, GNUNET_NO);
4023 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
4025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4026 _("Blacklisting all but small world (ring) topology\n"));
4028 unblacklisted_connections = create_small_world_ring (pg,
4029 &remove_connections,
4032 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
4034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4036 ("Blacklisting all but small world (2d-torus) topology\n"));
4038 unblacklisted_connections = create_small_world (pg, &remove_connections,
4041 case GNUNET_TESTING_TOPOLOGY_RING:
4043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4044 _("Blacklisting all but ring topology\n"));
4046 unblacklisted_connections
4047 = create_ring (pg, &remove_connections, BLACKLIST);
4049 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4052 _("Blacklisting all but 2d torus topology\n"));
4054 unblacklisted_connections = create_2d_torus (pg, &remove_connections,
4057 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4060 _("Blacklisting all but Erdos-Renyi topology\n"));
4062 unblacklisted_connections = create_erdos_renyi (pg, &remove_connections,
4065 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4068 _("Blacklisting all but InterNAT topology\n"));
4072 for (off = 0; off < pg->total; off++)
4074 conn_iter = pg->peers[off].allowed_peers_head;
4075 while (conn_iter != NULL)
4077 temp_conn = conn_iter->next;
4078 GNUNET_free(conn_iter);
4079 conn_iter = temp_conn;
4081 pg->peers[off].allowed_peers_head = NULL;
4082 pg->peers[off].allowed_peers_tail = NULL;
4084 conn_iter = pg->peers[off].connect_peers_head;
4085 while (conn_iter != NULL)
4087 temp_conn = conn_iter->next;
4088 GNUNET_free(conn_iter);
4089 conn_iter = temp_conn;
4091 pg->peers[off].connect_peers_head = NULL;
4092 pg->peers[off].connect_peers_tail = NULL;
4094 unblacklisted_connections
4095 = create_nated_internet_copy (pg, &remove_connections, BLACKLIST);
4097 unblacklisted_connections =
4098 create_nated_internet (pg, &remove_connections, BLACKLIST);
4102 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4105 _("Blacklisting all but Scale Free topology\n"));
4107 unblacklisted_connections = create_scale_free (pg, &remove_connections,
4110 case GNUNET_TESTING_TOPOLOGY_LINE:
4112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4113 _("Blacklisting all but straight line topology\n"));
4115 unblacklisted_connections
4116 = create_line (pg, &remove_connections, BLACKLIST);
4118 case GNUNET_TESTING_TOPOLOGY_NONE: /* Fall through */
4119 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
4121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4123 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
4125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
4126 ("Creating blacklist topology from allowed\n"));
4127 unblacklisted_connections = copy_allowed (pg, &remove_connections);
4132 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
4134 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n",
4135 restrict_transports);
4136 ret = create_and_copy_blacklist_files (pg, restrict_transports);
4137 if (ret != GNUNET_OK)
4140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4141 _("Failed during blacklist file copying!\n"));
4148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4149 _("Blacklist files created/copied successfully!\n"));
4153 return num_connections;
4158 * Iterator for choosing random peers to connect.
4160 * @param cls closure, a RandomContext
4161 * @param key the key the second Daemon was stored under
4162 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4164 * @return GNUNET_YES to continue iteration
4167 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4169 struct RandomContext *random_ctx = cls;
4170 double random_number;
4171 uint32_t second_pos;
4172 GNUNET_HashCode first_hash;
4175 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4176 UINT64_MAX)) / ((double) UINT64_MAX);
4177 if (random_number < random_ctx->percentage)
4179 GNUNET_assert (GNUNET_OK ==
4180 GNUNET_CONTAINER_multihashmap_put (random_ctx->
4181 first->connect_peers_working_set,
4183 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4186 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
4187 uid_from_hash (key, &second_pos);
4188 hash_from_uid (random_ctx->first_uid, &first_hash);
4189 GNUNET_assert (random_ctx->pg->total > second_pos);
4190 GNUNET_assert (GNUNET_YES ==
4191 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
4193 [second_pos].connect_peers,
4202 * Iterator for adding at least X peers to a peers connection set.
4204 * @param cls closure, MinimumContext
4205 * @param key the key the second Daemon was stored under
4206 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4208 * @return GNUNET_YES to continue iteration
4211 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4213 struct MinimumContext *min_ctx = cls;
4214 uint32_t second_pos;
4215 GNUNET_HashCode first_hash;
4218 if (GNUNET_CONTAINER_multihashmap_size
4219 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
4221 for (i = 0; i < min_ctx->num_to_add; i++)
4223 if (min_ctx->pg_array[i] == min_ctx->current)
4225 GNUNET_assert (GNUNET_OK ==
4226 GNUNET_CONTAINER_multihashmap_put
4227 (min_ctx->first->connect_peers_working_set, key,
4229 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4230 uid_from_hash (key, &second_pos);
4231 hash_from_uid (min_ctx->first_uid, &first_hash);
4232 GNUNET_assert (min_ctx->pg->total > second_pos);
4233 GNUNET_assert (GNUNET_OK ==
4234 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4236 [second_pos].connect_peers_working_set,
4240 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4241 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
4242 GNUNET_assert (GNUNET_YES ==
4243 GNUNET_CONTAINER_multihashmap_remove
4244 (min_ctx->pg->peers[second_pos].connect_peers,
4245 &first_hash, min_ctx->first->daemon));
4252 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
4257 * Iterator for adding peers to a connection set based on a depth first search.
4259 * @param cls closure, MinimumContext
4260 * @param key the key the second daemon was stored under
4261 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4263 * @return GNUNET_YES to continue iteration
4266 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4268 struct DFSContext *dfs_ctx = cls;
4269 GNUNET_HashCode first_hash;
4271 if (dfs_ctx->current == dfs_ctx->chosen)
4273 GNUNET_assert (GNUNET_OK ==
4274 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4275 first->connect_peers_working_set,
4277 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4278 uid_from_hash (key, &dfs_ctx->second_uid);
4279 hash_from_uid (dfs_ctx->first_uid, &first_hash);
4280 GNUNET_assert (GNUNET_OK ==
4281 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4283 [dfs_ctx->second_uid].connect_peers_working_set,
4287 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4288 GNUNET_assert (GNUNET_YES ==
4289 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
4291 [dfs_ctx->second_uid].connect_peers,
4295 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
4296 return GNUNET_NO; /* We have found our peer, don't iterate more */
4305 * From the set of connections possible, choose percentage percent of connections
4306 * to actually connect.
4308 * @param pg the peergroup we are dealing with
4309 * @param percentage what percent of total connections to make
4312 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg,
4315 struct RandomContext random_ctx;
4318 struct PeerConnection *temp_peers;
4319 struct PeerConnection *conn_iter;
4320 double random_number;
4323 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4325 random_ctx.first_uid = pg_iter;
4326 random_ctx.first = &pg->peers[pg_iter];
4327 random_ctx.percentage = percentage;
4331 conn_iter = pg->peers[pg_iter].connect_peers_head;
4332 while (conn_iter != NULL)
4335 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4337 / ((double) UINT64_MAX);
4338 if (random_number < percentage)
4340 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4343 conn_iter = conn_iter->next;
4346 pg->peers[pg_iter].connect_peers_working_set =
4347 GNUNET_CONTAINER_multihashmap_create (pg->total);
4348 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4349 &random_connect_iterator,
4351 /* Now remove the old connections */
4352 GNUNET_CONTAINER_multihashmap_destroy (pg->
4353 peers[pg_iter].connect_peers);
4354 /* And replace with the random set */
4355 pg->peers[pg_iter].connect_peers =
4356 pg->peers[pg_iter].connect_peers_working_set;
4360 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4362 conn_iter = pg->peers[pg_iter].connect_peers_head;
4363 while (pg->peers[pg_iter].connect_peers_head != NULL)
4364 remove_connections (pg, pg_iter,
4365 pg->peers[pg_iter].connect_peers_head->index,
4366 CONNECT, GNUNET_YES);
4368 pg->peers[pg_iter].connect_peers_head
4369 = pg->peers[pg_iter].connect_peers_working_set_head;
4370 pg->peers[pg_iter].connect_peers_tail
4371 = pg->peers[pg_iter].connect_peers_working_set_tail;
4372 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4373 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4378 * Count the number of connections in a linked list of connections.
4380 * @param conn_list the connection list to get the count of
4382 * @return the number of elements in the list
4385 count_connections(struct PeerConnection *conn_list)
4387 struct PeerConnection *iter;
4391 while (iter != NULL)
4400 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
4403 unsigned int pg_iter;
4405 struct PeerConnection *conn_iter;
4409 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4412 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
4413 while (conn_iter != NULL)
4416 conn_iter = conn_iter->next;
4420 GNUNET_CONTAINER_multihashmap_size (pg->
4422 [pg_iter].connect_peers_working_set);
4430 count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
4433 unsigned int pg_iter;
4435 struct PeerConnection *conn_iter;
4439 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4442 conn_iter = pg->peers[pg_iter].allowed_peers_head;
4443 while (conn_iter != NULL)
4446 conn_iter = conn_iter->next;
4450 GNUNET_CONTAINER_multihashmap_size (pg->
4452 [pg_iter].allowed_peers);
4460 * From the set of connections possible, choose at least num connections per
4463 * @param pg the peergroup we are dealing with
4464 * @param num how many connections at least should each peer have (if possible)?
4467 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4470 struct MinimumContext minimum_ctx;
4472 struct PeerConnection *conn_iter;
4473 unsigned int temp_list_size;
4476 uint32_t random; /* Random list entry to connect peer to */
4481 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4484 = count_connections (pg->peers[pg_iter].connect_peers_head);
4485 if (temp_list_size == 0)
4487 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4488 "Peer %d has 0 connections!?!?\n", pg_iter);
4491 for (i = 0; i < num; i++)
4493 random = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4495 conn_iter = pg->peers[pg_iter].connect_peers_head;
4496 for (count = 0; count < random; count++)
4497 conn_iter = conn_iter->next;
4498 /* We now have a random connection, connect it! */
4499 GNUNET_assert(conn_iter != NULL);
4500 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4505 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4507 pg->peers[pg_iter].connect_peers_working_set =
4508 GNUNET_CONTAINER_multihashmap_create (num);
4511 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4513 minimum_ctx.first_uid = pg_iter;
4514 minimum_ctx.pg_array =
4515 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
4516 GNUNET_CONTAINER_multihashmap_size
4517 (pg->peers[pg_iter].connect_peers));
4518 minimum_ctx.first = &pg->peers[pg_iter];
4519 minimum_ctx.pg = pg;
4520 minimum_ctx.num_to_add = num;
4521 minimum_ctx.current = 0;
4522 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4523 &minimum_connect_iterator,
4527 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4529 /* Remove the "old" connections */
4530 GNUNET_CONTAINER_multihashmap_destroy (pg->
4531 peers[pg_iter].connect_peers);
4532 /* And replace with the working set */
4533 pg->peers[pg_iter].connect_peers =
4534 pg->peers[pg_iter].connect_peers_working_set;
4537 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4539 while (pg->peers[pg_iter].connect_peers_head != NULL)
4541 conn_iter = pg->peers[pg_iter].connect_peers_head;
4542 GNUNET_CONTAINER_DLL_remove(pg->peers[pg_iter].connect_peers_head,
4543 pg->peers[pg_iter].connect_peers_tail,
4545 GNUNET_free(conn_iter);
4546 /*remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT, GNUNET_YES);*/
4549 pg->peers[pg_iter].connect_peers_head
4550 = pg->peers[pg_iter].connect_peers_working_set_head;
4551 pg->peers[pg_iter].connect_peers_tail
4552 = pg->peers[pg_iter].connect_peers_working_set_tail;
4553 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4554 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4559 struct FindClosestContext
4562 * The currently known closest peer.
4564 struct GNUNET_TESTING_Daemon *closest;
4567 * The info for the peer we are adding connections for.
4569 struct PeerData *curr_peer;
4572 * The distance (bits) between the current
4573 * peer and the currently known closest.
4575 unsigned int closest_dist;
4578 * The offset of the closest known peer in
4581 unsigned int closest_num;
4585 * Iterator over hash map entries of the allowed
4586 * peer connections. Find the closest, not already
4587 * connected peer and return it.
4589 * @param cls closure (struct FindClosestContext)
4590 * @param key current key code (hash of offset in pg)
4591 * @param value value in the hash map - a GNUNET_TESTING_Daemon
4592 * @return GNUNET_YES if we should continue to
4597 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
4599 struct FindClosestContext *closest_ctx = cls;
4600 struct GNUNET_TESTING_Daemon *daemon = value;
4602 if (((closest_ctx->closest == NULL) ||
4603 (GNUNET_CRYPTO_hash_matching_bits
4604 (&daemon->id.hashPubKey,
4605 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
4606 closest_ctx->closest_dist))
4608 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
4609 curr_peer->connect_peers,
4612 closest_ctx->closest_dist =
4613 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
4614 &closest_ctx->curr_peer->daemon->
4616 closest_ctx->closest = daemon;
4617 uid_from_hash (key, &closest_ctx->closest_num);
4623 * From the set of connections possible, choose at num connections per
4624 * peer based on depth which are closest out of those allowed. Guaranteed
4625 * to add num peers to connect to, provided there are that many peers
4626 * in the underlay topology to connect to.
4628 * @param pg the peergroup we are dealing with
4629 * @param num how many connections at least should each peer have (if possible)?
4630 * @param proc processor to actually add the connections
4631 * @param list the peer list to use
4634 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
4635 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
4640 struct FindClosestContext closest_ctx;
4645 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
4647 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4649 closest_ctx.curr_peer = &pg->peers[pg_iter];
4650 closest_ctx.closest = NULL;
4651 closest_ctx.closest_dist = 0;
4652 closest_ctx.closest_num = 0;
4653 GNUNET_CONTAINER_multihashmap_iterate (pg->
4654 peers[pg_iter].allowed_peers,
4655 &find_closest_peers,
4657 if (closest_ctx.closest != NULL)
4659 GNUNET_assert (closest_ctx.closest_num < pg->total);
4660 proc (pg, pg_iter, closest_ctx.closest_num, list);
4668 * From the set of connections possible, choose at least num connections per
4669 * peer based on depth first traversal of peer connections. If DFS leaves
4670 * peers unconnected, ensure those peers get connections.
4672 * @param pg the peergroup we are dealing with
4673 * @param num how many connections at least should each peer have (if possible)?
4676 perform_dfs(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4680 uint32_t starting_peer;
4681 uint32_t least_connections;
4682 uint32_t random_connection;
4684 unsigned int temp_count;
4685 struct PeerConnection *peer_iter;
4687 struct DFSContext dfs_ctx;
4688 GNUNET_HashCode second_hash;
4694 while ((count_workingset_connections (pg) < num * pg->total)
4695 && (count_allowed_connections (pg) > 0))
4697 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4699 least_connections = -1; /* Set to very high number */
4700 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4703 = count_connections (
4704 pg->peers[pg_iter].connect_peers_working_set_head);
4705 if (temp_count < least_connections)
4707 starting_peer = pg_iter;
4708 least_connections = temp_count;
4714 = count_connections (pg->peers[starting_peer].connect_peers_head);
4715 if (temp_count == 0)
4716 continue; /* FIXME: infinite loop? */
4718 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4721 peer_iter = pg->peers[starting_peer].connect_peers_head;
4722 while (temp_count < random_connection)
4724 peer_iter = peer_iter->next;
4727 GNUNET_assert(peer_iter != NULL);
4728 add_connections (pg, starting_peer, peer_iter->index, WORKING_SET,
4730 remove_connections (pg, starting_peer, peer_iter->index, CONNECT,
4732 starting_peer = peer_iter->index;
4737 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4739 pg->peers[pg_iter].connect_peers_working_set =
4740 GNUNET_CONTAINER_multihashmap_create (num);
4745 while ((count_workingset_connections (pg) < num * pg->total)
4746 && (count_allowed_connections (pg) > 0))
4748 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4750 least_connections = -1; /* Set to very high number */
4751 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4753 if (GNUNET_CONTAINER_multihashmap_size
4754 (pg->peers[pg_iter].connect_peers_working_set) <
4757 starting_peer = pg_iter;
4759 GNUNET_CONTAINER_multihashmap_size (pg->
4761 [pg_iter].connect_peers_working_set);
4766 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4772 /* Choose a random peer from the chosen peers set of connections to add */
4774 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4775 GNUNET_CONTAINER_multihashmap_size
4776 (pg->peers[starting_peer].connect_peers));
4777 dfs_ctx.first_uid = starting_peer;
4778 dfs_ctx.first = &pg->peers[starting_peer];
4780 dfs_ctx.current = 0;
4782 GNUNET_CONTAINER_multihashmap_iterate (pg->
4784 [starting_peer].connect_peers,
4785 &dfs_connect_iterator, &dfs_ctx);
4786 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4787 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4788 GNUNET_assert (GNUNET_YES ==
4789 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4790 [starting_peer].connect_peers,
4794 [dfs_ctx.second_uid].daemon));
4795 starting_peer = dfs_ctx.second_uid;
4798 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4800 /* Remove the "old" connections */
4801 GNUNET_CONTAINER_multihashmap_destroy (pg->
4802 peers[pg_iter].connect_peers);
4803 /* And replace with the working set */
4804 pg->peers[pg_iter].connect_peers =
4805 pg->peers[pg_iter].connect_peers_working_set;
4811 * Internal callback for topology information for a particular peer.
4814 internal_topology_callback(void *cls, const struct GNUNET_PeerIdentity *peer,
4815 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4817 struct CoreContext *core_ctx = cls;
4818 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4820 if (peer == NULL) /* Either finished, or something went wrong */
4822 iter_ctx->completed++;
4823 iter_ctx->connected--;
4824 /* One core context allocated per iteration, must free! */
4825 GNUNET_free (core_ctx);
4829 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id, peer, NULL);
4832 if (iter_ctx->completed == iter_ctx->total)
4834 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4835 /* Once all are done, free the iteration context */
4836 GNUNET_free (iter_ctx);
4841 * Check running topology iteration tasks, if below max start a new one, otherwise
4842 * schedule for some time in the future.
4845 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4847 struct CoreContext *core_context = cls;
4848 struct TopologyIterateContext *topology_context =
4849 (struct TopologyIterateContext *) core_context->iter_context;
4850 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4853 if (topology_context->connected
4854 > topology_context->pg->max_outstanding_connections)
4856 #if VERBOSE_TESTING > 2
4857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4859 ("Delaying connect, we have too many outstanding connections!\n"));
4861 GNUNET_SCHEDULER_add_delayed (
4862 GNUNET_TIME_relative_multiply (
4863 GNUNET_TIME_UNIT_MILLISECONDS,
4865 &schedule_get_topology, core_context);
4869 #if VERBOSE_TESTING > 2
4870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4871 _("Creating connection, outstanding_connections is %d\n"),
4872 outstanding_connects);
4874 topology_context->connected++;
4876 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4877 &internal_topology_callback,
4880 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4881 internal_topology_callback (core_context, NULL, NULL);
4887 * Iterate over all (running) peers in the peer group, retrieve
4888 * all connections that each currently has.
4891 GNUNET_TESTING_get_topology(struct GNUNET_TESTING_PeerGroup *pg,
4892 GNUNET_TESTING_NotifyTopology cb, void *cls)
4894 struct TopologyIterateContext *topology_context;
4895 struct CoreContext *core_ctx;
4897 unsigned int total_count;
4899 /* Allocate a single topology iteration context */
4900 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4901 topology_context->topology_cb = cb;
4902 topology_context->cls = cls;
4903 topology_context->pg = pg;
4905 for (i = 0; i < pg->total; i++)
4907 if (pg->peers[i].daemon->running == GNUNET_YES)
4909 /* Allocate one core context per core we need to connect to */
4910 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4911 core_ctx->daemon = pg->peers[i].daemon;
4912 /* Set back pointer to topology iteration context */
4913 core_ctx->iter_context = topology_context;
4914 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4918 if (total_count == 0)
4920 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4921 GNUNET_free (topology_context);
4924 topology_context->total = total_count;
4929 * Callback function to process statistic values.
4930 * This handler is here only really to insert a peer
4931 * identity (or daemon) so the statistics can be uniquely
4932 * tied to a single running peer.
4934 * @param cls closure
4935 * @param subsystem name of subsystem that created the statistic
4936 * @param name the name of the datum
4937 * @param value the current value
4938 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4939 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4942 internal_stats_callback(void *cls, const char *subsystem, const char *name,
4943 uint64_t value, int is_persistent)
4945 struct StatsCoreContext *core_context = cls;
4946 struct StatsIterateContext *stats_context =
4947 (struct StatsIterateContext *) core_context->iter_context;
4949 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4950 subsystem, name, value, is_persistent);
4954 * Internal continuation call for statistics iteration.
4956 * @param cls closure, the CoreContext for this iteration
4957 * @param success whether or not the statistics iterations
4958 * was canceled or not (we don't care)
4961 internal_stats_cont(void *cls, int success)
4963 struct StatsCoreContext *core_context = cls;
4964 struct StatsIterateContext *stats_context =
4965 (struct StatsIterateContext *) core_context->iter_context;
4967 stats_context->connected--;
4968 stats_context->completed++;
4970 if (stats_context->completed == stats_context->total)
4972 stats_context->cont (stats_context->cls, GNUNET_YES);
4973 GNUNET_free (stats_context);
4976 if (core_context->stats_handle != NULL)
4977 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4979 GNUNET_free (core_context);
4983 * Check running topology iteration tasks, if below max start a new one, otherwise
4984 * schedule for some time in the future.
4987 schedule_get_statistics(void *cls,
4988 const struct GNUNET_SCHEDULER_TaskContext *tc)
4990 struct StatsCoreContext *core_context = cls;
4991 struct StatsIterateContext *stats_context =
4992 (struct StatsIterateContext *) core_context->iter_context;
4994 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4997 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
4999 #if VERBOSE_TESTING > 2
5000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5002 ("Delaying connect, we have too many outstanding connections!\n"));
5004 GNUNET_SCHEDULER_add_delayed (
5005 GNUNET_TIME_relative_multiply (
5006 GNUNET_TIME_UNIT_MILLISECONDS,
5008 &schedule_get_statistics, core_context);
5012 #if VERBOSE_TESTING > 2
5013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5014 _("Creating connection, outstanding_connections is %d\n"),
5015 outstanding_connects);
5018 stats_context->connected++;
5019 core_context->stats_handle
5020 = GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
5021 if (core_context->stats_handle == NULL)
5023 internal_stats_cont (core_context, GNUNET_NO);
5027 core_context->stats_get_handle
5028 = GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
5029 GNUNET_TIME_relative_get_forever (),
5030 &internal_stats_cont,
5031 &internal_stats_callback, core_context);
5032 if (core_context->stats_get_handle == NULL)
5033 internal_stats_cont (core_context, GNUNET_NO);
5038 struct DuplicateStats
5041 * Next item in the list
5043 struct DuplicateStats *next;
5046 * Nasty string, concatenation of relevant information.
5048 char *unique_string;
5052 * Check whether the combination of port/host/unix domain socket
5053 * already exists in the list of peers being checked for statistics.
5055 * @param pg the peergroup in question
5056 * @param specific_peer the peer we're concerned with
5057 * @param stats_list the list to return to the caller
5059 * @return GNUNET_YES if the statistics instance has been seen already,
5060 * GNUNET_NO if not (and we may have added it to the list)
5063 stats_check_existing(struct GNUNET_TESTING_PeerGroup *pg,
5064 struct PeerData *specific_peer,
5065 struct DuplicateStats **stats_list)
5067 struct DuplicateStats *pos;
5068 char *unix_domain_socket;
5069 unsigned long long port;
5072 != GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
5073 "single_statistics_per_host"))
5074 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
5077 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg,
5080 &unix_domain_socket))
5083 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg,
5084 "statistics", "port",
5087 GNUNET_free(unix_domain_socket);
5091 if (specific_peer->daemon->hostname != NULL)
5092 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
5093 unix_domain_socket, port);
5095 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
5099 if (0 == strcmp (to_match, pos->unique_string))
5101 GNUNET_free (unix_domain_socket);
5102 GNUNET_free (to_match);
5107 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
5108 pos->unique_string = to_match;
5109 pos->next = *stats_list;
5111 GNUNET_free (unix_domain_socket);
5116 * Iterate over all (running) peers in the peer group, retrieve
5117 * all statistics from each.
5120 GNUNET_TESTING_get_statistics(struct GNUNET_TESTING_PeerGroup *pg,
5121 GNUNET_STATISTICS_Callback cont,
5122 GNUNET_TESTING_STATISTICS_Iterator proc,
5125 struct StatsIterateContext *stats_context;
5126 struct StatsCoreContext *core_ctx;
5128 unsigned int total_count;
5129 struct DuplicateStats *stats_list;
5130 struct DuplicateStats *pos;
5133 /* Allocate a single stats iteration context */
5134 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
5135 stats_context->cont = cont;
5136 stats_context->proc = proc;
5137 stats_context->cls = cls;
5138 stats_context->pg = pg;
5141 for (i = 0; i < pg->total; i++)
5143 if ((pg->peers[i].daemon->running == GNUNET_YES) && (GNUNET_NO
5144 == stats_check_existing (pg, &pg->peers[i], &stats_list)))
5146 /* Allocate one core context per core we need to connect to */
5147 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
5148 core_ctx->daemon = pg->peers[i].daemon;
5149 /* Set back pointer to topology iteration context */
5150 core_ctx->iter_context = stats_context;
5151 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
5156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5157 "Retrieving stats from %u total instances.\n", total_count);
5158 stats_context->total = total_count;
5159 if (stats_list != NULL)
5164 GNUNET_free (pos->unique_string);
5165 stats_list = pos->next;
5167 pos = stats_list->next;
5174 * Stop the connection process temporarily.
5176 * @param pg the peer group to stop connecting
5179 GNUNET_TESTING_stop_connections(struct GNUNET_TESTING_PeerGroup *pg)
5181 pg->stop_connects = GNUNET_YES;
5185 * Resume the connection process temporarily.
5187 * @param pg the peer group to resume connecting
5190 GNUNET_TESTING_resume_connections(struct GNUNET_TESTING_PeerGroup *pg)
5192 pg->stop_connects = GNUNET_NO;
5196 * There are many ways to connect peers that are supported by this function.
5197 * To connect peers in the same topology that was created via the
5198 * GNUNET_TESTING_create_topology, the topology variable must be set to
5199 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
5200 * a new instance of that topology will be generated and attempted to be
5201 * connected. This could result in some connections being impossible,
5202 * because some topologies are non-deterministic.
5204 * @param pg the peer group struct representing the running peers
5205 * @param topology which topology to connect the peers in
5206 * @param options options for connecting the topology
5207 * @param option_modifier modifier for options that take a parameter
5208 * @param connect_timeout how long to wait before giving up on connecting
5210 * @param connect_attempts how many times to attempt to connect two peers
5211 * over the connect_timeout duration
5212 * @param notify_callback notification to be called once all connections completed
5213 * @param notify_cls closure for notification callback
5215 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
5218 GNUNET_TESTING_connect_topology(
5219 struct GNUNET_TESTING_PeerGroup *pg,
5220 enum GNUNET_TESTING_Topology topology,
5221 enum GNUNET_TESTING_TopologyOption options,
5222 double option_modifier,
5223 struct GNUNET_TIME_Relative connect_timeout,
5224 unsigned int connect_attempts,
5225 GNUNET_TESTING_NotifyCompletion notify_callback,
5230 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
5231 #if VERBOSE_TOPOLOGY
5232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5233 _("Creating clique CONNECT topology\n"));
5235 create_clique (pg, &add_connections, CONNECT, GNUNET_NO);
5237 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
5238 #if VERBOSE_TOPOLOGY
5239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5240 _("Creating small world (ring) CONNECT topology\n"));
5242 create_small_world_ring (pg, &add_connections, CONNECT);
5244 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
5245 #if VERBOSE_TOPOLOGY
5246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5247 _("Creating small world (2d-torus) CONNECT topology\n"));
5249 create_small_world (pg, &add_connections, CONNECT);
5251 case GNUNET_TESTING_TOPOLOGY_RING:
5252 #if VERBOSE_TOPOLOGY
5253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring CONNECT topology\n"));
5255 create_ring (pg, &add_connections, CONNECT);
5257 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
5258 #if VERBOSE_TOPOLOGY
5259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5260 _("Creating 2d torus CONNECT topology\n"));
5262 create_2d_torus (pg, &add_connections, CONNECT);
5264 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
5265 #if VERBOSE_TOPOLOGY
5266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5267 _("Creating Erdos-Renyi CONNECT topology\n"));
5269 create_erdos_renyi (pg, &add_connections, CONNECT);
5271 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
5272 #if VERBOSE_TOPOLOGY
5273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5274 _("Creating InterNAT CONNECT topology\n"));
5276 create_nated_internet (pg, &add_connections, CONNECT);
5278 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
5279 #if VERBOSE_TOPOLOGY
5280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5281 _("Creating Scale Free CONNECT topology\n"));
5283 create_scale_free (pg, &add_connections, CONNECT);
5285 case GNUNET_TESTING_TOPOLOGY_LINE:
5286 #if VERBOSE_TOPOLOGY
5287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5288 _("Creating straight line CONNECT topology\n"));
5290 create_line (pg, &add_connections, CONNECT);
5292 case GNUNET_TESTING_TOPOLOGY_NONE:
5293 #if VERBOSE_TOPOLOGY
5294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating no CONNECT topology\n"));
5296 copy_allowed_topology (pg);
5299 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5300 ("Unknown topology specification, can't connect peers!\n"));
5301 return GNUNET_SYSERR;
5306 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
5307 #if VERBOSE_TOPOLOGY
5308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5309 ("Connecting random subset (%'.2f percent) of possible peers\n"), 100
5312 choose_random_connections (pg, option_modifier);
5314 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
5315 #if VERBOSE_TOPOLOGY
5316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5317 _("Connecting a minimum of %u peers each (if possible)\n"),
5318 (unsigned int) option_modifier);
5320 choose_minimum (pg, (unsigned int) option_modifier);
5322 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
5323 #if VERBOSE_TOPOLOGY
5324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5325 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
5326 (unsigned int) option_modifier);
5329 perform_dfs (pg, (int) option_modifier);
5332 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
5333 #if VERBOSE_TOPOLOGY
5334 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5335 ("Finding additional %u closest peers each (if possible)\n"),
5336 (unsigned int) option_modifier);
5339 add_closest (pg, (unsigned int) option_modifier,
5340 &add_connections, CONNECT);
5343 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
5345 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
5351 return connect_topology (pg, connect_timeout, connect_attempts,
5352 notify_callback, notify_cls);
5356 * Lookup and return the number of SSH connections to a host.
5358 * @param hostname the hostname to lookup in the list
5359 * @param pg the peergroup that the host belongs to
5361 * @return the number of current ssh connections to the host
5364 count_outstanding_at_host(const char *hostname,
5365 struct GNUNET_TESTING_PeerGroup *pg)
5367 struct OutstandingSSH *pos;
5369 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5371 GNUNET_assert(pos != NULL);
5372 return pos->outstanding;
5376 * Increment the number of SSH connections to a host by one.
5378 * @param hostname the hostname to lookup in the list
5379 * @param pg the peergroup that the host belongs to
5383 increment_outstanding_at_host(const char *hostname,
5384 struct GNUNET_TESTING_PeerGroup *pg)
5386 struct OutstandingSSH *pos;
5388 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5390 GNUNET_assert(pos != NULL);
5395 * Decrement the number of SSH connections to a host by one.
5397 * @param hostname the hostname to lookup in the list
5398 * @param pg the peergroup that the host belongs to
5402 decrement_outstanding_at_host(const char *hostname,
5403 struct GNUNET_TESTING_PeerGroup *pg)
5405 struct OutstandingSSH *pos;
5407 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5409 GNUNET_assert(pos != NULL);
5414 * Callback that is called whenever a hostkey is generated
5415 * for a peer. Call the real callback and decrement the
5416 * starting counter for the peergroup.
5418 * @param cls closure
5419 * @param id identifier for the daemon, NULL on error
5420 * @param d handle for the daemon
5421 * @param emsg error message (NULL on success)
5424 internal_hostkey_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5425 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5427 struct InternalStartContext *internal_context = cls;
5428 internal_context->peer->pg->starting--;
5429 internal_context->peer->pg->started++;
5430 if (internal_context->hostname != NULL)
5431 decrement_outstanding_at_host (internal_context->hostname,
5432 internal_context->peer->pg);
5433 if (internal_context->hostkey_callback != NULL)
5434 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
5436 else if (internal_context->peer->pg->started
5437 == internal_context->peer->pg->total)
5439 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
5440 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
5445 * Callback that is called whenever a peer has finished starting.
5446 * Call the real callback and decrement the starting counter
5447 * for the peergroup.
5449 * @param cls closure
5450 * @param id identifier for the daemon, NULL on error
5452 * @param d handle for the daemon
5453 * @param emsg error message (NULL on success)
5456 internal_startup_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5457 const struct GNUNET_CONFIGURATION_Handle *cfg,
5458 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5460 struct InternalStartContext *internal_context = cls;
5461 internal_context->peer->pg->starting--;
5462 if (internal_context->hostname != NULL)
5463 decrement_outstanding_at_host (internal_context->hostname,
5464 internal_context->peer->pg);
5465 if (internal_context->start_cb != NULL)
5466 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
5471 internal_continue_startup(void *cls,
5472 const struct GNUNET_SCHEDULER_TaskContext *tc)
5474 struct InternalStartContext *internal_context = cls;
5476 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
5481 if ((internal_context->peer->pg->starting
5482 < internal_context->peer->pg->max_concurrent_ssh)
5483 || ((internal_context->hostname != NULL)
5484 && (count_outstanding_at_host (internal_context->hostname,
5485 internal_context->peer->pg)
5486 < internal_context->peer->pg->max_concurrent_ssh)))
5488 if (internal_context->hostname != NULL)
5489 increment_outstanding_at_host (internal_context->hostname,
5490 internal_context->peer->pg);
5491 internal_context->peer->pg->starting++;
5492 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
5496 GNUNET_SCHEDULER_add_delayed (
5497 GNUNET_TIME_relative_multiply (
5498 GNUNET_TIME_UNIT_MILLISECONDS,
5500 &internal_continue_startup,
5506 * Callback for informing us about a successful
5507 * or unsuccessful churn start call.
5509 * @param cls a ChurnContext
5510 * @param id the peer identity of the started peer
5511 * @param cfg the handle to the configuration of the peer
5512 * @param d handle to the daemon for the peer
5513 * @param emsg NULL on success, non-NULL on failure
5517 churn_start_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5518 const struct GNUNET_CONFIGURATION_Handle *cfg,
5519 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5521 struct ChurnRestartContext *startup_ctx = cls;
5522 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
5524 unsigned int total_left;
5525 char *error_message;
5527 error_message = NULL;
5530 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5531 "Churn stop callback failed with error `%s'\n", emsg);
5532 churn_ctx->num_failed_start++;
5536 churn_ctx->num_to_start--;
5540 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5541 "Started peer, %d left.\n", churn_ctx->num_to_start);
5544 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
5545 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5547 if (total_left == 0)
5549 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
5552 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5553 churn_ctx->num_failed_start,
5554 churn_ctx->num_failed_stop);
5555 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5556 GNUNET_free_non_null (error_message);
5557 GNUNET_free (churn_ctx);
5558 GNUNET_free (startup_ctx);
5563 schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5565 struct PeerRestartContext *peer_restart_ctx = cls;
5566 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
5568 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5569 GNUNET_SCHEDULER_add_delayed (
5570 GNUNET_TIME_relative_multiply (
5571 GNUNET_TIME_UNIT_MILLISECONDS,
5573 &schedule_churn_restart, peer_restart_ctx);
5576 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
5577 startup_ctx->timeout,
5578 &churn_start_callback, startup_ctx);
5579 GNUNET_free (peer_restart_ctx);
5584 internal_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5586 struct InternalStartContext *internal_context = cls;
5588 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
5593 if ((internal_context->peer->pg->starting
5594 < internal_context->peer->pg->max_concurrent_ssh)
5595 || ((internal_context->hostname != NULL)
5596 && (count_outstanding_at_host (internal_context->hostname,
5597 internal_context->peer->pg)
5598 < internal_context->peer->pg->max_concurrent_ssh)))
5600 if (internal_context->hostname != NULL)
5601 increment_outstanding_at_host (internal_context->hostname,
5602 internal_context->peer->pg);
5603 internal_context->peer->pg->starting++;
5604 internal_context->peer->daemon
5605 = GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
5606 internal_context->timeout,
5607 internal_context->hostname,
5608 internal_context->username,
5609 internal_context->sshport,
5610 internal_context->hostkey,
5611 &internal_hostkey_callback,
5613 &internal_startup_callback,
5618 GNUNET_SCHEDULER_add_delayed (
5619 GNUNET_TIME_relative_multiply (
5620 GNUNET_TIME_UNIT_MILLISECONDS,
5622 &internal_start, internal_context);
5627 * Function which continues a peer group starting up
5628 * after successfully generating hostkeys for each peer.
5630 * @param pg the peer group to continue starting
5634 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
5639 for (i = 0; i < pg->total; i++)
5641 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5642 &pg->peers[i].internal_context);
5643 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
5648 * Start count gnunet instances with the same set of transports and
5649 * applications. The port numbers (any option called "PORT") will be
5650 * adjusted to ensure that no two peers running on the same system
5651 * have the same port(s) in their respective configurations.
5653 * @param cfg configuration template to use
5654 * @param total number of daemons to start
5655 * @param max_concurrent_connections for testing, how many peers can
5656 * we connect to simultaneously
5657 * @param max_concurrent_ssh when starting with ssh, how many ssh
5658 * connections will we allow at once (based on remote hosts allowed!)
5659 * @param timeout total time allowed for peers to start
5660 * @param hostkey_callback function to call on each peers hostkey generation
5661 * if NULL, peers will be started by this call, if non-null,
5662 * GNUNET_TESTING_daemons_continue_startup must be called after
5663 * successful hostkey generation
5664 * @param hostkey_cls closure for hostkey callback
5665 * @param cb function to call on each daemon that was started
5666 * @param cb_cls closure for cb
5667 * @param connect_callback function to call each time two hosts are connected
5668 * @param connect_callback_cls closure for connect_callback
5669 * @param hostnames linked list of host structs to use to start peers on
5670 * (NULL to run on localhost only)
5672 * @return NULL on error, otherwise handle to control peer group
5674 struct GNUNET_TESTING_PeerGroup *
5675 GNUNET_TESTING_daemons_start(
5676 const struct GNUNET_CONFIGURATION_Handle *cfg,
5678 unsigned int max_concurrent_connections,
5679 unsigned int max_concurrent_ssh,
5680 struct GNUNET_TIME_Relative timeout,
5681 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
5683 GNUNET_TESTING_NotifyDaemonRunning cb,
5685 GNUNET_TESTING_NotifyConnection connect_callback,
5686 void *connect_callback_cls,
5687 const struct GNUNET_TESTING_Host *hostnames)
5689 struct GNUNET_TESTING_PeerGroup *pg;
5690 const struct GNUNET_TESTING_Host *hostpos;
5696 const char *hostname;
5697 const char *username;
5698 char *baseservicehome;
5699 char *newservicehome;
5701 char *hostkeys_file;
5704 struct GNUNET_DISK_FileHandle *fd;
5705 struct GNUNET_CONFIGURATION_Handle *pcfg;
5707 struct OutstandingSSH *ssh_entry;
5708 unsigned int hostcnt;
5715 uint64_t total_hostkeys;
5716 struct GNUNET_OS_Process *proc;
5726 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
5728 pg->notify_connection = connect_callback;
5729 pg->notify_connection_cls = connect_callback_cls;
5731 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
5732 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
5733 pg->max_outstanding_connections = max_concurrent_connections;
5734 pg->max_concurrent_ssh = max_concurrent_ssh;
5735 if (NULL != hostnames)
5738 hostpos = hostnames;
5739 while (hostpos != NULL)
5741 hostpos = hostpos->next;
5744 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5747 hostpos = hostnames;
5748 while (hostpos != NULL)
5750 pg->hosts[off].minport = LOW_PORT;
5751 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
5752 if (hostpos->username != NULL)
5753 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
5754 pg->hosts[off].sshport = hostpos->port;
5755 hostpos = hostpos->next;
5765 pg->num_hosts = off;
5769 /* skip leading spaces */
5770 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
5773 while ('\0' != *rpos)
5775 if (isspace ((unsigned char) *rpos))
5779 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5781 start = GNUNET_strdup (hostnames);
5783 while ('\0' != *pos)
5785 if (isspace ((unsigned char) *pos))
5788 if (strlen (start) > 0)
5790 pg->hosts[off].minport = LOW_PORT;
5791 pg->hosts[off++].hostname = start;
5797 if (strlen (start) > 0)
5799 pg->hosts[off].minport = LOW_PORT;
5800 pg->hosts[off++].hostname = start;
5804 GNUNET_free (start);
5805 GNUNET_free (pg->hosts);
5809 minport = 0; /* make gcc happy */
5818 /* Create the servicehome directory for each remote peer */
5819 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
5821 for (i = 0; i < pg->num_hosts; i++)
5823 ssh_entry = GNUNET_malloc(sizeof(struct OutstandingSSH));
5824 ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
5825 GNUNET_CONTAINER_DLL_insert(pg->ssh_head, pg->ssh_tail, ssh_entry);
5826 GNUNET_asprintf(&tmpdir, "%s/%s", baseservicehome, pg->hosts[i].hostname);
5827 if (NULL != pg->hosts[i].username)
5828 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username,
5829 pg->hosts[i].hostname);
5831 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
5832 if (pg->hosts[i].sshport != 0)
5834 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
5835 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-P",
5840 arg, "mkdir -p", tmpdir,
5844 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5845 "mkdir -p", tmpdir, NULL);
5846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5847 "Creating remote dir with command ssh %s %s %s\n", arg,
5848 " mkdir -p ", tmpdir);
5849 GNUNET_free(tmpdir);
5851 GNUNET_OS_process_wait (proc);
5852 GNUNET_OS_process_close(proc);
5854 GNUNET_free(baseservicehome);
5856 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING",
5860 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
5861 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!\n");
5864 /* Check hostkey file size, read entire thing into memory */
5865 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
5866 GNUNET_DISK_PERM_NONE);
5869 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
5873 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs,
5878 GNUNET_ERROR_TYPE_WARNING,
5879 "Found file size %llu for hostkeys, expect hostkeys to be size %d\n",
5880 fs, HOSTKEYFILESIZE);
5882 if (fs % HOSTKEYFILESIZE != 0)
5884 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5885 "File size %llu seems incorrect for hostkeys...\n",
5890 total_hostkeys = fs / HOSTKEYFILESIZE;
5891 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5892 "Will read %llu hostkeys from file\n", total_hostkeys);
5893 pg->hostkey_data = GNUNET_malloc_large (fs);
5894 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
5895 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
5898 GNUNET_free(hostkeys_file);
5901 for (off = 0; off < total; off++)
5905 hostname = pg->hosts[off % hostcnt].hostname;
5906 username = pg->hosts[off % hostcnt].username;
5907 sshport = pg->hosts[off % hostcnt].sshport;
5908 pcfg = make_config (cfg, off, &pg->hosts[off % hostcnt].minport,
5909 &upnum, hostname, &fdnum);
5916 pcfg = make_config (cfg, off, &minport, &upnum, hostname, &fdnum);
5921 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5922 ("Could not create configuration for peer number %u on `%s'!\n"),
5923 off, hostname == NULL ? "localhost" : hostname);
5928 == GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS",
5932 if (hostname != NULL)
5933 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", baseservicehome, hostname, off);
5935 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
5936 GNUNET_free (baseservicehome);
5940 tmpdir = getenv ("TMPDIR");
5941 tmpdir = tmpdir ? tmpdir : "/tmp";
5942 if (hostname != NULL)
5943 GNUNET_asprintf (&newservicehome, "%s/%s/%s/%d/", tmpdir, hostname,
5944 "gnunet-testing-test-test", off);
5946 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", tmpdir,
5947 "gnunet-testing-test-test", off);
5949 GNUNET_CONFIGURATION_set_value_string (pcfg, "PATHS", "SERVICEHOME",
5951 GNUNET_free (newservicehome);
5952 pg->peers[off].cfg = pcfg;
5954 /* Can we do this later? */
5955 pg->peers[off].allowed_peers =
5956 GNUNET_CONTAINER_multihashmap_create (total);
5957 pg->peers[off].connect_peers =
5958 GNUNET_CONTAINER_multihashmap_create (total);
5959 pg->peers[off].blacklisted_peers =
5960 GNUNET_CONTAINER_multihashmap_create (total);
5963 pg->peers[off].pg = pg;
5964 pg->peers[off].internal_context.peer = &pg->peers[off];
5965 pg->peers[off].internal_context.timeout = timeout;
5966 pg->peers[off].internal_context.hostname = hostname;
5967 pg->peers[off].internal_context.username = username;
5968 pg->peers[off].internal_context.sshport = sshport;
5969 if (pg->hostkey_data != NULL)
5970 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off
5972 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
5973 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
5974 pg->peers[off].internal_context.start_cb = cb;
5975 pg->peers[off].internal_context.start_cb_cls = cb_cls;
5977 GNUNET_SCHEDULER_add_now (&internal_start,
5978 &pg->peers[off].internal_context);
5985 * Get a daemon by number, so callers don't have to do nasty
5986 * offsetting operation.
5988 struct GNUNET_TESTING_Daemon *
5989 GNUNET_TESTING_daemon_get(struct GNUNET_TESTING_PeerGroup *pg,
5990 unsigned int position)
5992 if (position < pg->total)
5993 return pg->peers[position].daemon;
5999 * Get a daemon by peer identity, so callers can
6000 * retrieve the daemon without knowing it's offset.
6002 * @param pg the peer group to retrieve the daemon from
6003 * @param peer_id the peer identity of the daemon to retrieve
6005 * @return the daemon on success, or NULL if no such peer identity is found
6007 struct GNUNET_TESTING_Daemon *
6008 GNUNET_TESTING_daemon_get_by_id(struct GNUNET_TESTING_PeerGroup *pg,
6009 struct GNUNET_PeerIdentity *peer_id)
6013 for (i = 0; i < pg->total; i++)
6015 if (0 == memcmp (&pg->peers[i].daemon->id, peer_id,
6016 sizeof(struct GNUNET_PeerIdentity)))
6017 return pg->peers[i].daemon;
6024 * Prototype of a function that will be called when a
6025 * particular operation was completed the testing library.
6027 * @param cls closure (a struct RestartContext)
6028 * @param id id of the peer that was restarted
6029 * @param cfg handle to the configuration of the peer
6030 * @param d handle to the daemon that was restarted
6031 * @param emsg NULL on success
6034 restart_callback(void *cls, const struct GNUNET_PeerIdentity *id,
6035 const struct GNUNET_CONFIGURATION_Handle *cfg,
6036 struct GNUNET_TESTING_Daemon *d, const char *emsg)
6038 struct RestartContext *restart_context = cls;
6042 restart_context->peers_restarted++;
6046 restart_context->peers_restart_failed++;
6049 if (restart_context->peers_restarted == restart_context->peer_group->total)
6051 restart_context->callback (restart_context->callback_cls, NULL);
6052 GNUNET_free (restart_context);
6054 else if (restart_context->peers_restart_failed
6055 + restart_context->peers_restarted == restart_context->peer_group->total)
6057 restart_context->callback (restart_context->callback_cls,
6058 "Failed to restart peers!");
6059 GNUNET_free (restart_context);
6065 * Callback for informing us about a successful
6066 * or unsuccessful churn stop call.
6068 * @param cls a ChurnContext
6069 * @param emsg NULL on success, non-NULL on failure
6073 churn_stop_callback(void *cls, const char *emsg)
6075 struct ShutdownContext *shutdown_ctx = cls;
6076 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
6077 unsigned int total_left;
6078 char *error_message;
6080 error_message = NULL;
6081 shutdown_ctx->outstanding--;
6085 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6086 "Churn stop callback failed with error `%s'\n", emsg);
6087 churn_ctx->num_failed_stop++;
6091 churn_ctx->num_to_stop--;
6095 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6096 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
6098 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
6099 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
6101 if (total_left == 0)
6103 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
6107 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
6108 churn_ctx->num_failed_start,
6109 churn_ctx->num_failed_stop);
6111 churn_ctx->cb (churn_ctx->cb_cls, error_message);
6112 GNUNET_free_non_null (error_message);
6113 GNUNET_free (churn_ctx);
6114 GNUNET_free (shutdown_ctx);
6119 * Count the number of running peers.
6121 * @param pg handle for the peer group
6123 * @return the number of currently running peers in the peer group
6126 GNUNET_TESTING_daemons_running(struct GNUNET_TESTING_PeerGroup *pg)
6129 unsigned int running = 0;
6130 for (i = 0; i < pg->total; i++)
6132 if (pg->peers[i].daemon->running == GNUNET_YES)
6134 GNUNET_assert (running != -1);
6142 * Task to rate limit the number of outstanding peer shutdown
6143 * requests. This is necessary for making sure we don't do
6144 * too many ssh connections at once, but is generally nicer
6145 * to any system as well (graduated task starts, as opposed
6146 * to calling gnunet-arm N times all at once).
6149 schedule_churn_shutdown_task(void *cls,
6150 const struct GNUNET_SCHEDULER_TaskContext *tc)
6152 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6153 struct ShutdownContext *shutdown_ctx;
6154 struct ChurnContext *churn_ctx;
6155 GNUNET_assert (peer_shutdown_ctx != NULL);
6156 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6157 GNUNET_assert (shutdown_ctx != NULL);
6158 churn_ctx = (struct ChurnContext *) shutdown_ctx->cb_cls;
6159 if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
6160 GNUNET_SCHEDULER_add_delayed (
6161 GNUNET_TIME_relative_multiply (
6162 GNUNET_TIME_UNIT_MILLISECONDS,
6164 &schedule_churn_shutdown_task,
6168 shutdown_ctx->outstanding++;
6169 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6170 shutdown_ctx->timeout, shutdown_ctx->cb,
6171 shutdown_ctx, GNUNET_NO, GNUNET_YES);
6172 GNUNET_free (peer_shutdown_ctx);
6177 * Simulate churn by stopping some peers (and possibly
6178 * re-starting others if churn is called multiple times). This
6179 * function can only be used to create leave-join churn (peers "never"
6180 * leave for good). First "voff" random peers that are currently
6181 * online will be taken offline; then "von" random peers that are then
6182 * offline will be put back online. No notifications will be
6183 * generated for any of these operations except for the callback upon
6186 * @param pg handle for the peer group
6187 * @param voff number of peers that should go offline
6188 * @param von number of peers that should come back online;
6189 * must be zero on first call (since "testbed_start"
6190 * always starts all of the peers)
6191 * @param timeout how long to wait for operations to finish before
6193 * @param cb function to call at the end
6194 * @param cb_cls closure for cb
6197 GNUNET_TESTING_daemons_churn(struct GNUNET_TESTING_PeerGroup *pg,
6198 unsigned int voff, unsigned int von,
6199 struct GNUNET_TIME_Relative timeout,
6200 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6202 struct ChurnContext *churn_ctx;
6203 struct ShutdownContext *shutdown_ctx;
6204 struct PeerShutdownContext *peer_shutdown_ctx;
6205 struct PeerRestartContext *peer_restart_ctx;
6206 struct ChurnRestartContext *churn_startup_ctx;
6208 unsigned int running;
6209 unsigned int stopped;
6210 unsigned int total_running;
6211 unsigned int total_stopped;
6213 unsigned int *running_arr;
6214 unsigned int *stopped_arr;
6215 unsigned int *running_permute;
6216 unsigned int *stopped_permute;
6221 if ((von == 0) && (voff == 0)) /* No peers at all? */
6227 for (i = 0; i < pg->total; i++)
6229 if (pg->peers[i].daemon->running == GNUNET_YES)
6231 GNUNET_assert (running != -1);
6236 GNUNET_assert (stopped != -1);
6243 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6244 "Trying to stop more peers than are currently running!\n");
6245 cb (cb_cls, "Trying to stop more peers than are currently running!");
6251 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6252 "Trying to start more peers than are currently stopped!\n");
6253 cb (cb_cls, "Trying to start more peers than are currently stopped!");
6257 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6261 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
6265 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
6267 running_permute = NULL;
6268 stopped_permute = NULL;
6271 running_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6274 stopped_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6277 total_running = running;
6278 total_stopped = stopped;
6282 churn_ctx->num_to_start = von;
6283 churn_ctx->num_to_stop = voff;
6285 churn_ctx->cb_cls = cb_cls;
6288 for (i = 0; i < pg->total; i++)
6290 if (pg->peers[i].daemon->running == GNUNET_YES)
6292 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6293 running_arr[running] = i;
6298 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6299 stopped_arr[stopped] = i;
6304 GNUNET_assert (running >= voff);
6307 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6308 shutdown_ctx->cb = &churn_stop_callback;
6309 shutdown_ctx->cb_cls = churn_ctx;
6310 shutdown_ctx->total_peers = voff;
6311 shutdown_ctx->timeout = timeout;
6314 for (i = 0; i < voff; i++)
6317 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
6318 running_permute[i]);
6320 GNUNET_assert (running_arr != NULL);
6321 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6322 peer_shutdown_ctx->daemon
6323 = pg->peers[running_arr[running_permute[i]]].daemon;
6324 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6325 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
6329 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
6331 &churn_stop_callback, churn_ctx,
6332 GNUNET_NO, GNUNET_YES); */
6335 GNUNET_assert (stopped >= von);
6338 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6339 churn_startup_ctx->churn_ctx = churn_ctx;
6340 churn_startup_ctx->timeout = timeout;
6341 churn_startup_ctx->pg = pg;
6343 for (i = 0; i < von; i++)
6346 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
6347 stopped_permute[i]);
6349 GNUNET_assert (stopped_arr != NULL);
6350 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
6351 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
6352 peer_restart_ctx->daemon
6353 = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
6354 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
6356 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
6357 timeout, &churn_start_callback, churn_ctx); */
6360 GNUNET_free_non_null (running_arr);
6361 GNUNET_free_non_null (stopped_arr);
6362 GNUNET_free_non_null (running_permute);
6363 GNUNET_free_non_null (stopped_permute);
6367 * Restart all peers in the given group.
6369 * @param pg the handle to the peer group
6370 * @param callback function to call on completion (or failure)
6371 * @param callback_cls closure for the callback function
6374 GNUNET_TESTING_daemons_restart(struct GNUNET_TESTING_PeerGroup *pg,
6375 GNUNET_TESTING_NotifyCompletion callback,
6378 struct RestartContext *restart_context;
6383 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
6384 restart_context->peer_group = pg;
6385 restart_context->peers_restarted = 0;
6386 restart_context->callback = callback;
6387 restart_context->callback_cls = callback_cls;
6389 for (off = 0; off < pg->total; off++)
6391 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
6392 &restart_callback, restart_context);
6398 * Start or stop an individual peer from the given group.
6400 * @param pg handle to the peer group
6401 * @param offset which peer to start or stop
6402 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
6403 * @param timeout how long to wait for shutdown
6404 * @param cb function to call at the end
6405 * @param cb_cls closure for cb
6408 GNUNET_TESTING_daemons_vary(struct GNUNET_TESTING_PeerGroup *pg,
6409 unsigned int offset, int desired_status,
6410 struct GNUNET_TIME_Relative timeout,
6411 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6413 struct ShutdownContext *shutdown_ctx;
6414 struct ChurnRestartContext *startup_ctx;
6415 struct ChurnContext *churn_ctx;
6417 if (GNUNET_NO == desired_status)
6419 if (NULL != pg->peers[offset].daemon)
6421 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6422 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6423 churn_ctx->num_to_start = 0;
6424 churn_ctx->num_to_stop = 1;
6426 churn_ctx->cb_cls = cb_cls;
6427 shutdown_ctx->cb_cls = churn_ctx;
6428 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon, timeout,
6429 &churn_stop_callback, shutdown_ctx,
6430 GNUNET_NO, GNUNET_YES);
6433 else if (GNUNET_YES == desired_status)
6435 if (NULL == pg->peers[offset].daemon)
6437 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6438 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6439 churn_ctx->num_to_start = 1;
6440 churn_ctx->num_to_stop = 0;
6442 churn_ctx->cb_cls = cb_cls;
6443 startup_ctx->churn_ctx = churn_ctx;
6444 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
6445 timeout, &churn_start_callback,
6454 * Callback for shutting down peers in a peer group.
6456 * @param cls closure (struct ShutdownContext)
6457 * @param emsg NULL on success
6460 internal_shutdown_callback(void *cls, const char *emsg)
6462 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6463 struct ShutdownContext *shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6465 struct OutstandingSSH *ssh_pos;
6467 shutdown_ctx->outstanding--;
6468 if (peer_shutdown_ctx->daemon->hostname != NULL)
6469 decrement_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6474 shutdown_ctx->peers_down++;
6478 shutdown_ctx->peers_failed++;
6481 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down
6482 + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
6484 if (shutdown_ctx->peers_failed > 0)
6485 shutdown_ctx->cb (shutdown_ctx->cb_cls,
6486 "Not all peers successfully shut down!");
6488 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
6490 GNUNET_free (shutdown_ctx->pg->peers);
6491 GNUNET_free_non_null(shutdown_ctx->pg->hostkey_data);
6492 for (off = 0; off < shutdown_ctx->pg->num_hosts; off++)
6494 GNUNET_free (shutdown_ctx->pg->hosts[off].hostname);
6495 GNUNET_free_non_null (shutdown_ctx->pg->hosts[off].username);
6497 GNUNET_free_non_null (shutdown_ctx->pg->hosts);
6498 while (NULL != (ssh_pos = shutdown_ctx->pg->ssh_head))
6500 GNUNET_CONTAINER_DLL_remove(shutdown_ctx->pg->ssh_head, shutdown_ctx->pg->ssh_tail, ssh_pos);
6501 GNUNET_free(ssh_pos);
6503 GNUNET_free (shutdown_ctx->pg);
6504 GNUNET_free (shutdown_ctx);
6506 GNUNET_free(peer_shutdown_ctx);
6510 * Task to rate limit the number of outstanding peer shutdown
6511 * requests. This is necessary for making sure we don't do
6512 * too many ssh connections at once, but is generally nicer
6513 * to any system as well (graduated task starts, as opposed
6514 * to calling gnunet-arm N times all at once).
6517 schedule_shutdown_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6519 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6520 struct ShutdownContext *shutdown_ctx;
6522 GNUNET_assert (peer_shutdown_ctx != NULL);
6523 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6524 GNUNET_assert (shutdown_ctx != NULL);
6526 if ((shutdown_ctx->outstanding < shutdown_ctx->pg->max_concurrent_ssh)
6527 || ((peer_shutdown_ctx->daemon->hostname != NULL)
6528 && (count_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6530 < shutdown_ctx->pg->max_concurrent_ssh)))
6532 if (peer_shutdown_ctx->daemon->hostname != NULL)
6533 increment_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6535 shutdown_ctx->outstanding++;
6536 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6537 shutdown_ctx->timeout,
6538 &internal_shutdown_callback, peer_shutdown_ctx,
6539 GNUNET_YES, GNUNET_NO);
6542 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
6544 &schedule_shutdown_task, peer_shutdown_ctx);
6549 * Shutdown all peers started in the given group.
6551 * @param pg handle to the peer group
6552 * @param timeout how long to wait for shutdown
6553 * @param cb callback to notify upon success or failure
6554 * @param cb_cls closure for cb
6557 GNUNET_TESTING_daemons_stop(struct GNUNET_TESTING_PeerGroup *pg,
6558 struct GNUNET_TIME_Relative timeout,
6559 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6562 struct ShutdownContext *shutdown_ctx;
6563 struct PeerShutdownContext *peer_shutdown_ctx;
6565 struct PeerConnection *conn_iter;
6566 struct PeerConnection *temp_conn;
6569 GNUNET_assert (pg->total > 0);
6571 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6572 shutdown_ctx->cb = cb;
6573 shutdown_ctx->cb_cls = cb_cls;
6574 shutdown_ctx->total_peers = pg->total;
6575 shutdown_ctx->timeout = timeout;
6576 shutdown_ctx->pg = pg;
6577 /* shtudown_ctx->outstanding = 0; */
6579 for (off = 0; off < pg->total; off++)
6581 GNUNET_assert (NULL != pg->peers[off].daemon);
6582 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6583 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
6584 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6585 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
6587 if (NULL != pg->peers[off].cfg)
6588 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
6590 conn_iter = pg->peers[off].allowed_peers_head;
6591 while (conn_iter != NULL)
6593 temp_conn = conn_iter->next;
6594 GNUNET_free(conn_iter);
6595 conn_iter = temp_conn;
6598 conn_iter = pg->peers[off].connect_peers_head;
6599 while (conn_iter != NULL)
6601 temp_conn = conn_iter->next;
6602 GNUNET_free(conn_iter);
6603 conn_iter = temp_conn;
6606 conn_iter = pg->peers[off].blacklisted_peers_head;
6607 while (conn_iter != NULL)
6609 temp_conn = conn_iter->next;
6610 GNUNET_free(conn_iter);
6611 conn_iter = temp_conn;
6614 conn_iter = pg->peers[off].connect_peers_working_set_head;
6615 while (conn_iter != NULL)
6617 temp_conn = conn_iter->next;
6618 GNUNET_free(conn_iter);
6619 conn_iter = temp_conn;
6622 if (pg->peers[off].allowed_peers != NULL)
6623 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
6624 if (pg->peers[off].connect_peers != NULL)
6625 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
6626 if (pg->peers[off].blacklisted_peers != NULL)
6627 GNUNET_CONTAINER_multihashmap_destroy (pg->
6628 peers[off].blacklisted_peers);
6633 /* end of testing_group.c */