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 (strcasecmp(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 (strcasecmp(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_CONFIGURATION_set_value_string(uc.ret, "transport", "ACCEPT_FROM", allowed_hosts);
669 GNUNET_free_non_null(control_host);
670 GNUNET_free(allowed_hosts);
674 /* arm needs to know to allow connections from the host on which it is running,
675 * otherwise gnunet-arm is unable to connect to it in some instances */
676 if (hostname != NULL)
678 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
679 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
680 GNUNET_free(allowed_hosts);
683 *port = (uint16_t) uc.nport;
690 * Add entries to the peers connect list
692 * @param pg the peer group we are working with
693 * @param first index of the first peer
694 * @param second index of the second peer
696 * @return the number of connections added
697 * technically should only be 0 or 2
701 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
707 GNUNET_HashCode hash_first;
708 GNUNET_HashCode hash_second;
710 hash_from_uid(first, &hash_first);
711 hash_from_uid(second, &hash_second);
713 add_first = GNUNET_NO;
714 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
716 add_first = GNUNET_YES;
719 add_second = GNUNET_NO;
720 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
722 add_second = GNUNET_YES;
728 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
729 pg->peers[first].num_connections++;
735 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
736 pg->peers[second].num_connections++;
745 * Add entries to the peers allowed connections list
747 * @param pg the peer group we are working with
748 * @param first index of the first peer
749 * @param second index of the second peer
751 * @return the number of connections added (can be 0, 1 or 2)
752 * technically should only be 0 or 2, but the small price
753 * of iterating over the lists (hashmaps in the future)
754 * for being sure doesn't bother me!
758 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
762 struct PeerConnection *first_iter;
763 struct PeerConnection *second_iter;
764 struct PeerConnection *new_first;
765 struct PeerConnection *new_second;
770 GNUNET_HashCode hash_first;
771 GNUNET_HashCode hash_second;
773 hash_from_uid(first, &hash_first);
774 hash_from_uid(second, &hash_second);
776 add_first = GNUNET_NO;
777 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
779 add_first = GNUNET_YES;
782 add_second = GNUNET_NO;
783 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
785 add_second = GNUNET_YES;
788 first_iter = pg->peers[first].connected_peers;
789 while (first_iter != NULL)
791 if (first_iter->daemon == pg->peers[second].daemon)
792 add_first = GNUNET_NO;
793 first_iter = first_iter->next;
796 second_iter = pg->peers[second].connected_peers;
797 add_second = GNUNET_YES;
798 while (second_iter != NULL)
800 if (second_iter->daemon == pg->peers[first].daemon)
801 add_second = GNUNET_NO;
802 second_iter = second_iter->next;
809 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
811 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
812 new_first->daemon = pg->peers[second].daemon;
813 new_first->next = pg->peers[first].connected_peers;
814 pg->peers[first].connected_peers = new_first;
816 pg->peers[first].num_connections++;
822 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
824 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
825 new_second->daemon = pg->peers[first].daemon;
826 new_second->next = pg->peers[second].connected_peers;
827 pg->peers[second].connected_peers = new_second;
828 pg->peers[first].num_connections++;
830 pg->peers[second].num_connections++;
838 * Add entries to the peers blacklisted list
840 * @param pg the peer group we are working with
841 * @param first index of the first peer
842 * @param second index of the second peer
844 * @return the number of connections added (can be 0, 1 or 2)
848 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
853 GNUNET_HashCode hash_first;
854 GNUNET_HashCode hash_second;
856 hash_from_uid(first, &hash_first);
857 hash_from_uid(second, &hash_second);
859 add_first = GNUNET_NO;
860 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
862 add_first = GNUNET_YES;
865 add_second = GNUNET_NO;
866 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
868 add_second = GNUNET_YES;
874 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
875 pg->peers[first].num_connections++;
881 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
882 pg->peers[second].num_connections++;
890 * Remove entries from the peers blacklisted list
892 * @param pg the peer group we are working with
893 * @param first index of the first peer
894 * @param second index of the second peer
896 * @return the number of connections removed (can be 0, 1 or 2)
900 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
905 GNUNET_HashCode hash_first;
906 GNUNET_HashCode hash_second;
908 hash_from_uid(first, &hash_first);
909 hash_from_uid(second, &hash_second);
911 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
912 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
917 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
923 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
931 * Scale free network construction as described in:
933 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
935 * Start with a network of "one" peer, then progressively add
936 * peers up to the total number. At each step, iterate over
937 * all possible peers and connect new peer based on number of
938 * existing connections of the target peer.
940 * @param pg the peer group we are dealing with
941 * @param proc the connection processor to use
943 * @return the number of connections created
946 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
949 unsigned int total_connections;
950 unsigned int outer_count;
952 unsigned int previous_total_connections;
956 GNUNET_assert(pg->total > 1);
958 /* Add a connection between the first two nodes */
959 total_connections = proc(pg, 0, 1);
961 for (outer_count = 1; outer_count < pg->total; outer_count++)
963 previous_total_connections = total_connections;
964 for (i = 0; i < outer_count; i++)
966 probability = pg->peers[i].num_connections / (double)previous_total_connections;
967 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
968 UINT64_MAX)) / ( (double) UINT64_MAX);
970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
971 "Considering connecting peer %d to peer %d\n",
974 if (random < probability)
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 "Connecting peer %d to peer %d\n",
981 total_connections += proc(pg, outer_count, i);
986 return total_connections;
990 * Create a topology given a peer group (set of running peers)
991 * and a connection processor.
993 * @param pg the peergroup to create the topology on
994 * @param proc the connection processor to call to actually set
995 * up connections between two peers
997 * @return the number of connections that were set up
1001 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1005 unsigned int natLog;
1006 unsigned int randomPeer;
1007 double random, logNModifier, percentage;
1008 unsigned int smallWorldConnections;
1013 unsigned int useAnd;
1014 int connect_attempts;
1016 logNModifier = 0.5; /* FIXME: default value? */
1017 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1022 if (sscanf(p_string, "%lf", &logNModifier) != 1)
1023 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1024 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1028 GNUNET_free (p_string);
1030 percentage = 0.5; /* FIXME: default percentage? */
1031 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1036 if (sscanf(p_string, "%lf", &percentage) != 1)
1037 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1038 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1042 GNUNET_free (p_string);
1044 natLog = log (pg->total);
1045 connsPerPeer = ceil (natLog * logNModifier);
1047 if (connsPerPeer % 2 == 1)
1050 smallWorldConnections = 0;
1051 connect_attempts = 0;
1052 for (i = 0; i < pg->total; i++)
1055 max = i + connsPerPeer / 2;
1056 min = i - connsPerPeer / 2;
1058 if (max > pg->total - 1)
1060 max = max - pg->total;
1066 min = pg->total - 1 + min;
1070 for (j = 0; j < connsPerPeer / 2; j++)
1072 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1073 UINT64_MAX) / ( (double) UINT64_MAX));
1074 if (random < percentage)
1076 /* Connect to uniformly selected random peer */
1078 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1080 while ((((randomPeer < max) && (randomPeer > min))
1081 && (useAnd == 0)) || (((randomPeer > min)
1082 || (randomPeer < max))
1086 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1089 smallWorldConnections +=
1090 proc (pg, i, randomPeer);
1094 nodeToConnect = i + j + 1;
1095 if (nodeToConnect > pg->total - 1)
1097 nodeToConnect = nodeToConnect - pg->total;
1100 proc (pg, i, nodeToConnect);
1106 connect_attempts += smallWorldConnections;
1108 return connect_attempts;
1112 * Create a topology given a peer group (set of running peers)
1113 * and a connection processor.
1115 * @param pg the peergroup to create the topology on
1116 * @param proc the connection processor to call to actually set
1117 * up connections between two peers
1119 * @return the number of connections that were set up
1123 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1125 unsigned int outer_count, inner_count;
1126 unsigned int cutoff;
1127 int connect_attempts;
1128 double nat_percentage;
1131 nat_percentage = 0.6; /* FIXME: default percentage? */
1132 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1137 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1138 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1139 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1143 GNUNET_free (p_string);
1148 cutoff = (unsigned int) (nat_percentage * pg->total);
1150 connect_attempts = 0;
1152 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1154 for (inner_count = outer_count + 1; inner_count < pg->total;
1157 if ((outer_count > cutoff) || (inner_count > cutoff))
1160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1161 "Connecting peer %d to peer %d\n",
1162 outer_count, inner_count);
1164 connect_attempts += proc(pg, outer_count, inner_count);
1169 return connect_attempts;
1174 * Create a topology given a peer group (set of running peers)
1175 * and a connection processor.
1177 * @param pg the peergroup to create the topology on
1178 * @param proc the connection processor to call to actually set
1179 * up connections between two peers
1181 * @return the number of connections that were set up
1185 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1187 unsigned int i, j, k;
1188 unsigned int square;
1191 unsigned int toggle = 1;
1192 unsigned int nodeToConnect;
1193 unsigned int natLog;
1194 unsigned int node1Row;
1195 unsigned int node1Col;
1196 unsigned int node2Row;
1197 unsigned int node2Col;
1198 unsigned int distance;
1199 double probability, random, percentage;
1200 unsigned int smallWorldConnections;
1202 int connect_attempts;
1203 square = floor (sqrt (pg->total));
1207 percentage = 0.5; /* FIXME: default percentage? */
1208 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1213 if (sscanf(p_string, "%lf", &percentage) != 1)
1214 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1215 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1219 GNUNET_free (p_string);
1221 probability = 0.5; /* FIXME: default percentage? */
1222 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1227 if (sscanf(p_string, "%lf", &probability) != 1)
1228 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1229 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1233 GNUNET_free (p_string);
1235 if (square * square != pg->total)
1237 while (rows * cols < pg->total)
1239 if (toggle % 2 == 0)
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1253 connect_attempts = 0;
1254 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1255 * to the node to its right and above. Once this is over, we'll have our torus!
1256 * Special case for the last node (if the rows and columns are not equal), connect
1257 * to the first in the row to maintain topology.
1259 for (i = 0; i < pg->total; i++)
1261 /* First connect to the node to the right */
1262 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1263 nodeToConnect = i + 1;
1264 else if (i + 1 == pg->total)
1265 nodeToConnect = rows * cols - cols;
1267 nodeToConnect = i - cols + 1;
1269 connect_attempts += proc (pg, i, nodeToConnect);
1272 nodeToConnect = (rows * cols) - cols + i;
1274 nodeToConnect = i - cols;
1276 if (nodeToConnect < pg->total)
1277 connect_attempts += proc (pg, i, nodeToConnect);
1279 natLog = log (pg->total);
1280 #if VERBOSE_TESTING > 2
1281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1282 _("natural log of %d is %d, will run %d iterations\n"),
1283 pg->total, natLog, (int) (natLog * percentage));
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1286 smallWorldConnections = 0;
1287 for (i = 0; i < (int) (natLog * percentage); i++)
1289 for (j = 0; j < pg->total; j++)
1291 /* Determine the row and column of node at position j on the 2d torus */
1292 node1Row = j / cols;
1293 node1Col = j - (node1Row * cols);
1294 for (k = 0; k < pg->total; k++)
1296 /* Determine the row and column of node at position k on the 2d torus */
1297 node2Row = k / cols;
1298 node2Col = k - (node2Row * cols);
1299 /* Simple Cartesian distance */
1300 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1303 /* Calculate probability as 1 over the square of the distance */
1304 probability = 1.0 / (distance * distance);
1305 /* Choose a random value between 0 and 1 */
1306 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1307 UINT64_MAX)) / ( (double) UINT64_MAX);
1308 /* If random < probability, then connect the two nodes */
1309 if (random < probability)
1310 smallWorldConnections += proc (pg, j, k);
1316 connect_attempts += smallWorldConnections;
1317 #if VERBOSE_TESTING > 2
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1319 _("Total connections added for small world: %d!\n"),
1320 smallWorldConnections);
1322 return connect_attempts;
1326 * Create a topology given a peer group (set of running peers)
1327 * and a connection processor.
1329 * @param pg the peergroup to create the topology on
1330 * @param proc the connection processor to call to actually set
1331 * up connections between two peers
1333 * @return the number of connections that were set up
1337 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1340 unsigned int outer_count;
1341 unsigned int inner_count;
1342 int connect_attempts;
1346 probability = 0.5; /* FIXME: default percentage? */
1347 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1352 if (sscanf(p_string, "%lf", &probability) != 1)
1353 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1354 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1358 GNUNET_free (p_string);
1360 connect_attempts = 0;
1361 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1363 for (inner_count = outer_count + 1; inner_count < pg->total;
1366 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1367 UINT64_MAX)) / ( (double) UINT64_MAX);
1369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1370 _("rand is %f probability is %f\n"), temp_rand,
1373 if (temp_rand < probability)
1375 connect_attempts += proc (pg, outer_count, inner_count);
1380 return connect_attempts;
1384 * Create a topology given a peer group (set of running peers)
1385 * and a connection processor.
1387 * @param pg the peergroup to create the topology on
1388 * @param proc the connection processor to call to actually set
1389 * up connections between two peers
1391 * @return the number of connections that were set up
1395 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1398 unsigned int square;
1401 unsigned int toggle = 1;
1402 unsigned int nodeToConnect;
1403 int connect_attempts;
1405 connect_attempts = 0;
1407 square = floor (sqrt (pg->total));
1411 if (square * square != pg->total)
1413 while (rows * cols < pg->total)
1415 if (toggle % 2 == 0)
1424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1425 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1428 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1429 * to the node to its right and above. Once this is over, we'll have our torus!
1430 * Special case for the last node (if the rows and columns are not equal), connect
1431 * to the first in the row to maintain topology.
1433 for (i = 0; i < pg->total; i++)
1435 /* First connect to the node to the right */
1436 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1437 nodeToConnect = i + 1;
1438 else if (i + 1 == pg->total)
1439 nodeToConnect = rows * cols - cols;
1441 nodeToConnect = i - cols + 1;
1443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1444 "Connecting peer %d to peer %d\n",
1447 connect_attempts += proc(pg, i, nodeToConnect);
1449 /* Second connect to the node immediately above */
1451 nodeToConnect = (rows * cols) - cols + i;
1453 nodeToConnect = i - cols;
1455 if (nodeToConnect < pg->total)
1458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1459 "Connecting peer %d to peer %d\n",
1462 connect_attempts += proc(pg, i, nodeToConnect);
1467 return connect_attempts;
1472 * Create a topology given a peer group (set of running peers)
1473 * and a connection processor.
1475 * @param pg the peergroup to create the topology on
1476 * @param proc the connection processor to call to actually set
1477 * up connections between two peers
1479 * @return the number of connections that were set up
1483 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1485 unsigned int outer_count;
1486 unsigned int inner_count;
1487 int connect_attempts;
1489 connect_attempts = 0;
1491 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1493 for (inner_count = outer_count + 1; inner_count < pg->total;
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1498 "Connecting peer %d to peer %d\n",
1499 outer_count, inner_count);
1501 connect_attempts += proc(pg, outer_count, inner_count);
1505 return connect_attempts;
1509 * Create a topology given a peer group (set of running peers)
1510 * and a connection processor.
1512 * @param pg the peergroup to create the topology on
1513 * @param proc the connection processor to call to actually set
1514 * up connections between two peers
1516 * @return the number of connections that were set up
1520 create_line (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1523 int connect_attempts;
1525 connect_attempts = 0;
1527 /* Connect each peer to the next highest numbered peer */
1528 for (count = 0; count < pg->total - 1; count++)
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1532 "Connecting peer %d to peer %d\n",
1535 connect_attempts += proc(pg, count, count + 1);
1538 return connect_attempts;
1542 * Create a topology given a peer group (set of running peers)
1543 * and a connection processor.
1545 * @param pg the peergroup to create the topology on
1546 * @param proc the connection processor to call to actually set
1547 * up connections between two peers
1549 * @return the number of connections that were set up
1553 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1556 int connect_attempts;
1558 connect_attempts = 0;
1560 /* Connect each peer to the next highest numbered peer */
1561 for (count = 0; count < pg->total - 1; count++)
1564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1565 "Connecting peer %d to peer %d\n",
1568 connect_attempts += proc(pg, count, count + 1);
1571 /* Connect the last peer to the first peer */
1572 connect_attempts += proc(pg, pg->total - 1, 0);
1574 return connect_attempts;
1579 * Iterator for writing friends of a peer to a file.
1581 * @param cls closure, an open writable file handle
1582 * @param key the key the daemon was stored under
1583 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1585 * @return GNUNET_YES to continue iteration
1587 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1588 * with a single file_iterator that takes a closure which contains
1589 * the prefix to write before the peer. Then this could be used
1590 * for blacklisting multiple transports and writing the friend
1591 * file. I'm sure *someone* will complain loudly about other
1592 * things that negate these functions even existing so no point in
1596 friend_file_iterator (void *cls,
1597 const GNUNET_HashCode * key,
1600 FILE *temp_friend_handle = cls;
1601 struct GNUNET_TESTING_Daemon *peer = value;
1602 struct GNUNET_PeerIdentity *temppeer;
1603 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1605 temppeer = &peer->id;
1606 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1607 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1612 struct BlacklistContext
1615 * The (open) file handle to write to
1617 FILE *temp_file_handle;
1620 * The transport that this peer will be blacklisted on.
1626 * Iterator for writing blacklist data to appropriate files.
1628 * @param cls closure, an open writable file handle
1629 * @param key the key the daemon was stored under
1630 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1632 * @return GNUNET_YES to continue iteration
1635 blacklist_file_iterator (void *cls,
1636 const GNUNET_HashCode * key,
1639 struct BlacklistContext *blacklist_ctx = cls;
1640 //FILE *temp_blacklist_handle = cls;
1641 struct GNUNET_TESTING_Daemon *peer = value;
1642 struct GNUNET_PeerIdentity *temppeer;
1643 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1645 temppeer = &peer->id;
1646 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1647 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1653 * Create the friend files based on the PeerConnection's
1654 * of each peer in the peer group, and copy the files
1655 * to the appropriate place
1657 * @param pg the peer group we are dealing with
1660 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1662 FILE *temp_friend_handle;
1663 unsigned int pg_iter;
1664 char *temp_service_path;
1668 enum GNUNET_OS_ProcessStatusType type;
1669 unsigned long return_code;
1674 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1675 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1677 mytemp = GNUNET_DISK_mktemp("friends");
1678 GNUNET_assert(mytemp != NULL);
1679 temp_friend_handle = fopen (mytemp, "wt");
1680 GNUNET_assert(temp_friend_handle != NULL);
1681 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1682 fclose(temp_friend_handle);
1685 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1687 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1688 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1691 if (UNLINK (mytemp) != 0)
1692 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1693 GNUNET_free (mytemp);
1697 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1699 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1700 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1701 "mv", mytemp, arg, NULL);
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1704 _("Copying file with command cp %s %s\n"), mytemp, arg);
1709 else /* Remote, scp the file to the correct place */
1711 if (NULL != pg->peers[pg_iter].daemon->username)
1712 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1714 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1715 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1716 "scp", mytemp, arg, NULL);
1719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1720 _("Copying file with command scp %s %s\n"), mytemp, arg);
1724 GNUNET_free (temp_service_path);
1725 GNUNET_free (mytemp);
1729 ret = GNUNET_SYSERR;
1730 while ((count < max_wait) && (ret != GNUNET_OK))
1733 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1737 _("Checking copy status of file %d\n"), pg_iter);
1739 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1741 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1743 ret = GNUNET_SYSERR;
1745 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1747 ret = GNUNET_SYSERR;
1751 pidarr[pg_iter] = 0;
1753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1754 _("File %d copied\n"), pg_iter);
1760 if (ret == GNUNET_SYSERR)
1762 /* FIXME: why sleep here? -CG */
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1769 _("Finished copying all friend files!\n"));
1771 GNUNET_free(pidarr);
1777 * Create the blacklist files based on the PeerConnection's
1778 * of each peer in the peer group, and copy the files
1779 * to the appropriate place.
1781 * @param pg the peer group we are dealing with
1782 * @param transports space delimited list of transports to blacklist
1785 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
1787 FILE *temp_file_handle;
1788 static struct BlacklistContext blacklist_ctx;
1789 unsigned int pg_iter;
1790 char *temp_service_path;
1794 enum GNUNET_OS_ProcessStatusType type;
1795 unsigned long return_code;
1802 char *temp_transports;
1804 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1805 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1807 mytemp = GNUNET_DISK_mktemp("blacklist");
1808 GNUNET_assert(mytemp != NULL);
1809 temp_file_handle = fopen (mytemp, "wt");
1810 GNUNET_assert(temp_file_handle != NULL);
1811 temp_transports = GNUNET_strdup(transports);
1812 blacklist_ctx.temp_file_handle = temp_file_handle;
1813 transport_len = strlen(temp_transports) + 1;
1816 for (i = 0; i < transport_len; i++)
1818 if ((temp_transports[i] == ' ') && (pos == NULL))
1819 continue; /* At start of string (whitespace) */
1820 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
1822 temp_transports[i] = '\0';
1823 blacklist_ctx.transport = pos;
1824 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
1826 } /* At beginning of actual string */
1827 else if (pos == NULL)
1829 pos = &temp_transports[i];
1833 GNUNET_free (temp_transports);
1834 fclose(temp_file_handle);
1837 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1839 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1840 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1843 if (UNLINK (mytemp) != 0)
1844 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1845 GNUNET_free (mytemp);
1849 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1851 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
1852 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1853 "mv", mytemp, arg, NULL);
1855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1856 _("Copying file with command cp %s %s\n"), mytemp, arg);
1861 else /* Remote, scp the file to the correct place */
1863 if (NULL != pg->peers[pg_iter].daemon->username)
1864 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1866 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1867 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1868 "scp", mytemp, arg, NULL);
1871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1872 _("Copying file with command scp %s %s\n"), mytemp, arg);
1876 GNUNET_free (temp_service_path);
1877 GNUNET_free (mytemp);
1881 ret = GNUNET_SYSERR;
1882 while ((count < max_wait) && (ret != GNUNET_OK))
1885 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1889 _("Checking copy status of file %d\n"), pg_iter);
1891 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1893 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1895 ret = GNUNET_SYSERR;
1897 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1899 ret = GNUNET_SYSERR;
1903 pidarr[pg_iter] = 0;
1905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1906 _("File %d copied\n"), pg_iter);
1912 if (ret == GNUNET_SYSERR)
1914 /* FIXME: why sleep here? -CG */
1920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1921 _("Finished copying all blacklist files!\n"));
1923 GNUNET_free(pidarr);
1929 * Internal notification of a connection, kept so that we can ensure some connections
1930 * happen instead of flooding all testing daemons with requests to connect.
1932 static void internal_connect_notify (void *cls,
1933 const struct GNUNET_PeerIdentity *first,
1934 const struct GNUNET_PeerIdentity *second,
1936 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
1937 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
1938 struct GNUNET_TESTING_Daemon *first_daemon,
1939 struct GNUNET_TESTING_Daemon *second_daemon,
1942 struct GNUNET_TESTING_PeerGroup *pg = cls;
1943 outstanding_connects--;
1945 pg->notify_connection(pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
1949 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1951 struct ConnectContext *connect_context = cls;
1953 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1956 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
1958 #if VERBOSE_TESTING > 2
1959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1960 _("Delaying connect, we have too many outstanding connections!\n"));
1962 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_connect, connect_context);
1966 #if VERBOSE_TESTING > 2
1967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1968 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
1970 outstanding_connects++;
1971 GNUNET_TESTING_daemons_connect (connect_context->first,
1972 connect_context->second,
1975 &internal_connect_notify,
1976 connect_context->pg);
1977 GNUNET_free(connect_context);
1983 * Iterator for actually scheduling connections to be created
1984 * between two peers.
1986 * @param cls closure, a GNUNET_TESTING_Daemon
1987 * @param key the key the second Daemon was stored under
1988 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1990 * @return GNUNET_YES to continue iteration
1993 connect_iterator (void *cls,
1994 const GNUNET_HashCode * key,
1997 struct PeerData *first = cls;
1998 struct GNUNET_TESTING_Daemon *second = value;
1999 struct ConnectContext *connect_context;
2001 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2002 connect_context->pg = first->pg;
2003 connect_context->first = first->daemon;
2004 connect_context->second = second;
2005 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
2012 * Iterator for copying all entries in the allowed hashmap to the
2015 * @param cls closure, a GNUNET_TESTING_Daemon
2016 * @param key the key the second Daemon was stored under
2017 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2019 * @return GNUNET_YES to continue iteration
2022 copy_topology_iterator (void *cls,
2023 const GNUNET_HashCode * key,
2026 struct PeerData *first = cls;
2028 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2034 * Make the peers to connect the same as those that are allowed to be
2037 * @param pg the peer group
2040 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2042 unsigned int pg_iter;
2047 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2049 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
2050 if (GNUNET_SYSERR == ret)
2051 return GNUNET_SYSERR;
2053 total = total + ret;
2061 * Connect the topology as specified by the PeerConnection's
2062 * of each peer in the peer group
2064 * @param pg the peer group we are dealing with
2065 * @return the number of connections that will be attempted
2068 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
2070 unsigned int pg_iter;
2074 struct PeerConnection *connection_iter;
2075 struct ConnectContext *connect_context;
2079 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2081 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
2082 if (GNUNET_SYSERR == ret)
2083 return GNUNET_SYSERR;
2085 total = total + ret;
2089 while (connection_iter != NULL)
2091 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2092 connect_context->pg = pg;
2093 connect_context->first = ;
2094 connect_context->second = connection_iter->daemon;
2095 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
2096 connection_iter = connection_iter->next;
2105 * Takes a peer group and creates a topology based on the
2106 * one specified. Creates a topology means generates friend
2107 * files for the peers so they can only connect to those allowed
2108 * by the topology. This will only have an effect once peers
2109 * are started if the FRIENDS_ONLY option is set in the base
2110 * config. Also takes an optional restrict topology which
2111 * disallows connections based on a particular transport
2112 * UNLESS they are specified in the restricted topology.
2114 * @param pg the peer group struct representing the running peers
2115 * @param topology which topology to connect the peers in
2116 * @param restrict_topology allow only direct TCP connections in this topology
2117 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2118 * @param restrict_transports space delimited list of transports to blacklist
2119 * to create restricted topology
2121 * @return the maximum number of connections were all allowed peers
2122 * connected to each other
2125 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2126 enum GNUNET_TESTING_Topology topology,
2127 enum GNUNET_TESTING_Topology restrict_topology,
2128 char *restrict_transports)
2131 int num_connections;
2132 int unblacklisted_connections;
2134 GNUNET_assert (pg->notify_connection != NULL);
2139 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2142 _("Creating clique topology\n"));
2144 num_connections = create_clique (pg, &add_allowed_connections);
2146 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2149 _("Creating small world (ring) topology\n"));
2151 num_connections = create_small_world_ring (pg, &add_allowed_connections);
2153 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2156 _("Creating small world (2d-torus) topology\n"));
2158 num_connections = create_small_world (pg, &add_allowed_connections);
2160 case GNUNET_TESTING_TOPOLOGY_RING:
2162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2163 _("Creating ring topology\n"));
2165 num_connections = create_ring (pg, &add_allowed_connections);
2167 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2170 _("Creating 2d torus topology\n"));
2172 num_connections = create_2d_torus (pg, &add_allowed_connections);
2174 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2177 _("Creating Erdos-Renyi topology\n"));
2179 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2181 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2184 _("Creating InterNAT topology\n"));
2186 num_connections = create_nated_internet (pg, &add_allowed_connections);
2188 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2191 _("Creating Scale Free topology\n"));
2193 num_connections = create_scale_free (pg, &add_allowed_connections);
2195 case GNUNET_TESTING_TOPOLOGY_LINE:
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 _("Creating straight line topology\n"));
2200 num_connections = create_line (pg, &add_allowed_connections);
2202 case GNUNET_TESTING_TOPOLOGY_NONE:
2204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2205 _("Creating no allowed topology (all peers can connect at core level)\n"));
2207 num_connections = 0;
2210 num_connections = 0;
2214 if (num_connections < 0)
2215 return GNUNET_SYSERR;
2217 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2219 ret = create_and_copy_friend_files(pg);
2220 if (ret != GNUNET_OK)
2223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2224 _("Failed during friend file copying!\n"));
2226 return GNUNET_SYSERR;
2231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2232 _("Friend files created/copied successfully!\n"));
2237 /* Use the create clique method to initially set all connections as blacklisted. */
2238 if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2239 create_clique (pg, &blacklist_connections);
2240 unblacklisted_connections = 0;
2241 /* Un-blacklist connections as per the topology specified */
2242 switch (restrict_topology)
2244 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2247 _("Blacklisting all but clique topology\n"));
2249 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2251 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2254 _("Blacklisting all but small world (ring) topology\n"));
2256 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2258 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2261 _("Blacklisting all but small world (2d-torus) topology\n"));
2263 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2265 case GNUNET_TESTING_TOPOLOGY_RING:
2267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2268 _("Blacklisting all but ring topology\n"));
2270 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2272 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2275 _("Blacklisting all but 2d torus topology\n"));
2277 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2279 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2282 _("Blacklisting all but Erdos-Renyi topology\n"));
2284 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2286 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2289 _("Blacklisting all but InterNAT topology\n"));
2291 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2293 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2296 _("Blacklisting all but Scale Free topology\n"));
2298 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2300 case GNUNET_TESTING_TOPOLOGY_LINE:
2302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2303 _("Blacklisting all but straight line topology\n"));
2305 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2307 case GNUNET_TESTING_TOPOLOGY_NONE:
2309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2310 _("Creating no blacklist topology (all peers can connect at transport level)\n"));
2316 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2318 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2319 if (ret != GNUNET_OK)
2322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2323 _("Failed during blacklist file copying!\n"));
2325 return GNUNET_SYSERR;
2330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2331 _("Blacklist files created/copied successfully!\n"));
2335 return num_connections;
2338 struct RandomContext
2343 struct GNUNET_TESTING_PeerGroup *pg;
2346 * uid of the first peer
2351 * Peer data for first peer.
2353 struct PeerData *first;
2356 * Random percentage to use
2361 struct MinimumContext
2366 struct GNUNET_TESTING_PeerGroup *pg;
2369 * uid of the first peer
2374 * Peer data for first peer.
2376 struct PeerData *first;
2379 * Number of conns per peer
2381 unsigned int num_to_add;
2384 * Permuted array of all possible connections. Only add the Nth
2385 * peer if it's in the Nth position.
2387 unsigned int *pg_array;
2390 * What number is the current element we are iterating over?
2392 unsigned int current;
2400 struct GNUNET_TESTING_PeerGroup *pg;
2403 * uid of the first peer
2408 * uid of the second peer
2410 uint32_t second_uid;
2413 * Peer data for first peer.
2415 struct PeerData *first;
2418 * Which peer has been chosen as the one to add?
2420 unsigned int chosen;
2423 * What number is the current element we are iterating over?
2425 unsigned int current;
2429 * Iterator for choosing random peers to connect.
2431 * @param cls closure, a RandomContext
2432 * @param key the key the second Daemon was stored under
2433 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2435 * @return GNUNET_YES to continue iteration
2438 random_connect_iterator (void *cls,
2439 const GNUNET_HashCode * key,
2442 struct RandomContext *random_ctx = cls;
2443 double random_number;
2444 uint32_t second_pos;
2445 GNUNET_HashCode first_hash;
2446 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2447 UINT64_MAX)) / ( (double) UINT64_MAX);
2448 if (random_number < random_ctx->percentage)
2450 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2452 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2453 uid_from_hash(key, &second_pos);
2454 hash_from_uid(random_ctx->first_uid, &first_hash);
2455 GNUNET_assert(random_ctx->pg->total > second_pos);
2456 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2462 * Iterator for adding at least X peers to a peers connection set.
2464 * @param cls closure, MinimumContext
2465 * @param key the key the second Daemon was stored under
2466 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2468 * @return GNUNET_YES to continue iteration
2471 minimum_connect_iterator (void *cls,
2472 const GNUNET_HashCode * key,
2475 struct MinimumContext *min_ctx = cls;
2476 uint32_t second_pos;
2477 GNUNET_HashCode first_hash;
2480 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2482 for (i = 0; i < min_ctx->num_to_add; i++)
2484 if (min_ctx->pg_array[i] == min_ctx->current)
2486 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2487 uid_from_hash(key, &second_pos);
2488 hash_from_uid(min_ctx->first_uid, &first_hash);
2489 GNUNET_assert(min_ctx->pg->total > second_pos);
2490 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));
2491 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2492 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2499 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2505 * Iterator for adding peers to a connection set based on a depth first search.
2507 * @param cls closure, MinimumContext
2508 * @param key the key the second daemon was stored under
2509 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2511 * @return GNUNET_YES to continue iteration
2514 dfs_connect_iterator (void *cls,
2515 const GNUNET_HashCode * key,
2518 struct DFSContext *dfs_ctx = cls;
2519 GNUNET_HashCode first_hash;
2521 if (dfs_ctx->current == dfs_ctx->chosen)
2523 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2524 uid_from_hash(key, &dfs_ctx->second_uid);
2525 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2526 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));
2527 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2528 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2529 return GNUNET_NO; /* We have found our peer, don't iterate more */
2538 * From the set of connections possible, choose percentage percent of connections
2539 * to actually connect.
2541 * @param pg the peergroup we are dealing with
2542 * @param percentage what percent of total connections to make
2545 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2547 struct RandomContext random_ctx;
2550 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2552 random_ctx.first_uid = pg_iter;
2553 random_ctx.first = &pg->peers[pg_iter];
2554 random_ctx.percentage = percentage;
2556 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2557 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2558 /* Now remove the old connections */
2559 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2560 /* And replace with the random set */
2561 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2566 * From the set of connections possible, choose at least num connections per
2569 * @param pg the peergroup we are dealing with
2570 * @param num how many connections at least should each peer have (if possible)?
2573 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2575 struct MinimumContext minimum_ctx;
2578 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2580 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2583 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2585 minimum_ctx.first_uid = pg_iter;
2586 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2587 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2588 minimum_ctx.first = &pg->peers[pg_iter];
2589 minimum_ctx.pg = pg;
2590 minimum_ctx.num_to_add = num;
2591 minimum_ctx.current = 0;
2592 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2593 &minimum_connect_iterator,
2597 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2599 /* Remove the "old" connections */
2600 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2601 /* And replace with the working set */
2602 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2609 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2612 unsigned int pg_iter;
2616 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2618 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2625 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2628 unsigned int pg_iter;
2632 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2634 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2641 * From the set of connections possible, choose at least num connections per
2642 * peer based on depth first traversal of peer connections. If DFS leaves
2643 * peers unconnected, ensure those peers get connections.
2645 * @param pg the peergroup we are dealing with
2646 * @param num how many connections at least should each peer have (if possible)?
2649 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2651 struct DFSContext dfs_ctx;
2654 uint32_t starting_peer;
2655 uint32_t least_connections;
2656 GNUNET_HashCode second_hash;
2658 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2660 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2665 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
2667 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
2669 least_connections = -1; /* Set to very high number */
2670 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2672 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
2674 starting_peer = pg_iter;
2675 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2680 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
2686 /* Choose a random peer from the chosen peers set of connections to add */
2687 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
2688 dfs_ctx.first_uid = starting_peer;
2689 dfs_ctx.first = &pg->peers[starting_peer];
2691 dfs_ctx.current = 0;
2693 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
2694 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
2695 hash_from_uid(dfs_ctx.second_uid, &second_hash);
2696 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
2697 starting_peer = dfs_ctx.second_uid;
2700 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2702 /* Remove the "old" connections */
2703 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2704 /* And replace with the working set */
2705 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2710 * There are many ways to connect peers that are supported by this function.
2711 * To connect peers in the same topology that was created via the
2712 * GNUNET_TESTING_create_topology, the topology variable must be set to
2713 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
2714 * a new instance of that topology will be generated and attempted to be
2715 * connected. This could result in some connections being impossible,
2716 * because some topologies are non-deterministic.
2718 * @param pg the peer group struct representing the running peers
2719 * @param topology which topology to connect the peers in
2720 * @param options options for connecting the topology
2721 * @param option_modifier modifier for options that take a parameter
2722 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
2725 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2726 enum GNUNET_TESTING_Topology topology,
2727 enum GNUNET_TESTING_TopologyOption options,
2728 double option_modifier)
2732 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2733 #if VERBOSE_TOPOLOGY
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2735 _("Creating clique CONNECT topology\n"));
2737 create_clique (pg, &add_actual_connections);
2739 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2740 #if VERBOSE_TOPOLOGY
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2742 _("Creating small world (ring) CONNECT topology\n"));
2744 create_small_world_ring (pg, &add_actual_connections);
2746 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2747 #if VERBOSE_TOPOLOGY
2748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2749 _("Creating small world (2d-torus) CONNECT topology\n"));
2751 create_small_world (pg, &add_actual_connections);
2753 case GNUNET_TESTING_TOPOLOGY_RING:
2754 #if VERBOSE_TOPOLOGY
2755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2756 _("Creating ring CONNECT topology\n"));
2758 create_ring (pg, &add_actual_connections);
2760 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2761 #if VERBOSE_TOPOLOGY
2762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2763 _("Creating 2d torus CONNECT topology\n"));
2765 create_2d_torus (pg, &add_actual_connections);
2767 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2768 #if VERBOSE_TOPOLOGY
2769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2770 _("Creating Erdos-Renyi CONNECT topology\n"));
2772 create_erdos_renyi (pg, &add_actual_connections);
2774 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2775 #if VERBOSE_TOPOLOGY
2776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2777 _("Creating InterNAT CONNECT topology\n"));
2779 create_nated_internet (pg, &add_actual_connections);
2781 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2782 #if VERBOSE_TOPOLOGY
2783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2784 _("Creating Scale Free CONNECT topology\n"));
2786 create_scale_free (pg, &add_actual_connections);
2788 case GNUNET_TESTING_TOPOLOGY_LINE:
2789 #if VERBOSE_TOPOLOGY
2790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2791 _("Creating straight line CONNECT topology\n"));
2793 create_line (pg, &add_actual_connections);
2795 case GNUNET_TESTING_TOPOLOGY_NONE:
2796 #if VERBOSE_TOPOLOGY
2797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2798 _("Creating no CONNECT topology\n"));
2800 copy_allowed_topology(pg);
2803 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2804 _("Unknown topology specification, can't connect peers!\n"));
2805 return GNUNET_SYSERR;
2810 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
2811 #if VERBOSE_TOPOLOGY
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2813 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
2815 choose_random_connections(pg, option_modifier);
2817 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
2818 #if VERBOSE_TOPOLOGY
2819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2820 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
2822 choose_minimum(pg, (unsigned int)option_modifier);
2824 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
2825 #if VERBOSE_TOPOLOGY
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2827 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
2829 perform_dfs(pg, (int)option_modifier);
2831 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
2833 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
2839 return connect_topology(pg);
2843 * Callback that is called whenever a hostkey is generated
2844 * for a peer. Call the real callback and decrement the
2845 * starting counter for the peergroup.
2847 * @param cls closure
2848 * @param id identifier for the daemon, NULL on error
2849 * @param d handle for the daemon
2850 * @param emsg error message (NULL on success)
2852 static void internal_hostkey_callback (void *cls,
2853 const struct GNUNET_PeerIdentity *id,
2854 struct GNUNET_TESTING_Daemon *d,
2857 struct InternalStartContext *internal_context = cls;
2858 internal_context->peer->pg->starting--;
2859 internal_context->peer->pg->started++;
2860 if (internal_context->hostkey_callback != NULL)
2861 internal_context->hostkey_callback(internal_context->hostkey_cls, id, d, emsg);
2862 else if (internal_context->peer->pg->started == internal_context->peer->pg->total)
2864 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
2865 GNUNET_TESTING_daemons_continue_startup(internal_context->peer->pg);
2870 * Callback that is called whenever a peer has finished starting.
2871 * Call the real callback and decrement the starting counter
2872 * for the peergroup.
2874 * @param cls closure
2875 * @param id identifier for the daemon, NULL on error
2876 * @param d handle for the daemon
2877 * @param emsg error message (NULL on success)
2879 static void internal_startup_callback (void *cls,
2880 const struct GNUNET_PeerIdentity *id,
2881 const struct GNUNET_CONFIGURATION_Handle *cfg,
2882 struct GNUNET_TESTING_Daemon *d,
2885 struct InternalStartContext *internal_context = cls;
2886 internal_context->peer->pg->starting--;
2887 if (internal_context->start_cb != NULL)
2888 internal_context->start_cb(internal_context->start_cb_cls, id, cfg, d, emsg);
2892 internal_continue_startup (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2894 struct InternalStartContext *internal_context = cls;
2896 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2901 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
2903 internal_context->peer->pg->starting++;
2904 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
2908 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_continue_startup, internal_context);
2913 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2915 struct InternalStartContext *internal_context = cls;
2917 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2922 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
2924 internal_context->peer->pg->starting++;
2925 internal_context->peer->daemon = GNUNET_TESTING_daemon_start (internal_context->peer->pg->sched,
2926 internal_context->peer->cfg,
2927 internal_context->timeout,
2928 internal_context->hostname,
2929 &internal_hostkey_callback,
2931 &internal_startup_callback,
2936 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_start, internal_context);
2941 * Function which continues a peer group starting up
2942 * after successfully generating hostkeys for each peer.
2944 * @param pg the peer group to continue starting
2948 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
2953 for (i = 0; i < pg->total; i++)
2955 GNUNET_SCHEDULER_add_now (pg->sched, &internal_continue_startup, &pg->peers[i].internal_context);
2956 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
2961 * Start count gnunet instances with the same set of transports and
2962 * applications. The port numbers (any option called "PORT") will be
2963 * adjusted to ensure that no two peers running on the same system
2964 * have the same port(s) in their respective configurations.
2966 * @param sched scheduler to use
2967 * @param cfg configuration template to use
2968 * @param total number of daemons to start
2969 * @param timeout total time allowed for peers to start
2970 * @param hostkey_callback function to call on each peers hostkey generation
2971 * if NULL, peers will be started by this call, if non-null,
2972 * GNUNET_TESTING_daemons_continue_startup must be called after
2973 * successful hostkey generation
2974 * @param hostkey_cls closure for hostkey callback
2975 * @param cb function to call on each daemon that was started
2976 * @param cb_cls closure for cb
2977 * @param connect_callback function to call each time two hosts are connected
2978 * @param connect_callback_cls closure for connect_callback
2979 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
2981 * @return NULL on error, otherwise handle to control peer group
2983 struct GNUNET_TESTING_PeerGroup *
2984 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
2985 const struct GNUNET_CONFIGURATION_Handle *cfg,
2987 struct GNUNET_TIME_Relative timeout,
2988 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
2990 GNUNET_TESTING_NotifyDaemonRunning cb,
2992 GNUNET_TESTING_NotifyConnection
2993 connect_callback, void *connect_callback_cls,
2994 const struct GNUNET_TESTING_Host *hostnames)
2996 struct GNUNET_TESTING_PeerGroup *pg;
2997 const struct GNUNET_TESTING_Host *hostpos;
3003 const char *hostname;
3004 char *baseservicehome;
3005 char *newservicehome;
3007 struct GNUNET_CONFIGURATION_Handle *pcfg;
3009 unsigned int hostcnt;
3019 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
3022 pg->notify_connection = connect_callback;
3023 pg->notify_connection_cls = connect_callback_cls;
3025 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
3026 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
3027 if (NULL != hostnames)
3030 hostpos = hostnames;
3031 while (hostpos != NULL)
3033 hostpos = hostpos->next;
3036 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3039 hostpos = hostnames;
3040 while (hostpos != NULL)
3042 pg->hosts[off].minport = LOW_PORT;
3043 pg->hosts[off++].hostname = GNUNET_strdup(hostpos->hostname);
3044 hostpos = hostpos->next;
3049 GNUNET_free (pg->hosts);
3057 /* skip leading spaces */
3058 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
3061 while ('\0' != *rpos)
3063 if (isspace ( (unsigned char) *rpos))
3067 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3069 start = GNUNET_strdup (hostnames);
3071 while ('\0' != *pos)
3073 if (isspace ( (unsigned char) *pos))
3076 if (strlen (start) > 0)
3078 pg->hosts[off].minport = LOW_PORT;
3079 pg->hosts[off++].hostname = start;
3085 if (strlen (start) > 0)
3087 pg->hosts[off].minport = LOW_PORT;
3088 pg->hosts[off++].hostname = start;
3092 GNUNET_free (start);
3093 GNUNET_free (pg->hosts);
3097 minport = 0; /* make gcc happy */
3105 for (off = 0; off < total; off++)
3109 hostname = pg->hosts[off % hostcnt].hostname;
3110 pcfg = make_config (cfg,
3111 &pg->hosts[off % hostcnt].minport,
3118 pcfg = make_config (cfg,
3126 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3128 ("Could not create configuration for peer number %u on `%s'!\n"),
3129 off, hostname == NULL ? "localhost" : hostname);
3134 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
3137 GNUNET_asprintf (&newservicehome,
3138 "%s/%d/", baseservicehome, off);
3139 GNUNET_free (baseservicehome);
3143 tmpdir = getenv ("TMPDIR");
3144 tmpdir = tmpdir ? tmpdir : "/tmp";
3145 GNUNET_asprintf (&newservicehome,
3148 "gnunet-testing-test-test", off);
3150 GNUNET_CONFIGURATION_set_value_string (pcfg,
3152 "SERVICEHOME", newservicehome);
3153 GNUNET_free (newservicehome);
3154 pg->peers[off].cfg = pcfg;
3155 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
3156 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
3157 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
3158 pg->peers[off].pg = pg;
3160 pg->peers[off].internal_context.peer = &pg->peers[off];
3161 pg->peers[off].internal_context.timeout = timeout;
3162 pg->peers[off].internal_context.hostname = hostname;
3163 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
3164 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
3165 pg->peers[off].internal_context.start_cb = cb;
3166 pg->peers[off].internal_context.start_cb_cls = cb_cls;
3168 GNUNET_SCHEDULER_add_now (sched, &internal_start, &pg->peers[off].internal_context);
3175 * Get a daemon by number, so callers don't have to do nasty
3176 * offsetting operation.
3178 struct GNUNET_TESTING_Daemon *
3179 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
3181 if (position < pg->total)
3182 return pg->peers[position].daemon;
3188 * Prototype of a function that will be called when a
3189 * particular operation was completed the testing library.
3191 * @param cls closure (a struct RestartContext)
3192 * @param id id of the peer that was restarted
3193 * @param cfg handle to the configuration of the peer
3194 * @param d handle to the daemon that was restarted
3195 * @param emsg NULL on success
3197 void restart_callback (void *cls,
3198 const struct GNUNET_PeerIdentity *id,
3199 const struct GNUNET_CONFIGURATION_Handle *cfg,
3200 struct GNUNET_TESTING_Daemon *d,
3203 struct RestartContext *restart_context = cls;
3207 restart_context->peers_restarted++;
3211 restart_context->peers_restart_failed++;
3214 if (restart_context->peers_restarted == restart_context->peer_group->total)
3216 restart_context->callback(restart_context->callback_cls, NULL);
3217 GNUNET_free(restart_context);
3219 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
3221 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
3222 GNUNET_free(restart_context);
3228 * Callback for informing us about a successful
3229 * or unsuccessful churn stop call.
3231 * @param cls a ChurnContext
3232 * @param emsg NULL on success, non-NULL on failure
3236 churn_stop_callback (void *cls, const char *emsg)
3238 struct ChurnContext *churn_ctx = cls;
3239 unsigned int total_left;
3240 char *error_message;
3242 error_message = NULL;
3245 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3246 "Churn stop callback failed with error `%s'\n", emsg);
3247 churn_ctx->num_failed_stop++;
3251 churn_ctx->num_to_stop--;
3255 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3256 "Stopped peer, %d left.\n",
3257 churn_ctx->num_to_stop);
3259 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3261 if (total_left == 0)
3263 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3265 GNUNET_asprintf(&error_message,
3266 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3267 churn_ctx->num_failed_start,
3268 churn_ctx->num_failed_stop);
3270 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3271 GNUNET_free_non_null(error_message);
3272 GNUNET_free(churn_ctx);
3277 * Callback for informing us about a successful
3278 * or unsuccessful churn start call.
3280 * @param cls a ChurnContext
3281 * @param id the peer identity of the started peer
3282 * @param cfg the handle to the configuration of the peer
3283 * @param d handle to the daemon for the peer
3284 * @param emsg NULL on success, non-NULL on failure
3288 churn_start_callback (void *cls,
3289 const struct GNUNET_PeerIdentity *id,
3290 const struct GNUNET_CONFIGURATION_Handle *cfg,
3291 struct GNUNET_TESTING_Daemon *d,
3294 struct ChurnContext *churn_ctx = cls;
3295 unsigned int total_left;
3296 char *error_message;
3298 error_message = NULL;
3301 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3302 "Churn stop callback failed with error `%s'\n",
3304 churn_ctx->num_failed_start++;
3308 churn_ctx->num_to_start--;
3312 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3313 "Started peer, %d left.\n",
3314 churn_ctx->num_to_start);
3317 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3319 if (total_left == 0)
3321 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3322 GNUNET_asprintf(&error_message,
3323 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3324 churn_ctx->num_failed_start,
3325 churn_ctx->num_failed_stop);
3326 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3327 GNUNET_free_non_null(error_message);
3328 GNUNET_free(churn_ctx);
3334 * Simulate churn by stopping some peers (and possibly
3335 * re-starting others if churn is called multiple times). This
3336 * function can only be used to create leave-join churn (peers "never"
3337 * leave for good). First "voff" random peers that are currently
3338 * online will be taken offline; then "von" random peers that are then
3339 * offline will be put back online. No notifications will be
3340 * generated for any of these operations except for the callback upon
3343 * @param pg handle for the peer group
3344 * @param voff number of peers that should go offline
3345 * @param von number of peers that should come back online;
3346 * must be zero on first call (since "testbed_start"
3347 * always starts all of the peers)
3348 * @param timeout how long to wait for operations to finish before
3350 * @param cb function to call at the end
3351 * @param cb_cls closure for cb
3354 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
3357 struct GNUNET_TIME_Relative timeout,
3358 GNUNET_TESTING_NotifyCompletion cb,
3361 struct ChurnContext *churn_ctx;
3362 unsigned int running;
3363 unsigned int stopped;
3364 unsigned int total_running;
3365 unsigned int total_stopped;
3367 unsigned int *running_arr;
3368 unsigned int *stopped_arr;
3369 unsigned int *running_permute;
3370 unsigned int *stopped_permute;
3375 if ((von == 0) && (voff == 0)) /* No peers at all? */
3381 for (i = 0; i < pg->total; i++)
3383 if (pg->peers[i].daemon->running == GNUNET_YES)
3385 GNUNET_assert(running != -1);
3390 GNUNET_assert(stopped != -1);
3397 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
3398 cb(cb_cls, "Trying to stop more peers than are currently running!");
3404 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
3405 cb(cb_cls, "Trying to start more peers than are currently stopped!");
3409 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3413 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
3417 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
3419 running_permute = NULL;
3420 stopped_permute = NULL;
3423 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
3425 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
3427 total_running = running;
3428 total_stopped = stopped;
3432 churn_ctx->num_to_start = von;
3433 churn_ctx->num_to_stop = voff;
3435 churn_ctx->cb_cls = cb_cls;
3437 for (i = 0; i < pg->total; i++)
3439 if (pg->peers[i].daemon->running == GNUNET_YES)
3441 GNUNET_assert((running_arr != NULL) && (total_running > running));
3442 running_arr[running] = i;
3447 GNUNET_assert((stopped_arr != NULL) && (total_stopped > stopped));
3448 stopped_arr[stopped] = i;
3453 for (i = 0; i < voff; i++)
3456 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
3458 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
3460 &churn_stop_callback, churn_ctx,
3461 GNUNET_NO, GNUNET_YES);
3464 for (i = 0; i < von; i++)
3467 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
3469 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
3470 timeout, &churn_start_callback, churn_ctx);
3473 GNUNET_free_non_null(running_arr);
3474 GNUNET_free_non_null(stopped_arr);
3475 GNUNET_free_non_null(running_permute);
3476 GNUNET_free_non_null(stopped_permute);
3481 * Restart all peers in the given group.
3483 * @param pg the handle to the peer group
3484 * @param callback function to call on completion (or failure)
3485 * @param callback_cls closure for the callback function
3488 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyCompletion callback, void *callback_cls)
3490 struct RestartContext *restart_context;
3495 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
3496 restart_context->peer_group = pg;
3497 restart_context->peers_restarted = 0;
3498 restart_context->callback = callback;
3499 restart_context->callback_cls = callback_cls;
3501 for (off = 0; off < pg->total; off++)
3503 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
3509 * Start or stop an individual peer from the given group.
3511 * @param pg handle to the peer group
3512 * @param offset which peer to start or stop
3513 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
3514 * @param timeout how long to wait for shutdown
3515 * @param cb function to call at the end
3516 * @param cb_cls closure for cb
3519 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
3520 unsigned int offset,
3522 struct GNUNET_TIME_Relative timeout,
3523 GNUNET_TESTING_NotifyCompletion cb,
3526 struct ChurnContext *churn_ctx;
3528 if (GNUNET_NO == desired_status)
3530 if (NULL != pg->peers[offset].daemon)
3532 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3533 churn_ctx->num_to_start = 0;
3534 churn_ctx->num_to_stop = 1;
3536 churn_ctx->cb_cls = cb_cls;
3537 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
3538 timeout, &churn_stop_callback, churn_ctx,
3539 GNUNET_NO, GNUNET_YES);
3542 else if (GNUNET_YES == desired_status)
3544 if (NULL == pg->peers[offset].daemon)
3546 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3547 churn_ctx->num_to_start = 1;
3548 churn_ctx->num_to_stop = 0;
3550 churn_ctx->cb_cls = cb_cls;
3551 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
3552 timeout, &churn_start_callback, churn_ctx);
3561 * Callback for shutting down peers in a peer group.
3563 * @param cls closure (struct ShutdownContext)
3564 * @param emsg NULL on success
3566 void internal_shutdown_callback (void *cls,
3569 struct ShutdownContext *shutdown_ctx = cls;
3573 shutdown_ctx->peers_down++;
3577 shutdown_ctx->peers_failed++;
3580 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
3582 if (shutdown_ctx->peers_failed > 0)
3583 shutdown_ctx->cb(shutdown_ctx->cb_cls, "Not all peers successfully shut down!");
3585 shutdown_ctx->cb(shutdown_ctx->cb_cls, NULL);
3586 GNUNET_free(shutdown_ctx);
3591 * Shutdown all peers started in the given group.
3593 * @param pg handle to the peer group
3594 * @param timeout how long to wait for shutdown
3595 * @param cb callback to notify upon success or failure
3596 * @param cb_cls closure for cb
3599 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
3600 struct GNUNET_TIME_Relative timeout,
3601 GNUNET_TESTING_NotifyCompletion cb,
3605 struct ShutdownContext *shutdown_ctx;
3606 GNUNET_TESTING_NotifyCompletion shutdown_cb;
3607 void *shutdown_cb_cls;
3609 GNUNET_assert(pg->total > 0);
3612 shutdown_ctx = NULL;
3616 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
3617 shutdown_ctx->cb = cb;
3618 shutdown_ctx->cb_cls = cb_cls;
3619 shutdown_ctx->total_peers = pg->total;
3620 shutdown_cb = &internal_shutdown_callback;
3621 shutdown_cb_cls = cb_cls;
3624 for (off = 0; off < pg->total; off++)
3626 GNUNET_assert(NULL != pg->peers[off].daemon);
3627 GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
3628 if (NULL != pg->peers[off].cfg)
3629 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
3630 if (pg->peers[off].allowed_peers != NULL)
3631 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
3632 if (pg->peers[off].connect_peers != NULL)
3633 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
3634 if (pg->peers[off].blacklisted_peers != NULL)
3635 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
3637 GNUNET_free (pg->peers);
3638 if (NULL != pg->hosts)
3640 GNUNET_free (pg->hosts[0].hostname);
3641 GNUNET_free (pg->hosts);
3647 /* end of testing_group.c */