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;
247 * Username to use when connecting to the
250 const char *username;
253 * Port to use for ssh.
260 * Data we keep per peer.
265 * (Initial) configuration of the host.
266 * (initial because clients could change
267 * it and we would not know about those
270 struct GNUNET_CONFIGURATION_Handle *cfg;
273 * Handle for controlling the daemon.
275 struct GNUNET_TESTING_Daemon *daemon;
278 * The peergroup this peer belongs to.
280 struct GNUNET_TESTING_PeerGroup *pg;
283 * Hash map of allowed peer connections (F2F created topology)
285 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
288 * Hash map of blacklisted peers
290 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
293 * Hash map of peer connections
295 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
298 * Temporary hash map of peer connections
300 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
303 * Temporary variable for topology creation, should be reset before
304 * creating any topology so the count is valid once finished.
309 * Context to keep track of peers being started, to
310 * stagger hostkey generation and peer startup.
312 struct InternalStartContext internal_context;
317 * Data we keep per host.
327 * SSH username to use when connecting to this host.
332 * SSH port to use when connecting to this host.
337 * Lowest port that we have not yet used
343 struct TopologyIterateContext
346 * Callback for notifying of two connected peers.
348 GNUNET_TESTING_NotifyTopology topology_cb;
351 * Closure for topology_cb
356 * Number of peers currently connected to.
358 unsigned int connected;
361 * Number of peers we have finished iterating.
363 unsigned int completed;
366 * Number of peers total.
371 struct StatsIterateContext
374 * Continuation to call once all stats information has been retrieved.
376 GNUNET_STATISTICS_Callback cont;
379 * Proc function to call on each value received.
381 GNUNET_TESTING_STATISTICS_Iterator proc;
384 * Closure for topology_cb
389 * Number of peers currently connected to.
391 unsigned int connected;
394 * Number of peers we have finished iterating.
396 unsigned int completed;
399 * Number of peers total.
407 struct GNUNET_TESTING_Daemon *daemon;
410 struct StatsCoreContext
413 struct GNUNET_TESTING_Daemon *daemon;
415 * Handle to the statistics service.
417 struct GNUNET_STATISTICS_Handle *stats_handle;
420 * Handle for getting statistics.
422 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
426 * Handle to a group of GNUnet peers.
428 struct GNUNET_TESTING_PeerGroup
433 struct GNUNET_SCHEDULER_Handle *sched;
436 * Configuration template.
438 const struct GNUNET_CONFIGURATION_Handle *cfg;
441 * Function to call on each started daemon.
443 //GNUNET_TESTING_NotifyDaemonRunning cb;
451 * Function to call on each topology connection created
453 GNUNET_TESTING_NotifyConnection notify_connection;
456 * Callback for notify_connection
458 void *notify_connection_cls;
461 * NULL-terminated array of information about
464 struct HostData *hosts;
467 * Array of "total" peers.
469 struct PeerData *peers;
472 * Number of peers in this group.
477 * At what time should we fail the peer startup process?
479 struct GNUNET_TIME_Absolute max_timeout;
482 * How many peers are being started right now?
484 unsigned int starting;
487 * How many peers have already been started?
489 unsigned int started;
494 struct GNUNET_CONFIGURATION_Handle *ret;
495 const struct GNUNET_CONFIGURATION_Handle *orig;
496 const char *hostname;
503 struct ConnectContext
505 struct GNUNET_TESTING_Daemon *first;
507 struct GNUNET_TESTING_Daemon *second;
509 struct GNUNET_TESTING_PeerGroup *pg;
513 * Convert unique ID to hash code.
515 * @param uid unique ID to convert
516 * @param hash set to uid (extended with zeros)
519 hash_from_uid (uint32_t uid,
520 GNUNET_HashCode *hash)
522 memset (hash, 0, sizeof(GNUNET_HashCode));
523 *((uint32_t*)hash) = uid;
527 * Convert hash code to unique ID.
529 * @param uid unique ID to convert
530 * @param hash set to uid (extended with zeros)
533 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
535 memcpy (uid, hash, sizeof(uint32_t));
539 * Number of connects we are waiting on, allows us to rate limit
542 static int outstanding_connects;
545 * Get a topology from a string input.
547 * @param topology where to write the retrieved topology
548 * @param topology_string The string to attempt to
549 * get a configuration value from
550 * @return GNUNET_YES if topology string matched a
551 * known topology, GNUNET_NO if not
554 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string)
557 * Strings representing topologies in enum
559 static const char * topology_strings[] =
562 * A clique (everyone connected to everyone else).
567 * Small-world network (2d torus plus random links).
572 * Small-world network (ring plus random links).
592 * Certain percentage of peers are unable to communicate directly
593 * replicating NAT conditions
598 * Scale free topology.
603 * Straight line topology.
608 * All peers are disconnected.
616 if (topology_string == NULL)
618 while (topology_strings[curr] != NULL)
620 if (strcasecmp(topology_strings[curr], topology_string) == 0)
627 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
633 * Get connect topology option from string input.
635 * @param topology_option where to write the retrieved topology
636 * @param topology_string The string to attempt to
637 * get a configuration value from
638 * @return GNUNET_YES if string matched a known
639 * topology option, GNUNET_NO if not
642 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption *topology_option,
643 char * topology_string)
646 * Options for connecting a topology as strings.
648 static const char * topology_option_strings[] =
651 * Try to connect all peers specified in the topology.
656 * Choose a random subset of connections to create.
658 "CONNECT_RANDOM_SUBSET",
661 * Create at least X connections for each peer.
666 * Using a depth first search, create one connection
667 * per peer. If any are missed (graph disconnected)
668 * start over at those peers until all have at least one
674 * No options specified.
682 if (topology_string == NULL)
684 while (NULL != topology_option_strings[curr])
686 if (strcasecmp(topology_option_strings[curr], topology_string) == 0)
688 *topology_option = curr;
693 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
698 * Function to iterate over options. Copies
699 * the options to the target configuration,
700 * updating PORT values as needed.
703 * @param section name of the section
704 * @param option name of the option
705 * @param value value of the option
708 update_config (void *cls,
709 const char *section, const char *option, const char *value)
711 struct UpdateContext *ctx = cls;
715 char *single_variable;
716 char *per_host_variable;
717 unsigned long long num_per_host;
719 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
721 GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
722 if ((ival != 0) && (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable)))
724 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
728 GNUNET_free(single_variable);
731 if (0 == strcmp (option, "UNIXPATH"))
733 GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
734 GNUNET_asprintf(&per_host_variable, "num_%s_per_host", section);
735 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable))
737 GNUNET_snprintf (uval,
739 "/tmp/test-service-%s-%u",
744 else if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_number(ctx->orig, "testing", per_host_variable, &num_per_host))
746 GNUNET_snprintf (uval,
748 "/tmp/test-service-%s-%u",
750 ctx->fdnum % num_per_host);
753 GNUNET_free(single_variable);
754 GNUNET_free(per_host_variable);
758 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
760 value = ctx->hostname;
763 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
768 * Create a new configuration using the given configuration
769 * as a template; however, each PORT in the existing cfg
770 * must be renumbered by incrementing "*port". If we run
771 * out of "*port" numbers, return NULL.
773 * @param cfg template configuration
774 * @param port port numbers to use, update to reflect
775 * port numbers that were used
776 * @param upnum number to make unix domain socket names unique
777 * @param hostname hostname of the controlling host, to allow control connections from
778 * @param fdnum number used to offset the unix domain socket for grouped processes
779 * (such as statistics or peerinfo, which can be shared among others)
781 * @return new configuration, NULL on error
783 static struct GNUNET_CONFIGURATION_Handle *
784 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
787 const char *hostname, uint32_t * fdnum)
789 struct UpdateContext uc;
798 uc.ret = GNUNET_CONFIGURATION_create ();
799 uc.hostname = hostname;
802 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
803 if (uc.nport >= HIGH_PORT)
806 GNUNET_CONFIGURATION_destroy (uc.ret);
810 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
812 if (hostname != NULL)
813 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1; %s;", control_host, hostname);
815 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
817 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
818 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport", "ACCEPT_FROM", allowed_hosts);
819 GNUNET_CONFIGURATION_set_value_string(uc.ret, "dht", "ACCEPT_FROM", allowed_hosts);
820 GNUNET_CONFIGURATION_set_value_string(uc.ret, "statistics", "ACCEPT_FROM", allowed_hosts);
821 GNUNET_free_non_null(control_host);
822 GNUNET_free(allowed_hosts);
826 /* arm needs to know to allow connections from the host on which it is running,
827 * otherwise gnunet-arm is unable to connect to it in some instances */
828 if (hostname != NULL)
830 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
831 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", hostname);
832 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", hostname);
833 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
834 GNUNET_free(allowed_hosts);
838 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", "127.0.0.1");
839 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", "127.0.0.1");
843 *port = (uint16_t) uc.nport;
852 * Add entries to the peers connect list
854 * @param pg the peer group we are working with
855 * @param first index of the first peer
856 * @param second index of the second peer
858 * @return the number of connections added
859 * technically should only be 0 or 2
863 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
869 GNUNET_HashCode hash_first;
870 GNUNET_HashCode hash_second;
872 hash_from_uid(first, &hash_first);
873 hash_from_uid(second, &hash_second);
875 add_first = GNUNET_NO;
876 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
878 add_first = GNUNET_YES;
881 add_second = GNUNET_NO;
882 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
884 add_second = GNUNET_YES;
890 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
891 pg->peers[first].num_connections++;
897 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
898 pg->peers[second].num_connections++;
907 * Add entries to the peers allowed connections list
909 * @param pg the peer group we are working with
910 * @param first index of the first peer
911 * @param second index of the second peer
913 * @return the number of connections added (can be 0, 1 or 2)
914 * technically should only be 0 or 2, but the small price
915 * of iterating over the lists (hashmaps in the future)
916 * for being sure doesn't bother me!
920 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
924 struct PeerConnection *first_iter;
925 struct PeerConnection *second_iter;
926 struct PeerConnection *new_first;
927 struct PeerConnection *new_second;
932 GNUNET_HashCode hash_first;
933 GNUNET_HashCode hash_second;
935 hash_from_uid(first, &hash_first);
936 hash_from_uid(second, &hash_second);
938 add_first = GNUNET_NO;
939 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
941 add_first = GNUNET_YES;
944 add_second = GNUNET_NO;
945 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
947 add_second = GNUNET_YES;
950 first_iter = pg->peers[first].connected_peers;
951 while (first_iter != NULL)
953 if (first_iter->daemon == pg->peers[second].daemon)
954 add_first = GNUNET_NO;
955 first_iter = first_iter->next;
958 second_iter = pg->peers[second].connected_peers;
959 add_second = GNUNET_YES;
960 while (second_iter != NULL)
962 if (second_iter->daemon == pg->peers[first].daemon)
963 add_second = GNUNET_NO;
964 second_iter = second_iter->next;
971 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
973 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
974 new_first->daemon = pg->peers[second].daemon;
975 new_first->next = pg->peers[first].connected_peers;
976 pg->peers[first].connected_peers = new_first;
978 pg->peers[first].num_connections++;
984 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
986 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
987 new_second->daemon = pg->peers[first].daemon;
988 new_second->next = pg->peers[second].connected_peers;
989 pg->peers[second].connected_peers = new_second;
990 pg->peers[first].num_connections++;
992 pg->peers[second].num_connections++;
1000 * Add entries to the peers blacklisted list
1002 * @param pg the peer group we are working with
1003 * @param first index of the first peer
1004 * @param second index of the second peer
1006 * @return the number of connections added (can be 0, 1 or 2)
1010 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1015 GNUNET_HashCode hash_first;
1016 GNUNET_HashCode hash_second;
1018 hash_from_uid(first, &hash_first);
1019 hash_from_uid(second, &hash_second);
1021 add_first = GNUNET_NO;
1022 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
1024 add_first = GNUNET_YES;
1027 add_second = GNUNET_NO;
1028 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
1030 add_second = GNUNET_YES;
1036 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1037 pg->peers[first].num_connections++;
1043 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1044 pg->peers[second].num_connections++;
1052 * Remove entries from the peers blacklisted list
1054 * @param pg the peer group we are working with
1055 * @param first index of the first peer
1056 * @param second index of the second peer
1058 * @return the number of connections removed (can be 0, 1 or 2)
1062 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1067 GNUNET_HashCode hash_first;
1068 GNUNET_HashCode hash_second;
1070 hash_from_uid(first, &hash_first);
1071 hash_from_uid(second, &hash_second);
1073 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
1074 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
1079 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
1085 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
1093 * Scale free network construction as described in:
1095 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1097 * Start with a network of "one" peer, then progressively add
1098 * peers up to the total number. At each step, iterate over
1099 * all possible peers and connect new peer based on number of
1100 * existing connections of the target peer.
1102 * @param pg the peer group we are dealing with
1103 * @param proc the connection processor to use
1105 * @return the number of connections created
1108 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1111 unsigned int total_connections;
1112 unsigned int outer_count;
1114 unsigned int previous_total_connections;
1118 GNUNET_assert(pg->total > 1);
1120 /* Add a connection between the first two nodes */
1121 total_connections = proc(pg, 0, 1);
1123 for (outer_count = 1; outer_count < pg->total; outer_count++)
1125 previous_total_connections = total_connections;
1126 for (i = 0; i < outer_count; i++)
1128 probability = pg->peers[i].num_connections / (double)previous_total_connections;
1129 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1130 UINT64_MAX)) / ( (double) UINT64_MAX);
1132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1133 "Considering connecting peer %d to peer %d\n",
1136 if (random < probability)
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 "Connecting peer %d to peer %d\n",
1143 total_connections += proc(pg, outer_count, i);
1148 return total_connections;
1152 * Create a topology given a peer group (set of running peers)
1153 * and a connection processor.
1155 * @param pg the peergroup to create the topology on
1156 * @param proc the connection processor to call to actually set
1157 * up connections between two peers
1159 * @return the number of connections that were set up
1163 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1167 unsigned int natLog;
1168 unsigned int randomPeer;
1169 double random, logNModifier, percentage;
1170 unsigned int smallWorldConnections;
1175 unsigned int useAnd;
1176 int connect_attempts;
1178 logNModifier = 0.5; /* FIXME: default value? */
1179 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1184 if (sscanf(p_string, "%lf", &logNModifier) != 1)
1185 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1186 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1190 GNUNET_free (p_string);
1192 percentage = 0.5; /* FIXME: default percentage? */
1193 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1198 if (sscanf(p_string, "%lf", &percentage) != 1)
1199 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1200 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1204 GNUNET_free (p_string);
1206 natLog = log (pg->total);
1207 connsPerPeer = ceil (natLog * logNModifier);
1209 if (connsPerPeer % 2 == 1)
1212 smallWorldConnections = 0;
1213 connect_attempts = 0;
1214 for (i = 0; i < pg->total; i++)
1217 max = i + connsPerPeer / 2;
1218 min = i - connsPerPeer / 2;
1220 if (max > pg->total - 1)
1222 max = max - pg->total;
1228 min = pg->total - 1 + min;
1232 for (j = 0; j < connsPerPeer / 2; j++)
1234 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1235 UINT64_MAX) / ( (double) UINT64_MAX));
1236 if (random < percentage)
1238 /* Connect to uniformly selected random peer */
1240 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1242 while ((((randomPeer < max) && (randomPeer > min))
1243 && (useAnd == 0)) || (((randomPeer > min)
1244 || (randomPeer < max))
1248 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1251 smallWorldConnections +=
1252 proc (pg, i, randomPeer);
1256 nodeToConnect = i + j + 1;
1257 if (nodeToConnect > pg->total - 1)
1259 nodeToConnect = nodeToConnect - pg->total;
1262 proc (pg, i, nodeToConnect);
1268 connect_attempts += smallWorldConnections;
1270 return connect_attempts;
1274 * Create a topology given a peer group (set of running peers)
1275 * and a connection processor.
1277 * @param pg the peergroup to create the topology on
1278 * @param proc the connection processor to call to actually set
1279 * up connections between two peers
1281 * @return the number of connections that were set up
1285 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1287 unsigned int outer_count, inner_count;
1288 unsigned int cutoff;
1289 int connect_attempts;
1290 double nat_percentage;
1293 nat_percentage = 0.6; /* FIXME: default percentage? */
1294 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1299 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1300 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1301 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1305 GNUNET_free (p_string);
1310 cutoff = (unsigned int) (nat_percentage * pg->total);
1312 connect_attempts = 0;
1314 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1316 for (inner_count = outer_count + 1; inner_count < pg->total;
1319 if ((outer_count > cutoff) || (inner_count > cutoff))
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1323 "Connecting peer %d to peer %d\n",
1324 outer_count, inner_count);
1326 connect_attempts += proc(pg, outer_count, inner_count);
1331 return connect_attempts;
1336 * Create a topology given a peer group (set of running peers)
1337 * and a connection processor.
1339 * @param pg the peergroup to create the topology on
1340 * @param proc the connection processor to call to actually set
1341 * up connections between two peers
1343 * @return the number of connections that were set up
1347 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1349 unsigned int i, j, k;
1350 unsigned int square;
1353 unsigned int toggle = 1;
1354 unsigned int nodeToConnect;
1355 unsigned int natLog;
1356 unsigned int node1Row;
1357 unsigned int node1Col;
1358 unsigned int node2Row;
1359 unsigned int node2Col;
1360 unsigned int distance;
1361 double probability, random, percentage;
1362 unsigned int smallWorldConnections;
1363 unsigned int small_world_it;
1365 int connect_attempts;
1366 square = floor (sqrt (pg->total));
1370 percentage = 0.5; /* FIXME: default percentage? */
1371 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1376 if (sscanf(p_string, "%lf", &percentage) != 1)
1377 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1378 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1382 GNUNET_free (p_string);
1384 if (percentage < 0.0)
1386 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1387 _("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1388 "PERCENTAGE", "TESTING", percentage);
1391 probability = 0.5; /* FIXME: default percentage? */
1392 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1397 if (sscanf(p_string, "%lf", &probability) != 1)
1398 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1399 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1403 GNUNET_free (p_string);
1405 if (square * square != pg->total)
1407 while (rows * cols < pg->total)
1409 if (toggle % 2 == 0)
1418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1419 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1423 connect_attempts = 0;
1424 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1425 * to the node to its right and above. Once this is over, we'll have our torus!
1426 * Special case for the last node (if the rows and columns are not equal), connect
1427 * to the first in the row to maintain topology.
1429 for (i = 0; i < pg->total; i++)
1431 /* First connect to the node to the right */
1432 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1433 nodeToConnect = i + 1;
1434 else if (i + 1 == pg->total)
1435 nodeToConnect = rows * cols - cols;
1437 nodeToConnect = i - cols + 1;
1439 connect_attempts += proc (pg, i, nodeToConnect);
1442 nodeToConnect = (rows * cols) - cols + i;
1444 nodeToConnect = i - cols;
1446 if (nodeToConnect < pg->total)
1447 connect_attempts += proc (pg, i, nodeToConnect);
1449 natLog = log (pg->total);
1450 #if VERBOSE_TESTING > 2
1451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1452 _("natural log of %d is %d, will run %d iterations\n"),
1453 pg->total, natLog, (int) (natLog * percentage));
1454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1456 smallWorldConnections = 0;
1457 small_world_it = (unsigned int)(natLog * percentage);
1458 GNUNET_assert(small_world_it > 0 && small_world_it < (unsigned int)-1);
1459 for (i = 0; i < small_world_it; i++)
1461 for (j = 0; j < pg->total; j++)
1463 /* Determine the row and column of node at position j on the 2d torus */
1464 node1Row = j / cols;
1465 node1Col = j - (node1Row * cols);
1466 for (k = 0; k < pg->total; k++)
1468 /* Determine the row and column of node at position k on the 2d torus */
1469 node2Row = k / cols;
1470 node2Col = k - (node2Row * cols);
1471 /* Simple Cartesian distance */
1472 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1475 /* Calculate probability as 1 over the square of the distance */
1476 probability = 1.0 / (distance * distance);
1477 /* Choose a random value between 0 and 1 */
1478 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1479 UINT64_MAX)) / ( (double) UINT64_MAX);
1480 /* If random < probability, then connect the two nodes */
1481 if (random < probability)
1482 smallWorldConnections += proc (pg, j, k);
1488 connect_attempts += smallWorldConnections;
1489 #if VERBOSE_TESTING > 2
1490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1491 _("Total connections added for small world: %d!\n"),
1492 smallWorldConnections);
1494 return connect_attempts;
1498 * Create a topology given a peer group (set of running peers)
1499 * and a connection processor.
1501 * @param pg the peergroup to create the topology on
1502 * @param proc the connection processor to call to actually set
1503 * up connections between two peers
1505 * @return the number of connections that were set up
1509 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1512 unsigned int outer_count;
1513 unsigned int inner_count;
1514 int connect_attempts;
1518 probability = 0.5; /* FIXME: default percentage? */
1519 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1524 if (sscanf(p_string, "%lf", &probability) != 1)
1525 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1526 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1530 GNUNET_free (p_string);
1532 connect_attempts = 0;
1533 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1535 for (inner_count = outer_count + 1; inner_count < pg->total;
1538 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1539 UINT64_MAX)) / ( (double) UINT64_MAX);
1541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1542 _("rand is %f probability is %f\n"), temp_rand,
1545 if (temp_rand < probability)
1547 connect_attempts += proc (pg, outer_count, inner_count);
1552 return connect_attempts;
1556 * Create a topology given a peer group (set of running peers)
1557 * and a connection processor.
1559 * @param pg the peergroup to create the topology on
1560 * @param proc the connection processor to call to actually set
1561 * up connections between two peers
1563 * @return the number of connections that were set up
1567 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1570 unsigned int square;
1573 unsigned int toggle = 1;
1574 unsigned int nodeToConnect;
1575 int connect_attempts;
1577 connect_attempts = 0;
1579 square = floor (sqrt (pg->total));
1583 if (square * square != pg->total)
1585 while (rows * cols < pg->total)
1587 if (toggle % 2 == 0)
1596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1597 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1600 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1601 * to the node to its right and above. Once this is over, we'll have our torus!
1602 * Special case for the last node (if the rows and columns are not equal), connect
1603 * to the first in the row to maintain topology.
1605 for (i = 0; i < pg->total; i++)
1607 /* First connect to the node to the right */
1608 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1609 nodeToConnect = i + 1;
1610 else if (i + 1 == pg->total)
1611 nodeToConnect = rows * cols - cols;
1613 nodeToConnect = i - cols + 1;
1615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1616 "Connecting peer %d to peer %d\n",
1619 connect_attempts += proc(pg, i, nodeToConnect);
1621 /* Second connect to the node immediately above */
1623 nodeToConnect = (rows * cols) - cols + i;
1625 nodeToConnect = i - cols;
1627 if (nodeToConnect < pg->total)
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "Connecting peer %d to peer %d\n",
1634 connect_attempts += proc(pg, i, nodeToConnect);
1639 return connect_attempts;
1644 * Create a topology given a peer group (set of running peers)
1645 * and a connection processor.
1647 * @param pg the peergroup to create the topology on
1648 * @param proc the connection processor to call to actually set
1649 * up connections between two peers
1651 * @return the number of connections that were set up
1655 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1657 unsigned int outer_count;
1658 unsigned int inner_count;
1659 int connect_attempts;
1661 connect_attempts = 0;
1663 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1665 for (inner_count = outer_count + 1; inner_count < pg->total;
1669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1670 "Connecting peer %d to peer %d\n",
1671 outer_count, inner_count);
1673 connect_attempts += proc(pg, outer_count, inner_count);
1677 return connect_attempts;
1681 * Create a topology given a peer group (set of running peers)
1682 * and a connection processor.
1684 * @param pg the peergroup to create the topology on
1685 * @param proc the connection processor to call to actually set
1686 * up connections between two peers
1688 * @return the number of connections that were set up
1692 create_line (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1695 int connect_attempts;
1697 connect_attempts = 0;
1699 /* Connect each peer to the next highest numbered peer */
1700 for (count = 0; count < pg->total - 1; count++)
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1704 "Connecting peer %d to peer %d\n",
1707 connect_attempts += proc(pg, count, count + 1);
1710 return connect_attempts;
1714 * Create a topology given a peer group (set of running peers)
1715 * and a connection processor.
1717 * @param pg the peergroup to create the topology on
1718 * @param proc the connection processor to call to actually set
1719 * up connections between two peers
1721 * @return the number of connections that were set up
1725 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1728 int connect_attempts;
1730 connect_attempts = 0;
1732 /* Connect each peer to the next highest numbered peer */
1733 for (count = 0; count < pg->total - 1; count++)
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1737 "Connecting peer %d to peer %d\n",
1740 connect_attempts += proc(pg, count, count + 1);
1743 /* Connect the last peer to the first peer */
1744 connect_attempts += proc(pg, pg->total - 1, 0);
1746 return connect_attempts;
1751 * Iterator for writing friends of a peer to a file.
1753 * @param cls closure, an open writable file handle
1754 * @param key the key the daemon was stored under
1755 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1757 * @return GNUNET_YES to continue iteration
1759 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1760 * with a single file_iterator that takes a closure which contains
1761 * the prefix to write before the peer. Then this could be used
1762 * for blacklisting multiple transports and writing the friend
1763 * file. I'm sure *someone* will complain loudly about other
1764 * things that negate these functions even existing so no point in
1768 friend_file_iterator (void *cls,
1769 const GNUNET_HashCode * key,
1772 FILE *temp_friend_handle = cls;
1773 struct GNUNET_TESTING_Daemon *peer = value;
1774 struct GNUNET_PeerIdentity *temppeer;
1775 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1777 temppeer = &peer->id;
1778 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1779 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1784 struct BlacklistContext
1787 * The (open) file handle to write to
1789 FILE *temp_file_handle;
1792 * The transport that this peer will be blacklisted on.
1798 * Iterator for writing blacklist data to appropriate files.
1800 * @param cls closure, an open writable file handle
1801 * @param key the key the daemon was stored under
1802 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1804 * @return GNUNET_YES to continue iteration
1807 blacklist_file_iterator (void *cls,
1808 const GNUNET_HashCode * key,
1811 struct BlacklistContext *blacklist_ctx = cls;
1812 //FILE *temp_blacklist_handle = cls;
1813 struct GNUNET_TESTING_Daemon *peer = value;
1814 struct GNUNET_PeerIdentity *temppeer;
1815 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1817 temppeer = &peer->id;
1818 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1819 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1825 * Create the friend files based on the PeerConnection's
1826 * of each peer in the peer group, and copy the files
1827 * to the appropriate place
1829 * @param pg the peer group we are dealing with
1832 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1834 FILE *temp_friend_handle;
1835 unsigned int pg_iter;
1836 char *temp_service_path;
1840 enum GNUNET_OS_ProcessStatusType type;
1841 unsigned long return_code;
1846 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1847 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1849 mytemp = GNUNET_DISK_mktemp("friends");
1850 GNUNET_assert(mytemp != NULL);
1851 temp_friend_handle = fopen (mytemp, "wt");
1852 GNUNET_assert(temp_friend_handle != NULL);
1853 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1854 fclose(temp_friend_handle);
1857 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1859 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1860 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1863 if (UNLINK (mytemp) != 0)
1864 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1865 GNUNET_free (mytemp);
1869 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1871 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1872 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1873 "mv", mytemp, arg, NULL);
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876 _("Copying file with command cp %s %s\n"), mytemp, arg);
1881 else /* Remote, scp the file to the correct place */
1883 if (NULL != pg->peers[pg_iter].daemon->username)
1884 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1886 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1887 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1888 "scp", mytemp, arg, NULL);
1891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1892 _("Copying file with command scp %s %s\n"), mytemp, arg);
1896 GNUNET_free (temp_service_path);
1897 GNUNET_free (mytemp);
1901 ret = GNUNET_SYSERR;
1902 while ((count < max_wait) && (ret != GNUNET_OK))
1905 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1909 _("Checking copy status of file %d\n"), pg_iter);
1911 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1913 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1915 ret = GNUNET_SYSERR;
1917 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1919 ret = GNUNET_SYSERR;
1923 pidarr[pg_iter] = 0;
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1926 _("File %d copied\n"), pg_iter);
1932 if (ret == GNUNET_SYSERR)
1934 /* FIXME: why sleep here? -CG */
1940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1941 _("Finished copying all friend files!\n"));
1943 GNUNET_free(pidarr);
1949 * Create the blacklist files based on the PeerConnection's
1950 * of each peer in the peer group, and copy the files
1951 * to the appropriate place.
1953 * @param pg the peer group we are dealing with
1954 * @param transports space delimited list of transports to blacklist
1957 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
1959 FILE *temp_file_handle;
1960 static struct BlacklistContext blacklist_ctx;
1961 unsigned int pg_iter;
1962 char *temp_service_path;
1966 enum GNUNET_OS_ProcessStatusType type;
1967 unsigned long return_code;
1974 char *temp_transports;
1976 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1977 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1979 mytemp = GNUNET_DISK_mktemp("blacklist");
1980 GNUNET_assert(mytemp != NULL);
1981 temp_file_handle = fopen (mytemp, "wt");
1982 GNUNET_assert(temp_file_handle != NULL);
1983 temp_transports = GNUNET_strdup(transports);
1984 blacklist_ctx.temp_file_handle = temp_file_handle;
1985 transport_len = strlen(temp_transports) + 1;
1988 for (i = 0; i < transport_len; i++)
1990 if ((temp_transports[i] == ' ') && (pos == NULL))
1991 continue; /* At start of string (whitespace) */
1992 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
1994 temp_transports[i] = '\0';
1995 blacklist_ctx.transport = pos;
1996 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
1998 } /* At beginning of actual string */
1999 else if (pos == NULL)
2001 pos = &temp_transports[i];
2005 GNUNET_free (temp_transports);
2006 fclose(temp_file_handle);
2009 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
2011 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2012 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2015 if (UNLINK (mytemp) != 0)
2016 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
2017 GNUNET_free (mytemp);
2021 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2023 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2024 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2025 "mv", mytemp, arg, NULL);
2027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2028 _("Copying file with command cp %s %s\n"), mytemp, arg);
2033 else /* Remote, scp the file to the correct place */
2035 if (NULL != pg->peers[pg_iter].daemon->username)
2036 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
2038 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
2039 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
2040 "scp", mytemp, arg, NULL);
2043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2044 _("Copying file with command scp %s %s\n"), mytemp, arg);
2048 GNUNET_free (temp_service_path);
2049 GNUNET_free (mytemp);
2053 ret = GNUNET_SYSERR;
2054 while ((count < max_wait) && (ret != GNUNET_OK))
2057 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2061 _("Checking copy status of file %d\n"), pg_iter);
2063 if (pidarr[pg_iter] != 0) /* Check for already completed! */
2065 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
2067 ret = GNUNET_SYSERR;
2069 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
2071 ret = GNUNET_SYSERR;
2075 pidarr[pg_iter] = 0;
2077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2078 _("File %d copied\n"), pg_iter);
2084 if (ret == GNUNET_SYSERR)
2086 /* FIXME: why sleep here? -CG */
2092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2093 _("Finished copying all blacklist files!\n"));
2095 GNUNET_free(pidarr);
2101 * Internal notification of a connection, kept so that we can ensure some connections
2102 * happen instead of flooding all testing daemons with requests to connect.
2104 static void internal_connect_notify (void *cls,
2105 const struct GNUNET_PeerIdentity *first,
2106 const struct GNUNET_PeerIdentity *second,
2108 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2109 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2110 struct GNUNET_TESTING_Daemon *first_daemon,
2111 struct GNUNET_TESTING_Daemon *second_daemon,
2114 struct GNUNET_TESTING_PeerGroup *pg = cls;
2115 outstanding_connects--;
2117 pg->notify_connection(pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
2122 * Either delay a connection (because there are too many outstanding)
2123 * or schedule it for right now.
2125 * @param cls a connection context
2126 * @param tc the task runtime context
2128 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2130 struct ConnectContext *connect_context = cls;
2132 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2135 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2137 #if VERBOSE_TESTING > 2
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2139 _("Delaying connect, we have too many outstanding connections!\n"));
2141 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_connect, connect_context);
2145 #if VERBOSE_TESTING > 2
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2147 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
2149 outstanding_connects++;
2150 GNUNET_TESTING_daemons_connect (connect_context->first,
2151 connect_context->second,
2154 &internal_connect_notify,
2155 connect_context->pg);
2156 GNUNET_free(connect_context);
2162 * Iterator for actually scheduling connections to be created
2163 * between two peers.
2165 * @param cls closure, a GNUNET_TESTING_Daemon
2166 * @param key the key the second Daemon was stored under
2167 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2169 * @return GNUNET_YES to continue iteration
2172 connect_iterator (void *cls,
2173 const GNUNET_HashCode * key,
2176 struct PeerData *first = cls;
2177 struct GNUNET_TESTING_Daemon *second = value;
2178 struct ConnectContext *connect_context;
2180 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2181 connect_context->pg = first->pg;
2182 connect_context->first = first->daemon;
2183 connect_context->second = second;
2184 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
2191 * Iterator for copying all entries in the allowed hashmap to the
2194 * @param cls closure, a GNUNET_TESTING_Daemon
2195 * @param key the key the second Daemon was stored under
2196 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2198 * @return GNUNET_YES to continue iteration
2201 copy_topology_iterator (void *cls,
2202 const GNUNET_HashCode * key,
2205 struct PeerData *first = cls;
2207 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2213 * Make the peers to connect the same as those that are allowed to be
2216 * @param pg the peer group
2219 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2221 unsigned int pg_iter;
2226 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2228 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
2229 if (GNUNET_SYSERR == ret)
2230 return GNUNET_SYSERR;
2232 total = total + ret;
2240 * Connect the topology as specified by the PeerConnection's
2241 * of each peer in the peer group
2243 * @param pg the peer group we are dealing with
2244 * @return the number of connections that will be attempted
2247 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
2249 unsigned int pg_iter;
2253 struct PeerConnection *connection_iter;
2254 struct ConnectContext *connect_context;
2258 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2260 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
2261 if (GNUNET_SYSERR == ret)
2262 return GNUNET_SYSERR;
2264 total = total + ret;
2268 while (connection_iter != NULL)
2270 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2271 connect_context->pg = pg;
2272 connect_context->first = ;
2273 connect_context->second = connection_iter->daemon;
2274 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
2275 connection_iter = connection_iter->next;
2284 * Takes a peer group and creates a topology based on the
2285 * one specified. Creates a topology means generates friend
2286 * files for the peers so they can only connect to those allowed
2287 * by the topology. This will only have an effect once peers
2288 * are started if the FRIENDS_ONLY option is set in the base
2289 * config. Also takes an optional restrict topology which
2290 * disallows connections based on a particular transport
2291 * UNLESS they are specified in the restricted topology.
2293 * @param pg the peer group struct representing the running peers
2294 * @param topology which topology to connect the peers in
2295 * @param restrict_topology allow only direct TCP connections in this topology
2296 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2297 * @param restrict_transports space delimited list of transports to blacklist
2298 * to create restricted topology
2300 * @return the maximum number of connections were all allowed peers
2301 * connected to each other
2304 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2305 enum GNUNET_TESTING_Topology topology,
2306 enum GNUNET_TESTING_Topology restrict_topology,
2307 char *restrict_transports)
2310 int num_connections;
2311 int unblacklisted_connections;
2313 GNUNET_assert (pg->notify_connection != NULL);
2317 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2320 _("Creating clique topology\n"));
2322 num_connections = create_clique (pg, &add_allowed_connections);
2324 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2327 _("Creating small world (ring) topology\n"));
2329 num_connections = create_small_world_ring (pg, &add_allowed_connections);
2331 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2334 _("Creating small world (2d-torus) topology\n"));
2336 num_connections = create_small_world (pg, &add_allowed_connections);
2338 case GNUNET_TESTING_TOPOLOGY_RING:
2340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2341 _("Creating ring topology\n"));
2343 num_connections = create_ring (pg, &add_allowed_connections);
2345 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2348 _("Creating 2d torus topology\n"));
2350 num_connections = create_2d_torus (pg, &add_allowed_connections);
2352 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2355 _("Creating Erdos-Renyi topology\n"));
2357 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2359 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2362 _("Creating InterNAT topology\n"));
2364 num_connections = create_nated_internet (pg, &add_allowed_connections);
2366 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2369 _("Creating Scale Free topology\n"));
2371 num_connections = create_scale_free (pg, &add_allowed_connections);
2373 case GNUNET_TESTING_TOPOLOGY_LINE:
2375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2376 _("Creating straight line topology\n"));
2378 num_connections = create_line (pg, &add_allowed_connections);
2380 case GNUNET_TESTING_TOPOLOGY_NONE:
2382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2383 _("Creating no allowed topology (all peers can connect at core level)\n"));
2385 num_connections = 0;
2388 num_connections = 0;
2392 if (num_connections < 0)
2393 return GNUNET_SYSERR;
2395 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2397 ret = create_and_copy_friend_files(pg);
2398 if (ret != GNUNET_OK)
2401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2402 _("Failed during friend file copying!\n"));
2404 return GNUNET_SYSERR;
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2410 _("Friend files created/copied successfully!\n"));
2415 /* Use the create clique method to initially set all connections as blacklisted. */
2416 if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2417 create_clique (pg, &blacklist_connections);
2418 unblacklisted_connections = 0;
2419 /* Un-blacklist connections as per the topology specified */
2420 switch (restrict_topology)
2422 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2425 _("Blacklisting all but clique topology\n"));
2427 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2429 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2432 _("Blacklisting all but small world (ring) topology\n"));
2434 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2436 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2439 _("Blacklisting all but small world (2d-torus) topology\n"));
2441 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2443 case GNUNET_TESTING_TOPOLOGY_RING:
2445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446 _("Blacklisting all but ring topology\n"));
2448 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2450 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2453 _("Blacklisting all but 2d torus topology\n"));
2455 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2457 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2460 _("Blacklisting all but Erdos-Renyi topology\n"));
2462 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2464 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2467 _("Blacklisting all but InterNAT topology\n"));
2469 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2471 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2474 _("Blacklisting all but Scale Free topology\n"));
2476 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2478 case GNUNET_TESTING_TOPOLOGY_LINE:
2480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2481 _("Blacklisting all but straight line topology\n"));
2483 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2485 case GNUNET_TESTING_TOPOLOGY_NONE:
2487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2488 _("Creating no blacklist topology (all peers can connect at transport level)\n"));
2494 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2496 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2497 if (ret != GNUNET_OK)
2500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2501 _("Failed during blacklist file copying!\n"));
2503 return GNUNET_SYSERR;
2508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2509 _("Blacklist files created/copied successfully!\n"));
2513 return num_connections;
2516 struct RandomContext
2521 struct GNUNET_TESTING_PeerGroup *pg;
2524 * uid of the first peer
2529 * Peer data for first peer.
2531 struct PeerData *first;
2534 * Random percentage to use
2539 struct MinimumContext
2544 struct GNUNET_TESTING_PeerGroup *pg;
2547 * uid of the first peer
2552 * Peer data for first peer.
2554 struct PeerData *first;
2557 * Number of conns per peer
2559 unsigned int num_to_add;
2562 * Permuted array of all possible connections. Only add the Nth
2563 * peer if it's in the Nth position.
2565 unsigned int *pg_array;
2568 * What number is the current element we are iterating over?
2570 unsigned int current;
2578 struct GNUNET_TESTING_PeerGroup *pg;
2581 * uid of the first peer
2586 * uid of the second peer
2588 uint32_t second_uid;
2591 * Peer data for first peer.
2593 struct PeerData *first;
2596 * Which peer has been chosen as the one to add?
2598 unsigned int chosen;
2601 * What number is the current element we are iterating over?
2603 unsigned int current;
2607 * Iterator for choosing random peers to connect.
2609 * @param cls closure, a RandomContext
2610 * @param key the key the second Daemon was stored under
2611 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2613 * @return GNUNET_YES to continue iteration
2616 random_connect_iterator (void *cls,
2617 const GNUNET_HashCode * key,
2620 struct RandomContext *random_ctx = cls;
2621 double random_number;
2622 uint32_t second_pos;
2623 GNUNET_HashCode first_hash;
2624 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2625 UINT64_MAX)) / ( (double) UINT64_MAX);
2626 if (random_number < random_ctx->percentage)
2628 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2630 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2631 uid_from_hash(key, &second_pos);
2632 hash_from_uid(random_ctx->first_uid, &first_hash);
2633 GNUNET_assert(random_ctx->pg->total > second_pos);
2634 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2640 * Iterator for adding at least X peers to a peers connection set.
2642 * @param cls closure, MinimumContext
2643 * @param key the key the second Daemon was stored under
2644 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2646 * @return GNUNET_YES to continue iteration
2649 minimum_connect_iterator (void *cls,
2650 const GNUNET_HashCode * key,
2653 struct MinimumContext *min_ctx = cls;
2654 uint32_t second_pos;
2655 GNUNET_HashCode first_hash;
2658 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2660 for (i = 0; i < min_ctx->num_to_add; i++)
2662 if (min_ctx->pg_array[i] == min_ctx->current)
2664 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2665 uid_from_hash(key, &second_pos);
2666 hash_from_uid(min_ctx->first_uid, &first_hash);
2667 GNUNET_assert(min_ctx->pg->total > second_pos);
2668 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));
2669 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2670 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2677 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2683 * Iterator for adding peers to a connection set based on a depth first search.
2685 * @param cls closure, MinimumContext
2686 * @param key the key the second daemon was stored under
2687 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2689 * @return GNUNET_YES to continue iteration
2692 dfs_connect_iterator (void *cls,
2693 const GNUNET_HashCode * key,
2696 struct DFSContext *dfs_ctx = cls;
2697 GNUNET_HashCode first_hash;
2699 if (dfs_ctx->current == dfs_ctx->chosen)
2701 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2702 uid_from_hash(key, &dfs_ctx->second_uid);
2703 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2704 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));
2705 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2706 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2707 return GNUNET_NO; /* We have found our peer, don't iterate more */
2716 * From the set of connections possible, choose percentage percent of connections
2717 * to actually connect.
2719 * @param pg the peergroup we are dealing with
2720 * @param percentage what percent of total connections to make
2723 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2725 struct RandomContext random_ctx;
2728 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2730 random_ctx.first_uid = pg_iter;
2731 random_ctx.first = &pg->peers[pg_iter];
2732 random_ctx.percentage = percentage;
2734 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2735 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2736 /* Now remove the old connections */
2737 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2738 /* And replace with the random set */
2739 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2744 * From the set of connections possible, choose at least num connections per
2747 * @param pg the peergroup we are dealing with
2748 * @param num how many connections at least should each peer have (if possible)?
2751 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2753 struct MinimumContext minimum_ctx;
2756 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2758 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2761 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2763 minimum_ctx.first_uid = pg_iter;
2764 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2765 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2766 minimum_ctx.first = &pg->peers[pg_iter];
2767 minimum_ctx.pg = pg;
2768 minimum_ctx.num_to_add = num;
2769 minimum_ctx.current = 0;
2770 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2771 &minimum_connect_iterator,
2775 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2777 /* Remove the "old" connections */
2778 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2779 /* And replace with the working set */
2780 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2787 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2790 unsigned int pg_iter;
2794 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2796 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2803 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2806 unsigned int pg_iter;
2810 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2812 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2819 * From the set of connections possible, choose at least num connections per
2820 * peer based on depth first traversal of peer connections. If DFS leaves
2821 * peers unconnected, ensure those peers get connections.
2823 * @param pg the peergroup we are dealing with
2824 * @param num how many connections at least should each peer have (if possible)?
2827 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2829 struct DFSContext dfs_ctx;
2832 uint32_t starting_peer;
2833 uint32_t least_connections;
2834 GNUNET_HashCode second_hash;
2836 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2838 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2843 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
2845 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
2847 least_connections = -1; /* Set to very high number */
2848 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2850 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
2852 starting_peer = pg_iter;
2853 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2858 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
2864 /* Choose a random peer from the chosen peers set of connections to add */
2865 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
2866 dfs_ctx.first_uid = starting_peer;
2867 dfs_ctx.first = &pg->peers[starting_peer];
2869 dfs_ctx.current = 0;
2871 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
2872 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
2873 hash_from_uid(dfs_ctx.second_uid, &second_hash);
2874 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
2875 starting_peer = dfs_ctx.second_uid;
2878 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2880 /* Remove the "old" connections */
2881 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2882 /* And replace with the working set */
2883 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2888 * Internal callback for topology information for a particular peer.
2891 internal_topology_callback(void *cls,
2892 const struct GNUNET_PeerIdentity *peer,
2893 struct GNUNET_TIME_Relative latency, uint32_t distance)
2895 struct CoreContext *core_ctx = cls;
2896 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
2898 if (peer == NULL) /* Either finished, or something went wrong */
2900 iter_ctx->completed++;
2901 iter_ctx->connected--;
2902 /* One core context allocated per iteration, must free! */
2903 GNUNET_free(core_ctx);
2907 iter_ctx->topology_cb(iter_ctx->cls, &core_ctx->daemon->id, peer, latency, distance, NULL);
2910 if (iter_ctx->completed == iter_ctx->total)
2912 iter_ctx->topology_cb(iter_ctx->cls, NULL, NULL, GNUNET_TIME_relative_get_zero(), 0, NULL);
2913 /* Once all are done, free the iteration context */
2914 GNUNET_free(iter_ctx);
2920 * Check running topology iteration tasks, if below max start a new one, otherwise
2921 * schedule for some time in the future.
2924 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2926 struct CoreContext *core_context = cls;
2927 struct TopologyIterateContext *topology_context = (struct TopologyIterateContext *)core_context->iter_context;
2928 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2931 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
2933 #if VERBOSE_TESTING > 2
2934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2935 _("Delaying connect, we have too many outstanding connections!\n"));
2937 GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_topology, core_context);
2941 #if VERBOSE_TESTING > 2
2942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2943 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
2945 topology_context->connected++;
2946 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->sched, core_context->daemon->cfg, &internal_topology_callback, core_context))
2947 internal_topology_callback(core_context, NULL, GNUNET_TIME_relative_get_zero(), 0);
2953 * Iterate over all (running) peers in the peer group, retrieve
2954 * all connections that each currently has.
2957 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyTopology cb, void *cls)
2959 struct TopologyIterateContext *topology_context;
2960 struct CoreContext *core_ctx;
2962 unsigned int total_count;
2964 /* Allocate a single topology iteration context */
2965 topology_context = GNUNET_malloc(sizeof(struct TopologyIterateContext));
2966 topology_context->topology_cb = cb;
2967 topology_context->cls = cls;
2969 for (i = 0; i < pg->total; i++)
2971 if (pg->peers[i].daemon->running == GNUNET_YES)
2973 /* Allocate one core context per core we need to connect to */
2974 core_ctx = GNUNET_malloc(sizeof(struct CoreContext));
2975 core_ctx->daemon = pg->peers[i].daemon;
2976 /* Set back pointer to topology iteration context */
2977 core_ctx->iter_context = topology_context;
2978 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_topology, core_ctx);
2982 topology_context->total = total_count;
2987 * Callback function to process statistic values.
2988 * This handler is here only really to insert a peer
2989 * identity (or daemon) so the statistics can be uniquely
2990 * tied to a single running peer.
2992 * @param cls closure
2993 * @param subsystem name of subsystem that created the statistic
2994 * @param name the name of the datum
2995 * @param value the current value
2996 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
2997 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
2999 static int internal_stats_callback (void *cls,
3000 const char *subsystem,
3005 struct StatsCoreContext *core_context = cls;
3006 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3008 return stats_context->proc(stats_context->cls, &core_context->daemon->id, subsystem, name, value, is_persistent);
3012 * Internal continuation call for statistics iteration.
3014 * @param cls closure, the CoreContext for this iteration
3015 * @param success whether or not the statistics iterations
3016 * was canceled or not (we don't care)
3018 static void internal_stats_cont (void *cls, int success)
3020 struct StatsCoreContext *core_context = cls;
3021 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3023 stats_context->connected--;
3024 stats_context->completed++;
3026 if (stats_context->completed == stats_context->total)
3028 stats_context->cont(stats_context->cls, GNUNET_YES);
3029 GNUNET_free(stats_context);
3032 if (core_context->stats_handle != NULL)
3033 GNUNET_STATISTICS_destroy(core_context->stats_handle, GNUNET_NO);
3035 GNUNET_free(core_context);
3039 * Check running topology iteration tasks, if below max start a new one, otherwise
3040 * schedule for some time in the future.
3043 schedule_get_statistics(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3045 struct StatsCoreContext *core_context = cls;
3046 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3048 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3051 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3053 #if VERBOSE_TESTING > 2
3054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3055 _("Delaying connect, we have too many outstanding connections!\n"));
3057 GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_statistics, core_context);
3061 #if VERBOSE_TESTING > 2
3062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3063 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3066 stats_context->connected++;
3067 core_context->stats_handle = GNUNET_STATISTICS_create(core_context->daemon->sched, "testing", core_context->daemon->cfg);
3068 if (core_context->stats_handle == NULL)
3070 internal_stats_cont (core_context, GNUNET_NO);
3074 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);
3075 if (core_context->stats_get_handle == NULL)
3076 internal_stats_cont (core_context, GNUNET_NO);
3083 * Iterate over all (running) peers in the peer group, retrieve
3084 * all statistics from each.
3087 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
3088 GNUNET_STATISTICS_Callback cont,
3089 GNUNET_TESTING_STATISTICS_Iterator proc, void *cls)
3091 struct StatsIterateContext *stats_context;
3092 struct StatsCoreContext *core_ctx;
3094 unsigned int total_count;
3096 /* Allocate a single stats iteration context */
3097 stats_context = GNUNET_malloc(sizeof(struct StatsIterateContext));
3098 stats_context->cont = cont;
3099 stats_context->proc = proc;
3100 stats_context->cls = cls;
3102 for (i = 0; i < pg->total; i++)
3104 if (pg->peers[i].daemon->running == GNUNET_YES)
3106 /* Allocate one core context per core we need to connect to */
3107 core_ctx = GNUNET_malloc(sizeof(struct StatsCoreContext));
3108 core_ctx->daemon = pg->peers[i].daemon;
3109 /* Set back pointer to topology iteration context */
3110 core_ctx->iter_context = stats_context;
3111 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_statistics, core_ctx);
3115 stats_context->total = total_count;
3120 * There are many ways to connect peers that are supported by this function.
3121 * To connect peers in the same topology that was created via the
3122 * GNUNET_TESTING_create_topology, the topology variable must be set to
3123 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
3124 * a new instance of that topology will be generated and attempted to be
3125 * connected. This could result in some connections being impossible,
3126 * because some topologies are non-deterministic.
3128 * @param pg the peer group struct representing the running peers
3129 * @param topology which topology to connect the peers in
3130 * @param options options for connecting the topology
3131 * @param option_modifier modifier for options that take a parameter
3132 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
3135 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3136 enum GNUNET_TESTING_Topology topology,
3137 enum GNUNET_TESTING_TopologyOption options,
3138 double option_modifier)
3142 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3143 #if VERBOSE_TOPOLOGY
3144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3145 _("Creating clique CONNECT topology\n"));
3147 create_clique (pg, &add_actual_connections);
3149 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3150 #if VERBOSE_TOPOLOGY
3151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3152 _("Creating small world (ring) CONNECT topology\n"));
3154 create_small_world_ring (pg, &add_actual_connections);
3156 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3157 #if VERBOSE_TOPOLOGY
3158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3159 _("Creating small world (2d-torus) CONNECT topology\n"));
3161 create_small_world (pg, &add_actual_connections);
3163 case GNUNET_TESTING_TOPOLOGY_RING:
3164 #if VERBOSE_TOPOLOGY
3165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3166 _("Creating ring CONNECT topology\n"));
3168 create_ring (pg, &add_actual_connections);
3170 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3171 #if VERBOSE_TOPOLOGY
3172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3173 _("Creating 2d torus CONNECT topology\n"));
3175 create_2d_torus (pg, &add_actual_connections);
3177 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3178 #if VERBOSE_TOPOLOGY
3179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3180 _("Creating Erdos-Renyi CONNECT topology\n"));
3182 create_erdos_renyi (pg, &add_actual_connections);
3184 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3185 #if VERBOSE_TOPOLOGY
3186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3187 _("Creating InterNAT CONNECT topology\n"));
3189 create_nated_internet (pg, &add_actual_connections);
3191 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3192 #if VERBOSE_TOPOLOGY
3193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3194 _("Creating Scale Free CONNECT topology\n"));
3196 create_scale_free (pg, &add_actual_connections);
3198 case GNUNET_TESTING_TOPOLOGY_LINE:
3199 #if VERBOSE_TOPOLOGY
3200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3201 _("Creating straight line CONNECT topology\n"));
3203 create_line (pg, &add_actual_connections);
3205 case GNUNET_TESTING_TOPOLOGY_NONE:
3206 #if VERBOSE_TOPOLOGY
3207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3208 _("Creating no CONNECT topology\n"));
3210 copy_allowed_topology(pg);
3213 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3214 _("Unknown topology specification, can't connect peers!\n"));
3215 return GNUNET_SYSERR;
3220 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
3221 #if VERBOSE_TOPOLOGY
3222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3223 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
3225 choose_random_connections(pg, option_modifier);
3227 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
3228 #if VERBOSE_TOPOLOGY
3229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3230 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3232 choose_minimum(pg, (unsigned int)option_modifier);
3234 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
3235 #if VERBOSE_TOPOLOGY
3236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3237 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3239 perform_dfs(pg, (int)option_modifier);
3241 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
3243 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
3249 return connect_topology(pg);
3253 * Callback that is called whenever a hostkey is generated
3254 * for a peer. Call the real callback and decrement the
3255 * starting counter for the peergroup.
3257 * @param cls closure
3258 * @param id identifier for the daemon, NULL on error
3259 * @param d handle for the daemon
3260 * @param emsg error message (NULL on success)
3262 static void internal_hostkey_callback (void *cls,
3263 const struct GNUNET_PeerIdentity *id,
3264 struct GNUNET_TESTING_Daemon *d,
3267 struct InternalStartContext *internal_context = cls;
3268 internal_context->peer->pg->starting--;
3269 internal_context->peer->pg->started++;
3270 if (internal_context->hostkey_callback != NULL)
3271 internal_context->hostkey_callback(internal_context->hostkey_cls, id, d, emsg);
3272 else if (internal_context->peer->pg->started == internal_context->peer->pg->total)
3274 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
3275 GNUNET_TESTING_daemons_continue_startup(internal_context->peer->pg);
3280 * Callback that is called whenever a peer has finished starting.
3281 * Call the real callback and decrement the starting counter
3282 * for the peergroup.
3284 * @param cls closure
3285 * @param id identifier for the daemon, NULL on error
3286 * @param d handle for the daemon
3287 * @param emsg error message (NULL on success)
3289 static void internal_startup_callback (void *cls,
3290 const struct GNUNET_PeerIdentity *id,
3291 const struct GNUNET_CONFIGURATION_Handle *cfg,
3292 struct GNUNET_TESTING_Daemon *d,
3295 struct InternalStartContext *internal_context = cls;
3296 internal_context->peer->pg->starting--;
3297 if (internal_context->start_cb != NULL)
3298 internal_context->start_cb(internal_context->start_cb_cls, id, cfg, d, emsg);
3302 internal_continue_startup (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3304 struct InternalStartContext *internal_context = cls;
3306 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3311 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
3313 internal_context->peer->pg->starting++;
3314 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
3318 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_continue_startup, internal_context);
3323 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3325 struct InternalStartContext *internal_context = cls;
3327 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3332 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
3334 internal_context->peer->pg->starting++;
3335 internal_context->peer->daemon = GNUNET_TESTING_daemon_start (internal_context->peer->pg->sched,
3336 internal_context->peer->cfg,
3337 internal_context->timeout,
3338 internal_context->hostname,
3339 internal_context->username,
3340 internal_context->sshport,
3341 &internal_hostkey_callback,
3343 &internal_startup_callback,
3348 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_start, internal_context);
3353 * Function which continues a peer group starting up
3354 * after successfully generating hostkeys for each peer.
3356 * @param pg the peer group to continue starting
3360 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
3365 for (i = 0; i < pg->total; i++)
3367 GNUNET_SCHEDULER_add_now (pg->sched, &internal_continue_startup, &pg->peers[i].internal_context);
3368 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
3373 * Start count gnunet instances with the same set of transports and
3374 * applications. The port numbers (any option called "PORT") will be
3375 * adjusted to ensure that no two peers running on the same system
3376 * have the same port(s) in their respective configurations.
3378 * @param sched scheduler to use
3379 * @param cfg configuration template to use
3380 * @param total number of daemons to start
3381 * @param timeout total time allowed for peers to start
3382 * @param hostkey_callback function to call on each peers hostkey generation
3383 * if NULL, peers will be started by this call, if non-null,
3384 * GNUNET_TESTING_daemons_continue_startup must be called after
3385 * successful hostkey generation
3386 * @param hostkey_cls closure for hostkey callback
3387 * @param cb function to call on each daemon that was started
3388 * @param cb_cls closure for cb
3389 * @param connect_callback function to call each time two hosts are connected
3390 * @param connect_callback_cls closure for connect_callback
3391 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
3393 * @return NULL on error, otherwise handle to control peer group
3395 struct GNUNET_TESTING_PeerGroup *
3396 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
3397 const struct GNUNET_CONFIGURATION_Handle *cfg,
3399 struct GNUNET_TIME_Relative timeout,
3400 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
3402 GNUNET_TESTING_NotifyDaemonRunning cb,
3404 GNUNET_TESTING_NotifyConnection
3405 connect_callback, void *connect_callback_cls,
3406 const struct GNUNET_TESTING_Host *hostnames)
3408 struct GNUNET_TESTING_PeerGroup *pg;
3409 const struct GNUNET_TESTING_Host *hostpos;
3415 const char *hostname;
3416 const char *username;
3417 char *baseservicehome;
3418 char *newservicehome;
3420 struct GNUNET_CONFIGURATION_Handle *pcfg;
3422 unsigned int hostcnt;
3435 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
3438 pg->notify_connection = connect_callback;
3439 pg->notify_connection_cls = connect_callback_cls;
3441 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
3442 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
3443 if (NULL != hostnames)
3446 hostpos = hostnames;
3447 while (hostpos != NULL)
3449 hostpos = hostpos->next;
3452 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3455 hostpos = hostnames;
3456 while (hostpos != NULL)
3458 pg->hosts[off].minport = LOW_PORT;
3460 pg->hosts[off].hostname = GNUNET_strdup(hostpos->hostname);
3461 if (hostpos->username != NULL)
3462 pg->hosts[off].username = GNUNET_strdup(hostpos->username);
3463 pg->hosts[off].sshport = hostpos->port;
3464 hostpos = hostpos->next;
3469 GNUNET_free (pg->hosts);
3477 /* skip leading spaces */
3478 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
3481 while ('\0' != *rpos)
3483 if (isspace ( (unsigned char) *rpos))
3487 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3489 start = GNUNET_strdup (hostnames);
3491 while ('\0' != *pos)
3493 if (isspace ( (unsigned char) *pos))
3496 if (strlen (start) > 0)
3498 pg->hosts[off].minport = LOW_PORT;
3499 pg->hosts[off++].hostname = start;
3505 if (strlen (start) > 0)
3507 pg->hosts[off].minport = LOW_PORT;
3508 pg->hosts[off++].hostname = start;
3512 GNUNET_free (start);
3513 GNUNET_free (pg->hosts);
3517 minport = 0; /* make gcc happy */
3525 for (off = 0; off < total; off++)
3529 hostname = pg->hosts[off % hostcnt].hostname;
3530 username = pg->hosts[off % hostcnt].username;
3531 sshport = pg->hosts[off % hostcnt].sshport;
3532 pcfg = make_config (cfg,
3533 &pg->hosts[off % hostcnt].minport,
3542 pcfg = make_config (cfg,
3550 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3552 ("Could not create configuration for peer number %u on `%s'!\n"),
3553 off, hostname == NULL ? "localhost" : hostname);
3558 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
3561 GNUNET_asprintf (&newservicehome,
3562 "%s/%d/", baseservicehome, off);
3563 GNUNET_free (baseservicehome);
3567 tmpdir = getenv ("TMPDIR");
3568 tmpdir = tmpdir ? tmpdir : "/tmp";
3569 GNUNET_asprintf (&newservicehome,
3572 "gnunet-testing-test-test", off);
3574 GNUNET_CONFIGURATION_set_value_string (pcfg,
3576 "SERVICEHOME", newservicehome);
3577 GNUNET_free (newservicehome);
3578 pg->peers[off].cfg = pcfg;
3579 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
3580 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
3581 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
3582 pg->peers[off].pg = pg;
3584 pg->peers[off].internal_context.peer = &pg->peers[off];
3585 pg->peers[off].internal_context.timeout = timeout;
3586 pg->peers[off].internal_context.hostname = hostname;
3587 pg->peers[off].internal_context.username = username;
3588 pg->peers[off].internal_context.sshport = sshport;
3589 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
3590 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
3591 pg->peers[off].internal_context.start_cb = cb;
3592 pg->peers[off].internal_context.start_cb_cls = cb_cls;
3594 GNUNET_SCHEDULER_add_now (sched, &internal_start, &pg->peers[off].internal_context);
3601 * Get a daemon by number, so callers don't have to do nasty
3602 * offsetting operation.
3604 struct GNUNET_TESTING_Daemon *
3605 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
3607 if (position < pg->total)
3608 return pg->peers[position].daemon;
3614 * Get a daemon by peer identity, so callers can
3615 * retrieve the daemon without knowing it's offset.
3617 * @param pg the peer group to retrieve the daemon from
3618 * @param peer_id the peer identity of the daemon to retrieve
3620 * @return the daemon on success, or NULL if no such peer identity is found
3622 struct GNUNET_TESTING_Daemon *
3623 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_PeerIdentity *peer_id)
3627 for (i = 0; i < pg->total; i ++)
3629 if (0 == memcmp(&pg->peers[i].daemon->id, peer_id, sizeof(struct GNUNET_PeerIdentity)))
3630 return pg->peers[i].daemon;
3637 * Prototype of a function that will be called when a
3638 * particular operation was completed the testing library.
3640 * @param cls closure (a struct RestartContext)
3641 * @param id id of the peer that was restarted
3642 * @param cfg handle to the configuration of the peer
3643 * @param d handle to the daemon that was restarted
3644 * @param emsg NULL on success
3646 void restart_callback (void *cls,
3647 const struct GNUNET_PeerIdentity *id,
3648 const struct GNUNET_CONFIGURATION_Handle *cfg,
3649 struct GNUNET_TESTING_Daemon *d,
3652 struct RestartContext *restart_context = cls;
3656 restart_context->peers_restarted++;
3660 restart_context->peers_restart_failed++;
3663 if (restart_context->peers_restarted == restart_context->peer_group->total)
3665 restart_context->callback(restart_context->callback_cls, NULL);
3666 GNUNET_free(restart_context);
3668 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
3670 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
3671 GNUNET_free(restart_context);
3677 * Callback for informing us about a successful
3678 * or unsuccessful churn stop call.
3680 * @param cls a ChurnContext
3681 * @param emsg NULL on success, non-NULL on failure
3685 churn_stop_callback (void *cls, const char *emsg)
3687 struct ChurnContext *churn_ctx = cls;
3688 unsigned int total_left;
3689 char *error_message;
3691 error_message = NULL;
3694 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3695 "Churn stop callback failed with error `%s'\n", emsg);
3696 churn_ctx->num_failed_stop++;
3700 churn_ctx->num_to_stop--;
3704 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3705 "Stopped peer, %d left.\n",
3706 churn_ctx->num_to_stop);
3708 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3710 if (total_left == 0)
3712 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3714 GNUNET_asprintf(&error_message,
3715 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3716 churn_ctx->num_failed_start,
3717 churn_ctx->num_failed_stop);
3719 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3720 GNUNET_free_non_null(error_message);
3721 GNUNET_free(churn_ctx);
3726 * Callback for informing us about a successful
3727 * or unsuccessful churn start call.
3729 * @param cls a ChurnContext
3730 * @param id the peer identity of the started peer
3731 * @param cfg the handle to the configuration of the peer
3732 * @param d handle to the daemon for the peer
3733 * @param emsg NULL on success, non-NULL on failure
3737 churn_start_callback (void *cls,
3738 const struct GNUNET_PeerIdentity *id,
3739 const struct GNUNET_CONFIGURATION_Handle *cfg,
3740 struct GNUNET_TESTING_Daemon *d,
3743 struct ChurnContext *churn_ctx = cls;
3744 unsigned int total_left;
3745 char *error_message;
3747 error_message = NULL;
3750 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3751 "Churn stop callback failed with error `%s'\n",
3753 churn_ctx->num_failed_start++;
3757 churn_ctx->num_to_start--;
3761 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3762 "Started peer, %d left.\n",
3763 churn_ctx->num_to_start);
3766 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3768 if (total_left == 0)
3770 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3771 GNUNET_asprintf(&error_message,
3772 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3773 churn_ctx->num_failed_start,
3774 churn_ctx->num_failed_stop);
3775 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3776 GNUNET_free_non_null(error_message);
3777 GNUNET_free(churn_ctx);
3782 * Count the number of running peers.
3784 * @param pg handle for the peer group
3786 * @return the number of currently running peers in the peer group
3789 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
3792 unsigned int running = 0;
3793 for (i = 0; i < pg->total; i++)
3795 if (pg->peers[i].daemon->running == GNUNET_YES)
3797 GNUNET_assert(running != -1);
3805 * Simulate churn by stopping some peers (and possibly
3806 * re-starting others if churn is called multiple times). This
3807 * function can only be used to create leave-join churn (peers "never"
3808 * leave for good). First "voff" random peers that are currently
3809 * online will be taken offline; then "von" random peers that are then
3810 * offline will be put back online. No notifications will be
3811 * generated for any of these operations except for the callback upon
3814 * @param pg handle for the peer group
3815 * @param voff number of peers that should go offline
3816 * @param von number of peers that should come back online;
3817 * must be zero on first call (since "testbed_start"
3818 * always starts all of the peers)
3819 * @param timeout how long to wait for operations to finish before
3821 * @param cb function to call at the end
3822 * @param cb_cls closure for cb
3825 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
3828 struct GNUNET_TIME_Relative timeout,
3829 GNUNET_TESTING_NotifyCompletion cb,
3832 struct ChurnContext *churn_ctx;
3833 unsigned int running;
3834 unsigned int stopped;
3835 unsigned int total_running;
3836 unsigned int total_stopped;
3838 unsigned int *running_arr;
3839 unsigned int *stopped_arr;
3840 unsigned int *running_permute;
3841 unsigned int *stopped_permute;
3846 if ((von == 0) && (voff == 0)) /* No peers at all? */
3852 for (i = 0; i < pg->total; i++)
3854 if (pg->peers[i].daemon->running == GNUNET_YES)
3856 GNUNET_assert(running != -1);
3861 GNUNET_assert(stopped != -1);
3868 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
3869 cb(cb_cls, "Trying to stop more peers than are currently running!");
3875 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
3876 cb(cb_cls, "Trying to start more peers than are currently stopped!");
3880 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3884 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
3888 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
3890 running_permute = NULL;
3891 stopped_permute = NULL;
3894 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
3896 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
3898 total_running = running;
3899 total_stopped = stopped;
3903 churn_ctx->num_to_start = von;
3904 churn_ctx->num_to_stop = voff;
3906 churn_ctx->cb_cls = cb_cls;
3908 for (i = 0; i < pg->total; i++)
3910 if (pg->peers[i].daemon->running == GNUNET_YES)
3912 GNUNET_assert((running_arr != NULL) && (total_running > running));
3913 running_arr[running] = i;
3918 GNUNET_assert((stopped_arr != NULL) && (total_stopped > stopped));
3919 stopped_arr[stopped] = i;
3924 GNUNET_assert(running >= voff);
3925 for (i = 0; i < voff; i++)
3928 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
3930 GNUNET_assert(running_arr != NULL);
3931 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
3933 &churn_stop_callback, churn_ctx,
3934 GNUNET_NO, GNUNET_YES);
3937 GNUNET_assert(stopped >= von);
3938 for (i = 0; i < von; i++)
3941 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
3943 GNUNET_assert(stopped_arr != NULL);
3944 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
3945 timeout, &churn_start_callback, churn_ctx);
3948 GNUNET_free_non_null(running_arr);
3949 GNUNET_free_non_null(stopped_arr);
3950 GNUNET_free_non_null(running_permute);
3951 GNUNET_free_non_null(stopped_permute);
3956 * Restart all peers in the given group.
3958 * @param pg the handle to the peer group
3959 * @param callback function to call on completion (or failure)
3960 * @param callback_cls closure for the callback function
3963 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
3964 GNUNET_TESTING_NotifyCompletion callback,
3967 struct RestartContext *restart_context;
3972 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
3973 restart_context->peer_group = pg;
3974 restart_context->peers_restarted = 0;
3975 restart_context->callback = callback;
3976 restart_context->callback_cls = callback_cls;
3978 for (off = 0; off < pg->total; off++)
3980 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
3986 * Start or stop an individual peer from the given group.
3988 * @param pg handle to the peer group
3989 * @param offset which peer to start or stop
3990 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
3991 * @param timeout how long to wait for shutdown
3992 * @param cb function to call at the end
3993 * @param cb_cls closure for cb
3996 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
3997 unsigned int offset,
3999 struct GNUNET_TIME_Relative timeout,
4000 GNUNET_TESTING_NotifyCompletion cb,
4003 struct ChurnContext *churn_ctx;
4005 if (GNUNET_NO == desired_status)
4007 if (NULL != pg->peers[offset].daemon)
4009 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4010 churn_ctx->num_to_start = 0;
4011 churn_ctx->num_to_stop = 1;
4013 churn_ctx->cb_cls = cb_cls;
4014 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
4015 timeout, &churn_stop_callback, churn_ctx,
4016 GNUNET_NO, GNUNET_YES);
4019 else if (GNUNET_YES == desired_status)
4021 if (NULL == pg->peers[offset].daemon)
4023 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4024 churn_ctx->num_to_start = 1;
4025 churn_ctx->num_to_stop = 0;
4027 churn_ctx->cb_cls = cb_cls;
4028 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
4029 timeout, &churn_start_callback, churn_ctx);
4038 * Callback for shutting down peers in a peer group.
4040 * @param cls closure (struct ShutdownContext)
4041 * @param emsg NULL on success
4043 void internal_shutdown_callback (void *cls,
4046 struct ShutdownContext *shutdown_ctx = cls;
4048 shutdown_ctx->outstanding--;
4051 shutdown_ctx->peers_down++;
4055 shutdown_ctx->peers_failed++;
4058 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
4060 if (shutdown_ctx->peers_failed > 0)
4061 shutdown_ctx->cb(shutdown_ctx->cb_cls, "Not all peers successfully shut down!");
4063 shutdown_ctx->cb(shutdown_ctx->cb_cls, NULL);
4064 GNUNET_free(shutdown_ctx);
4069 * Individual shutdown context for a particular peer.
4071 struct PeerShutdownContext
4074 * Pointer to the high level shutdown context.
4076 struct ShutdownContext *shutdown_ctx;
4079 * The daemon handle for the peer to shut down.
4081 struct GNUNET_TESTING_Daemon *daemon;
4085 * Task to rate limit the number of outstanding peer shutdown
4086 * requests. This is necessary for making sure we don't do
4087 * too many ssh connections at once, but is generally nicer
4088 * to any system as well (graduated task starts, as opposed
4089 * to calling gnunet-arm N times all at once).
4092 schedule_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
4094 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4095 struct ShutdownContext *shutdown_ctx;
4097 GNUNET_assert(peer_shutdown_ctx != NULL);
4098 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4099 GNUNET_assert(shutdown_ctx != NULL);
4101 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4102 GNUNET_SCHEDULER_add_delayed(peer_shutdown_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_shutdown_task, peer_shutdown_ctx);
4105 shutdown_ctx->outstanding++;
4106 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, &internal_shutdown_callback, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4107 GNUNET_free(peer_shutdown_ctx);
4111 * Shutdown all peers started in the given group.
4113 * @param pg handle to the peer group
4114 * @param timeout how long to wait for shutdown
4115 * @param cb callback to notify upon success or failure
4116 * @param cb_cls closure for cb
4119 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
4120 struct GNUNET_TIME_Relative timeout,
4121 GNUNET_TESTING_NotifyCompletion cb,
4125 struct ShutdownContext *shutdown_ctx;
4126 struct PeerShutdownContext *peer_shutdown_ctx;
4128 GNUNET_assert(pg->total > 0);
4130 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4131 shutdown_ctx->cb = cb;
4132 shutdown_ctx->cb_cls = cb_cls;
4133 shutdown_ctx->total_peers = pg->total;
4134 shutdown_ctx->timeout = timeout;
4135 /* shtudown_ctx->outstanding = 0; */
4137 for (off = 0; off < pg->total; off++)
4139 GNUNET_assert(NULL != pg->peers[off].daemon);
4140 peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4141 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
4142 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4143 GNUNET_SCHEDULER_add_now(pg->peers[off].daemon->sched, &schedule_shutdown_task, peer_shutdown_ctx);
4144 //GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4145 if (NULL != pg->peers[off].cfg)
4146 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
4147 if (pg->peers[off].allowed_peers != NULL)
4148 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
4149 if (pg->peers[off].connect_peers != NULL)
4150 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
4151 if (pg->peers[off].blacklisted_peers != NULL)
4152 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
4154 GNUNET_free (pg->peers);
4155 if (NULL != pg->hosts)
4157 GNUNET_free (pg->hosts[0].hostname);
4158 GNUNET_free (pg->hosts);
4164 /* end of testing_group.c */