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 CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 180)
54 struct PeerConnection *next;
57 * Pointer to daemon handle
59 struct GNUNET_TESTING_Daemon *daemon;
64 * Data we keep per peer.
69 * (Initial) configuration of the host.
70 * (initial because clients could change
71 * it and we would not know about those
74 struct GNUNET_CONFIGURATION_Handle *cfg;
77 * Handle for controlling the daemon.
79 struct GNUNET_TESTING_Daemon *daemon;
82 * Linked list of peer connections (simply indexes of PeerGroup)
83 * FIXME: Question, store pointer or integer? Pointer for now...
85 struct PeerConnection *connected_peers;
90 * Data we keep per host.
100 * Lowest port that we have not yet used
108 * Handle to a group of GNUnet peers.
110 struct GNUNET_TESTING_PeerGroup
115 struct GNUNET_SCHEDULER_Handle *sched;
118 * Configuration template.
120 const struct GNUNET_CONFIGURATION_Handle *cfg;
123 * Function to call on each started daemon.
125 GNUNET_TESTING_NotifyDaemonRunning cb;
133 * Function to call on each topology connection created
135 GNUNET_TESTING_NotifyConnection notify_connection;
138 * Callback for notify_connection
140 void *notify_connection_cls;
143 * NULL-terminated array of information about
146 struct HostData *hosts;
149 * Array of "total" peers.
151 struct PeerData *peers;
154 * Number of peers in this group.
163 struct GNUNET_CONFIGURATION_Handle *ret;
168 * Function to iterate over options. Copies
169 * the options to the target configuration,
170 * updating PORT values as needed.
173 * @param section name of the section
174 * @param option name of the option
175 * @param value value of the option
178 update_config (void *cls,
179 const char *section, const char *option, const char *value)
181 struct UpdateContext *ctx = cls;
185 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
187 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
190 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
195 * Create a new configuration using the given configuration
196 * as a template; however, each PORT in the existing cfg
197 * must be renumbered by incrementing "*port". If we run
198 * out of "*port" numbers, return NULL.
200 * @param cfg template configuration
201 * @param port port numbers to use, update to reflect
202 * port numbers that were used
203 * @return new configuration, NULL on error
205 static struct GNUNET_CONFIGURATION_Handle *
206 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg, uint16_t * port)
208 struct UpdateContext uc;
213 uc.ret = GNUNET_CONFIGURATION_create ();
214 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
215 if (uc.nport >= HIGH_PORT)
218 GNUNET_CONFIGURATION_destroy (uc.ret);
221 *port = (uint16_t) uc.nport;
226 * Add entries to the peers connected list
228 * @param pg the peer group we are working with
229 * @param first index of the first peer
230 * @param second index of the second peer
232 * @return the number of connections added (can be 0, 1 or 2)
234 * FIXME: add both, or only add one?
235 * - if both are added, then we have to keep track
236 * when connecting so we don't double connect
237 * - if only one is added, we need to iterate over
238 * both lists to find out if connection already exists
239 * - having both allows the whitelisting/friend file
240 * creation to be easier
242 * -- For now, add both, we have to iterate over each to
243 * check for duplicates anyways, so we'll take the performance
244 * hit assuming we don't have __too__ many connections
248 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
251 struct PeerConnection *first_iter;
252 struct PeerConnection *second_iter;
255 struct PeerConnection *new_first;
256 struct PeerConnection *new_second;
258 first_iter = pg->peers[first].connected_peers;
259 add_first = GNUNET_YES;
260 while (first_iter != NULL)
262 if (first_iter->daemon == pg->peers[second].daemon)
263 add_first = GNUNET_NO;
264 first_iter = first_iter->next;
267 second_iter = pg->peers[second].connected_peers;
268 add_second = GNUNET_YES;
269 while (second_iter != NULL)
271 if (second_iter->daemon == pg->peers[first].daemon)
272 add_second = GNUNET_NO;
273 second_iter = second_iter->next;
279 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
280 new_first->daemon = pg->peers[second].daemon;
281 new_first->next = pg->peers[first].connected_peers;
282 pg->peers[first].connected_peers = new_first;
288 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
289 new_second->daemon = pg->peers[first].daemon;
290 new_second->next = pg->peers[second].connected_peers;
291 pg->peers[second].connected_peers = new_second;
299 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg)
304 unsigned int randomPeer;
305 double random, logNModifier, percentage;
306 unsigned int smallWorldConnections;
312 int connect_attempts;
313 struct GNUNET_TIME_Absolute time;
315 GNUNET_CONFIGURATION_get_value_string(pg->cfg, "TESTING", "LOGNMODIFIER", &p_string);
316 if ((p_string == NULL) || (sscanf(p_string, "%lf", &logNModifier) != 1))
317 logNModifier = 0.5; /* FIXME: default modifier? */
319 GNUNET_free_non_null(p_string);
321 GNUNET_CONFIGURATION_get_value_string(pg->cfg, "TESTING", "PERCENTAGE", &p_string);
322 if ((p_string == NULL) || (sscanf(p_string, "%lf", &percentage) != 1))
323 percentage = 0.5; /* FIXME: default percentage? */
325 GNUNET_free_non_null(p_string);
327 natLog = log (pg->total);
328 connsPerPeer = ceil (natLog * logNModifier);
330 if (connsPerPeer % 2 == 1)
333 time = GNUNET_TIME_absolute_get ();
334 srand ((unsigned int) time.value);
335 smallWorldConnections = 0;
336 connect_attempts = 0;
337 for (i = 0; i < pg->total; i++)
340 max = i + connsPerPeer / 2;
341 min = i - connsPerPeer / 2;
343 if (max > pg->total - 1)
345 max = max - pg->total;
351 min = pg->total - 1 + min;
355 for (j = 0; j < connsPerPeer / 2; j++)
357 random = ((double) rand () / RAND_MAX);
358 if (random < percentage)
360 /* Connect to uniformly selected random peer */
362 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
364 while ((((randomPeer < max) && (randomPeer > min))
365 && (useAnd == 0)) || (((randomPeer > min)
366 || (randomPeer < max))
370 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
373 smallWorldConnections +=
374 add_connections (pg, i, randomPeer);
378 nodeToConnect = i + j + 1;
379 if (nodeToConnect > pg->total - 1)
381 nodeToConnect = nodeToConnect - pg->total;
384 add_connections (pg, i, nodeToConnect);
390 connect_attempts += smallWorldConnections;
392 return connect_attempts;
397 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg)
399 unsigned int outer_count, inner_count;
401 int connect_attempts;
402 double nat_percentage;
405 GNUNET_CONFIGURATION_get_value_string(pg->cfg, "TESTING", "NATPERCENTAGE", &p_string);
406 if ((p_string == NULL) || (sscanf(p_string, "%lf", &nat_percentage) != 1))
407 nat_percentage = 0.6; /* FIXME: default modifier? */
409 GNUNET_free_non_null(p_string);
411 cutoff = (unsigned int) (nat_percentage * pg->total);
413 connect_attempts = 0;
415 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
417 for (inner_count = outer_count + 1; inner_count < pg->total;
420 if ((outer_count > cutoff) || (inner_count > cutoff))
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "Connecting peer %d to peer %d\n",
425 outer_count, inner_count);
427 connect_attempts += add_connections(pg, outer_count, inner_count);
432 return connect_attempts;
439 create_small_world (struct GNUNET_TESTING_PeerGroup *pg)
441 unsigned int i, j, k;
445 unsigned int toggle = 1;
446 unsigned int nodeToConnect;
448 unsigned int node1Row;
449 unsigned int node1Col;
450 unsigned int node2Row;
451 unsigned int node2Col;
452 unsigned int distance;
453 double probability, random, percentage;
454 unsigned int smallWorldConnections;
456 int connect_attempts;
457 square = floor (sqrt (pg->total));
461 GNUNET_CONFIGURATION_get_value_string(pg->cfg, "TESTING", "PERCENTAGE", &p_string);
462 if ((p_string == NULL) || (sscanf(p_string, "%lf", &percentage) != 1))
463 percentage = 0.5; /* FIXME: default percentage? */
465 GNUNET_free_non_null(p_string);
467 GNUNET_CONFIGURATION_get_value_string(pg->cfg, "TESTING", "PROBABILITY", &p_string);
468 if ((p_string == NULL) || (sscanf(p_string, "%lf", &probability) != 1))
469 probability = 0.5; /* FIXME: default probability? */
471 GNUNET_free_non_null(p_string);
473 if (square * square != pg->total)
475 while (rows * cols < pg->total)
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
491 connect_attempts = 0;
492 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
493 * to the node to its right and above. Once this is over, we'll have our torus!
494 * Special case for the last node (if the rows and columns are not equal), connect
495 * to the first in the row to maintain topology.
497 for (i = 0; i < pg->total; i++)
499 /* First connect to the node to the right */
500 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
501 nodeToConnect = i + 1;
502 else if (i + 1 == pg->total)
503 nodeToConnect = rows * cols - cols;
505 nodeToConnect = i - cols + 1;
507 connect_attempts += add_connections (pg, i, nodeToConnect);
510 nodeToConnect = (rows * cols) - cols + i;
512 nodeToConnect = i - cols;
514 if (nodeToConnect < pg->total)
515 connect_attempts += add_connections (pg, i, nodeToConnect);
517 natLog = log (pg->total);
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 _("natural log of %d is %d, will run %d iterations\n"),
521 pg->total, natLog, (int) (natLog * percentage));
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
524 smallWorldConnections = 0;
525 for (i = 0; i < (int) (natLog * percentage); i++)
527 for (j = 0; j < pg->total; j++)
529 /* Determine the row and column of node at position j on the 2d torus */
531 node1Col = j - (node1Row * cols);
532 for (k = 0; k < pg->total; k++)
534 /* Determine the row and column of node at position k on the 2d torus */
536 node2Col = k - (node2Row * cols);
537 /* Simple Cartesian distance */
538 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
541 /* Calculate probability as 1 over the square of the distance */
542 probability = 1.0 / (distance * distance);
543 /* Choose a random, divide by RAND_MAX to get a number between 0 and 1 */
544 random = ((double) rand () / RAND_MAX);
545 /* If random < probability, then connect the two nodes */
546 if (random < probability)
547 smallWorldConnections += add_connections (pg, j, k);
553 connect_attempts += smallWorldConnections;
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
556 _("Total connections added for small world: %d!\n"),
557 smallWorldConnections);
559 return connect_attempts;
565 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg)
568 unsigned int outer_count;
569 unsigned int inner_count;
570 int connect_attempts;
573 connect_attempts = 0;
575 GNUNET_CONFIGURATION_get_value_string(pg->cfg, "TESTING", "PROBABILITY", &p_string);
576 if ((p_string == NULL) || (sscanf(p_string, "%lf", &probability) != 1))
577 probability = 0.5; /* FIXME: default probability? */
579 GNUNET_free_non_null (p_string);
580 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
582 for (inner_count = outer_count + 1; inner_count < pg->total;
585 temp_rand = ((double) RANDOM () / RAND_MAX);
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 _("rand is %f probability is %f\n"), temp_rand,
591 if (temp_rand < probability)
593 connect_attempts += add_connections (pg, outer_count, inner_count);
598 return connect_attempts;
602 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg)
608 unsigned int toggle = 1;
609 unsigned int nodeToConnect;
610 int connect_attempts;
612 connect_attempts = 0;
614 square = floor (sqrt (pg->total));
618 if (square * square != pg->total)
620 while (rows * cols < pg->total)
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
635 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
636 * to the node to its right and above. Once this is over, we'll have our torus!
637 * Special case for the last node (if the rows and columns are not equal), connect
638 * to the first in the row to maintain topology.
640 for (i = 0; i < pg->total; i++)
642 /* First connect to the node to the right */
643 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
644 nodeToConnect = i + 1;
645 else if (i + 1 == pg->total)
646 nodeToConnect = rows * cols - cols;
648 nodeToConnect = i - cols + 1;
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651 "Connecting peer %d to peer %d\n",
654 connect_attempts += add_connections(pg, i, nodeToConnect);
656 /* Second connect to the node immediately above */
658 nodeToConnect = (rows * cols) - cols + i;
660 nodeToConnect = i - cols;
662 if (nodeToConnect < pg->total)
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "Connecting peer %d to peer %d\n",
669 connect_attempts += add_connections(pg, i, nodeToConnect);
674 return connect_attempts;
680 create_clique (struct GNUNET_TESTING_PeerGroup *pg)
682 unsigned int outer_count;
683 unsigned int inner_count;
684 int connect_attempts;
686 connect_attempts = 0;
688 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
690 for (inner_count = outer_count + 1; inner_count < pg->total;
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695 "Connecting peer %d to peer %d\n",
696 outer_count, inner_count);
698 connect_attempts += add_connections(pg, outer_count, inner_count);
702 return connect_attempts;
707 create_ring (struct GNUNET_TESTING_PeerGroup *pg)
710 int connect_attempts;
712 connect_attempts = 0;
714 /* Connect each peer to the next highest numbered peer */
715 for (count = 0; count < pg->total - 1; count++)
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 "Connecting peer %d to peer %d\n",
722 connect_attempts += add_connections(pg, count, count + 1);
725 /* Connect the last peer to the first peer */
726 connect_attempts += add_connections(pg, pg->total - 1, 0);
728 return connect_attempts;
733 * Create the friend files based on the PeerConnection's
734 * of each peer in the peer group, and copy the files
735 * to the appropriate place
737 * @param pg the peer group we are dealing with
740 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
742 FILE *temp_friend_handle;
743 unsigned int pg_iter;
744 struct PeerConnection *connection_iter;
745 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
746 char *temp_service_path;
749 struct GNUNET_PeerIdentity *temppeer;
751 enum GNUNET_OS_ProcessStatusType type;
752 unsigned long return_code;
757 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
758 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
760 mytemp = GNUNET_DISK_mktemp("friends");
761 temp_friend_handle = fopen (mytemp, "wt");
762 connection_iter = pg->peers[pg_iter].connected_peers;
763 while (connection_iter != NULL)
765 temppeer = &connection_iter->daemon->id;
766 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
767 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
768 connection_iter = connection_iter->next;
771 fclose(temp_friend_handle);
773 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path);
775 if (temp_service_path == NULL)
777 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
778 _("No SERVICEHOME specified in peer configuration, can't copy friends file!\n"));
779 if (unlink(mytemp) != 0)
780 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
781 GNUNET_free (mytemp);
785 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
787 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
788 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
789 "mv", mytemp, arg, NULL);
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
792 _("Copying file with command cp %s %s\n"), mytemp, arg);
797 else /* Remote, scp the file to the correct place */
799 if (NULL != pg->peers[pg_iter].daemon->username)
800 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
802 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
803 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
804 "scp", mytemp, arg, NULL);
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808 _("Copying file with command scp %s %s\n"), mytemp, arg);
812 GNUNET_free (temp_service_path);
813 GNUNET_free (mytemp);
818 while ((count < max_wait) && (ret != GNUNET_OK))
821 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
825 _("Checking copy status of file %d\n"), pg_iter);
827 if (pidarr[pg_iter] != 0) /* Check for already completed! */
829 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
833 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
842 _("File %d copied\n"), pg_iter);
848 if (ret == GNUNET_SYSERR)
861 * Connect the topology as specified by the PeerConnection's
862 * of each peer in the peer group
864 * @param pg the peer group we are dealing with
867 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
869 unsigned int pg_iter;
870 struct PeerConnection *connection_iter;
874 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
876 connection_iter = pg->peers[pg_iter].connected_peers;
877 while (connection_iter != NULL)
879 GNUNET_TESTING_daemons_connect (pg->peers[pg_iter].daemon,
880 connection_iter->daemon,
882 pg->notify_connection,
883 pg->notify_connection_cls);
884 connection_iter = connection_iter->next;
886 if (connect_count % 50 == 0)
889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
890 _("Sleeping to give peers a chance to connect!\n"));
900 * Takes a peer group and attempts to create a topology based on the
901 * one specified in the configuration file. Returns the number of connections
902 * that will attempt to be created, but this will happen asynchronously(?) so
903 * the caller will have to keep track (via the callback) of whether or not
904 * the connection actually happened.
906 * @param pg the peer group struct representing the running peers
908 * @return the number of connections should be created by the topology, so the
909 * caller knows how many to wait for (if it so chooses)
913 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg)
915 unsigned long long topology_num;
919 GNUNET_assert (pg->notify_connection != NULL);
922 GNUNET_CONFIGURATION_get_value_number (pg->cfg, "testing", "topology",
925 switch (topology_num)
927 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
930 _("Creating clique topology (may take a bit!)\n"));
932 num_connections = create_clique (pg);
934 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
937 _("Creating small world (ring) topology (may take a bit!)\n"));
939 num_connections = create_small_world_ring (pg);
941 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944 _("Creating small world (2d-torus) topology (may take a bit!)\n"));
946 num_connections = create_small_world (pg);
948 case GNUNET_TESTING_TOPOLOGY_RING:
950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
951 _("Creating ring topology (may take a bit!)\n"));
953 num_connections = create_ring (pg);
955 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958 _("Creating 2d torus topology (may take a bit!)\n"));
960 num_connections = create_2d_torus (pg);
962 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965 _("Creating Erdos-Renyi topology (may take a bit!)\n"));
967 num_connections = create_erdos_renyi (pg);
969 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
972 _("Creating InterNAT topology (may take a bit!)\n"));
974 num_connections = create_nated_internet (pg);
976 case GNUNET_TESTING_TOPOLOGY_NONE:
983 if (num_connections < 1)
984 return GNUNET_SYSERR;
986 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
987 ret = create_and_copy_friend_files(pg);
988 if (ret == GNUNET_OK)
989 connect_topology(pg);
993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
994 _("Failed during friend file copying!\n"));
996 return GNUNET_SYSERR;
1001 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1002 _("No topology specified, was one intended?\n"));
1005 return num_connections;
1009 * Start count gnunetd processes with the same set of transports and
1010 * applications. The port numbers (any option called "PORT") will be
1011 * adjusted to ensure that no two peers running on the same system
1012 * have the same port(s) in their respective configurations.
1014 * @param sched scheduler to use
1015 * @param cfg configuration template to use
1016 * @param total number of daemons to start
1017 * @param cb function to call on each daemon that was started
1018 * @param cb_cls closure for cb
1019 * @param connect_callback function to call each time two hosts are connected
1020 * @param connect_callback_cls closure for connect_callback
1021 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
1022 * everything on localhost).
1023 * @return NULL on error, otherwise handle to control peer group
1025 struct GNUNET_TESTING_PeerGroup *
1026 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
1027 const struct GNUNET_CONFIGURATION_Handle *cfg,
1029 GNUNET_TESTING_NotifyDaemonRunning cb,
1031 GNUNET_TESTING_NotifyConnection
1032 connect_callback, void *connect_callback_cls,
1033 const char *hostnames)
1035 struct GNUNET_TESTING_PeerGroup *pg;
1039 const char *hostname;
1040 char *baseservicehome;
1041 char *newservicehome;
1043 struct GNUNET_CONFIGURATION_Handle *pcfg;
1045 unsigned int hostcnt;
1053 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
1057 pg->cb_cls = cb_cls;
1058 pg->notify_connection = connect_callback;
1059 pg->notify_connection_cls = connect_callback_cls;
1061 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
1062 if (NULL != hostnames)
1065 /* skip leading spaces */
1066 while ((0 != *hostnames) && (isspace (*hostnames)))
1069 while ('\0' != *rpos)
1071 if (isspace (*rpos))
1075 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
1077 start = GNUNET_strdup (hostnames);
1079 while ('\0' != *pos)
1084 if (strlen (start) > 0)
1086 pg->hosts[off].minport = LOW_PORT;
1087 pg->hosts[off++].hostname = start;
1093 if (strlen (start) > 0)
1095 pg->hosts[off].minport = LOW_PORT;
1096 pg->hosts[off++].hostname = start;
1100 GNUNET_free (start);
1101 GNUNET_free (pg->hosts);
1105 minport = 0; /* make gcc happy */
1112 for (off = 0; off < total; off++)
1116 hostname = pg->hosts[off % hostcnt].hostname;
1117 pcfg = make_config (cfg, &pg->hosts[off % hostcnt].minport);
1122 pcfg = make_config (cfg, &minport);
1126 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1128 ("Could not create configuration for peer number %u on `%s'!\n"),
1129 off, hostname == NULL ? "localhost" : hostname);
1134 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
1137 GNUNET_asprintf (&newservicehome,
1138 "%s/%d/", baseservicehome, off);
1139 GNUNET_free (baseservicehome);
1143 tmpdir = getenv ("TMPDIR");
1144 tmpdir = tmpdir ? tmpdir : "/tmp";
1145 GNUNET_asprintf (&newservicehome,
1148 "gnunet-testing-test-test", off);
1150 GNUNET_CONFIGURATION_set_value_string (pcfg,
1152 "SERVICEHOME", newservicehome);
1153 GNUNET_free (newservicehome);
1154 pg->peers[off].cfg = pcfg;
1155 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (sched,
1159 if (NULL == pg->peers[off].daemon)
1160 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1161 _("Could not start peer number %u!\n"), off);
1167 * Get a daemon by number, so callers don't have to do nasty
1168 * offsetting operation.
1170 struct GNUNET_TESTING_Daemon *
1171 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
1173 if (position < pg->total)
1174 return pg->peers[position].daemon;
1180 * Shutdown all peers started in the given group.
1182 * @param pg handle to the peer group
1185 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg)
1188 struct PeerConnection *pos;
1189 struct PeerConnection *next;
1191 for (off = 0; off < pg->total; off++)
1193 /* FIXME: should we wait for our
1194 continuations to be called here? This
1195 would require us to take a continuation
1198 if (NULL != pg->peers[off].daemon)
1199 GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, NULL, NULL);
1200 if (NULL != pg->peers[off].cfg)
1201 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
1203 pos = pg->peers[off].connected_peers;
1212 GNUNET_free (pg->peers);
1213 if (NULL != pg->hosts)
1215 GNUNET_free (pg->hosts[0].hostname);
1216 GNUNET_free (pg->hosts);
1222 /* end of testing_group.c */