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_YES
37 #define DEBUG_CHURN GNUNET_NO
40 * Lowest port used for GNUnet testing. Should be high enough to not
41 * conflict with other applications running on the hosts but be low
42 * enough to not conflict with client-ports (typically starting around
45 #define LOW_PORT 10000
48 * Highest port used for GNUnet testing. Should be low enough to not
49 * conflict with the port range for "local" ports (client apps; see
50 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
52 #define HIGH_PORT 56000
54 #define MAX_OUTSTANDING_CONNECTIONS 40
56 #define MAX_CONCURRENT_HOSTKEYS 10
58 #define MAX_CONCURRENT_STARTING 10
60 #define MAX_CONCURRENT_SHUTDOWN 10
62 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
64 #define CONNECT_ATTEMPTS 8
67 * Prototype of a function called whenever two peers would be connected
68 * in a certain topology.
70 typedef int (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup *pg,
76 * Context for handling churning a peer group
81 * Callback used to notify of churning finished
83 GNUNET_TESTING_NotifyCompletion cb;
86 * Closure for callback
91 * Number of peers that still need to be started
93 unsigned int num_to_start;
96 * Number of peers that still need to be stopped
98 unsigned int num_to_stop;
101 * Number of peers that failed to start
103 unsigned int num_failed_start;
106 * Number of peers that failed to stop
108 unsigned int num_failed_stop;
111 struct RestartContext
114 * The group of peers being restarted
116 struct GNUNET_TESTING_PeerGroup *peer_group;
119 * How many peers have been restarted thus far
121 unsigned int peers_restarted;
124 * How many peers got an error when restarting
126 unsigned int peers_restart_failed;
129 * The function to call once all peers have been restarted
131 GNUNET_TESTING_NotifyCompletion callback;
134 * Closure for callback function
141 struct ShutdownContext
144 * Total peers to wait for
146 unsigned int total_peers;
149 * Number of peers successfully shut down
151 unsigned int peers_down;
154 * Number of peers failed to shut down
156 unsigned int peers_failed;
159 * Number of peers we have started shutting
160 * down. If too many, wait on them.
162 unsigned int outstanding;
165 * Timeout for shutdown.
167 struct GNUNET_TIME_Relative timeout;
170 * Callback to call when all peers either
171 * shutdown or failed to shutdown
173 GNUNET_TESTING_NotifyCompletion cb;
182 * Individual shutdown context for a particular peer.
184 struct PeerShutdownContext
187 * Pointer to the high level shutdown context.
189 struct ShutdownContext *shutdown_ctx;
192 * The daemon handle for the peer to shut down.
194 struct GNUNET_TESTING_Daemon *daemon;
198 * Individual shutdown context for a particular peer.
200 struct PeerRestartContext
203 * Pointer to the high level restart context.
205 struct ChurnRestartContext *churn_restart_ctx;
208 * The daemon handle for the peer to shut down.
210 struct GNUNET_TESTING_Daemon *daemon;
214 struct CreateTopologyContext
218 * Function to call with number of connections
220 GNUNET_TESTING_NotifyConnections cont;
223 * Closure for connection notification
229 struct PeerConnection
234 struct PeerConnection *next;
237 * Pointer to daemon handle
239 struct GNUNET_TESTING_Daemon *daemon;
244 struct InternalStartContext
247 * Pointer to peerdata
249 struct PeerData *peer;
252 * Timeout for peer startup
254 struct GNUNET_TIME_Relative timeout;
257 * Client callback for hostkey notification
259 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
262 * Closure for hostkey_callback
267 * Client callback for peer start notification
269 GNUNET_TESTING_NotifyDaemonRunning start_cb;
277 * Hostname, where to start the peer
279 const char *hostname;
282 * Username to use when connecting to the
285 const char *username;
288 * Port to use for ssh.
294 struct ChurnRestartContext
297 * Number of restarts currently in flight.
299 unsigned int outstanding;
302 * Handle to the underlying churn context.
304 struct ChurnContext *churn_ctx;
307 * How long to allow the operation to take.
309 struct GNUNET_TIME_Relative timeout;
313 * Data we keep per peer.
318 * (Initial) configuration of the host.
319 * (initial because clients could change
320 * it and we would not know about those
323 struct GNUNET_CONFIGURATION_Handle *cfg;
326 * Handle for controlling the daemon.
328 struct GNUNET_TESTING_Daemon *daemon;
331 * The peergroup this peer belongs to.
333 struct GNUNET_TESTING_PeerGroup *pg;
336 * Hash map of allowed peer connections (F2F created topology)
338 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
341 * Hash map of blacklisted peers
343 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
346 * Hash map of peer connections
348 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
351 * Temporary hash map of peer connections
353 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
356 * Temporary variable for topology creation, should be reset before
357 * creating any topology so the count is valid once finished.
362 * Context to keep track of peers being started, to
363 * stagger hostkey generation and peer startup.
365 struct InternalStartContext internal_context;
370 * Linked list of per-host data.
380 * SSH username to use when connecting to this host.
385 * SSH port to use when connecting to this host.
390 * Lowest port that we have not yet used
396 struct TopologyIterateContext
399 * Callback for notifying of two connected peers.
401 GNUNET_TESTING_NotifyTopology topology_cb;
404 * Closure for topology_cb
409 * Number of peers currently connected to.
411 unsigned int connected;
414 * Number of peers we have finished iterating.
416 unsigned int completed;
419 * Number of peers total.
424 struct StatsIterateContext
427 * Continuation to call once all stats information has been retrieved.
429 GNUNET_STATISTICS_Callback cont;
432 * Proc function to call on each value received.
434 GNUNET_TESTING_STATISTICS_Iterator proc;
437 * Closure for topology_cb
442 * Number of peers currently connected to.
444 unsigned int connected;
447 * Number of peers we have finished iterating.
449 unsigned int completed;
452 * Number of peers total.
460 struct GNUNET_TESTING_Daemon *daemon;
463 struct StatsCoreContext
466 struct GNUNET_TESTING_Daemon *daemon;
468 * Handle to the statistics service.
470 struct GNUNET_STATISTICS_Handle *stats_handle;
473 * Handle for getting statistics.
475 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
479 * Handle to a group of GNUnet peers.
481 struct GNUNET_TESTING_PeerGroup
486 struct GNUNET_SCHEDULER_Handle *sched;
489 * Configuration template.
491 const struct GNUNET_CONFIGURATION_Handle *cfg;
494 * Function to call on each started daemon.
496 //GNUNET_TESTING_NotifyDaemonRunning cb;
504 * Function to call on each topology connection created
506 GNUNET_TESTING_NotifyConnection notify_connection;
509 * Callback for notify_connection
511 void *notify_connection_cls;
514 * Array of information about hosts.
516 struct HostData *hosts;
519 * Number of hosts (size of HostData)
521 unsigned int num_hosts;
524 * Array of "total" peers.
526 struct PeerData *peers;
529 * Number of peers in this group.
534 * At what time should we fail the peer startup process?
536 struct GNUNET_TIME_Absolute max_timeout;
539 * How many peers are being started right now?
541 unsigned int starting;
544 * How many peers have already been started?
546 unsigned int started;
551 struct GNUNET_CONFIGURATION_Handle *ret;
552 const struct GNUNET_CONFIGURATION_Handle *orig;
553 const char *hostname;
560 struct ConnectContext
562 struct GNUNET_TESTING_Daemon *first;
564 struct GNUNET_TESTING_Daemon *second;
566 struct GNUNET_TESTING_PeerGroup *pg;
570 * Convert unique ID to hash code.
572 * @param uid unique ID to convert
573 * @param hash set to uid (extended with zeros)
576 hash_from_uid (uint32_t uid,
577 GNUNET_HashCode *hash)
579 memset (hash, 0, sizeof(GNUNET_HashCode));
580 *((uint32_t*)hash) = uid;
584 * Convert hash code to unique ID.
586 * @param uid unique ID to convert
587 * @param hash set to uid (extended with zeros)
590 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
592 memcpy (uid, hash, sizeof(uint32_t));
596 * Number of connects we are waiting on, allows us to rate limit
599 static int outstanding_connects;
602 * Get a topology from a string input.
604 * @param topology where to write the retrieved topology
605 * @param topology_string The string to attempt to
606 * get a configuration value from
607 * @return GNUNET_YES if topology string matched a
608 * known topology, GNUNET_NO if not
611 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string)
614 * Strings representing topologies in enum
616 static const char * topology_strings[] =
619 * A clique (everyone connected to everyone else).
624 * Small-world network (2d torus plus random links).
629 * Small-world network (ring plus random links).
649 * Certain percentage of peers are unable to communicate directly
650 * replicating NAT conditions
655 * Scale free topology.
660 * Straight line topology.
665 * All peers are disconnected.
673 if (topology_string == NULL)
675 while (topology_strings[curr] != NULL)
677 if (strcasecmp(topology_strings[curr], topology_string) == 0)
684 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
690 * Get connect topology option from string input.
692 * @param topology_option where to write the retrieved topology
693 * @param topology_string The string to attempt to
694 * get a configuration value from
695 * @return GNUNET_YES if string matched a known
696 * topology option, GNUNET_NO if not
699 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption *topology_option,
700 char * topology_string)
703 * Options for connecting a topology as strings.
705 static const char * topology_option_strings[] =
708 * Try to connect all peers specified in the topology.
713 * Choose a random subset of connections to create.
715 "CONNECT_RANDOM_SUBSET",
718 * Create at least X connections for each peer.
723 * Using a depth first search, create one connection
724 * per peer. If any are missed (graph disconnected)
725 * start over at those peers until all have at least one
731 * Find the N closest peers to each allowed peer in the
732 * topology and make sure a connection to those peers
733 * exists in the connect topology.
738 * No options specified.
746 if (topology_string == NULL)
748 while (NULL != topology_option_strings[curr])
750 if (strcasecmp(topology_option_strings[curr], topology_string) == 0)
752 *topology_option = curr;
757 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
762 * Function to iterate over options. Copies
763 * the options to the target configuration,
764 * updating PORT values as needed.
767 * @param section name of the section
768 * @param option name of the option
769 * @param value value of the option
772 update_config (void *cls,
773 const char *section, const char *option, const char *value)
775 struct UpdateContext *ctx = cls;
779 char *single_variable;
780 char *per_host_variable;
781 unsigned long long num_per_host;
783 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
785 GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
786 if ((ival != 0) && (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable)))
788 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
792 GNUNET_free(single_variable);
795 if (0 == strcmp (option, "UNIXPATH"))
797 GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
798 GNUNET_asprintf(&per_host_variable, "num_%s_per_host", section);
799 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable))
801 GNUNET_snprintf (uval,
803 "/tmp/test-service-%s-%u",
808 else if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_number(ctx->orig, "testing", per_host_variable, &num_per_host))
810 GNUNET_snprintf (uval,
812 "/tmp/test-service-%s-%u",
814 ctx->fdnum % num_per_host);
817 GNUNET_free(single_variable);
818 GNUNET_free(per_host_variable);
822 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
824 value = ctx->hostname;
827 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
832 * Create a new configuration using the given configuration
833 * as a template; however, each PORT in the existing cfg
834 * must be renumbered by incrementing "*port". If we run
835 * out of "*port" numbers, return NULL.
837 * @param cfg template configuration
838 * @param port port numbers to use, update to reflect
839 * port numbers that were used
840 * @param upnum number to make unix domain socket names unique
841 * @param hostname hostname of the controlling host, to allow control connections from
842 * @param fdnum number used to offset the unix domain socket for grouped processes
843 * (such as statistics or peerinfo, which can be shared among others)
845 * @return new configuration, NULL on error
847 static struct GNUNET_CONFIGURATION_Handle *
848 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
851 const char *hostname, uint32_t * fdnum)
853 struct UpdateContext uc;
862 uc.ret = GNUNET_CONFIGURATION_create ();
863 uc.hostname = hostname;
866 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
867 if (uc.nport >= HIGH_PORT)
870 GNUNET_CONFIGURATION_destroy (uc.ret);
874 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
876 if (hostname != NULL)
877 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1; %s;", control_host, hostname);
879 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
881 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
882 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport", "ACCEPT_FROM", allowed_hosts);
883 GNUNET_CONFIGURATION_set_value_string(uc.ret, "dht", "ACCEPT_FROM", allowed_hosts);
884 GNUNET_CONFIGURATION_set_value_string(uc.ret, "statistics", "ACCEPT_FROM", allowed_hosts);
885 GNUNET_free_non_null(control_host);
886 GNUNET_free(allowed_hosts);
890 /* arm needs to know to allow connections from the host on which it is running,
891 * otherwise gnunet-arm is unable to connect to it in some instances */
892 if (hostname != NULL)
894 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
895 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", hostname);
896 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", hostname);
897 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
898 GNUNET_free(allowed_hosts);
902 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", "127.0.0.1");
903 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", "127.0.0.1");
907 *port = (uint16_t) uc.nport;
916 * Add entries to the peers connect list
918 * @param pg the peer group we are working with
919 * @param first index of the first peer
920 * @param second index of the second peer
922 * @return the number of connections added
923 * technically should only be 0 or 2
927 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
933 GNUNET_HashCode hash_first;
934 GNUNET_HashCode hash_second;
936 hash_from_uid(first, &hash_first);
937 hash_from_uid(second, &hash_second);
939 add_first = GNUNET_NO;
940 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
942 add_first = GNUNET_YES;
945 add_second = GNUNET_NO;
946 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
948 add_second = GNUNET_YES;
954 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
955 pg->peers[first].num_connections++;
961 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
962 pg->peers[second].num_connections++;
971 * Add entries to the peers allowed connections list
973 * @param pg the peer group we are working with
974 * @param first index of the first peer
975 * @param second index of the second peer
977 * @return the number of connections added (can be 0, 1 or 2)
978 * technically should only be 0 or 2, but the small price
979 * of iterating over the lists (hashmaps in the future)
980 * for being sure doesn't bother me!
984 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
988 struct PeerConnection *first_iter;
989 struct PeerConnection *second_iter;
990 struct PeerConnection *new_first;
991 struct PeerConnection *new_second;
996 GNUNET_HashCode hash_first;
997 GNUNET_HashCode hash_second;
999 hash_from_uid(first, &hash_first);
1000 hash_from_uid(second, &hash_second);
1002 add_first = GNUNET_NO;
1003 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
1005 add_first = GNUNET_YES;
1008 add_second = GNUNET_NO;
1009 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
1011 add_second = GNUNET_YES;
1014 first_iter = pg->peers[first].connected_peers;
1015 while (first_iter != NULL)
1017 if (first_iter->daemon == pg->peers[second].daemon)
1018 add_first = GNUNET_NO;
1019 first_iter = first_iter->next;
1022 second_iter = pg->peers[second].connected_peers;
1023 add_second = GNUNET_YES;
1024 while (second_iter != NULL)
1026 if (second_iter->daemon == pg->peers[first].daemon)
1027 add_second = GNUNET_NO;
1028 second_iter = second_iter->next;
1035 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1037 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
1038 new_first->daemon = pg->peers[second].daemon;
1039 new_first->next = pg->peers[first].connected_peers;
1040 pg->peers[first].connected_peers = new_first;
1042 pg->peers[first].num_connections++;
1048 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1050 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
1051 new_second->daemon = pg->peers[first].daemon;
1052 new_second->next = pg->peers[second].connected_peers;
1053 pg->peers[second].connected_peers = new_second;
1054 pg->peers[first].num_connections++;
1056 pg->peers[second].num_connections++;
1064 * Add entries to the peers blacklisted list
1066 * @param pg the peer group we are working with
1067 * @param first index of the first peer
1068 * @param second index of the second peer
1070 * @return the number of connections added (can be 0, 1 or 2)
1074 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1079 GNUNET_HashCode hash_first;
1080 GNUNET_HashCode hash_second;
1082 hash_from_uid(first, &hash_first);
1083 hash_from_uid(second, &hash_second);
1085 add_first = GNUNET_NO;
1086 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
1088 add_first = GNUNET_YES;
1091 add_second = GNUNET_NO;
1092 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
1094 add_second = GNUNET_YES;
1100 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1101 pg->peers[first].num_connections++;
1107 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1108 pg->peers[second].num_connections++;
1116 * Remove entries from the peers blacklisted list
1118 * @param pg the peer group we are working with
1119 * @param first index of the first peer
1120 * @param second index of the second peer
1122 * @return the number of connections removed (can be 0, 1 or 2)
1126 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1131 GNUNET_HashCode hash_first;
1132 GNUNET_HashCode hash_second;
1134 hash_from_uid(first, &hash_first);
1135 hash_from_uid(second, &hash_second);
1137 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
1138 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
1143 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
1149 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
1157 * Scale free network construction as described in:
1159 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1161 * Start with a network of "one" peer, then progressively add
1162 * peers up to the total number. At each step, iterate over
1163 * all possible peers and connect new peer based on number of
1164 * existing connections of the target peer.
1166 * @param pg the peer group we are dealing with
1167 * @param proc the connection processor to use
1169 * @return the number of connections created
1172 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1175 unsigned int total_connections;
1176 unsigned int outer_count;
1178 unsigned int previous_total_connections;
1182 GNUNET_assert(pg->total > 1);
1184 /* Add a connection between the first two nodes */
1185 total_connections = proc(pg, 0, 1);
1187 for (outer_count = 1; outer_count < pg->total; outer_count++)
1189 previous_total_connections = total_connections;
1190 for (i = 0; i < outer_count; i++)
1192 probability = pg->peers[i].num_connections / (double)previous_total_connections;
1193 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1194 UINT64_MAX)) / ( (double) UINT64_MAX);
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1197 "Considering connecting peer %d to peer %d\n",
1200 if (random < probability)
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1204 "Connecting peer %d to peer %d\n",
1207 total_connections += proc(pg, outer_count, i);
1212 return total_connections;
1216 * Create a topology given a peer group (set of running peers)
1217 * and a connection processor.
1219 * @param pg the peergroup to create the topology on
1220 * @param proc the connection processor to call to actually set
1221 * up connections between two peers
1223 * @return the number of connections that were set up
1227 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1231 unsigned int natLog;
1232 unsigned int randomPeer;
1233 double random, logNModifier, percentage;
1234 unsigned int smallWorldConnections;
1239 unsigned int useAnd;
1240 int connect_attempts;
1242 logNModifier = 0.5; /* FIXME: default value? */
1243 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1248 if (sscanf(p_string, "%lf", &logNModifier) != 1)
1249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1250 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1254 GNUNET_free (p_string);
1256 percentage = 0.5; /* FIXME: default percentage? */
1257 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1262 if (sscanf(p_string, "%lf", &percentage) != 1)
1263 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1264 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1268 GNUNET_free (p_string);
1270 natLog = log (pg->total);
1271 connsPerPeer = ceil (natLog * logNModifier);
1273 if (connsPerPeer % 2 == 1)
1276 smallWorldConnections = 0;
1277 connect_attempts = 0;
1278 for (i = 0; i < pg->total; i++)
1281 max = i + connsPerPeer / 2;
1282 min = i - connsPerPeer / 2;
1284 if (max > pg->total - 1)
1286 max = max - pg->total;
1292 min = pg->total - 1 + min;
1296 for (j = 0; j < connsPerPeer / 2; j++)
1298 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1299 UINT64_MAX) / ( (double) UINT64_MAX));
1300 if (random < percentage)
1302 /* Connect to uniformly selected random peer */
1304 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1306 while ((((randomPeer < max) && (randomPeer > min))
1307 && (useAnd == 0)) || (((randomPeer > min)
1308 || (randomPeer < max))
1312 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1315 smallWorldConnections +=
1316 proc (pg, i, randomPeer);
1320 nodeToConnect = i + j + 1;
1321 if (nodeToConnect > pg->total - 1)
1323 nodeToConnect = nodeToConnect - pg->total;
1326 proc (pg, i, nodeToConnect);
1332 connect_attempts += smallWorldConnections;
1334 return connect_attempts;
1338 * Create a topology given a peer group (set of running peers)
1339 * and a connection processor.
1341 * @param pg the peergroup to create the topology on
1342 * @param proc the connection processor to call to actually set
1343 * up connections between two peers
1345 * @return the number of connections that were set up
1349 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1351 unsigned int outer_count, inner_count;
1352 unsigned int cutoff;
1353 int connect_attempts;
1354 double nat_percentage;
1357 nat_percentage = 0.6; /* FIXME: default percentage? */
1358 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1363 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1364 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1365 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1369 GNUNET_free (p_string);
1374 cutoff = (unsigned int) (nat_percentage * pg->total);
1376 connect_attempts = 0;
1378 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1380 for (inner_count = outer_count + 1; inner_count < pg->total;
1383 if ((outer_count > cutoff) || (inner_count > cutoff))
1386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1387 "Connecting peer %d to peer %d\n",
1388 outer_count, inner_count);
1390 connect_attempts += proc(pg, outer_count, inner_count);
1395 return connect_attempts;
1400 * Create a topology given a peer group (set of running peers)
1401 * and a connection processor.
1403 * @param pg the peergroup to create the topology on
1404 * @param proc the connection processor to call to actually set
1405 * up connections between two peers
1407 * @return the number of connections that were set up
1411 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1413 unsigned int i, j, k;
1414 unsigned int square;
1417 unsigned int toggle = 1;
1418 unsigned int nodeToConnect;
1419 unsigned int natLog;
1420 unsigned int node1Row;
1421 unsigned int node1Col;
1422 unsigned int node2Row;
1423 unsigned int node2Col;
1424 unsigned int distance;
1425 double probability, random, percentage;
1426 unsigned int smallWorldConnections;
1427 unsigned int small_world_it;
1429 int connect_attempts;
1430 square = floor (sqrt (pg->total));
1434 percentage = 0.5; /* FIXME: default percentage? */
1435 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1440 if (sscanf(p_string, "%lf", &percentage) != 1)
1441 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1442 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1446 GNUNET_free (p_string);
1448 if (percentage < 0.0)
1450 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1451 _("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1452 "PERCENTAGE", "TESTING", percentage);
1455 probability = 0.5; /* FIXME: default percentage? */
1456 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1461 if (sscanf(p_string, "%lf", &probability) != 1)
1462 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1463 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1467 GNUNET_free (p_string);
1469 if (square * square != pg->total)
1471 while (rows * cols < pg->total)
1473 if (toggle % 2 == 0)
1482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1483 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1487 connect_attempts = 0;
1488 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1489 * to the node to its right and above. Once this is over, we'll have our torus!
1490 * Special case for the last node (if the rows and columns are not equal), connect
1491 * to the first in the row to maintain topology.
1493 for (i = 0; i < pg->total; i++)
1495 /* First connect to the node to the right */
1496 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1497 nodeToConnect = i + 1;
1498 else if (i + 1 == pg->total)
1499 nodeToConnect = rows * cols - cols;
1501 nodeToConnect = i - cols + 1;
1503 connect_attempts += proc (pg, i, nodeToConnect);
1506 nodeToConnect = (rows * cols) - cols + i;
1508 nodeToConnect = i - cols;
1510 if (nodeToConnect < pg->total)
1511 connect_attempts += proc (pg, i, nodeToConnect);
1513 natLog = log (pg->total);
1514 #if VERBOSE_TESTING > 2
1515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1516 _("natural log of %d is %d, will run %d iterations\n"),
1517 pg->total, natLog, (int) (natLog * percentage));
1518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1520 smallWorldConnections = 0;
1521 small_world_it = (unsigned int)(natLog * percentage);
1522 GNUNET_assert(small_world_it > 0 && small_world_it < (unsigned int)-1);
1523 for (i = 0; i < small_world_it; i++)
1525 for (j = 0; j < pg->total; j++)
1527 /* Determine the row and column of node at position j on the 2d torus */
1528 node1Row = j / cols;
1529 node1Col = j - (node1Row * cols);
1530 for (k = 0; k < pg->total; k++)
1532 /* Determine the row and column of node at position k on the 2d torus */
1533 node2Row = k / cols;
1534 node2Col = k - (node2Row * cols);
1535 /* Simple Cartesian distance */
1536 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1539 /* Calculate probability as 1 over the square of the distance */
1540 probability = 1.0 / (distance * distance);
1541 /* Choose a random value between 0 and 1 */
1542 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1543 UINT64_MAX)) / ( (double) UINT64_MAX);
1544 /* If random < probability, then connect the two nodes */
1545 if (random < probability)
1546 smallWorldConnections += proc (pg, j, k);
1552 connect_attempts += smallWorldConnections;
1553 #if VERBOSE_TESTING > 2
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1555 _("Total connections added for small world: %d!\n"),
1556 smallWorldConnections);
1558 return connect_attempts;
1562 * Create a topology given a peer group (set of running peers)
1563 * and a connection processor.
1565 * @param pg the peergroup to create the topology on
1566 * @param proc the connection processor to call to actually set
1567 * up connections between two peers
1569 * @return the number of connections that were set up
1573 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1576 unsigned int outer_count;
1577 unsigned int inner_count;
1578 int connect_attempts;
1582 probability = 0.5; /* FIXME: default percentage? */
1583 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1588 if (sscanf(p_string, "%lf", &probability) != 1)
1589 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1590 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1594 GNUNET_free (p_string);
1596 connect_attempts = 0;
1597 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1599 for (inner_count = outer_count + 1; inner_count < pg->total;
1602 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1603 UINT64_MAX)) / ( (double) UINT64_MAX);
1605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1606 _("rand is %f probability is %f\n"), temp_rand,
1609 if (temp_rand < probability)
1611 connect_attempts += proc (pg, outer_count, inner_count);
1616 return connect_attempts;
1620 * Create a topology given a peer group (set of running peers)
1621 * and a connection processor. This particular function creates
1622 * the connections for a 2d-torus, plus additional "closest"
1623 * connections per peer.
1625 * @param pg the peergroup to create the topology on
1626 * @param proc the connection processor to call to actually set
1627 * up connections between two peers
1629 * @return the number of connections that were set up
1633 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1636 unsigned int square;
1639 unsigned int toggle = 1;
1640 unsigned int nodeToConnect;
1641 int connect_attempts;
1643 connect_attempts = 0;
1645 square = floor (sqrt (pg->total));
1649 if (square * square != pg->total)
1651 while (rows * cols < pg->total)
1653 if (toggle % 2 == 0)
1662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1663 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1666 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1667 * to the node to its right and above. Once this is over, we'll have our torus!
1668 * Special case for the last node (if the rows and columns are not equal), connect
1669 * to the first in the row to maintain topology.
1671 for (i = 0; i < pg->total; i++)
1673 /* First connect to the node to the right */
1674 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1675 nodeToConnect = i + 1;
1676 else if (i + 1 == pg->total)
1677 nodeToConnect = rows * cols - cols;
1679 nodeToConnect = i - cols + 1;
1681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1682 "Connecting peer %d to peer %d\n",
1685 connect_attempts += proc(pg, i, nodeToConnect);
1687 /* Second connect to the node immediately above */
1689 nodeToConnect = (rows * cols) - cols + i;
1691 nodeToConnect = i - cols;
1693 if (nodeToConnect < pg->total)
1696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1697 "Connecting peer %d to peer %d\n",
1700 connect_attempts += proc(pg, i, nodeToConnect);
1705 return connect_attempts;
1710 * Create a topology given a peer group (set of running peers)
1711 * and a connection processor.
1713 * @param pg the peergroup to create the topology on
1714 * @param proc the connection processor to call to actually set
1715 * up connections between two peers
1717 * @return the number of connections that were set up
1721 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1723 unsigned int outer_count;
1724 unsigned int inner_count;
1725 int connect_attempts;
1727 connect_attempts = 0;
1729 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1731 for (inner_count = outer_count + 1; inner_count < pg->total;
1735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1736 "Connecting peer %d to peer %d\n",
1737 outer_count, inner_count);
1739 connect_attempts += proc(pg, outer_count, inner_count);
1743 return connect_attempts;
1747 * Create a topology given a peer group (set of running peers)
1748 * and a connection processor.
1750 * @param pg the peergroup to create the topology on
1751 * @param proc the connection processor to call to actually set
1752 * up connections between two peers
1754 * @return the number of connections that were set up
1758 create_line (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1761 int connect_attempts;
1763 connect_attempts = 0;
1765 /* Connect each peer to the next highest numbered peer */
1766 for (count = 0; count < pg->total - 1; count++)
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1770 "Connecting peer %d to peer %d\n",
1773 connect_attempts += proc(pg, count, count + 1);
1776 return connect_attempts;
1780 * Create a topology given a peer group (set of running peers)
1781 * and a connection processor.
1783 * @param pg the peergroup to create the topology on
1784 * @param proc the connection processor to call to actually set
1785 * up connections between two peers
1787 * @return the number of connections that were set up
1791 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1794 int connect_attempts;
1796 connect_attempts = 0;
1798 /* Connect each peer to the next highest numbered peer */
1799 for (count = 0; count < pg->total - 1; count++)
1802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1803 "Connecting peer %d to peer %d\n",
1806 connect_attempts += proc(pg, count, count + 1);
1809 /* Connect the last peer to the first peer */
1810 connect_attempts += proc(pg, pg->total - 1, 0);
1812 return connect_attempts;
1817 * Iterator for writing friends of a peer to a file.
1819 * @param cls closure, an open writable file handle
1820 * @param key the key the daemon was stored under
1821 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1823 * @return GNUNET_YES to continue iteration
1825 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1826 * with a single file_iterator that takes a closure which contains
1827 * the prefix to write before the peer. Then this could be used
1828 * for blacklisting multiple transports and writing the friend
1829 * file. I'm sure *someone* will complain loudly about other
1830 * things that negate these functions even existing so no point in
1834 friend_file_iterator (void *cls,
1835 const GNUNET_HashCode * key,
1838 FILE *temp_friend_handle = cls;
1839 struct GNUNET_TESTING_Daemon *peer = value;
1840 struct GNUNET_PeerIdentity *temppeer;
1841 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1843 temppeer = &peer->id;
1844 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1845 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1850 struct BlacklistContext
1853 * The (open) file handle to write to
1855 FILE *temp_file_handle;
1858 * The transport that this peer will be blacklisted on.
1864 * Iterator for writing blacklist data to appropriate files.
1866 * @param cls closure, an open writable file handle
1867 * @param key the key the daemon was stored under
1868 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1870 * @return GNUNET_YES to continue iteration
1873 blacklist_file_iterator (void *cls,
1874 const GNUNET_HashCode * key,
1877 struct BlacklistContext *blacklist_ctx = cls;
1878 //FILE *temp_blacklist_handle = cls;
1879 struct GNUNET_TESTING_Daemon *peer = value;
1880 struct GNUNET_PeerIdentity *temppeer;
1881 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1883 temppeer = &peer->id;
1884 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1885 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1891 * Create the friend files based on the PeerConnection's
1892 * of each peer in the peer group, and copy the files
1893 * to the appropriate place
1895 * @param pg the peer group we are dealing with
1898 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1900 FILE *temp_friend_handle;
1901 unsigned int pg_iter;
1902 char *temp_service_path;
1906 enum GNUNET_OS_ProcessStatusType type;
1907 unsigned long return_code;
1912 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1913 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1915 mytemp = GNUNET_DISK_mktemp("friends");
1916 GNUNET_assert(mytemp != NULL);
1917 temp_friend_handle = fopen (mytemp, "wt");
1918 GNUNET_assert(temp_friend_handle != NULL);
1919 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1920 fclose(temp_friend_handle);
1923 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1925 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1926 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1929 if (UNLINK (mytemp) != 0)
1930 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1931 GNUNET_free (mytemp);
1935 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1937 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1938 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1939 "mv", mytemp, arg, NULL);
1941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1942 _("Copying file with command cp %s %s\n"), mytemp, arg);
1947 else /* Remote, scp the file to the correct place */
1949 if (NULL != pg->peers[pg_iter].daemon->username)
1950 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1952 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1953 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1954 "scp", mytemp, arg, NULL);
1957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1958 _("Copying file with command scp %s %s\n"), mytemp, arg);
1962 GNUNET_free (temp_service_path);
1963 GNUNET_free (mytemp);
1967 ret = GNUNET_SYSERR;
1968 while ((count < max_wait) && (ret != GNUNET_OK))
1971 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1975 _("Checking copy status of file %d\n"), pg_iter);
1977 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1979 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1981 ret = GNUNET_SYSERR;
1983 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1985 ret = GNUNET_SYSERR;
1989 pidarr[pg_iter] = 0;
1991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1992 _("File %d copied\n"), pg_iter);
1998 if (ret == GNUNET_SYSERR)
2000 /* FIXME: why sleep here? -CG */
2006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2007 _("Finished copying all friend files!\n"));
2009 GNUNET_free(pidarr);
2015 * Create the blacklist files based on the PeerConnection's
2016 * of each peer in the peer group, and copy the files
2017 * to the appropriate place.
2019 * @param pg the peer group we are dealing with
2020 * @param transports space delimited list of transports to blacklist
2023 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
2025 FILE *temp_file_handle;
2026 static struct BlacklistContext blacklist_ctx;
2027 unsigned int pg_iter;
2028 char *temp_service_path;
2032 enum GNUNET_OS_ProcessStatusType type;
2033 unsigned long return_code;
2040 char *temp_transports;
2042 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
2043 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2045 mytemp = GNUNET_DISK_mktemp("blacklist");
2046 GNUNET_assert(mytemp != NULL);
2047 temp_file_handle = fopen (mytemp, "wt");
2048 GNUNET_assert(temp_file_handle != NULL);
2049 temp_transports = GNUNET_strdup(transports);
2050 blacklist_ctx.temp_file_handle = temp_file_handle;
2051 transport_len = strlen(temp_transports) + 1;
2054 for (i = 0; i < transport_len; i++)
2056 if ((temp_transports[i] == ' ') && (pos == NULL))
2057 continue; /* At start of string (whitespace) */
2058 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2060 temp_transports[i] = '\0';
2061 blacklist_ctx.transport = pos;
2062 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
2064 } /* At beginning of actual string */
2065 else if (pos == NULL)
2067 pos = &temp_transports[i];
2071 GNUNET_free (temp_transports);
2072 fclose(temp_file_handle);
2075 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
2077 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2078 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2081 if (UNLINK (mytemp) != 0)
2082 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
2083 GNUNET_free (mytemp);
2087 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2089 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2090 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2091 "mv", mytemp, arg, NULL);
2093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2094 _("Copying file with command cp %s %s\n"), mytemp, arg);
2099 else /* Remote, scp the file to the correct place */
2101 if (NULL != pg->peers[pg_iter].daemon->username)
2102 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
2104 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
2105 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
2106 "scp", mytemp, arg, NULL);
2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2110 _("Copying file with command scp %s %s\n"), mytemp, arg);
2114 GNUNET_free (temp_service_path);
2115 GNUNET_free (mytemp);
2119 ret = GNUNET_SYSERR;
2120 while ((count < max_wait) && (ret != GNUNET_OK))
2123 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2127 _("Checking copy status of file %d\n"), pg_iter);
2129 if (pidarr[pg_iter] != 0) /* Check for already completed! */
2131 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
2133 ret = GNUNET_SYSERR;
2135 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
2137 ret = GNUNET_SYSERR;
2141 pidarr[pg_iter] = 0;
2143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2144 _("File %d copied\n"), pg_iter);
2150 if (ret == GNUNET_SYSERR)
2152 /* FIXME: why sleep here? -CG */
2158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2159 _("Finished copying all blacklist files!\n"));
2161 GNUNET_free(pidarr);
2167 * Internal notification of a connection, kept so that we can ensure some connections
2168 * happen instead of flooding all testing daemons with requests to connect.
2170 static void internal_connect_notify (void *cls,
2171 const struct GNUNET_PeerIdentity *first,
2172 const struct GNUNET_PeerIdentity *second,
2174 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2175 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2176 struct GNUNET_TESTING_Daemon *first_daemon,
2177 struct GNUNET_TESTING_Daemon *second_daemon,
2180 struct GNUNET_TESTING_PeerGroup *pg = cls;
2181 outstanding_connects--;
2183 pg->notify_connection(pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
2188 * Either delay a connection (because there are too many outstanding)
2189 * or schedule it for right now.
2191 * @param cls a connection context
2192 * @param tc the task runtime context
2194 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2196 struct ConnectContext *connect_context = cls;
2198 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2201 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2203 #if VERBOSE_TESTING > 2
2204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2205 _("Delaying connect, we have too many outstanding connections!\n"));
2207 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_connect, connect_context);
2211 #if VERBOSE_TESTING > 2
2212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2213 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
2215 outstanding_connects++;
2216 GNUNET_TESTING_daemons_connect (connect_context->first,
2217 connect_context->second,
2220 &internal_connect_notify,
2221 connect_context->pg);
2222 GNUNET_free(connect_context);
2228 * Iterator for actually scheduling connections to be created
2229 * between two peers.
2231 * @param cls closure, a GNUNET_TESTING_Daemon
2232 * @param key the key the second Daemon was stored under
2233 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2235 * @return GNUNET_YES to continue iteration
2238 connect_iterator (void *cls,
2239 const GNUNET_HashCode * key,
2242 struct PeerData *first = cls;
2243 struct GNUNET_TESTING_Daemon *second = value;
2244 struct ConnectContext *connect_context;
2246 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2247 connect_context->pg = first->pg;
2248 connect_context->first = first->daemon;
2249 connect_context->second = second;
2250 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
2257 * Iterator for copying all entries in the allowed hashmap to the
2260 * @param cls closure, a GNUNET_TESTING_Daemon
2261 * @param key the key the second Daemon was stored under
2262 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2264 * @return GNUNET_YES to continue iteration
2267 copy_topology_iterator (void *cls,
2268 const GNUNET_HashCode * key,
2271 struct PeerData *first = cls;
2273 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2279 * Make the peers to connect the same as those that are allowed to be
2282 * @param pg the peer group
2285 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2287 unsigned int pg_iter;
2292 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2294 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
2295 if (GNUNET_SYSERR == ret)
2296 return GNUNET_SYSERR;
2298 total = total + ret;
2306 * Connect the topology as specified by the PeerConnection's
2307 * of each peer in the peer group
2309 * @param pg the peer group we are dealing with
2310 * @return the number of connections that will be attempted
2313 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
2315 unsigned int pg_iter;
2319 struct PeerConnection *connection_iter;
2320 struct ConnectContext *connect_context;
2324 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2326 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
2327 if (GNUNET_SYSERR == ret)
2328 return GNUNET_SYSERR;
2330 total = total + ret;
2334 while (connection_iter != NULL)
2336 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2337 connect_context->pg = pg;
2338 connect_context->first = ;
2339 connect_context->second = connection_iter->daemon;
2340 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
2341 connection_iter = connection_iter->next;
2350 * Takes a peer group and creates a topology based on the
2351 * one specified. Creates a topology means generates friend
2352 * files for the peers so they can only connect to those allowed
2353 * by the topology. This will only have an effect once peers
2354 * are started if the FRIENDS_ONLY option is set in the base
2355 * config. Also takes an optional restrict topology which
2356 * disallows connections based on a particular transport
2357 * UNLESS they are specified in the restricted topology.
2359 * @param pg the peer group struct representing the running peers
2360 * @param topology which topology to connect the peers in
2361 * @param restrict_topology allow only direct TCP connections in this topology
2362 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2363 * @param restrict_transports space delimited list of transports to blacklist
2364 * to create restricted topology
2366 * @return the maximum number of connections were all allowed peers
2367 * connected to each other
2370 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2371 enum GNUNET_TESTING_Topology topology,
2372 enum GNUNET_TESTING_Topology restrict_topology,
2373 char *restrict_transports)
2376 int num_connections;
2377 int unblacklisted_connections;
2379 GNUNET_assert (pg->notify_connection != NULL);
2383 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2386 _("Creating clique topology\n"));
2388 num_connections = create_clique (pg, &add_allowed_connections);
2390 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2393 _("Creating small world (ring) topology\n"));
2395 num_connections = create_small_world_ring (pg, &add_allowed_connections);
2397 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2400 _("Creating small world (2d-torus) topology\n"));
2402 num_connections = create_small_world (pg, &add_allowed_connections);
2404 case GNUNET_TESTING_TOPOLOGY_RING:
2406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2407 _("Creating ring topology\n"));
2409 num_connections = create_ring (pg, &add_allowed_connections);
2411 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2414 _("Creating 2d torus topology\n"));
2416 num_connections = create_2d_torus (pg, &add_allowed_connections);
2418 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2421 _("Creating Erdos-Renyi topology\n"));
2423 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2425 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2428 _("Creating InterNAT topology\n"));
2430 num_connections = create_nated_internet (pg, &add_allowed_connections);
2432 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2435 _("Creating Scale Free topology\n"));
2437 num_connections = create_scale_free (pg, &add_allowed_connections);
2439 case GNUNET_TESTING_TOPOLOGY_LINE:
2441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2442 _("Creating straight line topology\n"));
2444 num_connections = create_line (pg, &add_allowed_connections);
2446 case GNUNET_TESTING_TOPOLOGY_NONE:
2448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2449 _("Creating no allowed topology (all peers can connect at core level)\n"));
2451 num_connections = 0;
2454 num_connections = 0;
2458 if (num_connections < 0)
2459 return GNUNET_SYSERR;
2461 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2463 ret = create_and_copy_friend_files(pg);
2464 if (ret != GNUNET_OK)
2467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2468 _("Failed during friend file copying!\n"));
2470 return GNUNET_SYSERR;
2475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2476 _("Friend files created/copied successfully!\n"));
2481 /* Use the create clique method to initially set all connections as blacklisted. */
2482 if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2483 create_clique (pg, &blacklist_connections);
2484 unblacklisted_connections = 0;
2485 /* Un-blacklist connections as per the topology specified */
2486 switch (restrict_topology)
2488 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2491 _("Blacklisting all but clique topology\n"));
2493 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2495 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2498 _("Blacklisting all but small world (ring) topology\n"));
2500 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2502 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2505 _("Blacklisting all but small world (2d-torus) topology\n"));
2507 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2509 case GNUNET_TESTING_TOPOLOGY_RING:
2511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2512 _("Blacklisting all but ring topology\n"));
2514 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2516 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2519 _("Blacklisting all but 2d torus topology\n"));
2521 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2523 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2526 _("Blacklisting all but Erdos-Renyi topology\n"));
2528 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2530 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2533 _("Blacklisting all but InterNAT topology\n"));
2535 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2537 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2540 _("Blacklisting all but Scale Free topology\n"));
2542 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2544 case GNUNET_TESTING_TOPOLOGY_LINE:
2546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2547 _("Blacklisting all but straight line topology\n"));
2549 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2551 case GNUNET_TESTING_TOPOLOGY_NONE:
2553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2554 _("Creating no blacklist topology (all peers can connect at transport level)\n"));
2560 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2562 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2563 if (ret != GNUNET_OK)
2566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2567 _("Failed during blacklist file copying!\n"));
2569 return GNUNET_SYSERR;
2574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2575 _("Blacklist files created/copied successfully!\n"));
2579 return num_connections;
2582 struct RandomContext
2587 struct GNUNET_TESTING_PeerGroup *pg;
2590 * uid of the first peer
2595 * Peer data for first peer.
2597 struct PeerData *first;
2600 * Random percentage to use
2605 struct MinimumContext
2610 struct GNUNET_TESTING_PeerGroup *pg;
2613 * uid of the first peer
2618 * Peer data for first peer.
2620 struct PeerData *first;
2623 * Number of conns per peer
2625 unsigned int num_to_add;
2628 * Permuted array of all possible connections. Only add the Nth
2629 * peer if it's in the Nth position.
2631 unsigned int *pg_array;
2634 * What number is the current element we are iterating over?
2636 unsigned int current;
2644 struct GNUNET_TESTING_PeerGroup *pg;
2647 * uid of the first peer
2652 * uid of the second peer
2654 uint32_t second_uid;
2657 * Peer data for first peer.
2659 struct PeerData *first;
2662 * Which peer has been chosen as the one to add?
2664 unsigned int chosen;
2667 * What number is the current element we are iterating over?
2669 unsigned int current;
2673 * Iterator for choosing random peers to connect.
2675 * @param cls closure, a RandomContext
2676 * @param key the key the second Daemon was stored under
2677 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2679 * @return GNUNET_YES to continue iteration
2682 random_connect_iterator (void *cls,
2683 const GNUNET_HashCode * key,
2686 struct RandomContext *random_ctx = cls;
2687 double random_number;
2688 uint32_t second_pos;
2689 GNUNET_HashCode first_hash;
2690 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2691 UINT64_MAX)) / ( (double) UINT64_MAX);
2692 if (random_number < random_ctx->percentage)
2694 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2696 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2697 uid_from_hash(key, &second_pos);
2698 hash_from_uid(random_ctx->first_uid, &first_hash);
2699 GNUNET_assert(random_ctx->pg->total > second_pos);
2700 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2706 * Iterator for adding at least X peers to a peers connection set.
2708 * @param cls closure, MinimumContext
2709 * @param key the key the second Daemon was stored under
2710 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2712 * @return GNUNET_YES to continue iteration
2715 minimum_connect_iterator (void *cls,
2716 const GNUNET_HashCode * key,
2719 struct MinimumContext *min_ctx = cls;
2720 uint32_t second_pos;
2721 GNUNET_HashCode first_hash;
2724 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2726 for (i = 0; i < min_ctx->num_to_add; i++)
2728 if (min_ctx->pg_array[i] == min_ctx->current)
2730 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2731 uid_from_hash(key, &second_pos);
2732 hash_from_uid(min_ctx->first_uid, &first_hash);
2733 GNUNET_assert(min_ctx->pg->total > second_pos);
2734 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->pg->peers[second_pos].connect_peers_working_set, &first_hash, min_ctx->first->daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2735 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2736 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2743 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2749 * Iterator for adding peers to a connection set based on a depth first search.
2751 * @param cls closure, MinimumContext
2752 * @param key the key the second daemon was stored under
2753 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2755 * @return GNUNET_YES to continue iteration
2758 dfs_connect_iterator (void *cls,
2759 const GNUNET_HashCode * key,
2762 struct DFSContext *dfs_ctx = cls;
2763 GNUNET_HashCode first_hash;
2765 if (dfs_ctx->current == dfs_ctx->chosen)
2767 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2768 uid_from_hash(key, &dfs_ctx->second_uid);
2769 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2770 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers_working_set, &first_hash, dfs_ctx->first->daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2771 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2772 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2773 return GNUNET_NO; /* We have found our peer, don't iterate more */
2782 * From the set of connections possible, choose percentage percent of connections
2783 * to actually connect.
2785 * @param pg the peergroup we are dealing with
2786 * @param percentage what percent of total connections to make
2789 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2791 struct RandomContext random_ctx;
2794 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2796 random_ctx.first_uid = pg_iter;
2797 random_ctx.first = &pg->peers[pg_iter];
2798 random_ctx.percentage = percentage;
2800 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2801 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2802 /* Now remove the old connections */
2803 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2804 /* And replace with the random set */
2805 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2810 * From the set of connections possible, choose at least num connections per
2813 * @param pg the peergroup we are dealing with
2814 * @param num how many connections at least should each peer have (if possible)?
2817 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2819 struct MinimumContext minimum_ctx;
2822 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2824 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2827 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2829 minimum_ctx.first_uid = pg_iter;
2830 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2831 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2832 minimum_ctx.first = &pg->peers[pg_iter];
2833 minimum_ctx.pg = pg;
2834 minimum_ctx.num_to_add = num;
2835 minimum_ctx.current = 0;
2836 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2837 &minimum_connect_iterator,
2841 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2843 /* Remove the "old" connections */
2844 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2845 /* And replace with the working set */
2846 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2853 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2856 unsigned int pg_iter;
2860 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2862 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2869 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2872 unsigned int pg_iter;
2876 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2878 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2885 struct FindClosestContext
2888 * The currently known closest peer.
2890 struct GNUNET_TESTING_Daemon *closest;
2893 * The info for the peer we are adding connections for.
2895 struct PeerData *curr_peer;
2898 * The distance (bits) between the current
2899 * peer and the currently known closest.
2901 unsigned int closest_dist;
2904 * The offset of the closest known peer in
2907 unsigned int closest_num;
2911 * Iterator over hash map entries of the allowed
2912 * peer connections. Find the closest, not already
2913 * connected peer and return it.
2915 * @param cls closure (struct FindClosestContext)
2916 * @param key current key code (hash of offset in pg)
2917 * @param value value in the hash map - a GNUNET_TESTING_Daemon
2918 * @return GNUNET_YES if we should continue to
2923 int find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
2925 struct FindClosestContext *closest_ctx = cls;
2926 struct GNUNET_TESTING_Daemon *daemon = value;
2928 if (((closest_ctx->closest == NULL) ||
2929 (GNUNET_CRYPTO_hash_matching_bits(&daemon->id.hashPubKey, &closest_ctx->curr_peer->daemon->id.hashPubKey) > closest_ctx->closest_dist))
2930 && (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains(closest_ctx->curr_peer->connect_peers, key)))
2932 closest_ctx->closest_dist = GNUNET_CRYPTO_hash_matching_bits(&daemon->id.hashPubKey, &closest_ctx->curr_peer->daemon->id.hashPubKey);
2933 closest_ctx->closest = daemon;
2934 uid_from_hash(key, &closest_ctx->closest_num);
2940 * From the set of connections possible, choose at num connections per
2941 * peer based on depth which are closest out of those allowed. Guaranteed
2942 * to add num peers to connect to, provided there are that many peers
2943 * in the underlay topology to connect to.
2945 * @param pg the peergroup we are dealing with
2946 * @param num how many connections at least should each peer have (if possible)?
2947 * @param proc processor to actually add the connections
2950 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num, GNUNET_TESTING_ConnectionProcessor proc)
2952 struct FindClosestContext closest_ctx;
2956 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
2958 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2960 closest_ctx.curr_peer = &pg->peers[pg_iter];
2961 closest_ctx.closest = NULL;
2962 closest_ctx.closest_dist = 0;
2963 closest_ctx.closest_num = 0;
2964 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &find_closest_peers, &closest_ctx);
2965 if (closest_ctx.closest != NULL)
2967 GNUNET_assert((0 <= closest_ctx.closest_num) && (closest_ctx.closest_num < pg->total));
2968 proc(pg, pg_iter, closest_ctx.closest_num);
2975 * From the set of connections possible, choose at least num connections per
2976 * peer based on depth first traversal of peer connections. If DFS leaves
2977 * peers unconnected, ensure those peers get connections.
2979 * @param pg the peergroup we are dealing with
2980 * @param num how many connections at least should each peer have (if possible)?
2983 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2985 struct DFSContext dfs_ctx;
2988 uint32_t starting_peer;
2989 uint32_t least_connections;
2990 GNUNET_HashCode second_hash;
2992 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2994 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2999 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
3001 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3003 least_connections = -1; /* Set to very high number */
3004 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3006 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
3008 starting_peer = pg_iter;
3009 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
3014 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
3020 /* Choose a random peer from the chosen peers set of connections to add */
3021 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
3022 dfs_ctx.first_uid = starting_peer;
3023 dfs_ctx.first = &pg->peers[starting_peer];
3025 dfs_ctx.current = 0;
3027 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
3028 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
3029 hash_from_uid(dfs_ctx.second_uid, &second_hash);
3030 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
3031 starting_peer = dfs_ctx.second_uid;
3034 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3036 /* Remove the "old" connections */
3037 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
3038 /* And replace with the working set */
3039 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
3044 * Internal callback for topology information for a particular peer.
3047 internal_topology_callback(void *cls,
3048 const struct GNUNET_PeerIdentity *peer,
3049 struct GNUNET_TIME_Relative latency, uint32_t distance)
3051 struct CoreContext *core_ctx = cls;
3052 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
3054 if (peer == NULL) /* Either finished, or something went wrong */
3056 iter_ctx->completed++;
3057 iter_ctx->connected--;
3058 /* One core context allocated per iteration, must free! */
3059 GNUNET_free(core_ctx);
3063 iter_ctx->topology_cb(iter_ctx->cls, &core_ctx->daemon->id, peer, latency, distance, NULL);
3066 if (iter_ctx->completed == iter_ctx->total)
3068 iter_ctx->topology_cb(iter_ctx->cls, NULL, NULL, GNUNET_TIME_relative_get_zero(), 0, NULL);
3069 /* Once all are done, free the iteration context */
3070 GNUNET_free(iter_ctx);
3076 * Check running topology iteration tasks, if below max start a new one, otherwise
3077 * schedule for some time in the future.
3080 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3082 struct CoreContext *core_context = cls;
3083 struct TopologyIterateContext *topology_context = (struct TopologyIterateContext *)core_context->iter_context;
3084 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3087 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3089 #if VERBOSE_TESTING > 2
3090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3091 _("Delaying connect, we have too many outstanding connections!\n"));
3093 GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_topology, core_context);
3097 #if VERBOSE_TESTING > 2
3098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3099 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3101 topology_context->connected++;
3102 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->sched, core_context->daemon->cfg, &internal_topology_callback, core_context))
3103 internal_topology_callback(core_context, NULL, GNUNET_TIME_relative_get_zero(), 0);
3109 * Iterate over all (running) peers in the peer group, retrieve
3110 * all connections that each currently has.
3113 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyTopology cb, void *cls)
3115 struct TopologyIterateContext *topology_context;
3116 struct CoreContext *core_ctx;
3118 unsigned int total_count;
3120 /* Allocate a single topology iteration context */
3121 topology_context = GNUNET_malloc(sizeof(struct TopologyIterateContext));
3122 topology_context->topology_cb = cb;
3123 topology_context->cls = cls;
3125 for (i = 0; i < pg->total; i++)
3127 if (pg->peers[i].daemon->running == GNUNET_YES)
3129 /* Allocate one core context per core we need to connect to */
3130 core_ctx = GNUNET_malloc(sizeof(struct CoreContext));
3131 core_ctx->daemon = pg->peers[i].daemon;
3132 /* Set back pointer to topology iteration context */
3133 core_ctx->iter_context = topology_context;
3134 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_topology, core_ctx);
3138 topology_context->total = total_count;
3143 * Callback function to process statistic values.
3144 * This handler is here only really to insert a peer
3145 * identity (or daemon) so the statistics can be uniquely
3146 * tied to a single running peer.
3148 * @param cls closure
3149 * @param subsystem name of subsystem that created the statistic
3150 * @param name the name of the datum
3151 * @param value the current value
3152 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
3153 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
3155 static int internal_stats_callback (void *cls,
3156 const char *subsystem,
3161 struct StatsCoreContext *core_context = cls;
3162 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3164 return stats_context->proc(stats_context->cls, &core_context->daemon->id, subsystem, name, value, is_persistent);
3168 * Internal continuation call for statistics iteration.
3170 * @param cls closure, the CoreContext for this iteration
3171 * @param success whether or not the statistics iterations
3172 * was canceled or not (we don't care)
3174 static void internal_stats_cont (void *cls, int success)
3176 struct StatsCoreContext *core_context = cls;
3177 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3179 stats_context->connected--;
3180 stats_context->completed++;
3182 if (stats_context->completed == stats_context->total)
3184 stats_context->cont(stats_context->cls, GNUNET_YES);
3185 GNUNET_free(stats_context);
3188 if (core_context->stats_handle != NULL)
3189 GNUNET_STATISTICS_destroy(core_context->stats_handle, GNUNET_NO);
3191 GNUNET_free(core_context);
3195 * Check running topology iteration tasks, if below max start a new one, otherwise
3196 * schedule for some time in the future.
3199 schedule_get_statistics(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3201 struct StatsCoreContext *core_context = cls;
3202 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3204 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3207 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3209 #if VERBOSE_TESTING > 2
3210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3211 _("Delaying connect, we have too many outstanding connections!\n"));
3213 GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_statistics, core_context);
3217 #if VERBOSE_TESTING > 2
3218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3219 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3222 stats_context->connected++;
3223 core_context->stats_handle = GNUNET_STATISTICS_create(core_context->daemon->sched, "testing", core_context->daemon->cfg);
3224 if (core_context->stats_handle == NULL)
3226 internal_stats_cont (core_context, GNUNET_NO);
3230 core_context->stats_get_handle = GNUNET_STATISTICS_get(core_context->stats_handle, NULL, NULL, GNUNET_TIME_relative_get_forever(), &internal_stats_cont, &internal_stats_callback, core_context);
3231 if (core_context->stats_get_handle == NULL)
3232 internal_stats_cont (core_context, GNUNET_NO);
3239 * Iterate over all (running) peers in the peer group, retrieve
3240 * all statistics from each.
3243 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
3244 GNUNET_STATISTICS_Callback cont,
3245 GNUNET_TESTING_STATISTICS_Iterator proc, void *cls)
3247 struct StatsIterateContext *stats_context;
3248 struct StatsCoreContext *core_ctx;
3250 unsigned int total_count;
3252 /* Allocate a single stats iteration context */
3253 stats_context = GNUNET_malloc(sizeof(struct StatsIterateContext));
3254 stats_context->cont = cont;
3255 stats_context->proc = proc;
3256 stats_context->cls = cls;
3258 for (i = 0; i < pg->total; i++)
3260 if (pg->peers[i].daemon->running == GNUNET_YES)
3262 /* Allocate one core context per core we need to connect to */
3263 core_ctx = GNUNET_malloc(sizeof(struct StatsCoreContext));
3264 core_ctx->daemon = pg->peers[i].daemon;
3265 /* Set back pointer to topology iteration context */
3266 core_ctx->iter_context = stats_context;
3267 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_statistics, core_ctx);
3271 stats_context->total = total_count;
3276 * There are many ways to connect peers that are supported by this function.
3277 * To connect peers in the same topology that was created via the
3278 * GNUNET_TESTING_create_topology, the topology variable must be set to
3279 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
3280 * a new instance of that topology will be generated and attempted to be
3281 * connected. This could result in some connections being impossible,
3282 * because some topologies are non-deterministic.
3284 * @param pg the peer group struct representing the running peers
3285 * @param topology which topology to connect the peers in
3286 * @param options options for connecting the topology
3287 * @param option_modifier modifier for options that take a parameter
3288 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
3291 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3292 enum GNUNET_TESTING_Topology topology,
3293 enum GNUNET_TESTING_TopologyOption options,
3294 double option_modifier)
3298 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3299 #if VERBOSE_TOPOLOGY
3300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3301 _("Creating clique CONNECT topology\n"));
3303 create_clique (pg, &add_actual_connections);
3305 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3306 #if VERBOSE_TOPOLOGY
3307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3308 _("Creating small world (ring) CONNECT topology\n"));
3310 create_small_world_ring (pg, &add_actual_connections);
3312 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3313 #if VERBOSE_TOPOLOGY
3314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3315 _("Creating small world (2d-torus) CONNECT topology\n"));
3317 create_small_world (pg, &add_actual_connections);
3319 case GNUNET_TESTING_TOPOLOGY_RING:
3320 #if VERBOSE_TOPOLOGY
3321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3322 _("Creating ring CONNECT topology\n"));
3324 create_ring (pg, &add_actual_connections);
3326 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3327 #if VERBOSE_TOPOLOGY
3328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3329 _("Creating 2d torus CONNECT topology\n"));
3331 create_2d_torus (pg, &add_actual_connections);
3333 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3334 #if VERBOSE_TOPOLOGY
3335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3336 _("Creating Erdos-Renyi CONNECT topology\n"));
3338 create_erdos_renyi (pg, &add_actual_connections);
3340 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3341 #if VERBOSE_TOPOLOGY
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3343 _("Creating InterNAT CONNECT topology\n"));
3345 create_nated_internet (pg, &add_actual_connections);
3347 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3348 #if VERBOSE_TOPOLOGY
3349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3350 _("Creating Scale Free CONNECT topology\n"));
3352 create_scale_free (pg, &add_actual_connections);
3354 case GNUNET_TESTING_TOPOLOGY_LINE:
3355 #if VERBOSE_TOPOLOGY
3356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3357 _("Creating straight line CONNECT topology\n"));
3359 create_line (pg, &add_actual_connections);
3361 case GNUNET_TESTING_TOPOLOGY_NONE:
3362 #if VERBOSE_TOPOLOGY
3363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3364 _("Creating no CONNECT topology\n"));
3366 copy_allowed_topology(pg);
3369 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3370 _("Unknown topology specification, can't connect peers!\n"));
3371 return GNUNET_SYSERR;
3376 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
3377 #if VERBOSE_TOPOLOGY
3378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3379 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
3381 choose_random_connections(pg, option_modifier);
3383 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
3384 #if VERBOSE_TOPOLOGY
3385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3386 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3388 choose_minimum(pg, (unsigned int)option_modifier);
3390 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
3391 #if VERBOSE_TOPOLOGY
3392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3393 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3395 perform_dfs(pg, (int)option_modifier);
3397 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
3398 #if VERBOSE_TOPOLOGY
3399 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3400 _("Finding additional %u closest peers each (if possible)\n"), (unsigned int)option_modifier);
3402 add_closest(pg, (unsigned int)option_modifier, &add_actual_connections);
3404 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
3406 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
3412 return connect_topology(pg);
3416 * Callback that is called whenever a hostkey is generated
3417 * for a peer. Call the real callback and decrement the
3418 * starting counter for the peergroup.
3420 * @param cls closure
3421 * @param id identifier for the daemon, NULL on error
3422 * @param d handle for the daemon
3423 * @param emsg error message (NULL on success)
3425 static void internal_hostkey_callback (void *cls,
3426 const struct GNUNET_PeerIdentity *id,
3427 struct GNUNET_TESTING_Daemon *d,
3430 struct InternalStartContext *internal_context = cls;
3431 internal_context->peer->pg->starting--;
3432 internal_context->peer->pg->started++;
3433 if (internal_context->hostkey_callback != NULL)
3434 internal_context->hostkey_callback(internal_context->hostkey_cls, id, d, emsg);
3435 else if (internal_context->peer->pg->started == internal_context->peer->pg->total)
3437 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
3438 GNUNET_TESTING_daemons_continue_startup(internal_context->peer->pg);
3443 * Callback that is called whenever a peer has finished starting.
3444 * Call the real callback and decrement the starting counter
3445 * for the peergroup.
3447 * @param cls closure
3448 * @param id identifier for the daemon, NULL on error
3449 * @param d handle for the daemon
3450 * @param emsg error message (NULL on success)
3452 static void internal_startup_callback (void *cls,
3453 const struct GNUNET_PeerIdentity *id,
3454 const struct GNUNET_CONFIGURATION_Handle *cfg,
3455 struct GNUNET_TESTING_Daemon *d,
3458 struct InternalStartContext *internal_context = cls;
3459 internal_context->peer->pg->starting--;
3460 if (internal_context->start_cb != NULL)
3461 internal_context->start_cb(internal_context->start_cb_cls, id, cfg, d, emsg);
3465 internal_continue_startup (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3467 struct InternalStartContext *internal_context = cls;
3469 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3474 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
3476 internal_context->peer->pg->starting++;
3477 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
3481 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_continue_startup, internal_context);
3487 * Callback for informing us about a successful
3488 * or unsuccessful churn start call.
3490 * @param cls a ChurnContext
3491 * @param id the peer identity of the started peer
3492 * @param cfg the handle to the configuration of the peer
3493 * @param d handle to the daemon for the peer
3494 * @param emsg NULL on success, non-NULL on failure
3498 churn_start_callback (void *cls,
3499 const struct GNUNET_PeerIdentity *id,
3500 const struct GNUNET_CONFIGURATION_Handle *cfg,
3501 struct GNUNET_TESTING_Daemon *d,
3504 struct ChurnRestartContext *startup_ctx = cls;
3505 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
3507 unsigned int total_left;
3508 char *error_message;
3510 error_message = NULL;
3513 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3514 "Churn stop callback failed with error `%s'\n",
3516 churn_ctx->num_failed_start++;
3520 churn_ctx->num_to_start--;
3524 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3525 "Started peer, %d left.\n",
3526 churn_ctx->num_to_start);
3529 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3531 if (total_left == 0)
3533 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3534 GNUNET_asprintf(&error_message,
3535 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3536 churn_ctx->num_failed_start,
3537 churn_ctx->num_failed_stop);
3538 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3539 GNUNET_free_non_null(error_message);
3540 GNUNET_free(churn_ctx);
3541 GNUNET_free(startup_ctx);
3546 static void schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3548 struct PeerRestartContext *peer_restart_ctx = cls;
3549 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
3551 if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
3552 GNUNET_SCHEDULER_add_delayed(peer_restart_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_churn_restart, peer_restart_ctx);
3555 GNUNET_TESTING_daemon_start_stopped(peer_restart_ctx->daemon,
3556 startup_ctx->timeout,
3557 &churn_start_callback,
3559 GNUNET_free(peer_restart_ctx);
3564 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3566 struct InternalStartContext *internal_context = cls;
3568 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3573 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
3575 internal_context->peer->pg->starting++;
3576 internal_context->peer->daemon = GNUNET_TESTING_daemon_start (internal_context->peer->pg->sched,
3577 internal_context->peer->cfg,
3578 internal_context->timeout,
3579 internal_context->hostname,
3580 internal_context->username,
3581 internal_context->sshport,
3582 &internal_hostkey_callback,
3584 &internal_startup_callback,
3589 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_start, internal_context);
3594 * Function which continues a peer group starting up
3595 * after successfully generating hostkeys for each peer.
3597 * @param pg the peer group to continue starting
3601 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
3606 for (i = 0; i < pg->total; i++)
3608 GNUNET_SCHEDULER_add_now (pg->sched, &internal_continue_startup, &pg->peers[i].internal_context);
3609 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
3614 * Start count gnunet instances with the same set of transports and
3615 * applications. The port numbers (any option called "PORT") will be
3616 * adjusted to ensure that no two peers running on the same system
3617 * have the same port(s) in their respective configurations.
3619 * @param sched scheduler to use
3620 * @param cfg configuration template to use
3621 * @param total number of daemons to start
3622 * @param timeout total time allowed for peers to start
3623 * @param hostkey_callback function to call on each peers hostkey generation
3624 * if NULL, peers will be started by this call, if non-null,
3625 * GNUNET_TESTING_daemons_continue_startup must be called after
3626 * successful hostkey generation
3627 * @param hostkey_cls closure for hostkey callback
3628 * @param cb function to call on each daemon that was started
3629 * @param cb_cls closure for cb
3630 * @param connect_callback function to call each time two hosts are connected
3631 * @param connect_callback_cls closure for connect_callback
3632 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
3634 * @return NULL on error, otherwise handle to control peer group
3636 struct GNUNET_TESTING_PeerGroup *
3637 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
3638 const struct GNUNET_CONFIGURATION_Handle *cfg,
3640 struct GNUNET_TIME_Relative timeout,
3641 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
3643 GNUNET_TESTING_NotifyDaemonRunning cb,
3645 GNUNET_TESTING_NotifyConnection
3646 connect_callback, void *connect_callback_cls,
3647 const struct GNUNET_TESTING_Host *hostnames)
3649 struct GNUNET_TESTING_PeerGroup *pg;
3650 const struct GNUNET_TESTING_Host *hostpos;
3656 const char *hostname;
3657 const char *username;
3658 char *baseservicehome;
3659 char *newservicehome;
3661 struct GNUNET_CONFIGURATION_Handle *pcfg;
3663 unsigned int hostcnt;
3676 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
3679 pg->notify_connection = connect_callback;
3680 pg->notify_connection_cls = connect_callback_cls;
3682 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
3683 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
3684 if (NULL != hostnames)
3687 hostpos = hostnames;
3688 while (hostpos != NULL)
3690 hostpos = hostpos->next;
3693 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3696 hostpos = hostnames;
3697 while (hostpos != NULL)
3699 pg->hosts[off].minport = LOW_PORT;
3700 pg->hosts[off].hostname = GNUNET_strdup(hostpos->hostname);
3701 if (hostpos->username != NULL)
3702 pg->hosts[off].username = GNUNET_strdup(hostpos->username);
3703 pg->hosts[off].sshport = hostpos->port;
3704 hostpos = hostpos->next;
3714 pg->num_hosts = off;
3718 /* skip leading spaces */
3719 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
3722 while ('\0' != *rpos)
3724 if (isspace ( (unsigned char) *rpos))
3728 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3730 start = GNUNET_strdup (hostnames);
3732 while ('\0' != *pos)
3734 if (isspace ( (unsigned char) *pos))
3737 if (strlen (start) > 0)
3739 pg->hosts[off].minport = LOW_PORT;
3740 pg->hosts[off++].hostname = start;
3746 if (strlen (start) > 0)
3748 pg->hosts[off].minport = LOW_PORT;
3749 pg->hosts[off++].hostname = start;
3753 GNUNET_free (start);
3754 GNUNET_free (pg->hosts);
3758 minport = 0; /* make gcc happy */
3766 for (off = 0; off < total; off++)
3770 hostname = pg->hosts[off % hostcnt].hostname;
3771 username = pg->hosts[off % hostcnt].username;
3772 sshport = pg->hosts[off % hostcnt].sshport;
3773 pcfg = make_config (cfg,
3774 &pg->hosts[off % hostcnt].minport,
3783 pcfg = make_config (cfg,
3791 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3793 ("Could not create configuration for peer number %u on `%s'!\n"),
3794 off, hostname == NULL ? "localhost" : hostname);
3799 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
3802 GNUNET_asprintf (&newservicehome,
3803 "%s/%d/", baseservicehome, off);
3804 GNUNET_free (baseservicehome);
3808 tmpdir = getenv ("TMPDIR");
3809 tmpdir = tmpdir ? tmpdir : "/tmp";
3810 GNUNET_asprintf (&newservicehome,
3813 "gnunet-testing-test-test", off);
3815 GNUNET_CONFIGURATION_set_value_string (pcfg,
3817 "SERVICEHOME", newservicehome);
3818 GNUNET_free (newservicehome);
3819 pg->peers[off].cfg = pcfg;
3820 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
3821 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
3822 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
3823 pg->peers[off].pg = pg;
3825 pg->peers[off].internal_context.peer = &pg->peers[off];
3826 pg->peers[off].internal_context.timeout = timeout;
3827 pg->peers[off].internal_context.hostname = hostname;
3828 pg->peers[off].internal_context.username = username;
3829 pg->peers[off].internal_context.sshport = sshport;
3830 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
3831 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
3832 pg->peers[off].internal_context.start_cb = cb;
3833 pg->peers[off].internal_context.start_cb_cls = cb_cls;
3835 GNUNET_SCHEDULER_add_now (sched, &internal_start, &pg->peers[off].internal_context);
3842 * Get a daemon by number, so callers don't have to do nasty
3843 * offsetting operation.
3845 struct GNUNET_TESTING_Daemon *
3846 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
3848 if (position < pg->total)
3849 return pg->peers[position].daemon;
3855 * Get a daemon by peer identity, so callers can
3856 * retrieve the daemon without knowing it's offset.
3858 * @param pg the peer group to retrieve the daemon from
3859 * @param peer_id the peer identity of the daemon to retrieve
3861 * @return the daemon on success, or NULL if no such peer identity is found
3863 struct GNUNET_TESTING_Daemon *
3864 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_PeerIdentity *peer_id)
3868 for (i = 0; i < pg->total; i ++)
3870 if (0 == memcmp(&pg->peers[i].daemon->id, peer_id, sizeof(struct GNUNET_PeerIdentity)))
3871 return pg->peers[i].daemon;
3878 * Prototype of a function that will be called when a
3879 * particular operation was completed the testing library.
3881 * @param cls closure (a struct RestartContext)
3882 * @param id id of the peer that was restarted
3883 * @param cfg handle to the configuration of the peer
3884 * @param d handle to the daemon that was restarted
3885 * @param emsg NULL on success
3887 void restart_callback (void *cls,
3888 const struct GNUNET_PeerIdentity *id,
3889 const struct GNUNET_CONFIGURATION_Handle *cfg,
3890 struct GNUNET_TESTING_Daemon *d,
3893 struct RestartContext *restart_context = cls;
3897 restart_context->peers_restarted++;
3901 restart_context->peers_restart_failed++;
3904 if (restart_context->peers_restarted == restart_context->peer_group->total)
3906 restart_context->callback(restart_context->callback_cls, NULL);
3907 GNUNET_free(restart_context);
3909 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
3911 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
3912 GNUNET_free(restart_context);
3918 * Callback for informing us about a successful
3919 * or unsuccessful churn stop call.
3921 * @param cls a ChurnContext
3922 * @param emsg NULL on success, non-NULL on failure
3926 churn_stop_callback (void *cls, const char *emsg)
3928 struct ShutdownContext *shutdown_ctx = cls;
3929 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
3930 unsigned int total_left;
3931 char *error_message;
3933 error_message = NULL;
3934 shutdown_ctx->outstanding--;
3938 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3939 "Churn stop callback failed with error `%s'\n", emsg);
3940 churn_ctx->num_failed_stop++;
3944 churn_ctx->num_to_stop--;
3948 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3949 "Stopped peer, %d left.\n",
3950 churn_ctx->num_to_stop);
3952 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3954 if (total_left == 0)
3956 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3958 GNUNET_asprintf(&error_message,
3959 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3960 churn_ctx->num_failed_start,
3961 churn_ctx->num_failed_stop);
3963 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3964 GNUNET_free_non_null(error_message);
3965 GNUNET_free(churn_ctx);
3966 GNUNET_free(shutdown_ctx);
3971 * Count the number of running peers.
3973 * @param pg handle for the peer group
3975 * @return the number of currently running peers in the peer group
3978 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
3981 unsigned int running = 0;
3982 for (i = 0; i < pg->total; i++)
3984 if (pg->peers[i].daemon->running == GNUNET_YES)
3986 GNUNET_assert(running != -1);
3994 * Task to rate limit the number of outstanding peer shutdown
3995 * requests. This is necessary for making sure we don't do
3996 * too many ssh connections at once, but is generally nicer
3997 * to any system as well (graduated task starts, as opposed
3998 * to calling gnunet-arm N times all at once).
4001 schedule_churn_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
4003 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4004 struct ShutdownContext *shutdown_ctx;
4006 GNUNET_assert(peer_shutdown_ctx != NULL);
4007 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4008 GNUNET_assert(shutdown_ctx != NULL);
4010 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4011 GNUNET_SCHEDULER_add_delayed(peer_shutdown_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_churn_shutdown_task, peer_shutdown_ctx);
4014 shutdown_ctx->outstanding++;
4015 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, shutdown_ctx->cb, shutdown_ctx, GNUNET_NO, GNUNET_YES);
4016 GNUNET_free(peer_shutdown_ctx);
4021 * Simulate churn by stopping some peers (and possibly
4022 * re-starting others if churn is called multiple times). This
4023 * function can only be used to create leave-join churn (peers "never"
4024 * leave for good). First "voff" random peers that are currently
4025 * online will be taken offline; then "von" random peers that are then
4026 * offline will be put back online. No notifications will be
4027 * generated for any of these operations except for the callback upon
4030 * @param pg handle for the peer group
4031 * @param voff number of peers that should go offline
4032 * @param von number of peers that should come back online;
4033 * must be zero on first call (since "testbed_start"
4034 * always starts all of the peers)
4035 * @param timeout how long to wait for operations to finish before
4037 * @param cb function to call at the end
4038 * @param cb_cls closure for cb
4041 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
4044 struct GNUNET_TIME_Relative timeout,
4045 GNUNET_TESTING_NotifyCompletion cb,
4048 struct ChurnContext *churn_ctx;
4049 struct ShutdownContext *shutdown_ctx;
4050 struct PeerShutdownContext *peer_shutdown_ctx;
4051 struct PeerRestartContext *peer_restart_ctx;
4052 struct ChurnRestartContext *churn_startup_ctx;
4054 unsigned int running;
4055 unsigned int stopped;
4056 unsigned int total_running;
4057 unsigned int total_stopped;
4059 unsigned int *running_arr;
4060 unsigned int *stopped_arr;
4061 unsigned int *running_permute;
4062 unsigned int *stopped_permute;
4067 if ((von == 0) && (voff == 0)) /* No peers at all? */
4073 for (i = 0; i < pg->total; i++)
4075 if (pg->peers[i].daemon->running == GNUNET_YES)
4077 GNUNET_assert(running != -1);
4082 GNUNET_assert(stopped != -1);
4089 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
4090 cb(cb_cls, "Trying to stop more peers than are currently running!");
4096 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
4097 cb(cb_cls, "Trying to start more peers than are currently stopped!");
4101 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4105 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
4109 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
4111 running_permute = NULL;
4112 stopped_permute = NULL;
4115 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
4117 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
4119 total_running = running;
4120 total_stopped = stopped;
4124 churn_ctx->num_to_start = von;
4125 churn_ctx->num_to_stop = voff;
4127 churn_ctx->cb_cls = cb_cls;
4129 for (i = 0; i < pg->total; i++)
4131 if (pg->peers[i].daemon->running == GNUNET_YES)
4133 GNUNET_assert((running_arr != NULL) && (total_running > running));
4134 running_arr[running] = i;
4139 GNUNET_assert((stopped_arr != NULL) && (total_stopped > stopped));
4140 stopped_arr[stopped] = i;
4145 GNUNET_assert(running >= voff);
4148 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4149 shutdown_ctx->cb = &churn_stop_callback;
4150 shutdown_ctx->cb_cls = churn_ctx;
4151 shutdown_ctx->total_peers = voff;
4152 shutdown_ctx->timeout = timeout;
4155 for (i = 0; i < voff; i++)
4158 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
4160 GNUNET_assert(running_arr != NULL);
4161 peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4162 peer_shutdown_ctx->daemon = pg->peers[running_arr[running_permute[i]]].daemon;
4163 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4164 GNUNET_SCHEDULER_add_now(peer_shutdown_ctx->daemon->sched, &schedule_churn_shutdown_task, peer_shutdown_ctx);
4167 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
4169 &churn_stop_callback, churn_ctx,
4170 GNUNET_NO, GNUNET_YES); */
4173 GNUNET_assert(stopped >= von);
4176 churn_startup_ctx = GNUNET_malloc(sizeof(struct ChurnRestartContext));
4177 churn_startup_ctx->churn_ctx = churn_ctx;
4178 churn_startup_ctx->timeout = timeout;
4180 for (i = 0; i < von; i++)
4183 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
4185 GNUNET_assert(stopped_arr != NULL);
4186 peer_restart_ctx = GNUNET_malloc(sizeof(struct PeerRestartContext));
4187 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
4188 peer_restart_ctx->daemon = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
4189 GNUNET_SCHEDULER_add_now(peer_restart_ctx->daemon->sched, &schedule_churn_restart, peer_restart_ctx);
4191 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
4192 timeout, &churn_start_callback, churn_ctx);*/
4195 GNUNET_free_non_null(running_arr);
4196 GNUNET_free_non_null(stopped_arr);
4197 GNUNET_free_non_null(running_permute);
4198 GNUNET_free_non_null(stopped_permute);
4203 * Restart all peers in the given group.
4205 * @param pg the handle to the peer group
4206 * @param callback function to call on completion (or failure)
4207 * @param callback_cls closure for the callback function
4210 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
4211 GNUNET_TESTING_NotifyCompletion callback,
4214 struct RestartContext *restart_context;
4219 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
4220 restart_context->peer_group = pg;
4221 restart_context->peers_restarted = 0;
4222 restart_context->callback = callback;
4223 restart_context->callback_cls = callback_cls;
4225 for (off = 0; off < pg->total; off++)
4227 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
4233 * Start or stop an individual peer from the given group.
4235 * @param pg handle to the peer group
4236 * @param offset which peer to start or stop
4237 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
4238 * @param timeout how long to wait for shutdown
4239 * @param cb function to call at the end
4240 * @param cb_cls closure for cb
4243 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
4244 unsigned int offset,
4246 struct GNUNET_TIME_Relative timeout,
4247 GNUNET_TESTING_NotifyCompletion cb,
4250 struct ShutdownContext *shutdown_ctx;
4251 struct ChurnRestartContext *startup_ctx;
4252 struct ChurnContext *churn_ctx;
4254 if (GNUNET_NO == desired_status)
4256 if (NULL != pg->peers[offset].daemon)
4258 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4259 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4260 churn_ctx->num_to_start = 0;
4261 churn_ctx->num_to_stop = 1;
4263 churn_ctx->cb_cls = cb_cls;
4264 shutdown_ctx->cb_cls = churn_ctx;
4265 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
4266 timeout, &churn_stop_callback, shutdown_ctx,
4267 GNUNET_NO, GNUNET_YES);
4270 else if (GNUNET_YES == desired_status)
4272 if (NULL == pg->peers[offset].daemon)
4274 startup_ctx = GNUNET_malloc(sizeof(struct ChurnRestartContext));
4275 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4276 churn_ctx->num_to_start = 1;
4277 churn_ctx->num_to_stop = 0;
4279 churn_ctx->cb_cls = cb_cls;
4280 startup_ctx->churn_ctx = churn_ctx;
4281 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
4282 timeout, &churn_start_callback, startup_ctx);
4291 * Callback for shutting down peers in a peer group.
4293 * @param cls closure (struct ShutdownContext)
4294 * @param emsg NULL on success
4296 void internal_shutdown_callback (void *cls,
4299 struct ShutdownContext *shutdown_ctx = cls;
4301 shutdown_ctx->outstanding--;
4304 shutdown_ctx->peers_down++;
4308 shutdown_ctx->peers_failed++;
4311 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
4313 if (shutdown_ctx->peers_failed > 0)
4314 shutdown_ctx->cb(shutdown_ctx->cb_cls, "Not all peers successfully shut down!");
4316 shutdown_ctx->cb(shutdown_ctx->cb_cls, NULL);
4317 GNUNET_free(shutdown_ctx);
4323 * Task to rate limit the number of outstanding peer shutdown
4324 * requests. This is necessary for making sure we don't do
4325 * too many ssh connections at once, but is generally nicer
4326 * to any system as well (graduated task starts, as opposed
4327 * to calling gnunet-arm N times all at once).
4330 schedule_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
4332 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4333 struct ShutdownContext *shutdown_ctx;
4335 GNUNET_assert(peer_shutdown_ctx != NULL);
4336 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4337 GNUNET_assert(shutdown_ctx != NULL);
4339 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4340 GNUNET_SCHEDULER_add_delayed(peer_shutdown_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_shutdown_task, peer_shutdown_ctx);
4343 shutdown_ctx->outstanding++;
4344 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, &internal_shutdown_callback, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4345 GNUNET_free(peer_shutdown_ctx);
4350 * Shutdown all peers started in the given group.
4352 * @param pg handle to the peer group
4353 * @param timeout how long to wait for shutdown
4354 * @param cb callback to notify upon success or failure
4355 * @param cb_cls closure for cb
4358 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
4359 struct GNUNET_TIME_Relative timeout,
4360 GNUNET_TESTING_NotifyCompletion cb,
4364 struct ShutdownContext *shutdown_ctx;
4365 struct PeerShutdownContext *peer_shutdown_ctx;
4367 GNUNET_assert(pg->total > 0);
4369 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4370 shutdown_ctx->cb = cb;
4371 shutdown_ctx->cb_cls = cb_cls;
4372 shutdown_ctx->total_peers = pg->total;
4373 shutdown_ctx->timeout = timeout;
4374 /* shtudown_ctx->outstanding = 0; */
4376 for (off = 0; off < pg->total; off++)
4378 GNUNET_assert(NULL != pg->peers[off].daemon);
4379 peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4380 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
4381 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4382 GNUNET_SCHEDULER_add_now(pg->peers[off].daemon->sched, &schedule_shutdown_task, peer_shutdown_ctx);
4383 //GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4384 if (NULL != pg->peers[off].cfg)
4385 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
4386 if (pg->peers[off].allowed_peers != NULL)
4387 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
4388 if (pg->peers[off].connect_peers != NULL)
4389 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
4390 if (pg->peers[off].blacklisted_peers != NULL)
4391 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
4393 GNUNET_free (pg->peers);
4394 for (off = 0; off < pg->num_hosts; off++)
4396 GNUNET_free (pg->hosts[off].hostname);
4397 GNUNET_free_non_null (pg->hosts[off].username);
4399 GNUNET_free_non_null (pg->hosts);
4404 /* end of testing_group.c */