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 (num_connections < 0)
2519 return GNUNET_SYSERR;
2521 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2523 ret = create_and_copy_friend_files(pg);
2524 if (ret != GNUNET_OK)
2527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2528 _("Failed during friend file copying!\n"));
2530 return GNUNET_SYSERR;
2535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2536 _("Friend files created/copied successfully!\n"));
2541 /* Use the create clique method to initially set all connections as blacklisted. */
2542 if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2543 create_clique (pg, &blacklist_connections);
2545 unblacklisted_connections = 0;
2546 /* Un-blacklist connections as per the topology specified */
2547 switch (restrict_topology)
2549 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2552 _("Blacklisting all but clique topology\n"));
2554 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2556 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2559 _("Blacklisting all but small world (ring) topology\n"));
2561 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2563 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2566 _("Blacklisting all but small world (2d-torus) topology\n"));
2568 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2570 case GNUNET_TESTING_TOPOLOGY_RING:
2572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2573 _("Blacklisting all but ring topology\n"));
2575 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2577 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2580 _("Blacklisting all but 2d torus topology\n"));
2582 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2584 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2587 _("Blacklisting all but Erdos-Renyi topology\n"));
2589 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2591 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2594 _("Blacklisting all but InterNAT topology\n"));
2596 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2598 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2601 _("Blacklisting all but Scale Free topology\n"));
2603 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2605 case GNUNET_TESTING_TOPOLOGY_LINE:
2607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2608 _("Blacklisting all but straight line topology\n"));
2610 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2612 case GNUNET_TESTING_TOPOLOGY_NONE:
2614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2615 _("Creating no blacklist topology (all peers can connect at transport level)\n"));
2621 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2623 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2624 if (ret != GNUNET_OK)
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2628 _("Failed during blacklist file copying!\n"));
2635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2636 _("Blacklist files created/copied successfully!\n"));
2640 return num_connections;
2643 struct RandomContext
2648 struct GNUNET_TESTING_PeerGroup *pg;
2651 * uid of the first peer
2656 * Peer data for first peer.
2658 struct PeerData *first;
2661 * Random percentage to use
2666 struct MinimumContext
2671 struct GNUNET_TESTING_PeerGroup *pg;
2674 * uid of the first peer
2679 * Peer data for first peer.
2681 struct PeerData *first;
2684 * Number of conns per peer
2686 unsigned int num_to_add;
2689 * Permuted array of all possible connections. Only add the Nth
2690 * peer if it's in the Nth position.
2692 unsigned int *pg_array;
2695 * What number is the current element we are iterating over?
2697 unsigned int current;
2705 struct GNUNET_TESTING_PeerGroup *pg;
2708 * uid of the first peer
2713 * uid of the second peer
2715 uint32_t second_uid;
2718 * Peer data for first peer.
2720 struct PeerData *first;
2723 * Which peer has been chosen as the one to add?
2725 unsigned int chosen;
2728 * What number is the current element we are iterating over?
2730 unsigned int current;
2734 * Iterator for choosing random peers to connect.
2736 * @param cls closure, a RandomContext
2737 * @param key the key the second Daemon was stored under
2738 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2740 * @return GNUNET_YES to continue iteration
2743 random_connect_iterator (void *cls,
2744 const GNUNET_HashCode * key,
2747 struct RandomContext *random_ctx = cls;
2748 double random_number;
2749 uint32_t second_pos;
2750 GNUNET_HashCode first_hash;
2751 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2752 UINT64_MAX)) / ( (double) UINT64_MAX);
2753 if (random_number < random_ctx->percentage)
2755 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2757 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2758 uid_from_hash(key, &second_pos);
2759 hash_from_uid(random_ctx->first_uid, &first_hash);
2760 GNUNET_assert(random_ctx->pg->total > second_pos);
2761 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2767 * Iterator for adding at least X peers to a peers connection set.
2769 * @param cls closure, MinimumContext
2770 * @param key the key the second Daemon was stored under
2771 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2773 * @return GNUNET_YES to continue iteration
2776 minimum_connect_iterator (void *cls,
2777 const GNUNET_HashCode * key,
2780 struct MinimumContext *min_ctx = cls;
2781 uint32_t second_pos;
2782 GNUNET_HashCode first_hash;
2785 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2787 for (i = 0; i < min_ctx->num_to_add; i++)
2789 if (min_ctx->pg_array[i] == min_ctx->current)
2791 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2792 uid_from_hash(key, &second_pos);
2793 hash_from_uid(min_ctx->first_uid, &first_hash);
2794 GNUNET_assert(min_ctx->pg->total > second_pos);
2795 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));
2796 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2797 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2804 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2810 * Iterator for adding peers to a connection set based on a depth first search.
2812 * @param cls closure, MinimumContext
2813 * @param key the key the second daemon was stored under
2814 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2816 * @return GNUNET_YES to continue iteration
2819 dfs_connect_iterator (void *cls,
2820 const GNUNET_HashCode * key,
2823 struct DFSContext *dfs_ctx = cls;
2824 GNUNET_HashCode first_hash;
2826 if (dfs_ctx->current == dfs_ctx->chosen)
2828 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2829 uid_from_hash(key, &dfs_ctx->second_uid);
2830 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2831 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));
2832 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2833 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2834 return GNUNET_NO; /* We have found our peer, don't iterate more */
2843 * From the set of connections possible, choose percentage percent of connections
2844 * to actually connect.
2846 * @param pg the peergroup we are dealing with
2847 * @param percentage what percent of total connections to make
2850 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2852 struct RandomContext random_ctx;
2855 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2857 random_ctx.first_uid = pg_iter;
2858 random_ctx.first = &pg->peers[pg_iter];
2859 random_ctx.percentage = percentage;
2861 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2862 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2863 /* Now remove the old connections */
2864 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2865 /* And replace with the random set */
2866 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2871 * From the set of connections possible, choose at least num connections per
2874 * @param pg the peergroup we are dealing with
2875 * @param num how many connections at least should each peer have (if possible)?
2878 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2880 struct MinimumContext minimum_ctx;
2883 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2885 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2888 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2890 minimum_ctx.first_uid = pg_iter;
2891 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2892 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2893 minimum_ctx.first = &pg->peers[pg_iter];
2894 minimum_ctx.pg = pg;
2895 minimum_ctx.num_to_add = num;
2896 minimum_ctx.current = 0;
2897 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2898 &minimum_connect_iterator,
2902 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2904 /* Remove the "old" connections */
2905 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2906 /* And replace with the working set */
2907 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2914 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2917 unsigned int pg_iter;
2921 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2923 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2930 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2933 unsigned int pg_iter;
2937 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2939 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2946 struct FindClosestContext
2949 * The currently known closest peer.
2951 struct GNUNET_TESTING_Daemon *closest;
2954 * The info for the peer we are adding connections for.
2956 struct PeerData *curr_peer;
2959 * The distance (bits) between the current
2960 * peer and the currently known closest.
2962 unsigned int closest_dist;
2965 * The offset of the closest known peer in
2968 unsigned int closest_num;
2972 * Iterator over hash map entries of the allowed
2973 * peer connections. Find the closest, not already
2974 * connected peer and return it.
2976 * @param cls closure (struct FindClosestContext)
2977 * @param key current key code (hash of offset in pg)
2978 * @param value value in the hash map - a GNUNET_TESTING_Daemon
2979 * @return GNUNET_YES if we should continue to
2984 int find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
2986 struct FindClosestContext *closest_ctx = cls;
2987 struct GNUNET_TESTING_Daemon *daemon = value;
2989 if (((closest_ctx->closest == NULL) ||
2990 (GNUNET_CRYPTO_hash_matching_bits(&daemon->id.hashPubKey, &closest_ctx->curr_peer->daemon->id.hashPubKey) > closest_ctx->closest_dist))
2991 && (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains(closest_ctx->curr_peer->connect_peers, key)))
2993 closest_ctx->closest_dist = GNUNET_CRYPTO_hash_matching_bits(&daemon->id.hashPubKey, &closest_ctx->curr_peer->daemon->id.hashPubKey);
2994 closest_ctx->closest = daemon;
2995 uid_from_hash(key, &closest_ctx->closest_num);
3001 * From the set of connections possible, choose at num connections per
3002 * peer based on depth which are closest out of those allowed. Guaranteed
3003 * to add num peers to connect to, provided there are that many peers
3004 * in the underlay topology to connect to.
3006 * @param pg the peergroup we are dealing with
3007 * @param num how many connections at least should each peer have (if possible)?
3008 * @param proc processor to actually add the connections
3011 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num, GNUNET_TESTING_ConnectionProcessor proc)
3013 struct FindClosestContext closest_ctx;
3017 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
3019 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3021 closest_ctx.curr_peer = &pg->peers[pg_iter];
3022 closest_ctx.closest = NULL;
3023 closest_ctx.closest_dist = 0;
3024 closest_ctx.closest_num = 0;
3025 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &find_closest_peers, &closest_ctx);
3026 if (closest_ctx.closest != NULL)
3028 GNUNET_assert((0 <= closest_ctx.closest_num) && (closest_ctx.closest_num < pg->total));
3029 proc(pg, pg_iter, closest_ctx.closest_num);
3036 * From the set of connections possible, choose at least num connections per
3037 * peer based on depth first traversal of peer connections. If DFS leaves
3038 * peers unconnected, ensure those peers get connections.
3040 * @param pg the peergroup we are dealing with
3041 * @param num how many connections at least should each peer have (if possible)?
3044 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3046 struct DFSContext dfs_ctx;
3049 uint32_t starting_peer;
3050 uint32_t least_connections;
3051 GNUNET_HashCode second_hash;
3053 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3055 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
3060 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
3062 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3064 least_connections = -1; /* Set to very high number */
3065 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3067 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
3069 starting_peer = pg_iter;
3070 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
3075 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
3081 /* Choose a random peer from the chosen peers set of connections to add */
3082 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
3083 dfs_ctx.first_uid = starting_peer;
3084 dfs_ctx.first = &pg->peers[starting_peer];
3086 dfs_ctx.current = 0;
3088 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
3089 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
3090 hash_from_uid(dfs_ctx.second_uid, &second_hash);
3091 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
3092 starting_peer = dfs_ctx.second_uid;
3095 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3097 /* Remove the "old" connections */
3098 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
3099 /* And replace with the working set */
3100 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
3105 * Internal callback for topology information for a particular peer.
3108 internal_topology_callback(void *cls,
3109 const struct GNUNET_PeerIdentity *peer,
3110 struct GNUNET_TIME_Relative latency, uint32_t distance)
3112 struct CoreContext *core_ctx = cls;
3113 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
3115 if (peer == NULL) /* Either finished, or something went wrong */
3117 iter_ctx->completed++;
3118 iter_ctx->connected--;
3119 /* One core context allocated per iteration, must free! */
3120 GNUNET_free(core_ctx);
3124 iter_ctx->topology_cb(iter_ctx->cls, &core_ctx->daemon->id, peer, latency, distance, NULL);
3127 if (iter_ctx->completed == iter_ctx->total)
3129 iter_ctx->topology_cb(iter_ctx->cls, NULL, NULL, GNUNET_TIME_relative_get_zero(), 0, NULL);
3130 /* Once all are done, free the iteration context */
3131 GNUNET_free(iter_ctx);
3137 * Check running topology iteration tasks, if below max start a new one, otherwise
3138 * schedule for some time in the future.
3141 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3143 struct CoreContext *core_context = cls;
3144 struct TopologyIterateContext *topology_context = (struct TopologyIterateContext *)core_context->iter_context;
3145 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3148 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3150 #if VERBOSE_TESTING > 2
3151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3152 _("Delaying connect, we have too many outstanding connections!\n"));
3154 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_topology, core_context);
3158 #if VERBOSE_TESTING > 2
3159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3160 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3162 topology_context->connected++;
3163 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->cfg, &internal_topology_callback, core_context))
3164 internal_topology_callback(core_context, NULL, GNUNET_TIME_relative_get_zero(), 0);
3170 * Iterate over all (running) peers in the peer group, retrieve
3171 * all connections that each currently has.
3174 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyTopology cb, void *cls)
3176 struct TopologyIterateContext *topology_context;
3177 struct CoreContext *core_ctx;
3179 unsigned int total_count;
3181 /* Allocate a single topology iteration context */
3182 topology_context = GNUNET_malloc(sizeof(struct TopologyIterateContext));
3183 topology_context->topology_cb = cb;
3184 topology_context->cls = cls;
3186 for (i = 0; i < pg->total; i++)
3188 if (pg->peers[i].daemon->running == GNUNET_YES)
3190 /* Allocate one core context per core we need to connect to */
3191 core_ctx = GNUNET_malloc(sizeof(struct CoreContext));
3192 core_ctx->daemon = pg->peers[i].daemon;
3193 /* Set back pointer to topology iteration context */
3194 core_ctx->iter_context = topology_context;
3195 GNUNET_SCHEDULER_add_now(&schedule_get_topology, core_ctx);
3199 topology_context->total = total_count;
3204 * Callback function to process statistic values.
3205 * This handler is here only really to insert a peer
3206 * identity (or daemon) so the statistics can be uniquely
3207 * tied to a single running peer.
3209 * @param cls closure
3210 * @param subsystem name of subsystem that created the statistic
3211 * @param name the name of the datum
3212 * @param value the current value
3213 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
3214 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
3216 static int internal_stats_callback (void *cls,
3217 const char *subsystem,
3222 struct StatsCoreContext *core_context = cls;
3223 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3225 return stats_context->proc(stats_context->cls, &core_context->daemon->id, subsystem, name, value, is_persistent);
3229 * Internal continuation call for statistics iteration.
3231 * @param cls closure, the CoreContext for this iteration
3232 * @param success whether or not the statistics iterations
3233 * was canceled or not (we don't care)
3235 static void internal_stats_cont (void *cls, int success)
3237 struct StatsCoreContext *core_context = cls;
3238 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3240 stats_context->connected--;
3241 stats_context->completed++;
3243 if (stats_context->completed == stats_context->total)
3245 stats_context->cont(stats_context->cls, GNUNET_YES);
3246 GNUNET_free(stats_context);
3249 if (core_context->stats_handle != NULL)
3250 GNUNET_STATISTICS_destroy(core_context->stats_handle, GNUNET_NO);
3252 GNUNET_free(core_context);
3256 * Check running topology iteration tasks, if below max start a new one, otherwise
3257 * schedule for some time in the future.
3260 schedule_get_statistics(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3262 struct StatsCoreContext *core_context = cls;
3263 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3265 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3268 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3270 #if VERBOSE_TESTING > 2
3271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3272 _("Delaying connect, we have too many outstanding connections!\n"));
3274 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_statistics, core_context);
3278 #if VERBOSE_TESTING > 2
3279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3280 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3283 stats_context->connected++;
3284 core_context->stats_handle = GNUNET_STATISTICS_create("testing", core_context->daemon->cfg);
3285 if (core_context->stats_handle == NULL)
3287 internal_stats_cont (core_context, GNUNET_NO);
3291 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);
3292 if (core_context->stats_get_handle == NULL)
3293 internal_stats_cont (core_context, GNUNET_NO);
3298 struct DuplicateStats
3301 * Next item in the list
3303 struct DuplicateStats *next;
3306 * Nasty string, concatenation of relevant information.
3308 char *unique_string;
3312 * Check whether the combination of port/host/unix domain socket
3313 * already exists in the list of peers being checked for statistics.
3315 * @param pg the peergroup in question
3316 * @param specific_peer the peer we're concerned with
3317 * @param stats_list the list to return to the caller
3319 * @return GNUNET_YES if the statistics instance has been seen already,
3320 * GNUNET_NO if not (and we may have added it to the list)
3323 stats_check_existing(struct GNUNET_TESTING_PeerGroup *pg, struct PeerData *specific_peer, struct DuplicateStats **stats_list)
3325 struct DuplicateStats *pos;
3326 char *unix_domain_socket;
3327 unsigned long long port;
3329 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(pg->cfg, "testing", "single_statistics_per_host"))
3330 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
3333 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(specific_peer->cfg, "statistics", "unixpath", &unix_domain_socket))
3336 GNUNET_CONFIGURATION_get_value_number(specific_peer->cfg, "statistics", "port", &port);
3338 if (specific_peer->daemon->hostname != NULL)
3339 GNUNET_asprintf(&to_match, "%s%s%llu", specific_peer->daemon->hostname, unix_domain_socket, port);
3341 GNUNET_asprintf(&to_match, "%s%llu", unix_domain_socket, port);
3345 if (0 == strcmp(to_match, pos->unique_string))
3347 GNUNET_free(to_match);
3352 pos = GNUNET_malloc(sizeof(struct DuplicateStats));
3353 pos->unique_string = to_match;
3354 pos->next = *stats_list;
3360 * Iterate over all (running) peers in the peer group, retrieve
3361 * all statistics from each.
3364 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
3365 GNUNET_STATISTICS_Callback cont,
3366 GNUNET_TESTING_STATISTICS_Iterator proc, void *cls)
3368 struct StatsIterateContext *stats_context;
3369 struct StatsCoreContext *core_ctx;
3371 unsigned int total_count;
3372 struct DuplicateStats *stats_list;
3373 struct DuplicateStats *pos;
3376 /* Allocate a single stats iteration context */
3377 stats_context = GNUNET_malloc(sizeof(struct StatsIterateContext));
3378 stats_context->cont = cont;
3379 stats_context->proc = proc;
3380 stats_context->cls = cls;
3383 for (i = 0; i < pg->total; i++)
3385 if ((pg->peers[i].daemon->running == GNUNET_YES) && (GNUNET_NO == stats_check_existing(pg, &pg->peers[i], &stats_list)))
3387 /* Allocate one core context per core we need to connect to */
3388 core_ctx = GNUNET_malloc(sizeof(struct StatsCoreContext));
3389 core_ctx->daemon = pg->peers[i].daemon;
3390 /* Set back pointer to topology iteration context */
3391 core_ctx->iter_context = stats_context;
3392 GNUNET_SCHEDULER_add_now(&schedule_get_statistics, core_ctx);
3397 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Retrieving stats from %u total instances.\n", total_count);
3398 stats_context->total = total_count;
3399 if (stats_list != NULL)
3404 GNUNET_free(pos->unique_string);
3405 stats_list = pos->next;
3407 pos = stats_list->next;
3414 * There are many ways to connect peers that are supported by this function.
3415 * To connect peers in the same topology that was created via the
3416 * GNUNET_TESTING_create_topology, the topology variable must be set to
3417 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
3418 * a new instance of that topology will be generated and attempted to be
3419 * connected. This could result in some connections being impossible,
3420 * because some topologies are non-deterministic.
3422 * @param pg the peer group struct representing the running peers
3423 * @param topology which topology to connect the peers in
3424 * @param options options for connecting the topology
3425 * @param option_modifier modifier for options that take a parameter
3426 * @param notify_callback notification to be called once all connections completed
3427 * @param notify_cls closure for notification callback
3429 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
3432 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3433 enum GNUNET_TESTING_Topology topology,
3434 enum GNUNET_TESTING_TopologyOption options,
3435 double option_modifier,
3436 GNUNET_TESTING_NotifyCompletion notify_callback,
3441 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3442 #if VERBOSE_TOPOLOGY
3443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3444 _("Creating clique CONNECT topology\n"));
3446 create_clique (pg, &add_actual_connections);
3448 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3449 #if VERBOSE_TOPOLOGY
3450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3451 _("Creating small world (ring) CONNECT topology\n"));
3453 create_small_world_ring (pg, &add_actual_connections);
3455 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3456 #if VERBOSE_TOPOLOGY
3457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3458 _("Creating small world (2d-torus) CONNECT topology\n"));
3460 create_small_world (pg, &add_actual_connections);
3462 case GNUNET_TESTING_TOPOLOGY_RING:
3463 #if VERBOSE_TOPOLOGY
3464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3465 _("Creating ring CONNECT topology\n"));
3467 create_ring (pg, &add_actual_connections);
3469 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3470 #if VERBOSE_TOPOLOGY
3471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3472 _("Creating 2d torus CONNECT topology\n"));
3474 create_2d_torus (pg, &add_actual_connections);
3476 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3477 #if VERBOSE_TOPOLOGY
3478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3479 _("Creating Erdos-Renyi CONNECT topology\n"));
3481 create_erdos_renyi (pg, &add_actual_connections);
3483 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3484 #if VERBOSE_TOPOLOGY
3485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3486 _("Creating InterNAT CONNECT topology\n"));
3488 create_nated_internet (pg, &add_actual_connections);
3490 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3491 #if VERBOSE_TOPOLOGY
3492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3493 _("Creating Scale Free CONNECT topology\n"));
3495 create_scale_free (pg, &add_actual_connections);
3497 case GNUNET_TESTING_TOPOLOGY_LINE:
3498 #if VERBOSE_TOPOLOGY
3499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3500 _("Creating straight line CONNECT topology\n"));
3502 create_line (pg, &add_actual_connections);
3504 case GNUNET_TESTING_TOPOLOGY_NONE:
3505 #if VERBOSE_TOPOLOGY
3506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3507 _("Creating no CONNECT topology\n"));
3509 copy_allowed_topology(pg);
3512 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3513 _("Unknown topology specification, can't connect peers!\n"));
3514 return GNUNET_SYSERR;
3519 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
3520 #if VERBOSE_TOPOLOGY
3521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3522 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
3524 choose_random_connections(pg, option_modifier);
3526 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
3527 #if VERBOSE_TOPOLOGY
3528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3529 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3531 choose_minimum(pg, (unsigned int)option_modifier);
3533 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
3534 #if VERBOSE_TOPOLOGY
3535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3536 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3538 perform_dfs(pg, (int)option_modifier);
3540 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
3541 #if VERBOSE_TOPOLOGY
3542 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3543 _("Finding additional %u closest peers each (if possible)\n"), (unsigned int)option_modifier);
3545 add_closest(pg, (unsigned int)option_modifier, &add_actual_connections);
3547 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
3549 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
3555 return connect_topology(pg, notify_callback, notify_cls);
3559 * Callback that is called whenever a hostkey is generated
3560 * for a peer. Call the real callback and decrement the
3561 * starting counter for the peergroup.
3563 * @param cls closure
3564 * @param id identifier for the daemon, NULL on error
3565 * @param d handle for the daemon
3566 * @param emsg error message (NULL on success)
3568 static void internal_hostkey_callback (void *cls,
3569 const struct GNUNET_PeerIdentity *id,
3570 struct GNUNET_TESTING_Daemon *d,
3573 struct InternalStartContext *internal_context = cls;
3574 internal_context->peer->pg->starting--;
3575 internal_context->peer->pg->started++;
3576 if (internal_context->hostkey_callback != NULL)
3577 internal_context->hostkey_callback(internal_context->hostkey_cls, id, d, emsg);
3578 else if (internal_context->peer->pg->started == internal_context->peer->pg->total)
3580 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
3581 GNUNET_TESTING_daemons_continue_startup(internal_context->peer->pg);
3586 * Callback that is called whenever a peer has finished starting.
3587 * Call the real callback and decrement the starting counter
3588 * for the peergroup.
3590 * @param cls closure
3591 * @param id identifier for the daemon, NULL on error
3592 * @param d handle for the daemon
3593 * @param emsg error message (NULL on success)
3595 static void internal_startup_callback (void *cls,
3596 const struct GNUNET_PeerIdentity *id,
3597 const struct GNUNET_CONFIGURATION_Handle *cfg,
3598 struct GNUNET_TESTING_Daemon *d,
3601 struct InternalStartContext *internal_context = cls;
3602 internal_context->peer->pg->starting--;
3603 if (internal_context->start_cb != NULL)
3604 internal_context->start_cb(internal_context->start_cb_cls, id, cfg, d, emsg);
3608 internal_continue_startup (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3610 struct InternalStartContext *internal_context = cls;
3612 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3617 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
3619 internal_context->peer->pg->starting++;
3620 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
3624 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_continue_startup, internal_context);
3630 * Callback for informing us about a successful
3631 * or unsuccessful churn start call.
3633 * @param cls a ChurnContext
3634 * @param id the peer identity of the started peer
3635 * @param cfg the handle to the configuration of the peer
3636 * @param d handle to the daemon for the peer
3637 * @param emsg NULL on success, non-NULL on failure
3641 churn_start_callback (void *cls,
3642 const struct GNUNET_PeerIdentity *id,
3643 const struct GNUNET_CONFIGURATION_Handle *cfg,
3644 struct GNUNET_TESTING_Daemon *d,
3647 struct ChurnRestartContext *startup_ctx = cls;
3648 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
3650 unsigned int total_left;
3651 char *error_message;
3653 error_message = NULL;
3656 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3657 "Churn stop callback failed with error `%s'\n",
3659 churn_ctx->num_failed_start++;
3663 churn_ctx->num_to_start--;
3667 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3668 "Started peer, %d left.\n",
3669 churn_ctx->num_to_start);
3672 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3674 if (total_left == 0)
3676 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3677 GNUNET_asprintf(&error_message,
3678 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3679 churn_ctx->num_failed_start,
3680 churn_ctx->num_failed_stop);
3681 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3682 GNUNET_free_non_null(error_message);
3683 GNUNET_free(churn_ctx);
3684 GNUNET_free(startup_ctx);
3689 static void schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3691 struct PeerRestartContext *peer_restart_ctx = cls;
3692 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
3694 if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
3695 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_churn_restart, peer_restart_ctx);
3698 GNUNET_TESTING_daemon_start_stopped(peer_restart_ctx->daemon,
3699 startup_ctx->timeout,
3700 &churn_start_callback,
3702 GNUNET_free(peer_restart_ctx);
3707 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3709 struct InternalStartContext *internal_context = cls;
3711 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3716 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
3718 internal_context->peer->pg->starting++;
3719 internal_context->peer->daemon = GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
3720 internal_context->timeout,
3721 internal_context->hostname,
3722 internal_context->username,
3723 internal_context->sshport,
3724 &internal_hostkey_callback,
3726 &internal_startup_callback,
3731 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_start, internal_context);
3736 * Function which continues a peer group starting up
3737 * after successfully generating hostkeys for each peer.
3739 * @param pg the peer group to continue starting
3743 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
3748 for (i = 0; i < pg->total; i++)
3750 GNUNET_SCHEDULER_add_now (&internal_continue_startup, &pg->peers[i].internal_context);
3751 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
3756 * Start count gnunet instances with the same set of transports and
3757 * applications. The port numbers (any option called "PORT") will be
3758 * adjusted to ensure that no two peers running on the same system
3759 * have the same port(s) in their respective configurations.
3761 * @param cfg configuration template to use
3762 * @param total number of daemons to start
3763 * @param timeout total time allowed for peers to start
3764 * @param hostkey_callback function to call on each peers hostkey generation
3765 * if NULL, peers will be started by this call, if non-null,
3766 * GNUNET_TESTING_daemons_continue_startup must be called after
3767 * successful hostkey generation
3768 * @param hostkey_cls closure for hostkey callback
3769 * @param cb function to call on each daemon that was started
3770 * @param cb_cls closure for cb
3771 * @param connect_callback function to call each time two hosts are connected
3772 * @param connect_callback_cls closure for connect_callback
3773 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
3775 * @return NULL on error, otherwise handle to control peer group
3777 struct GNUNET_TESTING_PeerGroup *
3778 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
3780 struct GNUNET_TIME_Relative timeout,
3781 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
3783 GNUNET_TESTING_NotifyDaemonRunning cb,
3785 GNUNET_TESTING_NotifyConnection
3786 connect_callback, void *connect_callback_cls,
3787 const struct GNUNET_TESTING_Host *hostnames)
3789 struct GNUNET_TESTING_PeerGroup *pg;
3790 const struct GNUNET_TESTING_Host *hostpos;
3796 const char *hostname;
3797 const char *username;
3798 char *baseservicehome;
3799 char *newservicehome;
3801 struct GNUNET_CONFIGURATION_Handle *pcfg;
3803 unsigned int hostcnt;
3816 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
3818 pg->notify_connection = connect_callback;
3819 pg->notify_connection_cls = connect_callback_cls;
3821 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
3822 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
3823 if (NULL != hostnames)
3826 hostpos = hostnames;
3827 while (hostpos != NULL)
3829 hostpos = hostpos->next;
3832 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3835 hostpos = hostnames;
3836 while (hostpos != NULL)
3838 pg->hosts[off].minport = LOW_PORT;
3839 pg->hosts[off].hostname = GNUNET_strdup(hostpos->hostname);
3840 if (hostpos->username != NULL)
3841 pg->hosts[off].username = GNUNET_strdup(hostpos->username);
3842 pg->hosts[off].sshport = hostpos->port;
3843 hostpos = hostpos->next;
3853 pg->num_hosts = off;
3857 /* skip leading spaces */
3858 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
3861 while ('\0' != *rpos)
3863 if (isspace ( (unsigned char) *rpos))
3867 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3869 start = GNUNET_strdup (hostnames);
3871 while ('\0' != *pos)
3873 if (isspace ( (unsigned char) *pos))
3876 if (strlen (start) > 0)
3878 pg->hosts[off].minport = LOW_PORT;
3879 pg->hosts[off++].hostname = start;
3885 if (strlen (start) > 0)
3887 pg->hosts[off].minport = LOW_PORT;
3888 pg->hosts[off++].hostname = start;
3892 GNUNET_free (start);
3893 GNUNET_free (pg->hosts);
3897 minport = 0; /* make gcc happy */
3905 for (off = 0; off < total; off++)
3909 hostname = pg->hosts[off % hostcnt].hostname;
3910 username = pg->hosts[off % hostcnt].username;
3911 sshport = pg->hosts[off % hostcnt].sshport;
3912 pcfg = make_config (cfg,
3913 &pg->hosts[off % hostcnt].minport,
3922 pcfg = make_config (cfg,
3930 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3932 ("Could not create configuration for peer number %u on `%s'!\n"),
3933 off, hostname == NULL ? "localhost" : hostname);
3938 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
3941 GNUNET_asprintf (&newservicehome,
3942 "%s/%d/", baseservicehome, off);
3943 GNUNET_free (baseservicehome);
3947 tmpdir = getenv ("TMPDIR");
3948 tmpdir = tmpdir ? tmpdir : "/tmp";
3949 GNUNET_asprintf (&newservicehome,
3952 "gnunet-testing-test-test", off);
3954 GNUNET_CONFIGURATION_set_value_string (pcfg,
3956 "SERVICEHOME", newservicehome);
3957 GNUNET_free (newservicehome);
3958 pg->peers[off].cfg = pcfg;
3959 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
3960 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
3961 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
3962 pg->peers[off].pg = pg;
3964 pg->peers[off].internal_context.peer = &pg->peers[off];
3965 pg->peers[off].internal_context.timeout = timeout;
3966 pg->peers[off].internal_context.hostname = hostname;
3967 pg->peers[off].internal_context.username = username;
3968 pg->peers[off].internal_context.sshport = sshport;
3969 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
3970 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
3971 pg->peers[off].internal_context.start_cb = cb;
3972 pg->peers[off].internal_context.start_cb_cls = cb_cls;
3974 GNUNET_SCHEDULER_add_now (&internal_start, &pg->peers[off].internal_context);
3981 * Get a daemon by number, so callers don't have to do nasty
3982 * offsetting operation.
3984 struct GNUNET_TESTING_Daemon *
3985 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
3987 if (position < pg->total)
3988 return pg->peers[position].daemon;
3994 * Get a daemon by peer identity, so callers can
3995 * retrieve the daemon without knowing it's offset.
3997 * @param pg the peer group to retrieve the daemon from
3998 * @param peer_id the peer identity of the daemon to retrieve
4000 * @return the daemon on success, or NULL if no such peer identity is found
4002 struct GNUNET_TESTING_Daemon *
4003 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_PeerIdentity *peer_id)
4007 for (i = 0; i < pg->total; i ++)
4009 if (0 == memcmp(&pg->peers[i].daemon->id, peer_id, sizeof(struct GNUNET_PeerIdentity)))
4010 return pg->peers[i].daemon;
4017 * Prototype of a function that will be called when a
4018 * particular operation was completed the testing library.
4020 * @param cls closure (a struct RestartContext)
4021 * @param id id of the peer that was restarted
4022 * @param cfg handle to the configuration of the peer
4023 * @param d handle to the daemon that was restarted
4024 * @param emsg NULL on success
4026 void restart_callback (void *cls,
4027 const struct GNUNET_PeerIdentity *id,
4028 const struct GNUNET_CONFIGURATION_Handle *cfg,
4029 struct GNUNET_TESTING_Daemon *d,
4032 struct RestartContext *restart_context = cls;
4036 restart_context->peers_restarted++;
4040 restart_context->peers_restart_failed++;
4043 if (restart_context->peers_restarted == restart_context->peer_group->total)
4045 restart_context->callback(restart_context->callback_cls, NULL);
4046 GNUNET_free(restart_context);
4048 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
4050 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
4051 GNUNET_free(restart_context);
4057 * Callback for informing us about a successful
4058 * or unsuccessful churn stop call.
4060 * @param cls a ChurnContext
4061 * @param emsg NULL on success, non-NULL on failure
4065 churn_stop_callback (void *cls, const char *emsg)
4067 struct ShutdownContext *shutdown_ctx = cls;
4068 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
4069 unsigned int total_left;
4070 char *error_message;
4072 error_message = NULL;
4073 shutdown_ctx->outstanding--;
4077 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
4078 "Churn stop callback failed with error `%s'\n", emsg);
4079 churn_ctx->num_failed_stop++;
4083 churn_ctx->num_to_stop--;
4087 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
4088 "Stopped peer, %d left.\n",
4089 churn_ctx->num_to_stop);
4091 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4093 if (total_left == 0)
4095 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
4097 GNUNET_asprintf(&error_message,
4098 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4099 churn_ctx->num_failed_start,
4100 churn_ctx->num_failed_stop);
4102 churn_ctx->cb(churn_ctx->cb_cls, error_message);
4103 GNUNET_free_non_null(error_message);
4104 GNUNET_free(churn_ctx);
4105 GNUNET_free(shutdown_ctx);
4110 * Count the number of running peers.
4112 * @param pg handle for the peer group
4114 * @return the number of currently running peers in the peer group
4117 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
4120 unsigned int running = 0;
4121 for (i = 0; i < pg->total; i++)
4123 if (pg->peers[i].daemon->running == GNUNET_YES)
4125 GNUNET_assert(running != -1);
4133 * Task to rate limit the number of outstanding peer shutdown
4134 * requests. This is necessary for making sure we don't do
4135 * too many ssh connections at once, but is generally nicer
4136 * to any system as well (graduated task starts, as opposed
4137 * to calling gnunet-arm N times all at once).
4140 schedule_churn_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
4142 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4143 struct ShutdownContext *shutdown_ctx;
4145 GNUNET_assert(peer_shutdown_ctx != NULL);
4146 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4147 GNUNET_assert(shutdown_ctx != NULL);
4149 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4150 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_churn_shutdown_task, peer_shutdown_ctx);
4153 shutdown_ctx->outstanding++;
4154 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, shutdown_ctx->cb, shutdown_ctx, GNUNET_NO, GNUNET_YES);
4155 GNUNET_free(peer_shutdown_ctx);
4160 * Simulate churn by stopping some peers (and possibly
4161 * re-starting others if churn is called multiple times). This
4162 * function can only be used to create leave-join churn (peers "never"
4163 * leave for good). First "voff" random peers that are currently
4164 * online will be taken offline; then "von" random peers that are then
4165 * offline will be put back online. No notifications will be
4166 * generated for any of these operations except for the callback upon
4169 * @param pg handle for the peer group
4170 * @param voff number of peers that should go offline
4171 * @param von number of peers that should come back online;
4172 * must be zero on first call (since "testbed_start"
4173 * always starts all of the peers)
4174 * @param timeout how long to wait for operations to finish before
4176 * @param cb function to call at the end
4177 * @param cb_cls closure for cb
4180 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
4183 struct GNUNET_TIME_Relative timeout,
4184 GNUNET_TESTING_NotifyCompletion cb,
4187 struct ChurnContext *churn_ctx;
4188 struct ShutdownContext *shutdown_ctx;
4189 struct PeerShutdownContext *peer_shutdown_ctx;
4190 struct PeerRestartContext *peer_restart_ctx;
4191 struct ChurnRestartContext *churn_startup_ctx;
4193 unsigned int running;
4194 unsigned int stopped;
4195 unsigned int total_running;
4196 unsigned int total_stopped;
4198 unsigned int *running_arr;
4199 unsigned int *stopped_arr;
4200 unsigned int *running_permute;
4201 unsigned int *stopped_permute;
4206 if ((von == 0) && (voff == 0)) /* No peers at all? */
4212 for (i = 0; i < pg->total; i++)
4214 if (pg->peers[i].daemon->running == GNUNET_YES)
4216 GNUNET_assert(running != -1);
4221 GNUNET_assert(stopped != -1);
4228 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
4229 cb(cb_cls, "Trying to stop more peers than are currently running!");
4235 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
4236 cb(cb_cls, "Trying to start more peers than are currently stopped!");
4240 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4244 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
4248 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
4250 running_permute = NULL;
4251 stopped_permute = NULL;
4254 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
4256 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
4258 total_running = running;
4259 total_stopped = stopped;
4263 churn_ctx->num_to_start = von;
4264 churn_ctx->num_to_stop = voff;
4266 churn_ctx->cb_cls = cb_cls;
4268 for (i = 0; i < pg->total; i++)
4270 if (pg->peers[i].daemon->running == GNUNET_YES)
4272 GNUNET_assert((running_arr != NULL) && (total_running > running));
4273 running_arr[running] = i;
4278 GNUNET_assert((stopped_arr != NULL) && (total_stopped > stopped));
4279 stopped_arr[stopped] = i;
4284 GNUNET_assert(running >= voff);
4287 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4288 shutdown_ctx->cb = &churn_stop_callback;
4289 shutdown_ctx->cb_cls = churn_ctx;
4290 shutdown_ctx->total_peers = voff;
4291 shutdown_ctx->timeout = timeout;
4294 for (i = 0; i < voff; i++)
4297 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
4299 GNUNET_assert(running_arr != NULL);
4300 peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4301 peer_shutdown_ctx->daemon = pg->peers[running_arr[running_permute[i]]].daemon;
4302 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4303 GNUNET_SCHEDULER_add_now(&schedule_churn_shutdown_task, peer_shutdown_ctx);
4306 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
4308 &churn_stop_callback, churn_ctx,
4309 GNUNET_NO, GNUNET_YES); */
4312 GNUNET_assert(stopped >= von);
4315 churn_startup_ctx = GNUNET_malloc(sizeof(struct ChurnRestartContext));
4316 churn_startup_ctx->churn_ctx = churn_ctx;
4317 churn_startup_ctx->timeout = timeout;
4319 for (i = 0; i < von; i++)
4322 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
4324 GNUNET_assert(stopped_arr != NULL);
4325 peer_restart_ctx = GNUNET_malloc(sizeof(struct PeerRestartContext));
4326 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
4327 peer_restart_ctx->daemon = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
4328 GNUNET_SCHEDULER_add_now(&schedule_churn_restart, peer_restart_ctx);
4330 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
4331 timeout, &churn_start_callback, churn_ctx);*/
4334 GNUNET_free_non_null(running_arr);
4335 GNUNET_free_non_null(stopped_arr);
4336 GNUNET_free_non_null(running_permute);
4337 GNUNET_free_non_null(stopped_permute);
4342 * Restart all peers in the given group.
4344 * @param pg the handle to the peer group
4345 * @param callback function to call on completion (or failure)
4346 * @param callback_cls closure for the callback function
4349 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
4350 GNUNET_TESTING_NotifyCompletion callback,
4353 struct RestartContext *restart_context;
4358 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
4359 restart_context->peer_group = pg;
4360 restart_context->peers_restarted = 0;
4361 restart_context->callback = callback;
4362 restart_context->callback_cls = callback_cls;
4364 for (off = 0; off < pg->total; off++)
4366 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
4372 * Start or stop an individual peer from the given group.
4374 * @param pg handle to the peer group
4375 * @param offset which peer to start or stop
4376 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
4377 * @param timeout how long to wait for shutdown
4378 * @param cb function to call at the end
4379 * @param cb_cls closure for cb
4382 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
4383 unsigned int offset,
4385 struct GNUNET_TIME_Relative timeout,
4386 GNUNET_TESTING_NotifyCompletion cb,
4389 struct ShutdownContext *shutdown_ctx;
4390 struct ChurnRestartContext *startup_ctx;
4391 struct ChurnContext *churn_ctx;
4393 if (GNUNET_NO == desired_status)
4395 if (NULL != pg->peers[offset].daemon)
4397 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4398 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4399 churn_ctx->num_to_start = 0;
4400 churn_ctx->num_to_stop = 1;
4402 churn_ctx->cb_cls = cb_cls;
4403 shutdown_ctx->cb_cls = churn_ctx;
4404 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
4405 timeout, &churn_stop_callback, shutdown_ctx,
4406 GNUNET_NO, GNUNET_YES);
4409 else if (GNUNET_YES == desired_status)
4411 if (NULL == pg->peers[offset].daemon)
4413 startup_ctx = GNUNET_malloc(sizeof(struct ChurnRestartContext));
4414 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4415 churn_ctx->num_to_start = 1;
4416 churn_ctx->num_to_stop = 0;
4418 churn_ctx->cb_cls = cb_cls;
4419 startup_ctx->churn_ctx = churn_ctx;
4420 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
4421 timeout, &churn_start_callback, startup_ctx);
4430 * Callback for shutting down peers in a peer group.
4432 * @param cls closure (struct ShutdownContext)
4433 * @param emsg NULL on success
4435 void internal_shutdown_callback (void *cls,
4438 struct ShutdownContext *shutdown_ctx = cls;
4440 shutdown_ctx->outstanding--;
4443 shutdown_ctx->peers_down++;
4447 shutdown_ctx->peers_failed++;
4450 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
4452 if (shutdown_ctx->peers_failed > 0)
4453 shutdown_ctx->cb(shutdown_ctx->cb_cls, "Not all peers successfully shut down!");
4455 shutdown_ctx->cb(shutdown_ctx->cb_cls, NULL);
4456 GNUNET_free(shutdown_ctx);
4462 * Task to rate limit the number of outstanding peer shutdown
4463 * requests. This is necessary for making sure we don't do
4464 * too many ssh connections at once, but is generally nicer
4465 * to any system as well (graduated task starts, as opposed
4466 * to calling gnunet-arm N times all at once).
4469 schedule_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
4471 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4472 struct ShutdownContext *shutdown_ctx;
4474 GNUNET_assert(peer_shutdown_ctx != NULL);
4475 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4476 GNUNET_assert(shutdown_ctx != NULL);
4478 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4479 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_shutdown_task, peer_shutdown_ctx);
4482 shutdown_ctx->outstanding++;
4483 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, &internal_shutdown_callback, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4484 GNUNET_free(peer_shutdown_ctx);
4489 * Shutdown all peers started in the given group.
4491 * @param pg handle to the peer group
4492 * @param timeout how long to wait for shutdown
4493 * @param cb callback to notify upon success or failure
4494 * @param cb_cls closure for cb
4497 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
4498 struct GNUNET_TIME_Relative timeout,
4499 GNUNET_TESTING_NotifyCompletion cb,
4503 struct ShutdownContext *shutdown_ctx;
4504 struct PeerShutdownContext *peer_shutdown_ctx;
4506 GNUNET_assert(pg->total > 0);
4508 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4509 shutdown_ctx->cb = cb;
4510 shutdown_ctx->cb_cls = cb_cls;
4511 shutdown_ctx->total_peers = pg->total;
4512 shutdown_ctx->timeout = timeout;
4513 /* shtudown_ctx->outstanding = 0; */
4515 for (off = 0; off < pg->total; off++)
4517 GNUNET_assert(NULL != pg->peers[off].daemon);
4518 peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4519 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
4520 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4521 GNUNET_SCHEDULER_add_now(&schedule_shutdown_task, peer_shutdown_ctx);
4522 //GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4523 if (NULL != pg->peers[off].cfg)
4524 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
4525 if (pg->peers[off].allowed_peers != NULL)
4526 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
4527 if (pg->peers[off].connect_peers != NULL)
4528 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
4529 if (pg->peers[off].blacklisted_peers != NULL)
4530 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
4532 GNUNET_free (pg->peers);
4533 for (off = 0; off < pg->num_hosts; off++)
4535 GNUNET_free (pg->hosts[off].hostname);
4536 GNUNET_free_non_null (pg->hosts[off].username);
4538 GNUNET_free_non_null (pg->hosts);
4543 /* end of testing_group.c */