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
33 * Lowest port used for GNUnet testing. Should be high enough to not
34 * conflict with other applications running on the hosts but be low
35 * enough to not conflict with client-ports (typically starting around
38 #define LOW_PORT 10000
41 * Highest port used for GNUnet testing. Should be low enough to not
42 * conflict with the port range for "local" ports (client apps; see
43 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
45 #define HIGH_PORT 32000
47 #define MAX_OUTSTANDING_CONNECTIONS 50
49 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 160)
51 #define CONNECT_ATTEMPTS 8
58 struct PeerConnection *next;
61 * Pointer to daemon handle
63 struct GNUNET_TESTING_Daemon *daemon;
68 * Data we keep per peer.
73 * (Initial) configuration of the host.
74 * (initial because clients could change
75 * it and we would not know about those
78 struct GNUNET_CONFIGURATION_Handle *cfg;
81 * Handle for controlling the daemon.
83 struct GNUNET_TESTING_Daemon *daemon;
86 * Linked list of peer connections (simply indexes of PeerGroup)
87 * FIXME: Question, store pointer or integer? Pointer for now...
89 struct PeerConnection *connected_peers;
94 * Data we keep per host.
104 * Lowest port that we have not yet used
112 * Handle to a group of GNUnet peers.
114 struct GNUNET_TESTING_PeerGroup
119 struct GNUNET_SCHEDULER_Handle *sched;
122 * Configuration template.
124 const struct GNUNET_CONFIGURATION_Handle *cfg;
127 * Function to call on each started daemon.
129 GNUNET_TESTING_NotifyDaemonRunning cb;
137 * Function to call on each topology connection created
139 GNUNET_TESTING_NotifyConnection notify_connection;
142 * Callback for notify_connection
144 void *notify_connection_cls;
147 * NULL-terminated array of information about
150 struct HostData *hosts;
153 * Array of "total" peers.
155 struct PeerData *peers;
158 * Number of peers in this group.
167 struct GNUNET_CONFIGURATION_Handle *ret;
169 const char *hostname;
173 struct ConnectContext
175 struct GNUNET_TESTING_Daemon *first;
177 struct GNUNET_TESTING_Daemon *second;
179 struct GNUNET_TESTING_PeerGroup *pg;
183 * Number of connects we are waiting on, allows us to rate limit
186 static int outstanding_connects;
190 * Function to iterate over options. Copies
191 * the options to the target configuration,
192 * updating PORT values as needed.
195 * @param section name of the section
196 * @param option name of the option
197 * @param value value of the option
200 update_config (void *cls,
201 const char *section, const char *option, const char *value)
203 struct UpdateContext *ctx = cls;
207 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
209 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
213 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
215 value = ctx->hostname;
218 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
223 * Create a new configuration using the given configuration
224 * as a template; however, each PORT in the existing cfg
225 * must be renumbered by incrementing "*port". If we run
226 * out of "*port" numbers, return NULL.
228 * @param cfg template configuration
229 * @param port port numbers to use, update to reflect
230 * port numbers that were used
231 * @param hostname hostname of the controlling host, to allow control connections from
233 * @return new configuration, NULL on error
235 static struct GNUNET_CONFIGURATION_Handle *
236 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg, uint16_t * port, const char *hostname)
238 struct UpdateContext uc;
245 uc.ret = GNUNET_CONFIGURATION_create ();
246 uc.hostname = hostname;
248 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
249 if (uc.nport >= HIGH_PORT)
252 GNUNET_CONFIGURATION_destroy (uc.ret);
256 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
258 GNUNET_asprintf(&allowed_hosts, "%s 127.0.0.1;", control_host);
259 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
260 GNUNET_free_non_null(control_host);
261 GNUNET_free(allowed_hosts);
265 /* arm needs to know to allow connections from the host on which it is running,
266 * otherwise gnunet-arm is unable to connect to it in some instances */
267 if (hostname != NULL)
269 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
270 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
271 GNUNET_free(allowed_hosts);
274 *port = (uint16_t) uc.nport;
279 * Add entries to the peers connected list
281 * @param pg the peer group we are working with
282 * @param first index of the first peer
283 * @param second index of the second peer
285 * @return the number of connections added (can be 0, 1 or 2)
287 * FIXME: add both, or only add one?
288 * - if both are added, then we have to keep track
289 * when connecting so we don't double connect
290 * - if only one is added, we need to iterate over
291 * both lists to find out if connection already exists
292 * - having both allows the whitelisting/friend file
293 * creation to be easier
295 * -- For now, add both, we have to iterate over each to
296 * check for duplicates anyways, so we'll take the performance
297 * hit assuming we don't have __too__ many connections
301 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
304 struct PeerConnection *first_iter;
305 struct PeerConnection *second_iter;
308 struct PeerConnection *new_first;
309 struct PeerConnection *new_second;
311 first_iter = pg->peers[first].connected_peers;
312 add_first = GNUNET_YES;
313 while (first_iter != NULL)
315 if (first_iter->daemon == pg->peers[second].daemon)
316 add_first = GNUNET_NO;
317 first_iter = first_iter->next;
320 second_iter = pg->peers[second].connected_peers;
321 add_second = GNUNET_YES;
322 while (second_iter != NULL)
324 if (second_iter->daemon == pg->peers[first].daemon)
325 add_second = GNUNET_NO;
326 second_iter = second_iter->next;
332 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
333 new_first->daemon = pg->peers[second].daemon;
334 new_first->next = pg->peers[first].connected_peers;
335 pg->peers[first].connected_peers = new_first;
341 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
342 new_second->daemon = pg->peers[first].daemon;
343 new_second->next = pg->peers[second].connected_peers;
344 pg->peers[second].connected_peers = new_second;
352 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg)
357 unsigned int randomPeer;
358 double random, logNModifier, percentage;
359 unsigned int smallWorldConnections;
365 int connect_attempts;
367 logNModifier = 0.5; /* FIXME: default value? */
368 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
373 if (sscanf(p_string, "%lf", &logNModifier) != 1)
374 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
375 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
379 GNUNET_free (p_string);
381 percentage = 0.5; /* FIXME: default percentage? */
382 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
387 if (sscanf(p_string, "%lf", &percentage) != 1)
388 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
389 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
393 GNUNET_free (p_string);
395 natLog = log (pg->total);
396 connsPerPeer = ceil (natLog * logNModifier);
398 if (connsPerPeer % 2 == 1)
401 smallWorldConnections = 0;
402 connect_attempts = 0;
403 for (i = 0; i < pg->total; i++)
406 max = i + connsPerPeer / 2;
407 min = i - connsPerPeer / 2;
409 if (max > pg->total - 1)
411 max = max - pg->total;
417 min = pg->total - 1 + min;
421 for (j = 0; j < connsPerPeer / 2; j++)
423 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
424 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
425 if (random < percentage)
427 /* Connect to uniformly selected random peer */
429 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
431 while ((((randomPeer < max) && (randomPeer > min))
432 && (useAnd == 0)) || (((randomPeer > min)
433 || (randomPeer < max))
437 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
440 smallWorldConnections +=
441 add_connections (pg, i, randomPeer);
445 nodeToConnect = i + j + 1;
446 if (nodeToConnect > pg->total - 1)
448 nodeToConnect = nodeToConnect - pg->total;
451 add_connections (pg, i, nodeToConnect);
457 connect_attempts += smallWorldConnections;
459 return connect_attempts;
464 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg)
466 unsigned int outer_count, inner_count;
468 int connect_attempts;
469 double nat_percentage;
472 nat_percentage = 0.6; /* FIXME: default percentage? */
473 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
478 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
479 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
480 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
484 GNUNET_free (p_string);
489 cutoff = (unsigned int) (nat_percentage * pg->total);
491 connect_attempts = 0;
493 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
495 for (inner_count = outer_count + 1; inner_count < pg->total;
498 if ((outer_count > cutoff) || (inner_count > cutoff))
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
502 "Connecting peer %d to peer %d\n",
503 outer_count, inner_count);
505 connect_attempts += add_connections(pg, outer_count, inner_count);
510 return connect_attempts;
517 create_small_world (struct GNUNET_TESTING_PeerGroup *pg)
519 unsigned int i, j, k;
523 unsigned int toggle = 1;
524 unsigned int nodeToConnect;
526 unsigned int node1Row;
527 unsigned int node1Col;
528 unsigned int node2Row;
529 unsigned int node2Col;
530 unsigned int distance;
531 double probability, random, percentage;
532 unsigned int smallWorldConnections;
534 int connect_attempts;
535 square = floor (sqrt (pg->total));
539 percentage = 0.5; /* FIXME: default percentage? */
540 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
545 if (sscanf(p_string, "%lf", &percentage) != 1)
546 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
547 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
551 GNUNET_free (p_string);
553 probability = 0.5; /* FIXME: default percentage? */
554 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
559 if (sscanf(p_string, "%lf", &probability) != 1)
560 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
561 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
565 GNUNET_free (p_string);
567 if (square * square != pg->total)
569 while (rows * cols < pg->total)
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
581 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
585 connect_attempts = 0;
586 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
587 * to the node to its right and above. Once this is over, we'll have our torus!
588 * Special case for the last node (if the rows and columns are not equal), connect
589 * to the first in the row to maintain topology.
591 for (i = 0; i < pg->total; i++)
593 /* First connect to the node to the right */
594 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
595 nodeToConnect = i + 1;
596 else if (i + 1 == pg->total)
597 nodeToConnect = rows * cols - cols;
599 nodeToConnect = i - cols + 1;
601 connect_attempts += add_connections (pg, i, nodeToConnect);
604 nodeToConnect = (rows * cols) - cols + i;
606 nodeToConnect = i - cols;
608 if (nodeToConnect < pg->total)
609 connect_attempts += add_connections (pg, i, nodeToConnect);
611 natLog = log (pg->total);
612 #if VERBOSE_TESTING > 2
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
614 _("natural log of %d is %d, will run %d iterations\n"),
615 pg->total, natLog, (int) (natLog * percentage));
616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
618 smallWorldConnections = 0;
619 for (i = 0; i < (int) (natLog * percentage); i++)
621 for (j = 0; j < pg->total; j++)
623 /* Determine the row and column of node at position j on the 2d torus */
625 node1Col = j - (node1Row * cols);
626 for (k = 0; k < pg->total; k++)
628 /* Determine the row and column of node at position k on the 2d torus */
630 node2Col = k - (node2Row * cols);
631 /* Simple Cartesian distance */
632 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
635 /* Calculate probability as 1 over the square of the distance */
636 probability = 1.0 / (distance * distance);
637 /* Choose a random value between 0 and 1 */
638 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
639 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
640 /* If random < probability, then connect the two nodes */
641 if (random < probability)
642 smallWorldConnections += add_connections (pg, j, k);
648 connect_attempts += smallWorldConnections;
649 #if VERBOSE_TESTING > 2
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651 _("Total connections added for small world: %d!\n"),
652 smallWorldConnections);
654 return connect_attempts;
660 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg)
663 unsigned int outer_count;
664 unsigned int inner_count;
665 int connect_attempts;
669 probability = 0.5; /* FIXME: default percentage? */
670 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
675 if (sscanf(p_string, "%lf", &probability) != 1)
676 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
677 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
681 GNUNET_free (p_string);
683 connect_attempts = 0;
684 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
686 for (inner_count = outer_count + 1; inner_count < pg->total;
689 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
690 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
693 _("rand is %f probability is %f\n"), temp_rand,
696 if (temp_rand < probability)
698 connect_attempts += add_connections (pg, outer_count, inner_count);
703 return connect_attempts;
707 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg)
713 unsigned int toggle = 1;
714 unsigned int nodeToConnect;
715 int connect_attempts;
717 connect_attempts = 0;
719 square = floor (sqrt (pg->total));
723 if (square * square != pg->total)
725 while (rows * cols < pg->total)
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
737 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
740 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
741 * to the node to its right and above. Once this is over, we'll have our torus!
742 * Special case for the last node (if the rows and columns are not equal), connect
743 * to the first in the row to maintain topology.
745 for (i = 0; i < pg->total; i++)
747 /* First connect to the node to the right */
748 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
749 nodeToConnect = i + 1;
750 else if (i + 1 == pg->total)
751 nodeToConnect = rows * cols - cols;
753 nodeToConnect = i - cols + 1;
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 "Connecting peer %d to peer %d\n",
759 connect_attempts += add_connections(pg, i, nodeToConnect);
761 /* Second connect to the node immediately above */
763 nodeToConnect = (rows * cols) - cols + i;
765 nodeToConnect = i - cols;
767 if (nodeToConnect < pg->total)
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
771 "Connecting peer %d to peer %d\n",
774 connect_attempts += add_connections(pg, i, nodeToConnect);
779 return connect_attempts;
785 create_clique (struct GNUNET_TESTING_PeerGroup *pg)
787 unsigned int outer_count;
788 unsigned int inner_count;
789 int connect_attempts;
791 connect_attempts = 0;
793 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
795 for (inner_count = outer_count + 1; inner_count < pg->total;
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "Connecting peer %d to peer %d\n",
801 outer_count, inner_count);
803 connect_attempts += add_connections(pg, outer_count, inner_count);
807 return connect_attempts;
812 create_ring (struct GNUNET_TESTING_PeerGroup *pg)
815 int connect_attempts;
817 connect_attempts = 0;
819 /* Connect each peer to the next highest numbered peer */
820 for (count = 0; count < pg->total - 1; count++)
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
824 "Connecting peer %d to peer %d\n",
827 connect_attempts += add_connections(pg, count, count + 1);
830 /* Connect the last peer to the first peer */
831 connect_attempts += add_connections(pg, pg->total - 1, 0);
833 return connect_attempts;
838 * Create the friend files based on the PeerConnection's
839 * of each peer in the peer group, and copy the files
840 * to the appropriate place
842 * @param pg the peer group we are dealing with
845 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
847 FILE *temp_friend_handle;
848 unsigned int pg_iter;
849 struct PeerConnection *connection_iter;
850 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
851 char *temp_service_path;
854 struct GNUNET_PeerIdentity *temppeer;
856 enum GNUNET_OS_ProcessStatusType type;
857 unsigned long return_code;
862 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
863 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
865 mytemp = GNUNET_DISK_mktemp("friends");
866 temp_friend_handle = fopen (mytemp, "wt");
867 connection_iter = pg->peers[pg_iter].connected_peers;
868 while (connection_iter != NULL)
870 temppeer = &connection_iter->daemon->id;
871 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
872 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
873 connection_iter = connection_iter->next;
876 fclose(temp_friend_handle);
879 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
881 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
882 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
885 if (UNLINK (mytemp) != 0)
886 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
887 GNUNET_free (mytemp);
891 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
893 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
894 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
895 "mv", mytemp, arg, NULL);
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 _("Copying file with command cp %s %s\n"), mytemp, arg);
903 else /* Remote, scp the file to the correct place */
905 if (NULL != pg->peers[pg_iter].daemon->username)
906 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
908 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
909 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
910 "scp", mytemp, arg, NULL);
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
914 _("Copying file with command scp %s %s\n"), mytemp, arg);
918 GNUNET_free (temp_service_path);
919 GNUNET_free (mytemp);
924 while ((count < max_wait) && (ret != GNUNET_OK))
927 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
931 _("Checking copy status of file %d\n"), pg_iter);
933 if (pidarr[pg_iter] != 0) /* Check for already completed! */
935 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
939 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
948 _("File %d copied\n"), pg_iter);
954 if (ret == GNUNET_SYSERR)
965 * Internal notification of a connection, kept so that we can ensure some connections
966 * happen instead of flooding all testing daemons with requests to connect.
968 static void internal_connect_notify (void *cls,
969 const struct GNUNET_PeerIdentity *first,
970 const struct GNUNET_PeerIdentity *second,
971 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
972 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
973 struct GNUNET_TESTING_Daemon *first_daemon,
974 struct GNUNET_TESTING_Daemon *second_daemon,
977 struct GNUNET_TESTING_PeerGroup *pg = cls;
978 outstanding_connects--;
980 pg->notify_connection(pg->notify_connection_cls, first, second, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
984 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
986 struct ConnectContext *connect_context = cls;
988 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
991 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
993 #if VERBOSE_TESTING > 2
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 _("Delaying connect, we have too many outstanding connections!\n"));
997 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &schedule_connect, connect_context);
1001 #if VERBOSE_TESTING > 2
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1003 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
1005 outstanding_connects++;
1006 GNUNET_TESTING_daemons_connect (connect_context->first,
1007 connect_context->second,
1010 &internal_connect_notify,
1011 connect_context->pg);
1012 GNUNET_free(connect_context);
1017 * Connect the topology as specified by the PeerConnection's
1018 * of each peer in the peer group
1020 * @param pg the peer group we are dealing with
1023 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
1025 unsigned int pg_iter;
1026 struct PeerConnection *connection_iter;
1027 struct ConnectContext *connect_context;
1029 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1031 connection_iter = pg->peers[pg_iter].connected_peers;
1032 while (connection_iter != NULL)
1034 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1035 connect_context->pg = pg;
1036 connect_context->first = pg->peers[pg_iter].daemon;
1037 connect_context->second = connection_iter->daemon;
1038 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
1039 connection_iter = connection_iter->next;
1046 * Takes a peer group and attempts to create a topology based on the
1047 * one specified in the configuration file. Returns the number of connections
1048 * that will attempt to be created, but this will happen asynchronously(?) so
1049 * the caller will have to keep track (via the callback) of whether or not
1050 * the connection actually happened.
1052 * @param pg the peer group struct representing the running peers
1054 * @return the number of connections should be created by the topology, so the
1055 * caller knows how many to wait for (if it so chooses)
1059 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg)
1061 unsigned long long topology_num;
1063 int num_connections;
1065 GNUNET_assert (pg->notify_connection != NULL);
1068 GNUNET_CONFIGURATION_get_value_number (pg->cfg, "testing", "topology",
1071 switch (topology_num)
1073 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076 _("Creating clique topology (may take a bit!)\n"));
1078 num_connections = create_clique (pg);
1080 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1083 _("Creating small world (ring) topology (may take a bit!)\n"));
1085 num_connections = create_small_world_ring (pg);
1087 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090 _("Creating small world (2d-torus) topology (may take a bit!)\n"));
1092 num_connections = create_small_world (pg);
1094 case GNUNET_TESTING_TOPOLOGY_RING:
1096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1097 _("Creating ring topology (may take a bit!)\n"));
1099 num_connections = create_ring (pg);
1101 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1104 _("Creating 2d torus topology (may take a bit!)\n"));
1106 num_connections = create_2d_torus (pg);
1108 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1111 _("Creating Erdos-Renyi topology (may take a bit!)\n"));
1113 num_connections = create_erdos_renyi (pg);
1115 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
1117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1118 _("Creating InterNAT topology (may take a bit!)\n"));
1120 num_connections = create_nated_internet (pg);
1122 case GNUNET_TESTING_TOPOLOGY_NONE:
1123 num_connections = 0;
1126 num_connections = 0;
1129 if (num_connections < 1)
1130 return GNUNET_SYSERR;
1132 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
1133 ret = create_and_copy_friend_files(pg);
1134 if (ret == GNUNET_OK)
1135 connect_topology(pg);
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 _("Failed during friend file copying!\n"));
1142 return GNUNET_SYSERR;
1147 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1148 _("No topology specified, was one intended?\n"));
1149 return GNUNET_SYSERR;
1152 return num_connections;
1156 * Start count gnunetd processes with the same set of transports and
1157 * applications. The port numbers (any option called "PORT") will be
1158 * adjusted to ensure that no two peers running on the same system
1159 * have the same port(s) in their respective configurations.
1161 * @param sched scheduler to use
1162 * @param cfg configuration template to use
1163 * @param total number of daemons to start
1164 * @param cb function to call on each daemon that was started
1165 * @param cb_cls closure for cb
1166 * @param connect_callback function to call each time two hosts are connected
1167 * @param connect_callback_cls closure for connect_callback
1168 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
1169 * everything on localhost).
1170 * @return NULL on error, otherwise handle to control peer group
1172 struct GNUNET_TESTING_PeerGroup *
1173 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
1174 const struct GNUNET_CONFIGURATION_Handle *cfg,
1176 GNUNET_TESTING_NotifyDaemonRunning cb,
1178 GNUNET_TESTING_NotifyConnection
1179 connect_callback, void *connect_callback_cls,
1180 const char *hostnames)
1182 struct GNUNET_TESTING_PeerGroup *pg;
1186 const char *hostname;
1187 char *baseservicehome;
1188 char *newservicehome;
1190 struct GNUNET_CONFIGURATION_Handle *pcfg;
1192 unsigned int hostcnt;
1201 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
1205 pg->cb_cls = cb_cls;
1206 pg->notify_connection = connect_callback;
1207 pg->notify_connection_cls = connect_callback_cls;
1209 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
1210 if (NULL != hostnames)
1213 /* skip leading spaces */
1214 while ((0 != *hostnames) && (isspace (*hostnames)))
1217 while ('\0' != *rpos)
1219 if (isspace (*rpos))
1223 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
1225 start = GNUNET_strdup (hostnames);
1227 while ('\0' != *pos)
1232 if (strlen (start) > 0)
1234 pg->hosts[off].minport = LOW_PORT;
1235 pg->hosts[off++].hostname = start;
1241 if (strlen (start) > 0)
1243 pg->hosts[off].minport = LOW_PORT;
1244 pg->hosts[off++].hostname = start;
1248 GNUNET_free (start);
1249 GNUNET_free (pg->hosts);
1253 minport = 0; /* make gcc happy */
1260 for (off = 0; off < total; off++)
1264 hostname = pg->hosts[off % hostcnt].hostname;
1265 pcfg = make_config (cfg, &pg->hosts[off % hostcnt].minport, hostname);
1270 pcfg = make_config (cfg, &minport, hostname);
1275 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1277 ("Could not create configuration for peer number %u on `%s'!\n"),
1278 off, hostname == NULL ? "localhost" : hostname);
1283 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
1286 GNUNET_asprintf (&newservicehome,
1287 "%s/%d/", baseservicehome, off);
1288 GNUNET_free (baseservicehome);
1292 tmpdir = getenv ("TMPDIR");
1293 tmpdir = tmpdir ? tmpdir : "/tmp";
1294 GNUNET_asprintf (&newservicehome,
1297 "gnunet-testing-test-test", off);
1299 GNUNET_CONFIGURATION_set_value_string (pcfg,
1301 "SERVICEHOME", newservicehome);
1302 GNUNET_free (newservicehome);
1303 pg->peers[off].cfg = pcfg;
1304 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (sched,
1308 if (NULL == pg->peers[off].daemon)
1309 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1310 _("Could not start peer number %u!\n"), off);
1316 * Get a daemon by number, so callers don't have to do nasty
1317 * offsetting operation.
1319 struct GNUNET_TESTING_Daemon *
1320 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
1322 if (position < pg->total)
1323 return pg->peers[position].daemon;
1329 * Shutdown all peers started in the given group.
1331 * @param pg handle to the peer group
1334 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg)
1337 struct PeerConnection *pos;
1338 struct PeerConnection *next;
1340 for (off = 0; off < pg->total; off++)
1342 /* FIXME: should we wait for our
1343 continuations to be called here? This
1344 would require us to take a continuation
1347 if (NULL != pg->peers[off].daemon)
1348 GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, NULL, NULL);
1349 if (NULL != pg->peers[off].cfg)
1350 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
1352 pos = pg->peers[off].connected_peers;
1361 GNUNET_free (pg->peers);
1362 if (NULL != pg->hosts)
1364 GNUNET_free (pg->hosts[0].hostname);
1365 GNUNET_free (pg->hosts);
1371 /* end of testing_group.c */