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)(struct GNUNET_TESTING_PeerGroup *pg,
76 * Context for handling churning a peer group
81 * Callback used to notify of churning finished
83 GNUNET_TESTING_NotifyCompletion cb;
86 * Closure for callback
91 * Number of peers that still need to be started
93 unsigned int num_to_start;
96 * Number of peers that still need to be stopped
98 unsigned int num_to_stop;
101 * Number of peers that failed to start
103 unsigned int num_failed_start;
106 * Number of peers that failed to stop
108 unsigned int num_failed_stop;
111 struct RestartContext
114 * The group of peers being restarted
116 struct GNUNET_TESTING_PeerGroup *peer_group;
119 * How many peers have been restarted thus far
121 unsigned int peers_restarted;
124 * How many peers got an error when restarting
126 unsigned int peers_restart_failed;
129 * The function to call once all peers have been restarted
131 GNUNET_TESTING_NotifyCompletion callback;
134 * Closure for callback function
140 struct CreateTopologyContext
144 * Function to call with number of connections
146 GNUNET_TESTING_NotifyConnections cont;
149 * Closure for connection notification
155 struct PeerConnection
160 struct PeerConnection *next;
163 * Pointer to daemon handle
165 struct GNUNET_TESTING_Daemon *daemon;
171 * Data we keep per peer.
176 * (Initial) configuration of the host.
177 * (initial because clients could change
178 * it and we would not know about those
181 struct GNUNET_CONFIGURATION_Handle *cfg;
184 * Handle for controlling the daemon.
186 struct GNUNET_TESTING_Daemon *daemon;
189 * The peergroup this peer belongs to.
191 struct GNUNET_TESTING_PeerGroup *pg;
194 * Linked list of peer connections (pointers)
196 //struct PeerConnection *connected_peers;
198 * Hash map of allowed peer connections (F2F created topology)
200 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
203 * Hash map of blacklisted peers
205 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
208 * Hash map of peer connections
210 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
213 * Temporary hash map of peer connections
215 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
218 * Temporary variable for topology creation, should be reset before
219 * creating any topology so the count is valid once finished.
226 * Data we keep per host.
236 * Lowest port that we have not yet used
244 * Handle to a group of GNUnet peers.
246 struct GNUNET_TESTING_PeerGroup
251 struct GNUNET_SCHEDULER_Handle *sched;
254 * Configuration template.
256 const struct GNUNET_CONFIGURATION_Handle *cfg;
259 * Function to call on each started daemon.
261 GNUNET_TESTING_NotifyDaemonRunning cb;
269 * Function to call on each topology connection created
271 GNUNET_TESTING_NotifyConnection notify_connection;
274 * Callback for notify_connection
276 void *notify_connection_cls;
279 * NULL-terminated array of information about
282 struct HostData *hosts;
285 * Array of "total" peers.
287 struct PeerData *peers;
290 * Number of peers in this group.
295 * At what time should we fail the peer startup process?
297 struct GNUNET_TIME_Absolute max_timeout;
301 * Convert unique ID to hash code.
303 * @param uid unique ID to convert
304 * @param hash set to uid (extended with zeros)
307 hash_from_uid (uint32_t uid,
308 GNUNET_HashCode *hash)
310 memset (hash, 0, sizeof(GNUNET_HashCode));
311 *((uint32_t*)hash) = uid;
315 * Convert hash code to unique ID.
317 * @param uid unique ID to convert
318 * @param hash set to uid (extended with zeros)
321 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
323 memcpy (uid, hash, sizeof(uint32_t));
328 struct GNUNET_CONFIGURATION_Handle *ret;
329 const char *hostname;
335 struct ConnectContext
337 struct GNUNET_TESTING_Daemon *first;
339 struct GNUNET_TESTING_Daemon *second;
341 struct GNUNET_TESTING_PeerGroup *pg;
345 * Number of connects we are waiting on, allows us to rate limit
348 static int outstanding_connects;
351 * Get a topology from a string input.
353 * @param topology where to write the retrieved topology
354 * @param topology_string The string to attempt to
355 * get a configuration value from
356 * @return GNUNET_YES if topology string matched a
357 * known topology, GNUNET_NO if not
360 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string)
363 * Strings representing topologies in enum
365 static const char * topology_strings[] =
368 * A clique (everyone connected to everyone else).
373 * Small-world network (2d torus plus random links).
378 * Small-world network (ring plus random links).
398 * Certain percentage of peers are unable to communicate directly
399 * replicating NAT conditions
404 * Scale free topology.
409 * Straight line topology.
414 * All peers are disconnected.
422 if (topology_string == NULL)
424 while (topology_strings[curr] != NULL)
426 if (strcmp(topology_strings[curr], topology_string) == 0)
433 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
439 * Get connect topology option from string input.
441 * @param topology_option where to write the retrieved topology
442 * @param topology_string The string to attempt to
443 * get a configuration value from
444 * @return GNUNET_YES if string matched a known
445 * topology option, GNUNET_NO if not
448 GNUNET_TESTING_topology_option_get(enum GNUNET_TESTING_TopologyOption *topology_option,
449 char * topology_string)
452 * Options for connecting a topology as strings.
454 static const char * topology_option_strings[] =
457 * Try to connect all peers specified in the topology.
462 * Choose a random subset of connections to create.
464 "CONNECT_RANDOM_SUBSET",
467 * Create at least X connections for each peer.
472 * Using a depth first search, create one connection
473 * per peer. If any are missed (graph disconnected)
474 * start over at those peers until all have at least one
480 * No options specified.
488 if (topology_string == NULL)
490 while (NULL != topology_option_strings[curr])
492 if (strcmp(topology_option_strings[curr], topology_string) == 0)
494 *topology_option = curr;
499 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
504 * Function to iterate over options. Copies
505 * the options to the target configuration,
506 * updating PORT values as needed.
509 * @param section name of the section
510 * @param option name of the option
511 * @param value value of the option
514 update_config (void *cls,
515 const char *section, const char *option, const char *value)
517 struct UpdateContext *ctx = cls;
522 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
526 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
531 if (0 == strcmp (option, "UNIXPATH"))
533 GNUNET_snprintf (uval,
535 "/tmp/test-service-%s-%u",
541 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
543 value = ctx->hostname;
546 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
551 * Create a new configuration using the given configuration
552 * as a template; however, each PORT in the existing cfg
553 * must be renumbered by incrementing "*port". If we run
554 * out of "*port" numbers, return NULL.
556 * @param cfg template configuration
557 * @param port port numbers to use, update to reflect
558 * port numbers that were used
559 * @param upnum number to make unix domain socket names unique
560 * @param hostname hostname of the controlling host, to allow control connections from
562 * @return new configuration, NULL on error
564 static struct GNUNET_CONFIGURATION_Handle *
565 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
568 const char *hostname)
570 struct UpdateContext uc;
578 uc.ret = GNUNET_CONFIGURATION_create ();
579 uc.hostname = hostname;
581 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
582 if (uc.nport >= HIGH_PORT)
585 GNUNET_CONFIGURATION_destroy (uc.ret);
589 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
591 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
592 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
593 GNUNET_free_non_null(control_host);
594 GNUNET_free(allowed_hosts);
598 /* arm needs to know to allow connections from the host on which it is running,
599 * otherwise gnunet-arm is unable to connect to it in some instances */
600 if (hostname != NULL)
602 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
603 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
604 GNUNET_free(allowed_hosts);
607 *port = (uint16_t) uc.nport;
614 * Add entries to the peers connect list
616 * @param pg the peer group we are working with
617 * @param first index of the first peer
618 * @param second index of the second peer
620 * @return the number of connections added (can be 0, 1 or 2)
621 * technically should only be 0 or 2, but the small price
622 * of iterating over the lists (hashmaps in the future)
623 * for being sure doesn't bother me!
627 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
633 GNUNET_HashCode hash_first;
634 GNUNET_HashCode hash_second;
636 hash_from_uid(first, &hash_first);
637 hash_from_uid(second, &hash_second);
639 add_first = GNUNET_NO;
640 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
642 add_first = GNUNET_YES;
645 add_second = GNUNET_NO;
646 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
648 add_second = GNUNET_YES;
654 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
655 pg->peers[first].num_connections++;
661 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
662 pg->peers[second].num_connections++;
671 * Add entries to the peers allowed connections list
673 * @param pg the peer group we are working with
674 * @param first index of the first peer
675 * @param second index of the second peer
677 * @return the number of connections added (can be 0, 1 or 2)
678 * technically should only be 0 or 2, but the small price
679 * of iterating over the lists (hashmaps in the future)
680 * for being sure doesn't bother me!
684 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
688 struct PeerConnection *first_iter;
689 struct PeerConnection *second_iter;
690 struct PeerConnection *new_first;
691 struct PeerConnection *new_second;
696 GNUNET_HashCode hash_first;
697 GNUNET_HashCode hash_second;
699 hash_from_uid(first, &hash_first);
700 hash_from_uid(second, &hash_second);
702 add_first = GNUNET_NO;
703 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
705 add_first = GNUNET_YES;
708 add_second = GNUNET_NO;
709 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
711 add_second = GNUNET_YES;
714 first_iter = pg->peers[first].connected_peers;
715 while (first_iter != NULL)
717 if (first_iter->daemon == pg->peers[second].daemon)
718 add_first = GNUNET_NO;
719 first_iter = first_iter->next;
722 second_iter = pg->peers[second].connected_peers;
723 add_second = GNUNET_YES;
724 while (second_iter != NULL)
726 if (second_iter->daemon == pg->peers[first].daemon)
727 add_second = GNUNET_NO;
728 second_iter = second_iter->next;
735 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
737 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
738 new_first->daemon = pg->peers[second].daemon;
739 new_first->next = pg->peers[first].connected_peers;
740 pg->peers[first].connected_peers = new_first;
742 pg->peers[first].num_connections++;
748 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
750 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
751 new_second->daemon = pg->peers[first].daemon;
752 new_second->next = pg->peers[second].connected_peers;
753 pg->peers[second].connected_peers = new_second;
754 pg->peers[first].num_connections++;
756 pg->peers[second].num_connections++;
764 * Add entries to the peers blacklisted list
766 * @param pg the peer group we are working with
767 * @param first index of the first peer
768 * @param second index of the second peer
770 * @return the number of connections added (can be 0, 1 or 2)
774 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
779 GNUNET_HashCode hash_first;
780 GNUNET_HashCode hash_second;
782 hash_from_uid(first, &hash_first);
783 hash_from_uid(second, &hash_second);
785 add_first = GNUNET_NO;
786 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
788 add_first = GNUNET_YES;
791 add_second = GNUNET_NO;
792 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
794 add_second = GNUNET_YES;
800 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
801 pg->peers[first].num_connections++;
807 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
808 pg->peers[second].num_connections++;
816 * Remove entries from the peers blacklisted list
818 * @param pg the peer group we are working with
819 * @param first index of the first peer
820 * @param second index of the second peer
822 * @return the number of connections removed (can be 0, 1 or 2)
826 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
831 GNUNET_HashCode hash_first;
832 GNUNET_HashCode hash_second;
834 hash_from_uid(first, &hash_first);
835 hash_from_uid(second, &hash_second);
837 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
838 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
843 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
849 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
857 * Scale free network construction as described in:
859 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
861 * Start with a network of "one" peer, then progressively add
862 * peers up to the total number. At each step, iterate over
863 * all possible peers and connect new peer based on number of
864 * existing connections of the target peer.
866 * @param pg the peer group we are dealing with
867 * @param proc the connection processor to use
869 * @return the number of connections created
872 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
875 unsigned int total_connections;
876 unsigned int outer_count;
878 unsigned int previous_total_connections;
882 GNUNET_assert(pg->total > 1);
884 /* Add a connection between the first two nodes */
885 total_connections = proc(pg, 0, 1);
887 for (outer_count = 1; outer_count < pg->total; outer_count++)
889 previous_total_connections = total_connections;
890 for (i = 0; i < outer_count; i++)
892 probability = pg->peers[i].num_connections / (double)previous_total_connections;
893 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
894 UINT64_MAX)) / ( (double) UINT64_MAX);
896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
897 "Considering connecting peer %d to peer %d\n",
900 if (random < probability)
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 "Connecting peer %d to peer %d\n",
907 total_connections += proc(pg, outer_count, i);
912 return total_connections;
916 * Create a topology given a peer group (set of running peers)
917 * and a connection processor.
919 * @param pg the peergroup to create the topology on
920 * @param proc the connection processor to call to actually set
921 * up connections between two peers
923 * @return the number of connections that were set up
927 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
932 unsigned int randomPeer;
933 double random, logNModifier, percentage;
934 unsigned int smallWorldConnections;
940 int connect_attempts;
942 logNModifier = 0.5; /* FIXME: default value? */
943 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
948 if (sscanf(p_string, "%lf", &logNModifier) != 1)
949 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
950 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
954 GNUNET_free (p_string);
956 percentage = 0.5; /* FIXME: default percentage? */
957 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
962 if (sscanf(p_string, "%lf", &percentage) != 1)
963 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
964 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
968 GNUNET_free (p_string);
970 natLog = log (pg->total);
971 connsPerPeer = ceil (natLog * logNModifier);
973 if (connsPerPeer % 2 == 1)
976 smallWorldConnections = 0;
977 connect_attempts = 0;
978 for (i = 0; i < pg->total; i++)
981 max = i + connsPerPeer / 2;
982 min = i - connsPerPeer / 2;
984 if (max > pg->total - 1)
986 max = max - pg->total;
992 min = pg->total - 1 + min;
996 for (j = 0; j < connsPerPeer / 2; j++)
998 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
999 UINT64_MAX) / ( (double) UINT64_MAX));
1000 if (random < percentage)
1002 /* Connect to uniformly selected random peer */
1004 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1006 while ((((randomPeer < max) && (randomPeer > min))
1007 && (useAnd == 0)) || (((randomPeer > min)
1008 || (randomPeer < max))
1012 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1015 smallWorldConnections +=
1016 proc (pg, i, randomPeer);
1020 nodeToConnect = i + j + 1;
1021 if (nodeToConnect > pg->total - 1)
1023 nodeToConnect = nodeToConnect - pg->total;
1026 proc (pg, i, nodeToConnect);
1032 connect_attempts += smallWorldConnections;
1034 return connect_attempts;
1038 * Create a topology given a peer group (set of running peers)
1039 * and a connection processor.
1041 * @param pg the peergroup to create the topology on
1042 * @param proc the connection processor to call to actually set
1043 * up connections between two peers
1045 * @return the number of connections that were set up
1049 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1051 unsigned int outer_count, inner_count;
1052 unsigned int cutoff;
1053 int connect_attempts;
1054 double nat_percentage;
1057 nat_percentage = 0.6; /* FIXME: default percentage? */
1058 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1063 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1064 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1065 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1069 GNUNET_free (p_string);
1074 cutoff = (unsigned int) (nat_percentage * pg->total);
1076 connect_attempts = 0;
1078 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1080 for (inner_count = outer_count + 1; inner_count < pg->total;
1083 if ((outer_count > cutoff) || (inner_count > cutoff))
1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1087 "Connecting peer %d to peer %d\n",
1088 outer_count, inner_count);
1090 connect_attempts += proc(pg, outer_count, inner_count);
1095 return connect_attempts;
1100 * Create a topology given a peer group (set of running peers)
1101 * and a connection processor.
1103 * @param pg the peergroup to create the topology on
1104 * @param proc the connection processor to call to actually set
1105 * up connections between two peers
1107 * @return the number of connections that were set up
1111 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1113 unsigned int i, j, k;
1114 unsigned int square;
1117 unsigned int toggle = 1;
1118 unsigned int nodeToConnect;
1119 unsigned int natLog;
1120 unsigned int node1Row;
1121 unsigned int node1Col;
1122 unsigned int node2Row;
1123 unsigned int node2Col;
1124 unsigned int distance;
1125 double probability, random, percentage;
1126 unsigned int smallWorldConnections;
1128 int connect_attempts;
1129 square = floor (sqrt (pg->total));
1133 percentage = 0.5; /* FIXME: default percentage? */
1134 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1139 if (sscanf(p_string, "%lf", &percentage) != 1)
1140 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1141 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1145 GNUNET_free (p_string);
1147 probability = 0.5; /* FIXME: default percentage? */
1148 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1153 if (sscanf(p_string, "%lf", &probability) != 1)
1154 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1155 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1159 GNUNET_free (p_string);
1161 if (square * square != pg->total)
1163 while (rows * cols < pg->total)
1165 if (toggle % 2 == 0)
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1175 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1179 connect_attempts = 0;
1180 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1181 * to the node to its right and above. Once this is over, we'll have our torus!
1182 * Special case for the last node (if the rows and columns are not equal), connect
1183 * to the first in the row to maintain topology.
1185 for (i = 0; i < pg->total; i++)
1187 /* First connect to the node to the right */
1188 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1189 nodeToConnect = i + 1;
1190 else if (i + 1 == pg->total)
1191 nodeToConnect = rows * cols - cols;
1193 nodeToConnect = i - cols + 1;
1195 connect_attempts += proc (pg, i, nodeToConnect);
1198 nodeToConnect = (rows * cols) - cols + i;
1200 nodeToConnect = i - cols;
1202 if (nodeToConnect < pg->total)
1203 connect_attempts += proc (pg, i, nodeToConnect);
1205 natLog = log (pg->total);
1206 #if VERBOSE_TESTING > 2
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 _("natural log of %d is %d, will run %d iterations\n"),
1209 pg->total, natLog, (int) (natLog * percentage));
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1212 smallWorldConnections = 0;
1213 for (i = 0; i < (int) (natLog * percentage); i++)
1215 for (j = 0; j < pg->total; j++)
1217 /* Determine the row and column of node at position j on the 2d torus */
1218 node1Row = j / cols;
1219 node1Col = j - (node1Row * cols);
1220 for (k = 0; k < pg->total; k++)
1222 /* Determine the row and column of node at position k on the 2d torus */
1223 node2Row = k / cols;
1224 node2Col = k - (node2Row * cols);
1225 /* Simple Cartesian distance */
1226 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1229 /* Calculate probability as 1 over the square of the distance */
1230 probability = 1.0 / (distance * distance);
1231 /* Choose a random value between 0 and 1 */
1232 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1233 UINT64_MAX)) / ( (double) UINT64_MAX);
1234 /* If random < probability, then connect the two nodes */
1235 if (random < probability)
1236 smallWorldConnections += proc (pg, j, k);
1242 connect_attempts += smallWorldConnections;
1243 #if VERBOSE_TESTING > 2
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1245 _("Total connections added for small world: %d!\n"),
1246 smallWorldConnections);
1248 return connect_attempts;
1252 * Create a topology given a peer group (set of running peers)
1253 * and a connection processor.
1255 * @param pg the peergroup to create the topology on
1256 * @param proc the connection processor to call to actually set
1257 * up connections between two peers
1259 * @return the number of connections that were set up
1263 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1266 unsigned int outer_count;
1267 unsigned int inner_count;
1268 int connect_attempts;
1272 probability = 0.5; /* FIXME: default percentage? */
1273 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1278 if (sscanf(p_string, "%lf", &probability) != 1)
1279 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1280 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1284 GNUNET_free (p_string);
1286 connect_attempts = 0;
1287 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1289 for (inner_count = outer_count + 1; inner_count < pg->total;
1292 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1293 UINT64_MAX)) / ( (double) UINT64_MAX);
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1296 _("rand is %f probability is %f\n"), temp_rand,
1299 if (temp_rand < probability)
1301 connect_attempts += proc (pg, outer_count, inner_count);
1306 return connect_attempts;
1310 * Create a topology given a peer group (set of running peers)
1311 * and a connection processor.
1313 * @param pg the peergroup to create the topology on
1314 * @param proc the connection processor to call to actually set
1315 * up connections between two peers
1317 * @return the number of connections that were set up
1321 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1324 unsigned int square;
1327 unsigned int toggle = 1;
1328 unsigned int nodeToConnect;
1329 int connect_attempts;
1331 connect_attempts = 0;
1333 square = floor (sqrt (pg->total));
1337 if (square * square != pg->total)
1339 while (rows * cols < pg->total)
1341 if (toggle % 2 == 0)
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1351 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1354 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1355 * to the node to its right and above. Once this is over, we'll have our torus!
1356 * Special case for the last node (if the rows and columns are not equal), connect
1357 * to the first in the row to maintain topology.
1359 for (i = 0; i < pg->total; i++)
1361 /* First connect to the node to the right */
1362 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1363 nodeToConnect = i + 1;
1364 else if (i + 1 == pg->total)
1365 nodeToConnect = rows * cols - cols;
1367 nodeToConnect = i - cols + 1;
1369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1370 "Connecting peer %d to peer %d\n",
1373 connect_attempts += proc(pg, i, nodeToConnect);
1375 /* Second connect to the node immediately above */
1377 nodeToConnect = (rows * cols) - cols + i;
1379 nodeToConnect = i - cols;
1381 if (nodeToConnect < pg->total)
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1385 "Connecting peer %d to peer %d\n",
1388 connect_attempts += proc(pg, i, nodeToConnect);
1393 return connect_attempts;
1398 * Create a topology given a peer group (set of running peers)
1399 * and a connection processor.
1401 * @param pg the peergroup to create the topology on
1402 * @param proc the connection processor to call to actually set
1403 * up connections between two peers
1405 * @return the number of connections that were set up
1409 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1411 unsigned int outer_count;
1412 unsigned int inner_count;
1413 int connect_attempts;
1415 connect_attempts = 0;
1417 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1419 for (inner_count = outer_count + 1; inner_count < pg->total;
1423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1424 "Connecting peer %d to peer %d\n",
1425 outer_count, inner_count);
1427 connect_attempts += proc(pg, outer_count, inner_count);
1431 return connect_attempts;
1435 * Create a topology given a peer group (set of running peers)
1436 * and a connection processor.
1438 * @param pg the peergroup to create the topology on
1439 * @param proc the connection processor to call to actually set
1440 * up connections between two peers
1442 * @return the number of connections that were set up
1446 create_line (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1449 int connect_attempts;
1451 connect_attempts = 0;
1453 /* Connect each peer to the next highest numbered peer */
1454 for (count = 0; count < pg->total - 1; count++)
1457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1458 "Connecting peer %d to peer %d\n",
1461 connect_attempts += proc(pg, count, count + 1);
1464 return connect_attempts;
1468 * Create a topology given a peer group (set of running peers)
1469 * and a connection processor.
1471 * @param pg the peergroup to create the topology on
1472 * @param proc the connection processor to call to actually set
1473 * up connections between two peers
1475 * @return the number of connections that were set up
1479 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1482 int connect_attempts;
1484 connect_attempts = 0;
1486 /* Connect each peer to the next highest numbered peer */
1487 for (count = 0; count < pg->total - 1; count++)
1490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1491 "Connecting peer %d to peer %d\n",
1494 connect_attempts += proc(pg, count, count + 1);
1497 /* Connect the last peer to the first peer */
1498 connect_attempts += proc(pg, pg->total - 1, 0);
1500 return connect_attempts;
1505 * Iterator for writing friends of a peer to a file.
1507 * @param cls closure, an open writable file handle
1508 * @param key the key the daemon was stored under
1509 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1511 * @return GNUNET_YES to continue iteration
1513 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1514 * with a single file_iterator that takes a closure which contains
1515 * the prefix to write before the peer. Then this could be used
1516 * for blacklisting multiple transports and writing the friend
1517 * file. I'm sure *someone* will complain loudly about other
1518 * things that negate these functions even existing so no point in
1522 friend_file_iterator (void *cls,
1523 const GNUNET_HashCode * key,
1526 FILE *temp_friend_handle = cls;
1527 struct GNUNET_TESTING_Daemon *peer = value;
1528 struct GNUNET_PeerIdentity *temppeer;
1529 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1531 temppeer = &peer->id;
1532 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1533 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1538 struct BlacklistContext
1541 * The (open) file handle to write to
1543 FILE *temp_file_handle;
1546 * The transport that this peer will be blacklisted on.
1552 * Iterator for writing blacklist data to appropriate files.
1554 * @param cls closure, an open writable file handle
1555 * @param key the key the daemon was stored under
1556 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1558 * @return GNUNET_YES to continue iteration
1561 blacklist_file_iterator (void *cls,
1562 const GNUNET_HashCode * key,
1565 struct BlacklistContext *blacklist_ctx = cls;
1566 //FILE *temp_blacklist_handle = cls;
1567 struct GNUNET_TESTING_Daemon *peer = value;
1568 struct GNUNET_PeerIdentity *temppeer;
1569 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1571 temppeer = &peer->id;
1572 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1573 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1579 * Create the friend files based on the PeerConnection's
1580 * of each peer in the peer group, and copy the files
1581 * to the appropriate place
1583 * @param pg the peer group we are dealing with
1586 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1588 FILE *temp_friend_handle;
1589 unsigned int pg_iter;
1590 char *temp_service_path;
1594 enum GNUNET_OS_ProcessStatusType type;
1595 unsigned long return_code;
1600 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1601 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1603 mytemp = GNUNET_DISK_mktemp("friends");
1604 GNUNET_assert(mytemp != NULL);
1605 temp_friend_handle = fopen (mytemp, "wt");
1606 GNUNET_assert(temp_friend_handle != NULL);
1607 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1608 fclose(temp_friend_handle);
1611 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1613 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1614 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1617 if (UNLINK (mytemp) != 0)
1618 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1619 GNUNET_free (mytemp);
1623 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1625 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1626 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1627 "mv", mytemp, arg, NULL);
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1630 _("Copying file with command cp %s %s\n"), mytemp, arg);
1635 else /* Remote, scp the file to the correct place */
1637 if (NULL != pg->peers[pg_iter].daemon->username)
1638 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1640 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1641 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1642 "scp", mytemp, arg, NULL);
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1646 _("Copying file with command scp %s %s\n"), mytemp, arg);
1650 GNUNET_free (temp_service_path);
1651 GNUNET_free (mytemp);
1655 ret = GNUNET_SYSERR;
1656 while ((count < max_wait) && (ret != GNUNET_OK))
1659 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1663 _("Checking copy status of file %d\n"), pg_iter);
1665 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1667 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1669 ret = GNUNET_SYSERR;
1671 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1673 ret = GNUNET_SYSERR;
1677 pidarr[pg_iter] = 0;
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1680 _("File %d copied\n"), pg_iter);
1686 if (ret == GNUNET_SYSERR)
1688 /* FIXME: why sleep here? -CG */
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 _("Finished copying all friend files!\n"));
1697 GNUNET_free(pidarr);
1703 * Create the blacklist files based on the PeerConnection's
1704 * of each peer in the peer group, and copy the files
1705 * to the appropriate place.
1707 * @param pg the peer group we are dealing with
1708 * @param transports space delimited list of transports to blacklist
1711 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
1713 FILE *temp_file_handle;
1714 static struct BlacklistContext blacklist_ctx;
1715 unsigned int pg_iter;
1716 char *temp_service_path;
1720 enum GNUNET_OS_ProcessStatusType type;
1721 unsigned long return_code;
1728 char *temp_transports;
1730 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1731 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1733 mytemp = GNUNET_DISK_mktemp("blacklist");
1734 GNUNET_assert(mytemp != NULL);
1735 temp_file_handle = fopen (mytemp, "wt");
1736 GNUNET_assert(temp_file_handle != NULL);
1737 temp_transports = GNUNET_strdup(transports);
1738 blacklist_ctx.temp_file_handle = temp_file_handle;
1739 transport_len = strlen(temp_transports) + 1;
1742 for (i = 0; i < transport_len; i++)
1744 if ((temp_transports[i] == ' ') && (pos == NULL))
1745 continue; /* At start of string (whitespace) */
1746 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
1748 temp_transports[i] = '\0';
1749 blacklist_ctx.transport = pos;
1750 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
1752 } /* At beginning of actual string */
1753 else if (pos == NULL)
1755 pos = &temp_transports[i];
1759 GNUNET_free (temp_transports);
1760 fclose(temp_file_handle);
1763 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1765 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1766 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1769 if (UNLINK (mytemp) != 0)
1770 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1771 GNUNET_free (mytemp);
1775 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1777 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
1778 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1779 "mv", mytemp, arg, NULL);
1781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1782 _("Copying file with command cp %s %s\n"), mytemp, arg);
1787 else /* Remote, scp the file to the correct place */
1789 if (NULL != pg->peers[pg_iter].daemon->username)
1790 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1792 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1793 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1794 "scp", mytemp, arg, NULL);
1797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1798 _("Copying file with command scp %s %s\n"), mytemp, arg);
1802 GNUNET_free (temp_service_path);
1803 GNUNET_free (mytemp);
1807 ret = GNUNET_SYSERR;
1808 while ((count < max_wait) && (ret != GNUNET_OK))
1811 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1815 _("Checking copy status of file %d\n"), pg_iter);
1817 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1819 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1821 ret = GNUNET_SYSERR;
1823 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1825 ret = GNUNET_SYSERR;
1829 pidarr[pg_iter] = 0;
1831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1832 _("File %d copied\n"), pg_iter);
1838 if (ret == GNUNET_SYSERR)
1840 /* FIXME: why sleep here? -CG */
1846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1847 _("Finished copying all blacklist files!\n"));
1849 GNUNET_free(pidarr);
1855 * Internal notification of a connection, kept so that we can ensure some connections
1856 * happen instead of flooding all testing daemons with requests to connect.
1858 static void internal_connect_notify (void *cls,
1859 const struct GNUNET_PeerIdentity *first,
1860 const struct GNUNET_PeerIdentity *second,
1862 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
1863 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
1864 struct GNUNET_TESTING_Daemon *first_daemon,
1865 struct GNUNET_TESTING_Daemon *second_daemon,
1868 struct GNUNET_TESTING_PeerGroup *pg = cls;
1869 outstanding_connects--;
1871 pg->notify_connection(pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
1875 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1877 struct ConnectContext *connect_context = cls;
1879 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1882 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
1884 #if VERBOSE_TESTING > 2
1885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1886 _("Delaying connect, we have too many outstanding connections!\n"));
1888 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &schedule_connect, connect_context);
1892 #if VERBOSE_TESTING > 2
1893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1894 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
1896 outstanding_connects++;
1897 GNUNET_TESTING_daemons_connect (connect_context->first,
1898 connect_context->second,
1901 &internal_connect_notify,
1902 connect_context->pg);
1903 GNUNET_free(connect_context);
1909 * Iterator for actually scheduling connections to be created
1910 * between two peers.
1912 * @param cls closure, a GNUNET_TESTING_Daemon
1913 * @param key the key the second Daemon was stored under
1914 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1916 * @return GNUNET_YES to continue iteration
1919 connect_iterator (void *cls,
1920 const GNUNET_HashCode * key,
1923 struct PeerData *first = cls;
1924 struct GNUNET_TESTING_Daemon *second = value;
1925 struct ConnectContext *connect_context;
1927 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1928 connect_context->pg = first->pg;
1929 connect_context->first = first->daemon;
1930 connect_context->second = second;
1931 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
1938 * Iterator for copying all entries in the allowed hashmap to the
1941 * @param cls closure, a GNUNET_TESTING_Daemon
1942 * @param key the key the second Daemon was stored under
1943 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1945 * @return GNUNET_YES to continue iteration
1948 copy_topology_iterator (void *cls,
1949 const GNUNET_HashCode * key,
1952 struct PeerData *first = cls;
1954 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1960 * Make the peers to connect the same as those that are allowed to be
1963 * @param pg the peer group
1966 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
1968 unsigned int pg_iter;
1973 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1975 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
1976 if (GNUNET_SYSERR == ret)
1977 return GNUNET_SYSERR;
1979 total = total + ret;
1987 * Connect the topology as specified by the PeerConnection's
1988 * of each peer in the peer group
1990 * @param pg the peer group we are dealing with
1991 * @return the number of connections that will be attempted
1994 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
1996 unsigned int pg_iter;
2000 struct PeerConnection *connection_iter;
2001 struct ConnectContext *connect_context;
2005 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2007 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
2008 if (GNUNET_SYSERR == ret)
2009 return GNUNET_SYSERR;
2011 total = total + ret;
2015 while (connection_iter != NULL)
2017 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2018 connect_context->pg = pg;
2019 connect_context->first = ;
2020 connect_context->second = connection_iter->daemon;
2021 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
2022 connection_iter = connection_iter->next;
2031 * Takes a peer group and creates a topology based on the
2032 * one specified. Creates a topology means generates friend
2033 * files for the peers so they can only connect to those allowed
2034 * by the topology. This will only have an effect once peers
2035 * are started if the FRIENDS_ONLY option is set in the base
2036 * config. Also takes an optional restrict topology which
2037 * disallows direct TCP connections UNLESS they are specified in
2038 * the restricted topology.
2040 * @param pg the peer group struct representing the running peers
2041 * @param topology which topology to connect the peers in
2042 * @param restrict_topology allow only direct TCP connections in this topology
2043 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2044 * @param restrict_transports space delimited list of transports to blacklist
2045 * to create restricted topology
2047 * @return the maximum number of connections were all allowed peers
2048 * connected to each other
2051 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2052 enum GNUNET_TESTING_Topology topology,
2053 enum GNUNET_TESTING_Topology restrict_topology,
2054 char *restrict_transports)
2057 int num_connections;
2058 int unblacklisted_connections;
2060 GNUNET_assert (pg->notify_connection != NULL);
2065 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2066 #if VERBOSE_TOPOLOGY
2067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2068 _("Creating clique topology\n"));
2070 num_connections = create_clique (pg, &add_allowed_connections);
2072 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2073 #if VERBOSE_TOPOLOGY
2074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2075 _("Creating small world (ring) topology\n"));
2077 num_connections = create_small_world_ring (pg, &add_allowed_connections);
2079 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2080 #if VERBOSE_TOPOLOGY
2081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2082 _("Creating small world (2d-torus) topology\n"));
2084 num_connections = create_small_world (pg, &add_allowed_connections);
2086 case GNUNET_TESTING_TOPOLOGY_RING:
2087 #if VERBOSE_TOPOLOGY
2088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2089 _("Creating ring topology\n"));
2091 num_connections = create_ring (pg, &add_allowed_connections);
2093 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2094 #if VERBOSE_TOPOLOGY
2095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2096 _("Creating 2d torus topology\n"));
2098 num_connections = create_2d_torus (pg, &add_allowed_connections);
2100 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2101 #if VERBOSE_TOPOLOGY
2102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2103 _("Creating Erdos-Renyi topology\n"));
2105 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2107 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2108 #if VERBOSE_TOPOLOGY
2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2110 _("Creating InterNAT topology\n"));
2112 num_connections = create_nated_internet (pg, &add_allowed_connections);
2114 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2117 _("Creating Scale Free topology\n"));
2119 num_connections = create_scale_free (pg, &add_allowed_connections);
2121 case GNUNET_TESTING_TOPOLOGY_LINE:
2123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2124 _("Creating straight line topology\n"));
2126 num_connections = create_line (pg, &add_allowed_connections);
2128 case GNUNET_TESTING_TOPOLOGY_NONE:
2129 num_connections = 0;
2132 num_connections = 0;
2135 if (num_connections < 1)
2136 return GNUNET_SYSERR;
2138 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2140 ret = create_and_copy_friend_files(pg);
2143 if (ret != GNUNET_OK)
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2147 _("Failed during friend file copying!\n"));
2149 return GNUNET_SYSERR;
2154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2155 _("Friend files created/copied successfully!\n"));
2159 /* Use the create clique method to initially set all connections as blacklisted. */
2160 create_clique (pg, &blacklist_connections);
2161 unblacklisted_connections = 0;
2162 /* Un-blacklist connections as per the topology specified */
2163 switch (restrict_topology)
2165 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2168 _("Blacklisting all but clique topology\n"));
2170 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2172 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2175 _("Blacklisting all but small world (ring) topology\n"));
2177 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2179 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2182 _("Blacklisting all but small world (2d-torus) topology\n"));
2184 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2186 case GNUNET_TESTING_TOPOLOGY_RING:
2188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2189 _("Blacklisting all but ring topology\n"));
2191 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2193 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2196 _("Blacklisting all but 2d torus topology\n"));
2198 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2200 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2203 _("Blacklisting all but Erdos-Renyi topology\n"));
2205 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2207 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2210 _("Blacklisting all but InterNAT topology\n"));
2212 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2214 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2217 _("Blacklisting all but Scale Free topology\n"));
2219 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2221 case GNUNET_TESTING_TOPOLOGY_LINE:
2223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2224 _("Blacklisting all but straight line topology\n"));
2226 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2228 case GNUNET_TESTING_TOPOLOGY_NONE:
2234 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2236 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2237 if (ret != GNUNET_OK)
2240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2241 _("Failed during blacklist file copying!\n"));
2243 return GNUNET_SYSERR;
2248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2249 _("Blacklist files created/copied successfully!\n"));
2253 return num_connections;
2256 struct RandomContext
2261 struct GNUNET_TESTING_PeerGroup *pg;
2264 * uid of the first peer
2269 * Peer data for first peer.
2271 struct PeerData *first;
2274 * Random percentage to use
2279 struct MinimumContext
2284 struct GNUNET_TESTING_PeerGroup *pg;
2287 * uid of the first peer
2292 * Peer data for first peer.
2294 struct PeerData *first;
2297 * Number of conns per peer
2299 unsigned int num_to_add;
2302 * Permuted array of all possible connections. Only add the Nth
2303 * peer if it's in the Nth position.
2305 unsigned int *pg_array;
2308 * What number is the current element we are iterating over?
2310 unsigned int current;
2318 struct GNUNET_TESTING_PeerGroup *pg;
2321 * uid of the first peer
2326 * uid of the second peer
2328 uint32_t second_uid;
2331 * Peer data for first peer.
2333 struct PeerData *first;
2336 * Which peer has been chosen as the one to add?
2338 unsigned int chosen;
2341 * What number is the current element we are iterating over?
2343 unsigned int current;
2347 * Iterator for choosing random peers to connect.
2349 * @param cls closure, a RandomContext
2350 * @param key the key the second Daemon was stored under
2351 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2353 * @return GNUNET_YES to continue iteration
2356 random_connect_iterator (void *cls,
2357 const GNUNET_HashCode * key,
2360 struct RandomContext *random_ctx = cls;
2361 double random_number;
2362 uint32_t second_pos;
2363 GNUNET_HashCode first_hash;
2364 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2365 UINT64_MAX)) / ( (double) UINT64_MAX);
2366 if (random_number < random_ctx->percentage)
2368 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2370 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2371 uid_from_hash(key, &second_pos);
2372 hash_from_uid(random_ctx->first_uid, &first_hash);
2373 GNUNET_assert(random_ctx->pg->total > second_pos);
2374 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2380 * Iterator for adding at least X peers to a peers connection set.
2382 * @param cls closure, MinimumContext
2383 * @param key the key the second Daemon was stored under
2384 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2386 * @return GNUNET_YES to continue iteration
2389 minimum_connect_iterator (void *cls,
2390 const GNUNET_HashCode * key,
2393 struct MinimumContext *min_ctx = cls;
2394 uint32_t second_pos;
2395 GNUNET_HashCode first_hash;
2398 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2400 for (i = 0; i < min_ctx->num_to_add; i++)
2402 if (min_ctx->pg_array[i] == min_ctx->current)
2404 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2405 uid_from_hash(key, &second_pos);
2406 hash_from_uid(min_ctx->first_uid, &first_hash);
2407 GNUNET_assert(min_ctx->pg->total > second_pos);
2408 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));
2409 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2410 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2417 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2423 * Iterator for adding peers to a connection set based on a depth first search.
2425 * @param cls closure, MinimumContext
2426 * @param key the key the second daemon was stored under
2427 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2429 * @return GNUNET_YES to continue iteration
2432 dfs_connect_iterator (void *cls,
2433 const GNUNET_HashCode * key,
2436 struct DFSContext *dfs_ctx = cls;
2437 GNUNET_HashCode first_hash;
2439 if (dfs_ctx->current == dfs_ctx->chosen)
2441 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2442 uid_from_hash(key, &dfs_ctx->second_uid);
2443 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2444 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));
2445 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2446 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2447 return GNUNET_NO; /* We have found our peer, don't iterate more */
2456 * From the set of connections possible, choose percentage percent of connections
2457 * to actually connect.
2459 * @param pg the peergroup we are dealing with
2460 * @param percentage what percent of total connections to make
2463 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2465 struct RandomContext random_ctx;
2468 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2470 random_ctx.first_uid = pg_iter;
2471 random_ctx.first = &pg->peers[pg_iter];
2472 random_ctx.percentage = percentage;
2474 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2475 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2476 /* Now remove the old connections */
2477 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2478 /* And replace with the random set */
2479 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2484 * From the set of connections possible, choose at least num connections per
2487 * @param pg the peergroup we are dealing with
2488 * @param num how many connections at least should each peer have (if possible)?
2491 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2493 struct MinimumContext minimum_ctx;
2496 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2498 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2501 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2503 minimum_ctx.first_uid = pg_iter;
2504 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2505 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2506 minimum_ctx.first = &pg->peers[pg_iter];
2507 minimum_ctx.pg = pg;
2508 minimum_ctx.num_to_add = num;
2509 minimum_ctx.current = 0;
2510 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2511 &minimum_connect_iterator,
2515 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2517 /* Remove the "old" connections */
2518 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2519 /* And replace with the working set */
2520 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2527 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2530 unsigned int pg_iter;
2534 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2536 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2543 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2546 unsigned int pg_iter;
2550 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2552 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2559 * From the set of connections possible, choose at least num connections per
2560 * peer based on depth first traversal of peer connections. If DFS leaves
2561 * peers unconnected, ensure those peers get connections.
2563 * @param pg the peergroup we are dealing with
2564 * @param num how many connections at least should each peer have (if possible)?
2567 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2569 struct DFSContext dfs_ctx;
2572 uint32_t starting_peer;
2573 uint32_t least_connections;
2574 GNUNET_HashCode second_hash;
2576 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2578 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2583 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
2585 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
2587 least_connections = -1; /* Set to very high number */
2588 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2590 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
2592 starting_peer = pg_iter;
2593 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2598 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
2604 /* Choose a random peer from the chosen peers set of connections to add */
2605 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
2606 dfs_ctx.first_uid = starting_peer;
2607 dfs_ctx.first = &pg->peers[starting_peer];
2609 dfs_ctx.current = 0;
2611 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
2612 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
2613 hash_from_uid(dfs_ctx.second_uid, &second_hash);
2614 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
2615 starting_peer = dfs_ctx.second_uid;
2618 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2620 /* Remove the "old" connections */
2621 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2622 /* And replace with the working set */
2623 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2628 * There are many ways to connect peers that are supported by this function.
2629 * To connect peers in the same topology that was created via the
2630 * GNUNET_TESTING_create_topology, the topology variable must be set to
2631 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
2632 * a new instance of that topology will be generated and attempted to be
2633 * connected. This could result in some connections being impossible,
2634 * because some topologies are non-deterministic.
2636 * @param pg the peer group struct representing the running peers
2637 * @param topology which topology to connect the peers in
2638 * @param options options for connecting the topology
2639 * @param option_modifier modifier for options that take a parameter
2640 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
2643 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2644 enum GNUNET_TESTING_Topology topology,
2645 enum GNUNET_TESTING_TopologyOption options,
2646 double option_modifier)
2650 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2651 #if VERBOSE_TOPOLOGY
2652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2653 _("Creating clique CONNECT topology\n"));
2655 create_clique (pg, &add_actual_connections);
2657 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2658 #if VERBOSE_TOPOLOGY
2659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2660 _("Creating small world (ring) CONNECT topology\n"));
2662 create_small_world_ring (pg, &add_actual_connections);
2664 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2665 #if VERBOSE_TOPOLOGY
2666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2667 _("Creating small world (2d-torus) CONNECT topology\n"));
2669 create_small_world (pg, &add_actual_connections);
2671 case GNUNET_TESTING_TOPOLOGY_RING:
2672 #if VERBOSE_TOPOLOGY
2673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2674 _("Creating ring CONNECT topology\n"));
2676 create_ring (pg, &add_actual_connections);
2678 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2679 #if VERBOSE_TOPOLOGY
2680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2681 _("Creating 2d torus CONNECT topology\n"));
2683 create_2d_torus (pg, &add_actual_connections);
2685 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2686 #if VERBOSE_TOPOLOGY
2687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2688 _("Creating Erdos-Renyi CONNECT topology\n"));
2690 create_erdos_renyi (pg, &add_actual_connections);
2692 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2693 #if VERBOSE_TOPOLOGY
2694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2695 _("Creating InterNAT CONNECT topology\n"));
2697 create_nated_internet (pg, &add_actual_connections);
2699 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2700 #if VERBOSE_TOPOLOGY
2701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2702 _("Creating Scale Free CONNECT topology\n"));
2704 create_scale_free (pg, &add_actual_connections);
2706 case GNUNET_TESTING_TOPOLOGY_LINE:
2707 #if VERBOSE_TOPOLOGY
2708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2709 _("Creating straight line CONNECT topology\n"));
2711 create_line (pg, &add_actual_connections);
2713 case GNUNET_TESTING_TOPOLOGY_NONE:
2714 #if VERBOSE_TOPOLOGY
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2716 _("Creating no CONNECT topology\n"));
2718 copy_allowed_topology(pg);
2721 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2722 _("Unknown topology specification, can't connect peers!\n"));
2723 return GNUNET_SYSERR;
2728 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
2729 #if VERBOSE_TOPOLOGY
2730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2731 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
2733 choose_random_connections(pg, option_modifier);
2735 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
2736 #if VERBOSE_TOPOLOGY
2737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2738 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
2740 choose_minimum(pg, (unsigned int)option_modifier);
2742 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
2743 #if VERBOSE_TOPOLOGY
2744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2745 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
2747 perform_dfs(pg, (int)option_modifier);
2749 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
2751 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
2757 return connect_topology(pg);
2761 * Function which continues a peer group starting up
2762 * after successfully generating hostkeys for each peer.
2764 * @param pg the peer group to continue starting
2768 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
2772 for (i = 0; i < pg->total; i++)
2774 GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
2779 * Start count gnunetd processes with the same set of transports and
2780 * applications. The port numbers (any option called "PORT") will be
2781 * adjusted to ensure that no two peers running on the same system
2782 * have the same port(s) in their respective configurations.
2784 * @param sched scheduler to use
2785 * @param cfg configuration template to use
2786 * @param total number of daemons to start
2787 * @param timeout total time allowed for peers to start
2788 * @param hostkey_callback function to call on each peers hostkey generation
2789 * if NULL, peers will be started by this call, if non-null,
2790 * GNUNET_TESTING_daemons_continue_startup must be called after
2791 * successful hostkey generation
2792 * @param hostkey_cls closure for hostkey callback
2793 * @param cb function to call on each daemon that was started
2794 * @param cb_cls closure for cb
2795 * @param connect_callback function to call each time two hosts are connected
2796 * @param connect_callback_cls closure for connect_callback
2797 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
2798 * everything on localhost).
2799 * @return NULL on error, otherwise handle to control peer group
2801 struct GNUNET_TESTING_PeerGroup *
2802 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
2803 const struct GNUNET_CONFIGURATION_Handle *cfg,
2805 struct GNUNET_TIME_Relative timeout,
2806 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
2808 GNUNET_TESTING_NotifyDaemonRunning cb,
2810 GNUNET_TESTING_NotifyConnection
2811 connect_callback, void *connect_callback_cls,
2812 const char *hostnames)
2814 struct GNUNET_TESTING_PeerGroup *pg;
2818 const char *hostname;
2819 char *baseservicehome;
2820 char *newservicehome;
2822 struct GNUNET_CONFIGURATION_Handle *pcfg;
2824 unsigned int hostcnt;
2834 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
2838 pg->cb_cls = cb_cls;
2839 pg->notify_connection = connect_callback;
2840 pg->notify_connection_cls = connect_callback_cls;
2842 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
2843 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
2844 if (NULL != hostnames)
2847 /* skip leading spaces */
2848 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
2851 while ('\0' != *rpos)
2853 if (isspace ( (unsigned char) *rpos))
2857 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
2859 start = GNUNET_strdup (hostnames);
2861 while ('\0' != *pos)
2863 if (isspace ( (unsigned char) *pos))
2866 if (strlen (start) > 0)
2868 pg->hosts[off].minport = LOW_PORT;
2869 pg->hosts[off++].hostname = start;
2875 if (strlen (start) > 0)
2877 pg->hosts[off].minport = LOW_PORT;
2878 pg->hosts[off++].hostname = start;
2882 GNUNET_free (start);
2883 GNUNET_free (pg->hosts);
2887 minport = 0; /* make gcc happy */
2894 for (off = 0; off < total; off++)
2898 hostname = pg->hosts[off % hostcnt].hostname;
2899 pcfg = make_config (cfg,
2900 &pg->hosts[off % hostcnt].minport,
2907 pcfg = make_config (cfg,
2915 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2917 ("Could not create configuration for peer number %u on `%s'!\n"),
2918 off, hostname == NULL ? "localhost" : hostname);
2923 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
2926 GNUNET_asprintf (&newservicehome,
2927 "%s/%d/", baseservicehome, off);
2928 GNUNET_free (baseservicehome);
2932 tmpdir = getenv ("TMPDIR");
2933 tmpdir = tmpdir ? tmpdir : "/tmp";
2934 GNUNET_asprintf (&newservicehome,
2937 "gnunet-testing-test-test", off);
2939 GNUNET_CONFIGURATION_set_value_string (pcfg,
2941 "SERVICEHOME", newservicehome);
2942 GNUNET_free (newservicehome);
2943 pg->peers[off].cfg = pcfg;
2944 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
2945 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
2946 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
2947 pg->peers[off].pg = pg;
2948 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (sched,
2955 if (NULL == pg->peers[off].daemon)
2956 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2957 _("Could not start peer number %u!\n"), off);
2964 * Get a daemon by number, so callers don't have to do nasty
2965 * offsetting operation.
2967 struct GNUNET_TESTING_Daemon *
2968 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
2970 if (position < pg->total)
2971 return pg->peers[position].daemon;
2977 * Prototype of a function that will be called when a
2978 * particular operation was completed the testing library.
2980 * @param cls closure (a struct RestartContext)
2981 * @param id id of the peer that was restarted
2982 * @param cfg handle to the configuration of the peer
2983 * @param d handle to the daemon that was restarted
2984 * @param emsg NULL on success
2986 void restart_callback (void *cls,
2987 const struct GNUNET_PeerIdentity *id,
2988 const struct GNUNET_CONFIGURATION_Handle *cfg,
2989 struct GNUNET_TESTING_Daemon *d,
2992 struct RestartContext *restart_context = cls;
2996 restart_context->peers_restarted++;
3000 restart_context->peers_restart_failed++;
3003 if (restart_context->peers_restarted == restart_context->peer_group->total)
3005 restart_context->callback(restart_context->callback_cls, NULL);
3006 GNUNET_free(restart_context);
3008 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
3010 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
3011 GNUNET_free(restart_context);
3017 * Callback for informing us about a successful
3018 * or unsuccessful churn stop call.
3020 * @param cls a ChurnContext
3021 * @param emsg NULL on success, non-NULL on failure
3025 churn_stop_callback (void *cls, const char *emsg)
3027 struct ChurnContext *churn_ctx = cls;
3028 unsigned int total_left;
3029 char *error_message;
3031 error_message = NULL;
3034 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3035 "Churn stop callback failed with error `%s'\n", emsg);
3036 churn_ctx->num_failed_stop++;
3040 churn_ctx->num_to_stop--;
3044 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3045 "Stopped peer, %d left.\n",
3046 churn_ctx->num_to_stop);
3048 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3050 if (total_left == 0)
3052 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3054 GNUNET_asprintf(&error_message,
3055 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3056 churn_ctx->num_failed_start,
3057 churn_ctx->num_failed_stop);
3059 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3060 GNUNET_free_non_null(error_message);
3061 GNUNET_free(churn_ctx);
3066 * Callback for informing us about a successful
3067 * or unsuccessful churn start call.
3069 * @param cls a ChurnContext
3070 * @param id the peer identity of the started peer
3071 * @param cfg the handle to the configuration of the peer
3072 * @param d handle to the daemon for the peer
3073 * @param emsg NULL on success, non-NULL on failure
3077 churn_start_callback (void *cls,
3078 const struct GNUNET_PeerIdentity *id,
3079 const struct GNUNET_CONFIGURATION_Handle *cfg,
3080 struct GNUNET_TESTING_Daemon *d,
3083 struct ChurnContext *churn_ctx = cls;
3084 unsigned int total_left;
3085 char *error_message;
3087 error_message = NULL;
3090 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3091 "Churn stop callback failed with error `%s'\n",
3093 churn_ctx->num_failed_start++;
3097 churn_ctx->num_to_start--;
3101 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3102 "Started peer, %d left.\n",
3103 churn_ctx->num_to_start);
3106 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3108 if (total_left == 0)
3110 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3111 GNUNET_asprintf(&error_message,
3112 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3113 churn_ctx->num_failed_start,
3114 churn_ctx->num_failed_stop);
3115 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3116 GNUNET_free_non_null(error_message);
3117 GNUNET_free(churn_ctx);
3123 * Simulate churn by stopping some peers (and possibly
3124 * re-starting others if churn is called multiple times). This
3125 * function can only be used to create leave-join churn (peers "never"
3126 * leave for good). First "voff" random peers that are currently
3127 * online will be taken offline; then "von" random peers that are then
3128 * offline will be put back online. No notifications will be
3129 * generated for any of these operations except for the callback upon
3132 * @param pg handle for the peer group
3133 * @param voff number of peers that should go offline
3134 * @param von number of peers that should come back online;
3135 * must be zero on first call (since "testbed_start"
3136 * always starts all of the peers)
3137 * @param timeout how long to wait for operations to finish before
3139 * @param cb function to call at the end
3140 * @param cb_cls closure for cb
3143 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
3146 struct GNUNET_TIME_Relative timeout,
3147 GNUNET_TESTING_NotifyCompletion cb,
3150 struct ChurnContext *churn_ctx;
3151 unsigned int running;
3152 unsigned int stopped;
3154 unsigned int *running_arr;
3155 unsigned int *stopped_arr;
3156 unsigned int *running_permute;
3157 unsigned int *stopped_permute;
3162 if ((von == 0) && (voff == 0)) /* No peers at all? */
3168 for (i = 0; i < pg->total; i++)
3170 if (pg->peers[i].daemon->running == GNUNET_YES)
3172 GNUNET_assert(running != -1);
3177 GNUNET_assert(stopped != -1);
3184 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
3185 cb(cb_cls, "Trying to stop more peers than are currently running!");
3191 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
3192 cb(cb_cls, "Trying to start more peers than are currently stopped!");
3196 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3197 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
3198 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
3200 running_permute = NULL;
3201 stopped_permute = NULL;
3204 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
3206 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
3211 churn_ctx->num_to_start = von;
3212 churn_ctx->num_to_stop = voff;
3214 churn_ctx->cb_cls = cb_cls;
3216 for (i = 0; i < pg->total; i++)
3218 if (pg->peers[i].daemon->running == GNUNET_YES)
3220 running_arr[running] = i;
3225 stopped_arr[stopped] = i;
3230 for (i = 0; i < voff; i++)
3233 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
3235 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
3237 &churn_stop_callback, churn_ctx,
3238 GNUNET_NO, GNUNET_YES);
3241 for (i = 0; i < von; i++)
3244 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
3246 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
3247 timeout, &churn_start_callback, churn_ctx);
3250 GNUNET_free(running_arr);
3251 GNUNET_free(stopped_arr);
3252 GNUNET_free_non_null(running_permute);
3253 GNUNET_free_non_null(stopped_permute);
3258 * Restart all peers in the given group.
3260 * @param pg the handle to the peer group
3261 * @param callback function to call on completion (or failure)
3262 * @param callback_cls closure for the callback function
3265 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyCompletion callback, void *callback_cls)
3267 struct RestartContext *restart_context;
3272 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
3273 restart_context->peer_group = pg;
3274 restart_context->peers_restarted = 0;
3275 restart_context->callback = callback;
3276 restart_context->callback_cls = callback_cls;
3278 for (off = 0; off < pg->total; off++)
3280 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
3286 * Start or stop an individual peer from the given group.
3288 * @param pg handle to the peer group
3289 * @param offset which peer to start or stop
3290 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
3291 * @param timeout how long to wait for shutdown
3292 * @param cb function to call at the end
3293 * @param cb_cls closure for cb
3296 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
3297 unsigned int offset,
3299 struct GNUNET_TIME_Relative timeout,
3300 GNUNET_TESTING_NotifyCompletion cb,
3303 struct ChurnContext *churn_ctx;
3305 if (GNUNET_NO == desired_status)
3307 if (NULL != pg->peers[offset].daemon)
3309 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3310 churn_ctx->num_to_start = 0;
3311 churn_ctx->num_to_stop = 1;
3313 churn_ctx->cb_cls = cb_cls;
3314 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
3315 timeout, &churn_stop_callback, churn_ctx,
3316 GNUNET_NO, GNUNET_YES);
3319 else if (GNUNET_YES == desired_status)
3321 if (NULL == pg->peers[offset].daemon)
3323 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3324 churn_ctx->num_to_start = 1;
3325 churn_ctx->num_to_stop = 0;
3327 churn_ctx->cb_cls = cb_cls;
3328 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
3329 timeout, &churn_start_callback, churn_ctx);
3338 * Shutdown all peers started in the given group.
3340 * @param pg handle to the peer group
3341 * @param timeout how long to wait for shutdown
3344 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
3345 struct GNUNET_TIME_Relative timeout)
3349 for (off = 0; off < pg->total; off++)
3351 /* FIXME: should we wait for our continuations to be called
3352 here? This would require us to take a continuation as
3355 if (NULL != pg->peers[off].daemon)
3356 GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, NULL, NULL, GNUNET_YES, GNUNET_NO);
3357 if (NULL != pg->peers[off].cfg)
3358 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
3359 if (pg->peers[off].allowed_peers != NULL)
3360 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
3361 if (pg->peers[off].connect_peers != NULL)
3362 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
3363 if (pg->peers[off].blacklisted_peers != NULL)
3364 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
3366 GNUNET_free (pg->peers);
3367 if (NULL != pg->hosts)
3369 GNUNET_free (pg->hosts[0].hostname);
3370 GNUNET_free (pg->hosts);
3376 /* end of testing_group.c */