2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing_group.c
23 * @brief convenience API for writing testcases for GNUnet
24 * @author Christian Grothoff
27 #include "gnunet_arm_service.h"
28 #include "gnunet_testing_lib.h"
30 #define VERBOSE_TESTING GNUNET_NO
32 #define DEBUG_CHURN GNUNET_NO
35 * Lowest port used for GNUnet testing. Should be high enough to not
36 * conflict with other applications running on the hosts but be low
37 * enough to not conflict with client-ports (typically starting around
40 #define LOW_PORT 10000
43 * Highest port used for GNUnet testing. Should be low enough to not
44 * conflict with the port range for "local" ports (client apps; see
45 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
47 #define HIGH_PORT 32000
49 #define MAX_OUTSTANDING_CONNECTIONS 50
51 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
53 #define CONNECT_ATTEMPTS 8
56 * Prototype of a function called whenever two peers would be connected
57 * in a certain topology.
59 typedef int (*GNUNET_TESTING_ConnectionProcessor)
60 (struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second);
63 * Context for handling churning a peer group
68 * Callback used to notify of churning finished
70 GNUNET_TESTING_NotifyCompletion cb;
73 * Closure for callback
78 * Number of peers that still need to be started
80 unsigned int num_to_start;
83 * Number of peers that still need to be stopped
85 unsigned int num_to_stop;
88 * Number of peers that failed to start
90 unsigned int num_failed_start;
93 * Number of peers that failed to stop
95 unsigned int num_failed_stop;
101 * The group of peers being restarted
103 struct GNUNET_TESTING_PeerGroup *peer_group;
106 * How many peers have been restarted thus far
108 unsigned int peers_restarted;
111 * How many peers got an error when restarting
113 unsigned int peers_restart_failed;
116 * The function to call once all peers have been restarted
118 GNUNET_TESTING_NotifyCompletion callback;
121 * Closure for callback function
127 struct CreateTopologyContext
131 * Function to call with number of connections
133 GNUNET_TESTING_NotifyConnections cont;
136 * Closure for connection notification
142 struct PeerConnection
147 struct PeerConnection *next;
150 * Pointer to daemon handle
152 struct GNUNET_TESTING_Daemon *daemon;
158 * Data we keep per peer.
163 * (Initial) configuration of the host.
164 * (initial because clients could change
165 * it and we would not know about those
168 struct GNUNET_CONFIGURATION_Handle *cfg;
171 * Handle for controlling the daemon.
173 struct GNUNET_TESTING_Daemon *daemon;
176 * The peergroup this peer belongs to.
178 struct GNUNET_TESTING_PeerGroup *pg;
181 * Linked list of peer connections (pointers)
183 //struct PeerConnection *connected_peers;
185 * Hash map of allowed peer connections (F2F created topology)
187 struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
190 * Hash map of blacklisted peers
192 struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
195 * Hash map of peer connections
197 struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
200 * Temporary hash map of peer connections
202 struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
205 * Temporary variable for topology creation, should be reset before
206 * creating any topology so the count is valid once finished.
213 * Data we keep per host.
223 * Lowest port that we have not yet used
231 * Handle to a group of GNUnet peers.
233 struct GNUNET_TESTING_PeerGroup
238 struct GNUNET_SCHEDULER_Handle *sched;
241 * Configuration template.
243 const struct GNUNET_CONFIGURATION_Handle *cfg;
246 * Function to call on each started daemon.
248 GNUNET_TESTING_NotifyDaemonRunning cb;
256 * Function to call on each topology connection created
258 GNUNET_TESTING_NotifyConnection notify_connection;
261 * Callback for notify_connection
263 void *notify_connection_cls;
266 * NULL-terminated array of information about
269 struct HostData *hosts;
272 * Array of "total" peers.
274 struct PeerData *peers;
277 * Number of peers in this group.
282 * At what time should we fail the peer startup process?
284 struct GNUNET_TIME_Absolute max_timeout;
288 * Convert unique ID to hash code.
290 * @param uid unique ID to convert
291 * @param hash set to uid (extended with zeros)
294 hash_from_uid (uint32_t uid,
295 GNUNET_HashCode *hash)
297 memset (hash, 0, sizeof(GNUNET_HashCode));
298 *((uint32_t*)hash) = uid;
302 * Convert hash code to unique ID.
304 * @param uid unique ID to convert
305 * @param hash set to uid (extended with zeros)
308 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
310 memcpy (uid, hash, sizeof(uint32_t));
315 struct GNUNET_CONFIGURATION_Handle *ret;
316 const char *hostname;
322 struct ConnectContext
324 struct GNUNET_TESTING_Daemon *first;
326 struct GNUNET_TESTING_Daemon *second;
328 struct GNUNET_TESTING_PeerGroup *pg;
332 * Number of connects we are waiting on, allows us to rate limit
335 static int outstanding_connects;
339 * Function to iterate over options. Copies
340 * the options to the target configuration,
341 * updating PORT values as needed.
344 * @param section name of the section
345 * @param option name of the option
346 * @param value value of the option
349 update_config (void *cls,
350 const char *section, const char *option, const char *value)
352 struct UpdateContext *ctx = cls;
357 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
361 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
366 if (0 == strcmp (option, "UNIXPATH"))
368 GNUNET_snprintf (uval,
370 "/tmp/test-service-%s-%u",
376 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
378 value = ctx->hostname;
381 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
386 * Create a new configuration using the given configuration
387 * as a template; however, each PORT in the existing cfg
388 * must be renumbered by incrementing "*port". If we run
389 * out of "*port" numbers, return NULL.
391 * @param cfg template configuration
392 * @param port port numbers to use, update to reflect
393 * port numbers that were used
394 * @param upnum number to make unix domain socket names unique
395 * @param hostname hostname of the controlling host, to allow control connections from
397 * @return new configuration, NULL on error
399 static struct GNUNET_CONFIGURATION_Handle *
400 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
403 const char *hostname)
405 struct UpdateContext uc;
413 uc.ret = GNUNET_CONFIGURATION_create ();
414 uc.hostname = hostname;
416 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
417 if (uc.nport >= HIGH_PORT)
420 GNUNET_CONFIGURATION_destroy (uc.ret);
424 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
426 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
427 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
428 GNUNET_free_non_null(control_host);
429 GNUNET_free(allowed_hosts);
433 /* arm needs to know to allow connections from the host on which it is running,
434 * otherwise gnunet-arm is unable to connect to it in some instances */
435 if (hostname != NULL)
437 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
438 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
439 GNUNET_free(allowed_hosts);
442 *port = (uint16_t) uc.nport;
449 * Add entries to the peers connect list
451 * @param pg the peer group we are working with
452 * @param first index of the first peer
453 * @param second index of the second peer
455 * @return the number of connections added (can be 0, 1 or 2)
456 * technically should only be 0 or 2, but the small price
457 * of iterating over the lists (hashmaps in the future)
458 * for being sure doesn't bother me!
462 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
468 GNUNET_HashCode hash_first;
469 GNUNET_HashCode hash_second;
471 hash_from_uid(first, &hash_first);
472 hash_from_uid(second, &hash_second);
474 add_first = GNUNET_NO;
475 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
477 add_first = GNUNET_YES;
480 add_second = GNUNET_NO;
481 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
483 add_second = GNUNET_YES;
489 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
490 pg->peers[first].num_connections++;
496 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
497 pg->peers[second].num_connections++;
506 * Add entries to the peers allowed connections list
508 * @param pg the peer group we are working with
509 * @param first index of the first peer
510 * @param second index of the second peer
512 * @return the number of connections added (can be 0, 1 or 2)
513 * technically should only be 0 or 2, but the small price
514 * of iterating over the lists (hashmaps in the future)
515 * for being sure doesn't bother me!
519 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
523 struct PeerConnection *first_iter;
524 struct PeerConnection *second_iter;
525 struct PeerConnection *new_first;
526 struct PeerConnection *new_second;
531 GNUNET_HashCode hash_first;
532 GNUNET_HashCode hash_second;
534 hash_from_uid(first, &hash_first);
535 hash_from_uid(second, &hash_second);
537 add_first = GNUNET_NO;
538 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
540 add_first = GNUNET_YES;
543 add_second = GNUNET_NO;
544 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
546 add_second = GNUNET_YES;
549 first_iter = pg->peers[first].connected_peers;
550 while (first_iter != NULL)
552 if (first_iter->daemon == pg->peers[second].daemon)
553 add_first = GNUNET_NO;
554 first_iter = first_iter->next;
557 second_iter = pg->peers[second].connected_peers;
558 add_second = GNUNET_YES;
559 while (second_iter != NULL)
561 if (second_iter->daemon == pg->peers[first].daemon)
562 add_second = GNUNET_NO;
563 second_iter = second_iter->next;
570 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
572 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
573 new_first->daemon = pg->peers[second].daemon;
574 new_first->next = pg->peers[first].connected_peers;
575 pg->peers[first].connected_peers = new_first;
577 pg->peers[first].num_connections++;
583 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
585 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
586 new_second->daemon = pg->peers[first].daemon;
587 new_second->next = pg->peers[second].connected_peers;
588 pg->peers[second].connected_peers = new_second;
589 pg->peers[first].num_connections++;
591 pg->peers[second].num_connections++;
599 * Add entries to the peers blacklisted list
601 * @param pg the peer group we are working with
602 * @param first index of the first peer
603 * @param second index of the second peer
605 * @return the number of connections added (can be 0, 1 or 2)
609 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
614 GNUNET_HashCode hash_first;
615 GNUNET_HashCode hash_second;
617 hash_from_uid(first, &hash_first);
618 hash_from_uid(second, &hash_second);
620 add_first = GNUNET_NO;
621 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
623 add_first = GNUNET_YES;
626 add_second = GNUNET_NO;
627 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
629 add_second = GNUNET_YES;
635 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
636 pg->peers[first].num_connections++;
642 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
643 pg->peers[second].num_connections++;
651 * Remove entries from the peers blacklisted list
653 * @param pg the peer group we are working with
654 * @param first index of the first peer
655 * @param second index of the second peer
657 * @return the number of connections removed (can be 0, 1 or 2)
661 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
666 GNUNET_HashCode hash_first;
667 GNUNET_HashCode hash_second;
669 hash_from_uid(first, &hash_first);
670 hash_from_uid(second, &hash_second);
672 remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
673 remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
678 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
684 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
692 * Scale free network construction as described in:
694 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
696 * Start with a network of "one" peer, then progressively add
697 * peers up to the total number. At each step, iterate over
698 * all possible peers and connect new peer based on number of
699 * existing connections of the target peer.
701 * @param pg the peer group we are dealing with
702 * @param proc the connection processor to use
704 * @return the number of connections created
707 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
710 unsigned int total_connections;
711 unsigned int outer_count;
713 unsigned int previous_total_connections;
717 GNUNET_assert(pg->total > 1);
719 /* Add a connection between the first two nodes */
720 total_connections = proc(pg, 0, 1);
722 for (outer_count = 1; outer_count < pg->total; outer_count++)
724 previous_total_connections = total_connections;
725 for (i = 0; i < outer_count; i++)
727 probability = pg->peers[i].num_connections / (double)previous_total_connections;
728 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
729 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "Considering connecting peer %d to peer %d\n",
735 if (random < probability)
738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
739 "Connecting peer %d to peer %d\n",
742 total_connections += proc(pg, outer_count, i);
747 return total_connections;
751 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
756 unsigned int randomPeer;
757 double random, logNModifier, percentage;
758 unsigned int smallWorldConnections;
764 int connect_attempts;
766 logNModifier = 0.5; /* FIXME: default value? */
767 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
772 if (sscanf(p_string, "%lf", &logNModifier) != 1)
773 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
774 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
778 GNUNET_free (p_string);
780 percentage = 0.5; /* FIXME: default percentage? */
781 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
786 if (sscanf(p_string, "%lf", &percentage) != 1)
787 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
788 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
792 GNUNET_free (p_string);
794 natLog = log (pg->total);
795 connsPerPeer = ceil (natLog * logNModifier);
797 if (connsPerPeer % 2 == 1)
800 smallWorldConnections = 0;
801 connect_attempts = 0;
802 for (i = 0; i < pg->total; i++)
805 max = i + connsPerPeer / 2;
806 min = i - connsPerPeer / 2;
808 if (max > pg->total - 1)
810 max = max - pg->total;
816 min = pg->total - 1 + min;
820 for (j = 0; j < connsPerPeer / 2; j++)
822 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
823 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
824 if (random < percentage)
826 /* Connect to uniformly selected random peer */
828 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
830 while ((((randomPeer < max) && (randomPeer > min))
831 && (useAnd == 0)) || (((randomPeer > min)
832 || (randomPeer < max))
836 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
839 smallWorldConnections +=
840 proc (pg, i, randomPeer);
844 nodeToConnect = i + j + 1;
845 if (nodeToConnect > pg->total - 1)
847 nodeToConnect = nodeToConnect - pg->total;
850 proc (pg, i, nodeToConnect);
856 connect_attempts += smallWorldConnections;
858 return connect_attempts;
863 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
865 unsigned int outer_count, inner_count;
867 int connect_attempts;
868 double nat_percentage;
871 nat_percentage = 0.6; /* FIXME: default percentage? */
872 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
877 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
878 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
879 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
883 GNUNET_free (p_string);
888 cutoff = (unsigned int) (nat_percentage * pg->total);
890 connect_attempts = 0;
892 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
894 for (inner_count = outer_count + 1; inner_count < pg->total;
897 if ((outer_count > cutoff) || (inner_count > cutoff))
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "Connecting peer %d to peer %d\n",
902 outer_count, inner_count);
904 connect_attempts += proc(pg, outer_count, inner_count);
909 return connect_attempts;
916 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
918 unsigned int i, j, k;
922 unsigned int toggle = 1;
923 unsigned int nodeToConnect;
925 unsigned int node1Row;
926 unsigned int node1Col;
927 unsigned int node2Row;
928 unsigned int node2Col;
929 unsigned int distance;
930 double probability, random, percentage;
931 unsigned int smallWorldConnections;
933 int connect_attempts;
934 square = floor (sqrt (pg->total));
938 percentage = 0.5; /* FIXME: default percentage? */
939 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
944 if (sscanf(p_string, "%lf", &percentage) != 1)
945 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
946 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
950 GNUNET_free (p_string);
952 probability = 0.5; /* FIXME: default percentage? */
953 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
958 if (sscanf(p_string, "%lf", &probability) != 1)
959 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
960 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
964 GNUNET_free (p_string);
966 if (square * square != pg->total)
968 while (rows * cols < pg->total)
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
984 connect_attempts = 0;
985 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
986 * to the node to its right and above. Once this is over, we'll have our torus!
987 * Special case for the last node (if the rows and columns are not equal), connect
988 * to the first in the row to maintain topology.
990 for (i = 0; i < pg->total; i++)
992 /* First connect to the node to the right */
993 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
994 nodeToConnect = i + 1;
995 else if (i + 1 == pg->total)
996 nodeToConnect = rows * cols - cols;
998 nodeToConnect = i - cols + 1;
1000 connect_attempts += proc (pg, i, nodeToConnect);
1003 nodeToConnect = (rows * cols) - cols + i;
1005 nodeToConnect = i - cols;
1007 if (nodeToConnect < pg->total)
1008 connect_attempts += proc (pg, i, nodeToConnect);
1010 natLog = log (pg->total);
1011 #if VERBOSE_TESTING > 2
1012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1013 _("natural log of %d is %d, will run %d iterations\n"),
1014 pg->total, natLog, (int) (natLog * percentage));
1015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1017 smallWorldConnections = 0;
1018 for (i = 0; i < (int) (natLog * percentage); i++)
1020 for (j = 0; j < pg->total; j++)
1022 /* Determine the row and column of node at position j on the 2d torus */
1023 node1Row = j / cols;
1024 node1Col = j - (node1Row * cols);
1025 for (k = 0; k < pg->total; k++)
1027 /* Determine the row and column of node at position k on the 2d torus */
1028 node2Row = k / cols;
1029 node2Col = k - (node2Row * cols);
1030 /* Simple Cartesian distance */
1031 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1034 /* Calculate probability as 1 over the square of the distance */
1035 probability = 1.0 / (distance * distance);
1036 /* Choose a random value between 0 and 1 */
1037 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1038 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
1039 /* If random < probability, then connect the two nodes */
1040 if (random < probability)
1041 smallWorldConnections += proc (pg, j, k);
1047 connect_attempts += smallWorldConnections;
1048 #if VERBOSE_TESTING > 2
1049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1050 _("Total connections added for small world: %d!\n"),
1051 smallWorldConnections);
1053 return connect_attempts;
1059 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1062 unsigned int outer_count;
1063 unsigned int inner_count;
1064 int connect_attempts;
1068 probability = 0.5; /* FIXME: default percentage? */
1069 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1074 if (sscanf(p_string, "%lf", &probability) != 1)
1075 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1076 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1080 GNUNET_free (p_string);
1082 connect_attempts = 0;
1083 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1085 for (inner_count = outer_count + 1; inner_count < pg->total;
1088 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1089 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1092 _("rand is %f probability is %f\n"), temp_rand,
1095 if (temp_rand < probability)
1097 connect_attempts += proc (pg, outer_count, inner_count);
1102 return connect_attempts;
1106 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1109 unsigned int square;
1112 unsigned int toggle = 1;
1113 unsigned int nodeToConnect;
1114 int connect_attempts;
1116 connect_attempts = 0;
1118 square = floor (sqrt (pg->total));
1122 if (square * square != pg->total)
1124 while (rows * cols < pg->total)
1126 if (toggle % 2 == 0)
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1136 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1139 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1140 * to the node to its right and above. Once this is over, we'll have our torus!
1141 * Special case for the last node (if the rows and columns are not equal), connect
1142 * to the first in the row to maintain topology.
1144 for (i = 0; i < pg->total; i++)
1146 /* First connect to the node to the right */
1147 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1148 nodeToConnect = i + 1;
1149 else if (i + 1 == pg->total)
1150 nodeToConnect = rows * cols - cols;
1152 nodeToConnect = i - cols + 1;
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1155 "Connecting peer %d to peer %d\n",
1158 connect_attempts += proc(pg, i, nodeToConnect);
1160 /* Second connect to the node immediately above */
1162 nodeToConnect = (rows * cols) - cols + i;
1164 nodeToConnect = i - cols;
1166 if (nodeToConnect < pg->total)
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 "Connecting peer %d to peer %d\n",
1173 connect_attempts += proc(pg, i, nodeToConnect);
1178 return connect_attempts;
1184 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1186 unsigned int outer_count;
1187 unsigned int inner_count;
1188 int connect_attempts;
1190 connect_attempts = 0;
1192 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1194 for (inner_count = outer_count + 1; inner_count < pg->total;
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1199 "Connecting peer %d to peer %d\n",
1200 outer_count, inner_count);
1202 connect_attempts += proc(pg, outer_count, inner_count);
1206 return connect_attempts;
1211 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1214 int connect_attempts;
1216 connect_attempts = 0;
1218 /* Connect each peer to the next highest numbered peer */
1219 for (count = 0; count < pg->total - 1; count++)
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "Connecting peer %d to peer %d\n",
1226 connect_attempts += proc(pg, count, count + 1);
1229 /* Connect the last peer to the first peer */
1230 connect_attempts += proc(pg, pg->total - 1, 0);
1232 return connect_attempts;
1237 * Iterator for writing friends of a peer to a file.
1239 * @param cls closure, an open writable file handle
1240 * @param key the key the daemon was stored under
1241 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1243 * @return GNUNET_YES to continue iteration
1245 * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1246 * with a single file_iterator that takes a closure which contains
1247 * the prefix to write before the peer. Then this could be used
1248 * for blacklisting multiple transports and writing the friend
1249 * file. I'm sure *someone* will complain loudly about other
1250 * things that negate these functions even existing so no point in
1254 friend_file_iterator (void *cls,
1255 const GNUNET_HashCode * key,
1258 FILE *temp_friend_handle = cls;
1259 struct GNUNET_TESTING_Daemon *peer = value;
1260 struct GNUNET_PeerIdentity *temppeer;
1261 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1263 temppeer = &peer->id;
1264 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1265 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1270 struct BlacklistContext
1273 * The (open) file handle to write to
1275 FILE *temp_file_handle;
1278 * The transport that this peer will be blacklisted on.
1284 * Iterator for writing blacklist data to appropriate files.
1286 * @param cls closure, an open writable file handle
1287 * @param key the key the daemon was stored under
1288 * @param value the GNUNET_TESTING_Daemon that needs to be written.
1290 * @return GNUNET_YES to continue iteration
1293 blacklist_file_iterator (void *cls,
1294 const GNUNET_HashCode * key,
1297 struct BlacklistContext *blacklist_ctx = cls;
1298 //FILE *temp_blacklist_handle = cls;
1299 struct GNUNET_TESTING_Daemon *peer = value;
1300 struct GNUNET_PeerIdentity *temppeer;
1301 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1303 temppeer = &peer->id;
1304 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1305 fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1311 * Create the friend files based on the PeerConnection's
1312 * of each peer in the peer group, and copy the files
1313 * to the appropriate place
1315 * @param pg the peer group we are dealing with
1318 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1320 FILE *temp_friend_handle;
1321 unsigned int pg_iter;
1322 char *temp_service_path;
1326 enum GNUNET_OS_ProcessStatusType type;
1327 unsigned long return_code;
1332 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1333 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1335 mytemp = GNUNET_DISK_mktemp("friends");
1336 GNUNET_assert(mytemp != NULL);
1337 temp_friend_handle = fopen (mytemp, "wt");
1338 GNUNET_assert(temp_friend_handle != NULL);
1339 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1340 fclose(temp_friend_handle);
1343 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1345 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1346 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1349 if (UNLINK (mytemp) != 0)
1350 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1351 GNUNET_free (mytemp);
1355 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1357 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1358 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1359 "mv", mytemp, arg, NULL);
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1362 _("Copying file with command cp %s %s\n"), mytemp, arg);
1367 else /* Remote, scp the file to the correct place */
1369 if (NULL != pg->peers[pg_iter].daemon->username)
1370 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1372 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1373 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1374 "scp", mytemp, arg, NULL);
1377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1378 _("Copying file with command scp %s %s\n"), mytemp, arg);
1382 GNUNET_free (temp_service_path);
1383 GNUNET_free (mytemp);
1387 ret = GNUNET_SYSERR;
1388 while ((count < max_wait) && (ret != GNUNET_OK))
1391 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 _("Checking copy status of file %d\n"), pg_iter);
1397 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1399 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1401 ret = GNUNET_SYSERR;
1403 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1405 ret = GNUNET_SYSERR;
1409 pidarr[pg_iter] = 0;
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412 _("File %d copied\n"), pg_iter);
1418 if (ret == GNUNET_SYSERR)
1420 /* FIXME: why sleep here? -CG */
1426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1427 _("Finished copying all friend files!\n"));
1429 GNUNET_free(pidarr);
1435 * Create the blacklist files based on the PeerConnection's
1436 * of each peer in the peer group, and copy the files
1437 * to the appropriate place.
1439 * @param pg the peer group we are dealing with
1440 * @param transports space delimited list of transports to blacklist
1443 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
1445 FILE *temp_file_handle;
1446 static struct BlacklistContext blacklist_ctx;
1447 unsigned int pg_iter;
1448 char *temp_service_path;
1452 enum GNUNET_OS_ProcessStatusType type;
1453 unsigned long return_code;
1460 char *temp_transports;
1462 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1463 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1465 mytemp = GNUNET_DISK_mktemp("blacklist");
1466 GNUNET_assert(mytemp != NULL);
1467 temp_file_handle = fopen (mytemp, "wt");
1468 GNUNET_assert(temp_file_handle != NULL);
1469 temp_transports = GNUNET_strdup(transports);
1470 blacklist_ctx.temp_file_handle = temp_file_handle;
1471 transport_len = strlen(temp_transports) + 1;
1474 for (i = 0; i < transport_len; i++)
1476 if ((temp_transports[i] == ' ') && (pos == NULL))
1477 continue; /* At start of string (whitespace) */
1478 else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
1480 temp_transports[i] = '\0';
1481 blacklist_ctx.transport = pos;
1482 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
1484 } /* At beginning of actual string */
1485 else if (pos == NULL)
1487 pos = &temp_transports[i];
1491 GNUNET_free_non_null(temp_transports);
1492 fclose(temp_file_handle);
1495 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1497 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1498 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1501 if (UNLINK (mytemp) != 0)
1502 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1503 GNUNET_free (mytemp);
1507 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1509 GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
1510 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1511 "mv", mytemp, arg, NULL);
1513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1514 _("Copying file with command cp %s %s\n"), mytemp, arg);
1519 else /* Remote, scp the file to the correct place */
1521 if (NULL != pg->peers[pg_iter].daemon->username)
1522 GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1524 GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1525 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1526 "scp", mytemp, arg, NULL);
1529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1530 _("Copying file with command scp %s %s\n"), mytemp, arg);
1534 GNUNET_free (temp_service_path);
1535 GNUNET_free (mytemp);
1539 ret = GNUNET_SYSERR;
1540 while ((count < max_wait) && (ret != GNUNET_OK))
1543 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1547 _("Checking copy status of file %d\n"), pg_iter);
1549 if (pidarr[pg_iter] != 0) /* Check for already completed! */
1551 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1553 ret = GNUNET_SYSERR;
1555 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1557 ret = GNUNET_SYSERR;
1561 pidarr[pg_iter] = 0;
1563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1564 _("File %d copied\n"), pg_iter);
1570 if (ret == GNUNET_SYSERR)
1572 /* FIXME: why sleep here? -CG */
1578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1579 _("Finished copying all blacklist files!\n"));
1581 GNUNET_free(pidarr);
1587 * Internal notification of a connection, kept so that we can ensure some connections
1588 * happen instead of flooding all testing daemons with requests to connect.
1590 static void internal_connect_notify (void *cls,
1591 const struct GNUNET_PeerIdentity *first,
1592 const struct GNUNET_PeerIdentity *second,
1593 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
1594 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
1595 struct GNUNET_TESTING_Daemon *first_daemon,
1596 struct GNUNET_TESTING_Daemon *second_daemon,
1599 struct GNUNET_TESTING_PeerGroup *pg = cls;
1600 outstanding_connects--;
1602 pg->notify_connection(pg->notify_connection_cls, first, second, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
1606 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1608 struct ConnectContext *connect_context = cls;
1610 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1613 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
1615 #if VERBOSE_TESTING > 2
1616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1617 _("Delaying connect, we have too many outstanding connections!\n"));
1619 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &schedule_connect, connect_context);
1623 #if VERBOSE_TESTING > 2
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1625 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
1627 outstanding_connects++;
1628 GNUNET_TESTING_daemons_connect (connect_context->first,
1629 connect_context->second,
1632 &internal_connect_notify,
1633 connect_context->pg);
1634 GNUNET_free(connect_context);
1639 * Iterator for actually scheduling connections to be created
1640 * between two peers.
1642 * @param cls closure, a GNUNET_TESTING_Daemon
1643 * @param key the key the second Daemon was stored under
1644 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1646 * @return GNUNET_YES to continue iteration
1649 connect_iterator (void *cls,
1650 const GNUNET_HashCode * key,
1653 struct PeerData *first = cls;
1654 struct GNUNET_TESTING_Daemon *second = value;
1655 struct ConnectContext *connect_context;
1657 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1658 connect_context->pg = first->pg;
1659 connect_context->first = first->daemon;
1660 connect_context->second = second;
1661 GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
1667 * Iterator for copying all entries in the allowed hashmap to the
1670 * @param cls closure, a GNUNET_TESTING_Daemon
1671 * @param key the key the second Daemon was stored under
1672 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1674 * @return GNUNET_YES to continue iteration
1677 copy_topology_iterator (void *cls,
1678 const GNUNET_HashCode * key,
1681 struct PeerData *first = cls;
1683 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1689 * Make the peers to connect the same as those that are allowed to be
1692 * @param pg the peer group
1695 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
1697 unsigned int pg_iter;
1702 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1704 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, ©_topology_iterator, &pg->peers[pg_iter]);
1705 if (GNUNET_SYSERR == ret)
1706 return GNUNET_SYSERR;
1708 total = total + ret;
1716 * Connect the topology as specified by the PeerConnection's
1717 * of each peer in the peer group
1719 * @param pg the peer group we are dealing with
1720 * @return the number of connections that will be attempted
1723 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
1725 unsigned int pg_iter;
1729 struct PeerConnection *connection_iter;
1730 struct ConnectContext *connect_context;
1734 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1736 ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
1737 if (GNUNET_SYSERR == ret)
1738 return GNUNET_SYSERR;
1740 total = total + ret;
1744 while (connection_iter != NULL)
1746 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1747 connect_context->pg = pg;
1748 connect_context->first = ;
1749 connect_context->second = connection_iter->daemon;
1750 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
1751 connection_iter = connection_iter->next;
1760 * Takes a peer group and creates a topology based on the
1761 * one specified. Creates a topology means generates friend
1762 * files for the peers so they can only connect to those allowed
1763 * by the topology. This will only have an effect once peers
1764 * are started if the FRIENDS_ONLY option is set in the base
1765 * config. Also takes an optional restrict topology which
1766 * disallows direct TCP connections UNLESS they are specified in
1767 * the restricted topology.
1769 * @param pg the peer group struct representing the running peers
1770 * @param topology which topology to connect the peers in
1771 * @param restrict_topology allow only direct TCP connections in this topology
1772 * use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
1773 * @param restrict_transports space delimited list of transports to blacklist
1774 * to create restricted topology
1776 * @return the maximum number of connections were all allowed peers
1777 * connected to each other
1780 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
1781 enum GNUNET_TESTING_Topology topology,
1782 enum GNUNET_TESTING_Topology restrict_topology,
1783 char *restrict_transports)
1786 int num_connections;
1787 int unblacklisted_connections;
1789 GNUNET_assert (pg->notify_connection != NULL);
1794 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
1796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1797 _("Creating clique topology\n"));
1799 num_connections = create_clique (pg, &add_allowed_connections);
1801 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
1803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1804 _("Creating small world (ring) topology\n"));
1806 num_connections = create_small_world_ring (pg, &add_allowed_connections);
1808 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
1810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1811 _("Creating small world (2d-torus) topology\n"));
1813 num_connections = create_small_world (pg, &add_allowed_connections);
1815 case GNUNET_TESTING_TOPOLOGY_RING:
1817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1818 _("Creating ring topology\n"));
1820 num_connections = create_ring (pg, &add_allowed_connections);
1822 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
1824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1825 _("Creating 2d torus topology\n"));
1827 num_connections = create_2d_torus (pg, &add_allowed_connections);
1829 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
1831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1832 _("Creating Erdos-Renyi topology\n"));
1834 num_connections = create_erdos_renyi (pg, &add_allowed_connections);
1836 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
1838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1839 _("Creating InterNAT topology\n"));
1841 num_connections = create_nated_internet (pg, &add_allowed_connections);
1843 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
1845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1846 _("Creating Scale Free topology\n"));
1848 num_connections = create_scale_free (pg, &add_allowed_connections);
1850 case GNUNET_TESTING_TOPOLOGY_NONE:
1851 num_connections = 0;
1854 num_connections = 0;
1857 if (num_connections < 1)
1858 return GNUNET_SYSERR;
1860 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
1862 ret = create_and_copy_friend_files(pg);
1865 if (ret != GNUNET_OK)
1868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1869 _("Failed during friend file copying!\n"));
1871 return GNUNET_SYSERR;
1876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1877 _("Friend files created/copied successfully!\n"));
1881 /* Use the create clique method to initially set all connections as blacklisted. */
1882 create_clique (pg, &blacklist_connections);
1883 unblacklisted_connections = 0;
1884 /* Un-blacklist connections as per the topology specified */
1885 switch (restrict_topology)
1887 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
1889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1890 _("Blacklisting all but clique topology\n"));
1892 unblacklisted_connections = create_clique (pg, &unblacklist_connections);
1894 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
1896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1897 _("Blacklisting all but small world (ring) topology\n"));
1899 unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
1901 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
1903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1904 _("Blacklisting all but small world (2d-torus) topology\n"));
1906 unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
1908 case GNUNET_TESTING_TOPOLOGY_RING:
1910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1911 _("Blacklisting all but ring topology\n"));
1913 unblacklisted_connections = create_ring (pg, &unblacklist_connections);
1915 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1918 _("Blacklisting all but 2d torus topology\n"));
1920 unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
1922 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
1924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1925 _("Blacklisting all but Erdos-Renyi topology\n"));
1927 unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
1929 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
1931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1932 _("Blacklisting all but InterNAT topology\n"));
1934 unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
1936 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
1938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1939 _("Blacklisting all but Scale Free topology\n"));
1941 unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
1943 case GNUNET_TESTING_TOPOLOGY_NONE:
1949 if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
1951 ret = create_and_copy_blacklist_files(pg, restrict_transports);
1952 if (ret != GNUNET_OK)
1955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1956 _("Failed during blacklist file copying!\n"));
1958 return GNUNET_SYSERR;
1963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1964 _("Blacklist files created/copied successfully!\n"));
1968 return num_connections;
1971 struct RandomContext
1976 struct GNUNET_TESTING_PeerGroup *pg;
1979 * uid of the first peer
1984 * Peer data for first peer.
1986 struct PeerData *first;
1989 * Random percentage to use
1994 struct MinimumContext
1999 struct GNUNET_TESTING_PeerGroup *pg;
2002 * uid of the first peer
2007 * Peer data for first peer.
2009 struct PeerData *first;
2012 * Number of conns per peer
2014 unsigned int num_to_add;
2017 * Permuted array of all possible connections. Only add the Nth
2018 * peer if it's in the Nth position.
2020 unsigned int *pg_array;
2023 * What number is the current element we are iterating over?
2025 unsigned int current;
2033 struct GNUNET_TESTING_PeerGroup *pg;
2036 * uid of the first peer
2041 * uid of the second peer
2043 uint32_t second_uid;
2046 * Peer data for first peer.
2048 struct PeerData *first;
2051 * Which peer has been chosen as the one to add?
2053 unsigned int chosen;
2056 * What number is the current element we are iterating over?
2058 unsigned int current;
2062 * Iterator for choosing random peers to connect.
2064 * @param cls closure, a RandomContext
2065 * @param key the key the second Daemon was stored under
2066 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2068 * @return GNUNET_YES to continue iteration
2071 random_connect_iterator (void *cls,
2072 const GNUNET_HashCode * key,
2075 struct RandomContext *random_ctx = cls;
2076 double random_number;
2077 uint32_t second_pos;
2078 GNUNET_HashCode first_hash;
2079 random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2080 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
2081 if (random_number < random_ctx->percentage)
2083 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2085 /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2086 uid_from_hash(key, &second_pos);
2087 hash_from_uid(random_ctx->first_uid, &first_hash);
2088 GNUNET_assert(random_ctx->pg->total > second_pos);
2089 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2095 * Iterator for adding at least X peers to a peers connection set.
2097 * @param cls closure, MinimumContext
2098 * @param key the key the second Daemon was stored under
2099 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2101 * @return GNUNET_YES to continue iteration
2104 minimum_connect_iterator (void *cls,
2105 const GNUNET_HashCode * key,
2108 struct MinimumContext *min_ctx = cls;
2109 uint32_t second_pos;
2110 GNUNET_HashCode first_hash;
2113 if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2115 for (i = 0; i < min_ctx->num_to_add; i++)
2117 if (min_ctx->pg_array[i] == min_ctx->current)
2119 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2120 uid_from_hash(key, &second_pos);
2121 hash_from_uid(min_ctx->first_uid, &first_hash);
2122 GNUNET_assert(min_ctx->pg->total > second_pos);
2123 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));
2124 /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2125 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2132 return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2138 * Iterator for adding peers to a connection set based on a depth first search.
2140 * @param cls closure, MinimumContext
2141 * @param key the key the second daemon was stored under
2142 * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2144 * @return GNUNET_YES to continue iteration
2147 dfs_connect_iterator (void *cls,
2148 const GNUNET_HashCode * key,
2151 struct DFSContext *dfs_ctx = cls;
2152 GNUNET_HashCode first_hash;
2154 if (dfs_ctx->current == dfs_ctx->chosen)
2156 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2157 uid_from_hash(key, &dfs_ctx->second_uid);
2158 hash_from_uid(dfs_ctx->first_uid, &first_hash);
2159 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));
2160 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2161 /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2162 return GNUNET_NO; /* We have found our peer, don't iterate more */
2171 * From the set of connections possible, choose percentage percent of connections
2172 * to actually connect.
2174 * @param pg the peergroup we are dealing with
2175 * @param percentage what percent of total connections to make
2178 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2180 struct RandomContext random_ctx;
2183 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2185 random_ctx.first_uid = pg_iter;
2186 random_ctx.first = &pg->peers[pg_iter];
2187 random_ctx.percentage = percentage;
2189 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2190 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2191 /* Now remove the old connections */
2192 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2193 /* And replace with the random set */
2194 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2199 * From the set of connections possible, choose at least num connections per
2202 * @param pg the peergroup we are dealing with
2203 * @param num how many connections at least should each peer have (if possible)?
2206 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2208 struct MinimumContext minimum_ctx;
2211 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2213 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2216 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2218 minimum_ctx.first_uid = pg_iter;
2219 minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2220 minimum_ctx.first = &pg->peers[pg_iter];
2221 minimum_ctx.pg = pg;
2222 minimum_ctx.num_to_add = num;
2223 minimum_ctx.current = 0;
2224 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2225 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &minimum_connect_iterator, &minimum_ctx);
2228 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2230 /* Remove the "old" connections */
2231 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2232 /* And replace with the working set */
2233 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2234 fprintf(stderr, "Finished! Hashmap size %u\n", GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2240 static unsigned int count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2243 unsigned int pg_iter;
2247 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2249 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2256 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2259 unsigned int pg_iter;
2263 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2265 count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2272 * From the set of connections possible, choose at least num connections per
2273 * peer based on depth first traversal of peer connections. If DFS leaves
2274 * peers unconnected, ensure those peers get connections.
2276 * @param pg the peergroup we are dealing with
2277 * @param num how many connections at least should each peer have (if possible)?
2280 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2282 struct DFSContext dfs_ctx;
2285 uint32_t starting_peer;
2286 uint32_t least_connections;
2287 GNUNET_HashCode second_hash;
2289 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2291 pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2296 while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
2298 if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
2300 least_connections = -1; /* Set to very high number */
2301 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2303 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
2305 starting_peer = pg_iter;
2306 least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2311 if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0) /* Ensure there is at least one peer left to connect! */
2317 /* Choose a random peer from the chosen peers set of connections to add */
2318 dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
2319 dfs_ctx.first_uid = starting_peer;
2320 dfs_ctx.first = &pg->peers[starting_peer];
2322 dfs_ctx.current = 0;
2324 GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
2325 /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
2326 hash_from_uid(dfs_ctx.second_uid, &second_hash);
2327 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
2328 starting_peer = dfs_ctx.second_uid;
2331 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2336 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2338 /* Remove the "old" connections */
2339 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2340 /* And replace with the working set */
2341 pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2342 fprintf(stderr, "Finished! Hashmap size %u\n", GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2348 * There are many ways to connect peers that are supported by this function.
2349 * To connect peers in the same topology that was created via the
2350 * GNUNET_TESTING_create_topology, the topology variable must be set to
2351 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
2352 * a new instance of that topology will be generated and attempted to be
2353 * connected. This could result in some connections being impossible,
2354 * because some topologies are non-deterministic.
2356 * @param pg the peer group struct representing the running peers
2357 * @param topology which topology to connect the peers in
2358 * @param options options for connecting the topology
2359 * @param option_modifier modifier for options that take a parameter
2360 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
2363 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2364 enum GNUNET_TESTING_Topology topology,
2365 enum GNUNET_TESTING_TopologyOption options,
2366 double option_modifier)
2370 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2373 _("Creating clique topology\n"));
2375 create_clique (pg, &add_actual_connections);
2377 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2380 _("Creating small world (ring) topology\n"));
2382 create_small_world_ring (pg, &add_actual_connections);
2384 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2387 _("Creating small world (2d-torus) topology\n"));
2389 create_small_world (pg, &add_actual_connections);
2391 case GNUNET_TESTING_TOPOLOGY_RING:
2393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2394 _("Creating ring topology\n"));
2396 create_ring (pg, &add_actual_connections);
2398 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2401 _("Creating 2d torus topology\n"));
2403 create_2d_torus (pg, &add_actual_connections);
2405 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2408 _("Creating Erdos-Renyi topology\n"));
2410 create_erdos_renyi (pg, &add_actual_connections);
2412 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2415 _("Creating InterNAT topology\n"));
2417 create_nated_internet (pg, &add_actual_connections);
2419 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2422 _("Creating Scale Free topology\n"));
2424 create_scale_free (pg, &add_actual_connections);
2426 case GNUNET_TESTING_TOPOLOGY_NONE:
2427 copy_allowed_topology(pg);
2430 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2431 _("Unknown topology specification, can't connect peers!\n"));
2432 return GNUNET_SYSERR;
2437 case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
2438 choose_random_connections(pg, option_modifier);
2440 case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
2441 choose_minimum(pg, (unsigned int)option_modifier);
2443 case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
2444 perform_dfs(pg, (int)option_modifier);
2446 case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
2448 case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
2454 return connect_topology(pg);
2458 * Function which continues a peer group starting up
2459 * after successfully generating hostkeys for each peer.
2461 * @param pg the peer group to continue starting
2465 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
2469 for (i = 0; i < pg->total; i++)
2471 GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
2476 * Start count gnunetd processes with the same set of transports and
2477 * applications. The port numbers (any option called "PORT") will be
2478 * adjusted to ensure that no two peers running on the same system
2479 * have the same port(s) in their respective configurations.
2481 * @param sched scheduler to use
2482 * @param cfg configuration template to use
2483 * @param total number of daemons to start
2484 * @param timeout total time allowed for peers to start
2485 * @param hostkey_callback function to call on each peers hostkey generation
2486 * if NULL, peers will be started by this call, if non-null,
2487 * GNUNET_TESTING_daemons_continue_startup must be called after
2488 * successful hostkey generation
2489 * @param hostkey_cls closure for hostkey callback
2490 * @param cb function to call on each daemon that was started
2491 * @param cb_cls closure for cb
2492 * @param connect_callback function to call each time two hosts are connected
2493 * @param connect_callback_cls closure for connect_callback
2494 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
2495 * everything on localhost).
2496 * @return NULL on error, otherwise handle to control peer group
2498 struct GNUNET_TESTING_PeerGroup *
2499 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
2500 const struct GNUNET_CONFIGURATION_Handle *cfg,
2502 struct GNUNET_TIME_Relative timeout,
2503 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
2505 GNUNET_TESTING_NotifyDaemonRunning cb,
2507 GNUNET_TESTING_NotifyConnection
2508 connect_callback, void *connect_callback_cls,
2509 const char *hostnames)
2511 struct GNUNET_TESTING_PeerGroup *pg;
2515 const char *hostname;
2516 char *baseservicehome;
2517 char *newservicehome;
2519 struct GNUNET_CONFIGURATION_Handle *pcfg;
2521 unsigned int hostcnt;
2531 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
2535 pg->cb_cls = cb_cls;
2536 pg->notify_connection = connect_callback;
2537 pg->notify_connection_cls = connect_callback_cls;
2539 pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
2540 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
2541 if (NULL != hostnames)
2544 /* skip leading spaces */
2545 while ((0 != *hostnames) && (isspace (*hostnames)))
2548 while ('\0' != *rpos)
2550 if (isspace (*rpos))
2554 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
2556 start = GNUNET_strdup (hostnames);
2558 while ('\0' != *pos)
2563 if (strlen (start) > 0)
2565 pg->hosts[off].minport = LOW_PORT;
2566 pg->hosts[off++].hostname = start;
2572 if (strlen (start) > 0)
2574 pg->hosts[off].minport = LOW_PORT;
2575 pg->hosts[off++].hostname = start;
2579 GNUNET_free (start);
2580 GNUNET_free (pg->hosts);
2584 minport = 0; /* make gcc happy */
2591 for (off = 0; off < total; off++)
2595 hostname = pg->hosts[off % hostcnt].hostname;
2596 pcfg = make_config (cfg,
2597 &pg->hosts[off % hostcnt].minport,
2604 pcfg = make_config (cfg,
2612 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2614 ("Could not create configuration for peer number %u on `%s'!\n"),
2615 off, hostname == NULL ? "localhost" : hostname);
2620 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
2623 GNUNET_asprintf (&newservicehome,
2624 "%s/%d/", baseservicehome, off);
2625 GNUNET_free (baseservicehome);
2629 tmpdir = getenv ("TMPDIR");
2630 tmpdir = tmpdir ? tmpdir : "/tmp";
2631 GNUNET_asprintf (&newservicehome,
2634 "gnunet-testing-test-test", off);
2636 GNUNET_CONFIGURATION_set_value_string (pcfg,
2638 "SERVICEHOME", newservicehome);
2639 GNUNET_free (newservicehome);
2640 pg->peers[off].cfg = pcfg;
2641 pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
2642 pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
2643 pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
2644 pg->peers[off].pg = pg;
2645 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (sched,
2652 if (NULL == pg->peers[off].daemon)
2653 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2654 _("Could not start peer number %u!\n"), off);
2661 * Get a daemon by number, so callers don't have to do nasty
2662 * offsetting operation.
2664 struct GNUNET_TESTING_Daemon *
2665 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
2667 if (position < pg->total)
2668 return pg->peers[position].daemon;
2674 * Prototype of a function that will be called when a
2675 * particular operation was completed the testing library.
2677 * @param cls closure (a struct RestartContext)
2678 * @param id id of the peer that was restarted
2679 * @param cfg handle to the configuration of the peer
2680 * @param d handle to the daemon that was restarted
2681 * @param emsg NULL on success
2683 void restart_callback (void *cls,
2684 const struct GNUNET_PeerIdentity *id,
2685 const struct GNUNET_CONFIGURATION_Handle *cfg,
2686 struct GNUNET_TESTING_Daemon *d,
2689 struct RestartContext *restart_context = cls;
2693 restart_context->peers_restarted++;
2697 restart_context->peers_restart_failed++;
2700 if (restart_context->peers_restarted == restart_context->peer_group->total)
2702 restart_context->callback(restart_context->callback_cls, NULL);
2703 GNUNET_free(restart_context);
2705 else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
2707 restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
2708 GNUNET_free(restart_context);
2714 * Callback for informing us about a successful
2715 * or unsuccessful churn stop call.
2717 * @param cls a ChurnContext
2718 * @param emsg NULL on success, non-NULL on failure
2722 churn_stop_callback (void *cls, const char *emsg)
2724 struct ChurnContext *churn_ctx = cls;
2725 unsigned int total_left;
2726 char *error_message;
2728 error_message = NULL;
2731 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2732 "Churn stop callback failed with error `%s'\n", emsg);
2733 churn_ctx->num_failed_stop++;
2737 churn_ctx->num_to_stop--;
2741 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2742 "Stopped peer, %d left.\n",
2743 churn_ctx->num_to_stop);
2745 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
2747 if (total_left == 0)
2749 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
2751 GNUNET_asprintf(&error_message,
2752 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
2753 churn_ctx->num_failed_start,
2754 churn_ctx->num_failed_stop);
2756 churn_ctx->cb(churn_ctx->cb_cls, error_message);
2757 GNUNET_free_non_null(error_message);
2758 GNUNET_free(churn_ctx);
2763 * Callback for informing us about a successful
2764 * or unsuccessful churn start call.
2766 * @param cls a ChurnContext
2767 * @param id the peer identity of the started peer
2768 * @param cfg the handle to the configuration of the peer
2769 * @param d handle to the daemon for the peer
2770 * @param emsg NULL on success, non-NULL on failure
2774 churn_start_callback (void *cls,
2775 const struct GNUNET_PeerIdentity *id,
2776 const struct GNUNET_CONFIGURATION_Handle *cfg,
2777 struct GNUNET_TESTING_Daemon *d,
2780 struct ChurnContext *churn_ctx = cls;
2781 unsigned int total_left;
2782 char *error_message;
2784 error_message = NULL;
2787 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2788 "Churn stop callback failed with error `%s'\n",
2790 churn_ctx->num_failed_start++;
2794 churn_ctx->num_to_start--;
2798 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2799 "Started peer, %d left.\n",
2800 churn_ctx->num_to_start);
2803 total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
2805 if (total_left == 0)
2807 if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
2808 GNUNET_asprintf(&error_message,
2809 "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
2810 churn_ctx->num_failed_start,
2811 churn_ctx->num_failed_stop);
2812 churn_ctx->cb(churn_ctx->cb_cls, error_message);
2813 GNUNET_free_non_null(error_message);
2814 GNUNET_free(churn_ctx);
2820 * Simulate churn by stopping some peers (and possibly
2821 * re-starting others if churn is called multiple times). This
2822 * function can only be used to create leave-join churn (peers "never"
2823 * leave for good). First "voff" random peers that are currently
2824 * online will be taken offline; then "von" random peers that are then
2825 * offline will be put back online. No notifications will be
2826 * generated for any of these operations except for the callback upon
2829 * @param pg handle for the peer group
2830 * @param voff number of peers that should go offline
2831 * @param von number of peers that should come back online;
2832 * must be zero on first call (since "testbed_start"
2833 * always starts all of the peers)
2834 * @param timeout how long to wait for operations to finish before
2836 * @param cb function to call at the end
2837 * @param cb_cls closure for cb
2840 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
2843 struct GNUNET_TIME_Relative timeout,
2844 GNUNET_TESTING_NotifyCompletion cb,
2847 struct ChurnContext *churn_ctx;
2848 unsigned int running;
2849 unsigned int stopped;
2851 unsigned int *running_arr;
2852 unsigned int *stopped_arr;
2853 unsigned int *running_permute;
2854 unsigned int *stopped_permute;
2859 if ((von == 0) && (voff == 0)) /* No peers at all? */
2865 for (i = 0; i < pg->total; i++)
2867 if (pg->peers[i].daemon->running == GNUNET_YES)
2879 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
2880 cb(cb_cls, "Trying to stop more peers than are currently running!");
2886 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
2887 cb(cb_cls, "Trying to start more peers than are currently stopped!");
2891 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
2892 running_arr = GNUNET_malloc(running * sizeof(unsigned int));
2893 stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
2895 running_permute = NULL;
2896 stopped_permute = NULL;
2899 running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
2901 stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
2906 churn_ctx->num_to_start = von;
2907 churn_ctx->num_to_stop = voff;
2909 churn_ctx->cb_cls = cb_cls;
2911 for (i = 0; i < pg->total; i++)
2913 if (pg->peers[i].daemon->running == GNUNET_YES)
2915 running_arr[running] = i;
2920 stopped_arr[stopped] = i;
2925 for (i = 0; i < voff; i++)
2928 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
2930 GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
2932 &churn_stop_callback, churn_ctx,
2933 GNUNET_NO, GNUNET_YES);
2936 for (i = 0; i < von; i++)
2939 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
2941 GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
2942 timeout, &churn_start_callback, churn_ctx);
2945 GNUNET_free(running_arr);
2946 GNUNET_free(stopped_arr);
2947 GNUNET_free_non_null(running_permute);
2948 GNUNET_free_non_null(stopped_permute);
2953 * Restart all peers in the given group.
2955 * @param pg the handle to the peer group
2956 * @param callback function to call on completion (or failure)
2957 * @param callback_cls closure for the callback function
2960 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyCompletion callback, void *callback_cls)
2962 struct RestartContext *restart_context;
2967 restart_context = GNUNET_malloc(sizeof(struct RestartContext));
2968 restart_context->peer_group = pg;
2969 restart_context->peers_restarted = 0;
2970 restart_context->callback = callback;
2971 restart_context->callback_cls = callback_cls;
2973 for (off = 0; off < pg->total; off++)
2975 GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
2981 * Start or stop an individual peer from the given group.
2983 * @param pg handle to the peer group
2984 * @param offset which peer to start or stop
2985 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
2986 * @param timeout how long to wait for shutdown
2987 * @param cb function to call at the end
2988 * @param cb_cls closure for cb
2991 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
2992 unsigned int offset,
2994 struct GNUNET_TIME_Relative timeout,
2995 GNUNET_TESTING_NotifyCompletion cb,
2998 struct ChurnContext *churn_ctx;
3000 if (GNUNET_NO == desired_status)
3002 if (NULL != pg->peers[offset].daemon)
3004 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3005 churn_ctx->num_to_start = 0;
3006 churn_ctx->num_to_stop = 1;
3008 churn_ctx->cb_cls = cb_cls;
3009 GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
3010 timeout, &churn_stop_callback, churn_ctx,
3011 GNUNET_NO, GNUNET_YES);
3014 else if (GNUNET_YES == desired_status)
3016 if (NULL == pg->peers[offset].daemon)
3018 churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
3019 churn_ctx->num_to_start = 1;
3020 churn_ctx->num_to_stop = 0;
3022 churn_ctx->cb_cls = cb_cls;
3023 GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
3024 timeout, &churn_start_callback, churn_ctx);
3033 * Shutdown all peers started in the given group.
3035 * @param pg handle to the peer group
3036 * @param timeout how long to wait for shutdown
3039 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
3040 struct GNUNET_TIME_Relative timeout)
3044 for (off = 0; off < pg->total; off++)
3046 /* FIXME: should we wait for our continuations to be called
3047 here? This would require us to take a continuation as
3050 if (NULL != pg->peers[off].daemon)
3051 GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, NULL, NULL, GNUNET_YES, GNUNET_NO);
3052 if (NULL != pg->peers[off].cfg)
3053 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
3054 if (pg->peers[off].allowed_peers != NULL)
3055 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
3056 if (pg->peers[off].connect_peers != NULL)
3057 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
3058 if (pg->peers[off].blacklisted_peers != NULL)
3059 GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
3061 GNUNET_free (pg->peers);
3062 if (NULL != pg->hosts)
3064 GNUNET_free (pg->hosts[0].hostname);
3065 GNUNET_free (pg->hosts);
3071 /* end of testing_group.c */