2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing_group.c
23 * @brief convenience API for writing testcases for GNUnet
24 * @author Christian Grothoff
27 #include "gnunet_arm_service.h"
28 #include "gnunet_testing_lib.h"
30 #define VERBOSE_TESTING GNUNET_NO
33 * Lowest port used for GNUnet testing. Should be high enough to not
34 * conflict with other applications running on the hosts but be low
35 * enough to not conflict with client-ports (typically starting around
38 #define LOW_PORT 10000
41 * Highest port used for GNUnet testing. Should be low enough to not
42 * conflict with the port range for "local" ports (client apps; see
43 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
45 #define HIGH_PORT 32000
47 #define MAX_OUTSTANDING_CONNECTIONS 50
49 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 160)
51 #define CONNECT_ATTEMPTS 8
58 struct PeerConnection *next;
61 * Pointer to daemon handle
63 struct GNUNET_TESTING_Daemon *daemon;
68 * Data we keep per peer.
73 * (Initial) configuration of the host.
74 * (initial because clients could change
75 * it and we would not know about those
78 struct GNUNET_CONFIGURATION_Handle *cfg;
81 * Handle for controlling the daemon.
83 struct GNUNET_TESTING_Daemon *daemon;
86 * Linked list of peer connections (simply indexes of PeerGroup)
88 struct PeerConnection *connected_peers;
91 * Total number of connections this peer has
98 * Data we keep per host.
108 * Lowest port that we have not yet used
116 * Handle to a group of GNUnet peers.
118 struct GNUNET_TESTING_PeerGroup
123 struct GNUNET_SCHEDULER_Handle *sched;
126 * Configuration template.
128 const struct GNUNET_CONFIGURATION_Handle *cfg;
131 * Function to call on each started daemon.
133 GNUNET_TESTING_NotifyDaemonRunning cb;
141 * Function to call on each topology connection created
143 GNUNET_TESTING_NotifyConnection notify_connection;
146 * Callback for notify_connection
148 void *notify_connection_cls;
151 * NULL-terminated array of information about
154 struct HostData *hosts;
157 * Array of "total" peers.
159 struct PeerData *peers;
162 * Number of peers in this group.
171 struct GNUNET_CONFIGURATION_Handle *ret;
173 const char *hostname;
177 struct ConnectContext
179 struct GNUNET_TESTING_Daemon *first;
181 struct GNUNET_TESTING_Daemon *second;
183 struct GNUNET_TESTING_PeerGroup *pg;
187 * Number of connects we are waiting on, allows us to rate limit
190 static int outstanding_connects;
194 * Function to iterate over options. Copies
195 * the options to the target configuration,
196 * updating PORT values as needed.
199 * @param section name of the section
200 * @param option name of the option
201 * @param value value of the option
204 update_config (void *cls,
205 const char *section, const char *option, const char *value)
207 struct UpdateContext *ctx = cls;
211 if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
213 GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
217 if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
219 value = ctx->hostname;
222 GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
227 * Create a new configuration using the given configuration
228 * as a template; however, each PORT in the existing cfg
229 * must be renumbered by incrementing "*port". If we run
230 * out of "*port" numbers, return NULL.
232 * @param cfg template configuration
233 * @param port port numbers to use, update to reflect
234 * port numbers that were used
235 * @param hostname hostname of the controlling host, to allow control connections from
237 * @return new configuration, NULL on error
239 static struct GNUNET_CONFIGURATION_Handle *
240 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg, uint16_t * port, const char *hostname)
242 struct UpdateContext uc;
249 uc.ret = GNUNET_CONFIGURATION_create ();
250 uc.hostname = hostname;
252 GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
253 if (uc.nport >= HIGH_PORT)
256 GNUNET_CONFIGURATION_destroy (uc.ret);
260 if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
262 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
263 GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
264 GNUNET_free_non_null(control_host);
265 GNUNET_free(allowed_hosts);
269 /* arm needs to know to allow connections from the host on which it is running,
270 * otherwise gnunet-arm is unable to connect to it in some instances */
271 if (hostname != NULL)
273 GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
274 GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
275 GNUNET_free(allowed_hosts);
278 *port = (uint16_t) uc.nport;
283 * Add entries to the peers connected list
285 * @param pg the peer group we are working with
286 * @param first index of the first peer
287 * @param second index of the second peer
289 * @return the number of connections added (can be 0, 1 or 2)
291 * FIXME: add both, or only add one?
292 * - if both are added, then we have to keep track
293 * when connecting so we don't double connect
294 * - if only one is added, we need to iterate over
295 * both lists to find out if connection already exists
296 * - having both allows the whitelisting/friend file
297 * creation to be easier
299 * -- For now, add both, we have to iterate over each to
300 * check for duplicates anyways, so we'll take the performance
301 * hit assuming we don't have __too__ many connections
305 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
308 struct PeerConnection *first_iter;
309 struct PeerConnection *second_iter;
312 struct PeerConnection *new_first;
313 struct PeerConnection *new_second;
315 first_iter = pg->peers[first].connected_peers;
316 add_first = GNUNET_YES;
317 while (first_iter != NULL)
319 if (first_iter->daemon == pg->peers[second].daemon)
320 add_first = GNUNET_NO;
321 first_iter = first_iter->next;
324 second_iter = pg->peers[second].connected_peers;
325 add_second = GNUNET_YES;
326 while (second_iter != NULL)
328 if (second_iter->daemon == pg->peers[first].daemon)
329 add_second = GNUNET_NO;
330 second_iter = second_iter->next;
336 new_first = GNUNET_malloc(sizeof(struct PeerConnection));
337 new_first->daemon = pg->peers[second].daemon;
338 new_first->next = pg->peers[first].connected_peers;
339 pg->peers[first].connected_peers = new_first;
340 pg->peers[first].num_connections++;
346 new_second = GNUNET_malloc(sizeof(struct PeerConnection));
347 new_second->daemon = pg->peers[first].daemon;
348 new_second->next = pg->peers[second].connected_peers;
349 pg->peers[second].connected_peers = new_second;
350 pg->peers[first].num_connections++;
358 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg)
361 int total_connections;
364 int previous_total_connections;
368 GNUNET_assert(pg->total > 1);
370 /* Add a connection between the first two nodes */
371 total_connections = add_connections(pg, 0, 1);
373 for (outer_count = 1; outer_count < pg->total - 1; outer_count++)
375 previous_total_connections = total_connections;
376 for (i = 0; i < outer_count; i++)
378 probability = pg->peers[i].num_connections / previous_total_connections;
379 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
380 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
381 if (random < probability)
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "Connecting peer %d to peer %d\n",
388 total_connections += add_connections(pg, outer_count, i);
393 return total_connections;
397 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg)
402 unsigned int randomPeer;
403 double random, logNModifier, percentage;
404 unsigned int smallWorldConnections;
410 int connect_attempts;
412 logNModifier = 0.5; /* FIXME: default value? */
413 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
418 if (sscanf(p_string, "%lf", &logNModifier) != 1)
419 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
420 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
424 GNUNET_free (p_string);
426 percentage = 0.5; /* FIXME: default percentage? */
427 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
432 if (sscanf(p_string, "%lf", &percentage) != 1)
433 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
434 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
438 GNUNET_free (p_string);
440 natLog = log (pg->total);
441 connsPerPeer = ceil (natLog * logNModifier);
443 if (connsPerPeer % 2 == 1)
446 smallWorldConnections = 0;
447 connect_attempts = 0;
448 for (i = 0; i < pg->total; i++)
451 max = i + connsPerPeer / 2;
452 min = i - connsPerPeer / 2;
454 if (max > pg->total - 1)
456 max = max - pg->total;
462 min = pg->total - 1 + min;
466 for (j = 0; j < connsPerPeer / 2; j++)
468 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
469 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
470 if (random < percentage)
472 /* Connect to uniformly selected random peer */
474 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
476 while ((((randomPeer < max) && (randomPeer > min))
477 && (useAnd == 0)) || (((randomPeer > min)
478 || (randomPeer < max))
482 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
485 smallWorldConnections +=
486 add_connections (pg, i, randomPeer);
490 nodeToConnect = i + j + 1;
491 if (nodeToConnect > pg->total - 1)
493 nodeToConnect = nodeToConnect - pg->total;
496 add_connections (pg, i, nodeToConnect);
502 connect_attempts += smallWorldConnections;
504 return connect_attempts;
509 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg)
511 unsigned int outer_count, inner_count;
513 int connect_attempts;
514 double nat_percentage;
517 nat_percentage = 0.6; /* FIXME: default percentage? */
518 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
523 if (sscanf(p_string, "%lf", &nat_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);
534 cutoff = (unsigned int) (nat_percentage * pg->total);
536 connect_attempts = 0;
538 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
540 for (inner_count = outer_count + 1; inner_count < pg->total;
543 if ((outer_count > cutoff) || (inner_count > cutoff))
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "Connecting peer %d to peer %d\n",
548 outer_count, inner_count);
550 connect_attempts += add_connections(pg, outer_count, inner_count);
555 return connect_attempts;
562 create_small_world (struct GNUNET_TESTING_PeerGroup *pg)
564 unsigned int i, j, k;
568 unsigned int toggle = 1;
569 unsigned int nodeToConnect;
571 unsigned int node1Row;
572 unsigned int node1Col;
573 unsigned int node2Row;
574 unsigned int node2Col;
575 unsigned int distance;
576 double probability, random, percentage;
577 unsigned int smallWorldConnections;
579 int connect_attempts;
580 square = floor (sqrt (pg->total));
584 percentage = 0.5; /* FIXME: default percentage? */
585 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
590 if (sscanf(p_string, "%lf", &percentage) != 1)
591 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
592 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
596 GNUNET_free (p_string);
598 probability = 0.5; /* FIXME: default percentage? */
599 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
604 if (sscanf(p_string, "%lf", &probability) != 1)
605 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
606 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
610 GNUNET_free (p_string);
612 if (square * square != pg->total)
614 while (rows * cols < pg->total)
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
630 connect_attempts = 0;
631 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
632 * to the node to its right and above. Once this is over, we'll have our torus!
633 * Special case for the last node (if the rows and columns are not equal), connect
634 * to the first in the row to maintain topology.
636 for (i = 0; i < pg->total; i++)
638 /* First connect to the node to the right */
639 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
640 nodeToConnect = i + 1;
641 else if (i + 1 == pg->total)
642 nodeToConnect = rows * cols - cols;
644 nodeToConnect = i - cols + 1;
646 connect_attempts += add_connections (pg, i, nodeToConnect);
649 nodeToConnect = (rows * cols) - cols + i;
651 nodeToConnect = i - cols;
653 if (nodeToConnect < pg->total)
654 connect_attempts += add_connections (pg, i, nodeToConnect);
656 natLog = log (pg->total);
657 #if VERBOSE_TESTING > 2
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
659 _("natural log of %d is %d, will run %d iterations\n"),
660 pg->total, natLog, (int) (natLog * percentage));
661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
663 smallWorldConnections = 0;
664 for (i = 0; i < (int) (natLog * percentage); i++)
666 for (j = 0; j < pg->total; j++)
668 /* Determine the row and column of node at position j on the 2d torus */
670 node1Col = j - (node1Row * cols);
671 for (k = 0; k < pg->total; k++)
673 /* Determine the row and column of node at position k on the 2d torus */
675 node2Col = k - (node2Row * cols);
676 /* Simple Cartesian distance */
677 distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
680 /* Calculate probability as 1 over the square of the distance */
681 probability = 1.0 / (distance * distance);
682 /* Choose a random value between 0 and 1 */
683 random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
684 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
685 /* If random < probability, then connect the two nodes */
686 if (random < probability)
687 smallWorldConnections += add_connections (pg, j, k);
693 connect_attempts += smallWorldConnections;
694 #if VERBOSE_TESTING > 2
695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
696 _("Total connections added for small world: %d!\n"),
697 smallWorldConnections);
699 return connect_attempts;
705 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg)
708 unsigned int outer_count;
709 unsigned int inner_count;
710 int connect_attempts;
714 probability = 0.5; /* FIXME: default percentage? */
715 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
720 if (sscanf(p_string, "%lf", &probability) != 1)
721 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
722 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
726 GNUNET_free (p_string);
728 connect_attempts = 0;
729 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
731 for (inner_count = outer_count + 1; inner_count < pg->total;
734 temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
735 (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738 _("rand is %f probability is %f\n"), temp_rand,
741 if (temp_rand < probability)
743 connect_attempts += add_connections (pg, outer_count, inner_count);
748 return connect_attempts;
752 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg)
758 unsigned int toggle = 1;
759 unsigned int nodeToConnect;
760 int connect_attempts;
762 connect_attempts = 0;
764 square = floor (sqrt (pg->total));
768 if (square * square != pg->total)
770 while (rows * cols < pg->total)
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
785 /* Rows and columns are all sorted out, now iterate over all nodes and connect each
786 * to the node to its right and above. Once this is over, we'll have our torus!
787 * Special case for the last node (if the rows and columns are not equal), connect
788 * to the first in the row to maintain topology.
790 for (i = 0; i < pg->total; i++)
792 /* First connect to the node to the right */
793 if (((i + 1) % cols != 0) && (i + 1 != pg->total))
794 nodeToConnect = i + 1;
795 else if (i + 1 == pg->total)
796 nodeToConnect = rows * cols - cols;
798 nodeToConnect = i - cols + 1;
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "Connecting peer %d to peer %d\n",
804 connect_attempts += add_connections(pg, i, nodeToConnect);
806 /* Second connect to the node immediately above */
808 nodeToConnect = (rows * cols) - cols + i;
810 nodeToConnect = i - cols;
812 if (nodeToConnect < pg->total)
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816 "Connecting peer %d to peer %d\n",
819 connect_attempts += add_connections(pg, i, nodeToConnect);
824 return connect_attempts;
830 create_clique (struct GNUNET_TESTING_PeerGroup *pg)
832 unsigned int outer_count;
833 unsigned int inner_count;
834 int connect_attempts;
836 connect_attempts = 0;
838 for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
840 for (inner_count = outer_count + 1; inner_count < pg->total;
844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
845 "Connecting peer %d to peer %d\n",
846 outer_count, inner_count);
848 connect_attempts += add_connections(pg, outer_count, inner_count);
852 return connect_attempts;
857 create_ring (struct GNUNET_TESTING_PeerGroup *pg)
860 int connect_attempts;
862 connect_attempts = 0;
864 /* Connect each peer to the next highest numbered peer */
865 for (count = 0; count < pg->total - 1; count++)
868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
869 "Connecting peer %d to peer %d\n",
872 connect_attempts += add_connections(pg, count, count + 1);
875 /* Connect the last peer to the first peer */
876 connect_attempts += add_connections(pg, pg->total - 1, 0);
878 return connect_attempts;
883 * Create the friend files based on the PeerConnection's
884 * of each peer in the peer group, and copy the files
885 * to the appropriate place
887 * @param pg the peer group we are dealing with
890 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
892 FILE *temp_friend_handle;
893 unsigned int pg_iter;
894 struct PeerConnection *connection_iter;
895 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
896 char *temp_service_path;
899 struct GNUNET_PeerIdentity *temppeer;
901 enum GNUNET_OS_ProcessStatusType type;
902 unsigned long return_code;
907 pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
908 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
910 mytemp = GNUNET_DISK_mktemp("friends");
911 temp_friend_handle = fopen (mytemp, "wt");
912 connection_iter = pg->peers[pg_iter].connected_peers;
913 while (connection_iter != NULL)
915 temppeer = &connection_iter->daemon->id;
916 GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
917 fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
918 connection_iter = connection_iter->next;
921 fclose(temp_friend_handle);
924 GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
926 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
927 _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
930 if (UNLINK (mytemp) != 0)
931 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
932 GNUNET_free (mytemp);
936 if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
938 GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
939 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
940 "mv", mytemp, arg, NULL);
942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943 _("Copying file with command cp %s %s\n"), mytemp, arg);
948 else /* Remote, scp the file to the correct place */
950 if (NULL != pg->peers[pg_iter].daemon->username)
951 GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
953 GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
954 pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
955 "scp", mytemp, arg, NULL);
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
959 _("Copying file with command scp %s %s\n"), mytemp, arg);
963 GNUNET_free (temp_service_path);
964 GNUNET_free (mytemp);
969 while ((count < max_wait) && (ret != GNUNET_OK))
972 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
976 _("Checking copy status of file %d\n"), pg_iter);
978 if (pidarr[pg_iter] != 0) /* Check for already completed! */
980 if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
984 else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993 _("File %d copied\n"), pg_iter);
999 if (ret == GNUNET_SYSERR)
1006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1007 _("Finished copying all friend files!\n"));
1009 GNUNET_free(pidarr);
1014 * Internal notification of a connection, kept so that we can ensure some connections
1015 * happen instead of flooding all testing daemons with requests to connect.
1017 static void internal_connect_notify (void *cls,
1018 const struct GNUNET_PeerIdentity *first,
1019 const struct GNUNET_PeerIdentity *second,
1020 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
1021 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
1022 struct GNUNET_TESTING_Daemon *first_daemon,
1023 struct GNUNET_TESTING_Daemon *second_daemon,
1026 struct GNUNET_TESTING_PeerGroup *pg = cls;
1027 outstanding_connects--;
1029 pg->notify_connection(pg->notify_connection_cls, first, second, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
1033 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1035 struct ConnectContext *connect_context = cls;
1037 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1040 if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
1042 #if VERBOSE_TESTING > 2
1043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1044 _("Delaying connect, we have too many outstanding connections!\n"));
1046 GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &schedule_connect, connect_context);
1050 #if VERBOSE_TESTING > 2
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052 _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
1054 outstanding_connects++;
1055 GNUNET_TESTING_daemons_connect (connect_context->first,
1056 connect_context->second,
1059 &internal_connect_notify,
1060 connect_context->pg);
1061 GNUNET_free(connect_context);
1066 * Connect the topology as specified by the PeerConnection's
1067 * of each peer in the peer group
1069 * @param pg the peer group we are dealing with
1072 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
1074 unsigned int pg_iter;
1075 struct PeerConnection *connection_iter;
1076 struct ConnectContext *connect_context;
1078 for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1080 connection_iter = pg->peers[pg_iter].connected_peers;
1081 while (connection_iter != NULL)
1083 connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1084 connect_context->pg = pg;
1085 connect_context->first = pg->peers[pg_iter].daemon;
1086 connect_context->second = connection_iter->daemon;
1087 GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
1088 connection_iter = connection_iter->next;
1095 * Takes a peer group and attempts to create a topology based on the
1096 * one specified in the configuration file. Returns the number of connections
1097 * that will attempt to be created, but this will happen asynchronously(?) so
1098 * the caller will have to keep track (via the callback) of whether or not
1099 * the connection actually happened.
1101 * @param pg the peer group struct representing the running peers
1103 * @return the number of connections should be created by the topology, so the
1104 * caller knows how many to wait for (if it so chooses)
1108 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg)
1110 unsigned long long topology_num;
1112 int num_connections;
1114 GNUNET_assert (pg->notify_connection != NULL);
1117 GNUNET_CONFIGURATION_get_value_number (pg->cfg, "testing", "topology",
1120 switch (topology_num)
1122 case GNUNET_TESTING_TOPOLOGY_CLIQUE:
1124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1125 _("Creating clique topology\n"));
1127 num_connections = create_clique (pg);
1129 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 _("Creating small world (ring) topology\n"));
1134 num_connections = create_small_world_ring (pg);
1136 case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1139 _("Creating small world (2d-torus) topology\n"));
1141 num_connections = create_small_world (pg);
1143 case GNUNET_TESTING_TOPOLOGY_RING:
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146 _("Creating ring topology\n"));
1148 num_connections = create_ring (pg);
1150 case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 _("Creating 2d torus topology\n"));
1155 num_connections = create_2d_torus (pg);
1157 case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1160 _("Creating Erdos-Renyi topology\n"));
1162 num_connections = create_erdos_renyi (pg);
1164 case GNUNET_TESTING_TOPOLOGY_INTERNAT:
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1167 _("Creating InterNAT topology\n"));
1169 num_connections = create_nated_internet (pg);
1171 case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1174 _("Creating Scale Free topology\n"));
1176 num_connections = create_scale_free (pg);
1178 case GNUNET_TESTING_TOPOLOGY_NONE:
1179 num_connections = 0;
1182 num_connections = 0;
1185 if (num_connections < 1)
1186 return GNUNET_SYSERR;
1188 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
1189 ret = create_and_copy_friend_files(pg);
1190 if (ret == GNUNET_OK)
1191 connect_topology(pg);
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196 _("Failed during friend file copying!\n"));
1198 return GNUNET_SYSERR;
1203 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1204 _("No topology specified, was one intended?\n"));
1205 return GNUNET_SYSERR;
1208 return num_connections;
1212 * Start count gnunetd processes with the same set of transports and
1213 * applications. The port numbers (any option called "PORT") will be
1214 * adjusted to ensure that no two peers running on the same system
1215 * have the same port(s) in their respective configurations.
1217 * @param sched scheduler to use
1218 * @param cfg configuration template to use
1219 * @param total number of daemons to start
1220 * @param cb function to call on each daemon that was started
1221 * @param cb_cls closure for cb
1222 * @param connect_callback function to call each time two hosts are connected
1223 * @param connect_callback_cls closure for connect_callback
1224 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
1225 * everything on localhost).
1226 * @return NULL on error, otherwise handle to control peer group
1228 struct GNUNET_TESTING_PeerGroup *
1229 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
1230 const struct GNUNET_CONFIGURATION_Handle *cfg,
1232 GNUNET_TESTING_NotifyDaemonRunning cb,
1234 GNUNET_TESTING_NotifyConnection
1235 connect_callback, void *connect_callback_cls,
1236 const char *hostnames)
1238 struct GNUNET_TESTING_PeerGroup *pg;
1242 const char *hostname;
1243 char *baseservicehome;
1244 char *newservicehome;
1246 struct GNUNET_CONFIGURATION_Handle *pcfg;
1248 unsigned int hostcnt;
1257 pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
1261 pg->cb_cls = cb_cls;
1262 pg->notify_connection = connect_callback;
1263 pg->notify_connection_cls = connect_callback_cls;
1265 pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
1266 if (NULL != hostnames)
1269 /* skip leading spaces */
1270 while ((0 != *hostnames) && (isspace (*hostnames)))
1273 while ('\0' != *rpos)
1275 if (isspace (*rpos))
1279 pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
1281 start = GNUNET_strdup (hostnames);
1283 while ('\0' != *pos)
1288 if (strlen (start) > 0)
1290 pg->hosts[off].minport = LOW_PORT;
1291 pg->hosts[off++].hostname = start;
1297 if (strlen (start) > 0)
1299 pg->hosts[off].minport = LOW_PORT;
1300 pg->hosts[off++].hostname = start;
1304 GNUNET_free (start);
1305 GNUNET_free (pg->hosts);
1309 minport = 0; /* make gcc happy */
1316 for (off = 0; off < total; off++)
1320 hostname = pg->hosts[off % hostcnt].hostname;
1321 pcfg = make_config (cfg, &pg->hosts[off % hostcnt].minport, hostname);
1326 pcfg = make_config (cfg, &minport, hostname);
1331 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1333 ("Could not create configuration for peer number %u on `%s'!\n"),
1334 off, hostname == NULL ? "localhost" : hostname);
1339 GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
1342 GNUNET_asprintf (&newservicehome,
1343 "%s/%d/", baseservicehome, off);
1344 GNUNET_free (baseservicehome);
1348 tmpdir = getenv ("TMPDIR");
1349 tmpdir = tmpdir ? tmpdir : "/tmp";
1350 GNUNET_asprintf (&newservicehome,
1353 "gnunet-testing-test-test", off);
1355 GNUNET_CONFIGURATION_set_value_string (pcfg,
1357 "SERVICEHOME", newservicehome);
1358 GNUNET_free (newservicehome);
1359 pg->peers[off].cfg = pcfg;
1360 pg->peers[off].daemon = GNUNET_TESTING_daemon_start (sched,
1364 if (NULL == pg->peers[off].daemon)
1365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1366 _("Could not start peer number %u!\n"), off);
1372 * Get a daemon by number, so callers don't have to do nasty
1373 * offsetting operation.
1375 struct GNUNET_TESTING_Daemon *
1376 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
1378 if (position < pg->total)
1379 return pg->peers[position].daemon;
1385 * Shutdown all peers started in the given group.
1387 * @param pg handle to the peer group
1390 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg)
1393 struct PeerConnection *pos;
1394 struct PeerConnection *next;
1396 for (off = 0; off < pg->total; off++)
1398 /* FIXME: should we wait for our
1399 continuations to be called here? This
1400 would require us to take a continuation
1403 if (NULL != pg->peers[off].daemon)
1404 GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, NULL, NULL);
1405 if (NULL != pg->peers[off].cfg)
1406 GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
1408 pos = pg->peers[off].connected_peers;
1417 GNUNET_free (pg->peers);
1418 if (NULL != pg->hosts)
1420 GNUNET_free (pg->hosts[0].hostname);
1421 GNUNET_free (pg->hosts);
1427 /* end of testing_group.c */