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
486 struct GNUNET_SCHEDULER_Handle *sched;
489 * Configuration template.
491 const struct GNUNET_CONFIGURATION_Handle *cfg;
494 * Function to call on each started daemon.
496 //GNUNET_TESTING_NotifyDaemonRunning cb;
504 * Function to call on each topology connection created
506 GNUNET_TESTING_NotifyConnection notify_connection;
509 * Callback for notify_connection
511 void *notify_connection_cls;
514 * Array of information about hosts.
516 struct HostData *hosts;
519 * Number of hosts (size of HostData)
521 unsigned int num_hosts;
524 * Array of "total" peers.
526 struct PeerData *peers;
529 * Number of peers in this group.
534 * At what time should we fail the peer startup process?
536 struct GNUNET_TIME_Absolute max_timeout;
539 * How many peers are being started right now?
541 unsigned int starting;
544 * How many peers have already been started?
546 unsigned int started;
551 struct GNUNET_CONFIGURATION_Handle *ret;
552 const struct GNUNET_CONFIGURATION_Handle *orig;
553 const char *hostname;
559 struct ConnectTopologyContext
562 * How many connections are left to create.
564 unsigned int remaining_connections;
567 * Handle to group of peers.
569 struct GNUNET_TESTING_PeerGroup *pg;
572 * Temp value set for each iteration.
574 struct PeerData *first;
577 * Notification that all peers are connected.
579 GNUNET_TESTING_NotifyCompletion notify_connections_done;
582 * Closure for notify.
587 struct ConnectContext
590 * Peer to connect second to.
592 struct GNUNET_TESTING_Daemon *first;
595 * Peer to connect first to.
597 struct GNUNET_TESTING_Daemon *second;
600 * Higher level topology connection context.
602 struct ConnectTopologyContext *ct_ctx;
606 * Convert unique ID to hash code.
608 * @param uid unique ID to convert
609 * @param hash set to uid (extended with zeros)
612 hash_from_uid (uint32_t uid,
613 GNUNET_HashCode *hash)
615 memset (hash, 0, sizeof(GNUNET_HashCode));
616 *((uint32_t*)hash) = uid;
620 * Convert hash code to unique ID.
622 * @param uid unique ID to convert
623 * @param hash set to uid (extended with zeros)
626 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
628 memcpy (uid, hash, sizeof(uint32_t));
632 * Number of connects we are waiting on, allows us to rate limit
635 static int outstanding_connects;
638 * Get a topology from a string input.
640 * @param topology where to write the retrieved topology
641 * @param topology_string The string to attempt to
642 * get a configuration value from
643 * @return GNUNET_YES if topology string matched a
644 * known topology, GNUNET_NO if not
647 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, const char * topology_string)
650 * Strings representing topologies in enum
652 static const char * topology_strings[] =
655 * A clique (everyone connected to everyone else).
660 * Small-world network (2d torus plus random links).
665 * Small-world network (ring plus random links).
685 * Certain percentage of peers are unable to communicate directly
686 * replicating NAT conditions
691 * Scale free topology.
696 * Straight line topology.
701 * All peers are disconnected.
709 if (topology_string == NULL)
711 while (topology_strings[curr] != NULL)
713 if (strcasecmp(topology_strings[curr], topology_string) == 0)
720 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
726 * Get connect topology option from string input.
728 * @param topology_option where to write the retrieved topology
729 * @param topology_string The string to attempt to
730 * get a configuration value from
731 * @return GNUNET_YES if string matched a known
732 * topology option, GNUNET_NO if not
735 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption *topology_option,
736 const char * topology_string)
739 * Options for connecting a topology as strings.
741 static const char * topology_option_strings[] =
744 * Try to connect all peers specified in the topology.
749 * Choose a random subset of connections to create.
751 "CONNECT_RANDOM_SUBSET",
754 * Create at least X connections for each peer.
759 * Using a depth first search, create one connection
760 * per peer. If any are missed (graph disconnected)
761 * start over at those peers until all have at least one
767 * Find the N closest peers to each allowed peer in the
768 * topology and make sure a connection to those peers
769 * exists in the connect topology.
774 * No options specified.
782 if (topology_string == NULL)
784 while (NULL != topology_option_strings[curr])
786 if (strcasecmp(topology_option_strings[curr], topology_string) == 0)
788 *topology_option = curr;
793 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
798 * Function to iterate over options. Copies
799 * the options to the target configuration,
800 * updating PORT values as needed.
803 * @param section name of the section
804 * @param option name of the option
805 * @param value value of the option
808 update_config (void *cls,
809 const char *section, const char *option, const char *value)
811 struct UpdateContext *ctx = cls;
815 char *single_variable;
816 char *per_host_variable;
817 unsigned long long num_per_host;
819 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
821 GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
822 if ((ival != 0) && (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable)))
824 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
828 GNUNET_free(single_variable);
831 if (0 == strcmp (option, "UNIXPATH"))
833 GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
834 GNUNET_asprintf(&per_host_variable, "num_%s_per_host", section);
835 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable))
837 GNUNET_snprintf (uval,
839 "/tmp/test-service-%s-%u",
844 else if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_number(ctx->orig, "testing", per_host_variable, &num_per_host))
846 GNUNET_snprintf (uval,
848 "/tmp/test-service-%s-%u",
850 ctx->fdnum % num_per_host);
853 GNUNET_free(single_variable);
854 GNUNET_free(per_host_variable);
858 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
860 value = ctx->hostname;
863 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
868 * Create a new configuration using the given configuration
869 * as a template; however, each PORT in the existing cfg
870 * must be renumbered by incrementing "*port". If we run
871 * out of "*port" numbers, return NULL.
873 * @param cfg template configuration
874 * @param port port numbers to use, update to reflect
875 * port numbers that were used
876 * @param upnum number to make unix domain socket names unique
877 * @param hostname hostname of the controlling host, to allow control connections from
878 * @param fdnum number used to offset the unix domain socket for grouped processes
879 * (such as statistics or peerinfo, which can be shared among others)
881 * @return new configuration, NULL on error
883 static struct GNUNET_CONFIGURATION_Handle *
884 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
887 const char *hostname, uint32_t * fdnum)
889 struct UpdateContext uc;
898 uc.ret = GNUNET_CONFIGURATION_create ();
899 uc.hostname = hostname;
902 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
903 if (uc.nport >= HIGH_PORT)
906 GNUNET_CONFIGURATION_destroy (uc.ret);
910 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
912 if (hostname != NULL)
913 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1; %s;", control_host, hostname);
915 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
917 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
918 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport", "ACCEPT_FROM", allowed_hosts);
919 GNUNET_CONFIGURATION_set_value_string(uc.ret, "dht", "ACCEPT_FROM", allowed_hosts);
920 GNUNET_CONFIGURATION_set_value_string(uc.ret, "statistics", "ACCEPT_FROM", allowed_hosts);
921 GNUNET_free_non_null(control_host);
922 GNUNET_free(allowed_hosts);
926 /* arm needs to know to allow connections from the host on which it is running,
927 * otherwise gnunet-arm is unable to connect to it in some instances */
928 if (hostname != NULL)
930 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
931 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", hostname);
932 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", hostname);
933 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
934 GNUNET_free(allowed_hosts);
938 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", "127.0.0.1");
939 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", "127.0.0.1");
942 *port = (uint16_t) uc.nport;
951 * Add entries to the peers connect list
953 * @param pg the peer group we are working with
954 * @param first index of the first peer
955 * @param second index of the second peer
957 * @return the number of connections added
958 * technically should only be 0 or 2
962 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
968 GNUNET_HashCode hash_first;
969 GNUNET_HashCode hash_second;
971 hash_from_uid(first, &hash_first);
972 hash_from_uid(second, &hash_second);
974 add_first = GNUNET_NO;
975 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
977 add_first = GNUNET_YES;
980 add_second = GNUNET_NO;
981 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
983 add_second = GNUNET_YES;
989 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
990 pg->peers[first].num_connections++;
996 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
997 pg->peers[second].num_connections++;
1006 * Add entries to the peers allowed connections list
1008 * @param pg the peer group we are working with
1009 * @param first index of the first peer
1010 * @param second index of the second peer
1012 * @return the number of connections added (can be 0, 1 or 2)
1013 * technically should only be 0 or 2, but the small price
1014 * of iterating over the lists (hashmaps in the future)
1015 * for being sure doesn't bother me!
1019 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1023 struct PeerConnection *first_iter;
1024 struct PeerConnection *second_iter;
1025 struct PeerConnection *new_first;
1026 struct PeerConnection *new_second;
1031 GNUNET_HashCode hash_first;
1032 GNUNET_HashCode hash_second;
1034 hash_from_uid(first, &hash_first);
1035 hash_from_uid(second, &hash_second);
1037 add_first = GNUNET_NO;
1038 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
1040 add_first = GNUNET_YES;
1043 add_second = GNUNET_NO;
1044 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
1046 add_second = GNUNET_YES;
1049 first_iter = pg->peers[first].connected_peers;
1050 while (first_iter != NULL)
1052 if (first_iter->daemon == pg->peers[second].daemon)
1053 add_first = GNUNET_NO;
1054 first_iter = first_iter->next;
1057 second_iter = pg->peers[second].connected_peers;
1058 add_second = GNUNET_YES;
1059 while (second_iter != NULL)
1061 if (second_iter->daemon == pg->peers[first].daemon)
1062 add_second = GNUNET_NO;
1063 second_iter = second_iter->next;
1070 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1072 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
1073 new_first->daemon = pg->peers[second].daemon;
1074 new_first->next = pg->peers[first].connected_peers;
1075 pg->peers[first].connected_peers = new_first;
1077 pg->peers[first].num_connections++;
1083 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1085 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
1086 new_second->daemon = pg->peers[first].daemon;
1087 new_second->next = pg->peers[second].connected_peers;
1088 pg->peers[second].connected_peers = new_second;
1089 pg->peers[first].num_connections++;
1091 pg->peers[second].num_connections++;
1099 * Add entries to the peers blacklisted list
1101 * @param pg the peer group we are working with
1102 * @param first index of the first peer
1103 * @param second index of the second peer
1105 * @return the number of connections added (can be 0, 1 or 2)
1109 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1114 GNUNET_HashCode hash_first;
1115 GNUNET_HashCode hash_second;
1117 hash_from_uid(first, &hash_first);
1118 hash_from_uid(second, &hash_second);
1120 add_first = GNUNET_NO;
1121 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
1123 add_first = GNUNET_YES;
1126 add_second = GNUNET_NO;
1127 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
1129 add_second = GNUNET_YES;
1135 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1136 pg->peers[first].num_connections++;
1142 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1143 pg->peers[second].num_connections++;
1151 * Remove entries from the peers blacklisted list
1153 * @param pg the peer group we are working with
1154 * @param first index of the first peer
1155 * @param second index of the second peer
1157 * @return the number of connections removed (can be 0, 1 or 2)
1161 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1166 GNUNET_HashCode hash_first;
1167 GNUNET_HashCode hash_second;
1169 hash_from_uid(first, &hash_first);
1170 hash_from_uid(second, &hash_second);
1172 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
1173 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
1178 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
1184 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
1192 * Scale free network construction as described in:
1194 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1196 * Start with a network of "one" peer, then progressively add
1197 * peers up to the total number. At each step, iterate over
1198 * all possible peers and connect new peer based on number of
1199 * existing connections of the target peer.
1201 * @param pg the peer group we are dealing with
1202 * @param proc the connection processor to use
1204 * @return the number of connections created
1207 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1210 unsigned int total_connections;
1211 unsigned int outer_count;
1213 unsigned int previous_total_connections;
1217 GNUNET_assert(pg->total > 1);
1219 /* Add a connection between the first two nodes */
1220 total_connections = proc(pg, 0, 1);
1222 for (outer_count = 1; outer_count < pg->total; outer_count++)
1224 previous_total_connections = total_connections;
1225 for (i = 0; i < outer_count; i++)
1227 probability = pg->peers[i].num_connections / (double)previous_total_connections;
1228 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1229 UINT64_MAX)) / ( (double) UINT64_MAX);
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1232 "Considering connecting peer %d to peer %d\n",
1235 if (random < probability)
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1239 "Connecting peer %d to peer %d\n",
1242 total_connections += proc(pg, outer_count, i);
1247 return total_connections;
1251 * Create a topology given a peer group (set of running peers)
1252 * and a connection processor.
1254 * @param pg the peergroup to create the topology on
1255 * @param proc the connection processor to call to actually set
1256 * up connections between two peers
1258 * @return the number of connections that were set up
1262 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1266 unsigned int natLog;
1267 unsigned int randomPeer;
1268 double random, logNModifier, percentage;
1269 unsigned int smallWorldConnections;
1274 unsigned int useAnd;
1275 int connect_attempts;
1277 logNModifier = 0.5; /* FIXME: default value? */
1278 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1283 if (sscanf(p_string, "%lf", &logNModifier) != 1)
1284 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1285 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1289 GNUNET_free (p_string);
1291 percentage = 0.5; /* FIXME: default percentage? */
1292 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1297 if (sscanf(p_string, "%lf", &percentage) != 1)
1298 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1299 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1303 GNUNET_free (p_string);
1305 natLog = log (pg->total);
1306 connsPerPeer = ceil (natLog * logNModifier);
1308 if (connsPerPeer % 2 == 1)
1311 smallWorldConnections = 0;
1312 connect_attempts = 0;
1313 for (i = 0; i < pg->total; i++)
1316 max = i + connsPerPeer / 2;
1317 min = i - connsPerPeer / 2;
1319 if (max > pg->total - 1)
1321 max = max - pg->total;
1327 min = pg->total - 1 + min;
1331 for (j = 0; j < connsPerPeer / 2; j++)
1333 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1334 UINT64_MAX) / ( (double) UINT64_MAX));
1335 if (random < percentage)
1337 /* Connect to uniformly selected random peer */
1339 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1341 while ((((randomPeer < max) && (randomPeer > min))
1342 && (useAnd == 0)) || (((randomPeer > min)
1343 || (randomPeer < max))
1347 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1350 smallWorldConnections +=
1351 proc (pg, i, randomPeer);
1355 nodeToConnect = i + j + 1;
1356 if (nodeToConnect > pg->total - 1)
1358 nodeToConnect = nodeToConnect - pg->total;
1361 proc (pg, i, nodeToConnect);
1367 connect_attempts += smallWorldConnections;
1369 return connect_attempts;
1373 * Create a topology given a peer group (set of running peers)
1374 * and a connection processor.
1376 * @param pg the peergroup to create the topology on
1377 * @param proc the connection processor to call to actually set
1378 * up connections between two peers
1380 * @return the number of connections that were set up
1384 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1386 unsigned int outer_count, inner_count;
1387 unsigned int cutoff;
1388 int connect_attempts;
1389 double nat_percentage;
1392 nat_percentage = 0.6; /* FIXME: default percentage? */
1393 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1398 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1399 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1400 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1404 GNUNET_free (p_string);
1409 cutoff = (unsigned int) (nat_percentage * pg->total);
1411 connect_attempts = 0;
1413 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1415 for (inner_count = outer_count + 1; inner_count < pg->total;
1418 if ((outer_count > cutoff) || (inner_count > cutoff))
1421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1422 "Connecting peer %d to peer %d\n",
1423 outer_count, inner_count);
1425 connect_attempts += proc(pg, outer_count, inner_count);
1430 return connect_attempts;
1435 * Create a topology given a peer group (set of running peers)
1436 * and a connection processor.
1438 * @param pg the peergroup to create the topology on
1439 * @param proc the connection processor to call to actually set
1440 * up connections between two peers
1442 * @return the number of connections that were set up
1446 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1448 unsigned int i, j, k;
1449 unsigned int square;
1452 unsigned int toggle = 1;
1453 unsigned int nodeToConnect;
1454 unsigned int natLog;
1455 unsigned int node1Row;
1456 unsigned int node1Col;
1457 unsigned int node2Row;
1458 unsigned int node2Col;
1459 unsigned int distance;
1460 double probability, random, percentage;
1461 unsigned int smallWorldConnections;
1462 unsigned int small_world_it;
1464 int connect_attempts;
1465 square = floor (sqrt (pg->total));
1469 percentage = 0.5; /* FIXME: default percentage? */
1470 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1475 if (sscanf(p_string, "%lf", &percentage) != 1)
1476 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1477 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1481 GNUNET_free (p_string);
1483 if (percentage < 0.0)
1485 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1486 _("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1487 "PERCENTAGE", "TESTING", percentage);
1490 probability = 0.5; /* FIXME: default percentage? */
1491 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1496 if (sscanf(p_string, "%lf", &probability) != 1)
1497 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1498 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1502 GNUNET_free (p_string);
1504 if (square * square != pg->total)
1506 while (rows * cols < pg->total)
1508 if (toggle % 2 == 0)
1517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1518 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1522 connect_attempts = 0;
1523 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1524 * to the node to its right and above. Once this is over, we'll have our torus!
1525 * Special case for the last node (if the rows and columns are not equal), connect
1526 * to the first in the row to maintain topology.
1528 for (i = 0; i < pg->total; i++)
1530 /* First connect to the node to the right */
1531 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1532 nodeToConnect = i + 1;
1533 else if (i + 1 == pg->total)
1534 nodeToConnect = rows * cols - cols;
1536 nodeToConnect = i - cols + 1;
1538 connect_attempts += proc (pg, i, nodeToConnect);
1541 nodeToConnect = (rows * cols) - cols + i;
1543 nodeToConnect = i - cols;
1545 if (nodeToConnect < pg->total)
1546 connect_attempts += proc (pg, i, nodeToConnect);
1548 natLog = log (pg->total);
1549 #if VERBOSE_TESTING > 2
1550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1551 _("natural log of %d is %d, will run %d iterations\n"),
1552 pg->total, natLog, (int) (natLog * percentage));
1553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1555 smallWorldConnections = 0;
1556 small_world_it = (unsigned int)(natLog * percentage);
1557 if (small_world_it < 1)
1559 GNUNET_assert(small_world_it > 0 && small_world_it < (unsigned int)-1);
1560 for (i = 0; i < small_world_it; i++)
1562 for (j = 0; j < pg->total; j++)
1564 /* Determine the row and column of node at position j on the 2d torus */
1565 node1Row = j / cols;
1566 node1Col = j - (node1Row * cols);
1567 for (k = 0; k < pg->total; k++)
1569 /* Determine the row and column of node at position k on the 2d torus */
1570 node2Row = k / cols;
1571 node2Col = k - (node2Row * cols);
1572 /* Simple Cartesian distance */
1573 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1576 /* Calculate probability as 1 over the square of the distance */
1577 probability = 1.0 / (distance * distance);
1578 /* Choose a random value between 0 and 1 */
1579 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1580 UINT64_MAX)) / ( (double) UINT64_MAX);
1581 /* If random < probability, then connect the two nodes */
1582 if (random < probability)
1583 smallWorldConnections += proc (pg, j, k);
1589 connect_attempts += smallWorldConnections;
1590 #if VERBOSE_TESTING > 2
1591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1592 _("Total connections added for small world: %d!\n"),
1593 smallWorldConnections);
1595 return connect_attempts;
1599 * Create a topology given a peer group (set of running peers)
1600 * and a connection processor.
1602 * @param pg the peergroup to create the topology on
1603 * @param proc the connection processor to call to actually set
1604 * up connections between two peers
1606 * @return the number of connections that were set up
1610 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1613 unsigned int outer_count;
1614 unsigned int inner_count;
1615 int connect_attempts;
1619 probability = 0.5; /* FIXME: default percentage? */
1620 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1625 if (sscanf(p_string, "%lf", &probability) != 1)
1626 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1627 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1631 GNUNET_free (p_string);
1633 connect_attempts = 0;
1634 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1636 for (inner_count = outer_count + 1; inner_count < pg->total;
1639 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1640 UINT64_MAX)) / ( (double) UINT64_MAX);
1642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1643 _("rand is %f probability is %f\n"), temp_rand,
1646 if (temp_rand < probability)
1648 connect_attempts += proc (pg, outer_count, inner_count);
1653 return connect_attempts;
1657 * Create a topology given a peer group (set of running peers)
1658 * and a connection processor. This particular function creates
1659 * the connections for a 2d-torus, plus additional "closest"
1660 * connections per peer.
1662 * @param pg the peergroup to create the topology on
1663 * @param proc the connection processor to call to actually set
1664 * up connections between two peers
1666 * @return the number of connections that were set up
1670 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1673 unsigned int square;
1676 unsigned int toggle = 1;
1677 unsigned int nodeToConnect;
1678 int connect_attempts;
1680 connect_attempts = 0;
1682 square = floor (sqrt (pg->total));
1686 if (square * square != pg->total)
1688 while (rows * cols < pg->total)
1690 if (toggle % 2 == 0)
1699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1700 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1703 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1704 * to the node to its right and above. Once this is over, we'll have our torus!
1705 * Special case for the last node (if the rows and columns are not equal), connect
1706 * to the first in the row to maintain topology.
1708 for (i = 0; i < pg->total; i++)
1710 /* First connect to the node to the right */
1711 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1712 nodeToConnect = i + 1;
1713 else if (i + 1 == pg->total)
1714 nodeToConnect = rows * cols - cols;
1716 nodeToConnect = i - cols + 1;
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1719 "Connecting peer %d to peer %d\n",
1722 connect_attempts += proc(pg, i, nodeToConnect);
1724 /* Second connect to the node immediately above */
1726 nodeToConnect = (rows * cols) - cols + i;
1728 nodeToConnect = i - cols;
1730 if (nodeToConnect < pg->total)
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1734 "Connecting peer %d to peer %d\n",
1737 connect_attempts += proc(pg, i, nodeToConnect);
1742 return connect_attempts;
1747 * Create a topology given a peer group (set of running peers)
1748 * and a connection processor.
1750 * @param pg the peergroup to create the topology on
1751 * @param proc the connection processor to call to actually set
1752 * up connections between two peers
1754 * @return the number of connections that were set up
1758 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1760 unsigned int outer_count;
1761 unsigned int inner_count;
1762 int connect_attempts;
1764 connect_attempts = 0;
1766 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1768 for (inner_count = outer_count + 1; inner_count < pg->total;
1772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1773 "Connecting peer %d to peer %d\n",
1774 outer_count, inner_count);
1776 connect_attempts += proc(pg, outer_count, inner_count);
1780 return connect_attempts;
1784 * Create a topology given a peer group (set of running peers)
1785 * and a connection processor.
1787 * @param pg the peergroup to create the topology on
1788 * @param proc the connection processor to call to actually set
1789 * up connections between two peers
1791 * @return the number of connections that were set up
1795 create_line (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1798 int connect_attempts;
1800 connect_attempts = 0;
1802 /* Connect each peer to the next highest numbered peer */
1803 for (count = 0; count < pg->total - 1; count++)
1806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1807 "Connecting peer %d to peer %d\n",
1810 connect_attempts += proc(pg, count, count + 1);
1813 return connect_attempts;
1817 * Create a topology given a peer group (set of running peers)
1818 * and a connection processor.
1820 * @param pg the peergroup to create the topology on
1821 * @param proc the connection processor to call to actually set
1822 * up connections between two peers
1824 * @return the number of connections that were set up
1828 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1831 int connect_attempts;
1833 connect_attempts = 0;
1835 /* Connect each peer to the next highest numbered peer */
1836 for (count = 0; count < pg->total - 1; count++)
1839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1840 "Connecting peer %d to peer %d\n",
1843 connect_attempts += proc(pg, count, count + 1);
1846 /* Connect the last peer to the first peer */
1847 connect_attempts += proc(pg, pg->total - 1, 0);
1849 return connect_attempts;
1854 * Iterator for writing friends of a peer to a file.
1856 * @param cls closure, an open writable file handle
1857 * @param key the key the daemon was stored under
1858 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1860 * @return GNUNET_YES to continue iteration
1862 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1863 * with a single file_iterator that takes a closure which contains
1864 * the prefix to write before the peer. Then this could be used
1865 * for blacklisting multiple transports and writing the friend
1866 * file. I'm sure *someone* will complain loudly about other
1867 * things that negate these functions even existing so no point in
1871 friend_file_iterator (void *cls,
1872 const GNUNET_HashCode * key,
1875 FILE *temp_friend_handle = cls;
1876 struct GNUNET_TESTING_Daemon *peer = value;
1877 struct GNUNET_PeerIdentity *temppeer;
1878 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1880 temppeer = &peer->id;
1881 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1882 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1887 struct BlacklistContext
1890 * The (open) file handle to write to
1892 FILE *temp_file_handle;
1895 * The transport that this peer will be blacklisted on.
1901 * Iterator for writing blacklist data to appropriate files.
1903 * @param cls closure, an open writable file handle
1904 * @param key the key the daemon was stored under
1905 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1907 * @return GNUNET_YES to continue iteration
1910 blacklist_file_iterator (void *cls,
1911 const GNUNET_HashCode * key,
1914 struct BlacklistContext *blacklist_ctx = cls;
1915 //FILE *temp_blacklist_handle = cls;
1916 struct GNUNET_TESTING_Daemon *peer = value;
1917 struct GNUNET_PeerIdentity *temppeer;
1918 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1920 temppeer = &peer->id;
1921 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1922 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1928 * Create the friend files based on the PeerConnection's
1929 * of each peer in the peer group, and copy the files
1930 * to the appropriate place
1932 * @param pg the peer group we are dealing with
1935 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1937 FILE *temp_friend_handle;
1938 unsigned int pg_iter;
1939 char *temp_service_path;
1940 struct GNUNET_OS_Process **procarr;
1943 enum GNUNET_OS_ProcessStatusType type;
1944 unsigned long return_code;
1949 procarr = GNUNET_malloc(sizeof(struct GNUNET_OS_Process *) * pg->total);
1950 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1952 mytemp = GNUNET_DISK_mktemp("friends");
1953 GNUNET_assert(mytemp != NULL);
1954 temp_friend_handle = fopen (mytemp, "wt");
1955 GNUNET_assert(temp_friend_handle != NULL);
1956 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1957 fclose(temp_friend_handle);
1960 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1962 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1963 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1966 if (UNLINK (mytemp) != 0)
1967 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1968 GNUNET_free (mytemp);
1972 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1974 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1975 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1976 "mv", mytemp, arg, NULL);
1978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1979 _("Copying file with command cp %s %s\n"), mytemp, arg);
1984 else /* Remote, scp the file to the correct place */
1986 if (NULL != pg->peers[pg_iter].daemon->username)
1987 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1989 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1990 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1991 "scp", mytemp, arg, NULL);
1994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1995 _("Copying file with command scp %s %s\n"), mytemp, arg);
1999 GNUNET_free (temp_service_path);
2000 GNUNET_free (mytemp);
2004 ret = GNUNET_SYSERR;
2005 while ((count < max_wait) && (ret != GNUNET_OK))
2008 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2012 _("Checking copy status of file %d\n"), pg_iter);
2014 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2016 if (GNUNET_OS_process_status(procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2018 ret = GNUNET_SYSERR;
2020 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
2022 ret = GNUNET_SYSERR;
2026 GNUNET_OS_process_close (procarr[pg_iter]);
2027 procarr[pg_iter] = NULL;
2029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2030 _("File %d copied\n"), pg_iter);
2036 if (ret == GNUNET_SYSERR)
2038 /* FIXME: why sleep here? -CG */
2044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2045 _("Finished copying all friend files!\n"));
2047 GNUNET_free(procarr);
2053 * Create the blacklist files based on the PeerConnection's
2054 * of each peer in the peer group, and copy the files
2055 * to the appropriate place.
2057 * @param pg the peer group we are dealing with
2058 * @param transports space delimited list of transports to blacklist
2061 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, const char *transports)
2063 FILE *temp_file_handle;
2064 static struct BlacklistContext blacklist_ctx;
2065 unsigned int pg_iter;
2066 char *temp_service_path;
2067 struct GNUNET_OS_Process **procarr;
2070 enum GNUNET_OS_ProcessStatusType type;
2071 unsigned long return_code;
2078 char *temp_transports;
2080 procarr = GNUNET_malloc(sizeof(struct GNUNET_OS_Process *) * pg->total);
2081 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2083 mytemp = GNUNET_DISK_mktemp("blacklist");
2084 GNUNET_assert(mytemp != NULL);
2085 temp_file_handle = fopen (mytemp, "wt");
2086 GNUNET_assert(temp_file_handle != NULL);
2087 temp_transports = GNUNET_strdup(transports);
2088 blacklist_ctx.temp_file_handle = temp_file_handle;
2089 transport_len = strlen(temp_transports) + 1;
2092 for (i = 0; i < transport_len; i++)
2094 if ((temp_transports[i] == ' ') && (pos == NULL))
2095 continue; /* At start of string (whitespace) */
2096 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2098 temp_transports[i] = '\0';
2099 blacklist_ctx.transport = pos;
2100 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
2102 } /* At beginning of actual string */
2103 else if (pos == NULL)
2105 pos = &temp_transports[i];
2109 GNUNET_free (temp_transports);
2110 fclose(temp_file_handle);
2113 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
2115 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2116 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2119 if (UNLINK (mytemp) != 0)
2120 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
2121 GNUNET_free (mytemp);
2125 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2127 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2128 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2129 "mv", mytemp, arg, NULL);
2131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2132 _("Copying file with command cp %s %s\n"), mytemp, arg);
2137 else /* Remote, scp the file to the correct place */
2139 if (NULL != pg->peers[pg_iter].daemon->username)
2140 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
2142 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
2143 procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
2144 "scp", mytemp, arg, NULL);
2147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2148 _("Copying file with command scp %s %s\n"), mytemp, arg);
2152 GNUNET_free (temp_service_path);
2153 GNUNET_free (mytemp);
2157 ret = GNUNET_SYSERR;
2158 while ((count < max_wait) && (ret != GNUNET_OK))
2161 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2165 _("Checking copy status of file %d\n"), pg_iter);
2167 if (procarr[pg_iter] != NULL) /* Check for already completed! */
2169 if (GNUNET_OS_process_status(procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2171 ret = GNUNET_SYSERR;
2173 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
2175 ret = GNUNET_SYSERR;
2179 GNUNET_OS_process_close (procarr[pg_iter]);
2180 procarr[pg_iter] = NULL;
2182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2183 _("File %d copied\n"), pg_iter);
2189 if (ret == GNUNET_SYSERR)
2191 /* FIXME: why sleep here? -CG */
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 _("Finished copying all blacklist files!\n"));
2200 GNUNET_free(procarr);
2206 * Internal notification of a connection, kept so that we can ensure some connections
2207 * happen instead of flooding all testing daemons with requests to connect.
2209 static void internal_connect_notify (void *cls,
2210 const struct GNUNET_PeerIdentity *first,
2211 const struct GNUNET_PeerIdentity *second,
2213 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2214 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2215 struct GNUNET_TESTING_Daemon *first_daemon,
2216 struct GNUNET_TESTING_Daemon *second_daemon,
2219 struct ConnectTopologyContext *ct_ctx = cls;
2220 struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
2221 outstanding_connects--;
2222 ct_ctx->remaining_connections--;
2223 if (ct_ctx->remaining_connections == 0)
2225 if (ct_ctx->notify_connections_done != NULL)
2226 ct_ctx->notify_connections_done(ct_ctx->notify_cls, NULL);
2227 GNUNET_free(ct_ctx);
2230 if (pg->notify_connection != NULL)
2231 pg->notify_connection (pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
2236 * Either delay a connection (because there are too many outstanding)
2237 * or schedule it for right now.
2239 * @param cls a connection context
2240 * @param tc the task runtime context
2242 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2244 struct ConnectContext *connect_context = cls;
2246 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2249 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2251 #if VERBOSE_TESTING > 2
2252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2253 _("Delaying connect, we have too many outstanding connections!\n"));
2255 GNUNET_SCHEDULER_add_delayed(connect_context->ct_ctx->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_connect, connect_context);
2259 #if VERBOSE_TESTING > 2
2260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2261 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
2263 outstanding_connects++;
2264 GNUNET_TESTING_daemons_connect (connect_context->first,
2265 connect_context->second,
2268 &internal_connect_notify,
2269 connect_context->ct_ctx);
2270 GNUNET_free(connect_context);
2276 * Iterator for actually scheduling connections to be created
2277 * between two peers.
2279 * @param cls closure, a GNUNET_TESTING_Daemon
2280 * @param key the key the second Daemon was stored under
2281 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2283 * @return GNUNET_YES to continue iteration
2286 connect_iterator (void *cls,
2287 const GNUNET_HashCode * key,
2290 struct ConnectTopologyContext *ct_ctx = cls;
2291 struct PeerData *first = ct_ctx->first;
2292 struct GNUNET_TESTING_Daemon *second = value;
2293 struct ConnectContext *connect_context;
2295 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2296 connect_context->first = first->daemon;
2297 connect_context->second = second;
2298 connect_context->ct_ctx = ct_ctx;
2299 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
2306 * Iterator for copying all entries in the allowed hashmap to the
2309 * @param cls closure, a GNUNET_TESTING_Daemon
2310 * @param key the key the second Daemon was stored under
2311 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2313 * @return GNUNET_YES to continue iteration
2316 copy_topology_iterator (void *cls,
2317 const GNUNET_HashCode * key,
2320 struct PeerData *first = cls;
2322 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2328 * Make the peers to connect the same as those that are allowed to be
2331 * @param pg the peer group
2334 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2336 unsigned int pg_iter;
2341 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2343 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
2344 if (GNUNET_SYSERR == ret)
2345 return GNUNET_SYSERR;
2347 total = total + ret;
2355 * Connect the topology as specified by the PeerConnection's
2356 * of each peer in the peer group
2358 * @param pg the peer group we are dealing with
2359 * @return the number of connections that will be attempted
2362 connect_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyCompletion notify_callback, void *notify_cls)
2364 unsigned int pg_iter;
2367 struct ConnectTopologyContext *ct_ctx;
2369 struct PeerConnection *connection_iter;
2370 struct ConnectContext *connect_context;
2374 ct_ctx = GNUNET_malloc(sizeof(struct ConnectTopologyContext));
2375 ct_ctx->notify_connections_done = notify_callback;
2376 ct_ctx->notify_cls = notify_cls;
2379 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2381 total += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2386 GNUNET_free(ct_ctx);
2389 ct_ctx->remaining_connections = total;
2392 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2394 ct_ctx->first = &pg->peers[pg_iter];
2395 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, ct_ctx);
2396 GNUNET_assert(GNUNET_SYSERR != ret && ret >= 0);
2397 total = total + ret;
2400 connection_iter = FIXME;
2401 while (connection_iter != NULL)
2403 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2404 connect_context->pg = pg;
2405 connect_context->first = FIXME;
2406 connect_context->second = connection_iter->daemon;
2407 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
2408 connection_iter = connection_iter->next;
2417 * Takes a peer group and creates a topology based on the
2418 * one specified. Creates a topology means generates friend
2419 * files for the peers so they can only connect to those allowed
2420 * by the topology. This will only have an effect once peers
2421 * are started if the FRIENDS_ONLY option is set in the base
2422 * config. Also takes an optional restrict topology which
2423 * disallows connections based on a particular transport
2424 * UNLESS they are specified in the restricted topology.
2426 * @param pg the peer group struct representing the running peers
2427 * @param topology which topology to connect the peers in
2428 * @param restrict_topology allow only direct TCP connections in this topology
2429 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2430 * @param restrict_transports space delimited list of transports to blacklist
2431 * to create restricted topology
2433 * @return the maximum number of connections were all allowed peers
2434 * connected to each other
2437 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2438 enum GNUNET_TESTING_Topology topology,
2439 enum GNUNET_TESTING_Topology restrict_topology,
2440 const char *restrict_transports)
2443 unsigned int num_connections;
2444 int unblacklisted_connections;
2448 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2451 _("Creating clique topology\n"));
2453 num_connections = create_clique (pg, &add_allowed_connections);
2455 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2458 _("Creating small world (ring) topology\n"));
2460 num_connections = create_small_world_ring (pg, &add_allowed_connections);
2462 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2465 _("Creating small world (2d-torus) topology\n"));
2467 num_connections = create_small_world (pg, &add_allowed_connections);
2469 case GNUNET_TESTING_TOPOLOGY_RING:
2471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2472 _("Creating ring topology\n"));
2474 num_connections = create_ring (pg, &add_allowed_connections);
2476 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2479 _("Creating 2d torus topology\n"));
2481 num_connections = create_2d_torus (pg, &add_allowed_connections);
2483 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2486 _("Creating Erdos-Renyi topology\n"));
2488 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2490 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2493 _("Creating InterNAT topology\n"));
2495 num_connections = create_nated_internet (pg, &add_allowed_connections);
2497 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2500 _("Creating Scale Free topology\n"));
2502 num_connections = create_scale_free (pg, &add_allowed_connections);
2504 case GNUNET_TESTING_TOPOLOGY_LINE:
2506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2507 _("Creating straight line topology\n"));
2509 num_connections = create_line (pg, &add_allowed_connections);
2511 case GNUNET_TESTING_TOPOLOGY_NONE:
2513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2514 _("Creating no allowed topology (all peers can connect at core level)\n"));
2516 num_connections = 0;
2519 num_connections = 0;
2523 if (num_connections < 0)
2524 return GNUNET_SYSERR;
2526 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2528 ret = create_and_copy_friend_files(pg);
2529 if (ret != GNUNET_OK)
2532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2533 _("Failed during friend file copying!\n"));
2535 return GNUNET_SYSERR;
2540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2541 _("Friend files created/copied successfully!\n"));
2546 /* Use the create clique method to initially set all connections as blacklisted. */
2547 if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2548 create_clique (pg, &blacklist_connections);
2550 unblacklisted_connections = 0;
2551 /* Un-blacklist connections as per the topology specified */
2552 switch (restrict_topology)
2554 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2557 _("Blacklisting all but clique topology\n"));
2559 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2561 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2564 _("Blacklisting all but small world (ring) topology\n"));
2566 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2568 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2571 _("Blacklisting all but small world (2d-torus) topology\n"));
2573 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2575 case GNUNET_TESTING_TOPOLOGY_RING:
2577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2578 _("Blacklisting all but ring topology\n"));
2580 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2582 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2585 _("Blacklisting all but 2d torus topology\n"));
2587 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2589 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2592 _("Blacklisting all but Erdos-Renyi topology\n"));
2594 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2596 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2599 _("Blacklisting all but InterNAT topology\n"));
2601 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2603 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2606 _("Blacklisting all but Scale Free topology\n"));
2608 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2610 case GNUNET_TESTING_TOPOLOGY_LINE:
2612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2613 _("Blacklisting all but straight line topology\n"));
2615 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2617 case GNUNET_TESTING_TOPOLOGY_NONE:
2619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2620 _("Creating no blacklist topology (all peers can connect at transport level)\n"));
2626 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2628 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2629 if (ret != GNUNET_OK)
2632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2633 _("Failed during blacklist file copying!\n"));
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2641 _("Blacklist files created/copied successfully!\n"));
2645 return num_connections;
2648 struct RandomContext
2653 struct GNUNET_TESTING_PeerGroup *pg;
2656 * uid of the first peer
2661 * Peer data for first peer.
2663 struct PeerData *first;
2666 * Random percentage to use
2671 struct MinimumContext
2676 struct GNUNET_TESTING_PeerGroup *pg;
2679 * uid of the first peer
2684 * Peer data for first peer.
2686 struct PeerData *first;
2689 * Number of conns per peer
2691 unsigned int num_to_add;
2694 * Permuted array of all possible connections. Only add the Nth
2695 * peer if it's in the Nth position.
2697 unsigned int *pg_array;
2700 * What number is the current element we are iterating over?
2702 unsigned int current;
2710 struct GNUNET_TESTING_PeerGroup *pg;
2713 * uid of the first peer
2718 * uid of the second peer
2720 uint32_t second_uid;
2723 * Peer data for first peer.
2725 struct PeerData *first;
2728 * Which peer has been chosen as the one to add?
2730 unsigned int chosen;
2733 * What number is the current element we are iterating over?
2735 unsigned int current;
2739 * Iterator for choosing random peers to connect.
2741 * @param cls closure, a RandomContext
2742 * @param key the key the second Daemon was stored under
2743 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2745 * @return GNUNET_YES to continue iteration
2748 random_connect_iterator (void *cls,
2749 const GNUNET_HashCode * key,
2752 struct RandomContext *random_ctx = cls;
2753 double random_number;
2754 uint32_t second_pos;
2755 GNUNET_HashCode first_hash;
2756 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2757 UINT64_MAX)) / ( (double) UINT64_MAX);
2758 if (random_number < random_ctx->percentage)
2760 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2762 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2763 uid_from_hash(key, &second_pos);
2764 hash_from_uid(random_ctx->first_uid, &first_hash);
2765 GNUNET_assert(random_ctx->pg->total > second_pos);
2766 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2772 * Iterator for adding at least X peers to a peers connection set.
2774 * @param cls closure, MinimumContext
2775 * @param key the key the second Daemon was stored under
2776 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2778 * @return GNUNET_YES to continue iteration
2781 minimum_connect_iterator (void *cls,
2782 const GNUNET_HashCode * key,
2785 struct MinimumContext *min_ctx = cls;
2786 uint32_t second_pos;
2787 GNUNET_HashCode first_hash;
2790 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2792 for (i = 0; i < min_ctx->num_to_add; i++)
2794 if (min_ctx->pg_array[i] == min_ctx->current)
2796 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2797 uid_from_hash(key, &second_pos);
2798 hash_from_uid(min_ctx->first_uid, &first_hash);
2799 GNUNET_assert(min_ctx->pg->total > second_pos);
2800 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));
2801 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2802 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2809 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2815 * Iterator for adding peers to a connection set based on a depth first search.
2817 * @param cls closure, MinimumContext
2818 * @param key the key the second daemon was stored under
2819 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2821 * @return GNUNET_YES to continue iteration
2824 dfs_connect_iterator (void *cls,
2825 const GNUNET_HashCode * key,
2828 struct DFSContext *dfs_ctx = cls;
2829 GNUNET_HashCode first_hash;
2831 if (dfs_ctx->current == dfs_ctx->chosen)
2833 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2834 uid_from_hash(key, &dfs_ctx->second_uid);
2835 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2836 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));
2837 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2838 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2839 return GNUNET_NO; /* We have found our peer, don't iterate more */
2848 * From the set of connections possible, choose percentage percent of connections
2849 * to actually connect.
2851 * @param pg the peergroup we are dealing with
2852 * @param percentage what percent of total connections to make
2855 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2857 struct RandomContext random_ctx;
2860 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2862 random_ctx.first_uid = pg_iter;
2863 random_ctx.first = &pg->peers[pg_iter];
2864 random_ctx.percentage = percentage;
2866 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2867 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2868 /* Now remove the old connections */
2869 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2870 /* And replace with the random set */
2871 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2876 * From the set of connections possible, choose at least num connections per
2879 * @param pg the peergroup we are dealing with
2880 * @param num how many connections at least should each peer have (if possible)?
2883 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2885 struct MinimumContext minimum_ctx;
2888 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2890 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2893 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2895 minimum_ctx.first_uid = pg_iter;
2896 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2897 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2898 minimum_ctx.first = &pg->peers[pg_iter];
2899 minimum_ctx.pg = pg;
2900 minimum_ctx.num_to_add = num;
2901 minimum_ctx.current = 0;
2902 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2903 &minimum_connect_iterator,
2907 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2909 /* Remove the "old" connections */
2910 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2911 /* And replace with the working set */
2912 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2919 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2922 unsigned int pg_iter;
2926 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2928 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2935 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2938 unsigned int pg_iter;
2942 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2944 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2951 struct FindClosestContext
2954 * The currently known closest peer.
2956 struct GNUNET_TESTING_Daemon *closest;
2959 * The info for the peer we are adding connections for.
2961 struct PeerData *curr_peer;
2964 * The distance (bits) between the current
2965 * peer and the currently known closest.
2967 unsigned int closest_dist;
2970 * The offset of the closest known peer in
2973 unsigned int closest_num;
2977 * Iterator over hash map entries of the allowed
2978 * peer connections. Find the closest, not already
2979 * connected peer and return it.
2981 * @param cls closure (struct FindClosestContext)
2982 * @param key current key code (hash of offset in pg)
2983 * @param value value in the hash map - a GNUNET_TESTING_Daemon
2984 * @return GNUNET_YES if we should continue to
2989 int find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
2991 struct FindClosestContext *closest_ctx = cls;
2992 struct GNUNET_TESTING_Daemon *daemon = value;
2994 if (((closest_ctx->closest == NULL) ||
2995 (GNUNET_CRYPTO_hash_matching_bits(&daemon->id.hashPubKey, &closest_ctx->curr_peer->daemon->id.hashPubKey) > closest_ctx->closest_dist))
2996 && (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains(closest_ctx->curr_peer->connect_peers, key)))
2998 closest_ctx->closest_dist = GNUNET_CRYPTO_hash_matching_bits(&daemon->id.hashPubKey, &closest_ctx->curr_peer->daemon->id.hashPubKey);
2999 closest_ctx->closest = daemon;
3000 uid_from_hash(key, &closest_ctx->closest_num);
3006 * From the set of connections possible, choose at num connections per
3007 * peer based on depth which are closest out of those allowed. Guaranteed
3008 * to add num peers to connect to, provided there are that many peers
3009 * in the underlay topology to connect to.
3011 * @param pg the peergroup we are dealing with
3012 * @param num how many connections at least should each peer have (if possible)?
3013 * @param proc processor to actually add the connections
3016 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num, GNUNET_TESTING_ConnectionProcessor proc)
3018 struct FindClosestContext closest_ctx;
3022 for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
3024 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3026 closest_ctx.curr_peer = &pg->peers[pg_iter];
3027 closest_ctx.closest = NULL;
3028 closest_ctx.closest_dist = 0;
3029 closest_ctx.closest_num = 0;
3030 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &find_closest_peers, &closest_ctx);
3031 if (closest_ctx.closest != NULL)
3033 GNUNET_assert((0 <= closest_ctx.closest_num) && (closest_ctx.closest_num < pg->total));
3034 proc(pg, pg_iter, closest_ctx.closest_num);
3041 * From the set of connections possible, choose at least num connections per
3042 * peer based on depth first traversal of peer connections. If DFS leaves
3043 * peers unconnected, ensure those peers get connections.
3045 * @param pg the peergroup we are dealing with
3046 * @param num how many connections at least should each peer have (if possible)?
3049 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
3051 struct DFSContext dfs_ctx;
3054 uint32_t starting_peer;
3055 uint32_t least_connections;
3056 GNUNET_HashCode second_hash;
3058 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3060 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
3065 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
3067 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3069 least_connections = -1; /* Set to very high number */
3070 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3072 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
3074 starting_peer = pg_iter;
3075 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
3080 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
3086 /* Choose a random peer from the chosen peers set of connections to add */
3087 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
3088 dfs_ctx.first_uid = starting_peer;
3089 dfs_ctx.first = &pg->peers[starting_peer];
3091 dfs_ctx.current = 0;
3093 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
3094 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
3095 hash_from_uid(dfs_ctx.second_uid, &second_hash);
3096 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
3097 starting_peer = dfs_ctx.second_uid;
3100 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3102 /* Remove the "old" connections */
3103 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
3104 /* And replace with the working set */
3105 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
3110 * Internal callback for topology information for a particular peer.
3113 internal_topology_callback(void *cls,
3114 const struct GNUNET_PeerIdentity *peer,
3115 struct GNUNET_TIME_Relative latency, uint32_t distance)
3117 struct CoreContext *core_ctx = cls;
3118 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
3120 if (peer == NULL) /* Either finished, or something went wrong */
3122 iter_ctx->completed++;
3123 iter_ctx->connected--;
3124 /* One core context allocated per iteration, must free! */
3125 GNUNET_free(core_ctx);
3129 iter_ctx->topology_cb(iter_ctx->cls, &core_ctx->daemon->id, peer, latency, distance, NULL);
3132 if (iter_ctx->completed == iter_ctx->total)
3134 iter_ctx->topology_cb(iter_ctx->cls, NULL, NULL, GNUNET_TIME_relative_get_zero(), 0, NULL);
3135 /* Once all are done, free the iteration context */
3136 GNUNET_free(iter_ctx);
3142 * Check running topology iteration tasks, if below max start a new one, otherwise
3143 * schedule for some time in the future.
3146 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3148 struct CoreContext *core_context = cls;
3149 struct TopologyIterateContext *topology_context = (struct TopologyIterateContext *)core_context->iter_context;
3150 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3153 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3155 #if VERBOSE_TESTING > 2
3156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3157 _("Delaying connect, we have too many outstanding connections!\n"));
3159 GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_topology, core_context);
3163 #if VERBOSE_TESTING > 2
3164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3165 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3167 topology_context->connected++;
3168 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->sched, core_context->daemon->cfg, &internal_topology_callback, core_context))
3169 internal_topology_callback(core_context, NULL, GNUNET_TIME_relative_get_zero(), 0);
3175 * Iterate over all (running) peers in the peer group, retrieve
3176 * all connections that each currently has.
3179 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyTopology cb, void *cls)
3181 struct TopologyIterateContext *topology_context;
3182 struct CoreContext *core_ctx;
3184 unsigned int total_count;
3186 /* Allocate a single topology iteration context */
3187 topology_context = GNUNET_malloc(sizeof(struct TopologyIterateContext));
3188 topology_context->topology_cb = cb;
3189 topology_context->cls = cls;
3191 for (i = 0; i < pg->total; i++)
3193 if (pg->peers[i].daemon->running == GNUNET_YES)
3195 /* Allocate one core context per core we need to connect to */
3196 core_ctx = GNUNET_malloc(sizeof(struct CoreContext));
3197 core_ctx->daemon = pg->peers[i].daemon;
3198 /* Set back pointer to topology iteration context */
3199 core_ctx->iter_context = topology_context;
3200 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_topology, core_ctx);
3204 topology_context->total = total_count;
3209 * Callback function to process statistic values.
3210 * This handler is here only really to insert a peer
3211 * identity (or daemon) so the statistics can be uniquely
3212 * tied to a single running peer.
3214 * @param cls closure
3215 * @param subsystem name of subsystem that created the statistic
3216 * @param name the name of the datum
3217 * @param value the current value
3218 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
3219 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
3221 static int internal_stats_callback (void *cls,
3222 const char *subsystem,
3227 struct StatsCoreContext *core_context = cls;
3228 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3230 return stats_context->proc(stats_context->cls, &core_context->daemon->id, subsystem, name, value, is_persistent);
3234 * Internal continuation call for statistics iteration.
3236 * @param cls closure, the CoreContext for this iteration
3237 * @param success whether or not the statistics iterations
3238 * was canceled or not (we don't care)
3240 static void internal_stats_cont (void *cls, int success)
3242 struct StatsCoreContext *core_context = cls;
3243 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3245 stats_context->connected--;
3246 stats_context->completed++;
3248 if (stats_context->completed == stats_context->total)
3250 stats_context->cont(stats_context->cls, GNUNET_YES);
3251 GNUNET_free(stats_context);
3254 if (core_context->stats_handle != NULL)
3255 GNUNET_STATISTICS_destroy(core_context->stats_handle, GNUNET_NO);
3257 GNUNET_free(core_context);
3261 * Check running topology iteration tasks, if below max start a new one, otherwise
3262 * schedule for some time in the future.
3265 schedule_get_statistics(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3267 struct StatsCoreContext *core_context = cls;
3268 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3270 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3273 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3275 #if VERBOSE_TESTING > 2
3276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3277 _("Delaying connect, we have too many outstanding connections!\n"));
3279 GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_statistics, core_context);
3283 #if VERBOSE_TESTING > 2
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3285 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3288 stats_context->connected++;
3289 core_context->stats_handle = GNUNET_STATISTICS_create(core_context->daemon->sched, "testing", core_context->daemon->cfg);
3290 if (core_context->stats_handle == NULL)
3292 internal_stats_cont (core_context, GNUNET_NO);
3296 core_context->stats_get_handle = GNUNET_STATISTICS_get(core_context->stats_handle, NULL, NULL, GNUNET_TIME_relative_get_forever(), &internal_stats_cont, &internal_stats_callback, core_context);
3297 if (core_context->stats_get_handle == NULL)
3298 internal_stats_cont (core_context, GNUNET_NO);
3303 struct DuplicateStats
3306 * Next item in the list
3308 struct DuplicateStats *next;
3311 * Nasty string, concatenation of relevant information.
3313 char *unique_string;
3317 * Check whether the combination of port/host/unix domain socket
3318 * already exists in the list of peers being checked for statistics.
3320 * @param pg the peergroup in question
3321 * @param specific_peer the peer we're concerned with
3322 * @param stats_list the list to return to the caller
3324 * @return GNUNET_YES if the statistics instance has been seen already,
3325 * GNUNET_NO if not (and we may have added it to the list)
3328 stats_check_existing(struct GNUNET_TESTING_PeerGroup *pg, struct PeerData *specific_peer, struct DuplicateStats **stats_list)
3330 struct DuplicateStats *pos;
3331 char *unix_domain_socket;
3332 unsigned long long port;
3334 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(pg->cfg, "testing", "single_statistics_per_host"))
3335 return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
3338 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(specific_peer->cfg, "statistics", "unixpath", &unix_domain_socket))
3341 GNUNET_CONFIGURATION_get_value_number(specific_peer->cfg, "statistics", "port", &port);
3343 if (specific_peer->daemon->hostname != NULL)
3344 GNUNET_asprintf(&to_match, "%s%s%llu", specific_peer->daemon->hostname, unix_domain_socket, port);
3346 GNUNET_asprintf(&to_match, "%s%llu", unix_domain_socket, port);
3350 if (0 == strcmp(to_match, pos->unique_string))
3352 GNUNET_free(to_match);
3357 pos = GNUNET_malloc(sizeof(struct DuplicateStats));
3358 pos->unique_string = to_match;
3359 pos->next = *stats_list;
3365 * Iterate over all (running) peers in the peer group, retrieve
3366 * all statistics from each.
3369 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
3370 GNUNET_STATISTICS_Callback cont,
3371 GNUNET_TESTING_STATISTICS_Iterator proc, void *cls)
3373 struct StatsIterateContext *stats_context;
3374 struct StatsCoreContext *core_ctx;
3376 unsigned int total_count;
3377 struct DuplicateStats *stats_list;
3378 struct DuplicateStats *pos;
3381 /* Allocate a single stats iteration context */
3382 stats_context = GNUNET_malloc(sizeof(struct StatsIterateContext));
3383 stats_context->cont = cont;
3384 stats_context->proc = proc;
3385 stats_context->cls = cls;
3388 for (i = 0; i < pg->total; i++)
3390 if ((pg->peers[i].daemon->running == GNUNET_YES) && (GNUNET_NO == stats_check_existing(pg, &pg->peers[i], &stats_list)))
3392 /* Allocate one core context per core we need to connect to */
3393 core_ctx = GNUNET_malloc(sizeof(struct StatsCoreContext));
3394 core_ctx->daemon = pg->peers[i].daemon;
3395 /* Set back pointer to topology iteration context */
3396 core_ctx->iter_context = stats_context;
3397 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_statistics, core_ctx);
3402 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Retrieving stats from %u total instances.\n", total_count);
3403 stats_context->total = total_count;
3404 if (stats_list != NULL)
3409 GNUNET_free(pos->unique_string);
3410 stats_list = pos->next;
3412 pos = stats_list->next;
3419 * There are many ways to connect peers that are supported by this function.
3420 * To connect peers in the same topology that was created via the
3421 * GNUNET_TESTING_create_topology, the topology variable must be set to
3422 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
3423 * a new instance of that topology will be generated and attempted to be
3424 * connected. This could result in some connections being impossible,
3425 * because some topologies are non-deterministic.
3427 * @param pg the peer group struct representing the running peers
3428 * @param topology which topology to connect the peers in
3429 * @param options options for connecting the topology
3430 * @param option_modifier modifier for options that take a parameter
3431 * @param notify_callback notification to be called once all connections completed
3432 * @param notify_cls closure for notification callback
3434 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
3437 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3438 enum GNUNET_TESTING_Topology topology,
3439 enum GNUNET_TESTING_TopologyOption options,
3440 double option_modifier,
3441 GNUNET_TESTING_NotifyCompletion notify_callback,
3446 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3447 #if VERBOSE_TOPOLOGY
3448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3449 _("Creating clique CONNECT topology\n"));
3451 create_clique (pg, &add_actual_connections);
3453 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3454 #if VERBOSE_TOPOLOGY
3455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3456 _("Creating small world (ring) CONNECT topology\n"));
3458 create_small_world_ring (pg, &add_actual_connections);
3460 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3461 #if VERBOSE_TOPOLOGY
3462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3463 _("Creating small world (2d-torus) CONNECT topology\n"));
3465 create_small_world (pg, &add_actual_connections);
3467 case GNUNET_TESTING_TOPOLOGY_RING:
3468 #if VERBOSE_TOPOLOGY
3469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3470 _("Creating ring CONNECT topology\n"));
3472 create_ring (pg, &add_actual_connections);
3474 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3475 #if VERBOSE_TOPOLOGY
3476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3477 _("Creating 2d torus CONNECT topology\n"));
3479 create_2d_torus (pg, &add_actual_connections);
3481 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3482 #if VERBOSE_TOPOLOGY
3483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3484 _("Creating Erdos-Renyi CONNECT topology\n"));
3486 create_erdos_renyi (pg, &add_actual_connections);
3488 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3489 #if VERBOSE_TOPOLOGY
3490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3491 _("Creating InterNAT CONNECT topology\n"));
3493 create_nated_internet (pg, &add_actual_connections);
3495 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3496 #if VERBOSE_TOPOLOGY
3497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3498 _("Creating Scale Free CONNECT topology\n"));
3500 create_scale_free (pg, &add_actual_connections);
3502 case GNUNET_TESTING_TOPOLOGY_LINE:
3503 #if VERBOSE_TOPOLOGY
3504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3505 _("Creating straight line CONNECT topology\n"));
3507 create_line (pg, &add_actual_connections);
3509 case GNUNET_TESTING_TOPOLOGY_NONE:
3510 #if VERBOSE_TOPOLOGY
3511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3512 _("Creating no CONNECT topology\n"));
3514 copy_allowed_topology(pg);
3517 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3518 _("Unknown topology specification, can't connect peers!\n"));
3519 return GNUNET_SYSERR;
3524 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
3525 #if VERBOSE_TOPOLOGY
3526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3527 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
3529 choose_random_connections(pg, option_modifier);
3531 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
3532 #if VERBOSE_TOPOLOGY
3533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3534 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3536 choose_minimum(pg, (unsigned int)option_modifier);
3538 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
3539 #if VERBOSE_TOPOLOGY
3540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3541 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3543 perform_dfs(pg, (int)option_modifier);
3545 case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
3546 #if VERBOSE_TOPOLOGY
3547 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3548 _("Finding additional %u closest peers each (if possible)\n"), (unsigned int)option_modifier);
3550 add_closest(pg, (unsigned int)option_modifier, &add_actual_connections);
3552 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
3554 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
3560 return connect_topology(pg, notify_callback, notify_cls);
3564 * Callback that is called whenever a hostkey is generated
3565 * for a peer. Call the real callback and decrement the
3566 * starting counter for the peergroup.
3568 * @param cls closure
3569 * @param id identifier for the daemon, NULL on error
3570 * @param d handle for the daemon
3571 * @param emsg error message (NULL on success)
3573 static void internal_hostkey_callback (void *cls,
3574 const struct GNUNET_PeerIdentity *id,
3575 struct GNUNET_TESTING_Daemon *d,
3578 struct InternalStartContext *internal_context = cls;
3579 internal_context->peer->pg->starting--;
3580 internal_context->peer->pg->started++;
3581 if (internal_context->hostkey_callback != NULL)
3582 internal_context->hostkey_callback(internal_context->hostkey_cls, id, d, emsg);
3583 else if (internal_context->peer->pg->started == internal_context->peer->pg->total)
3585 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
3586 GNUNET_TESTING_daemons_continue_startup(internal_context->peer->pg);
3591 * Callback that is called whenever a peer has finished starting.
3592 * Call the real callback and decrement the starting counter
3593 * for the peergroup.
3595 * @param cls closure
3596 * @param id identifier for the daemon, NULL on error
3597 * @param d handle for the daemon
3598 * @param emsg error message (NULL on success)
3600 static void internal_startup_callback (void *cls,
3601 const struct GNUNET_PeerIdentity *id,
3602 const struct GNUNET_CONFIGURATION_Handle *cfg,
3603 struct GNUNET_TESTING_Daemon *d,
3606 struct InternalStartContext *internal_context = cls;
3607 internal_context->peer->pg->starting--;
3608 if (internal_context->start_cb != NULL)
3609 internal_context->start_cb(internal_context->start_cb_cls, id, cfg, d, emsg);
3613 internal_continue_startup (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3615 struct InternalStartContext *internal_context = cls;
3617 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3622 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
3624 internal_context->peer->pg->starting++;
3625 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
3629 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_continue_startup, internal_context);
3635 * Callback for informing us about a successful
3636 * or unsuccessful churn start call.
3638 * @param cls a ChurnContext
3639 * @param id the peer identity of the started peer
3640 * @param cfg the handle to the configuration of the peer
3641 * @param d handle to the daemon for the peer
3642 * @param emsg NULL on success, non-NULL on failure
3646 churn_start_callback (void *cls,
3647 const struct GNUNET_PeerIdentity *id,
3648 const struct GNUNET_CONFIGURATION_Handle *cfg,
3649 struct GNUNET_TESTING_Daemon *d,
3652 struct ChurnRestartContext *startup_ctx = cls;
3653 struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
3655 unsigned int total_left;
3656 char *error_message;
3658 error_message = NULL;
3661 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3662 "Churn stop callback failed with error `%s'\n",
3664 churn_ctx->num_failed_start++;
3668 churn_ctx->num_to_start--;
3672 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3673 "Started peer, %d left.\n",
3674 churn_ctx->num_to_start);
3677 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3679 if (total_left == 0)
3681 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3682 GNUNET_asprintf(&error_message,
3683 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3684 churn_ctx->num_failed_start,
3685 churn_ctx->num_failed_stop);
3686 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3687 GNUNET_free_non_null(error_message);
3688 GNUNET_free(churn_ctx);
3689 GNUNET_free(startup_ctx);
3694 static void schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3696 struct PeerRestartContext *peer_restart_ctx = cls;
3697 struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
3699 if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
3700 GNUNET_SCHEDULER_add_delayed(peer_restart_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_churn_restart, peer_restart_ctx);
3703 GNUNET_TESTING_daemon_start_stopped(peer_restart_ctx->daemon,
3704 startup_ctx->timeout,
3705 &churn_start_callback,
3707 GNUNET_free(peer_restart_ctx);
3712 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3714 struct InternalStartContext *internal_context = cls;
3716 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3721 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
3723 internal_context->peer->pg->starting++;
3724 internal_context->peer->daemon = GNUNET_TESTING_daemon_start (internal_context->peer->pg->sched,
3725 internal_context->peer->cfg,
3726 internal_context->timeout,
3727 internal_context->hostname,
3728 internal_context->username,
3729 internal_context->sshport,
3730 &internal_hostkey_callback,
3732 &internal_startup_callback,
3737 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_start, internal_context);
3742 * Function which continues a peer group starting up
3743 * after successfully generating hostkeys for each peer.
3745 * @param pg the peer group to continue starting
3749 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
3754 for (i = 0; i < pg->total; i++)
3756 GNUNET_SCHEDULER_add_now (pg->sched, &internal_continue_startup, &pg->peers[i].internal_context);
3757 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
3762 * Start count gnunet instances with the same set of transports and
3763 * applications. The port numbers (any option called "PORT") will be
3764 * adjusted to ensure that no two peers running on the same system
3765 * have the same port(s) in their respective configurations.
3767 * @param sched scheduler to use
3768 * @param cfg configuration template to use
3769 * @param total number of daemons to start
3770 * @param timeout total time allowed for peers to start
3771 * @param hostkey_callback function to call on each peers hostkey generation
3772 * if NULL, peers will be started by this call, if non-null,
3773 * GNUNET_TESTING_daemons_continue_startup must be called after
3774 * successful hostkey generation
3775 * @param hostkey_cls closure for hostkey callback
3776 * @param cb function to call on each daemon that was started
3777 * @param cb_cls closure for cb
3778 * @param connect_callback function to call each time two hosts are connected
3779 * @param connect_callback_cls closure for connect_callback
3780 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
3782 * @return NULL on error, otherwise handle to control peer group
3784 struct GNUNET_TESTING_PeerGroup *
3785 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
3786 const struct GNUNET_CONFIGURATION_Handle *cfg,
3788 struct GNUNET_TIME_Relative timeout,
3789 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
3791 GNUNET_TESTING_NotifyDaemonRunning cb,
3793 GNUNET_TESTING_NotifyConnection
3794 connect_callback, void *connect_callback_cls,
3795 const struct GNUNET_TESTING_Host *hostnames)
3797 struct GNUNET_TESTING_PeerGroup *pg;
3798 const struct GNUNET_TESTING_Host *hostpos;
3804 const char *hostname;
3805 const char *username;
3806 char *baseservicehome;
3807 char *newservicehome;
3809 struct GNUNET_CONFIGURATION_Handle *pcfg;
3811 unsigned int hostcnt;
3824 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
3827 pg->notify_connection = connect_callback;
3828 pg->notify_connection_cls = connect_callback_cls;
3830 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
3831 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
3832 if (NULL != hostnames)
3835 hostpos = hostnames;
3836 while (hostpos != NULL)
3838 hostpos = hostpos->next;
3841 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3844 hostpos = hostnames;
3845 while (hostpos != NULL)
3847 pg->hosts[off].minport = LOW_PORT;
3848 pg->hosts[off].hostname = GNUNET_strdup(hostpos->hostname);
3849 if (hostpos->username != NULL)
3850 pg->hosts[off].username = GNUNET_strdup(hostpos->username);
3851 pg->hosts[off].sshport = hostpos->port;
3852 hostpos = hostpos->next;
3862 pg->num_hosts = off;
3866 /* skip leading spaces */
3867 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
3870 while ('\0' != *rpos)
3872 if (isspace ( (unsigned char) *rpos))
3876 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3878 start = GNUNET_strdup (hostnames);
3880 while ('\0' != *pos)
3882 if (isspace ( (unsigned char) *pos))
3885 if (strlen (start) > 0)
3887 pg->hosts[off].minport = LOW_PORT;
3888 pg->hosts[off++].hostname = start;
3894 if (strlen (start) > 0)
3896 pg->hosts[off].minport = LOW_PORT;
3897 pg->hosts[off++].hostname = start;
3901 GNUNET_free (start);
3902 GNUNET_free (pg->hosts);
3906 minport = 0; /* make gcc happy */
3914 for (off = 0; off < total; off++)
3918 hostname = pg->hosts[off % hostcnt].hostname;
3919 username = pg->hosts[off % hostcnt].username;
3920 sshport = pg->hosts[off % hostcnt].sshport;
3921 pcfg = make_config (cfg,
3922 &pg->hosts[off % hostcnt].minport,
3931 pcfg = make_config (cfg,
3939 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3941 ("Could not create configuration for peer number %u on `%s'!\n"),
3942 off, hostname == NULL ? "localhost" : hostname);
3947 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
3950 GNUNET_asprintf (&newservicehome,
3951 "%s/%d/", baseservicehome, off);
3952 GNUNET_free (baseservicehome);
3956 tmpdir = getenv ("TMPDIR");
3957 tmpdir = tmpdir ? tmpdir : "/tmp";
3958 GNUNET_asprintf (&newservicehome,
3961 "gnunet-testing-test-test", off);
3963 GNUNET_CONFIGURATION_set_value_string (pcfg,
3965 "SERVICEHOME", newservicehome);
3966 GNUNET_free (newservicehome);
3967 pg->peers[off].cfg = pcfg;
3968 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
3969 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
3970 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
3971 pg->peers[off].pg = pg;
3973 pg->peers[off].internal_context.peer = &pg->peers[off];
3974 pg->peers[off].internal_context.timeout = timeout;
3975 pg->peers[off].internal_context.hostname = hostname;
3976 pg->peers[off].internal_context.username = username;
3977 pg->peers[off].internal_context.sshport = sshport;
3978 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
3979 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
3980 pg->peers[off].internal_context.start_cb = cb;
3981 pg->peers[off].internal_context.start_cb_cls = cb_cls;
3983 GNUNET_SCHEDULER_add_now (sched, &internal_start, &pg->peers[off].internal_context);
3990 * Get a daemon by number, so callers don't have to do nasty
3991 * offsetting operation.
3993 struct GNUNET_TESTING_Daemon *
3994 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
3996 if (position < pg->total)
3997 return pg->peers[position].daemon;
4003 * Get a daemon by peer identity, so callers can
4004 * retrieve the daemon without knowing it's offset.
4006 * @param pg the peer group to retrieve the daemon from
4007 * @param peer_id the peer identity of the daemon to retrieve
4009 * @return the daemon on success, or NULL if no such peer identity is found
4011 struct GNUNET_TESTING_Daemon *
4012 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_PeerIdentity *peer_id)
4016 for (i = 0; i < pg->total; i ++)
4018 if (0 == memcmp(&pg->peers[i].daemon->id, peer_id, sizeof(struct GNUNET_PeerIdentity)))
4019 return pg->peers[i].daemon;
4026 * Prototype of a function that will be called when a
4027 * particular operation was completed the testing library.
4029 * @param cls closure (a struct RestartContext)
4030 * @param id id of the peer that was restarted
4031 * @param cfg handle to the configuration of the peer
4032 * @param d handle to the daemon that was restarted
4033 * @param emsg NULL on success
4035 void restart_callback (void *cls,
4036 const struct GNUNET_PeerIdentity *id,
4037 const struct GNUNET_CONFIGURATION_Handle *cfg,
4038 struct GNUNET_TESTING_Daemon *d,
4041 struct RestartContext *restart_context = cls;
4045 restart_context->peers_restarted++;
4049 restart_context->peers_restart_failed++;
4052 if (restart_context->peers_restarted == restart_context->peer_group->total)
4054 restart_context->callback(restart_context->callback_cls, NULL);
4055 GNUNET_free(restart_context);
4057 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
4059 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
4060 GNUNET_free(restart_context);
4066 * Callback for informing us about a successful
4067 * or unsuccessful churn stop call.
4069 * @param cls a ChurnContext
4070 * @param emsg NULL on success, non-NULL on failure
4074 churn_stop_callback (void *cls, const char *emsg)
4076 struct ShutdownContext *shutdown_ctx = cls;
4077 struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
4078 unsigned int total_left;
4079 char *error_message;
4081 error_message = NULL;
4082 shutdown_ctx->outstanding--;
4086 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
4087 "Churn stop callback failed with error `%s'\n", emsg);
4088 churn_ctx->num_failed_stop++;
4092 churn_ctx->num_to_stop--;
4096 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
4097 "Stopped peer, %d left.\n",
4098 churn_ctx->num_to_stop);
4100 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
4102 if (total_left == 0)
4104 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
4106 GNUNET_asprintf(&error_message,
4107 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
4108 churn_ctx->num_failed_start,
4109 churn_ctx->num_failed_stop);
4111 churn_ctx->cb(churn_ctx->cb_cls, error_message);
4112 GNUNET_free_non_null(error_message);
4113 GNUNET_free(churn_ctx);
4114 GNUNET_free(shutdown_ctx);
4119 * Count the number of running peers.
4121 * @param pg handle for the peer group
4123 * @return the number of currently running peers in the peer group
4126 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
4129 unsigned int running = 0;
4130 for (i = 0; i < pg->total; i++)
4132 if (pg->peers[i].daemon->running == GNUNET_YES)
4134 GNUNET_assert(running != -1);
4142 * Task to rate limit the number of outstanding peer shutdown
4143 * requests. This is necessary for making sure we don't do
4144 * too many ssh connections at once, but is generally nicer
4145 * to any system as well (graduated task starts, as opposed
4146 * to calling gnunet-arm N times all at once).
4149 schedule_churn_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
4151 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4152 struct ShutdownContext *shutdown_ctx;
4154 GNUNET_assert(peer_shutdown_ctx != NULL);
4155 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4156 GNUNET_assert(shutdown_ctx != NULL);
4158 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4159 GNUNET_SCHEDULER_add_delayed(peer_shutdown_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_churn_shutdown_task, peer_shutdown_ctx);
4162 shutdown_ctx->outstanding++;
4163 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, shutdown_ctx->cb, shutdown_ctx, GNUNET_NO, GNUNET_YES);
4164 GNUNET_free(peer_shutdown_ctx);
4169 * Simulate churn by stopping some peers (and possibly
4170 * re-starting others if churn is called multiple times). This
4171 * function can only be used to create leave-join churn (peers "never"
4172 * leave for good). First "voff" random peers that are currently
4173 * online will be taken offline; then "von" random peers that are then
4174 * offline will be put back online. No notifications will be
4175 * generated for any of these operations except for the callback upon
4178 * @param pg handle for the peer group
4179 * @param voff number of peers that should go offline
4180 * @param von number of peers that should come back online;
4181 * must be zero on first call (since "testbed_start"
4182 * always starts all of the peers)
4183 * @param timeout how long to wait for operations to finish before
4185 * @param cb function to call at the end
4186 * @param cb_cls closure for cb
4189 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
4192 struct GNUNET_TIME_Relative timeout,
4193 GNUNET_TESTING_NotifyCompletion cb,
4196 struct ChurnContext *churn_ctx;
4197 struct ShutdownContext *shutdown_ctx;
4198 struct PeerShutdownContext *peer_shutdown_ctx;
4199 struct PeerRestartContext *peer_restart_ctx;
4200 struct ChurnRestartContext *churn_startup_ctx;
4202 unsigned int running;
4203 unsigned int stopped;
4204 unsigned int total_running;
4205 unsigned int total_stopped;
4207 unsigned int *running_arr;
4208 unsigned int *stopped_arr;
4209 unsigned int *running_permute;
4210 unsigned int *stopped_permute;
4215 if ((von == 0) && (voff == 0)) /* No peers at all? */
4221 for (i = 0; i < pg->total; i++)
4223 if (pg->peers[i].daemon->running == GNUNET_YES)
4225 GNUNET_assert(running != -1);
4230 GNUNET_assert(stopped != -1);
4237 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
4238 cb(cb_cls, "Trying to stop more peers than are currently running!");
4244 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
4245 cb(cb_cls, "Trying to start more peers than are currently stopped!");
4249 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4253 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
4257 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
4259 running_permute = NULL;
4260 stopped_permute = NULL;
4263 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
4265 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
4267 total_running = running;
4268 total_stopped = stopped;
4272 churn_ctx->num_to_start = von;
4273 churn_ctx->num_to_stop = voff;
4275 churn_ctx->cb_cls = cb_cls;
4277 for (i = 0; i < pg->total; i++)
4279 if (pg->peers[i].daemon->running == GNUNET_YES)
4281 GNUNET_assert((running_arr != NULL) && (total_running > running));
4282 running_arr[running] = i;
4287 GNUNET_assert((stopped_arr != NULL) && (total_stopped > stopped));
4288 stopped_arr[stopped] = i;
4293 GNUNET_assert(running >= voff);
4296 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4297 shutdown_ctx->cb = &churn_stop_callback;
4298 shutdown_ctx->cb_cls = churn_ctx;
4299 shutdown_ctx->total_peers = voff;
4300 shutdown_ctx->timeout = timeout;
4303 for (i = 0; i < voff; i++)
4306 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
4308 GNUNET_assert(running_arr != NULL);
4309 peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4310 peer_shutdown_ctx->daemon = pg->peers[running_arr[running_permute[i]]].daemon;
4311 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4312 GNUNET_SCHEDULER_add_now(peer_shutdown_ctx->daemon->sched, &schedule_churn_shutdown_task, peer_shutdown_ctx);
4315 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
4317 &churn_stop_callback, churn_ctx,
4318 GNUNET_NO, GNUNET_YES); */
4321 GNUNET_assert(stopped >= von);
4324 churn_startup_ctx = GNUNET_malloc(sizeof(struct ChurnRestartContext));
4325 churn_startup_ctx->churn_ctx = churn_ctx;
4326 churn_startup_ctx->timeout = timeout;
4328 for (i = 0; i < von; i++)
4331 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
4333 GNUNET_assert(stopped_arr != NULL);
4334 peer_restart_ctx = GNUNET_malloc(sizeof(struct PeerRestartContext));
4335 peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
4336 peer_restart_ctx->daemon = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
4337 GNUNET_SCHEDULER_add_now(peer_restart_ctx->daemon->sched, &schedule_churn_restart, peer_restart_ctx);
4339 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
4340 timeout, &churn_start_callback, churn_ctx);*/
4343 GNUNET_free_non_null(running_arr);
4344 GNUNET_free_non_null(stopped_arr);
4345 GNUNET_free_non_null(running_permute);
4346 GNUNET_free_non_null(stopped_permute);
4351 * Restart all peers in the given group.
4353 * @param pg the handle to the peer group
4354 * @param callback function to call on completion (or failure)
4355 * @param callback_cls closure for the callback function
4358 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
4359 GNUNET_TESTING_NotifyCompletion callback,
4362 struct RestartContext *restart_context;
4367 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
4368 restart_context->peer_group = pg;
4369 restart_context->peers_restarted = 0;
4370 restart_context->callback = callback;
4371 restart_context->callback_cls = callback_cls;
4373 for (off = 0; off < pg->total; off++)
4375 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
4381 * Start or stop an individual peer from the given group.
4383 * @param pg handle to the peer group
4384 * @param offset which peer to start or stop
4385 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
4386 * @param timeout how long to wait for shutdown
4387 * @param cb function to call at the end
4388 * @param cb_cls closure for cb
4391 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
4392 unsigned int offset,
4394 struct GNUNET_TIME_Relative timeout,
4395 GNUNET_TESTING_NotifyCompletion cb,
4398 struct ShutdownContext *shutdown_ctx;
4399 struct ChurnRestartContext *startup_ctx;
4400 struct ChurnContext *churn_ctx;
4402 if (GNUNET_NO == desired_status)
4404 if (NULL != pg->peers[offset].daemon)
4406 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4407 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4408 churn_ctx->num_to_start = 0;
4409 churn_ctx->num_to_stop = 1;
4411 churn_ctx->cb_cls = cb_cls;
4412 shutdown_ctx->cb_cls = churn_ctx;
4413 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
4414 timeout, &churn_stop_callback, shutdown_ctx,
4415 GNUNET_NO, GNUNET_YES);
4418 else if (GNUNET_YES == desired_status)
4420 if (NULL == pg->peers[offset].daemon)
4422 startup_ctx = GNUNET_malloc(sizeof(struct ChurnRestartContext));
4423 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4424 churn_ctx->num_to_start = 1;
4425 churn_ctx->num_to_stop = 0;
4427 churn_ctx->cb_cls = cb_cls;
4428 startup_ctx->churn_ctx = churn_ctx;
4429 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
4430 timeout, &churn_start_callback, startup_ctx);
4439 * Callback for shutting down peers in a peer group.
4441 * @param cls closure (struct ShutdownContext)
4442 * @param emsg NULL on success
4444 void internal_shutdown_callback (void *cls,
4447 struct ShutdownContext *shutdown_ctx = cls;
4449 shutdown_ctx->outstanding--;
4452 shutdown_ctx->peers_down++;
4456 shutdown_ctx->peers_failed++;
4459 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
4461 if (shutdown_ctx->peers_failed > 0)
4462 shutdown_ctx->cb(shutdown_ctx->cb_cls, "Not all peers successfully shut down!");
4464 shutdown_ctx->cb(shutdown_ctx->cb_cls, NULL);
4465 GNUNET_free(shutdown_ctx);
4471 * Task to rate limit the number of outstanding peer shutdown
4472 * requests. This is necessary for making sure we don't do
4473 * too many ssh connections at once, but is generally nicer
4474 * to any system as well (graduated task starts, as opposed
4475 * to calling gnunet-arm N times all at once).
4478 schedule_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
4480 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4481 struct ShutdownContext *shutdown_ctx;
4483 GNUNET_assert(peer_shutdown_ctx != NULL);
4484 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4485 GNUNET_assert(shutdown_ctx != NULL);
4487 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4488 GNUNET_SCHEDULER_add_delayed(peer_shutdown_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_shutdown_task, peer_shutdown_ctx);
4491 shutdown_ctx->outstanding++;
4492 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, &internal_shutdown_callback, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4493 GNUNET_free(peer_shutdown_ctx);
4498 * Shutdown all peers started in the given group.
4500 * @param pg handle to the peer group
4501 * @param timeout how long to wait for shutdown
4502 * @param cb callback to notify upon success or failure
4503 * @param cb_cls closure for cb
4506 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
4507 struct GNUNET_TIME_Relative timeout,
4508 GNUNET_TESTING_NotifyCompletion cb,
4512 struct ShutdownContext *shutdown_ctx;
4513 struct PeerShutdownContext *peer_shutdown_ctx;
4515 GNUNET_assert(pg->total > 0);
4517 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4518 shutdown_ctx->cb = cb;
4519 shutdown_ctx->cb_cls = cb_cls;
4520 shutdown_ctx->total_peers = pg->total;
4521 shutdown_ctx->timeout = timeout;
4522 /* shtudown_ctx->outstanding = 0; */
4524 for (off = 0; off < pg->total; off++)
4526 GNUNET_assert(NULL != pg->peers[off].daemon);
4527 peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4528 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
4529 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4530 GNUNET_SCHEDULER_add_now(pg->peers[off].daemon->sched, &schedule_shutdown_task, peer_shutdown_ctx);
4531 //GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4532 if (NULL != pg->peers[off].cfg)
4533 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
4534 if (pg->peers[off].allowed_peers != NULL)
4535 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
4536 if (pg->peers[off].connect_peers != NULL)
4537 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
4538 if (pg->peers[off].blacklisted_peers != NULL)
4539 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
4541 GNUNET_free (pg->peers);
4542 for (off = 0; off < pg->num_hosts; off++)
4544 GNUNET_free (pg->hosts[off].hostname);
4545 GNUNET_free_non_null (pg->hosts[off].username);
4547 GNUNET_free_non_null (pg->hosts);
4552 /* end of testing_group.c */