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_YES
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, 180)
56 struct PeerConnection *next;
59 * Pointer to daemon handle
61 struct GNUNET_TESTING_Daemon *daemon;
66 * Data we keep per peer.
71 * (Initial) configuration of the host.
72 * (initial because clients could change
73 * it and we would not know about those
76 struct GNUNET_CONFIGURATION_Handle *cfg;
79 * Handle for controlling the daemon.
81 struct GNUNET_TESTING_Daemon *daemon;
84 * Linked list of peer connections (simply indexes of PeerGroup)
85 * FIXME: Question, store pointer or integer? Pointer for now...
87 struct PeerConnection *connected_peers;
92 * Data we keep per host.
102 * Lowest port that we have not yet used
110 * Handle to a group of GNUnet peers.
112 struct GNUNET_TESTING_PeerGroup
117 struct GNUNET_SCHEDULER_Handle *sched;
120 * Configuration template.
122 const struct GNUNET_CONFIGURATION_Handle *cfg;
125 * Function to call on each started daemon.
127 GNUNET_TESTING_NotifyDaemonRunning cb;
135 * Function to call on each topology connection created
137 GNUNET_TESTING_NotifyConnection notify_connection;
140 * Callback for notify_connection
142 void *notify_connection_cls;
145 * NULL-terminated array of information about
148 struct HostData *hosts;
151 * Array of "total" peers.
153 struct PeerData *peers;
156 * Number of peers in this group.
165 struct GNUNET_CONFIGURATION_Handle *ret;
167 const char *hostname;
171 struct ConnectContext
173 struct GNUNET_TESTING_Daemon *first;
175 struct GNUNET_TESTING_Daemon *second;
177 struct GNUNET_TESTING_PeerGroup *pg;
181 * Number of connects we are waiting on, allows us to rate limit
184 static int outstanding_connects;
188 * Function to iterate over options. Copies
189 * the options to the target configuration,
190 * updating PORT values as needed.
193 * @param section name of the section
194 * @param option name of the option
195 * @param value value of the option
198 update_config (void *cls,
199 const char *section, const char *option, const char *value)
201 struct UpdateContext *ctx = cls;
205 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
207 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
211 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
213 value = ctx->hostname;
216 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
221 * Create a new configuration using the given configuration
222 * as a template; however, each PORT in the existing cfg
223 * must be renumbered by incrementing "*port". If we run
224 * out of "*port" numbers, return NULL.
226 * @param cfg template configuration
227 * @param port port numbers to use, update to reflect
228 * port numbers that were used
229 * @return new configuration, NULL on error
231 static struct GNUNET_CONFIGURATION_Handle *
232 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg, uint16_t * port, const char *hostname)
234 struct UpdateContext uc;
241 uc.ret = GNUNET_CONFIGURATION_create ();
242 uc.hostname = hostname;
244 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
245 if (uc.nport >= HIGH_PORT)
248 GNUNET_CONFIGURATION_destroy (uc.ret);
252 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
254 GNUNET_asprintf(&allowed_hosts, "%s 127.0.0.1;", control_host);
255 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
256 GNUNET_free_non_null(control_host);
257 GNUNET_free(allowed_hosts);
261 /* arm needs to know to allow connections from the host on which it is running,
262 * otherwise gnunet-arm is unable to connect to it in some instances */
263 if (hostname != NULL)
265 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
266 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
267 GNUNET_free(allowed_hosts);
270 *port = (uint16_t) uc.nport;
275 * Add entries to the peers connected list
277 * @param pg the peer group we are working with
278 * @param first index of the first peer
279 * @param second index of the second peer
281 * @return the number of connections added (can be 0, 1 or 2)
283 * FIXME: add both, or only add one?
284 * - if both are added, then we have to keep track
285 * when connecting so we don't double connect
286 * - if only one is added, we need to iterate over
287 * both lists to find out if connection already exists
288 * - having both allows the whitelisting/friend file
289 * creation to be easier
291 * -- For now, add both, we have to iterate over each to
292 * check for duplicates anyways, so we'll take the performance
293 * hit assuming we don't have __too__ many connections
297 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
300 struct PeerConnection *first_iter;
301 struct PeerConnection *second_iter;
304 struct PeerConnection *new_first;
305 struct PeerConnection *new_second;
307 first_iter = pg->peers[first].connected_peers;
308 add_first = GNUNET_YES;
309 while (first_iter != NULL)
311 if (first_iter->daemon == pg->peers[second].daemon)
312 add_first = GNUNET_NO;
313 first_iter = first_iter->next;
316 second_iter = pg->peers[second].connected_peers;
317 add_second = GNUNET_YES;
318 while (second_iter != NULL)
320 if (second_iter->daemon == pg->peers[first].daemon)
321 add_second = GNUNET_NO;
322 second_iter = second_iter->next;
328 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
329 new_first->daemon = pg->peers[second].daemon;
330 new_first->next = pg->peers[first].connected_peers;
331 pg->peers[first].connected_peers = new_first;
337 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
338 new_second->daemon = pg->peers[first].daemon;
339 new_second->next = pg->peers[second].connected_peers;
340 pg->peers[second].connected_peers = new_second;
348 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg)
353 unsigned int randomPeer;
354 double random, logNModifier, percentage;
355 unsigned int smallWorldConnections;
361 int connect_attempts;
363 logNModifier = 0.5; /* FIXME: default value? */
364 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
369 if (sscanf(p_string, "%lf", &logNModifier) != 1)
370 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
371 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
375 GNUNET_free (p_string);
377 percentage = 0.5; /* FIXME: default percentage? */
378 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
383 if (sscanf(p_string, "%lf", &percentage) != 1)
384 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
385 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
389 GNUNET_free (p_string);
391 natLog = log (pg->total);
392 connsPerPeer = ceil (natLog * logNModifier);
394 if (connsPerPeer % 2 == 1)
397 smallWorldConnections = 0;
398 connect_attempts = 0;
399 for (i = 0; i < pg->total; i++)
402 max = i + connsPerPeer / 2;
403 min = i - connsPerPeer / 2;
405 if (max > pg->total - 1)
407 max = max - pg->total;
413 min = pg->total - 1 + min;
417 for (j = 0; j < connsPerPeer / 2; j++)
419 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
420 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
421 if (random < percentage)
423 /* Connect to uniformly selected random peer */
425 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
427 while ((((randomPeer < max) && (randomPeer > min))
428 && (useAnd == 0)) || (((randomPeer > min)
429 || (randomPeer < max))
433 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
436 smallWorldConnections +=
437 add_connections (pg, i, randomPeer);
441 nodeToConnect = i + j + 1;
442 if (nodeToConnect > pg->total - 1)
444 nodeToConnect = nodeToConnect - pg->total;
447 add_connections (pg, i, nodeToConnect);
453 connect_attempts += smallWorldConnections;
455 return connect_attempts;
460 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg)
462 unsigned int outer_count, inner_count;
464 int connect_attempts;
465 double nat_percentage;
468 nat_percentage = 0.6; /* FIXME: default percentage? */
469 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
474 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
475 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
476 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
480 GNUNET_free (p_string);
485 cutoff = (unsigned int) (nat_percentage * pg->total);
487 connect_attempts = 0;
489 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
491 for (inner_count = outer_count + 1; inner_count < pg->total;
494 if ((outer_count > cutoff) || (inner_count > cutoff))
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Connecting peer %d to peer %d\n",
499 outer_count, inner_count);
501 connect_attempts += add_connections(pg, outer_count, inner_count);
506 return connect_attempts;
513 create_small_world (struct GNUNET_TESTING_PeerGroup *pg)
515 unsigned int i, j, k;
519 unsigned int toggle = 1;
520 unsigned int nodeToConnect;
522 unsigned int node1Row;
523 unsigned int node1Col;
524 unsigned int node2Row;
525 unsigned int node2Col;
526 unsigned int distance;
527 double probability, random, percentage;
528 unsigned int smallWorldConnections;
530 int connect_attempts;
531 square = floor (sqrt (pg->total));
535 percentage = 0.5; /* FIXME: default percentage? */
536 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
541 if (sscanf(p_string, "%lf", &percentage) != 1)
542 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
543 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
547 GNUNET_free (p_string);
549 probability = 0.5; /* FIXME: default percentage? */
550 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
555 if (sscanf(p_string, "%lf", &probability) != 1)
556 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
557 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
561 GNUNET_free (p_string);
563 if (square * square != pg->total)
565 while (rows * cols < pg->total)
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
577 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
581 connect_attempts = 0;
582 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
583 * to the node to its right and above. Once this is over, we'll have our torus!
584 * Special case for the last node (if the rows and columns are not equal), connect
585 * to the first in the row to maintain topology.
587 for (i = 0; i < pg->total; i++)
589 /* First connect to the node to the right */
590 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
591 nodeToConnect = i + 1;
592 else if (i + 1 == pg->total)
593 nodeToConnect = rows * cols - cols;
595 nodeToConnect = i - cols + 1;
597 connect_attempts += add_connections (pg, i, nodeToConnect);
600 nodeToConnect = (rows * cols) - cols + i;
602 nodeToConnect = i - cols;
604 if (nodeToConnect < pg->total)
605 connect_attempts += add_connections (pg, i, nodeToConnect);
607 natLog = log (pg->total);
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
610 _("natural log of %d is %d, will run %d iterations\n"),
611 pg->total, natLog, (int) (natLog * percentage));
612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
614 smallWorldConnections = 0;
615 for (i = 0; i < (int) (natLog * percentage); i++)
617 for (j = 0; j < pg->total; j++)
619 /* Determine the row and column of node at position j on the 2d torus */
621 node1Col = j - (node1Row * cols);
622 for (k = 0; k < pg->total; k++)
624 /* Determine the row and column of node at position k on the 2d torus */
626 node2Col = k - (node2Row * cols);
627 /* Simple Cartesian distance */
628 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
631 /* Calculate probability as 1 over the square of the distance */
632 probability = 1.0 / (distance * distance);
633 /* Choose a random value between 0 and 1 */
634 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
635 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
636 /* If random < probability, then connect the two nodes */
637 if (random < probability)
638 smallWorldConnections += add_connections (pg, j, k);
644 connect_attempts += smallWorldConnections;
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
647 _("Total connections added for small world: %d!\n"),
648 smallWorldConnections);
650 return connect_attempts;
656 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg)
659 unsigned int outer_count;
660 unsigned int inner_count;
661 int connect_attempts;
665 probability = 0.5; /* FIXME: default percentage? */
666 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
671 if (sscanf(p_string, "%lf", &probability) != 1)
672 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
673 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
677 GNUNET_free (p_string);
679 connect_attempts = 0;
680 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
682 for (inner_count = outer_count + 1; inner_count < pg->total;
685 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
686 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689 _("rand is %f probability is %f\n"), temp_rand,
692 if (temp_rand < probability)
694 connect_attempts += add_connections (pg, outer_count, inner_count);
699 return connect_attempts;
703 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg)
709 unsigned int toggle = 1;
710 unsigned int nodeToConnect;
711 int connect_attempts;
713 connect_attempts = 0;
715 square = floor (sqrt (pg->total));
719 if (square * square != pg->total)
721 while (rows * cols < pg->total)
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
736 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
737 * to the node to its right and above. Once this is over, we'll have our torus!
738 * Special case for the last node (if the rows and columns are not equal), connect
739 * to the first in the row to maintain topology.
741 for (i = 0; i < pg->total; i++)
743 /* First connect to the node to the right */
744 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
745 nodeToConnect = i + 1;
746 else if (i + 1 == pg->total)
747 nodeToConnect = rows * cols - cols;
749 nodeToConnect = i - cols + 1;
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "Connecting peer %d to peer %d\n",
755 connect_attempts += add_connections(pg, i, nodeToConnect);
757 /* Second connect to the node immediately above */
759 nodeToConnect = (rows * cols) - cols + i;
761 nodeToConnect = i - cols;
763 if (nodeToConnect < pg->total)
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767 "Connecting peer %d to peer %d\n",
770 connect_attempts += add_connections(pg, i, nodeToConnect);
775 return connect_attempts;
781 create_clique (struct GNUNET_TESTING_PeerGroup *pg)
783 unsigned int outer_count;
784 unsigned int inner_count;
785 int connect_attempts;
787 connect_attempts = 0;
789 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
791 for (inner_count = outer_count + 1; inner_count < pg->total;
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
796 "Connecting peer %d to peer %d\n",
797 outer_count, inner_count);
799 connect_attempts += add_connections(pg, outer_count, inner_count);
803 return connect_attempts;
808 create_ring (struct GNUNET_TESTING_PeerGroup *pg)
811 int connect_attempts;
813 connect_attempts = 0;
815 /* Connect each peer to the next highest numbered peer */
816 for (count = 0; count < pg->total - 1; count++)
819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
820 "Connecting peer %d to peer %d\n",
823 connect_attempts += add_connections(pg, count, count + 1);
826 /* Connect the last peer to the first peer */
827 connect_attempts += add_connections(pg, pg->total - 1, 0);
829 return connect_attempts;
834 * Create the friend files based on the PeerConnection's
835 * of each peer in the peer group, and copy the files
836 * to the appropriate place
838 * @param pg the peer group we are dealing with
841 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
843 FILE *temp_friend_handle;
844 unsigned int pg_iter;
845 struct PeerConnection *connection_iter;
846 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
847 char *temp_service_path;
850 struct GNUNET_PeerIdentity *temppeer;
852 enum GNUNET_OS_ProcessStatusType type;
853 unsigned long return_code;
858 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
859 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
861 mytemp = GNUNET_DISK_mktemp("friends");
862 temp_friend_handle = fopen (mytemp, "wt");
863 connection_iter = pg->peers[pg_iter].connected_peers;
864 while (connection_iter != NULL)
866 temppeer = &connection_iter->daemon->id;
867 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
868 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
869 connection_iter = connection_iter->next;
872 fclose(temp_friend_handle);
875 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
877 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
878 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
881 if (UNLINK (mytemp) != 0)
882 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
883 GNUNET_free (mytemp);
887 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
889 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
890 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
891 "mv", mytemp, arg, NULL);
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
894 _("Copying file with command cp %s %s\n"), mytemp, arg);
899 else /* Remote, scp the file to the correct place */
901 if (NULL != pg->peers[pg_iter].daemon->username)
902 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
904 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
905 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
906 "scp", mytemp, arg, NULL);
909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
910 _("Copying file with command scp %s %s\n"), mytemp, arg);
914 GNUNET_free (temp_service_path);
915 GNUNET_free (mytemp);
920 while ((count < max_wait) && (ret != GNUNET_OK))
923 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
927 _("Checking copy status of file %d\n"), pg_iter);
929 if (pidarr[pg_iter] != 0) /* Check for already completed! */
931 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
935 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944 _("File %d copied\n"), pg_iter);
950 if (ret == GNUNET_SYSERR)
961 * Internal notification of a connection, kept so that we can ensure some connections
962 * happen instead of flooding all testing daemons with requests to connect.
964 static void internal_connect_notify (void *cls,
965 const struct GNUNET_PeerIdentity *first,
966 const struct GNUNET_PeerIdentity *second,
967 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
968 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
969 struct GNUNET_TESTING_Daemon *first_daemon,
970 struct GNUNET_TESTING_Daemon *second_daemon,
973 struct GNUNET_TESTING_PeerGroup *pg = cls;
974 outstanding_connects--;
976 pg->notify_connection(pg->notify_connection_cls, first, second, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
980 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
982 struct ConnectContext *connect_context = cls;
984 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
988 _("Delaying connect, we have too many outstanding connections!\n"));
990 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &schedule_connect, connect_context);
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
996 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
998 outstanding_connects++;
999 GNUNET_TESTING_daemons_connect (connect_context->first,
1000 connect_context->second,
1002 &internal_connect_notify,
1003 connect_context->pg);
1004 GNUNET_free(connect_context);
1009 * Connect the topology as specified by the PeerConnection's
1010 * of each peer in the peer group
1012 * @param pg the peer group we are dealing with
1015 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
1017 unsigned int pg_iter;
1018 struct PeerConnection *connection_iter;
1019 struct ConnectContext *connect_context;
1021 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1023 connection_iter = pg->peers[pg_iter].connected_peers;
1024 while (connection_iter != NULL)
1026 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1027 connect_context->pg = pg;
1028 connect_context->first = pg->peers[pg_iter].daemon;
1029 connect_context->second = connection_iter->daemon;
1031 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
1032 /*GNUNET_TESTING_daemons_connect (pg->peers[pg_iter].daemon,
1033 connection_iter->daemon,
1035 pg->notify_connection,
1036 pg->notify_connection_cls);*/
1037 connection_iter = connection_iter->next;
1039 /*if (outstanding_connects > MAX_OUTSTANDING_CONNECTS)
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1043 _("Sleeping to give peers a chance to connect!\n"));
1053 * Takes a peer group and attempts to create a topology based on the
1054 * one specified in the configuration file. Returns the number of connections
1055 * that will attempt to be created, but this will happen asynchronously(?) so
1056 * the caller will have to keep track (via the callback) of whether or not
1057 * the connection actually happened.
1059 * @param pg the peer group struct representing the running peers
1061 * @return the number of connections should be created by the topology, so the
1062 * caller knows how many to wait for (if it so chooses)
1066 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg)
1068 unsigned long long topology_num;
1070 int num_connections;
1072 GNUNET_assert (pg->notify_connection != NULL);
1075 GNUNET_CONFIGURATION_get_value_number (pg->cfg, "testing", "topology",
1078 switch (topology_num)
1080 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1083 _("Creating clique topology (may take a bit!)\n"));
1085 num_connections = create_clique (pg);
1087 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090 _("Creating small world (ring) topology (may take a bit!)\n"));
1092 num_connections = create_small_world_ring (pg);
1094 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
1096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1097 _("Creating small world (2d-torus) topology (may take a bit!)\n"));
1099 num_connections = create_small_world (pg);
1101 case GNUNET_TESTING_TOPOLOGY_RING:
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1104 _("Creating ring topology (may take a bit!)\n"));
1106 num_connections = create_ring (pg);
1108 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1111 _("Creating 2d torus topology (may take a bit!)\n"));
1113 num_connections = create_2d_torus (pg);
1115 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
1117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1118 _("Creating Erdos-Renyi topology (may take a bit!)\n"));
1120 num_connections = create_erdos_renyi (pg);
1122 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
1124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1125 _("Creating InterNAT topology (may take a bit!)\n"));
1127 num_connections = create_nated_internet (pg);
1129 case GNUNET_TESTING_TOPOLOGY_NONE:
1130 num_connections = 0;
1133 num_connections = 0;
1136 if (num_connections < 1)
1137 return GNUNET_SYSERR;
1139 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
1140 ret = create_and_copy_friend_files(pg);
1141 if (ret == GNUNET_OK)
1142 connect_topology(pg);
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 _("Failed during friend file copying!\n"));
1149 return GNUNET_SYSERR;
1154 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1155 _("No topology specified, was one intended?\n"));
1156 return GNUNET_SYSERR;
1159 return num_connections;
1163 * Start count gnunetd processes with the same set of transports and
1164 * applications. The port numbers (any option called "PORT") will be
1165 * adjusted to ensure that no two peers running on the same system
1166 * have the same port(s) in their respective configurations.
1168 * @param sched scheduler to use
1169 * @param cfg configuration template to use
1170 * @param total number of daemons to start
1171 * @param cb function to call on each daemon that was started
1172 * @param cb_cls closure for cb
1173 * @param connect_callback function to call each time two hosts are connected
1174 * @param connect_callback_cls closure for connect_callback
1175 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
1176 * everything on localhost).
1177 * @return NULL on error, otherwise handle to control peer group
1179 struct GNUNET_TESTING_PeerGroup *
1180 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
1181 const struct GNUNET_CONFIGURATION_Handle *cfg,
1183 GNUNET_TESTING_NotifyDaemonRunning cb,
1185 GNUNET_TESTING_NotifyConnection
1186 connect_callback, void *connect_callback_cls,
1187 const char *hostnames)
1189 struct GNUNET_TESTING_PeerGroup *pg;
1193 const char *hostname;
1194 char *baseservicehome;
1195 char *newservicehome;
1197 struct GNUNET_CONFIGURATION_Handle *pcfg;
1199 unsigned int hostcnt;
1208 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
1212 pg->cb_cls = cb_cls;
1213 pg->notify_connection = connect_callback;
1214 pg->notify_connection_cls = connect_callback_cls;
1216 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
1217 if (NULL != hostnames)
1220 /* skip leading spaces */
1221 while ((0 != *hostnames) && (isspace (*hostnames)))
1224 while ('\0' != *rpos)
1226 if (isspace (*rpos))
1230 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
1232 start = GNUNET_strdup (hostnames);
1234 while ('\0' != *pos)
1239 if (strlen (start) > 0)
1241 pg->hosts[off].minport = LOW_PORT;
1242 pg->hosts[off++].hostname = start;
1248 if (strlen (start) > 0)
1250 pg->hosts[off].minport = LOW_PORT;
1251 pg->hosts[off++].hostname = start;
1255 GNUNET_free (start);
1256 GNUNET_free (pg->hosts);
1260 minport = 0; /* make gcc happy */
1267 for (off = 0; off < total; off++)
1271 hostname = pg->hosts[off % hostcnt].hostname;
1272 pcfg = make_config (cfg, &pg->hosts[off % hostcnt].minport, hostname);
1277 pcfg = make_config (cfg, &minport, hostname);
1282 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1284 ("Could not create configuration for peer number %u on `%s'!\n"),
1285 off, hostname == NULL ? "localhost" : hostname);
1290 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
1293 GNUNET_asprintf (&newservicehome,
1294 "%s/%d/", baseservicehome, off);
1295 GNUNET_free (baseservicehome);
1299 tmpdir = getenv ("TMPDIR");
1300 tmpdir = tmpdir ? tmpdir : "/tmp";
1301 GNUNET_asprintf (&newservicehome,
1304 "gnunet-testing-test-test", off);
1306 GNUNET_CONFIGURATION_set_value_string (pcfg,
1308 "SERVICEHOME", newservicehome);
1309 GNUNET_free (newservicehome);
1310 pg->peers[off].cfg = pcfg;
1311 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (sched,
1315 if (NULL == pg->peers[off].daemon)
1316 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1317 _("Could not start peer number %u!\n"), off);
1323 * Get a daemon by number, so callers don't have to do nasty
1324 * offsetting operation.
1326 struct GNUNET_TESTING_Daemon *
1327 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
1329 if (position < pg->total)
1330 return pg->peers[position].daemon;
1336 * Shutdown all peers started in the given group.
1338 * @param pg handle to the peer group
1341 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg)
1344 struct PeerConnection *pos;
1345 struct PeerConnection *next;
1347 for (off = 0; off < pg->total; off++)
1349 /* FIXME: should we wait for our
1350 continuations to be called here? This
1351 would require us to take a continuation
1354 if (NULL != pg->peers[off].daemon)
1355 GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, NULL, NULL);
1356 if (NULL != pg->peers[off].cfg)
1357 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
1359 pos = pg->peers[off].connected_peers;
1368 GNUNET_free (pg->peers);
1369 if (NULL != pg->hosts)
1371 GNUNET_free (pg->hosts[0].hostname);
1372 GNUNET_free (pg->hosts);
1378 /* end of testing_group.c */