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 2, 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 CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
57 #define CONNECT_ATTEMPTS 8
60 * Prototype of a function called whenever two peers would be connected
61 * in a certain topology.
63 typedef int (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup *pg,
69 * Context for handling churning a peer group
74 * Callback used to notify of churning finished
76 GNUNET_TESTING_NotifyCompletion cb;
79 * Closure for callback
84 * Number of peers that still need to be started
86 unsigned int num_to_start;
89 * Number of peers that still need to be stopped
91 unsigned int num_to_stop;
94 * Number of peers that failed to start
96 unsigned int num_failed_start;
99 * Number of peers that failed to stop
101 unsigned int num_failed_stop;
104 struct RestartContext
107 * The group of peers being restarted
109 struct GNUNET_TESTING_PeerGroup *peer_group;
112 * How many peers have been restarted thus far
114 unsigned int peers_restarted;
117 * How many peers got an error when restarting
119 unsigned int peers_restart_failed;
122 * The function to call once all peers have been restarted
124 GNUNET_TESTING_NotifyCompletion callback;
127 * Closure for callback function
134 struct ShutdownContext
137 * Total peers to wait for
142 * Number of peers successfully shut down
147 * Number of peers failed to shut down
152 * Callback to call when all peers either
153 * shutdown or failed to shutdown
155 GNUNET_TESTING_NotifyCompletion cb;
163 struct CreateTopologyContext
167 * Function to call with number of connections
169 GNUNET_TESTING_NotifyConnections cont;
172 * Closure for connection notification
178 struct PeerConnection
183 struct PeerConnection *next;
186 * Pointer to daemon handle
188 struct GNUNET_TESTING_Daemon *daemon;
194 * Data we keep per peer.
199 * (Initial) configuration of the host.
200 * (initial because clients could change
201 * it and we would not know about those
204 struct GNUNET_CONFIGURATION_Handle *cfg;
207 * Handle for controlling the daemon.
209 struct GNUNET_TESTING_Daemon *daemon;
212 * The peergroup this peer belongs to.
214 struct GNUNET_TESTING_PeerGroup *pg;
217 * Linked list of peer connections (pointers)
219 //struct PeerConnection *connected_peers;
221 * Hash map of allowed peer connections (F2F created topology)
223 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
226 * Hash map of blacklisted peers
228 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
231 * Hash map of peer connections
233 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
236 * Temporary hash map of peer connections
238 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
241 * Temporary variable for topology creation, should be reset before
242 * creating any topology so the count is valid once finished.
249 * Data we keep per host.
259 * Lowest port that we have not yet used
267 * Handle to a group of GNUnet peers.
269 struct GNUNET_TESTING_PeerGroup
274 struct GNUNET_SCHEDULER_Handle *sched;
277 * Configuration template.
279 const struct GNUNET_CONFIGURATION_Handle *cfg;
282 * Function to call on each started daemon.
284 GNUNET_TESTING_NotifyDaemonRunning cb;
292 * Function to call on each topology connection created
294 GNUNET_TESTING_NotifyConnection notify_connection;
297 * Callback for notify_connection
299 void *notify_connection_cls;
302 * NULL-terminated array of information about
305 struct HostData *hosts;
308 * Array of "total" peers.
310 struct PeerData *peers;
313 * Number of peers in this group.
318 * At what time should we fail the peer startup process?
320 struct GNUNET_TIME_Absolute max_timeout;
323 * How many peers are being started right now?
325 unsigned int starting;
329 * Convert unique ID to hash code.
331 * @param uid unique ID to convert
332 * @param hash set to uid (extended with zeros)
335 hash_from_uid (uint32_t uid,
336 GNUNET_HashCode *hash)
338 memset (hash, 0, sizeof(GNUNET_HashCode));
339 *((uint32_t*)hash) = uid;
343 * Convert hash code to unique ID.
345 * @param uid unique ID to convert
346 * @param hash set to uid (extended with zeros)
349 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
351 memcpy (uid, hash, sizeof(uint32_t));
356 struct GNUNET_CONFIGURATION_Handle *ret;
357 const char *hostname;
363 struct ConnectContext
365 struct GNUNET_TESTING_Daemon *first;
367 struct GNUNET_TESTING_Daemon *second;
369 struct GNUNET_TESTING_PeerGroup *pg;
373 * Number of connects we are waiting on, allows us to rate limit
376 static int outstanding_connects;
379 * Get a topology from a string input.
381 * @param topology where to write the retrieved topology
382 * @param topology_string The string to attempt to
383 * get a configuration value from
384 * @return GNUNET_YES if topology string matched a
385 * known topology, GNUNET_NO if not
388 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string)
391 * Strings representing topologies in enum
393 static const char * topology_strings[] =
396 * A clique (everyone connected to everyone else).
401 * Small-world network (2d torus plus random links).
406 * Small-world network (ring plus random links).
426 * Certain percentage of peers are unable to communicate directly
427 * replicating NAT conditions
432 * Scale free topology.
437 * Straight line topology.
442 * All peers are disconnected.
450 if (topology_string == NULL)
452 while (topology_strings[curr] != NULL)
454 if (strcmp(topology_strings[curr], topology_string) == 0)
461 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
467 * Get connect topology option from string input.
469 * @param topology_option where to write the retrieved topology
470 * @param topology_string The string to attempt to
471 * get a configuration value from
472 * @return GNUNET_YES if string matched a known
473 * topology option, GNUNET_NO if not
476 GNUNET_TESTING_topology_option_get(enum GNUNET_TESTING_TopologyOption *topology_option,
477 char * topology_string)
480 * Options for connecting a topology as strings.
482 static const char * topology_option_strings[] =
485 * Try to connect all peers specified in the topology.
490 * Choose a random subset of connections to create.
492 "CONNECT_RANDOM_SUBSET",
495 * Create at least X connections for each peer.
500 * Using a depth first search, create one connection
501 * per peer. If any are missed (graph disconnected)
502 * start over at those peers until all have at least one
508 * No options specified.
516 if (topology_string == NULL)
518 while (NULL != topology_option_strings[curr])
520 if (strcmp(topology_option_strings[curr], topology_string) == 0)
522 *topology_option = curr;
527 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
532 * Function to iterate over options. Copies
533 * the options to the target configuration,
534 * updating PORT values as needed.
537 * @param section name of the section
538 * @param option name of the option
539 * @param value value of the option
542 update_config (void *cls,
543 const char *section, const char *option, const char *value)
545 struct UpdateContext *ctx = cls;
550 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
554 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
559 if (0 == strcmp (option, "UNIXPATH"))
561 GNUNET_snprintf (uval,
563 "/tmp/test-service-%s-%u",
569 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
571 value = ctx->hostname;
574 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
579 * Create a new configuration using the given configuration
580 * as a template; however, each PORT in the existing cfg
581 * must be renumbered by incrementing "*port". If we run
582 * out of "*port" numbers, return NULL.
584 * @param cfg template configuration
585 * @param port port numbers to use, update to reflect
586 * port numbers that were used
587 * @param upnum number to make unix domain socket names unique
588 * @param hostname hostname of the controlling host, to allow control connections from
590 * @return new configuration, NULL on error
592 static struct GNUNET_CONFIGURATION_Handle *
593 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
596 const char *hostname)
598 struct UpdateContext uc;
606 uc.ret = GNUNET_CONFIGURATION_create ();
607 uc.hostname = hostname;
609 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
610 if (uc.nport >= HIGH_PORT)
613 GNUNET_CONFIGURATION_destroy (uc.ret);
617 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
619 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
620 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
621 GNUNET_free_non_null(control_host);
622 GNUNET_free(allowed_hosts);
626 /* arm needs to know to allow connections from the host on which it is running,
627 * otherwise gnunet-arm is unable to connect to it in some instances */
628 if (hostname != NULL)
630 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
631 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
632 GNUNET_free(allowed_hosts);
635 *port = (uint16_t) uc.nport;
642 * Add entries to the peers connect list
644 * @param pg the peer group we are working with
645 * @param first index of the first peer
646 * @param second index of the second peer
648 * @return the number of connections added
649 * technically should only be 0 or 2
653 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
659 GNUNET_HashCode hash_first;
660 GNUNET_HashCode hash_second;
662 hash_from_uid(first, &hash_first);
663 hash_from_uid(second, &hash_second);
665 add_first = GNUNET_NO;
666 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
668 add_first = GNUNET_YES;
671 add_second = GNUNET_NO;
672 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
674 add_second = GNUNET_YES;
680 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
681 pg->peers[first].num_connections++;
687 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
688 pg->peers[second].num_connections++;
697 * Add entries to the peers allowed connections list
699 * @param pg the peer group we are working with
700 * @param first index of the first peer
701 * @param second index of the second peer
703 * @return the number of connections added (can be 0, 1 or 2)
704 * technically should only be 0 or 2, but the small price
705 * of iterating over the lists (hashmaps in the future)
706 * for being sure doesn't bother me!
710 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
714 struct PeerConnection *first_iter;
715 struct PeerConnection *second_iter;
716 struct PeerConnection *new_first;
717 struct PeerConnection *new_second;
722 GNUNET_HashCode hash_first;
723 GNUNET_HashCode hash_second;
725 hash_from_uid(first, &hash_first);
726 hash_from_uid(second, &hash_second);
728 add_first = GNUNET_NO;
729 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
731 add_first = GNUNET_YES;
734 add_second = GNUNET_NO;
735 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
737 add_second = GNUNET_YES;
740 first_iter = pg->peers[first].connected_peers;
741 while (first_iter != NULL)
743 if (first_iter->daemon == pg->peers[second].daemon)
744 add_first = GNUNET_NO;
745 first_iter = first_iter->next;
748 second_iter = pg->peers[second].connected_peers;
749 add_second = GNUNET_YES;
750 while (second_iter != NULL)
752 if (second_iter->daemon == pg->peers[first].daemon)
753 add_second = GNUNET_NO;
754 second_iter = second_iter->next;
761 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
763 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
764 new_first->daemon = pg->peers[second].daemon;
765 new_first->next = pg->peers[first].connected_peers;
766 pg->peers[first].connected_peers = new_first;
768 pg->peers[first].num_connections++;
774 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
776 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
777 new_second->daemon = pg->peers[first].daemon;
778 new_second->next = pg->peers[second].connected_peers;
779 pg->peers[second].connected_peers = new_second;
780 pg->peers[first].num_connections++;
782 pg->peers[second].num_connections++;
790 * Add entries to the peers blacklisted list
792 * @param pg the peer group we are working with
793 * @param first index of the first peer
794 * @param second index of the second peer
796 * @return the number of connections added (can be 0, 1 or 2)
800 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
805 GNUNET_HashCode hash_first;
806 GNUNET_HashCode hash_second;
808 hash_from_uid(first, &hash_first);
809 hash_from_uid(second, &hash_second);
811 add_first = GNUNET_NO;
812 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
814 add_first = GNUNET_YES;
817 add_second = GNUNET_NO;
818 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
820 add_second = GNUNET_YES;
826 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
827 pg->peers[first].num_connections++;
833 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
834 pg->peers[second].num_connections++;
842 * Remove entries from the peers blacklisted list
844 * @param pg the peer group we are working with
845 * @param first index of the first peer
846 * @param second index of the second peer
848 * @return the number of connections removed (can be 0, 1 or 2)
852 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
857 GNUNET_HashCode hash_first;
858 GNUNET_HashCode hash_second;
860 hash_from_uid(first, &hash_first);
861 hash_from_uid(second, &hash_second);
863 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
864 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
869 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
875 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
883 * Scale free network construction as described in:
885 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
887 * Start with a network of "one" peer, then progressively add
888 * peers up to the total number. At each step, iterate over
889 * all possible peers and connect new peer based on number of
890 * existing connections of the target peer.
892 * @param pg the peer group we are dealing with
893 * @param proc the connection processor to use
895 * @return the number of connections created
898 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
901 unsigned int total_connections;
902 unsigned int outer_count;
904 unsigned int previous_total_connections;
908 GNUNET_assert(pg->total > 1);
910 /* Add a connection between the first two nodes */
911 total_connections = proc(pg, 0, 1);
913 for (outer_count = 1; outer_count < pg->total; outer_count++)
915 previous_total_connections = total_connections;
916 for (i = 0; i < outer_count; i++)
918 probability = pg->peers[i].num_connections / (double)previous_total_connections;
919 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
920 UINT64_MAX)) / ( (double) UINT64_MAX);
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
923 "Considering connecting peer %d to peer %d\n",
926 if (random < probability)
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
930 "Connecting peer %d to peer %d\n",
933 total_connections += proc(pg, outer_count, i);
938 return total_connections;
942 * Create a topology given a peer group (set of running peers)
943 * and a connection processor.
945 * @param pg the peergroup to create the topology on
946 * @param proc the connection processor to call to actually set
947 * up connections between two peers
949 * @return the number of connections that were set up
953 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
958 unsigned int randomPeer;
959 double random, logNModifier, percentage;
960 unsigned int smallWorldConnections;
966 int connect_attempts;
968 logNModifier = 0.5; /* FIXME: default value? */
969 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
974 if (sscanf(p_string, "%lf", &logNModifier) != 1)
975 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
976 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
980 GNUNET_free (p_string);
982 percentage = 0.5; /* FIXME: default percentage? */
983 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
988 if (sscanf(p_string, "%lf", &percentage) != 1)
989 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
990 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
994 GNUNET_free (p_string);
996 natLog = log (pg->total);
997 connsPerPeer = ceil (natLog * logNModifier);
999 if (connsPerPeer % 2 == 1)
1002 smallWorldConnections = 0;
1003 connect_attempts = 0;
1004 for (i = 0; i < pg->total; i++)
1007 max = i + connsPerPeer / 2;
1008 min = i - connsPerPeer / 2;
1010 if (max > pg->total - 1)
1012 max = max - pg->total;
1018 min = pg->total - 1 + min;
1022 for (j = 0; j < connsPerPeer / 2; j++)
1024 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1025 UINT64_MAX) / ( (double) UINT64_MAX));
1026 if (random < percentage)
1028 /* Connect to uniformly selected random peer */
1030 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1032 while ((((randomPeer < max) && (randomPeer > min))
1033 && (useAnd == 0)) || (((randomPeer > min)
1034 || (randomPeer < max))
1038 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1041 smallWorldConnections +=
1042 proc (pg, i, randomPeer);
1046 nodeToConnect = i + j + 1;
1047 if (nodeToConnect > pg->total - 1)
1049 nodeToConnect = nodeToConnect - pg->total;
1052 proc (pg, i, nodeToConnect);
1058 connect_attempts += smallWorldConnections;
1060 return connect_attempts;
1064 * Create a topology given a peer group (set of running peers)
1065 * and a connection processor.
1067 * @param pg the peergroup to create the topology on
1068 * @param proc the connection processor to call to actually set
1069 * up connections between two peers
1071 * @return the number of connections that were set up
1075 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1077 unsigned int outer_count, inner_count;
1078 unsigned int cutoff;
1079 int connect_attempts;
1080 double nat_percentage;
1083 nat_percentage = 0.6; /* FIXME: default percentage? */
1084 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1089 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1090 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1091 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1095 GNUNET_free (p_string);
1100 cutoff = (unsigned int) (nat_percentage * pg->total);
1102 connect_attempts = 0;
1104 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1106 for (inner_count = outer_count + 1; inner_count < pg->total;
1109 if ((outer_count > cutoff) || (inner_count > cutoff))
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113 "Connecting peer %d to peer %d\n",
1114 outer_count, inner_count);
1116 connect_attempts += proc(pg, outer_count, inner_count);
1121 return connect_attempts;
1126 * Create a topology given a peer group (set of running peers)
1127 * and a connection processor.
1129 * @param pg the peergroup to create the topology on
1130 * @param proc the connection processor to call to actually set
1131 * up connections between two peers
1133 * @return the number of connections that were set up
1137 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1139 unsigned int i, j, k;
1140 unsigned int square;
1143 unsigned int toggle = 1;
1144 unsigned int nodeToConnect;
1145 unsigned int natLog;
1146 unsigned int node1Row;
1147 unsigned int node1Col;
1148 unsigned int node2Row;
1149 unsigned int node2Col;
1150 unsigned int distance;
1151 double probability, random, percentage;
1152 unsigned int smallWorldConnections;
1154 int connect_attempts;
1155 square = floor (sqrt (pg->total));
1159 percentage = 0.5; /* FIXME: default percentage? */
1160 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1165 if (sscanf(p_string, "%lf", &percentage) != 1)
1166 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1167 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1171 GNUNET_free (p_string);
1173 probability = 0.5; /* FIXME: default percentage? */
1174 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1179 if (sscanf(p_string, "%lf", &probability) != 1)
1180 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1181 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1185 GNUNET_free (p_string);
1187 if (square * square != pg->total)
1189 while (rows * cols < pg->total)
1191 if (toggle % 2 == 0)
1200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1201 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1205 connect_attempts = 0;
1206 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1207 * to the node to its right and above. Once this is over, we'll have our torus!
1208 * Special case for the last node (if the rows and columns are not equal), connect
1209 * to the first in the row to maintain topology.
1211 for (i = 0; i < pg->total; i++)
1213 /* First connect to the node to the right */
1214 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1215 nodeToConnect = i + 1;
1216 else if (i + 1 == pg->total)
1217 nodeToConnect = rows * cols - cols;
1219 nodeToConnect = i - cols + 1;
1221 connect_attempts += proc (pg, i, nodeToConnect);
1224 nodeToConnect = (rows * cols) - cols + i;
1226 nodeToConnect = i - cols;
1228 if (nodeToConnect < pg->total)
1229 connect_attempts += proc (pg, i, nodeToConnect);
1231 natLog = log (pg->total);
1232 #if VERBOSE_TESTING > 2
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1234 _("natural log of %d is %d, will run %d iterations\n"),
1235 pg->total, natLog, (int) (natLog * percentage));
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1238 smallWorldConnections = 0;
1239 for (i = 0; i < (int) (natLog * percentage); i++)
1241 for (j = 0; j < pg->total; j++)
1243 /* Determine the row and column of node at position j on the 2d torus */
1244 node1Row = j / cols;
1245 node1Col = j - (node1Row * cols);
1246 for (k = 0; k < pg->total; k++)
1248 /* Determine the row and column of node at position k on the 2d torus */
1249 node2Row = k / cols;
1250 node2Col = k - (node2Row * cols);
1251 /* Simple Cartesian distance */
1252 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1255 /* Calculate probability as 1 over the square of the distance */
1256 probability = 1.0 / (distance * distance);
1257 /* Choose a random value between 0 and 1 */
1258 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1259 UINT64_MAX)) / ( (double) UINT64_MAX);
1260 /* If random < probability, then connect the two nodes */
1261 if (random < probability)
1262 smallWorldConnections += proc (pg, j, k);
1268 connect_attempts += smallWorldConnections;
1269 #if VERBOSE_TESTING > 2
1270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1271 _("Total connections added for small world: %d!\n"),
1272 smallWorldConnections);
1274 return connect_attempts;
1278 * Create a topology given a peer group (set of running peers)
1279 * and a connection processor.
1281 * @param pg the peergroup to create the topology on
1282 * @param proc the connection processor to call to actually set
1283 * up connections between two peers
1285 * @return the number of connections that were set up
1289 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1292 unsigned int outer_count;
1293 unsigned int inner_count;
1294 int connect_attempts;
1298 probability = 0.5; /* FIXME: default percentage? */
1299 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1304 if (sscanf(p_string, "%lf", &probability) != 1)
1305 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1306 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1310 GNUNET_free (p_string);
1312 connect_attempts = 0;
1313 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1315 for (inner_count = outer_count + 1; inner_count < pg->total;
1318 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1319 UINT64_MAX)) / ( (double) UINT64_MAX);
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322 _("rand is %f probability is %f\n"), temp_rand,
1325 if (temp_rand < probability)
1327 connect_attempts += proc (pg, outer_count, inner_count);
1332 return connect_attempts;
1336 * Create a topology given a peer group (set of running peers)
1337 * and a connection processor.
1339 * @param pg the peergroup to create the topology on
1340 * @param proc the connection processor to call to actually set
1341 * up connections between two peers
1343 * @return the number of connections that were set up
1347 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1350 unsigned int square;
1353 unsigned int toggle = 1;
1354 unsigned int nodeToConnect;
1355 int connect_attempts;
1357 connect_attempts = 0;
1359 square = floor (sqrt (pg->total));
1363 if (square * square != pg->total)
1365 while (rows * cols < pg->total)
1367 if (toggle % 2 == 0)
1376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1377 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1380 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1381 * to the node to its right and above. Once this is over, we'll have our torus!
1382 * Special case for the last node (if the rows and columns are not equal), connect
1383 * to the first in the row to maintain topology.
1385 for (i = 0; i < pg->total; i++)
1387 /* First connect to the node to the right */
1388 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1389 nodeToConnect = i + 1;
1390 else if (i + 1 == pg->total)
1391 nodeToConnect = rows * cols - cols;
1393 nodeToConnect = i - cols + 1;
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1396 "Connecting peer %d to peer %d\n",
1399 connect_attempts += proc(pg, i, nodeToConnect);
1401 /* Second connect to the node immediately above */
1403 nodeToConnect = (rows * cols) - cols + i;
1405 nodeToConnect = i - cols;
1407 if (nodeToConnect < pg->total)
1410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1411 "Connecting peer %d to peer %d\n",
1414 connect_attempts += proc(pg, i, nodeToConnect);
1419 return connect_attempts;
1424 * Create a topology given a peer group (set of running peers)
1425 * and a connection processor.
1427 * @param pg the peergroup to create the topology on
1428 * @param proc the connection processor to call to actually set
1429 * up connections between two peers
1431 * @return the number of connections that were set up
1435 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1437 unsigned int outer_count;
1438 unsigned int inner_count;
1439 int connect_attempts;
1441 connect_attempts = 0;
1443 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1445 for (inner_count = outer_count + 1; inner_count < pg->total;
1449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1450 "Connecting peer %d to peer %d\n",
1451 outer_count, inner_count);
1453 connect_attempts += proc(pg, outer_count, inner_count);
1457 return connect_attempts;
1461 * Create a topology given a peer group (set of running peers)
1462 * and a connection processor.
1464 * @param pg the peergroup to create the topology on
1465 * @param proc the connection processor to call to actually set
1466 * up connections between two peers
1468 * @return the number of connections that were set up
1472 create_line (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1475 int connect_attempts;
1477 connect_attempts = 0;
1479 /* Connect each peer to the next highest numbered peer */
1480 for (count = 0; count < pg->total - 1; count++)
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1484 "Connecting peer %d to peer %d\n",
1487 connect_attempts += proc(pg, count, count + 1);
1490 return connect_attempts;
1494 * Create a topology given a peer group (set of running peers)
1495 * and a connection processor.
1497 * @param pg the peergroup to create the topology on
1498 * @param proc the connection processor to call to actually set
1499 * up connections between two peers
1501 * @return the number of connections that were set up
1505 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1508 int connect_attempts;
1510 connect_attempts = 0;
1512 /* Connect each peer to the next highest numbered peer */
1513 for (count = 0; count < pg->total - 1; count++)
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1517 "Connecting peer %d to peer %d\n",
1520 connect_attempts += proc(pg, count, count + 1);
1523 /* Connect the last peer to the first peer */
1524 connect_attempts += proc(pg, pg->total - 1, 0);
1526 return connect_attempts;
1531 * Iterator for writing friends of a peer to a file.
1533 * @param cls closure, an open writable file handle
1534 * @param key the key the daemon was stored under
1535 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1537 * @return GNUNET_YES to continue iteration
1539 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1540 * with a single file_iterator that takes a closure which contains
1541 * the prefix to write before the peer. Then this could be used
1542 * for blacklisting multiple transports and writing the friend
1543 * file. I'm sure *someone* will complain loudly about other
1544 * things that negate these functions even existing so no point in
1548 friend_file_iterator (void *cls,
1549 const GNUNET_HashCode * key,
1552 FILE *temp_friend_handle = cls;
1553 struct GNUNET_TESTING_Daemon *peer = value;
1554 struct GNUNET_PeerIdentity *temppeer;
1555 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1557 temppeer = &peer->id;
1558 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1559 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1564 struct BlacklistContext
1567 * The (open) file handle to write to
1569 FILE *temp_file_handle;
1572 * The transport that this peer will be blacklisted on.
1578 * Iterator for writing blacklist data to appropriate files.
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
1587 blacklist_file_iterator (void *cls,
1588 const GNUNET_HashCode * key,
1591 struct BlacklistContext *blacklist_ctx = cls;
1592 //FILE *temp_blacklist_handle = cls;
1593 struct GNUNET_TESTING_Daemon *peer = value;
1594 struct GNUNET_PeerIdentity *temppeer;
1595 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1597 temppeer = &peer->id;
1598 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1599 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1605 * Create the friend files based on the PeerConnection's
1606 * of each peer in the peer group, and copy the files
1607 * to the appropriate place
1609 * @param pg the peer group we are dealing with
1612 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1614 FILE *temp_friend_handle;
1615 unsigned int pg_iter;
1616 char *temp_service_path;
1620 enum GNUNET_OS_ProcessStatusType type;
1621 unsigned long return_code;
1626 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1627 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1629 mytemp = GNUNET_DISK_mktemp("friends");
1630 GNUNET_assert(mytemp != NULL);
1631 temp_friend_handle = fopen (mytemp, "wt");
1632 GNUNET_assert(temp_friend_handle != NULL);
1633 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1634 fclose(temp_friend_handle);
1637 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1639 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1640 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1643 if (UNLINK (mytemp) != 0)
1644 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1645 GNUNET_free (mytemp);
1649 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1651 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1652 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1653 "mv", mytemp, arg, NULL);
1655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1656 _("Copying file with command cp %s %s\n"), mytemp, arg);
1661 else /* Remote, scp the file to the correct place */
1663 if (NULL != pg->peers[pg_iter].daemon->username)
1664 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1666 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1667 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1668 "scp", mytemp, arg, NULL);
1671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1672 _("Copying file with command scp %s %s\n"), mytemp, arg);
1676 GNUNET_free (temp_service_path);
1677 GNUNET_free (mytemp);
1681 ret = GNUNET_SYSERR;
1682 while ((count < max_wait) && (ret != GNUNET_OK))
1685 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1689 _("Checking copy status of file %d\n"), pg_iter);
1691 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1693 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1695 ret = GNUNET_SYSERR;
1697 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1699 ret = GNUNET_SYSERR;
1703 pidarr[pg_iter] = 0;
1705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1706 _("File %d copied\n"), pg_iter);
1712 if (ret == GNUNET_SYSERR)
1714 /* FIXME: why sleep here? -CG */
1720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1721 _("Finished copying all friend files!\n"));
1723 GNUNET_free(pidarr);
1729 * Create the blacklist files based on the PeerConnection's
1730 * of each peer in the peer group, and copy the files
1731 * to the appropriate place.
1733 * @param pg the peer group we are dealing with
1734 * @param transports space delimited list of transports to blacklist
1737 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
1739 FILE *temp_file_handle;
1740 static struct BlacklistContext blacklist_ctx;
1741 unsigned int pg_iter;
1742 char *temp_service_path;
1746 enum GNUNET_OS_ProcessStatusType type;
1747 unsigned long return_code;
1754 char *temp_transports;
1756 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1757 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1759 mytemp = GNUNET_DISK_mktemp("blacklist");
1760 GNUNET_assert(mytemp != NULL);
1761 temp_file_handle = fopen (mytemp, "wt");
1762 GNUNET_assert(temp_file_handle != NULL);
1763 temp_transports = GNUNET_strdup(transports);
1764 blacklist_ctx.temp_file_handle = temp_file_handle;
1765 transport_len = strlen(temp_transports) + 1;
1768 for (i = 0; i < transport_len; i++)
1770 if ((temp_transports[i] == ' ') && (pos == NULL))
1771 continue; /* At start of string (whitespace) */
1772 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
1774 temp_transports[i] = '\0';
1775 blacklist_ctx.transport = pos;
1776 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
1778 } /* At beginning of actual string */
1779 else if (pos == NULL)
1781 pos = &temp_transports[i];
1785 GNUNET_free (temp_transports);
1786 fclose(temp_file_handle);
1789 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1791 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1792 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1795 if (UNLINK (mytemp) != 0)
1796 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1797 GNUNET_free (mytemp);
1801 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1803 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
1804 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1805 "mv", mytemp, arg, NULL);
1807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1808 _("Copying file with command cp %s %s\n"), mytemp, arg);
1813 else /* Remote, scp the file to the correct place */
1815 if (NULL != pg->peers[pg_iter].daemon->username)
1816 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1818 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1819 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1820 "scp", mytemp, arg, NULL);
1823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1824 _("Copying file with command scp %s %s\n"), mytemp, arg);
1828 GNUNET_free (temp_service_path);
1829 GNUNET_free (mytemp);
1833 ret = GNUNET_SYSERR;
1834 while ((count < max_wait) && (ret != GNUNET_OK))
1837 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1841 _("Checking copy status of file %d\n"), pg_iter);
1843 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1845 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1847 ret = GNUNET_SYSERR;
1849 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1851 ret = GNUNET_SYSERR;
1855 pidarr[pg_iter] = 0;
1857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1858 _("File %d copied\n"), pg_iter);
1864 if (ret == GNUNET_SYSERR)
1866 /* FIXME: why sleep here? -CG */
1872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1873 _("Finished copying all blacklist files!\n"));
1875 GNUNET_free(pidarr);
1881 * Internal notification of a connection, kept so that we can ensure some connections
1882 * happen instead of flooding all testing daemons with requests to connect.
1884 static void internal_connect_notify (void *cls,
1885 const struct GNUNET_PeerIdentity *first,
1886 const struct GNUNET_PeerIdentity *second,
1888 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
1889 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
1890 struct GNUNET_TESTING_Daemon *first_daemon,
1891 struct GNUNET_TESTING_Daemon *second_daemon,
1894 struct GNUNET_TESTING_PeerGroup *pg = cls;
1895 outstanding_connects--;
1897 pg->notify_connection(pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
1901 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1903 struct ConnectContext *connect_context = cls;
1905 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1908 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
1910 #if VERBOSE_TESTING > 2
1911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1912 _("Delaying connect, we have too many outstanding connections!\n"));
1914 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &schedule_connect, connect_context);
1918 #if VERBOSE_TESTING > 2
1919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1920 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
1922 outstanding_connects++;
1923 GNUNET_TESTING_daemons_connect (connect_context->first,
1924 connect_context->second,
1927 &internal_connect_notify,
1928 connect_context->pg);
1929 GNUNET_free(connect_context);
1935 * Iterator for actually scheduling connections to be created
1936 * between two peers.
1938 * @param cls closure, a GNUNET_TESTING_Daemon
1939 * @param key the key the second Daemon was stored under
1940 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1942 * @return GNUNET_YES to continue iteration
1945 connect_iterator (void *cls,
1946 const GNUNET_HashCode * key,
1949 struct PeerData *first = cls;
1950 struct GNUNET_TESTING_Daemon *second = value;
1951 struct ConnectContext *connect_context;
1953 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1954 connect_context->pg = first->pg;
1955 connect_context->first = first->daemon;
1956 connect_context->second = second;
1957 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
1964 * Iterator for copying all entries in the allowed hashmap to the
1967 * @param cls closure, a GNUNET_TESTING_Daemon
1968 * @param key the key the second Daemon was stored under
1969 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1971 * @return GNUNET_YES to continue iteration
1974 copy_topology_iterator (void *cls,
1975 const GNUNET_HashCode * key,
1978 struct PeerData *first = cls;
1980 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1986 * Make the peers to connect the same as those that are allowed to be
1989 * @param pg the peer group
1992 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
1994 unsigned int pg_iter;
1999 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2001 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
2002 if (GNUNET_SYSERR == ret)
2003 return GNUNET_SYSERR;
2005 total = total + ret;
2013 * Connect the topology as specified by the PeerConnection's
2014 * of each peer in the peer group
2016 * @param pg the peer group we are dealing with
2017 * @return the number of connections that will be attempted
2020 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
2022 unsigned int pg_iter;
2026 struct PeerConnection *connection_iter;
2027 struct ConnectContext *connect_context;
2031 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2033 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
2034 if (GNUNET_SYSERR == ret)
2035 return GNUNET_SYSERR;
2037 total = total + ret;
2041 while (connection_iter != NULL)
2043 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2044 connect_context->pg = pg;
2045 connect_context->first = ;
2046 connect_context->second = connection_iter->daemon;
2047 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
2048 connection_iter = connection_iter->next;
2057 * Takes a peer group and creates a topology based on the
2058 * one specified. Creates a topology means generates friend
2059 * files for the peers so they can only connect to those allowed
2060 * by the topology. This will only have an effect once peers
2061 * are started if the FRIENDS_ONLY option is set in the base
2062 * config. Also takes an optional restrict topology which
2063 * disallows connections based on a particular transport
2064 * UNLESS they are specified in the restricted topology.
2066 * @param pg the peer group struct representing the running peers
2067 * @param topology which topology to connect the peers in
2068 * @param restrict_topology allow only direct TCP connections in this topology
2069 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2070 * @param restrict_transports space delimited list of transports to blacklist
2071 * to create restricted topology
2073 * @return the maximum number of connections were all allowed peers
2074 * connected to each other
2077 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2078 enum GNUNET_TESTING_Topology topology,
2079 enum GNUNET_TESTING_Topology restrict_topology,
2080 char *restrict_transports)
2083 int num_connections;
2084 int unblacklisted_connections;
2086 GNUNET_assert (pg->notify_connection != NULL);
2091 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2094 _("Creating clique topology\n"));
2096 num_connections = create_clique (pg, &add_allowed_connections);
2098 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2101 _("Creating small world (ring) topology\n"));
2103 num_connections = create_small_world_ring (pg, &add_allowed_connections);
2105 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2108 _("Creating small world (2d-torus) topology\n"));
2110 num_connections = create_small_world (pg, &add_allowed_connections);
2112 case GNUNET_TESTING_TOPOLOGY_RING:
2114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2115 _("Creating ring topology\n"));
2117 num_connections = create_ring (pg, &add_allowed_connections);
2119 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2122 _("Creating 2d torus topology\n"));
2124 num_connections = create_2d_torus (pg, &add_allowed_connections);
2126 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2129 _("Creating Erdos-Renyi topology\n"));
2131 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2133 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2136 _("Creating InterNAT topology\n"));
2138 num_connections = create_nated_internet (pg, &add_allowed_connections);
2140 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2143 _("Creating Scale Free topology\n"));
2145 num_connections = create_scale_free (pg, &add_allowed_connections);
2147 case GNUNET_TESTING_TOPOLOGY_LINE:
2149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2150 _("Creating straight line topology\n"));
2152 num_connections = create_line (pg, &add_allowed_connections);
2154 case GNUNET_TESTING_TOPOLOGY_NONE:
2156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2157 _("Creating no allowed topology (all peers can connect at core level)\n"));
2159 num_connections = 0;
2162 num_connections = 0;
2166 if (num_connections < 0)
2167 return GNUNET_SYSERR;
2169 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2171 ret = create_and_copy_friend_files(pg);
2172 if (ret != GNUNET_OK)
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2176 _("Failed during friend file copying!\n"));
2178 return GNUNET_SYSERR;
2183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2184 _("Friend files created/copied successfully!\n"));
2189 /* Use the create clique method to initially set all connections as blacklisted. */
2190 if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2191 create_clique (pg, &blacklist_connections);
2192 unblacklisted_connections = 0;
2193 /* Un-blacklist connections as per the topology specified */
2194 switch (restrict_topology)
2196 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2199 _("Blacklisting all but clique topology\n"));
2201 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2203 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2206 _("Blacklisting all but small world (ring) topology\n"));
2208 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2210 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2213 _("Blacklisting all but small world (2d-torus) topology\n"));
2215 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2217 case GNUNET_TESTING_TOPOLOGY_RING:
2219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2220 _("Blacklisting all but ring topology\n"));
2222 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2224 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2227 _("Blacklisting all but 2d torus topology\n"));
2229 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2231 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2234 _("Blacklisting all but Erdos-Renyi topology\n"));
2236 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2238 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2241 _("Blacklisting all but InterNAT topology\n"));
2243 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2245 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2248 _("Blacklisting all but Scale Free topology\n"));
2250 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2252 case GNUNET_TESTING_TOPOLOGY_LINE:
2254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2255 _("Blacklisting all but straight line topology\n"));
2257 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2259 case GNUNET_TESTING_TOPOLOGY_NONE:
2261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2262 _("Creating no blacklist topology (all peers can connect at transport level)\n"));
2268 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2270 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2271 if (ret != GNUNET_OK)
2274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2275 _("Failed during blacklist file copying!\n"));
2277 return GNUNET_SYSERR;
2282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2283 _("Blacklist files created/copied successfully!\n"));
2287 return num_connections;
2290 struct RandomContext
2295 struct GNUNET_TESTING_PeerGroup *pg;
2298 * uid of the first peer
2303 * Peer data for first peer.
2305 struct PeerData *first;
2308 * Random percentage to use
2313 struct MinimumContext
2318 struct GNUNET_TESTING_PeerGroup *pg;
2321 * uid of the first peer
2326 * Peer data for first peer.
2328 struct PeerData *first;
2331 * Number of conns per peer
2333 unsigned int num_to_add;
2336 * Permuted array of all possible connections. Only add the Nth
2337 * peer if it's in the Nth position.
2339 unsigned int *pg_array;
2342 * What number is the current element we are iterating over?
2344 unsigned int current;
2352 struct GNUNET_TESTING_PeerGroup *pg;
2355 * uid of the first peer
2360 * uid of the second peer
2362 uint32_t second_uid;
2365 * Peer data for first peer.
2367 struct PeerData *first;
2370 * Which peer has been chosen as the one to add?
2372 unsigned int chosen;
2375 * What number is the current element we are iterating over?
2377 unsigned int current;
2381 * Iterator for choosing random peers to connect.
2383 * @param cls closure, a RandomContext
2384 * @param key the key the second Daemon was stored under
2385 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2387 * @return GNUNET_YES to continue iteration
2390 random_connect_iterator (void *cls,
2391 const GNUNET_HashCode * key,
2394 struct RandomContext *random_ctx = cls;
2395 double random_number;
2396 uint32_t second_pos;
2397 GNUNET_HashCode first_hash;
2398 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2399 UINT64_MAX)) / ( (double) UINT64_MAX);
2400 if (random_number < random_ctx->percentage)
2402 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2404 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2405 uid_from_hash(key, &second_pos);
2406 hash_from_uid(random_ctx->first_uid, &first_hash);
2407 GNUNET_assert(random_ctx->pg->total > second_pos);
2408 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2414 * Iterator for adding at least X peers to a peers connection set.
2416 * @param cls closure, MinimumContext
2417 * @param key the key the second Daemon was stored under
2418 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2420 * @return GNUNET_YES to continue iteration
2423 minimum_connect_iterator (void *cls,
2424 const GNUNET_HashCode * key,
2427 struct MinimumContext *min_ctx = cls;
2428 uint32_t second_pos;
2429 GNUNET_HashCode first_hash;
2432 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2434 for (i = 0; i < min_ctx->num_to_add; i++)
2436 if (min_ctx->pg_array[i] == min_ctx->current)
2438 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2439 uid_from_hash(key, &second_pos);
2440 hash_from_uid(min_ctx->first_uid, &first_hash);
2441 GNUNET_assert(min_ctx->pg->total > second_pos);
2442 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));
2443 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2444 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2451 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2457 * Iterator for adding peers to a connection set based on a depth first search.
2459 * @param cls closure, MinimumContext
2460 * @param key the key the second daemon was stored under
2461 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2463 * @return GNUNET_YES to continue iteration
2466 dfs_connect_iterator (void *cls,
2467 const GNUNET_HashCode * key,
2470 struct DFSContext *dfs_ctx = cls;
2471 GNUNET_HashCode first_hash;
2473 if (dfs_ctx->current == dfs_ctx->chosen)
2475 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2476 uid_from_hash(key, &dfs_ctx->second_uid);
2477 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2478 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));
2479 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2480 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2481 return GNUNET_NO; /* We have found our peer, don't iterate more */
2490 * From the set of connections possible, choose percentage percent of connections
2491 * to actually connect.
2493 * @param pg the peergroup we are dealing with
2494 * @param percentage what percent of total connections to make
2497 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2499 struct RandomContext random_ctx;
2502 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2504 random_ctx.first_uid = pg_iter;
2505 random_ctx.first = &pg->peers[pg_iter];
2506 random_ctx.percentage = percentage;
2508 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2509 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2510 /* Now remove the old connections */
2511 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2512 /* And replace with the random set */
2513 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2518 * From the set of connections possible, choose at least num connections per
2521 * @param pg the peergroup we are dealing with
2522 * @param num how many connections at least should each peer have (if possible)?
2525 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2527 struct MinimumContext minimum_ctx;
2530 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2532 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2535 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2537 minimum_ctx.first_uid = pg_iter;
2538 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2539 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2540 minimum_ctx.first = &pg->peers[pg_iter];
2541 minimum_ctx.pg = pg;
2542 minimum_ctx.num_to_add = num;
2543 minimum_ctx.current = 0;
2544 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2545 &minimum_connect_iterator,
2549 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2551 /* Remove the "old" connections */
2552 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2553 /* And replace with the working set */
2554 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2561 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2564 unsigned int pg_iter;
2568 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2570 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2577 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2580 unsigned int pg_iter;
2584 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2586 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2593 * From the set of connections possible, choose at least num connections per
2594 * peer based on depth first traversal of peer connections. If DFS leaves
2595 * peers unconnected, ensure those peers get connections.
2597 * @param pg the peergroup we are dealing with
2598 * @param num how many connections at least should each peer have (if possible)?
2601 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2603 struct DFSContext dfs_ctx;
2606 uint32_t starting_peer;
2607 uint32_t least_connections;
2608 GNUNET_HashCode second_hash;
2610 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2612 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2617 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
2619 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
2621 least_connections = -1; /* Set to very high number */
2622 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2624 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
2626 starting_peer = pg_iter;
2627 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2632 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
2638 /* Choose a random peer from the chosen peers set of connections to add */
2639 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
2640 dfs_ctx.first_uid = starting_peer;
2641 dfs_ctx.first = &pg->peers[starting_peer];
2643 dfs_ctx.current = 0;
2645 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
2646 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
2647 hash_from_uid(dfs_ctx.second_uid, &second_hash);
2648 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
2649 starting_peer = dfs_ctx.second_uid;
2652 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2654 /* Remove the "old" connections */
2655 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2656 /* And replace with the working set */
2657 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2662 * There are many ways to connect peers that are supported by this function.
2663 * To connect peers in the same topology that was created via the
2664 * GNUNET_TESTING_create_topology, the topology variable must be set to
2665 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
2666 * a new instance of that topology will be generated and attempted to be
2667 * connected. This could result in some connections being impossible,
2668 * because some topologies are non-deterministic.
2670 * @param pg the peer group struct representing the running peers
2671 * @param topology which topology to connect the peers in
2672 * @param options options for connecting the topology
2673 * @param option_modifier modifier for options that take a parameter
2674 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
2677 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2678 enum GNUNET_TESTING_Topology topology,
2679 enum GNUNET_TESTING_TopologyOption options,
2680 double option_modifier)
2684 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2685 #if VERBOSE_TOPOLOGY
2686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2687 _("Creating clique CONNECT topology\n"));
2689 create_clique (pg, &add_actual_connections);
2691 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2692 #if VERBOSE_TOPOLOGY
2693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2694 _("Creating small world (ring) CONNECT topology\n"));
2696 create_small_world_ring (pg, &add_actual_connections);
2698 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2699 #if VERBOSE_TOPOLOGY
2700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2701 _("Creating small world (2d-torus) CONNECT topology\n"));
2703 create_small_world (pg, &add_actual_connections);
2705 case GNUNET_TESTING_TOPOLOGY_RING:
2706 #if VERBOSE_TOPOLOGY
2707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2708 _("Creating ring CONNECT topology\n"));
2710 create_ring (pg, &add_actual_connections);
2712 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2713 #if VERBOSE_TOPOLOGY
2714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2715 _("Creating 2d torus CONNECT topology\n"));
2717 create_2d_torus (pg, &add_actual_connections);
2719 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2720 #if VERBOSE_TOPOLOGY
2721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2722 _("Creating Erdos-Renyi CONNECT topology\n"));
2724 create_erdos_renyi (pg, &add_actual_connections);
2726 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2727 #if VERBOSE_TOPOLOGY
2728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2729 _("Creating InterNAT CONNECT topology\n"));
2731 create_nated_internet (pg, &add_actual_connections);
2733 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2734 #if VERBOSE_TOPOLOGY
2735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2736 _("Creating Scale Free CONNECT topology\n"));
2738 create_scale_free (pg, &add_actual_connections);
2740 case GNUNET_TESTING_TOPOLOGY_LINE:
2741 #if VERBOSE_TOPOLOGY
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2743 _("Creating straight line CONNECT topology\n"));
2745 create_line (pg, &add_actual_connections);
2747 case GNUNET_TESTING_TOPOLOGY_NONE:
2748 #if VERBOSE_TOPOLOGY
2749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2750 _("Creating no CONNECT topology\n"));
2752 copy_allowed_topology(pg);
2755 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2756 _("Unknown topology specification, can't connect peers!\n"));
2757 return GNUNET_SYSERR;
2762 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
2763 #if VERBOSE_TOPOLOGY
2764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2765 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
2767 choose_random_connections(pg, option_modifier);
2769 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
2770 #if VERBOSE_TOPOLOGY
2771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2772 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
2774 choose_minimum(pg, (unsigned int)option_modifier);
2776 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
2777 #if VERBOSE_TOPOLOGY
2778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2779 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
2781 perform_dfs(pg, (int)option_modifier);
2783 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
2785 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
2791 return connect_topology(pg);
2795 * Function which continues a peer group starting up
2796 * after successfully generating hostkeys for each peer.
2798 * @param pg the peer group to continue starting
2802 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
2806 for (i = 0; i < pg->total; i++)
2808 GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
2812 struct InternalStartContext
2814 struct PeerData *peer;
2815 struct GNUNET_SCHEDULER_Handle *sched;
2816 const struct GNUNET_CONFIGURATION_Handle *pcfg;
2817 struct GNUNET_TIME_Relative timeout;
2818 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
2820 GNUNET_TESTING_NotifyDaemonRunning cb;
2822 const char *hostname;
2827 * Prototype of a function that will be called whenever
2828 * a daemon was started by the testing library.
2830 * @param cls closure
2831 * @param id identifier for the daemon, NULL on error
2832 * @param d handle for the daemon
2833 * @param emsg error message (NULL on success)
2835 static void internal_hostkey_callback (void *cls,
2836 const struct GNUNET_PeerIdentity *id,
2837 struct GNUNET_TESTING_Daemon *d,
2840 struct InternalStartContext *internal_context = cls;
2841 internal_context->peer->pg->starting--;
2842 internal_context->hostkey_callback(internal_context->hostkey_cls, id, d, emsg);
2843 GNUNET_free(internal_context);
2847 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2849 struct InternalStartContext *internal_context = cls;
2851 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2853 GNUNET_free(internal_context);
2857 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
2859 internal_context->peer->pg->starting++;
2860 internal_context->peer->daemon = GNUNET_TESTING_daemon_start (internal_context->sched,
2861 internal_context->pcfg,
2862 internal_context->timeout,
2863 internal_context->hostname,
2864 &internal_hostkey_callback,
2866 internal_context->cb,
2867 internal_context->cb_cls);
2871 GNUNET_SCHEDULER_add_delayed(internal_context->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_start, internal_context);
2876 * Start count gnunetd processes with the same set of transports and
2877 * applications. The port numbers (any option called "PORT") will be
2878 * adjusted to ensure that no two peers running on the same system
2879 * have the same port(s) in their respective configurations.
2881 * @param sched scheduler to use
2882 * @param cfg configuration template to use
2883 * @param total number of daemons to start
2884 * @param timeout total time allowed for peers to start
2885 * @param hostkey_callback function to call on each peers hostkey generation
2886 * if NULL, peers will be started by this call, if non-null,
2887 * GNUNET_TESTING_daemons_continue_startup must be called after
2888 * successful hostkey generation
2889 * @param hostkey_cls closure for hostkey callback
2890 * @param cb function to call on each daemon that was started
2891 * @param cb_cls closure for cb
2892 * @param connect_callback function to call each time two hosts are connected
2893 * @param connect_callback_cls closure for connect_callback
2894 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
2895 * everything on localhost).
2896 * @return NULL on error, otherwise handle to control peer group
2898 struct GNUNET_TESTING_PeerGroup *
2899 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
2900 const struct GNUNET_CONFIGURATION_Handle *cfg,
2902 struct GNUNET_TIME_Relative timeout,
2903 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
2905 GNUNET_TESTING_NotifyDaemonRunning cb,
2907 GNUNET_TESTING_NotifyConnection
2908 connect_callback, void *connect_callback_cls,
2909 const char *hostnames)
2911 struct GNUNET_TESTING_PeerGroup *pg;
2915 const char *hostname;
2916 char *baseservicehome;
2917 char *newservicehome;
2919 struct InternalStartContext *internal_context;
2920 struct GNUNET_CONFIGURATION_Handle *pcfg;
2922 unsigned int hostcnt;
2932 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
2936 pg->cb_cls = cb_cls;
2937 pg->notify_connection = connect_callback;
2938 pg->notify_connection_cls = connect_callback_cls;
2940 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
2941 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
2942 if (NULL != hostnames)
2945 /* skip leading spaces */
2946 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
2949 while ('\0' != *rpos)
2951 if (isspace ( (unsigned char) *rpos))
2955 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
2957 start = GNUNET_strdup (hostnames);
2959 while ('\0' != *pos)
2961 if (isspace ( (unsigned char) *pos))
2964 if (strlen (start) > 0)
2966 pg->hosts[off].minport = LOW_PORT;
2967 pg->hosts[off++].hostname = start;
2973 if (strlen (start) > 0)
2975 pg->hosts[off].minport = LOW_PORT;
2976 pg->hosts[off++].hostname = start;
2980 GNUNET_free (start);
2981 GNUNET_free (pg->hosts);
2985 minport = 0; /* make gcc happy */
2992 for (off = 0; off < total; off++)
2996 hostname = pg->hosts[off % hostcnt].hostname;
2997 pcfg = make_config (cfg,
2998 &pg->hosts[off % hostcnt].minport,
3005 pcfg = make_config (cfg,
3013 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3015 ("Could not create configuration for peer number %u on `%s'!\n"),
3016 off, hostname == NULL ? "localhost" : hostname);
3021 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
3024 GNUNET_asprintf (&newservicehome,
3025 "%s/%d/", baseservicehome, off);
3026 GNUNET_free (baseservicehome);
3030 tmpdir = getenv ("TMPDIR");
3031 tmpdir = tmpdir ? tmpdir : "/tmp";
3032 GNUNET_asprintf (&newservicehome,
3035 "gnunet-testing-test-test", off);
3037 GNUNET_CONFIGURATION_set_value_string (pcfg,
3039 "SERVICEHOME", newservicehome);
3040 GNUNET_free (newservicehome);
3041 pg->peers[off].cfg = pcfg;
3042 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
3043 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
3044 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
3045 pg->peers[off].pg = pg;
3046 internal_context = GNUNET_malloc(sizeof(struct InternalStartContext));
3047 internal_context->sched = sched;
3048 internal_context->peer = &pg->peers[off];
3049 internal_context->pcfg = pcfg;
3050 internal_context->timeout = timeout;
3051 internal_context->hostname = hostname;
3052 internal_context->hostkey_callback = hostkey_callback;
3053 internal_context->hostkey_cls = hostkey_cls;
3054 internal_context->cb = cb;
3055 internal_context->cb_cls = cb_cls;
3057 GNUNET_SCHEDULER_add_now (sched, &internal_start, internal_context);
3060 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (sched,
3067 if (NULL == pg->peers[off].daemon)
3068 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3069 _("Could not start peer number %u!\n"), off);
3077 * Get a daemon by number, so callers don't have to do nasty
3078 * offsetting operation.
3080 struct GNUNET_TESTING_Daemon *
3081 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
3083 if (position < pg->total)
3084 return pg->peers[position].daemon;
3090 * Prototype of a function that will be called when a
3091 * particular operation was completed the testing library.
3093 * @param cls closure (a struct RestartContext)
3094 * @param id id of the peer that was restarted
3095 * @param cfg handle to the configuration of the peer
3096 * @param d handle to the daemon that was restarted
3097 * @param emsg NULL on success
3099 void restart_callback (void *cls,
3100 const struct GNUNET_PeerIdentity *id,
3101 const struct GNUNET_CONFIGURATION_Handle *cfg,
3102 struct GNUNET_TESTING_Daemon *d,
3105 struct RestartContext *restart_context = cls;
3109 restart_context->peers_restarted++;
3113 restart_context->peers_restart_failed++;
3116 if (restart_context->peers_restarted == restart_context->peer_group->total)
3118 restart_context->callback(restart_context->callback_cls, NULL);
3119 GNUNET_free(restart_context);
3121 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
3123 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
3124 GNUNET_free(restart_context);
3130 * Callback for informing us about a successful
3131 * or unsuccessful churn stop call.
3133 * @param cls a ChurnContext
3134 * @param emsg NULL on success, non-NULL on failure
3138 churn_stop_callback (void *cls, const char *emsg)
3140 struct ChurnContext *churn_ctx = cls;
3141 unsigned int total_left;
3142 char *error_message;
3144 error_message = NULL;
3147 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3148 "Churn stop callback failed with error `%s'\n", emsg);
3149 churn_ctx->num_failed_stop++;
3153 churn_ctx->num_to_stop--;
3157 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3158 "Stopped peer, %d left.\n",
3159 churn_ctx->num_to_stop);
3161 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3163 if (total_left == 0)
3165 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3167 GNUNET_asprintf(&error_message,
3168 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3169 churn_ctx->num_failed_start,
3170 churn_ctx->num_failed_stop);
3172 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3173 GNUNET_free_non_null(error_message);
3174 GNUNET_free(churn_ctx);
3179 * Callback for informing us about a successful
3180 * or unsuccessful churn start call.
3182 * @param cls a ChurnContext
3183 * @param id the peer identity of the started peer
3184 * @param cfg the handle to the configuration of the peer
3185 * @param d handle to the daemon for the peer
3186 * @param emsg NULL on success, non-NULL on failure
3190 churn_start_callback (void *cls,
3191 const struct GNUNET_PeerIdentity *id,
3192 const struct GNUNET_CONFIGURATION_Handle *cfg,
3193 struct GNUNET_TESTING_Daemon *d,
3196 struct ChurnContext *churn_ctx = cls;
3197 unsigned int total_left;
3198 char *error_message;
3200 error_message = NULL;
3203 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3204 "Churn stop callback failed with error `%s'\n",
3206 churn_ctx->num_failed_start++;
3210 churn_ctx->num_to_start--;
3214 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3215 "Started peer, %d left.\n",
3216 churn_ctx->num_to_start);
3219 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3221 if (total_left == 0)
3223 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3224 GNUNET_asprintf(&error_message,
3225 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3226 churn_ctx->num_failed_start,
3227 churn_ctx->num_failed_stop);
3228 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3229 GNUNET_free_non_null(error_message);
3230 GNUNET_free(churn_ctx);
3236 * Simulate churn by stopping some peers (and possibly
3237 * re-starting others if churn is called multiple times). This
3238 * function can only be used to create leave-join churn (peers "never"
3239 * leave for good). First "voff" random peers that are currently
3240 * online will be taken offline; then "von" random peers that are then
3241 * offline will be put back online. No notifications will be
3242 * generated for any of these operations except for the callback upon
3245 * @param pg handle for the peer group
3246 * @param voff number of peers that should go offline
3247 * @param von number of peers that should come back online;
3248 * must be zero on first call (since "testbed_start"
3249 * always starts all of the peers)
3250 * @param timeout how long to wait for operations to finish before
3252 * @param cb function to call at the end
3253 * @param cb_cls closure for cb
3256 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
3259 struct GNUNET_TIME_Relative timeout,
3260 GNUNET_TESTING_NotifyCompletion cb,
3263 struct ChurnContext *churn_ctx;
3264 unsigned int running;
3265 unsigned int stopped;
3266 unsigned int total_running;
3267 unsigned int total_stopped;
3269 unsigned int *running_arr;
3270 unsigned int *stopped_arr;
3271 unsigned int *running_permute;
3272 unsigned int *stopped_permute;
3277 if ((von == 0) && (voff == 0)) /* No peers at all? */
3283 for (i = 0; i < pg->total; i++)
3285 if (pg->peers[i].daemon->running == GNUNET_YES)
3287 GNUNET_assert(running != -1);
3292 GNUNET_assert(stopped != -1);
3299 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
3300 cb(cb_cls, "Trying to stop more peers than are currently running!");
3306 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
3307 cb(cb_cls, "Trying to start more peers than are currently stopped!");
3311 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3315 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
3319 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
3321 running_permute = NULL;
3322 stopped_permute = NULL;
3325 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
3327 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
3329 total_running = running;
3330 total_stopped = stopped;
3334 churn_ctx->num_to_start = von;
3335 churn_ctx->num_to_stop = voff;
3337 churn_ctx->cb_cls = cb_cls;
3339 for (i = 0; i < pg->total; i++)
3341 if (pg->peers[i].daemon->running == GNUNET_YES)
3343 GNUNET_assert((running_arr != NULL) && (total_running > running));
3344 running_arr[running] = i;
3349 GNUNET_assert((stopped_arr != NULL) && (total_stopped > stopped));
3350 stopped_arr[stopped] = i;
3355 for (i = 0; i < voff; i++)
3358 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
3360 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
3362 &churn_stop_callback, churn_ctx,
3363 GNUNET_NO, GNUNET_YES);
3366 for (i = 0; i < von; i++)
3369 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
3371 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
3372 timeout, &churn_start_callback, churn_ctx);
3375 GNUNET_free_non_null(running_arr);
3376 GNUNET_free_non_null(stopped_arr);
3377 GNUNET_free_non_null(running_permute);
3378 GNUNET_free_non_null(stopped_permute);
3383 * Restart all peers in the given group.
3385 * @param pg the handle to the peer group
3386 * @param callback function to call on completion (or failure)
3387 * @param callback_cls closure for the callback function
3390 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyCompletion callback, void *callback_cls)
3392 struct RestartContext *restart_context;
3397 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
3398 restart_context->peer_group = pg;
3399 restart_context->peers_restarted = 0;
3400 restart_context->callback = callback;
3401 restart_context->callback_cls = callback_cls;
3403 for (off = 0; off < pg->total; off++)
3405 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
3411 * Start or stop an individual peer from the given group.
3413 * @param pg handle to the peer group
3414 * @param offset which peer to start or stop
3415 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
3416 * @param timeout how long to wait for shutdown
3417 * @param cb function to call at the end
3418 * @param cb_cls closure for cb
3421 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
3422 unsigned int offset,
3424 struct GNUNET_TIME_Relative timeout,
3425 GNUNET_TESTING_NotifyCompletion cb,
3428 struct ChurnContext *churn_ctx;
3430 if (GNUNET_NO == desired_status)
3432 if (NULL != pg->peers[offset].daemon)
3434 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3435 churn_ctx->num_to_start = 0;
3436 churn_ctx->num_to_stop = 1;
3438 churn_ctx->cb_cls = cb_cls;
3439 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
3440 timeout, &churn_stop_callback, churn_ctx,
3441 GNUNET_NO, GNUNET_YES);
3444 else if (GNUNET_YES == desired_status)
3446 if (NULL == pg->peers[offset].daemon)
3448 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3449 churn_ctx->num_to_start = 1;
3450 churn_ctx->num_to_stop = 0;
3452 churn_ctx->cb_cls = cb_cls;
3453 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
3454 timeout, &churn_start_callback, churn_ctx);
3463 * Callback for shutting down peers in a peer group.
3465 * @param cls closure (struct ShutdownContext)
3466 * @param emsg NULL on success
3468 void internal_shutdown_callback (void *cls,
3471 struct ShutdownContext *shutdown_ctx = cls;
3475 shutdown_ctx->peers_down++;
3479 shutdown_ctx->peers_failed++;
3482 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
3484 if (shutdown_ctx->peers_failed > 0)
3485 shutdown_ctx->cb(shutdown_ctx->cb_cls, "Not all peers successfully shut down!");
3487 shutdown_ctx->cb(shutdown_ctx->cb_cls, NULL);
3488 GNUNET_free(shutdown_ctx);
3493 * Shutdown all peers started in the given group.
3495 * @param pg handle to the peer group
3496 * @param timeout how long to wait for shutdown
3497 * @param cb callback to notify upon success or failure
3498 * @param cb_cls closure for cb
3501 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
3502 struct GNUNET_TIME_Relative timeout,
3503 GNUNET_TESTING_NotifyCompletion cb,
3507 struct ShutdownContext *shutdown_ctx;
3508 GNUNET_TESTING_NotifyCompletion shutdown_cb;
3509 void *shutdown_cb_cls;
3511 GNUNET_assert(pg->total > 0);
3514 shutdown_ctx = NULL;
3518 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
3519 shutdown_ctx->cb = cb;
3520 shutdown_ctx->cb_cls = cb_cls;
3521 shutdown_ctx->total_peers = pg->total;
3522 shutdown_cb = &internal_shutdown_callback;
3523 shutdown_cb_cls = cb_cls;
3526 for (off = 0; off < pg->total; off++)
3528 if (NULL != pg->peers[off].daemon)
3529 GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
3530 if (NULL != pg->peers[off].cfg)
3531 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
3532 if (pg->peers[off].allowed_peers != NULL)
3533 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
3534 if (pg->peers[off].connect_peers != NULL)
3535 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
3536 if (pg->peers[off].blacklisted_peers != NULL)
3537 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
3539 GNUNET_free (pg->peers);
3540 if (NULL != pg->hosts)
3542 GNUNET_free (pg->hosts[0].hostname);
3543 GNUNET_free (pg->hosts);
3549 /* end of testing_group.c */