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"
32 #define VERBOSE_TESTING GNUNET_NO
34 #define DEBUG_CHURN GNUNET_NO
37 * Lowest port used for GNUnet testing. Should be high enough to not
38 * conflict with other applications running on the hosts but be low
39 * enough to not conflict with client-ports (typically starting around
42 #define LOW_PORT 10000
45 * Highest port used for GNUnet testing. Should be low enough to not
46 * conflict with the port range for "local" ports (client apps; see
47 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
49 #define HIGH_PORT 56000
51 #define MAX_OUTSTANDING_CONNECTIONS 50
53 #define MAX_CONCURRENT_HOSTKEYS 16
55 #define MAX_CONCURRENT_STARTING 50
57 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
59 #define CONNECT_ATTEMPTS 8
62 * Prototype of a function called whenever two peers would be connected
63 * in a certain topology.
65 typedef int (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup *pg,
71 * Context for handling churning a peer group
76 * Callback used to notify of churning finished
78 GNUNET_TESTING_NotifyCompletion cb;
81 * Closure for callback
86 * Number of peers that still need to be started
88 unsigned int num_to_start;
91 * Number of peers that still need to be stopped
93 unsigned int num_to_stop;
96 * Number of peers that failed to start
98 unsigned int num_failed_start;
101 * Number of peers that failed to stop
103 unsigned int num_failed_stop;
106 struct RestartContext
109 * The group of peers being restarted
111 struct GNUNET_TESTING_PeerGroup *peer_group;
114 * How many peers have been restarted thus far
116 unsigned int peers_restarted;
119 * How many peers got an error when restarting
121 unsigned int peers_restart_failed;
124 * The function to call once all peers have been restarted
126 GNUNET_TESTING_NotifyCompletion callback;
129 * Closure for callback function
136 struct ShutdownContext
139 * Total peers to wait for
144 * Number of peers successfully shut down
149 * Number of peers failed to shut down
154 * Callback to call when all peers either
155 * shutdown or failed to shutdown
157 GNUNET_TESTING_NotifyCompletion cb;
165 struct CreateTopologyContext
169 * Function to call with number of connections
171 GNUNET_TESTING_NotifyConnections cont;
174 * Closure for connection notification
180 struct PeerConnection
185 struct PeerConnection *next;
188 * Pointer to daemon handle
190 struct GNUNET_TESTING_Daemon *daemon;
195 struct InternalStartContext
198 * Pointer to peerdata
200 struct PeerData *peer;
203 * Timeout for peer startup
205 struct GNUNET_TIME_Relative timeout;
208 * Client callback for hostkey notification
210 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
213 * Closure for hostkey_callback
218 * Client callback for peer start notification
220 GNUNET_TESTING_NotifyDaemonRunning start_cb;
228 * Hostname, where to start the peer
230 const char *hostname;
234 * Data we keep per peer.
239 * (Initial) configuration of the host.
240 * (initial because clients could change
241 * it and we would not know about those
244 struct GNUNET_CONFIGURATION_Handle *cfg;
247 * Handle for controlling the daemon.
249 struct GNUNET_TESTING_Daemon *daemon;
252 * The peergroup this peer belongs to.
254 struct GNUNET_TESTING_PeerGroup *pg;
257 * Hash map of allowed peer connections (F2F created topology)
259 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
262 * Hash map of blacklisted peers
264 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
267 * Hash map of peer connections
269 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
272 * Temporary hash map of peer connections
274 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
277 * Temporary variable for topology creation, should be reset before
278 * creating any topology so the count is valid once finished.
283 * Context to keep track of peers being started, to
284 * stagger hostkey generation and peer startup.
286 struct InternalStartContext internal_context;
291 * Data we keep per host.
301 * Lowest port that we have not yet used
309 * Handle to a group of GNUnet peers.
311 struct GNUNET_TESTING_PeerGroup
316 struct GNUNET_SCHEDULER_Handle *sched;
319 * Configuration template.
321 const struct GNUNET_CONFIGURATION_Handle *cfg;
324 * Function to call on each started daemon.
326 //GNUNET_TESTING_NotifyDaemonRunning cb;
334 * Function to call on each topology connection created
336 GNUNET_TESTING_NotifyConnection notify_connection;
339 * Callback for notify_connection
341 void *notify_connection_cls;
344 * NULL-terminated array of information about
347 struct HostData *hosts;
350 * Array of "total" peers.
352 struct PeerData *peers;
355 * Number of peers in this group.
360 * At what time should we fail the peer startup process?
362 struct GNUNET_TIME_Absolute max_timeout;
365 * How many peers are being started right now?
367 unsigned int starting;
370 * How many peers have already been started?
372 unsigned int started;
377 struct GNUNET_CONFIGURATION_Handle *ret;
378 const char *hostname;
384 struct ConnectContext
386 struct GNUNET_TESTING_Daemon *first;
388 struct GNUNET_TESTING_Daemon *second;
390 struct GNUNET_TESTING_PeerGroup *pg;
394 * Convert unique ID to hash code.
396 * @param uid unique ID to convert
397 * @param hash set to uid (extended with zeros)
400 hash_from_uid (uint32_t uid,
401 GNUNET_HashCode *hash)
403 memset (hash, 0, sizeof(GNUNET_HashCode));
404 *((uint32_t*)hash) = uid;
408 * Convert hash code to unique ID.
410 * @param uid unique ID to convert
411 * @param hash set to uid (extended with zeros)
414 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
416 memcpy (uid, hash, sizeof(uint32_t));
420 * Number of connects we are waiting on, allows us to rate limit
423 static int outstanding_connects;
426 * Get a topology from a string input.
428 * @param topology where to write the retrieved topology
429 * @param topology_string The string to attempt to
430 * get a configuration value from
431 * @return GNUNET_YES if topology string matched a
432 * known topology, GNUNET_NO if not
435 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string)
438 * Strings representing topologies in enum
440 static const char * topology_strings[] =
443 * A clique (everyone connected to everyone else).
448 * Small-world network (2d torus plus random links).
453 * Small-world network (ring plus random links).
473 * Certain percentage of peers are unable to communicate directly
474 * replicating NAT conditions
479 * Scale free topology.
484 * Straight line topology.
489 * All peers are disconnected.
497 if (topology_string == NULL)
499 while (topology_strings[curr] != NULL)
501 if (strcmp(topology_strings[curr], topology_string) == 0)
508 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
514 * Get connect topology option from string input.
516 * @param topology_option where to write the retrieved topology
517 * @param topology_string The string to attempt to
518 * get a configuration value from
519 * @return GNUNET_YES if string matched a known
520 * topology option, GNUNET_NO if not
523 GNUNET_TESTING_topology_option_get(enum GNUNET_TESTING_TopologyOption *topology_option,
524 char * topology_string)
527 * Options for connecting a topology as strings.
529 static const char * topology_option_strings[] =
532 * Try to connect all peers specified in the topology.
537 * Choose a random subset of connections to create.
539 "CONNECT_RANDOM_SUBSET",
542 * Create at least X connections for each peer.
547 * Using a depth first search, create one connection
548 * per peer. If any are missed (graph disconnected)
549 * start over at those peers until all have at least one
555 * No options specified.
563 if (topology_string == NULL)
565 while (NULL != topology_option_strings[curr])
567 if (strcmp(topology_option_strings[curr], topology_string) == 0)
569 *topology_option = curr;
574 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
579 * Function to iterate over options. Copies
580 * the options to the target configuration,
581 * updating PORT values as needed.
584 * @param section name of the section
585 * @param option name of the option
586 * @param value value of the option
589 update_config (void *cls,
590 const char *section, const char *option, const char *value)
592 struct UpdateContext *ctx = cls;
597 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
601 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
606 if (0 == strcmp (option, "UNIXPATH"))
608 GNUNET_snprintf (uval,
610 "/tmp/test-service-%s-%u",
616 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
618 value = ctx->hostname;
621 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
626 * Create a new configuration using the given configuration
627 * as a template; however, each PORT in the existing cfg
628 * must be renumbered by incrementing "*port". If we run
629 * out of "*port" numbers, return NULL.
631 * @param cfg template configuration
632 * @param port port numbers to use, update to reflect
633 * port numbers that were used
634 * @param upnum number to make unix domain socket names unique
635 * @param hostname hostname of the controlling host, to allow control connections from
637 * @return new configuration, NULL on error
639 static struct GNUNET_CONFIGURATION_Handle *
640 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
643 const char *hostname)
645 struct UpdateContext uc;
653 uc.ret = GNUNET_CONFIGURATION_create ();
654 uc.hostname = hostname;
656 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
657 if (uc.nport >= HIGH_PORT)
660 GNUNET_CONFIGURATION_destroy (uc.ret);
664 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
666 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
667 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
668 GNUNET_free_non_null(control_host);
669 GNUNET_free(allowed_hosts);
673 /* arm needs to know to allow connections from the host on which it is running,
674 * otherwise gnunet-arm is unable to connect to it in some instances */
675 if (hostname != NULL)
677 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
678 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
679 GNUNET_free(allowed_hosts);
682 *port = (uint16_t) uc.nport;
689 * Add entries to the peers connect list
691 * @param pg the peer group we are working with
692 * @param first index of the first peer
693 * @param second index of the second peer
695 * @return the number of connections added
696 * technically should only be 0 or 2
700 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
706 GNUNET_HashCode hash_first;
707 GNUNET_HashCode hash_second;
709 hash_from_uid(first, &hash_first);
710 hash_from_uid(second, &hash_second);
712 add_first = GNUNET_NO;
713 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
715 add_first = GNUNET_YES;
718 add_second = GNUNET_NO;
719 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
721 add_second = GNUNET_YES;
727 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
728 pg->peers[first].num_connections++;
734 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
735 pg->peers[second].num_connections++;
744 * Add entries to the peers allowed connections list
746 * @param pg the peer group we are working with
747 * @param first index of the first peer
748 * @param second index of the second peer
750 * @return the number of connections added (can be 0, 1 or 2)
751 * technically should only be 0 or 2, but the small price
752 * of iterating over the lists (hashmaps in the future)
753 * for being sure doesn't bother me!
757 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
761 struct PeerConnection *first_iter;
762 struct PeerConnection *second_iter;
763 struct PeerConnection *new_first;
764 struct PeerConnection *new_second;
769 GNUNET_HashCode hash_first;
770 GNUNET_HashCode hash_second;
772 hash_from_uid(first, &hash_first);
773 hash_from_uid(second, &hash_second);
775 add_first = GNUNET_NO;
776 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
778 add_first = GNUNET_YES;
781 add_second = GNUNET_NO;
782 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
784 add_second = GNUNET_YES;
787 first_iter = pg->peers[first].connected_peers;
788 while (first_iter != NULL)
790 if (first_iter->daemon == pg->peers[second].daemon)
791 add_first = GNUNET_NO;
792 first_iter = first_iter->next;
795 second_iter = pg->peers[second].connected_peers;
796 add_second = GNUNET_YES;
797 while (second_iter != NULL)
799 if (second_iter->daemon == pg->peers[first].daemon)
800 add_second = GNUNET_NO;
801 second_iter = second_iter->next;
808 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
810 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
811 new_first->daemon = pg->peers[second].daemon;
812 new_first->next = pg->peers[first].connected_peers;
813 pg->peers[first].connected_peers = new_first;
815 pg->peers[first].num_connections++;
821 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
823 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
824 new_second->daemon = pg->peers[first].daemon;
825 new_second->next = pg->peers[second].connected_peers;
826 pg->peers[second].connected_peers = new_second;
827 pg->peers[first].num_connections++;
829 pg->peers[second].num_connections++;
837 * Add entries to the peers blacklisted list
839 * @param pg the peer group we are working with
840 * @param first index of the first peer
841 * @param second index of the second peer
843 * @return the number of connections added (can be 0, 1 or 2)
847 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
852 GNUNET_HashCode hash_first;
853 GNUNET_HashCode hash_second;
855 hash_from_uid(first, &hash_first);
856 hash_from_uid(second, &hash_second);
858 add_first = GNUNET_NO;
859 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
861 add_first = GNUNET_YES;
864 add_second = GNUNET_NO;
865 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
867 add_second = GNUNET_YES;
873 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
874 pg->peers[first].num_connections++;
880 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
881 pg->peers[second].num_connections++;
889 * Remove entries from the peers blacklisted list
891 * @param pg the peer group we are working with
892 * @param first index of the first peer
893 * @param second index of the second peer
895 * @return the number of connections removed (can be 0, 1 or 2)
899 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
904 GNUNET_HashCode hash_first;
905 GNUNET_HashCode hash_second;
907 hash_from_uid(first, &hash_first);
908 hash_from_uid(second, &hash_second);
910 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
911 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
916 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
922 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
930 * Scale free network construction as described in:
932 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
934 * Start with a network of "one" peer, then progressively add
935 * peers up to the total number. At each step, iterate over
936 * all possible peers and connect new peer based on number of
937 * existing connections of the target peer.
939 * @param pg the peer group we are dealing with
940 * @param proc the connection processor to use
942 * @return the number of connections created
945 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
948 unsigned int total_connections;
949 unsigned int outer_count;
951 unsigned int previous_total_connections;
955 GNUNET_assert(pg->total > 1);
957 /* Add a connection between the first two nodes */
958 total_connections = proc(pg, 0, 1);
960 for (outer_count = 1; outer_count < pg->total; outer_count++)
962 previous_total_connections = total_connections;
963 for (i = 0; i < outer_count; i++)
965 probability = pg->peers[i].num_connections / (double)previous_total_connections;
966 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
967 UINT64_MAX)) / ( (double) UINT64_MAX);
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
970 "Considering connecting peer %d to peer %d\n",
973 if (random < probability)
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
977 "Connecting peer %d to peer %d\n",
980 total_connections += proc(pg, outer_count, i);
985 return total_connections;
989 * Create a topology given a peer group (set of running peers)
990 * and a connection processor.
992 * @param pg the peergroup to create the topology on
993 * @param proc the connection processor to call to actually set
994 * up connections between two peers
996 * @return the number of connections that were set up
1000 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1004 unsigned int natLog;
1005 unsigned int randomPeer;
1006 double random, logNModifier, percentage;
1007 unsigned int smallWorldConnections;
1012 unsigned int useAnd;
1013 int connect_attempts;
1015 logNModifier = 0.5; /* FIXME: default value? */
1016 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1021 if (sscanf(p_string, "%lf", &logNModifier) != 1)
1022 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1023 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1027 GNUNET_free (p_string);
1029 percentage = 0.5; /* FIXME: default percentage? */
1030 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1035 if (sscanf(p_string, "%lf", &percentage) != 1)
1036 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1037 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1041 GNUNET_free (p_string);
1043 natLog = log (pg->total);
1044 connsPerPeer = ceil (natLog * logNModifier);
1046 if (connsPerPeer % 2 == 1)
1049 smallWorldConnections = 0;
1050 connect_attempts = 0;
1051 for (i = 0; i < pg->total; i++)
1054 max = i + connsPerPeer / 2;
1055 min = i - connsPerPeer / 2;
1057 if (max > pg->total - 1)
1059 max = max - pg->total;
1065 min = pg->total - 1 + min;
1069 for (j = 0; j < connsPerPeer / 2; j++)
1071 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1072 UINT64_MAX) / ( (double) UINT64_MAX));
1073 if (random < percentage)
1075 /* Connect to uniformly selected random peer */
1077 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1079 while ((((randomPeer < max) && (randomPeer > min))
1080 && (useAnd == 0)) || (((randomPeer > min)
1081 || (randomPeer < max))
1085 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1088 smallWorldConnections +=
1089 proc (pg, i, randomPeer);
1093 nodeToConnect = i + j + 1;
1094 if (nodeToConnect > pg->total - 1)
1096 nodeToConnect = nodeToConnect - pg->total;
1099 proc (pg, i, nodeToConnect);
1105 connect_attempts += smallWorldConnections;
1107 return connect_attempts;
1111 * Create a topology given a peer group (set of running peers)
1112 * and a connection processor.
1114 * @param pg the peergroup to create the topology on
1115 * @param proc the connection processor to call to actually set
1116 * up connections between two peers
1118 * @return the number of connections that were set up
1122 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1124 unsigned int outer_count, inner_count;
1125 unsigned int cutoff;
1126 int connect_attempts;
1127 double nat_percentage;
1130 nat_percentage = 0.6; /* FIXME: default percentage? */
1131 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1136 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1137 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1138 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1142 GNUNET_free (p_string);
1147 cutoff = (unsigned int) (nat_percentage * pg->total);
1149 connect_attempts = 0;
1151 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1153 for (inner_count = outer_count + 1; inner_count < pg->total;
1156 if ((outer_count > cutoff) || (inner_count > cutoff))
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1160 "Connecting peer %d to peer %d\n",
1161 outer_count, inner_count);
1163 connect_attempts += proc(pg, outer_count, inner_count);
1168 return connect_attempts;
1173 * Create a topology given a peer group (set of running peers)
1174 * and a connection processor.
1176 * @param pg the peergroup to create the topology on
1177 * @param proc the connection processor to call to actually set
1178 * up connections between two peers
1180 * @return the number of connections that were set up
1184 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1186 unsigned int i, j, k;
1187 unsigned int square;
1190 unsigned int toggle = 1;
1191 unsigned int nodeToConnect;
1192 unsigned int natLog;
1193 unsigned int node1Row;
1194 unsigned int node1Col;
1195 unsigned int node2Row;
1196 unsigned int node2Col;
1197 unsigned int distance;
1198 double probability, random, percentage;
1199 unsigned int smallWorldConnections;
1201 int connect_attempts;
1202 square = floor (sqrt (pg->total));
1206 percentage = 0.5; /* FIXME: default percentage? */
1207 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1212 if (sscanf(p_string, "%lf", &percentage) != 1)
1213 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1214 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1218 GNUNET_free (p_string);
1220 probability = 0.5; /* FIXME: default percentage? */
1221 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1226 if (sscanf(p_string, "%lf", &probability) != 1)
1227 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1228 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1232 GNUNET_free (p_string);
1234 if (square * square != pg->total)
1236 while (rows * cols < pg->total)
1238 if (toggle % 2 == 0)
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1252 connect_attempts = 0;
1253 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1254 * to the node to its right and above. Once this is over, we'll have our torus!
1255 * Special case for the last node (if the rows and columns are not equal), connect
1256 * to the first in the row to maintain topology.
1258 for (i = 0; i < pg->total; i++)
1260 /* First connect to the node to the right */
1261 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1262 nodeToConnect = i + 1;
1263 else if (i + 1 == pg->total)
1264 nodeToConnect = rows * cols - cols;
1266 nodeToConnect = i - cols + 1;
1268 connect_attempts += proc (pg, i, nodeToConnect);
1271 nodeToConnect = (rows * cols) - cols + i;
1273 nodeToConnect = i - cols;
1275 if (nodeToConnect < pg->total)
1276 connect_attempts += proc (pg, i, nodeToConnect);
1278 natLog = log (pg->total);
1279 #if VERBOSE_TESTING > 2
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1281 _("natural log of %d is %d, will run %d iterations\n"),
1282 pg->total, natLog, (int) (natLog * percentage));
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1285 smallWorldConnections = 0;
1286 for (i = 0; i < (int) (natLog * percentage); i++)
1288 for (j = 0; j < pg->total; j++)
1290 /* Determine the row and column of node at position j on the 2d torus */
1291 node1Row = j / cols;
1292 node1Col = j - (node1Row * cols);
1293 for (k = 0; k < pg->total; k++)
1295 /* Determine the row and column of node at position k on the 2d torus */
1296 node2Row = k / cols;
1297 node2Col = k - (node2Row * cols);
1298 /* Simple Cartesian distance */
1299 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1302 /* Calculate probability as 1 over the square of the distance */
1303 probability = 1.0 / (distance * distance);
1304 /* Choose a random value between 0 and 1 */
1305 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1306 UINT64_MAX)) / ( (double) UINT64_MAX);
1307 /* If random < probability, then connect the two nodes */
1308 if (random < probability)
1309 smallWorldConnections += proc (pg, j, k);
1315 connect_attempts += smallWorldConnections;
1316 #if VERBOSE_TESTING > 2
1317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1318 _("Total connections added for small world: %d!\n"),
1319 smallWorldConnections);
1321 return connect_attempts;
1325 * Create a topology given a peer group (set of running peers)
1326 * and a connection processor.
1328 * @param pg the peergroup to create the topology on
1329 * @param proc the connection processor to call to actually set
1330 * up connections between two peers
1332 * @return the number of connections that were set up
1336 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1339 unsigned int outer_count;
1340 unsigned int inner_count;
1341 int connect_attempts;
1345 probability = 0.5; /* FIXME: default percentage? */
1346 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1351 if (sscanf(p_string, "%lf", &probability) != 1)
1352 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1353 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1357 GNUNET_free (p_string);
1359 connect_attempts = 0;
1360 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1362 for (inner_count = outer_count + 1; inner_count < pg->total;
1365 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1366 UINT64_MAX)) / ( (double) UINT64_MAX);
1368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1369 _("rand is %f probability is %f\n"), temp_rand,
1372 if (temp_rand < probability)
1374 connect_attempts += proc (pg, outer_count, inner_count);
1379 return connect_attempts;
1383 * Create a topology given a peer group (set of running peers)
1384 * and a connection processor.
1386 * @param pg the peergroup to create the topology on
1387 * @param proc the connection processor to call to actually set
1388 * up connections between two peers
1390 * @return the number of connections that were set up
1394 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1397 unsigned int square;
1400 unsigned int toggle = 1;
1401 unsigned int nodeToConnect;
1402 int connect_attempts;
1404 connect_attempts = 0;
1406 square = floor (sqrt (pg->total));
1410 if (square * square != pg->total)
1412 while (rows * cols < pg->total)
1414 if (toggle % 2 == 0)
1423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1424 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1427 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1428 * to the node to its right and above. Once this is over, we'll have our torus!
1429 * Special case for the last node (if the rows and columns are not equal), connect
1430 * to the first in the row to maintain topology.
1432 for (i = 0; i < pg->total; i++)
1434 /* First connect to the node to the right */
1435 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1436 nodeToConnect = i + 1;
1437 else if (i + 1 == pg->total)
1438 nodeToConnect = rows * cols - cols;
1440 nodeToConnect = i - cols + 1;
1442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1443 "Connecting peer %d to peer %d\n",
1446 connect_attempts += proc(pg, i, nodeToConnect);
1448 /* Second connect to the node immediately above */
1450 nodeToConnect = (rows * cols) - cols + i;
1452 nodeToConnect = i - cols;
1454 if (nodeToConnect < pg->total)
1457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1458 "Connecting peer %d to peer %d\n",
1461 connect_attempts += proc(pg, i, nodeToConnect);
1466 return connect_attempts;
1471 * Create a topology given a peer group (set of running peers)
1472 * and a connection processor.
1474 * @param pg the peergroup to create the topology on
1475 * @param proc the connection processor to call to actually set
1476 * up connections between two peers
1478 * @return the number of connections that were set up
1482 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1484 unsigned int outer_count;
1485 unsigned int inner_count;
1486 int connect_attempts;
1488 connect_attempts = 0;
1490 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1492 for (inner_count = outer_count + 1; inner_count < pg->total;
1496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1497 "Connecting peer %d to peer %d\n",
1498 outer_count, inner_count);
1500 connect_attempts += proc(pg, outer_count, inner_count);
1504 return connect_attempts;
1508 * Create a topology given a peer group (set of running peers)
1509 * and a connection processor.
1511 * @param pg the peergroup to create the topology on
1512 * @param proc the connection processor to call to actually set
1513 * up connections between two peers
1515 * @return the number of connections that were set up
1519 create_line (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1522 int connect_attempts;
1524 connect_attempts = 0;
1526 /* Connect each peer to the next highest numbered peer */
1527 for (count = 0; count < pg->total - 1; count++)
1530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1531 "Connecting peer %d to peer %d\n",
1534 connect_attempts += proc(pg, count, count + 1);
1537 return connect_attempts;
1541 * Create a topology given a peer group (set of running peers)
1542 * and a connection processor.
1544 * @param pg the peergroup to create the topology on
1545 * @param proc the connection processor to call to actually set
1546 * up connections between two peers
1548 * @return the number of connections that were set up
1552 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1555 int connect_attempts;
1557 connect_attempts = 0;
1559 /* Connect each peer to the next highest numbered peer */
1560 for (count = 0; count < pg->total - 1; count++)
1563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1564 "Connecting peer %d to peer %d\n",
1567 connect_attempts += proc(pg, count, count + 1);
1570 /* Connect the last peer to the first peer */
1571 connect_attempts += proc(pg, pg->total - 1, 0);
1573 return connect_attempts;
1578 * Iterator for writing friends of a peer to a file.
1580 * @param cls closure, an open writable file handle
1581 * @param key the key the daemon was stored under
1582 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1584 * @return GNUNET_YES to continue iteration
1586 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1587 * with a single file_iterator that takes a closure which contains
1588 * the prefix to write before the peer. Then this could be used
1589 * for blacklisting multiple transports and writing the friend
1590 * file. I'm sure *someone* will complain loudly about other
1591 * things that negate these functions even existing so no point in
1595 friend_file_iterator (void *cls,
1596 const GNUNET_HashCode * key,
1599 FILE *temp_friend_handle = cls;
1600 struct GNUNET_TESTING_Daemon *peer = value;
1601 struct GNUNET_PeerIdentity *temppeer;
1602 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1604 temppeer = &peer->id;
1605 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1606 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1611 struct BlacklistContext
1614 * The (open) file handle to write to
1616 FILE *temp_file_handle;
1619 * The transport that this peer will be blacklisted on.
1625 * Iterator for writing blacklist data to appropriate files.
1627 * @param cls closure, an open writable file handle
1628 * @param key the key the daemon was stored under
1629 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1631 * @return GNUNET_YES to continue iteration
1634 blacklist_file_iterator (void *cls,
1635 const GNUNET_HashCode * key,
1638 struct BlacklistContext *blacklist_ctx = cls;
1639 //FILE *temp_blacklist_handle = cls;
1640 struct GNUNET_TESTING_Daemon *peer = value;
1641 struct GNUNET_PeerIdentity *temppeer;
1642 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1644 temppeer = &peer->id;
1645 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1646 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1652 * Create the friend files based on the PeerConnection's
1653 * of each peer in the peer group, and copy the files
1654 * to the appropriate place
1656 * @param pg the peer group we are dealing with
1659 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1661 FILE *temp_friend_handle;
1662 unsigned int pg_iter;
1663 char *temp_service_path;
1667 enum GNUNET_OS_ProcessStatusType type;
1668 unsigned long return_code;
1673 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1674 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1676 mytemp = GNUNET_DISK_mktemp("friends");
1677 GNUNET_assert(mytemp != NULL);
1678 temp_friend_handle = fopen (mytemp, "wt");
1679 GNUNET_assert(temp_friend_handle != NULL);
1680 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1681 fclose(temp_friend_handle);
1684 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1686 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1687 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1690 if (UNLINK (mytemp) != 0)
1691 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1692 GNUNET_free (mytemp);
1696 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1698 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1699 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1700 "mv", mytemp, arg, NULL);
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1703 _("Copying file with command cp %s %s\n"), mytemp, arg);
1708 else /* Remote, scp the file to the correct place */
1710 if (NULL != pg->peers[pg_iter].daemon->username)
1711 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1713 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1714 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1715 "scp", mytemp, arg, NULL);
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1719 _("Copying file with command scp %s %s\n"), mytemp, arg);
1723 GNUNET_free (temp_service_path);
1724 GNUNET_free (mytemp);
1728 ret = GNUNET_SYSERR;
1729 while ((count < max_wait) && (ret != GNUNET_OK))
1732 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1736 _("Checking copy status of file %d\n"), pg_iter);
1738 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1740 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1742 ret = GNUNET_SYSERR;
1744 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1746 ret = GNUNET_SYSERR;
1750 pidarr[pg_iter] = 0;
1752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1753 _("File %d copied\n"), pg_iter);
1759 if (ret == GNUNET_SYSERR)
1761 /* FIXME: why sleep here? -CG */
1767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1768 _("Finished copying all friend files!\n"));
1770 GNUNET_free(pidarr);
1776 * Create the blacklist files based on the PeerConnection's
1777 * of each peer in the peer group, and copy the files
1778 * to the appropriate place.
1780 * @param pg the peer group we are dealing with
1781 * @param transports space delimited list of transports to blacklist
1784 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
1786 FILE *temp_file_handle;
1787 static struct BlacklistContext blacklist_ctx;
1788 unsigned int pg_iter;
1789 char *temp_service_path;
1793 enum GNUNET_OS_ProcessStatusType type;
1794 unsigned long return_code;
1801 char *temp_transports;
1803 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1804 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1806 mytemp = GNUNET_DISK_mktemp("blacklist");
1807 GNUNET_assert(mytemp != NULL);
1808 temp_file_handle = fopen (mytemp, "wt");
1809 GNUNET_assert(temp_file_handle != NULL);
1810 temp_transports = GNUNET_strdup(transports);
1811 blacklist_ctx.temp_file_handle = temp_file_handle;
1812 transport_len = strlen(temp_transports) + 1;
1815 for (i = 0; i < transport_len; i++)
1817 if ((temp_transports[i] == ' ') && (pos == NULL))
1818 continue; /* At start of string (whitespace) */
1819 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
1821 temp_transports[i] = '\0';
1822 blacklist_ctx.transport = pos;
1823 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
1825 } /* At beginning of actual string */
1826 else if (pos == NULL)
1828 pos = &temp_transports[i];
1832 GNUNET_free (temp_transports);
1833 fclose(temp_file_handle);
1836 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1838 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1839 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1842 if (UNLINK (mytemp) != 0)
1843 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1844 GNUNET_free (mytemp);
1848 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1850 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
1851 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1852 "mv", mytemp, arg, NULL);
1854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1855 _("Copying file with command cp %s %s\n"), mytemp, arg);
1860 else /* Remote, scp the file to the correct place */
1862 if (NULL != pg->peers[pg_iter].daemon->username)
1863 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1865 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1866 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1867 "scp", mytemp, arg, NULL);
1870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1871 _("Copying file with command scp %s %s\n"), mytemp, arg);
1875 GNUNET_free (temp_service_path);
1876 GNUNET_free (mytemp);
1880 ret = GNUNET_SYSERR;
1881 while ((count < max_wait) && (ret != GNUNET_OK))
1884 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1888 _("Checking copy status of file %d\n"), pg_iter);
1890 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1892 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1894 ret = GNUNET_SYSERR;
1896 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1898 ret = GNUNET_SYSERR;
1902 pidarr[pg_iter] = 0;
1904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1905 _("File %d copied\n"), pg_iter);
1911 if (ret == GNUNET_SYSERR)
1913 /* FIXME: why sleep here? -CG */
1919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1920 _("Finished copying all blacklist files!\n"));
1922 GNUNET_free(pidarr);
1928 * Internal notification of a connection, kept so that we can ensure some connections
1929 * happen instead of flooding all testing daemons with requests to connect.
1931 static void internal_connect_notify (void *cls,
1932 const struct GNUNET_PeerIdentity *first,
1933 const struct GNUNET_PeerIdentity *second,
1935 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
1936 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
1937 struct GNUNET_TESTING_Daemon *first_daemon,
1938 struct GNUNET_TESTING_Daemon *second_daemon,
1941 struct GNUNET_TESTING_PeerGroup *pg = cls;
1942 outstanding_connects--;
1944 pg->notify_connection(pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
1948 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1950 struct ConnectContext *connect_context = cls;
1952 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1955 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
1957 #if VERBOSE_TESTING > 2
1958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1959 _("Delaying connect, we have too many outstanding connections!\n"));
1961 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_connect, connect_context);
1965 #if VERBOSE_TESTING > 2
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1967 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
1969 outstanding_connects++;
1970 GNUNET_TESTING_daemons_connect (connect_context->first,
1971 connect_context->second,
1974 &internal_connect_notify,
1975 connect_context->pg);
1976 GNUNET_free(connect_context);
1982 * Iterator for actually scheduling connections to be created
1983 * between two peers.
1985 * @param cls closure, a GNUNET_TESTING_Daemon
1986 * @param key the key the second Daemon was stored under
1987 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1989 * @return GNUNET_YES to continue iteration
1992 connect_iterator (void *cls,
1993 const GNUNET_HashCode * key,
1996 struct PeerData *first = cls;
1997 struct GNUNET_TESTING_Daemon *second = value;
1998 struct ConnectContext *connect_context;
2000 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2001 connect_context->pg = first->pg;
2002 connect_context->first = first->daemon;
2003 connect_context->second = second;
2004 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
2011 * Iterator for copying all entries in the allowed hashmap to the
2014 * @param cls closure, a GNUNET_TESTING_Daemon
2015 * @param key the key the second Daemon was stored under
2016 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2018 * @return GNUNET_YES to continue iteration
2021 copy_topology_iterator (void *cls,
2022 const GNUNET_HashCode * key,
2025 struct PeerData *first = cls;
2027 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2033 * Make the peers to connect the same as those that are allowed to be
2036 * @param pg the peer group
2039 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2041 unsigned int pg_iter;
2046 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2048 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
2049 if (GNUNET_SYSERR == ret)
2050 return GNUNET_SYSERR;
2052 total = total + ret;
2060 * Connect the topology as specified by the PeerConnection's
2061 * of each peer in the peer group
2063 * @param pg the peer group we are dealing with
2064 * @return the number of connections that will be attempted
2067 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
2069 unsigned int pg_iter;
2073 struct PeerConnection *connection_iter;
2074 struct ConnectContext *connect_context;
2078 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2080 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
2081 if (GNUNET_SYSERR == ret)
2082 return GNUNET_SYSERR;
2084 total = total + ret;
2088 while (connection_iter != NULL)
2090 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2091 connect_context->pg = pg;
2092 connect_context->first = ;
2093 connect_context->second = connection_iter->daemon;
2094 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
2095 connection_iter = connection_iter->next;
2104 * Takes a peer group and creates a topology based on the
2105 * one specified. Creates a topology means generates friend
2106 * files for the peers so they can only connect to those allowed
2107 * by the topology. This will only have an effect once peers
2108 * are started if the FRIENDS_ONLY option is set in the base
2109 * config. Also takes an optional restrict topology which
2110 * disallows connections based on a particular transport
2111 * UNLESS they are specified in the restricted topology.
2113 * @param pg the peer group struct representing the running peers
2114 * @param topology which topology to connect the peers in
2115 * @param restrict_topology allow only direct TCP connections in this topology
2116 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2117 * @param restrict_transports space delimited list of transports to blacklist
2118 * to create restricted topology
2120 * @return the maximum number of connections were all allowed peers
2121 * connected to each other
2124 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2125 enum GNUNET_TESTING_Topology topology,
2126 enum GNUNET_TESTING_Topology restrict_topology,
2127 char *restrict_transports)
2130 int num_connections;
2131 int unblacklisted_connections;
2133 GNUNET_assert (pg->notify_connection != NULL);
2138 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2141 _("Creating clique topology\n"));
2143 num_connections = create_clique (pg, &add_allowed_connections);
2145 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2148 _("Creating small world (ring) topology\n"));
2150 num_connections = create_small_world_ring (pg, &add_allowed_connections);
2152 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2155 _("Creating small world (2d-torus) topology\n"));
2157 num_connections = create_small_world (pg, &add_allowed_connections);
2159 case GNUNET_TESTING_TOPOLOGY_RING:
2161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2162 _("Creating ring topology\n"));
2164 num_connections = create_ring (pg, &add_allowed_connections);
2166 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2169 _("Creating 2d torus topology\n"));
2171 num_connections = create_2d_torus (pg, &add_allowed_connections);
2173 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2176 _("Creating Erdos-Renyi topology\n"));
2178 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2180 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2183 _("Creating InterNAT topology\n"));
2185 num_connections = create_nated_internet (pg, &add_allowed_connections);
2187 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2190 _("Creating Scale Free topology\n"));
2192 num_connections = create_scale_free (pg, &add_allowed_connections);
2194 case GNUNET_TESTING_TOPOLOGY_LINE:
2196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2197 _("Creating straight line topology\n"));
2199 num_connections = create_line (pg, &add_allowed_connections);
2201 case GNUNET_TESTING_TOPOLOGY_NONE:
2203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2204 _("Creating no allowed topology (all peers can connect at core level)\n"));
2206 num_connections = 0;
2209 num_connections = 0;
2213 if (num_connections < 0)
2214 return GNUNET_SYSERR;
2216 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2218 ret = create_and_copy_friend_files(pg);
2219 if (ret != GNUNET_OK)
2222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2223 _("Failed during friend file copying!\n"));
2225 return GNUNET_SYSERR;
2230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2231 _("Friend files created/copied successfully!\n"));
2236 /* Use the create clique method to initially set all connections as blacklisted. */
2237 if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2238 create_clique (pg, &blacklist_connections);
2239 unblacklisted_connections = 0;
2240 /* Un-blacklist connections as per the topology specified */
2241 switch (restrict_topology)
2243 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2246 _("Blacklisting all but clique topology\n"));
2248 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2250 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2253 _("Blacklisting all but small world (ring) topology\n"));
2255 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2257 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2260 _("Blacklisting all but small world (2d-torus) topology\n"));
2262 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2264 case GNUNET_TESTING_TOPOLOGY_RING:
2266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2267 _("Blacklisting all but ring topology\n"));
2269 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2271 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2274 _("Blacklisting all but 2d torus topology\n"));
2276 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2278 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2281 _("Blacklisting all but Erdos-Renyi topology\n"));
2283 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2285 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2288 _("Blacklisting all but InterNAT topology\n"));
2290 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2292 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2295 _("Blacklisting all but Scale Free topology\n"));
2297 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2299 case GNUNET_TESTING_TOPOLOGY_LINE:
2301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2302 _("Blacklisting all but straight line topology\n"));
2304 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2306 case GNUNET_TESTING_TOPOLOGY_NONE:
2308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2309 _("Creating no blacklist topology (all peers can connect at transport level)\n"));
2315 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2317 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2318 if (ret != GNUNET_OK)
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2322 _("Failed during blacklist file copying!\n"));
2324 return GNUNET_SYSERR;
2329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2330 _("Blacklist files created/copied successfully!\n"));
2334 return num_connections;
2337 struct RandomContext
2342 struct GNUNET_TESTING_PeerGroup *pg;
2345 * uid of the first peer
2350 * Peer data for first peer.
2352 struct PeerData *first;
2355 * Random percentage to use
2360 struct MinimumContext
2365 struct GNUNET_TESTING_PeerGroup *pg;
2368 * uid of the first peer
2373 * Peer data for first peer.
2375 struct PeerData *first;
2378 * Number of conns per peer
2380 unsigned int num_to_add;
2383 * Permuted array of all possible connections. Only add the Nth
2384 * peer if it's in the Nth position.
2386 unsigned int *pg_array;
2389 * What number is the current element we are iterating over?
2391 unsigned int current;
2399 struct GNUNET_TESTING_PeerGroup *pg;
2402 * uid of the first peer
2407 * uid of the second peer
2409 uint32_t second_uid;
2412 * Peer data for first peer.
2414 struct PeerData *first;
2417 * Which peer has been chosen as the one to add?
2419 unsigned int chosen;
2422 * What number is the current element we are iterating over?
2424 unsigned int current;
2428 * Iterator for choosing random peers to connect.
2430 * @param cls closure, a RandomContext
2431 * @param key the key the second Daemon was stored under
2432 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2434 * @return GNUNET_YES to continue iteration
2437 random_connect_iterator (void *cls,
2438 const GNUNET_HashCode * key,
2441 struct RandomContext *random_ctx = cls;
2442 double random_number;
2443 uint32_t second_pos;
2444 GNUNET_HashCode first_hash;
2445 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2446 UINT64_MAX)) / ( (double) UINT64_MAX);
2447 if (random_number < random_ctx->percentage)
2449 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2451 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2452 uid_from_hash(key, &second_pos);
2453 hash_from_uid(random_ctx->first_uid, &first_hash);
2454 GNUNET_assert(random_ctx->pg->total > second_pos);
2455 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2461 * Iterator for adding at least X peers to a peers connection set.
2463 * @param cls closure, MinimumContext
2464 * @param key the key the second Daemon was stored under
2465 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2467 * @return GNUNET_YES to continue iteration
2470 minimum_connect_iterator (void *cls,
2471 const GNUNET_HashCode * key,
2474 struct MinimumContext *min_ctx = cls;
2475 uint32_t second_pos;
2476 GNUNET_HashCode first_hash;
2479 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2481 for (i = 0; i < min_ctx->num_to_add; i++)
2483 if (min_ctx->pg_array[i] == min_ctx->current)
2485 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2486 uid_from_hash(key, &second_pos);
2487 hash_from_uid(min_ctx->first_uid, &first_hash);
2488 GNUNET_assert(min_ctx->pg->total > second_pos);
2489 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));
2490 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2491 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2498 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2504 * Iterator for adding peers to a connection set based on a depth first search.
2506 * @param cls closure, MinimumContext
2507 * @param key the key the second daemon was stored under
2508 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2510 * @return GNUNET_YES to continue iteration
2513 dfs_connect_iterator (void *cls,
2514 const GNUNET_HashCode * key,
2517 struct DFSContext *dfs_ctx = cls;
2518 GNUNET_HashCode first_hash;
2520 if (dfs_ctx->current == dfs_ctx->chosen)
2522 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2523 uid_from_hash(key, &dfs_ctx->second_uid);
2524 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2525 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));
2526 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2527 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2528 return GNUNET_NO; /* We have found our peer, don't iterate more */
2537 * From the set of connections possible, choose percentage percent of connections
2538 * to actually connect.
2540 * @param pg the peergroup we are dealing with
2541 * @param percentage what percent of total connections to make
2544 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2546 struct RandomContext random_ctx;
2549 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2551 random_ctx.first_uid = pg_iter;
2552 random_ctx.first = &pg->peers[pg_iter];
2553 random_ctx.percentage = percentage;
2555 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2556 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2557 /* Now remove the old connections */
2558 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2559 /* And replace with the random set */
2560 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2565 * From the set of connections possible, choose at least num connections per
2568 * @param pg the peergroup we are dealing with
2569 * @param num how many connections at least should each peer have (if possible)?
2572 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2574 struct MinimumContext minimum_ctx;
2577 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2579 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2582 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2584 minimum_ctx.first_uid = pg_iter;
2585 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2586 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2587 minimum_ctx.first = &pg->peers[pg_iter];
2588 minimum_ctx.pg = pg;
2589 minimum_ctx.num_to_add = num;
2590 minimum_ctx.current = 0;
2591 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2592 &minimum_connect_iterator,
2596 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2598 /* Remove the "old" connections */
2599 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2600 /* And replace with the working set */
2601 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2608 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2611 unsigned int pg_iter;
2615 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2617 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2624 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2627 unsigned int pg_iter;
2631 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2633 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2640 * From the set of connections possible, choose at least num connections per
2641 * peer based on depth first traversal of peer connections. If DFS leaves
2642 * peers unconnected, ensure those peers get connections.
2644 * @param pg the peergroup we are dealing with
2645 * @param num how many connections at least should each peer have (if possible)?
2648 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2650 struct DFSContext dfs_ctx;
2653 uint32_t starting_peer;
2654 uint32_t least_connections;
2655 GNUNET_HashCode second_hash;
2657 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2659 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2664 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
2666 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
2668 least_connections = -1; /* Set to very high number */
2669 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2671 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
2673 starting_peer = pg_iter;
2674 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2679 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
2685 /* Choose a random peer from the chosen peers set of connections to add */
2686 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
2687 dfs_ctx.first_uid = starting_peer;
2688 dfs_ctx.first = &pg->peers[starting_peer];
2690 dfs_ctx.current = 0;
2692 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
2693 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
2694 hash_from_uid(dfs_ctx.second_uid, &second_hash);
2695 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
2696 starting_peer = dfs_ctx.second_uid;
2699 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2701 /* Remove the "old" connections */
2702 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2703 /* And replace with the working set */
2704 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2709 * There are many ways to connect peers that are supported by this function.
2710 * To connect peers in the same topology that was created via the
2711 * GNUNET_TESTING_create_topology, the topology variable must be set to
2712 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
2713 * a new instance of that topology will be generated and attempted to be
2714 * connected. This could result in some connections being impossible,
2715 * because some topologies are non-deterministic.
2717 * @param pg the peer group struct representing the running peers
2718 * @param topology which topology to connect the peers in
2719 * @param options options for connecting the topology
2720 * @param option_modifier modifier for options that take a parameter
2721 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
2724 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2725 enum GNUNET_TESTING_Topology topology,
2726 enum GNUNET_TESTING_TopologyOption options,
2727 double option_modifier)
2731 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2732 #if VERBOSE_TOPOLOGY
2733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2734 _("Creating clique CONNECT topology\n"));
2736 create_clique (pg, &add_actual_connections);
2738 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2739 #if VERBOSE_TOPOLOGY
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2741 _("Creating small world (ring) CONNECT topology\n"));
2743 create_small_world_ring (pg, &add_actual_connections);
2745 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2746 #if VERBOSE_TOPOLOGY
2747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2748 _("Creating small world (2d-torus) CONNECT topology\n"));
2750 create_small_world (pg, &add_actual_connections);
2752 case GNUNET_TESTING_TOPOLOGY_RING:
2753 #if VERBOSE_TOPOLOGY
2754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2755 _("Creating ring CONNECT topology\n"));
2757 create_ring (pg, &add_actual_connections);
2759 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2760 #if VERBOSE_TOPOLOGY
2761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2762 _("Creating 2d torus CONNECT topology\n"));
2764 create_2d_torus (pg, &add_actual_connections);
2766 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2767 #if VERBOSE_TOPOLOGY
2768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2769 _("Creating Erdos-Renyi CONNECT topology\n"));
2771 create_erdos_renyi (pg, &add_actual_connections);
2773 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2774 #if VERBOSE_TOPOLOGY
2775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2776 _("Creating InterNAT CONNECT topology\n"));
2778 create_nated_internet (pg, &add_actual_connections);
2780 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2781 #if VERBOSE_TOPOLOGY
2782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2783 _("Creating Scale Free CONNECT topology\n"));
2785 create_scale_free (pg, &add_actual_connections);
2787 case GNUNET_TESTING_TOPOLOGY_LINE:
2788 #if VERBOSE_TOPOLOGY
2789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2790 _("Creating straight line CONNECT topology\n"));
2792 create_line (pg, &add_actual_connections);
2794 case GNUNET_TESTING_TOPOLOGY_NONE:
2795 #if VERBOSE_TOPOLOGY
2796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2797 _("Creating no CONNECT topology\n"));
2799 copy_allowed_topology(pg);
2802 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2803 _("Unknown topology specification, can't connect peers!\n"));
2804 return GNUNET_SYSERR;
2809 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
2810 #if VERBOSE_TOPOLOGY
2811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2812 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
2814 choose_random_connections(pg, option_modifier);
2816 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
2817 #if VERBOSE_TOPOLOGY
2818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2819 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
2821 choose_minimum(pg, (unsigned int)option_modifier);
2823 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
2824 #if VERBOSE_TOPOLOGY
2825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2826 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
2828 perform_dfs(pg, (int)option_modifier);
2830 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
2832 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
2838 return connect_topology(pg);
2842 * Callback that is called whenever a hostkey is generated
2843 * for a peer. Call the real callback and decrement the
2844 * starting counter for the peergroup.
2846 * @param cls closure
2847 * @param id identifier for the daemon, NULL on error
2848 * @param d handle for the daemon
2849 * @param emsg error message (NULL on success)
2851 static void internal_hostkey_callback (void *cls,
2852 const struct GNUNET_PeerIdentity *id,
2853 struct GNUNET_TESTING_Daemon *d,
2856 struct InternalStartContext *internal_context = cls;
2857 internal_context->peer->pg->starting--;
2858 internal_context->peer->pg->started++;
2859 if (internal_context->hostkey_callback != NULL)
2860 internal_context->hostkey_callback(internal_context->hostkey_cls, id, d, emsg);
2861 else if (internal_context->peer->pg->started == internal_context->peer->pg->total)
2863 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
2864 GNUNET_TESTING_daemons_continue_startup(internal_context->peer->pg);
2869 * Callback that is called whenever a peer has finished starting.
2870 * Call the real callback and decrement the starting counter
2871 * for the peergroup.
2873 * @param cls closure
2874 * @param id identifier for the daemon, NULL on error
2875 * @param d handle for the daemon
2876 * @param emsg error message (NULL on success)
2878 static void internal_startup_callback (void *cls,
2879 const struct GNUNET_PeerIdentity *id,
2880 const struct GNUNET_CONFIGURATION_Handle *cfg,
2881 struct GNUNET_TESTING_Daemon *d,
2884 struct InternalStartContext *internal_context = cls;
2885 internal_context->peer->pg->starting--;
2886 if (internal_context->start_cb != NULL)
2887 internal_context->start_cb(internal_context->start_cb_cls, id, cfg, d, emsg);
2891 internal_continue_startup (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2893 struct InternalStartContext *internal_context = cls;
2895 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2900 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
2902 internal_context->peer->pg->starting++;
2903 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
2907 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_continue_startup, internal_context);
2912 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2914 struct InternalStartContext *internal_context = cls;
2916 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2921 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
2923 internal_context->peer->pg->starting++;
2924 internal_context->peer->daemon = GNUNET_TESTING_daemon_start (internal_context->peer->pg->sched,
2925 internal_context->peer->cfg,
2926 internal_context->timeout,
2927 internal_context->hostname,
2928 &internal_hostkey_callback,
2930 &internal_startup_callback,
2935 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_start, internal_context);
2940 * Function which continues a peer group starting up
2941 * after successfully generating hostkeys for each peer.
2943 * @param pg the peer group to continue starting
2947 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
2952 for (i = 0; i < pg->total; i++)
2954 GNUNET_SCHEDULER_add_now (pg->sched, &internal_continue_startup, &pg->peers[i].internal_context);
2955 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
2960 * Start count gnunet instances with the same set of transports and
2961 * applications. The port numbers (any option called "PORT") will be
2962 * adjusted to ensure that no two peers running on the same system
2963 * have the same port(s) in their respective configurations.
2965 * @param sched scheduler to use
2966 * @param cfg configuration template to use
2967 * @param total number of daemons to start
2968 * @param timeout total time allowed for peers to start
2969 * @param hostkey_callback function to call on each peers hostkey generation
2970 * if NULL, peers will be started by this call, if non-null,
2971 * GNUNET_TESTING_daemons_continue_startup must be called after
2972 * successful hostkey generation
2973 * @param hostkey_cls closure for hostkey callback
2974 * @param cb function to call on each daemon that was started
2975 * @param cb_cls closure for cb
2976 * @param connect_callback function to call each time two hosts are connected
2977 * @param connect_callback_cls closure for connect_callback
2978 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
2979 * everything on localhost).
2980 * @return NULL on error, otherwise handle to control peer group
2982 struct GNUNET_TESTING_PeerGroup *
2983 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
2984 const struct GNUNET_CONFIGURATION_Handle *cfg,
2986 struct GNUNET_TIME_Relative timeout,
2987 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
2989 GNUNET_TESTING_NotifyDaemonRunning cb,
2991 GNUNET_TESTING_NotifyConnection
2992 connect_callback, void *connect_callback_cls,
2993 const char *hostnames)
2995 struct GNUNET_TESTING_PeerGroup *pg;
2999 const char *hostname;
3000 char *baseservicehome;
3001 char *newservicehome;
3003 struct GNUNET_CONFIGURATION_Handle *pcfg;
3005 unsigned int hostcnt;
3015 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
3018 pg->notify_connection = connect_callback;
3019 pg->notify_connection_cls = connect_callback_cls;
3021 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
3022 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
3023 if (NULL != hostnames)
3026 /* skip leading spaces */
3027 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
3030 while ('\0' != *rpos)
3032 if (isspace ( (unsigned char) *rpos))
3036 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3038 start = GNUNET_strdup (hostnames);
3040 while ('\0' != *pos)
3042 if (isspace ( (unsigned char) *pos))
3045 if (strlen (start) > 0)
3047 pg->hosts[off].minport = LOW_PORT;
3048 pg->hosts[off++].hostname = start;
3054 if (strlen (start) > 0)
3056 pg->hosts[off].minport = LOW_PORT;
3057 pg->hosts[off++].hostname = start;
3061 GNUNET_free (start);
3062 GNUNET_free (pg->hosts);
3066 minport = 0; /* make gcc happy */
3073 for (off = 0; off < total; off++)
3077 hostname = pg->hosts[off % hostcnt].hostname;
3078 pcfg = make_config (cfg,
3079 &pg->hosts[off % hostcnt].minport,
3086 pcfg = make_config (cfg,
3094 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3096 ("Could not create configuration for peer number %u on `%s'!\n"),
3097 off, hostname == NULL ? "localhost" : hostname);
3102 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
3105 GNUNET_asprintf (&newservicehome,
3106 "%s/%d/", baseservicehome, off);
3107 GNUNET_free (baseservicehome);
3111 tmpdir = getenv ("TMPDIR");
3112 tmpdir = tmpdir ? tmpdir : "/tmp";
3113 GNUNET_asprintf (&newservicehome,
3116 "gnunet-testing-test-test", off);
3118 GNUNET_CONFIGURATION_set_value_string (pcfg,
3120 "SERVICEHOME", newservicehome);
3121 GNUNET_free (newservicehome);
3122 pg->peers[off].cfg = pcfg;
3123 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
3124 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
3125 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
3126 pg->peers[off].pg = pg;
3128 pg->peers[off].internal_context.peer = &pg->peers[off];
3129 pg->peers[off].internal_context.timeout = timeout;
3130 pg->peers[off].internal_context.hostname = hostname;
3131 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
3132 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
3133 pg->peers[off].internal_context.start_cb = cb;
3134 pg->peers[off].internal_context.start_cb_cls = cb_cls;
3136 GNUNET_SCHEDULER_add_now (sched, &internal_start, &pg->peers[off].internal_context);
3143 * Get a daemon by number, so callers don't have to do nasty
3144 * offsetting operation.
3146 struct GNUNET_TESTING_Daemon *
3147 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
3149 if (position < pg->total)
3150 return pg->peers[position].daemon;
3156 * Prototype of a function that will be called when a
3157 * particular operation was completed the testing library.
3159 * @param cls closure (a struct RestartContext)
3160 * @param id id of the peer that was restarted
3161 * @param cfg handle to the configuration of the peer
3162 * @param d handle to the daemon that was restarted
3163 * @param emsg NULL on success
3165 void restart_callback (void *cls,
3166 const struct GNUNET_PeerIdentity *id,
3167 const struct GNUNET_CONFIGURATION_Handle *cfg,
3168 struct GNUNET_TESTING_Daemon *d,
3171 struct RestartContext *restart_context = cls;
3175 restart_context->peers_restarted++;
3179 restart_context->peers_restart_failed++;
3182 if (restart_context->peers_restarted == restart_context->peer_group->total)
3184 restart_context->callback(restart_context->callback_cls, NULL);
3185 GNUNET_free(restart_context);
3187 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
3189 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
3190 GNUNET_free(restart_context);
3196 * Callback for informing us about a successful
3197 * or unsuccessful churn stop call.
3199 * @param cls a ChurnContext
3200 * @param emsg NULL on success, non-NULL on failure
3204 churn_stop_callback (void *cls, const char *emsg)
3206 struct ChurnContext *churn_ctx = cls;
3207 unsigned int total_left;
3208 char *error_message;
3210 error_message = NULL;
3213 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3214 "Churn stop callback failed with error `%s'\n", emsg);
3215 churn_ctx->num_failed_stop++;
3219 churn_ctx->num_to_stop--;
3223 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3224 "Stopped peer, %d left.\n",
3225 churn_ctx->num_to_stop);
3227 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3229 if (total_left == 0)
3231 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3233 GNUNET_asprintf(&error_message,
3234 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3235 churn_ctx->num_failed_start,
3236 churn_ctx->num_failed_stop);
3238 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3239 GNUNET_free_non_null(error_message);
3240 GNUNET_free(churn_ctx);
3245 * Callback for informing us about a successful
3246 * or unsuccessful churn start call.
3248 * @param cls a ChurnContext
3249 * @param id the peer identity of the started peer
3250 * @param cfg the handle to the configuration of the peer
3251 * @param d handle to the daemon for the peer
3252 * @param emsg NULL on success, non-NULL on failure
3256 churn_start_callback (void *cls,
3257 const struct GNUNET_PeerIdentity *id,
3258 const struct GNUNET_CONFIGURATION_Handle *cfg,
3259 struct GNUNET_TESTING_Daemon *d,
3262 struct ChurnContext *churn_ctx = cls;
3263 unsigned int total_left;
3264 char *error_message;
3266 error_message = NULL;
3269 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3270 "Churn stop callback failed with error `%s'\n",
3272 churn_ctx->num_failed_start++;
3276 churn_ctx->num_to_start--;
3280 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3281 "Started peer, %d left.\n",
3282 churn_ctx->num_to_start);
3285 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3287 if (total_left == 0)
3289 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3290 GNUNET_asprintf(&error_message,
3291 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3292 churn_ctx->num_failed_start,
3293 churn_ctx->num_failed_stop);
3294 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3295 GNUNET_free_non_null(error_message);
3296 GNUNET_free(churn_ctx);
3302 * Simulate churn by stopping some peers (and possibly
3303 * re-starting others if churn is called multiple times). This
3304 * function can only be used to create leave-join churn (peers "never"
3305 * leave for good). First "voff" random peers that are currently
3306 * online will be taken offline; then "von" random peers that are then
3307 * offline will be put back online. No notifications will be
3308 * generated for any of these operations except for the callback upon
3311 * @param pg handle for the peer group
3312 * @param voff number of peers that should go offline
3313 * @param von number of peers that should come back online;
3314 * must be zero on first call (since "testbed_start"
3315 * always starts all of the peers)
3316 * @param timeout how long to wait for operations to finish before
3318 * @param cb function to call at the end
3319 * @param cb_cls closure for cb
3322 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
3325 struct GNUNET_TIME_Relative timeout,
3326 GNUNET_TESTING_NotifyCompletion cb,
3329 struct ChurnContext *churn_ctx;
3330 unsigned int running;
3331 unsigned int stopped;
3332 unsigned int total_running;
3333 unsigned int total_stopped;
3335 unsigned int *running_arr;
3336 unsigned int *stopped_arr;
3337 unsigned int *running_permute;
3338 unsigned int *stopped_permute;
3343 if ((von == 0) && (voff == 0)) /* No peers at all? */
3349 for (i = 0; i < pg->total; i++)
3351 if (pg->peers[i].daemon->running == GNUNET_YES)
3353 GNUNET_assert(running != -1);
3358 GNUNET_assert(stopped != -1);
3365 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
3366 cb(cb_cls, "Trying to stop more peers than are currently running!");
3372 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
3373 cb(cb_cls, "Trying to start more peers than are currently stopped!");
3377 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3381 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
3385 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
3387 running_permute = NULL;
3388 stopped_permute = NULL;
3391 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
3393 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
3395 total_running = running;
3396 total_stopped = stopped;
3400 churn_ctx->num_to_start = von;
3401 churn_ctx->num_to_stop = voff;
3403 churn_ctx->cb_cls = cb_cls;
3405 for (i = 0; i < pg->total; i++)
3407 if (pg->peers[i].daemon->running == GNUNET_YES)
3409 GNUNET_assert((running_arr != NULL) && (total_running > running));
3410 running_arr[running] = i;
3415 GNUNET_assert((stopped_arr != NULL) && (total_stopped > stopped));
3416 stopped_arr[stopped] = i;
3421 for (i = 0; i < voff; i++)
3424 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
3426 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
3428 &churn_stop_callback, churn_ctx,
3429 GNUNET_NO, GNUNET_YES);
3432 for (i = 0; i < von; i++)
3435 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
3437 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
3438 timeout, &churn_start_callback, churn_ctx);
3441 GNUNET_free_non_null(running_arr);
3442 GNUNET_free_non_null(stopped_arr);
3443 GNUNET_free_non_null(running_permute);
3444 GNUNET_free_non_null(stopped_permute);
3449 * Restart all peers in the given group.
3451 * @param pg the handle to the peer group
3452 * @param callback function to call on completion (or failure)
3453 * @param callback_cls closure for the callback function
3456 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyCompletion callback, void *callback_cls)
3458 struct RestartContext *restart_context;
3463 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
3464 restart_context->peer_group = pg;
3465 restart_context->peers_restarted = 0;
3466 restart_context->callback = callback;
3467 restart_context->callback_cls = callback_cls;
3469 for (off = 0; off < pg->total; off++)
3471 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
3477 * Start or stop an individual peer from the given group.
3479 * @param pg handle to the peer group
3480 * @param offset which peer to start or stop
3481 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
3482 * @param timeout how long to wait for shutdown
3483 * @param cb function to call at the end
3484 * @param cb_cls closure for cb
3487 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
3488 unsigned int offset,
3490 struct GNUNET_TIME_Relative timeout,
3491 GNUNET_TESTING_NotifyCompletion cb,
3494 struct ChurnContext *churn_ctx;
3496 if (GNUNET_NO == desired_status)
3498 if (NULL != pg->peers[offset].daemon)
3500 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3501 churn_ctx->num_to_start = 0;
3502 churn_ctx->num_to_stop = 1;
3504 churn_ctx->cb_cls = cb_cls;
3505 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
3506 timeout, &churn_stop_callback, churn_ctx,
3507 GNUNET_NO, GNUNET_YES);
3510 else if (GNUNET_YES == desired_status)
3512 if (NULL == pg->peers[offset].daemon)
3514 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3515 churn_ctx->num_to_start = 1;
3516 churn_ctx->num_to_stop = 0;
3518 churn_ctx->cb_cls = cb_cls;
3519 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
3520 timeout, &churn_start_callback, churn_ctx);
3529 * Callback for shutting down peers in a peer group.
3531 * @param cls closure (struct ShutdownContext)
3532 * @param emsg NULL on success
3534 void internal_shutdown_callback (void *cls,
3537 struct ShutdownContext *shutdown_ctx = cls;
3541 shutdown_ctx->peers_down++;
3545 shutdown_ctx->peers_failed++;
3548 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
3550 if (shutdown_ctx->peers_failed > 0)
3551 shutdown_ctx->cb(shutdown_ctx->cb_cls, "Not all peers successfully shut down!");
3553 shutdown_ctx->cb(shutdown_ctx->cb_cls, NULL);
3554 GNUNET_free(shutdown_ctx);
3559 * Shutdown all peers started in the given group.
3561 * @param pg handle to the peer group
3562 * @param timeout how long to wait for shutdown
3563 * @param cb callback to notify upon success or failure
3564 * @param cb_cls closure for cb
3567 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
3568 struct GNUNET_TIME_Relative timeout,
3569 GNUNET_TESTING_NotifyCompletion cb,
3573 struct ShutdownContext *shutdown_ctx;
3574 GNUNET_TESTING_NotifyCompletion shutdown_cb;
3575 void *shutdown_cb_cls;
3577 GNUNET_assert(pg->total > 0);
3580 shutdown_ctx = NULL;
3582 if ((cb != NULL) && (pg->total > 0))
3584 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
3585 shutdown_ctx->cb = cb;
3586 shutdown_ctx->cb_cls = cb_cls;
3587 shutdown_ctx->total_peers = pg->total;
3588 shutdown_cb = &internal_shutdown_callback;
3589 shutdown_cb_cls = cb_cls;
3592 for (off = 0; off < pg->total; off++)
3594 GNUNET_assert(NULL != pg->peers[off].daemon);
3595 GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
3596 if (NULL != pg->peers[off].cfg)
3597 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
3598 if (pg->peers[off].allowed_peers != NULL)
3599 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
3600 if (pg->peers[off].connect_peers != NULL)
3601 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
3602 if (pg->peers[off].blacklisted_peers != NULL)
3603 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
3605 GNUNET_free (pg->peers);
3606 if (NULL != pg->hosts)
3608 GNUNET_free (pg->hosts[0].hostname);
3609 GNUNET_free (pg->hosts);
3615 /* end of testing_group.c */