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 30
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;
231 uc.ret = GNUNET_CONFIGURATION_create ();
232 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
233 if (uc.nport >= HIGH_PORT)
236 GNUNET_CONFIGURATION_destroy (uc.ret);
239 *port = (uint16_t) uc.nport;
244 * Add entries to the peers connected list
246 * @param pg the peer group we are working with
247 * @param first index of the first peer
248 * @param second index of the second peer
250 * @return the number of connections added (can be 0, 1 or 2)
252 * FIXME: add both, or only add one?
253 * - if both are added, then we have to keep track
254 * when connecting so we don't double connect
255 * - if only one is added, we need to iterate over
256 * both lists to find out if connection already exists
257 * - having both allows the whitelisting/friend file
258 * creation to be easier
260 * -- For now, add both, we have to iterate over each to
261 * check for duplicates anyways, so we'll take the performance
262 * hit assuming we don't have __too__ many connections
266 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
269 struct PeerConnection *first_iter;
270 struct PeerConnection *second_iter;
273 struct PeerConnection *new_first;
274 struct PeerConnection *new_second;
276 first_iter = pg->peers[first].connected_peers;
277 add_first = GNUNET_YES;
278 while (first_iter != NULL)
280 if (first_iter->daemon == pg->peers[second].daemon)
281 add_first = GNUNET_NO;
282 first_iter = first_iter->next;
285 second_iter = pg->peers[second].connected_peers;
286 add_second = GNUNET_YES;
287 while (second_iter != NULL)
289 if (second_iter->daemon == pg->peers[first].daemon)
290 add_second = GNUNET_NO;
291 second_iter = second_iter->next;
297 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
298 new_first->daemon = pg->peers[second].daemon;
299 new_first->next = pg->peers[first].connected_peers;
300 pg->peers[first].connected_peers = new_first;
306 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
307 new_second->daemon = pg->peers[first].daemon;
308 new_second->next = pg->peers[second].connected_peers;
309 pg->peers[second].connected_peers = new_second;
317 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg)
322 unsigned int randomPeer;
323 double random, logNModifier, percentage;
324 unsigned int smallWorldConnections;
330 int connect_attempts;
332 logNModifier = 0.5; /* FIXME: default value? */
333 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
338 if (sscanf(p_string, "%lf", &logNModifier) != 1)
339 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
340 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
344 GNUNET_free (p_string);
346 percentage = 0.5; /* FIXME: default percentage? */
347 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
352 if (sscanf(p_string, "%lf", &percentage) != 1)
353 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
354 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
358 GNUNET_free (p_string);
360 natLog = log (pg->total);
361 connsPerPeer = ceil (natLog * logNModifier);
363 if (connsPerPeer % 2 == 1)
366 smallWorldConnections = 0;
367 connect_attempts = 0;
368 for (i = 0; i < pg->total; i++)
371 max = i + connsPerPeer / 2;
372 min = i - connsPerPeer / 2;
374 if (max > pg->total - 1)
376 max = max - pg->total;
382 min = pg->total - 1 + min;
386 for (j = 0; j < connsPerPeer / 2; j++)
388 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
389 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
390 if (random < percentage)
392 /* Connect to uniformly selected random peer */
394 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
396 while ((((randomPeer < max) && (randomPeer > min))
397 && (useAnd == 0)) || (((randomPeer > min)
398 || (randomPeer < max))
402 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
405 smallWorldConnections +=
406 add_connections (pg, i, randomPeer);
410 nodeToConnect = i + j + 1;
411 if (nodeToConnect > pg->total - 1)
413 nodeToConnect = nodeToConnect - pg->total;
416 add_connections (pg, i, nodeToConnect);
422 connect_attempts += smallWorldConnections;
424 return connect_attempts;
429 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg)
431 unsigned int outer_count, inner_count;
433 int connect_attempts;
434 double nat_percentage;
437 nat_percentage = 0.6; /* FIXME: default percentage? */
438 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
443 if (sscanf(p_string, "%lf", &nat_percentage) != 1)
444 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
445 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
449 GNUNET_free (p_string);
454 cutoff = (unsigned int) (nat_percentage * pg->total);
456 connect_attempts = 0;
458 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
460 for (inner_count = outer_count + 1; inner_count < pg->total;
463 if ((outer_count > cutoff) || (inner_count > cutoff))
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
467 "Connecting peer %d to peer %d\n",
468 outer_count, inner_count);
470 connect_attempts += add_connections(pg, outer_count, inner_count);
475 return connect_attempts;
482 create_small_world (struct GNUNET_TESTING_PeerGroup *pg)
484 unsigned int i, j, k;
488 unsigned int toggle = 1;
489 unsigned int nodeToConnect;
491 unsigned int node1Row;
492 unsigned int node1Col;
493 unsigned int node2Row;
494 unsigned int node2Col;
495 unsigned int distance;
496 double probability, random, percentage;
497 unsigned int smallWorldConnections;
499 int connect_attempts;
500 square = floor (sqrt (pg->total));
504 percentage = 0.5; /* FIXME: default percentage? */
505 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
510 if (sscanf(p_string, "%lf", &percentage) != 1)
511 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
512 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
516 GNUNET_free (p_string);
518 probability = 0.5; /* FIXME: default percentage? */
519 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
524 if (sscanf(p_string, "%lf", &probability) != 1)
525 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
526 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
530 GNUNET_free (p_string);
532 if (square * square != pg->total)
534 while (rows * cols < pg->total)
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
550 connect_attempts = 0;
551 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
552 * to the node to its right and above. Once this is over, we'll have our torus!
553 * Special case for the last node (if the rows and columns are not equal), connect
554 * to the first in the row to maintain topology.
556 for (i = 0; i < pg->total; i++)
558 /* First connect to the node to the right */
559 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
560 nodeToConnect = i + 1;
561 else if (i + 1 == pg->total)
562 nodeToConnect = rows * cols - cols;
564 nodeToConnect = i - cols + 1;
566 connect_attempts += add_connections (pg, i, nodeToConnect);
569 nodeToConnect = (rows * cols) - cols + i;
571 nodeToConnect = i - cols;
573 if (nodeToConnect < pg->total)
574 connect_attempts += add_connections (pg, i, nodeToConnect);
576 natLog = log (pg->total);
578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
579 _("natural log of %d is %d, will run %d iterations\n"),
580 pg->total, natLog, (int) (natLog * percentage));
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
583 smallWorldConnections = 0;
584 for (i = 0; i < (int) (natLog * percentage); i++)
586 for (j = 0; j < pg->total; j++)
588 /* Determine the row and column of node at position j on the 2d torus */
590 node1Col = j - (node1Row * cols);
591 for (k = 0; k < pg->total; k++)
593 /* Determine the row and column of node at position k on the 2d torus */
595 node2Col = k - (node2Row * cols);
596 /* Simple Cartesian distance */
597 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
600 /* Calculate probability as 1 over the square of the distance */
601 probability = 1.0 / (distance * distance);
602 /* Choose a random value between 0 and 1 */
603 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
604 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
605 /* If random < probability, then connect the two nodes */
606 if (random < probability)
607 smallWorldConnections += add_connections (pg, j, k);
613 connect_attempts += smallWorldConnections;
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 _("Total connections added for small world: %d!\n"),
617 smallWorldConnections);
619 return connect_attempts;
625 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg)
628 unsigned int outer_count;
629 unsigned int inner_count;
630 int connect_attempts;
634 probability = 0.5; /* FIXME: default percentage? */
635 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
640 if (sscanf(p_string, "%lf", &probability) != 1)
641 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
642 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
646 GNUNET_free (p_string);
648 connect_attempts = 0;
649 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
651 for (inner_count = outer_count + 1; inner_count < pg->total;
654 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
655 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
658 _("rand is %f probability is %f\n"), temp_rand,
661 if (temp_rand < probability)
663 connect_attempts += add_connections (pg, outer_count, inner_count);
668 return connect_attempts;
672 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg)
678 unsigned int toggle = 1;
679 unsigned int nodeToConnect;
680 int connect_attempts;
682 connect_attempts = 0;
684 square = floor (sqrt (pg->total));
688 if (square * square != pg->total)
690 while (rows * cols < pg->total)
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
705 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
706 * to the node to its right and above. Once this is over, we'll have our torus!
707 * Special case for the last node (if the rows and columns are not equal), connect
708 * to the first in the row to maintain topology.
710 for (i = 0; i < pg->total; i++)
712 /* First connect to the node to the right */
713 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
714 nodeToConnect = i + 1;
715 else if (i + 1 == pg->total)
716 nodeToConnect = rows * cols - cols;
718 nodeToConnect = i - cols + 1;
720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
721 "Connecting peer %d to peer %d\n",
724 connect_attempts += add_connections(pg, i, nodeToConnect);
726 /* Second connect to the node immediately above */
728 nodeToConnect = (rows * cols) - cols + i;
730 nodeToConnect = i - cols;
732 if (nodeToConnect < pg->total)
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "Connecting peer %d to peer %d\n",
739 connect_attempts += add_connections(pg, i, nodeToConnect);
744 return connect_attempts;
750 create_clique (struct GNUNET_TESTING_PeerGroup *pg)
752 unsigned int outer_count;
753 unsigned int inner_count;
754 int connect_attempts;
756 connect_attempts = 0;
758 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
760 for (inner_count = outer_count + 1; inner_count < pg->total;
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 "Connecting peer %d to peer %d\n",
766 outer_count, inner_count);
768 connect_attempts += add_connections(pg, outer_count, inner_count);
772 return connect_attempts;
777 create_ring (struct GNUNET_TESTING_PeerGroup *pg)
780 int connect_attempts;
782 connect_attempts = 0;
784 /* Connect each peer to the next highest numbered peer */
785 for (count = 0; count < pg->total - 1; count++)
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789 "Connecting peer %d to peer %d\n",
792 connect_attempts += add_connections(pg, count, count + 1);
795 /* Connect the last peer to the first peer */
796 connect_attempts += add_connections(pg, pg->total - 1, 0);
798 return connect_attempts;
803 * Create the friend files based on the PeerConnection's
804 * of each peer in the peer group, and copy the files
805 * to the appropriate place
807 * @param pg the peer group we are dealing with
810 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
812 FILE *temp_friend_handle;
813 unsigned int pg_iter;
814 struct PeerConnection *connection_iter;
815 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
816 char *temp_service_path;
819 struct GNUNET_PeerIdentity *temppeer;
821 enum GNUNET_OS_ProcessStatusType type;
822 unsigned long return_code;
827 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
828 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
830 mytemp = GNUNET_DISK_mktemp("friends");
831 temp_friend_handle = fopen (mytemp, "wt");
832 connection_iter = pg->peers[pg_iter].connected_peers;
833 while (connection_iter != NULL)
835 temppeer = &connection_iter->daemon->id;
836 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
837 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
838 connection_iter = connection_iter->next;
841 fclose(temp_friend_handle);
844 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
846 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
847 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
850 if (UNLINK (mytemp) != 0)
851 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
852 GNUNET_free (mytemp);
856 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
858 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
859 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
860 "mv", mytemp, arg, NULL);
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863 _("Copying file with command cp %s %s\n"), mytemp, arg);
868 else /* Remote, scp the file to the correct place */
870 if (NULL != pg->peers[pg_iter].daemon->username)
871 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
873 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
874 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
875 "scp", mytemp, arg, NULL);
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 _("Copying file with command scp %s %s\n"), mytemp, arg);
883 GNUNET_free (temp_service_path);
884 GNUNET_free (mytemp);
889 while ((count < max_wait) && (ret != GNUNET_OK))
892 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 _("Checking copy status of file %d\n"), pg_iter);
898 if (pidarr[pg_iter] != 0) /* Check for already completed! */
900 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
904 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
913 _("File %d copied\n"), pg_iter);
919 if (ret == GNUNET_SYSERR)
930 * Internal notification of a connection, kept so that we can ensure some connections
931 * happen instead of flooding all testing daemons with requests to connect.
933 static void internal_connect_notify (void *cls,
934 const struct GNUNET_PeerIdentity *first,
935 const struct GNUNET_PeerIdentity *second,
936 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
937 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
938 struct GNUNET_TESTING_Daemon *first_daemon,
939 struct GNUNET_TESTING_Daemon *second_daemon,
942 struct GNUNET_TESTING_PeerGroup *pg = cls;
943 outstanding_connects--;
945 pg->notify_connection(pg->notify_connection_cls, first, second, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
949 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
951 struct ConnectContext *connect_context = cls;
953 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957 _("Delaying connect, we have too many outstanding connections!\n"));
959 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &schedule_connect, connect_context);
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
967 outstanding_connects++;
968 GNUNET_TESTING_daemons_connect (connect_context->first,
969 connect_context->second,
971 &internal_connect_notify,
972 connect_context->pg);
973 GNUNET_free(connect_context);
978 * Connect the topology as specified by the PeerConnection's
979 * of each peer in the peer group
981 * @param pg the peer group we are dealing with
984 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
986 unsigned int pg_iter;
987 struct PeerConnection *connection_iter;
988 struct ConnectContext *connect_context;
990 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
992 connection_iter = pg->peers[pg_iter].connected_peers;
993 while (connection_iter != NULL)
995 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
996 connect_context->pg = pg;
997 connect_context->first = pg->peers[pg_iter].daemon;
998 connect_context->second = connection_iter->daemon;
1000 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
1001 /*GNUNET_TESTING_daemons_connect (pg->peers[pg_iter].daemon,
1002 connection_iter->daemon,
1004 pg->notify_connection,
1005 pg->notify_connection_cls);*/
1006 connection_iter = connection_iter->next;
1008 /*if (outstanding_connects > MAX_OUTSTANDING_CONNECTS)
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012 _("Sleeping to give peers a chance to connect!\n"));
1022 * Takes a peer group and attempts to create a topology based on the
1023 * one specified in the configuration file. Returns the number of connections
1024 * that will attempt to be created, but this will happen asynchronously(?) so
1025 * the caller will have to keep track (via the callback) of whether or not
1026 * the connection actually happened.
1028 * @param pg the peer group struct representing the running peers
1030 * @return the number of connections should be created by the topology, so the
1031 * caller knows how many to wait for (if it so chooses)
1035 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg)
1037 unsigned long long topology_num;
1039 int num_connections;
1041 GNUNET_assert (pg->notify_connection != NULL);
1044 GNUNET_CONFIGURATION_get_value_number (pg->cfg, "testing", "topology",
1047 switch (topology_num)
1049 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052 _("Creating clique topology (may take a bit!)\n"));
1054 num_connections = create_clique (pg);
1056 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1059 _("Creating small world (ring) topology (may take a bit!)\n"));
1061 num_connections = create_small_world_ring (pg);
1063 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1066 _("Creating small world (2d-torus) topology (may take a bit!)\n"));
1068 num_connections = create_small_world (pg);
1070 case GNUNET_TESTING_TOPOLOGY_RING:
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1073 _("Creating ring topology (may take a bit!)\n"));
1075 num_connections = create_ring (pg);
1077 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 _("Creating 2d torus topology (may take a bit!)\n"));
1082 num_connections = create_2d_torus (pg);
1084 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1087 _("Creating Erdos-Renyi topology (may take a bit!)\n"));
1089 num_connections = create_erdos_renyi (pg);
1091 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094 _("Creating InterNAT topology (may take a bit!)\n"));
1096 num_connections = create_nated_internet (pg);
1098 case GNUNET_TESTING_TOPOLOGY_NONE:
1099 num_connections = 0;
1102 num_connections = 0;
1105 if (num_connections < 1)
1106 return GNUNET_SYSERR;
1108 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
1109 ret = create_and_copy_friend_files(pg);
1110 if (ret == GNUNET_OK)
1111 connect_topology(pg);
1115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1116 _("Failed during friend file copying!\n"));
1118 return GNUNET_SYSERR;
1123 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1124 _("No topology specified, was one intended?\n"));
1125 return GNUNET_SYSERR;
1128 return num_connections;
1132 * Start count gnunetd processes with the same set of transports and
1133 * applications. The port numbers (any option called "PORT") will be
1134 * adjusted to ensure that no two peers running on the same system
1135 * have the same port(s) in their respective configurations.
1137 * @param sched scheduler to use
1138 * @param cfg configuration template to use
1139 * @param total number of daemons to start
1140 * @param cb function to call on each daemon that was started
1141 * @param cb_cls closure for cb
1142 * @param connect_callback function to call each time two hosts are connected
1143 * @param connect_callback_cls closure for connect_callback
1144 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
1145 * everything on localhost).
1146 * @return NULL on error, otherwise handle to control peer group
1148 struct GNUNET_TESTING_PeerGroup *
1149 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
1150 const struct GNUNET_CONFIGURATION_Handle *cfg,
1152 GNUNET_TESTING_NotifyDaemonRunning cb,
1154 GNUNET_TESTING_NotifyConnection
1155 connect_callback, void *connect_callback_cls,
1156 const char *hostnames)
1158 struct GNUNET_TESTING_PeerGroup *pg;
1162 const char *hostname;
1163 char *baseservicehome;
1164 char *newservicehome;
1166 struct GNUNET_CONFIGURATION_Handle *pcfg;
1168 unsigned int hostcnt;
1176 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
1180 pg->cb_cls = cb_cls;
1181 pg->notify_connection = connect_callback;
1182 pg->notify_connection_cls = connect_callback_cls;
1184 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
1185 if (NULL != hostnames)
1188 /* skip leading spaces */
1189 while ((0 != *hostnames) && (isspace (*hostnames)))
1192 while ('\0' != *rpos)
1194 if (isspace (*rpos))
1198 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
1200 start = GNUNET_strdup (hostnames);
1202 while ('\0' != *pos)
1207 if (strlen (start) > 0)
1209 pg->hosts[off].minport = LOW_PORT;
1210 pg->hosts[off++].hostname = start;
1216 if (strlen (start) > 0)
1218 pg->hosts[off].minport = LOW_PORT;
1219 pg->hosts[off++].hostname = start;
1223 GNUNET_free (start);
1224 GNUNET_free (pg->hosts);
1228 minport = 0; /* make gcc happy */
1235 for (off = 0; off < total; off++)
1239 hostname = pg->hosts[off % hostcnt].hostname;
1240 pcfg = make_config (cfg, &pg->hosts[off % hostcnt].minport);
1245 pcfg = make_config (cfg, &minport);
1249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1251 ("Could not create configuration for peer number %u on `%s'!\n"),
1252 off, hostname == NULL ? "localhost" : hostname);
1257 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
1260 GNUNET_asprintf (&newservicehome,
1261 "%s/%d/", baseservicehome, off);
1262 GNUNET_free (baseservicehome);
1266 tmpdir = getenv ("TMPDIR");
1267 tmpdir = tmpdir ? tmpdir : "/tmp";
1268 GNUNET_asprintf (&newservicehome,
1271 "gnunet-testing-test-test", off);
1273 GNUNET_CONFIGURATION_set_value_string (pcfg,
1275 "SERVICEHOME", newservicehome);
1276 GNUNET_free (newservicehome);
1277 pg->peers[off].cfg = pcfg;
1278 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (sched,
1282 if (NULL == pg->peers[off].daemon)
1283 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1284 _("Could not start peer number %u!\n"), off);
1290 * Get a daemon by number, so callers don't have to do nasty
1291 * offsetting operation.
1293 struct GNUNET_TESTING_Daemon *
1294 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
1296 if (position < pg->total)
1297 return pg->peers[position].daemon;
1303 * Shutdown all peers started in the given group.
1305 * @param pg handle to the peer group
1308 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg)
1311 struct PeerConnection *pos;
1312 struct PeerConnection *next;
1314 for (off = 0; off < pg->total; off++)
1316 /* FIXME: should we wait for our
1317 continuations to be called here? This
1318 would require us to take a continuation
1321 if (NULL != pg->peers[off].daemon)
1322 GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, NULL, NULL);
1323 if (NULL != pg->peers[off].cfg)
1324 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
1326 pos = pg->peers[off].connected_peers;
1335 GNUNET_free (pg->peers);
1336 if (NULL != pg->hosts)
1338 GNUNET_free (pg->hosts[0].hostname);
1339 GNUNET_free (pg->hosts);
1345 /* end of testing_group.c */