2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing_group.c
23 * @brief convenience API for writing testcases for GNUnet
24 * @author Nathan Evans
25 * @author Christian Grothoff
29 #include "gnunet_arm_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_core_service.h"
33 #define VERBOSE_TESTING GNUNET_NO
35 #define DEBUG_CHURN GNUNET_NO
38 * Lowest port used for GNUnet testing. Should be high enough to not
39 * conflict with other applications running on the hosts but be low
40 * enough to not conflict with client-ports (typically starting around
43 #define LOW_PORT 10000
46 * Highest port used for GNUnet testing. Should be low enough to not
47 * conflict with the port range for "local" ports (client apps; see
48 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
50 #define HIGH_PORT 56000
52 #define MAX_OUTSTANDING_CONNECTIONS 10
54 #define MAX_CONCURRENT_HOSTKEYS 10
56 #define MAX_CONCURRENT_STARTING 10
58 #define MAX_CONCURRENT_SHUTDOWN 10
60 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
62 #define CONNECT_ATTEMPTS 8
65 * Prototype of a function called whenever two peers would be connected
66 * in a certain topology.
68 typedef int (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup *pg,
74 * Context for handling churning a peer group
79 * Callback used to notify of churning finished
81 GNUNET_TESTING_NotifyCompletion cb;
84 * Closure for callback
89 * Number of peers that still need to be started
91 unsigned int num_to_start;
94 * Number of peers that still need to be stopped
96 unsigned int num_to_stop;
99 * Number of peers that failed to start
101 unsigned int num_failed_start;
104 * Number of peers that failed to stop
106 unsigned int num_failed_stop;
109 struct RestartContext
112 * The group of peers being restarted
114 struct GNUNET_TESTING_PeerGroup *peer_group;
117 * How many peers have been restarted thus far
119 unsigned int peers_restarted;
122 * How many peers got an error when restarting
124 unsigned int peers_restart_failed;
127 * The function to call once all peers have been restarted
129 GNUNET_TESTING_NotifyCompletion callback;
132 * Closure for callback function
139 struct ShutdownContext
142 * Total peers to wait for
144 unsigned int total_peers;
147 * Number of peers successfully shut down
149 unsigned int peers_down;
152 * Number of peers failed to shut down
154 unsigned int peers_failed;
157 * Number of peers we have started shutting
158 * down. If too many, wait on them.
160 unsigned int outstanding;
163 * Timeout for shutdown.
165 struct GNUNET_TIME_Relative timeout;
168 * Callback to call when all peers either
169 * shutdown or failed to shutdown
171 GNUNET_TESTING_NotifyCompletion cb;
179 struct CreateTopologyContext
183 * Function to call with number of connections
185 GNUNET_TESTING_NotifyConnections cont;
188 * Closure for connection notification
194 struct PeerConnection
199 struct PeerConnection *next;
202 * Pointer to daemon handle
204 struct GNUNET_TESTING_Daemon *daemon;
209 struct InternalStartContext
212 * Pointer to peerdata
214 struct PeerData *peer;
217 * Timeout for peer startup
219 struct GNUNET_TIME_Relative timeout;
222 * Client callback for hostkey notification
224 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
227 * Closure for hostkey_callback
232 * Client callback for peer start notification
234 GNUNET_TESTING_NotifyDaemonRunning start_cb;
242 * Hostname, where to start the peer
244 const char *hostname;
248 * Data we keep per peer.
253 * (Initial) configuration of the host.
254 * (initial because clients could change
255 * it and we would not know about those
258 struct GNUNET_CONFIGURATION_Handle *cfg;
261 * Handle for controlling the daemon.
263 struct GNUNET_TESTING_Daemon *daemon;
266 * The peergroup this peer belongs to.
268 struct GNUNET_TESTING_PeerGroup *pg;
271 * Hash map of allowed peer connections (F2F created topology)
273 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
276 * Hash map of blacklisted peers
278 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
281 * Hash map of peer connections
283 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
286 * Temporary hash map of peer connections
288 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
291 * Temporary variable for topology creation, should be reset before
292 * creating any topology so the count is valid once finished.
297 * Context to keep track of peers being started, to
298 * stagger hostkey generation and peer startup.
300 struct InternalStartContext internal_context;
305 * Data we keep per host.
315 * Lowest port that we have not yet used
321 struct TopologyIterateContext
324 * Callback for notifying of two connected peers.
326 GNUNET_TESTING_NotifyTopology topology_cb;
329 * Closure for topology_cb
334 * Number of peers currently connected to.
336 unsigned int connected;
339 * Number of peers we have finished iterating.
341 unsigned int completed;
344 * Number of peers total.
349 struct StatsIterateContext
352 * Continuation to call once all stats information has been retrieved.
354 GNUNET_STATISTICS_Callback cont;
357 * Proc function to call on each value received.
359 GNUNET_TESTING_STATISTICS_Iterator proc;
362 * Closure for topology_cb
367 * Number of peers currently connected to.
369 unsigned int connected;
372 * Number of peers we have finished iterating.
374 unsigned int completed;
377 * Number of peers total.
385 struct GNUNET_TESTING_Daemon *daemon;
388 struct StatsCoreContext
391 struct GNUNET_TESTING_Daemon *daemon;
393 * Handle to the statistics service.
395 struct GNUNET_STATISTICS_Handle *stats_handle;
398 * Handle for getting statistics.
400 struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
404 * Handle to a group of GNUnet peers.
406 struct GNUNET_TESTING_PeerGroup
411 struct GNUNET_SCHEDULER_Handle *sched;
414 * Configuration template.
416 const struct GNUNET_CONFIGURATION_Handle *cfg;
419 * Function to call on each started daemon.
421 //GNUNET_TESTING_NotifyDaemonRunning cb;
429 * Function to call on each topology connection created
431 GNUNET_TESTING_NotifyConnection notify_connection;
434 * Callback for notify_connection
436 void *notify_connection_cls;
439 * NULL-terminated array of information about
442 struct HostData *hosts;
445 * Array of "total" peers.
447 struct PeerData *peers;
450 * Number of peers in this group.
455 * At what time should we fail the peer startup process?
457 struct GNUNET_TIME_Absolute max_timeout;
460 * How many peers are being started right now?
462 unsigned int starting;
465 * How many peers have already been started?
467 unsigned int started;
472 struct GNUNET_CONFIGURATION_Handle *ret;
473 const struct GNUNET_CONFIGURATION_Handle *orig;
474 const char *hostname;
480 struct ConnectContext
482 struct GNUNET_TESTING_Daemon *first;
484 struct GNUNET_TESTING_Daemon *second;
486 struct GNUNET_TESTING_PeerGroup *pg;
490 * Convert unique ID to hash code.
492 * @param uid unique ID to convert
493 * @param hash set to uid (extended with zeros)
496 hash_from_uid (uint32_t uid,
497 GNUNET_HashCode *hash)
499 memset (hash, 0, sizeof(GNUNET_HashCode));
500 *((uint32_t*)hash) = uid;
504 * Convert hash code to unique ID.
506 * @param uid unique ID to convert
507 * @param hash set to uid (extended with zeros)
510 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
512 memcpy (uid, hash, sizeof(uint32_t));
516 * Number of connects we are waiting on, allows us to rate limit
519 static int outstanding_connects;
522 * Get a topology from a string input.
524 * @param topology where to write the retrieved topology
525 * @param topology_string The string to attempt to
526 * get a configuration value from
527 * @return GNUNET_YES if topology string matched a
528 * known topology, GNUNET_NO if not
531 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string)
534 * Strings representing topologies in enum
536 static const char * topology_strings[] =
539 * A clique (everyone connected to everyone else).
544 * Small-world network (2d torus plus random links).
549 * Small-world network (ring plus random links).
569 * Certain percentage of peers are unable to communicate directly
570 * replicating NAT conditions
575 * Scale free topology.
580 * Straight line topology.
585 * All peers are disconnected.
593 if (topology_string == NULL)
595 while (topology_strings[curr] != NULL)
597 if (strcasecmp(topology_strings[curr], topology_string) == 0)
604 *topology = GNUNET_TESTING_TOPOLOGY_NONE;
610 * Get connect topology option from string input.
612 * @param topology_option where to write the retrieved topology
613 * @param topology_string The string to attempt to
614 * get a configuration value from
615 * @return GNUNET_YES if string matched a known
616 * topology option, GNUNET_NO if not
619 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption *topology_option,
620 char * topology_string)
623 * Options for connecting a topology as strings.
625 static const char * topology_option_strings[] =
628 * Try to connect all peers specified in the topology.
633 * Choose a random subset of connections to create.
635 "CONNECT_RANDOM_SUBSET",
638 * Create at least X connections for each peer.
643 * Using a depth first search, create one connection
644 * per peer. If any are missed (graph disconnected)
645 * start over at those peers until all have at least one
651 * No options specified.
659 if (topology_string == NULL)
661 while (NULL != topology_option_strings[curr])
663 if (strcasecmp(topology_option_strings[curr], topology_string) == 0)
665 *topology_option = curr;
670 *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
675 * Function to iterate over options. Copies
676 * the options to the target configuration,
677 * updating PORT values as needed.
680 * @param section name of the section
681 * @param option name of the option
682 * @param value value of the option
685 update_config (void *cls,
686 const char *section, const char *option, const char *value)
688 struct UpdateContext *ctx = cls;
692 char *single_variable;
694 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
696 GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
697 if ((ival != 0) && (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable)))
699 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
703 GNUNET_free(single_variable);
706 if (0 == strcmp (option, "UNIXPATH"))
708 GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
709 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable))
711 GNUNET_snprintf (uval,
713 "/tmp/test-service-%s-%u",
718 GNUNET_free(single_variable);
722 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
724 value = ctx->hostname;
727 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
732 * Create a new configuration using the given configuration
733 * as a template; however, each PORT in the existing cfg
734 * must be renumbered by incrementing "*port". If we run
735 * out of "*port" numbers, return NULL.
737 * @param cfg template configuration
738 * @param port port numbers to use, update to reflect
739 * port numbers that were used
740 * @param upnum number to make unix domain socket names unique
741 * @param hostname hostname of the controlling host, to allow control connections from
743 * @return new configuration, NULL on error
745 static struct GNUNET_CONFIGURATION_Handle *
746 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
749 const char *hostname)
751 struct UpdateContext uc;
759 uc.ret = GNUNET_CONFIGURATION_create ();
760 uc.hostname = hostname;
763 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
764 if (uc.nport >= HIGH_PORT)
767 GNUNET_CONFIGURATION_destroy (uc.ret);
771 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
773 if (hostname != NULL)
774 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1; %s;", control_host, hostname);
776 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
778 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
779 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport", "ACCEPT_FROM", allowed_hosts);
780 GNUNET_CONFIGURATION_set_value_string(uc.ret, "dht", "ACCEPT_FROM", allowed_hosts);
781 GNUNET_CONFIGURATION_set_value_string(uc.ret, "statistics", "ACCEPT_FROM", allowed_hosts);
782 GNUNET_free_non_null(control_host);
783 GNUNET_free(allowed_hosts);
787 /* arm needs to know to allow connections from the host on which it is running,
788 * otherwise gnunet-arm is unable to connect to it in some instances */
789 if (hostname != NULL)
791 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
792 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", hostname);
793 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", hostname);
794 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
795 GNUNET_free(allowed_hosts);
799 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", "127.0.0.1");
800 GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", "127.0.0.1");
803 *port = (uint16_t) uc.nport;
810 * Add entries to the peers connect list
812 * @param pg the peer group we are working with
813 * @param first index of the first peer
814 * @param second index of the second peer
816 * @return the number of connections added
817 * technically should only be 0 or 2
821 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
827 GNUNET_HashCode hash_first;
828 GNUNET_HashCode hash_second;
830 hash_from_uid(first, &hash_first);
831 hash_from_uid(second, &hash_second);
833 add_first = GNUNET_NO;
834 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
836 add_first = GNUNET_YES;
839 add_second = GNUNET_NO;
840 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
842 add_second = GNUNET_YES;
848 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
849 pg->peers[first].num_connections++;
855 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
856 pg->peers[second].num_connections++;
865 * Add entries to the peers allowed connections list
867 * @param pg the peer group we are working with
868 * @param first index of the first peer
869 * @param second index of the second peer
871 * @return the number of connections added (can be 0, 1 or 2)
872 * technically should only be 0 or 2, but the small price
873 * of iterating over the lists (hashmaps in the future)
874 * for being sure doesn't bother me!
878 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
882 struct PeerConnection *first_iter;
883 struct PeerConnection *second_iter;
884 struct PeerConnection *new_first;
885 struct PeerConnection *new_second;
890 GNUNET_HashCode hash_first;
891 GNUNET_HashCode hash_second;
893 hash_from_uid(first, &hash_first);
894 hash_from_uid(second, &hash_second);
896 add_first = GNUNET_NO;
897 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
899 add_first = GNUNET_YES;
902 add_second = GNUNET_NO;
903 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
905 add_second = GNUNET_YES;
908 first_iter = pg->peers[first].connected_peers;
909 while (first_iter != NULL)
911 if (first_iter->daemon == pg->peers[second].daemon)
912 add_first = GNUNET_NO;
913 first_iter = first_iter->next;
916 second_iter = pg->peers[second].connected_peers;
917 add_second = GNUNET_YES;
918 while (second_iter != NULL)
920 if (second_iter->daemon == pg->peers[first].daemon)
921 add_second = GNUNET_NO;
922 second_iter = second_iter->next;
929 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
931 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
932 new_first->daemon = pg->peers[second].daemon;
933 new_first->next = pg->peers[first].connected_peers;
934 pg->peers[first].connected_peers = new_first;
936 pg->peers[first].num_connections++;
942 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
944 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
945 new_second->daemon = pg->peers[first].daemon;
946 new_second->next = pg->peers[second].connected_peers;
947 pg->peers[second].connected_peers = new_second;
948 pg->peers[first].num_connections++;
950 pg->peers[second].num_connections++;
958 * Add entries to the peers blacklisted list
960 * @param pg the peer group we are working with
961 * @param first index of the first peer
962 * @param second index of the second peer
964 * @return the number of connections added (can be 0, 1 or 2)
968 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
973 GNUNET_HashCode hash_first;
974 GNUNET_HashCode hash_second;
976 hash_from_uid(first, &hash_first);
977 hash_from_uid(second, &hash_second);
979 add_first = GNUNET_NO;
980 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
982 add_first = GNUNET_YES;
985 add_second = GNUNET_NO;
986 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
988 add_second = GNUNET_YES;
994 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
995 pg->peers[first].num_connections++;
1001 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1002 pg->peers[second].num_connections++;
1010 * Remove entries from the peers blacklisted list
1012 * @param pg the peer group we are working with
1013 * @param first index of the first peer
1014 * @param second index of the second peer
1016 * @return the number of connections removed (can be 0, 1 or 2)
1020 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1025 GNUNET_HashCode hash_first;
1026 GNUNET_HashCode hash_second;
1028 hash_from_uid(first, &hash_first);
1029 hash_from_uid(second, &hash_second);
1031 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
1032 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
1037 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
1043 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
1051 * Scale free network construction as described in:
1053 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1055 * Start with a network of "one" peer, then progressively add
1056 * peers up to the total number. At each step, iterate over
1057 * all possible peers and connect new peer based on number of
1058 * existing connections of the target peer.
1060 * @param pg the peer group we are dealing with
1061 * @param proc the connection processor to use
1063 * @return the number of connections created
1066 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1069 unsigned int total_connections;
1070 unsigned int outer_count;
1072 unsigned int previous_total_connections;
1076 GNUNET_assert(pg->total > 1);
1078 /* Add a connection between the first two nodes */
1079 total_connections = proc(pg, 0, 1);
1081 for (outer_count = 1; outer_count < pg->total; outer_count++)
1083 previous_total_connections = total_connections;
1084 for (i = 0; i < outer_count; i++)
1086 probability = pg->peers[i].num_connections / (double)previous_total_connections;
1087 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1088 UINT64_MAX)) / ( (double) UINT64_MAX);
1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1091 "Considering connecting peer %d to peer %d\n",
1094 if (random < probability)
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1098 "Connecting peer %d to peer %d\n",
1101 total_connections += proc(pg, outer_count, i);
1106 return total_connections;
1110 * Create a topology given a peer group (set of running peers)
1111 * and a connection processor.
1113 * @param pg the peergroup to create the topology on
1114 * @param proc the connection processor to call to actually set
1115 * up connections between two peers
1117 * @return the number of connections that were set up
1121 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1125 unsigned int natLog;
1126 unsigned int randomPeer;
1127 double random, logNModifier, percentage;
1128 unsigned int smallWorldConnections;
1133 unsigned int useAnd;
1134 int connect_attempts;
1136 logNModifier = 0.5; /* FIXME: default value? */
1137 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1142 if (sscanf(p_string, "%lf", &logNModifier) != 1)
1143 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1144 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1148 GNUNET_free (p_string);
1150 percentage = 0.5; /* FIXME: default percentage? */
1151 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1156 if (sscanf(p_string, "%lf", &percentage) != 1)
1157 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1158 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1162 GNUNET_free (p_string);
1164 natLog = log (pg->total);
1165 connsPerPeer = ceil (natLog * logNModifier);
1167 if (connsPerPeer % 2 == 1)
1170 smallWorldConnections = 0;
1171 connect_attempts = 0;
1172 for (i = 0; i < pg->total; i++)
1175 max = i + connsPerPeer / 2;
1176 min = i - connsPerPeer / 2;
1178 if (max > pg->total - 1)
1180 max = max - pg->total;
1186 min = pg->total - 1 + min;
1190 for (j = 0; j < connsPerPeer / 2; j++)
1192 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1193 UINT64_MAX) / ( (double) UINT64_MAX));
1194 if (random < percentage)
1196 /* Connect to uniformly selected random peer */
1198 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1200 while ((((randomPeer < max) && (randomPeer > min))
1201 && (useAnd == 0)) || (((randomPeer > min)
1202 || (randomPeer < max))
1206 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1209 smallWorldConnections +=
1210 proc (pg, i, randomPeer);
1214 nodeToConnect = i + j + 1;
1215 if (nodeToConnect > pg->total - 1)
1217 nodeToConnect = nodeToConnect - pg->total;
1220 proc (pg, i, nodeToConnect);
1226 connect_attempts += smallWorldConnections;
1228 return connect_attempts;
1232 * Create a topology given a peer group (set of running peers)
1233 * and a connection processor.
1235 * @param pg the peergroup to create the topology on
1236 * @param proc the connection processor to call to actually set
1237 * up connections between two peers
1239 * @return the number of connections that were set up
1243 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1245 unsigned int outer_count, inner_count;
1246 unsigned int cutoff;
1247 int connect_attempts;
1248 double nat_percentage;
1251 nat_percentage = 0.6; /* FIXME: default percentage? */
1252 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1257 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1258 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1259 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1263 GNUNET_free (p_string);
1268 cutoff = (unsigned int) (nat_percentage * pg->total);
1270 connect_attempts = 0;
1272 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1274 for (inner_count = outer_count + 1; inner_count < pg->total;
1277 if ((outer_count > cutoff) || (inner_count > cutoff))
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1281 "Connecting peer %d to peer %d\n",
1282 outer_count, inner_count);
1284 connect_attempts += proc(pg, outer_count, inner_count);
1289 return connect_attempts;
1294 * Create a topology given a peer group (set of running peers)
1295 * and a connection processor.
1297 * @param pg the peergroup to create the topology on
1298 * @param proc the connection processor to call to actually set
1299 * up connections between two peers
1301 * @return the number of connections that were set up
1305 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1307 unsigned int i, j, k;
1308 unsigned int square;
1311 unsigned int toggle = 1;
1312 unsigned int nodeToConnect;
1313 unsigned int natLog;
1314 unsigned int node1Row;
1315 unsigned int node1Col;
1316 unsigned int node2Row;
1317 unsigned int node2Col;
1318 unsigned int distance;
1319 double probability, random, percentage;
1320 unsigned int smallWorldConnections;
1321 unsigned int small_world_it;
1323 int connect_attempts;
1324 square = floor (sqrt (pg->total));
1328 percentage = 0.5; /* FIXME: default percentage? */
1329 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1334 if (sscanf(p_string, "%lf", &percentage) != 1)
1335 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1336 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1340 GNUNET_free (p_string);
1342 if (percentage < 0.0)
1344 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1345 _("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1346 "PERCENTAGE", "TESTING", percentage);
1349 probability = 0.5; /* FIXME: default percentage? */
1350 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1355 if (sscanf(p_string, "%lf", &probability) != 1)
1356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1357 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1361 GNUNET_free (p_string);
1363 if (square * square != pg->total)
1365 while (rows * cols < pg->total)
1367 if (toggle % 2 == 0)
1376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1377 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1381 connect_attempts = 0;
1382 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1383 * to the node to its right and above. Once this is over, we'll have our torus!
1384 * Special case for the last node (if the rows and columns are not equal), connect
1385 * to the first in the row to maintain topology.
1387 for (i = 0; i < pg->total; i++)
1389 /* First connect to the node to the right */
1390 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1391 nodeToConnect = i + 1;
1392 else if (i + 1 == pg->total)
1393 nodeToConnect = rows * cols - cols;
1395 nodeToConnect = i - cols + 1;
1397 connect_attempts += proc (pg, i, nodeToConnect);
1400 nodeToConnect = (rows * cols) - cols + i;
1402 nodeToConnect = i - cols;
1404 if (nodeToConnect < pg->total)
1405 connect_attempts += proc (pg, i, nodeToConnect);
1407 natLog = log (pg->total);
1408 #if VERBOSE_TESTING > 2
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1410 _("natural log of %d is %d, will run %d iterations\n"),
1411 pg->total, natLog, (int) (natLog * percentage));
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1414 smallWorldConnections = 0;
1415 small_world_it = (unsigned int)(natLog * percentage);
1416 GNUNET_assert(small_world_it > 0 && small_world_it < (unsigned int)-1);
1417 for (i = 0; i < small_world_it; i++)
1419 for (j = 0; j < pg->total; j++)
1421 /* Determine the row and column of node at position j on the 2d torus */
1422 node1Row = j / cols;
1423 node1Col = j - (node1Row * cols);
1424 for (k = 0; k < pg->total; k++)
1426 /* Determine the row and column of node at position k on the 2d torus */
1427 node2Row = k / cols;
1428 node2Col = k - (node2Row * cols);
1429 /* Simple Cartesian distance */
1430 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1433 /* Calculate probability as 1 over the square of the distance */
1434 probability = 1.0 / (distance * distance);
1435 /* Choose a random value between 0 and 1 */
1436 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1437 UINT64_MAX)) / ( (double) UINT64_MAX);
1438 /* If random < probability, then connect the two nodes */
1439 if (random < probability)
1440 smallWorldConnections += proc (pg, j, k);
1446 connect_attempts += smallWorldConnections;
1447 #if VERBOSE_TESTING > 2
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1449 _("Total connections added for small world: %d!\n"),
1450 smallWorldConnections);
1452 return connect_attempts;
1456 * Create a topology given a peer group (set of running peers)
1457 * and a connection processor.
1459 * @param pg the peergroup to create the topology on
1460 * @param proc the connection processor to call to actually set
1461 * up connections between two peers
1463 * @return the number of connections that were set up
1467 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1470 unsigned int outer_count;
1471 unsigned int inner_count;
1472 int connect_attempts;
1476 probability = 0.5; /* FIXME: default percentage? */
1477 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1482 if (sscanf(p_string, "%lf", &probability) != 1)
1483 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1484 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1488 GNUNET_free (p_string);
1490 connect_attempts = 0;
1491 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1493 for (inner_count = outer_count + 1; inner_count < pg->total;
1496 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1497 UINT64_MAX)) / ( (double) UINT64_MAX);
1499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1500 _("rand is %f probability is %f\n"), temp_rand,
1503 if (temp_rand < probability)
1505 connect_attempts += proc (pg, outer_count, inner_count);
1510 return connect_attempts;
1514 * Create a topology given a peer group (set of running peers)
1515 * and a connection processor.
1517 * @param pg the peergroup to create the topology on
1518 * @param proc the connection processor to call to actually set
1519 * up connections between two peers
1521 * @return the number of connections that were set up
1525 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1528 unsigned int square;
1531 unsigned int toggle = 1;
1532 unsigned int nodeToConnect;
1533 int connect_attempts;
1535 connect_attempts = 0;
1537 square = floor (sqrt (pg->total));
1541 if (square * square != pg->total)
1543 while (rows * cols < pg->total)
1545 if (toggle % 2 == 0)
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1555 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1558 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1559 * to the node to its right and above. Once this is over, we'll have our torus!
1560 * Special case for the last node (if the rows and columns are not equal), connect
1561 * to the first in the row to maintain topology.
1563 for (i = 0; i < pg->total; i++)
1565 /* First connect to the node to the right */
1566 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1567 nodeToConnect = i + 1;
1568 else if (i + 1 == pg->total)
1569 nodeToConnect = rows * cols - cols;
1571 nodeToConnect = i - cols + 1;
1573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1574 "Connecting peer %d to peer %d\n",
1577 connect_attempts += proc(pg, i, nodeToConnect);
1579 /* Second connect to the node immediately above */
1581 nodeToConnect = (rows * cols) - cols + i;
1583 nodeToConnect = i - cols;
1585 if (nodeToConnect < pg->total)
1588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1589 "Connecting peer %d to peer %d\n",
1592 connect_attempts += proc(pg, i, nodeToConnect);
1597 return connect_attempts;
1602 * Create a topology given a peer group (set of running peers)
1603 * and a connection processor.
1605 * @param pg the peergroup to create the topology on
1606 * @param proc the connection processor to call to actually set
1607 * up connections between two peers
1609 * @return the number of connections that were set up
1613 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1615 unsigned int outer_count;
1616 unsigned int inner_count;
1617 int connect_attempts;
1619 connect_attempts = 0;
1621 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1623 for (inner_count = outer_count + 1; inner_count < pg->total;
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1628 "Connecting peer %d to peer %d\n",
1629 outer_count, inner_count);
1631 connect_attempts += proc(pg, outer_count, inner_count);
1635 return connect_attempts;
1639 * Create a topology given a peer group (set of running peers)
1640 * and a connection processor.
1642 * @param pg the peergroup to create the topology on
1643 * @param proc the connection processor to call to actually set
1644 * up connections between two peers
1646 * @return the number of connections that were set up
1650 create_line (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1653 int connect_attempts;
1655 connect_attempts = 0;
1657 /* Connect each peer to the next highest numbered peer */
1658 for (count = 0; count < pg->total - 1; count++)
1661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1662 "Connecting peer %d to peer %d\n",
1665 connect_attempts += proc(pg, count, count + 1);
1668 return connect_attempts;
1672 * Create a topology given a peer group (set of running peers)
1673 * and a connection processor.
1675 * @param pg the peergroup to create the topology on
1676 * @param proc the connection processor to call to actually set
1677 * up connections between two peers
1679 * @return the number of connections that were set up
1683 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1686 int connect_attempts;
1688 connect_attempts = 0;
1690 /* Connect each peer to the next highest numbered peer */
1691 for (count = 0; count < pg->total - 1; count++)
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "Connecting peer %d to peer %d\n",
1698 connect_attempts += proc(pg, count, count + 1);
1701 /* Connect the last peer to the first peer */
1702 connect_attempts += proc(pg, pg->total - 1, 0);
1704 return connect_attempts;
1709 * Iterator for writing friends of a peer to a file.
1711 * @param cls closure, an open writable file handle
1712 * @param key the key the daemon was stored under
1713 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1715 * @return GNUNET_YES to continue iteration
1717 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1718 * with a single file_iterator that takes a closure which contains
1719 * the prefix to write before the peer. Then this could be used
1720 * for blacklisting multiple transports and writing the friend
1721 * file. I'm sure *someone* will complain loudly about other
1722 * things that negate these functions even existing so no point in
1726 friend_file_iterator (void *cls,
1727 const GNUNET_HashCode * key,
1730 FILE *temp_friend_handle = cls;
1731 struct GNUNET_TESTING_Daemon *peer = value;
1732 struct GNUNET_PeerIdentity *temppeer;
1733 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1735 temppeer = &peer->id;
1736 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1737 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1742 struct BlacklistContext
1745 * The (open) file handle to write to
1747 FILE *temp_file_handle;
1750 * The transport that this peer will be blacklisted on.
1756 * Iterator for writing blacklist data to appropriate files.
1758 * @param cls closure, an open writable file handle
1759 * @param key the key the daemon was stored under
1760 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1762 * @return GNUNET_YES to continue iteration
1765 blacklist_file_iterator (void *cls,
1766 const GNUNET_HashCode * key,
1769 struct BlacklistContext *blacklist_ctx = cls;
1770 //FILE *temp_blacklist_handle = cls;
1771 struct GNUNET_TESTING_Daemon *peer = value;
1772 struct GNUNET_PeerIdentity *temppeer;
1773 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1775 temppeer = &peer->id;
1776 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1777 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1783 * Create the friend files based on the PeerConnection's
1784 * of each peer in the peer group, and copy the files
1785 * to the appropriate place
1787 * @param pg the peer group we are dealing with
1790 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1792 FILE *temp_friend_handle;
1793 unsigned int pg_iter;
1794 char *temp_service_path;
1798 enum GNUNET_OS_ProcessStatusType type;
1799 unsigned long return_code;
1804 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1805 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1807 mytemp = GNUNET_DISK_mktemp("friends");
1808 GNUNET_assert(mytemp != NULL);
1809 temp_friend_handle = fopen (mytemp, "wt");
1810 GNUNET_assert(temp_friend_handle != NULL);
1811 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1812 fclose(temp_friend_handle);
1815 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1817 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1818 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1821 if (UNLINK (mytemp) != 0)
1822 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1823 GNUNET_free (mytemp);
1827 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1829 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1830 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1831 "mv", mytemp, arg, NULL);
1833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1834 _("Copying file with command cp %s %s\n"), mytemp, arg);
1839 else /* Remote, scp the file to the correct place */
1841 if (NULL != pg->peers[pg_iter].daemon->username)
1842 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1844 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1845 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1846 "scp", mytemp, arg, NULL);
1849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1850 _("Copying file with command scp %s %s\n"), mytemp, arg);
1854 GNUNET_free (temp_service_path);
1855 GNUNET_free (mytemp);
1859 ret = GNUNET_SYSERR;
1860 while ((count < max_wait) && (ret != GNUNET_OK))
1863 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1867 _("Checking copy status of file %d\n"), pg_iter);
1869 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1871 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1873 ret = GNUNET_SYSERR;
1875 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1877 ret = GNUNET_SYSERR;
1881 pidarr[pg_iter] = 0;
1883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1884 _("File %d copied\n"), pg_iter);
1890 if (ret == GNUNET_SYSERR)
1892 /* FIXME: why sleep here? -CG */
1898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1899 _("Finished copying all friend files!\n"));
1901 GNUNET_free(pidarr);
1907 * Create the blacklist files based on the PeerConnection's
1908 * of each peer in the peer group, and copy the files
1909 * to the appropriate place.
1911 * @param pg the peer group we are dealing with
1912 * @param transports space delimited list of transports to blacklist
1915 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
1917 FILE *temp_file_handle;
1918 static struct BlacklistContext blacklist_ctx;
1919 unsigned int pg_iter;
1920 char *temp_service_path;
1924 enum GNUNET_OS_ProcessStatusType type;
1925 unsigned long return_code;
1932 char *temp_transports;
1934 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1935 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1937 mytemp = GNUNET_DISK_mktemp("blacklist");
1938 GNUNET_assert(mytemp != NULL);
1939 temp_file_handle = fopen (mytemp, "wt");
1940 GNUNET_assert(temp_file_handle != NULL);
1941 temp_transports = GNUNET_strdup(transports);
1942 blacklist_ctx.temp_file_handle = temp_file_handle;
1943 transport_len = strlen(temp_transports) + 1;
1946 for (i = 0; i < transport_len; i++)
1948 if ((temp_transports[i] == ' ') && (pos == NULL))
1949 continue; /* At start of string (whitespace) */
1950 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
1952 temp_transports[i] = '\0';
1953 blacklist_ctx.transport = pos;
1954 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
1956 } /* At beginning of actual string */
1957 else if (pos == NULL)
1959 pos = &temp_transports[i];
1963 GNUNET_free (temp_transports);
1964 fclose(temp_file_handle);
1967 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1969 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1970 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1973 if (UNLINK (mytemp) != 0)
1974 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1975 GNUNET_free (mytemp);
1979 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1981 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
1982 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1983 "mv", mytemp, arg, NULL);
1985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1986 _("Copying file with command cp %s %s\n"), mytemp, arg);
1991 else /* Remote, scp the file to the correct place */
1993 if (NULL != pg->peers[pg_iter].daemon->username)
1994 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1996 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1997 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1998 "scp", mytemp, arg, NULL);
2001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2002 _("Copying file with command scp %s %s\n"), mytemp, arg);
2006 GNUNET_free (temp_service_path);
2007 GNUNET_free (mytemp);
2011 ret = GNUNET_SYSERR;
2012 while ((count < max_wait) && (ret != GNUNET_OK))
2015 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2019 _("Checking copy status of file %d\n"), pg_iter);
2021 if (pidarr[pg_iter] != 0) /* Check for already completed! */
2023 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
2025 ret = GNUNET_SYSERR;
2027 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
2029 ret = GNUNET_SYSERR;
2033 pidarr[pg_iter] = 0;
2035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2036 _("File %d copied\n"), pg_iter);
2042 if (ret == GNUNET_SYSERR)
2044 /* FIXME: why sleep here? -CG */
2050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2051 _("Finished copying all blacklist files!\n"));
2053 GNUNET_free(pidarr);
2059 * Internal notification of a connection, kept so that we can ensure some connections
2060 * happen instead of flooding all testing daemons with requests to connect.
2062 static void internal_connect_notify (void *cls,
2063 const struct GNUNET_PeerIdentity *first,
2064 const struct GNUNET_PeerIdentity *second,
2066 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2067 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2068 struct GNUNET_TESTING_Daemon *first_daemon,
2069 struct GNUNET_TESTING_Daemon *second_daemon,
2072 struct GNUNET_TESTING_PeerGroup *pg = cls;
2073 outstanding_connects--;
2075 pg->notify_connection(pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
2080 * Either delay a connection (because there are too many outstanding)
2081 * or schedule it for right now.
2083 * @param cls a connection context
2084 * @param tc the task runtime context
2086 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2088 struct ConnectContext *connect_context = cls;
2090 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2093 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2095 #if VERBOSE_TESTING > 2
2096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2097 _("Delaying connect, we have too many outstanding connections!\n"));
2099 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_connect, connect_context);
2103 #if VERBOSE_TESTING > 2
2104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2105 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
2107 outstanding_connects++;
2108 GNUNET_TESTING_daemons_connect (connect_context->first,
2109 connect_context->second,
2112 &internal_connect_notify,
2113 connect_context->pg);
2114 GNUNET_free(connect_context);
2120 * Iterator for actually scheduling connections to be created
2121 * between two peers.
2123 * @param cls closure, a GNUNET_TESTING_Daemon
2124 * @param key the key the second Daemon was stored under
2125 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2127 * @return GNUNET_YES to continue iteration
2130 connect_iterator (void *cls,
2131 const GNUNET_HashCode * key,
2134 struct PeerData *first = cls;
2135 struct GNUNET_TESTING_Daemon *second = value;
2136 struct ConnectContext *connect_context;
2138 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2139 connect_context->pg = first->pg;
2140 connect_context->first = first->daemon;
2141 connect_context->second = second;
2142 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
2149 * Iterator for copying all entries in the allowed hashmap to the
2152 * @param cls closure, a GNUNET_TESTING_Daemon
2153 * @param key the key the second Daemon was stored under
2154 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2156 * @return GNUNET_YES to continue iteration
2159 copy_topology_iterator (void *cls,
2160 const GNUNET_HashCode * key,
2163 struct PeerData *first = cls;
2165 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2171 * Make the peers to connect the same as those that are allowed to be
2174 * @param pg the peer group
2177 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2179 unsigned int pg_iter;
2184 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2186 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
2187 if (GNUNET_SYSERR == ret)
2188 return GNUNET_SYSERR;
2190 total = total + ret;
2198 * Connect the topology as specified by the PeerConnection's
2199 * of each peer in the peer group
2201 * @param pg the peer group we are dealing with
2202 * @return the number of connections that will be attempted
2205 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
2207 unsigned int pg_iter;
2211 struct PeerConnection *connection_iter;
2212 struct ConnectContext *connect_context;
2216 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2218 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
2219 if (GNUNET_SYSERR == ret)
2220 return GNUNET_SYSERR;
2222 total = total + ret;
2226 while (connection_iter != NULL)
2228 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2229 connect_context->pg = pg;
2230 connect_context->first = ;
2231 connect_context->second = connection_iter->daemon;
2232 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
2233 connection_iter = connection_iter->next;
2242 * Takes a peer group and creates a topology based on the
2243 * one specified. Creates a topology means generates friend
2244 * files for the peers so they can only connect to those allowed
2245 * by the topology. This will only have an effect once peers
2246 * are started if the FRIENDS_ONLY option is set in the base
2247 * config. Also takes an optional restrict topology which
2248 * disallows connections based on a particular transport
2249 * UNLESS they are specified in the restricted topology.
2251 * @param pg the peer group struct representing the running peers
2252 * @param topology which topology to connect the peers in
2253 * @param restrict_topology allow only direct TCP connections in this topology
2254 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2255 * @param restrict_transports space delimited list of transports to blacklist
2256 * to create restricted topology
2258 * @return the maximum number of connections were all allowed peers
2259 * connected to each other
2262 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2263 enum GNUNET_TESTING_Topology topology,
2264 enum GNUNET_TESTING_Topology restrict_topology,
2265 char *restrict_transports)
2268 int num_connections;
2269 int unblacklisted_connections;
2271 GNUNET_assert (pg->notify_connection != NULL);
2275 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2278 _("Creating clique topology\n"));
2280 num_connections = create_clique (pg, &add_allowed_connections);
2282 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2285 _("Creating small world (ring) topology\n"));
2287 num_connections = create_small_world_ring (pg, &add_allowed_connections);
2289 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2292 _("Creating small world (2d-torus) topology\n"));
2294 num_connections = create_small_world (pg, &add_allowed_connections);
2296 case GNUNET_TESTING_TOPOLOGY_RING:
2298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2299 _("Creating ring topology\n"));
2301 num_connections = create_ring (pg, &add_allowed_connections);
2303 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2306 _("Creating 2d torus topology\n"));
2308 num_connections = create_2d_torus (pg, &add_allowed_connections);
2310 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2313 _("Creating Erdos-Renyi topology\n"));
2315 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2317 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2320 _("Creating InterNAT topology\n"));
2322 num_connections = create_nated_internet (pg, &add_allowed_connections);
2324 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2327 _("Creating Scale Free topology\n"));
2329 num_connections = create_scale_free (pg, &add_allowed_connections);
2331 case GNUNET_TESTING_TOPOLOGY_LINE:
2333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2334 _("Creating straight line topology\n"));
2336 num_connections = create_line (pg, &add_allowed_connections);
2338 case GNUNET_TESTING_TOPOLOGY_NONE:
2340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2341 _("Creating no allowed topology (all peers can connect at core level)\n"));
2343 num_connections = 0;
2346 num_connections = 0;
2350 if (num_connections < 0)
2351 return GNUNET_SYSERR;
2353 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2355 ret = create_and_copy_friend_files(pg);
2356 if (ret != GNUNET_OK)
2359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2360 _("Failed during friend file copying!\n"));
2362 return GNUNET_SYSERR;
2367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2368 _("Friend files created/copied successfully!\n"));
2373 /* Use the create clique method to initially set all connections as blacklisted. */
2374 if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2375 create_clique (pg, &blacklist_connections);
2376 unblacklisted_connections = 0;
2377 /* Un-blacklist connections as per the topology specified */
2378 switch (restrict_topology)
2380 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2383 _("Blacklisting all but clique topology\n"));
2385 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2387 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2390 _("Blacklisting all but small world (ring) topology\n"));
2392 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2394 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2397 _("Blacklisting all but small world (2d-torus) topology\n"));
2399 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2401 case GNUNET_TESTING_TOPOLOGY_RING:
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2404 _("Blacklisting all but ring topology\n"));
2406 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2408 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2411 _("Blacklisting all but 2d torus topology\n"));
2413 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2415 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2418 _("Blacklisting all but Erdos-Renyi topology\n"));
2420 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2422 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2425 _("Blacklisting all but InterNAT topology\n"));
2427 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2429 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2432 _("Blacklisting all but Scale Free topology\n"));
2434 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2436 case GNUNET_TESTING_TOPOLOGY_LINE:
2438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2439 _("Blacklisting all but straight line topology\n"));
2441 unblacklisted_connections = create_line (pg, &unblacklist_connections);
2443 case GNUNET_TESTING_TOPOLOGY_NONE:
2445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446 _("Creating no blacklist topology (all peers can connect at transport level)\n"));
2452 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2454 ret = create_and_copy_blacklist_files(pg, restrict_transports);
2455 if (ret != GNUNET_OK)
2458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2459 _("Failed during blacklist file copying!\n"));
2461 return GNUNET_SYSERR;
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2467 _("Blacklist files created/copied successfully!\n"));
2471 return num_connections;
2474 struct RandomContext
2479 struct GNUNET_TESTING_PeerGroup *pg;
2482 * uid of the first peer
2487 * Peer data for first peer.
2489 struct PeerData *first;
2492 * Random percentage to use
2497 struct MinimumContext
2502 struct GNUNET_TESTING_PeerGroup *pg;
2505 * uid of the first peer
2510 * Peer data for first peer.
2512 struct PeerData *first;
2515 * Number of conns per peer
2517 unsigned int num_to_add;
2520 * Permuted array of all possible connections. Only add the Nth
2521 * peer if it's in the Nth position.
2523 unsigned int *pg_array;
2526 * What number is the current element we are iterating over?
2528 unsigned int current;
2536 struct GNUNET_TESTING_PeerGroup *pg;
2539 * uid of the first peer
2544 * uid of the second peer
2546 uint32_t second_uid;
2549 * Peer data for first peer.
2551 struct PeerData *first;
2554 * Which peer has been chosen as the one to add?
2556 unsigned int chosen;
2559 * What number is the current element we are iterating over?
2561 unsigned int current;
2565 * Iterator for choosing random peers to connect.
2567 * @param cls closure, a RandomContext
2568 * @param key the key the second Daemon was stored under
2569 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2571 * @return GNUNET_YES to continue iteration
2574 random_connect_iterator (void *cls,
2575 const GNUNET_HashCode * key,
2578 struct RandomContext *random_ctx = cls;
2579 double random_number;
2580 uint32_t second_pos;
2581 GNUNET_HashCode first_hash;
2582 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2583 UINT64_MAX)) / ( (double) UINT64_MAX);
2584 if (random_number < random_ctx->percentage)
2586 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2588 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2589 uid_from_hash(key, &second_pos);
2590 hash_from_uid(random_ctx->first_uid, &first_hash);
2591 GNUNET_assert(random_ctx->pg->total > second_pos);
2592 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2598 * Iterator for adding at least X peers to a peers connection set.
2600 * @param cls closure, MinimumContext
2601 * @param key the key the second Daemon was stored under
2602 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2604 * @return GNUNET_YES to continue iteration
2607 minimum_connect_iterator (void *cls,
2608 const GNUNET_HashCode * key,
2611 struct MinimumContext *min_ctx = cls;
2612 uint32_t second_pos;
2613 GNUNET_HashCode first_hash;
2616 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2618 for (i = 0; i < min_ctx->num_to_add; i++)
2620 if (min_ctx->pg_array[i] == min_ctx->current)
2622 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2623 uid_from_hash(key, &second_pos);
2624 hash_from_uid(min_ctx->first_uid, &first_hash);
2625 GNUNET_assert(min_ctx->pg->total > second_pos);
2626 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));
2627 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2628 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2635 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2641 * Iterator for adding peers to a connection set based on a depth first search.
2643 * @param cls closure, MinimumContext
2644 * @param key the key the second daemon was stored under
2645 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2647 * @return GNUNET_YES to continue iteration
2650 dfs_connect_iterator (void *cls,
2651 const GNUNET_HashCode * key,
2654 struct DFSContext *dfs_ctx = cls;
2655 GNUNET_HashCode first_hash;
2657 if (dfs_ctx->current == dfs_ctx->chosen)
2659 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2660 uid_from_hash(key, &dfs_ctx->second_uid);
2661 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2662 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));
2663 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2664 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2665 return GNUNET_NO; /* We have found our peer, don't iterate more */
2674 * From the set of connections possible, choose percentage percent of connections
2675 * to actually connect.
2677 * @param pg the peergroup we are dealing with
2678 * @param percentage what percent of total connections to make
2681 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2683 struct RandomContext random_ctx;
2686 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2688 random_ctx.first_uid = pg_iter;
2689 random_ctx.first = &pg->peers[pg_iter];
2690 random_ctx.percentage = percentage;
2692 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2693 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2694 /* Now remove the old connections */
2695 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2696 /* And replace with the random set */
2697 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2702 * From the set of connections possible, choose at least num connections per
2705 * @param pg the peergroup we are dealing with
2706 * @param num how many connections at least should each peer have (if possible)?
2709 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2711 struct MinimumContext minimum_ctx;
2714 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2716 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2719 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2721 minimum_ctx.first_uid = pg_iter;
2722 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK,
2723 GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2724 minimum_ctx.first = &pg->peers[pg_iter];
2725 minimum_ctx.pg = pg;
2726 minimum_ctx.num_to_add = num;
2727 minimum_ctx.current = 0;
2728 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2729 &minimum_connect_iterator,
2733 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2735 /* Remove the "old" connections */
2736 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2737 /* And replace with the working set */
2738 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2745 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2748 unsigned int pg_iter;
2752 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2754 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2761 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2764 unsigned int pg_iter;
2768 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2770 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2777 * From the set of connections possible, choose at least num connections per
2778 * peer based on depth first traversal of peer connections. If DFS leaves
2779 * peers unconnected, ensure those peers get connections.
2781 * @param pg the peergroup we are dealing with
2782 * @param num how many connections at least should each peer have (if possible)?
2785 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2787 struct DFSContext dfs_ctx;
2790 uint32_t starting_peer;
2791 uint32_t least_connections;
2792 GNUNET_HashCode second_hash;
2794 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2796 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2801 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
2803 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
2805 least_connections = -1; /* Set to very high number */
2806 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2808 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
2810 starting_peer = pg_iter;
2811 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2816 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
2822 /* Choose a random peer from the chosen peers set of connections to add */
2823 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
2824 dfs_ctx.first_uid = starting_peer;
2825 dfs_ctx.first = &pg->peers[starting_peer];
2827 dfs_ctx.current = 0;
2829 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
2830 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
2831 hash_from_uid(dfs_ctx.second_uid, &second_hash);
2832 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
2833 starting_peer = dfs_ctx.second_uid;
2836 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2838 /* Remove the "old" connections */
2839 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2840 /* And replace with the working set */
2841 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2846 * Internal callback for topology information for a particular peer.
2849 internal_topology_callback(void *cls,
2850 const struct GNUNET_PeerIdentity *peer,
2851 struct GNUNET_TIME_Relative latency, uint32_t distance)
2853 struct CoreContext *core_ctx = cls;
2854 struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
2856 if (peer == NULL) /* Either finished, or something went wrong */
2858 iter_ctx->completed++;
2859 iter_ctx->connected--;
2860 /* One core context allocated per iteration, must free! */
2861 GNUNET_free(core_ctx);
2865 iter_ctx->topology_cb(iter_ctx->cls, &core_ctx->daemon->id, peer, latency, distance, NULL);
2868 if (iter_ctx->completed == iter_ctx->total)
2870 iter_ctx->topology_cb(iter_ctx->cls, NULL, NULL, GNUNET_TIME_relative_get_zero(), 0, NULL);
2871 /* Once all are done, free the iteration context */
2872 GNUNET_free(iter_ctx);
2878 * Check running topology iteration tasks, if below max start a new one, otherwise
2879 * schedule for some time in the future.
2882 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2884 struct CoreContext *core_context = cls;
2885 struct TopologyIterateContext *topology_context = (struct TopologyIterateContext *)core_context->iter_context;
2886 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2889 if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
2891 #if VERBOSE_TESTING > 2
2892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2893 _("Delaying connect, we have too many outstanding connections!\n"));
2895 GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_topology, core_context);
2899 #if VERBOSE_TESTING > 2
2900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2901 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
2903 topology_context->connected++;
2904 if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->sched, core_context->daemon->cfg, &internal_topology_callback, core_context))
2905 internal_topology_callback(core_context, NULL, GNUNET_TIME_relative_get_zero(), 0);
2911 * Iterate over all (running) peers in the peer group, retrieve
2912 * all connections that each currently has.
2915 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyTopology cb, void *cls)
2917 struct TopologyIterateContext *topology_context;
2918 struct CoreContext *core_ctx;
2920 unsigned int total_count;
2922 /* Allocate a single topology iteration context */
2923 topology_context = GNUNET_malloc(sizeof(struct TopologyIterateContext));
2924 topology_context->topology_cb = cb;
2925 topology_context->cls = cls;
2927 for (i = 0; i < pg->total; i++)
2929 if (pg->peers[i].daemon->running == GNUNET_YES)
2931 /* Allocate one core context per core we need to connect to */
2932 core_ctx = GNUNET_malloc(sizeof(struct CoreContext));
2933 core_ctx->daemon = pg->peers[i].daemon;
2934 /* Set back pointer to topology iteration context */
2935 core_ctx->iter_context = topology_context;
2936 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_topology, core_ctx);
2940 topology_context->total = total_count;
2945 * Callback function to process statistic values.
2946 * This handler is here only really to insert a peer
2947 * identity (or daemon) so the statistics can be uniquely
2948 * tied to a single running peer.
2950 * @param cls closure
2951 * @param subsystem name of subsystem that created the statistic
2952 * @param name the name of the datum
2953 * @param value the current value
2954 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
2955 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
2957 static int internal_stats_callback (void *cls,
2958 const char *subsystem,
2963 struct StatsCoreContext *core_context = cls;
2964 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
2966 return stats_context->proc(stats_context->cls, &core_context->daemon->id, subsystem, name, value, is_persistent);
2970 * Internal continuation call for statistics iteration.
2972 * @param cls closure, the CoreContext for this iteration
2973 * @param success whether or not the statistics iterations
2974 * was canceled or not (we don't care)
2976 static void internal_stats_cont (void *cls, int success)
2978 struct StatsCoreContext *core_context = cls;
2979 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
2981 stats_context->connected--;
2982 stats_context->completed++;
2984 if (stats_context->completed == stats_context->total)
2986 stats_context->cont(stats_context->cls, GNUNET_YES);
2987 GNUNET_free(stats_context);
2990 if (core_context->stats_handle != NULL)
2991 GNUNET_STATISTICS_destroy(core_context->stats_handle, GNUNET_NO);
2993 GNUNET_free(core_context);
2997 * Check running topology iteration tasks, if below max start a new one, otherwise
2998 * schedule for some time in the future.
3001 schedule_get_statistics(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3003 struct StatsCoreContext *core_context = cls;
3004 struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3006 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3009 if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3011 #if VERBOSE_TESTING > 2
3012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3013 _("Delaying connect, we have too many outstanding connections!\n"));
3015 GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_statistics, core_context);
3019 #if VERBOSE_TESTING > 2
3020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3021 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3024 stats_context->connected++;
3025 core_context->stats_handle = GNUNET_STATISTICS_create(core_context->daemon->sched, "testing", core_context->daemon->cfg);
3026 if (core_context->stats_handle == NULL)
3028 internal_stats_cont (core_context, GNUNET_NO);
3032 core_context->stats_get_handle = GNUNET_STATISTICS_get(core_context->stats_handle, NULL, NULL, GNUNET_TIME_relative_get_forever(), &internal_stats_cont, &internal_stats_callback, core_context);
3033 if (core_context->stats_get_handle == NULL)
3034 internal_stats_cont (core_context, GNUNET_NO);
3041 * Iterate over all (running) peers in the peer group, retrieve
3042 * all statistics from each.
3045 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
3046 GNUNET_STATISTICS_Callback cont,
3047 GNUNET_TESTING_STATISTICS_Iterator proc, void *cls)
3049 struct StatsIterateContext *stats_context;
3050 struct StatsCoreContext *core_ctx;
3052 unsigned int total_count;
3054 /* Allocate a single stats iteration context */
3055 stats_context = GNUNET_malloc(sizeof(struct StatsIterateContext));
3056 stats_context->cont = cont;
3057 stats_context->proc = proc;
3058 stats_context->cls = cls;
3060 for (i = 0; i < pg->total; i++)
3062 if (pg->peers[i].daemon->running == GNUNET_YES)
3064 /* Allocate one core context per core we need to connect to */
3065 core_ctx = GNUNET_malloc(sizeof(struct StatsCoreContext));
3066 core_ctx->daemon = pg->peers[i].daemon;
3067 /* Set back pointer to topology iteration context */
3068 core_ctx->iter_context = stats_context;
3069 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_statistics, core_ctx);
3073 stats_context->total = total_count;
3078 * There are many ways to connect peers that are supported by this function.
3079 * To connect peers in the same topology that was created via the
3080 * GNUNET_TESTING_create_topology, the topology variable must be set to
3081 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
3082 * a new instance of that topology will be generated and attempted to be
3083 * connected. This could result in some connections being impossible,
3084 * because some topologies are non-deterministic.
3086 * @param pg the peer group struct representing the running peers
3087 * @param topology which topology to connect the peers in
3088 * @param options options for connecting the topology
3089 * @param option_modifier modifier for options that take a parameter
3090 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
3093 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3094 enum GNUNET_TESTING_Topology topology,
3095 enum GNUNET_TESTING_TopologyOption options,
3096 double option_modifier)
3100 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3101 #if VERBOSE_TOPOLOGY
3102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3103 _("Creating clique CONNECT topology\n"));
3105 create_clique (pg, &add_actual_connections);
3107 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3108 #if VERBOSE_TOPOLOGY
3109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3110 _("Creating small world (ring) CONNECT topology\n"));
3112 create_small_world_ring (pg, &add_actual_connections);
3114 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3115 #if VERBOSE_TOPOLOGY
3116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3117 _("Creating small world (2d-torus) CONNECT topology\n"));
3119 create_small_world (pg, &add_actual_connections);
3121 case GNUNET_TESTING_TOPOLOGY_RING:
3122 #if VERBOSE_TOPOLOGY
3123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3124 _("Creating ring CONNECT topology\n"));
3126 create_ring (pg, &add_actual_connections);
3128 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3129 #if VERBOSE_TOPOLOGY
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3131 _("Creating 2d torus CONNECT topology\n"));
3133 create_2d_torus (pg, &add_actual_connections);
3135 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3136 #if VERBOSE_TOPOLOGY
3137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3138 _("Creating Erdos-Renyi CONNECT topology\n"));
3140 create_erdos_renyi (pg, &add_actual_connections);
3142 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3143 #if VERBOSE_TOPOLOGY
3144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3145 _("Creating InterNAT CONNECT topology\n"));
3147 create_nated_internet (pg, &add_actual_connections);
3149 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3150 #if VERBOSE_TOPOLOGY
3151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3152 _("Creating Scale Free CONNECT topology\n"));
3154 create_scale_free (pg, &add_actual_connections);
3156 case GNUNET_TESTING_TOPOLOGY_LINE:
3157 #if VERBOSE_TOPOLOGY
3158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3159 _("Creating straight line CONNECT topology\n"));
3161 create_line (pg, &add_actual_connections);
3163 case GNUNET_TESTING_TOPOLOGY_NONE:
3164 #if VERBOSE_TOPOLOGY
3165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3166 _("Creating no CONNECT topology\n"));
3168 copy_allowed_topology(pg);
3171 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3172 _("Unknown topology specification, can't connect peers!\n"));
3173 return GNUNET_SYSERR;
3178 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
3179 #if VERBOSE_TOPOLOGY
3180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3181 _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
3183 choose_random_connections(pg, option_modifier);
3185 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
3186 #if VERBOSE_TOPOLOGY
3187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3188 _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3190 choose_minimum(pg, (unsigned int)option_modifier);
3192 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
3193 #if VERBOSE_TOPOLOGY
3194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3195 _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3197 perform_dfs(pg, (int)option_modifier);
3199 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
3201 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
3207 return connect_topology(pg);
3211 * Callback that is called whenever a hostkey is generated
3212 * for a peer. Call the real callback and decrement the
3213 * starting counter for the peergroup.
3215 * @param cls closure
3216 * @param id identifier for the daemon, NULL on error
3217 * @param d handle for the daemon
3218 * @param emsg error message (NULL on success)
3220 static void internal_hostkey_callback (void *cls,
3221 const struct GNUNET_PeerIdentity *id,
3222 struct GNUNET_TESTING_Daemon *d,
3225 struct InternalStartContext *internal_context = cls;
3226 internal_context->peer->pg->starting--;
3227 internal_context->peer->pg->started++;
3228 if (internal_context->hostkey_callback != NULL)
3229 internal_context->hostkey_callback(internal_context->hostkey_cls, id, d, emsg);
3230 else if (internal_context->peer->pg->started == internal_context->peer->pg->total)
3232 internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
3233 GNUNET_TESTING_daemons_continue_startup(internal_context->peer->pg);
3238 * Callback that is called whenever a peer has finished starting.
3239 * Call the real callback and decrement the starting counter
3240 * for the peergroup.
3242 * @param cls closure
3243 * @param id identifier for the daemon, NULL on error
3244 * @param d handle for the daemon
3245 * @param emsg error message (NULL on success)
3247 static void internal_startup_callback (void *cls,
3248 const struct GNUNET_PeerIdentity *id,
3249 const struct GNUNET_CONFIGURATION_Handle *cfg,
3250 struct GNUNET_TESTING_Daemon *d,
3253 struct InternalStartContext *internal_context = cls;
3254 internal_context->peer->pg->starting--;
3255 if (internal_context->start_cb != NULL)
3256 internal_context->start_cb(internal_context->start_cb_cls, id, cfg, d, emsg);
3260 internal_continue_startup (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3262 struct InternalStartContext *internal_context = cls;
3264 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3269 if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
3271 internal_context->peer->pg->starting++;
3272 GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
3276 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_continue_startup, internal_context);
3281 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3283 struct InternalStartContext *internal_context = cls;
3285 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3290 if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
3292 internal_context->peer->pg->starting++;
3293 internal_context->peer->daemon = GNUNET_TESTING_daemon_start (internal_context->peer->pg->sched,
3294 internal_context->peer->cfg,
3295 internal_context->timeout,
3296 internal_context->hostname,
3297 &internal_hostkey_callback,
3299 &internal_startup_callback,
3304 GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_start, internal_context);
3309 * Function which continues a peer group starting up
3310 * after successfully generating hostkeys for each peer.
3312 * @param pg the peer group to continue starting
3316 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
3321 for (i = 0; i < pg->total; i++)
3323 GNUNET_SCHEDULER_add_now (pg->sched, &internal_continue_startup, &pg->peers[i].internal_context);
3324 //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
3329 * Start count gnunet instances with the same set of transports and
3330 * applications. The port numbers (any option called "PORT") will be
3331 * adjusted to ensure that no two peers running on the same system
3332 * have the same port(s) in their respective configurations.
3334 * @param sched scheduler to use
3335 * @param cfg configuration template to use
3336 * @param total number of daemons to start
3337 * @param timeout total time allowed for peers to start
3338 * @param hostkey_callback function to call on each peers hostkey generation
3339 * if NULL, peers will be started by this call, if non-null,
3340 * GNUNET_TESTING_daemons_continue_startup must be called after
3341 * successful hostkey generation
3342 * @param hostkey_cls closure for hostkey callback
3343 * @param cb function to call on each daemon that was started
3344 * @param cb_cls closure for cb
3345 * @param connect_callback function to call each time two hosts are connected
3346 * @param connect_callback_cls closure for connect_callback
3347 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
3349 * @return NULL on error, otherwise handle to control peer group
3351 struct GNUNET_TESTING_PeerGroup *
3352 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
3353 const struct GNUNET_CONFIGURATION_Handle *cfg,
3355 struct GNUNET_TIME_Relative timeout,
3356 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
3358 GNUNET_TESTING_NotifyDaemonRunning cb,
3360 GNUNET_TESTING_NotifyConnection
3361 connect_callback, void *connect_callback_cls,
3362 const struct GNUNET_TESTING_Host *hostnames)
3364 struct GNUNET_TESTING_PeerGroup *pg;
3365 const struct GNUNET_TESTING_Host *hostpos;
3371 const char *hostname;
3372 char *baseservicehome;
3373 char *newservicehome;
3375 struct GNUNET_CONFIGURATION_Handle *pcfg;
3377 unsigned int hostcnt;
3387 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
3390 pg->notify_connection = connect_callback;
3391 pg->notify_connection_cls = connect_callback_cls;
3393 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
3394 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
3395 if (NULL != hostnames)
3398 hostpos = hostnames;
3399 while (hostpos != NULL)
3401 hostpos = hostpos->next;
3404 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3407 hostpos = hostnames;
3408 while (hostpos != NULL)
3410 pg->hosts[off].minport = LOW_PORT;
3411 pg->hosts[off++].hostname = GNUNET_strdup(hostpos->hostname);
3412 hostpos = hostpos->next;
3417 GNUNET_free (pg->hosts);
3425 /* skip leading spaces */
3426 while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
3429 while ('\0' != *rpos)
3431 if (isspace ( (unsigned char) *rpos))
3435 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3437 start = GNUNET_strdup (hostnames);
3439 while ('\0' != *pos)
3441 if (isspace ( (unsigned char) *pos))
3444 if (strlen (start) > 0)
3446 pg->hosts[off].minport = LOW_PORT;
3447 pg->hosts[off++].hostname = start;
3453 if (strlen (start) > 0)
3455 pg->hosts[off].minport = LOW_PORT;
3456 pg->hosts[off++].hostname = start;
3460 GNUNET_free (start);
3461 GNUNET_free (pg->hosts);
3465 minport = 0; /* make gcc happy */
3473 for (off = 0; off < total; off++)
3477 hostname = pg->hosts[off % hostcnt].hostname;
3478 pcfg = make_config (cfg,
3479 &pg->hosts[off % hostcnt].minport,
3486 pcfg = make_config (cfg,
3494 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3496 ("Could not create configuration for peer number %u on `%s'!\n"),
3497 off, hostname == NULL ? "localhost" : hostname);
3502 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
3505 GNUNET_asprintf (&newservicehome,
3506 "%s/%d/", baseservicehome, off);
3507 GNUNET_free (baseservicehome);
3511 tmpdir = getenv ("TMPDIR");
3512 tmpdir = tmpdir ? tmpdir : "/tmp";
3513 GNUNET_asprintf (&newservicehome,
3516 "gnunet-testing-test-test", off);
3518 GNUNET_CONFIGURATION_set_value_string (pcfg,
3520 "SERVICEHOME", newservicehome);
3521 GNUNET_free (newservicehome);
3522 pg->peers[off].cfg = pcfg;
3523 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
3524 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
3525 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
3526 pg->peers[off].pg = pg;
3528 pg->peers[off].internal_context.peer = &pg->peers[off];
3529 pg->peers[off].internal_context.timeout = timeout;
3530 pg->peers[off].internal_context.hostname = hostname;
3531 pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
3532 pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
3533 pg->peers[off].internal_context.start_cb = cb;
3534 pg->peers[off].internal_context.start_cb_cls = cb_cls;
3536 GNUNET_SCHEDULER_add_now (sched, &internal_start, &pg->peers[off].internal_context);
3543 * Get a daemon by number, so callers don't have to do nasty
3544 * offsetting operation.
3546 struct GNUNET_TESTING_Daemon *
3547 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
3549 if (position < pg->total)
3550 return pg->peers[position].daemon;
3556 * Get a daemon by peer identity, so callers can
3557 * retrieve the daemon without knowing it's offset.
3559 * @param pg the peer group to retrieve the daemon from
3560 * @param peer_id the peer identity of the daemon to retrieve
3562 * @return the daemon on success, or NULL if no such peer identity is found
3564 struct GNUNET_TESTING_Daemon *
3565 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_PeerIdentity *peer_id)
3569 for (i = 0; i < pg->total; i ++)
3571 if (0 == memcmp(&pg->peers[i].daemon->id, peer_id, sizeof(struct GNUNET_PeerIdentity)))
3572 return pg->peers[i].daemon;
3579 * Prototype of a function that will be called when a
3580 * particular operation was completed the testing library.
3582 * @param cls closure (a struct RestartContext)
3583 * @param id id of the peer that was restarted
3584 * @param cfg handle to the configuration of the peer
3585 * @param d handle to the daemon that was restarted
3586 * @param emsg NULL on success
3588 void restart_callback (void *cls,
3589 const struct GNUNET_PeerIdentity *id,
3590 const struct GNUNET_CONFIGURATION_Handle *cfg,
3591 struct GNUNET_TESTING_Daemon *d,
3594 struct RestartContext *restart_context = cls;
3598 restart_context->peers_restarted++;
3602 restart_context->peers_restart_failed++;
3605 if (restart_context->peers_restarted == restart_context->peer_group->total)
3607 restart_context->callback(restart_context->callback_cls, NULL);
3608 GNUNET_free(restart_context);
3610 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
3612 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
3613 GNUNET_free(restart_context);
3619 * Callback for informing us about a successful
3620 * or unsuccessful churn stop call.
3622 * @param cls a ChurnContext
3623 * @param emsg NULL on success, non-NULL on failure
3627 churn_stop_callback (void *cls, const char *emsg)
3629 struct ChurnContext *churn_ctx = cls;
3630 unsigned int total_left;
3631 char *error_message;
3633 error_message = NULL;
3636 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3637 "Churn stop callback failed with error `%s'\n", emsg);
3638 churn_ctx->num_failed_stop++;
3642 churn_ctx->num_to_stop--;
3646 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3647 "Stopped peer, %d left.\n",
3648 churn_ctx->num_to_stop);
3650 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3652 if (total_left == 0)
3654 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3656 GNUNET_asprintf(&error_message,
3657 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3658 churn_ctx->num_failed_start,
3659 churn_ctx->num_failed_stop);
3661 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3662 GNUNET_free_non_null(error_message);
3663 GNUNET_free(churn_ctx);
3668 * Callback for informing us about a successful
3669 * or unsuccessful churn start call.
3671 * @param cls a ChurnContext
3672 * @param id the peer identity of the started peer
3673 * @param cfg the handle to the configuration of the peer
3674 * @param d handle to the daemon for the peer
3675 * @param emsg NULL on success, non-NULL on failure
3679 churn_start_callback (void *cls,
3680 const struct GNUNET_PeerIdentity *id,
3681 const struct GNUNET_CONFIGURATION_Handle *cfg,
3682 struct GNUNET_TESTING_Daemon *d,
3685 struct ChurnContext *churn_ctx = cls;
3686 unsigned int total_left;
3687 char *error_message;
3689 error_message = NULL;
3692 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3693 "Churn stop callback failed with error `%s'\n",
3695 churn_ctx->num_failed_start++;
3699 churn_ctx->num_to_start--;
3703 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3704 "Started peer, %d left.\n",
3705 churn_ctx->num_to_start);
3708 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3710 if (total_left == 0)
3712 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3713 GNUNET_asprintf(&error_message,
3714 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3715 churn_ctx->num_failed_start,
3716 churn_ctx->num_failed_stop);
3717 churn_ctx->cb(churn_ctx->cb_cls, error_message);
3718 GNUNET_free_non_null(error_message);
3719 GNUNET_free(churn_ctx);
3725 * Simulate churn by stopping some peers (and possibly
3726 * re-starting others if churn is called multiple times). This
3727 * function can only be used to create leave-join churn (peers "never"
3728 * leave for good). First "voff" random peers that are currently
3729 * online will be taken offline; then "von" random peers that are then
3730 * offline will be put back online. No notifications will be
3731 * generated for any of these operations except for the callback upon
3734 * @param pg handle for the peer group
3735 * @param voff number of peers that should go offline
3736 * @param von number of peers that should come back online;
3737 * must be zero on first call (since "testbed_start"
3738 * always starts all of the peers)
3739 * @param timeout how long to wait for operations to finish before
3741 * @param cb function to call at the end
3742 * @param cb_cls closure for cb
3745 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
3748 struct GNUNET_TIME_Relative timeout,
3749 GNUNET_TESTING_NotifyCompletion cb,
3752 struct ChurnContext *churn_ctx;
3753 unsigned int running;
3754 unsigned int stopped;
3755 unsigned int total_running;
3756 unsigned int total_stopped;
3758 unsigned int *running_arr;
3759 unsigned int *stopped_arr;
3760 unsigned int *running_permute;
3761 unsigned int *stopped_permute;
3766 if ((von == 0) && (voff == 0)) /* No peers at all? */
3772 for (i = 0; i < pg->total; i++)
3774 if (pg->peers[i].daemon->running == GNUNET_YES)
3776 GNUNET_assert(running != -1);
3781 GNUNET_assert(stopped != -1);
3788 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
3789 cb(cb_cls, "Trying to stop more peers than are currently running!");
3795 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
3796 cb(cb_cls, "Trying to start more peers than are currently stopped!");
3800 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3804 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
3808 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
3810 running_permute = NULL;
3811 stopped_permute = NULL;
3814 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
3816 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
3818 total_running = running;
3819 total_stopped = stopped;
3823 churn_ctx->num_to_start = von;
3824 churn_ctx->num_to_stop = voff;
3826 churn_ctx->cb_cls = cb_cls;
3828 for (i = 0; i < pg->total; i++)
3830 if (pg->peers[i].daemon->running == GNUNET_YES)
3832 GNUNET_assert((running_arr != NULL) && (total_running > running));
3833 running_arr[running] = i;
3838 GNUNET_assert((stopped_arr != NULL) && (total_stopped > stopped));
3839 stopped_arr[stopped] = i;
3844 GNUNET_assert(running >= voff);
3845 for (i = 0; i < voff; i++)
3848 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
3850 GNUNET_assert(running_arr != NULL);
3851 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
3853 &churn_stop_callback, churn_ctx,
3854 GNUNET_NO, GNUNET_YES);
3857 GNUNET_assert(stopped >= von);
3858 for (i = 0; i < von; i++)
3861 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
3863 GNUNET_assert(stopped_arr != NULL);
3864 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
3865 timeout, &churn_start_callback, churn_ctx);
3868 GNUNET_free_non_null(running_arr);
3869 GNUNET_free_non_null(stopped_arr);
3870 GNUNET_free_non_null(running_permute);
3871 GNUNET_free_non_null(stopped_permute);
3876 * Restart all peers in the given group.
3878 * @param pg the handle to the peer group
3879 * @param callback function to call on completion (or failure)
3880 * @param callback_cls closure for the callback function
3883 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
3884 GNUNET_TESTING_NotifyCompletion callback,
3887 struct RestartContext *restart_context;
3892 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
3893 restart_context->peer_group = pg;
3894 restart_context->peers_restarted = 0;
3895 restart_context->callback = callback;
3896 restart_context->callback_cls = callback_cls;
3898 for (off = 0; off < pg->total; off++)
3900 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
3906 * Start or stop an individual peer from the given group.
3908 * @param pg handle to the peer group
3909 * @param offset which peer to start or stop
3910 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
3911 * @param timeout how long to wait for shutdown
3912 * @param cb function to call at the end
3913 * @param cb_cls closure for cb
3916 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
3917 unsigned int offset,
3919 struct GNUNET_TIME_Relative timeout,
3920 GNUNET_TESTING_NotifyCompletion cb,
3923 struct ChurnContext *churn_ctx;
3925 if (GNUNET_NO == desired_status)
3927 if (NULL != pg->peers[offset].daemon)
3929 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3930 churn_ctx->num_to_start = 0;
3931 churn_ctx->num_to_stop = 1;
3933 churn_ctx->cb_cls = cb_cls;
3934 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
3935 timeout, &churn_stop_callback, churn_ctx,
3936 GNUNET_NO, GNUNET_YES);
3939 else if (GNUNET_YES == desired_status)
3941 if (NULL == pg->peers[offset].daemon)
3943 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3944 churn_ctx->num_to_start = 1;
3945 churn_ctx->num_to_stop = 0;
3947 churn_ctx->cb_cls = cb_cls;
3948 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
3949 timeout, &churn_start_callback, churn_ctx);
3958 * Callback for shutting down peers in a peer group.
3960 * @param cls closure (struct ShutdownContext)
3961 * @param emsg NULL on success
3963 void internal_shutdown_callback (void *cls,
3966 struct ShutdownContext *shutdown_ctx = cls;
3968 shutdown_ctx->outstanding--;
3971 shutdown_ctx->peers_down++;
3975 shutdown_ctx->peers_failed++;
3978 if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
3980 if (shutdown_ctx->peers_failed > 0)
3981 shutdown_ctx->cb(shutdown_ctx->cb_cls, "Not all peers successfully shut down!");
3983 shutdown_ctx->cb(shutdown_ctx->cb_cls, NULL);
3984 GNUNET_free(shutdown_ctx);
3989 * Individual shutdown context for a particular peer.
3991 struct PeerShutdownContext
3994 * Pointer to the high level shutdown context.
3996 struct ShutdownContext *shutdown_ctx;
3999 * The daemon handle for the peer to shut down.
4001 struct GNUNET_TESTING_Daemon *daemon;
4005 * Task to rate limit the number of outstanding peer shutdown
4006 * requests. This is necessary for making sure we don't do
4007 * too many ssh connections at once, but is generally nicer
4008 * to any system as well (graduated task starts, as opposed
4009 * to calling gnunet-arm N times all at once).
4012 schedule_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
4014 struct PeerShutdownContext *peer_shutdown_ctx = cls;
4015 struct ShutdownContext *shutdown_ctx;
4017 GNUNET_assert(peer_shutdown_ctx != NULL);
4018 shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4019 GNUNET_assert(shutdown_ctx != NULL);
4021 if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4022 GNUNET_SCHEDULER_add_delayed(peer_shutdown_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_shutdown_task, peer_shutdown_ctx);
4025 shutdown_ctx->outstanding++;
4026 GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, &internal_shutdown_callback, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4027 GNUNET_free(peer_shutdown_ctx);
4031 * Shutdown all peers started in the given group.
4033 * @param pg handle to the peer group
4034 * @param timeout how long to wait for shutdown
4035 * @param cb callback to notify upon success or failure
4036 * @param cb_cls closure for cb
4039 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
4040 struct GNUNET_TIME_Relative timeout,
4041 GNUNET_TESTING_NotifyCompletion cb,
4045 struct ShutdownContext *shutdown_ctx;
4046 struct PeerShutdownContext *peer_shutdown_ctx;
4048 GNUNET_assert(pg->total > 0);
4050 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4051 shutdown_ctx->cb = cb;
4052 shutdown_ctx->cb_cls = cb_cls;
4053 shutdown_ctx->total_peers = pg->total;
4054 shutdown_ctx->timeout = timeout;
4055 /* shtudown_ctx->outstanding = 0; */
4057 for (off = 0; off < pg->total; off++)
4059 GNUNET_assert(NULL != pg->peers[off].daemon);
4060 peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4061 peer_shutdown_ctx->daemon = pg->peers[off].daemon;
4062 peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4063 GNUNET_SCHEDULER_add_now(pg->peers[off].daemon->sched, &schedule_shutdown_task, peer_shutdown_ctx);
4064 //GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4065 if (NULL != pg->peers[off].cfg)
4066 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
4067 if (pg->peers[off].allowed_peers != NULL)
4068 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
4069 if (pg->peers[off].connect_peers != NULL)
4070 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
4071 if (pg->peers[off].blacklisted_peers != NULL)
4072 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
4074 GNUNET_free (pg->peers);
4075 if (NULL != pg->hosts)
4077 GNUNET_free (pg->hosts[0].hostname);
4078 GNUNET_free (pg->hosts);
4084 /* end of testing_group.c */