2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing_group.c
23 * @brief convenience API for writing testcases for GNUnet
24 * @author Nathan Evans
25 * @author Christian Grothoff
29 #include "gnunet_arm_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_core_service.h"
33 #define VERBOSE_TESTING GNUNET_NO
35 #define DEBUG_CHURN GNUNET_NO
38 * Lowest port used for GNUnet testing. Should be high enough to not
39 * conflict with other applications running on the hosts but be low
40 * enough to not conflict with client-ports (typically starting around
43 #define LOW_PORT 10000
46 * Highest port used for GNUnet testing. Should be low enough to not
47 * conflict with the port range for "local" ports (client apps; see
48 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
50 #define HIGH_PORT 56000
52 #define MAX_OUTSTANDING_CONNECTIONS 10
54 #define MAX_CONCURRENT_HOSTKEYS 10
56 #define MAX_CONCURRENT_STARTING 10
58 #define MAX_CONCURRENT_SHUTDOWN 10
60 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
62 #define CONNECT_ATTEMPTS 8
65 * Prototype of a function called whenever two peers would be connected
66 * in a certain topology.
68 typedef int (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup *pg,
74 * Context for handling churning a peer group
79 * Callback used to notify of churning finished
81 GNUNET_TESTING_NotifyCompletion cb;
84 * Closure for callback
89 * Number of peers that still need to be started
91 unsigned int num_to_start;
94 * Number of peers that still need to be stopped
96 unsigned int num_to_stop;
99 * Number of peers that failed to start
101 unsigned int num_failed_start;
104 * Number of peers that failed to stop
106 unsigned int num_failed_stop;
109 struct RestartContext
112 * The group of peers being restarted
114 struct GNUNET_TESTING_PeerGroup *peer_group;
117 * How many peers have been restarted thus far
119 unsigned int peers_restarted;
122 * How many peers got an error when restarting
124 unsigned int peers_restart_failed;
127 * The function to call once all peers have been restarted
129 GNUNET_TESTING_NotifyCompletion callback;
132 * Closure for callback function
139 struct ShutdownContext
142 * Total peers to wait for
144 unsigned int total_peers;
147 * Number of peers successfully shut down
149 unsigned int peers_down;
152 * Number of peers failed to shut down
154 unsigned int peers_failed;
157 * Number of peers we have started shutting
158 * down. If too many, wait on them.
160 unsigned int outstanding;
163 * Timeout for shutdown.
165 struct GNUNET_TIME_Relative timeout;
168 * Callback to call when all peers either
169 * shutdown or failed to shutdown
171 GNUNET_TESTING_NotifyCompletion cb;
179 struct CreateTopologyContext
183 * Function to call with number of connections
185 GNUNET_TESTING_NotifyConnections cont;
188 * Closure for connection notification
194 struct PeerConnection
199 struct PeerConnection *next;
202 * Pointer to daemon handle
204 struct GNUNET_TESTING_Daemon *daemon;
209 struct InternalStartContext
212 * Pointer to peerdata
214 struct PeerData *peer;
217 * Timeout for peer startup
219 struct GNUNET_TIME_Relative timeout;
222 * Client callback for hostkey notification
224 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
227 * Closure for hostkey_callback
232 * Client callback for peer start notification
234 GNUNET_TESTING_NotifyDaemonRunning start_cb;
242 * Hostname, where to start the peer
244 const char *hostname;
248 * Data we keep per peer.
253 * (Initial) configuration of the host.
254 * (initial because clients could change
255 * it and we would not know about those
258 struct GNUNET_CONFIGURATION_Handle *cfg;
261 * Handle for controlling the daemon.
263 struct GNUNET_TESTING_Daemon *daemon;
266 * The peergroup this peer belongs to.
268 struct GNUNET_TESTING_PeerGroup *pg;
271 * Hash map of allowed peer connections (F2F created topology)
273 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
276 * Hash map of blacklisted peers
278 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
281 * Hash map of peer connections
283 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
286 * Temporary hash map of peer connections
288 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
291 * Temporary variable for topology creation, should be reset before
292 * creating any topology so the count is valid once finished.
297 * Context to keep track of peers being started, to
298 * stagger hostkey generation and peer startup.
300 struct InternalStartContext internal_context;
305 * Data we keep per host.
315 * Lowest port that we have not yet used
321 struct TopologyIterateContext
324 * Callback for notifying of two connected peers.
326 GNUNET_TESTING_NotifyTopology topology_cb;
329 * Closure for topology_cb
334 * Number of peers currently connected to.
336 unsigned int connected;
339 * Number of peers we have finished iterating.
341 unsigned int completed;
344 * Number of peers total.
349 struct StatsIterateContext
352 * Continuation to call once all stats information has been retrieved.
354 GNUNET_STATISTICS_Callback cont;
357 * Proc function to call on each value received.
359 GNUNET_TESTING_STATISTICS_Iterator proc;
362 * Closure for topology_cb
367 * Number of peers currently connected to.
369 unsigned int connected;
372 * Number of peers we have finished iterating.
374 unsigned int completed;
377 * Number of peers total.
385 struct GNUNET_TESTING_Daemon *daemon;
388 struct StatsCoreContext
391 struct GNUNET_TESTING_Daemon *daemon;
393 * Handle to the statistics service.
395 struct GNUNET_STATISTICS_Handle *stats_handle;
398 * Handle for getting statistics.
400 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
404 * Handle to a group of GNUnet peers.
406 struct GNUNET_TESTING_PeerGroup
411 struct GNUNET_SCHEDULER_Handle *sched;
414 * Configuration template.
416 const struct GNUNET_CONFIGURATION_Handle *cfg;
419 * Function to call on each started daemon.
421 //GNUNET_TESTING_NotifyDaemonRunning cb;
429 * Function to call on each topology connection created
431 GNUNET_TESTING_NotifyConnection notify_connection;
434 * Callback for notify_connection
436 void *notify_connection_cls;
439 * NULL-terminated array of information about
442 struct HostData *hosts;
445 * Array of "total" peers.
447 struct PeerData *peers;
450 * Number of peers in this group.
455 * At what time should we fail the peer startup process?
457 struct GNUNET_TIME_Absolute max_timeout;
460 * How many peers are being started right now?
462 unsigned int starting;
465 * How many peers have already been started?
467 unsigned int started;
472 struct GNUNET_CONFIGURATION_Handle *ret;
473 const char *hostname;
479 struct ConnectContext
481 struct GNUNET_TESTING_Daemon *first;
483 struct GNUNET_TESTING_Daemon *second;
485 struct GNUNET_TESTING_PeerGroup *pg;
489 * Convert unique ID to hash code.
491 * @param uid unique ID to convert
492 * @param hash set to uid (extended with zeros)
495 hash_from_uid (uint32_t uid,
496 GNUNET_HashCode *hash)
498 memset (hash, 0, sizeof(GNUNET_HashCode));
499 *((uint32_t*)hash) = uid;
503 * Convert hash code to unique ID.
505 * @param uid unique ID to convert
506 * @param hash set to uid (extended with zeros)
509 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
511 memcpy (uid, hash, sizeof(uint32_t));
515 * Number of connects we are waiting on, allows us to rate limit
518 static int outstanding_connects;
521 * Get a topology from a string input.
523 * @param topology where to write the retrieved topology
524 * @param topology_string The string to attempt to
525 * get a configuration value from
526 * @return GNUNET_YES if topology string matched a
527 * known topology, GNUNET_NO if not
530 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string)
533 * Strings representing topologies in enum
535 static const char * topology_strings[] =
538 * A clique (everyone connected to everyone else).
543 * Small-world network (2d torus plus random links).
548 * Small-world network (ring plus random links).
568 * Certain percentage of peers are unable to communicate directly
569 * replicating NAT conditions
574 * Scale free topology.
579 * Straight line topology.
584 * All peers are disconnected.
592 if (topology_string == NULL)
594 while (topology_strings[curr] != NULL)
596 if (strcasecmp(topology_strings[curr], topology_string) == 0)
603 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
609 * Get connect topology option from string input.
611 * @param topology_option where to write the retrieved topology
612 * @param topology_string The string to attempt to
613 * get a configuration value from
614 * @return GNUNET_YES if string matched a known
615 * topology option, GNUNET_NO if not
618 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption *topology_option,
619 char * topology_string)
622 * Options for connecting a topology as strings.
624 static const char * topology_option_strings[] =
627 * Try to connect all peers specified in the topology.
632 * Choose a random subset of connections to create.
634 "CONNECT_RANDOM_SUBSET",
637 * Create at least X connections for each peer.
642 * Using a depth first search, create one connection
643 * per peer. If any are missed (graph disconnected)
644 * start over at those peers until all have at least one
650 * No options specified.
658 if (topology_string == NULL)
660 while (NULL != topology_option_strings[curr])
662 if (strcasecmp(topology_option_strings[curr], topology_string) == 0)
664 *topology_option = curr;
669 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
674 * Function to iterate over options. Copies
675 * the options to the target configuration,
676 * updating PORT values as needed.
679 * @param section name of the section
680 * @param option name of the option
681 * @param value value of the option
684 update_config (void *cls,
685 const char *section, const char *option, const char *value)
687 struct UpdateContext *ctx = cls;
692 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
696 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
701 if (0 == strcmp (option, "UNIXPATH"))
703 GNUNET_snprintf (uval,
705 "/tmp/test-service-%s-%u",
711 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
713 value = ctx->hostname;
716 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
721 * Create a new configuration using the given configuration
722 * as a template; however, each PORT in the existing cfg
723 * must be renumbered by incrementing "*port". If we run
724 * out of "*port" numbers, return NULL.
726 * @param cfg template configuration
727 * @param port port numbers to use, update to reflect
728 * port numbers that were used
729 * @param upnum number to make unix domain socket names unique
730 * @param hostname hostname of the controlling host, to allow control connections from
732 * @return new configuration, NULL on error
734 static struct GNUNET_CONFIGURATION_Handle *
735 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
738 const char *hostname)
740 struct UpdateContext uc;
748 uc.ret = GNUNET_CONFIGURATION_create ();
749 uc.hostname = hostname;
751 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
752 if (uc.nport >= HIGH_PORT)
755 GNUNET_CONFIGURATION_destroy (uc.ret);
759 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
761 if (hostname != NULL)
762 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1; %s;", control_host, hostname);
764 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
766 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
767 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport", "ACCEPT_FROM", allowed_hosts);
768 GNUNET_CONFIGURATION_set_value_string(uc.ret, "dht", "ACCEPT_FROM", allowed_hosts);
769 GNUNET_CONFIGURATION_set_value_string(uc.ret, "statistics", "ACCEPT_FROM", allowed_hosts);
770 GNUNET_free_non_null(control_host);
771 GNUNET_free(allowed_hosts);
775 /* arm needs to know to allow connections from the host on which it is running,
776 * otherwise gnunet-arm is unable to connect to it in some instances */
777 if (hostname != NULL)
779 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
780 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", hostname);
781 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", hostname);
782 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
783 GNUNET_free(allowed_hosts);
787 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", "127.0.0.1");
788 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", "127.0.0.1");
791 *port = (uint16_t) uc.nport;
798 * Add entries to the peers connect list
800 * @param pg the peer group we are working with
801 * @param first index of the first peer
802 * @param second index of the second peer
804 * @return the number of connections added
805 * technically should only be 0 or 2
809 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
815 GNUNET_HashCode hash_first;
816 GNUNET_HashCode hash_second;
818 hash_from_uid(first, &hash_first);
819 hash_from_uid(second, &hash_second);
821 add_first = GNUNET_NO;
822 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
824 add_first = GNUNET_YES;
827 add_second = GNUNET_NO;
828 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
830 add_second = GNUNET_YES;
836 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
837 pg->peers[first].num_connections++;
843 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
844 pg->peers[second].num_connections++;
853 * Add entries to the peers allowed connections list
855 * @param pg the peer group we are working with
856 * @param first index of the first peer
857 * @param second index of the second peer
859 * @return the number of connections added (can be 0, 1 or 2)
860 * technically should only be 0 or 2, but the small price
861 * of iterating over the lists (hashmaps in the future)
862 * for being sure doesn't bother me!
866 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
870 struct PeerConnection *first_iter;
871 struct PeerConnection *second_iter;
872 struct PeerConnection *new_first;
873 struct PeerConnection *new_second;
878 GNUNET_HashCode hash_first;
879 GNUNET_HashCode hash_second;
881 hash_from_uid(first, &hash_first);
882 hash_from_uid(second, &hash_second);
884 add_first = GNUNET_NO;
885 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
887 add_first = GNUNET_YES;
890 add_second = GNUNET_NO;
891 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
893 add_second = GNUNET_YES;
896 first_iter = pg->peers[first].connected_peers;
897 while (first_iter != NULL)
899 if (first_iter->daemon == pg->peers[second].daemon)
900 add_first = GNUNET_NO;
901 first_iter = first_iter->next;
904 second_iter = pg->peers[second].connected_peers;
905 add_second = GNUNET_YES;
906 while (second_iter != NULL)
908 if (second_iter->daemon == pg->peers[first].daemon)
909 add_second = GNUNET_NO;
910 second_iter = second_iter->next;
917 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
919 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
920 new_first->daemon = pg->peers[second].daemon;
921 new_first->next = pg->peers[first].connected_peers;
922 pg->peers[first].connected_peers = new_first;
924 pg->peers[first].num_connections++;
930 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
932 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
933 new_second->daemon = pg->peers[first].daemon;
934 new_second->next = pg->peers[second].connected_peers;
935 pg->peers[second].connected_peers = new_second;
936 pg->peers[first].num_connections++;
938 pg->peers[second].num_connections++;
946 * Add entries to the peers blacklisted list
948 * @param pg the peer group we are working with
949 * @param first index of the first peer
950 * @param second index of the second peer
952 * @return the number of connections added (can be 0, 1 or 2)
956 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
961 GNUNET_HashCode hash_first;
962 GNUNET_HashCode hash_second;
964 hash_from_uid(first, &hash_first);
965 hash_from_uid(second, &hash_second);
967 add_first = GNUNET_NO;
968 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
970 add_first = GNUNET_YES;
973 add_second = GNUNET_NO;
974 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
976 add_second = GNUNET_YES;
982 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
983 pg->peers[first].num_connections++;
989 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
990 pg->peers[second].num_connections++;
998 * Remove entries from the peers blacklisted list
1000 * @param pg the peer group we are working with
1001 * @param first index of the first peer
1002 * @param second index of the second peer
1004 * @return the number of connections removed (can be 0, 1 or 2)
1008 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1013 GNUNET_HashCode hash_first;
1014 GNUNET_HashCode hash_second;
1016 hash_from_uid(first, &hash_first);
1017 hash_from_uid(second, &hash_second);
1019 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
1020 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
1025 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
1031 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
1039 * Scale free network construction as described in:
1041 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1043 * Start with a network of "one" peer, then progressively add
1044 * peers up to the total number. At each step, iterate over
1045 * all possible peers and connect new peer based on number of
1046 * existing connections of the target peer.
1048 * @param pg the peer group we are dealing with
1049 * @param proc the connection processor to use
1051 * @return the number of connections created
1054 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1057 unsigned int total_connections;
1058 unsigned int outer_count;
1060 unsigned int previous_total_connections;
1064 GNUNET_assert(pg->total > 1);
1066 /* Add a connection between the first two nodes */
1067 total_connections = proc(pg, 0, 1);
1069 for (outer_count = 1; outer_count < pg->total; outer_count++)
1071 previous_total_connections = total_connections;
1072 for (i = 0; i < outer_count; i++)
1074 probability = pg->peers[i].num_connections / (double)previous_total_connections;
1075 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1076 UINT64_MAX)) / ( (double) UINT64_MAX);
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 "Considering connecting peer %d to peer %d\n",
1082 if (random < probability)
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 "Connecting peer %d to peer %d\n",
1089 total_connections += proc(pg, outer_count, i);
1094 return total_connections;
1098 * Create a topology given a peer group (set of running peers)
1099 * and a connection processor.
1101 * @param pg the peergroup to create the topology on
1102 * @param proc the connection processor to call to actually set
1103 * up connections between two peers
1105 * @return the number of connections that were set up
1109 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1113 unsigned int natLog;
1114 unsigned int randomPeer;
1115 double random, logNModifier, percentage;
1116 unsigned int smallWorldConnections;
1121 unsigned int useAnd;
1122 int connect_attempts;
1124 logNModifier = 0.5; /* FIXME: default value? */
1125 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1130 if (sscanf(p_string, "%lf", &logNModifier) != 1)
1131 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1132 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1136 GNUNET_free (p_string);
1138 percentage = 0.5; /* FIXME: default percentage? */
1139 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1144 if (sscanf(p_string, "%lf", &percentage) != 1)
1145 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1146 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1150 GNUNET_free (p_string);
1152 natLog = log (pg->total);
1153 connsPerPeer = ceil (natLog * logNModifier);
1155 if (connsPerPeer % 2 == 1)
1158 smallWorldConnections = 0;
1159 connect_attempts = 0;
1160 for (i = 0; i < pg->total; i++)
1163 max = i + connsPerPeer / 2;
1164 min = i - connsPerPeer / 2;
1166 if (max > pg->total - 1)
1168 max = max - pg->total;
1174 min = pg->total - 1 + min;
1178 for (j = 0; j < connsPerPeer / 2; j++)
1180 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1181 UINT64_MAX) / ( (double) UINT64_MAX));
1182 if (random < percentage)
1184 /* Connect to uniformly selected random peer */
1186 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1188 while ((((randomPeer < max) && (randomPeer > min))
1189 && (useAnd == 0)) || (((randomPeer > min)
1190 || (randomPeer < max))
1194 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1197 smallWorldConnections +=
1198 proc (pg, i, randomPeer);
1202 nodeToConnect = i + j + 1;
1203 if (nodeToConnect > pg->total - 1)
1205 nodeToConnect = nodeToConnect - pg->total;
1208 proc (pg, i, nodeToConnect);
1214 connect_attempts += smallWorldConnections;
1216 return connect_attempts;
1220 * Create a topology given a peer group (set of running peers)
1221 * and a connection processor.
1223 * @param pg the peergroup to create the topology on
1224 * @param proc the connection processor to call to actually set
1225 * up connections between two peers
1227 * @return the number of connections that were set up
1231 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1233 unsigned int outer_count, inner_count;
1234 unsigned int cutoff;
1235 int connect_attempts;
1236 double nat_percentage;
1239 nat_percentage = 0.6; /* FIXME: default percentage? */
1240 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1245 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1246 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1247 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1251 GNUNET_free (p_string);
1256 cutoff = (unsigned int) (nat_percentage * pg->total);
1258 connect_attempts = 0;
1260 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1262 for (inner_count = outer_count + 1; inner_count < pg->total;
1265 if ((outer_count > cutoff) || (inner_count > cutoff))
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269 "Connecting peer %d to peer %d\n",
1270 outer_count, inner_count);
1272 connect_attempts += proc(pg, outer_count, inner_count);
1277 return connect_attempts;
1282 * Create a topology given a peer group (set of running peers)
1283 * and a connection processor.
1285 * @param pg the peergroup to create the topology on
1286 * @param proc the connection processor to call to actually set
1287 * up connections between two peers
1289 * @return the number of connections that were set up
1293 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1295 unsigned int i, j, k;
1296 unsigned int square;
1299 unsigned int toggle = 1;
1300 unsigned int nodeToConnect;
1301 unsigned int natLog;
1302 unsigned int node1Row;
1303 unsigned int node1Col;
1304 unsigned int node2Row;
1305 unsigned int node2Col;
1306 unsigned int distance;
1307 double probability, random, percentage;
1308 unsigned int smallWorldConnections;
1310 int connect_attempts;
1311 square = floor (sqrt (pg->total));
1315 percentage = 0.5; /* FIXME: default percentage? */
1316 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1321 if (sscanf(p_string, "%lf", &percentage) != 1)
1322 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1323 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1327 GNUNET_free (p_string);
1329 probability = 0.5; /* FIXME: default percentage? */
1330 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1335 if (sscanf(p_string, "%lf", &probability) != 1)
1336 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1337 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1341 GNUNET_free (p_string);
1343 if (square * square != pg->total)
1345 while (rows * cols < pg->total)
1347 if (toggle % 2 == 0)
1356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1357 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1361 connect_attempts = 0;
1362 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1363 * to the node to its right and above. Once this is over, we'll have our torus!
1364 * Special case for the last node (if the rows and columns are not equal), connect
1365 * to the first in the row to maintain topology.
1367 for (i = 0; i < pg->total; i++)
1369 /* First connect to the node to the right */
1370 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1371 nodeToConnect = i + 1;
1372 else if (i + 1 == pg->total)
1373 nodeToConnect = rows * cols - cols;
1375 nodeToConnect = i - cols + 1;
1377 connect_attempts += proc (pg, i, nodeToConnect);
1380 nodeToConnect = (rows * cols) - cols + i;
1382 nodeToConnect = i - cols;
1384 if (nodeToConnect < pg->total)
1385 connect_attempts += proc (pg, i, nodeToConnect);
1387 natLog = log (pg->total);
1388 #if VERBOSE_TESTING > 2
1389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1390 _("natural log of %d is %d, will run %d iterations\n"),
1391 pg->total, natLog, (int) (natLog * percentage));
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1394 smallWorldConnections = 0;
1395 for (i = 0; i < (int) (natLog * percentage); i++)
1397 for (j = 0; j < pg->total; j++)
1399 /* Determine the row and column of node at position j on the 2d torus */
1400 node1Row = j / cols;
1401 node1Col = j - (node1Row * cols);
1402 for (k = 0; k < pg->total; k++)
1404 /* Determine the row and column of node at position k on the 2d torus */
1405 node2Row = k / cols;
1406 node2Col = k - (node2Row * cols);
1407 /* Simple Cartesian distance */
1408 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1411 /* Calculate probability as 1 over the square of the distance */
1412 probability = 1.0 / (distance * distance);
1413 /* Choose a random value between 0 and 1 */
1414 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1415 UINT64_MAX)) / ( (double) UINT64_MAX);
1416 /* If random < probability, then connect the two nodes */
1417 if (random < probability)
1418 smallWorldConnections += proc (pg, j, k);
1424 connect_attempts += smallWorldConnections;
1425 #if VERBOSE_TESTING > 2
1426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1427 _("Total connections added for small world: %d!\n"),
1428 smallWorldConnections);
1430 return connect_attempts;
1434 * Create a topology given a peer group (set of running peers)
1435 * and a connection processor.
1437 * @param pg the peergroup to create the topology on
1438 * @param proc the connection processor to call to actually set
1439 * up connections between two peers
1441 * @return the number of connections that were set up
1445 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1448 unsigned int outer_count;
1449 unsigned int inner_count;
1450 int connect_attempts;
1454 probability = 0.5; /* FIXME: default percentage? */
1455 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1460 if (sscanf(p_string, "%lf", &probability) != 1)
1461 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1462 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1466 GNUNET_free (p_string);
1468 connect_attempts = 0;
1469 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1471 for (inner_count = outer_count + 1; inner_count < pg->total;
1474 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1475 UINT64_MAX)) / ( (double) UINT64_MAX);
1477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1478 _("rand is %f probability is %f\n"), temp_rand,
1481 if (temp_rand < probability)
1483 connect_attempts += proc (pg, outer_count, inner_count);
1488 return connect_attempts;
1492 * Create a topology given a peer group (set of running peers)
1493 * and a connection processor.
1495 * @param pg the peergroup to create the topology on
1496 * @param proc the connection processor to call to actually set
1497 * up connections between two peers
1499 * @return the number of connections that were set up
1503 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1506 unsigned int square;
1509 unsigned int toggle = 1;
1510 unsigned int nodeToConnect;
1511 int connect_attempts;
1513 connect_attempts = 0;
1515 square = floor (sqrt (pg->total));
1519 if (square * square != pg->total)
1521 while (rows * cols < pg->total)
1523 if (toggle % 2 == 0)
1532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1533 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1536 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1537 * to the node to its right and above. Once this is over, we'll have our torus!
1538 * Special case for the last node (if the rows and columns are not equal), connect
1539 * to the first in the row to maintain topology.
1541 for (i = 0; i < pg->total; i++)
1543 /* First connect to the node to the right */
1544 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1545 nodeToConnect = i + 1;
1546 else if (i + 1 == pg->total)
1547 nodeToConnect = rows * cols - cols;
1549 nodeToConnect = i - cols + 1;
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "Connecting peer %d to peer %d\n",
1555 connect_attempts += proc(pg, i, nodeToConnect);
1557 /* Second connect to the node immediately above */
1559 nodeToConnect = (rows * cols) - cols + i;
1561 nodeToConnect = i - cols;
1563 if (nodeToConnect < pg->total)
1566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1567 "Connecting peer %d to peer %d\n",
1570 connect_attempts += proc(pg, i, nodeToConnect);
1575 return connect_attempts;
1580 * Create a topology given a peer group (set of running peers)
1581 * and a connection processor.
1583 * @param pg the peergroup to create the topology on
1584 * @param proc the connection processor to call to actually set
1585 * up connections between two peers
1587 * @return the number of connections that were set up
1591 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1593 unsigned int outer_count;
1594 unsigned int inner_count;
1595 int connect_attempts;
1597 connect_attempts = 0;
1599 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1601 for (inner_count = outer_count + 1; inner_count < pg->total;
1605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1606 "Connecting peer %d to peer %d\n",
1607 outer_count, inner_count);
1609 connect_attempts += proc(pg, outer_count, inner_count);
1613 return connect_attempts;
1617 * Create a topology given a peer group (set of running peers)
1618 * and a connection processor.
1620 * @param pg the peergroup to create the topology on
1621 * @param proc the connection processor to call to actually set
1622 * up connections between two peers
1624 * @return the number of connections that were set up
1628 create_line (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1631 int connect_attempts;
1633 connect_attempts = 0;
1635 /* Connect each peer to the next highest numbered peer */
1636 for (count = 0; count < pg->total - 1; count++)
1639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1640 "Connecting peer %d to peer %d\n",
1643 connect_attempts += proc(pg, count, count + 1);
1646 return connect_attempts;
1650 * Create a topology given a peer group (set of running peers)
1651 * and a connection processor.
1653 * @param pg the peergroup to create the topology on
1654 * @param proc the connection processor to call to actually set
1655 * up connections between two peers
1657 * @return the number of connections that were set up
1661 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1664 int connect_attempts;
1666 connect_attempts = 0;
1668 /* Connect each peer to the next highest numbered peer */
1669 for (count = 0; count < pg->total - 1; count++)
1672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1673 "Connecting peer %d to peer %d\n",
1676 connect_attempts += proc(pg, count, count + 1);
1679 /* Connect the last peer to the first peer */
1680 connect_attempts += proc(pg, pg->total - 1, 0);
1682 return connect_attempts;
1687 * Iterator for writing friends of a peer to a file.
1689 * @param cls closure, an open writable file handle
1690 * @param key the key the daemon was stored under
1691 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1693 * @return GNUNET_YES to continue iteration
1695 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1696 * with a single file_iterator that takes a closure which contains
1697 * the prefix to write before the peer. Then this could be used
1698 * for blacklisting multiple transports and writing the friend
1699 * file. I'm sure *someone* will complain loudly about other
1700 * things that negate these functions even existing so no point in
1704 friend_file_iterator (void *cls,
1705 const GNUNET_HashCode * key,
1708 FILE *temp_friend_handle = cls;
1709 struct GNUNET_TESTING_Daemon *peer = value;
1710 struct GNUNET_PeerIdentity *temppeer;
1711 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1713 temppeer = &peer->id;
1714 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1715 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1720 struct BlacklistContext
1723 * The (open) file handle to write to
1725 FILE *temp_file_handle;
1728 * The transport that this peer will be blacklisted on.
1734 * Iterator for writing blacklist data to appropriate files.
1736 * @param cls closure, an open writable file handle
1737 * @param key the key the daemon was stored under
1738 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1740 * @return GNUNET_YES to continue iteration
1743 blacklist_file_iterator (void *cls,
1744 const GNUNET_HashCode * key,
1747 struct BlacklistContext *blacklist_ctx = cls;
1748 //FILE *temp_blacklist_handle = cls;
1749 struct GNUNET_TESTING_Daemon *peer = value;
1750 struct GNUNET_PeerIdentity *temppeer;
1751 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1753 temppeer = &peer->id;
1754 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1755 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1761 * Create the friend files based on the PeerConnection's
1762 * of each peer in the peer group, and copy the files
1763 * to the appropriate place
1765 * @param pg the peer group we are dealing with
1768 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1770 FILE *temp_friend_handle;
1771 unsigned int pg_iter;
1772 char *temp_service_path;
1776 enum GNUNET_OS_ProcessStatusType type;
1777 unsigned long return_code;
1782 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1783 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1785 mytemp = GNUNET_DISK_mktemp("friends");
1786 GNUNET_assert(mytemp != NULL);
1787 temp_friend_handle = fopen (mytemp, "wt");
1788 GNUNET_assert(temp_friend_handle != NULL);
1789 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1790 fclose(temp_friend_handle);
1793 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1795 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1796 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1799 if (UNLINK (mytemp) != 0)
1800 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1801 GNUNET_free (mytemp);
1805 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1807 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1808 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1809 "mv", mytemp, arg, NULL);
1811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1812 _("Copying file with command cp %s %s\n"), mytemp, arg);
1817 else /* Remote, scp the file to the correct place */
1819 if (NULL != pg->peers[pg_iter].daemon->username)
1820 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1822 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1823 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1824 "scp", mytemp, arg, NULL);
1827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1828 _("Copying file with command scp %s %s\n"), mytemp, arg);
1832 GNUNET_free (temp_service_path);
1833 GNUNET_free (mytemp);
1837 ret = GNUNET_SYSERR;
1838 while ((count < max_wait) && (ret != GNUNET_OK))
1841 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1845 _("Checking copy status of file %d\n"), pg_iter);
1847 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1849 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1851 ret = GNUNET_SYSERR;
1853 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1855 ret = GNUNET_SYSERR;
1859 pidarr[pg_iter] = 0;
1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1862 _("File %d copied\n"), pg_iter);
1868 if (ret == GNUNET_SYSERR)
1870 /* FIXME: why sleep here? -CG */
1876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1877 _("Finished copying all friend files!\n"));
1879 GNUNET_free(pidarr);
1885 * Create the blacklist files based on the PeerConnection's
1886 * of each peer in the peer group, and copy the files
1887 * to the appropriate place.
1889 * @param pg the peer group we are dealing with
1890 * @param transports space delimited list of transports to blacklist
1893 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
1895 FILE *temp_file_handle;
1896 static struct BlacklistContext blacklist_ctx;
1897 unsigned int pg_iter;
1898 char *temp_service_path;
1902 enum GNUNET_OS_ProcessStatusType type;
1903 unsigned long return_code;
1910 char *temp_transports;
1912 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1913 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1915 mytemp = GNUNET_DISK_mktemp("blacklist");
1916 GNUNET_assert(mytemp != NULL);
1917 temp_file_handle = fopen (mytemp, "wt");
1918 GNUNET_assert(temp_file_handle != NULL);
1919 temp_transports = GNUNET_strdup(transports);
1920 blacklist_ctx.temp_file_handle = temp_file_handle;
1921 transport_len = strlen(temp_transports) + 1;
1924 for (i = 0; i < transport_len; i++)
1926 if ((temp_transports[i] == ' ') && (pos == NULL))
1927 continue; /* At start of string (whitespace) */
1928 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
1930 temp_transports[i] = '\0';
1931 blacklist_ctx.transport = pos;
1932 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
1934 } /* At beginning of actual string */
1935 else if (pos == NULL)
1937 pos = &temp_transports[i];
1941 GNUNET_free (temp_transports);
1942 fclose(temp_file_handle);
1945 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1947 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1948 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1951 if (UNLINK (mytemp) != 0)
1952 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1953 GNUNET_free (mytemp);
1957 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1959 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
1960 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1961 "mv", mytemp, arg, NULL);
1963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1964 _("Copying file with command cp %s %s\n"), mytemp, arg);
1969 else /* Remote, scp the file to the correct place */
1971 if (NULL != pg->peers[pg_iter].daemon->username)
1972 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1974 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1975 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1976 "scp", mytemp, arg, NULL);
1979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1980 _("Copying file with command scp %s %s\n"), mytemp, arg);
1984 GNUNET_free (temp_service_path);
1985 GNUNET_free (mytemp);
1989 ret = GNUNET_SYSERR;
1990 while ((count < max_wait) && (ret != GNUNET_OK))
1993 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1997 _("Checking copy status of file %d\n"), pg_iter);
1999 if (pidarr[pg_iter] != 0) /* Check for already completed! */
2001 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
2003 ret = GNUNET_SYSERR;
2005 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
2007 ret = GNUNET_SYSERR;
2011 pidarr[pg_iter] = 0;
2013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2014 _("File %d copied\n"), pg_iter);
2020 if (ret == GNUNET_SYSERR)
2022 /* FIXME: why sleep here? -CG */
2028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2029 _("Finished copying all blacklist files!\n"));
2031 GNUNET_free(pidarr);
2037 * Internal notification of a connection, kept so that we can ensure some connections
2038 * happen instead of flooding all testing daemons with requests to connect.
2040 static void internal_connect_notify (void *cls,
2041 const struct GNUNET_PeerIdentity *first,
2042 const struct GNUNET_PeerIdentity *second,
2044 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2045 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2046 struct GNUNET_TESTING_Daemon *first_daemon,
2047 struct GNUNET_TESTING_Daemon *second_daemon,
2050 struct GNUNET_TESTING_PeerGroup *pg = cls;
2051 outstanding_connects--;
2053 pg->notify_connection(pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
2058 * Either delay a connection (because there are too many outstanding)
2059 * or schedule it for right now.
2061 * @param cls a connection context
2062 * @param tc the task runtime context
2064 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2066 struct ConnectContext *connect_context = cls;
2068 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2071 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2073 #if VERBOSE_TESTING > 2
2074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2075 _("Delaying connect, we have too many outstanding connections!\n"));
2077 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_connect, connect_context);
2081 #if VERBOSE_TESTING > 2
2082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2083 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
2085 outstanding_connects++;
2086 GNUNET_TESTING_daemons_connect (connect_context->first,
2087 connect_context->second,
2090 &internal_connect_notify,
2091 connect_context->pg);
2092 GNUNET_free(connect_context);
2098 * Iterator for actually scheduling connections to be created
2099 * between two peers.
2101 * @param cls closure, a GNUNET_TESTING_Daemon
2102 * @param key the key the second Daemon was stored under
2103 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2105 * @return GNUNET_YES to continue iteration
2108 connect_iterator (void *cls,
2109 const GNUNET_HashCode * key,
2112 struct PeerData *first = cls;
2113 struct GNUNET_TESTING_Daemon *second = value;
2114 struct ConnectContext *connect_context;
2116 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2117 connect_context->pg = first->pg;
2118 connect_context->first = first->daemon;
2119 connect_context->second = second;
2120 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
2127 * Iterator for copying all entries in the allowed hashmap to the
2130 * @param cls closure, a GNUNET_TESTING_Daemon
2131 * @param key the key the second Daemon was stored under
2132 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2134 * @return GNUNET_YES to continue iteration
2137 copy_topology_iterator (void *cls,
2138 const GNUNET_HashCode * key,
2141 struct PeerData *first = cls;
2143 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2149 * Make the peers to connect the same as those that are allowed to be
2152 * @param pg the peer group
2155 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2157 unsigned int pg_iter;
2162 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2164 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
2165 if (GNUNET_SYSERR == ret)
2166 return GNUNET_SYSERR;
2168 total = total + ret;
2176 * Connect the topology as specified by the PeerConnection's
2177 * of each peer in the peer group
2179 * @param pg the peer group we are dealing with
2180 * @return the number of connections that will be attempted
2183 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
2185 unsigned int pg_iter;
2189 struct PeerConnection *connection_iter;
2190 struct ConnectContext *connect_context;
2194 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2196 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
2197 if (GNUNET_SYSERR == ret)
2198 return GNUNET_SYSERR;
2200 total = total + ret;
2204 while (connection_iter != NULL)
2206 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2207 connect_context->pg = pg;
2208 connect_context->first = ;
2209 connect_context->second = connection_iter->daemon;
2210 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
2211 connection_iter = connection_iter->next;
2220 * Takes a peer group and creates a topology based on the
2221 * one specified. Creates a topology means generates friend
2222 * files for the peers so they can only connect to those allowed
2223 * by the topology. This will only have an effect once peers
2224 * are started if the FRIENDS_ONLY option is set in the base
2225 * config. Also takes an optional restrict topology which
2226 * disallows connections based on a particular transport
2227 * UNLESS they are specified in the restricted topology.
2229 * @param pg the peer group struct representing the running peers
2230 * @param topology which topology to connect the peers in
2231 * @param restrict_topology allow only direct TCP connections in this topology
2232 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2233 * @param restrict_transports space delimited list of transports to blacklist
2234 * to create restricted topology
2236 * @return the maximum number of connections were all allowed peers
2237 * connected to each other
2240 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2241 enum GNUNET_TESTING_Topology topology,
2242 enum GNUNET_TESTING_Topology restrict_topology,
2243 char *restrict_transports)
2246 int num_connections;
2247 int unblacklisted_connections;
2249 GNUNET_assert (pg->notify_connection != NULL);
2254 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2257 _("Creating clique topology\n"));
2259 num_connections = create_clique (pg, &add_allowed_connections);
2261 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2264 _("Creating small world (ring) topology\n"));
2266 num_connections = create_small_world_ring (pg, &add_allowed_connections);
2268 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2271 _("Creating small world (2d-torus) topology\n"));
2273 num_connections = create_small_world (pg, &add_allowed_connections);
2275 case GNUNET_TESTING_TOPOLOGY_RING:
2277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2278 _("Creating ring topology\n"));
2280 num_connections = create_ring (pg, &add_allowed_connections);
2282 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2285 _("Creating 2d torus topology\n"));
2287 num_connections = create_2d_torus (pg, &add_allowed_connections);
2289 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2292 _("Creating Erdos-Renyi topology\n"));
2294 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2296 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2299 _("Creating InterNAT topology\n"));
2301 num_connections = create_nated_internet (pg, &add_allowed_connections);
2303 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2306 _("Creating Scale Free topology\n"));
2308 num_connections = create_scale_free (pg, &add_allowed_connections);
2310 case GNUNET_TESTING_TOPOLOGY_LINE:
2312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2313 _("Creating straight line topology\n"));
2315 num_connections = create_line (pg, &add_allowed_connections);
2317 case GNUNET_TESTING_TOPOLOGY_NONE:
2319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2320 _("Creating no allowed topology (all peers can connect at core level)\n"));
2322 num_connections = 0;
2325 num_connections = 0;
2329 if (num_connections < 0)
2330 return GNUNET_SYSERR;
2332 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2334 ret = create_and_copy_friend_files(pg);
2335 if (ret != GNUNET_OK)
2338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2339 _("Failed during friend file copying!\n"));
2341 return GNUNET_SYSERR;
2346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2347 _("Friend files created/copied successfully!\n"));
2352 /* Use the create clique method to initially set all connections as blacklisted. */
2353 if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2354 create_clique (pg, &blacklist_connections);
2355 unblacklisted_connections = 0;
2356 /* Un-blacklist connections as per the topology specified */
2357 switch (restrict_topology)
2359 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2362 _("Blacklisting all but clique topology\n"));
2364 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2366 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2369 _("Blacklisting all but small world (ring) topology\n"));
2371 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2373 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2376 _("Blacklisting all but small world (2d-torus) topology\n"));
2378 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2380 case GNUNET_TESTING_TOPOLOGY_RING:
2382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2383 _("Blacklisting all but ring topology\n"));
2385 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2387 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2390 _("Blacklisting all but 2d torus topology\n"));
2392 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2394 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2397 _("Blacklisting all but Erdos-Renyi topology\n"));
2399 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2401 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2404 _("Blacklisting all but InterNAT topology\n"));
2406 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2408 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2411 _("Blacklisting all but Scale Free topology\n"));
2413 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2415 case GNUNET_TESTING_TOPOLOGY_LINE:
2417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2418 _("Blacklisting all but straight line topology\n"));
2420 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2422 case GNUNET_TESTING_TOPOLOGY_NONE:
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2425 _("Creating no blacklist topology (all peers can connect at transport level)\n"));
2431 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2433 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2434 if (ret != GNUNET_OK)
2437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2438 _("Failed during blacklist file copying!\n"));
2440 return GNUNET_SYSERR;
2445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446 _("Blacklist files created/copied successfully!\n"));
2450 return num_connections;
2453 struct RandomContext
2458 struct GNUNET_TESTING_PeerGroup *pg;
2461 * uid of the first peer
2466 * Peer data for first peer.
2468 struct PeerData *first;
2471 * Random percentage to use
2476 struct MinimumContext
2481 struct GNUNET_TESTING_PeerGroup *pg;
2484 * uid of the first peer
2489 * Peer data for first peer.
2491 struct PeerData *first;
2494 * Number of conns per peer
2496 unsigned int num_to_add;
2499 * Permuted array of all possible connections. Only add the Nth
2500 * peer if it's in the Nth position.
2502 unsigned int *pg_array;
2505 * What number is the current element we are iterating over?
2507 unsigned int current;
2515 struct GNUNET_TESTING_PeerGroup *pg;
2518 * uid of the first peer
2523 * uid of the second peer
2525 uint32_t second_uid;
2528 * Peer data for first peer.
2530 struct PeerData *first;
2533 * Which peer has been chosen as the one to add?
2535 unsigned int chosen;
2538 * What number is the current element we are iterating over?
2540 unsigned int current;
2544 * Iterator for choosing random peers to connect.
2546 * @param cls closure, a RandomContext
2547 * @param key the key the second Daemon was stored under
2548 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2550 * @return GNUNET_YES to continue iteration
2553 random_connect_iterator (void *cls,
2554 const GNUNET_HashCode * key,
2557 struct RandomContext *random_ctx = cls;
2558 double random_number;
2559 uint32_t second_pos;
2560 GNUNET_HashCode first_hash;
2561 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2562 UINT64_MAX)) / ( (double) UINT64_MAX);
2563 if (random_number < random_ctx->percentage)
2565 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2567 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2568 uid_from_hash(key, &second_pos);
2569 hash_from_uid(random_ctx->first_uid, &first_hash);
2570 GNUNET_assert(random_ctx->pg->total > second_pos);
2571 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2577 * Iterator for adding at least X peers to a peers connection set.
2579 * @param cls closure, MinimumContext
2580 * @param key the key the second Daemon was stored under
2581 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2583 * @return GNUNET_YES to continue iteration
2586 minimum_connect_iterator (void *cls,
2587 const GNUNET_HashCode * key,
2590 struct MinimumContext *min_ctx = cls;
2591 uint32_t second_pos;
2592 GNUNET_HashCode first_hash;
2595 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2597 for (i = 0; i < min_ctx->num_to_add; i++)
2599 if (min_ctx->pg_array[i] == min_ctx->current)
2601 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2602 uid_from_hash(key, &second_pos);
2603 hash_from_uid(min_ctx->first_uid, &first_hash);
2604 GNUNET_assert(min_ctx->pg->total > second_pos);
2605 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));
2606 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2607 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2614 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2620 * Iterator for adding peers to a connection set based on a depth first search.
2622 * @param cls closure, MinimumContext
2623 * @param key the key the second daemon was stored under
2624 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2626 * @return GNUNET_YES to continue iteration
2629 dfs_connect_iterator (void *cls,
2630 const GNUNET_HashCode * key,
2633 struct DFSContext *dfs_ctx = cls;
2634 GNUNET_HashCode first_hash;
2636 if (dfs_ctx->current == dfs_ctx->chosen)
2638 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2639 uid_from_hash(key, &dfs_ctx->second_uid);
2640 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2641 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));
2642 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2643 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2644 return GNUNET_NO; /* We have found our peer, don't iterate more */
2653 * From the set of connections possible, choose percentage percent of connections
2654 * to actually connect.
2656 * @param pg the peergroup we are dealing with
2657 * @param percentage what percent of total connections to make
2660 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2662 struct RandomContext random_ctx;
2665 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2667 random_ctx.first_uid = pg_iter;
2668 random_ctx.first = &pg->peers[pg_iter];
2669 random_ctx.percentage = percentage;
2671 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2672 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2673 /* Now remove the old connections */
2674 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2675 /* And replace with the random set */
2676 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2681 * From the set of connections possible, choose at least num connections per
2684 * @param pg the peergroup we are dealing with
2685 * @param num how many connections at least should each peer have (if possible)?
2688 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2690 struct MinimumContext minimum_ctx;
2693 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2695 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2698 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2700 minimum_ctx.first_uid = pg_iter;
2701 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2702 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2703 minimum_ctx.first = &pg->peers[pg_iter];
2704 minimum_ctx.pg = pg;
2705 minimum_ctx.num_to_add = num;
2706 minimum_ctx.current = 0;
2707 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2708 &minimum_connect_iterator,
2712 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2714 /* Remove the "old" connections */
2715 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2716 /* And replace with the working set */
2717 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2724 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2727 unsigned int pg_iter;
2731 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2733 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2740 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2743 unsigned int pg_iter;
2747 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2749 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2756 * From the set of connections possible, choose at least num connections per
2757 * peer based on depth first traversal of peer connections. If DFS leaves
2758 * peers unconnected, ensure those peers get connections.
2760 * @param pg the peergroup we are dealing with
2761 * @param num how many connections at least should each peer have (if possible)?
2764 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2766 struct DFSContext dfs_ctx;
2769 uint32_t starting_peer;
2770 uint32_t least_connections;
2771 GNUNET_HashCode second_hash;
2773 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2775 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2780 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
2782 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
2784 least_connections = -1; /* Set to very high number */
2785 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2787 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
2789 starting_peer = pg_iter;
2790 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2795 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
2801 /* Choose a random peer from the chosen peers set of connections to add */
2802 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
2803 dfs_ctx.first_uid = starting_peer;
2804 dfs_ctx.first = &pg->peers[starting_peer];
2806 dfs_ctx.current = 0;
2808 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
2809 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
2810 hash_from_uid(dfs_ctx.second_uid, &second_hash);
2811 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
2812 starting_peer = dfs_ctx.second_uid;
2815 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2817 /* Remove the "old" connections */
2818 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2819 /* And replace with the working set */
2820 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2825 * Internal callback for topology information for a particular peer.
2828 internal_topology_callback(void *cls,
2829 const struct GNUNET_PeerIdentity *peer,
2830 struct GNUNET_TIME_Relative latency, uint32_t distance)
2832 struct CoreContext *core_ctx = cls;
2833 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
2835 if (peer == NULL) /* Either finished, or something went wrong */
2837 iter_ctx->completed++;
2838 iter_ctx->connected--;
2839 /* One core context allocated per iteration, must free! */
2840 GNUNET_free(core_ctx);
2844 iter_ctx->topology_cb(iter_ctx->cls, &core_ctx->daemon->id, peer, latency, distance, NULL);
2847 if (iter_ctx->completed == iter_ctx->total)
2849 iter_ctx->topology_cb(iter_ctx->cls, NULL, NULL, GNUNET_TIME_relative_get_zero(), 0, NULL);
2850 /* Once all are done, free the iteration context */
2851 GNUNET_free(iter_ctx);
2857 * Check running topology iteration tasks, if below max start a new one, otherwise
2858 * schedule for some time in the future.
2861 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2863 struct CoreContext *core_context = cls;
2864 struct TopologyIterateContext *topology_context = (struct TopologyIterateContext *)core_context->iter_context;
2865 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2868 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
2870 #if VERBOSE_TESTING > 2
2871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2872 _("Delaying connect, we have too many outstanding connections!\n"));
2874 GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_topology, core_context);
2878 #if VERBOSE_TESTING > 2
2879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2880 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
2882 topology_context->connected++;
2883 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->sched, core_context->daemon->cfg, &internal_topology_callback, core_context))
2884 internal_topology_callback(core_context, NULL, GNUNET_TIME_relative_get_zero(), 0);
2890 * Iterate over all (running) peers in the peer group, retrieve
2891 * all connections that each currently has.
2894 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyTopology cb, void *cls)
2896 struct TopologyIterateContext *topology_context;
2897 struct CoreContext *core_ctx;
2899 unsigned int total_count;
2901 /* Allocate a single topology iteration context */
2902 topology_context = GNUNET_malloc(sizeof(struct TopologyIterateContext));
2903 topology_context->topology_cb = cb;
2904 topology_context->cls = cls;
2906 for (i = 0; i < pg->total; i++)
2908 if (pg->peers[i].daemon->running == GNUNET_YES)
2910 /* Allocate one core context per core we need to connect to */
2911 core_ctx = GNUNET_malloc(sizeof(struct CoreContext));
2912 core_ctx->daemon = pg->peers[i].daemon;
2913 /* Set back pointer to topology iteration context */
2914 core_ctx->iter_context = topology_context;
2915 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_topology, core_ctx);
2919 topology_context->total = total_count;
2924 * Callback function to process statistic values.
2925 * This handler is here only really to insert a peer
2926 * identity (or daemon) so the statistics can be uniquely
2927 * tied to a single running peer.
2929 * @param cls closure
2930 * @param subsystem name of subsystem that created the statistic
2931 * @param name the name of the datum
2932 * @param value the current value
2933 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
2934 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
2936 static int internal_stats_callback (void *cls,
2937 const char *subsystem,
2942 struct StatsCoreContext *core_context = cls;
2943 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
2945 return stats_context->proc(stats_context->cls, &core_context->daemon->id, subsystem, name, value, is_persistent);
2949 * Internal continuation call for statistics iteration.
2951 * @param cls closure, the CoreContext for this iteration
2952 * @param success whether or not the statistics iterations
2953 * was canceled or not (we don't care)
2955 static void internal_stats_cont (void *cls, int success)
2957 struct StatsCoreContext *core_context = cls;
2958 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
2960 stats_context->connected--;
2961 stats_context->completed++;
2963 if (stats_context->completed == stats_context->total)
2965 stats_context->cont(stats_context->cls, GNUNET_YES);
2966 GNUNET_free(stats_context);
2969 if (core_context->stats_handle != NULL)
2970 GNUNET_STATISTICS_destroy(core_context->stats_handle, GNUNET_NO);
2972 GNUNET_free(core_context);
2976 * Check running topology iteration tasks, if below max start a new one, otherwise
2977 * schedule for some time in the future.
2980 schedule_get_statistics(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2982 struct StatsCoreContext *core_context = cls;
2983 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
2985 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2988 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
2990 #if VERBOSE_TESTING > 2
2991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2992 _("Delaying connect, we have too many outstanding connections!\n"));
2994 GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_statistics, core_context);
2998 #if VERBOSE_TESTING > 2
2999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3000 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3003 stats_context->connected++;
3004 core_context->stats_handle = GNUNET_STATISTICS_create(core_context->daemon->sched, "testing", core_context->daemon->cfg);
3005 if (core_context->stats_handle == NULL)
3007 internal_stats_cont (core_context, GNUNET_NO);
3011 core_context->stats_get_handle = GNUNET_STATISTICS_get(core_context->stats_handle, NULL, NULL, GNUNET_TIME_relative_get_forever(), &internal_stats_cont, &internal_stats_callback, core_context);
3012 if (core_context->stats_get_handle == NULL)
3013 internal_stats_cont (core_context, GNUNET_NO);
3020 * Iterate over all (running) peers in the peer group, retrieve
3021 * all statistics from each.
3024 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
3025 GNUNET_STATISTICS_Callback cont,
3026 GNUNET_TESTING_STATISTICS_Iterator proc, void *cls)
3028 struct StatsIterateContext *stats_context;
3029 struct StatsCoreContext *core_ctx;
3031 unsigned int total_count;
3033 /* Allocate a single stats iteration context */
3034 stats_context = GNUNET_malloc(sizeof(struct StatsIterateContext));
3035 stats_context->cont = cont;
3036 stats_context->proc = proc;
3037 stats_context->cls = cls;
3039 for (i = 0; i < pg->total; i++)
3041 if (pg->peers[i].daemon->running == GNUNET_YES)
3043 /* Allocate one core context per core we need to connect to */
3044 core_ctx = GNUNET_malloc(sizeof(struct StatsCoreContext));
3045 core_ctx->daemon = pg->peers[i].daemon;
3046 /* Set back pointer to topology iteration context */
3047 core_ctx->iter_context = stats_context;
3048 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_statistics, core_ctx);
3052 stats_context->total = total_count;
3057 * There are many ways to connect peers that are supported by this function.
3058 * To connect peers in the same topology that was created via the
3059 * GNUNET_TESTING_create_topology, the topology variable must be set to
3060 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
3061 * a new instance of that topology will be generated and attempted to be
3062 * connected. This could result in some connections being impossible,
3063 * because some topologies are non-deterministic.
3065 * @param pg the peer group struct representing the running peers
3066 * @param topology which topology to connect the peers in
3067 * @param options options for connecting the topology
3068 * @param option_modifier modifier for options that take a parameter
3069 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
3072 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3073 enum GNUNET_TESTING_Topology topology,
3074 enum GNUNET_TESTING_TopologyOption options,
3075 double option_modifier)
3079 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3080 #if VERBOSE_TOPOLOGY
3081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3082 _("Creating clique CONNECT topology\n"));
3084 create_clique (pg, &add_actual_connections);
3086 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3087 #if VERBOSE_TOPOLOGY
3088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3089 _("Creating small world (ring) CONNECT topology\n"));
3091 create_small_world_ring (pg, &add_actual_connections);
3093 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3094 #if VERBOSE_TOPOLOGY
3095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3096 _("Creating small world (2d-torus) CONNECT topology\n"));
3098 create_small_world (pg, &add_actual_connections);
3100 case GNUNET_TESTING_TOPOLOGY_RING:
3101 #if VERBOSE_TOPOLOGY
3102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3103 _("Creating ring CONNECT topology\n"));
3105 create_ring (pg, &add_actual_connections);
3107 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3108 #if VERBOSE_TOPOLOGY
3109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3110 _("Creating 2d torus CONNECT topology\n"));
3112 create_2d_torus (pg, &add_actual_connections);
3114 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3115 #if VERBOSE_TOPOLOGY
3116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3117 _("Creating Erdos-Renyi CONNECT topology\n"));
3119 create_erdos_renyi (pg, &add_actual_connections);
3121 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3122 #if VERBOSE_TOPOLOGY
3123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3124 _("Creating InterNAT CONNECT topology\n"));
3126 create_nated_internet (pg, &add_actual_connections);
3128 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3129 #if VERBOSE_TOPOLOGY
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3131 _("Creating Scale Free CONNECT topology\n"));
3133 create_scale_free (pg, &add_actual_connections);
3135 case GNUNET_TESTING_TOPOLOGY_LINE:
3136 #if VERBOSE_TOPOLOGY
3137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3138 _("Creating straight line CONNECT topology\n"));
3140 create_line (pg, &add_actual_connections);
3142 case GNUNET_TESTING_TOPOLOGY_NONE:
3143 #if VERBOSE_TOPOLOGY
3144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3145 _("Creating no CONNECT topology\n"));
3147 copy_allowed_topology(pg);
3150 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3151 _("Unknown topology specification, can't connect peers!\n"));
3152 return GNUNET_SYSERR;
3157 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
3158 #if VERBOSE_TOPOLOGY
3159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3160 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
3162 choose_random_connections(pg, option_modifier);
3164 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
3165 #if VERBOSE_TOPOLOGY
3166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3167 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3169 choose_minimum(pg, (unsigned int)option_modifier);
3171 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
3172 #if VERBOSE_TOPOLOGY
3173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3174 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3176 perform_dfs(pg, (int)option_modifier);
3178 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
3180 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
3186 return connect_topology(pg);
3190 * Callback that is called whenever a hostkey is generated
3191 * for a peer. Call the real callback and decrement the
3192 * starting counter for the peergroup.
3194 * @param cls closure
3195 * @param id identifier for the daemon, NULL on error
3196 * @param d handle for the daemon
3197 * @param emsg error message (NULL on success)
3199 static void internal_hostkey_callback (void *cls,
3200 const struct GNUNET_PeerIdentity *id,
3201 struct GNUNET_TESTING_Daemon *d,
3204 struct InternalStartContext *internal_context = cls;
3205 internal_context->peer->pg->starting--;
3206 internal_context->peer->pg->started++;
3207 if (internal_context->hostkey_callback != NULL)
3208 internal_context->hostkey_callback(internal_context->hostkey_cls, id, d, emsg);
3209 else if (internal_context->peer->pg->started == internal_context->peer->pg->total)
3211 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
3212 GNUNET_TESTING_daemons_continue_startup(internal_context->peer->pg);
3217 * Callback that is called whenever a peer has finished starting.
3218 * Call the real callback and decrement the starting counter
3219 * for the peergroup.
3221 * @param cls closure
3222 * @param id identifier for the daemon, NULL on error
3223 * @param d handle for the daemon
3224 * @param emsg error message (NULL on success)
3226 static void internal_startup_callback (void *cls,
3227 const struct GNUNET_PeerIdentity *id,
3228 const struct GNUNET_CONFIGURATION_Handle *cfg,
3229 struct GNUNET_TESTING_Daemon *d,
3232 struct InternalStartContext *internal_context = cls;
3233 internal_context->peer->pg->starting--;
3234 if (internal_context->start_cb != NULL)
3235 internal_context->start_cb(internal_context->start_cb_cls, id, cfg, d, emsg);
3239 internal_continue_startup (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3241 struct InternalStartContext *internal_context = cls;
3243 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3248 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
3250 internal_context->peer->pg->starting++;
3251 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
3255 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_continue_startup, internal_context);
3260 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3262 struct InternalStartContext *internal_context = cls;
3264 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3269 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
3271 internal_context->peer->pg->starting++;
3272 internal_context->peer->daemon = GNUNET_TESTING_daemon_start (internal_context->peer->pg->sched,
3273 internal_context->peer->cfg,
3274 internal_context->timeout,
3275 internal_context->hostname,
3276 &internal_hostkey_callback,
3278 &internal_startup_callback,
3283 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_start, internal_context);
3288 * Function which continues a peer group starting up
3289 * after successfully generating hostkeys for each peer.
3291 * @param pg the peer group to continue starting
3295 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
3300 for (i = 0; i < pg->total; i++)
3302 GNUNET_SCHEDULER_add_now (pg->sched, &internal_continue_startup, &pg->peers[i].internal_context);
3303 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
3308 * Start count gnunet instances with the same set of transports and
3309 * applications. The port numbers (any option called "PORT") will be
3310 * adjusted to ensure that no two peers running on the same system
3311 * have the same port(s) in their respective configurations.
3313 * @param sched scheduler to use
3314 * @param cfg configuration template to use
3315 * @param total number of daemons to start
3316 * @param timeout total time allowed for peers to start
3317 * @param hostkey_callback function to call on each peers hostkey generation
3318 * if NULL, peers will be started by this call, if non-null,
3319 * GNUNET_TESTING_daemons_continue_startup must be called after
3320 * successful hostkey generation
3321 * @param hostkey_cls closure for hostkey callback
3322 * @param cb function to call on each daemon that was started
3323 * @param cb_cls closure for cb
3324 * @param connect_callback function to call each time two hosts are connected
3325 * @param connect_callback_cls closure for connect_callback
3326 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
3328 * @return NULL on error, otherwise handle to control peer group
3330 struct GNUNET_TESTING_PeerGroup *
3331 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
3332 const struct GNUNET_CONFIGURATION_Handle *cfg,
3334 struct GNUNET_TIME_Relative timeout,
3335 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
3337 GNUNET_TESTING_NotifyDaemonRunning cb,
3339 GNUNET_TESTING_NotifyConnection
3340 connect_callback, void *connect_callback_cls,
3341 const struct GNUNET_TESTING_Host *hostnames)
3343 struct GNUNET_TESTING_PeerGroup *pg;
3344 const struct GNUNET_TESTING_Host *hostpos;
3350 const char *hostname;
3351 char *baseservicehome;
3352 char *newservicehome;
3354 struct GNUNET_CONFIGURATION_Handle *pcfg;
3356 unsigned int hostcnt;
3366 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
3369 pg->notify_connection = connect_callback;
3370 pg->notify_connection_cls = connect_callback_cls;
3372 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
3373 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
3374 if (NULL != hostnames)
3377 hostpos = hostnames;
3378 while (hostpos != NULL)
3380 hostpos = hostpos->next;
3383 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3386 hostpos = hostnames;
3387 while (hostpos != NULL)
3389 pg->hosts[off].minport = LOW_PORT;
3390 pg->hosts[off++].hostname = GNUNET_strdup(hostpos->hostname);
3391 hostpos = hostpos->next;
3396 GNUNET_free (pg->hosts);
3404 /* skip leading spaces */
3405 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
3408 while ('\0' != *rpos)
3410 if (isspace ( (unsigned char) *rpos))
3414 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3416 start = GNUNET_strdup (hostnames);
3418 while ('\0' != *pos)
3420 if (isspace ( (unsigned char) *pos))
3423 if (strlen (start) > 0)
3425 pg->hosts[off].minport = LOW_PORT;
3426 pg->hosts[off++].hostname = start;
3432 if (strlen (start) > 0)
3434 pg->hosts[off].minport = LOW_PORT;
3435 pg->hosts[off++].hostname = start;
3439 GNUNET_free (start);
3440 GNUNET_free (pg->hosts);
3444 minport = 0; /* make gcc happy */
3452 for (off = 0; off < total; off++)
3456 hostname = pg->hosts[off % hostcnt].hostname;
3457 pcfg = make_config (cfg,
3458 &pg->hosts[off % hostcnt].minport,
3465 pcfg = make_config (cfg,
3473 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3475 ("Could not create configuration for peer number %u on `%s'!\n"),
3476 off, hostname == NULL ? "localhost" : hostname);
3481 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
3484 GNUNET_asprintf (&newservicehome,
3485 "%s/%d/", baseservicehome, off);
3486 GNUNET_free (baseservicehome);
3490 tmpdir = getenv ("TMPDIR");
3491 tmpdir = tmpdir ? tmpdir : "/tmp";
3492 GNUNET_asprintf (&newservicehome,
3495 "gnunet-testing-test-test", off);
3497 GNUNET_CONFIGURATION_set_value_string (pcfg,
3499 "SERVICEHOME", newservicehome);
3500 GNUNET_free (newservicehome);
3501 pg->peers[off].cfg = pcfg;
3502 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
3503 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
3504 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
3505 pg->peers[off].pg = pg;
3507 pg->peers[off].internal_context.peer = &pg->peers[off];
3508 pg->peers[off].internal_context.timeout = timeout;
3509 pg->peers[off].internal_context.hostname = hostname;
3510 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
3511 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
3512 pg->peers[off].internal_context.start_cb = cb;
3513 pg->peers[off].internal_context.start_cb_cls = cb_cls;
3515 GNUNET_SCHEDULER_add_now (sched, &internal_start, &pg->peers[off].internal_context);
3522 * Get a daemon by number, so callers don't have to do nasty
3523 * offsetting operation.
3525 struct GNUNET_TESTING_Daemon *
3526 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
3528 if (position < pg->total)
3529 return pg->peers[position].daemon;
3535 * Prototype of a function that will be called when a
3536 * particular operation was completed the testing library.
3538 * @param cls closure (a struct RestartContext)
3539 * @param id id of the peer that was restarted
3540 * @param cfg handle to the configuration of the peer
3541 * @param d handle to the daemon that was restarted
3542 * @param emsg NULL on success
3544 void restart_callback (void *cls,
3545 const struct GNUNET_PeerIdentity *id,
3546 const struct GNUNET_CONFIGURATION_Handle *cfg,
3547 struct GNUNET_TESTING_Daemon *d,
3550 struct RestartContext *restart_context = cls;
3554 restart_context->peers_restarted++;
3558 restart_context->peers_restart_failed++;
3561 if (restart_context->peers_restarted == restart_context->peer_group->total)
3563 restart_context->callback(restart_context->callback_cls, NULL);
3564 GNUNET_free(restart_context);
3566 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
3568 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
3569 GNUNET_free(restart_context);
3575 * Callback for informing us about a successful
3576 * or unsuccessful churn stop call.
3578 * @param cls a ChurnContext
3579 * @param emsg NULL on success, non-NULL on failure
3583 churn_stop_callback (void *cls, const char *emsg)
3585 struct ChurnContext *churn_ctx = cls;
3586 unsigned int total_left;
3587 char *error_message;
3589 error_message = NULL;
3592 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3593 "Churn stop callback failed with error `%s'\n", emsg);
3594 churn_ctx->num_failed_stop++;
3598 churn_ctx->num_to_stop--;
3602 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3603 "Stopped peer, %d left.\n",
3604 churn_ctx->num_to_stop);
3606 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3608 if (total_left == 0)
3610 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3612 GNUNET_asprintf(&error_message,
3613 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3614 churn_ctx->num_failed_start,
3615 churn_ctx->num_failed_stop);
3617 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3618 GNUNET_free_non_null(error_message);
3619 GNUNET_free(churn_ctx);
3624 * Callback for informing us about a successful
3625 * or unsuccessful churn start call.
3627 * @param cls a ChurnContext
3628 * @param id the peer identity of the started peer
3629 * @param cfg the handle to the configuration of the peer
3630 * @param d handle to the daemon for the peer
3631 * @param emsg NULL on success, non-NULL on failure
3635 churn_start_callback (void *cls,
3636 const struct GNUNET_PeerIdentity *id,
3637 const struct GNUNET_CONFIGURATION_Handle *cfg,
3638 struct GNUNET_TESTING_Daemon *d,
3641 struct ChurnContext *churn_ctx = cls;
3642 unsigned int total_left;
3643 char *error_message;
3645 error_message = NULL;
3648 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3649 "Churn stop callback failed with error `%s'\n",
3651 churn_ctx->num_failed_start++;
3655 churn_ctx->num_to_start--;
3659 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3660 "Started peer, %d left.\n",
3661 churn_ctx->num_to_start);
3664 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3666 if (total_left == 0)
3668 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3669 GNUNET_asprintf(&error_message,
3670 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3671 churn_ctx->num_failed_start,
3672 churn_ctx->num_failed_stop);
3673 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3674 GNUNET_free_non_null(error_message);
3675 GNUNET_free(churn_ctx);
3681 * Simulate churn by stopping some peers (and possibly
3682 * re-starting others if churn is called multiple times). This
3683 * function can only be used to create leave-join churn (peers "never"
3684 * leave for good). First "voff" random peers that are currently
3685 * online will be taken offline; then "von" random peers that are then
3686 * offline will be put back online. No notifications will be
3687 * generated for any of these operations except for the callback upon
3690 * @param pg handle for the peer group
3691 * @param voff number of peers that should go offline
3692 * @param von number of peers that should come back online;
3693 * must be zero on first call (since "testbed_start"
3694 * always starts all of the peers)
3695 * @param timeout how long to wait for operations to finish before
3697 * @param cb function to call at the end
3698 * @param cb_cls closure for cb
3701 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
3704 struct GNUNET_TIME_Relative timeout,
3705 GNUNET_TESTING_NotifyCompletion cb,
3708 struct ChurnContext *churn_ctx;
3709 unsigned int running;
3710 unsigned int stopped;
3711 unsigned int total_running;
3712 unsigned int total_stopped;
3714 unsigned int *running_arr;
3715 unsigned int *stopped_arr;
3716 unsigned int *running_permute;
3717 unsigned int *stopped_permute;
3722 if ((von == 0) && (voff == 0)) /* No peers at all? */
3728 for (i = 0; i < pg->total; i++)
3730 if (pg->peers[i].daemon->running == GNUNET_YES)
3732 GNUNET_assert(running != -1);
3737 GNUNET_assert(stopped != -1);
3744 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
3745 cb(cb_cls, "Trying to stop more peers than are currently running!");
3751 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
3752 cb(cb_cls, "Trying to start more peers than are currently stopped!");
3756 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3760 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
3764 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
3766 running_permute = NULL;
3767 stopped_permute = NULL;
3770 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
3772 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
3774 total_running = running;
3775 total_stopped = stopped;
3779 churn_ctx->num_to_start = von;
3780 churn_ctx->num_to_stop = voff;
3782 churn_ctx->cb_cls = cb_cls;
3784 for (i = 0; i < pg->total; i++)
3786 if (pg->peers[i].daemon->running == GNUNET_YES)
3788 GNUNET_assert((running_arr != NULL) && (total_running > running));
3789 running_arr[running] = i;
3794 GNUNET_assert((stopped_arr != NULL) && (total_stopped > stopped));
3795 stopped_arr[stopped] = i;
3800 for (i = 0; i < voff; i++)
3803 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
3805 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
3807 &churn_stop_callback, churn_ctx,
3808 GNUNET_NO, GNUNET_YES);
3811 for (i = 0; i < von; i++)
3814 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
3816 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
3817 timeout, &churn_start_callback, churn_ctx);
3820 GNUNET_free_non_null(running_arr);
3821 GNUNET_free_non_null(stopped_arr);
3822 GNUNET_free_non_null(running_permute);
3823 GNUNET_free_non_null(stopped_permute);
3828 * Restart all peers in the given group.
3830 * @param pg the handle to the peer group
3831 * @param callback function to call on completion (or failure)
3832 * @param callback_cls closure for the callback function
3835 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
3836 GNUNET_TESTING_NotifyCompletion callback,
3839 struct RestartContext *restart_context;
3844 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
3845 restart_context->peer_group = pg;
3846 restart_context->peers_restarted = 0;
3847 restart_context->callback = callback;
3848 restart_context->callback_cls = callback_cls;
3850 for (off = 0; off < pg->total; off++)
3852 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
3858 * Start or stop an individual peer from the given group.
3860 * @param pg handle to the peer group
3861 * @param offset which peer to start or stop
3862 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
3863 * @param timeout how long to wait for shutdown
3864 * @param cb function to call at the end
3865 * @param cb_cls closure for cb
3868 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
3869 unsigned int offset,
3871 struct GNUNET_TIME_Relative timeout,
3872 GNUNET_TESTING_NotifyCompletion cb,
3875 struct ChurnContext *churn_ctx;
3877 if (GNUNET_NO == desired_status)
3879 if (NULL != pg->peers[offset].daemon)
3881 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3882 churn_ctx->num_to_start = 0;
3883 churn_ctx->num_to_stop = 1;
3885 churn_ctx->cb_cls = cb_cls;
3886 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
3887 timeout, &churn_stop_callback, churn_ctx,
3888 GNUNET_NO, GNUNET_YES);
3891 else if (GNUNET_YES == desired_status)
3893 if (NULL == pg->peers[offset].daemon)
3895 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3896 churn_ctx->num_to_start = 1;
3897 churn_ctx->num_to_stop = 0;
3899 churn_ctx->cb_cls = cb_cls;
3900 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
3901 timeout, &churn_start_callback, churn_ctx);
3910 * Callback for shutting down peers in a peer group.
3912 * @param cls closure (struct ShutdownContext)
3913 * @param emsg NULL on success
3915 void internal_shutdown_callback (void *cls,
3918 struct ShutdownContext *shutdown_ctx = cls;
3920 shutdown_ctx->outstanding--;
3923 shutdown_ctx->peers_down++;
3927 shutdown_ctx->peers_failed++;
3930 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
3932 if (shutdown_ctx->peers_failed > 0)
3933 shutdown_ctx->cb(shutdown_ctx->cb_cls, "Not all peers successfully shut down!");
3935 shutdown_ctx->cb(shutdown_ctx->cb_cls, NULL);
3936 GNUNET_free(shutdown_ctx);
3941 * Individual shutdown context for a particular peer.
3943 struct PeerShutdownContext
3946 * Pointer to the high level shutdown context.
3948 struct ShutdownContext *shutdown_ctx;
3951 * The daemon handle for the peer to shut down.
3953 struct GNUNET_TESTING_Daemon *daemon;
3957 * Task to rate limit the number of outstanding peer shutdown
3958 * requests. This is necessary for making sure we don't do
3959 * too many ssh connections at once, but is generally nicer
3960 * to any system as well (graduated task starts, as opposed
3961 * to calling gnunet-arm N times all at once).
3964 schedule_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3966 struct PeerShutdownContext *peer_shutdown_ctx = cls;
3967 struct ShutdownContext *shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
3969 GNUNET_assert(peer_shutdown_ctx != NULL);
3970 GNUNET_assert(shutdown_ctx != NULL);
3972 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
3973 GNUNET_SCHEDULER_add_delayed(peer_shutdown_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_shutdown_task, peer_shutdown_ctx);
3976 shutdown_ctx->outstanding++;
3977 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, &internal_shutdown_callback, shutdown_ctx, GNUNET_YES, GNUNET_NO);
3978 GNUNET_free(peer_shutdown_ctx);
3982 * Shutdown all peers started in the given group.
3984 * @param pg handle to the peer group
3985 * @param timeout how long to wait for shutdown
3986 * @param cb callback to notify upon success or failure
3987 * @param cb_cls closure for cb
3990 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
3991 struct GNUNET_TIME_Relative timeout,
3992 GNUNET_TESTING_NotifyCompletion cb,
3996 struct ShutdownContext *shutdown_ctx;
3997 struct PeerShutdownContext *peer_shutdown_ctx;
3999 GNUNET_assert(pg->total > 0);
4001 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4002 shutdown_ctx->cb = cb;
4003 shutdown_ctx->cb_cls = cb_cls;
4004 shutdown_ctx->total_peers = pg->total;
4005 shutdown_ctx->timeout = timeout;
4006 /* shtudown_ctx->outstanding = 0; */
4008 for (off = 0; off < pg->total; off++)
4010 GNUNET_assert(NULL != pg->peers[off].daemon);
4011 peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4012 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
4013 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4014 GNUNET_SCHEDULER_add_now(pg->peers[off].daemon->sched, &schedule_shutdown_task, peer_shutdown_ctx);
4015 //GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4016 if (NULL != pg->peers[off].cfg)
4017 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
4018 if (pg->peers[off].allowed_peers != NULL)
4019 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
4020 if (pg->peers[off].connect_peers != NULL)
4021 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
4022 if (pg->peers[off].blacklisted_peers != NULL)
4023 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
4025 GNUNET_free (pg->peers);
4026 if (NULL != pg->hosts)
4028 GNUNET_free (pg->hosts[0].hostname);
4029 GNUNET_free (pg->hosts);
4035 /* end of testing_group.c */