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 DEBUG_CHURN GNUNET_NO
38 * Lowest port used for GNUnet testing. Should be high enough to not
39 * conflict with other applications running on the hosts but be low
40 * enough to not conflict with client-ports (typically starting around
43 #define LOW_PORT 10000
46 * Highest port used for GNUnet testing. Should be low enough to not
47 * conflict with the port range for "local" ports (client apps; see
48 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
50 #define HIGH_PORT 56000
52 #define MAX_OUTSTANDING_CONNECTIONS 10
54 #define MAX_CONCURRENT_HOSTKEYS 10
56 #define MAX_CONCURRENT_STARTING 10
58 #define MAX_CONCURRENT_SHUTDOWN 10
60 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
62 #define CONNECT_ATTEMPTS 8
65 * Prototype of a function called whenever two peers would be connected
66 * in a certain topology.
68 typedef int (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup *pg,
74 * Context for handling churning a peer group
79 * Callback used to notify of churning finished
81 GNUNET_TESTING_NotifyCompletion cb;
84 * Closure for callback
89 * Number of peers that still need to be started
91 unsigned int num_to_start;
94 * Number of peers that still need to be stopped
96 unsigned int num_to_stop;
99 * Number of peers that failed to start
101 unsigned int num_failed_start;
104 * Number of peers that failed to stop
106 unsigned int num_failed_stop;
109 struct RestartContext
112 * The group of peers being restarted
114 struct GNUNET_TESTING_PeerGroup *peer_group;
117 * How many peers have been restarted thus far
119 unsigned int peers_restarted;
122 * How many peers got an error when restarting
124 unsigned int peers_restart_failed;
127 * The function to call once all peers have been restarted
129 GNUNET_TESTING_NotifyCompletion callback;
132 * Closure for callback function
139 struct ShutdownContext
142 * Total peers to wait for
144 unsigned int total_peers;
147 * Number of peers successfully shut down
149 unsigned int peers_down;
152 * Number of peers failed to shut down
154 unsigned int peers_failed;
157 * Number of peers we have started shutting
158 * down. If too many, wait on them.
160 unsigned int outstanding;
163 * Timeout for shutdown.
165 struct GNUNET_TIME_Relative timeout;
168 * Callback to call when all peers either
169 * shutdown or failed to shutdown
171 GNUNET_TESTING_NotifyCompletion cb;
180 * Individual shutdown context for a particular peer.
182 struct PeerShutdownContext
185 * Pointer to the high level shutdown context.
187 struct ShutdownContext *shutdown_ctx;
190 * The daemon handle for the peer to shut down.
192 struct GNUNET_TESTING_Daemon *daemon;
196 * Individual shutdown context for a particular peer.
198 struct PeerRestartContext
201 * Pointer to the high level restart context.
203 struct ChurnRestartContext *churn_restart_ctx;
206 * The daemon handle for the peer to shut down.
208 struct GNUNET_TESTING_Daemon *daemon;
212 struct CreateTopologyContext
216 * Function to call with number of connections
218 GNUNET_TESTING_NotifyConnections cont;
221 * Closure for connection notification
227 struct PeerConnection
232 struct PeerConnection *next;
235 * Pointer to daemon handle
237 struct GNUNET_TESTING_Daemon *daemon;
242 struct InternalStartContext
245 * Pointer to peerdata
247 struct PeerData *peer;
250 * Timeout for peer startup
252 struct GNUNET_TIME_Relative timeout;
255 * Client callback for hostkey notification
257 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
260 * Closure for hostkey_callback
265 * Client callback for peer start notification
267 GNUNET_TESTING_NotifyDaemonRunning start_cb;
275 * Hostname, where to start the peer
277 const char *hostname;
280 * Username to use when connecting to the
283 const char *username;
286 * Port to use for ssh.
292 struct ChurnRestartContext
295 * Number of restarts currently in flight.
297 unsigned int outstanding;
300 * Handle to the underlying churn context.
302 struct ChurnContext *churn_ctx;
305 * How long to allow the operation to take.
307 struct GNUNET_TIME_Relative timeout;
311 * Data we keep per peer.
316 * (Initial) configuration of the host.
317 * (initial because clients could change
318 * it and we would not know about those
321 struct GNUNET_CONFIGURATION_Handle *cfg;
324 * Handle for controlling the daemon.
326 struct GNUNET_TESTING_Daemon *daemon;
329 * The peergroup this peer belongs to.
331 struct GNUNET_TESTING_PeerGroup *pg;
334 * Hash map of allowed peer connections (F2F created topology)
336 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
339 * Hash map of blacklisted peers
341 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
344 * Hash map of peer connections
346 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
349 * Temporary hash map of peer connections
351 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
354 * Temporary variable for topology creation, should be reset before
355 * creating any topology so the count is valid once finished.
360 * Context to keep track of peers being started, to
361 * stagger hostkey generation and peer startup.
363 struct InternalStartContext internal_context;
368 * Data we keep per host.
378 * SSH username to use when connecting to this host.
383 * SSH port to use when connecting to this host.
388 * Lowest port that we have not yet used
394 struct TopologyIterateContext
397 * Callback for notifying of two connected peers.
399 GNUNET_TESTING_NotifyTopology topology_cb;
402 * Closure for topology_cb
407 * Number of peers currently connected to.
409 unsigned int connected;
412 * Number of peers we have finished iterating.
414 unsigned int completed;
417 * Number of peers total.
422 struct StatsIterateContext
425 * Continuation to call once all stats information has been retrieved.
427 GNUNET_STATISTICS_Callback cont;
430 * Proc function to call on each value received.
432 GNUNET_TESTING_STATISTICS_Iterator proc;
435 * Closure for topology_cb
440 * Number of peers currently connected to.
442 unsigned int connected;
445 * Number of peers we have finished iterating.
447 unsigned int completed;
450 * Number of peers total.
458 struct GNUNET_TESTING_Daemon *daemon;
461 struct StatsCoreContext
464 struct GNUNET_TESTING_Daemon *daemon;
466 * Handle to the statistics service.
468 struct GNUNET_STATISTICS_Handle *stats_handle;
471 * Handle for getting statistics.
473 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
477 * Handle to a group of GNUnet peers.
479 struct GNUNET_TESTING_PeerGroup
484 struct GNUNET_SCHEDULER_Handle *sched;
487 * Configuration template.
489 const struct GNUNET_CONFIGURATION_Handle *cfg;
492 * Function to call on each started daemon.
494 //GNUNET_TESTING_NotifyDaemonRunning cb;
502 * Function to call on each topology connection created
504 GNUNET_TESTING_NotifyConnection notify_connection;
507 * Callback for notify_connection
509 void *notify_connection_cls;
512 * NULL-terminated array of information about
515 struct HostData *hosts;
518 * Array of "total" peers.
520 struct PeerData *peers;
523 * Number of peers in this group.
528 * At what time should we fail the peer startup process?
530 struct GNUNET_TIME_Absolute max_timeout;
533 * How many peers are being started right now?
535 unsigned int starting;
538 * How many peers have already been started?
540 unsigned int started;
545 struct GNUNET_CONFIGURATION_Handle *ret;
546 const struct GNUNET_CONFIGURATION_Handle *orig;
547 const char *hostname;
554 struct ConnectContext
556 struct GNUNET_TESTING_Daemon *first;
558 struct GNUNET_TESTING_Daemon *second;
560 struct GNUNET_TESTING_PeerGroup *pg;
564 * Convert unique ID to hash code.
566 * @param uid unique ID to convert
567 * @param hash set to uid (extended with zeros)
570 hash_from_uid (uint32_t uid,
571 GNUNET_HashCode *hash)
573 memset (hash, 0, sizeof(GNUNET_HashCode));
574 *((uint32_t*)hash) = uid;
578 * Convert hash code to unique ID.
580 * @param uid unique ID to convert
581 * @param hash set to uid (extended with zeros)
584 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
586 memcpy (uid, hash, sizeof(uint32_t));
590 * Number of connects we are waiting on, allows us to rate limit
593 static int outstanding_connects;
596 * Get a topology from a string input.
598 * @param topology where to write the retrieved topology
599 * @param topology_string The string to attempt to
600 * get a configuration value from
601 * @return GNUNET_YES if topology string matched a
602 * known topology, GNUNET_NO if not
605 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string)
608 * Strings representing topologies in enum
610 static const char * topology_strings[] =
613 * A clique (everyone connected to everyone else).
618 * Small-world network (2d torus plus random links).
623 * Small-world network (ring plus random links).
643 * Certain percentage of peers are unable to communicate directly
644 * replicating NAT conditions
649 * Scale free topology.
654 * Straight line topology.
659 * All peers are disconnected.
667 if (topology_string == NULL)
669 while (topology_strings[curr] != NULL)
671 if (strcasecmp(topology_strings[curr], topology_string) == 0)
678 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
684 * Get connect topology option from string input.
686 * @param topology_option where to write the retrieved topology
687 * @param topology_string The string to attempt to
688 * get a configuration value from
689 * @return GNUNET_YES if string matched a known
690 * topology option, GNUNET_NO if not
693 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption *topology_option,
694 char * topology_string)
697 * Options for connecting a topology as strings.
699 static const char * topology_option_strings[] =
702 * Try to connect all peers specified in the topology.
707 * Choose a random subset of connections to create.
709 "CONNECT_RANDOM_SUBSET",
712 * Create at least X connections for each peer.
717 * Using a depth first search, create one connection
718 * per peer. If any are missed (graph disconnected)
719 * start over at those peers until all have at least one
725 * No options specified.
733 if (topology_string == NULL)
735 while (NULL != topology_option_strings[curr])
737 if (strcasecmp(topology_option_strings[curr], topology_string) == 0)
739 *topology_option = curr;
744 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
749 * Function to iterate over options. Copies
750 * the options to the target configuration,
751 * updating PORT values as needed.
754 * @param section name of the section
755 * @param option name of the option
756 * @param value value of the option
759 update_config (void *cls,
760 const char *section, const char *option, const char *value)
762 struct UpdateContext *ctx = cls;
766 char *single_variable;
767 char *per_host_variable;
768 unsigned long long num_per_host;
770 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
772 GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
773 if ((ival != 0) && (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable)))
775 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
779 GNUNET_free(single_variable);
782 if (0 == strcmp (option, "UNIXPATH"))
784 GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
785 GNUNET_asprintf(&per_host_variable, "num_%s_per_host", section);
786 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable))
788 GNUNET_snprintf (uval,
790 "/tmp/test-service-%s-%u",
795 else if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_number(ctx->orig, "testing", per_host_variable, &num_per_host))
797 GNUNET_snprintf (uval,
799 "/tmp/test-service-%s-%u",
801 ctx->fdnum % num_per_host);
804 GNUNET_free(single_variable);
805 GNUNET_free(per_host_variable);
809 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
811 value = ctx->hostname;
814 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
819 * Create a new configuration using the given configuration
820 * as a template; however, each PORT in the existing cfg
821 * must be renumbered by incrementing "*port". If we run
822 * out of "*port" numbers, return NULL.
824 * @param cfg template configuration
825 * @param port port numbers to use, update to reflect
826 * port numbers that were used
827 * @param upnum number to make unix domain socket names unique
828 * @param hostname hostname of the controlling host, to allow control connections from
829 * @param fdnum number used to offset the unix domain socket for grouped processes
830 * (such as statistics or peerinfo, which can be shared among others)
832 * @return new configuration, NULL on error
834 static struct GNUNET_CONFIGURATION_Handle *
835 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
838 const char *hostname, uint32_t * fdnum)
840 struct UpdateContext uc;
849 uc.ret = GNUNET_CONFIGURATION_create ();
850 uc.hostname = hostname;
853 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
854 if (uc.nport >= HIGH_PORT)
857 GNUNET_CONFIGURATION_destroy (uc.ret);
861 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
863 if (hostname != NULL)
864 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1; %s;", control_host, hostname);
866 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
868 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
869 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport", "ACCEPT_FROM", allowed_hosts);
870 GNUNET_CONFIGURATION_set_value_string(uc.ret, "dht", "ACCEPT_FROM", allowed_hosts);
871 GNUNET_CONFIGURATION_set_value_string(uc.ret, "statistics", "ACCEPT_FROM", allowed_hosts);
872 GNUNET_free_non_null(control_host);
873 GNUNET_free(allowed_hosts);
877 /* arm needs to know to allow connections from the host on which it is running,
878 * otherwise gnunet-arm is unable to connect to it in some instances */
879 if (hostname != NULL)
881 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
882 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", hostname);
883 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", hostname);
884 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
885 GNUNET_free(allowed_hosts);
889 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", "127.0.0.1");
890 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", "127.0.0.1");
894 *port = (uint16_t) uc.nport;
903 * Add entries to the peers connect list
905 * @param pg the peer group we are working with
906 * @param first index of the first peer
907 * @param second index of the second peer
909 * @return the number of connections added
910 * technically should only be 0 or 2
914 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
920 GNUNET_HashCode hash_first;
921 GNUNET_HashCode hash_second;
923 hash_from_uid(first, &hash_first);
924 hash_from_uid(second, &hash_second);
926 add_first = GNUNET_NO;
927 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
929 add_first = GNUNET_YES;
932 add_second = GNUNET_NO;
933 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
935 add_second = GNUNET_YES;
941 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
942 pg->peers[first].num_connections++;
948 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
949 pg->peers[second].num_connections++;
958 * Add entries to the peers allowed connections list
960 * @param pg the peer group we are working with
961 * @param first index of the first peer
962 * @param second index of the second peer
964 * @return the number of connections added (can be 0, 1 or 2)
965 * technically should only be 0 or 2, but the small price
966 * of iterating over the lists (hashmaps in the future)
967 * for being sure doesn't bother me!
971 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
975 struct PeerConnection *first_iter;
976 struct PeerConnection *second_iter;
977 struct PeerConnection *new_first;
978 struct PeerConnection *new_second;
983 GNUNET_HashCode hash_first;
984 GNUNET_HashCode hash_second;
986 hash_from_uid(first, &hash_first);
987 hash_from_uid(second, &hash_second);
989 add_first = GNUNET_NO;
990 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
992 add_first = GNUNET_YES;
995 add_second = GNUNET_NO;
996 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
998 add_second = GNUNET_YES;
1001 first_iter = pg->peers[first].connected_peers;
1002 while (first_iter != NULL)
1004 if (first_iter->daemon == pg->peers[second].daemon)
1005 add_first = GNUNET_NO;
1006 first_iter = first_iter->next;
1009 second_iter = pg->peers[second].connected_peers;
1010 add_second = GNUNET_YES;
1011 while (second_iter != NULL)
1013 if (second_iter->daemon == pg->peers[first].daemon)
1014 add_second = GNUNET_NO;
1015 second_iter = second_iter->next;
1022 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1024 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
1025 new_first->daemon = pg->peers[second].daemon;
1026 new_first->next = pg->peers[first].connected_peers;
1027 pg->peers[first].connected_peers = new_first;
1029 pg->peers[first].num_connections++;
1035 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1037 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
1038 new_second->daemon = pg->peers[first].daemon;
1039 new_second->next = pg->peers[second].connected_peers;
1040 pg->peers[second].connected_peers = new_second;
1041 pg->peers[first].num_connections++;
1043 pg->peers[second].num_connections++;
1051 * Add entries to the peers blacklisted list
1053 * @param pg the peer group we are working with
1054 * @param first index of the first peer
1055 * @param second index of the second peer
1057 * @return the number of connections added (can be 0, 1 or 2)
1061 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1066 GNUNET_HashCode hash_first;
1067 GNUNET_HashCode hash_second;
1069 hash_from_uid(first, &hash_first);
1070 hash_from_uid(second, &hash_second);
1072 add_first = GNUNET_NO;
1073 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
1075 add_first = GNUNET_YES;
1078 add_second = GNUNET_NO;
1079 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
1081 add_second = GNUNET_YES;
1087 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1088 pg->peers[first].num_connections++;
1094 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1095 pg->peers[second].num_connections++;
1103 * Remove entries from the peers blacklisted list
1105 * @param pg the peer group we are working with
1106 * @param first index of the first peer
1107 * @param second index of the second peer
1109 * @return the number of connections removed (can be 0, 1 or 2)
1113 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1118 GNUNET_HashCode hash_first;
1119 GNUNET_HashCode hash_second;
1121 hash_from_uid(first, &hash_first);
1122 hash_from_uid(second, &hash_second);
1124 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
1125 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
1130 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
1136 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
1144 * Scale free network construction as described in:
1146 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1148 * Start with a network of "one" peer, then progressively add
1149 * peers up to the total number. At each step, iterate over
1150 * all possible peers and connect new peer based on number of
1151 * existing connections of the target peer.
1153 * @param pg the peer group we are dealing with
1154 * @param proc the connection processor to use
1156 * @return the number of connections created
1159 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1162 unsigned int total_connections;
1163 unsigned int outer_count;
1165 unsigned int previous_total_connections;
1169 GNUNET_assert(pg->total > 1);
1171 /* Add a connection between the first two nodes */
1172 total_connections = proc(pg, 0, 1);
1174 for (outer_count = 1; outer_count < pg->total; outer_count++)
1176 previous_total_connections = total_connections;
1177 for (i = 0; i < outer_count; i++)
1179 probability = pg->peers[i].num_connections / (double)previous_total_connections;
1180 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1181 UINT64_MAX)) / ( (double) UINT64_MAX);
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 "Considering connecting peer %d to peer %d\n",
1187 if (random < probability)
1190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1191 "Connecting peer %d to peer %d\n",
1194 total_connections += proc(pg, outer_count, i);
1199 return total_connections;
1203 * Create a topology given a peer group (set of running peers)
1204 * and a connection processor.
1206 * @param pg the peergroup to create the topology on
1207 * @param proc the connection processor to call to actually set
1208 * up connections between two peers
1210 * @return the number of connections that were set up
1214 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1218 unsigned int natLog;
1219 unsigned int randomPeer;
1220 double random, logNModifier, percentage;
1221 unsigned int smallWorldConnections;
1226 unsigned int useAnd;
1227 int connect_attempts;
1229 logNModifier = 0.5; /* FIXME: default value? */
1230 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1235 if (sscanf(p_string, "%lf", &logNModifier) != 1)
1236 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1237 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1241 GNUNET_free (p_string);
1243 percentage = 0.5; /* FIXME: default percentage? */
1244 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1249 if (sscanf(p_string, "%lf", &percentage) != 1)
1250 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1251 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1255 GNUNET_free (p_string);
1257 natLog = log (pg->total);
1258 connsPerPeer = ceil (natLog * logNModifier);
1260 if (connsPerPeer % 2 == 1)
1263 smallWorldConnections = 0;
1264 connect_attempts = 0;
1265 for (i = 0; i < pg->total; i++)
1268 max = i + connsPerPeer / 2;
1269 min = i - connsPerPeer / 2;
1271 if (max > pg->total - 1)
1273 max = max - pg->total;
1279 min = pg->total - 1 + min;
1283 for (j = 0; j < connsPerPeer / 2; j++)
1285 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1286 UINT64_MAX) / ( (double) UINT64_MAX));
1287 if (random < percentage)
1289 /* Connect to uniformly selected random peer */
1291 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1293 while ((((randomPeer < max) && (randomPeer > min))
1294 && (useAnd == 0)) || (((randomPeer > min)
1295 || (randomPeer < max))
1299 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1302 smallWorldConnections +=
1303 proc (pg, i, randomPeer);
1307 nodeToConnect = i + j + 1;
1308 if (nodeToConnect > pg->total - 1)
1310 nodeToConnect = nodeToConnect - pg->total;
1313 proc (pg, i, nodeToConnect);
1319 connect_attempts += smallWorldConnections;
1321 return connect_attempts;
1325 * Create a topology given a peer group (set of running peers)
1326 * and a connection processor.
1328 * @param pg the peergroup to create the topology on
1329 * @param proc the connection processor to call to actually set
1330 * up connections between two peers
1332 * @return the number of connections that were set up
1336 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1338 unsigned int outer_count, inner_count;
1339 unsigned int cutoff;
1340 int connect_attempts;
1341 double nat_percentage;
1344 nat_percentage = 0.6; /* FIXME: default percentage? */
1345 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1350 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1351 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1352 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1356 GNUNET_free (p_string);
1361 cutoff = (unsigned int) (nat_percentage * pg->total);
1363 connect_attempts = 0;
1365 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1367 for (inner_count = outer_count + 1; inner_count < pg->total;
1370 if ((outer_count > cutoff) || (inner_count > cutoff))
1373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1374 "Connecting peer %d to peer %d\n",
1375 outer_count, inner_count);
1377 connect_attempts += proc(pg, outer_count, inner_count);
1382 return connect_attempts;
1387 * Create a topology given a peer group (set of running peers)
1388 * and a connection processor.
1390 * @param pg the peergroup to create the topology on
1391 * @param proc the connection processor to call to actually set
1392 * up connections between two peers
1394 * @return the number of connections that were set up
1398 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1400 unsigned int i, j, k;
1401 unsigned int square;
1404 unsigned int toggle = 1;
1405 unsigned int nodeToConnect;
1406 unsigned int natLog;
1407 unsigned int node1Row;
1408 unsigned int node1Col;
1409 unsigned int node2Row;
1410 unsigned int node2Col;
1411 unsigned int distance;
1412 double probability, random, percentage;
1413 unsigned int smallWorldConnections;
1414 unsigned int small_world_it;
1416 int connect_attempts;
1417 square = floor (sqrt (pg->total));
1421 percentage = 0.5; /* FIXME: default percentage? */
1422 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1427 if (sscanf(p_string, "%lf", &percentage) != 1)
1428 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1429 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1433 GNUNET_free (p_string);
1435 if (percentage < 0.0)
1437 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1438 _("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1439 "PERCENTAGE", "TESTING", percentage);
1442 probability = 0.5; /* FIXME: default percentage? */
1443 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1448 if (sscanf(p_string, "%lf", &probability) != 1)
1449 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1450 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1454 GNUNET_free (p_string);
1456 if (square * square != pg->total)
1458 while (rows * cols < pg->total)
1460 if (toggle % 2 == 0)
1469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1470 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1474 connect_attempts = 0;
1475 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1476 * to the node to its right and above. Once this is over, we'll have our torus!
1477 * Special case for the last node (if the rows and columns are not equal), connect
1478 * to the first in the row to maintain topology.
1480 for (i = 0; i < pg->total; i++)
1482 /* First connect to the node to the right */
1483 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1484 nodeToConnect = i + 1;
1485 else if (i + 1 == pg->total)
1486 nodeToConnect = rows * cols - cols;
1488 nodeToConnect = i - cols + 1;
1490 connect_attempts += proc (pg, i, nodeToConnect);
1493 nodeToConnect = (rows * cols) - cols + i;
1495 nodeToConnect = i - cols;
1497 if (nodeToConnect < pg->total)
1498 connect_attempts += proc (pg, i, nodeToConnect);
1500 natLog = log (pg->total);
1501 #if VERBOSE_TESTING > 2
1502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1503 _("natural log of %d is %d, will run %d iterations\n"),
1504 pg->total, natLog, (int) (natLog * percentage));
1505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1507 smallWorldConnections = 0;
1508 small_world_it = (unsigned int)(natLog * percentage);
1509 GNUNET_assert(small_world_it > 0 && small_world_it < (unsigned int)-1);
1510 for (i = 0; i < small_world_it; i++)
1512 for (j = 0; j < pg->total; j++)
1514 /* Determine the row and column of node at position j on the 2d torus */
1515 node1Row = j / cols;
1516 node1Col = j - (node1Row * cols);
1517 for (k = 0; k < pg->total; k++)
1519 /* Determine the row and column of node at position k on the 2d torus */
1520 node2Row = k / cols;
1521 node2Col = k - (node2Row * cols);
1522 /* Simple Cartesian distance */
1523 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1526 /* Calculate probability as 1 over the square of the distance */
1527 probability = 1.0 / (distance * distance);
1528 /* Choose a random value between 0 and 1 */
1529 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1530 UINT64_MAX)) / ( (double) UINT64_MAX);
1531 /* If random < probability, then connect the two nodes */
1532 if (random < probability)
1533 smallWorldConnections += proc (pg, j, k);
1539 connect_attempts += smallWorldConnections;
1540 #if VERBOSE_TESTING > 2
1541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1542 _("Total connections added for small world: %d!\n"),
1543 smallWorldConnections);
1545 return connect_attempts;
1549 * Create a topology given a peer group (set of running peers)
1550 * and a connection processor.
1552 * @param pg the peergroup to create the topology on
1553 * @param proc the connection processor to call to actually set
1554 * up connections between two peers
1556 * @return the number of connections that were set up
1560 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1563 unsigned int outer_count;
1564 unsigned int inner_count;
1565 int connect_attempts;
1569 probability = 0.5; /* FIXME: default percentage? */
1570 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1575 if (sscanf(p_string, "%lf", &probability) != 1)
1576 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1577 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1581 GNUNET_free (p_string);
1583 connect_attempts = 0;
1584 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1586 for (inner_count = outer_count + 1; inner_count < pg->total;
1589 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1590 UINT64_MAX)) / ( (double) UINT64_MAX);
1592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1593 _("rand is %f probability is %f\n"), temp_rand,
1596 if (temp_rand < probability)
1598 connect_attempts += proc (pg, outer_count, inner_count);
1603 return connect_attempts;
1607 * Create a topology given a peer group (set of running peers)
1608 * and a connection processor.
1610 * @param pg the peergroup to create the topology on
1611 * @param proc the connection processor to call to actually set
1612 * up connections between two peers
1614 * @return the number of connections that were set up
1618 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1621 unsigned int square;
1624 unsigned int toggle = 1;
1625 unsigned int nodeToConnect;
1626 int connect_attempts;
1628 connect_attempts = 0;
1630 square = floor (sqrt (pg->total));
1634 if (square * square != pg->total)
1636 while (rows * cols < pg->total)
1638 if (toggle % 2 == 0)
1647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1648 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1651 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1652 * to the node to its right and above. Once this is over, we'll have our torus!
1653 * Special case for the last node (if the rows and columns are not equal), connect
1654 * to the first in the row to maintain topology.
1656 for (i = 0; i < pg->total; i++)
1658 /* First connect to the node to the right */
1659 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1660 nodeToConnect = i + 1;
1661 else if (i + 1 == pg->total)
1662 nodeToConnect = rows * cols - cols;
1664 nodeToConnect = i - cols + 1;
1666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1667 "Connecting peer %d to peer %d\n",
1670 connect_attempts += proc(pg, i, nodeToConnect);
1672 /* Second connect to the node immediately above */
1674 nodeToConnect = (rows * cols) - cols + i;
1676 nodeToConnect = i - cols;
1678 if (nodeToConnect < pg->total)
1681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1682 "Connecting peer %d to peer %d\n",
1685 connect_attempts += proc(pg, i, nodeToConnect);
1690 return connect_attempts;
1695 * Create a topology given a peer group (set of running peers)
1696 * and a connection processor.
1698 * @param pg the peergroup to create the topology on
1699 * @param proc the connection processor to call to actually set
1700 * up connections between two peers
1702 * @return the number of connections that were set up
1706 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1708 unsigned int outer_count;
1709 unsigned int inner_count;
1710 int connect_attempts;
1712 connect_attempts = 0;
1714 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1716 for (inner_count = outer_count + 1; inner_count < pg->total;
1720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1721 "Connecting peer %d to peer %d\n",
1722 outer_count, inner_count);
1724 connect_attempts += proc(pg, outer_count, inner_count);
1728 return connect_attempts;
1732 * Create a topology given a peer group (set of running peers)
1733 * and a connection processor.
1735 * @param pg the peergroup to create the topology on
1736 * @param proc the connection processor to call to actually set
1737 * up connections between two peers
1739 * @return the number of connections that were set up
1743 create_line (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1746 int connect_attempts;
1748 connect_attempts = 0;
1750 /* Connect each peer to the next highest numbered peer */
1751 for (count = 0; count < pg->total - 1; count++)
1754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1755 "Connecting peer %d to peer %d\n",
1758 connect_attempts += proc(pg, count, count + 1);
1761 return connect_attempts;
1765 * Create a topology given a peer group (set of running peers)
1766 * and a connection processor.
1768 * @param pg the peergroup to create the topology on
1769 * @param proc the connection processor to call to actually set
1770 * up connections between two peers
1772 * @return the number of connections that were set up
1776 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1779 int connect_attempts;
1781 connect_attempts = 0;
1783 /* Connect each peer to the next highest numbered peer */
1784 for (count = 0; count < pg->total - 1; count++)
1787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1788 "Connecting peer %d to peer %d\n",
1791 connect_attempts += proc(pg, count, count + 1);
1794 /* Connect the last peer to the first peer */
1795 connect_attempts += proc(pg, pg->total - 1, 0);
1797 return connect_attempts;
1802 * Iterator for writing friends of a peer to a file.
1804 * @param cls closure, an open writable file handle
1805 * @param key the key the daemon was stored under
1806 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1808 * @return GNUNET_YES to continue iteration
1810 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1811 * with a single file_iterator that takes a closure which contains
1812 * the prefix to write before the peer. Then this could be used
1813 * for blacklisting multiple transports and writing the friend
1814 * file. I'm sure *someone* will complain loudly about other
1815 * things that negate these functions even existing so no point in
1819 friend_file_iterator (void *cls,
1820 const GNUNET_HashCode * key,
1823 FILE *temp_friend_handle = cls;
1824 struct GNUNET_TESTING_Daemon *peer = value;
1825 struct GNUNET_PeerIdentity *temppeer;
1826 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1828 temppeer = &peer->id;
1829 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1830 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1835 struct BlacklistContext
1838 * The (open) file handle to write to
1840 FILE *temp_file_handle;
1843 * The transport that this peer will be blacklisted on.
1849 * Iterator for writing blacklist data to appropriate files.
1851 * @param cls closure, an open writable file handle
1852 * @param key the key the daemon was stored under
1853 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1855 * @return GNUNET_YES to continue iteration
1858 blacklist_file_iterator (void *cls,
1859 const GNUNET_HashCode * key,
1862 struct BlacklistContext *blacklist_ctx = cls;
1863 //FILE *temp_blacklist_handle = cls;
1864 struct GNUNET_TESTING_Daemon *peer = value;
1865 struct GNUNET_PeerIdentity *temppeer;
1866 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1868 temppeer = &peer->id;
1869 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1870 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1876 * Create the friend files based on the PeerConnection's
1877 * of each peer in the peer group, and copy the files
1878 * to the appropriate place
1880 * @param pg the peer group we are dealing with
1883 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1885 FILE *temp_friend_handle;
1886 unsigned int pg_iter;
1887 char *temp_service_path;
1891 enum GNUNET_OS_ProcessStatusType type;
1892 unsigned long return_code;
1897 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1898 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1900 mytemp = GNUNET_DISK_mktemp("friends");
1901 GNUNET_assert(mytemp != NULL);
1902 temp_friend_handle = fopen (mytemp, "wt");
1903 GNUNET_assert(temp_friend_handle != NULL);
1904 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1905 fclose(temp_friend_handle);
1908 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1910 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1911 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1914 if (UNLINK (mytemp) != 0)
1915 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1916 GNUNET_free (mytemp);
1920 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1922 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1923 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1924 "mv", mytemp, arg, NULL);
1926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1927 _("Copying file with command cp %s %s\n"), mytemp, arg);
1932 else /* Remote, scp the file to the correct place */
1934 if (NULL != pg->peers[pg_iter].daemon->username)
1935 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1937 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1938 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1939 "scp", mytemp, arg, NULL);
1942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1943 _("Copying file with command scp %s %s\n"), mytemp, arg);
1947 GNUNET_free (temp_service_path);
1948 GNUNET_free (mytemp);
1952 ret = GNUNET_SYSERR;
1953 while ((count < max_wait) && (ret != GNUNET_OK))
1956 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1960 _("Checking copy status of file %d\n"), pg_iter);
1962 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1964 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1966 ret = GNUNET_SYSERR;
1968 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1970 ret = GNUNET_SYSERR;
1974 pidarr[pg_iter] = 0;
1976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1977 _("File %d copied\n"), pg_iter);
1983 if (ret == GNUNET_SYSERR)
1985 /* FIXME: why sleep here? -CG */
1991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1992 _("Finished copying all friend files!\n"));
1994 GNUNET_free(pidarr);
2000 * Create the blacklist files based on the PeerConnection's
2001 * of each peer in the peer group, and copy the files
2002 * to the appropriate place.
2004 * @param pg the peer group we are dealing with
2005 * @param transports space delimited list of transports to blacklist
2008 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
2010 FILE *temp_file_handle;
2011 static struct BlacklistContext blacklist_ctx;
2012 unsigned int pg_iter;
2013 char *temp_service_path;
2017 enum GNUNET_OS_ProcessStatusType type;
2018 unsigned long return_code;
2025 char *temp_transports;
2027 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
2028 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2030 mytemp = GNUNET_DISK_mktemp("blacklist");
2031 GNUNET_assert(mytemp != NULL);
2032 temp_file_handle = fopen (mytemp, "wt");
2033 GNUNET_assert(temp_file_handle != NULL);
2034 temp_transports = GNUNET_strdup(transports);
2035 blacklist_ctx.temp_file_handle = temp_file_handle;
2036 transport_len = strlen(temp_transports) + 1;
2039 for (i = 0; i < transport_len; i++)
2041 if ((temp_transports[i] == ' ') && (pos == NULL))
2042 continue; /* At start of string (whitespace) */
2043 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2045 temp_transports[i] = '\0';
2046 blacklist_ctx.transport = pos;
2047 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
2049 } /* At beginning of actual string */
2050 else if (pos == NULL)
2052 pos = &temp_transports[i];
2056 GNUNET_free (temp_transports);
2057 fclose(temp_file_handle);
2060 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
2062 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2063 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2066 if (UNLINK (mytemp) != 0)
2067 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
2068 GNUNET_free (mytemp);
2072 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2074 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2075 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2076 "mv", mytemp, arg, NULL);
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2079 _("Copying file with command cp %s %s\n"), mytemp, arg);
2084 else /* Remote, scp the file to the correct place */
2086 if (NULL != pg->peers[pg_iter].daemon->username)
2087 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
2089 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
2090 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
2091 "scp", mytemp, arg, NULL);
2094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2095 _("Copying file with command scp %s %s\n"), mytemp, arg);
2099 GNUNET_free (temp_service_path);
2100 GNUNET_free (mytemp);
2104 ret = GNUNET_SYSERR;
2105 while ((count < max_wait) && (ret != GNUNET_OK))
2108 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2112 _("Checking copy status of file %d\n"), pg_iter);
2114 if (pidarr[pg_iter] != 0) /* Check for already completed! */
2116 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
2118 ret = GNUNET_SYSERR;
2120 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
2122 ret = GNUNET_SYSERR;
2126 pidarr[pg_iter] = 0;
2128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2129 _("File %d copied\n"), pg_iter);
2135 if (ret == GNUNET_SYSERR)
2137 /* FIXME: why sleep here? -CG */
2143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2144 _("Finished copying all blacklist files!\n"));
2146 GNUNET_free(pidarr);
2152 * Internal notification of a connection, kept so that we can ensure some connections
2153 * happen instead of flooding all testing daemons with requests to connect.
2155 static void internal_connect_notify (void *cls,
2156 const struct GNUNET_PeerIdentity *first,
2157 const struct GNUNET_PeerIdentity *second,
2159 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2160 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2161 struct GNUNET_TESTING_Daemon *first_daemon,
2162 struct GNUNET_TESTING_Daemon *second_daemon,
2165 struct GNUNET_TESTING_PeerGroup *pg = cls;
2166 outstanding_connects--;
2168 pg->notify_connection(pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
2173 * Either delay a connection (because there are too many outstanding)
2174 * or schedule it for right now.
2176 * @param cls a connection context
2177 * @param tc the task runtime context
2179 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2181 struct ConnectContext *connect_context = cls;
2183 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2186 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2188 #if VERBOSE_TESTING > 2
2189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2190 _("Delaying connect, we have too many outstanding connections!\n"));
2192 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_connect, connect_context);
2196 #if VERBOSE_TESTING > 2
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
2200 outstanding_connects++;
2201 GNUNET_TESTING_daemons_connect (connect_context->first,
2202 connect_context->second,
2205 &internal_connect_notify,
2206 connect_context->pg);
2207 GNUNET_free(connect_context);
2213 * Iterator for actually scheduling connections to be created
2214 * between two peers.
2216 * @param cls closure, a GNUNET_TESTING_Daemon
2217 * @param key the key the second Daemon was stored under
2218 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2220 * @return GNUNET_YES to continue iteration
2223 connect_iterator (void *cls,
2224 const GNUNET_HashCode * key,
2227 struct PeerData *first = cls;
2228 struct GNUNET_TESTING_Daemon *second = value;
2229 struct ConnectContext *connect_context;
2231 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2232 connect_context->pg = first->pg;
2233 connect_context->first = first->daemon;
2234 connect_context->second = second;
2235 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
2242 * Iterator for copying all entries in the allowed hashmap to the
2245 * @param cls closure, a GNUNET_TESTING_Daemon
2246 * @param key the key the second Daemon was stored under
2247 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2249 * @return GNUNET_YES to continue iteration
2252 copy_topology_iterator (void *cls,
2253 const GNUNET_HashCode * key,
2256 struct PeerData *first = cls;
2258 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2264 * Make the peers to connect the same as those that are allowed to be
2267 * @param pg the peer group
2270 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2272 unsigned int pg_iter;
2277 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2279 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
2280 if (GNUNET_SYSERR == ret)
2281 return GNUNET_SYSERR;
2283 total = total + ret;
2291 * Connect the topology as specified by the PeerConnection's
2292 * of each peer in the peer group
2294 * @param pg the peer group we are dealing with
2295 * @return the number of connections that will be attempted
2298 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
2300 unsigned int pg_iter;
2304 struct PeerConnection *connection_iter;
2305 struct ConnectContext *connect_context;
2309 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2311 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
2312 if (GNUNET_SYSERR == ret)
2313 return GNUNET_SYSERR;
2315 total = total + ret;
2319 while (connection_iter != NULL)
2321 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2322 connect_context->pg = pg;
2323 connect_context->first = ;
2324 connect_context->second = connection_iter->daemon;
2325 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
2326 connection_iter = connection_iter->next;
2335 * Takes a peer group and creates a topology based on the
2336 * one specified. Creates a topology means generates friend
2337 * files for the peers so they can only connect to those allowed
2338 * by the topology. This will only have an effect once peers
2339 * are started if the FRIENDS_ONLY option is set in the base
2340 * config. Also takes an optional restrict topology which
2341 * disallows connections based on a particular transport
2342 * UNLESS they are specified in the restricted topology.
2344 * @param pg the peer group struct representing the running peers
2345 * @param topology which topology to connect the peers in
2346 * @param restrict_topology allow only direct TCP connections in this topology
2347 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2348 * @param restrict_transports space delimited list of transports to blacklist
2349 * to create restricted topology
2351 * @return the maximum number of connections were all allowed peers
2352 * connected to each other
2355 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2356 enum GNUNET_TESTING_Topology topology,
2357 enum GNUNET_TESTING_Topology restrict_topology,
2358 char *restrict_transports)
2361 int num_connections;
2362 int unblacklisted_connections;
2364 GNUNET_assert (pg->notify_connection != NULL);
2368 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2371 _("Creating clique topology\n"));
2373 num_connections = create_clique (pg, &add_allowed_connections);
2375 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2378 _("Creating small world (ring) topology\n"));
2380 num_connections = create_small_world_ring (pg, &add_allowed_connections);
2382 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2385 _("Creating small world (2d-torus) topology\n"));
2387 num_connections = create_small_world (pg, &add_allowed_connections);
2389 case GNUNET_TESTING_TOPOLOGY_RING:
2391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2392 _("Creating ring topology\n"));
2394 num_connections = create_ring (pg, &add_allowed_connections);
2396 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2399 _("Creating 2d torus topology\n"));
2401 num_connections = create_2d_torus (pg, &add_allowed_connections);
2403 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2406 _("Creating Erdos-Renyi topology\n"));
2408 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2410 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2413 _("Creating InterNAT topology\n"));
2415 num_connections = create_nated_internet (pg, &add_allowed_connections);
2417 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2420 _("Creating Scale Free topology\n"));
2422 num_connections = create_scale_free (pg, &add_allowed_connections);
2424 case GNUNET_TESTING_TOPOLOGY_LINE:
2426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2427 _("Creating straight line topology\n"));
2429 num_connections = create_line (pg, &add_allowed_connections);
2431 case GNUNET_TESTING_TOPOLOGY_NONE:
2433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2434 _("Creating no allowed topology (all peers can connect at core level)\n"));
2436 num_connections = 0;
2439 num_connections = 0;
2443 if (num_connections < 0)
2444 return GNUNET_SYSERR;
2446 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2448 ret = create_and_copy_friend_files(pg);
2449 if (ret != GNUNET_OK)
2452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2453 _("Failed during friend file copying!\n"));
2455 return GNUNET_SYSERR;
2460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2461 _("Friend files created/copied successfully!\n"));
2466 /* Use the create clique method to initially set all connections as blacklisted. */
2467 if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2468 create_clique (pg, &blacklist_connections);
2469 unblacklisted_connections = 0;
2470 /* Un-blacklist connections as per the topology specified */
2471 switch (restrict_topology)
2473 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2476 _("Blacklisting all but clique topology\n"));
2478 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2480 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2483 _("Blacklisting all but small world (ring) topology\n"));
2485 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2487 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2490 _("Blacklisting all but small world (2d-torus) topology\n"));
2492 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2494 case GNUNET_TESTING_TOPOLOGY_RING:
2496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2497 _("Blacklisting all but ring topology\n"));
2499 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2501 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2504 _("Blacklisting all but 2d torus topology\n"));
2506 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2508 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2511 _("Blacklisting all but Erdos-Renyi topology\n"));
2513 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2515 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2518 _("Blacklisting all but InterNAT topology\n"));
2520 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2522 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2525 _("Blacklisting all but Scale Free topology\n"));
2527 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2529 case GNUNET_TESTING_TOPOLOGY_LINE:
2531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2532 _("Blacklisting all but straight line topology\n"));
2534 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2536 case GNUNET_TESTING_TOPOLOGY_NONE:
2538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2539 _("Creating no blacklist topology (all peers can connect at transport level)\n"));
2545 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2547 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2548 if (ret != GNUNET_OK)
2551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2552 _("Failed during blacklist file copying!\n"));
2554 return GNUNET_SYSERR;
2559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2560 _("Blacklist files created/copied successfully!\n"));
2564 return num_connections;
2567 struct RandomContext
2572 struct GNUNET_TESTING_PeerGroup *pg;
2575 * uid of the first peer
2580 * Peer data for first peer.
2582 struct PeerData *first;
2585 * Random percentage to use
2590 struct MinimumContext
2595 struct GNUNET_TESTING_PeerGroup *pg;
2598 * uid of the first peer
2603 * Peer data for first peer.
2605 struct PeerData *first;
2608 * Number of conns per peer
2610 unsigned int num_to_add;
2613 * Permuted array of all possible connections. Only add the Nth
2614 * peer if it's in the Nth position.
2616 unsigned int *pg_array;
2619 * What number is the current element we are iterating over?
2621 unsigned int current;
2629 struct GNUNET_TESTING_PeerGroup *pg;
2632 * uid of the first peer
2637 * uid of the second peer
2639 uint32_t second_uid;
2642 * Peer data for first peer.
2644 struct PeerData *first;
2647 * Which peer has been chosen as the one to add?
2649 unsigned int chosen;
2652 * What number is the current element we are iterating over?
2654 unsigned int current;
2658 * Iterator for choosing random peers to connect.
2660 * @param cls closure, a RandomContext
2661 * @param key the key the second Daemon was stored under
2662 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2664 * @return GNUNET_YES to continue iteration
2667 random_connect_iterator (void *cls,
2668 const GNUNET_HashCode * key,
2671 struct RandomContext *random_ctx = cls;
2672 double random_number;
2673 uint32_t second_pos;
2674 GNUNET_HashCode first_hash;
2675 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2676 UINT64_MAX)) / ( (double) UINT64_MAX);
2677 if (random_number < random_ctx->percentage)
2679 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2681 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2682 uid_from_hash(key, &second_pos);
2683 hash_from_uid(random_ctx->first_uid, &first_hash);
2684 GNUNET_assert(random_ctx->pg->total > second_pos);
2685 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2691 * Iterator for adding at least X peers to a peers connection set.
2693 * @param cls closure, MinimumContext
2694 * @param key the key the second Daemon was stored under
2695 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2697 * @return GNUNET_YES to continue iteration
2700 minimum_connect_iterator (void *cls,
2701 const GNUNET_HashCode * key,
2704 struct MinimumContext *min_ctx = cls;
2705 uint32_t second_pos;
2706 GNUNET_HashCode first_hash;
2709 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2711 for (i = 0; i < min_ctx->num_to_add; i++)
2713 if (min_ctx->pg_array[i] == min_ctx->current)
2715 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2716 uid_from_hash(key, &second_pos);
2717 hash_from_uid(min_ctx->first_uid, &first_hash);
2718 GNUNET_assert(min_ctx->pg->total > second_pos);
2719 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));
2720 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2721 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2728 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2734 * Iterator for adding peers to a connection set based on a depth first search.
2736 * @param cls closure, MinimumContext
2737 * @param key the key the second daemon was stored under
2738 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2740 * @return GNUNET_YES to continue iteration
2743 dfs_connect_iterator (void *cls,
2744 const GNUNET_HashCode * key,
2747 struct DFSContext *dfs_ctx = cls;
2748 GNUNET_HashCode first_hash;
2750 if (dfs_ctx->current == dfs_ctx->chosen)
2752 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2753 uid_from_hash(key, &dfs_ctx->second_uid);
2754 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2755 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));
2756 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2757 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2758 return GNUNET_NO; /* We have found our peer, don't iterate more */
2767 * From the set of connections possible, choose percentage percent of connections
2768 * to actually connect.
2770 * @param pg the peergroup we are dealing with
2771 * @param percentage what percent of total connections to make
2774 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2776 struct RandomContext random_ctx;
2779 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2781 random_ctx.first_uid = pg_iter;
2782 random_ctx.first = &pg->peers[pg_iter];
2783 random_ctx.percentage = percentage;
2785 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2786 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2787 /* Now remove the old connections */
2788 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2789 /* And replace with the random set */
2790 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2795 * From the set of connections possible, choose at least num connections per
2798 * @param pg the peergroup we are dealing with
2799 * @param num how many connections at least should each peer have (if possible)?
2802 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2804 struct MinimumContext minimum_ctx;
2807 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2809 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2812 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2814 minimum_ctx.first_uid = pg_iter;
2815 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2816 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2817 minimum_ctx.first = &pg->peers[pg_iter];
2818 minimum_ctx.pg = pg;
2819 minimum_ctx.num_to_add = num;
2820 minimum_ctx.current = 0;
2821 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2822 &minimum_connect_iterator,
2826 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2828 /* Remove the "old" connections */
2829 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2830 /* And replace with the working set */
2831 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2838 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2841 unsigned int pg_iter;
2845 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2847 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2854 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2857 unsigned int pg_iter;
2861 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2863 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2870 * From the set of connections possible, choose at least num connections per
2871 * peer based on depth first traversal of peer connections. If DFS leaves
2872 * peers unconnected, ensure those peers get connections.
2874 * @param pg the peergroup we are dealing with
2875 * @param num how many connections at least should each peer have (if possible)?
2878 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2880 struct DFSContext dfs_ctx;
2883 uint32_t starting_peer;
2884 uint32_t least_connections;
2885 GNUNET_HashCode second_hash;
2887 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2889 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2894 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
2896 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
2898 least_connections = -1; /* Set to very high number */
2899 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2901 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
2903 starting_peer = pg_iter;
2904 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2909 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
2915 /* Choose a random peer from the chosen peers set of connections to add */
2916 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
2917 dfs_ctx.first_uid = starting_peer;
2918 dfs_ctx.first = &pg->peers[starting_peer];
2920 dfs_ctx.current = 0;
2922 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
2923 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
2924 hash_from_uid(dfs_ctx.second_uid, &second_hash);
2925 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
2926 starting_peer = dfs_ctx.second_uid;
2929 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2931 /* Remove the "old" connections */
2932 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2933 /* And replace with the working set */
2934 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2939 * Internal callback for topology information for a particular peer.
2942 internal_topology_callback(void *cls,
2943 const struct GNUNET_PeerIdentity *peer,
2944 struct GNUNET_TIME_Relative latency, uint32_t distance)
2946 struct CoreContext *core_ctx = cls;
2947 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
2949 if (peer == NULL) /* Either finished, or something went wrong */
2951 iter_ctx->completed++;
2952 iter_ctx->connected--;
2953 /* One core context allocated per iteration, must free! */
2954 GNUNET_free(core_ctx);
2958 iter_ctx->topology_cb(iter_ctx->cls, &core_ctx->daemon->id, peer, latency, distance, NULL);
2961 if (iter_ctx->completed == iter_ctx->total)
2963 iter_ctx->topology_cb(iter_ctx->cls, NULL, NULL, GNUNET_TIME_relative_get_zero(), 0, NULL);
2964 /* Once all are done, free the iteration context */
2965 GNUNET_free(iter_ctx);
2971 * Check running topology iteration tasks, if below max start a new one, otherwise
2972 * schedule for some time in the future.
2975 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2977 struct CoreContext *core_context = cls;
2978 struct TopologyIterateContext *topology_context = (struct TopologyIterateContext *)core_context->iter_context;
2979 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2982 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
2984 #if VERBOSE_TESTING > 2
2985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2986 _("Delaying connect, we have too many outstanding connections!\n"));
2988 GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_topology, core_context);
2992 #if VERBOSE_TESTING > 2
2993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2994 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
2996 topology_context->connected++;
2997 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->sched, core_context->daemon->cfg, &internal_topology_callback, core_context))
2998 internal_topology_callback(core_context, NULL, GNUNET_TIME_relative_get_zero(), 0);
3004 * Iterate over all (running) peers in the peer group, retrieve
3005 * all connections that each currently has.
3008 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyTopology cb, void *cls)
3010 struct TopologyIterateContext *topology_context;
3011 struct CoreContext *core_ctx;
3013 unsigned int total_count;
3015 /* Allocate a single topology iteration context */
3016 topology_context = GNUNET_malloc(sizeof(struct TopologyIterateContext));
3017 topology_context->topology_cb = cb;
3018 topology_context->cls = cls;
3020 for (i = 0; i < pg->total; i++)
3022 if (pg->peers[i].daemon->running == GNUNET_YES)
3024 /* Allocate one core context per core we need to connect to */
3025 core_ctx = GNUNET_malloc(sizeof(struct CoreContext));
3026 core_ctx->daemon = pg->peers[i].daemon;
3027 /* Set back pointer to topology iteration context */
3028 core_ctx->iter_context = topology_context;
3029 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_topology, core_ctx);
3033 topology_context->total = total_count;
3038 * Callback function to process statistic values.
3039 * This handler is here only really to insert a peer
3040 * identity (or daemon) so the statistics can be uniquely
3041 * tied to a single running peer.
3043 * @param cls closure
3044 * @param subsystem name of subsystem that created the statistic
3045 * @param name the name of the datum
3046 * @param value the current value
3047 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
3048 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
3050 static int internal_stats_callback (void *cls,
3051 const char *subsystem,
3056 struct StatsCoreContext *core_context = cls;
3057 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3059 return stats_context->proc(stats_context->cls, &core_context->daemon->id, subsystem, name, value, is_persistent);
3063 * Internal continuation call for statistics iteration.
3065 * @param cls closure, the CoreContext for this iteration
3066 * @param success whether or not the statistics iterations
3067 * was canceled or not (we don't care)
3069 static void internal_stats_cont (void *cls, int success)
3071 struct StatsCoreContext *core_context = cls;
3072 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3074 stats_context->connected--;
3075 stats_context->completed++;
3077 if (stats_context->completed == stats_context->total)
3079 stats_context->cont(stats_context->cls, GNUNET_YES);
3080 GNUNET_free(stats_context);
3083 if (core_context->stats_handle != NULL)
3084 GNUNET_STATISTICS_destroy(core_context->stats_handle, GNUNET_NO);
3086 GNUNET_free(core_context);
3090 * Check running topology iteration tasks, if below max start a new one, otherwise
3091 * schedule for some time in the future.
3094 schedule_get_statistics(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3096 struct StatsCoreContext *core_context = cls;
3097 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3099 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3102 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3104 #if VERBOSE_TESTING > 2
3105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3106 _("Delaying connect, we have too many outstanding connections!\n"));
3108 GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_statistics, core_context);
3112 #if VERBOSE_TESTING > 2
3113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3114 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3117 stats_context->connected++;
3118 core_context->stats_handle = GNUNET_STATISTICS_create(core_context->daemon->sched, "testing", core_context->daemon->cfg);
3119 if (core_context->stats_handle == NULL)
3121 internal_stats_cont (core_context, GNUNET_NO);
3125 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);
3126 if (core_context->stats_get_handle == NULL)
3127 internal_stats_cont (core_context, GNUNET_NO);
3134 * Iterate over all (running) peers in the peer group, retrieve
3135 * all statistics from each.
3138 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
3139 GNUNET_STATISTICS_Callback cont,
3140 GNUNET_TESTING_STATISTICS_Iterator proc, void *cls)
3142 struct StatsIterateContext *stats_context;
3143 struct StatsCoreContext *core_ctx;
3145 unsigned int total_count;
3147 /* Allocate a single stats iteration context */
3148 stats_context = GNUNET_malloc(sizeof(struct StatsIterateContext));
3149 stats_context->cont = cont;
3150 stats_context->proc = proc;
3151 stats_context->cls = cls;
3153 for (i = 0; i < pg->total; i++)
3155 if (pg->peers[i].daemon->running == GNUNET_YES)
3157 /* Allocate one core context per core we need to connect to */
3158 core_ctx = GNUNET_malloc(sizeof(struct StatsCoreContext));
3159 core_ctx->daemon = pg->peers[i].daemon;
3160 /* Set back pointer to topology iteration context */
3161 core_ctx->iter_context = stats_context;
3162 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_statistics, core_ctx);
3166 stats_context->total = total_count;
3171 * There are many ways to connect peers that are supported by this function.
3172 * To connect peers in the same topology that was created via the
3173 * GNUNET_TESTING_create_topology, the topology variable must be set to
3174 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
3175 * a new instance of that topology will be generated and attempted to be
3176 * connected. This could result in some connections being impossible,
3177 * because some topologies are non-deterministic.
3179 * @param pg the peer group struct representing the running peers
3180 * @param topology which topology to connect the peers in
3181 * @param options options for connecting the topology
3182 * @param option_modifier modifier for options that take a parameter
3183 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
3186 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3187 enum GNUNET_TESTING_Topology topology,
3188 enum GNUNET_TESTING_TopologyOption options,
3189 double option_modifier)
3193 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3194 #if VERBOSE_TOPOLOGY
3195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3196 _("Creating clique CONNECT topology\n"));
3198 create_clique (pg, &add_actual_connections);
3200 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3201 #if VERBOSE_TOPOLOGY
3202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3203 _("Creating small world (ring) CONNECT topology\n"));
3205 create_small_world_ring (pg, &add_actual_connections);
3207 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3208 #if VERBOSE_TOPOLOGY
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3210 _("Creating small world (2d-torus) CONNECT topology\n"));
3212 create_small_world (pg, &add_actual_connections);
3214 case GNUNET_TESTING_TOPOLOGY_RING:
3215 #if VERBOSE_TOPOLOGY
3216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3217 _("Creating ring CONNECT topology\n"));
3219 create_ring (pg, &add_actual_connections);
3221 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3222 #if VERBOSE_TOPOLOGY
3223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3224 _("Creating 2d torus CONNECT topology\n"));
3226 create_2d_torus (pg, &add_actual_connections);
3228 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3229 #if VERBOSE_TOPOLOGY
3230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3231 _("Creating Erdos-Renyi CONNECT topology\n"));
3233 create_erdos_renyi (pg, &add_actual_connections);
3235 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3236 #if VERBOSE_TOPOLOGY
3237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3238 _("Creating InterNAT CONNECT topology\n"));
3240 create_nated_internet (pg, &add_actual_connections);
3242 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3243 #if VERBOSE_TOPOLOGY
3244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3245 _("Creating Scale Free CONNECT topology\n"));
3247 create_scale_free (pg, &add_actual_connections);
3249 case GNUNET_TESTING_TOPOLOGY_LINE:
3250 #if VERBOSE_TOPOLOGY
3251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3252 _("Creating straight line CONNECT topology\n"));
3254 create_line (pg, &add_actual_connections);
3256 case GNUNET_TESTING_TOPOLOGY_NONE:
3257 #if VERBOSE_TOPOLOGY
3258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3259 _("Creating no CONNECT topology\n"));
3261 copy_allowed_topology(pg);
3264 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3265 _("Unknown topology specification, can't connect peers!\n"));
3266 return GNUNET_SYSERR;
3271 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
3272 #if VERBOSE_TOPOLOGY
3273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3274 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
3276 choose_random_connections(pg, option_modifier);
3278 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
3279 #if VERBOSE_TOPOLOGY
3280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3281 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3283 choose_minimum(pg, (unsigned int)option_modifier);
3285 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
3286 #if VERBOSE_TOPOLOGY
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3288 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3290 perform_dfs(pg, (int)option_modifier);
3292 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
3294 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
3300 return connect_topology(pg);
3304 * Callback that is called whenever a hostkey is generated
3305 * for a peer. Call the real callback and decrement the
3306 * starting counter for the peergroup.
3308 * @param cls closure
3309 * @param id identifier for the daemon, NULL on error
3310 * @param d handle for the daemon
3311 * @param emsg error message (NULL on success)
3313 static void internal_hostkey_callback (void *cls,
3314 const struct GNUNET_PeerIdentity *id,
3315 struct GNUNET_TESTING_Daemon *d,
3318 struct InternalStartContext *internal_context = cls;
3319 internal_context->peer->pg->starting--;
3320 internal_context->peer->pg->started++;
3321 if (internal_context->hostkey_callback != NULL)
3322 internal_context->hostkey_callback(internal_context->hostkey_cls, id, d, emsg);
3323 else if (internal_context->peer->pg->started == internal_context->peer->pg->total)
3325 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
3326 GNUNET_TESTING_daemons_continue_startup(internal_context->peer->pg);
3331 * Callback that is called whenever a peer has finished starting.
3332 * Call the real callback and decrement the starting counter
3333 * for the peergroup.
3335 * @param cls closure
3336 * @param id identifier for the daemon, NULL on error
3337 * @param d handle for the daemon
3338 * @param emsg error message (NULL on success)
3340 static void internal_startup_callback (void *cls,
3341 const struct GNUNET_PeerIdentity *id,
3342 const struct GNUNET_CONFIGURATION_Handle *cfg,
3343 struct GNUNET_TESTING_Daemon *d,
3346 struct InternalStartContext *internal_context = cls;
3347 internal_context->peer->pg->starting--;
3348 if (internal_context->start_cb != NULL)
3349 internal_context->start_cb(internal_context->start_cb_cls, id, cfg, d, emsg);
3353 internal_continue_startup (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3355 struct InternalStartContext *internal_context = cls;
3357 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3362 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
3364 internal_context->peer->pg->starting++;
3365 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
3369 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_continue_startup, internal_context);
3375 * Callback for informing us about a successful
3376 * or unsuccessful churn start call.
3378 * @param cls a ChurnContext
3379 * @param id the peer identity of the started peer
3380 * @param cfg the handle to the configuration of the peer
3381 * @param d handle to the daemon for the peer
3382 * @param emsg NULL on success, non-NULL on failure
3386 churn_start_callback (void *cls,
3387 const struct GNUNET_PeerIdentity *id,
3388 const struct GNUNET_CONFIGURATION_Handle *cfg,
3389 struct GNUNET_TESTING_Daemon *d,
3392 struct ChurnRestartContext *startup_ctx = cls;
3393 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
3395 unsigned int total_left;
3396 char *error_message;
3398 error_message = NULL;
3401 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3402 "Churn stop callback failed with error `%s'\n",
3404 churn_ctx->num_failed_start++;
3408 churn_ctx->num_to_start--;
3412 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3413 "Started peer, %d left.\n",
3414 churn_ctx->num_to_start);
3417 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3419 if (total_left == 0)
3421 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3422 GNUNET_asprintf(&error_message,
3423 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3424 churn_ctx->num_failed_start,
3425 churn_ctx->num_failed_stop);
3426 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3427 GNUNET_free_non_null(error_message);
3428 GNUNET_free(churn_ctx);
3429 GNUNET_free(startup_ctx);
3434 static void schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3436 struct PeerRestartContext *peer_restart_ctx = cls;
3437 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
3439 if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
3440 GNUNET_SCHEDULER_add_delayed(peer_restart_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_churn_restart, peer_restart_ctx);
3443 GNUNET_TESTING_daemon_start_stopped(peer_restart_ctx->daemon,
3444 startup_ctx->timeout,
3445 &churn_start_callback,
3447 GNUNET_free(peer_restart_ctx);
3452 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3454 struct InternalStartContext *internal_context = cls;
3456 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3461 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
3463 internal_context->peer->pg->starting++;
3464 internal_context->peer->daemon = GNUNET_TESTING_daemon_start (internal_context->peer->pg->sched,
3465 internal_context->peer->cfg,
3466 internal_context->timeout,
3467 internal_context->hostname,
3468 internal_context->username,
3469 internal_context->sshport,
3470 &internal_hostkey_callback,
3472 &internal_startup_callback,
3477 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_start, internal_context);
3482 * Function which continues a peer group starting up
3483 * after successfully generating hostkeys for each peer.
3485 * @param pg the peer group to continue starting
3489 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
3494 for (i = 0; i < pg->total; i++)
3496 GNUNET_SCHEDULER_add_now (pg->sched, &internal_continue_startup, &pg->peers[i].internal_context);
3497 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
3502 * Start count gnunet instances with the same set of transports and
3503 * applications. The port numbers (any option called "PORT") will be
3504 * adjusted to ensure that no two peers running on the same system
3505 * have the same port(s) in their respective configurations.
3507 * @param sched scheduler to use
3508 * @param cfg configuration template to use
3509 * @param total number of daemons to start
3510 * @param timeout total time allowed for peers to start
3511 * @param hostkey_callback function to call on each peers hostkey generation
3512 * if NULL, peers will be started by this call, if non-null,
3513 * GNUNET_TESTING_daemons_continue_startup must be called after
3514 * successful hostkey generation
3515 * @param hostkey_cls closure for hostkey callback
3516 * @param cb function to call on each daemon that was started
3517 * @param cb_cls closure for cb
3518 * @param connect_callback function to call each time two hosts are connected
3519 * @param connect_callback_cls closure for connect_callback
3520 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
3522 * @return NULL on error, otherwise handle to control peer group
3524 struct GNUNET_TESTING_PeerGroup *
3525 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
3526 const struct GNUNET_CONFIGURATION_Handle *cfg,
3528 struct GNUNET_TIME_Relative timeout,
3529 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
3531 GNUNET_TESTING_NotifyDaemonRunning cb,
3533 GNUNET_TESTING_NotifyConnection
3534 connect_callback, void *connect_callback_cls,
3535 const struct GNUNET_TESTING_Host *hostnames)
3537 struct GNUNET_TESTING_PeerGroup *pg;
3538 const struct GNUNET_TESTING_Host *hostpos;
3544 const char *hostname;
3545 const char *username;
3546 char *baseservicehome;
3547 char *newservicehome;
3549 struct GNUNET_CONFIGURATION_Handle *pcfg;
3551 unsigned int hostcnt;
3564 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
3567 pg->notify_connection = connect_callback;
3568 pg->notify_connection_cls = connect_callback_cls;
3570 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
3571 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
3572 if (NULL != hostnames)
3575 hostpos = hostnames;
3576 while (hostpos != NULL)
3578 hostpos = hostpos->next;
3581 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3584 hostpos = hostnames;
3585 while (hostpos != NULL)
3587 pg->hosts[off].minport = LOW_PORT;
3589 pg->hosts[off].hostname = GNUNET_strdup(hostpos->hostname);
3590 if (hostpos->username != NULL)
3591 pg->hosts[off].username = GNUNET_strdup(hostpos->username);
3592 pg->hosts[off].sshport = hostpos->port;
3593 hostpos = hostpos->next;
3598 GNUNET_free (pg->hosts);
3606 /* skip leading spaces */
3607 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
3610 while ('\0' != *rpos)
3612 if (isspace ( (unsigned char) *rpos))
3616 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3618 start = GNUNET_strdup (hostnames);
3620 while ('\0' != *pos)
3622 if (isspace ( (unsigned char) *pos))
3625 if (strlen (start) > 0)
3627 pg->hosts[off].minport = LOW_PORT;
3628 pg->hosts[off++].hostname = start;
3634 if (strlen (start) > 0)
3636 pg->hosts[off].minport = LOW_PORT;
3637 pg->hosts[off++].hostname = start;
3641 GNUNET_free (start);
3642 GNUNET_free (pg->hosts);
3646 minport = 0; /* make gcc happy */
3654 for (off = 0; off < total; off++)
3658 hostname = pg->hosts[off % hostcnt].hostname;
3659 username = pg->hosts[off % hostcnt].username;
3660 sshport = pg->hosts[off % hostcnt].sshport;
3661 pcfg = make_config (cfg,
3662 &pg->hosts[off % hostcnt].minport,
3671 pcfg = make_config (cfg,
3679 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3681 ("Could not create configuration for peer number %u on `%s'!\n"),
3682 off, hostname == NULL ? "localhost" : hostname);
3687 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
3690 GNUNET_asprintf (&newservicehome,
3691 "%s/%d/", baseservicehome, off);
3692 GNUNET_free (baseservicehome);
3696 tmpdir = getenv ("TMPDIR");
3697 tmpdir = tmpdir ? tmpdir : "/tmp";
3698 GNUNET_asprintf (&newservicehome,
3701 "gnunet-testing-test-test", off);
3703 GNUNET_CONFIGURATION_set_value_string (pcfg,
3705 "SERVICEHOME", newservicehome);
3706 GNUNET_free (newservicehome);
3707 pg->peers[off].cfg = pcfg;
3708 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
3709 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
3710 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
3711 pg->peers[off].pg = pg;
3713 pg->peers[off].internal_context.peer = &pg->peers[off];
3714 pg->peers[off].internal_context.timeout = timeout;
3715 pg->peers[off].internal_context.hostname = hostname;
3716 pg->peers[off].internal_context.username = username;
3717 pg->peers[off].internal_context.sshport = sshport;
3718 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
3719 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
3720 pg->peers[off].internal_context.start_cb = cb;
3721 pg->peers[off].internal_context.start_cb_cls = cb_cls;
3723 GNUNET_SCHEDULER_add_now (sched, &internal_start, &pg->peers[off].internal_context);
3730 * Get a daemon by number, so callers don't have to do nasty
3731 * offsetting operation.
3733 struct GNUNET_TESTING_Daemon *
3734 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
3736 if (position < pg->total)
3737 return pg->peers[position].daemon;
3743 * Get a daemon by peer identity, so callers can
3744 * retrieve the daemon without knowing it's offset.
3746 * @param pg the peer group to retrieve the daemon from
3747 * @param peer_id the peer identity of the daemon to retrieve
3749 * @return the daemon on success, or NULL if no such peer identity is found
3751 struct GNUNET_TESTING_Daemon *
3752 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_PeerIdentity *peer_id)
3756 for (i = 0; i < pg->total; i ++)
3758 if (0 == memcmp(&pg->peers[i].daemon->id, peer_id, sizeof(struct GNUNET_PeerIdentity)))
3759 return pg->peers[i].daemon;
3766 * Prototype of a function that will be called when a
3767 * particular operation was completed the testing library.
3769 * @param cls closure (a struct RestartContext)
3770 * @param id id of the peer that was restarted
3771 * @param cfg handle to the configuration of the peer
3772 * @param d handle to the daemon that was restarted
3773 * @param emsg NULL on success
3775 void restart_callback (void *cls,
3776 const struct GNUNET_PeerIdentity *id,
3777 const struct GNUNET_CONFIGURATION_Handle *cfg,
3778 struct GNUNET_TESTING_Daemon *d,
3781 struct RestartContext *restart_context = cls;
3785 restart_context->peers_restarted++;
3789 restart_context->peers_restart_failed++;
3792 if (restart_context->peers_restarted == restart_context->peer_group->total)
3794 restart_context->callback(restart_context->callback_cls, NULL);
3795 GNUNET_free(restart_context);
3797 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
3799 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
3800 GNUNET_free(restart_context);
3806 * Callback for informing us about a successful
3807 * or unsuccessful churn stop call.
3809 * @param cls a ChurnContext
3810 * @param emsg NULL on success, non-NULL on failure
3814 churn_stop_callback (void *cls, const char *emsg)
3816 struct ShutdownContext *shutdown_ctx = cls;
3817 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
3818 unsigned int total_left;
3819 char *error_message;
3821 error_message = NULL;
3822 shutdown_ctx->outstanding--;
3826 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3827 "Churn stop callback failed with error `%s'\n", emsg);
3828 churn_ctx->num_failed_stop++;
3832 churn_ctx->num_to_stop--;
3836 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3837 "Stopped peer, %d left.\n",
3838 churn_ctx->num_to_stop);
3840 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3842 if (total_left == 0)
3844 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3846 GNUNET_asprintf(&error_message,
3847 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3848 churn_ctx->num_failed_start,
3849 churn_ctx->num_failed_stop);
3851 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3852 GNUNET_free_non_null(error_message);
3853 GNUNET_free(churn_ctx);
3854 GNUNET_free(shutdown_ctx);
3859 * Count the number of running peers.
3861 * @param pg handle for the peer group
3863 * @return the number of currently running peers in the peer group
3866 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
3869 unsigned int running = 0;
3870 for (i = 0; i < pg->total; i++)
3872 if (pg->peers[i].daemon->running == GNUNET_YES)
3874 GNUNET_assert(running != -1);
3882 * Task to rate limit the number of outstanding peer shutdown
3883 * requests. This is necessary for making sure we don't do
3884 * too many ssh connections at once, but is generally nicer
3885 * to any system as well (graduated task starts, as opposed
3886 * to calling gnunet-arm N times all at once).
3889 schedule_churn_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3891 struct PeerShutdownContext *peer_shutdown_ctx = cls;
3892 struct ShutdownContext *shutdown_ctx;
3894 GNUNET_assert(peer_shutdown_ctx != NULL);
3895 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
3896 GNUNET_assert(shutdown_ctx != NULL);
3898 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
3899 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);
3902 shutdown_ctx->outstanding++;
3903 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, shutdown_ctx->cb, shutdown_ctx, GNUNET_NO, GNUNET_YES);
3904 GNUNET_free(peer_shutdown_ctx);
3909 * Simulate churn by stopping some peers (and possibly
3910 * re-starting others if churn is called multiple times). This
3911 * function can only be used to create leave-join churn (peers "never"
3912 * leave for good). First "voff" random peers that are currently
3913 * online will be taken offline; then "von" random peers that are then
3914 * offline will be put back online. No notifications will be
3915 * generated for any of these operations except for the callback upon
3918 * @param pg handle for the peer group
3919 * @param voff number of peers that should go offline
3920 * @param von number of peers that should come back online;
3921 * must be zero on first call (since "testbed_start"
3922 * always starts all of the peers)
3923 * @param timeout how long to wait for operations to finish before
3925 * @param cb function to call at the end
3926 * @param cb_cls closure for cb
3929 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
3932 struct GNUNET_TIME_Relative timeout,
3933 GNUNET_TESTING_NotifyCompletion cb,
3936 struct ChurnContext *churn_ctx;
3937 struct ShutdownContext *shutdown_ctx;
3938 struct PeerShutdownContext *peer_shutdown_ctx;
3939 struct PeerRestartContext *peer_restart_ctx;
3940 struct ChurnRestartContext *churn_startup_ctx;
3942 unsigned int running;
3943 unsigned int stopped;
3944 unsigned int total_running;
3945 unsigned int total_stopped;
3947 unsigned int *running_arr;
3948 unsigned int *stopped_arr;
3949 unsigned int *running_permute;
3950 unsigned int *stopped_permute;
3955 if ((von == 0) && (voff == 0)) /* No peers at all? */
3961 for (i = 0; i < pg->total; i++)
3963 if (pg->peers[i].daemon->running == GNUNET_YES)
3965 GNUNET_assert(running != -1);
3970 GNUNET_assert(stopped != -1);
3977 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
3978 cb(cb_cls, "Trying to stop more peers than are currently running!");
3984 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
3985 cb(cb_cls, "Trying to start more peers than are currently stopped!");
3989 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3993 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
3997 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
3999 running_permute = NULL;
4000 stopped_permute = NULL;
4003 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
4005 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
4007 total_running = running;
4008 total_stopped = stopped;
4012 churn_ctx->num_to_start = von;
4013 churn_ctx->num_to_stop = voff;
4015 churn_ctx->cb_cls = cb_cls;
4017 for (i = 0; i < pg->total; i++)
4019 if (pg->peers[i].daemon->running == GNUNET_YES)
4021 GNUNET_assert((running_arr != NULL) && (total_running > running));
4022 running_arr[running] = i;
4027 GNUNET_assert((stopped_arr != NULL) && (total_stopped > stopped));
4028 stopped_arr[stopped] = i;
4033 GNUNET_assert(running >= voff);
4036 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4037 shutdown_ctx->cb = &churn_stop_callback;
4038 shutdown_ctx->cb_cls = churn_ctx;
4039 shutdown_ctx->total_peers = voff;
4040 shutdown_ctx->timeout = timeout;
4043 for (i = 0; i < voff; i++)
4046 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
4048 GNUNET_assert(running_arr != NULL);
4049 peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4050 peer_shutdown_ctx->daemon = pg->peers[running_arr[running_permute[i]]].daemon;
4051 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4052 GNUNET_SCHEDULER_add_now(peer_shutdown_ctx->daemon->sched, &schedule_churn_shutdown_task, peer_shutdown_ctx);
4055 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
4057 &churn_stop_callback, churn_ctx,
4058 GNUNET_NO, GNUNET_YES); */
4061 GNUNET_assert(stopped >= von);
4064 churn_startup_ctx = GNUNET_malloc(sizeof(struct ChurnRestartContext));
4065 churn_startup_ctx->churn_ctx = churn_ctx;
4066 churn_startup_ctx->timeout = timeout;
4068 for (i = 0; i < von; i++)
4071 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
4073 GNUNET_assert(stopped_arr != NULL);
4074 peer_restart_ctx = GNUNET_malloc(sizeof(struct PeerRestartContext));
4075 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
4076 peer_restart_ctx->daemon = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
4077 GNUNET_SCHEDULER_add_now(peer_restart_ctx->daemon->sched, &schedule_churn_restart, peer_restart_ctx);
4079 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
4080 timeout, &churn_start_callback, churn_ctx);*/
4083 GNUNET_free_non_null(running_arr);
4084 GNUNET_free_non_null(stopped_arr);
4085 GNUNET_free_non_null(running_permute);
4086 GNUNET_free_non_null(stopped_permute);
4091 * Restart all peers in the given group.
4093 * @param pg the handle to the peer group
4094 * @param callback function to call on completion (or failure)
4095 * @param callback_cls closure for the callback function
4098 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
4099 GNUNET_TESTING_NotifyCompletion callback,
4102 struct RestartContext *restart_context;
4107 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
4108 restart_context->peer_group = pg;
4109 restart_context->peers_restarted = 0;
4110 restart_context->callback = callback;
4111 restart_context->callback_cls = callback_cls;
4113 for (off = 0; off < pg->total; off++)
4115 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
4121 * Start or stop an individual peer from the given group.
4123 * @param pg handle to the peer group
4124 * @param offset which peer to start or stop
4125 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
4126 * @param timeout how long to wait for shutdown
4127 * @param cb function to call at the end
4128 * @param cb_cls closure for cb
4131 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
4132 unsigned int offset,
4134 struct GNUNET_TIME_Relative timeout,
4135 GNUNET_TESTING_NotifyCompletion cb,
4138 struct ChurnContext *churn_ctx;
4140 if (GNUNET_NO == desired_status)
4142 if (NULL != pg->peers[offset].daemon)
4144 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4145 churn_ctx->num_to_start = 0;
4146 churn_ctx->num_to_stop = 1;
4148 churn_ctx->cb_cls = cb_cls;
4149 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
4150 timeout, &churn_stop_callback, churn_ctx,
4151 GNUNET_NO, GNUNET_YES);
4154 else if (GNUNET_YES == desired_status)
4156 if (NULL == pg->peers[offset].daemon)
4158 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4159 churn_ctx->num_to_start = 1;
4160 churn_ctx->num_to_stop = 0;
4162 churn_ctx->cb_cls = cb_cls;
4163 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
4164 timeout, &churn_start_callback, churn_ctx);
4173 * Callback for shutting down peers in a peer group.
4175 * @param cls closure (struct ShutdownContext)
4176 * @param emsg NULL on success
4178 void internal_shutdown_callback (void *cls,
4181 struct ShutdownContext *shutdown_ctx = cls;
4183 shutdown_ctx->outstanding--;
4186 shutdown_ctx->peers_down++;
4190 shutdown_ctx->peers_failed++;
4193 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
4195 if (shutdown_ctx->peers_failed > 0)
4196 shutdown_ctx->cb(shutdown_ctx->cb_cls, "Not all peers successfully shut down!");
4198 shutdown_ctx->cb(shutdown_ctx->cb_cls, NULL);
4199 GNUNET_free(shutdown_ctx);
4205 * Task to rate limit the number of outstanding peer shutdown
4206 * requests. This is necessary for making sure we don't do
4207 * too many ssh connections at once, but is generally nicer
4208 * to any system as well (graduated task starts, as opposed
4209 * to calling gnunet-arm N times all at once).
4212 schedule_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
4214 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4215 struct ShutdownContext *shutdown_ctx;
4217 GNUNET_assert(peer_shutdown_ctx != NULL);
4218 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4219 GNUNET_assert(shutdown_ctx != NULL);
4221 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4222 GNUNET_SCHEDULER_add_delayed(peer_shutdown_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_shutdown_task, peer_shutdown_ctx);
4225 shutdown_ctx->outstanding++;
4226 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, &internal_shutdown_callback, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4227 GNUNET_free(peer_shutdown_ctx);
4232 * Shutdown all peers started in the given group.
4234 * @param pg handle to the peer group
4235 * @param timeout how long to wait for shutdown
4236 * @param cb callback to notify upon success or failure
4237 * @param cb_cls closure for cb
4240 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
4241 struct GNUNET_TIME_Relative timeout,
4242 GNUNET_TESTING_NotifyCompletion cb,
4246 struct ShutdownContext *shutdown_ctx;
4247 struct PeerShutdownContext *peer_shutdown_ctx;
4249 GNUNET_assert(pg->total > 0);
4251 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4252 shutdown_ctx->cb = cb;
4253 shutdown_ctx->cb_cls = cb_cls;
4254 shutdown_ctx->total_peers = pg->total;
4255 shutdown_ctx->timeout = timeout;
4256 /* shtudown_ctx->outstanding = 0; */
4258 for (off = 0; off < pg->total; off++)
4260 GNUNET_assert(NULL != pg->peers[off].daemon);
4261 peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4262 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
4263 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4264 GNUNET_SCHEDULER_add_now(pg->peers[off].daemon->sched, &schedule_shutdown_task, peer_shutdown_ctx);
4265 //GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4266 if (NULL != pg->peers[off].cfg)
4267 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
4268 if (pg->peers[off].allowed_peers != NULL)
4269 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
4270 if (pg->peers[off].connect_peers != NULL)
4271 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
4272 if (pg->peers[off].blacklisted_peers != NULL)
4273 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
4275 GNUNET_free (pg->peers);
4276 if (NULL != pg->hosts)
4278 GNUNET_free (pg->hosts[0].hostname);
4279 GNUNET_free (pg->hosts);
4285 /* end of testing_group.c */