2 This file is part of GNUnet
3 (C) 2008--2012 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 3, 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 testbed/testbed_api_testbed.c
23 * @brief high-level testbed management
24 * @author Christian Grothoff
25 * @author Sree Harsha Totakura
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31 #include "testbed_api_peers.h"
32 #include "testbed_api_hosts.h"
33 #include "testbed_api_topology.h"
36 * Generic loggins shorthand
38 #define LOG(kind,...) \
39 GNUNET_log_from (kind, "testbed-api-testbed", __VA_ARGS__)
42 * Debug logging shortcut
45 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
53 * The testbed operation handle
55 struct GNUNET_TESTBED_Operation *op;
58 * Context information for GNUNET_TESTBED_run()
60 struct RunContext *rc;
68 * The next pointer for DLL
70 struct DLLOperation *next;
73 * The prev pointer for DLL
75 struct DLLOperation *prev;
80 * States of RunContext
90 * Controllers on given hosts started and linked
100 * The testbed run is ready and the master callback can be called now. At this
101 * time the peers are all started and if a topology is provided in the
102 * configuration the topology would have been attempted
112 * Peers are destroyed
124 * The controller handle
126 struct GNUNET_TESTBED_Controller *c;
129 * The configuration of the controller. This is based on the cfg given to the
130 * function GNUNET_TESTBED_run(). We also use this config as a template while
133 struct GNUNET_CONFIGURATION_Handle *cfg;
136 * Handle to the host on which the controller runs
138 struct GNUNET_TESTBED_Host *h;
141 * The handle to the controller process
143 struct GNUNET_TESTBED_ControllerProc *cproc;
146 * The callback to use as controller callback
148 GNUNET_TESTBED_ControllerCallback cc;
151 * The pointer to the controller callback
156 * The trusted IP string
161 * TestMaster callback to call when testbed initialization is done
163 GNUNET_TESTBED_TestMaster test_master;
166 * The closure for the TestMaster callback
168 void *test_master_cls;
171 * The head element of DLL operations
173 struct DLLOperation *dll_op_head;
176 * The tail element of DLL operations
178 struct DLLOperation *dll_op_tail;
181 * An array of hosts loaded from the hostkeys file
183 struct GNUNET_TESTBED_Host **hosts;
186 * The handle for whether a host is habitable or not
188 struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handles;
191 * Array of peers which we create
193 struct GNUNET_TESTBED_Peer **peers;
196 * The topology generation operation. Will be null if no topology is set in
199 struct GNUNET_TESTBED_Operation *topology_operation;
202 * The file containing topology data. Only used if the topology is set to 'FROM_FILE'
207 * Host registration handle
209 struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
212 * Profiling start time
214 struct GNUNET_TIME_Absolute pstart_time;
217 * Host registration task
219 GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
222 * Task to be run while shutting down
224 GNUNET_SCHEDULER_TaskIdentifier shutdown_run_task;
227 * The event mask for the controller
232 * State of this context
237 * The topology which has to be achieved with the peers started in this context
239 enum GNUNET_TESTBED_TopologyOption topology;
242 * Have we already shutdown
247 * Number of hosts in the given host file
249 unsigned int num_hosts;
252 * Number of registered hosts. Also used as a counter while checking
253 * habitabillity of hosts
255 unsigned int reg_hosts;
258 * Current peer count for an operation; Set this to 0 and increment for each
259 * successful operation on a peer
261 unsigned int peer_count;
264 * number of peers to start
266 unsigned int num_peers;
269 * Expected overlay connects. Should be zero if no topology is relavant
274 * Number of random links to established
276 unsigned int random_links;
282 * Function to return the string representation of the duration between current
283 * time and `pstart_time' in `RunContext'
285 * @param rc the RunContext
286 * @return the representation string; this is NOT reentrant
289 prof_time (struct RunContext *rc)
291 struct GNUNET_TIME_Relative ptime;
293 ptime = GNUNET_TIME_absolute_get_duration (rc->pstart_time);
294 return GNUNET_STRINGS_relative_time_to_string (ptime, GNUNET_YES);
299 * Task for starting peers
301 * @param cls the RunHandle
302 * @param tc the task context from scheduler
305 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
307 struct RunContext *rc = cls;
308 struct DLLOperation *dll_op;
311 DEBUG ("Starting Peers\n");
312 rc->pstart_time = GNUNET_TIME_absolute_get ();
313 for (peer = 0; peer < rc->num_peers; peer++)
315 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
316 dll_op->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL);
317 dll_op->cls = rc->peers[peer];
318 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
325 * Functions of this signature are called when a peer has been successfully
328 * @param cls the closure from GNUNET_TESTBED_peer_create()
329 * @param peer the handle for the created peer; NULL on any error during
331 * @param emsg NULL if peer is not NULL; else MAY contain the error description
334 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
336 struct DLLOperation *dll_op = cls;
337 struct RunContext *rc;
339 GNUNET_assert (NULL != dll_op);
341 GNUNET_assert (NULL != rc);
342 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
343 GNUNET_TESTBED_operation_done (dll_op->op);
344 GNUNET_free (dll_op);
348 LOG (GNUNET_ERROR_TYPE_WARNING, "Error while creating a peer: %s\n",
350 /* FIXME: GNUNET_TESTBED_shutdown_run()? */
353 rc->peers[rc->peer_count] = peer;
355 if (rc->peer_count < rc->num_peers)
357 DEBUG ("%u peers created in %s\n", rc->num_peers, prof_time (rc));
358 rc->state = RC_PEERS_CREATED;
359 GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
364 * Assuming all peers have been destroyed cleanup run handle
366 * @param cls the run handle
367 * @param tc the task context from scheduler
370 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
372 struct RunContext *rc = cls;
373 struct DLLOperation *dll_op;
376 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rc->register_hosts_task);
377 GNUNET_assert (NULL == rc->reg_handle);
378 GNUNET_assert (NULL == rc->peers);
379 GNUNET_assert (NULL == rc->hc_handles);
380 GNUNET_assert (RC_PEERS_DESTROYED == rc->state);
381 if (NULL != rc->dll_op_head)
382 { /* cancel our pending operations */
383 while (NULL != (dll_op = rc->dll_op_head))
385 GNUNET_TESTBED_operation_done (dll_op->op);
386 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
387 GNUNET_free (dll_op);
391 GNUNET_TESTBED_controller_disconnect (rc->c);
392 if (NULL != rc->cproc)
393 GNUNET_TESTBED_controller_stop (rc->cproc);
395 GNUNET_TESTBED_host_destroy (rc->h);
396 for (hid = 0; hid < rc->num_hosts; hid++)
397 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
398 GNUNET_free_non_null (rc->hosts);
400 GNUNET_CONFIGURATION_destroy (rc->cfg);
401 GNUNET_free_non_null (rc->topo_file);
402 GNUNET_free_non_null (rc->trusted_ip);
408 * Stops the testbed run and releases any used resources
410 * @param cls the tesbed run handle
411 * @param tc the task context from scheduler
414 shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
418 * Function to shutdown now
420 * @param rc the RunContext
423 shutdown_now (struct RunContext *rc)
425 if (GNUNET_YES == rc->shutdown)
427 if (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task)
428 GNUNET_SCHEDULER_cancel (rc->shutdown_run_task);
429 rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc);
434 * Stops the testbed run and releases any used resources
436 * @param cls the tesbed run handle
437 * @param tc the task context from scheduler
440 shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
442 struct RunContext *rc = cls;
443 struct DLLOperation *dll_op;
444 int all_peers_destroyed;
448 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task);
449 rc->shutdown_run_task = GNUNET_SCHEDULER_NO_TASK;
450 GNUNET_assert (GNUNET_NO == rc->shutdown);
451 rc->shutdown = GNUNET_YES;
452 if (NULL != rc->hc_handles)
454 for (nhost = 0; nhost < rc->num_hosts; nhost++)
455 if (NULL != rc->hc_handles[nhost])
456 GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
457 GNUNET_free (rc->hc_handles);
458 rc->hc_handles = NULL;
460 /* Stop register hosts task if it is running */
461 if (GNUNET_SCHEDULER_NO_TASK != rc->register_hosts_task)
463 GNUNET_SCHEDULER_cancel (rc->register_hosts_task);
464 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
466 if (NULL != rc->reg_handle)
468 GNUNET_TESTBED_cancel_registration (rc->reg_handle);
469 rc->reg_handle = NULL;
473 if (NULL != rc->peers)
475 if (NULL != rc->topology_operation)
477 GNUNET_TESTBED_operation_done (rc->topology_operation);
478 rc->topology_operation = NULL;
480 if (RC_INIT == rc->state)
481 rc->state = RC_READY; /* Even though we haven't called the master callback */
483 /* Check if some peers are stopped */
484 for (peer = 0; peer < rc->num_peers; peer++)
486 if (NULL == rc->peers[peer])
488 if (PS_STOPPED != rc->peers[peer]->state)
491 if (peer == rc->num_peers)
493 /* All peers are stopped */
494 rc->state = RC_PEERS_STOPPED;
495 all_peers_destroyed = GNUNET_YES;
496 for (peer = 0; peer < rc->num_peers; peer++)
498 if (NULL == rc->peers[peer])
500 all_peers_destroyed = GNUNET_NO;
501 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
502 dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer]);
503 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
506 if (all_peers_destroyed == GNUNET_NO)
508 DEBUG ("Destroying peers\n");
509 rc->pstart_time = GNUNET_TIME_absolute_get ();
513 /* Some peers are stopped */
514 DEBUG ("Stopping peers\n");
515 rc->pstart_time = GNUNET_TIME_absolute_get ();
516 for (peer = 0; peer < rc->num_peers; peer++)
518 if ((NULL == rc->peers[peer]) || (PS_STARTED != rc->peers[peer]->state))
523 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
524 dll_op->op = GNUNET_TESTBED_peer_stop (rc->peers[peer], NULL, NULL);
525 dll_op->cls = rc->peers[peer];
526 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
529 if (rc->peer_count != rc->num_peers)
531 GNUNET_free (rc->peers);
535 rc->state = RC_PEERS_DESTROYED; /* No peers are present so we consider the
536 * state where all peers are destroyed */
537 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
542 * Task to call master task
544 * @param cls the run context
545 * @param tc the task context
548 call_master (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
550 struct RunContext *rc = cls;
552 if (NULL != rc->topology_operation)
554 DEBUG ("Overlay topology generated in %s\n", prof_time (rc));
555 GNUNET_TESTBED_operation_done (rc->topology_operation);
556 rc->topology_operation = NULL;
558 if (NULL != rc->test_master)
559 rc->test_master (rc->test_master_cls, rc->num_peers, rc->peers);
564 * Callbacks of this type are called when topology configuration is completed
566 * @param cls the operation closure given to
567 * GNUNET_TESTBED_overlay_configure_topology_va() and
568 * GNUNET_TESTBED_overlay_configure() calls
569 * @param nsuccess the number of successful overlay connects
570 * @param nfailures the number of overlay connects which failed
573 topology_completion_callback (void *cls, unsigned int nsuccess,
574 unsigned int nfailures)
576 struct RunContext *rc = cls;
578 rc->state = RC_READY;
579 GNUNET_SCHEDULER_add_continuation (&call_master, rc,
580 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
585 * Function to create peers
587 * @param rc the RunContext
590 create_peers (struct RunContext *rc)
592 struct DLLOperation *dll_op;
595 DEBUG ("Creating peers\n");
596 rc->pstart_time = GNUNET_TIME_absolute_get ();
598 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
599 GNUNET_assert (NULL != rc->c);
601 for (peer = 0; peer < rc->num_peers; peer++)
603 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
606 GNUNET_TESTBED_peer_create (rc->c,
608 rc->num_hosts) ? rc->h : rc->hosts[peer %
610 rc->cfg, peer_create_cb, dll_op);
611 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
617 * Signature of the event handler function called by the
618 * respective event controller.
621 * @param event information about the event
624 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
626 struct RunContext *rc = cls;
627 struct DLLOperation *dll_op;
628 unsigned int peer_id;
630 if (RC_INIT == rc->state)
634 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
635 dll_op = event->details.operation_finished.op_cls;
636 if (NULL != event->details.operation_finished.emsg)
638 LOG (GNUNET_ERROR_TYPE_ERROR, _("Linking controllers failed. Exiting"));
643 GNUNET_assert (event->details.operation_finished.operation == dll_op->op);
644 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
645 GNUNET_TESTBED_operation_done (dll_op->op);
646 GNUNET_free (dll_op);
647 if (rc->reg_hosts == rc->num_hosts)
649 rc->state = RC_LINKED;
659 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
661 if ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) &&
662 (event->details.operation_finished.operation == dll_op->op))
664 if ((GNUNET_TESTBED_ET_PEER_STOP == event->type) &&
665 (event->details.peer_stop.peer == dll_op->cls))
670 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
671 GNUNET_TESTBED_operation_done (dll_op->op);
672 GNUNET_free (dll_op);
674 if (rc->peer_count < rc->num_peers)
678 case RC_PEERS_CREATED:
680 rc->state = RC_PEERS_STOPPED;
681 DEBUG ("Peers stopped in %s\n", prof_time (rc));
682 DEBUG ("Destroying peers\n");
683 rc->pstart_time = GNUNET_TIME_absolute_get ();
685 for (peer_id = 0; peer_id < rc->num_peers; peer_id++)
687 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
688 dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer_id]);
689 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
693 case RC_PEERS_STOPPED:
694 rc->state = RC_PEERS_DESTROYED;
695 GNUNET_free (rc->peers);
697 DEBUG ("Peers destroyed in %s\n", prof_time (rc));
698 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
706 if ((0 != (rc->event_mask & (1LL << event->type))) && (NULL != rc->cc))
707 rc->cc (rc->cc_cls, event);
708 if (GNUNET_TESTBED_ET_PEER_START != event->type)
710 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
711 if ((NULL != dll_op->cls) &&
712 (event->details.peer_start.peer == dll_op->cls))
714 if (NULL == dll_op) /* Not our operation */
716 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
717 GNUNET_TESTBED_operation_done (dll_op->op);
718 GNUNET_free (dll_op);
720 if (rc->peer_count < rc->num_peers)
722 DEBUG ("%u peers started in %s\n", rc->num_peers, prof_time (rc));
723 if (GNUNET_TESTBED_TOPOLOGY_NONE != rc->topology)
725 if ((GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology) ||
726 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology) ||
727 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD == rc->topology))
729 rc->topology_operation =
730 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
731 rc->peers, &rc->num_oc,
732 &topology_completion_callback,
736 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
738 else if (GNUNET_TESTBED_TOPOLOGY_FROM_FILE == rc->topology)
740 GNUNET_assert (NULL != rc->topo_file);
741 rc->topology_operation =
742 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
743 rc->peers, &rc->num_oc,
744 &topology_completion_callback,
748 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
751 rc->topology_operation =
752 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
753 rc->peers, &rc->num_oc,
754 &topology_completion_callback,
757 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
758 if (NULL == rc->topology_operation)
759 LOG (GNUNET_ERROR_TYPE_WARNING,
760 "Not generating topology. Check number of peers\n");
763 DEBUG ("Creating overlay topology\n");
764 rc->pstart_time = GNUNET_TIME_absolute_get ();
768 rc->state = RC_READY;
769 GNUNET_SCHEDULER_add_continuation (&call_master, rc,
770 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
775 * Task to register all hosts available in the global host list
777 * @param cls the RunContext
778 * @param tc the scheduler task context
781 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
785 * Callback which will be called to after a host registration succeeded or failed
787 * @param cls the closure
788 * @param emsg the error message; NULL if host registration is successful
791 host_registration_completion (void *cls, const char *emsg)
793 struct RunContext *rc = cls;
795 rc->reg_handle = NULL;
798 LOG (GNUNET_ERROR_TYPE_WARNING,
799 _("Host registration failed for a host. Error: %s\n"), emsg);
803 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
808 * Task to register all hosts available in the global host list
810 * @param cls RunContext
811 * @param tc the scheduler task context
814 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
816 struct RunContext *rc = cls;
817 struct DLLOperation *dll_op;
820 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
821 if (rc->reg_hosts == rc->num_hosts)
823 DEBUG ("All hosts successfully registered\n");
825 for (slave = 0; slave < rc->num_hosts; slave++)
827 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
830 GNUNET_TESTBED_controller_link (dll_op, rc->c, rc->hosts[slave],
831 rc->h, rc->cfg, GNUNET_YES);
832 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
839 GNUNET_TESTBED_register_host (rc->c, rc->hosts[rc->reg_hosts],
840 host_registration_completion, rc);
846 * Callback to signal successfull startup of the controller process
848 * @param cls the closure from GNUNET_TESTBED_controller_start()
849 * @param cfg the configuration with which the controller has been started;
850 * NULL if status is not GNUNET_OK
851 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
852 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
855 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
858 struct RunContext *rc = cls;
861 if (status != GNUNET_OK)
866 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Testbed startup failed\n");
869 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
870 "Controller crash detected. Shutting down.\n");
876 GNUNET_CONFIGURATION_destroy (rc->cfg);
877 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
878 event_mask = rc->event_mask;
879 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
880 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
881 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
882 if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
883 event_mask |= GNUNET_TESTBED_ET_CONNECT;
885 GNUNET_TESTBED_controller_connect (rc->cfg, rc->h, event_mask, &event_cb,
887 if (0 < rc->num_hosts)
890 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
893 rc->state = RC_LINKED;
899 * Callback function invoked for each interface found.
902 * @param name name of the interface (can be NULL for unknown)
903 * @param isDefault is this presumably the default interface
904 * @param addr address of this interface (can be NULL for unknown or unassigned)
905 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
906 * @param netmask the network mask (can be NULL for unknown or unassigned))
907 * @param addrlen length of the address
908 * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
911 netint_proc (void *cls, const char *name, int isDefault,
912 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
913 const struct sockaddr *netmask, socklen_t addrlen)
915 struct RunContext *rc = cls;
916 char hostip[NI_MAXHOST];
919 if (sizeof (struct sockaddr_in) != addrlen)
920 return GNUNET_OK; /* Only consider IPv4 for now */
922 getnameinfo (addr, addrlen, hostip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
923 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getnameinfo");
924 if (NULL == rc->trusted_ip)
926 rc->trusted_ip = GNUNET_strdup (hostip);
929 (void) GNUNET_asprintf (&buf, "%s; %s", rc->trusted_ip, hostip);
930 GNUNET_free (rc->trusted_ip);
931 rc->trusted_ip = buf;
937 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
938 * inform whether the given host is habitable or not. The Handle returned by
939 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
942 * @param host the host whose status is being reported; will be NULL if the host
943 * given to GNUNET_TESTBED_is_host_habitable() is NULL
944 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
947 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host,
950 struct RunContext *rc = cls;
951 struct GNUNET_TESTBED_Host **old_hosts;
954 for (nhost = 0; nhost < rc->num_hosts; nhost++)
956 if (host == rc->hosts[nhost])
959 GNUNET_assert (nhost != rc->num_hosts);
960 rc->hc_handles[nhost] = NULL;
961 if (GNUNET_NO == status)
963 if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
964 LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
965 GNUNET_TESTBED_host_get_hostname (host));
967 LOG (GNUNET_ERROR_TYPE_ERROR,
968 _("Testbed cannot be started on localhost\n"));
973 if (rc->reg_hosts < rc->num_hosts)
975 GNUNET_free (rc->hc_handles);
976 rc->hc_handles = NULL;
977 rc->h = rc->hosts[0];
979 if (0 < rc->num_hosts)
981 old_hosts = rc->hosts;
983 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts);
984 memcpy (rc->hosts, &old_hosts[1],
985 (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts));
986 GNUNET_free (old_hosts);
990 GNUNET_free (rc->hosts);
993 GNUNET_OS_network_interfaces_list (netint_proc, rc);
994 if (NULL == rc->trusted_ip)
995 rc->trusted_ip = GNUNET_strdup ("127.0.0.1");
997 GNUNET_TESTBED_controller_start (rc->trusted_ip, rc->h, rc->cfg,
998 &controller_status_cb, rc);
999 GNUNET_free (rc->trusted_ip);
1000 rc->trusted_ip = NULL;
1001 if (NULL == rc->cproc)
1003 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot start the master controller"));
1010 * Convenience method for running a testbed with
1011 * a single call. Underlay and overlay topology
1012 * are configured using the "UNDERLAY" and "OVERLAY"
1013 * options in the "[testbed]" section of the configuration\
1014 * (with possible options given in "UNDERLAY_XXX" and/or
1017 * The testbed is to be terminated using a call to
1018 * "GNUNET_SCHEDULER_shutdown".
1020 * @param host_filename name of the file with the 'hosts', NULL
1021 * to run everything on 'localhost'
1022 * @param cfg configuration to use (for testbed, controller and peers)
1023 * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
1024 * @param event_mask bit mask with set of events to call 'cc' for;
1025 * or-ed values of "1LL" shifted by the
1026 * respective 'enum GNUNET_TESTBED_EventType'
1027 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
1028 * @param cc controller callback to invoke on events; This callback is called
1029 * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
1030 * set in the event_mask as this is the only way get access to the
1031 * handle of each peer
1032 * @param cc_cls closure for cc
1033 * @param test_master this callback will be called once the test is ready
1034 * @param test_master_cls closure for 'test_master'.
1037 GNUNET_TESTBED_run (const char *host_filename,
1038 const struct GNUNET_CONFIGURATION_Handle *cfg,
1039 unsigned int num_peers, uint64_t event_mask,
1040 GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
1041 GNUNET_TESTBED_TestMaster test_master,
1042 void *test_master_cls)
1044 struct RunContext *rc;
1046 unsigned long long random_links;
1050 GNUNET_assert (num_peers > 0);
1051 rc = GNUNET_malloc (sizeof (struct RunContext));
1052 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
1053 if (NULL != host_filename)
1056 GNUNET_TESTBED_hosts_load_from_file (host_filename, rc->cfg,
1058 if (0 == rc->num_hosts)
1060 LOG (GNUNET_ERROR_TYPE_WARNING,
1061 _("No hosts loaded. Need at least one host\n"));
1066 rc->h = GNUNET_TESTBED_host_create (NULL, NULL, rc->cfg, 0);
1067 rc->num_peers = num_peers;
1068 rc->event_mask = event_mask;
1070 rc->cc_cls = cc_cls;
1071 rc->test_master = test_master;
1072 rc->test_master_cls = test_master_cls;
1073 rc->state = RC_INIT;
1074 rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;
1076 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1077 "OVERLAY_TOPOLOGY", &topology))
1079 if (GNUNET_NO == GNUNET_TESTBED_topology_get_ (&rc->topology, topology))
1081 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "testbed",
1084 ("Specified topology must be supported by testbed"));
1086 GNUNET_free (topology);
1088 switch (rc->topology)
1090 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
1091 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
1092 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
1094 GNUNET_CONFIGURATION_get_value_number (rc->cfg, "testbed",
1095 "OVERLAY_RANDOM_LINKS",
1098 /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
1099 * option to be set to the number of random links to be established */
1100 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1101 "OVERLAY_RANDOM_LINKS");
1104 if (random_links > UINT32_MAX)
1106 GNUNET_break (0); /* Too big number */
1109 rc->random_links = (unsigned int) random_links;
1111 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
1113 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1114 "OVERLAY_TOPOLOGY_FILE",
1117 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1118 "OVERLAY_TOPOLOGY_FILE");
1122 /* Warn if OVERLAY_RANDOM_LINKS is present that it will be ignored */
1124 GNUNET_CONFIGURATION_have_value (rc->cfg, "testbed",
1125 "OVERLAY_RANDOM_LINKS"))
1126 LOG (GNUNET_ERROR_TYPE_WARNING,
1127 "Ignoring value of `OVERLAY_RANDOM_LINKS' in given configuration\n");
1130 if (NULL != host_filename)
1133 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostHabitableCheckHandle *)
1135 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1138 (rc->hc_handles[nhost] =
1139 GNUNET_TESTBED_is_host_habitable (rc->hosts[nhost], rc->cfg,
1140 &host_habitable_cb, rc)))
1143 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1144 if (NULL != rc->hc_handles[nhost])
1145 GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
1146 GNUNET_free (rc->hc_handles);
1147 rc->hc_handles = NULL;
1154 GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h, rc->cfg,
1155 &controller_status_cb, rc);
1156 rc->shutdown_run_task =
1157 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_run,
1163 GNUNET_TESTBED_host_destroy (rc->h);
1164 if (NULL != rc->hosts)
1166 for (hid = 0; hid < rc->num_hosts; hid++)
1167 if (NULL != rc->hosts[hid])
1168 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
1169 GNUNET_free (rc->hosts);
1171 if (NULL != rc->cfg)
1172 GNUNET_CONFIGURATION_destroy (rc->cfg);
1177 /* end of testbed_api_testbed.c */