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 unsigned 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
484 * Configuration template.
486 const struct GNUNET_CONFIGURATION_Handle *cfg;
489 * Function to call on each started daemon.
491 //GNUNET_TESTING_NotifyDaemonRunning cb;
499 * Function to call on each topology connection created
501 GNUNET_TESTING_NotifyConnection notify_connection;
504 * Callback for notify_connection
506 void *notify_connection_cls;
509 * Array of information about hosts.
511 struct HostData *hosts;
514 * Number of hosts (size of HostData)
516 unsigned int num_hosts;
519 * Array of "total" peers.
521 struct PeerData *peers;
524 * Number of peers in this group.
529 * At what time should we fail the peer startup process?
531 struct GNUNET_TIME_Absolute max_timeout;
534 * How many peers are being started right now?
536 unsigned int starting;
539 * How many peers have already been started?
541 unsigned int started;
546 struct GNUNET_CONFIGURATION_Handle *ret;
547 const struct GNUNET_CONFIGURATION_Handle *orig;
548 const char *hostname;
554 struct ConnectTopologyContext
557 * How many connections are left to create.
559 unsigned int remaining_connections;
562 * Handle to group of peers.
564 struct GNUNET_TESTING_PeerGroup *pg;
567 * Temp value set for each iteration.
569 struct PeerData *first;
572 * Notification that all peers are connected.
574 GNUNET_TESTING_NotifyCompletion notify_connections_done;
577 * Closure for notify.
582 struct ConnectContext
585 * Peer to connect second to.
587 struct GNUNET_TESTING_Daemon *first;
590 * Peer to connect first to.
592 struct GNUNET_TESTING_Daemon *second;
595 * Higher level topology connection context.
597 struct ConnectTopologyContext *ct_ctx;
601 * Convert unique ID to hash code.
603 * @param uid unique ID to convert
604 * @param hash set to uid (extended with zeros)
607 hash_from_uid (uint32_t uid,
608 GNUNET_HashCode *hash)
610 memset (hash, 0, sizeof(GNUNET_HashCode));
611 *((uint32_t*)hash) = uid;
615 * Convert hash code to unique ID.
617 * @param uid unique ID to convert
618 * @param hash set to uid (extended with zeros)
621 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
623 memcpy (uid, hash, sizeof(uint32_t));
627 * Number of connects we are waiting on, allows us to rate limit
630 static int outstanding_connects;
633 * Get a topology from a string input.
635 * @param topology where to write the retrieved topology
636 * @param topology_string The string to attempt to
637 * get a configuration value from
638 * @return GNUNET_YES if topology string matched a
639 * known topology, GNUNET_NO if not
642 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, const char * topology_string)
645 * Strings representing topologies in enum
647 static const char * topology_strings[] =
650 * A clique (everyone connected to everyone else).
655 * Small-world network (2d torus plus random links).
660 * Small-world network (ring plus random links).
680 * Certain percentage of peers are unable to communicate directly
681 * replicating NAT conditions
686 * Scale free topology.
691 * Straight line topology.
696 * All peers are disconnected.
704 if (topology_string == NULL)
706 while (topology_strings[curr] != NULL)
708 if (strcasecmp(topology_strings[curr], topology_string) == 0)
715 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
721 * Get connect topology option from string input.
723 * @param topology_option where to write the retrieved topology
724 * @param topology_string The string to attempt to
725 * get a configuration value from
726 * @return GNUNET_YES if string matched a known
727 * topology option, GNUNET_NO if not
730 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption *topology_option,
731 const char * topology_string)
734 * Options for connecting a topology as strings.
736 static const char * topology_option_strings[] =
739 * Try to connect all peers specified in the topology.
744 * Choose a random subset of connections to create.
746 "CONNECT_RANDOM_SUBSET",
749 * Create at least X connections for each peer.
754 * Using a depth first search, create one connection
755 * per peer. If any are missed (graph disconnected)
756 * start over at those peers until all have at least one
762 * Find the N closest peers to each allowed peer in the
763 * topology and make sure a connection to those peers
764 * exists in the connect topology.
769 * No options specified.
777 if (topology_string == NULL)
779 while (NULL != topology_option_strings[curr])
781 if (strcasecmp(topology_option_strings[curr], topology_string) == 0)
783 *topology_option = curr;
788 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
793 * Function to iterate over options. Copies
794 * the options to the target configuration,
795 * updating PORT values as needed.
798 * @param section name of the section
799 * @param option name of the option
800 * @param value value of the option
803 update_config (void *cls,
804 const char *section, const char *option, const char *value)
806 struct UpdateContext *ctx = cls;
810 char *single_variable;
811 char *per_host_variable;
812 unsigned long long num_per_host;
814 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
816 GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
817 if ((ival != 0) && (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable)))
819 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
823 GNUNET_free(single_variable);
826 if (0 == strcmp (option, "UNIXPATH"))
828 GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
829 GNUNET_asprintf(&per_host_variable, "num_%s_per_host", section);
830 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable))
832 GNUNET_snprintf (uval,
834 "/tmp/test-service-%s-%u",
839 else if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_number(ctx->orig, "testing", per_host_variable, &num_per_host))
841 GNUNET_snprintf (uval,
843 "/tmp/test-service-%s-%u",
845 ctx->fdnum % num_per_host);
848 GNUNET_free(single_variable);
849 GNUNET_free(per_host_variable);
853 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
855 value = ctx->hostname;
858 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
863 * Create a new configuration using the given configuration
864 * as a template; however, each PORT in the existing cfg
865 * must be renumbered by incrementing "*port". If we run
866 * out of "*port" numbers, return NULL.
868 * @param cfg template configuration
869 * @param port port numbers to use, update to reflect
870 * port numbers that were used
871 * @param upnum number to make unix domain socket names unique
872 * @param hostname hostname of the controlling host, to allow control connections from
873 * @param fdnum number used to offset the unix domain socket for grouped processes
874 * (such as statistics or peerinfo, which can be shared among others)
876 * @return new configuration, NULL on error
878 static struct GNUNET_CONFIGURATION_Handle *
879 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
882 const char *hostname, uint32_t * fdnum)
884 struct UpdateContext uc;
893 uc.ret = GNUNET_CONFIGURATION_create ();
894 uc.hostname = hostname;
897 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
898 if (uc.nport >= HIGH_PORT)
901 GNUNET_CONFIGURATION_destroy (uc.ret);
905 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
907 if (hostname != NULL)
908 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1; %s;", control_host, hostname);
910 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
912 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
913 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport", "ACCEPT_FROM", allowed_hosts);
914 GNUNET_CONFIGURATION_set_value_string(uc.ret, "dht", "ACCEPT_FROM", allowed_hosts);
915 GNUNET_CONFIGURATION_set_value_string(uc.ret, "statistics", "ACCEPT_FROM", allowed_hosts);
916 GNUNET_free_non_null(control_host);
917 GNUNET_free(allowed_hosts);
921 /* arm needs to know to allow connections from the host on which it is running,
922 * otherwise gnunet-arm is unable to connect to it in some instances */
923 if (hostname != NULL)
925 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
926 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", hostname);
927 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", hostname);
928 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
929 GNUNET_free(allowed_hosts);
933 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", "127.0.0.1");
934 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", "127.0.0.1");
937 *port = (uint16_t) uc.nport;
946 * Add entries to the peers connect list
948 * @param pg the peer group we are working with
949 * @param first index of the first peer
950 * @param second index of the second peer
952 * @return the number of connections added
953 * technically should only be 0 or 2
957 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
963 GNUNET_HashCode hash_first;
964 GNUNET_HashCode hash_second;
966 hash_from_uid(first, &hash_first);
967 hash_from_uid(second, &hash_second);
969 add_first = GNUNET_NO;
970 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
972 add_first = GNUNET_YES;
975 add_second = GNUNET_NO;
976 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
978 add_second = GNUNET_YES;
984 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
985 pg->peers[first].num_connections++;
991 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
992 pg->peers[second].num_connections++;
1001 * Add entries to the peers allowed connections list
1003 * @param pg the peer group we are working with
1004 * @param first index of the first peer
1005 * @param second index of the second peer
1007 * @return the number of connections added (can be 0, 1 or 2)
1008 * technically should only be 0 or 2, but the small price
1009 * of iterating over the lists (hashmaps in the future)
1010 * for being sure doesn't bother me!
1014 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1018 struct PeerConnection *first_iter;
1019 struct PeerConnection *second_iter;
1020 struct PeerConnection *new_first;
1021 struct PeerConnection *new_second;
1026 GNUNET_HashCode hash_first;
1027 GNUNET_HashCode hash_second;
1029 hash_from_uid(first, &hash_first);
1030 hash_from_uid(second, &hash_second);
1032 add_first = GNUNET_NO;
1033 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
1035 add_first = GNUNET_YES;
1038 add_second = GNUNET_NO;
1039 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
1041 add_second = GNUNET_YES;
1044 first_iter = pg->peers[first].connected_peers;
1045 while (first_iter != NULL)
1047 if (first_iter->daemon == pg->peers[second].daemon)
1048 add_first = GNUNET_NO;
1049 first_iter = first_iter->next;
1052 second_iter = pg->peers[second].connected_peers;
1053 add_second = GNUNET_YES;
1054 while (second_iter != NULL)
1056 if (second_iter->daemon == pg->peers[first].daemon)
1057 add_second = GNUNET_NO;
1058 second_iter = second_iter->next;
1065 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1067 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
1068 new_first->daemon = pg->peers[second].daemon;
1069 new_first->next = pg->peers[first].connected_peers;
1070 pg->peers[first].connected_peers = new_first;
1072 pg->peers[first].num_connections++;
1078 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1080 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
1081 new_second->daemon = pg->peers[first].daemon;
1082 new_second->next = pg->peers[second].connected_peers;
1083 pg->peers[second].connected_peers = new_second;
1084 pg->peers[first].num_connections++;
1086 pg->peers[second].num_connections++;
1094 * Add entries to the peers blacklisted list
1096 * @param pg the peer group we are working with
1097 * @param first index of the first peer
1098 * @param second index of the second peer
1100 * @return the number of connections added (can be 0, 1 or 2)
1104 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1109 GNUNET_HashCode hash_first;
1110 GNUNET_HashCode hash_second;
1112 hash_from_uid(first, &hash_first);
1113 hash_from_uid(second, &hash_second);
1115 add_first = GNUNET_NO;
1116 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
1118 add_first = GNUNET_YES;
1121 add_second = GNUNET_NO;
1122 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
1124 add_second = GNUNET_YES;
1130 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1131 pg->peers[first].num_connections++;
1137 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1138 pg->peers[second].num_connections++;
1146 * Remove entries from the peers blacklisted list
1148 * @param pg the peer group we are working with
1149 * @param first index of the first peer
1150 * @param second index of the second peer
1152 * @return the number of connections removed (can be 0, 1 or 2)
1156 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1161 GNUNET_HashCode hash_first;
1162 GNUNET_HashCode hash_second;
1164 hash_from_uid(first, &hash_first);
1165 hash_from_uid(second, &hash_second);
1167 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
1168 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
1173 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
1179 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
1187 * Scale free network construction as described in:
1189 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1191 * Start with a network of "one" peer, then progressively add
1192 * peers up to the total number. At each step, iterate over
1193 * all possible peers and connect new peer based on number of
1194 * existing connections of the target peer.
1196 * @param pg the peer group we are dealing with
1197 * @param proc the connection processor to use
1199 * @return the number of connections created
1202 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1205 unsigned int total_connections;
1206 unsigned int outer_count;
1208 unsigned int previous_total_connections;
1212 GNUNET_assert(pg->total > 1);
1214 /* Add a connection between the first two nodes */
1215 total_connections = proc(pg, 0, 1);
1217 for (outer_count = 1; outer_count < pg->total; outer_count++)
1219 previous_total_connections = total_connections;
1220 for (i = 0; i < outer_count; i++)
1222 probability = pg->peers[i].num_connections / (double)previous_total_connections;
1223 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1224 UINT64_MAX)) / ( (double) UINT64_MAX);
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "Considering connecting peer %d to peer %d\n",
1230 if (random < probability)
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1234 "Connecting peer %d to peer %d\n",
1237 total_connections += proc(pg, outer_count, i);
1242 return total_connections;
1246 * Create a topology given a peer group (set of running peers)
1247 * and a connection processor.
1249 * @param pg the peergroup to create the topology on
1250 * @param proc the connection processor to call to actually set
1251 * up connections between two peers
1253 * @return the number of connections that were set up
1257 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1261 unsigned int natLog;
1262 unsigned int randomPeer;
1263 double random, logNModifier, percentage;
1264 unsigned int smallWorldConnections;
1269 unsigned int useAnd;
1270 int connect_attempts;
1272 logNModifier = 0.5; /* FIXME: default value? */
1273 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1278 if (sscanf(p_string, "%lf", &logNModifier) != 1)
1279 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1280 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1284 GNUNET_free (p_string);
1286 percentage = 0.5; /* FIXME: default percentage? */
1287 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1292 if (sscanf(p_string, "%lf", &percentage) != 1)
1293 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1294 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1298 GNUNET_free (p_string);
1300 natLog = log (pg->total);
1301 connsPerPeer = ceil (natLog * logNModifier);
1303 if (connsPerPeer % 2 == 1)
1306 smallWorldConnections = 0;
1307 connect_attempts = 0;
1308 for (i = 0; i < pg->total; i++)
1311 max = i + connsPerPeer / 2;
1312 min = i - connsPerPeer / 2;
1314 if (max > pg->total - 1)
1316 max = max - pg->total;
1322 min = pg->total - 1 + min;
1326 for (j = 0; j < connsPerPeer / 2; j++)
1328 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1329 UINT64_MAX) / ( (double) UINT64_MAX));
1330 if (random < percentage)
1332 /* Connect to uniformly selected random peer */
1334 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1336 while ((((randomPeer < max) && (randomPeer > min))
1337 && (useAnd == 0)) || (((randomPeer > min)
1338 || (randomPeer < max))
1342 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1345 smallWorldConnections +=
1346 proc (pg, i, randomPeer);
1350 nodeToConnect = i + j + 1;
1351 if (nodeToConnect > pg->total - 1)
1353 nodeToConnect = nodeToConnect - pg->total;
1356 proc (pg, i, nodeToConnect);
1362 connect_attempts += smallWorldConnections;
1364 return connect_attempts;
1368 * Create a topology given a peer group (set of running peers)
1369 * and a connection processor.
1371 * @param pg the peergroup to create the topology on
1372 * @param proc the connection processor to call to actually set
1373 * up connections between two peers
1375 * @return the number of connections that were set up
1379 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1381 unsigned int outer_count, inner_count;
1382 unsigned int cutoff;
1383 int connect_attempts;
1384 double nat_percentage;
1387 nat_percentage = 0.6; /* FIXME: default percentage? */
1388 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1393 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1394 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1395 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1399 GNUNET_free (p_string);
1404 cutoff = (unsigned int) (nat_percentage * pg->total);
1406 connect_attempts = 0;
1408 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1410 for (inner_count = outer_count + 1; inner_count < pg->total;
1413 if ((outer_count > cutoff) || (inner_count > cutoff))
1416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1417 "Connecting peer %d to peer %d\n",
1418 outer_count, inner_count);
1420 connect_attempts += proc(pg, outer_count, inner_count);
1425 return connect_attempts;
1430 * Create a topology given a peer group (set of running peers)
1431 * and a connection processor.
1433 * @param pg the peergroup to create the topology on
1434 * @param proc the connection processor to call to actually set
1435 * up connections between two peers
1437 * @return the number of connections that were set up
1441 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1443 unsigned int i, j, k;
1444 unsigned int square;
1447 unsigned int toggle = 1;
1448 unsigned int nodeToConnect;
1449 unsigned int natLog;
1450 unsigned int node1Row;
1451 unsigned int node1Col;
1452 unsigned int node2Row;
1453 unsigned int node2Col;
1454 unsigned int distance;
1455 double probability, random, percentage;
1456 unsigned int smallWorldConnections;
1457 unsigned int small_world_it;
1459 int connect_attempts;
1460 square = floor (sqrt (pg->total));
1464 percentage = 0.5; /* FIXME: default percentage? */
1465 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1470 if (sscanf(p_string, "%lf", &percentage) != 1)
1471 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1472 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1476 GNUNET_free (p_string);
1478 if (percentage < 0.0)
1480 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1481 _("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1482 "PERCENTAGE", "TESTING", percentage);
1485 probability = 0.5; /* FIXME: default percentage? */
1486 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1491 if (sscanf(p_string, "%lf", &probability) != 1)
1492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1493 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1497 GNUNET_free (p_string);
1499 if (square * square != pg->total)
1501 while (rows * cols < pg->total)
1503 if (toggle % 2 == 0)
1512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1513 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1517 connect_attempts = 0;
1518 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1519 * to the node to its right and above. Once this is over, we'll have our torus!
1520 * Special case for the last node (if the rows and columns are not equal), connect
1521 * to the first in the row to maintain topology.
1523 for (i = 0; i < pg->total; i++)
1525 /* First connect to the node to the right */
1526 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1527 nodeToConnect = i + 1;
1528 else if (i + 1 == pg->total)
1529 nodeToConnect = rows * cols - cols;
1531 nodeToConnect = i - cols + 1;
1533 connect_attempts += proc (pg, i, nodeToConnect);
1536 nodeToConnect = (rows * cols) - cols + i;
1538 nodeToConnect = i - cols;
1540 if (nodeToConnect < pg->total)
1541 connect_attempts += proc (pg, i, nodeToConnect);
1543 natLog = log (pg->total);
1544 #if VERBOSE_TESTING > 2
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1546 _("natural log of %d is %d, will run %d iterations\n"),
1547 pg->total, natLog, (int) (natLog * percentage));
1548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1550 smallWorldConnections = 0;
1551 small_world_it = (unsigned int)(natLog * percentage);
1552 if (small_world_it < 1)
1554 GNUNET_assert(small_world_it > 0 && small_world_it < (unsigned int)-1);
1555 for (i = 0; i < small_world_it; i++)
1557 for (j = 0; j < pg->total; j++)
1559 /* Determine the row and column of node at position j on the 2d torus */
1560 node1Row = j / cols;
1561 node1Col = j - (node1Row * cols);
1562 for (k = 0; k < pg->total; k++)
1564 /* Determine the row and column of node at position k on the 2d torus */
1565 node2Row = k / cols;
1566 node2Col = k - (node2Row * cols);
1567 /* Simple Cartesian distance */
1568 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1571 /* Calculate probability as 1 over the square of the distance */
1572 probability = 1.0 / (distance * distance);
1573 /* Choose a random value between 0 and 1 */
1574 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1575 UINT64_MAX)) / ( (double) UINT64_MAX);
1576 /* If random < probability, then connect the two nodes */
1577 if (random < probability)
1578 smallWorldConnections += proc (pg, j, k);
1584 connect_attempts += smallWorldConnections;
1585 #if VERBOSE_TESTING > 2
1586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1587 _("Total connections added for small world: %d!\n"),
1588 smallWorldConnections);
1590 return connect_attempts;
1594 * Create a topology given a peer group (set of running peers)
1595 * and a connection processor.
1597 * @param pg the peergroup to create the topology on
1598 * @param proc the connection processor to call to actually set
1599 * up connections between two peers
1601 * @return the number of connections that were set up
1605 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1608 unsigned int outer_count;
1609 unsigned int inner_count;
1610 int connect_attempts;
1614 probability = 0.5; /* FIXME: default percentage? */
1615 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1620 if (sscanf(p_string, "%lf", &probability) != 1)
1621 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1622 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1626 GNUNET_free (p_string);
1628 connect_attempts = 0;
1629 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1631 for (inner_count = outer_count + 1; inner_count < pg->total;
1634 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1635 UINT64_MAX)) / ( (double) UINT64_MAX);
1637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1638 _("rand is %f probability is %f\n"), temp_rand,
1641 if (temp_rand < probability)
1643 connect_attempts += proc (pg, outer_count, inner_count);
1648 return connect_attempts;
1652 * Create a topology given a peer group (set of running peers)
1653 * and a connection processor. This particular function creates
1654 * the connections for a 2d-torus, plus additional "closest"
1655 * connections per peer.
1657 * @param pg the peergroup to create the topology on
1658 * @param proc the connection processor to call to actually set
1659 * up connections between two peers
1661 * @return the number of connections that were set up
1665 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1668 unsigned int square;
1671 unsigned int toggle = 1;
1672 unsigned int nodeToConnect;
1673 int connect_attempts;
1675 connect_attempts = 0;
1677 square = floor (sqrt (pg->total));
1681 if (square * square != pg->total)
1683 while (rows * cols < pg->total)
1685 if (toggle % 2 == 0)
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1698 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1699 * to the node to its right and above. Once this is over, we'll have our torus!
1700 * Special case for the last node (if the rows and columns are not equal), connect
1701 * to the first in the row to maintain topology.
1703 for (i = 0; i < pg->total; i++)
1705 /* First connect to the node to the right */
1706 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1707 nodeToConnect = i + 1;
1708 else if (i + 1 == pg->total)
1709 nodeToConnect = rows * cols - cols;
1711 nodeToConnect = i - cols + 1;
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1714 "Connecting peer %d to peer %d\n",
1717 connect_attempts += proc(pg, i, nodeToConnect);
1719 /* Second connect to the node immediately above */
1721 nodeToConnect = (rows * cols) - cols + i;
1723 nodeToConnect = i - cols;
1725 if (nodeToConnect < pg->total)
1728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1729 "Connecting peer %d to peer %d\n",
1732 connect_attempts += proc(pg, i, nodeToConnect);
1737 return connect_attempts;
1742 * Create a topology given a peer group (set of running peers)
1743 * and a connection processor.
1745 * @param pg the peergroup to create the topology on
1746 * @param proc the connection processor to call to actually set
1747 * up connections between two peers
1749 * @return the number of connections that were set up
1753 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1755 unsigned int outer_count;
1756 unsigned int inner_count;
1757 int connect_attempts;
1759 connect_attempts = 0;
1761 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1763 for (inner_count = outer_count + 1; inner_count < pg->total;
1767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1768 "Connecting peer %d to peer %d\n",
1769 outer_count, inner_count);
1771 connect_attempts += proc(pg, outer_count, inner_count);
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_line (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 return connect_attempts;
1812 * Create a topology given a peer group (set of running peers)
1813 * and a connection processor.
1815 * @param pg the peergroup to create the topology on
1816 * @param proc the connection processor to call to actually set
1817 * up connections between two peers
1819 * @return the number of connections that were set up
1823 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1826 int connect_attempts;
1828 connect_attempts = 0;
1830 /* Connect each peer to the next highest numbered peer */
1831 for (count = 0; count < pg->total - 1; count++)
1834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1835 "Connecting peer %d to peer %d\n",
1838 connect_attempts += proc(pg, count, count + 1);
1841 /* Connect the last peer to the first peer */
1842 connect_attempts += proc(pg, pg->total - 1, 0);
1844 return connect_attempts;
1849 * Iterator for writing friends of a peer to a file.
1851 * @param cls closure, an open writable file handle
1852 * @param key the key the daemon was stored under
1853 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1855 * @return GNUNET_YES to continue iteration
1857 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1858 * with a single file_iterator that takes a closure which contains
1859 * the prefix to write before the peer. Then this could be used
1860 * for blacklisting multiple transports and writing the friend
1861 * file. I'm sure *someone* will complain loudly about other
1862 * things that negate these functions even existing so no point in
1866 friend_file_iterator (void *cls,
1867 const GNUNET_HashCode * key,
1870 FILE *temp_friend_handle = cls;
1871 struct GNUNET_TESTING_Daemon *peer = value;
1872 struct GNUNET_PeerIdentity *temppeer;
1873 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1875 temppeer = &peer->id;
1876 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1877 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1882 struct BlacklistContext
1885 * The (open) file handle to write to
1887 FILE *temp_file_handle;
1890 * The transport that this peer will be blacklisted on.
1896 * Iterator for writing blacklist data to appropriate files.
1898 * @param cls closure, an open writable file handle
1899 * @param key the key the daemon was stored under
1900 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1902 * @return GNUNET_YES to continue iteration
1905 blacklist_file_iterator (void *cls,
1906 const GNUNET_HashCode * key,
1909 struct BlacklistContext *blacklist_ctx = cls;
1910 //FILE *temp_blacklist_handle = cls;
1911 struct GNUNET_TESTING_Daemon *peer = value;
1912 struct GNUNET_PeerIdentity *temppeer;
1913 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1915 temppeer = &peer->id;
1916 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1917 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1923 * Create the friend files based on the PeerConnection's
1924 * of each peer in the peer group, and copy the files
1925 * to the appropriate place
1927 * @param pg the peer group we are dealing with
1930 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1932 FILE *temp_friend_handle;
1933 unsigned int pg_iter;
1934 char *temp_service_path;
1935 struct GNUNET_OS_Process **procarr;
1938 enum GNUNET_OS_ProcessStatusType type;
1939 unsigned long return_code;
1944 procarr = GNUNET_malloc(sizeof(struct GNUNET_OS_Process *) * pg->total);
1945 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1947 mytemp = GNUNET_DISK_mktemp("friends");
1948 GNUNET_assert(mytemp != NULL);
1949 temp_friend_handle = fopen (mytemp, "wt");
1950 GNUNET_assert(temp_friend_handle != NULL);
1951 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1952 fclose(temp_friend_handle);
1955 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1957 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1958 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1961 if (UNLINK (mytemp) != 0)
1962 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1963 GNUNET_free (mytemp);
1967 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1969 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1970 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1971 "mv", mytemp, arg, NULL);
1973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1974 _("Copying file with command cp %s %s\n"), mytemp, arg);
1979 else /* Remote, scp the file to the correct place */
1981 if (NULL != pg->peers[pg_iter].daemon->username)
1982 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1984 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1985 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1986 "scp", mytemp, arg, NULL);
1989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1990 _("Copying file with command scp %s %s\n"), mytemp, arg);
1994 GNUNET_free (temp_service_path);
1995 GNUNET_free (mytemp);
1999 ret = GNUNET_SYSERR;
2000 while ((count < max_wait) && (ret != GNUNET_OK))
2003 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2007 _("Checking copy status of file %d\n"), pg_iter);
2009 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2011 if (GNUNET_OS_process_status(procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2013 ret = GNUNET_SYSERR;
2015 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
2017 ret = GNUNET_SYSERR;
2021 GNUNET_OS_process_close (procarr[pg_iter]);
2022 procarr[pg_iter] = NULL;
2024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2025 _("File %d copied\n"), pg_iter);
2031 if (ret == GNUNET_SYSERR)
2033 /* FIXME: why sleep here? -CG */
2039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2040 _("Finished copying all friend files!\n"));
2042 GNUNET_free(procarr);
2048 * Create the blacklist files based on the PeerConnection's
2049 * of each peer in the peer group, and copy the files
2050 * to the appropriate place.
2052 * @param pg the peer group we are dealing with
2053 * @param transports space delimited list of transports to blacklist
2056 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, const char *transports)
2058 FILE *temp_file_handle;
2059 static struct BlacklistContext blacklist_ctx;
2060 unsigned int pg_iter;
2061 char *temp_service_path;
2062 struct GNUNET_OS_Process **procarr;
2065 enum GNUNET_OS_ProcessStatusType type;
2066 unsigned long return_code;
2073 char *temp_transports;
2075 procarr = GNUNET_malloc(sizeof(struct GNUNET_OS_Process *) * pg->total);
2076 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2078 mytemp = GNUNET_DISK_mktemp("blacklist");
2079 GNUNET_assert(mytemp != NULL);
2080 temp_file_handle = fopen (mytemp, "wt");
2081 GNUNET_assert(temp_file_handle != NULL);
2082 temp_transports = GNUNET_strdup(transports);
2083 blacklist_ctx.temp_file_handle = temp_file_handle;
2084 transport_len = strlen(temp_transports) + 1;
2087 for (i = 0; i < transport_len; i++)
2089 if ((temp_transports[i] == ' ') && (pos == NULL))
2090 continue; /* At start of string (whitespace) */
2091 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2093 temp_transports[i] = '\0';
2094 blacklist_ctx.transport = pos;
2095 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
2097 } /* At beginning of actual string */
2098 else if (pos == NULL)
2100 pos = &temp_transports[i];
2104 GNUNET_free (temp_transports);
2105 fclose(temp_file_handle);
2108 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
2110 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2111 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2114 if (UNLINK (mytemp) != 0)
2115 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
2116 GNUNET_free (mytemp);
2120 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2122 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2123 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2124 "mv", mytemp, arg, NULL);
2126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2127 _("Copying file with command cp %s %s\n"), mytemp, arg);
2132 else /* Remote, scp the file to the correct place */
2134 if (NULL != pg->peers[pg_iter].daemon->username)
2135 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
2137 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
2138 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
2139 "scp", mytemp, arg, NULL);
2142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2143 _("Copying file with command scp %s %s\n"), mytemp, arg);
2147 GNUNET_free (temp_service_path);
2148 GNUNET_free (mytemp);
2152 ret = GNUNET_SYSERR;
2153 while ((count < max_wait) && (ret != GNUNET_OK))
2156 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2160 _("Checking copy status of file %d\n"), pg_iter);
2162 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2164 if (GNUNET_OS_process_status(procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2166 ret = GNUNET_SYSERR;
2168 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
2170 ret = GNUNET_SYSERR;
2174 GNUNET_OS_process_close (procarr[pg_iter]);
2175 procarr[pg_iter] = NULL;
2177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2178 _("File %d copied\n"), pg_iter);
2184 if (ret == GNUNET_SYSERR)
2186 /* FIXME: why sleep here? -CG */
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2193 _("Finished copying all blacklist files!\n"));
2195 GNUNET_free(procarr);
2201 * Internal notification of a connection, kept so that we can ensure some connections
2202 * happen instead of flooding all testing daemons with requests to connect.
2204 static void internal_connect_notify (void *cls,
2205 const struct GNUNET_PeerIdentity *first,
2206 const struct GNUNET_PeerIdentity *second,
2208 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2209 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2210 struct GNUNET_TESTING_Daemon *first_daemon,
2211 struct GNUNET_TESTING_Daemon *second_daemon,
2214 struct ConnectTopologyContext *ct_ctx = cls;
2215 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
2216 outstanding_connects--;
2217 ct_ctx->remaining_connections--;
2218 if (ct_ctx->remaining_connections == 0)
2220 if (ct_ctx->notify_connections_done != NULL)
2221 ct_ctx->notify_connections_done(ct_ctx->notify_cls, NULL);
2222 GNUNET_free(ct_ctx);
2225 if (pg->notify_connection != NULL)
2226 pg->notify_connection (pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
2231 * Either delay a connection (because there are too many outstanding)
2232 * or schedule it for right now.
2234 * @param cls a connection context
2235 * @param tc the task runtime context
2237 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2239 struct ConnectContext *connect_context = cls;
2241 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2244 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2246 #if VERBOSE_TESTING > 2
2247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2248 _("Delaying connect, we have too many outstanding connections!\n"));
2250 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_connect, connect_context);
2254 #if VERBOSE_TESTING > 2
2255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2256 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
2258 outstanding_connects++;
2259 GNUNET_TESTING_daemons_connect (connect_context->first,
2260 connect_context->second,
2263 &internal_connect_notify,
2264 connect_context->ct_ctx);
2265 GNUNET_free(connect_context);
2271 * Iterator for actually scheduling connections to be created
2272 * between two peers.
2274 * @param cls closure, a GNUNET_TESTING_Daemon
2275 * @param key the key the second Daemon was stored under
2276 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2278 * @return GNUNET_YES to continue iteration
2281 connect_iterator (void *cls,
2282 const GNUNET_HashCode * key,
2285 struct ConnectTopologyContext *ct_ctx = cls;
2286 struct PeerData *first = ct_ctx->first;
2287 struct GNUNET_TESTING_Daemon *second = value;
2288 struct ConnectContext *connect_context;
2290 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2291 connect_context->first = first->daemon;
2292 connect_context->second = second;
2293 connect_context->ct_ctx = ct_ctx;
2294 GNUNET_SCHEDULER_add_now(&schedule_connect, connect_context);
2301 * Iterator for copying all entries in the allowed hashmap to the
2304 * @param cls closure, a GNUNET_TESTING_Daemon
2305 * @param key the key the second Daemon was stored under
2306 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2308 * @return GNUNET_YES to continue iteration
2311 copy_topology_iterator (void *cls,
2312 const GNUNET_HashCode * key,
2315 struct PeerData *first = cls;
2317 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2323 * Make the peers to connect the same as those that are allowed to be
2326 * @param pg the peer group
2329 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2331 unsigned int pg_iter;
2336 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2338 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
2339 if (GNUNET_SYSERR == ret)
2340 return GNUNET_SYSERR;
2342 total = total + ret;
2350 * Connect the topology as specified by the PeerConnection's
2351 * of each peer in the peer group
2353 * @param pg the peer group we are dealing with
2354 * @return the number of connections that will be attempted
2357 connect_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyCompletion notify_callback, void *notify_cls)
2359 unsigned int pg_iter;
2362 struct ConnectTopologyContext *ct_ctx;
2364 struct PeerConnection *connection_iter;
2365 struct ConnectContext *connect_context;
2369 ct_ctx = GNUNET_malloc(sizeof(struct ConnectTopologyContext));
2370 ct_ctx->notify_connections_done = notify_callback;
2371 ct_ctx->notify_cls = notify_cls;
2374 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2376 total += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2381 GNUNET_free(ct_ctx);
2384 ct_ctx->remaining_connections = total;
2387 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2389 ct_ctx->first = &pg->peers[pg_iter];
2390 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, ct_ctx);
2391 GNUNET_assert(GNUNET_SYSERR != ret && ret >= 0);
2392 total = total + ret;
2395 connection_iter = FIXME;
2396 while (connection_iter != NULL)
2398 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2399 connect_context->pg = pg;
2400 connect_context->first = FIXME;
2401 connect_context->second = connection_iter->daemon;
2402 GNUNET_SCHEDULER_add_now(&schedule_connect, connect_context);
2403 connection_iter = connection_iter->next;
2412 * Takes a peer group and creates a topology based on the
2413 * one specified. Creates a topology means generates friend
2414 * files for the peers so they can only connect to those allowed
2415 * by the topology. This will only have an effect once peers
2416 * are started if the FRIENDS_ONLY option is set in the base
2417 * config. Also takes an optional restrict topology which
2418 * disallows connections based on a particular transport
2419 * UNLESS they are specified in the restricted topology.
2421 * @param pg the peer group struct representing the running peers
2422 * @param topology which topology to connect the peers in
2423 * @param restrict_topology allow only direct TCP connections in this topology
2424 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2425 * @param restrict_transports space delimited list of transports to blacklist
2426 * to create restricted topology
2428 * @return the maximum number of connections were all allowed peers
2429 * connected to each other
2432 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2433 enum GNUNET_TESTING_Topology topology,
2434 enum GNUNET_TESTING_Topology restrict_topology,
2435 const char *restrict_transports)
2438 unsigned int num_connections;
2439 int unblacklisted_connections;
2443 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446 _("Creating clique topology\n"));
2448 num_connections = create_clique (pg, &add_allowed_connections);
2450 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2453 _("Creating small world (ring) topology\n"));
2455 num_connections = create_small_world_ring (pg, &add_allowed_connections);
2457 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2460 _("Creating small world (2d-torus) topology\n"));
2462 num_connections = create_small_world (pg, &add_allowed_connections);
2464 case GNUNET_TESTING_TOPOLOGY_RING:
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2467 _("Creating ring topology\n"));
2469 num_connections = create_ring (pg, &add_allowed_connections);
2471 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2474 _("Creating 2d torus topology\n"));
2476 num_connections = create_2d_torus (pg, &add_allowed_connections);
2478 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2481 _("Creating Erdos-Renyi topology\n"));
2483 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2485 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2488 _("Creating InterNAT topology\n"));
2490 num_connections = create_nated_internet (pg, &add_allowed_connections);
2492 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2495 _("Creating Scale Free topology\n"));
2497 num_connections = create_scale_free (pg, &add_allowed_connections);
2499 case GNUNET_TESTING_TOPOLOGY_LINE:
2501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2502 _("Creating straight line topology\n"));
2504 num_connections = create_line (pg, &add_allowed_connections);
2506 case GNUNET_TESTING_TOPOLOGY_NONE:
2508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2509 _("Creating no allowed topology (all peers can connect at core level)\n"));
2511 num_connections = 0;
2514 num_connections = 0;
2518 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2520 ret = create_and_copy_friend_files(pg);
2521 if (ret != GNUNET_OK)
2524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2525 _("Failed during friend file copying!\n"));
2527 return GNUNET_SYSERR;
2532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2533 _("Friend files created/copied successfully!\n"));
2538 /* Use the create clique method to initially set all connections as blacklisted. */
2539 if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2540 create_clique (pg, &blacklist_connections);
2542 unblacklisted_connections = 0;
2543 /* Un-blacklist connections as per the topology specified */
2544 switch (restrict_topology)
2546 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2549 _("Blacklisting all but clique topology\n"));
2551 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2553 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2556 _("Blacklisting all but small world (ring) topology\n"));
2558 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2560 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2563 _("Blacklisting all but small world (2d-torus) topology\n"));
2565 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2567 case GNUNET_TESTING_TOPOLOGY_RING:
2569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2570 _("Blacklisting all but ring topology\n"));
2572 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2574 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2577 _("Blacklisting all but 2d torus topology\n"));
2579 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2581 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2584 _("Blacklisting all but Erdos-Renyi topology\n"));
2586 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2588 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2591 _("Blacklisting all but InterNAT topology\n"));
2593 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2595 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2598 _("Blacklisting all but Scale Free topology\n"));
2600 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2602 case GNUNET_TESTING_TOPOLOGY_LINE:
2604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2605 _("Blacklisting all but straight line topology\n"));
2607 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2609 case GNUNET_TESTING_TOPOLOGY_NONE:
2611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2612 _("Creating no blacklist topology (all peers can connect at transport level)\n"));
2618 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2620 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2621 if (ret != GNUNET_OK)
2624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2625 _("Failed during blacklist file copying!\n"));
2632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2633 _("Blacklist files created/copied successfully!\n"));
2637 return num_connections;
2640 struct RandomContext
2645 struct GNUNET_TESTING_PeerGroup *pg;
2648 * uid of the first peer
2653 * Peer data for first peer.
2655 struct PeerData *first;
2658 * Random percentage to use
2663 struct MinimumContext
2668 struct GNUNET_TESTING_PeerGroup *pg;
2671 * uid of the first peer
2676 * Peer data for first peer.
2678 struct PeerData *first;
2681 * Number of conns per peer
2683 unsigned int num_to_add;
2686 * Permuted array of all possible connections. Only add the Nth
2687 * peer if it's in the Nth position.
2689 unsigned int *pg_array;
2692 * What number is the current element we are iterating over?
2694 unsigned int current;
2702 struct GNUNET_TESTING_PeerGroup *pg;
2705 * uid of the first peer
2710 * uid of the second peer
2712 uint32_t second_uid;
2715 * Peer data for first peer.
2717 struct PeerData *first;
2720 * Which peer has been chosen as the one to add?
2722 unsigned int chosen;
2725 * What number is the current element we are iterating over?
2727 unsigned int current;
2731 * Iterator for choosing random peers to connect.
2733 * @param cls closure, a RandomContext
2734 * @param key the key the second Daemon was stored under
2735 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2737 * @return GNUNET_YES to continue iteration
2740 random_connect_iterator (void *cls,
2741 const GNUNET_HashCode * key,
2744 struct RandomContext *random_ctx = cls;
2745 double random_number;
2746 uint32_t second_pos;
2747 GNUNET_HashCode first_hash;
2748 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2749 UINT64_MAX)) / ( (double) UINT64_MAX);
2750 if (random_number < random_ctx->percentage)
2752 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2754 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2755 uid_from_hash(key, &second_pos);
2756 hash_from_uid(random_ctx->first_uid, &first_hash);
2757 GNUNET_assert(random_ctx->pg->total > second_pos);
2758 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2764 * Iterator for adding at least X peers to a peers connection set.
2766 * @param cls closure, MinimumContext
2767 * @param key the key the second Daemon was stored under
2768 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2770 * @return GNUNET_YES to continue iteration
2773 minimum_connect_iterator (void *cls,
2774 const GNUNET_HashCode * key,
2777 struct MinimumContext *min_ctx = cls;
2778 uint32_t second_pos;
2779 GNUNET_HashCode first_hash;
2782 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2784 for (i = 0; i < min_ctx->num_to_add; i++)
2786 if (min_ctx->pg_array[i] == min_ctx->current)
2788 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2789 uid_from_hash(key, &second_pos);
2790 hash_from_uid(min_ctx->first_uid, &first_hash);
2791 GNUNET_assert(min_ctx->pg->total > second_pos);
2792 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));
2793 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2794 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2801 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2807 * Iterator for adding peers to a connection set based on a depth first search.
2809 * @param cls closure, MinimumContext
2810 * @param key the key the second daemon was stored under
2811 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2813 * @return GNUNET_YES to continue iteration
2816 dfs_connect_iterator (void *cls,
2817 const GNUNET_HashCode * key,
2820 struct DFSContext *dfs_ctx = cls;
2821 GNUNET_HashCode first_hash;
2823 if (dfs_ctx->current == dfs_ctx->chosen)
2825 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2826 uid_from_hash(key, &dfs_ctx->second_uid);
2827 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2828 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));
2829 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2830 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2831 return GNUNET_NO; /* We have found our peer, don't iterate more */
2840 * From the set of connections possible, choose percentage percent of connections
2841 * to actually connect.
2843 * @param pg the peergroup we are dealing with
2844 * @param percentage what percent of total connections to make
2847 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2849 struct RandomContext random_ctx;
2852 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2854 random_ctx.first_uid = pg_iter;
2855 random_ctx.first = &pg->peers[pg_iter];
2856 random_ctx.percentage = percentage;
2858 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2859 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2860 /* Now remove the old connections */
2861 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2862 /* And replace with the random set */
2863 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2868 * From the set of connections possible, choose at least num connections per
2871 * @param pg the peergroup we are dealing with
2872 * @param num how many connections at least should each peer have (if possible)?
2875 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2877 struct MinimumContext minimum_ctx;
2880 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2882 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2885 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2887 minimum_ctx.first_uid = pg_iter;
2888 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2889 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2890 minimum_ctx.first = &pg->peers[pg_iter];
2891 minimum_ctx.pg = pg;
2892 minimum_ctx.num_to_add = num;
2893 minimum_ctx.current = 0;
2894 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2895 &minimum_connect_iterator,
2899 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2901 /* Remove the "old" connections */
2902 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2903 /* And replace with the working set */
2904 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2911 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2914 unsigned int pg_iter;
2918 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2920 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2927 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2930 unsigned int pg_iter;
2934 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2936 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2943 struct FindClosestContext
2946 * The currently known closest peer.
2948 struct GNUNET_TESTING_Daemon *closest;
2951 * The info for the peer we are adding connections for.
2953 struct PeerData *curr_peer;
2956 * The distance (bits) between the current
2957 * peer and the currently known closest.
2959 unsigned int closest_dist;
2962 * The offset of the closest known peer in
2965 unsigned int closest_num;
2969 * Iterator over hash map entries of the allowed
2970 * peer connections. Find the closest, not already
2971 * connected peer and return it.
2973 * @param cls closure (struct FindClosestContext)
2974 * @param key current key code (hash of offset in pg)
2975 * @param value value in the hash map - a GNUNET_TESTING_Daemon
2976 * @return GNUNET_YES if we should continue to
2981 int find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
2983 struct FindClosestContext *closest_ctx = cls;
2984 struct GNUNET_TESTING_Daemon *daemon = value;
2986 if (((closest_ctx->closest == NULL) ||
2987 (GNUNET_CRYPTO_hash_matching_bits(&daemon->id.hashPubKey, &closest_ctx->curr_peer->daemon->id.hashPubKey) > closest_ctx->closest_dist))
2988 && (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains(closest_ctx->curr_peer->connect_peers, key)))
2990 closest_ctx->closest_dist = GNUNET_CRYPTO_hash_matching_bits(&daemon->id.hashPubKey, &closest_ctx->curr_peer->daemon->id.hashPubKey);
2991 closest_ctx->closest = daemon;
2992 uid_from_hash(key, &closest_ctx->closest_num);
2998 * From the set of connections possible, choose at num connections per
2999 * peer based on depth which are closest out of those allowed. Guaranteed
3000 * to add num peers to connect to, provided there are that many peers
3001 * in the underlay topology to connect to.
3003 * @param pg the peergroup we are dealing with
3004 * @param num how many connections at least should each peer have (if possible)?
3005 * @param proc processor to actually add the connections
3008 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num, GNUNET_TESTING_ConnectionProcessor proc)
3010 struct FindClosestContext closest_ctx;
3014 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
3016 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3018 closest_ctx.curr_peer = &pg->peers[pg_iter];
3019 closest_ctx.closest = NULL;
3020 closest_ctx.closest_dist = 0;
3021 closest_ctx.closest_num = 0;
3022 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &find_closest_peers, &closest_ctx);
3023 if (closest_ctx.closest != NULL)
3025 GNUNET_assert(closest_ctx.closest_num < pg->total);
3026 proc(pg, pg_iter, closest_ctx.closest_num);
3033 * From the set of connections possible, choose at least num connections per
3034 * peer based on depth first traversal of peer connections. If DFS leaves
3035 * peers unconnected, ensure those peers get connections.
3037 * @param pg the peergroup we are dealing with
3038 * @param num how many connections at least should each peer have (if possible)?
3041 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3043 struct DFSContext dfs_ctx;
3046 uint32_t starting_peer;
3047 uint32_t least_connections;
3048 GNUNET_HashCode second_hash;
3050 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3052 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
3057 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
3059 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3061 least_connections = -1; /* Set to very high number */
3062 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3064 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
3066 starting_peer = pg_iter;
3067 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
3072 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
3078 /* Choose a random peer from the chosen peers set of connections to add */
3079 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
3080 dfs_ctx.first_uid = starting_peer;
3081 dfs_ctx.first = &pg->peers[starting_peer];
3083 dfs_ctx.current = 0;
3085 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
3086 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
3087 hash_from_uid(dfs_ctx.second_uid, &second_hash);
3088 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
3089 starting_peer = dfs_ctx.second_uid;
3092 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3094 /* Remove the "old" connections */
3095 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
3096 /* And replace with the working set */
3097 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
3102 * Internal callback for topology information for a particular peer.
3105 internal_topology_callback(void *cls,
3106 const struct GNUNET_PeerIdentity *peer,
3107 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3109 struct CoreContext *core_ctx = cls;
3110 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
3112 if (peer == NULL) /* Either finished, or something went wrong */
3114 iter_ctx->completed++;
3115 iter_ctx->connected--;
3116 /* One core context allocated per iteration, must free! */
3117 GNUNET_free(core_ctx);
3121 iter_ctx->topology_cb(iter_ctx->cls, &core_ctx->daemon->id,
3125 if (iter_ctx->completed == iter_ctx->total)
3127 iter_ctx->topology_cb(iter_ctx->cls, NULL, NULL,
3129 /* Once all are done, free the iteration context */
3130 GNUNET_free(iter_ctx);
3136 * Check running topology iteration tasks, if below max start a new one, otherwise
3137 * schedule for some time in the future.
3140 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3142 struct CoreContext *core_context = cls;
3143 struct TopologyIterateContext *topology_context = (struct TopologyIterateContext *)core_context->iter_context;
3144 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3147 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3149 #if VERBOSE_TESTING > 2
3150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3151 _("Delaying connect, we have too many outstanding connections!\n"));
3153 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_topology, core_context);
3157 #if VERBOSE_TESTING > 2
3158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3159 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3161 topology_context->connected++;
3162 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->server, &internal_topology_callback, core_context))
3163 internal_topology_callback(core_context, NULL, NULL);
3169 * Iterate over all (running) peers in the peer group, retrieve
3170 * all connections that each currently has.
3173 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyTopology cb, void *cls)
3175 struct TopologyIterateContext *topology_context;
3176 struct CoreContext *core_ctx;
3178 unsigned int total_count;
3180 /* Allocate a single topology iteration context */
3181 topology_context = GNUNET_malloc(sizeof(struct TopologyIterateContext));
3182 topology_context->topology_cb = cb;
3183 topology_context->cls = cls;
3185 for (i = 0; i < pg->total; i++)
3187 if (pg->peers[i].daemon->running == GNUNET_YES)
3189 /* Allocate one core context per core we need to connect to */
3190 core_ctx = GNUNET_malloc(sizeof(struct CoreContext));
3191 core_ctx->daemon = pg->peers[i].daemon;
3192 /* Set back pointer to topology iteration context */
3193 core_ctx->iter_context = topology_context;
3194 GNUNET_SCHEDULER_add_now(&schedule_get_topology, core_ctx);
3198 if (total_count == 0)
3200 cb(cls, NULL, NULL, "Cannot iterate over topology, no running peers!");
3201 GNUNET_free(topology_context);
3204 topology_context->total = total_count;
3209 * Callback function to process statistic values.
3210 * This handler is here only really to insert a peer
3211 * identity (or daemon) so the statistics can be uniquely
3212 * tied to a single running peer.
3214 * @param cls closure
3215 * @param subsystem name of subsystem that created the statistic
3216 * @param name the name of the datum
3217 * @param value the current value
3218 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
3219 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
3221 static int internal_stats_callback (void *cls,
3222 const char *subsystem,
3227 struct StatsCoreContext *core_context = cls;
3228 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3230 return stats_context->proc(stats_context->cls, &core_context->daemon->id, subsystem, name, value, is_persistent);
3234 * Internal continuation call for statistics iteration.
3236 * @param cls closure, the CoreContext for this iteration
3237 * @param success whether or not the statistics iterations
3238 * was canceled or not (we don't care)
3240 static void internal_stats_cont (void *cls, int success)
3242 struct StatsCoreContext *core_context = cls;
3243 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3245 stats_context->connected--;
3246 stats_context->completed++;
3248 if (stats_context->completed == stats_context->total)
3250 stats_context->cont(stats_context->cls, GNUNET_YES);
3251 GNUNET_free(stats_context);
3254 if (core_context->stats_handle != NULL)
3255 GNUNET_STATISTICS_destroy(core_context->stats_handle, GNUNET_NO);
3257 GNUNET_free(core_context);
3261 * Check running topology iteration tasks, if below max start a new one, otherwise
3262 * schedule for some time in the future.
3265 schedule_get_statistics(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3267 struct StatsCoreContext *core_context = cls;
3268 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3270 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3273 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3275 #if VERBOSE_TESTING > 2
3276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3277 _("Delaying connect, we have too many outstanding connections!\n"));
3279 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_statistics, core_context);
3283 #if VERBOSE_TESTING > 2
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3285 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3288 stats_context->connected++;
3289 core_context->stats_handle = GNUNET_STATISTICS_create("testing", core_context->daemon->cfg);
3290 if (core_context->stats_handle == NULL)
3292 internal_stats_cont (core_context, GNUNET_NO);
3296 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);
3297 if (core_context->stats_get_handle == NULL)
3298 internal_stats_cont (core_context, GNUNET_NO);
3303 struct DuplicateStats
3306 * Next item in the list
3308 struct DuplicateStats *next;
3311 * Nasty string, concatenation of relevant information.
3313 char *unique_string;
3317 * Check whether the combination of port/host/unix domain socket
3318 * already exists in the list of peers being checked for statistics.
3320 * @param pg the peergroup in question
3321 * @param specific_peer the peer we're concerned with
3322 * @param stats_list the list to return to the caller
3324 * @return GNUNET_YES if the statistics instance has been seen already,
3325 * GNUNET_NO if not (and we may have added it to the list)
3328 stats_check_existing(struct GNUNET_TESTING_PeerGroup *pg, struct PeerData *specific_peer, struct DuplicateStats **stats_list)
3330 struct DuplicateStats *pos;
3331 char *unix_domain_socket;
3332 unsigned long long port;
3334 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(pg->cfg, "testing", "single_statistics_per_host"))
3335 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
3338 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(specific_peer->cfg, "statistics", "unixpath", &unix_domain_socket))
3341 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(specific_peer->cfg, "statistics", "port", &port))
3344 if (specific_peer->daemon->hostname != NULL)
3345 GNUNET_asprintf(&to_match, "%s%s%llu", specific_peer->daemon->hostname, unix_domain_socket, port);
3347 GNUNET_asprintf(&to_match, "%s%llu", unix_domain_socket, port);
3351 if (0 == strcmp(to_match, pos->unique_string))
3353 GNUNET_free (unix_domain_socket);
3354 GNUNET_free (to_match);
3359 pos = GNUNET_malloc(sizeof(struct DuplicateStats));
3360 pos->unique_string = to_match;
3361 pos->next = *stats_list;
3363 GNUNET_free (unix_domain_socket);
3368 * Iterate over all (running) peers in the peer group, retrieve
3369 * all statistics from each.
3372 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
3373 GNUNET_STATISTICS_Callback cont,
3374 GNUNET_TESTING_STATISTICS_Iterator proc, void *cls)
3376 struct StatsIterateContext *stats_context;
3377 struct StatsCoreContext *core_ctx;
3379 unsigned int total_count;
3380 struct DuplicateStats *stats_list;
3381 struct DuplicateStats *pos;
3384 /* Allocate a single stats iteration context */
3385 stats_context = GNUNET_malloc(sizeof(struct StatsIterateContext));
3386 stats_context->cont = cont;
3387 stats_context->proc = proc;
3388 stats_context->cls = cls;
3391 for (i = 0; i < pg->total; i++)
3393 if ((pg->peers[i].daemon->running == GNUNET_YES) && (GNUNET_NO == stats_check_existing(pg, &pg->peers[i], &stats_list)))
3395 /* Allocate one core context per core we need to connect to */
3396 core_ctx = GNUNET_malloc(sizeof(struct StatsCoreContext));
3397 core_ctx->daemon = pg->peers[i].daemon;
3398 /* Set back pointer to topology iteration context */
3399 core_ctx->iter_context = stats_context;
3400 GNUNET_SCHEDULER_add_now(&schedule_get_statistics, core_ctx);
3405 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Retrieving stats from %u total instances.\n", total_count);
3406 stats_context->total = total_count;
3407 if (stats_list != NULL)
3412 GNUNET_free(pos->unique_string);
3413 stats_list = pos->next;
3415 pos = stats_list->next;
3422 * There are many ways to connect peers that are supported by this function.
3423 * To connect peers in the same topology that was created via the
3424 * GNUNET_TESTING_create_topology, the topology variable must be set to
3425 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
3426 * a new instance of that topology will be generated and attempted to be
3427 * connected. This could result in some connections being impossible,
3428 * because some topologies are non-deterministic.
3430 * @param pg the peer group struct representing the running peers
3431 * @param topology which topology to connect the peers in
3432 * @param options options for connecting the topology
3433 * @param option_modifier modifier for options that take a parameter
3434 * @param notify_callback notification to be called once all connections completed
3435 * @param notify_cls closure for notification callback
3437 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
3440 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3441 enum GNUNET_TESTING_Topology topology,
3442 enum GNUNET_TESTING_TopologyOption options,
3443 double option_modifier,
3444 GNUNET_TESTING_NotifyCompletion notify_callback,
3449 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3450 #if VERBOSE_TOPOLOGY
3451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3452 _("Creating clique CONNECT topology\n"));
3454 create_clique (pg, &add_actual_connections);
3456 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3457 #if VERBOSE_TOPOLOGY
3458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3459 _("Creating small world (ring) CONNECT topology\n"));
3461 create_small_world_ring (pg, &add_actual_connections);
3463 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3464 #if VERBOSE_TOPOLOGY
3465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3466 _("Creating small world (2d-torus) CONNECT topology\n"));
3468 create_small_world (pg, &add_actual_connections);
3470 case GNUNET_TESTING_TOPOLOGY_RING:
3471 #if VERBOSE_TOPOLOGY
3472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3473 _("Creating ring CONNECT topology\n"));
3475 create_ring (pg, &add_actual_connections);
3477 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3478 #if VERBOSE_TOPOLOGY
3479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3480 _("Creating 2d torus CONNECT topology\n"));
3482 create_2d_torus (pg, &add_actual_connections);
3484 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3485 #if VERBOSE_TOPOLOGY
3486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3487 _("Creating Erdos-Renyi CONNECT topology\n"));
3489 create_erdos_renyi (pg, &add_actual_connections);
3491 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3492 #if VERBOSE_TOPOLOGY
3493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3494 _("Creating InterNAT CONNECT topology\n"));
3496 create_nated_internet (pg, &add_actual_connections);
3498 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3499 #if VERBOSE_TOPOLOGY
3500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3501 _("Creating Scale Free CONNECT topology\n"));
3503 create_scale_free (pg, &add_actual_connections);
3505 case GNUNET_TESTING_TOPOLOGY_LINE:
3506 #if VERBOSE_TOPOLOGY
3507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3508 _("Creating straight line CONNECT topology\n"));
3510 create_line (pg, &add_actual_connections);
3512 case GNUNET_TESTING_TOPOLOGY_NONE:
3513 #if VERBOSE_TOPOLOGY
3514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3515 _("Creating no CONNECT topology\n"));
3517 copy_allowed_topology(pg);
3520 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3521 _("Unknown topology specification, can't connect peers!\n"));
3522 return GNUNET_SYSERR;
3527 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
3528 #if VERBOSE_TOPOLOGY
3529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3530 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
3532 choose_random_connections(pg, option_modifier);
3534 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
3535 #if VERBOSE_TOPOLOGY
3536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3537 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3539 choose_minimum(pg, (unsigned int)option_modifier);
3541 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
3542 #if VERBOSE_TOPOLOGY
3543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3544 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3546 perform_dfs(pg, (int)option_modifier);
3548 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
3549 #if VERBOSE_TOPOLOGY
3550 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3551 _("Finding additional %u closest peers each (if possible)\n"), (unsigned int)option_modifier);
3553 add_closest(pg, (unsigned int)option_modifier, &add_actual_connections);
3555 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
3557 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
3563 return connect_topology(pg, notify_callback, notify_cls);
3567 * Callback that is called whenever a hostkey is generated
3568 * for a peer. Call the real callback and decrement the
3569 * starting counter for the peergroup.
3571 * @param cls closure
3572 * @param id identifier for the daemon, NULL on error
3573 * @param d handle for the daemon
3574 * @param emsg error message (NULL on success)
3576 static void internal_hostkey_callback (void *cls,
3577 const struct GNUNET_PeerIdentity *id,
3578 struct GNUNET_TESTING_Daemon *d,
3581 struct InternalStartContext *internal_context = cls;
3582 internal_context->peer->pg->starting--;
3583 internal_context->peer->pg->started++;
3584 if (internal_context->hostkey_callback != NULL)
3585 internal_context->hostkey_callback(internal_context->hostkey_cls, id, d, emsg);
3586 else if (internal_context->peer->pg->started == internal_context->peer->pg->total)
3588 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
3589 GNUNET_TESTING_daemons_continue_startup(internal_context->peer->pg);
3594 * Callback that is called whenever a peer has finished starting.
3595 * Call the real callback and decrement the starting counter
3596 * for the peergroup.
3598 * @param cls closure
3599 * @param id identifier for the daemon, NULL on error
3600 * @param d handle for the daemon
3601 * @param emsg error message (NULL on success)
3603 static void internal_startup_callback (void *cls,
3604 const struct GNUNET_PeerIdentity *id,
3605 const struct GNUNET_CONFIGURATION_Handle *cfg,
3606 struct GNUNET_TESTING_Daemon *d,
3609 struct InternalStartContext *internal_context = cls;
3610 internal_context->peer->pg->starting--;
3611 if (internal_context->start_cb != NULL)
3612 internal_context->start_cb(internal_context->start_cb_cls, id, cfg, d, emsg);
3616 internal_continue_startup (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3618 struct InternalStartContext *internal_context = cls;
3620 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3625 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
3627 internal_context->peer->pg->starting++;
3628 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
3632 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_continue_startup, internal_context);
3638 * Callback for informing us about a successful
3639 * or unsuccessful churn start call.
3641 * @param cls a ChurnContext
3642 * @param id the peer identity of the started peer
3643 * @param cfg the handle to the configuration of the peer
3644 * @param d handle to the daemon for the peer
3645 * @param emsg NULL on success, non-NULL on failure
3649 churn_start_callback (void *cls,
3650 const struct GNUNET_PeerIdentity *id,
3651 const struct GNUNET_CONFIGURATION_Handle *cfg,
3652 struct GNUNET_TESTING_Daemon *d,
3655 struct ChurnRestartContext *startup_ctx = cls;
3656 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
3658 unsigned int total_left;
3659 char *error_message;
3661 error_message = NULL;
3664 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3665 "Churn stop callback failed with error `%s'\n",
3667 churn_ctx->num_failed_start++;
3671 churn_ctx->num_to_start--;
3675 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3676 "Started peer, %d left.\n",
3677 churn_ctx->num_to_start);
3680 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3682 if (total_left == 0)
3684 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3685 GNUNET_asprintf(&error_message,
3686 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3687 churn_ctx->num_failed_start,
3688 churn_ctx->num_failed_stop);
3689 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3690 GNUNET_free_non_null(error_message);
3691 GNUNET_free(churn_ctx);
3692 GNUNET_free(startup_ctx);
3697 static void schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3699 struct PeerRestartContext *peer_restart_ctx = cls;
3700 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
3702 if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
3703 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_churn_restart, peer_restart_ctx);
3706 GNUNET_TESTING_daemon_start_stopped(peer_restart_ctx->daemon,
3707 startup_ctx->timeout,
3708 &churn_start_callback,
3710 GNUNET_free(peer_restart_ctx);
3715 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3717 struct InternalStartContext *internal_context = cls;
3719 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3724 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
3726 internal_context->peer->pg->starting++;
3727 internal_context->peer->daemon = GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
3728 internal_context->timeout,
3729 internal_context->hostname,
3730 internal_context->username,
3731 internal_context->sshport,
3732 &internal_hostkey_callback,
3734 &internal_startup_callback,
3739 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_start, internal_context);
3744 * Function which continues a peer group starting up
3745 * after successfully generating hostkeys for each peer.
3747 * @param pg the peer group to continue starting
3751 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
3756 for (i = 0; i < pg->total; i++)
3758 GNUNET_SCHEDULER_add_now (&internal_continue_startup, &pg->peers[i].internal_context);
3759 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
3764 * Start count gnunet instances with the same set of transports and
3765 * applications. The port numbers (any option called "PORT") will be
3766 * adjusted to ensure that no two peers running on the same system
3767 * have the same port(s) in their respective configurations.
3769 * @param cfg configuration template to use
3770 * @param total number of daemons to start
3771 * @param timeout total time allowed for peers to start
3772 * @param hostkey_callback function to call on each peers hostkey generation
3773 * if NULL, peers will be started by this call, if non-null,
3774 * GNUNET_TESTING_daemons_continue_startup must be called after
3775 * successful hostkey generation
3776 * @param hostkey_cls closure for hostkey callback
3777 * @param cb function to call on each daemon that was started
3778 * @param cb_cls closure for cb
3779 * @param connect_callback function to call each time two hosts are connected
3780 * @param connect_callback_cls closure for connect_callback
3781 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
3783 * @return NULL on error, otherwise handle to control peer group
3785 struct GNUNET_TESTING_PeerGroup *
3786 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
3788 struct GNUNET_TIME_Relative timeout,
3789 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
3791 GNUNET_TESTING_NotifyDaemonRunning cb,
3793 GNUNET_TESTING_NotifyConnection
3794 connect_callback, void *connect_callback_cls,
3795 const struct GNUNET_TESTING_Host *hostnames)
3797 struct GNUNET_TESTING_PeerGroup *pg;
3798 const struct GNUNET_TESTING_Host *hostpos;
3804 const char *hostname;
3805 const char *username;
3806 char *baseservicehome;
3807 char *newservicehome;
3809 struct GNUNET_CONFIGURATION_Handle *pcfg;
3811 unsigned int hostcnt;
3824 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
3826 pg->notify_connection = connect_callback;
3827 pg->notify_connection_cls = connect_callback_cls;
3829 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
3830 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
3831 if (NULL != hostnames)
3834 hostpos = hostnames;
3835 while (hostpos != NULL)
3837 hostpos = hostpos->next;
3840 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3843 hostpos = hostnames;
3844 while (hostpos != NULL)
3846 pg->hosts[off].minport = LOW_PORT;
3847 pg->hosts[off].hostname = GNUNET_strdup(hostpos->hostname);
3848 if (hostpos->username != NULL)
3849 pg->hosts[off].username = GNUNET_strdup(hostpos->username);
3850 pg->hosts[off].sshport = hostpos->port;
3851 hostpos = hostpos->next;
3861 pg->num_hosts = off;
3865 /* skip leading spaces */
3866 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
3869 while ('\0' != *rpos)
3871 if (isspace ( (unsigned char) *rpos))
3875 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3877 start = GNUNET_strdup (hostnames);
3879 while ('\0' != *pos)
3881 if (isspace ( (unsigned char) *pos))
3884 if (strlen (start) > 0)
3886 pg->hosts[off].minport = LOW_PORT;
3887 pg->hosts[off++].hostname = start;
3893 if (strlen (start) > 0)
3895 pg->hosts[off].minport = LOW_PORT;
3896 pg->hosts[off++].hostname = start;
3900 GNUNET_free (start);
3901 GNUNET_free (pg->hosts);
3905 minport = 0; /* make gcc happy */
3913 for (off = 0; off < total; off++)
3917 hostname = pg->hosts[off % hostcnt].hostname;
3918 username = pg->hosts[off % hostcnt].username;
3919 sshport = pg->hosts[off % hostcnt].sshport;
3920 pcfg = make_config (cfg,
3921 &pg->hosts[off % hostcnt].minport,
3930 pcfg = make_config (cfg,
3938 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3940 ("Could not create configuration for peer number %u on `%s'!\n"),
3941 off, hostname == NULL ? "localhost" : hostname);
3946 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
3949 GNUNET_asprintf (&newservicehome,
3950 "%s/%d/", baseservicehome, off);
3951 GNUNET_free (baseservicehome);
3955 tmpdir = getenv ("TMPDIR");
3956 tmpdir = tmpdir ? tmpdir : "/tmp";
3957 GNUNET_asprintf (&newservicehome,
3960 "gnunet-testing-test-test", off);
3962 GNUNET_CONFIGURATION_set_value_string (pcfg,
3964 "SERVICEHOME", newservicehome);
3965 GNUNET_free (newservicehome);
3966 pg->peers[off].cfg = pcfg;
3967 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
3968 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
3969 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
3970 pg->peers[off].pg = pg;
3972 pg->peers[off].internal_context.peer = &pg->peers[off];
3973 pg->peers[off].internal_context.timeout = timeout;
3974 pg->peers[off].internal_context.hostname = hostname;
3975 pg->peers[off].internal_context.username = username;
3976 pg->peers[off].internal_context.sshport = sshport;
3977 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
3978 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
3979 pg->peers[off].internal_context.start_cb = cb;
3980 pg->peers[off].internal_context.start_cb_cls = cb_cls;
3982 GNUNET_SCHEDULER_add_now (&internal_start, &pg->peers[off].internal_context);
3989 * Get a daemon by number, so callers don't have to do nasty
3990 * offsetting operation.
3992 struct GNUNET_TESTING_Daemon *
3993 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
3995 if (position < pg->total)
3996 return pg->peers[position].daemon;
4002 * Get a daemon by peer identity, so callers can
4003 * retrieve the daemon without knowing it's offset.
4005 * @param pg the peer group to retrieve the daemon from
4006 * @param peer_id the peer identity of the daemon to retrieve
4008 * @return the daemon on success, or NULL if no such peer identity is found
4010 struct GNUNET_TESTING_Daemon *
4011 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_PeerIdentity *peer_id)
4015 for (i = 0; i < pg->total; i ++)
4017 if (0 == memcmp(&pg->peers[i].daemon->id, peer_id, sizeof(struct GNUNET_PeerIdentity)))
4018 return pg->peers[i].daemon;
4025 * Prototype of a function that will be called when a
4026 * particular operation was completed the testing library.
4028 * @param cls closure (a struct RestartContext)
4029 * @param id id of the peer that was restarted
4030 * @param cfg handle to the configuration of the peer
4031 * @param d handle to the daemon that was restarted
4032 * @param emsg NULL on success
4034 void restart_callback (void *cls,
4035 const struct GNUNET_PeerIdentity *id,
4036 const struct GNUNET_CONFIGURATION_Handle *cfg,
4037 struct GNUNET_TESTING_Daemon *d,
4040 struct RestartContext *restart_context = cls;
4044 restart_context->peers_restarted++;
4048 restart_context->peers_restart_failed++;
4051 if (restart_context->peers_restarted == restart_context->peer_group->total)
4053 restart_context->callback(restart_context->callback_cls, NULL);
4054 GNUNET_free(restart_context);
4056 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
4058 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
4059 GNUNET_free(restart_context);
4065 * Callback for informing us about a successful
4066 * or unsuccessful churn stop call.
4068 * @param cls a ChurnContext
4069 * @param emsg NULL on success, non-NULL on failure
4073 churn_stop_callback (void *cls, const char *emsg)
4075 struct ShutdownContext *shutdown_ctx = cls;
4076 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
4077 unsigned int total_left;
4078 char *error_message;
4080 error_message = NULL;
4081 shutdown_ctx->outstanding--;
4085 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
4086 "Churn stop callback failed with error `%s'\n", emsg);
4087 churn_ctx->num_failed_stop++;
4091 churn_ctx->num_to_stop--;
4095 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
4096 "Stopped peer, %d left.\n",
4097 churn_ctx->num_to_stop);
4099 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4101 if (total_left == 0)
4103 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
4105 GNUNET_asprintf(&error_message,
4106 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4107 churn_ctx->num_failed_start,
4108 churn_ctx->num_failed_stop);
4110 churn_ctx->cb(churn_ctx->cb_cls, error_message);
4111 GNUNET_free_non_null(error_message);
4112 GNUNET_free(churn_ctx);
4113 GNUNET_free(shutdown_ctx);
4118 * Count the number of running peers.
4120 * @param pg handle for the peer group
4122 * @return the number of currently running peers in the peer group
4125 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
4128 unsigned int running = 0;
4129 for (i = 0; i < pg->total; i++)
4131 if (pg->peers[i].daemon->running == GNUNET_YES)
4133 GNUNET_assert(running != -1);
4141 * Task to rate limit the number of outstanding peer shutdown
4142 * requests. This is necessary for making sure we don't do
4143 * too many ssh connections at once, but is generally nicer
4144 * to any system as well (graduated task starts, as opposed
4145 * to calling gnunet-arm N times all at once).
4148 schedule_churn_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
4150 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4151 struct ShutdownContext *shutdown_ctx;
4153 GNUNET_assert(peer_shutdown_ctx != NULL);
4154 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4155 GNUNET_assert(shutdown_ctx != NULL);
4157 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4158 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_churn_shutdown_task, peer_shutdown_ctx);
4161 shutdown_ctx->outstanding++;
4162 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, shutdown_ctx->cb, shutdown_ctx, GNUNET_NO, GNUNET_YES);
4163 GNUNET_free(peer_shutdown_ctx);
4168 * Simulate churn by stopping some peers (and possibly
4169 * re-starting others if churn is called multiple times). This
4170 * function can only be used to create leave-join churn (peers "never"
4171 * leave for good). First "voff" random peers that are currently
4172 * online will be taken offline; then "von" random peers that are then
4173 * offline will be put back online. No notifications will be
4174 * generated for any of these operations except for the callback upon
4177 * @param pg handle for the peer group
4178 * @param voff number of peers that should go offline
4179 * @param von number of peers that should come back online;
4180 * must be zero on first call (since "testbed_start"
4181 * always starts all of the peers)
4182 * @param timeout how long to wait for operations to finish before
4184 * @param cb function to call at the end
4185 * @param cb_cls closure for cb
4188 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
4191 struct GNUNET_TIME_Relative timeout,
4192 GNUNET_TESTING_NotifyCompletion cb,
4195 struct ChurnContext *churn_ctx;
4196 struct ShutdownContext *shutdown_ctx;
4197 struct PeerShutdownContext *peer_shutdown_ctx;
4198 struct PeerRestartContext *peer_restart_ctx;
4199 struct ChurnRestartContext *churn_startup_ctx;
4201 unsigned int running;
4202 unsigned int stopped;
4203 unsigned int total_running;
4204 unsigned int total_stopped;
4206 unsigned int *running_arr;
4207 unsigned int *stopped_arr;
4208 unsigned int *running_permute;
4209 unsigned int *stopped_permute;
4214 if ((von == 0) && (voff == 0)) /* No peers at all? */
4220 for (i = 0; i < pg->total; i++)
4222 if (pg->peers[i].daemon->running == GNUNET_YES)
4224 GNUNET_assert(running != -1);
4229 GNUNET_assert(stopped != -1);
4236 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
4237 cb(cb_cls, "Trying to stop more peers than are currently running!");
4243 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
4244 cb(cb_cls, "Trying to start more peers than are currently stopped!");
4248 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4252 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
4256 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
4258 running_permute = NULL;
4259 stopped_permute = NULL;
4262 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
4264 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
4266 total_running = running;
4267 total_stopped = stopped;
4271 churn_ctx->num_to_start = von;
4272 churn_ctx->num_to_stop = voff;
4274 churn_ctx->cb_cls = cb_cls;
4276 for (i = 0; i < pg->total; i++)
4278 if (pg->peers[i].daemon->running == GNUNET_YES)
4280 GNUNET_assert((running_arr != NULL) && (total_running > running));
4281 running_arr[running] = i;
4286 GNUNET_assert((stopped_arr != NULL) && (total_stopped > stopped));
4287 stopped_arr[stopped] = i;
4292 GNUNET_assert(running >= voff);
4295 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4296 shutdown_ctx->cb = &churn_stop_callback;
4297 shutdown_ctx->cb_cls = churn_ctx;
4298 shutdown_ctx->total_peers = voff;
4299 shutdown_ctx->timeout = timeout;
4302 for (i = 0; i < voff; i++)
4305 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
4307 GNUNET_assert(running_arr != NULL);
4308 peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4309 peer_shutdown_ctx->daemon = pg->peers[running_arr[running_permute[i]]].daemon;
4310 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4311 GNUNET_SCHEDULER_add_now(&schedule_churn_shutdown_task, peer_shutdown_ctx);
4314 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
4316 &churn_stop_callback, churn_ctx,
4317 GNUNET_NO, GNUNET_YES); */
4320 GNUNET_assert(stopped >= von);
4323 churn_startup_ctx = GNUNET_malloc(sizeof(struct ChurnRestartContext));
4324 churn_startup_ctx->churn_ctx = churn_ctx;
4325 churn_startup_ctx->timeout = timeout;
4327 for (i = 0; i < von; i++)
4330 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
4332 GNUNET_assert(stopped_arr != NULL);
4333 peer_restart_ctx = GNUNET_malloc(sizeof(struct PeerRestartContext));
4334 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
4335 peer_restart_ctx->daemon = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
4336 GNUNET_SCHEDULER_add_now(&schedule_churn_restart, peer_restart_ctx);
4338 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
4339 timeout, &churn_start_callback, churn_ctx);*/
4342 GNUNET_free_non_null(running_arr);
4343 GNUNET_free_non_null(stopped_arr);
4344 GNUNET_free_non_null(running_permute);
4345 GNUNET_free_non_null(stopped_permute);
4350 * Restart all peers in the given group.
4352 * @param pg the handle to the peer group
4353 * @param callback function to call on completion (or failure)
4354 * @param callback_cls closure for the callback function
4357 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
4358 GNUNET_TESTING_NotifyCompletion callback,
4361 struct RestartContext *restart_context;
4366 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
4367 restart_context->peer_group = pg;
4368 restart_context->peers_restarted = 0;
4369 restart_context->callback = callback;
4370 restart_context->callback_cls = callback_cls;
4372 for (off = 0; off < pg->total; off++)
4374 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
4380 * Start or stop an individual peer from the given group.
4382 * @param pg handle to the peer group
4383 * @param offset which peer to start or stop
4384 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
4385 * @param timeout how long to wait for shutdown
4386 * @param cb function to call at the end
4387 * @param cb_cls closure for cb
4390 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
4391 unsigned int offset,
4393 struct GNUNET_TIME_Relative timeout,
4394 GNUNET_TESTING_NotifyCompletion cb,
4397 struct ShutdownContext *shutdown_ctx;
4398 struct ChurnRestartContext *startup_ctx;
4399 struct ChurnContext *churn_ctx;
4401 if (GNUNET_NO == desired_status)
4403 if (NULL != pg->peers[offset].daemon)
4405 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4406 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4407 churn_ctx->num_to_start = 0;
4408 churn_ctx->num_to_stop = 1;
4410 churn_ctx->cb_cls = cb_cls;
4411 shutdown_ctx->cb_cls = churn_ctx;
4412 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
4413 timeout, &churn_stop_callback, shutdown_ctx,
4414 GNUNET_NO, GNUNET_YES);
4417 else if (GNUNET_YES == desired_status)
4419 if (NULL == pg->peers[offset].daemon)
4421 startup_ctx = GNUNET_malloc(sizeof(struct ChurnRestartContext));
4422 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4423 churn_ctx->num_to_start = 1;
4424 churn_ctx->num_to_stop = 0;
4426 churn_ctx->cb_cls = cb_cls;
4427 startup_ctx->churn_ctx = churn_ctx;
4428 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
4429 timeout, &churn_start_callback, startup_ctx);
4438 * Callback for shutting down peers in a peer group.
4440 * @param cls closure (struct ShutdownContext)
4441 * @param emsg NULL on success
4443 void internal_shutdown_callback (void *cls,
4446 struct ShutdownContext *shutdown_ctx = cls;
4448 shutdown_ctx->outstanding--;
4451 shutdown_ctx->peers_down++;
4455 shutdown_ctx->peers_failed++;
4458 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
4460 if (shutdown_ctx->peers_failed > 0)
4461 shutdown_ctx->cb(shutdown_ctx->cb_cls, "Not all peers successfully shut down!");
4463 shutdown_ctx->cb(shutdown_ctx->cb_cls, NULL);
4464 GNUNET_free(shutdown_ctx);
4470 * Task to rate limit the number of outstanding peer shutdown
4471 * requests. This is necessary for making sure we don't do
4472 * too many ssh connections at once, but is generally nicer
4473 * to any system as well (graduated task starts, as opposed
4474 * to calling gnunet-arm N times all at once).
4477 schedule_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
4479 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4480 struct ShutdownContext *shutdown_ctx;
4482 GNUNET_assert(peer_shutdown_ctx != NULL);
4483 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4484 GNUNET_assert(shutdown_ctx != NULL);
4486 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4487 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_shutdown_task, peer_shutdown_ctx);
4490 shutdown_ctx->outstanding++;
4491 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, &internal_shutdown_callback, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4492 GNUNET_free(peer_shutdown_ctx);
4497 * Shutdown all peers started in the given group.
4499 * @param pg handle to the peer group
4500 * @param timeout how long to wait for shutdown
4501 * @param cb callback to notify upon success or failure
4502 * @param cb_cls closure for cb
4505 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
4506 struct GNUNET_TIME_Relative timeout,
4507 GNUNET_TESTING_NotifyCompletion cb,
4511 struct ShutdownContext *shutdown_ctx;
4512 struct PeerShutdownContext *peer_shutdown_ctx;
4514 GNUNET_assert(pg->total > 0);
4516 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4517 shutdown_ctx->cb = cb;
4518 shutdown_ctx->cb_cls = cb_cls;
4519 shutdown_ctx->total_peers = pg->total;
4520 shutdown_ctx->timeout = timeout;
4521 /* shtudown_ctx->outstanding = 0; */
4523 for (off = 0; off < pg->total; off++)
4525 GNUNET_assert(NULL != pg->peers[off].daemon);
4526 peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4527 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
4528 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4529 GNUNET_SCHEDULER_add_now(&schedule_shutdown_task, peer_shutdown_ctx);
4530 //GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4531 if (NULL != pg->peers[off].cfg)
4532 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
4533 if (pg->peers[off].allowed_peers != NULL)
4534 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
4535 if (pg->peers[off].connect_peers != NULL)
4536 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
4537 if (pg->peers[off].blacklisted_peers != NULL)
4538 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
4540 GNUNET_free (pg->peers);
4541 for (off = 0; off < pg->num_hosts; off++)
4543 GNUNET_free (pg->hosts[off].hostname);
4544 GNUNET_free_non_null (pg->hosts[off].username);
4546 GNUNET_free_non_null (pg->hosts);
4551 /* end of testing_group.c */