2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing_group.c
23 * @brief convenience API for writing testcases for GNUnet
24 * @author Christian Grothoff
26 * FIXME: have connection processor functions take a cls argument
27 * which specifies where to write the connection information
28 * instead of assuming it's certain peergroup places. (maybe?)
29 * FIXME: create static struct which contains the TOPOLOGY enum, the
30 * associated string, and the function used to create it.
31 * Then replace the create_X calls with topology_struct[i][2]
32 * or something. (Store function pointers instead of using
36 #include "gnunet_arm_service.h"
37 #include "gnunet_testing_lib.h"
39 #define VERBOSE_TESTING GNUNET_NO
41 #define VERBOSE_TOPOLOGY GNUNET_NO
43 #define DEBUG_CHURN GNUNET_NO
46 * Lowest port used for GNUnet testing. Should be high enough to not
47 * conflict with other applications running on the hosts but be low
48 * enough to not conflict with client-ports (typically starting around
51 #define LOW_PORT 10000
54 * Highest port used for GNUnet testing. Should be low enough to not
55 * conflict with the port range for "local" ports (client apps; see
56 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
58 #define HIGH_PORT 32000
60 #define MAX_OUTSTANDING_CONNECTIONS 50
62 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
64 #define CONNECT_ATTEMPTS 8
67 * Prototype of a function called whenever two peers would be connected
68 * in a certain topology.
70 typedef int (*GNUNET_TESTING_ConnectionProcessor)
71 (struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second);
74 * Strings representing topologies in enum
76 static char * GNUNET_TESTING_TopologyStrings[] =
79 * A clique (everyone connected to everyone else).
84 * Small-world network (2d torus plus random links).
89 * Small-world network (ring plus random links).
109 * Certain percentage of peers are unable to communicate directly
110 * replicating NAT conditions
115 * Scale free topology.
120 * Straight line topology.
125 * All peers are disconnected.
131 * Options for connecting a topology as strings.
133 static char * GNUNET_TESTING_TopologyOptionStrings[] =
136 * Try to connect all peers specified in the topology.
141 * Choose a random subset of connections to create.
143 "CONNECT_RANDOM_SUBSET",
146 * Create at least X connections for each peer.
151 * Using a depth first search, create one connection
152 * per peer. If any are missed (graph disconnected)
153 * start over at those peers until all have at least one
159 * No options specified.
165 * Context for handling churning a peer group
170 * Callback used to notify of churning finished
172 GNUNET_TESTING_NotifyCompletion cb;
175 * Closure for callback
180 * Number of peers that still need to be started
182 unsigned int num_to_start;
185 * Number of peers that still need to be stopped
187 unsigned int num_to_stop;
190 * Number of peers that failed to start
192 unsigned int num_failed_start;
195 * Number of peers that failed to stop
197 unsigned int num_failed_stop;
200 struct RestartContext
203 * The group of peers being restarted
205 struct GNUNET_TESTING_PeerGroup *peer_group;
208 * How many peers have been restarted thus far
210 unsigned int peers_restarted;
213 * How many peers got an error when restarting
215 unsigned int peers_restart_failed;
218 * The function to call once all peers have been restarted
220 GNUNET_TESTING_NotifyCompletion callback;
223 * Closure for callback function
229 struct CreateTopologyContext
233 * Function to call with number of connections
235 GNUNET_TESTING_NotifyConnections cont;
238 * Closure for connection notification
244 struct PeerConnection
249 struct PeerConnection *next;
252 * Pointer to daemon handle
254 struct GNUNET_TESTING_Daemon *daemon;
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 * Linked list of peer connections (pointers)
285 //struct PeerConnection *connected_peers;
287 * Hash map of allowed peer connections (F2F created topology)
289 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
292 * Hash map of blacklisted peers
294 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
297 * Hash map of peer connections
299 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
302 * Temporary hash map of peer connections
304 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
307 * Temporary variable for topology creation, should be reset before
308 * creating any topology so the count is valid once finished.
315 * Data we keep per host.
325 * Lowest port that we have not yet used
333 * Handle to a group of GNUnet peers.
335 struct GNUNET_TESTING_PeerGroup
340 struct GNUNET_SCHEDULER_Handle *sched;
343 * Configuration template.
345 const struct GNUNET_CONFIGURATION_Handle *cfg;
348 * Function to call on each started daemon.
350 GNUNET_TESTING_NotifyDaemonRunning cb;
358 * Function to call on each topology connection created
360 GNUNET_TESTING_NotifyConnection notify_connection;
363 * Callback for notify_connection
365 void *notify_connection_cls;
368 * NULL-terminated array of information about
371 struct HostData *hosts;
374 * Array of "total" peers.
376 struct PeerData *peers;
379 * Number of peers in this group.
384 * At what time should we fail the peer startup process?
386 struct GNUNET_TIME_Absolute max_timeout;
390 * Convert unique ID to hash code.
392 * @param uid unique ID to convert
393 * @param hash set to uid (extended with zeros)
396 hash_from_uid (uint32_t uid,
397 GNUNET_HashCode *hash)
399 memset (hash, 0, sizeof(GNUNET_HashCode));
400 *((uint32_t*)hash) = uid;
404 * Convert hash code to unique ID.
406 * @param uid unique ID to convert
407 * @param hash set to uid (extended with zeros)
410 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
412 memcpy (uid, hash, sizeof(uint32_t));
417 struct GNUNET_CONFIGURATION_Handle *ret;
418 const char *hostname;
424 struct ConnectContext
426 struct GNUNET_TESTING_Daemon *first;
428 struct GNUNET_TESTING_Daemon *second;
430 struct GNUNET_TESTING_PeerGroup *pg;
434 * Number of connects we are waiting on, allows us to rate limit
437 static int outstanding_connects;
440 * Get a topology from a string input.
442 * @param topology where to write the retrieved topology
443 * @param topology_string The string to attempt to
444 * get a configuration value from
445 * @return GNUNET_YES if topology string matched a
446 * known topology, GNUNET_NO if not
449 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string)
454 if (topology_string == NULL)
459 if (strcmp(GNUNET_TESTING_TopologyStrings[curr], topology_string) == 0)
465 } while (strcmp(GNUNET_TESTING_TopologyStrings[curr], "NONE") != 0);
474 * Get connect topology option from string input.
476 * @param topology where to write the retrieved topology
477 * @param topology_string The string to attempt to
478 * get a configuration value from
479 * @return GNUNET_YES if string matched a known
480 * topology option, GNUNET_NO if not
483 GNUNET_TESTING_topology_option_get(enum GNUNET_TESTING_TopologyOption *topology, char * topology_string)
488 if (topology_string == NULL)
493 if (strcmp(GNUNET_TESTING_TopologyOptionStrings[curr], topology_string) == 0)
499 } while (strcmp(GNUNET_TESTING_TopologyOptionStrings[curr], "CONNECT_NONE") != 0);
508 * Function to iterate over options. Copies
509 * the options to the target configuration,
510 * updating PORT values as needed.
513 * @param section name of the section
514 * @param option name of the option
515 * @param value value of the option
518 update_config (void *cls,
519 const char *section, const char *option, const char *value)
521 struct UpdateContext *ctx = cls;
526 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
530 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
535 if (0 == strcmp (option, "UNIXPATH"))
537 GNUNET_snprintf (uval,
539 "/tmp/test-service-%s-%u",
545 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
547 value = ctx->hostname;
550 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
555 * Create a new configuration using the given configuration
556 * as a template; however, each PORT in the existing cfg
557 * must be renumbered by incrementing "*port". If we run
558 * out of "*port" numbers, return NULL.
560 * @param cfg template configuration
561 * @param port port numbers to use, update to reflect
562 * port numbers that were used
563 * @param upnum number to make unix domain socket names unique
564 * @param hostname hostname of the controlling host, to allow control connections from
566 * @return new configuration, NULL on error
568 static struct GNUNET_CONFIGURATION_Handle *
569 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
572 const char *hostname)
574 struct UpdateContext uc;
582 uc.ret = GNUNET_CONFIGURATION_create ();
583 uc.hostname = hostname;
585 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
586 if (uc.nport >= HIGH_PORT)
589 GNUNET_CONFIGURATION_destroy (uc.ret);
593 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
595 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
596 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
597 GNUNET_free_non_null(control_host);
598 GNUNET_free(allowed_hosts);
602 /* arm needs to know to allow connections from the host on which it is running,
603 * otherwise gnunet-arm is unable to connect to it in some instances */
604 if (hostname != NULL)
606 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
607 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
608 GNUNET_free(allowed_hosts);
611 *port = (uint16_t) uc.nport;
618 * Add entries to the peers connect list
620 * @param pg the peer group we are working with
621 * @param first index of the first peer
622 * @param second index of the second peer
624 * @return the number of connections added (can be 0, 1 or 2)
625 * technically should only be 0 or 2, but the small price
626 * of iterating over the lists (hashmaps in the future)
627 * for being sure doesn't bother me!
631 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
637 GNUNET_HashCode hash_first;
638 GNUNET_HashCode hash_second;
640 hash_from_uid(first, &hash_first);
641 hash_from_uid(second, &hash_second);
643 add_first = GNUNET_NO;
644 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
646 add_first = GNUNET_YES;
649 add_second = GNUNET_NO;
650 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
652 add_second = GNUNET_YES;
658 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
659 pg->peers[first].num_connections++;
665 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
666 pg->peers[second].num_connections++;
675 * Add entries to the peers allowed connections list
677 * @param pg the peer group we are working with
678 * @param first index of the first peer
679 * @param second index of the second peer
681 * @return the number of connections added (can be 0, 1 or 2)
682 * technically should only be 0 or 2, but the small price
683 * of iterating over the lists (hashmaps in the future)
684 * for being sure doesn't bother me!
688 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
692 struct PeerConnection *first_iter;
693 struct PeerConnection *second_iter;
694 struct PeerConnection *new_first;
695 struct PeerConnection *new_second;
700 GNUNET_HashCode hash_first;
701 GNUNET_HashCode hash_second;
703 hash_from_uid(first, &hash_first);
704 hash_from_uid(second, &hash_second);
706 add_first = GNUNET_NO;
707 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
709 add_first = GNUNET_YES;
712 add_second = GNUNET_NO;
713 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
715 add_second = GNUNET_YES;
718 first_iter = pg->peers[first].connected_peers;
719 while (first_iter != NULL)
721 if (first_iter->daemon == pg->peers[second].daemon)
722 add_first = GNUNET_NO;
723 first_iter = first_iter->next;
726 second_iter = pg->peers[second].connected_peers;
727 add_second = GNUNET_YES;
728 while (second_iter != NULL)
730 if (second_iter->daemon == pg->peers[first].daemon)
731 add_second = GNUNET_NO;
732 second_iter = second_iter->next;
739 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
741 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
742 new_first->daemon = pg->peers[second].daemon;
743 new_first->next = pg->peers[first].connected_peers;
744 pg->peers[first].connected_peers = new_first;
746 pg->peers[first].num_connections++;
752 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
754 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
755 new_second->daemon = pg->peers[first].daemon;
756 new_second->next = pg->peers[second].connected_peers;
757 pg->peers[second].connected_peers = new_second;
758 pg->peers[first].num_connections++;
760 pg->peers[second].num_connections++;
768 * Add entries to the peers blacklisted list
770 * @param pg the peer group we are working with
771 * @param first index of the first peer
772 * @param second index of the second peer
774 * @return the number of connections added (can be 0, 1 or 2)
778 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
783 GNUNET_HashCode hash_first;
784 GNUNET_HashCode hash_second;
786 hash_from_uid(first, &hash_first);
787 hash_from_uid(second, &hash_second);
789 add_first = GNUNET_NO;
790 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
792 add_first = GNUNET_YES;
795 add_second = GNUNET_NO;
796 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
798 add_second = GNUNET_YES;
804 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
805 pg->peers[first].num_connections++;
811 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
812 pg->peers[second].num_connections++;
820 * Remove entries from the peers blacklisted list
822 * @param pg the peer group we are working with
823 * @param first index of the first peer
824 * @param second index of the second peer
826 * @return the number of connections removed (can be 0, 1 or 2)
830 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
835 GNUNET_HashCode hash_first;
836 GNUNET_HashCode hash_second;
838 hash_from_uid(first, &hash_first);
839 hash_from_uid(second, &hash_second);
841 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
842 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
847 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
853 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
861 * Scale free network construction as described in:
863 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
865 * Start with a network of "one" peer, then progressively add
866 * peers up to the total number. At each step, iterate over
867 * all possible peers and connect new peer based on number of
868 * existing connections of the target peer.
870 * @param pg the peer group we are dealing with
871 * @param proc the connection processor to use
873 * @return the number of connections created
876 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
879 unsigned int total_connections;
880 unsigned int outer_count;
882 unsigned int previous_total_connections;
886 GNUNET_assert(pg->total > 1);
888 /* Add a connection between the first two nodes */
889 total_connections = proc(pg, 0, 1);
891 for (outer_count = 1; outer_count < pg->total; outer_count++)
893 previous_total_connections = total_connections;
894 for (i = 0; i < outer_count; i++)
896 probability = pg->peers[i].num_connections / (double)previous_total_connections;
897 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
898 UINT64_MAX)) / ( (double) UINT64_MAX);
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "Considering connecting peer %d to peer %d\n",
904 if (random < probability)
907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
908 "Connecting peer %d to peer %d\n",
911 total_connections += proc(pg, outer_count, i);
916 return total_connections;
920 * Create a topology given a peer group (set of running peers)
921 * and a connection processor.
923 * @param pg the peergroup to create the topology on
924 * @param proc the connection processor to call to actually set
925 * up connections between two peers
927 * @return the number of connections that were set up
931 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
936 unsigned int randomPeer;
937 double random, logNModifier, percentage;
938 unsigned int smallWorldConnections;
944 int connect_attempts;
946 logNModifier = 0.5; /* FIXME: default value? */
947 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
952 if (sscanf(p_string, "%lf", &logNModifier) != 1)
953 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
954 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
958 GNUNET_free (p_string);
960 percentage = 0.5; /* FIXME: default percentage? */
961 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
966 if (sscanf(p_string, "%lf", &percentage) != 1)
967 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
968 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
972 GNUNET_free (p_string);
974 natLog = log (pg->total);
975 connsPerPeer = ceil (natLog * logNModifier);
977 if (connsPerPeer % 2 == 1)
980 smallWorldConnections = 0;
981 connect_attempts = 0;
982 for (i = 0; i < pg->total; i++)
985 max = i + connsPerPeer / 2;
986 min = i - connsPerPeer / 2;
988 if (max > pg->total - 1)
990 max = max - pg->total;
996 min = pg->total - 1 + min;
1000 for (j = 0; j < connsPerPeer / 2; j++)
1002 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1003 UINT64_MAX) / ( (double) UINT64_MAX));
1004 if (random < percentage)
1006 /* Connect to uniformly selected random peer */
1008 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1010 while ((((randomPeer < max) && (randomPeer > min))
1011 && (useAnd == 0)) || (((randomPeer > min)
1012 || (randomPeer < max))
1016 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1019 smallWorldConnections +=
1020 proc (pg, i, randomPeer);
1024 nodeToConnect = i + j + 1;
1025 if (nodeToConnect > pg->total - 1)
1027 nodeToConnect = nodeToConnect - pg->total;
1030 proc (pg, i, nodeToConnect);
1036 connect_attempts += smallWorldConnections;
1038 return connect_attempts;
1042 * Create a topology given a peer group (set of running peers)
1043 * and a connection processor.
1045 * @param pg the peergroup to create the topology on
1046 * @param proc the connection processor to call to actually set
1047 * up connections between two peers
1049 * @return the number of connections that were set up
1053 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1055 unsigned int outer_count, inner_count;
1056 unsigned int cutoff;
1057 int connect_attempts;
1058 double nat_percentage;
1061 nat_percentage = 0.6; /* FIXME: default percentage? */
1062 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1067 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1068 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1069 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1073 GNUNET_free (p_string);
1078 cutoff = (unsigned int) (nat_percentage * pg->total);
1080 connect_attempts = 0;
1082 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1084 for (inner_count = outer_count + 1; inner_count < pg->total;
1087 if ((outer_count > cutoff) || (inner_count > cutoff))
1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1091 "Connecting peer %d to peer %d\n",
1092 outer_count, inner_count);
1094 connect_attempts += proc(pg, outer_count, inner_count);
1099 return connect_attempts;
1104 * Create a topology given a peer group (set of running peers)
1105 * and a connection processor.
1107 * @param pg the peergroup to create the topology on
1108 * @param proc the connection processor to call to actually set
1109 * up connections between two peers
1111 * @return the number of connections that were set up
1115 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1117 unsigned int i, j, k;
1118 unsigned int square;
1121 unsigned int toggle = 1;
1122 unsigned int nodeToConnect;
1123 unsigned int natLog;
1124 unsigned int node1Row;
1125 unsigned int node1Col;
1126 unsigned int node2Row;
1127 unsigned int node2Col;
1128 unsigned int distance;
1129 double probability, random, percentage;
1130 unsigned int smallWorldConnections;
1132 int connect_attempts;
1133 square = floor (sqrt (pg->total));
1137 percentage = 0.5; /* FIXME: default percentage? */
1138 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1143 if (sscanf(p_string, "%lf", &percentage) != 1)
1144 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1145 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1149 GNUNET_free (p_string);
1151 probability = 0.5; /* FIXME: default percentage? */
1152 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1157 if (sscanf(p_string, "%lf", &probability) != 1)
1158 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1159 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1163 GNUNET_free (p_string);
1165 if (square * square != pg->total)
1167 while (rows * cols < pg->total)
1169 if (toggle % 2 == 0)
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1179 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1183 connect_attempts = 0;
1184 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1185 * to the node to its right and above. Once this is over, we'll have our torus!
1186 * Special case for the last node (if the rows and columns are not equal), connect
1187 * to the first in the row to maintain topology.
1189 for (i = 0; i < pg->total; i++)
1191 /* First connect to the node to the right */
1192 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1193 nodeToConnect = i + 1;
1194 else if (i + 1 == pg->total)
1195 nodeToConnect = rows * cols - cols;
1197 nodeToConnect = i - cols + 1;
1199 connect_attempts += proc (pg, i, nodeToConnect);
1202 nodeToConnect = (rows * cols) - cols + i;
1204 nodeToConnect = i - cols;
1206 if (nodeToConnect < pg->total)
1207 connect_attempts += proc (pg, i, nodeToConnect);
1209 natLog = log (pg->total);
1210 #if VERBOSE_TESTING > 2
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1212 _("natural log of %d is %d, will run %d iterations\n"),
1213 pg->total, natLog, (int) (natLog * percentage));
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1216 smallWorldConnections = 0;
1217 for (i = 0; i < (int) (natLog * percentage); i++)
1219 for (j = 0; j < pg->total; j++)
1221 /* Determine the row and column of node at position j on the 2d torus */
1222 node1Row = j / cols;
1223 node1Col = j - (node1Row * cols);
1224 for (k = 0; k < pg->total; k++)
1226 /* Determine the row and column of node at position k on the 2d torus */
1227 node2Row = k / cols;
1228 node2Col = k - (node2Row * cols);
1229 /* Simple Cartesian distance */
1230 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1233 /* Calculate probability as 1 over the square of the distance */
1234 probability = 1.0 / (distance * distance);
1235 /* Choose a random value between 0 and 1 */
1236 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1237 UINT64_MAX)) / ( (double) UINT64_MAX);
1238 /* If random < probability, then connect the two nodes */
1239 if (random < probability)
1240 smallWorldConnections += proc (pg, j, k);
1246 connect_attempts += smallWorldConnections;
1247 #if VERBOSE_TESTING > 2
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 _("Total connections added for small world: %d!\n"),
1250 smallWorldConnections);
1252 return connect_attempts;
1256 * Create a topology given a peer group (set of running peers)
1257 * and a connection processor.
1259 * @param pg the peergroup to create the topology on
1260 * @param proc the connection processor to call to actually set
1261 * up connections between two peers
1263 * @return the number of connections that were set up
1267 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1270 unsigned int outer_count;
1271 unsigned int inner_count;
1272 int connect_attempts;
1276 probability = 0.5; /* FIXME: default percentage? */
1277 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1282 if (sscanf(p_string, "%lf", &probability) != 1)
1283 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1284 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1288 GNUNET_free (p_string);
1290 connect_attempts = 0;
1291 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1293 for (inner_count = outer_count + 1; inner_count < pg->total;
1296 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1297 UINT64_MAX)) / ( (double) UINT64_MAX);
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1300 _("rand is %f probability is %f\n"), temp_rand,
1303 if (temp_rand < probability)
1305 connect_attempts += proc (pg, outer_count, inner_count);
1310 return connect_attempts;
1314 * Create a topology given a peer group (set of running peers)
1315 * and a connection processor.
1317 * @param pg the peergroup to create the topology on
1318 * @param proc the connection processor to call to actually set
1319 * up connections between two peers
1321 * @return the number of connections that were set up
1325 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1328 unsigned int square;
1331 unsigned int toggle = 1;
1332 unsigned int nodeToConnect;
1333 int connect_attempts;
1335 connect_attempts = 0;
1337 square = floor (sqrt (pg->total));
1341 if (square * square != pg->total)
1343 while (rows * cols < pg->total)
1345 if (toggle % 2 == 0)
1354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1355 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1358 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1359 * to the node to its right and above. Once this is over, we'll have our torus!
1360 * Special case for the last node (if the rows and columns are not equal), connect
1361 * to the first in the row to maintain topology.
1363 for (i = 0; i < pg->total; i++)
1365 /* First connect to the node to the right */
1366 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1367 nodeToConnect = i + 1;
1368 else if (i + 1 == pg->total)
1369 nodeToConnect = rows * cols - cols;
1371 nodeToConnect = i - cols + 1;
1373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1374 "Connecting peer %d to peer %d\n",
1377 connect_attempts += proc(pg, i, nodeToConnect);
1379 /* Second connect to the node immediately above */
1381 nodeToConnect = (rows * cols) - cols + i;
1383 nodeToConnect = i - cols;
1385 if (nodeToConnect < pg->total)
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1389 "Connecting peer %d to peer %d\n",
1392 connect_attempts += proc(pg, i, nodeToConnect);
1397 return connect_attempts;
1402 * Create a topology given a peer group (set of running peers)
1403 * and a connection processor.
1405 * @param pg the peergroup to create the topology on
1406 * @param proc the connection processor to call to actually set
1407 * up connections between two peers
1409 * @return the number of connections that were set up
1413 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1415 unsigned int outer_count;
1416 unsigned int inner_count;
1417 int connect_attempts;
1419 connect_attempts = 0;
1421 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1423 for (inner_count = outer_count + 1; inner_count < pg->total;
1427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1428 "Connecting peer %d to peer %d\n",
1429 outer_count, inner_count);
1431 connect_attempts += proc(pg, outer_count, inner_count);
1435 return connect_attempts;
1439 * Create a topology given a peer group (set of running peers)
1440 * and a connection processor.
1442 * @param pg the peergroup to create the topology on
1443 * @param proc the connection processor to call to actually set
1444 * up connections between two peers
1446 * @return the number of connections that were set up
1450 create_line (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1453 int connect_attempts;
1455 connect_attempts = 0;
1457 /* Connect each peer to the next highest numbered peer */
1458 for (count = 0; count < pg->total - 1; count++)
1461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1462 "Connecting peer %d to peer %d\n",
1465 connect_attempts += proc(pg, count, count + 1);
1468 return connect_attempts;
1472 * Create a topology given a peer group (set of running peers)
1473 * and a connection processor.
1475 * @param pg the peergroup to create the topology on
1476 * @param proc the connection processor to call to actually set
1477 * up connections between two peers
1479 * @return the number of connections that were set up
1483 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1486 int connect_attempts;
1488 connect_attempts = 0;
1490 /* Connect each peer to the next highest numbered peer */
1491 for (count = 0; count < pg->total - 1; count++)
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1495 "Connecting peer %d to peer %d\n",
1498 connect_attempts += proc(pg, count, count + 1);
1501 /* Connect the last peer to the first peer */
1502 connect_attempts += proc(pg, pg->total - 1, 0);
1504 return connect_attempts;
1509 * Iterator for writing friends of a peer to a file.
1511 * @param cls closure, an open writable file handle
1512 * @param key the key the daemon was stored under
1513 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1515 * @return GNUNET_YES to continue iteration
1517 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1518 * with a single file_iterator that takes a closure which contains
1519 * the prefix to write before the peer. Then this could be used
1520 * for blacklisting multiple transports and writing the friend
1521 * file. I'm sure *someone* will complain loudly about other
1522 * things that negate these functions even existing so no point in
1526 friend_file_iterator (void *cls,
1527 const GNUNET_HashCode * key,
1530 FILE *temp_friend_handle = cls;
1531 struct GNUNET_TESTING_Daemon *peer = value;
1532 struct GNUNET_PeerIdentity *temppeer;
1533 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1535 temppeer = &peer->id;
1536 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1537 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1542 struct BlacklistContext
1545 * The (open) file handle to write to
1547 FILE *temp_file_handle;
1550 * The transport that this peer will be blacklisted on.
1556 * Iterator for writing blacklist data to appropriate files.
1558 * @param cls closure, an open writable file handle
1559 * @param key the key the daemon was stored under
1560 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1562 * @return GNUNET_YES to continue iteration
1565 blacklist_file_iterator (void *cls,
1566 const GNUNET_HashCode * key,
1569 struct BlacklistContext *blacklist_ctx = cls;
1570 //FILE *temp_blacklist_handle = cls;
1571 struct GNUNET_TESTING_Daemon *peer = value;
1572 struct GNUNET_PeerIdentity *temppeer;
1573 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1575 temppeer = &peer->id;
1576 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1577 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1583 * Create the friend files based on the PeerConnection's
1584 * of each peer in the peer group, and copy the files
1585 * to the appropriate place
1587 * @param pg the peer group we are dealing with
1590 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1592 FILE *temp_friend_handle;
1593 unsigned int pg_iter;
1594 char *temp_service_path;
1598 enum GNUNET_OS_ProcessStatusType type;
1599 unsigned long return_code;
1604 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1605 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1607 mytemp = GNUNET_DISK_mktemp("friends");
1608 GNUNET_assert(mytemp != NULL);
1609 temp_friend_handle = fopen (mytemp, "wt");
1610 GNUNET_assert(temp_friend_handle != NULL);
1611 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1612 fclose(temp_friend_handle);
1615 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1617 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1618 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1621 if (UNLINK (mytemp) != 0)
1622 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1623 GNUNET_free (mytemp);
1627 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1629 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1630 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1631 "mv", mytemp, arg, NULL);
1633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1634 _("Copying file with command cp %s %s\n"), mytemp, arg);
1639 else /* Remote, scp the file to the correct place */
1641 if (NULL != pg->peers[pg_iter].daemon->username)
1642 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1644 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1645 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1646 "scp", mytemp, arg, NULL);
1649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1650 _("Copying file with command scp %s %s\n"), mytemp, arg);
1654 GNUNET_free (temp_service_path);
1655 GNUNET_free (mytemp);
1659 ret = GNUNET_SYSERR;
1660 while ((count < max_wait) && (ret != GNUNET_OK))
1663 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1667 _("Checking copy status of file %d\n"), pg_iter);
1669 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1671 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1673 ret = GNUNET_SYSERR;
1675 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1677 ret = GNUNET_SYSERR;
1681 pidarr[pg_iter] = 0;
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1684 _("File %d copied\n"), pg_iter);
1690 if (ret == GNUNET_SYSERR)
1692 /* FIXME: why sleep here? -CG */
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1699 _("Finished copying all friend files!\n"));
1701 GNUNET_free(pidarr);
1707 * Create the blacklist files based on the PeerConnection's
1708 * of each peer in the peer group, and copy the files
1709 * to the appropriate place.
1711 * @param pg the peer group we are dealing with
1712 * @param transports space delimited list of transports to blacklist
1715 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
1717 FILE *temp_file_handle;
1718 static struct BlacklistContext blacklist_ctx;
1719 unsigned int pg_iter;
1720 char *temp_service_path;
1724 enum GNUNET_OS_ProcessStatusType type;
1725 unsigned long return_code;
1732 char *temp_transports;
1734 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1735 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1737 mytemp = GNUNET_DISK_mktemp("blacklist");
1738 GNUNET_assert(mytemp != NULL);
1739 temp_file_handle = fopen (mytemp, "wt");
1740 GNUNET_assert(temp_file_handle != NULL);
1741 temp_transports = GNUNET_strdup(transports);
1742 blacklist_ctx.temp_file_handle = temp_file_handle;
1743 transport_len = strlen(temp_transports) + 1;
1746 for (i = 0; i < transport_len; i++)
1748 if ((temp_transports[i] == ' ') && (pos == NULL))
1749 continue; /* At start of string (whitespace) */
1750 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
1752 temp_transports[i] = '\0';
1753 blacklist_ctx.transport = pos;
1754 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
1756 } /* At beginning of actual string */
1757 else if (pos == NULL)
1759 pos = &temp_transports[i];
1763 GNUNET_free (temp_transports);
1764 fclose(temp_file_handle);
1767 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1769 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1770 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1773 if (UNLINK (mytemp) != 0)
1774 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1775 GNUNET_free (mytemp);
1779 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1781 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
1782 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1783 "mv", mytemp, arg, NULL);
1785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786 _("Copying file with command cp %s %s\n"), mytemp, arg);
1791 else /* Remote, scp the file to the correct place */
1793 if (NULL != pg->peers[pg_iter].daemon->username)
1794 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1796 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1797 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1798 "scp", mytemp, arg, NULL);
1801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1802 _("Copying file with command scp %s %s\n"), mytemp, arg);
1806 GNUNET_free (temp_service_path);
1807 GNUNET_free (mytemp);
1811 ret = GNUNET_SYSERR;
1812 while ((count < max_wait) && (ret != GNUNET_OK))
1815 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1819 _("Checking copy status of file %d\n"), pg_iter);
1821 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1823 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1825 ret = GNUNET_SYSERR;
1827 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1829 ret = GNUNET_SYSERR;
1833 pidarr[pg_iter] = 0;
1835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1836 _("File %d copied\n"), pg_iter);
1842 if (ret == GNUNET_SYSERR)
1844 /* FIXME: why sleep here? -CG */
1850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1851 _("Finished copying all blacklist files!\n"));
1853 GNUNET_free(pidarr);
1859 * Internal notification of a connection, kept so that we can ensure some connections
1860 * happen instead of flooding all testing daemons with requests to connect.
1862 static void internal_connect_notify (void *cls,
1863 const struct GNUNET_PeerIdentity *first,
1864 const struct GNUNET_PeerIdentity *second,
1866 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
1867 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
1868 struct GNUNET_TESTING_Daemon *first_daemon,
1869 struct GNUNET_TESTING_Daemon *second_daemon,
1872 struct GNUNET_TESTING_PeerGroup *pg = cls;
1873 outstanding_connects--;
1875 pg->notify_connection(pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
1879 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1881 struct ConnectContext *connect_context = cls;
1883 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1886 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
1888 #if VERBOSE_TESTING > 2
1889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1890 _("Delaying connect, we have too many outstanding connections!\n"));
1892 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &schedule_connect, connect_context);
1896 #if VERBOSE_TESTING > 2
1897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1898 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
1900 outstanding_connects++;
1901 GNUNET_TESTING_daemons_connect (connect_context->first,
1902 connect_context->second,
1905 &internal_connect_notify,
1906 connect_context->pg);
1907 GNUNET_free(connect_context);
1913 * Iterator for actually scheduling connections to be created
1914 * between two peers.
1916 * @param cls closure, a GNUNET_TESTING_Daemon
1917 * @param key the key the second Daemon was stored under
1918 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1920 * @return GNUNET_YES to continue iteration
1923 connect_iterator (void *cls,
1924 const GNUNET_HashCode * key,
1927 struct PeerData *first = cls;
1928 struct GNUNET_TESTING_Daemon *second = value;
1929 struct ConnectContext *connect_context;
1931 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1932 connect_context->pg = first->pg;
1933 connect_context->first = first->daemon;
1934 connect_context->second = second;
1935 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
1942 * Iterator for copying all entries in the allowed hashmap to the
1945 * @param cls closure, a GNUNET_TESTING_Daemon
1946 * @param key the key the second Daemon was stored under
1947 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1949 * @return GNUNET_YES to continue iteration
1952 copy_topology_iterator (void *cls,
1953 const GNUNET_HashCode * key,
1956 struct PeerData *first = cls;
1958 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1964 * Make the peers to connect the same as those that are allowed to be
1967 * @param pg the peer group
1970 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
1972 unsigned int pg_iter;
1977 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1979 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
1980 if (GNUNET_SYSERR == ret)
1981 return GNUNET_SYSERR;
1983 total = total + ret;
1991 * Connect the topology as specified by the PeerConnection's
1992 * of each peer in the peer group
1994 * @param pg the peer group we are dealing with
1995 * @return the number of connections that will be attempted
1998 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
2000 unsigned int pg_iter;
2004 struct PeerConnection *connection_iter;
2005 struct ConnectContext *connect_context;
2009 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2011 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
2012 if (GNUNET_SYSERR == ret)
2013 return GNUNET_SYSERR;
2015 total = total + ret;
2019 while (connection_iter != NULL)
2021 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2022 connect_context->pg = pg;
2023 connect_context->first = ;
2024 connect_context->second = connection_iter->daemon;
2025 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
2026 connection_iter = connection_iter->next;
2035 * Takes a peer group and creates a topology based on the
2036 * one specified. Creates a topology means generates friend
2037 * files for the peers so they can only connect to those allowed
2038 * by the topology. This will only have an effect once peers
2039 * are started if the FRIENDS_ONLY option is set in the base
2040 * config. Also takes an optional restrict topology which
2041 * disallows direct TCP connections UNLESS they are specified in
2042 * the restricted topology.
2044 * @param pg the peer group struct representing the running peers
2045 * @param topology which topology to connect the peers in
2046 * @param restrict_topology allow only direct TCP connections in this topology
2047 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2048 * @param restrict_transports space delimited list of transports to blacklist
2049 * to create restricted topology
2051 * @return the maximum number of connections were all allowed peers
2052 * connected to each other
2055 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2056 enum GNUNET_TESTING_Topology topology,
2057 enum GNUNET_TESTING_Topology restrict_topology,
2058 char *restrict_transports)
2061 int num_connections;
2062 int unblacklisted_connections;
2064 GNUNET_assert (pg->notify_connection != NULL);
2069 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2070 #if VERBOSE_TOPOLOGY
2071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2072 _("Creating clique topology\n"));
2074 num_connections = create_clique (pg, &add_allowed_connections);
2076 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2077 #if VERBOSE_TOPOLOGY
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2079 _("Creating small world (ring) topology\n"));
2081 num_connections = create_small_world_ring (pg, &add_allowed_connections);
2083 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2084 #if VERBOSE_TOPOLOGY
2085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2086 _("Creating small world (2d-torus) topology\n"));
2088 num_connections = create_small_world (pg, &add_allowed_connections);
2090 case GNUNET_TESTING_TOPOLOGY_RING:
2091 #if VERBOSE_TOPOLOGY
2092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2093 _("Creating ring topology\n"));
2095 num_connections = create_ring (pg, &add_allowed_connections);
2097 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2098 #if VERBOSE_TOPOLOGY
2099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2100 _("Creating 2d torus topology\n"));
2102 num_connections = create_2d_torus (pg, &add_allowed_connections);
2104 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2105 #if VERBOSE_TOPOLOGY
2106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2107 _("Creating Erdos-Renyi topology\n"));
2109 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2111 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2112 #if VERBOSE_TOPOLOGY
2113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2114 _("Creating InterNAT topology\n"));
2116 num_connections = create_nated_internet (pg, &add_allowed_connections);
2118 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2121 _("Creating Scale Free topology\n"));
2123 num_connections = create_scale_free (pg, &add_allowed_connections);
2125 case GNUNET_TESTING_TOPOLOGY_LINE:
2127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2128 _("Creating straight line topology\n"));
2130 num_connections = create_line (pg, &add_allowed_connections);
2132 case GNUNET_TESTING_TOPOLOGY_NONE:
2133 num_connections = 0;
2136 num_connections = 0;
2139 if (num_connections < 1)
2140 return GNUNET_SYSERR;
2142 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2144 ret = create_and_copy_friend_files(pg);
2147 if (ret != GNUNET_OK)
2150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2151 _("Failed during friend file copying!\n"));
2153 return GNUNET_SYSERR;
2158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2159 _("Friend files created/copied successfully!\n"));
2163 /* Use the create clique method to initially set all connections as blacklisted. */
2164 create_clique (pg, &blacklist_connections);
2165 unblacklisted_connections = 0;
2166 /* Un-blacklist connections as per the topology specified */
2167 switch (restrict_topology)
2169 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2172 _("Blacklisting all but clique topology\n"));
2174 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2176 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2179 _("Blacklisting all but small world (ring) topology\n"));
2181 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2183 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2186 _("Blacklisting all but small world (2d-torus) topology\n"));
2188 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2190 case GNUNET_TESTING_TOPOLOGY_RING:
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2193 _("Blacklisting all but ring topology\n"));
2195 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2197 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2200 _("Blacklisting all but 2d torus topology\n"));
2202 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2204 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2207 _("Blacklisting all but Erdos-Renyi topology\n"));
2209 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2211 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2214 _("Blacklisting all but InterNAT topology\n"));
2216 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2218 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2221 _("Blacklisting all but Scale Free topology\n"));
2223 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2225 case GNUNET_TESTING_TOPOLOGY_LINE:
2227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2228 _("Blacklisting all but straight line topology\n"));
2230 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2232 case GNUNET_TESTING_TOPOLOGY_NONE:
2238 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2240 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2241 if (ret != GNUNET_OK)
2244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2245 _("Failed during blacklist file copying!\n"));
2247 return GNUNET_SYSERR;
2252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2253 _("Blacklist files created/copied successfully!\n"));
2257 return num_connections;
2260 struct RandomContext
2265 struct GNUNET_TESTING_PeerGroup *pg;
2268 * uid of the first peer
2273 * Peer data for first peer.
2275 struct PeerData *first;
2278 * Random percentage to use
2283 struct MinimumContext
2288 struct GNUNET_TESTING_PeerGroup *pg;
2291 * uid of the first peer
2296 * Peer data for first peer.
2298 struct PeerData *first;
2301 * Number of conns per peer
2303 unsigned int num_to_add;
2306 * Permuted array of all possible connections. Only add the Nth
2307 * peer if it's in the Nth position.
2309 unsigned int *pg_array;
2312 * What number is the current element we are iterating over?
2314 unsigned int current;
2322 struct GNUNET_TESTING_PeerGroup *pg;
2325 * uid of the first peer
2330 * uid of the second peer
2332 uint32_t second_uid;
2335 * Peer data for first peer.
2337 struct PeerData *first;
2340 * Which peer has been chosen as the one to add?
2342 unsigned int chosen;
2345 * What number is the current element we are iterating over?
2347 unsigned int current;
2351 * Iterator for choosing random peers to connect.
2353 * @param cls closure, a RandomContext
2354 * @param key the key the second Daemon was stored under
2355 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2357 * @return GNUNET_YES to continue iteration
2360 random_connect_iterator (void *cls,
2361 const GNUNET_HashCode * key,
2364 struct RandomContext *random_ctx = cls;
2365 double random_number;
2366 uint32_t second_pos;
2367 GNUNET_HashCode first_hash;
2368 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2369 UINT64_MAX)) / ( (double) UINT64_MAX);
2370 if (random_number < random_ctx->percentage)
2372 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2374 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2375 uid_from_hash(key, &second_pos);
2376 hash_from_uid(random_ctx->first_uid, &first_hash);
2377 GNUNET_assert(random_ctx->pg->total > second_pos);
2378 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2384 * Iterator for adding at least X peers to a peers connection set.
2386 * @param cls closure, MinimumContext
2387 * @param key the key the second Daemon was stored under
2388 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2390 * @return GNUNET_YES to continue iteration
2393 minimum_connect_iterator (void *cls,
2394 const GNUNET_HashCode * key,
2397 struct MinimumContext *min_ctx = cls;
2398 uint32_t second_pos;
2399 GNUNET_HashCode first_hash;
2402 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2404 for (i = 0; i < min_ctx->num_to_add; i++)
2406 if (min_ctx->pg_array[i] == min_ctx->current)
2408 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2409 uid_from_hash(key, &second_pos);
2410 hash_from_uid(min_ctx->first_uid, &first_hash);
2411 GNUNET_assert(min_ctx->pg->total > second_pos);
2412 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));
2413 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2414 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2421 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2427 * Iterator for adding peers to a connection set based on a depth first search.
2429 * @param cls closure, MinimumContext
2430 * @param key the key the second daemon was stored under
2431 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2433 * @return GNUNET_YES to continue iteration
2436 dfs_connect_iterator (void *cls,
2437 const GNUNET_HashCode * key,
2440 struct DFSContext *dfs_ctx = cls;
2441 GNUNET_HashCode first_hash;
2443 if (dfs_ctx->current == dfs_ctx->chosen)
2445 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2446 uid_from_hash(key, &dfs_ctx->second_uid);
2447 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2448 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));
2449 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2450 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2451 return GNUNET_NO; /* We have found our peer, don't iterate more */
2460 * From the set of connections possible, choose percentage percent of connections
2461 * to actually connect.
2463 * @param pg the peergroup we are dealing with
2464 * @param percentage what percent of total connections to make
2467 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2469 struct RandomContext random_ctx;
2472 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2474 random_ctx.first_uid = pg_iter;
2475 random_ctx.first = &pg->peers[pg_iter];
2476 random_ctx.percentage = percentage;
2478 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2479 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2480 /* Now remove the old connections */
2481 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2482 /* And replace with the random set */
2483 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2488 * From the set of connections possible, choose at least num connections per
2491 * @param pg the peergroup we are dealing with
2492 * @param num how many connections at least should each peer have (if possible)?
2495 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2497 struct MinimumContext minimum_ctx;
2500 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2502 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2505 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2507 minimum_ctx.first_uid = pg_iter;
2508 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2509 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2510 minimum_ctx.first = &pg->peers[pg_iter];
2511 minimum_ctx.pg = pg;
2512 minimum_ctx.num_to_add = num;
2513 minimum_ctx.current = 0;
2514 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2515 &minimum_connect_iterator,
2519 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2521 /* Remove the "old" connections */
2522 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2523 /* And replace with the working set */
2524 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2531 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2534 unsigned int pg_iter;
2538 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2540 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2547 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2550 unsigned int pg_iter;
2554 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2556 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2563 * From the set of connections possible, choose at least num connections per
2564 * peer based on depth first traversal of peer connections. If DFS leaves
2565 * peers unconnected, ensure those peers get connections.
2567 * @param pg the peergroup we are dealing with
2568 * @param num how many connections at least should each peer have (if possible)?
2571 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2573 struct DFSContext dfs_ctx;
2576 uint32_t starting_peer;
2577 uint32_t least_connections;
2578 GNUNET_HashCode second_hash;
2580 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2582 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2587 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
2589 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
2591 least_connections = -1; /* Set to very high number */
2592 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2594 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
2596 starting_peer = pg_iter;
2597 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2602 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
2608 /* Choose a random peer from the chosen peers set of connections to add */
2609 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
2610 dfs_ctx.first_uid = starting_peer;
2611 dfs_ctx.first = &pg->peers[starting_peer];
2613 dfs_ctx.current = 0;
2615 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
2616 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
2617 hash_from_uid(dfs_ctx.second_uid, &second_hash);
2618 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
2619 starting_peer = dfs_ctx.second_uid;
2622 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2624 /* Remove the "old" connections */
2625 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2626 /* And replace with the working set */
2627 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2632 * There are many ways to connect peers that are supported by this function.
2633 * To connect peers in the same topology that was created via the
2634 * GNUNET_TESTING_create_topology, the topology variable must be set to
2635 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
2636 * a new instance of that topology will be generated and attempted to be
2637 * connected. This could result in some connections being impossible,
2638 * because some topologies are non-deterministic.
2640 * @param pg the peer group struct representing the running peers
2641 * @param topology which topology to connect the peers in
2642 * @param options options for connecting the topology
2643 * @param option_modifier modifier for options that take a parameter
2644 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
2647 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2648 enum GNUNET_TESTING_Topology topology,
2649 enum GNUNET_TESTING_TopologyOption options,
2650 double option_modifier)
2654 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2655 #if VERBOSE_TOPOLOGY
2656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2657 _("Creating clique CONNECT topology\n"));
2659 create_clique (pg, &add_actual_connections);
2661 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2662 #if VERBOSE_TOPOLOGY
2663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2664 _("Creating small world (ring) CONNECT topology\n"));
2666 create_small_world_ring (pg, &add_actual_connections);
2668 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2669 #if VERBOSE_TOPOLOGY
2670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2671 _("Creating small world (2d-torus) CONNECT topology\n"));
2673 create_small_world (pg, &add_actual_connections);
2675 case GNUNET_TESTING_TOPOLOGY_RING:
2676 #if VERBOSE_TOPOLOGY
2677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2678 _("Creating ring CONNECT topology\n"));
2680 create_ring (pg, &add_actual_connections);
2682 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2683 #if VERBOSE_TOPOLOGY
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2685 _("Creating 2d torus CONNECT topology\n"));
2687 create_2d_torus (pg, &add_actual_connections);
2689 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2690 #if VERBOSE_TOPOLOGY
2691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2692 _("Creating Erdos-Renyi CONNECT topology\n"));
2694 create_erdos_renyi (pg, &add_actual_connections);
2696 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2697 #if VERBOSE_TOPOLOGY
2698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2699 _("Creating InterNAT CONNECT topology\n"));
2701 create_nated_internet (pg, &add_actual_connections);
2703 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2704 #if VERBOSE_TOPOLOGY
2705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2706 _("Creating Scale Free CONNECT topology\n"));
2708 create_scale_free (pg, &add_actual_connections);
2710 case GNUNET_TESTING_TOPOLOGY_LINE:
2711 #if VERBOSE_TOPOLOGY
2712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2713 _("Creating straight line CONNECT topology\n"));
2715 create_line (pg, &add_actual_connections);
2717 case GNUNET_TESTING_TOPOLOGY_NONE:
2718 #if VERBOSE_TOPOLOGY
2719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2720 _("Creating no CONNECT topology\n"));
2722 copy_allowed_topology(pg);
2725 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2726 _("Unknown topology specification, can't connect peers!\n"));
2727 return GNUNET_SYSERR;
2732 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
2733 #if VERBOSE_TOPOLOGY
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2735 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
2737 choose_random_connections(pg, option_modifier);
2739 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
2740 #if VERBOSE_TOPOLOGY
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2742 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
2744 choose_minimum(pg, (unsigned int)option_modifier);
2746 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
2747 #if VERBOSE_TOPOLOGY
2748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2749 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
2751 perform_dfs(pg, (int)option_modifier);
2753 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
2755 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
2761 return connect_topology(pg);
2765 * Function which continues a peer group starting up
2766 * after successfully generating hostkeys for each peer.
2768 * @param pg the peer group to continue starting
2772 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
2776 for (i = 0; i < pg->total; i++)
2778 GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
2783 * Start count gnunetd processes with the same set of transports and
2784 * applications. The port numbers (any option called "PORT") will be
2785 * adjusted to ensure that no two peers running on the same system
2786 * have the same port(s) in their respective configurations.
2788 * @param sched scheduler to use
2789 * @param cfg configuration template to use
2790 * @param total number of daemons to start
2791 * @param timeout total time allowed for peers to start
2792 * @param hostkey_callback function to call on each peers hostkey generation
2793 * if NULL, peers will be started by this call, if non-null,
2794 * GNUNET_TESTING_daemons_continue_startup must be called after
2795 * successful hostkey generation
2796 * @param hostkey_cls closure for hostkey callback
2797 * @param cb function to call on each daemon that was started
2798 * @param cb_cls closure for cb
2799 * @param connect_callback function to call each time two hosts are connected
2800 * @param connect_callback_cls closure for connect_callback
2801 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
2802 * everything on localhost).
2803 * @return NULL on error, otherwise handle to control peer group
2805 struct GNUNET_TESTING_PeerGroup *
2806 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
2807 const struct GNUNET_CONFIGURATION_Handle *cfg,
2809 struct GNUNET_TIME_Relative timeout,
2810 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
2812 GNUNET_TESTING_NotifyDaemonRunning cb,
2814 GNUNET_TESTING_NotifyConnection
2815 connect_callback, void *connect_callback_cls,
2816 const char *hostnames)
2818 struct GNUNET_TESTING_PeerGroup *pg;
2822 const char *hostname;
2823 char *baseservicehome;
2824 char *newservicehome;
2826 struct GNUNET_CONFIGURATION_Handle *pcfg;
2828 unsigned int hostcnt;
2838 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
2842 pg->cb_cls = cb_cls;
2843 pg->notify_connection = connect_callback;
2844 pg->notify_connection_cls = connect_callback_cls;
2846 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
2847 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
2848 if (NULL != hostnames)
2851 /* skip leading spaces */
2852 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
2855 while ('\0' != *rpos)
2857 if (isspace ( (unsigned char) *rpos))
2861 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
2863 start = GNUNET_strdup (hostnames);
2865 while ('\0' != *pos)
2867 if (isspace ( (unsigned char) *pos))
2870 if (strlen (start) > 0)
2872 pg->hosts[off].minport = LOW_PORT;
2873 pg->hosts[off++].hostname = start;
2879 if (strlen (start) > 0)
2881 pg->hosts[off].minport = LOW_PORT;
2882 pg->hosts[off++].hostname = start;
2886 GNUNET_free (start);
2887 GNUNET_free (pg->hosts);
2891 minport = 0; /* make gcc happy */
2898 for (off = 0; off < total; off++)
2902 hostname = pg->hosts[off % hostcnt].hostname;
2903 pcfg = make_config (cfg,
2904 &pg->hosts[off % hostcnt].minport,
2911 pcfg = make_config (cfg,
2919 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2921 ("Could not create configuration for peer number %u on `%s'!\n"),
2922 off, hostname == NULL ? "localhost" : hostname);
2927 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
2930 GNUNET_asprintf (&newservicehome,
2931 "%s/%d/", baseservicehome, off);
2932 GNUNET_free (baseservicehome);
2936 tmpdir = getenv ("TMPDIR");
2937 tmpdir = tmpdir ? tmpdir : "/tmp";
2938 GNUNET_asprintf (&newservicehome,
2941 "gnunet-testing-test-test", off);
2943 GNUNET_CONFIGURATION_set_value_string (pcfg,
2945 "SERVICEHOME", newservicehome);
2946 GNUNET_free (newservicehome);
2947 pg->peers[off].cfg = pcfg;
2948 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
2949 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
2950 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
2951 pg->peers[off].pg = pg;
2952 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (sched,
2959 if (NULL == pg->peers[off].daemon)
2960 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2961 _("Could not start peer number %u!\n"), off);
2968 * Get a daemon by number, so callers don't have to do nasty
2969 * offsetting operation.
2971 struct GNUNET_TESTING_Daemon *
2972 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
2974 if (position < pg->total)
2975 return pg->peers[position].daemon;
2981 * Prototype of a function that will be called when a
2982 * particular operation was completed the testing library.
2984 * @param cls closure (a struct RestartContext)
2985 * @param id id of the peer that was restarted
2986 * @param cfg handle to the configuration of the peer
2987 * @param d handle to the daemon that was restarted
2988 * @param emsg NULL on success
2990 void restart_callback (void *cls,
2991 const struct GNUNET_PeerIdentity *id,
2992 const struct GNUNET_CONFIGURATION_Handle *cfg,
2993 struct GNUNET_TESTING_Daemon *d,
2996 struct RestartContext *restart_context = cls;
3000 restart_context->peers_restarted++;
3004 restart_context->peers_restart_failed++;
3007 if (restart_context->peers_restarted == restart_context->peer_group->total)
3009 restart_context->callback(restart_context->callback_cls, NULL);
3010 GNUNET_free(restart_context);
3012 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
3014 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
3015 GNUNET_free(restart_context);
3021 * Callback for informing us about a successful
3022 * or unsuccessful churn stop call.
3024 * @param cls a ChurnContext
3025 * @param emsg NULL on success, non-NULL on failure
3029 churn_stop_callback (void *cls, const char *emsg)
3031 struct ChurnContext *churn_ctx = cls;
3032 unsigned int total_left;
3033 char *error_message;
3035 error_message = NULL;
3038 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3039 "Churn stop callback failed with error `%s'\n", emsg);
3040 churn_ctx->num_failed_stop++;
3044 churn_ctx->num_to_stop--;
3048 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3049 "Stopped peer, %d left.\n",
3050 churn_ctx->num_to_stop);
3052 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3054 if (total_left == 0)
3056 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3058 GNUNET_asprintf(&error_message,
3059 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3060 churn_ctx->num_failed_start,
3061 churn_ctx->num_failed_stop);
3063 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3064 GNUNET_free_non_null(error_message);
3065 GNUNET_free(churn_ctx);
3070 * Callback for informing us about a successful
3071 * or unsuccessful churn start call.
3073 * @param cls a ChurnContext
3074 * @param id the peer identity of the started peer
3075 * @param cfg the handle to the configuration of the peer
3076 * @param d handle to the daemon for the peer
3077 * @param emsg NULL on success, non-NULL on failure
3081 churn_start_callback (void *cls,
3082 const struct GNUNET_PeerIdentity *id,
3083 const struct GNUNET_CONFIGURATION_Handle *cfg,
3084 struct GNUNET_TESTING_Daemon *d,
3087 struct ChurnContext *churn_ctx = cls;
3088 unsigned int total_left;
3089 char *error_message;
3091 error_message = NULL;
3094 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3095 "Churn stop callback failed with error `%s'\n",
3097 churn_ctx->num_failed_start++;
3101 churn_ctx->num_to_start--;
3105 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3106 "Started peer, %d left.\n",
3107 churn_ctx->num_to_start);
3110 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3112 if (total_left == 0)
3114 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3115 GNUNET_asprintf(&error_message,
3116 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3117 churn_ctx->num_failed_start,
3118 churn_ctx->num_failed_stop);
3119 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3120 GNUNET_free_non_null(error_message);
3121 GNUNET_free(churn_ctx);
3127 * Simulate churn by stopping some peers (and possibly
3128 * re-starting others if churn is called multiple times). This
3129 * function can only be used to create leave-join churn (peers "never"
3130 * leave for good). First "voff" random peers that are currently
3131 * online will be taken offline; then "von" random peers that are then
3132 * offline will be put back online. No notifications will be
3133 * generated for any of these operations except for the callback upon
3136 * @param pg handle for the peer group
3137 * @param voff number of peers that should go offline
3138 * @param von number of peers that should come back online;
3139 * must be zero on first call (since "testbed_start"
3140 * always starts all of the peers)
3141 * @param timeout how long to wait for operations to finish before
3143 * @param cb function to call at the end
3144 * @param cb_cls closure for cb
3147 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
3150 struct GNUNET_TIME_Relative timeout,
3151 GNUNET_TESTING_NotifyCompletion cb,
3154 struct ChurnContext *churn_ctx;
3155 unsigned int running;
3156 unsigned int stopped;
3158 unsigned int *running_arr;
3159 unsigned int *stopped_arr;
3160 unsigned int *running_permute;
3161 unsigned int *stopped_permute;
3166 if ((von == 0) && (voff == 0)) /* No peers at all? */
3172 for (i = 0; i < pg->total; i++)
3174 if (pg->peers[i].daemon->running == GNUNET_YES)
3176 GNUNET_assert(running != -1);
3181 GNUNET_assert(stopped != -1);
3188 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
3189 cb(cb_cls, "Trying to stop more peers than are currently running!");
3195 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
3196 cb(cb_cls, "Trying to start more peers than are currently stopped!");
3200 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3201 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
3202 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
3204 running_permute = NULL;
3205 stopped_permute = NULL;
3208 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
3210 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
3215 churn_ctx->num_to_start = von;
3216 churn_ctx->num_to_stop = voff;
3218 churn_ctx->cb_cls = cb_cls;
3220 for (i = 0; i < pg->total; i++)
3222 if (pg->peers[i].daemon->running == GNUNET_YES)
3224 running_arr[running] = i;
3229 stopped_arr[stopped] = i;
3234 for (i = 0; i < voff; i++)
3237 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
3239 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
3241 &churn_stop_callback, churn_ctx,
3242 GNUNET_NO, GNUNET_YES);
3245 for (i = 0; i < von; i++)
3248 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
3250 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
3251 timeout, &churn_start_callback, churn_ctx);
3254 GNUNET_free(running_arr);
3255 GNUNET_free(stopped_arr);
3256 GNUNET_free_non_null(running_permute);
3257 GNUNET_free_non_null(stopped_permute);
3262 * Restart all peers in the given group.
3264 * @param pg the handle to the peer group
3265 * @param callback function to call on completion (or failure)
3266 * @param callback_cls closure for the callback function
3269 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyCompletion callback, void *callback_cls)
3271 struct RestartContext *restart_context;
3276 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
3277 restart_context->peer_group = pg;
3278 restart_context->peers_restarted = 0;
3279 restart_context->callback = callback;
3280 restart_context->callback_cls = callback_cls;
3282 for (off = 0; off < pg->total; off++)
3284 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
3290 * Start or stop an individual peer from the given group.
3292 * @param pg handle to the peer group
3293 * @param offset which peer to start or stop
3294 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
3295 * @param timeout how long to wait for shutdown
3296 * @param cb function to call at the end
3297 * @param cb_cls closure for cb
3300 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
3301 unsigned int offset,
3303 struct GNUNET_TIME_Relative timeout,
3304 GNUNET_TESTING_NotifyCompletion cb,
3307 struct ChurnContext *churn_ctx;
3309 if (GNUNET_NO == desired_status)
3311 if (NULL != pg->peers[offset].daemon)
3313 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3314 churn_ctx->num_to_start = 0;
3315 churn_ctx->num_to_stop = 1;
3317 churn_ctx->cb_cls = cb_cls;
3318 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
3319 timeout, &churn_stop_callback, churn_ctx,
3320 GNUNET_NO, GNUNET_YES);
3323 else if (GNUNET_YES == desired_status)
3325 if (NULL == pg->peers[offset].daemon)
3327 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3328 churn_ctx->num_to_start = 1;
3329 churn_ctx->num_to_stop = 0;
3331 churn_ctx->cb_cls = cb_cls;
3332 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
3333 timeout, &churn_start_callback, churn_ctx);
3342 * Shutdown all peers started in the given group.
3344 * @param pg handle to the peer group
3345 * @param timeout how long to wait for shutdown
3348 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
3349 struct GNUNET_TIME_Relative timeout)
3353 for (off = 0; off < pg->total; off++)
3355 /* FIXME: should we wait for our continuations to be called
3356 here? This would require us to take a continuation as
3359 if (NULL != pg->peers[off].daemon)
3360 GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, NULL, NULL, GNUNET_YES, GNUNET_NO);
3361 if (NULL != pg->peers[off].cfg)
3362 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
3363 if (pg->peers[off].allowed_peers != NULL)
3364 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
3365 if (pg->peers[off].connect_peers != NULL)
3366 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
3367 if (pg->peers[off].blacklisted_peers != NULL)
3368 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
3370 GNUNET_free (pg->peers);
3371 if (NULL != pg->hosts)
3373 GNUNET_free (pg->hosts[0].hostname);
3374 GNUNET_free (pg->hosts);
3380 /* end of testing_group.c */