2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing_group.c
23 * @brief convenience API for writing testcases for GNUnet
24 * @author Nathan Evans
25 * @author Christian Grothoff
29 #include "gnunet_arm_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_core_service.h"
33 #define VERBOSE_TESTING GNUNET_NO
35 #define VERBOSE_TOPOLOGY GNUNET_YES
37 #define DEBUG_CHURN GNUNET_NO
40 * Lowest port used for GNUnet testing. Should be high enough to not
41 * conflict with other applications running on the hosts but be low
42 * enough to not conflict with client-ports (typically starting around
45 #define LOW_PORT 10000
48 * Highest port used for GNUnet testing. Should be low enough to not
49 * conflict with the port range for "local" ports (client apps; see
50 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
52 #define HIGH_PORT 56000
54 #define MAX_OUTSTANDING_CONNECTIONS 40
56 #define MAX_CONCURRENT_HOSTKEYS 10
58 #define MAX_CONCURRENT_STARTING 10
60 #define MAX_CONCURRENT_SHUTDOWN 10
62 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
64 #define CONNECT_ATTEMPTS 8
67 * Prototype of a function called whenever two peers would be connected
68 * in a certain topology.
70 typedef int (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup *pg,
76 * Context for handling churning a peer group
81 * Callback used to notify of churning finished
83 GNUNET_TESTING_NotifyCompletion cb;
86 * Closure for callback
91 * Number of peers that still need to be started
93 unsigned int num_to_start;
96 * Number of peers that still need to be stopped
98 unsigned int num_to_stop;
101 * Number of peers that failed to start
103 unsigned int num_failed_start;
106 * Number of peers that failed to stop
108 unsigned int num_failed_stop;
111 struct RestartContext
114 * The group of peers being restarted
116 struct GNUNET_TESTING_PeerGroup *peer_group;
119 * How many peers have been restarted thus far
121 unsigned int peers_restarted;
124 * How many peers got an error when restarting
126 unsigned int peers_restart_failed;
129 * The function to call once all peers have been restarted
131 GNUNET_TESTING_NotifyCompletion callback;
134 * Closure for callback function
141 struct ShutdownContext
144 * Total peers to wait for
146 unsigned int total_peers;
149 * Number of peers successfully shut down
151 unsigned int peers_down;
154 * Number of peers failed to shut down
156 unsigned int peers_failed;
159 * Number of peers we have started shutting
160 * down. If too many, wait on them.
162 unsigned int outstanding;
165 * Timeout for shutdown.
167 struct GNUNET_TIME_Relative timeout;
170 * Callback to call when all peers either
171 * shutdown or failed to shutdown
173 GNUNET_TESTING_NotifyCompletion cb;
182 * Individual shutdown context for a particular peer.
184 struct PeerShutdownContext
187 * Pointer to the high level shutdown context.
189 struct ShutdownContext *shutdown_ctx;
192 * The daemon handle for the peer to shut down.
194 struct GNUNET_TESTING_Daemon *daemon;
198 * Individual shutdown context for a particular peer.
200 struct PeerRestartContext
203 * Pointer to the high level restart context.
205 struct ChurnRestartContext *churn_restart_ctx;
208 * The daemon handle for the peer to shut down.
210 struct GNUNET_TESTING_Daemon *daemon;
214 struct CreateTopologyContext
218 * Function to call with number of connections
220 GNUNET_TESTING_NotifyConnections cont;
223 * Closure for connection notification
229 struct PeerConnection
234 struct PeerConnection *next;
237 * Pointer to daemon handle
239 struct GNUNET_TESTING_Daemon *daemon;
244 struct InternalStartContext
247 * Pointer to peerdata
249 struct PeerData *peer;
252 * Timeout for peer startup
254 struct GNUNET_TIME_Relative timeout;
257 * Client callback for hostkey notification
259 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
262 * Closure for hostkey_callback
267 * Client callback for peer start notification
269 GNUNET_TESTING_NotifyDaemonRunning start_cb;
277 * Hostname, where to start the peer
279 const char *hostname;
282 * Username to use when connecting to the
285 const char *username;
288 * Port to use for ssh.
294 struct ChurnRestartContext
297 * Number of restarts currently in flight.
299 unsigned int outstanding;
302 * Handle to the underlying churn context.
304 struct ChurnContext *churn_ctx;
307 * How long to allow the operation to take.
309 struct GNUNET_TIME_Relative timeout;
313 * Data we keep per peer.
318 * (Initial) configuration of the host.
319 * (initial because clients could change
320 * it and we would not know about those
323 struct GNUNET_CONFIGURATION_Handle *cfg;
326 * Handle for controlling the daemon.
328 struct GNUNET_TESTING_Daemon *daemon;
331 * The peergroup this peer belongs to.
333 struct GNUNET_TESTING_PeerGroup *pg;
336 * Hash map of allowed peer connections (F2F created topology)
338 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
341 * Hash map of blacklisted peers
343 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
346 * Hash map of peer connections
348 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
351 * Temporary hash map of peer connections
353 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
356 * Temporary variable for topology creation, should be reset before
357 * creating any topology so the count is valid once finished.
362 * Context to keep track of peers being started, to
363 * stagger hostkey generation and peer startup.
365 struct InternalStartContext internal_context;
370 * Linked list of per-host data.
380 * SSH username to use when connecting to this host.
385 * SSH port to use when connecting to this host.
390 * Lowest port that we have not yet used
396 struct TopologyIterateContext
399 * Callback for notifying of two connected peers.
401 GNUNET_TESTING_NotifyTopology topology_cb;
404 * Closure for topology_cb
409 * Number of peers currently connected to.
411 unsigned int connected;
414 * Number of peers we have finished iterating.
416 unsigned int completed;
419 * Number of peers total.
424 struct StatsIterateContext
427 * Continuation to call once all stats information has been retrieved.
429 GNUNET_STATISTICS_Callback cont;
432 * Proc function to call on each value received.
434 GNUNET_TESTING_STATISTICS_Iterator proc;
437 * Closure for topology_cb
442 * Number of peers currently connected to.
444 unsigned int connected;
447 * Number of peers we have finished iterating.
449 unsigned int completed;
452 * Number of peers total.
460 struct GNUNET_TESTING_Daemon *daemon;
463 struct StatsCoreContext
466 struct GNUNET_TESTING_Daemon *daemon;
468 * Handle to the statistics service.
470 struct GNUNET_STATISTICS_Handle *stats_handle;
473 * Handle for getting statistics.
475 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
479 * Handle to a group of GNUnet peers.
481 struct GNUNET_TESTING_PeerGroup
486 struct GNUNET_SCHEDULER_Handle *sched;
489 * Configuration template.
491 const struct GNUNET_CONFIGURATION_Handle *cfg;
494 * Function to call on each started daemon.
496 //GNUNET_TESTING_NotifyDaemonRunning cb;
504 * Function to call on each topology connection created
506 GNUNET_TESTING_NotifyConnection notify_connection;
509 * Callback for notify_connection
511 void *notify_connection_cls;
514 * Array of information about hosts.
516 struct HostData *hosts;
519 * Number of hosts (size of HostData)
521 unsigned int num_hosts;
524 * Array of "total" peers.
526 struct PeerData *peers;
529 * Number of peers in this group.
534 * At what time should we fail the peer startup process?
536 struct GNUNET_TIME_Absolute max_timeout;
539 * How many peers are being started right now?
541 unsigned int starting;
544 * How many peers have already been started?
546 unsigned int started;
551 struct GNUNET_CONFIGURATION_Handle *ret;
552 const struct GNUNET_CONFIGURATION_Handle *orig;
553 const char *hostname;
560 struct ConnectContext
562 struct GNUNET_TESTING_Daemon *first;
564 struct GNUNET_TESTING_Daemon *second;
566 struct GNUNET_TESTING_PeerGroup *pg;
570 * Convert unique ID to hash code.
572 * @param uid unique ID to convert
573 * @param hash set to uid (extended with zeros)
576 hash_from_uid (uint32_t uid,
577 GNUNET_HashCode *hash)
579 memset (hash, 0, sizeof(GNUNET_HashCode));
580 *((uint32_t*)hash) = uid;
584 * Convert hash code to unique ID.
586 * @param uid unique ID to convert
587 * @param hash set to uid (extended with zeros)
590 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
592 memcpy (uid, hash, sizeof(uint32_t));
596 * Number of connects we are waiting on, allows us to rate limit
599 static int outstanding_connects;
602 * Get a topology from a string input.
604 * @param topology where to write the retrieved topology
605 * @param topology_string The string to attempt to
606 * get a configuration value from
607 * @return GNUNET_YES if topology string matched a
608 * known topology, GNUNET_NO if not
611 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string)
614 * Strings representing topologies in enum
616 static const char * topology_strings[] =
619 * A clique (everyone connected to everyone else).
624 * Small-world network (2d torus plus random links).
629 * Small-world network (ring plus random links).
649 * Certain percentage of peers are unable to communicate directly
650 * replicating NAT conditions
655 * Scale free topology.
660 * Straight line topology.
665 * All peers are disconnected.
673 if (topology_string == NULL)
675 while (topology_strings[curr] != NULL)
677 if (strcasecmp(topology_strings[curr], topology_string) == 0)
684 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
690 * Get connect topology option from string input.
692 * @param topology_option where to write the retrieved topology
693 * @param topology_string The string to attempt to
694 * get a configuration value from
695 * @return GNUNET_YES if string matched a known
696 * topology option, GNUNET_NO if not
699 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption *topology_option,
700 char * topology_string)
703 * Options for connecting a topology as strings.
705 static const char * topology_option_strings[] =
708 * Try to connect all peers specified in the topology.
713 * Choose a random subset of connections to create.
715 "CONNECT_RANDOM_SUBSET",
718 * Create at least X connections for each peer.
723 * Using a depth first search, create one connection
724 * per peer. If any are missed (graph disconnected)
725 * start over at those peers until all have at least one
731 * Find the N closest peers to each allowed peer in the
732 * topology and make sure a connection to those peers
733 * exists in the connect topology.
738 * No options specified.
746 if (topology_string == NULL)
748 while (NULL != topology_option_strings[curr])
750 if (strcasecmp(topology_option_strings[curr], topology_string) == 0)
752 *topology_option = curr;
757 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
762 * Function to iterate over options. Copies
763 * the options to the target configuration,
764 * updating PORT values as needed.
767 * @param section name of the section
768 * @param option name of the option
769 * @param value value of the option
772 update_config (void *cls,
773 const char *section, const char *option, const char *value)
775 struct UpdateContext *ctx = cls;
779 char *single_variable;
780 char *per_host_variable;
781 unsigned long long num_per_host;
783 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
785 GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
786 if ((ival != 0) && (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable)))
788 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
792 GNUNET_free(single_variable);
795 if (0 == strcmp (option, "UNIXPATH"))
797 GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
798 GNUNET_asprintf(&per_host_variable, "num_%s_per_host", section);
799 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable))
801 GNUNET_snprintf (uval,
803 "/tmp/test-service-%s-%u",
808 else if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_number(ctx->orig, "testing", per_host_variable, &num_per_host))
810 GNUNET_snprintf (uval,
812 "/tmp/test-service-%s-%u",
814 ctx->fdnum % num_per_host);
817 GNUNET_free(single_variable);
818 GNUNET_free(per_host_variable);
822 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
824 value = ctx->hostname;
827 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
832 * Create a new configuration using the given configuration
833 * as a template; however, each PORT in the existing cfg
834 * must be renumbered by incrementing "*port". If we run
835 * out of "*port" numbers, return NULL.
837 * @param cfg template configuration
838 * @param port port numbers to use, update to reflect
839 * port numbers that were used
840 * @param upnum number to make unix domain socket names unique
841 * @param hostname hostname of the controlling host, to allow control connections from
842 * @param fdnum number used to offset the unix domain socket for grouped processes
843 * (such as statistics or peerinfo, which can be shared among others)
845 * @return new configuration, NULL on error
847 static struct GNUNET_CONFIGURATION_Handle *
848 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
851 const char *hostname, uint32_t * fdnum)
853 struct UpdateContext uc;
862 uc.ret = GNUNET_CONFIGURATION_create ();
863 uc.hostname = hostname;
866 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
867 if (uc.nport >= HIGH_PORT)
870 GNUNET_CONFIGURATION_destroy (uc.ret);
874 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
876 if (hostname != NULL)
877 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1; %s;", control_host, hostname);
879 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
881 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
882 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport", "ACCEPT_FROM", allowed_hosts);
883 GNUNET_CONFIGURATION_set_value_string(uc.ret, "dht", "ACCEPT_FROM", allowed_hosts);
884 GNUNET_CONFIGURATION_set_value_string(uc.ret, "statistics", "ACCEPT_FROM", allowed_hosts);
885 GNUNET_free_non_null(control_host);
886 GNUNET_free(allowed_hosts);
890 /* arm needs to know to allow connections from the host on which it is running,
891 * otherwise gnunet-arm is unable to connect to it in some instances */
892 if (hostname != NULL)
894 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
895 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", hostname);
896 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", hostname);
897 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
898 GNUNET_free(allowed_hosts);
902 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", "127.0.0.1");
903 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", "127.0.0.1");
906 *port = (uint16_t) uc.nport;
915 * Add entries to the peers connect list
917 * @param pg the peer group we are working with
918 * @param first index of the first peer
919 * @param second index of the second peer
921 * @return the number of connections added
922 * technically should only be 0 or 2
926 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
932 GNUNET_HashCode hash_first;
933 GNUNET_HashCode hash_second;
935 hash_from_uid(first, &hash_first);
936 hash_from_uid(second, &hash_second);
938 add_first = GNUNET_NO;
939 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
941 add_first = GNUNET_YES;
944 add_second = GNUNET_NO;
945 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
947 add_second = GNUNET_YES;
953 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
954 pg->peers[first].num_connections++;
960 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
961 pg->peers[second].num_connections++;
970 * Add entries to the peers allowed connections list
972 * @param pg the peer group we are working with
973 * @param first index of the first peer
974 * @param second index of the second peer
976 * @return the number of connections added (can be 0, 1 or 2)
977 * technically should only be 0 or 2, but the small price
978 * of iterating over the lists (hashmaps in the future)
979 * for being sure doesn't bother me!
983 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
987 struct PeerConnection *first_iter;
988 struct PeerConnection *second_iter;
989 struct PeerConnection *new_first;
990 struct PeerConnection *new_second;
995 GNUNET_HashCode hash_first;
996 GNUNET_HashCode hash_second;
998 hash_from_uid(first, &hash_first);
999 hash_from_uid(second, &hash_second);
1001 add_first = GNUNET_NO;
1002 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
1004 add_first = GNUNET_YES;
1007 add_second = GNUNET_NO;
1008 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
1010 add_second = GNUNET_YES;
1013 first_iter = pg->peers[first].connected_peers;
1014 while (first_iter != NULL)
1016 if (first_iter->daemon == pg->peers[second].daemon)
1017 add_first = GNUNET_NO;
1018 first_iter = first_iter->next;
1021 second_iter = pg->peers[second].connected_peers;
1022 add_second = GNUNET_YES;
1023 while (second_iter != NULL)
1025 if (second_iter->daemon == pg->peers[first].daemon)
1026 add_second = GNUNET_NO;
1027 second_iter = second_iter->next;
1034 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1036 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
1037 new_first->daemon = pg->peers[second].daemon;
1038 new_first->next = pg->peers[first].connected_peers;
1039 pg->peers[first].connected_peers = new_first;
1041 pg->peers[first].num_connections++;
1047 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1049 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
1050 new_second->daemon = pg->peers[first].daemon;
1051 new_second->next = pg->peers[second].connected_peers;
1052 pg->peers[second].connected_peers = new_second;
1053 pg->peers[first].num_connections++;
1055 pg->peers[second].num_connections++;
1063 * Add entries to the peers blacklisted list
1065 * @param pg the peer group we are working with
1066 * @param first index of the first peer
1067 * @param second index of the second peer
1069 * @return the number of connections added (can be 0, 1 or 2)
1073 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1078 GNUNET_HashCode hash_first;
1079 GNUNET_HashCode hash_second;
1081 hash_from_uid(first, &hash_first);
1082 hash_from_uid(second, &hash_second);
1084 add_first = GNUNET_NO;
1085 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
1087 add_first = GNUNET_YES;
1090 add_second = GNUNET_NO;
1091 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
1093 add_second = GNUNET_YES;
1099 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1100 pg->peers[first].num_connections++;
1106 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1107 pg->peers[second].num_connections++;
1115 * Remove entries from the peers blacklisted list
1117 * @param pg the peer group we are working with
1118 * @param first index of the first peer
1119 * @param second index of the second peer
1121 * @return the number of connections removed (can be 0, 1 or 2)
1125 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1130 GNUNET_HashCode hash_first;
1131 GNUNET_HashCode hash_second;
1133 hash_from_uid(first, &hash_first);
1134 hash_from_uid(second, &hash_second);
1136 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
1137 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
1142 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
1148 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
1156 * Scale free network construction as described in:
1158 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1160 * Start with a network of "one" peer, then progressively add
1161 * peers up to the total number. At each step, iterate over
1162 * all possible peers and connect new peer based on number of
1163 * existing connections of the target peer.
1165 * @param pg the peer group we are dealing with
1166 * @param proc the connection processor to use
1168 * @return the number of connections created
1171 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1174 unsigned int total_connections;
1175 unsigned int outer_count;
1177 unsigned int previous_total_connections;
1181 GNUNET_assert(pg->total > 1);
1183 /* Add a connection between the first two nodes */
1184 total_connections = proc(pg, 0, 1);
1186 for (outer_count = 1; outer_count < pg->total; outer_count++)
1188 previous_total_connections = total_connections;
1189 for (i = 0; i < outer_count; i++)
1191 probability = pg->peers[i].num_connections / (double)previous_total_connections;
1192 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1193 UINT64_MAX)) / ( (double) UINT64_MAX);
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196 "Considering connecting peer %d to peer %d\n",
1199 if (random < probability)
1202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1203 "Connecting peer %d to peer %d\n",
1206 total_connections += proc(pg, outer_count, i);
1211 return total_connections;
1215 * Create a topology given a peer group (set of running peers)
1216 * and a connection processor.
1218 * @param pg the peergroup to create the topology on
1219 * @param proc the connection processor to call to actually set
1220 * up connections between two peers
1222 * @return the number of connections that were set up
1226 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1230 unsigned int natLog;
1231 unsigned int randomPeer;
1232 double random, logNModifier, percentage;
1233 unsigned int smallWorldConnections;
1238 unsigned int useAnd;
1239 int connect_attempts;
1241 logNModifier = 0.5; /* FIXME: default value? */
1242 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1247 if (sscanf(p_string, "%lf", &logNModifier) != 1)
1248 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1249 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1253 GNUNET_free (p_string);
1255 percentage = 0.5; /* FIXME: default percentage? */
1256 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1261 if (sscanf(p_string, "%lf", &percentage) != 1)
1262 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1263 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1267 GNUNET_free (p_string);
1269 natLog = log (pg->total);
1270 connsPerPeer = ceil (natLog * logNModifier);
1272 if (connsPerPeer % 2 == 1)
1275 smallWorldConnections = 0;
1276 connect_attempts = 0;
1277 for (i = 0; i < pg->total; i++)
1280 max = i + connsPerPeer / 2;
1281 min = i - connsPerPeer / 2;
1283 if (max > pg->total - 1)
1285 max = max - pg->total;
1291 min = pg->total - 1 + min;
1295 for (j = 0; j < connsPerPeer / 2; j++)
1297 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1298 UINT64_MAX) / ( (double) UINT64_MAX));
1299 if (random < percentage)
1301 /* Connect to uniformly selected random peer */
1303 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1305 while ((((randomPeer < max) && (randomPeer > min))
1306 && (useAnd == 0)) || (((randomPeer > min)
1307 || (randomPeer < max))
1311 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1314 smallWorldConnections +=
1315 proc (pg, i, randomPeer);
1319 nodeToConnect = i + j + 1;
1320 if (nodeToConnect > pg->total - 1)
1322 nodeToConnect = nodeToConnect - pg->total;
1325 proc (pg, i, nodeToConnect);
1331 connect_attempts += smallWorldConnections;
1333 return connect_attempts;
1337 * Create a topology given a peer group (set of running peers)
1338 * and a connection processor.
1340 * @param pg the peergroup to create the topology on
1341 * @param proc the connection processor to call to actually set
1342 * up connections between two peers
1344 * @return the number of connections that were set up
1348 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1350 unsigned int outer_count, inner_count;
1351 unsigned int cutoff;
1352 int connect_attempts;
1353 double nat_percentage;
1356 nat_percentage = 0.6; /* FIXME: default percentage? */
1357 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1362 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1364 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1368 GNUNET_free (p_string);
1373 cutoff = (unsigned int) (nat_percentage * pg->total);
1375 connect_attempts = 0;
1377 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1379 for (inner_count = outer_count + 1; inner_count < pg->total;
1382 if ((outer_count > cutoff) || (inner_count > cutoff))
1385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1386 "Connecting peer %d to peer %d\n",
1387 outer_count, inner_count);
1389 connect_attempts += proc(pg, outer_count, inner_count);
1394 return connect_attempts;
1399 * Create a topology given a peer group (set of running peers)
1400 * and a connection processor.
1402 * @param pg the peergroup to create the topology on
1403 * @param proc the connection processor to call to actually set
1404 * up connections between two peers
1406 * @return the number of connections that were set up
1410 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1412 unsigned int i, j, k;
1413 unsigned int square;
1416 unsigned int toggle = 1;
1417 unsigned int nodeToConnect;
1418 unsigned int natLog;
1419 unsigned int node1Row;
1420 unsigned int node1Col;
1421 unsigned int node2Row;
1422 unsigned int node2Col;
1423 unsigned int distance;
1424 double probability, random, percentage;
1425 unsigned int smallWorldConnections;
1426 unsigned int small_world_it;
1428 int connect_attempts;
1429 square = floor (sqrt (pg->total));
1433 percentage = 0.5; /* FIXME: default percentage? */
1434 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1439 if (sscanf(p_string, "%lf", &percentage) != 1)
1440 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1441 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1445 GNUNET_free (p_string);
1447 if (percentage < 0.0)
1449 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1450 _("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1451 "PERCENTAGE", "TESTING", percentage);
1454 probability = 0.5; /* FIXME: default percentage? */
1455 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1460 if (sscanf(p_string, "%lf", &probability) != 1)
1461 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1462 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1466 GNUNET_free (p_string);
1468 if (square * square != pg->total)
1470 while (rows * cols < pg->total)
1472 if (toggle % 2 == 0)
1481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1482 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1486 connect_attempts = 0;
1487 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1488 * to the node to its right and above. Once this is over, we'll have our torus!
1489 * Special case for the last node (if the rows and columns are not equal), connect
1490 * to the first in the row to maintain topology.
1492 for (i = 0; i < pg->total; i++)
1494 /* First connect to the node to the right */
1495 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1496 nodeToConnect = i + 1;
1497 else if (i + 1 == pg->total)
1498 nodeToConnect = rows * cols - cols;
1500 nodeToConnect = i - cols + 1;
1502 connect_attempts += proc (pg, i, nodeToConnect);
1505 nodeToConnect = (rows * cols) - cols + i;
1507 nodeToConnect = i - cols;
1509 if (nodeToConnect < pg->total)
1510 connect_attempts += proc (pg, i, nodeToConnect);
1512 natLog = log (pg->total);
1513 #if VERBOSE_TESTING > 2
1514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1515 _("natural log of %d is %d, will run %d iterations\n"),
1516 pg->total, natLog, (int) (natLog * percentage));
1517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1519 smallWorldConnections = 0;
1520 small_world_it = (unsigned int)(natLog * percentage);
1521 GNUNET_assert(small_world_it > 0 && small_world_it < (unsigned int)-1);
1522 for (i = 0; i < small_world_it; i++)
1524 for (j = 0; j < pg->total; j++)
1526 /* Determine the row and column of node at position j on the 2d torus */
1527 node1Row = j / cols;
1528 node1Col = j - (node1Row * cols);
1529 for (k = 0; k < pg->total; k++)
1531 /* Determine the row and column of node at position k on the 2d torus */
1532 node2Row = k / cols;
1533 node2Col = k - (node2Row * cols);
1534 /* Simple Cartesian distance */
1535 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1538 /* Calculate probability as 1 over the square of the distance */
1539 probability = 1.0 / (distance * distance);
1540 /* Choose a random value between 0 and 1 */
1541 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1542 UINT64_MAX)) / ( (double) UINT64_MAX);
1543 /* If random < probability, then connect the two nodes */
1544 if (random < probability)
1545 smallWorldConnections += proc (pg, j, k);
1551 connect_attempts += smallWorldConnections;
1552 #if VERBOSE_TESTING > 2
1553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1554 _("Total connections added for small world: %d!\n"),
1555 smallWorldConnections);
1557 return connect_attempts;
1561 * Create a topology given a peer group (set of running peers)
1562 * and a connection processor.
1564 * @param pg the peergroup to create the topology on
1565 * @param proc the connection processor to call to actually set
1566 * up connections between two peers
1568 * @return the number of connections that were set up
1572 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1575 unsigned int outer_count;
1576 unsigned int inner_count;
1577 int connect_attempts;
1581 probability = 0.5; /* FIXME: default percentage? */
1582 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1587 if (sscanf(p_string, "%lf", &probability) != 1)
1588 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1589 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1593 GNUNET_free (p_string);
1595 connect_attempts = 0;
1596 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1598 for (inner_count = outer_count + 1; inner_count < pg->total;
1601 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1602 UINT64_MAX)) / ( (double) UINT64_MAX);
1604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1605 _("rand is %f probability is %f\n"), temp_rand,
1608 if (temp_rand < probability)
1610 connect_attempts += proc (pg, outer_count, inner_count);
1615 return connect_attempts;
1619 * Create a topology given a peer group (set of running peers)
1620 * and a connection processor. This particular function creates
1621 * the connections for a 2d-torus, plus additional "closest"
1622 * connections per peer.
1624 * @param pg the peergroup to create the topology on
1625 * @param proc the connection processor to call to actually set
1626 * up connections between two peers
1628 * @return the number of connections that were set up
1632 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1635 unsigned int square;
1638 unsigned int toggle = 1;
1639 unsigned int nodeToConnect;
1640 int connect_attempts;
1642 connect_attempts = 0;
1644 square = floor (sqrt (pg->total));
1648 if (square * square != pg->total)
1650 while (rows * cols < pg->total)
1652 if (toggle % 2 == 0)
1661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1662 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1665 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1666 * to the node to its right and above. Once this is over, we'll have our torus!
1667 * Special case for the last node (if the rows and columns are not equal), connect
1668 * to the first in the row to maintain topology.
1670 for (i = 0; i < pg->total; i++)
1672 /* First connect to the node to the right */
1673 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1674 nodeToConnect = i + 1;
1675 else if (i + 1 == pg->total)
1676 nodeToConnect = rows * cols - cols;
1678 nodeToConnect = i - cols + 1;
1680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1681 "Connecting peer %d to peer %d\n",
1684 connect_attempts += proc(pg, i, nodeToConnect);
1686 /* Second connect to the node immediately above */
1688 nodeToConnect = (rows * cols) - cols + i;
1690 nodeToConnect = i - cols;
1692 if (nodeToConnect < pg->total)
1695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1696 "Connecting peer %d to peer %d\n",
1699 connect_attempts += proc(pg, i, nodeToConnect);
1704 return connect_attempts;
1709 * Create a topology given a peer group (set of running peers)
1710 * and a connection processor.
1712 * @param pg the peergroup to create the topology on
1713 * @param proc the connection processor to call to actually set
1714 * up connections between two peers
1716 * @return the number of connections that were set up
1720 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1722 unsigned int outer_count;
1723 unsigned int inner_count;
1724 int connect_attempts;
1726 connect_attempts = 0;
1728 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1730 for (inner_count = outer_count + 1; inner_count < pg->total;
1734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1735 "Connecting peer %d to peer %d\n",
1736 outer_count, inner_count);
1738 connect_attempts += proc(pg, outer_count, inner_count);
1742 return connect_attempts;
1746 * Create a topology given a peer group (set of running peers)
1747 * and a connection processor.
1749 * @param pg the peergroup to create the topology on
1750 * @param proc the connection processor to call to actually set
1751 * up connections between two peers
1753 * @return the number of connections that were set up
1757 create_line (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1760 int connect_attempts;
1762 connect_attempts = 0;
1764 /* Connect each peer to the next highest numbered peer */
1765 for (count = 0; count < pg->total - 1; count++)
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1769 "Connecting peer %d to peer %d\n",
1772 connect_attempts += proc(pg, count, count + 1);
1775 return connect_attempts;
1779 * Create a topology given a peer group (set of running peers)
1780 * and a connection processor.
1782 * @param pg the peergroup to create the topology on
1783 * @param proc the connection processor to call to actually set
1784 * up connections between two peers
1786 * @return the number of connections that were set up
1790 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1793 int connect_attempts;
1795 connect_attempts = 0;
1797 /* Connect each peer to the next highest numbered peer */
1798 for (count = 0; count < pg->total - 1; count++)
1801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1802 "Connecting peer %d to peer %d\n",
1805 connect_attempts += proc(pg, count, count + 1);
1808 /* Connect the last peer to the first peer */
1809 connect_attempts += proc(pg, pg->total - 1, 0);
1811 return connect_attempts;
1816 * Iterator for writing friends of a peer to a file.
1818 * @param cls closure, an open writable file handle
1819 * @param key the key the daemon was stored under
1820 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1822 * @return GNUNET_YES to continue iteration
1824 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1825 * with a single file_iterator that takes a closure which contains
1826 * the prefix to write before the peer. Then this could be used
1827 * for blacklisting multiple transports and writing the friend
1828 * file. I'm sure *someone* will complain loudly about other
1829 * things that negate these functions even existing so no point in
1833 friend_file_iterator (void *cls,
1834 const GNUNET_HashCode * key,
1837 FILE *temp_friend_handle = cls;
1838 struct GNUNET_TESTING_Daemon *peer = value;
1839 struct GNUNET_PeerIdentity *temppeer;
1840 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1842 temppeer = &peer->id;
1843 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1844 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1849 struct BlacklistContext
1852 * The (open) file handle to write to
1854 FILE *temp_file_handle;
1857 * The transport that this peer will be blacklisted on.
1863 * Iterator for writing blacklist data to appropriate files.
1865 * @param cls closure, an open writable file handle
1866 * @param key the key the daemon was stored under
1867 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1869 * @return GNUNET_YES to continue iteration
1872 blacklist_file_iterator (void *cls,
1873 const GNUNET_HashCode * key,
1876 struct BlacklistContext *blacklist_ctx = cls;
1877 //FILE *temp_blacklist_handle = cls;
1878 struct GNUNET_TESTING_Daemon *peer = value;
1879 struct GNUNET_PeerIdentity *temppeer;
1880 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1882 temppeer = &peer->id;
1883 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1884 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1890 * Create the friend files based on the PeerConnection's
1891 * of each peer in the peer group, and copy the files
1892 * to the appropriate place
1894 * @param pg the peer group we are dealing with
1897 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1899 FILE *temp_friend_handle;
1900 unsigned int pg_iter;
1901 char *temp_service_path;
1905 enum GNUNET_OS_ProcessStatusType type;
1906 unsigned long return_code;
1911 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1912 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1914 mytemp = GNUNET_DISK_mktemp("friends");
1915 GNUNET_assert(mytemp != NULL);
1916 temp_friend_handle = fopen (mytemp, "wt");
1917 GNUNET_assert(temp_friend_handle != NULL);
1918 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1919 fclose(temp_friend_handle);
1922 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1924 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1925 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1928 if (UNLINK (mytemp) != 0)
1929 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1930 GNUNET_free (mytemp);
1934 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1936 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1937 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1938 "mv", mytemp, arg, NULL);
1940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1941 _("Copying file with command cp %s %s\n"), mytemp, arg);
1946 else /* Remote, scp the file to the correct place */
1948 if (NULL != pg->peers[pg_iter].daemon->username)
1949 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1951 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1952 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1953 "scp", mytemp, arg, NULL);
1956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1957 _("Copying file with command scp %s %s\n"), mytemp, arg);
1961 GNUNET_free (temp_service_path);
1962 GNUNET_free (mytemp);
1966 ret = GNUNET_SYSERR;
1967 while ((count < max_wait) && (ret != GNUNET_OK))
1970 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1974 _("Checking copy status of file %d\n"), pg_iter);
1976 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1978 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1980 ret = GNUNET_SYSERR;
1982 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1984 ret = GNUNET_SYSERR;
1988 pidarr[pg_iter] = 0;
1990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1991 _("File %d copied\n"), pg_iter);
1997 if (ret == GNUNET_SYSERR)
1999 /* FIXME: why sleep here? -CG */
2005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2006 _("Finished copying all friend files!\n"));
2008 GNUNET_free(pidarr);
2014 * Create the blacklist files based on the PeerConnection's
2015 * of each peer in the peer group, and copy the files
2016 * to the appropriate place.
2018 * @param pg the peer group we are dealing with
2019 * @param transports space delimited list of transports to blacklist
2022 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
2024 FILE *temp_file_handle;
2025 static struct BlacklistContext blacklist_ctx;
2026 unsigned int pg_iter;
2027 char *temp_service_path;
2031 enum GNUNET_OS_ProcessStatusType type;
2032 unsigned long return_code;
2039 char *temp_transports;
2041 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
2042 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2044 mytemp = GNUNET_DISK_mktemp("blacklist");
2045 GNUNET_assert(mytemp != NULL);
2046 temp_file_handle = fopen (mytemp, "wt");
2047 GNUNET_assert(temp_file_handle != NULL);
2048 temp_transports = GNUNET_strdup(transports);
2049 blacklist_ctx.temp_file_handle = temp_file_handle;
2050 transport_len = strlen(temp_transports) + 1;
2053 for (i = 0; i < transport_len; i++)
2055 if ((temp_transports[i] == ' ') && (pos == NULL))
2056 continue; /* At start of string (whitespace) */
2057 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2059 temp_transports[i] = '\0';
2060 blacklist_ctx.transport = pos;
2061 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
2063 } /* At beginning of actual string */
2064 else if (pos == NULL)
2066 pos = &temp_transports[i];
2070 GNUNET_free (temp_transports);
2071 fclose(temp_file_handle);
2074 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
2076 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2077 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2080 if (UNLINK (mytemp) != 0)
2081 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
2082 GNUNET_free (mytemp);
2086 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2088 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2089 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2090 "mv", mytemp, arg, NULL);
2092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2093 _("Copying file with command cp %s %s\n"), mytemp, arg);
2098 else /* Remote, scp the file to the correct place */
2100 if (NULL != pg->peers[pg_iter].daemon->username)
2101 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
2103 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
2104 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
2105 "scp", mytemp, arg, NULL);
2108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2109 _("Copying file with command scp %s %s\n"), mytemp, arg);
2113 GNUNET_free (temp_service_path);
2114 GNUNET_free (mytemp);
2118 ret = GNUNET_SYSERR;
2119 while ((count < max_wait) && (ret != GNUNET_OK))
2122 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2126 _("Checking copy status of file %d\n"), pg_iter);
2128 if (pidarr[pg_iter] != 0) /* Check for already completed! */
2130 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
2132 ret = GNUNET_SYSERR;
2134 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
2136 ret = GNUNET_SYSERR;
2140 pidarr[pg_iter] = 0;
2142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2143 _("File %d copied\n"), pg_iter);
2149 if (ret == GNUNET_SYSERR)
2151 /* FIXME: why sleep here? -CG */
2157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2158 _("Finished copying all blacklist files!\n"));
2160 GNUNET_free(pidarr);
2166 * Internal notification of a connection, kept so that we can ensure some connections
2167 * happen instead of flooding all testing daemons with requests to connect.
2169 static void internal_connect_notify (void *cls,
2170 const struct GNUNET_PeerIdentity *first,
2171 const struct GNUNET_PeerIdentity *second,
2173 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2174 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2175 struct GNUNET_TESTING_Daemon *first_daemon,
2176 struct GNUNET_TESTING_Daemon *second_daemon,
2179 struct GNUNET_TESTING_PeerGroup *pg = cls;
2180 outstanding_connects--;
2182 pg->notify_connection(pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
2187 * Either delay a connection (because there are too many outstanding)
2188 * or schedule it for right now.
2190 * @param cls a connection context
2191 * @param tc the task runtime context
2193 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2195 struct ConnectContext *connect_context = cls;
2197 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2200 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2202 #if VERBOSE_TESTING > 2
2203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2204 _("Delaying connect, we have too many outstanding connections!\n"));
2206 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_connect, connect_context);
2210 #if VERBOSE_TESTING > 2
2211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2212 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
2214 outstanding_connects++;
2215 GNUNET_TESTING_daemons_connect (connect_context->first,
2216 connect_context->second,
2219 &internal_connect_notify,
2220 connect_context->pg);
2221 GNUNET_free(connect_context);
2227 * Iterator for actually scheduling connections to be created
2228 * between two peers.
2230 * @param cls closure, a GNUNET_TESTING_Daemon
2231 * @param key the key the second Daemon was stored under
2232 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2234 * @return GNUNET_YES to continue iteration
2237 connect_iterator (void *cls,
2238 const GNUNET_HashCode * key,
2241 struct PeerData *first = cls;
2242 struct GNUNET_TESTING_Daemon *second = value;
2243 struct ConnectContext *connect_context;
2245 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2246 connect_context->pg = first->pg;
2247 connect_context->first = first->daemon;
2248 connect_context->second = second;
2249 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
2256 * Iterator for copying all entries in the allowed hashmap to the
2259 * @param cls closure, a GNUNET_TESTING_Daemon
2260 * @param key the key the second Daemon was stored under
2261 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2263 * @return GNUNET_YES to continue iteration
2266 copy_topology_iterator (void *cls,
2267 const GNUNET_HashCode * key,
2270 struct PeerData *first = cls;
2272 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2278 * Make the peers to connect the same as those that are allowed to be
2281 * @param pg the peer group
2284 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2286 unsigned int pg_iter;
2291 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2293 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
2294 if (GNUNET_SYSERR == ret)
2295 return GNUNET_SYSERR;
2297 total = total + ret;
2305 * Connect the topology as specified by the PeerConnection's
2306 * of each peer in the peer group
2308 * @param pg the peer group we are dealing with
2309 * @return the number of connections that will be attempted
2312 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
2314 unsigned int pg_iter;
2318 struct PeerConnection *connection_iter;
2319 struct ConnectContext *connect_context;
2323 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2325 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
2326 if (GNUNET_SYSERR == ret)
2327 return GNUNET_SYSERR;
2329 total = total + ret;
2333 while (connection_iter != NULL)
2335 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2336 connect_context->pg = pg;
2337 connect_context->first = ;
2338 connect_context->second = connection_iter->daemon;
2339 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
2340 connection_iter = connection_iter->next;
2349 * Takes a peer group and creates a topology based on the
2350 * one specified. Creates a topology means generates friend
2351 * files for the peers so they can only connect to those allowed
2352 * by the topology. This will only have an effect once peers
2353 * are started if the FRIENDS_ONLY option is set in the base
2354 * config. Also takes an optional restrict topology which
2355 * disallows connections based on a particular transport
2356 * UNLESS they are specified in the restricted topology.
2358 * @param pg the peer group struct representing the running peers
2359 * @param topology which topology to connect the peers in
2360 * @param restrict_topology allow only direct TCP connections in this topology
2361 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2362 * @param restrict_transports space delimited list of transports to blacklist
2363 * to create restricted topology
2365 * @return the maximum number of connections were all allowed peers
2366 * connected to each other
2369 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2370 enum GNUNET_TESTING_Topology topology,
2371 enum GNUNET_TESTING_Topology restrict_topology,
2372 char *restrict_transports)
2375 int num_connections;
2376 int unblacklisted_connections;
2378 GNUNET_assert (pg->notify_connection != NULL);
2382 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2385 _("Creating clique topology\n"));
2387 num_connections = create_clique (pg, &add_allowed_connections);
2389 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2392 _("Creating small world (ring) topology\n"));
2394 num_connections = create_small_world_ring (pg, &add_allowed_connections);
2396 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2399 _("Creating small world (2d-torus) topology\n"));
2401 num_connections = create_small_world (pg, &add_allowed_connections);
2403 case GNUNET_TESTING_TOPOLOGY_RING:
2405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2406 _("Creating ring topology\n"));
2408 num_connections = create_ring (pg, &add_allowed_connections);
2410 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2413 _("Creating 2d torus topology\n"));
2415 num_connections = create_2d_torus (pg, &add_allowed_connections);
2417 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2420 _("Creating Erdos-Renyi topology\n"));
2422 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2424 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2427 _("Creating InterNAT topology\n"));
2429 num_connections = create_nated_internet (pg, &add_allowed_connections);
2431 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2434 _("Creating Scale Free topology\n"));
2436 num_connections = create_scale_free (pg, &add_allowed_connections);
2438 case GNUNET_TESTING_TOPOLOGY_LINE:
2440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2441 _("Creating straight line topology\n"));
2443 num_connections = create_line (pg, &add_allowed_connections);
2445 case GNUNET_TESTING_TOPOLOGY_NONE:
2447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2448 _("Creating no allowed topology (all peers can connect at core level)\n"));
2450 num_connections = 0;
2453 num_connections = 0;
2457 if (num_connections < 0)
2458 return GNUNET_SYSERR;
2460 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2462 ret = create_and_copy_friend_files(pg);
2463 if (ret != GNUNET_OK)
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2467 _("Failed during friend file copying!\n"));
2469 return GNUNET_SYSERR;
2474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2475 _("Friend files created/copied successfully!\n"));
2480 /* Use the create clique method to initially set all connections as blacklisted. */
2481 if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2482 create_clique (pg, &blacklist_connections);
2483 unblacklisted_connections = 0;
2484 /* Un-blacklist connections as per the topology specified */
2485 switch (restrict_topology)
2487 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2490 _("Blacklisting all but clique topology\n"));
2492 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2494 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2497 _("Blacklisting all but small world (ring) topology\n"));
2499 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2501 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2504 _("Blacklisting all but small world (2d-torus) topology\n"));
2506 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2508 case GNUNET_TESTING_TOPOLOGY_RING:
2510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2511 _("Blacklisting all but ring topology\n"));
2513 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2515 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2518 _("Blacklisting all but 2d torus topology\n"));
2520 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2522 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2525 _("Blacklisting all but Erdos-Renyi topology\n"));
2527 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2529 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2532 _("Blacklisting all but InterNAT topology\n"));
2534 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2536 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2539 _("Blacklisting all but Scale Free topology\n"));
2541 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2543 case GNUNET_TESTING_TOPOLOGY_LINE:
2545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2546 _("Blacklisting all but straight line topology\n"));
2548 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2550 case GNUNET_TESTING_TOPOLOGY_NONE:
2552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2553 _("Creating no blacklist topology (all peers can connect at transport level)\n"));
2559 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2561 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2562 if (ret != GNUNET_OK)
2565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2566 _("Failed during blacklist file copying!\n"));
2568 return GNUNET_SYSERR;
2573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2574 _("Blacklist files created/copied successfully!\n"));
2578 return num_connections;
2581 struct RandomContext
2586 struct GNUNET_TESTING_PeerGroup *pg;
2589 * uid of the first peer
2594 * Peer data for first peer.
2596 struct PeerData *first;
2599 * Random percentage to use
2604 struct MinimumContext
2609 struct GNUNET_TESTING_PeerGroup *pg;
2612 * uid of the first peer
2617 * Peer data for first peer.
2619 struct PeerData *first;
2622 * Number of conns per peer
2624 unsigned int num_to_add;
2627 * Permuted array of all possible connections. Only add the Nth
2628 * peer if it's in the Nth position.
2630 unsigned int *pg_array;
2633 * What number is the current element we are iterating over?
2635 unsigned int current;
2643 struct GNUNET_TESTING_PeerGroup *pg;
2646 * uid of the first peer
2651 * uid of the second peer
2653 uint32_t second_uid;
2656 * Peer data for first peer.
2658 struct PeerData *first;
2661 * Which peer has been chosen as the one to add?
2663 unsigned int chosen;
2666 * What number is the current element we are iterating over?
2668 unsigned int current;
2672 * Iterator for choosing random peers to connect.
2674 * @param cls closure, a RandomContext
2675 * @param key the key the second Daemon was stored under
2676 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2678 * @return GNUNET_YES to continue iteration
2681 random_connect_iterator (void *cls,
2682 const GNUNET_HashCode * key,
2685 struct RandomContext *random_ctx = cls;
2686 double random_number;
2687 uint32_t second_pos;
2688 GNUNET_HashCode first_hash;
2689 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2690 UINT64_MAX)) / ( (double) UINT64_MAX);
2691 if (random_number < random_ctx->percentage)
2693 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2695 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2696 uid_from_hash(key, &second_pos);
2697 hash_from_uid(random_ctx->first_uid, &first_hash);
2698 GNUNET_assert(random_ctx->pg->total > second_pos);
2699 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2705 * Iterator for adding at least X peers to a peers connection set.
2707 * @param cls closure, MinimumContext
2708 * @param key the key the second Daemon was stored under
2709 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2711 * @return GNUNET_YES to continue iteration
2714 minimum_connect_iterator (void *cls,
2715 const GNUNET_HashCode * key,
2718 struct MinimumContext *min_ctx = cls;
2719 uint32_t second_pos;
2720 GNUNET_HashCode first_hash;
2723 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2725 for (i = 0; i < min_ctx->num_to_add; i++)
2727 if (min_ctx->pg_array[i] == min_ctx->current)
2729 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2730 uid_from_hash(key, &second_pos);
2731 hash_from_uid(min_ctx->first_uid, &first_hash);
2732 GNUNET_assert(min_ctx->pg->total > second_pos);
2733 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));
2734 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2735 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2742 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2748 * Iterator for adding peers to a connection set based on a depth first search.
2750 * @param cls closure, MinimumContext
2751 * @param key the key the second daemon was stored under
2752 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2754 * @return GNUNET_YES to continue iteration
2757 dfs_connect_iterator (void *cls,
2758 const GNUNET_HashCode * key,
2761 struct DFSContext *dfs_ctx = cls;
2762 GNUNET_HashCode first_hash;
2764 if (dfs_ctx->current == dfs_ctx->chosen)
2766 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2767 uid_from_hash(key, &dfs_ctx->second_uid);
2768 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2769 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));
2770 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2771 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2772 return GNUNET_NO; /* We have found our peer, don't iterate more */
2781 * From the set of connections possible, choose percentage percent of connections
2782 * to actually connect.
2784 * @param pg the peergroup we are dealing with
2785 * @param percentage what percent of total connections to make
2788 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2790 struct RandomContext random_ctx;
2793 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2795 random_ctx.first_uid = pg_iter;
2796 random_ctx.first = &pg->peers[pg_iter];
2797 random_ctx.percentage = percentage;
2799 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2800 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2801 /* Now remove the old connections */
2802 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2803 /* And replace with the random set */
2804 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2809 * From the set of connections possible, choose at least num connections per
2812 * @param pg the peergroup we are dealing with
2813 * @param num how many connections at least should each peer have (if possible)?
2816 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2818 struct MinimumContext minimum_ctx;
2821 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2823 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2826 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2828 minimum_ctx.first_uid = pg_iter;
2829 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2830 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2831 minimum_ctx.first = &pg->peers[pg_iter];
2832 minimum_ctx.pg = pg;
2833 minimum_ctx.num_to_add = num;
2834 minimum_ctx.current = 0;
2835 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2836 &minimum_connect_iterator,
2840 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2842 /* Remove the "old" connections */
2843 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2844 /* And replace with the working set */
2845 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2852 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2855 unsigned int pg_iter;
2859 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2861 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2868 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2871 unsigned int pg_iter;
2875 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2877 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2884 struct FindClosestContext
2887 * The currently known closest peer.
2889 struct GNUNET_TESTING_Daemon *closest;
2892 * The info for the peer we are adding connections for.
2894 struct PeerData *curr_peer;
2897 * The distance (bits) between the current
2898 * peer and the currently known closest.
2900 unsigned int closest_dist;
2903 * The offset of the closest known peer in
2906 unsigned int closest_num;
2910 * Iterator over hash map entries of the allowed
2911 * peer connections. Find the closest, not already
2912 * connected peer and return it.
2914 * @param cls closure (struct FindClosestContext)
2915 * @param key current key code (hash of offset in pg)
2916 * @param value value in the hash map - a GNUNET_TESTING_Daemon
2917 * @return GNUNET_YES if we should continue to
2922 int find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
2924 struct FindClosestContext *closest_ctx = cls;
2925 struct GNUNET_TESTING_Daemon *daemon = value;
2927 if (((closest_ctx->closest == NULL) ||
2928 (GNUNET_CRYPTO_hash_matching_bits(&daemon->id.hashPubKey, &closest_ctx->curr_peer->daemon->id.hashPubKey) > closest_ctx->closest_dist))
2929 && (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains(closest_ctx->curr_peer->connect_peers, key)))
2931 closest_ctx->closest_dist = GNUNET_CRYPTO_hash_matching_bits(&daemon->id.hashPubKey, &closest_ctx->curr_peer->daemon->id.hashPubKey);
2932 closest_ctx->closest = daemon;
2933 uid_from_hash(key, &closest_ctx->closest_num);
2939 * From the set of connections possible, choose at num connections per
2940 * peer based on depth which are closest out of those allowed. Guaranteed
2941 * to add num peers to connect to, provided there are that many peers
2942 * in the underlay topology to connect to.
2944 * @param pg the peergroup we are dealing with
2945 * @param num how many connections at least should each peer have (if possible)?
2946 * @param proc processor to actually add the connections
2949 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num, GNUNET_TESTING_ConnectionProcessor proc)
2951 struct FindClosestContext closest_ctx;
2955 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
2957 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2959 closest_ctx.curr_peer = &pg->peers[pg_iter];
2960 closest_ctx.closest = NULL;
2961 closest_ctx.closest_dist = 0;
2962 closest_ctx.closest_num = 0;
2963 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &find_closest_peers, &closest_ctx);
2964 if (closest_ctx.closest != NULL)
2966 GNUNET_assert((0 <= closest_ctx.closest_num) && (closest_ctx.closest_num < pg->total));
2967 proc(pg, pg_iter, closest_ctx.closest_num);
2974 * From the set of connections possible, choose at least num connections per
2975 * peer based on depth first traversal of peer connections. If DFS leaves
2976 * peers unconnected, ensure those peers get connections.
2978 * @param pg the peergroup we are dealing with
2979 * @param num how many connections at least should each peer have (if possible)?
2982 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2984 struct DFSContext dfs_ctx;
2987 uint32_t starting_peer;
2988 uint32_t least_connections;
2989 GNUNET_HashCode second_hash;
2991 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2993 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2998 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
3000 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3002 least_connections = -1; /* Set to very high number */
3003 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3005 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
3007 starting_peer = pg_iter;
3008 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
3013 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
3019 /* Choose a random peer from the chosen peers set of connections to add */
3020 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
3021 dfs_ctx.first_uid = starting_peer;
3022 dfs_ctx.first = &pg->peers[starting_peer];
3024 dfs_ctx.current = 0;
3026 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
3027 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
3028 hash_from_uid(dfs_ctx.second_uid, &second_hash);
3029 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
3030 starting_peer = dfs_ctx.second_uid;
3033 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3035 /* Remove the "old" connections */
3036 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
3037 /* And replace with the working set */
3038 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
3043 * Internal callback for topology information for a particular peer.
3046 internal_topology_callback(void *cls,
3047 const struct GNUNET_PeerIdentity *peer,
3048 struct GNUNET_TIME_Relative latency, uint32_t distance)
3050 struct CoreContext *core_ctx = cls;
3051 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
3053 if (peer == NULL) /* Either finished, or something went wrong */
3055 iter_ctx->completed++;
3056 iter_ctx->connected--;
3057 /* One core context allocated per iteration, must free! */
3058 GNUNET_free(core_ctx);
3062 iter_ctx->topology_cb(iter_ctx->cls, &core_ctx->daemon->id, peer, latency, distance, NULL);
3065 if (iter_ctx->completed == iter_ctx->total)
3067 iter_ctx->topology_cb(iter_ctx->cls, NULL, NULL, GNUNET_TIME_relative_get_zero(), 0, NULL);
3068 /* Once all are done, free the iteration context */
3069 GNUNET_free(iter_ctx);
3075 * Check running topology iteration tasks, if below max start a new one, otherwise
3076 * schedule for some time in the future.
3079 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3081 struct CoreContext *core_context = cls;
3082 struct TopologyIterateContext *topology_context = (struct TopologyIterateContext *)core_context->iter_context;
3083 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3086 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3088 #if VERBOSE_TESTING > 2
3089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3090 _("Delaying connect, we have too many outstanding connections!\n"));
3092 GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_topology, core_context);
3096 #if VERBOSE_TESTING > 2
3097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3098 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3100 topology_context->connected++;
3101 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->sched, core_context->daemon->cfg, &internal_topology_callback, core_context))
3102 internal_topology_callback(core_context, NULL, GNUNET_TIME_relative_get_zero(), 0);
3108 * Iterate over all (running) peers in the peer group, retrieve
3109 * all connections that each currently has.
3112 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyTopology cb, void *cls)
3114 struct TopologyIterateContext *topology_context;
3115 struct CoreContext *core_ctx;
3117 unsigned int total_count;
3119 /* Allocate a single topology iteration context */
3120 topology_context = GNUNET_malloc(sizeof(struct TopologyIterateContext));
3121 topology_context->topology_cb = cb;
3122 topology_context->cls = cls;
3124 for (i = 0; i < pg->total; i++)
3126 if (pg->peers[i].daemon->running == GNUNET_YES)
3128 /* Allocate one core context per core we need to connect to */
3129 core_ctx = GNUNET_malloc(sizeof(struct CoreContext));
3130 core_ctx->daemon = pg->peers[i].daemon;
3131 /* Set back pointer to topology iteration context */
3132 core_ctx->iter_context = topology_context;
3133 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_topology, core_ctx);
3137 topology_context->total = total_count;
3142 * Callback function to process statistic values.
3143 * This handler is here only really to insert a peer
3144 * identity (or daemon) so the statistics can be uniquely
3145 * tied to a single running peer.
3147 * @param cls closure
3148 * @param subsystem name of subsystem that created the statistic
3149 * @param name the name of the datum
3150 * @param value the current value
3151 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
3152 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
3154 static int internal_stats_callback (void *cls,
3155 const char *subsystem,
3160 struct StatsCoreContext *core_context = cls;
3161 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3163 return stats_context->proc(stats_context->cls, &core_context->daemon->id, subsystem, name, value, is_persistent);
3167 * Internal continuation call for statistics iteration.
3169 * @param cls closure, the CoreContext for this iteration
3170 * @param success whether or not the statistics iterations
3171 * was canceled or not (we don't care)
3173 static void internal_stats_cont (void *cls, int success)
3175 struct StatsCoreContext *core_context = cls;
3176 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3178 stats_context->connected--;
3179 stats_context->completed++;
3181 if (stats_context->completed == stats_context->total)
3183 stats_context->cont(stats_context->cls, GNUNET_YES);
3184 GNUNET_free(stats_context);
3187 if (core_context->stats_handle != NULL)
3188 GNUNET_STATISTICS_destroy(core_context->stats_handle, GNUNET_NO);
3190 GNUNET_free(core_context);
3194 * Check running topology iteration tasks, if below max start a new one, otherwise
3195 * schedule for some time in the future.
3198 schedule_get_statistics(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3200 struct StatsCoreContext *core_context = cls;
3201 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3203 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3206 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3208 #if VERBOSE_TESTING > 2
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3210 _("Delaying connect, we have too many outstanding connections!\n"));
3212 GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_statistics, core_context);
3216 #if VERBOSE_TESTING > 2
3217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3218 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3221 stats_context->connected++;
3222 core_context->stats_handle = GNUNET_STATISTICS_create(core_context->daemon->sched, "testing", core_context->daemon->cfg);
3223 if (core_context->stats_handle == NULL)
3225 internal_stats_cont (core_context, GNUNET_NO);
3229 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);
3230 if (core_context->stats_get_handle == NULL)
3231 internal_stats_cont (core_context, GNUNET_NO);
3236 struct DuplicateStats
3239 * Next item in the list
3241 struct DuplicateStats *next;
3244 * Nasty string, concatenation of relevant information.
3246 char *unique_string;
3250 * Check whether the combination of port/host/unix domain socket
3251 * already exists in the list of peers being checked for statistics.
3253 * @param pg the peergroup in question
3254 * @param specific_peer the peer we're concerned with
3255 * @param stats_list the list to return to the caller
3257 * @return GNUNET_YES if the statistics instance has been seen already,
3258 * GNUNET_NO if not (and we may have added it to the list)
3261 stats_check_existing(struct GNUNET_TESTING_PeerGroup *pg, struct PeerData *specific_peer, struct DuplicateStats **stats_list)
3263 struct DuplicateStats *pos;
3264 char *unix_domain_socket;
3265 unsigned long long port;
3267 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(pg->cfg, "testing", "single_statistics_per_host"))
3268 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
3271 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(specific_peer->cfg, "statistics", "unixpath", &unix_domain_socket))
3274 GNUNET_CONFIGURATION_get_value_number(specific_peer->cfg, "statistics", "port", &port);
3276 if (specific_peer->daemon->hostname != NULL)
3277 GNUNET_asprintf(&to_match, "%s%s%llu", specific_peer->daemon->hostname, unix_domain_socket, port);
3279 GNUNET_asprintf(&to_match, "%s%llu", unix_domain_socket, port);
3283 if (0 == strcmp(to_match, pos->unique_string))
3285 GNUNET_free(to_match);
3290 pos = GNUNET_malloc(sizeof(struct DuplicateStats));
3291 pos->unique_string = to_match;
3292 pos->next = *stats_list;
3298 * Iterate over all (running) peers in the peer group, retrieve
3299 * all statistics from each.
3302 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
3303 GNUNET_STATISTICS_Callback cont,
3304 GNUNET_TESTING_STATISTICS_Iterator proc, void *cls)
3306 struct StatsIterateContext *stats_context;
3307 struct StatsCoreContext *core_ctx;
3309 unsigned int total_count;
3310 struct DuplicateStats *stats_list;
3311 struct DuplicateStats *pos;
3314 /* Allocate a single stats iteration context */
3315 stats_context = GNUNET_malloc(sizeof(struct StatsIterateContext));
3316 stats_context->cont = cont;
3317 stats_context->proc = proc;
3318 stats_context->cls = cls;
3321 for (i = 0; i < pg->total; i++)
3323 if ((pg->peers[i].daemon->running == GNUNET_YES) && (GNUNET_NO == stats_check_existing(pg, &pg->peers[i], &stats_list)))
3325 /* Allocate one core context per core we need to connect to */
3326 core_ctx = GNUNET_malloc(sizeof(struct StatsCoreContext));
3327 core_ctx->daemon = pg->peers[i].daemon;
3328 /* Set back pointer to topology iteration context */
3329 core_ctx->iter_context = stats_context;
3330 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_statistics, core_ctx);
3335 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Retrieving stats from %u total instances.\n", total_count);
3336 stats_context->total = total_count;
3337 if (stats_list != NULL)
3342 GNUNET_free(pos->unique_string);
3343 stats_list = pos->next;
3345 pos = stats_list->next;
3352 * There are many ways to connect peers that are supported by this function.
3353 * To connect peers in the same topology that was created via the
3354 * GNUNET_TESTING_create_topology, the topology variable must be set to
3355 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
3356 * a new instance of that topology will be generated and attempted to be
3357 * connected. This could result in some connections being impossible,
3358 * because some topologies are non-deterministic.
3360 * @param pg the peer group struct representing the running peers
3361 * @param topology which topology to connect the peers in
3362 * @param options options for connecting the topology
3363 * @param option_modifier modifier for options that take a parameter
3364 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
3367 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3368 enum GNUNET_TESTING_Topology topology,
3369 enum GNUNET_TESTING_TopologyOption options,
3370 double option_modifier)
3374 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3375 #if VERBOSE_TOPOLOGY
3376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3377 _("Creating clique CONNECT topology\n"));
3379 create_clique (pg, &add_actual_connections);
3381 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3382 #if VERBOSE_TOPOLOGY
3383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3384 _("Creating small world (ring) CONNECT topology\n"));
3386 create_small_world_ring (pg, &add_actual_connections);
3388 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3389 #if VERBOSE_TOPOLOGY
3390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3391 _("Creating small world (2d-torus) CONNECT topology\n"));
3393 create_small_world (pg, &add_actual_connections);
3395 case GNUNET_TESTING_TOPOLOGY_RING:
3396 #if VERBOSE_TOPOLOGY
3397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3398 _("Creating ring CONNECT topology\n"));
3400 create_ring (pg, &add_actual_connections);
3402 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3403 #if VERBOSE_TOPOLOGY
3404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3405 _("Creating 2d torus CONNECT topology\n"));
3407 create_2d_torus (pg, &add_actual_connections);
3409 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3410 #if VERBOSE_TOPOLOGY
3411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3412 _("Creating Erdos-Renyi CONNECT topology\n"));
3414 create_erdos_renyi (pg, &add_actual_connections);
3416 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3417 #if VERBOSE_TOPOLOGY
3418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3419 _("Creating InterNAT CONNECT topology\n"));
3421 create_nated_internet (pg, &add_actual_connections);
3423 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3424 #if VERBOSE_TOPOLOGY
3425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3426 _("Creating Scale Free CONNECT topology\n"));
3428 create_scale_free (pg, &add_actual_connections);
3430 case GNUNET_TESTING_TOPOLOGY_LINE:
3431 #if VERBOSE_TOPOLOGY
3432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3433 _("Creating straight line CONNECT topology\n"));
3435 create_line (pg, &add_actual_connections);
3437 case GNUNET_TESTING_TOPOLOGY_NONE:
3438 #if VERBOSE_TOPOLOGY
3439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3440 _("Creating no CONNECT topology\n"));
3442 copy_allowed_topology(pg);
3445 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3446 _("Unknown topology specification, can't connect peers!\n"));
3447 return GNUNET_SYSERR;
3452 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
3453 #if VERBOSE_TOPOLOGY
3454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3455 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
3457 choose_random_connections(pg, option_modifier);
3459 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
3460 #if VERBOSE_TOPOLOGY
3461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3462 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3464 choose_minimum(pg, (unsigned int)option_modifier);
3466 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
3467 #if VERBOSE_TOPOLOGY
3468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3469 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3471 perform_dfs(pg, (int)option_modifier);
3473 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
3474 #if VERBOSE_TOPOLOGY
3475 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3476 _("Finding additional %u closest peers each (if possible)\n"), (unsigned int)option_modifier);
3478 add_closest(pg, (unsigned int)option_modifier, &add_actual_connections);
3480 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
3482 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
3488 return connect_topology(pg);
3492 * Callback that is called whenever a hostkey is generated
3493 * for a peer. Call the real callback and decrement the
3494 * starting counter for the peergroup.
3496 * @param cls closure
3497 * @param id identifier for the daemon, NULL on error
3498 * @param d handle for the daemon
3499 * @param emsg error message (NULL on success)
3501 static void internal_hostkey_callback (void *cls,
3502 const struct GNUNET_PeerIdentity *id,
3503 struct GNUNET_TESTING_Daemon *d,
3506 struct InternalStartContext *internal_context = cls;
3507 internal_context->peer->pg->starting--;
3508 internal_context->peer->pg->started++;
3509 if (internal_context->hostkey_callback != NULL)
3510 internal_context->hostkey_callback(internal_context->hostkey_cls, id, d, emsg);
3511 else if (internal_context->peer->pg->started == internal_context->peer->pg->total)
3513 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
3514 GNUNET_TESTING_daemons_continue_startup(internal_context->peer->pg);
3519 * Callback that is called whenever a peer has finished starting.
3520 * Call the real callback and decrement the starting counter
3521 * for the peergroup.
3523 * @param cls closure
3524 * @param id identifier for the daemon, NULL on error
3525 * @param d handle for the daemon
3526 * @param emsg error message (NULL on success)
3528 static void internal_startup_callback (void *cls,
3529 const struct GNUNET_PeerIdentity *id,
3530 const struct GNUNET_CONFIGURATION_Handle *cfg,
3531 struct GNUNET_TESTING_Daemon *d,
3534 struct InternalStartContext *internal_context = cls;
3535 internal_context->peer->pg->starting--;
3536 if (internal_context->start_cb != NULL)
3537 internal_context->start_cb(internal_context->start_cb_cls, id, cfg, d, emsg);
3541 internal_continue_startup (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3543 struct InternalStartContext *internal_context = cls;
3545 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3550 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
3552 internal_context->peer->pg->starting++;
3553 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
3557 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_continue_startup, internal_context);
3563 * Callback for informing us about a successful
3564 * or unsuccessful churn start call.
3566 * @param cls a ChurnContext
3567 * @param id the peer identity of the started peer
3568 * @param cfg the handle to the configuration of the peer
3569 * @param d handle to the daemon for the peer
3570 * @param emsg NULL on success, non-NULL on failure
3574 churn_start_callback (void *cls,
3575 const struct GNUNET_PeerIdentity *id,
3576 const struct GNUNET_CONFIGURATION_Handle *cfg,
3577 struct GNUNET_TESTING_Daemon *d,
3580 struct ChurnRestartContext *startup_ctx = cls;
3581 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
3583 unsigned int total_left;
3584 char *error_message;
3586 error_message = NULL;
3589 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3590 "Churn stop callback failed with error `%s'\n",
3592 churn_ctx->num_failed_start++;
3596 churn_ctx->num_to_start--;
3600 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3601 "Started peer, %d left.\n",
3602 churn_ctx->num_to_start);
3605 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3607 if (total_left == 0)
3609 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3610 GNUNET_asprintf(&error_message,
3611 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3612 churn_ctx->num_failed_start,
3613 churn_ctx->num_failed_stop);
3614 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3615 GNUNET_free_non_null(error_message);
3616 GNUNET_free(churn_ctx);
3617 GNUNET_free(startup_ctx);
3622 static void schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3624 struct PeerRestartContext *peer_restart_ctx = cls;
3625 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
3627 if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
3628 GNUNET_SCHEDULER_add_delayed(peer_restart_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_churn_restart, peer_restart_ctx);
3631 GNUNET_TESTING_daemon_start_stopped(peer_restart_ctx->daemon,
3632 startup_ctx->timeout,
3633 &churn_start_callback,
3635 GNUNET_free(peer_restart_ctx);
3640 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3642 struct InternalStartContext *internal_context = cls;
3644 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3649 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
3651 internal_context->peer->pg->starting++;
3652 internal_context->peer->daemon = GNUNET_TESTING_daemon_start (internal_context->peer->pg->sched,
3653 internal_context->peer->cfg,
3654 internal_context->timeout,
3655 internal_context->hostname,
3656 internal_context->username,
3657 internal_context->sshport,
3658 &internal_hostkey_callback,
3660 &internal_startup_callback,
3665 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_start, internal_context);
3670 * Function which continues a peer group starting up
3671 * after successfully generating hostkeys for each peer.
3673 * @param pg the peer group to continue starting
3677 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
3682 for (i = 0; i < pg->total; i++)
3684 GNUNET_SCHEDULER_add_now (pg->sched, &internal_continue_startup, &pg->peers[i].internal_context);
3685 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
3690 * Start count gnunet instances with the same set of transports and
3691 * applications. The port numbers (any option called "PORT") will be
3692 * adjusted to ensure that no two peers running on the same system
3693 * have the same port(s) in their respective configurations.
3695 * @param sched scheduler to use
3696 * @param cfg configuration template to use
3697 * @param total number of daemons to start
3698 * @param timeout total time allowed for peers to start
3699 * @param hostkey_callback function to call on each peers hostkey generation
3700 * if NULL, peers will be started by this call, if non-null,
3701 * GNUNET_TESTING_daemons_continue_startup must be called after
3702 * successful hostkey generation
3703 * @param hostkey_cls closure for hostkey callback
3704 * @param cb function to call on each daemon that was started
3705 * @param cb_cls closure for cb
3706 * @param connect_callback function to call each time two hosts are connected
3707 * @param connect_callback_cls closure for connect_callback
3708 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
3710 * @return NULL on error, otherwise handle to control peer group
3712 struct GNUNET_TESTING_PeerGroup *
3713 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
3714 const struct GNUNET_CONFIGURATION_Handle *cfg,
3716 struct GNUNET_TIME_Relative timeout,
3717 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
3719 GNUNET_TESTING_NotifyDaemonRunning cb,
3721 GNUNET_TESTING_NotifyConnection
3722 connect_callback, void *connect_callback_cls,
3723 const struct GNUNET_TESTING_Host *hostnames)
3725 struct GNUNET_TESTING_PeerGroup *pg;
3726 const struct GNUNET_TESTING_Host *hostpos;
3732 const char *hostname;
3733 const char *username;
3734 char *baseservicehome;
3735 char *newservicehome;
3737 struct GNUNET_CONFIGURATION_Handle *pcfg;
3739 unsigned int hostcnt;
3752 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
3755 pg->notify_connection = connect_callback;
3756 pg->notify_connection_cls = connect_callback_cls;
3758 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
3759 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
3760 if (NULL != hostnames)
3763 hostpos = hostnames;
3764 while (hostpos != NULL)
3766 hostpos = hostpos->next;
3769 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3772 hostpos = hostnames;
3773 while (hostpos != NULL)
3775 pg->hosts[off].minport = LOW_PORT;
3776 pg->hosts[off].hostname = GNUNET_strdup(hostpos->hostname);
3777 if (hostpos->username != NULL)
3778 pg->hosts[off].username = GNUNET_strdup(hostpos->username);
3779 pg->hosts[off].sshport = hostpos->port;
3780 hostpos = hostpos->next;
3790 pg->num_hosts = off;
3794 /* skip leading spaces */
3795 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
3798 while ('\0' != *rpos)
3800 if (isspace ( (unsigned char) *rpos))
3804 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3806 start = GNUNET_strdup (hostnames);
3808 while ('\0' != *pos)
3810 if (isspace ( (unsigned char) *pos))
3813 if (strlen (start) > 0)
3815 pg->hosts[off].minport = LOW_PORT;
3816 pg->hosts[off++].hostname = start;
3822 if (strlen (start) > 0)
3824 pg->hosts[off].minport = LOW_PORT;
3825 pg->hosts[off++].hostname = start;
3829 GNUNET_free (start);
3830 GNUNET_free (pg->hosts);
3834 minport = 0; /* make gcc happy */
3842 for (off = 0; off < total; off++)
3846 hostname = pg->hosts[off % hostcnt].hostname;
3847 username = pg->hosts[off % hostcnt].username;
3848 sshport = pg->hosts[off % hostcnt].sshport;
3849 pcfg = make_config (cfg,
3850 &pg->hosts[off % hostcnt].minport,
3859 pcfg = make_config (cfg,
3867 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3869 ("Could not create configuration for peer number %u on `%s'!\n"),
3870 off, hostname == NULL ? "localhost" : hostname);
3875 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
3878 GNUNET_asprintf (&newservicehome,
3879 "%s/%d/", baseservicehome, off);
3880 GNUNET_free (baseservicehome);
3884 tmpdir = getenv ("TMPDIR");
3885 tmpdir = tmpdir ? tmpdir : "/tmp";
3886 GNUNET_asprintf (&newservicehome,
3889 "gnunet-testing-test-test", off);
3891 GNUNET_CONFIGURATION_set_value_string (pcfg,
3893 "SERVICEHOME", newservicehome);
3894 GNUNET_free (newservicehome);
3895 pg->peers[off].cfg = pcfg;
3896 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
3897 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
3898 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
3899 pg->peers[off].pg = pg;
3901 pg->peers[off].internal_context.peer = &pg->peers[off];
3902 pg->peers[off].internal_context.timeout = timeout;
3903 pg->peers[off].internal_context.hostname = hostname;
3904 pg->peers[off].internal_context.username = username;
3905 pg->peers[off].internal_context.sshport = sshport;
3906 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
3907 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
3908 pg->peers[off].internal_context.start_cb = cb;
3909 pg->peers[off].internal_context.start_cb_cls = cb_cls;
3911 GNUNET_SCHEDULER_add_now (sched, &internal_start, &pg->peers[off].internal_context);
3918 * Get a daemon by number, so callers don't have to do nasty
3919 * offsetting operation.
3921 struct GNUNET_TESTING_Daemon *
3922 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
3924 if (position < pg->total)
3925 return pg->peers[position].daemon;
3931 * Get a daemon by peer identity, so callers can
3932 * retrieve the daemon without knowing it's offset.
3934 * @param pg the peer group to retrieve the daemon from
3935 * @param peer_id the peer identity of the daemon to retrieve
3937 * @return the daemon on success, or NULL if no such peer identity is found
3939 struct GNUNET_TESTING_Daemon *
3940 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_PeerIdentity *peer_id)
3944 for (i = 0; i < pg->total; i ++)
3946 if (0 == memcmp(&pg->peers[i].daemon->id, peer_id, sizeof(struct GNUNET_PeerIdentity)))
3947 return pg->peers[i].daemon;
3954 * Prototype of a function that will be called when a
3955 * particular operation was completed the testing library.
3957 * @param cls closure (a struct RestartContext)
3958 * @param id id of the peer that was restarted
3959 * @param cfg handle to the configuration of the peer
3960 * @param d handle to the daemon that was restarted
3961 * @param emsg NULL on success
3963 void restart_callback (void *cls,
3964 const struct GNUNET_PeerIdentity *id,
3965 const struct GNUNET_CONFIGURATION_Handle *cfg,
3966 struct GNUNET_TESTING_Daemon *d,
3969 struct RestartContext *restart_context = cls;
3973 restart_context->peers_restarted++;
3977 restart_context->peers_restart_failed++;
3980 if (restart_context->peers_restarted == restart_context->peer_group->total)
3982 restart_context->callback(restart_context->callback_cls, NULL);
3983 GNUNET_free(restart_context);
3985 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
3987 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
3988 GNUNET_free(restart_context);
3994 * Callback for informing us about a successful
3995 * or unsuccessful churn stop call.
3997 * @param cls a ChurnContext
3998 * @param emsg NULL on success, non-NULL on failure
4002 churn_stop_callback (void *cls, const char *emsg)
4004 struct ShutdownContext *shutdown_ctx = cls;
4005 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
4006 unsigned int total_left;
4007 char *error_message;
4009 error_message = NULL;
4010 shutdown_ctx->outstanding--;
4014 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
4015 "Churn stop callback failed with error `%s'\n", emsg);
4016 churn_ctx->num_failed_stop++;
4020 churn_ctx->num_to_stop--;
4024 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
4025 "Stopped peer, %d left.\n",
4026 churn_ctx->num_to_stop);
4028 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4030 if (total_left == 0)
4032 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
4034 GNUNET_asprintf(&error_message,
4035 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4036 churn_ctx->num_failed_start,
4037 churn_ctx->num_failed_stop);
4039 churn_ctx->cb(churn_ctx->cb_cls, error_message);
4040 GNUNET_free_non_null(error_message);
4041 GNUNET_free(churn_ctx);
4042 GNUNET_free(shutdown_ctx);
4047 * Count the number of running peers.
4049 * @param pg handle for the peer group
4051 * @return the number of currently running peers in the peer group
4054 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
4057 unsigned int running = 0;
4058 for (i = 0; i < pg->total; i++)
4060 if (pg->peers[i].daemon->running == GNUNET_YES)
4062 GNUNET_assert(running != -1);
4070 * Task to rate limit the number of outstanding peer shutdown
4071 * requests. This is necessary for making sure we don't do
4072 * too many ssh connections at once, but is generally nicer
4073 * to any system as well (graduated task starts, as opposed
4074 * to calling gnunet-arm N times all at once).
4077 schedule_churn_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
4079 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4080 struct ShutdownContext *shutdown_ctx;
4082 GNUNET_assert(peer_shutdown_ctx != NULL);
4083 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4084 GNUNET_assert(shutdown_ctx != NULL);
4086 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4087 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);
4090 shutdown_ctx->outstanding++;
4091 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, shutdown_ctx->cb, shutdown_ctx, GNUNET_NO, GNUNET_YES);
4092 GNUNET_free(peer_shutdown_ctx);
4097 * Simulate churn by stopping some peers (and possibly
4098 * re-starting others if churn is called multiple times). This
4099 * function can only be used to create leave-join churn (peers "never"
4100 * leave for good). First "voff" random peers that are currently
4101 * online will be taken offline; then "von" random peers that are then
4102 * offline will be put back online. No notifications will be
4103 * generated for any of these operations except for the callback upon
4106 * @param pg handle for the peer group
4107 * @param voff number of peers that should go offline
4108 * @param von number of peers that should come back online;
4109 * must be zero on first call (since "testbed_start"
4110 * always starts all of the peers)
4111 * @param timeout how long to wait for operations to finish before
4113 * @param cb function to call at the end
4114 * @param cb_cls closure for cb
4117 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
4120 struct GNUNET_TIME_Relative timeout,
4121 GNUNET_TESTING_NotifyCompletion cb,
4124 struct ChurnContext *churn_ctx;
4125 struct ShutdownContext *shutdown_ctx;
4126 struct PeerShutdownContext *peer_shutdown_ctx;
4127 struct PeerRestartContext *peer_restart_ctx;
4128 struct ChurnRestartContext *churn_startup_ctx;
4130 unsigned int running;
4131 unsigned int stopped;
4132 unsigned int total_running;
4133 unsigned int total_stopped;
4135 unsigned int *running_arr;
4136 unsigned int *stopped_arr;
4137 unsigned int *running_permute;
4138 unsigned int *stopped_permute;
4143 if ((von == 0) && (voff == 0)) /* No peers at all? */
4149 for (i = 0; i < pg->total; i++)
4151 if (pg->peers[i].daemon->running == GNUNET_YES)
4153 GNUNET_assert(running != -1);
4158 GNUNET_assert(stopped != -1);
4165 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
4166 cb(cb_cls, "Trying to stop more peers than are currently running!");
4172 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
4173 cb(cb_cls, "Trying to start more peers than are currently stopped!");
4177 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4181 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
4185 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
4187 running_permute = NULL;
4188 stopped_permute = NULL;
4191 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
4193 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
4195 total_running = running;
4196 total_stopped = stopped;
4200 churn_ctx->num_to_start = von;
4201 churn_ctx->num_to_stop = voff;
4203 churn_ctx->cb_cls = cb_cls;
4205 for (i = 0; i < pg->total; i++)
4207 if (pg->peers[i].daemon->running == GNUNET_YES)
4209 GNUNET_assert((running_arr != NULL) && (total_running > running));
4210 running_arr[running] = i;
4215 GNUNET_assert((stopped_arr != NULL) && (total_stopped > stopped));
4216 stopped_arr[stopped] = i;
4221 GNUNET_assert(running >= voff);
4224 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4225 shutdown_ctx->cb = &churn_stop_callback;
4226 shutdown_ctx->cb_cls = churn_ctx;
4227 shutdown_ctx->total_peers = voff;
4228 shutdown_ctx->timeout = timeout;
4231 for (i = 0; i < voff; i++)
4234 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
4236 GNUNET_assert(running_arr != NULL);
4237 peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4238 peer_shutdown_ctx->daemon = pg->peers[running_arr[running_permute[i]]].daemon;
4239 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4240 GNUNET_SCHEDULER_add_now(peer_shutdown_ctx->daemon->sched, &schedule_churn_shutdown_task, peer_shutdown_ctx);
4243 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
4245 &churn_stop_callback, churn_ctx,
4246 GNUNET_NO, GNUNET_YES); */
4249 GNUNET_assert(stopped >= von);
4252 churn_startup_ctx = GNUNET_malloc(sizeof(struct ChurnRestartContext));
4253 churn_startup_ctx->churn_ctx = churn_ctx;
4254 churn_startup_ctx->timeout = timeout;
4256 for (i = 0; i < von; i++)
4259 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
4261 GNUNET_assert(stopped_arr != NULL);
4262 peer_restart_ctx = GNUNET_malloc(sizeof(struct PeerRestartContext));
4263 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
4264 peer_restart_ctx->daemon = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
4265 GNUNET_SCHEDULER_add_now(peer_restart_ctx->daemon->sched, &schedule_churn_restart, peer_restart_ctx);
4267 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
4268 timeout, &churn_start_callback, churn_ctx);*/
4271 GNUNET_free_non_null(running_arr);
4272 GNUNET_free_non_null(stopped_arr);
4273 GNUNET_free_non_null(running_permute);
4274 GNUNET_free_non_null(stopped_permute);
4279 * Restart all peers in the given group.
4281 * @param pg the handle to the peer group
4282 * @param callback function to call on completion (or failure)
4283 * @param callback_cls closure for the callback function
4286 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
4287 GNUNET_TESTING_NotifyCompletion callback,
4290 struct RestartContext *restart_context;
4295 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
4296 restart_context->peer_group = pg;
4297 restart_context->peers_restarted = 0;
4298 restart_context->callback = callback;
4299 restart_context->callback_cls = callback_cls;
4301 for (off = 0; off < pg->total; off++)
4303 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
4309 * Start or stop an individual peer from the given group.
4311 * @param pg handle to the peer group
4312 * @param offset which peer to start or stop
4313 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
4314 * @param timeout how long to wait for shutdown
4315 * @param cb function to call at the end
4316 * @param cb_cls closure for cb
4319 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
4320 unsigned int offset,
4322 struct GNUNET_TIME_Relative timeout,
4323 GNUNET_TESTING_NotifyCompletion cb,
4326 struct ShutdownContext *shutdown_ctx;
4327 struct ChurnRestartContext *startup_ctx;
4328 struct ChurnContext *churn_ctx;
4330 if (GNUNET_NO == desired_status)
4332 if (NULL != pg->peers[offset].daemon)
4334 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4335 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4336 churn_ctx->num_to_start = 0;
4337 churn_ctx->num_to_stop = 1;
4339 churn_ctx->cb_cls = cb_cls;
4340 shutdown_ctx->cb_cls = churn_ctx;
4341 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
4342 timeout, &churn_stop_callback, shutdown_ctx,
4343 GNUNET_NO, GNUNET_YES);
4346 else if (GNUNET_YES == desired_status)
4348 if (NULL == pg->peers[offset].daemon)
4350 startup_ctx = GNUNET_malloc(sizeof(struct ChurnRestartContext));
4351 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4352 churn_ctx->num_to_start = 1;
4353 churn_ctx->num_to_stop = 0;
4355 churn_ctx->cb_cls = cb_cls;
4356 startup_ctx->churn_ctx = churn_ctx;
4357 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
4358 timeout, &churn_start_callback, startup_ctx);
4367 * Callback for shutting down peers in a peer group.
4369 * @param cls closure (struct ShutdownContext)
4370 * @param emsg NULL on success
4372 void internal_shutdown_callback (void *cls,
4375 struct ShutdownContext *shutdown_ctx = cls;
4377 shutdown_ctx->outstanding--;
4380 shutdown_ctx->peers_down++;
4384 shutdown_ctx->peers_failed++;
4387 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
4389 if (shutdown_ctx->peers_failed > 0)
4390 shutdown_ctx->cb(shutdown_ctx->cb_cls, "Not all peers successfully shut down!");
4392 shutdown_ctx->cb(shutdown_ctx->cb_cls, NULL);
4393 GNUNET_free(shutdown_ctx);
4399 * Task to rate limit the number of outstanding peer shutdown
4400 * requests. This is necessary for making sure we don't do
4401 * too many ssh connections at once, but is generally nicer
4402 * to any system as well (graduated task starts, as opposed
4403 * to calling gnunet-arm N times all at once).
4406 schedule_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
4408 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4409 struct ShutdownContext *shutdown_ctx;
4411 GNUNET_assert(peer_shutdown_ctx != NULL);
4412 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4413 GNUNET_assert(shutdown_ctx != NULL);
4415 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4416 GNUNET_SCHEDULER_add_delayed(peer_shutdown_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_shutdown_task, peer_shutdown_ctx);
4419 shutdown_ctx->outstanding++;
4420 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, &internal_shutdown_callback, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4421 GNUNET_free(peer_shutdown_ctx);
4426 * Shutdown all peers started in the given group.
4428 * @param pg handle to the peer group
4429 * @param timeout how long to wait for shutdown
4430 * @param cb callback to notify upon success or failure
4431 * @param cb_cls closure for cb
4434 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
4435 struct GNUNET_TIME_Relative timeout,
4436 GNUNET_TESTING_NotifyCompletion cb,
4440 struct ShutdownContext *shutdown_ctx;
4441 struct PeerShutdownContext *peer_shutdown_ctx;
4443 GNUNET_assert(pg->total > 0);
4445 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4446 shutdown_ctx->cb = cb;
4447 shutdown_ctx->cb_cls = cb_cls;
4448 shutdown_ctx->total_peers = pg->total;
4449 shutdown_ctx->timeout = timeout;
4450 /* shtudown_ctx->outstanding = 0; */
4452 for (off = 0; off < pg->total; off++)
4454 GNUNET_assert(NULL != pg->peers[off].daemon);
4455 peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4456 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
4457 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4458 GNUNET_SCHEDULER_add_now(pg->peers[off].daemon->sched, &schedule_shutdown_task, peer_shutdown_ctx);
4459 //GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4460 if (NULL != pg->peers[off].cfg)
4461 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
4462 if (pg->peers[off].allowed_peers != NULL)
4463 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
4464 if (pg->peers[off].connect_peers != NULL)
4465 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
4466 if (pg->peers[off].blacklisted_peers != NULL)
4467 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
4469 GNUNET_free (pg->peers);
4470 for (off = 0; off < pg->num_hosts; off++)
4472 GNUNET_free (pg->hosts[off].hostname);
4473 GNUNET_free_non_null (pg->hosts[off].username);
4475 GNUNET_free_non_null (pg->hosts);
4480 /* end of testing_group.c */