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
27 #include "gnunet_arm_service.h"
28 #include "gnunet_testing_lib.h"
30 #define VERBOSE_TESTING GNUNET_NO
32 #define VERBOSE_TOPOLOGY GNUNET_NO
34 #define DEBUG_CHURN GNUNET_NO
37 * Lowest port used for GNUnet testing. Should be high enough to not
38 * conflict with other applications running on the hosts but be low
39 * enough to not conflict with client-ports (typically starting around
42 #define LOW_PORT 10000
45 * Highest port used for GNUnet testing. Should be low enough to not
46 * conflict with the port range for "local" ports (client apps; see
47 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
49 #define HIGH_PORT 32000
51 #define MAX_OUTSTANDING_CONNECTIONS 50
53 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
55 #define CONNECT_ATTEMPTS 8
58 * Prototype of a function called whenever two peers would be connected
59 * in a certain topology.
61 typedef int (*GNUNET_TESTING_ConnectionProcessor)
62 (struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second);
65 * Strings representing topologies in enum
67 static char * GNUNET_TESTING_TopologyStrings[] =
70 * A clique (everyone connected to everyone else).
75 * Small-world network (2d torus plus random links).
80 * Small-world network (ring plus random links).
100 * Certain percentage of peers are unable to communicate directly
101 * replicating NAT conditions
106 * Scale free topology.
111 * All peers are disconnected.
117 * Options for connecting a topology as strings.
119 static char * GNUNET_TESTING_TopologyOptionStrings[] =
122 * Try to connect all peers specified in the topology.
127 * Choose a random subset of connections to create.
129 "CONNECT_RANDOM_SUBSET",
132 * Create at least X connections for each peer.
137 * Using a depth first search, create one connection
138 * per peer. If any are missed (graph disconnected)
139 * start over at those peers until all have at least one
145 * No options specified.
151 * Context for handling churning a peer group
156 * Callback used to notify of churning finished
158 GNUNET_TESTING_NotifyCompletion cb;
161 * Closure for callback
166 * Number of peers that still need to be started
168 unsigned int num_to_start;
171 * Number of peers that still need to be stopped
173 unsigned int num_to_stop;
176 * Number of peers that failed to start
178 unsigned int num_failed_start;
181 * Number of peers that failed to stop
183 unsigned int num_failed_stop;
186 struct RestartContext
189 * The group of peers being restarted
191 struct GNUNET_TESTING_PeerGroup *peer_group;
194 * How many peers have been restarted thus far
196 unsigned int peers_restarted;
199 * How many peers got an error when restarting
201 unsigned int peers_restart_failed;
204 * The function to call once all peers have been restarted
206 GNUNET_TESTING_NotifyCompletion callback;
209 * Closure for callback function
215 struct CreateTopologyContext
219 * Function to call with number of connections
221 GNUNET_TESTING_NotifyConnections cont;
224 * Closure for connection notification
230 struct PeerConnection
235 struct PeerConnection *next;
238 * Pointer to daemon handle
240 struct GNUNET_TESTING_Daemon *daemon;
246 * Data we keep per peer.
251 * (Initial) configuration of the host.
252 * (initial because clients could change
253 * it and we would not know about those
256 struct GNUNET_CONFIGURATION_Handle *cfg;
259 * Handle for controlling the daemon.
261 struct GNUNET_TESTING_Daemon *daemon;
264 * The peergroup this peer belongs to.
266 struct GNUNET_TESTING_PeerGroup *pg;
269 * Linked list of peer connections (pointers)
271 //struct PeerConnection *connected_peers;
273 * Hash map of allowed peer connections (F2F created topology)
275 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
278 * Hash map of blacklisted peers
280 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
283 * Hash map of peer connections
285 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
288 * Temporary hash map of peer connections
290 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
293 * Temporary variable for topology creation, should be reset before
294 * creating any topology so the count is valid once finished.
301 * Data we keep per host.
311 * Lowest port that we have not yet used
319 * Handle to a group of GNUnet peers.
321 struct GNUNET_TESTING_PeerGroup
326 struct GNUNET_SCHEDULER_Handle *sched;
329 * Configuration template.
331 const struct GNUNET_CONFIGURATION_Handle *cfg;
334 * Function to call on each started daemon.
336 GNUNET_TESTING_NotifyDaemonRunning cb;
344 * Function to call on each topology connection created
346 GNUNET_TESTING_NotifyConnection notify_connection;
349 * Callback for notify_connection
351 void *notify_connection_cls;
354 * NULL-terminated array of information about
357 struct HostData *hosts;
360 * Array of "total" peers.
362 struct PeerData *peers;
365 * Number of peers in this group.
370 * At what time should we fail the peer startup process?
372 struct GNUNET_TIME_Absolute max_timeout;
376 * Convert unique ID to hash code.
378 * @param uid unique ID to convert
379 * @param hash set to uid (extended with zeros)
382 hash_from_uid (uint32_t uid,
383 GNUNET_HashCode *hash)
385 memset (hash, 0, sizeof(GNUNET_HashCode));
386 *((uint32_t*)hash) = uid;
390 * Convert hash code to unique ID.
392 * @param uid unique ID to convert
393 * @param hash set to uid (extended with zeros)
396 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
398 memcpy (uid, hash, sizeof(uint32_t));
403 struct GNUNET_CONFIGURATION_Handle *ret;
404 const char *hostname;
410 struct ConnectContext
412 struct GNUNET_TESTING_Daemon *first;
414 struct GNUNET_TESTING_Daemon *second;
416 struct GNUNET_TESTING_PeerGroup *pg;
420 * Number of connects we are waiting on, allows us to rate limit
423 static int outstanding_connects;
426 * Get a topology from a string input.
428 * @param topology where to write the retrieved topology
429 * @param topology_string The string to attempt to
430 * get a configuration value from
431 * @return GNUNET_YES if topology string matched a
432 * known topology, GNUNET_NO if not
435 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string)
440 if (topology_string == NULL)
445 if (strcmp(GNUNET_TESTING_TopologyStrings[curr], topology_string) == 0)
451 } while (strcmp(GNUNET_TESTING_TopologyStrings[curr], "NONE") != 0);
460 * Get connect topology option from string input.
462 * @param topology where to write the retrieved topology
463 * @param topology_string The string to attempt to
464 * get a configuration value from
465 * @return GNUNET_YES if string matched a known
466 * topology option, GNUNET_NO if not
469 GNUNET_TESTING_topology_option_get(enum GNUNET_TESTING_TopologyOption *topology, char * topology_string)
474 if (topology_string == NULL)
479 if (strcmp(GNUNET_TESTING_TopologyOptionStrings[curr], topology_string) == 0)
485 } while (strcmp(GNUNET_TESTING_TopologyOptionStrings[curr], "CONNECT_NONE") != 0);
494 * Function to iterate over options. Copies
495 * the options to the target configuration,
496 * updating PORT values as needed.
499 * @param section name of the section
500 * @param option name of the option
501 * @param value value of the option
504 update_config (void *cls,
505 const char *section, const char *option, const char *value)
507 struct UpdateContext *ctx = cls;
512 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
516 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
521 if (0 == strcmp (option, "UNIXPATH"))
523 GNUNET_snprintf (uval,
525 "/tmp/test-service-%s-%u",
531 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
533 value = ctx->hostname;
536 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
541 * Create a new configuration using the given configuration
542 * as a template; however, each PORT in the existing cfg
543 * must be renumbered by incrementing "*port". If we run
544 * out of "*port" numbers, return NULL.
546 * @param cfg template configuration
547 * @param port port numbers to use, update to reflect
548 * port numbers that were used
549 * @param upnum number to make unix domain socket names unique
550 * @param hostname hostname of the controlling host, to allow control connections from
552 * @return new configuration, NULL on error
554 static struct GNUNET_CONFIGURATION_Handle *
555 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
558 const char *hostname)
560 struct UpdateContext uc;
568 uc.ret = GNUNET_CONFIGURATION_create ();
569 uc.hostname = hostname;
571 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
572 if (uc.nport >= HIGH_PORT)
575 GNUNET_CONFIGURATION_destroy (uc.ret);
579 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
581 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
582 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
583 GNUNET_free_non_null(control_host);
584 GNUNET_free(allowed_hosts);
588 /* arm needs to know to allow connections from the host on which it is running,
589 * otherwise gnunet-arm is unable to connect to it in some instances */
590 if (hostname != NULL)
592 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
593 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
594 GNUNET_free(allowed_hosts);
597 *port = (uint16_t) uc.nport;
604 * Add entries to the peers connect list
606 * @param pg the peer group we are working with
607 * @param first index of the first peer
608 * @param second index of the second peer
610 * @return the number of connections added (can be 0, 1 or 2)
611 * technically should only be 0 or 2, but the small price
612 * of iterating over the lists (hashmaps in the future)
613 * for being sure doesn't bother me!
617 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
623 GNUNET_HashCode hash_first;
624 GNUNET_HashCode hash_second;
626 hash_from_uid(first, &hash_first);
627 hash_from_uid(second, &hash_second);
629 add_first = GNUNET_NO;
630 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
632 add_first = GNUNET_YES;
635 add_second = GNUNET_NO;
636 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
638 add_second = GNUNET_YES;
644 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
645 pg->peers[first].num_connections++;
651 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
652 pg->peers[second].num_connections++;
661 * Add entries to the peers allowed connections list
663 * @param pg the peer group we are working with
664 * @param first index of the first peer
665 * @param second index of the second peer
667 * @return the number of connections added (can be 0, 1 or 2)
668 * technically should only be 0 or 2, but the small price
669 * of iterating over the lists (hashmaps in the future)
670 * for being sure doesn't bother me!
674 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
678 struct PeerConnection *first_iter;
679 struct PeerConnection *second_iter;
680 struct PeerConnection *new_first;
681 struct PeerConnection *new_second;
686 GNUNET_HashCode hash_first;
687 GNUNET_HashCode hash_second;
689 hash_from_uid(first, &hash_first);
690 hash_from_uid(second, &hash_second);
692 add_first = GNUNET_NO;
693 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
695 add_first = GNUNET_YES;
698 add_second = GNUNET_NO;
699 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
701 add_second = GNUNET_YES;
704 first_iter = pg->peers[first].connected_peers;
705 while (first_iter != NULL)
707 if (first_iter->daemon == pg->peers[second].daemon)
708 add_first = GNUNET_NO;
709 first_iter = first_iter->next;
712 second_iter = pg->peers[second].connected_peers;
713 add_second = GNUNET_YES;
714 while (second_iter != NULL)
716 if (second_iter->daemon == pg->peers[first].daemon)
717 add_second = GNUNET_NO;
718 second_iter = second_iter->next;
725 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
727 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
728 new_first->daemon = pg->peers[second].daemon;
729 new_first->next = pg->peers[first].connected_peers;
730 pg->peers[first].connected_peers = new_first;
732 pg->peers[first].num_connections++;
738 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
740 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
741 new_second->daemon = pg->peers[first].daemon;
742 new_second->next = pg->peers[second].connected_peers;
743 pg->peers[second].connected_peers = new_second;
744 pg->peers[first].num_connections++;
746 pg->peers[second].num_connections++;
754 * Add entries to the peers blacklisted list
756 * @param pg the peer group we are working with
757 * @param first index of the first peer
758 * @param second index of the second peer
760 * @return the number of connections added (can be 0, 1 or 2)
764 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
769 GNUNET_HashCode hash_first;
770 GNUNET_HashCode hash_second;
772 hash_from_uid(first, &hash_first);
773 hash_from_uid(second, &hash_second);
775 add_first = GNUNET_NO;
776 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
778 add_first = GNUNET_YES;
781 add_second = GNUNET_NO;
782 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
784 add_second = GNUNET_YES;
790 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
791 pg->peers[first].num_connections++;
797 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
798 pg->peers[second].num_connections++;
806 * Remove entries from the peers blacklisted list
808 * @param pg the peer group we are working with
809 * @param first index of the first peer
810 * @param second index of the second peer
812 * @return the number of connections removed (can be 0, 1 or 2)
816 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
821 GNUNET_HashCode hash_first;
822 GNUNET_HashCode hash_second;
824 hash_from_uid(first, &hash_first);
825 hash_from_uid(second, &hash_second);
827 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
828 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
833 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
839 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
847 * Scale free network construction as described in:
849 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
851 * Start with a network of "one" peer, then progressively add
852 * peers up to the total number. At each step, iterate over
853 * all possible peers and connect new peer based on number of
854 * existing connections of the target peer.
856 * @param pg the peer group we are dealing with
857 * @param proc the connection processor to use
859 * @return the number of connections created
862 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
865 unsigned int total_connections;
866 unsigned int outer_count;
868 unsigned int previous_total_connections;
872 GNUNET_assert(pg->total > 1);
874 /* Add a connection between the first two nodes */
875 total_connections = proc(pg, 0, 1);
877 for (outer_count = 1; outer_count < pg->total; outer_count++)
879 previous_total_connections = total_connections;
880 for (i = 0; i < outer_count; i++)
882 probability = pg->peers[i].num_connections / (double)previous_total_connections;
883 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
884 UINT64_MAX)) / ( (double) UINT64_MAX);
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
887 "Considering connecting peer %d to peer %d\n",
890 if (random < probability)
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
894 "Connecting peer %d to peer %d\n",
897 total_connections += proc(pg, outer_count, i);
902 return total_connections;
906 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
911 unsigned int randomPeer;
912 double random, logNModifier, percentage;
913 unsigned int smallWorldConnections;
919 int connect_attempts;
921 logNModifier = 0.5; /* FIXME: default value? */
922 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
927 if (sscanf(p_string, "%lf", &logNModifier) != 1)
928 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
929 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
933 GNUNET_free (p_string);
935 percentage = 0.5; /* FIXME: default percentage? */
936 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
941 if (sscanf(p_string, "%lf", &percentage) != 1)
942 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
943 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
947 GNUNET_free (p_string);
949 natLog = log (pg->total);
950 connsPerPeer = ceil (natLog * logNModifier);
952 if (connsPerPeer % 2 == 1)
955 smallWorldConnections = 0;
956 connect_attempts = 0;
957 for (i = 0; i < pg->total; i++)
960 max = i + connsPerPeer / 2;
961 min = i - connsPerPeer / 2;
963 if (max > pg->total - 1)
965 max = max - pg->total;
971 min = pg->total - 1 + min;
975 for (j = 0; j < connsPerPeer / 2; j++)
977 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
978 UINT64_MAX) / ( (double) UINT64_MAX));
979 if (random < percentage)
981 /* Connect to uniformly selected random peer */
983 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
985 while ((((randomPeer < max) && (randomPeer > min))
986 && (useAnd == 0)) || (((randomPeer > min)
987 || (randomPeer < max))
991 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
994 smallWorldConnections +=
995 proc (pg, i, randomPeer);
999 nodeToConnect = i + j + 1;
1000 if (nodeToConnect > pg->total - 1)
1002 nodeToConnect = nodeToConnect - pg->total;
1005 proc (pg, i, nodeToConnect);
1011 connect_attempts += smallWorldConnections;
1013 return connect_attempts;
1018 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1020 unsigned int outer_count, inner_count;
1021 unsigned int cutoff;
1022 int connect_attempts;
1023 double nat_percentage;
1026 nat_percentage = 0.6; /* FIXME: default percentage? */
1027 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1032 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1033 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1034 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1038 GNUNET_free (p_string);
1043 cutoff = (unsigned int) (nat_percentage * pg->total);
1045 connect_attempts = 0;
1047 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1049 for (inner_count = outer_count + 1; inner_count < pg->total;
1052 if ((outer_count > cutoff) || (inner_count > cutoff))
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1056 "Connecting peer %d to peer %d\n",
1057 outer_count, inner_count);
1059 connect_attempts += proc(pg, outer_count, inner_count);
1064 return connect_attempts;
1071 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1073 unsigned int i, j, k;
1074 unsigned int square;
1077 unsigned int toggle = 1;
1078 unsigned int nodeToConnect;
1079 unsigned int natLog;
1080 unsigned int node1Row;
1081 unsigned int node1Col;
1082 unsigned int node2Row;
1083 unsigned int node2Col;
1084 unsigned int distance;
1085 double probability, random, percentage;
1086 unsigned int smallWorldConnections;
1088 int connect_attempts;
1089 square = floor (sqrt (pg->total));
1093 percentage = 0.5; /* FIXME: default percentage? */
1094 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1099 if (sscanf(p_string, "%lf", &percentage) != 1)
1100 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1101 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1105 GNUNET_free (p_string);
1107 probability = 0.5; /* FIXME: default percentage? */
1108 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1113 if (sscanf(p_string, "%lf", &probability) != 1)
1114 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1115 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1119 GNUNET_free (p_string);
1121 if (square * square != pg->total)
1123 while (rows * cols < pg->total)
1125 if (toggle % 2 == 0)
1134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1135 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1139 connect_attempts = 0;
1140 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1141 * to the node to its right and above. Once this is over, we'll have our torus!
1142 * Special case for the last node (if the rows and columns are not equal), connect
1143 * to the first in the row to maintain topology.
1145 for (i = 0; i < pg->total; i++)
1147 /* First connect to the node to the right */
1148 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1149 nodeToConnect = i + 1;
1150 else if (i + 1 == pg->total)
1151 nodeToConnect = rows * cols - cols;
1153 nodeToConnect = i - cols + 1;
1155 connect_attempts += proc (pg, i, nodeToConnect);
1158 nodeToConnect = (rows * cols) - cols + i;
1160 nodeToConnect = i - cols;
1162 if (nodeToConnect < pg->total)
1163 connect_attempts += proc (pg, i, nodeToConnect);
1165 natLog = log (pg->total);
1166 #if VERBOSE_TESTING > 2
1167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1168 _("natural log of %d is %d, will run %d iterations\n"),
1169 pg->total, natLog, (int) (natLog * percentage));
1170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1172 smallWorldConnections = 0;
1173 for (i = 0; i < (int) (natLog * percentage); i++)
1175 for (j = 0; j < pg->total; j++)
1177 /* Determine the row and column of node at position j on the 2d torus */
1178 node1Row = j / cols;
1179 node1Col = j - (node1Row * cols);
1180 for (k = 0; k < pg->total; k++)
1182 /* Determine the row and column of node at position k on the 2d torus */
1183 node2Row = k / cols;
1184 node2Col = k - (node2Row * cols);
1185 /* Simple Cartesian distance */
1186 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1189 /* Calculate probability as 1 over the square of the distance */
1190 probability = 1.0 / (distance * distance);
1191 /* Choose a random value between 0 and 1 */
1192 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1193 UINT64_MAX)) / ( (double) UINT64_MAX);
1194 /* If random < probability, then connect the two nodes */
1195 if (random < probability)
1196 smallWorldConnections += proc (pg, j, k);
1202 connect_attempts += smallWorldConnections;
1203 #if VERBOSE_TESTING > 2
1204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1205 _("Total connections added for small world: %d!\n"),
1206 smallWorldConnections);
1208 return connect_attempts;
1214 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1217 unsigned int outer_count;
1218 unsigned int inner_count;
1219 int connect_attempts;
1223 probability = 0.5; /* FIXME: default percentage? */
1224 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1229 if (sscanf(p_string, "%lf", &probability) != 1)
1230 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1231 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1235 GNUNET_free (p_string);
1237 connect_attempts = 0;
1238 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1240 for (inner_count = outer_count + 1; inner_count < pg->total;
1243 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1244 UINT64_MAX)) / ( (double) UINT64_MAX);
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247 _("rand is %f probability is %f\n"), temp_rand,
1250 if (temp_rand < probability)
1252 connect_attempts += proc (pg, outer_count, inner_count);
1257 return connect_attempts;
1261 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1264 unsigned int square;
1267 unsigned int toggle = 1;
1268 unsigned int nodeToConnect;
1269 int connect_attempts;
1271 connect_attempts = 0;
1273 square = floor (sqrt (pg->total));
1277 if (square * square != pg->total)
1279 while (rows * cols < pg->total)
1281 if (toggle % 2 == 0)
1290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1291 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1294 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1295 * to the node to its right and above. Once this is over, we'll have our torus!
1296 * Special case for the last node (if the rows and columns are not equal), connect
1297 * to the first in the row to maintain topology.
1299 for (i = 0; i < pg->total; i++)
1301 /* First connect to the node to the right */
1302 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1303 nodeToConnect = i + 1;
1304 else if (i + 1 == pg->total)
1305 nodeToConnect = rows * cols - cols;
1307 nodeToConnect = i - cols + 1;
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1310 "Connecting peer %d to peer %d\n",
1313 connect_attempts += proc(pg, i, nodeToConnect);
1315 /* Second connect to the node immediately above */
1317 nodeToConnect = (rows * cols) - cols + i;
1319 nodeToConnect = i - cols;
1321 if (nodeToConnect < pg->total)
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1325 "Connecting peer %d to peer %d\n",
1328 connect_attempts += proc(pg, i, nodeToConnect);
1333 return connect_attempts;
1339 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1341 unsigned int outer_count;
1342 unsigned int inner_count;
1343 int connect_attempts;
1345 connect_attempts = 0;
1347 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1349 for (inner_count = outer_count + 1; inner_count < pg->total;
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1354 "Connecting peer %d to peer %d\n",
1355 outer_count, inner_count);
1357 connect_attempts += proc(pg, outer_count, inner_count);
1361 return connect_attempts;
1366 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1369 int connect_attempts;
1371 connect_attempts = 0;
1373 /* Connect each peer to the next highest numbered peer */
1374 for (count = 0; count < pg->total - 1; count++)
1377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1378 "Connecting peer %d to peer %d\n",
1381 connect_attempts += proc(pg, count, count + 1);
1384 /* Connect the last peer to the first peer */
1385 connect_attempts += proc(pg, pg->total - 1, 0);
1387 return connect_attempts;
1392 * Iterator for writing friends of a peer to a file.
1394 * @param cls closure, an open writable file handle
1395 * @param key the key the daemon was stored under
1396 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1398 * @return GNUNET_YES to continue iteration
1400 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1401 * with a single file_iterator that takes a closure which contains
1402 * the prefix to write before the peer. Then this could be used
1403 * for blacklisting multiple transports and writing the friend
1404 * file. I'm sure *someone* will complain loudly about other
1405 * things that negate these functions even existing so no point in
1409 friend_file_iterator (void *cls,
1410 const GNUNET_HashCode * key,
1413 FILE *temp_friend_handle = cls;
1414 struct GNUNET_TESTING_Daemon *peer = value;
1415 struct GNUNET_PeerIdentity *temppeer;
1416 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1418 temppeer = &peer->id;
1419 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1420 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1425 struct BlacklistContext
1428 * The (open) file handle to write to
1430 FILE *temp_file_handle;
1433 * The transport that this peer will be blacklisted on.
1439 * Iterator for writing blacklist data to appropriate files.
1441 * @param cls closure, an open writable file handle
1442 * @param key the key the daemon was stored under
1443 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1445 * @return GNUNET_YES to continue iteration
1448 blacklist_file_iterator (void *cls,
1449 const GNUNET_HashCode * key,
1452 struct BlacklistContext *blacklist_ctx = cls;
1453 //FILE *temp_blacklist_handle = cls;
1454 struct GNUNET_TESTING_Daemon *peer = value;
1455 struct GNUNET_PeerIdentity *temppeer;
1456 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1458 temppeer = &peer->id;
1459 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1460 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1466 * Create the friend files based on the PeerConnection's
1467 * of each peer in the peer group, and copy the files
1468 * to the appropriate place
1470 * @param pg the peer group we are dealing with
1473 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1475 FILE *temp_friend_handle;
1476 unsigned int pg_iter;
1477 char *temp_service_path;
1481 enum GNUNET_OS_ProcessStatusType type;
1482 unsigned long return_code;
1487 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1488 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1490 mytemp = GNUNET_DISK_mktemp("friends");
1491 GNUNET_assert(mytemp != NULL);
1492 temp_friend_handle = fopen (mytemp, "wt");
1493 GNUNET_assert(temp_friend_handle != NULL);
1494 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1495 fclose(temp_friend_handle);
1498 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1500 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1501 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1504 if (UNLINK (mytemp) != 0)
1505 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1506 GNUNET_free (mytemp);
1510 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1512 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1513 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1514 "mv", mytemp, arg, NULL);
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1517 _("Copying file with command cp %s %s\n"), mytemp, arg);
1522 else /* Remote, scp the file to the correct place */
1524 if (NULL != pg->peers[pg_iter].daemon->username)
1525 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1527 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1528 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1529 "scp", mytemp, arg, NULL);
1532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1533 _("Copying file with command scp %s %s\n"), mytemp, arg);
1537 GNUNET_free (temp_service_path);
1538 GNUNET_free (mytemp);
1542 ret = GNUNET_SYSERR;
1543 while ((count < max_wait) && (ret != GNUNET_OK))
1546 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1550 _("Checking copy status of file %d\n"), pg_iter);
1552 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1554 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1556 ret = GNUNET_SYSERR;
1558 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1560 ret = GNUNET_SYSERR;
1564 pidarr[pg_iter] = 0;
1566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1567 _("File %d copied\n"), pg_iter);
1573 if (ret == GNUNET_SYSERR)
1575 /* FIXME: why sleep here? -CG */
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1582 _("Finished copying all friend files!\n"));
1584 GNUNET_free(pidarr);
1590 * Create the blacklist files based on the PeerConnection's
1591 * of each peer in the peer group, and copy the files
1592 * to the appropriate place.
1594 * @param pg the peer group we are dealing with
1595 * @param transports space delimited list of transports to blacklist
1598 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
1600 FILE *temp_file_handle;
1601 static struct BlacklistContext blacklist_ctx;
1602 unsigned int pg_iter;
1603 char *temp_service_path;
1607 enum GNUNET_OS_ProcessStatusType type;
1608 unsigned long return_code;
1615 char *temp_transports;
1617 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1618 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1620 mytemp = GNUNET_DISK_mktemp("blacklist");
1621 GNUNET_assert(mytemp != NULL);
1622 temp_file_handle = fopen (mytemp, "wt");
1623 GNUNET_assert(temp_file_handle != NULL);
1624 temp_transports = GNUNET_strdup(transports);
1625 blacklist_ctx.temp_file_handle = temp_file_handle;
1626 transport_len = strlen(temp_transports) + 1;
1629 for (i = 0; i < transport_len; i++)
1631 if ((temp_transports[i] == ' ') && (pos == NULL))
1632 continue; /* At start of string (whitespace) */
1633 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
1635 temp_transports[i] = '\0';
1636 blacklist_ctx.transport = pos;
1637 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
1639 } /* At beginning of actual string */
1640 else if (pos == NULL)
1642 pos = &temp_transports[i];
1646 GNUNET_free (temp_transports);
1647 fclose(temp_file_handle);
1650 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1652 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1653 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1656 if (UNLINK (mytemp) != 0)
1657 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1658 GNUNET_free (mytemp);
1662 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1664 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
1665 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1666 "mv", mytemp, arg, NULL);
1668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1669 _("Copying file with command cp %s %s\n"), mytemp, arg);
1674 else /* Remote, scp the file to the correct place */
1676 if (NULL != pg->peers[pg_iter].daemon->username)
1677 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1679 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1680 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1681 "scp", mytemp, arg, NULL);
1684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1685 _("Copying file with command scp %s %s\n"), mytemp, arg);
1689 GNUNET_free (temp_service_path);
1690 GNUNET_free (mytemp);
1694 ret = GNUNET_SYSERR;
1695 while ((count < max_wait) && (ret != GNUNET_OK))
1698 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1702 _("Checking copy status of file %d\n"), pg_iter);
1704 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1706 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1708 ret = GNUNET_SYSERR;
1710 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1712 ret = GNUNET_SYSERR;
1716 pidarr[pg_iter] = 0;
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1719 _("File %d copied\n"), pg_iter);
1725 if (ret == GNUNET_SYSERR)
1727 /* FIXME: why sleep here? -CG */
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1734 _("Finished copying all blacklist files!\n"));
1736 GNUNET_free(pidarr);
1742 * Internal notification of a connection, kept so that we can ensure some connections
1743 * happen instead of flooding all testing daemons with requests to connect.
1745 static void internal_connect_notify (void *cls,
1746 const struct GNUNET_PeerIdentity *first,
1747 const struct GNUNET_PeerIdentity *second,
1748 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
1749 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
1750 struct GNUNET_TESTING_Daemon *first_daemon,
1751 struct GNUNET_TESTING_Daemon *second_daemon,
1754 struct GNUNET_TESTING_PeerGroup *pg = cls;
1755 outstanding_connects--;
1757 pg->notify_connection(pg->notify_connection_cls, first, second, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
1761 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1763 struct ConnectContext *connect_context = cls;
1765 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1768 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
1770 #if VERBOSE_TESTING > 2
1771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1772 _("Delaying connect, we have too many outstanding connections!\n"));
1774 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &schedule_connect, connect_context);
1778 #if VERBOSE_TESTING > 2
1779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1780 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
1782 outstanding_connects++;
1783 GNUNET_TESTING_daemons_connect (connect_context->first,
1784 connect_context->second,
1787 &internal_connect_notify,
1788 connect_context->pg);
1789 GNUNET_free(connect_context);
1794 * Iterator for actually scheduling connections to be created
1795 * between two peers.
1797 * @param cls closure, a GNUNET_TESTING_Daemon
1798 * @param key the key the second Daemon was stored under
1799 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1801 * @return GNUNET_YES to continue iteration
1804 connect_iterator (void *cls,
1805 const GNUNET_HashCode * key,
1808 struct PeerData *first = cls;
1809 struct GNUNET_TESTING_Daemon *second = value;
1810 struct ConnectContext *connect_context;
1812 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1813 connect_context->pg = first->pg;
1814 connect_context->first = first->daemon;
1815 connect_context->second = second;
1816 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
1822 * Iterator for copying all entries in the allowed hashmap to the
1825 * @param cls closure, a GNUNET_TESTING_Daemon
1826 * @param key the key the second Daemon was stored under
1827 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1829 * @return GNUNET_YES to continue iteration
1832 copy_topology_iterator (void *cls,
1833 const GNUNET_HashCode * key,
1836 struct PeerData *first = cls;
1838 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1844 * Make the peers to connect the same as those that are allowed to be
1847 * @param pg the peer group
1850 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
1852 unsigned int pg_iter;
1857 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1859 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
1860 if (GNUNET_SYSERR == ret)
1861 return GNUNET_SYSERR;
1863 total = total + ret;
1871 * Connect the topology as specified by the PeerConnection's
1872 * of each peer in the peer group
1874 * @param pg the peer group we are dealing with
1875 * @return the number of connections that will be attempted
1878 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
1880 unsigned int pg_iter;
1884 struct PeerConnection *connection_iter;
1885 struct ConnectContext *connect_context;
1889 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1891 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
1892 if (GNUNET_SYSERR == ret)
1893 return GNUNET_SYSERR;
1895 total = total + ret;
1899 while (connection_iter != NULL)
1901 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1902 connect_context->pg = pg;
1903 connect_context->first = ;
1904 connect_context->second = connection_iter->daemon;
1905 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
1906 connection_iter = connection_iter->next;
1915 * Takes a peer group and creates a topology based on the
1916 * one specified. Creates a topology means generates friend
1917 * files for the peers so they can only connect to those allowed
1918 * by the topology. This will only have an effect once peers
1919 * are started if the FRIENDS_ONLY option is set in the base
1920 * config. Also takes an optional restrict topology which
1921 * disallows direct TCP connections UNLESS they are specified in
1922 * the restricted topology.
1924 * @param pg the peer group struct representing the running peers
1925 * @param topology which topology to connect the peers in
1926 * @param restrict_topology allow only direct TCP connections in this topology
1927 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
1928 * @param restrict_transports space delimited list of transports to blacklist
1929 * to create restricted topology
1931 * @return the maximum number of connections were all allowed peers
1932 * connected to each other
1935 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
1936 enum GNUNET_TESTING_Topology topology,
1937 enum GNUNET_TESTING_Topology restrict_topology,
1938 char *restrict_transports)
1941 int num_connections;
1942 int unblacklisted_connections;
1944 GNUNET_assert (pg->notify_connection != NULL);
1949 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
1950 #if VERBOSE_TOPOLOGY
1951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1952 _("Creating clique topology\n"));
1954 num_connections = create_clique (pg, &add_allowed_connections);
1956 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
1957 #if VERBOSE_TOPOLOGY
1958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1959 _("Creating small world (ring) topology\n"));
1961 num_connections = create_small_world_ring (pg, &add_allowed_connections);
1963 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
1964 #if VERBOSE_TOPOLOGY
1965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1966 _("Creating small world (2d-torus) topology\n"));
1968 num_connections = create_small_world (pg, &add_allowed_connections);
1970 case GNUNET_TESTING_TOPOLOGY_RING:
1971 #if VERBOSE_TOPOLOGY
1972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1973 _("Creating ring topology\n"));
1975 num_connections = create_ring (pg, &add_allowed_connections);
1977 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
1978 #if VERBOSE_TOPOLOGY
1979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1980 _("Creating 2d torus topology\n"));
1982 num_connections = create_2d_torus (pg, &add_allowed_connections);
1984 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
1985 #if VERBOSE_TOPOLOGY
1986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1987 _("Creating Erdos-Renyi topology\n"));
1989 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
1991 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
1992 #if VERBOSE_TOPOLOGY
1993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1994 _("Creating InterNAT topology\n"));
1996 num_connections = create_nated_internet (pg, &add_allowed_connections);
1998 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2001 _("Creating Scale Free topology\n"));
2003 num_connections = create_scale_free (pg, &add_allowed_connections);
2005 case GNUNET_TESTING_TOPOLOGY_NONE:
2006 num_connections = 0;
2009 num_connections = 0;
2012 if (num_connections < 1)
2013 return GNUNET_SYSERR;
2015 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2017 ret = create_and_copy_friend_files(pg);
2020 if (ret != GNUNET_OK)
2023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2024 _("Failed during friend file copying!\n"));
2026 return GNUNET_SYSERR;
2031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2032 _("Friend files created/copied successfully!\n"));
2036 /* Use the create clique method to initially set all connections as blacklisted. */
2037 create_clique (pg, &blacklist_connections);
2038 unblacklisted_connections = 0;
2039 /* Un-blacklist connections as per the topology specified */
2040 switch (restrict_topology)
2042 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2045 _("Blacklisting all but clique topology\n"));
2047 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2049 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2052 _("Blacklisting all but small world (ring) topology\n"));
2054 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2056 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2059 _("Blacklisting all but small world (2d-torus) topology\n"));
2061 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2063 case GNUNET_TESTING_TOPOLOGY_RING:
2065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2066 _("Blacklisting all but ring topology\n"));
2068 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2070 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2073 _("Blacklisting all but 2d torus topology\n"));
2075 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2077 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2080 _("Blacklisting all but Erdos-Renyi topology\n"));
2082 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2084 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2087 _("Blacklisting all but InterNAT topology\n"));
2089 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2091 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2094 _("Blacklisting all but Scale Free topology\n"));
2096 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2098 case GNUNET_TESTING_TOPOLOGY_NONE:
2104 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2106 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2107 if (ret != GNUNET_OK)
2110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2111 _("Failed during blacklist file copying!\n"));
2113 return GNUNET_SYSERR;
2118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2119 _("Blacklist files created/copied successfully!\n"));
2123 return num_connections;
2126 struct RandomContext
2131 struct GNUNET_TESTING_PeerGroup *pg;
2134 * uid of the first peer
2139 * Peer data for first peer.
2141 struct PeerData *first;
2144 * Random percentage to use
2149 struct MinimumContext
2154 struct GNUNET_TESTING_PeerGroup *pg;
2157 * uid of the first peer
2162 * Peer data for first peer.
2164 struct PeerData *first;
2167 * Number of conns per peer
2169 unsigned int num_to_add;
2172 * Permuted array of all possible connections. Only add the Nth
2173 * peer if it's in the Nth position.
2175 unsigned int *pg_array;
2178 * What number is the current element we are iterating over?
2180 unsigned int current;
2188 struct GNUNET_TESTING_PeerGroup *pg;
2191 * uid of the first peer
2196 * uid of the second peer
2198 uint32_t second_uid;
2201 * Peer data for first peer.
2203 struct PeerData *first;
2206 * Which peer has been chosen as the one to add?
2208 unsigned int chosen;
2211 * What number is the current element we are iterating over?
2213 unsigned int current;
2217 * Iterator for choosing random peers to connect.
2219 * @param cls closure, a RandomContext
2220 * @param key the key the second Daemon was stored under
2221 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2223 * @return GNUNET_YES to continue iteration
2226 random_connect_iterator (void *cls,
2227 const GNUNET_HashCode * key,
2230 struct RandomContext *random_ctx = cls;
2231 double random_number;
2232 uint32_t second_pos;
2233 GNUNET_HashCode first_hash;
2234 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2235 UINT64_MAX)) / ( (double) UINT64_MAX);
2236 if (random_number < random_ctx->percentage)
2238 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2240 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2241 uid_from_hash(key, &second_pos);
2242 hash_from_uid(random_ctx->first_uid, &first_hash);
2243 GNUNET_assert(random_ctx->pg->total > second_pos);
2244 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2250 * Iterator for adding at least X peers to a peers connection set.
2252 * @param cls closure, MinimumContext
2253 * @param key the key the second Daemon was stored under
2254 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2256 * @return GNUNET_YES to continue iteration
2259 minimum_connect_iterator (void *cls,
2260 const GNUNET_HashCode * key,
2263 struct MinimumContext *min_ctx = cls;
2264 uint32_t second_pos;
2265 GNUNET_HashCode first_hash;
2268 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2270 for (i = 0; i < min_ctx->num_to_add; i++)
2272 if (min_ctx->pg_array[i] == min_ctx->current)
2274 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2275 uid_from_hash(key, &second_pos);
2276 hash_from_uid(min_ctx->first_uid, &first_hash);
2277 GNUNET_assert(min_ctx->pg->total > second_pos);
2278 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));
2279 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2280 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2287 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2293 * Iterator for adding peers to a connection set based on a depth first search.
2295 * @param cls closure, MinimumContext
2296 * @param key the key the second daemon was stored under
2297 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2299 * @return GNUNET_YES to continue iteration
2302 dfs_connect_iterator (void *cls,
2303 const GNUNET_HashCode * key,
2306 struct DFSContext *dfs_ctx = cls;
2307 GNUNET_HashCode first_hash;
2309 if (dfs_ctx->current == dfs_ctx->chosen)
2311 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2312 uid_from_hash(key, &dfs_ctx->second_uid);
2313 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2314 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));
2315 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2316 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2317 return GNUNET_NO; /* We have found our peer, don't iterate more */
2326 * From the set of connections possible, choose percentage percent of connections
2327 * to actually connect.
2329 * @param pg the peergroup we are dealing with
2330 * @param percentage what percent of total connections to make
2333 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2335 struct RandomContext random_ctx;
2338 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2340 random_ctx.first_uid = pg_iter;
2341 random_ctx.first = &pg->peers[pg_iter];
2342 random_ctx.percentage = percentage;
2344 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2345 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2346 /* Now remove the old connections */
2347 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2348 /* And replace with the random set */
2349 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2354 * From the set of connections possible, choose at least num connections per
2357 * @param pg the peergroup we are dealing with
2358 * @param num how many connections at least should each peer have (if possible)?
2361 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2363 struct MinimumContext minimum_ctx;
2366 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2368 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2371 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2373 minimum_ctx.first_uid = pg_iter;
2374 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2375 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2376 minimum_ctx.first = &pg->peers[pg_iter];
2377 minimum_ctx.pg = pg;
2378 minimum_ctx.num_to_add = num;
2379 minimum_ctx.current = 0;
2380 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2381 &minimum_connect_iterator,
2385 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2387 /* Remove the "old" connections */
2388 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2389 /* And replace with the working set */
2390 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2397 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2400 unsigned int pg_iter;
2404 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2406 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2413 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2416 unsigned int pg_iter;
2420 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2422 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2429 * From the set of connections possible, choose at least num connections per
2430 * peer based on depth first traversal of peer connections. If DFS leaves
2431 * peers unconnected, ensure those peers get connections.
2433 * @param pg the peergroup we are dealing with
2434 * @param num how many connections at least should each peer have (if possible)?
2437 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2439 struct DFSContext dfs_ctx;
2442 uint32_t starting_peer;
2443 uint32_t least_connections;
2444 GNUNET_HashCode second_hash;
2446 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2448 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2453 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
2455 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
2457 least_connections = -1; /* Set to very high number */
2458 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2460 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
2462 starting_peer = pg_iter;
2463 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2468 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
2474 /* Choose a random peer from the chosen peers set of connections to add */
2475 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
2476 dfs_ctx.first_uid = starting_peer;
2477 dfs_ctx.first = &pg->peers[starting_peer];
2479 dfs_ctx.current = 0;
2481 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
2482 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
2483 hash_from_uid(dfs_ctx.second_uid, &second_hash);
2484 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
2485 starting_peer = dfs_ctx.second_uid;
2488 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2490 /* Remove the "old" connections */
2491 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2492 /* And replace with the working set */
2493 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2498 * There are many ways to connect peers that are supported by this function.
2499 * To connect peers in the same topology that was created via the
2500 * GNUNET_TESTING_create_topology, the topology variable must be set to
2501 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
2502 * a new instance of that topology will be generated and attempted to be
2503 * connected. This could result in some connections being impossible,
2504 * because some topologies are non-deterministic.
2506 * @param pg the peer group struct representing the running peers
2507 * @param topology which topology to connect the peers in
2508 * @param options options for connecting the topology
2509 * @param option_modifier modifier for options that take a parameter
2510 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
2513 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2514 enum GNUNET_TESTING_Topology topology,
2515 enum GNUNET_TESTING_TopologyOption options,
2516 double option_modifier)
2520 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2521 #if VERBOSE_TOPOLOGY
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2523 _("Creating clique CONNECT topology\n"));
2525 create_clique (pg, &add_actual_connections);
2527 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2528 #if VERBOSE_TOPOLOGY
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2530 _("Creating small world (ring) CONNECT topology\n"));
2532 create_small_world_ring (pg, &add_actual_connections);
2534 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2535 #if VERBOSE_TOPOLOGY
2536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2537 _("Creating small world (2d-torus) CONNECT topology\n"));
2539 create_small_world (pg, &add_actual_connections);
2541 case GNUNET_TESTING_TOPOLOGY_RING:
2542 #if VERBOSE_TOPOLOGY
2543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2544 _("Creating ring CONNECT topology\n"));
2546 create_ring (pg, &add_actual_connections);
2548 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2549 #if VERBOSE_TOPOLOGY
2550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2551 _("Creating 2d torus CONNECT topology\n"));
2553 create_2d_torus (pg, &add_actual_connections);
2555 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2556 #if VERBOSE_TOPOLOGY
2557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2558 _("Creating Erdos-Renyi CONNECT topology\n"));
2560 create_erdos_renyi (pg, &add_actual_connections);
2562 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2563 #if VERBOSE_TOPOLOGY
2564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2565 _("Creating InterNAT CONNECT topology\n"));
2567 create_nated_internet (pg, &add_actual_connections);
2569 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2570 #if VERBOSE_TOPOLOGY
2571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2572 _("Creating Scale Free CONNECT topology\n"));
2574 create_scale_free (pg, &add_actual_connections);
2576 case GNUNET_TESTING_TOPOLOGY_NONE:
2577 #if VERBOSE_TOPOLOGY
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2579 _("Creating no CONNECT topology\n"));
2581 copy_allowed_topology(pg);
2584 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2585 _("Unknown topology specification, can't connect peers!\n"));
2586 return GNUNET_SYSERR;
2591 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
2592 #if VERBOSE_TOPOLOGY
2593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2594 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
2596 choose_random_connections(pg, option_modifier);
2598 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
2599 #if VERBOSE_TOPOLOGY
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2601 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
2603 choose_minimum(pg, (unsigned int)option_modifier);
2605 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
2606 #if VERBOSE_TOPOLOGY
2607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2608 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
2610 perform_dfs(pg, (int)option_modifier);
2612 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
2614 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
2620 return connect_topology(pg);
2624 * Function which continues a peer group starting up
2625 * after successfully generating hostkeys for each peer.
2627 * @param pg the peer group to continue starting
2631 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
2635 for (i = 0; i < pg->total; i++)
2637 GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
2642 * Start count gnunetd processes with the same set of transports and
2643 * applications. The port numbers (any option called "PORT") will be
2644 * adjusted to ensure that no two peers running on the same system
2645 * have the same port(s) in their respective configurations.
2647 * @param sched scheduler to use
2648 * @param cfg configuration template to use
2649 * @param total number of daemons to start
2650 * @param timeout total time allowed for peers to start
2651 * @param hostkey_callback function to call on each peers hostkey generation
2652 * if NULL, peers will be started by this call, if non-null,
2653 * GNUNET_TESTING_daemons_continue_startup must be called after
2654 * successful hostkey generation
2655 * @param hostkey_cls closure for hostkey callback
2656 * @param cb function to call on each daemon that was started
2657 * @param cb_cls closure for cb
2658 * @param connect_callback function to call each time two hosts are connected
2659 * @param connect_callback_cls closure for connect_callback
2660 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
2661 * everything on localhost).
2662 * @return NULL on error, otherwise handle to control peer group
2664 struct GNUNET_TESTING_PeerGroup *
2665 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
2666 const struct GNUNET_CONFIGURATION_Handle *cfg,
2668 struct GNUNET_TIME_Relative timeout,
2669 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
2671 GNUNET_TESTING_NotifyDaemonRunning cb,
2673 GNUNET_TESTING_NotifyConnection
2674 connect_callback, void *connect_callback_cls,
2675 const char *hostnames)
2677 struct GNUNET_TESTING_PeerGroup *pg;
2681 const char *hostname;
2682 char *baseservicehome;
2683 char *newservicehome;
2685 struct GNUNET_CONFIGURATION_Handle *pcfg;
2687 unsigned int hostcnt;
2697 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
2701 pg->cb_cls = cb_cls;
2702 pg->notify_connection = connect_callback;
2703 pg->notify_connection_cls = connect_callback_cls;
2705 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
2706 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
2707 if (NULL != hostnames)
2710 /* skip leading spaces */
2711 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
2714 while ('\0' != *rpos)
2716 if (isspace ( (unsigned char) *rpos))
2720 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
2722 start = GNUNET_strdup (hostnames);
2724 while ('\0' != *pos)
2726 if (isspace ( (unsigned char) *pos))
2729 if (strlen (start) > 0)
2731 pg->hosts[off].minport = LOW_PORT;
2732 pg->hosts[off++].hostname = start;
2738 if (strlen (start) > 0)
2740 pg->hosts[off].minport = LOW_PORT;
2741 pg->hosts[off++].hostname = start;
2745 GNUNET_free (start);
2746 GNUNET_free (pg->hosts);
2750 minport = 0; /* make gcc happy */
2757 for (off = 0; off < total; off++)
2761 hostname = pg->hosts[off % hostcnt].hostname;
2762 pcfg = make_config (cfg,
2763 &pg->hosts[off % hostcnt].minport,
2770 pcfg = make_config (cfg,
2778 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2780 ("Could not create configuration for peer number %u on `%s'!\n"),
2781 off, hostname == NULL ? "localhost" : hostname);
2786 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
2789 GNUNET_asprintf (&newservicehome,
2790 "%s/%d/", baseservicehome, off);
2791 GNUNET_free (baseservicehome);
2795 tmpdir = getenv ("TMPDIR");
2796 tmpdir = tmpdir ? tmpdir : "/tmp";
2797 GNUNET_asprintf (&newservicehome,
2800 "gnunet-testing-test-test", off);
2802 GNUNET_CONFIGURATION_set_value_string (pcfg,
2804 "SERVICEHOME", newservicehome);
2805 GNUNET_free (newservicehome);
2806 pg->peers[off].cfg = pcfg;
2807 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
2808 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
2809 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
2810 pg->peers[off].pg = pg;
2811 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (sched,
2818 if (NULL == pg->peers[off].daemon)
2819 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2820 _("Could not start peer number %u!\n"), off);
2827 * Get a daemon by number, so callers don't have to do nasty
2828 * offsetting operation.
2830 struct GNUNET_TESTING_Daemon *
2831 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
2833 if (position < pg->total)
2834 return pg->peers[position].daemon;
2840 * Prototype of a function that will be called when a
2841 * particular operation was completed the testing library.
2843 * @param cls closure (a struct RestartContext)
2844 * @param id id of the peer that was restarted
2845 * @param cfg handle to the configuration of the peer
2846 * @param d handle to the daemon that was restarted
2847 * @param emsg NULL on success
2849 void restart_callback (void *cls,
2850 const struct GNUNET_PeerIdentity *id,
2851 const struct GNUNET_CONFIGURATION_Handle *cfg,
2852 struct GNUNET_TESTING_Daemon *d,
2855 struct RestartContext *restart_context = cls;
2859 restart_context->peers_restarted++;
2863 restart_context->peers_restart_failed++;
2866 if (restart_context->peers_restarted == restart_context->peer_group->total)
2868 restart_context->callback(restart_context->callback_cls, NULL);
2869 GNUNET_free(restart_context);
2871 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
2873 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
2874 GNUNET_free(restart_context);
2880 * Callback for informing us about a successful
2881 * or unsuccessful churn stop call.
2883 * @param cls a ChurnContext
2884 * @param emsg NULL on success, non-NULL on failure
2888 churn_stop_callback (void *cls, const char *emsg)
2890 struct ChurnContext *churn_ctx = cls;
2891 unsigned int total_left;
2892 char *error_message;
2894 error_message = NULL;
2897 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2898 "Churn stop callback failed with error `%s'\n", emsg);
2899 churn_ctx->num_failed_stop++;
2903 churn_ctx->num_to_stop--;
2907 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2908 "Stopped peer, %d left.\n",
2909 churn_ctx->num_to_stop);
2911 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
2913 if (total_left == 0)
2915 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
2917 GNUNET_asprintf(&error_message,
2918 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
2919 churn_ctx->num_failed_start,
2920 churn_ctx->num_failed_stop);
2922 churn_ctx->cb(churn_ctx->cb_cls, error_message);
2923 GNUNET_free_non_null(error_message);
2924 GNUNET_free(churn_ctx);
2929 * Callback for informing us about a successful
2930 * or unsuccessful churn start call.
2932 * @param cls a ChurnContext
2933 * @param id the peer identity of the started peer
2934 * @param cfg the handle to the configuration of the peer
2935 * @param d handle to the daemon for the peer
2936 * @param emsg NULL on success, non-NULL on failure
2940 churn_start_callback (void *cls,
2941 const struct GNUNET_PeerIdentity *id,
2942 const struct GNUNET_CONFIGURATION_Handle *cfg,
2943 struct GNUNET_TESTING_Daemon *d,
2946 struct ChurnContext *churn_ctx = cls;
2947 unsigned int total_left;
2948 char *error_message;
2950 error_message = NULL;
2953 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2954 "Churn stop callback failed with error `%s'\n",
2956 churn_ctx->num_failed_start++;
2960 churn_ctx->num_to_start--;
2964 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2965 "Started peer, %d left.\n",
2966 churn_ctx->num_to_start);
2969 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
2971 if (total_left == 0)
2973 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
2974 GNUNET_asprintf(&error_message,
2975 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
2976 churn_ctx->num_failed_start,
2977 churn_ctx->num_failed_stop);
2978 churn_ctx->cb(churn_ctx->cb_cls, error_message);
2979 GNUNET_free_non_null(error_message);
2980 GNUNET_free(churn_ctx);
2986 * Simulate churn by stopping some peers (and possibly
2987 * re-starting others if churn is called multiple times). This
2988 * function can only be used to create leave-join churn (peers "never"
2989 * leave for good). First "voff" random peers that are currently
2990 * online will be taken offline; then "von" random peers that are then
2991 * offline will be put back online. No notifications will be
2992 * generated for any of these operations except for the callback upon
2995 * @param pg handle for the peer group
2996 * @param voff number of peers that should go offline
2997 * @param von number of peers that should come back online;
2998 * must be zero on first call (since "testbed_start"
2999 * always starts all of the peers)
3000 * @param timeout how long to wait for operations to finish before
3002 * @param cb function to call at the end
3003 * @param cb_cls closure for cb
3006 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
3009 struct GNUNET_TIME_Relative timeout,
3010 GNUNET_TESTING_NotifyCompletion cb,
3013 struct ChurnContext *churn_ctx;
3014 unsigned int running;
3015 unsigned int stopped;
3017 unsigned int *running_arr;
3018 unsigned int *stopped_arr;
3019 unsigned int *running_permute;
3020 unsigned int *stopped_permute;
3025 if ((von == 0) && (voff == 0)) /* No peers at all? */
3031 for (i = 0; i < pg->total; i++)
3033 if (pg->peers[i].daemon->running == GNUNET_YES)
3035 GNUNET_assert(running != -1);
3040 GNUNET_assert(stopped != -1);
3047 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
3048 cb(cb_cls, "Trying to stop more peers than are currently running!");
3054 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
3055 cb(cb_cls, "Trying to start more peers than are currently stopped!");
3059 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3060 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
3061 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
3063 running_permute = NULL;
3064 stopped_permute = NULL;
3067 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
3069 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
3074 churn_ctx->num_to_start = von;
3075 churn_ctx->num_to_stop = voff;
3077 churn_ctx->cb_cls = cb_cls;
3079 for (i = 0; i < pg->total; i++)
3081 if (pg->peers[i].daemon->running == GNUNET_YES)
3083 running_arr[running] = i;
3088 stopped_arr[stopped] = i;
3093 for (i = 0; i < voff; i++)
3096 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
3098 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
3100 &churn_stop_callback, churn_ctx,
3101 GNUNET_NO, GNUNET_YES);
3104 for (i = 0; i < von; i++)
3107 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
3109 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
3110 timeout, &churn_start_callback, churn_ctx);
3113 GNUNET_free(running_arr);
3114 GNUNET_free(stopped_arr);
3115 GNUNET_free_non_null(running_permute);
3116 GNUNET_free_non_null(stopped_permute);
3121 * Restart all peers in the given group.
3123 * @param pg the handle to the peer group
3124 * @param callback function to call on completion (or failure)
3125 * @param callback_cls closure for the callback function
3128 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyCompletion callback, void *callback_cls)
3130 struct RestartContext *restart_context;
3135 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
3136 restart_context->peer_group = pg;
3137 restart_context->peers_restarted = 0;
3138 restart_context->callback = callback;
3139 restart_context->callback_cls = callback_cls;
3141 for (off = 0; off < pg->total; off++)
3143 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
3149 * Start or stop an individual peer from the given group.
3151 * @param pg handle to the peer group
3152 * @param offset which peer to start or stop
3153 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
3154 * @param timeout how long to wait for shutdown
3155 * @param cb function to call at the end
3156 * @param cb_cls closure for cb
3159 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
3160 unsigned int offset,
3162 struct GNUNET_TIME_Relative timeout,
3163 GNUNET_TESTING_NotifyCompletion cb,
3166 struct ChurnContext *churn_ctx;
3168 if (GNUNET_NO == desired_status)
3170 if (NULL != pg->peers[offset].daemon)
3172 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3173 churn_ctx->num_to_start = 0;
3174 churn_ctx->num_to_stop = 1;
3176 churn_ctx->cb_cls = cb_cls;
3177 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
3178 timeout, &churn_stop_callback, churn_ctx,
3179 GNUNET_NO, GNUNET_YES);
3182 else if (GNUNET_YES == desired_status)
3184 if (NULL == pg->peers[offset].daemon)
3186 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3187 churn_ctx->num_to_start = 1;
3188 churn_ctx->num_to_stop = 0;
3190 churn_ctx->cb_cls = cb_cls;
3191 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
3192 timeout, &churn_start_callback, churn_ctx);
3201 * Shutdown all peers started in the given group.
3203 * @param pg handle to the peer group
3204 * @param timeout how long to wait for shutdown
3207 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
3208 struct GNUNET_TIME_Relative timeout)
3212 for (off = 0; off < pg->total; off++)
3214 /* FIXME: should we wait for our continuations to be called
3215 here? This would require us to take a continuation as
3218 if (NULL != pg->peers[off].daemon)
3219 GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, NULL, NULL, GNUNET_YES, GNUNET_NO);
3220 if (NULL != pg->peers[off].cfg)
3221 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
3222 if (pg->peers[off].allowed_peers != NULL)
3223 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
3224 if (pg->peers[off].connect_peers != NULL)
3225 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
3226 if (pg->peers[off].blacklisted_peers != NULL)
3227 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
3229 GNUNET_free (pg->peers);
3230 if (NULL != pg->hosts)
3232 GNUNET_free (pg->hosts[0].hostname);
3233 GNUNET_free (pg->hosts);
3239 /* end of testing_group.c */