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
47 * Lowest port used for GNUnet testing. Should be high enough to not
48 * conflict with other applications running on the hosts but be low
49 * enough to not conflict with client-ports (typically starting around
52 #define LOW_PORT 12000
55 * Highest port used for GNUnet testing. Should be low enough to not
56 * conflict with the port range for "local" ports (client apps; see
57 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
59 #define HIGH_PORT 56000
61 /* Maximum time to delay connect attempt */
62 #define MAX_CONNECT_DELAY 300
65 * Which list of peers do we need to modify?
69 /** Modify allowed peers */
72 /** Modify connect peers */
75 /** Modify blacklist peers */
78 /** Modify workingset peers */
83 * Prototype of a function called whenever two peers would be connected
84 * in a certain topology.
87 (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup * pg,
88 unsigned int first, unsigned int second,
89 enum PeerLists list, unsigned int check);
92 * Context for handling churning a peer group
97 * The peergroup we are dealing with.
99 struct GNUNET_TESTING_PeerGroup *pg;
102 * Callback used to notify of churning finished
104 GNUNET_TESTING_NotifyCompletion cb;
107 * Closure for callback
112 * Number of peers that still need to be started
114 unsigned int num_to_start;
117 * Number of peers that still need to be stopped
119 unsigned int num_to_stop;
122 * Number of peers that failed to start
124 unsigned int num_failed_start;
127 * Number of peers that failed to stop
129 unsigned int num_failed_stop;
132 struct RestartContext
135 * The group of peers being restarted
137 struct GNUNET_TESTING_PeerGroup *peer_group;
140 * How many peers have been restarted thus far
142 unsigned int peers_restarted;
145 * How many peers got an error when restarting
147 unsigned int peers_restart_failed;
150 * The function to call once all peers have been restarted
152 GNUNET_TESTING_NotifyCompletion callback;
155 * Closure for callback function
161 struct SendHelloContext
164 * Global handle to the peer group.
166 struct GNUNET_TESTING_PeerGroup *pg;
169 * The data about this specific peer.
171 struct PeerData *peer;
174 * The next HELLO that needs sent to this peer.
176 struct PeerConnection *peer_pos;
179 * Are we connected to CORE yet?
181 unsigned int core_ready;
184 * How many attempts should we make for failed connections?
186 unsigned int connect_attempts;
189 * Task for scheduling core connect requests to be sent.
191 GNUNET_SCHEDULER_TaskIdentifier core_connect_task;
194 struct ShutdownContext
196 struct GNUNET_TESTING_PeerGroup *pg;
198 * Total peers to wait for
200 unsigned int total_peers;
203 * Number of peers successfully shut down
205 unsigned int peers_down;
208 * Number of peers failed to shut down
210 unsigned int peers_failed;
213 * Number of peers we have started shutting
214 * down. If too many, wait on them.
216 unsigned int outstanding;
219 * Timeout for shutdown.
221 struct GNUNET_TIME_Relative timeout;
224 * Callback to call when all peers either
225 * shutdown or failed to shutdown
227 GNUNET_TESTING_NotifyCompletion cb;
236 * Individual shutdown context for a particular peer.
238 struct PeerShutdownContext
241 * Pointer to the high level shutdown context.
243 struct ShutdownContext *shutdown_ctx;
246 * The daemon handle for the peer to shut down.
248 struct GNUNET_TESTING_Daemon *daemon;
252 * Individual shutdown context for a particular peer.
254 struct PeerRestartContext
257 * Pointer to the high level restart context.
259 struct ChurnRestartContext *churn_restart_ctx;
262 * The daemon handle for the peer to shut down.
264 struct GNUNET_TESTING_Daemon *daemon;
267 struct CreateTopologyContext
271 * Function to call with number of connections
273 GNUNET_TESTING_NotifyConnections cont;
276 * Closure for connection notification
283 /** Waiting to read number of peers */
286 /** Should find next peer index */
289 /** Should find colon */
292 /** Should read other peer index, space, or endline */
297 struct PeerConnection
302 struct PeerConnection *prev;
307 struct PeerConnection *next;
310 * Index of daemon in pg->peers
317 struct InternalStartContext
320 * Pointer to peerdata
322 struct PeerData *peer;
325 * Timeout for peer startup
327 struct GNUNET_TIME_Relative timeout;
330 * Client callback for hostkey notification
332 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
335 * Closure for hostkey_callback
340 * Client callback for peer start notification
342 GNUNET_TESTING_NotifyDaemonRunning start_cb;
350 * Hostname, where to start the peer
352 const char *hostname;
355 * Username to use when connecting to the
358 const char *username;
361 * Pointer to starting memory location of a hostkey
366 * Port to use for ssh.
372 struct ChurnRestartContext
375 * PeerGroup that we are working with.
377 struct GNUNET_TESTING_PeerGroup *pg;
380 * Number of restarts currently in flight.
382 unsigned int outstanding;
385 * Handle to the underlying churn context.
387 struct ChurnContext *churn_ctx;
390 * How long to allow the operation to take.
392 struct GNUNET_TIME_Relative timeout;
395 struct OutstandingSSH
397 struct OutstandingSSH *next;
399 struct OutstandingSSH *prev;
402 * Number of current ssh connections.
404 uint32_t outstanding;
407 * The hostname of this peer.
409 const char *hostname;
413 * Data we keep per peer.
418 * (Initial) configuration of the host.
419 * (initial because clients could change
420 * it and we would not know about those
423 struct GNUNET_CONFIGURATION_Handle *cfg;
426 * Handle for controlling the daemon.
428 struct GNUNET_TESTING_Daemon *daemon;
431 * The peergroup this peer belongs to.
433 struct GNUNET_TESTING_PeerGroup *pg;
437 * Linked list of allowed peer connections.
439 struct PeerConnection *allowed_peers_head;
442 * Linked list of allowed peer connections.
444 struct PeerConnection *allowed_peers_tail;
447 * Linked list of blacklisted peer connections.
449 struct PeerConnection *blacklisted_peers_head;
452 * Linked list of blacklisted peer connections.
454 struct PeerConnection *blacklisted_peers_tail;
457 * Linked list of connect peer connections.
459 struct PeerConnection *connect_peers_head;
462 * Linked list of connect peer connections.
464 struct PeerConnection *connect_peers_tail;
467 * Linked list of connect peer connections.
469 struct PeerConnection *connect_peers_working_set_head;
472 * Linked list of connect peer connections.
474 struct PeerConnection *connect_peers_working_set_tail;
478 * Hash map of allowed peer connections (F2F created topology)
480 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
483 * Hash map of blacklisted peers
485 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
488 * Hash map of peer connections
490 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
493 * Temporary hash map of peer connections
495 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
499 * Temporary variable for topology creation, should be reset before
500 * creating any topology so the count is valid once finished.
505 * Context to keep track of peers being started, to
506 * stagger hostkey generation and peer startup.
508 struct InternalStartContext internal_context;
512 * Linked list of per-host data.
522 * SSH username to use when connecting to this host.
527 * SSH port to use when connecting to this host.
532 * Lowest port that we have not yet used
538 struct TopologyIterateContext
541 * The peergroup we are working with.
543 struct GNUNET_TESTING_PeerGroup *pg;
546 * Callback for notifying of two connected peers.
548 GNUNET_TESTING_NotifyTopology topology_cb;
551 * Closure for topology_cb
556 * Number of peers currently connected to.
558 unsigned int connected;
561 * Number of peers we have finished iterating.
563 unsigned int completed;
566 * Number of peers total.
571 struct StatsIterateContext
574 * The peergroup that we are dealing with.
576 struct GNUNET_TESTING_PeerGroup *pg;
579 * Continuation to call once all stats information has been retrieved.
581 GNUNET_STATISTICS_Callback cont;
584 * Proc function to call on each value received.
586 GNUNET_TESTING_STATISTICS_Iterator proc;
589 * Closure for topology_cb
594 * Number of peers currently connected to.
596 unsigned int connected;
599 * Number of peers we have finished iterating.
601 unsigned int completed;
604 * Number of peers total.
612 struct GNUNET_TESTING_Daemon *daemon;
615 struct StatsCoreContext
618 struct GNUNET_TESTING_Daemon *daemon;
620 * Handle to the statistics service.
622 struct GNUNET_STATISTICS_Handle *stats_handle;
625 * Handle for getting statistics.
627 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
630 struct ConnectTopologyContext
633 * How many connections are left to create.
635 unsigned int remaining_connections;
638 * Handle to group of peers.
640 struct GNUNET_TESTING_PeerGroup *pg;
643 * How long to try this connection before timing out.
645 struct GNUNET_TIME_Relative connect_timeout;
648 * How many times to retry connecting the two peers.
650 unsigned int connect_attempts;
653 * Temp value set for each iteration.
655 //struct PeerData *first;
658 * Notification that all peers are connected.
660 GNUNET_TESTING_NotifyCompletion notify_connections_done;
663 * Closure for notify.
669 * Handle to a group of GNUnet peers.
671 struct GNUNET_TESTING_PeerGroup
674 * Configuration template.
676 const struct GNUNET_CONFIGURATION_Handle *cfg;
679 * Function to call on each started daemon.
681 //GNUNET_TESTING_NotifyDaemonRunning cb;
689 * Function to call on each topology connection created
691 GNUNET_TESTING_NotifyConnection notify_connection;
694 * Callback for notify_connection
696 void *notify_connection_cls;
699 * Array of information about hosts.
701 struct HostData *hosts;
704 * Number of hosts (size of HostData)
706 unsigned int num_hosts;
709 * Array of "total" peers.
711 struct PeerData *peers;
714 * Number of peers in this group.
719 * At what time should we fail the peer startup process?
721 struct GNUNET_TIME_Absolute max_timeout;
724 * How many peers are being started right now?
726 unsigned int starting;
729 * How many peers have already been started?
731 unsigned int started;
734 * Number of possible connections to peers
737 unsigned int max_outstanding_connections;
740 * Number of ssh connections to peers (max).
742 unsigned int max_concurrent_ssh;
745 * Number of connects we are waiting on, allows us to rate limit
748 unsigned int outstanding_connects;
751 * Number of HELLOs we have yet to send.
753 unsigned int remaining_hellos;
756 * How many connects have already been scheduled?
758 unsigned int total_connects_scheduled;
761 * Hostkeys loaded from a file.
766 * Head of DLL to keep track of the number of outstanding
767 * ssh connections per peer.
769 struct OutstandingSSH *ssh_head;
772 * Tail of DLL to keep track of the number of outstanding
773 * ssh connections per peer.
775 struct OutstandingSSH *ssh_tail;
778 * Stop scheduling peers connecting.
780 unsigned int stop_connects;
783 * Connection context for peer group.
785 struct ConnectTopologyContext ct_ctx;
791 * The altered configuration.
793 struct GNUNET_CONFIGURATION_Handle *ret;
796 * The original configuration to alter.
798 const struct GNUNET_CONFIGURATION_Handle *orig;
801 * The hostname that this peer will run on.
803 const char *hostname;
806 * The next possible port to assign.
811 * Unique number for unix domain sockets.
816 * Unique number for this peer/host to offset
817 * things that are grouped by host.
822 struct ConnectContext
825 * Index of peer to connect second to.
827 uint32_t first_index;
830 * Index of peer to connect first to.
832 uint32_t second_index;
835 * Higher level topology connection context.
837 struct ConnectTopologyContext *ct_ctx;
840 * Whether this connection has been accounted for in the schedule_connect call.
845 struct UnblacklistContext
850 struct GNUNET_TESTING_PeerGroup *pg;
853 * uid of the first peer
863 struct GNUNET_TESTING_PeerGroup *pg;
866 * uid of the first peer
871 * Peer data for first peer.
873 struct PeerData *first;
876 * Random percentage to use
881 struct MinimumContext
886 struct GNUNET_TESTING_PeerGroup *pg;
889 * uid of the first peer
894 * Peer data for first peer.
896 struct PeerData *first;
899 * Number of conns per peer
901 unsigned int num_to_add;
904 * Permuted array of all possible connections. Only add the Nth
905 * peer if it's in the Nth position.
907 unsigned int *pg_array;
910 * What number is the current element we are iterating over?
912 unsigned int current;
920 struct GNUNET_TESTING_PeerGroup *pg;
923 * uid of the first peer
928 * uid of the second peer
933 * Peer data for first peer.
935 struct PeerData *first;
938 * Which peer has been chosen as the one to add?
943 * What number is the current element we are iterating over?
945 unsigned int current;
949 * Simple struct to keep track of progress, and print a
950 * nice little percentage meter for long running tasks.
960 unsigned int completed;
964 char *startup_string;
969 * Convert unique ID to hash code.
971 * @param uid unique ID to convert
972 * @param hash set to uid (extended with zeros)
975 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
977 memset (hash, 0, sizeof (GNUNET_HashCode));
978 *((uint32_t *) hash) = uid;
982 * Convert hash code to unique ID.
984 * @param uid unique ID to convert
985 * @param hash set to uid (extended with zeros)
988 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
990 memcpy (uid, hash, sizeof (uint32_t));
995 static struct GNUNET_CORE_MessageHandler no_handlers[] =
1001 * Create a meter to keep track of the progress of some task.
1003 * @param total the total number of items to complete
1004 * @param start_string a string to prefix the meter with (if printing)
1005 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
1008 * @return the progress meter
1010 static struct ProgressMeter *
1011 create_meter(unsigned int total, char * start_string, int print)
1013 struct ProgressMeter *ret;
1014 ret = GNUNET_malloc(sizeof(struct ProgressMeter));
1017 ret->modnum = total / 4;
1018 if (ret->modnum == 0) /* Divide by zero check */
1020 ret->dotnum = (total / 50) + 1;
1021 if (start_string != NULL)
1022 ret->startup_string = GNUNET_strdup(start_string);
1024 ret->startup_string = GNUNET_strdup("");
1030 * Update progress meter (increment by one).
1032 * @param meter the meter to update and print info for
1034 * @return GNUNET_YES if called the total requested,
1035 * GNUNET_NO if more items expected
1038 update_meter(struct ProgressMeter *meter)
1040 if (meter->print == GNUNET_YES)
1042 if (meter->completed % meter->modnum == 0)
1044 if (meter->completed == 0)
1046 fprintf (stdout, "%sProgress: [0%%", meter->startup_string);
1049 fprintf (stdout, "%d%%", (int) (((float) meter->completed
1050 / meter->total) * 100));
1052 else if (meter->completed % meter->dotnum == 0)
1053 fprintf (stdout, ".");
1055 if (meter->completed + 1 == meter->total)
1056 fprintf (stdout, "%d%%]\n", 100);
1061 if (meter->completed == meter->total)
1063 if (meter->completed > meter->total)
1064 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
1069 * Reset progress meter.
1071 * @param meter the meter to reset
1073 * @return GNUNET_YES if meter reset,
1074 * GNUNET_SYSERR on error
1077 reset_meter(struct ProgressMeter *meter)
1080 return GNUNET_SYSERR;
1082 meter->completed = 0;
1087 * Release resources for meter
1089 * @param meter the meter to free
1092 free_meter(struct ProgressMeter *meter)
1094 GNUNET_free_non_null (meter->startup_string);
1095 GNUNET_free (meter);
1099 * Get a topology from a string input.
1101 * @param topology where to write the retrieved topology
1102 * @param topology_string The string to attempt to
1103 * get a configuration value from
1104 * @return GNUNET_YES if topology string matched a
1105 * known topology, GNUNET_NO if not
1108 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology,
1109 const char *topology_string)
1112 * Strings representing topologies in enum
1114 static const char *topology_strings[] =
1117 * A clique (everyone connected to everyone else).
1122 * Small-world network (2d torus plus random links).
1127 * Small-world network (ring plus random links).
1147 * Certain percentage of peers are unable to communicate directly
1148 * replicating NAT conditions
1153 * Scale free topology.
1158 * Straight line topology.
1163 * All peers are disconnected.
1168 * Read the topology from a file.
1175 if (topology_string == NULL)
1177 while (topology_strings[curr] != NULL)
1179 if (strcasecmp (topology_strings[curr], topology_string) == 0)
1186 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
1191 * Get connect topology option from string input.
1193 * @param topology_option where to write the retrieved topology
1194 * @param topology_string The string to attempt to
1195 * get a configuration value from
1196 * @return GNUNET_YES if string matched a known
1197 * topology option, GNUNET_NO if not
1200 GNUNET_TESTING_topology_option_get(
1201 enum GNUNET_TESTING_TopologyOption *topology_option,
1202 const char *topology_string)
1205 * Options for connecting a topology as strings.
1207 static const char *topology_option_strings[] =
1210 * Try to connect all peers specified in the topology.
1215 * Choose a random subset of connections to create.
1217 "CONNECT_RANDOM_SUBSET",
1220 * Create at least X connections for each peer.
1225 * Using a depth first search, create one connection
1226 * per peer. If any are missed (graph disconnected)
1227 * start over at those peers until all have at least one
1233 * Find the N closest peers to each allowed peer in the
1234 * topology and make sure a connection to those peers
1235 * exists in the connect topology.
1240 * No options specified.
1247 if (topology_string == NULL)
1249 while (NULL != topology_option_strings[curr])
1251 if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1253 *topology_option = curr;
1258 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1263 * Function to iterate over options. Copies
1264 * the options to the target configuration,
1265 * updating PORT values as needed.
1267 * @param cls closure
1268 * @param section name of the section
1269 * @param option name of the option
1270 * @param value value of the option
1273 update_config(void *cls, const char *section, const char *option,
1276 struct UpdateContext *ctx = cls;
1280 char *single_variable;
1281 char *per_host_variable;
1282 unsigned long long num_per_host;
1284 GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1285 GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1287 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1289 if ((ival != 0) && (GNUNET_YES
1290 != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1293 GNUNET_snprintf (cval, sizeof(cval), "%u", ctx->nport++);
1296 else if ((ival != 0) && (GNUNET_YES
1297 == GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1299 && GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1303 GNUNET_snprintf (cval, sizeof(cval), "%u", ival + ctx->fdnum
1309 if (0 == strcmp (option, "UNIXPATH"))
1311 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig,
1315 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1316 section, ctx->upnum++);
1319 else if ((GNUNET_YES
1320 == GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1323 && (num_per_host > 0))
1326 GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1327 section, ctx->fdnum % num_per_host);
1332 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1334 value = ctx->hostname;
1336 GNUNET_free (single_variable);
1337 GNUNET_free (per_host_variable);
1338 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1342 * Create a new configuration using the given configuration
1343 * as a template; however, each PORT in the existing cfg
1344 * must be renumbered by incrementing "*port". If we run
1345 * out of "*port" numbers, return NULL.
1347 * @param cfg template configuration
1348 * @param off the current peer offset
1349 * @param port port numbers to use, update to reflect
1350 * port numbers that were used
1351 * @param upnum number to make unix domain socket names unique
1352 * @param hostname hostname of the controlling host, to allow control connections from
1353 * @param fdnum number used to offset the unix domain socket for grouped processes
1354 * (such as statistics or peerinfo, which can be shared among others)
1356 * @return new configuration, NULL on error
1358 static struct GNUNET_CONFIGURATION_Handle *
1359 make_config(const struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t off,
1360 uint16_t * port, uint32_t * upnum, const char *hostname,
1363 struct UpdateContext uc;
1366 char *allowed_hosts;
1372 uc.ret = GNUNET_CONFIGURATION_create ();
1373 uc.hostname = hostname;
1376 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1377 if (uc.nport >= HIGH_PORT)
1380 GNUNET_CONFIGURATION_destroy (uc.ret);
1384 if (GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "control_host",
1385 &control_host) == GNUNET_OK)
1387 if (hostname != NULL)
1388 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1391 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1393 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1395 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1396 "ACCEPT_FROM", allowed_hosts);
1397 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1399 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1400 "ACCEPT_FROM", allowed_hosts);
1402 GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH", "");
1403 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH",
1405 GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH", "");
1406 GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1409 GNUNET_free_non_null (control_host);
1410 GNUNET_free (allowed_hosts);
1413 /* arm needs to know to allow connections from the host on which it is running,
1414 * otherwise gnunet-arm is unable to connect to it in some instances */
1415 if (hostname != NULL)
1417 GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1418 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1420 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1422 GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1424 GNUNET_free (allowed_hosts);
1428 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1430 GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1434 *port = (uint16_t) uc.nport;
1442 * Remove entries from the peer connection list
1444 * @param pg the peer group we are working with
1445 * @param first index of the first peer
1446 * @param second index of the second peer
1447 * @param list the peer list to use
1448 * @param check UNUSED
1450 * @return the number of connections added (can be 0, 1 or 2)
1454 remove_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1455 unsigned int second, enum PeerLists list, unsigned int check)
1459 struct PeerConnection **first_list;
1460 struct PeerConnection **second_list;
1461 struct PeerConnection *first_iter;
1462 struct PeerConnection *second_iter;
1463 struct PeerConnection **first_tail;
1464 struct PeerConnection **second_tail;
1467 GNUNET_HashCode hash_first;
1468 GNUNET_HashCode hash_second;
1470 hash_from_uid (first, &hash_first);
1471 hash_from_uid (second, &hash_second);
1479 first_list = &pg->peers[first].allowed_peers_head;
1480 second_list = &pg->peers[second].allowed_peers_head;
1481 first_tail = &pg->peers[first].allowed_peers_tail;
1482 second_tail = &pg->peers[second].allowed_peers_tail;
1485 first_list = &pg->peers[first].connect_peers_head;
1486 second_list = &pg->peers[second].connect_peers_head;
1487 first_tail = &pg->peers[first].connect_peers_tail;
1488 second_tail = &pg->peers[second].connect_peers_tail;
1491 first_list = &pg->peers[first].blacklisted_peers_head;
1492 second_list = &pg->peers[second].blacklisted_peers_head;
1493 first_tail = &pg->peers[first].blacklisted_peers_tail;
1494 second_tail = &pg->peers[second].blacklisted_peers_tail;
1497 first_list = &pg->peers[first].connect_peers_working_set_head;
1498 second_list = &pg->peers[second].connect_peers_working_set_head;
1499 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1500 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1507 first_iter = *first_list;
1508 while (first_iter != NULL)
1510 if (first_iter->index == second)
1512 GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1513 GNUNET_free(first_iter);
1517 first_iter = first_iter->next;
1520 second_iter = *second_list;
1521 while (second_iter != NULL)
1523 if (second_iter->index == first)
1525 GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1526 GNUNET_free(second_iter);
1530 second_iter = second_iter->next;
1534 GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1537 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1542 GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1545 GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1554 * Add entries to the some list
1556 * @param pg the peer group we are working with
1557 * @param first index of the first peer
1558 * @param second index of the second peer
1559 * @param list the list type that we should modify
1560 * @param check GNUNET_YES to check lists before adding
1561 * GNUNET_NO to force add
1563 * @return the number of connections added (can be 0, 1 or 2)
1567 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1568 unsigned int second, enum PeerLists list, unsigned int check)
1574 struct PeerConnection **first_list;
1575 struct PeerConnection **second_list;
1576 struct PeerConnection *first_iter;
1577 struct PeerConnection *second_iter;
1578 struct PeerConnection *new_first;
1579 struct PeerConnection *new_second;
1580 struct PeerConnection **first_tail;
1581 struct PeerConnection **second_tail;
1586 first_list = &pg->peers[first].allowed_peers_head;
1587 second_list = &pg->peers[second].allowed_peers_head;
1588 first_tail = &pg->peers[first].allowed_peers_tail;
1589 second_tail = &pg->peers[second].allowed_peers_tail;
1592 first_list = &pg->peers[first].connect_peers_head;
1593 second_list = &pg->peers[second].connect_peers_head;
1594 first_tail = &pg->peers[first].connect_peers_tail;
1595 second_tail = &pg->peers[second].connect_peers_tail;
1598 first_list = &pg->peers[first].blacklisted_peers_head;
1599 second_list = &pg->peers[second].blacklisted_peers_head;
1600 first_tail = &pg->peers[first].blacklisted_peers_tail;
1601 second_tail = &pg->peers[second].blacklisted_peers_tail;
1604 first_list = &pg->peers[first].connect_peers_working_set_head;
1605 second_list = &pg->peers[second].connect_peers_working_set_head;
1606 first_tail = &pg->peers[first].connect_peers_working_set_tail;
1607 second_tail = &pg->peers[second].connect_peers_working_set_tail;
1614 add_first = GNUNET_YES;
1615 add_second = GNUNET_YES;
1617 if (check == GNUNET_YES)
1619 first_iter = *first_list;
1620 while (first_iter != NULL)
1622 if (first_iter->index == second)
1624 add_first = GNUNET_NO;
1627 first_iter = first_iter->next;
1630 second_iter = *second_list;
1631 while (second_iter != NULL)
1633 if (second_iter->index == first)
1635 add_second = GNUNET_NO;
1638 second_iter = second_iter->next;
1645 new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1646 new_first->index = second;
1647 GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1648 pg->peers[first].num_connections++;
1654 new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1655 new_second->index = first;
1656 GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1657 pg->peers[second].num_connections++;
1665 * Scale free network construction as described in:
1667 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1669 * Start with a network of "one" peer, then progressively add
1670 * peers up to the total number. At each step, iterate over
1671 * all possible peers and connect new peer based on number of
1672 * existing connections of the target peer.
1674 * @param pg the peer group we are dealing with
1675 * @param proc the connection processor to use
1676 * @param list the peer list to use
1678 * @return the number of connections created
1681 create_scale_free(struct GNUNET_TESTING_PeerGroup *pg,
1682 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1685 unsigned int total_connections;
1686 unsigned int outer_count;
1688 unsigned int previous_total_connections;
1692 GNUNET_assert (pg->total > 1);
1694 /* Add a connection between the first two nodes */
1695 total_connections = proc (pg, 0, 1, list, GNUNET_YES);
1697 for (outer_count = 1; outer_count < pg->total; outer_count++)
1699 previous_total_connections = total_connections;
1700 for (i = 0; i < outer_count; i++)
1702 probability = pg->peers[i].num_connections
1703 / (double) previous_total_connections;
1705 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1707 / ((double) UINT64_MAX);
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1710 "Considering connecting peer %d to peer %d\n",
1713 if (random < probability)
1716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1717 "Connecting peer %d to peer %d\n", outer_count, i);
1719 total_connections += proc (pg, outer_count, i, list, GNUNET_YES);
1724 return total_connections;
1728 * Create a topology given a peer group (set of running peers)
1729 * and a connection processor. Creates a small world topology
1730 * according to the rewired ring construction. The basic
1731 * behavior is that a ring topology is created, but with some
1732 * probability instead of connecting a peer to the next
1733 * neighbor in the ring a connection will be created to a peer
1734 * selected uniformly at random. We use the TESTING
1735 * PERCENTAGE option to specify what number of
1736 * connections each peer should have. Default is 2,
1737 * which makes the ring, any given number is multiplied by
1738 * the log of the network size; i.e. a PERCENTAGE of 2 makes
1739 * each peer have on average 2logn connections. The additional
1740 * connections are made at increasing distance around the ring
1741 * from the original peer, or to random peers based on the re-
1742 * wiring probability. The TESTING
1743 * PROBABILITY option is used as the probability that a given
1744 * connection is rewired.
1746 * @param pg the peergroup to create the topology on
1747 * @param proc the connection processor to call to actually set
1748 * up connections between two peers
1749 * @param list the peer list to use
1751 * @return the number of connections that were set up
1755 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg,
1756 GNUNET_TESTING_ConnectionProcessor proc,
1757 enum PeerLists list)
1761 unsigned int natLog;
1762 unsigned int randomPeer;
1763 double random, logNModifier, probability;
1764 unsigned int smallWorldConnections;
1769 unsigned int useAnd;
1770 int connect_attempts;
1772 logNModifier = 0.5; /* FIXME: default value? */
1773 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1777 if (sscanf (p_string, "%lf", &logNModifier) != 1)
1779 GNUNET_ERROR_TYPE_WARNING,
1781 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1782 p_string, "LOGNMODIFIER", "TESTING");
1783 GNUNET_free (p_string);
1785 probability = 0.5; /* FIXME: default percentage? */
1786 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1790 if (sscanf (p_string, "%lf", &probability) != 1)
1792 GNUNET_ERROR_TYPE_WARNING,
1794 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1795 p_string, "PERCENTAGE", "TESTING");
1796 GNUNET_free (p_string);
1798 natLog = log (pg->total);
1799 connsPerPeer = ceil (natLog * logNModifier);
1801 if (connsPerPeer % 2 == 1)
1804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."),
1807 smallWorldConnections = 0;
1808 connect_attempts = 0;
1809 for (i = 0; i < pg->total; i++)
1812 max = i + connsPerPeer / 2;
1813 min = i - connsPerPeer / 2;
1815 if (max > pg->total - 1)
1817 max = max - pg->total;
1823 min = pg->total - 1 + min;
1827 for (j = 0; j < connsPerPeer / 2; j++)
1830 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1832 / ((double) UINT64_MAX));
1833 if (random < probability)
1835 /* Connect to uniformly selected random peer */
1837 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1839 while ((((randomPeer < max) && (randomPeer > min)) && (useAnd
1840 == 0)) || (((randomPeer > min) || (randomPeer < max))
1844 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1847 smallWorldConnections += proc (pg, i, randomPeer, list,
1852 nodeToConnect = i + j + 1;
1853 if (nodeToConnect > pg->total - 1)
1855 nodeToConnect = nodeToConnect - pg->total;
1857 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
1863 connect_attempts += smallWorldConnections;
1865 return connect_attempts;
1869 * Create a topology given a peer group (set of running peers)
1870 * and a connection processor.
1872 * @param pg the peergroup to create the topology on
1873 * @param proc the connection processor to call to actually set
1874 * up connections between two peers
1875 * @param list the peer list to use
1877 * @return the number of connections that were set up
1881 create_nated_internet(struct GNUNET_TESTING_PeerGroup *pg,
1882 GNUNET_TESTING_ConnectionProcessor proc,
1883 enum PeerLists list)
1885 unsigned int outer_count, inner_count;
1886 unsigned int cutoff;
1887 int connect_attempts;
1888 double nat_percentage;
1891 nat_percentage = 0.6; /* FIXME: default percentage? */
1892 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1896 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1898 GNUNET_ERROR_TYPE_WARNING,
1900 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1901 p_string, "PERCENTAGE", "TESTING");
1902 GNUNET_free (p_string);
1905 cutoff = (unsigned int) (nat_percentage * pg->total);
1906 connect_attempts = 0;
1907 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1909 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1911 if ((outer_count > cutoff) || (inner_count > cutoff))
1914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1915 "Connecting peer %d to peer %d\n",
1916 outer_count, inner_count);
1918 connect_attempts += proc (pg, outer_count, inner_count, list,
1923 return connect_attempts;
1928 * Create a topology given a peer group (set of running peers)
1929 * and a connection processor.
1931 * @param pg the peergroup to create the topology on
1932 * @param proc the connection processor to call to actually set
1933 * up connections between two peers
1934 * @param list the peer list to use
1936 * @return the number of connections that were set up
1940 create_nated_internet_copy(struct GNUNET_TESTING_PeerGroup *pg,
1941 GNUNET_TESTING_ConnectionProcessor proc,
1942 enum PeerLists list)
1944 unsigned int outer_count, inner_count;
1945 unsigned int cutoff;
1946 int connect_attempts;
1947 double nat_percentage;
1950 struct ProgressMeter *conn_meter;
1952 nat_percentage = 0.6; /* FIXME: default percentage? */
1953 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1957 if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1959 GNUNET_ERROR_TYPE_WARNING,
1961 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1962 p_string, "PERCENTAGE", "TESTING");
1963 GNUNET_free (p_string);
1966 cutoff = (unsigned int) (nat_percentage * pg->total);
1968 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1970 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1972 if ((outer_count > cutoff) || (inner_count > cutoff))
1978 conn_meter = create_meter (count, "NAT COPY", GNUNET_YES);
1979 connect_attempts = 0;
1980 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1982 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1984 if ((outer_count > cutoff) || (inner_count > cutoff))
1987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1988 "Connecting peer %d to peer %d\n",
1989 outer_count, inner_count);
1991 connect_attempts += proc (pg, outer_count, inner_count, list,
1993 add_connections (pg, outer_count, inner_count, ALLOWED, GNUNET_NO);
1994 update_meter (conn_meter);
1998 free_meter (conn_meter);
2000 return connect_attempts;
2005 * Create a topology given a peer group (set of running peers)
2006 * and a connection processor.
2008 * @param pg the peergroup to create the topology on
2009 * @param proc the connection processor to call to actually set
2010 * up connections between two peers
2011 * @param list the peer list to use
2013 * @return the number of connections that were set up
2017 create_small_world(struct GNUNET_TESTING_PeerGroup *pg,
2018 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2020 unsigned int i, j, k;
2021 unsigned int square;
2024 unsigned int toggle = 1;
2025 unsigned int nodeToConnect;
2026 unsigned int natLog;
2027 unsigned int node1Row;
2028 unsigned int node1Col;
2029 unsigned int node2Row;
2030 unsigned int node2Col;
2031 unsigned int distance;
2032 double probability, random, percentage;
2033 unsigned int smallWorldConnections;
2034 unsigned int small_world_it;
2036 int connect_attempts;
2037 square = floor (sqrt (pg->total));
2041 percentage = 0.5; /* FIXME: default percentage? */
2042 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2046 if (sscanf (p_string, "%lf", &percentage) != 1)
2048 GNUNET_ERROR_TYPE_WARNING,
2050 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2051 p_string, "PERCENTAGE", "TESTING");
2052 GNUNET_free (p_string);
2054 if (percentage < 0.0)
2057 GNUNET_ERROR_TYPE_WARNING,
2059 ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
2060 "PERCENTAGE", "TESTING", percentage);
2063 probability = 0.5; /* FIXME: default percentage? */
2064 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2068 if (sscanf (p_string, "%lf", &probability) != 1)
2070 GNUNET_ERROR_TYPE_WARNING,
2072 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2073 p_string, "PROBABILITY", "TESTING");
2074 GNUNET_free (p_string);
2076 if (square * square != pg->total)
2078 while (rows * cols < pg->total)
2080 if (toggle % 2 == 0)
2089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2091 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2095 connect_attempts = 0;
2096 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2097 * to the node to its right and above. Once this is over, we'll have our torus!
2098 * Special case for the last node (if the rows and columns are not equal), connect
2099 * to the first in the row to maintain topology.
2101 for (i = 0; i < pg->total; i++)
2103 /* First connect to the node to the right */
2104 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2105 nodeToConnect = i + 1;
2106 else if (i + 1 == pg->total)
2107 nodeToConnect = rows * cols - cols;
2109 nodeToConnect = i - cols + 1;
2111 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2115 nodeToConnect = (rows * cols) - cols + i;
2116 if (nodeToConnect >= pg->total)
2117 nodeToConnect -= cols;
2120 nodeToConnect = i - cols;
2122 if (nodeToConnect < pg->total)
2123 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2125 natLog = log (pg->total);
2126 #if VERBOSE_TESTING > 2
2127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2128 _("natural log of %d is %d, will run %d iterations\n"),
2129 pg->total, natLog, (int) (natLog * percentage));
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2131 _("Total connections added thus far: %u!\n"), connect_attempts);
2133 smallWorldConnections = 0;
2134 small_world_it = (unsigned int) (natLog * percentage);
2135 if (small_world_it < 1)
2137 GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
2138 for (i = 0; i < small_world_it; i++)
2140 for (j = 0; j < pg->total; j++)
2142 /* Determine the row and column of node at position j on the 2d torus */
2143 node1Row = j / cols;
2144 node1Col = j - (node1Row * cols);
2145 for (k = 0; k < pg->total; k++)
2147 /* Determine the row and column of node at position k on the 2d torus */
2148 node2Row = k / cols;
2149 node2Col = k - (node2Row * cols);
2150 /* Simple Cartesian distance */
2151 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
2154 /* Calculate probability as 1 over the square of the distance */
2155 probability = 1.0 / (distance * distance);
2156 /* Choose a random value between 0 and 1 */
2158 = ((double) GNUNET_CRYPTO_random_u64 (
2159 GNUNET_CRYPTO_QUALITY_WEAK,
2161 / ((double) UINT64_MAX);
2162 /* If random < probability, then connect the two nodes */
2163 if (random < probability)
2164 smallWorldConnections += proc (pg, j, k, list, GNUNET_YES);
2170 connect_attempts += smallWorldConnections;
2171 #if VERBOSE_TESTING > 2
2172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2173 _("Total connections added for small world: %d!\n"),
2174 smallWorldConnections);
2176 return connect_attempts;
2180 * Create a topology given a peer group (set of running peers)
2181 * and a connection processor.
2183 * @param pg the peergroup to create the topology on
2184 * @param proc the connection processor to call to actually set
2185 * up connections between two peers
2186 * @param list the peer list to use
2188 * @return the number of connections that were set up
2192 create_erdos_renyi(struct GNUNET_TESTING_PeerGroup *pg,
2193 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2196 unsigned int outer_count;
2197 unsigned int inner_count;
2198 int connect_attempts;
2202 probability = 0.5; /* FIXME: default percentage? */
2203 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2207 if (sscanf (p_string, "%lf", &probability) != 1)
2209 GNUNET_ERROR_TYPE_WARNING,
2211 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2212 p_string, "PROBABILITY", "TESTING");
2213 GNUNET_free (p_string);
2215 connect_attempts = 0;
2216 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2218 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2221 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2223 / ((double) UINT64_MAX);
2225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2226 _("rand is %f probability is %f\n"), temp_rand,
2229 if (temp_rand < probability)
2231 connect_attempts += proc (pg, outer_count, inner_count, list,
2237 return connect_attempts;
2241 * Create a topology given a peer group (set of running peers)
2242 * and a connection processor. This particular function creates
2243 * the connections for a 2d-torus, plus additional "closest"
2244 * connections per peer.
2246 * @param pg the peergroup to create the topology on
2247 * @param proc the connection processor to call to actually set
2248 * up connections between two peers
2249 * @param list the peer list to use
2251 * @return the number of connections that were set up
2255 create_2d_torus(struct GNUNET_TESTING_PeerGroup *pg,
2256 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2259 unsigned int square;
2262 unsigned int toggle = 1;
2263 unsigned int nodeToConnect;
2264 int connect_attempts;
2266 connect_attempts = 0;
2268 square = floor (sqrt (pg->total));
2272 if (square * square != pg->total)
2274 while (rows * cols < pg->total)
2276 if (toggle % 2 == 0)
2285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2287 ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2290 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2291 * to the node to its right and above. Once this is over, we'll have our torus!
2292 * Special case for the last node (if the rows and columns are not equal), connect
2293 * to the first in the row to maintain topology.
2295 for (i = 0; i < pg->total; i++)
2297 /* First connect to the node to the right */
2298 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2299 nodeToConnect = i + 1;
2300 else if (i + 1 == pg->total)
2301 nodeToConnect = rows * cols - cols;
2303 nodeToConnect = i - cols + 1;
2305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2306 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2308 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2310 /* Second connect to the node immediately above */
2313 nodeToConnect = (rows * cols) - cols + i;
2314 if (nodeToConnect >= pg->total)
2315 nodeToConnect -= cols;
2318 nodeToConnect = i - cols;
2320 if (nodeToConnect < pg->total)
2323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2324 "Connecting peer %d to peer %d\n", i, nodeToConnect);
2326 connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2331 return connect_attempts;
2335 * Create a topology given a peer group (set of running peers)
2336 * and a connection processor.
2338 * @param pg the peergroup to create the topology on
2339 * @param proc the connection processor to call to actually set
2340 * up connections between two peers
2341 * @param list the peer list to use
2342 * @param check does the connection processor need to check before
2343 * performing an action on the list?
2345 * @return the number of connections that were set up
2349 create_clique(struct GNUNET_TESTING_PeerGroup *pg,
2350 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list,
2353 unsigned int outer_count;
2354 unsigned int inner_count;
2355 int connect_attempts;
2356 struct ProgressMeter *conn_meter;
2357 connect_attempts = 0;
2359 conn_meter = create_meter ((((pg->total * pg->total) + pg->total) / 2)
2360 - pg->total, "Create Clique ", GNUNET_YES);
2361 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2363 for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2367 "Connecting peer %d to peer %d\n",
2368 outer_count, inner_count);
2370 connect_attempts += proc (pg, outer_count, inner_count, list, check);
2371 update_meter (conn_meter);
2374 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Meter has %d left\n",
2375 conn_meter->total - conn_meter->completed);
2376 reset_meter (conn_meter);
2377 free_meter (conn_meter);
2378 return connect_attempts;
2383 * Iterator over hash map entries.
2385 * @param cls closure the peer group
2386 * @param key the key stored in the hashmap is the
2387 * index of the peer to connect to
2388 * @param value value in the hash map, handle to the peer daemon
2389 * @return GNUNET_YES if we should continue to
2394 unblacklist_iterator (void *cls,
2395 const GNUNET_HashCode * key,
2398 struct UnblacklistContext *un_ctx = cls;
2399 uint32_t second_pos;
2401 uid_from_hash (key, &second_pos);
2403 unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2410 * Create a blacklist topology based on the allowed topology
2411 * which disallows any connections not in the allowed topology
2412 * at the transport level.
2414 * @param pg the peergroup to create the topology on
2415 * @param proc the connection processor to call to allow
2416 * up connections between two peers
2418 * @return the number of connections that were set up
2422 copy_allowed(struct GNUNET_TESTING_PeerGroup *pg,
2423 GNUNET_TESTING_ConnectionProcessor proc)
2425 struct UnblacklistContext un_ctx;
2428 struct PeerConnection *iter;
2432 for (count = 0; count < pg->total - 1; count++)
2434 un_ctx.first_uid = count;
2436 iter = pg->peers[count].allowed_peers_head;
2437 while (iter != NULL)
2439 remove_connections (pg, count, iter->index, BLACKLIST, GNUNET_YES);
2440 //unblacklist_connections(pg, count, iter->index);
2444 total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers, &unblacklist_iterator, &un_ctx);
2447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2452 * Create a topology given a peer group (set of running peers)
2453 * and a connection processor.
2455 * @param pg the peergroup to create the topology on
2456 * @param proc the connection processor to call to actually set
2457 * up connections between two peers
2458 * @param list which list should be modified
2460 * @return the number of connections that were set up
2464 create_line(struct GNUNET_TESTING_PeerGroup *pg,
2465 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2468 int connect_attempts;
2470 connect_attempts = 0;
2472 /* Connect each peer to the next highest numbered peer */
2473 for (count = 0; count < pg->total - 1; count++)
2476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2477 "Connecting peer %d to peer %d\n", count, count + 1);
2479 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2482 return connect_attempts;
2486 * Create a topology given a peer group (set of running peers)
2487 * and a connection processor.
2489 * @param pg the peergroup to create the topology on
2490 * @param filename the file to read topology information from
2491 * @param proc the connection processor to call to actually set
2492 * up connections between two peers
2493 * @param list the peer list to use
2495 * @return the number of connections that were set up
2499 create_from_file(struct GNUNET_TESTING_PeerGroup *pg, char *filename,
2500 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2502 int connect_attempts;
2503 unsigned int first_peer_index;
2504 unsigned int second_peer_index;
2505 connect_attempts = 0;
2510 unsigned int total_peers;
2512 enum States curr_state;
2514 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2515 GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2517 if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2519 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2520 "Could not open file `%s' specified for topology!", filename);
2521 return connect_attempts;
2524 data = GNUNET_malloc_large (frstat.st_size);
2525 GNUNET_assert(data != NULL);
2526 if (frstat.st_size != GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2529 GNUNET_ERROR_TYPE_ERROR,
2530 "Could not read file %s specified for host list, ending test!",
2533 return connect_attempts;
2538 first_peer_index = 0;
2539 /* First line should contain a single integer, specifying the number of peers */
2540 /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2541 curr_state = NUM_PEERS;
2542 while (count < frstat.st_size - 1)
2544 if ((buf[count] == '\n') || (buf[count] == ' '))
2554 total_peers = strtoul(&buf[count], NULL, 10);
2557 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2558 "Failed to read number of peers from topology file!\n");
2559 GNUNET_free_non_null(data);
2560 return connect_attempts;
2562 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2563 "Read %u total peers in topology\n", total_peers);
2564 GNUNET_assert(total_peers == pg->total);
2565 curr_state = PEER_INDEX;
2566 while ((buf[count] != '\n') && (count < frstat.st_size - 1))
2572 first_peer_index = strtoul(&buf[count], NULL, 10);
2575 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2576 "Failed to read peer index from topology file!\n");
2577 GNUNET_free_non_null(data);
2578 return connect_attempts;
2580 while ((buf[count] != ':') && (count < frstat.st_size - 1))
2583 curr_state = OTHER_PEER_INDEX;
2586 if (1 == sscanf (&buf[count], ":"))
2587 curr_state = OTHER_PEER_INDEX;
2590 case OTHER_PEER_INDEX:
2592 second_peer_index = strtoul(&buf[count], NULL, 10);
2595 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2596 "Failed to peer index from topology file!\n");
2597 GNUNET_free_non_null(data);
2598 return connect_attempts;
2600 /* Assume file is written with first peer 1, but array index is 0 */
2601 connect_attempts += proc (pg, first_peer_index - 1, second_peer_index
2602 - 1, list, GNUNET_YES);
2603 while ((buf[count] != '\n') && (buf[count] != ',') && (count
2604 < frstat.st_size - 1))
2606 if (buf[count] == '\n')
2608 curr_state = PEER_INDEX;
2610 else if (buf[count] != ',')
2612 curr_state = OTHER_PEER_INDEX;
2617 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2618 "Found bad data in topology file while in state %d!\n",
2621 return connect_attempts;
2625 return connect_attempts;
2629 * Create a topology given a peer group (set of running peers)
2630 * and a connection processor.
2632 * @param pg the peergroup to create the topology on
2633 * @param proc the connection processor to call to actually set
2634 * up connections between two peers
2635 * @param list the peer list to use
2637 * @return the number of connections that were set up
2641 create_ring(struct GNUNET_TESTING_PeerGroup *pg,
2642 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2645 int connect_attempts;
2647 connect_attempts = 0;
2649 /* Connect each peer to the next highest numbered peer */
2650 for (count = 0; count < pg->total - 1; count++)
2653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2654 "Connecting peer %d to peer %d\n", count, count + 1);
2656 connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2659 /* Connect the last peer to the first peer */
2660 connect_attempts += proc (pg, pg->total - 1, 0, list, GNUNET_YES);
2662 return connect_attempts;
2667 * Iterator for writing friends of a peer to a file.
2669 * @param cls closure, an open writable file handle
2670 * @param key the key the daemon was stored under
2671 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2673 * @return GNUNET_YES to continue iteration
2675 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2676 * with a single file_iterator that takes a closure which contains
2677 * the prefix to write before the peer. Then this could be used
2678 * for blacklisting multiple transports and writing the friend
2679 * file. I'm sure *someone* will complain loudly about other
2680 * things that negate these functions even existing so no point in
2684 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2686 FILE *temp_friend_handle = cls;
2687 struct GNUNET_TESTING_Daemon *peer = value;
2688 struct GNUNET_PeerIdentity *temppeer;
2689 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2691 temppeer = &peer->id;
2692 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2693 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2698 struct BlacklistContext
2701 * The (open) file handle to write to
2703 FILE *temp_file_handle;
2706 * The transport that this peer will be blacklisted on.
2712 * Iterator for writing blacklist data to appropriate files.
2714 * @param cls closure, an open writable file handle
2715 * @param key the key the daemon was stored under
2716 * @param value the GNUNET_TESTING_Daemon that needs to be written.
2718 * @return GNUNET_YES to continue iteration
2721 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2723 struct BlacklistContext *blacklist_ctx = cls;
2724 struct GNUNET_TESTING_Daemon *peer = value;
2725 struct GNUNET_PeerIdentity *temppeer;
2726 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2728 temppeer = &peer->id;
2729 GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2730 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2731 fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2732 blacklist_ctx->transport, (char *) &peer_enc);
2739 * Create the friend files based on the PeerConnection's
2740 * of each peer in the peer group, and copy the files
2741 * to the appropriate place
2743 * @param pg the peer group we are dealing with
2746 create_and_copy_friend_files(struct GNUNET_TESTING_PeerGroup *pg)
2748 FILE *temp_friend_handle;
2749 unsigned int pg_iter;
2750 char *temp_service_path;
2751 struct GNUNET_OS_Process **procarr;
2755 enum GNUNET_OS_ProcessStatusType type;
2756 unsigned long return_code;
2764 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2765 struct PeerConnection *conn_iter;
2767 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2768 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2770 mytemp = GNUNET_DISK_mktemp ("friends");
2771 GNUNET_assert (mytemp != NULL);
2772 temp_friend_handle = fopen (mytemp, "wt");
2773 GNUNET_assert (temp_friend_handle != NULL);
2775 conn_iter = pg->peers[pg_iter].allowed_peers_head;
2776 while (conn_iter != NULL)
2778 GNUNET_CRYPTO_hash_to_enc (
2779 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2781 fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2782 conn_iter = conn_iter->next;
2785 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2786 &friend_file_iterator,
2787 temp_friend_handle);
2789 fclose (temp_friend_handle);
2792 != GNUNET_CONFIGURATION_get_value_string (
2793 pg->peers[pg_iter]. daemon->cfg,
2794 "PATHS", "SERVICEHOME",
2795 &temp_service_path))
2798 GNUNET_ERROR_TYPE_WARNING,
2800 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2801 "SERVICEHOME", "PATHS");
2802 if (UNLINK (mytemp) != 0)
2803 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2805 GNUNET_free (mytemp);
2809 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2811 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2812 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
2815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2816 _("Copying file with command cp %s %s\n"), mytemp, arg);
2818 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2819 GNUNET_OS_process_close (procarr[pg_iter]);
2822 else /* Remote, scp the file to the correct place */
2824 if (NULL != pg->peers[pg_iter].daemon->username)
2825 GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2826 pg->peers[pg_iter].daemon->username,
2827 pg->peers[pg_iter].daemon->hostname,
2830 GNUNET_asprintf (&arg, "%s:%s/friends",
2831 pg->peers[pg_iter].daemon->hostname,
2833 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
2836 ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2837 GNUNET_OS_process_close (procarr[pg_iter]);
2838 if (ret != GNUNET_OK)
2840 procarr[pg_iter] = NULL;
2842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2843 _("Copying file with command scp %s %s\n"), mytemp,
2848 GNUNET_free (temp_service_path);
2849 GNUNET_free (mytemp);
2854 ret = GNUNET_SYSERR;
2855 while ((count < max_wait) && (ret != GNUNET_OK))
2858 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2862 _("Checking copy status of file %d\n"), pg_iter);
2864 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2866 if (GNUNET_OS_process_status
2867 (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2869 ret = GNUNET_SYSERR;
2871 else if ((type != GNUNET_OS_PROCESS_EXITED)
2872 || (return_code != 0))
2874 ret = GNUNET_SYSERR;
2878 GNUNET_OS_process_close (procarr[pg_iter]);
2879 procarr[pg_iter] = NULL;
2881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2882 _("File %d copied\n"), pg_iter);
2888 if (ret == GNUNET_SYSERR)
2890 /* FIXME: why sleep here? -CG */
2896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2897 _("Finished copying all friend files!\n"));
2900 GNUNET_free (procarr);
2905 * Create the blacklist files based on the PeerConnection's
2906 * of each peer in the peer group, and copy the files
2907 * to the appropriate place.
2909 * @param pg the peer group we are dealing with
2910 * @param transports space delimited list of transports to blacklist
2913 create_and_copy_blacklist_files(struct GNUNET_TESTING_PeerGroup *pg,
2914 const char *transports)
2916 FILE *temp_file_handle;
2917 unsigned int pg_iter;
2918 char *temp_service_path;
2919 struct GNUNET_OS_Process **procarr;
2922 enum GNUNET_OS_ProcessStatusType type;
2923 unsigned long return_code;
2930 char *temp_transports;
2933 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2934 struct PeerConnection *conn_iter;
2936 static struct BlacklistContext blacklist_ctx;
2939 procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2940 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2942 mytemp = GNUNET_DISK_mktemp ("blacklist");
2943 GNUNET_assert (mytemp != NULL);
2944 temp_file_handle = fopen (mytemp, "wt");
2945 GNUNET_assert (temp_file_handle != NULL);
2946 temp_transports = GNUNET_strdup (transports);
2948 blacklist_ctx.temp_file_handle = temp_file_handle;
2950 transport_len = strlen (temp_transports) + 1;
2953 for (i = 0; i < transport_len; i++)
2955 if ((temp_transports[i] == ' ') && (pos == NULL))
2956 continue; /* At start of string (whitespace) */
2957 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2959 temp_transports[i] = '\0';
2961 conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
2962 while (conn_iter != NULL)
2964 GNUNET_CRYPTO_hash_to_enc (
2965 &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2967 fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
2968 conn_iter = conn_iter->next;
2972 blacklist_ctx.transport = pos;
2973 entry_count = GNUNET_CONTAINER_multihashmap_iterate (pg->
2975 [pg_iter].blacklisted_peers,
2976 &blacklist_file_iterator,
2980 } /* At beginning of actual string */
2981 else if (pos == NULL)
2983 pos = &temp_transports[i];
2987 GNUNET_free (temp_transports);
2988 fclose (temp_file_handle);
2991 != GNUNET_CONFIGURATION_get_value_string (
2992 pg->peers[pg_iter]. daemon->cfg,
2993 "PATHS", "SERVICEHOME",
2994 &temp_service_path))
2997 GNUNET_ERROR_TYPE_WARNING,
2999 ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
3000 "SERVICEHOME", "PATHS");
3001 if (UNLINK (mytemp) != 0)
3002 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
3004 GNUNET_free (mytemp);
3008 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
3010 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
3011 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
3014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3015 _("Copying file with command cp %s %s\n"), mytemp, arg);
3020 else /* Remote, scp the file to the correct place */
3022 if (NULL != pg->peers[pg_iter].daemon->username)
3023 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
3024 pg->peers[pg_iter].daemon->username,
3025 pg->peers[pg_iter].daemon->hostname,
3028 GNUNET_asprintf (&arg, "%s:%s/blacklist",
3029 pg->peers[pg_iter].daemon->hostname,
3031 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
3034 GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3038 _("Copying file with command scp %s %s\n"), mytemp,
3043 GNUNET_free (temp_service_path);
3044 GNUNET_free (mytemp);
3048 ret = GNUNET_SYSERR;
3049 while ((count < max_wait) && (ret != GNUNET_OK))
3052 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3056 _("Checking copy status of file %d\n"), pg_iter);
3058 if (procarr[pg_iter] != NULL) /* Check for already completed! */
3060 if (GNUNET_OS_process_status (procarr[pg_iter], &type,
3061 &return_code) != GNUNET_OK)
3063 ret = GNUNET_SYSERR;
3065 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
3067 ret = GNUNET_SYSERR;
3071 GNUNET_OS_process_close (procarr[pg_iter]);
3072 procarr[pg_iter] = NULL;
3074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3075 _("File %d copied\n"), pg_iter);
3081 if (ret == GNUNET_SYSERR)
3083 /* FIXME: why sleep here? -CG */
3089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3090 _("Finished copying all blacklist files!\n"));
3092 GNUNET_free (procarr);
3096 /* Forward Declaration */
3098 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3101 * Choose a random peer's next connection to create, and
3102 * call schedule_connect to set up the connect task.
3104 * @param ct_ctx the overall connection context
3107 preschedule_connect(struct GNUNET_TESTING_PeerGroup *pg)
3109 struct ConnectTopologyContext *ct_ctx = &pg->ct_ctx;
3110 struct PeerConnection *connection_iter;
3111 struct ConnectContext *connect_context;
3112 uint32_t random_peer;
3114 if (ct_ctx->remaining_connections == 0)
3117 = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3118 while (pg->peers[random_peer].connect_peers_head == NULL)
3119 random_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3122 connection_iter = pg->peers[random_peer].connect_peers_head;
3123 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3124 connect_context->first_index = random_peer;
3125 connect_context->second_index = connection_iter->index;
3126 connect_context->ct_ctx = ct_ctx;
3127 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3128 GNUNET_CONTAINER_DLL_remove(pg->peers[random_peer].connect_peers_head, pg->peers[random_peer].connect_peers_tail, connection_iter);
3129 GNUNET_free(connection_iter);
3130 ct_ctx->remaining_connections--;
3134 /* Forward declaration */
3135 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3138 * Close connections and free the hello context.
3140 * @param cls the 'struct SendHelloContext *'
3141 * @param tc scheduler context
3144 free_hello_context (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3146 struct SendHelloContext *send_hello_context = cls;
3147 if (send_hello_context->peer->daemon->server != NULL)
3149 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3150 send_hello_context->peer->daemon->server = NULL;
3152 if (send_hello_context->peer->daemon->th != NULL)
3154 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3155 send_hello_context->peer->daemon->th = NULL;
3157 if (send_hello_context->core_connect_task != GNUNET_SCHEDULER_NO_TASK)
3159 GNUNET_SCHEDULER_cancel(send_hello_context->core_connect_task);
3160 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3162 send_hello_context->pg->outstanding_connects--;
3163 GNUNET_free(send_hello_context);
3167 * For peers that haven't yet connected, notify
3168 * the caller that they have failed (timeout).
3170 * @param cls the 'struct SendHelloContext *'
3171 * @param tc scheduler context
3174 notify_remaining_connections_failed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3176 struct SendHelloContext *send_hello_context = cls;
3177 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3178 struct PeerConnection *connection;
3180 GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3181 send_hello_context->peer->daemon->server = NULL;
3183 connection = send_hello_context->peer->connect_peers_head;
3185 while (connection != NULL)
3187 if (pg->notify_connection != NULL)
3189 pg->notify_connection(pg->notify_connection_cls,
3190 &send_hello_context->peer->daemon->id,
3191 &pg->peers[connection->index].daemon->id,
3193 send_hello_context->peer->daemon->cfg,
3194 pg->peers[connection->index].daemon->cfg,
3195 send_hello_context->peer->daemon,
3196 pg->peers[connection->index].daemon,
3197 "Peers failed to connect (timeout)");
3199 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3200 GNUNET_free(connection);
3201 connection = connection->next;
3203 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3205 other_peer = &pg->peers[connection->index];
3210 * For peers that haven't yet connected, send
3211 * CORE connect requests.
3213 * @param cls the 'struct SendHelloContext *'
3214 * @param tc scheduler context
3217 send_core_connect_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3219 struct SendHelloContext *send_hello_context = cls;
3220 struct PeerConnection *conn;
3221 GNUNET_assert(send_hello_context->peer->daemon->server != NULL);
3223 send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3225 send_hello_context->connect_attempts++;
3226 if (send_hello_context->connect_attempts < send_hello_context->pg->ct_ctx.connect_attempts)
3228 conn = send_hello_context->peer->connect_peers_head;
3229 while (conn != NULL)
3231 GNUNET_CORE_peer_request_connect(send_hello_context->peer->daemon->server,
3232 GNUNET_TIME_relative_get_forever(),
3233 &send_hello_context->pg->peers[conn->index].daemon->id,
3238 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) ,
3239 &send_core_connect_requests,
3240 send_hello_context);
3244 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Timeout before all connections created, marking rest as failed!\n");
3245 GNUNET_SCHEDULER_add_now(¬ify_remaining_connections_failed, send_hello_context);
3251 * Success, connection is up. Signal client our success.
3253 * @param cls our "struct SendHelloContext"
3254 * @param peer identity of the peer that has connected
3255 * @param atsi performance information
3257 * FIXME: remove peers from BOTH lists, call notify twice, should
3258 * double the speed of connections as long as the list iteration
3259 * doesn't take too long!
3262 core_connect_notify (void *cls,
3263 const struct GNUNET_PeerIdentity *peer,
3264 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3266 struct SendHelloContext *send_hello_context = cls;
3267 struct PeerConnection *connection;
3268 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3270 struct PeerData *other_peer;
3273 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3274 "Connected peer %s to peer %s\n",
3275 ctx->d1->shortname, GNUNET_i2s(peer));
3278 if (0 == memcmp(&send_hello_context->peer->daemon->id, peer, sizeof(struct GNUNET_PeerIdentity)))
3281 connection = send_hello_context->peer->connect_peers_head;
3286 while ((connection != NULL) &&
3287 (0 != memcmp(&pg->peers[connection->index].daemon->id, peer, sizeof(struct GNUNET_PeerIdentity))))
3289 connection = connection->next;
3292 if (connection == NULL)
3294 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);
3299 other_peer = &pg->peers[connection->index];
3301 if (pg->notify_connection != NULL)
3303 pg->notify_connection(pg->notify_connection_cls,
3304 &send_hello_context->peer->daemon->id,
3307 send_hello_context->peer->daemon->cfg,
3308 pg->peers[connection->index].daemon->cfg,
3309 send_hello_context->peer->daemon,
3310 pg->peers[connection->index].daemon,
3313 GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3314 GNUNET_free(connection);
3318 /* Notify of reverse connection and remove from other peers list of outstanding */
3319 if (other_peer != NULL)
3321 connection = other_peer->connect_peers_head;
3322 while ((connection != NULL) &&
3323 (0 != memcmp(&send_hello_context->peer->daemon->id, &pg->peers[connection->index].daemon->id, sizeof(struct GNUNET_PeerIdentity))))
3325 connection = connection->next;
3327 if (connection != NULL)
3329 if (pg->notify_connection != NULL)
3331 pg->notify_connection(pg->notify_connection_cls,
3333 &send_hello_context->peer->daemon->id,
3335 pg->peers[connection->index].daemon->cfg,
3336 send_hello_context->peer->daemon->cfg,
3337 pg->peers[connection->index].daemon,
3338 send_hello_context->peer->daemon,
3342 GNUNET_CONTAINER_DLL_remove(other_peer->connect_peers_head, other_peer->connect_peers_tail, connection);
3343 GNUNET_free(connection);
3348 if (send_hello_context->peer->connect_peers_head == NULL)
3350 GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3355 * Notify of a successful connection to the core service.
3357 * @param cls a struct SendHelloContext *
3358 * @param server handle to the core service
3359 * @param my_identity the peer identity of this peer
3360 * @param publicKey the public key of the peer
3363 core_init (void *cls,
3364 struct GNUNET_CORE_Handle * server,
3365 const struct GNUNET_PeerIdentity *
3368 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
3371 struct SendHelloContext *send_hello_context = cls;
3372 send_hello_context->core_ready = GNUNET_YES;
3376 * Function called once a hello has been sent
3377 * to the transport, move on to the next one
3378 * or go away forever.
3380 * @param cls the 'struct SendHelloContext *'
3381 * @param tc scheduler context
3384 hello_sent_callback (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3386 struct SendHelloContext *send_hello_context = cls;
3387 //unsigned int pg_iter;
3388 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3390 GNUNET_free(send_hello_context);
3394 send_hello_context->pg->remaining_hellos--;
3396 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sent HELLO, have %d remaining!\n", send_hello_context->pg->remaining_hellos);
3398 if (send_hello_context->peer_pos == NULL) /* All HELLOs (for this peer!) have been transmitted! */
3401 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All hellos for this peer sent, disconnecting transport!\n");
3403 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3404 GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3405 send_hello_context->peer->daemon->th = NULL;
3407 /*if (send_hello_context->pg->remaining_hellos == 0)
3409 for (pg_iter = 0; pg_iter < send_hello_context->pg->max_outstanding_connections; pg_iter++)
3411 preschedule_connect(&send_hello_context->pg->ct_ctx);
3415 GNUNET_assert (send_hello_context->peer->daemon->server == NULL);
3416 send_hello_context->peer->daemon->server = GNUNET_CORE_connect(send_hello_context->peer->cfg,
3420 &core_connect_notify,
3427 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),
3428 &send_core_connect_requests,
3429 send_hello_context);
3432 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3436 * Connect to a peer, give it all the HELLO's of those peers
3437 * we will later ask it to connect to.
3439 * @param ct_ctx the overall connection context
3441 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3443 struct SendHelloContext *send_hello_context = cls;
3444 struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3446 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3448 GNUNET_free(send_hello_context);
3452 GNUNET_assert(send_hello_context->peer_pos != NULL); /* All of the HELLO sends to be scheduled have been scheduled! */
3454 if (((send_hello_context->peer->daemon->th == NULL) &&
3455 (pg->outstanding_connects > pg->max_outstanding_connections)) ||
3456 (pg->stop_connects == GNUNET_YES))
3458 #if VERBOSE_TESTING > 2
3459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3461 ("Delaying connect, we have too many outstanding connections!\n"));
3463 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3464 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3465 &schedule_send_hellos, send_hello_context);
3469 #if VERBOSE_TESTING > 2
3470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3471 _("Creating connection, outstanding_connections is %d\n"),
3472 outstanding_connects);
3474 if (send_hello_context->peer->daemon->th == NULL)
3476 pg->outstanding_connects++; /* Actual TRANSPORT, CORE connections! */
3477 send_hello_context->peer->daemon->th = GNUNET_TRANSPORT_connect(send_hello_context->peer->cfg,
3485 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3486 _("Offering Hello of peer %s to peer %s\n"),
3487 send_hello_context->peer->daemon->shortname, pg->peers[send_hello_context->peer_pos->index].daemon->shortname);
3489 GNUNET_TRANSPORT_offer_hello(send_hello_context->peer->daemon->th,
3490 (const struct GNUNET_MessageHeader *)pg->peers[send_hello_context->peer_pos->index].daemon->hello,
3491 &hello_sent_callback,
3492 send_hello_context);
3493 send_hello_context->peer_pos = send_hello_context->peer_pos->next;
3494 GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3500 * Internal notification of a connection, kept so that we can ensure some connections
3501 * happen instead of flooding all testing daemons with requests to connect.
3504 internal_connect_notify(void *cls, const struct GNUNET_PeerIdentity *first,
3505 const struct GNUNET_PeerIdentity *second,
3507 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
3508 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
3509 struct GNUNET_TESTING_Daemon *first_daemon,
3510 struct GNUNET_TESTING_Daemon *second_daemon,
3513 struct ConnectContext *connect_ctx = cls;
3514 struct ConnectTopologyContext *ct_ctx = connect_ctx->ct_ctx;
3515 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
3516 struct PeerConnection *connection;
3517 pg->outstanding_connects--;
3520 * Check whether the inverse connection has been scheduled yet,
3521 * if not, we can remove it from the other peers list and avoid
3522 * even trying to connect them again!
3524 connection = pg->peers[connect_ctx->second_index].connect_peers_head;
3529 while ((connection != NULL) && (0
3530 != memcmp (first, &pg->peers[connection->index].daemon->id,
3531 sizeof(struct GNUNET_PeerIdentity))))
3533 connection = connection->next;
3536 if (connection != NULL) /* Can safely remove! */
3538 ct_ctx->remaining_connections--;
3539 if (pg->notify_connection != NULL) /* Notify of reverse connection */
3540 pg->notify_connection (pg->notify_connection_cls, second, first,
3541 distance, second_cfg, first_cfg, second_daemon,
3542 first_daemon, emsg);
3544 GNUNET_CONTAINER_DLL_remove(pg->peers[connect_ctx->second_index].connect_peers_head, pg->peers[connect_ctx->second_index].connect_peers_tail, connection);
3545 GNUNET_free(connection);
3548 if (ct_ctx->remaining_connections == 0)
3550 if (ct_ctx->notify_connections_done != NULL)
3551 ct_ctx->notify_connections_done (ct_ctx->notify_cls, NULL);
3554 preschedule_connect (pg);
3556 if (pg->notify_connection != NULL)
3557 pg->notify_connection (pg->notify_connection_cls, first, second, distance,
3558 first_cfg, second_cfg, first_daemon, second_daemon,
3561 GNUNET_free(connect_ctx);
3565 * Either delay a connection (because there are too many outstanding)
3566 * or schedule it for right now.
3568 * @param cls a connection context
3569 * @param tc the task runtime context
3572 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3574 struct ConnectContext *connect_context = cls;
3575 struct GNUNET_TESTING_PeerGroup *pg = connect_context->ct_ctx->pg;
3577 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3580 if ((pg->outstanding_connects > pg->max_outstanding_connections)
3581 || (pg->stop_connects == GNUNET_YES))
3584 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3586 ("Delaying connect, we have too many outstanding connections!\n"));
3588 GNUNET_SCHEDULER_add_delayed (
3589 GNUNET_TIME_relative_multiply (
3590 GNUNET_TIME_UNIT_MILLISECONDS,
3592 &schedule_connect, connect_context);
3597 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3598 _("Creating connection, outstanding_connections is %d (max %d)\n"),
3599 pg->outstanding_connects, pg->max_outstanding_connections);
3601 pg->outstanding_connects++;
3602 pg->total_connects_scheduled++;
3603 GNUNET_TESTING_daemons_connect (
3604 pg->peers[connect_context->first_index].daemon,
3605 pg->peers[connect_context->second_index].daemon,
3606 connect_context->ct_ctx->connect_timeout,
3607 connect_context->ct_ctx->connect_attempts,
3613 &internal_connect_notify, connect_context); /* FIXME: free connect context! */
3619 * Iterator for actually scheduling connections to be created
3620 * between two peers.
3622 * @param cls closure, a GNUNET_TESTING_Daemon
3623 * @param key the key the second Daemon was stored under
3624 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3626 * @return GNUNET_YES to continue iteration
3629 connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3631 struct ConnectTopologyContext *ct_ctx = cls;
3632 struct PeerData *first = ct_ctx->first;
3633 struct GNUNET_TESTING_Daemon *second = value;
3634 struct ConnectContext *connect_context;
3636 connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3637 connect_context->first = first->daemon;
3638 connect_context->second = second;
3639 connect_context->ct_ctx = ct_ctx;
3640 GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3648 * Iterator for copying all entries in the allowed hashmap to the
3651 * @param cls closure, a GNUNET_TESTING_Daemon
3652 * @param key the key the second Daemon was stored under
3653 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
3655 * @return GNUNET_YES to continue iteration
3658 copy_topology_iterator (void *cls, const GNUNET_HashCode * key, void *value)
3660 struct PeerData *first = cls;
3662 GNUNET_assert (GNUNET_OK ==
3663 GNUNET_CONTAINER_multihashmap_put (first->connect_peers, key,
3665 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3672 * Make the peers to connect the same as those that are allowed to be
3675 * @param pg the peer group
3678 copy_allowed_topology(struct GNUNET_TESTING_PeerGroup *pg)
3680 unsigned int pg_iter;
3684 struct PeerConnection *iter;
3688 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3691 iter = pg->peers[pg_iter].allowed_peers_head;
3692 while (iter != NULL)
3694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3695 "Creating connection between %d and %d\n", pg_iter,
3697 total += add_connections (pg, pg_iter, iter->index, CONNECT,
3699 //total += add_actual_connections(pg, pg_iter, iter->index);
3704 GNUNET_CONTAINER_multihashmap_iterate (pg->
3705 peers[pg_iter].allowed_peers,
3706 ©_topology_iterator,
3707 &pg->peers[pg_iter]);
3709 if (GNUNET_SYSERR == ret)
3710 return GNUNET_SYSERR;
3712 total = total + ret;
3719 * Connect the topology as specified by the PeerConnection's
3720 * of each peer in the peer group
3722 * @param pg the peer group we are dealing with
3723 * @param connect_timeout how long try connecting two peers
3724 * @param connect_attempts how many times (max) to attempt
3725 * @param notify_callback callback to notify when finished
3726 * @param notify_cls closure for notify callback
3728 * @return the number of connections that will be attempted
3731 connect_topology(struct GNUNET_TESTING_PeerGroup *pg,
3732 struct GNUNET_TIME_Relative connect_timeout,
3733 unsigned int connect_attempts,
3734 GNUNET_TESTING_NotifyCompletion notify_callback,
3737 unsigned int pg_iter;
3741 struct PeerConnection *connection_iter;
3744 struct SendHelloContext *send_hello_context
3748 pg->ct_ctx.notify_connections_done = notify_callback;
3749 pg->ct_ctx.notify_cls = notify_cls;
3752 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3755 connection_iter = pg->peers[pg_iter].connect_peers_head;
3756 while (connection_iter != NULL)
3758 connection_iter = connection_iter->next;
3763 GNUNET_CONTAINER_multihashmap_size (pg->peers[pg_iter].connect_peers);
3770 pg->ct_ctx.connect_timeout = connect_timeout;
3771 pg->ct_ctx.connect_attempts = connect_attempts;
3772 pg->ct_ctx.remaining_connections = total;
3775 /* First give all peers the HELLO's of other peers (connect to first peer's transport service, give HELLO's of other peers, continue...) */
3776 pg->remaining_hellos = total;
3777 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3779 send_hello_context = GNUNET_malloc(sizeof(struct SendHelloContext));
3780 send_hello_context->peer = &pg->peers[pg_iter];
3781 send_hello_context->peer_pos = pg->peers[pg_iter].connect_peers_head;
3782 send_hello_context->pg = pg;
3783 GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3786 for (pg_iter = 0; pg_iter < pg->max_outstanding_connections; pg_iter++)
3788 preschedule_connect (pg);
3796 * Takes a peer group and creates a topology based on the
3797 * one specified. Creates a topology means generates friend
3798 * files for the peers so they can only connect to those allowed
3799 * by the topology. This will only have an effect once peers
3800 * are started if the FRIENDS_ONLY option is set in the base
3801 * config. Also takes an optional restrict topology which
3802 * disallows connections based on particular transports
3803 * UNLESS they are specified in the restricted topology.
3805 * @param pg the peer group struct representing the running peers
3806 * @param topology which topology to connect the peers in
3807 * @param restrict_topology disallow restrict_transports transport
3808 * connections to peers NOT in this topology
3809 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
3810 * @param restrict_transports space delimited list of transports to blacklist
3811 * to create restricted topology
3813 * @return the maximum number of connections were all allowed peers
3814 * connected to each other
3817 GNUNET_TESTING_create_topology(struct GNUNET_TESTING_PeerGroup *pg,
3818 enum GNUNET_TESTING_Topology topology,
3819 enum GNUNET_TESTING_Topology restrict_topology,
3820 const char *restrict_transports)
3824 unsigned int num_connections;
3825 int unblacklisted_connections;
3827 struct PeerConnection *conn_iter;
3828 struct PeerConnection *temp_conn;
3833 for (i = 0; i < pg->total; i++)
3835 pg->peers[i].allowed_peers =
3836 GNUNET_CONTAINER_multihashmap_create (100);
3837 pg->peers[i].connect_peers =
3838 GNUNET_CONTAINER_multihashmap_create (100);
3839 pg->peers[i].blacklisted_peers =
3840 GNUNET_CONTAINER_multihashmap_create (100);
3841 pg->peers[i].pg = pg;
3847 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating clique topology\n"));
3851 num_connections = create_clique (pg, &add_connections, ALLOWED, GNUNET_NO);
3853 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3856 _("Creating small world (ring) topology\n"));
3858 num_connections = create_small_world_ring (pg, &add_connections, ALLOWED);
3860 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3863 _("Creating small world (2d-torus) topology\n"));
3865 num_connections = create_small_world (pg, &add_connections, ALLOWED);
3867 case GNUNET_TESTING_TOPOLOGY_RING:
3869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring topology\n"));
3871 num_connections = create_ring (pg, &add_connections, ALLOWED);
3873 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating 2d torus topology\n"));
3877 num_connections = create_2d_torus (pg, &add_connections, ALLOWED);
3879 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3882 _("Creating Erdos-Renyi topology\n"));
3884 num_connections = create_erdos_renyi (pg, &add_connections, ALLOWED);
3886 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating InterNAT topology\n"));
3890 num_connections = create_nated_internet (pg, &add_connections, ALLOWED);
3892 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3895 _("Creating Scale Free topology\n"));
3897 num_connections = create_scale_free (pg, &add_connections, ALLOWED);
3899 case GNUNET_TESTING_TOPOLOGY_LINE:
3901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3902 _("Creating straight line topology\n"));
3904 num_connections = create_line (pg, &add_connections, ALLOWED);
3906 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
3908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3909 _("Creating topology from file!\n"));
3911 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "testing",
3914 num_connections = create_from_file (pg, filename, &add_connections,
3919 GNUNET_ERROR_TYPE_WARNING,
3920 "Missing configuration option TESTING:TOPOLOGY_FILE for creating topology from file!\n");
3921 num_connections = 0;
3924 case GNUNET_TESTING_TOPOLOGY_NONE:
3926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3928 ("Creating no allowed topology (all peers can connect at core level)\n"));
3930 num_connections = pg->total * pg->total; /* Clique is allowed! */
3933 num_connections = 0;
3937 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING",
3940 ret = create_and_copy_friend_files (pg);
3941 if (ret != GNUNET_OK)
3944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3945 _("Failed during friend file copying!\n"));
3947 return GNUNET_SYSERR;
3952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3953 _("Friend files created/copied successfully!\n"));
3958 /* Use the create clique method to initially set all connections as blacklisted. */
3959 if ((restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE) && (restrict_topology
3960 != GNUNET_TESTING_TOPOLOGY_FROM_FILE))
3961 create_clique (pg, &add_connections, BLACKLIST, GNUNET_NO);
3963 unblacklisted_connections = 0;
3964 /* Un-blacklist connections as per the topology specified */
3965 switch (restrict_topology)
3967 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3970 _("Blacklisting all but clique topology\n"));
3972 unblacklisted_connections = create_clique (pg, &remove_connections,
3973 BLACKLIST, GNUNET_NO);
3975 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3978 _("Blacklisting all but small world (ring) topology\n"));
3980 unblacklisted_connections = create_small_world_ring (pg,
3981 &remove_connections,
3984 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3988 ("Blacklisting all but small world (2d-torus) topology\n"));
3990 unblacklisted_connections = create_small_world (pg, &remove_connections,
3993 case GNUNET_TESTING_TOPOLOGY_RING:
3995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3996 _("Blacklisting all but ring topology\n"));
3998 unblacklisted_connections
3999 = create_ring (pg, &remove_connections, BLACKLIST);
4001 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
4003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4004 _("Blacklisting all but 2d torus topology\n"));
4006 unblacklisted_connections = create_2d_torus (pg, &remove_connections,
4009 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
4011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4012 _("Blacklisting all but Erdos-Renyi topology\n"));
4014 unblacklisted_connections = create_erdos_renyi (pg, &remove_connections,
4017 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
4019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4020 _("Blacklisting all but InterNAT topology\n"));
4024 for (off = 0; off < pg->total; off++)
4026 conn_iter = pg->peers[off].allowed_peers_head;
4027 while (conn_iter != NULL)
4029 temp_conn = conn_iter->next;
4030 GNUNET_free(conn_iter);
4031 conn_iter = temp_conn;
4033 pg->peers[off].allowed_peers_head = NULL;
4034 pg->peers[off].allowed_peers_tail = NULL;
4036 conn_iter = pg->peers[off].connect_peers_head;
4037 while (conn_iter != NULL)
4039 temp_conn = conn_iter->next;
4040 GNUNET_free(conn_iter);
4041 conn_iter = temp_conn;
4043 pg->peers[off].connect_peers_head = NULL;
4044 pg->peers[off].connect_peers_tail = NULL;
4046 unblacklisted_connections
4047 = create_nated_internet_copy (pg, &remove_connections, BLACKLIST);
4049 unblacklisted_connections =
4050 create_nated_internet (pg, &remove_connections, BLACKLIST);
4054 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
4056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4057 _("Blacklisting all but Scale Free topology\n"));
4059 unblacklisted_connections = create_scale_free (pg, &remove_connections,
4062 case GNUNET_TESTING_TOPOLOGY_LINE:
4064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4065 _("Blacklisting all but straight line topology\n"));
4067 unblacklisted_connections
4068 = create_line (pg, &remove_connections, BLACKLIST);
4070 case GNUNET_TESTING_TOPOLOGY_NONE: /* Fall through */
4071 case GNUNET_TESTING_TOPOLOGY_FROM_FILE:
4073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4075 ("Creating no blacklist topology (all peers can connect at transport level)\n"));
4077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
4078 ("Creating blacklist topology from allowed\n"));
4079 unblacklisted_connections = copy_allowed (pg, &remove_connections);
4084 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
4086 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating blacklist with `%s'\n",
4087 restrict_transports);
4088 ret = create_and_copy_blacklist_files (pg, restrict_transports);
4089 if (ret != GNUNET_OK)
4092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4093 _("Failed during blacklist file copying!\n"));
4100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4101 _("Blacklist files created/copied successfully!\n"));
4105 return num_connections;
4110 * Iterator for choosing random peers to connect.
4112 * @param cls closure, a RandomContext
4113 * @param key the key the second Daemon was stored under
4114 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4116 * @return GNUNET_YES to continue iteration
4119 random_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4121 struct RandomContext *random_ctx = cls;
4122 double random_number;
4123 uint32_t second_pos;
4124 GNUNET_HashCode first_hash;
4127 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4128 UINT64_MAX)) / ((double) UINT64_MAX);
4129 if (random_number < random_ctx->percentage)
4131 GNUNET_assert (GNUNET_OK ==
4132 GNUNET_CONTAINER_multihashmap_put (random_ctx->
4133 first->connect_peers_working_set,
4135 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4138 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
4139 uid_from_hash (key, &second_pos);
4140 hash_from_uid (random_ctx->first_uid, &first_hash);
4141 GNUNET_assert (random_ctx->pg->total > second_pos);
4142 GNUNET_assert (GNUNET_YES ==
4143 GNUNET_CONTAINER_multihashmap_remove (random_ctx->
4145 [second_pos].connect_peers,
4154 * Iterator for adding at least X peers to a peers connection set.
4156 * @param cls closure, MinimumContext
4157 * @param key the key the second Daemon was stored under
4158 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4160 * @return GNUNET_YES to continue iteration
4163 minimum_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4165 struct MinimumContext *min_ctx = cls;
4166 uint32_t second_pos;
4167 GNUNET_HashCode first_hash;
4170 if (GNUNET_CONTAINER_multihashmap_size
4171 (min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
4173 for (i = 0; i < min_ctx->num_to_add; i++)
4175 if (min_ctx->pg_array[i] == min_ctx->current)
4177 GNUNET_assert (GNUNET_OK ==
4178 GNUNET_CONTAINER_multihashmap_put
4179 (min_ctx->first->connect_peers_working_set, key,
4181 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4182 uid_from_hash (key, &second_pos);
4183 hash_from_uid (min_ctx->first_uid, &first_hash);
4184 GNUNET_assert (min_ctx->pg->total > second_pos);
4185 GNUNET_assert (GNUNET_OK ==
4186 GNUNET_CONTAINER_multihashmap_put (min_ctx->
4188 [second_pos].connect_peers_working_set,
4192 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4193 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
4194 GNUNET_assert (GNUNET_YES ==
4195 GNUNET_CONTAINER_multihashmap_remove
4196 (min_ctx->pg->peers[second_pos].connect_peers,
4197 &first_hash, min_ctx->first->daemon));
4204 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
4209 * Iterator for adding peers to a connection set based on a depth first search.
4211 * @param cls closure, MinimumContext
4212 * @param key the key the second daemon was stored under
4213 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
4215 * @return GNUNET_YES to continue iteration
4218 dfs_connect_iterator (void *cls, const GNUNET_HashCode * key, void *value)
4220 struct DFSContext *dfs_ctx = cls;
4221 GNUNET_HashCode first_hash;
4223 if (dfs_ctx->current == dfs_ctx->chosen)
4225 GNUNET_assert (GNUNET_OK ==
4226 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4227 first->connect_peers_working_set,
4229 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4230 uid_from_hash (key, &dfs_ctx->second_uid);
4231 hash_from_uid (dfs_ctx->first_uid, &first_hash);
4232 GNUNET_assert (GNUNET_OK ==
4233 GNUNET_CONTAINER_multihashmap_put (dfs_ctx->
4235 [dfs_ctx->second_uid].connect_peers_working_set,
4239 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4240 GNUNET_assert (GNUNET_YES ==
4241 GNUNET_CONTAINER_multihashmap_remove (dfs_ctx->
4243 [dfs_ctx->second_uid].connect_peers,
4247 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
4248 return GNUNET_NO; /* We have found our peer, don't iterate more */
4257 * From the set of connections possible, choose percentage percent of connections
4258 * to actually connect.
4260 * @param pg the peergroup we are dealing with
4261 * @param percentage what percent of total connections to make
4264 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg,
4267 struct RandomContext random_ctx;
4270 struct PeerConnection *temp_peers;
4271 struct PeerConnection *conn_iter;
4272 double random_number;
4275 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4277 random_ctx.first_uid = pg_iter;
4278 random_ctx.first = &pg->peers[pg_iter];
4279 random_ctx.percentage = percentage;
4283 conn_iter = pg->peers[pg_iter].connect_peers_head;
4284 while (conn_iter != NULL)
4287 = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
4289 / ((double) UINT64_MAX);
4290 if (random_number < percentage)
4292 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4295 conn_iter = conn_iter->next;
4298 pg->peers[pg_iter].connect_peers_working_set =
4299 GNUNET_CONTAINER_multihashmap_create (pg->total);
4300 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4301 &random_connect_iterator,
4303 /* Now remove the old connections */
4304 GNUNET_CONTAINER_multihashmap_destroy (pg->
4305 peers[pg_iter].connect_peers);
4306 /* And replace with the random set */
4307 pg->peers[pg_iter].connect_peers =
4308 pg->peers[pg_iter].connect_peers_working_set;
4312 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4314 conn_iter = pg->peers[pg_iter].connect_peers_head;
4315 while (pg->peers[pg_iter].connect_peers_head != NULL)
4316 remove_connections (pg, pg_iter,
4317 pg->peers[pg_iter].connect_peers_head->index,
4318 CONNECT, GNUNET_YES);
4320 pg->peers[pg_iter].connect_peers_head
4321 = pg->peers[pg_iter].connect_peers_working_set_head;
4322 pg->peers[pg_iter].connect_peers_tail
4323 = pg->peers[pg_iter].connect_peers_working_set_tail;
4324 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4325 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4330 * Count the number of connections in a linked list of connections.
4332 * @param conn_list the connection list to get the count of
4334 * @return the number of elements in the list
4337 count_connections(struct PeerConnection *conn_list)
4339 struct PeerConnection *iter;
4343 while (iter != NULL)
4352 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
4355 unsigned int pg_iter;
4357 struct PeerConnection *conn_iter;
4361 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4364 conn_iter = pg->peers[pg_iter].connect_peers_working_set_head;
4365 while (conn_iter != NULL)
4368 conn_iter = conn_iter->next;
4372 GNUNET_CONTAINER_multihashmap_size (pg->
4374 [pg_iter].connect_peers_working_set);
4382 count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
4385 unsigned int pg_iter;
4387 struct PeerConnection *conn_iter;
4391 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4394 conn_iter = pg->peers[pg_iter].allowed_peers_head;
4395 while (conn_iter != NULL)
4398 conn_iter = conn_iter->next;
4402 GNUNET_CONTAINER_multihashmap_size (pg->
4404 [pg_iter].allowed_peers);
4412 * From the set of connections possible, choose at least num connections per
4415 * @param pg the peergroup we are dealing with
4416 * @param num how many connections at least should each peer have (if possible)?
4419 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4422 struct MinimumContext minimum_ctx;
4424 struct PeerConnection *conn_iter;
4425 unsigned int temp_list_size;
4428 uint32_t random; /* Random list entry to connect peer to */
4433 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4436 = count_connections (pg->peers[pg_iter].connect_peers_head);
4437 if (temp_list_size == 0)
4439 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4440 "Peer %d has 0 connections!?!?\n", pg_iter);
4443 for (i = 0; i < num; i++)
4445 random = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4447 conn_iter = pg->peers[pg_iter].connect_peers_head;
4448 for (count = 0; count < random; count++)
4449 conn_iter = conn_iter->next;
4450 /* We now have a random connection, connect it! */
4451 GNUNET_assert(conn_iter != NULL);
4452 add_connections (pg, pg_iter, conn_iter->index, WORKING_SET,
4457 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4459 pg->peers[pg_iter].connect_peers_working_set =
4460 GNUNET_CONTAINER_multihashmap_create (num);
4463 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4465 minimum_ctx.first_uid = pg_iter;
4466 minimum_ctx.pg_array =
4467 GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
4468 GNUNET_CONTAINER_multihashmap_size
4469 (pg->peers[pg_iter].connect_peers));
4470 minimum_ctx.first = &pg->peers[pg_iter];
4471 minimum_ctx.pg = pg;
4472 minimum_ctx.num_to_add = num;
4473 minimum_ctx.current = 0;
4474 GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].connect_peers,
4475 &minimum_connect_iterator,
4479 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4481 /* Remove the "old" connections */
4482 GNUNET_CONTAINER_multihashmap_destroy (pg->
4483 peers[pg_iter].connect_peers);
4484 /* And replace with the working set */
4485 pg->peers[pg_iter].connect_peers =
4486 pg->peers[pg_iter].connect_peers_working_set;
4489 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4491 while (pg->peers[pg_iter].connect_peers_head != NULL)
4493 conn_iter = pg->peers[pg_iter].connect_peers_head;
4494 GNUNET_CONTAINER_DLL_remove(pg->peers[pg_iter].connect_peers_head,
4495 pg->peers[pg_iter].connect_peers_tail,
4497 GNUNET_free(conn_iter);
4498 /*remove_connections(pg, pg_iter, pg->peers[pg_iter].connect_peers_head->index, CONNECT, GNUNET_YES);*/
4501 pg->peers[pg_iter].connect_peers_head
4502 = pg->peers[pg_iter].connect_peers_working_set_head;
4503 pg->peers[pg_iter].connect_peers_tail
4504 = pg->peers[pg_iter].connect_peers_working_set_tail;
4505 pg->peers[pg_iter].connect_peers_working_set_head = NULL;
4506 pg->peers[pg_iter].connect_peers_working_set_tail = NULL;
4511 struct FindClosestContext
4514 * The currently known closest peer.
4516 struct GNUNET_TESTING_Daemon *closest;
4519 * The info for the peer we are adding connections for.
4521 struct PeerData *curr_peer;
4524 * The distance (bits) between the current
4525 * peer and the currently known closest.
4527 unsigned int closest_dist;
4530 * The offset of the closest known peer in
4533 unsigned int closest_num;
4537 * Iterator over hash map entries of the allowed
4538 * peer connections. Find the closest, not already
4539 * connected peer and return it.
4541 * @param cls closure (struct FindClosestContext)
4542 * @param key current key code (hash of offset in pg)
4543 * @param value value in the hash map - a GNUNET_TESTING_Daemon
4544 * @return GNUNET_YES if we should continue to
4549 find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
4551 struct FindClosestContext *closest_ctx = cls;
4552 struct GNUNET_TESTING_Daemon *daemon = value;
4554 if (((closest_ctx->closest == NULL) ||
4555 (GNUNET_CRYPTO_hash_matching_bits
4556 (&daemon->id.hashPubKey,
4557 &closest_ctx->curr_peer->daemon->id.hashPubKey) >
4558 closest_ctx->closest_dist))
4560 GNUNET_CONTAINER_multihashmap_contains (closest_ctx->
4561 curr_peer->connect_peers,
4564 closest_ctx->closest_dist =
4565 GNUNET_CRYPTO_hash_matching_bits (&daemon->id.hashPubKey,
4566 &closest_ctx->curr_peer->daemon->
4568 closest_ctx->closest = daemon;
4569 uid_from_hash (key, &closest_ctx->closest_num);
4575 * From the set of connections possible, choose at num connections per
4576 * peer based on depth which are closest out of those allowed. Guaranteed
4577 * to add num peers to connect to, provided there are that many peers
4578 * in the underlay topology to connect to.
4580 * @param pg the peergroup we are dealing with
4581 * @param num how many connections at least should each peer have (if possible)?
4582 * @param proc processor to actually add the connections
4583 * @param list the peer list to use
4586 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num,
4587 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
4592 struct FindClosestContext closest_ctx;
4597 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
4599 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4601 closest_ctx.curr_peer = &pg->peers[pg_iter];
4602 closest_ctx.closest = NULL;
4603 closest_ctx.closest_dist = 0;
4604 closest_ctx.closest_num = 0;
4605 GNUNET_CONTAINER_multihashmap_iterate (pg->
4606 peers[pg_iter].allowed_peers,
4607 &find_closest_peers,
4609 if (closest_ctx.closest != NULL)
4611 GNUNET_assert (closest_ctx.closest_num < pg->total);
4612 proc (pg, pg_iter, closest_ctx.closest_num, list);
4620 * From the set of connections possible, choose at least num connections per
4621 * peer based on depth first traversal of peer connections. If DFS leaves
4622 * peers unconnected, ensure those peers get connections.
4624 * @param pg the peergroup we are dealing with
4625 * @param num how many connections at least should each peer have (if possible)?
4628 perform_dfs(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
4632 uint32_t starting_peer;
4633 uint32_t least_connections;
4634 uint32_t random_connection;
4636 unsigned int temp_count;
4637 struct PeerConnection *peer_iter;
4639 struct DFSContext dfs_ctx;
4640 GNUNET_HashCode second_hash;
4646 while ((count_workingset_connections (pg) < num * pg->total)
4647 && (count_allowed_connections (pg) > 0))
4649 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4651 least_connections = -1; /* Set to very high number */
4652 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4655 = count_connections (
4656 pg->peers[pg_iter].connect_peers_working_set_head);
4657 if (temp_count < least_connections)
4659 starting_peer = pg_iter;
4660 least_connections = temp_count;
4666 = count_connections (pg->peers[starting_peer].connect_peers_head);
4667 if (temp_count == 0)
4668 continue; /* FIXME: infinite loop? */
4670 random_connection = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4673 peer_iter = pg->peers[starting_peer].connect_peers_head;
4674 while (temp_count < random_connection)
4676 peer_iter = peer_iter->next;
4679 GNUNET_assert(peer_iter != NULL);
4680 add_connections (pg, starting_peer, peer_iter->index, WORKING_SET,
4682 remove_connections (pg, starting_peer, peer_iter->index, CONNECT,
4684 starting_peer = peer_iter->index;
4689 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4691 pg->peers[pg_iter].connect_peers_working_set =
4692 GNUNET_CONTAINER_multihashmap_create (num);
4697 while ((count_workingset_connections (pg) < num * pg->total)
4698 && (count_allowed_connections (pg) > 0))
4700 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
4702 least_connections = -1; /* Set to very high number */
4703 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4705 if (GNUNET_CONTAINER_multihashmap_size
4706 (pg->peers[pg_iter].connect_peers_working_set) <
4709 starting_peer = pg_iter;
4711 GNUNET_CONTAINER_multihashmap_size (pg->
4713 [pg_iter].connect_peers_working_set);
4718 if (GNUNET_CONTAINER_multihashmap_size (pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
4724 /* Choose a random peer from the chosen peers set of connections to add */
4726 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4727 GNUNET_CONTAINER_multihashmap_size
4728 (pg->peers[starting_peer].connect_peers));
4729 dfs_ctx.first_uid = starting_peer;
4730 dfs_ctx.first = &pg->peers[starting_peer];
4732 dfs_ctx.current = 0;
4734 GNUNET_CONTAINER_multihashmap_iterate (pg->
4736 [starting_peer].connect_peers,
4737 &dfs_connect_iterator, &dfs_ctx);
4738 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
4739 hash_from_uid (dfs_ctx.second_uid, &second_hash);
4740 GNUNET_assert (GNUNET_YES ==
4741 GNUNET_CONTAINER_multihashmap_remove (pg->peers
4742 [starting_peer].connect_peers,
4746 [dfs_ctx.second_uid].daemon));
4747 starting_peer = dfs_ctx.second_uid;
4750 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
4752 /* Remove the "old" connections */
4753 GNUNET_CONTAINER_multihashmap_destroy (pg->
4754 peers[pg_iter].connect_peers);
4755 /* And replace with the working set */
4756 pg->peers[pg_iter].connect_peers =
4757 pg->peers[pg_iter].connect_peers_working_set;
4763 * Internal callback for topology information for a particular peer.
4766 internal_topology_callback(void *cls, const struct GNUNET_PeerIdentity *peer,
4767 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4769 struct CoreContext *core_ctx = cls;
4770 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
4772 if (peer == NULL) /* Either finished, or something went wrong */
4774 iter_ctx->completed++;
4775 iter_ctx->connected--;
4776 /* One core context allocated per iteration, must free! */
4777 GNUNET_free (core_ctx);
4781 iter_ctx->topology_cb (iter_ctx->cls, &core_ctx->daemon->id, peer, NULL);
4784 if (iter_ctx->completed == iter_ctx->total)
4786 iter_ctx->topology_cb (iter_ctx->cls, NULL, NULL, NULL);
4787 /* Once all are done, free the iteration context */
4788 GNUNET_free (iter_ctx);
4793 * Check running topology iteration tasks, if below max start a new one, otherwise
4794 * schedule for some time in the future.
4797 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4799 struct CoreContext *core_context = cls;
4800 struct TopologyIterateContext *topology_context =
4801 (struct TopologyIterateContext *) core_context->iter_context;
4802 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4805 if (topology_context->connected
4806 > topology_context->pg->max_outstanding_connections)
4808 #if VERBOSE_TESTING > 2
4809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4811 ("Delaying connect, we have too many outstanding connections!\n"));
4813 GNUNET_SCHEDULER_add_delayed (
4814 GNUNET_TIME_relative_multiply (
4815 GNUNET_TIME_UNIT_MILLISECONDS,
4817 &schedule_get_topology, core_context);
4821 #if VERBOSE_TESTING > 2
4822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4823 _("Creating connection, outstanding_connections is %d\n"),
4824 outstanding_connects);
4826 topology_context->connected++;
4828 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->cfg,
4829 &internal_topology_callback,
4832 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Topology iteration failed.\n");
4833 internal_topology_callback (core_context, NULL, NULL);
4839 * Iterate over all (running) peers in the peer group, retrieve
4840 * all connections that each currently has.
4843 GNUNET_TESTING_get_topology(struct GNUNET_TESTING_PeerGroup *pg,
4844 GNUNET_TESTING_NotifyTopology cb, void *cls)
4846 struct TopologyIterateContext *topology_context;
4847 struct CoreContext *core_ctx;
4849 unsigned int total_count;
4851 /* Allocate a single topology iteration context */
4852 topology_context = GNUNET_malloc (sizeof (struct TopologyIterateContext));
4853 topology_context->topology_cb = cb;
4854 topology_context->cls = cls;
4855 topology_context->pg = pg;
4857 for (i = 0; i < pg->total; i++)
4859 if (pg->peers[i].daemon->running == GNUNET_YES)
4861 /* Allocate one core context per core we need to connect to */
4862 core_ctx = GNUNET_malloc (sizeof (struct CoreContext));
4863 core_ctx->daemon = pg->peers[i].daemon;
4864 /* Set back pointer to topology iteration context */
4865 core_ctx->iter_context = topology_context;
4866 GNUNET_SCHEDULER_add_now (&schedule_get_topology, core_ctx);
4870 if (total_count == 0)
4872 cb (cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
4873 GNUNET_free (topology_context);
4876 topology_context->total = total_count;
4881 * Callback function to process statistic values.
4882 * This handler is here only really to insert a peer
4883 * identity (or daemon) so the statistics can be uniquely
4884 * tied to a single running peer.
4886 * @param cls closure
4887 * @param subsystem name of subsystem that created the statistic
4888 * @param name the name of the datum
4889 * @param value the current value
4890 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
4891 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
4894 internal_stats_callback(void *cls, const char *subsystem, const char *name,
4895 uint64_t value, int is_persistent)
4897 struct StatsCoreContext *core_context = cls;
4898 struct StatsIterateContext *stats_context =
4899 (struct StatsIterateContext *) core_context->iter_context;
4901 return stats_context->proc (stats_context->cls, &core_context->daemon->id,
4902 subsystem, name, value, is_persistent);
4906 * Internal continuation call for statistics iteration.
4908 * @param cls closure, the CoreContext for this iteration
4909 * @param success whether or not the statistics iterations
4910 * was canceled or not (we don't care)
4913 internal_stats_cont(void *cls, int success)
4915 struct StatsCoreContext *core_context = cls;
4916 struct StatsIterateContext *stats_context =
4917 (struct StatsIterateContext *) core_context->iter_context;
4919 stats_context->connected--;
4920 stats_context->completed++;
4922 if (stats_context->completed == stats_context->total)
4924 stats_context->cont (stats_context->cls, GNUNET_YES);
4925 GNUNET_free (stats_context);
4928 if (core_context->stats_handle != NULL)
4929 GNUNET_STATISTICS_destroy (core_context->stats_handle, GNUNET_NO);
4931 GNUNET_free (core_context);
4935 * Check running topology iteration tasks, if below max start a new one, otherwise
4936 * schedule for some time in the future.
4939 schedule_get_statistics(void *cls,
4940 const struct GNUNET_SCHEDULER_TaskContext *tc)
4942 struct StatsCoreContext *core_context = cls;
4943 struct StatsIterateContext *stats_context =
4944 (struct StatsIterateContext *) core_context->iter_context;
4946 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4949 if (stats_context->connected > stats_context->pg->max_outstanding_connections)
4951 #if VERBOSE_TESTING > 2
4952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4954 ("Delaying connect, we have too many outstanding connections!\n"));
4956 GNUNET_SCHEDULER_add_delayed (
4957 GNUNET_TIME_relative_multiply (
4958 GNUNET_TIME_UNIT_MILLISECONDS,
4960 &schedule_get_statistics, core_context);
4964 #if VERBOSE_TESTING > 2
4965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4966 _("Creating connection, outstanding_connections is %d\n"),
4967 outstanding_connects);
4970 stats_context->connected++;
4971 core_context->stats_handle
4972 = GNUNET_STATISTICS_create ("testing", core_context->daemon->cfg);
4973 if (core_context->stats_handle == NULL)
4975 internal_stats_cont (core_context, GNUNET_NO);
4979 core_context->stats_get_handle
4980 = GNUNET_STATISTICS_get (core_context->stats_handle, NULL, NULL,
4981 GNUNET_TIME_relative_get_forever (),
4982 &internal_stats_cont,
4983 &internal_stats_callback, core_context);
4984 if (core_context->stats_get_handle == NULL)
4985 internal_stats_cont (core_context, GNUNET_NO);
4990 struct DuplicateStats
4993 * Next item in the list
4995 struct DuplicateStats *next;
4998 * Nasty string, concatenation of relevant information.
5000 char *unique_string;
5004 * Check whether the combination of port/host/unix domain socket
5005 * already exists in the list of peers being checked for statistics.
5007 * @param pg the peergroup in question
5008 * @param specific_peer the peer we're concerned with
5009 * @param stats_list the list to return to the caller
5011 * @return GNUNET_YES if the statistics instance has been seen already,
5012 * GNUNET_NO if not (and we may have added it to the list)
5015 stats_check_existing(struct GNUNET_TESTING_PeerGroup *pg,
5016 struct PeerData *specific_peer,
5017 struct DuplicateStats **stats_list)
5019 struct DuplicateStats *pos;
5020 char *unix_domain_socket;
5021 unsigned long long port;
5024 != GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "testing",
5025 "single_statistics_per_host"))
5026 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
5029 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (specific_peer->cfg,
5032 &unix_domain_socket))
5035 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (specific_peer->cfg,
5036 "statistics", "port",
5039 GNUNET_free(unix_domain_socket);
5043 if (specific_peer->daemon->hostname != NULL)
5044 GNUNET_asprintf (&to_match, "%s%s%llu", specific_peer->daemon->hostname,
5045 unix_domain_socket, port);
5047 GNUNET_asprintf (&to_match, "%s%llu", unix_domain_socket, port);
5051 if (0 == strcmp (to_match, pos->unique_string))
5053 GNUNET_free (unix_domain_socket);
5054 GNUNET_free (to_match);
5059 pos = GNUNET_malloc (sizeof (struct DuplicateStats));
5060 pos->unique_string = to_match;
5061 pos->next = *stats_list;
5063 GNUNET_free (unix_domain_socket);
5068 * Iterate over all (running) peers in the peer group, retrieve
5069 * all statistics from each.
5072 GNUNET_TESTING_get_statistics(struct GNUNET_TESTING_PeerGroup *pg,
5073 GNUNET_STATISTICS_Callback cont,
5074 GNUNET_TESTING_STATISTICS_Iterator proc,
5077 struct StatsIterateContext *stats_context;
5078 struct StatsCoreContext *core_ctx;
5080 unsigned int total_count;
5081 struct DuplicateStats *stats_list;
5082 struct DuplicateStats *pos;
5085 /* Allocate a single stats iteration context */
5086 stats_context = GNUNET_malloc (sizeof (struct StatsIterateContext));
5087 stats_context->cont = cont;
5088 stats_context->proc = proc;
5089 stats_context->cls = cls;
5090 stats_context->pg = pg;
5093 for (i = 0; i < pg->total; i++)
5095 if ((pg->peers[i].daemon->running == GNUNET_YES) && (GNUNET_NO
5096 == stats_check_existing (pg, &pg->peers[i], &stats_list)))
5098 /* Allocate one core context per core we need to connect to */
5099 core_ctx = GNUNET_malloc (sizeof (struct StatsCoreContext));
5100 core_ctx->daemon = pg->peers[i].daemon;
5101 /* Set back pointer to topology iteration context */
5102 core_ctx->iter_context = stats_context;
5103 GNUNET_SCHEDULER_add_now (&schedule_get_statistics, core_ctx);
5108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5109 "Retrieving stats from %u total instances.\n", total_count);
5110 stats_context->total = total_count;
5111 if (stats_list != NULL)
5116 GNUNET_free (pos->unique_string);
5117 stats_list = pos->next;
5119 pos = stats_list->next;
5126 * Stop the connection process temporarily.
5128 * @param pg the peer group to stop connecting
5131 GNUNET_TESTING_stop_connections(struct GNUNET_TESTING_PeerGroup *pg)
5133 pg->stop_connects = GNUNET_YES;
5137 * Resume the connection process temporarily.
5139 * @param pg the peer group to resume connecting
5142 GNUNET_TESTING_resume_connections(struct GNUNET_TESTING_PeerGroup *pg)
5144 pg->stop_connects = GNUNET_NO;
5148 * There are many ways to connect peers that are supported by this function.
5149 * To connect peers in the same topology that was created via the
5150 * GNUNET_TESTING_create_topology, the topology variable must be set to
5151 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
5152 * a new instance of that topology will be generated and attempted to be
5153 * connected. This could result in some connections being impossible,
5154 * because some topologies are non-deterministic.
5156 * @param pg the peer group struct representing the running peers
5157 * @param topology which topology to connect the peers in
5158 * @param options options for connecting the topology
5159 * @param option_modifier modifier for options that take a parameter
5160 * @param connect_timeout how long to wait before giving up on connecting
5162 * @param connect_attempts how many times to attempt to connect two peers
5163 * over the connect_timeout duration
5164 * @param notify_callback notification to be called once all connections completed
5165 * @param notify_cls closure for notification callback
5167 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
5170 GNUNET_TESTING_connect_topology(
5171 struct GNUNET_TESTING_PeerGroup *pg,
5172 enum GNUNET_TESTING_Topology topology,
5173 enum GNUNET_TESTING_TopologyOption options,
5174 double option_modifier,
5175 struct GNUNET_TIME_Relative connect_timeout,
5176 unsigned int connect_attempts,
5177 GNUNET_TESTING_NotifyCompletion notify_callback,
5182 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
5183 #if VERBOSE_TOPOLOGY
5184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5185 _("Creating clique CONNECT topology\n"));
5187 create_clique (pg, &add_connections, CONNECT, GNUNET_NO);
5189 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
5190 #if VERBOSE_TOPOLOGY
5191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5192 _("Creating small world (ring) CONNECT topology\n"));
5194 create_small_world_ring (pg, &add_connections, CONNECT);
5196 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
5197 #if VERBOSE_TOPOLOGY
5198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5199 _("Creating small world (2d-torus) CONNECT topology\n"));
5201 create_small_world (pg, &add_connections, CONNECT);
5203 case GNUNET_TESTING_TOPOLOGY_RING:
5204 #if VERBOSE_TOPOLOGY
5205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating ring CONNECT topology\n"));
5207 create_ring (pg, &add_connections, CONNECT);
5209 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
5210 #if VERBOSE_TOPOLOGY
5211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5212 _("Creating 2d torus CONNECT topology\n"));
5214 create_2d_torus (pg, &add_connections, CONNECT);
5216 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
5217 #if VERBOSE_TOPOLOGY
5218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5219 _("Creating Erdos-Renyi CONNECT topology\n"));
5221 create_erdos_renyi (pg, &add_connections, CONNECT);
5223 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
5224 #if VERBOSE_TOPOLOGY
5225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5226 _("Creating InterNAT CONNECT topology\n"));
5228 create_nated_internet (pg, &add_connections, CONNECT);
5230 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
5231 #if VERBOSE_TOPOLOGY
5232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5233 _("Creating Scale Free CONNECT topology\n"));
5235 create_scale_free (pg, &add_connections, CONNECT);
5237 case GNUNET_TESTING_TOPOLOGY_LINE:
5238 #if VERBOSE_TOPOLOGY
5239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5240 _("Creating straight line CONNECT topology\n"));
5242 create_line (pg, &add_connections, CONNECT);
5244 case GNUNET_TESTING_TOPOLOGY_NONE:
5245 #if VERBOSE_TOPOLOGY
5246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Creating no CONNECT topology\n"));
5248 copy_allowed_topology (pg);
5251 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5252 ("Unknown topology specification, can't connect peers!\n"));
5253 return GNUNET_SYSERR;
5258 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
5259 #if VERBOSE_TOPOLOGY
5260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5261 ("Connecting random subset (%'.2f percent) of possible peers\n"), 100
5264 choose_random_connections (pg, option_modifier);
5266 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
5267 #if VERBOSE_TOPOLOGY
5268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5269 _("Connecting a minimum of %u peers each (if possible)\n"),
5270 (unsigned int) option_modifier);
5272 choose_minimum (pg, (unsigned int) option_modifier);
5274 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
5275 #if VERBOSE_TOPOLOGY
5276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _
5277 ("Using DFS to connect a minimum of %u peers each (if possible)\n"),
5278 (unsigned int) option_modifier);
5281 perform_dfs (pg, (int) option_modifier);
5284 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
5285 #if VERBOSE_TOPOLOGY
5286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5287 ("Finding additional %u closest peers each (if possible)\n"),
5288 (unsigned int) option_modifier);
5291 add_closest (pg, (unsigned int) option_modifier,
5292 &add_connections, CONNECT);
5295 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
5297 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
5303 return connect_topology (pg, connect_timeout, connect_attempts,
5304 notify_callback, notify_cls);
5308 * Lookup and return the number of SSH connections to a host.
5310 * @param hostname the hostname to lookup in the list
5311 * @param pg the peergroup that the host belongs to
5313 * @return the number of current ssh connections to the host
5316 count_outstanding_at_host(const char *hostname,
5317 struct GNUNET_TESTING_PeerGroup *pg)
5319 struct OutstandingSSH *pos;
5321 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5323 GNUNET_assert(pos != NULL);
5324 return pos->outstanding;
5328 * Increment the number of SSH connections to a host by one.
5330 * @param hostname the hostname to lookup in the list
5331 * @param pg the peergroup that the host belongs to
5335 increment_outstanding_at_host(const char *hostname,
5336 struct GNUNET_TESTING_PeerGroup *pg)
5338 struct OutstandingSSH *pos;
5340 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5342 GNUNET_assert(pos != NULL);
5347 * Decrement the number of SSH connections to a host by one.
5349 * @param hostname the hostname to lookup in the list
5350 * @param pg the peergroup that the host belongs to
5354 decrement_outstanding_at_host(const char *hostname,
5355 struct GNUNET_TESTING_PeerGroup *pg)
5357 struct OutstandingSSH *pos;
5359 while ((pos != NULL) && (strcmp (pos->hostname, hostname) != 0))
5361 GNUNET_assert(pos != NULL);
5366 * Callback that is called whenever a hostkey is generated
5367 * for a peer. Call the real callback and decrement the
5368 * starting counter for the peergroup.
5370 * @param cls closure
5371 * @param id identifier for the daemon, NULL on error
5372 * @param d handle for the daemon
5373 * @param emsg error message (NULL on success)
5376 internal_hostkey_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5377 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5379 struct InternalStartContext *internal_context = cls;
5380 internal_context->peer->pg->starting--;
5381 internal_context->peer->pg->started++;
5382 if (internal_context->hostname != NULL)
5383 decrement_outstanding_at_host (internal_context->hostname,
5384 internal_context->peer->pg);
5385 if (internal_context->hostkey_callback != NULL)
5386 internal_context->hostkey_callback (internal_context->hostkey_cls, id, d,
5388 else if (internal_context->peer->pg->started
5389 == internal_context->peer->pg->total)
5391 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
5392 GNUNET_TESTING_daemons_continue_startup (internal_context->peer->pg);
5397 * Callback that is called whenever a peer has finished starting.
5398 * Call the real callback and decrement the starting counter
5399 * for the peergroup.
5401 * @param cls closure
5402 * @param id identifier for the daemon, NULL on error
5404 * @param d handle for the daemon
5405 * @param emsg error message (NULL on success)
5408 internal_startup_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5409 const struct GNUNET_CONFIGURATION_Handle *cfg,
5410 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5412 struct InternalStartContext *internal_context = cls;
5413 internal_context->peer->pg->starting--;
5414 if (internal_context->hostname != NULL)
5415 decrement_outstanding_at_host (internal_context->hostname,
5416 internal_context->peer->pg);
5417 if (internal_context->start_cb != NULL)
5418 internal_context->start_cb (internal_context->start_cb_cls, id, cfg, d,
5423 internal_continue_startup(void *cls,
5424 const struct GNUNET_SCHEDULER_TaskContext *tc)
5426 struct InternalStartContext *internal_context = cls;
5428 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
5433 if ((internal_context->peer->pg->starting
5434 < internal_context->peer->pg->max_concurrent_ssh)
5435 || ((internal_context->hostname != NULL)
5436 && (count_outstanding_at_host (internal_context->hostname,
5437 internal_context->peer->pg)
5438 < internal_context->peer->pg->max_concurrent_ssh)))
5440 if (internal_context->hostname != NULL)
5441 increment_outstanding_at_host (internal_context->hostname,
5442 internal_context->peer->pg);
5443 internal_context->peer->pg->starting++;
5444 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
5448 GNUNET_SCHEDULER_add_delayed (
5449 GNUNET_TIME_relative_multiply (
5450 GNUNET_TIME_UNIT_MILLISECONDS,
5452 &internal_continue_startup,
5458 * Callback for informing us about a successful
5459 * or unsuccessful churn start call.
5461 * @param cls a ChurnContext
5462 * @param id the peer identity of the started peer
5463 * @param cfg the handle to the configuration of the peer
5464 * @param d handle to the daemon for the peer
5465 * @param emsg NULL on success, non-NULL on failure
5469 churn_start_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5470 const struct GNUNET_CONFIGURATION_Handle *cfg,
5471 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5473 struct ChurnRestartContext *startup_ctx = cls;
5474 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
5476 unsigned int total_left;
5477 char *error_message;
5479 error_message = NULL;
5482 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5483 "Churn stop callback failed with error `%s'\n", emsg);
5484 churn_ctx->num_failed_start++;
5488 churn_ctx->num_to_start--;
5492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5493 "Started peer, %d left.\n", churn_ctx->num_to_start);
5496 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
5497 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
5499 if (total_left == 0)
5501 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
5504 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
5505 churn_ctx->num_failed_start,
5506 churn_ctx->num_failed_stop);
5507 churn_ctx->cb (churn_ctx->cb_cls, error_message);
5508 GNUNET_free_non_null (error_message);
5509 GNUNET_free (churn_ctx);
5510 GNUNET_free (startup_ctx);
5515 schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5517 struct PeerRestartContext *peer_restart_ctx = cls;
5518 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
5520 if (startup_ctx->outstanding > startup_ctx->pg->max_concurrent_ssh)
5521 GNUNET_SCHEDULER_add_delayed (
5522 GNUNET_TIME_relative_multiply (
5523 GNUNET_TIME_UNIT_MILLISECONDS,
5525 &schedule_churn_restart, peer_restart_ctx);
5528 GNUNET_TESTING_daemon_start_stopped (peer_restart_ctx->daemon,
5529 startup_ctx->timeout,
5530 &churn_start_callback, startup_ctx);
5531 GNUNET_free (peer_restart_ctx);
5536 internal_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5538 struct InternalStartContext *internal_context = cls;
5540 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
5545 if ((internal_context->peer->pg->starting
5546 < internal_context->peer->pg->max_concurrent_ssh)
5547 || ((internal_context->hostname != NULL)
5548 && (count_outstanding_at_host (internal_context->hostname,
5549 internal_context->peer->pg)
5550 < internal_context->peer->pg->max_concurrent_ssh)))
5552 if (internal_context->hostname != NULL)
5553 increment_outstanding_at_host (internal_context->hostname,
5554 internal_context->peer->pg);
5555 internal_context->peer->pg->starting++;
5556 internal_context->peer->daemon
5557 = GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
5558 internal_context->timeout,
5559 internal_context->hostname,
5560 internal_context->username,
5561 internal_context->sshport,
5562 internal_context->hostkey,
5563 &internal_hostkey_callback,
5565 &internal_startup_callback,
5570 GNUNET_SCHEDULER_add_delayed (
5571 GNUNET_TIME_relative_multiply (
5572 GNUNET_TIME_UNIT_MILLISECONDS,
5574 &internal_start, internal_context);
5579 * Function which continues a peer group starting up
5580 * after successfully generating hostkeys for each peer.
5582 * @param pg the peer group to continue starting
5586 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
5591 for (i = 0; i < pg->total; i++)
5593 GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5594 &pg->peers[i].internal_context);
5595 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
5600 * Start count gnunet instances with the same set of transports and
5601 * applications. The port numbers (any option called "PORT") will be
5602 * adjusted to ensure that no two peers running on the same system
5603 * have the same port(s) in their respective configurations.
5605 * @param cfg configuration template to use
5606 * @param total number of daemons to start
5607 * @param max_concurrent_connections for testing, how many peers can
5608 * we connect to simultaneously
5609 * @param max_concurrent_ssh when starting with ssh, how many ssh
5610 * connections will we allow at once (based on remote hosts allowed!)
5611 * @param timeout total time allowed for peers to start
5612 * @param hostkey_callback function to call on each peers hostkey generation
5613 * if NULL, peers will be started by this call, if non-null,
5614 * GNUNET_TESTING_daemons_continue_startup must be called after
5615 * successful hostkey generation
5616 * @param hostkey_cls closure for hostkey callback
5617 * @param cb function to call on each daemon that was started
5618 * @param cb_cls closure for cb
5619 * @param connect_callback function to call each time two hosts are connected
5620 * @param connect_callback_cls closure for connect_callback
5621 * @param hostnames linked list of host structs to use to start peers on
5622 * (NULL to run on localhost only)
5624 * @return NULL on error, otherwise handle to control peer group
5626 struct GNUNET_TESTING_PeerGroup *
5627 GNUNET_TESTING_daemons_start(
5628 const struct GNUNET_CONFIGURATION_Handle *cfg,
5630 unsigned int max_concurrent_connections,
5631 unsigned int max_concurrent_ssh,
5632 struct GNUNET_TIME_Relative timeout,
5633 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
5635 GNUNET_TESTING_NotifyDaemonRunning cb,
5637 GNUNET_TESTING_NotifyConnection connect_callback,
5638 void *connect_callback_cls,
5639 const struct GNUNET_TESTING_Host *hostnames)
5641 struct GNUNET_TESTING_PeerGroup *pg;
5642 const struct GNUNET_TESTING_Host *hostpos;
5648 const char *hostname;
5649 const char *username;
5650 char *baseservicehome;
5651 char *newservicehome;
5653 char *hostkeys_file;
5656 struct GNUNET_DISK_FileHandle *fd;
5657 struct GNUNET_CONFIGURATION_Handle *pcfg;
5659 struct OutstandingSSH *ssh_entry;
5660 unsigned int hostcnt;
5667 uint64_t total_hostkeys;
5668 struct GNUNET_OS_Process *proc;
5678 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
5680 pg->notify_connection = connect_callback;
5681 pg->notify_connection_cls = connect_callback_cls;
5683 pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
5684 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
5685 pg->max_outstanding_connections = max_concurrent_connections;
5686 pg->max_concurrent_ssh = max_concurrent_ssh;
5687 if (NULL != hostnames)
5690 hostpos = hostnames;
5691 while (hostpos != NULL)
5693 hostpos = hostpos->next;
5696 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5699 hostpos = hostnames;
5700 while (hostpos != NULL)
5702 pg->hosts[off].minport = LOW_PORT;
5703 pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
5704 if (hostpos->username != NULL)
5705 pg->hosts[off].username = GNUNET_strdup (hostpos->username);
5706 pg->hosts[off].sshport = hostpos->port;
5707 hostpos = hostpos->next;
5717 pg->num_hosts = off;
5721 /* skip leading spaces */
5722 while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
5725 while ('\0' != *rpos)
5727 if (isspace ((unsigned char) *rpos))
5731 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5733 start = GNUNET_strdup (hostnames);
5735 while ('\0' != *pos)
5737 if (isspace ((unsigned char) *pos))
5740 if (strlen (start) > 0)
5742 pg->hosts[off].minport = LOW_PORT;
5743 pg->hosts[off++].hostname = start;
5749 if (strlen (start) > 0)
5751 pg->hosts[off].minport = LOW_PORT;
5752 pg->hosts[off++].hostname = start;
5756 GNUNET_free (start);
5757 GNUNET_free (pg->hosts);
5761 minport = 0; /* make gcc happy */
5770 /* Create the servicehome directory for each remote peer */
5771 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
5773 for (i = 0; i < pg->num_hosts; i++)
5775 ssh_entry = GNUNET_malloc(sizeof(struct OutstandingSSH));
5776 ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
5777 GNUNET_CONTAINER_DLL_insert(pg->ssh_head, pg->ssh_tail, ssh_entry);
5778 GNUNET_asprintf(&tmpdir, "%s/%s", baseservicehome, pg->hosts[i].hostname);
5779 if (NULL != pg->hosts[i].username)
5780 GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username,
5781 pg->hosts[i].hostname);
5783 GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
5784 if (pg->hosts[i].sshport != 0)
5786 GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
5787 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-P",
5792 arg, "mkdir -p", tmpdir,
5796 proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5797 "mkdir -p", tmpdir, NULL);
5798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5799 "Creating remote dir with command ssh %s %s %s\n", arg,
5800 " mkdir -p ", tmpdir);
5801 GNUNET_free(tmpdir);
5803 GNUNET_OS_process_wait (proc);
5804 GNUNET_OS_process_close(proc);
5806 GNUNET_free(baseservicehome);
5808 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING",
5812 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
5813 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!\n");
5816 /* Check hostkey file size, read entire thing into memory */
5817 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
5818 GNUNET_DISK_PERM_NONE);
5821 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
5825 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs,
5830 GNUNET_ERROR_TYPE_WARNING,
5831 "Found file size %llu for hostkeys, expect hostkeys to be size %d\n",
5832 fs, HOSTKEYFILESIZE);
5834 if (fs % HOSTKEYFILESIZE != 0)
5836 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5837 "File size %llu seems incorrect for hostkeys...\n",
5842 total_hostkeys = fs / HOSTKEYFILESIZE;
5843 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5844 "Will read %llu hostkeys from file\n", total_hostkeys);
5845 pg->hostkey_data = GNUNET_malloc_large (fs);
5846 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
5847 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
5850 GNUNET_free(hostkeys_file);
5853 for (off = 0; off < total; off++)
5857 hostname = pg->hosts[off % hostcnt].hostname;
5858 username = pg->hosts[off % hostcnt].username;
5859 sshport = pg->hosts[off % hostcnt].sshport;
5860 pcfg = make_config (cfg, off, &pg->hosts[off % hostcnt].minport,
5861 &upnum, hostname, &fdnum);
5868 pcfg = make_config (cfg, off, &minport, &upnum, hostname, &fdnum);
5873 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5874 ("Could not create configuration for peer number %u on `%s'!\n"),
5875 off, hostname == NULL ? "localhost" : hostname);
5880 == GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS",
5884 if (hostname != NULL)
5885 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", baseservicehome, hostname, off);
5887 GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
5888 GNUNET_free (baseservicehome);
5892 tmpdir = getenv ("TMPDIR");
5893 tmpdir = tmpdir ? tmpdir : "/tmp";
5894 if (hostname != NULL)
5895 GNUNET_asprintf (&newservicehome, "%s/%s/%s/%d/", tmpdir, hostname,
5896 "gnunet-testing-test-test", off);
5898 GNUNET_asprintf (&newservicehome, "%s/%s/%d/", tmpdir,
5899 "gnunet-testing-test-test", off);
5901 GNUNET_CONFIGURATION_set_value_string (pcfg, "PATHS", "SERVICEHOME",
5903 GNUNET_free (newservicehome);
5904 pg->peers[off].cfg = pcfg;
5906 /* Can we do this later? */
5907 pg->peers[off].allowed_peers =
5908 GNUNET_CONTAINER_multihashmap_create (total);
5909 pg->peers[off].connect_peers =
5910 GNUNET_CONTAINER_multihashmap_create (total);
5911 pg->peers[off].blacklisted_peers =
5912 GNUNET_CONTAINER_multihashmap_create (total);
5915 pg->peers[off].pg = pg;
5916 pg->peers[off].internal_context.peer = &pg->peers[off];
5917 pg->peers[off].internal_context.timeout = timeout;
5918 pg->peers[off].internal_context.hostname = hostname;
5919 pg->peers[off].internal_context.username = username;
5920 pg->peers[off].internal_context.sshport = sshport;
5921 if (pg->hostkey_data != NULL)
5922 pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off
5924 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
5925 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
5926 pg->peers[off].internal_context.start_cb = cb;
5927 pg->peers[off].internal_context.start_cb_cls = cb_cls;
5929 GNUNET_SCHEDULER_add_now (&internal_start,
5930 &pg->peers[off].internal_context);
5937 * Get a daemon by number, so callers don't have to do nasty
5938 * offsetting operation.
5940 struct GNUNET_TESTING_Daemon *
5941 GNUNET_TESTING_daemon_get(struct GNUNET_TESTING_PeerGroup *pg,
5942 unsigned int position)
5944 if (position < pg->total)
5945 return pg->peers[position].daemon;
5951 * Get a daemon by peer identity, so callers can
5952 * retrieve the daemon without knowing it's offset.
5954 * @param pg the peer group to retrieve the daemon from
5955 * @param peer_id the peer identity of the daemon to retrieve
5957 * @return the daemon on success, or NULL if no such peer identity is found
5959 struct GNUNET_TESTING_Daemon *
5960 GNUNET_TESTING_daemon_get_by_id(struct GNUNET_TESTING_PeerGroup *pg,
5961 struct GNUNET_PeerIdentity *peer_id)
5965 for (i = 0; i < pg->total; i++)
5967 if (0 == memcmp (&pg->peers[i].daemon->id, peer_id,
5968 sizeof(struct GNUNET_PeerIdentity)))
5969 return pg->peers[i].daemon;
5976 * Prototype of a function that will be called when a
5977 * particular operation was completed the testing library.
5979 * @param cls closure (a struct RestartContext)
5980 * @param id id of the peer that was restarted
5981 * @param cfg handle to the configuration of the peer
5982 * @param d handle to the daemon that was restarted
5983 * @param emsg NULL on success
5986 restart_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5987 const struct GNUNET_CONFIGURATION_Handle *cfg,
5988 struct GNUNET_TESTING_Daemon *d, const char *emsg)
5990 struct RestartContext *restart_context = cls;
5994 restart_context->peers_restarted++;
5998 restart_context->peers_restart_failed++;
6001 if (restart_context->peers_restarted == restart_context->peer_group->total)
6003 restart_context->callback (restart_context->callback_cls, NULL);
6004 GNUNET_free (restart_context);
6006 else if (restart_context->peers_restart_failed
6007 + restart_context->peers_restarted == restart_context->peer_group->total)
6009 restart_context->callback (restart_context->callback_cls,
6010 "Failed to restart peers!");
6011 GNUNET_free (restart_context);
6017 * Callback for informing us about a successful
6018 * or unsuccessful churn stop call.
6020 * @param cls a ChurnContext
6021 * @param emsg NULL on success, non-NULL on failure
6025 churn_stop_callback(void *cls, const char *emsg)
6027 struct ShutdownContext *shutdown_ctx = cls;
6028 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
6029 unsigned int total_left;
6030 char *error_message;
6032 error_message = NULL;
6033 shutdown_ctx->outstanding--;
6037 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6038 "Churn stop callback failed with error `%s'\n", emsg);
6039 churn_ctx->num_failed_stop++;
6043 churn_ctx->num_to_stop--;
6047 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6048 "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
6050 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
6051 + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
6053 if (total_left == 0)
6055 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
6059 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
6060 churn_ctx->num_failed_start,
6061 churn_ctx->num_failed_stop);
6063 churn_ctx->cb (churn_ctx->cb_cls, error_message);
6064 GNUNET_free_non_null (error_message);
6065 GNUNET_free (churn_ctx);
6066 GNUNET_free (shutdown_ctx);
6071 * Count the number of running peers.
6073 * @param pg handle for the peer group
6075 * @return the number of currently running peers in the peer group
6078 GNUNET_TESTING_daemons_running(struct GNUNET_TESTING_PeerGroup *pg)
6081 unsigned int running = 0;
6082 for (i = 0; i < pg->total; i++)
6084 if (pg->peers[i].daemon->running == GNUNET_YES)
6086 GNUNET_assert (running != -1);
6094 * Task to rate limit the number of outstanding peer shutdown
6095 * requests. This is necessary for making sure we don't do
6096 * too many ssh connections at once, but is generally nicer
6097 * to any system as well (graduated task starts, as opposed
6098 * to calling gnunet-arm N times all at once).
6101 schedule_churn_shutdown_task(void *cls,
6102 const struct GNUNET_SCHEDULER_TaskContext *tc)
6104 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6105 struct ShutdownContext *shutdown_ctx;
6106 struct ChurnContext *churn_ctx;
6107 GNUNET_assert (peer_shutdown_ctx != NULL);
6108 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6109 GNUNET_assert (shutdown_ctx != NULL);
6110 churn_ctx = (struct ChurnContext *) shutdown_ctx->cb_cls;
6111 if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
6112 GNUNET_SCHEDULER_add_delayed (
6113 GNUNET_TIME_relative_multiply (
6114 GNUNET_TIME_UNIT_MILLISECONDS,
6116 &schedule_churn_shutdown_task,
6120 shutdown_ctx->outstanding++;
6121 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6122 shutdown_ctx->timeout, shutdown_ctx->cb,
6123 shutdown_ctx, GNUNET_NO, GNUNET_YES);
6124 GNUNET_free (peer_shutdown_ctx);
6129 * Simulate churn by stopping some peers (and possibly
6130 * re-starting others if churn is called multiple times). This
6131 * function can only be used to create leave-join churn (peers "never"
6132 * leave for good). First "voff" random peers that are currently
6133 * online will be taken offline; then "von" random peers that are then
6134 * offline will be put back online. No notifications will be
6135 * generated for any of these operations except for the callback upon
6138 * @param pg handle for the peer group
6139 * @param voff number of peers that should go offline
6140 * @param von number of peers that should come back online;
6141 * must be zero on first call (since "testbed_start"
6142 * always starts all of the peers)
6143 * @param timeout how long to wait for operations to finish before
6145 * @param cb function to call at the end
6146 * @param cb_cls closure for cb
6149 GNUNET_TESTING_daemons_churn(struct GNUNET_TESTING_PeerGroup *pg,
6150 unsigned int voff, unsigned int von,
6151 struct GNUNET_TIME_Relative timeout,
6152 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6154 struct ChurnContext *churn_ctx;
6155 struct ShutdownContext *shutdown_ctx;
6156 struct PeerShutdownContext *peer_shutdown_ctx;
6157 struct PeerRestartContext *peer_restart_ctx;
6158 struct ChurnRestartContext *churn_startup_ctx;
6160 unsigned int running;
6161 unsigned int stopped;
6162 unsigned int total_running;
6163 unsigned int total_stopped;
6165 unsigned int *running_arr;
6166 unsigned int *stopped_arr;
6167 unsigned int *running_permute;
6168 unsigned int *stopped_permute;
6173 if ((von == 0) && (voff == 0)) /* No peers at all? */
6179 for (i = 0; i < pg->total; i++)
6181 if (pg->peers[i].daemon->running == GNUNET_YES)
6183 GNUNET_assert (running != -1);
6188 GNUNET_assert (stopped != -1);
6195 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6196 "Trying to stop more peers than are currently running!\n");
6197 cb (cb_cls, "Trying to stop more peers than are currently running!");
6203 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6204 "Trying to start more peers than are currently stopped!\n");
6205 cb (cb_cls, "Trying to start more peers than are currently stopped!");
6209 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6213 running_arr = GNUNET_malloc (running * sizeof (unsigned int));
6217 stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
6219 running_permute = NULL;
6220 stopped_permute = NULL;
6223 running_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6226 stopped_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6229 total_running = running;
6230 total_stopped = stopped;
6234 churn_ctx->num_to_start = von;
6235 churn_ctx->num_to_stop = voff;
6237 churn_ctx->cb_cls = cb_cls;
6240 for (i = 0; i < pg->total; i++)
6242 if (pg->peers[i].daemon->running == GNUNET_YES)
6244 GNUNET_assert ((running_arr != NULL) && (total_running > running));
6245 running_arr[running] = i;
6250 GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6251 stopped_arr[stopped] = i;
6256 GNUNET_assert (running >= voff);
6259 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6260 shutdown_ctx->cb = &churn_stop_callback;
6261 shutdown_ctx->cb_cls = churn_ctx;
6262 shutdown_ctx->total_peers = voff;
6263 shutdown_ctx->timeout = timeout;
6266 for (i = 0; i < voff; i++)
6269 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
6270 running_permute[i]);
6272 GNUNET_assert (running_arr != NULL);
6273 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6274 peer_shutdown_ctx->daemon
6275 = pg->peers[running_arr[running_permute[i]]].daemon;
6276 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6277 GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
6281 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
6283 &churn_stop_callback, churn_ctx,
6284 GNUNET_NO, GNUNET_YES); */
6287 GNUNET_assert (stopped >= von);
6290 churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6291 churn_startup_ctx->churn_ctx = churn_ctx;
6292 churn_startup_ctx->timeout = timeout;
6293 churn_startup_ctx->pg = pg;
6295 for (i = 0; i < von; i++)
6298 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
6299 stopped_permute[i]);
6301 GNUNET_assert (stopped_arr != NULL);
6302 peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
6303 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
6304 peer_restart_ctx->daemon
6305 = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
6306 GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
6308 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
6309 timeout, &churn_start_callback, churn_ctx); */
6312 GNUNET_free_non_null (running_arr);
6313 GNUNET_free_non_null (stopped_arr);
6314 GNUNET_free_non_null (running_permute);
6315 GNUNET_free_non_null (stopped_permute);
6319 * Restart all peers in the given group.
6321 * @param pg the handle to the peer group
6322 * @param callback function to call on completion (or failure)
6323 * @param callback_cls closure for the callback function
6326 GNUNET_TESTING_daemons_restart(struct GNUNET_TESTING_PeerGroup *pg,
6327 GNUNET_TESTING_NotifyCompletion callback,
6330 struct RestartContext *restart_context;
6335 restart_context = GNUNET_malloc (sizeof (struct RestartContext));
6336 restart_context->peer_group = pg;
6337 restart_context->peers_restarted = 0;
6338 restart_context->callback = callback;
6339 restart_context->callback_cls = callback_cls;
6341 for (off = 0; off < pg->total; off++)
6343 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
6344 &restart_callback, restart_context);
6350 * Start or stop an individual peer from the given group.
6352 * @param pg handle to the peer group
6353 * @param offset which peer to start or stop
6354 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
6355 * @param timeout how long to wait for shutdown
6356 * @param cb function to call at the end
6357 * @param cb_cls closure for cb
6360 GNUNET_TESTING_daemons_vary(struct GNUNET_TESTING_PeerGroup *pg,
6361 unsigned int offset, int desired_status,
6362 struct GNUNET_TIME_Relative timeout,
6363 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6365 struct ShutdownContext *shutdown_ctx;
6366 struct ChurnRestartContext *startup_ctx;
6367 struct ChurnContext *churn_ctx;
6369 if (GNUNET_NO == desired_status)
6371 if (NULL != pg->peers[offset].daemon)
6373 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6374 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6375 churn_ctx->num_to_start = 0;
6376 churn_ctx->num_to_stop = 1;
6378 churn_ctx->cb_cls = cb_cls;
6379 shutdown_ctx->cb_cls = churn_ctx;
6380 GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon, timeout,
6381 &churn_stop_callback, shutdown_ctx,
6382 GNUNET_NO, GNUNET_YES);
6385 else if (GNUNET_YES == desired_status)
6387 if (NULL == pg->peers[offset].daemon)
6389 startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6390 churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6391 churn_ctx->num_to_start = 1;
6392 churn_ctx->num_to_stop = 0;
6394 churn_ctx->cb_cls = cb_cls;
6395 startup_ctx->churn_ctx = churn_ctx;
6396 GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
6397 timeout, &churn_start_callback,
6406 * Callback for shutting down peers in a peer group.
6408 * @param cls closure (struct ShutdownContext)
6409 * @param emsg NULL on success
6412 internal_shutdown_callback(void *cls, const char *emsg)
6414 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6415 struct ShutdownContext *shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6417 struct OutstandingSSH *ssh_pos;
6419 shutdown_ctx->outstanding--;
6420 if (peer_shutdown_ctx->daemon->hostname != NULL)
6421 decrement_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6426 shutdown_ctx->peers_down++;
6430 shutdown_ctx->peers_failed++;
6433 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down
6434 + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
6436 if (shutdown_ctx->peers_failed > 0)
6437 shutdown_ctx->cb (shutdown_ctx->cb_cls,
6438 "Not all peers successfully shut down!");
6440 shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
6442 GNUNET_free (shutdown_ctx->pg->peers);
6443 GNUNET_free_non_null(shutdown_ctx->pg->hostkey_data);
6444 for (off = 0; off < shutdown_ctx->pg->num_hosts; off++)
6446 GNUNET_free (shutdown_ctx->pg->hosts[off].hostname);
6447 GNUNET_free_non_null (shutdown_ctx->pg->hosts[off].username);
6449 GNUNET_free_non_null (shutdown_ctx->pg->hosts);
6450 while (NULL != (ssh_pos = shutdown_ctx->pg->ssh_head))
6452 GNUNET_CONTAINER_DLL_remove(shutdown_ctx->pg->ssh_head, shutdown_ctx->pg->ssh_tail, ssh_pos);
6453 GNUNET_free(ssh_pos);
6455 GNUNET_free (shutdown_ctx->pg);
6456 GNUNET_free (shutdown_ctx);
6458 GNUNET_free(peer_shutdown_ctx);
6462 * Task to rate limit the number of outstanding peer shutdown
6463 * requests. This is necessary for making sure we don't do
6464 * too many ssh connections at once, but is generally nicer
6465 * to any system as well (graduated task starts, as opposed
6466 * to calling gnunet-arm N times all at once).
6469 schedule_shutdown_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6471 struct PeerShutdownContext *peer_shutdown_ctx = cls;
6472 struct ShutdownContext *shutdown_ctx;
6474 GNUNET_assert (peer_shutdown_ctx != NULL);
6475 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6476 GNUNET_assert (shutdown_ctx != NULL);
6478 if ((shutdown_ctx->outstanding < shutdown_ctx->pg->max_concurrent_ssh)
6479 || ((peer_shutdown_ctx->daemon->hostname != NULL)
6480 && (count_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6482 < shutdown_ctx->pg->max_concurrent_ssh)))
6484 if (peer_shutdown_ctx->daemon->hostname != NULL)
6485 increment_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6487 shutdown_ctx->outstanding++;
6488 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6489 shutdown_ctx->timeout,
6490 &internal_shutdown_callback, peer_shutdown_ctx,
6491 GNUNET_YES, GNUNET_NO);
6494 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
6496 &schedule_shutdown_task, peer_shutdown_ctx);
6501 * Shutdown all peers started in the given group.
6503 * @param pg handle to the peer group
6504 * @param timeout how long to wait for shutdown
6505 * @param cb callback to notify upon success or failure
6506 * @param cb_cls closure for cb
6509 GNUNET_TESTING_daemons_stop(struct GNUNET_TESTING_PeerGroup *pg,
6510 struct GNUNET_TIME_Relative timeout,
6511 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6514 struct ShutdownContext *shutdown_ctx;
6515 struct PeerShutdownContext *peer_shutdown_ctx;
6517 struct PeerConnection *conn_iter;
6518 struct PeerConnection *temp_conn;
6521 GNUNET_assert (pg->total > 0);
6523 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6524 shutdown_ctx->cb = cb;
6525 shutdown_ctx->cb_cls = cb_cls;
6526 shutdown_ctx->total_peers = pg->total;
6527 shutdown_ctx->timeout = timeout;
6528 shutdown_ctx->pg = pg;
6529 /* shtudown_ctx->outstanding = 0; */
6531 for (off = 0; off < pg->total; off++)
6533 GNUNET_assert (NULL != pg->peers[off].daemon);
6534 peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6535 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
6536 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6537 GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
6539 if (NULL != pg->peers[off].cfg)
6540 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
6542 conn_iter = pg->peers[off].allowed_peers_head;
6543 while (conn_iter != NULL)
6545 temp_conn = conn_iter->next;
6546 GNUNET_free(conn_iter);
6547 conn_iter = temp_conn;
6550 conn_iter = pg->peers[off].connect_peers_head;
6551 while (conn_iter != NULL)
6553 temp_conn = conn_iter->next;
6554 GNUNET_free(conn_iter);
6555 conn_iter = temp_conn;
6558 conn_iter = pg->peers[off].blacklisted_peers_head;
6559 while (conn_iter != NULL)
6561 temp_conn = conn_iter->next;
6562 GNUNET_free(conn_iter);
6563 conn_iter = temp_conn;
6566 conn_iter = pg->peers[off].connect_peers_working_set_head;
6567 while (conn_iter != NULL)
6569 temp_conn = conn_iter->next;
6570 GNUNET_free(conn_iter);
6571 conn_iter = temp_conn;
6574 if (pg->peers[off].allowed_peers != NULL)
6575 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
6576 if (pg->peers[off].connect_peers != NULL)
6577 GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
6578 if (pg->peers[off].blacklisted_peers != NULL)
6579 GNUNET_CONTAINER_multihashmap_destroy (pg->
6580 peers[off].blacklisted_peers);
6585 /* end of testing_group.c */