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;
170 struct ConnectContext
172 struct GNUNET_TESTING_Daemon *first;
174 struct GNUNET_TESTING_Daemon *second;
176 struct GNUNET_TESTING_PeerGroup *pg;
180 * Number of connects we are waiting on, allows us to rate limit
183 static int outstanding_connects;
186 * Function to iterate over options. Copies
187 * the options to the target configuration,
188 * updating PORT values as needed.
191 * @param section name of the section
192 * @param option name of the option
193 * @param value value of the option
196 update_config (void *cls,
197 const char *section, const char *option, const char *value)
199 struct UpdateContext *ctx = cls;
203 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
205 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
208 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
213 * Create a new configuration using the given configuration
214 * as a template; however, each PORT in the existing cfg
215 * must be renumbered by incrementing "*port". If we run
216 * out of "*port" numbers, return NULL.
218 * @param cfg template configuration
219 * @param port port numbers to use, update to reflect
220 * port numbers that were used
221 * @return new configuration, NULL on error
223 static struct GNUNET_CONFIGURATION_Handle *
224 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg, uint16_t * port)
226 struct UpdateContext uc;
233 uc.ret = GNUNET_CONFIGURATION_create ();
235 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
236 if (uc.nport >= HIGH_PORT)
239 GNUNET_CONFIGURATION_destroy (uc.ret);
243 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
245 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
246 fprintf(stderr, "FOUND CONTROL_HOST OPTION %s, setting to %s\n", control_host, allowed_hosts);
247 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
248 GNUNET_free_non_null(control_host);
249 GNUNET_free(allowed_hosts);
252 *port = (uint16_t) uc.nport;
257 * Add entries to the peers connected list
259 * @param pg the peer group we are working with
260 * @param first index of the first peer
261 * @param second index of the second peer
263 * @return the number of connections added (can be 0, 1 or 2)
265 * FIXME: add both, or only add one?
266 * - if both are added, then we have to keep track
267 * when connecting so we don't double connect
268 * - if only one is added, we need to iterate over
269 * both lists to find out if connection already exists
270 * - having both allows the whitelisting/friend file
271 * creation to be easier
273 * -- For now, add both, we have to iterate over each to
274 * check for duplicates anyways, so we'll take the performance
275 * hit assuming we don't have __too__ many connections
279 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
282 struct PeerConnection *first_iter;
283 struct PeerConnection *second_iter;
286 struct PeerConnection *new_first;
287 struct PeerConnection *new_second;
289 first_iter = pg->peers[first].connected_peers;
290 add_first = GNUNET_YES;
291 while (first_iter != NULL)
293 if (first_iter->daemon == pg->peers[second].daemon)
294 add_first = GNUNET_NO;
295 first_iter = first_iter->next;
298 second_iter = pg->peers[second].connected_peers;
299 add_second = GNUNET_YES;
300 while (second_iter != NULL)
302 if (second_iter->daemon == pg->peers[first].daemon)
303 add_second = GNUNET_NO;
304 second_iter = second_iter->next;
310 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
311 new_first->daemon = pg->peers[second].daemon;
312 new_first->next = pg->peers[first].connected_peers;
313 pg->peers[first].connected_peers = new_first;
319 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
320 new_second->daemon = pg->peers[first].daemon;
321 new_second->next = pg->peers[second].connected_peers;
322 pg->peers[second].connected_peers = new_second;
330 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg)
335 unsigned int randomPeer;
336 double random, logNModifier, percentage;
337 unsigned int smallWorldConnections;
343 int connect_attempts;
345 logNModifier = 0.5; /* FIXME: default value? */
346 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
351 if (sscanf(p_string, "%lf", &logNModifier) != 1)
352 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
353 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
357 GNUNET_free (p_string);
359 percentage = 0.5; /* FIXME: default percentage? */
360 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
365 if (sscanf(p_string, "%lf", &percentage) != 1)
366 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
367 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
371 GNUNET_free (p_string);
373 natLog = log (pg->total);
374 connsPerPeer = ceil (natLog * logNModifier);
376 if (connsPerPeer % 2 == 1)
379 smallWorldConnections = 0;
380 connect_attempts = 0;
381 for (i = 0; i < pg->total; i++)
384 max = i + connsPerPeer / 2;
385 min = i - connsPerPeer / 2;
387 if (max > pg->total - 1)
389 max = max - pg->total;
395 min = pg->total - 1 + min;
399 for (j = 0; j < connsPerPeer / 2; j++)
401 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
402 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
403 if (random < percentage)
405 /* Connect to uniformly selected random peer */
407 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
409 while ((((randomPeer < max) && (randomPeer > min))
410 && (useAnd == 0)) || (((randomPeer > min)
411 || (randomPeer < max))
415 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
418 smallWorldConnections +=
419 add_connections (pg, i, randomPeer);
423 nodeToConnect = i + j + 1;
424 if (nodeToConnect > pg->total - 1)
426 nodeToConnect = nodeToConnect - pg->total;
429 add_connections (pg, i, nodeToConnect);
435 connect_attempts += smallWorldConnections;
437 return connect_attempts;
442 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg)
444 unsigned int outer_count, inner_count;
446 int connect_attempts;
447 double nat_percentage;
450 nat_percentage = 0.6; /* FIXME: default percentage? */
451 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
456 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
457 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
458 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
462 GNUNET_free (p_string);
467 cutoff = (unsigned int) (nat_percentage * pg->total);
469 connect_attempts = 0;
471 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
473 for (inner_count = outer_count + 1; inner_count < pg->total;
476 if ((outer_count > cutoff) || (inner_count > cutoff))
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Connecting peer %d to peer %d\n",
481 outer_count, inner_count);
483 connect_attempts += add_connections(pg, outer_count, inner_count);
488 return connect_attempts;
495 create_small_world (struct GNUNET_TESTING_PeerGroup *pg)
497 unsigned int i, j, k;
501 unsigned int toggle = 1;
502 unsigned int nodeToConnect;
504 unsigned int node1Row;
505 unsigned int node1Col;
506 unsigned int node2Row;
507 unsigned int node2Col;
508 unsigned int distance;
509 double probability, random, percentage;
510 unsigned int smallWorldConnections;
512 int connect_attempts;
513 square = floor (sqrt (pg->total));
517 percentage = 0.5; /* FIXME: default percentage? */
518 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
523 if (sscanf(p_string, "%lf", &percentage) != 1)
524 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
525 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
529 GNUNET_free (p_string);
531 probability = 0.5; /* FIXME: default percentage? */
532 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
537 if (sscanf(p_string, "%lf", &probability) != 1)
538 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
539 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
543 GNUNET_free (p_string);
545 if (square * square != pg->total)
547 while (rows * cols < pg->total)
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
563 connect_attempts = 0;
564 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
565 * to the node to its right and above. Once this is over, we'll have our torus!
566 * Special case for the last node (if the rows and columns are not equal), connect
567 * to the first in the row to maintain topology.
569 for (i = 0; i < pg->total; i++)
571 /* First connect to the node to the right */
572 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
573 nodeToConnect = i + 1;
574 else if (i + 1 == pg->total)
575 nodeToConnect = rows * cols - cols;
577 nodeToConnect = i - cols + 1;
579 connect_attempts += add_connections (pg, i, nodeToConnect);
582 nodeToConnect = (rows * cols) - cols + i;
584 nodeToConnect = i - cols;
586 if (nodeToConnect < pg->total)
587 connect_attempts += add_connections (pg, i, nodeToConnect);
589 natLog = log (pg->total);
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
592 _("natural log of %d is %d, will run %d iterations\n"),
593 pg->total, natLog, (int) (natLog * percentage));
594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
596 smallWorldConnections = 0;
597 for (i = 0; i < (int) (natLog * percentage); i++)
599 for (j = 0; j < pg->total; j++)
601 /* Determine the row and column of node at position j on the 2d torus */
603 node1Col = j - (node1Row * cols);
604 for (k = 0; k < pg->total; k++)
606 /* Determine the row and column of node at position k on the 2d torus */
608 node2Col = k - (node2Row * cols);
609 /* Simple Cartesian distance */
610 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
613 /* Calculate probability as 1 over the square of the distance */
614 probability = 1.0 / (distance * distance);
615 /* Choose a random value between 0 and 1 */
616 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
617 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
618 /* If random < probability, then connect the two nodes */
619 if (random < probability)
620 smallWorldConnections += add_connections (pg, j, k);
626 connect_attempts += smallWorldConnections;
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
629 _("Total connections added for small world: %d!\n"),
630 smallWorldConnections);
632 return connect_attempts;
638 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg)
641 unsigned int outer_count;
642 unsigned int inner_count;
643 int connect_attempts;
647 probability = 0.5; /* FIXME: default percentage? */
648 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
653 if (sscanf(p_string, "%lf", &probability) != 1)
654 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
655 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
659 GNUNET_free (p_string);
661 connect_attempts = 0;
662 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
664 for (inner_count = outer_count + 1; inner_count < pg->total;
667 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
668 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671 _("rand is %f probability is %f\n"), temp_rand,
674 if (temp_rand < probability)
676 connect_attempts += add_connections (pg, outer_count, inner_count);
681 return connect_attempts;
685 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg)
691 unsigned int toggle = 1;
692 unsigned int nodeToConnect;
693 int connect_attempts;
695 connect_attempts = 0;
697 square = floor (sqrt (pg->total));
701 if (square * square != pg->total)
703 while (rows * cols < pg->total)
714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
715 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
718 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
719 * to the node to its right and above. Once this is over, we'll have our torus!
720 * Special case for the last node (if the rows and columns are not equal), connect
721 * to the first in the row to maintain topology.
723 for (i = 0; i < pg->total; i++)
725 /* First connect to the node to the right */
726 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
727 nodeToConnect = i + 1;
728 else if (i + 1 == pg->total)
729 nodeToConnect = rows * cols - cols;
731 nodeToConnect = i - cols + 1;
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
734 "Connecting peer %d to peer %d\n",
737 connect_attempts += add_connections(pg, i, nodeToConnect);
739 /* Second connect to the node immediately above */
741 nodeToConnect = (rows * cols) - cols + i;
743 nodeToConnect = i - cols;
745 if (nodeToConnect < pg->total)
748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
749 "Connecting peer %d to peer %d\n",
752 connect_attempts += add_connections(pg, i, nodeToConnect);
757 return connect_attempts;
763 create_clique (struct GNUNET_TESTING_PeerGroup *pg)
765 unsigned int outer_count;
766 unsigned int inner_count;
767 int connect_attempts;
769 connect_attempts = 0;
771 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
773 for (inner_count = outer_count + 1; inner_count < pg->total;
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
778 "Connecting peer %d to peer %d\n",
779 outer_count, inner_count);
781 connect_attempts += add_connections(pg, outer_count, inner_count);
785 return connect_attempts;
790 create_ring (struct GNUNET_TESTING_PeerGroup *pg)
793 int connect_attempts;
795 connect_attempts = 0;
797 /* Connect each peer to the next highest numbered peer */
798 for (count = 0; count < pg->total - 1; count++)
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "Connecting peer %d to peer %d\n",
805 connect_attempts += add_connections(pg, count, count + 1);
808 /* Connect the last peer to the first peer */
809 connect_attempts += add_connections(pg, pg->total - 1, 0);
811 return connect_attempts;
816 * Create the friend files based on the PeerConnection's
817 * of each peer in the peer group, and copy the files
818 * to the appropriate place
820 * @param pg the peer group we are dealing with
823 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
825 FILE *temp_friend_handle;
826 unsigned int pg_iter;
827 struct PeerConnection *connection_iter;
828 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
829 char *temp_service_path;
832 struct GNUNET_PeerIdentity *temppeer;
834 enum GNUNET_OS_ProcessStatusType type;
835 unsigned long return_code;
840 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
841 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
843 mytemp = GNUNET_DISK_mktemp("friends");
844 temp_friend_handle = fopen (mytemp, "wt");
845 connection_iter = pg->peers[pg_iter].connected_peers;
846 while (connection_iter != NULL)
848 temppeer = &connection_iter->daemon->id;
849 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
850 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
851 connection_iter = connection_iter->next;
854 fclose(temp_friend_handle);
857 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
859 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
860 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
863 if (UNLINK (mytemp) != 0)
864 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
865 GNUNET_free (mytemp);
869 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
871 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
872 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
873 "mv", mytemp, arg, NULL);
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 _("Copying file with command cp %s %s\n"), mytemp, arg);
881 else /* Remote, scp the file to the correct place */
883 if (NULL != pg->peers[pg_iter].daemon->username)
884 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
886 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
887 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
888 "scp", mytemp, arg, NULL);
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892 _("Copying file with command scp %s %s\n"), mytemp, arg);
896 GNUNET_free (temp_service_path);
897 GNUNET_free (mytemp);
902 while ((count < max_wait) && (ret != GNUNET_OK))
905 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
909 _("Checking copy status of file %d\n"), pg_iter);
911 if (pidarr[pg_iter] != 0) /* Check for already completed! */
913 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
917 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 _("File %d copied\n"), pg_iter);
932 if (ret == GNUNET_SYSERR)
943 * Internal notification of a connection, kept so that we can ensure some connections
944 * happen instead of flooding all testing daemons with requests to connect.
946 static void internal_connect_notify (void *cls,
947 const struct GNUNET_PeerIdentity *first,
948 const struct GNUNET_PeerIdentity *second,
949 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
950 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
951 struct GNUNET_TESTING_Daemon *first_daemon,
952 struct GNUNET_TESTING_Daemon *second_daemon,
955 struct GNUNET_TESTING_PeerGroup *pg = cls;
956 outstanding_connects--;
958 pg->notify_connection(pg->notify_connection_cls, first, second, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
962 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
964 struct ConnectContext *connect_context = cls;
966 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
970 _("Delaying connect, we have too many outstanding connections!\n"));
972 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &schedule_connect, connect_context);
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
980 outstanding_connects++;
981 GNUNET_TESTING_daemons_connect (connect_context->first,
982 connect_context->second,
984 &internal_connect_notify,
985 connect_context->pg);
986 GNUNET_free(connect_context);
991 * Connect the topology as specified by the PeerConnection's
992 * of each peer in the peer group
994 * @param pg the peer group we are dealing with
997 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
999 unsigned int pg_iter;
1000 struct PeerConnection *connection_iter;
1001 struct ConnectContext *connect_context;
1003 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1005 connection_iter = pg->peers[pg_iter].connected_peers;
1006 while (connection_iter != NULL)
1008 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1009 connect_context->pg = pg;
1010 connect_context->first = pg->peers[pg_iter].daemon;
1011 connect_context->second = connection_iter->daemon;
1013 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
1014 /*GNUNET_TESTING_daemons_connect (pg->peers[pg_iter].daemon,
1015 connection_iter->daemon,
1017 pg->notify_connection,
1018 pg->notify_connection_cls);*/
1019 connection_iter = connection_iter->next;
1021 /*if (outstanding_connects > MAX_OUTSTANDING_CONNECTS)
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 _("Sleeping to give peers a chance to connect!\n"));
1035 * Takes a peer group and attempts to create a topology based on the
1036 * one specified in the configuration file. Returns the number of connections
1037 * that will attempt to be created, but this will happen asynchronously(?) so
1038 * the caller will have to keep track (via the callback) of whether or not
1039 * the connection actually happened.
1041 * @param pg the peer group struct representing the running peers
1043 * @return the number of connections should be created by the topology, so the
1044 * caller knows how many to wait for (if it so chooses)
1048 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg)
1050 unsigned long long topology_num;
1052 int num_connections;
1054 GNUNET_assert (pg->notify_connection != NULL);
1057 GNUNET_CONFIGURATION_get_value_number (pg->cfg, "testing", "topology",
1060 switch (topology_num)
1062 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
1064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1065 _("Creating clique topology (may take a bit!)\n"));
1067 num_connections = create_clique (pg);
1069 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
1071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072 _("Creating small world (ring) topology (may take a bit!)\n"));
1074 num_connections = create_small_world_ring (pg);
1076 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 _("Creating small world (2d-torus) topology (may take a bit!)\n"));
1081 num_connections = create_small_world (pg);
1083 case GNUNET_TESTING_TOPOLOGY_RING:
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 _("Creating ring topology (may take a bit!)\n"));
1088 num_connections = create_ring (pg);
1090 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
1092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1093 _("Creating 2d torus topology (may take a bit!)\n"));
1095 num_connections = create_2d_torus (pg);
1097 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
1099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1100 _("Creating Erdos-Renyi topology (may take a bit!)\n"));
1102 num_connections = create_erdos_renyi (pg);
1104 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
1106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1107 _("Creating InterNAT topology (may take a bit!)\n"));
1109 num_connections = create_nated_internet (pg);
1111 case GNUNET_TESTING_TOPOLOGY_NONE:
1112 num_connections = 0;
1115 num_connections = 0;
1118 if (num_connections < 1)
1119 return GNUNET_SYSERR;
1121 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
1122 ret = create_and_copy_friend_files(pg);
1123 if (ret == GNUNET_OK)
1124 connect_topology(pg);
1128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1129 _("Failed during friend file copying!\n"));
1131 return GNUNET_SYSERR;
1136 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1137 _("No topology specified, was one intended?\n"));
1138 return GNUNET_SYSERR;
1141 return num_connections;
1145 * Start count gnunetd processes with the same set of transports and
1146 * applications. The port numbers (any option called "PORT") will be
1147 * adjusted to ensure that no two peers running on the same system
1148 * have the same port(s) in their respective configurations.
1150 * @param sched scheduler to use
1151 * @param cfg configuration template to use
1152 * @param total number of daemons to start
1153 * @param cb function to call on each daemon that was started
1154 * @param cb_cls closure for cb
1155 * @param connect_callback function to call each time two hosts are connected
1156 * @param connect_callback_cls closure for connect_callback
1157 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
1158 * everything on localhost).
1159 * @return NULL on error, otherwise handle to control peer group
1161 struct GNUNET_TESTING_PeerGroup *
1162 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
1163 const struct GNUNET_CONFIGURATION_Handle *cfg,
1165 GNUNET_TESTING_NotifyDaemonRunning cb,
1167 GNUNET_TESTING_NotifyConnection
1168 connect_callback, void *connect_callback_cls,
1169 const char *hostnames)
1171 struct GNUNET_TESTING_PeerGroup *pg;
1175 const char *hostname;
1176 char *baseservicehome;
1177 char *newservicehome;
1179 struct GNUNET_CONFIGURATION_Handle *pcfg;
1181 unsigned int hostcnt;
1190 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
1194 pg->cb_cls = cb_cls;
1195 pg->notify_connection = connect_callback;
1196 pg->notify_connection_cls = connect_callback_cls;
1198 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
1199 if (NULL != hostnames)
1202 /* skip leading spaces */
1203 while ((0 != *hostnames) && (isspace (*hostnames)))
1206 while ('\0' != *rpos)
1208 if (isspace (*rpos))
1212 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
1214 start = GNUNET_strdup (hostnames);
1216 while ('\0' != *pos)
1221 if (strlen (start) > 0)
1223 pg->hosts[off].minport = LOW_PORT;
1224 pg->hosts[off++].hostname = start;
1230 if (strlen (start) > 0)
1232 pg->hosts[off].minport = LOW_PORT;
1233 pg->hosts[off++].hostname = start;
1237 GNUNET_free (start);
1238 GNUNET_free (pg->hosts);
1242 minport = 0; /* make gcc happy */
1249 for (off = 0; off < total; off++)
1253 hostname = pg->hosts[off % hostcnt].hostname;
1254 pcfg = make_config (cfg, &pg->hosts[off % hostcnt].minport);
1259 pcfg = make_config (cfg, &minport);
1263 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1265 ("Could not create configuration for peer number %u on `%s'!\n"),
1266 off, hostname == NULL ? "localhost" : hostname);
1271 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
1274 GNUNET_asprintf (&newservicehome,
1275 "%s/%d/", baseservicehome, off);
1276 GNUNET_free (baseservicehome);
1280 tmpdir = getenv ("TMPDIR");
1281 tmpdir = tmpdir ? tmpdir : "/tmp";
1282 GNUNET_asprintf (&newservicehome,
1285 "gnunet-testing-test-test", off);
1287 GNUNET_CONFIGURATION_set_value_string (pcfg,
1289 "SERVICEHOME", newservicehome);
1290 GNUNET_free (newservicehome);
1291 pg->peers[off].cfg = pcfg;
1292 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (sched,
1296 if (NULL == pg->peers[off].daemon)
1297 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1298 _("Could not start peer number %u!\n"), off);
1304 * Get a daemon by number, so callers don't have to do nasty
1305 * offsetting operation.
1307 struct GNUNET_TESTING_Daemon *
1308 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
1310 if (position < pg->total)
1311 return pg->peers[position].daemon;
1317 * Shutdown all peers started in the given group.
1319 * @param pg handle to the peer group
1322 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg)
1325 struct PeerConnection *pos;
1326 struct PeerConnection *next;
1328 for (off = 0; off < pg->total; off++)
1330 /* FIXME: should we wait for our
1331 continuations to be called here? This
1332 would require us to take a continuation
1335 if (NULL != pg->peers[off].daemon)
1336 GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, NULL, NULL);
1337 if (NULL != pg->peers[off].cfg)
1338 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
1340 pos = pg->peers[off].connected_peers;
1349 GNUNET_free (pg->peers);
1350 if (NULL != pg->hosts)
1352 GNUNET_free (pg->hosts[0].hostname);
1353 GNUNET_free (pg->hosts);
1359 /* end of testing_group.c */