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_hosts.c
23 * @brief API for manipulating 'hosts' controlled by the GNUnet testing service;
24 * allows parsing hosts files, starting, stopping and communicating (via
25 * SSH/stdin/stdout) with the remote (or local) processes
26 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31 #include "gnunet_core_service.h"
32 #include "gnunet_transport_service.h"
34 #include "testbed_api.h"
35 #include "testbed_api_hosts.h"
36 #include "testbed_api_operations.h"
37 #include "testbed_api_sd.h"
42 * Generic logging shorthand
44 #define LOG(kind, ...) \
45 GNUNET_log_from (kind, "testbed-api-hosts", __VA_ARGS__);
48 * Debug logging shorthand
50 #define LOG_DEBUG(...) \
51 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
54 * Number of extra elements we create space for when we grow host list
56 #define HOST_LIST_GROW_STEP 10
60 * A list entry for registered controllers list
62 struct RegisteredController
65 * The controller at which this host is registered
67 const struct GNUNET_TESTBED_Controller *controller;
70 * The next ptr for DLL
72 struct RegisteredController *next;
75 * The prev ptr for DLL
77 struct RegisteredController *prev;
82 * A slot to record time taken by an overlay connect operation
87 * A key to identify this timeslot
94 struct GNUNET_TIME_Relative time;
97 * Number of timing values accumulated
104 * Opaque handle to a host running experiments managed by the testing framework.
105 * The master process must be able to SSH to this host without password (via
108 struct GNUNET_TESTBED_Host
112 * The next pointer for DLL
114 struct GNUNET_TESTBED_Host *next;
117 * The prev pointer for DLL
119 struct GNUNET_TESTBED_Host *prev;
122 * The hostname of the host; NULL for localhost
124 const char *hostname;
127 * The username to be used for SSH login
129 const char *username;
132 * the configuration to use as a template while starting a controller on this
133 * host. Operation queue size specific to a host are also read from this
134 * configuration handle
136 struct GNUNET_CONFIGURATION_Handle *cfg;
139 * The head for the list of controllers where this host is registered
141 struct RegisteredController *rc_head;
144 * The tail for the list of controllers where this host is registered
146 struct RegisteredController *rc_tail;
149 * Operation queue for simultaneous overlay connect operations target at this
152 struct OperationQueue *opq_parallel_overlay_connect_operations;
155 * An array of timing slots; size should be equal to the current number of parallel
158 struct TimeSlot *tslots;
161 * Handle for SD calculations amount parallel overlay connect operation finish
164 struct SDHandle *poc_sd;
167 * The number of parallel overlay connects we do currently
169 unsigned int num_parallel_connects;
172 * Counter to indicate when all the available time slots are filled
174 unsigned int tslots_filled;
177 * Is a controller started on this host?
179 int controller_started;
182 * Global ID we use to refer to a host on the network
187 * The port which is to be used for SSH
195 * Array of available hosts
197 static struct GNUNET_TESTBED_Host **host_list;
200 * The size of the available hosts list
202 static unsigned int host_list_size;
206 * Lookup a host by ID.
208 * @param id global host ID assigned to the host; 0 is
209 * reserved to always mean 'localhost'
210 * @return handle to the host, NULL if host not found
212 struct GNUNET_TESTBED_Host *
213 GNUNET_TESTBED_host_lookup_by_id_ (uint32_t id)
215 if (host_list_size <= id)
217 return host_list[id];
222 * Create a host by ID; given this host handle, we could not
223 * run peers at the host, but we can talk about the host
226 * @param id global host ID assigned to the host; 0 is
227 * reserved to always mean 'localhost'
228 * @param cfg the configuration to use as a template while starting a controller
229 * on this host. Operation queue sizes specific to a host are also
230 * read from this configuration handle
231 * @return handle to the host, NULL on error
233 struct GNUNET_TESTBED_Host *
234 GNUNET_TESTBED_host_create_by_id_ (uint32_t id,
235 const struct GNUNET_CONFIGURATION_Handle
238 return GNUNET_TESTBED_host_create_with_id (id, NULL, NULL, cfg, 0);
243 * Obtain the host's unique global ID.
245 * @param host handle to the host, NULL means 'localhost'
246 * @return id global host ID assigned to the host (0 is
247 * 'localhost', but then obviously not globally unique)
250 GNUNET_TESTBED_host_get_id_ (const struct GNUNET_TESTBED_Host * host)
257 * Obtain the host's hostname.
259 * @param host handle to the host, NULL means 'localhost'
260 * @return hostname of the host
263 GNUNET_TESTBED_host_get_hostname (const struct GNUNET_TESTBED_Host *host)
265 return host->hostname;
270 * Obtain the host's username
272 * @param host handle to the host, NULL means 'localhost'
273 * @return username to login to the host
276 GNUNET_TESTBED_host_get_username_ (const struct GNUNET_TESTBED_Host *host)
278 return host->username;
283 * Obtain the host's ssh port
285 * @param host handle to the host, NULL means 'localhost'
286 * @return username to login to the host
289 GNUNET_TESTBED_host_get_ssh_port_ (const struct GNUNET_TESTBED_Host * host)
296 * Check whether a controller is already started on the given host
298 * @param host the handle to the host
299 * @return GNUNET_YES if the controller is already started; GNUNET_NO if not
302 GNUNET_TESTBED_host_controller_started (const struct GNUNET_TESTBED_Host *host)
304 return host->controller_started;
309 * Obtain the host's configuration template
311 * @param host handle to the host
312 * @return the host's configuration template
314 const struct GNUNET_CONFIGURATION_Handle *
315 GNUNET_TESTBED_host_get_cfg_ (const struct GNUNET_TESTBED_Host *host)
322 * Create a host to run peers and controllers on.
324 * @param id global host ID assigned to the host; 0 is
325 * reserved to always mean 'localhost'
326 * @param hostname name of the host, use "NULL" for localhost
327 * @param username username to use for the login; may be NULL
328 * @param cfg the configuration to use as a template while starting a controller
329 * on this host. Operation queue sizes specific to a host are also
330 * read from this configuration handle
331 * @param port port number to use for ssh; use 0 to let ssh decide
332 * @return handle to the host, NULL on error
334 struct GNUNET_TESTBED_Host *
335 GNUNET_TESTBED_host_create_with_id (uint32_t id, const char *hostname,
336 const char *username,
337 const struct GNUNET_CONFIGURATION_Handle
341 struct GNUNET_TESTBED_Host *host;
342 unsigned int new_size;
344 if ((id < host_list_size) && (NULL != host_list[id]))
346 LOG (GNUNET_ERROR_TYPE_WARNING, "Host with id: %u already created\n", id);
349 host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
350 host->hostname = (NULL != hostname) ? GNUNET_strdup (hostname) : NULL;
351 host->username = (NULL != username) ? GNUNET_strdup (username) : NULL;
353 host->port = (0 == port) ? 22 : port;
354 host->cfg = GNUNET_CONFIGURATION_dup (cfg);
355 host->opq_parallel_overlay_connect_operations =
356 GNUNET_TESTBED_operation_queue_create_ (0);
357 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (host, 1);
358 host->poc_sd = GNUNET_TESTBED_SD_init_ (10);
359 new_size = host_list_size;
360 while (id >= new_size)
361 new_size += HOST_LIST_GROW_STEP;
362 if (new_size != host_list_size)
363 GNUNET_array_grow (host_list, host_list_size, new_size);
364 GNUNET_assert (id < host_list_size);
365 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding host with id: %u\n", host->id);
366 host_list[id] = host;
372 * Create a host to run peers and controllers on.
374 * @param hostname name of the host, use "NULL" for localhost
375 * @param username username to use for the login; may be NULL
376 * @param cfg the configuration to use as a template while starting a controller
377 * on this host. Operation queue sizes specific to a host are also
378 * read from this configuration handle
379 * @param port port number to use for ssh; use 0 to let ssh decide
380 * @return handle to the host, NULL on error
382 struct GNUNET_TESTBED_Host *
383 GNUNET_TESTBED_host_create (const char *hostname, const char *username,
384 const struct GNUNET_CONFIGURATION_Handle *cfg,
387 static uint32_t uid_generator;
389 if (NULL == hostname)
390 return GNUNET_TESTBED_host_create_with_id (0, hostname, username,
392 return GNUNET_TESTBED_host_create_with_id (++uid_generator, hostname,
393 username, cfg, port);
398 * Load a set of hosts from a configuration file.
400 * @param filename file with the host specification
401 * @param cfg the configuration to use as a template while starting a controller
402 * on any of the loaded hosts. Operation queue sizes specific to a host
403 * are also read from this configuration handle
404 * @param hosts set to the hosts found in the file; caller must free this if
405 * number of hosts returned is greater than 0
406 * @return number of hosts returned in 'hosts', 0 on error
409 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
410 const struct GNUNET_CONFIGURATION_Handle
412 struct GNUNET_TESTBED_Host ***hosts)
414 //struct GNUNET_TESTBED_Host **host_array;
415 struct GNUNET_TESTBED_Host *starting_host;
427 GNUNET_assert (NULL != filename);
428 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
430 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s not found\n"), filename);
434 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
438 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s has no data\n"), filename);
441 data = GNUNET_malloc (fs);
442 if (fs != GNUNET_DISK_fn_read (filename, data, fs))
445 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s cannot be read\n"),
451 starting_host = NULL;
453 while (offset < (fs - 1))
456 if (((data[offset] == '\n')) && (buf != &data[offset]))
460 SSCANF (buf, "%255[a-zA-Z0-9_]@%255[a-zA-Z0-9.]:%5hd", username,
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
465 "Successfully read host %s, port %d and user %s from file\n",
466 hostname, port, username);
467 /* We store hosts in a static list; hence we only require the starting
468 * host pointer in that list to access the newly created list of hosts */
469 if (NULL == starting_host)
470 starting_host = GNUNET_TESTBED_host_create (hostname, username, cfg,
473 (void) GNUNET_TESTBED_host_create (hostname, username, cfg, port);
477 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
478 "Error reading line `%s' in hostfile\n", buf);
479 buf = &data[offset + 1];
481 else if ((data[offset] == '\n') || (data[offset] == '\0'))
482 buf = &data[offset + 1];
485 if (NULL == starting_host)
487 *hosts = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * count);
488 memcpy (*hosts, &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)],
489 sizeof (struct GNUNET_TESTBED_Host *) * count);
495 * Destroy a host handle. Must only be called once everything
496 * running on that host has been stopped.
498 * @param host handle to destroy
501 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
503 struct RegisteredController *rc;
506 GNUNET_assert (host->id < host_list_size);
507 GNUNET_assert (host_list[host->id] == host);
508 host_list[host->id] = NULL;
509 /* clear registered controllers list */
510 for (rc = host->rc_head; NULL != rc; rc = host->rc_head)
512 GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
515 GNUNET_free_non_null ((char *) host->username);
516 GNUNET_free_non_null ((char *) host->hostname);
517 GNUNET_TESTBED_operation_queue_destroy_
518 (host->opq_parallel_overlay_connect_operations);
519 GNUNET_TESTBED_SD_destroy_ (host->poc_sd);
520 GNUNET_free_non_null (host->tslots);
521 GNUNET_CONFIGURATION_destroy (host->cfg);
523 while (host_list_size >= HOST_LIST_GROW_STEP)
525 for (id = host_list_size - 1; id > host_list_size - HOST_LIST_GROW_STEP;
527 if (NULL != host_list[id])
529 if (id != host_list_size - HOST_LIST_GROW_STEP)
531 if (NULL != host_list[id])
533 host_list_size -= HOST_LIST_GROW_STEP;
536 GNUNET_realloc (host_list,
537 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size);
542 * Marks a host as registered with a controller
544 * @param host the host to mark
545 * @param controller the controller at which this host is registered
548 GNUNET_TESTBED_mark_host_registered_at_ (struct GNUNET_TESTBED_Host *host,
549 const struct GNUNET_TESTBED_Controller
552 struct RegisteredController *rc;
554 for (rc = host->rc_head; NULL != rc; rc = rc->next)
556 if (controller == rc->controller) /* already registered at controller */
562 rc = GNUNET_malloc (sizeof (struct RegisteredController));
563 rc->controller = controller;
564 GNUNET_CONTAINER_DLL_insert_tail (host->rc_head, host->rc_tail, rc);
569 * Checks whether a host has been registered
571 * @param host the host to check
572 * @param controller the controller at which host's registration is checked
573 * @return GNUNET_YES if registered; GNUNET_NO if not
576 GNUNET_TESTBED_is_host_registered_ (const struct GNUNET_TESTBED_Host *host,
577 const struct GNUNET_TESTBED_Controller
580 struct RegisteredController *rc;
582 for (rc = host->rc_head; NULL != rc; rc = rc->next)
584 if (controller == rc->controller) /* already registered at controller */
594 * Handle for controller process
596 struct GNUNET_TESTBED_ControllerProc
601 struct GNUNET_HELPER_Handle *helper;
604 * The arguments used to start the helper
609 * The host where the helper is run
611 struct GNUNET_TESTBED_Host *host;
614 * The controller error callback
616 GNUNET_TESTBED_ControllerStatusCallback cb;
619 * The closure for the above callback
624 * The send handle for the helper
626 struct GNUNET_HELPER_SendHandle *shandle;
629 * The message corresponding to send handle
631 struct GNUNET_MessageHeader *msg;
634 * The configuration of the running testbed service
636 struct GNUNET_CONFIGURATION_Handle *cfg;
642 * Function to copy NULL terminated list of arguments
644 * @param argv the NULL terminated list of arguments. Cannot be NULL.
645 * @return the copied NULL terminated arguments
648 copy_argv (const char *const *argv)
653 GNUNET_assert (NULL != argv);
654 for (argp = 0; NULL != argv[argp]; argp++) ;
655 argv_dup = GNUNET_malloc (sizeof (char *) * (argp + 1));
656 for (argp = 0; NULL != argv[argp]; argp++)
657 argv_dup[argp] = strdup (argv[argp]);
663 * Function to join NULL terminated list of arguments
665 * @param argv1 the NULL terminated list of arguments. Cannot be NULL.
666 * @param argv2 the NULL terminated list of arguments. Cannot be NULL.
667 * @return the joined NULL terminated arguments
670 join_argv (const char *const *argv1, const char *const *argv2)
679 for (cnt = 0; NULL != argv1[cnt]; cnt++)
681 argv = GNUNET_strdup (argv1[cnt]);
682 GNUNET_array_append (argvj, carg, argv);
684 for (cnt = 0; NULL != argv2[cnt]; cnt++)
686 argv = GNUNET_strdup (argv2[cnt]);
687 GNUNET_array_append (argvj, carg, argv);
689 GNUNET_array_append (argvj, carg, NULL);
695 * Frees the given NULL terminated arguments
697 * @param argv the NULL terminated list of arguments
700 free_argv (char **argv)
704 for (argp = 0; NULL != argv[argp]; argp++)
705 GNUNET_free (argv[argp]);
711 * Generates arguments for opening a remote shell. Builds up the arguments
712 * from the environment variable GNUNET_TESTBED_RSH_CMD. The variable
713 * should not mention `-p' (port) option and destination address as these will
714 * be set locally in the function from its parameteres. If the environmental
715 * variable is not found then it defaults to `ssh -o BatchMode=yes -o
716 * NoHostAuthenticationForLocalhost=yes'
718 * @param port the destination port number
719 * @param dst the destination address
720 * @return NULL terminated list of arguments
723 gen_rsh_args (const char *port, const char *dst)
725 static const char *default_ssh_args[] = {
730 "NoHostAuthenticationForLocalhost=yes",
740 if (NULL != (ssh_cmd = getenv ("GNUNET_TESTBED_RSH_CMD")))
742 ssh_cmd = GNUNET_strdup (ssh_cmd);
743 ssh_cmd_cp = ssh_cmd;
744 for (cnt = 0; NULL != (arg = strtok (ssh_cmd, " ")); ssh_cmd = NULL)
745 GNUNET_array_append (ssh_args, cnt, GNUNET_strdup (arg));
746 GNUNET_free (ssh_cmd_cp);
750 ssh_args = copy_argv (default_ssh_args);
751 cnt = (sizeof (default_ssh_args)) / (sizeof (const char *));
752 GNUNET_array_grow (ssh_args, cnt, cnt - 1);
754 GNUNET_array_append (ssh_args, cnt, GNUNET_strdup ("-p"));
755 GNUNET_array_append (ssh_args, cnt, GNUNET_strdup (port));
756 GNUNET_array_append (ssh_args, cnt, GNUNET_strdup (dst));
757 GNUNET_array_append (ssh_args, cnt, NULL);
763 * Generates the arguments needed for executing the given binary in a remote
764 * shell. Builds the arguments from the environmental variable
765 * GNUNET_TETSBED_RSH_CMD_SUFFIX. If the environmental variable is not found,
766 * only the given binary name will be present in the returned arguments
768 * @param append_args the arguments to append after generating the suffix
769 * arguments. Can be NULL; if not must be NULL terminated 'char *' array
770 * @return NULL-terminated args
773 gen_rsh_suffix_args (const char * const *append_args)
780 unsigned int append_cnt;
784 if (NULL != (rshell_cmd = getenv ("GNUNET_TESTBED_RSH_CMD_SUFFIX")))
786 rshell_cmd = GNUNET_strdup (rshell_cmd);
787 rshell_cmd_cp = rshell_cmd;
788 for (; NULL != (arg = strtok (rshell_cmd, " ")); rshell_cmd = NULL)
789 GNUNET_array_append (rshell_args, cnt, GNUNET_strdup (arg));
790 GNUNET_free (rshell_cmd_cp);
792 if (NULL != append_args)
794 for (append_cnt = 0; NULL != append_args[append_cnt]; append_cnt++)
795 GNUNET_array_append (rshell_args, cnt, GNUNET_strdup (append_args[append_cnt]));
797 GNUNET_array_append (rshell_args, cnt, NULL);
803 * Functions with this signature are called whenever a
804 * complete message is received by the tokenizer.
806 * Do not call GNUNET_SERVER_mst_destroy in callback
809 * @param client identification of the client
810 * @param message the actual message
812 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
815 helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
817 struct GNUNET_TESTBED_ControllerProc *cp = cls;
818 const struct GNUNET_TESTBED_HelperReply *msg;
819 const char *hostname;
824 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
825 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) <
826 ntohs (msg->header.size));
827 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY ==
828 ntohs (msg->header.type));
829 config_size = (uLongf) ntohs (msg->config_size);
831 (uLongf) (ntohs (msg->header.size) -
832 sizeof (struct GNUNET_TESTBED_HelperReply));
833 config = GNUNET_malloc (config_size);
834 GNUNET_assert (Z_OK ==
835 uncompress ((Bytef *) config, &config_size,
836 (const Bytef *) &msg[1], xconfig_size));
837 GNUNET_assert (NULL == cp->cfg);
838 cp->cfg = GNUNET_CONFIGURATION_create ();
839 GNUNET_assert (GNUNET_CONFIGURATION_deserialize
840 (cp->cfg, config, config_size, GNUNET_NO));
841 GNUNET_free (config);
842 if ((NULL == cp->host) ||
843 (NULL == (hostname = GNUNET_TESTBED_host_get_hostname (cp->host))))
844 hostname = "localhost";
845 /* Change the hostname so that we can connect to it */
846 GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
848 cp->cb (cp->cls, cp->cfg, GNUNET_OK);
854 * Continuation function from GNUNET_HELPER_send()
857 * @param result GNUNET_OK on success,
858 * GNUNET_NO if helper process died
859 * GNUNET_SYSERR during GNUNET_HELPER_stop
862 clear_msg (void *cls, int result)
864 struct GNUNET_TESTBED_ControllerProc *cp = cls;
866 GNUNET_assert (NULL != cp->shandle);
868 GNUNET_free (cp->msg);
873 * Callback that will be called when the helper process dies. This is not called
874 * when the helper process is stoped using GNUNET_HELPER_stop()
876 * @param cls the closure from GNUNET_HELPER_start()
879 helper_exp_cb (void *cls)
881 struct GNUNET_TESTBED_ControllerProc *cp = cls;
882 GNUNET_TESTBED_ControllerStatusCallback cb;
888 GNUNET_TESTBED_controller_stop (cp);
890 cb (cb_cls, NULL, GNUNET_SYSERR);
895 * Starts a controller process at the given host
897 * @param trusted_ip the ip address of the controller which will be set as TRUSTED
898 * HOST(all connections form this ip are permitted by the testbed) when
899 * starting testbed controller at host. This can either be a single ip
900 * address or a network address in CIDR notation.
901 * @param host the host where the controller has to be started; NULL for
903 * @param cfg template configuration to use for the remote controller; the
904 * remote controller will be started with a slightly modified
905 * configuration (port numbers, unix domain sockets and service home
906 * values are changed as per TESTING library on the remote host)
907 * @param cb function called when the controller is successfully started or
908 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
909 * called if cb is called with GNUNET_SYSERR as status. Will never be
910 * called in the same task as 'GNUNET_TESTBED_controller_start'
911 * (synchronous errors will be signalled by returning NULL). This
912 * parameter cannot be NULL.
913 * @param cls closure for above callbacks
914 * @return the controller process handle, NULL on errors
916 struct GNUNET_TESTBED_ControllerProc *
917 GNUNET_TESTBED_controller_start (const char *trusted_ip,
918 struct GNUNET_TESTBED_Host *host,
919 const struct GNUNET_CONFIGURATION_Handle *cfg,
920 GNUNET_TESTBED_ControllerStatusCallback cb,
923 struct GNUNET_TESTBED_ControllerProc *cp;
924 struct GNUNET_TESTBED_HelperInit *msg;
925 const char *hostname;
927 static char *const binary_argv[] = {
928 HELPER_TESTBED_BINARY, NULL
932 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
933 if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
936 GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv,
937 &helper_mst, &helper_exp_cb, cp);
941 char *helper_binary_path_args[2];
943 char **rsh_suffix_args;
944 const char *username;
948 username = GNUNET_TESTBED_host_get_username_ (host);
949 hostname = GNUNET_TESTBED_host_get_hostname (host);
950 GNUNET_asprintf (&port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
951 if (NULL == username)
952 GNUNET_asprintf (&dst, "%s", hostname);
954 GNUNET_asprintf (&dst, "%s@%s", username, hostname);
955 LOG_DEBUG ("Starting SSH to destination %s\n", dst);
958 GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
959 "HELPER_BINARY_PATH",
960 &helper_binary_path_args[0]))
961 helper_binary_path_args[0] =
962 GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
963 helper_binary_path_args[1] = NULL;
964 rsh_args = gen_rsh_args (port, dst);
965 rsh_suffix_args = gen_rsh_suffix_args ((const char **) helper_binary_path_args);
967 join_argv ((const char **) rsh_args, (const char **) rsh_suffix_args);
968 free_argv (rsh_args);
969 free_argv (rsh_suffix_args);
973 GNUNET_HELPER_start (GNUNET_NO, cp->helper_argv[0], cp->helper_argv, &helper_mst,
975 GNUNET_free (helper_binary_path_args[0]);
977 if (NULL == cp->helper)
979 if (NULL != cp->helper_argv)
980 free_argv (cp->helper_argv);
987 msg = GNUNET_TESTBED_create_helper_init_msg_ (trusted_ip, hostname, cfg);
988 cp->msg = &msg->header;
990 GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO, &clear_msg, cp);
991 if (NULL == cp->shandle)
994 GNUNET_TESTBED_controller_stop (cp);
1002 * Stop the controller process (also will terminate all peers and controllers
1003 * dependent on this controller). This function blocks until the testbed has
1004 * been fully terminated (!). The controller status cb from
1005 * GNUNET_TESTBED_controller_start() will not be called.
1007 * @param cproc the controller process handle
1010 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
1012 if (NULL != cproc->shandle)
1013 GNUNET_HELPER_send_cancel (cproc->shandle);
1014 if (NULL != cproc->helper)
1015 GNUNET_HELPER_soft_stop (cproc->helper);
1016 if (NULL != cproc->cfg)
1017 GNUNET_CONFIGURATION_destroy (cproc->cfg);
1018 if (NULL != cproc->helper_argv)
1019 free_argv (cproc->helper_argv);
1020 GNUNET_free (cproc);
1025 * The handle for whether a host is habitable or not
1027 struct GNUNET_TESTBED_HostHabitableCheckHandle
1032 const struct GNUNET_TESTBED_Host *host;
1035 /* * the configuration handle to lookup the path of the testbed helper */
1037 /* const struct GNUNET_CONFIGURATION_Handle *cfg; */
1040 * The callback to call once we have the status
1042 GNUNET_TESTBED_HostHabitableCallback cb;
1045 * The callback closure
1050 * The process handle for the SSH process
1052 struct GNUNET_OS_Process *auxp;
1055 * The arguments used to start the helper
1060 * Task id for the habitability check task
1062 GNUNET_SCHEDULER_TaskIdentifier habitability_check_task;
1065 * How long we wait before checking the process status. Should grow
1068 struct GNUNET_TIME_Relative wait_time;
1074 * Task for checking whether a host is habitable or not
1076 * @param cls GNUNET_TESTBED_HostHabitableCheckHandle
1077 * @param tc the scheduler task context
1080 habitability_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1082 struct GNUNET_TESTBED_HostHabitableCheckHandle *h = cls;
1084 GNUNET_TESTBED_HostHabitableCallback cb;
1085 const struct GNUNET_TESTBED_Host *host;
1087 enum GNUNET_OS_ProcessStatusType type;
1090 h->habitability_check_task = GNUNET_SCHEDULER_NO_TASK;
1091 ret = GNUNET_OS_process_status (h->auxp, &type, &code);
1092 if (GNUNET_SYSERR == ret)
1098 if (GNUNET_NO == ret)
1100 h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
1101 h->habitability_check_task =
1102 GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
1105 GNUNET_OS_process_destroy (h->auxp);
1107 ret = (0 != code) ? GNUNET_NO : GNUNET_YES;
1110 if (NULL != h->auxp)
1111 GNUNET_OS_process_destroy (h->auxp);
1117 cb (cb_cls, host, ret);
1122 * Checks whether a host can be used to start testbed service
1124 * @param host the host to check
1125 * @param config the configuration handle to lookup the path of the testbed
1127 * @param cb the callback to call to inform about habitability of the given host
1128 * @param cb_cls the closure for the callback
1129 * @return NULL upon any error or a handle which can be passed to
1130 * GNUNET_TESTBED_is_host_habitable_cancel()
1132 struct GNUNET_TESTBED_HostHabitableCheckHandle *
1133 GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
1134 const struct GNUNET_CONFIGURATION_Handle
1136 GNUNET_TESTBED_HostHabitableCallback cb,
1139 struct GNUNET_TESTBED_HostHabitableCheckHandle *h;
1141 char **rsh_suffix_args;
1143 const char *hostname;
1147 h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostHabitableCheckHandle));
1151 hostname = (NULL == host->hostname) ? "127.0.0.1" : host->hostname;
1152 if (NULL == host->username)
1153 dst = GNUNET_strdup (hostname);
1155 GNUNET_asprintf (&dst, "%s@%s", host->username, hostname);
1157 GNUNET_CONFIGURATION_get_value_string (config, "testbed",
1158 "HELPER_BINARY_PATH",
1161 GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
1162 GNUNET_asprintf (&port, "%u", host->port);
1163 rsh_args = gen_rsh_args (port, dst);
1168 stat_args[0] = "stat";
1169 stat_args[2] = NULL;
1170 rsh_suffix_args = gen_rsh_suffix_args ((const char **) stat_args);
1171 h->helper_argv = join_argv ((const char **) rsh_args,
1172 (const char **) rsh_suffix_args);
1173 GNUNET_free (rsh_suffix_args);
1174 GNUNET_free (rsh_args);
1176 GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, NULL,
1177 NULL, h->helper_argv[0], h->helper_argv);
1178 if (NULL == h->auxp)
1180 GNUNET_break (0); /* Cannot exec SSH? */
1184 h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
1185 h->habitability_check_task =
1186 GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
1192 * Function to cancel a request started using GNUNET_TESTBED_is_host_habitable()
1194 * @param handle the habitability check handle
1197 GNUNET_TESTBED_is_host_habitable_cancel (struct
1198 GNUNET_TESTBED_HostHabitableCheckHandle
1201 GNUNET_SCHEDULER_cancel (handle->habitability_check_task);
1202 (void) GNUNET_OS_process_kill (handle->auxp, SIGTERM);
1203 (void) GNUNET_OS_process_wait (handle->auxp);
1204 GNUNET_OS_process_destroy (handle->auxp);
1205 free_argv (handle->helper_argv);
1206 GNUNET_free (handle);
1211 * Initializes the operation queue for parallel overlay connects
1213 * @param h the host handle
1214 * @param npoc the number of parallel overlay connects - the queue size
1217 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (struct
1218 GNUNET_TESTBED_Host *h,
1221 //fprintf (stderr, "%d", npoc);
1222 GNUNET_free_non_null (h->tslots);
1223 h->tslots_filled = 0;
1224 h->num_parallel_connects = npoc;
1225 h->tslots = GNUNET_malloc (npoc * sizeof (struct TimeSlot));
1226 GNUNET_TESTBED_operation_queue_reset_max_active_
1227 (h->opq_parallel_overlay_connect_operations, npoc);
1232 * Returns a timing slot which will be exclusively locked
1234 * @param h the host handle
1235 * @param key a pointer which is associated to the returned slot; should not be
1236 * NULL. It serves as a key to determine the correct owner of the slot
1237 * @return the time slot index in the array of time slots in the controller
1241 GNUNET_TESTBED_get_tslot_ (struct GNUNET_TESTBED_Host *h, void *key)
1245 GNUNET_assert (NULL != h->tslots);
1246 GNUNET_assert (NULL != key);
1247 for (slot = 0; slot < h->num_parallel_connects; slot++)
1248 if (NULL == h->tslots[slot].key)
1250 h->tslots[slot].key = key;
1253 GNUNET_assert (0); /* We should always find a free tslot */
1258 * Decides whether any change in the number of parallel overlay connects is
1259 * necessary to adapt to the load on the system
1261 * @param h the host handle
1264 decide_npoc (struct GNUNET_TESTBED_Host *h)
1266 struct GNUNET_TIME_Relative avg;
1271 if (h->tslots_filled != h->num_parallel_connects)
1273 avg = GNUNET_TIME_UNIT_ZERO;
1275 for (slot = 0; slot < h->num_parallel_connects; slot++)
1277 avg = GNUNET_TIME_relative_add (avg, h->tslots[slot].time);
1278 nvals += h->tslots[slot].nvals;
1280 GNUNET_assert (nvals >= h->num_parallel_connects);
1281 avg = GNUNET_TIME_relative_divide (avg, nvals);
1282 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != avg.rel_value);
1283 sd = GNUNET_TESTBED_SD_deviation_factor_ (h->poc_sd, (unsigned int) avg.rel_value);
1285 (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1286 h->num_parallel_connects)) )
1287 GNUNET_TESTBED_SD_add_data_ (h->poc_sd, (unsigned int) avg.rel_value);
1288 if (GNUNET_SYSERR == sd)
1290 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1291 h->num_parallel_connects);
1294 GNUNET_assert (0 <= sd);
1297 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1298 h->num_parallel_connects
1304 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1305 h->num_parallel_connects
1309 if (1 == h->num_parallel_connects)
1311 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h, 1);
1316 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1317 h->num_parallel_connects
1321 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1322 h->num_parallel_connects /
1328 * Releases a time slot thus making it available for be used again
1330 * @param h the host handle
1331 * @param index the index of the the time slot
1332 * @param key the key to prove ownership of the timeslot
1333 * @return GNUNET_YES if the time slot is successfully removed; GNUNET_NO if the
1334 * time slot cannot be removed - this could be because of the index
1335 * greater than existing number of time slots or `key' being different
1338 GNUNET_TESTBED_release_time_slot_ (struct GNUNET_TESTBED_Host *h,
1339 unsigned int index, void *key)
1341 struct TimeSlot *slot;
1343 GNUNET_assert (NULL != key);
1344 if (index >= h->num_parallel_connects)
1346 slot = &h->tslots[index];
1347 if (key != slot->key)
1355 * Function to update a time slot
1357 * @param h the host handle
1358 * @param index the index of the time slot to update
1359 * @param key the key to identify ownership of the slot
1360 * @param time the new time
1361 * @param failed should this reading be treated as coming from a fail event
1364 GNUNET_TESTBED_update_time_slot_ (struct GNUNET_TESTBED_Host *h,
1365 unsigned int index, void *key,
1366 struct GNUNET_TIME_Relative time, int failed)
1368 struct TimeSlot *slot;
1370 if (GNUNET_YES == failed)
1372 if (1 == h->num_parallel_connects)
1374 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h, 1);
1377 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1378 h->num_parallel_connects
1381 if (GNUNET_NO == GNUNET_TESTBED_release_time_slot_ (h, index, key))
1383 slot = &h->tslots[index];
1385 if (GNUNET_TIME_UNIT_ZERO.rel_value == slot->time.rel_value)
1392 slot->time = GNUNET_TIME_relative_add (slot->time, time);
1397 * Queues the given operation in the queue for parallel overlay connects of the
1400 * @param h the host handle
1401 * @param op the operation to queue in the given host's parally overlay connect
1405 GNUNET_TESTBED_host_queue_oc_ (struct GNUNET_TESTBED_Host *h,
1406 struct GNUNET_TESTBED_Operation *op)
1408 GNUNET_TESTBED_operation_queue_insert_
1409 (h->opq_parallel_overlay_connect_operations, op);
1412 /* end of testbed_api_hosts.c */