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_helper.h"
37 #include "testbed_api_operations.h"
38 #include "testbed_api_sd.h"
43 * Generic logging shorthand
45 #define LOG(kind, ...) \
46 GNUNET_log_from (kind, "testbed-api-hosts", __VA_ARGS__);
49 * Debug logging shorthand
51 #define LOG_DEBUG(...) \
52 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
55 * Prints API violation message
57 #define API_VIOLATION(cond,errstr) \
61 LOG (GNUNET_ERROR_TYPE_ERROR, "API violation detected: %s\n", errstr); \
66 * Number of extra elements we create space for when we grow host list
68 #define HOST_LIST_GROW_STEP 10
72 * A list entry for registered controllers list
74 struct RegisteredController
77 * The controller at which this host is registered
79 const struct GNUNET_TESTBED_Controller *controller;
82 * The next ptr for DLL
84 struct RegisteredController *next;
87 * The prev ptr for DLL
89 struct RegisteredController *prev;
94 * A slot to record time taken by an overlay connect operation
99 * A key to identify this timeslot
106 struct GNUNET_TIME_Relative time;
109 * Number of timing values accumulated
116 * Opaque handle to a host running experiments managed by the testing framework.
117 * The master process must be able to SSH to this host without password (via
120 struct GNUNET_TESTBED_Host
124 * The next pointer for DLL
126 struct GNUNET_TESTBED_Host *next;
129 * The prev pointer for DLL
131 struct GNUNET_TESTBED_Host *prev;
134 * The hostname of the host; NULL for localhost
136 const char *hostname;
139 * The username to be used for SSH login
141 const char *username;
144 * the configuration to use as a template while starting a controller on this
145 * host. Operation queue size specific to a host are also read from this
146 * configuration handle. After starting the controller, it points to the actual
147 * configuration with which the controller is running
149 struct GNUNET_CONFIGURATION_Handle *cfg;
152 * The head for the list of controllers where this host is registered
154 struct RegisteredController *rc_head;
157 * The tail for the list of controllers where this host is registered
159 struct RegisteredController *rc_tail;
162 * Operation queue for simultaneous overlay connect operations target at this
165 struct OperationQueue *opq_parallel_overlay_connect_operations;
168 * An array of timing slots; size should be equal to the current number of parallel
171 struct TimeSlot *tslots;
174 * Handle for SD calculations amount parallel overlay connect operation finish
177 struct SDHandle *poc_sd;
180 * The number of parallel overlay connects we do currently
182 unsigned int num_parallel_connects;
185 * Counter to indicate when all the available time slots are filled
187 unsigned int tslots_filled;
190 * Is a controller started on this host? FIXME: Is this needed?
192 int controller_started;
195 * Is this host locked by GNUNET_TESTBED_controller_start()?
200 * Global ID we use to refer to a host on the network
205 * The port which is to be used for SSH
213 * Array of available hosts
215 static struct GNUNET_TESTBED_Host **host_list;
218 * The size of the available hosts list
220 static unsigned int host_list_size;
224 * Lookup a host by ID.
226 * @param id global host ID assigned to the host; 0 is
227 * reserved to always mean 'localhost'
228 * @return handle to the host, NULL if host not found
230 struct GNUNET_TESTBED_Host *
231 GNUNET_TESTBED_host_lookup_by_id_ (uint32_t id)
233 if (host_list_size <= id)
235 return host_list[id];
240 * Create a host by ID; given this host handle, we could not
241 * run peers at the host, but we can talk about the host
244 * @param id global host ID assigned to the host; 0 is
245 * reserved to always mean 'localhost'
246 * @param cfg the configuration to use as a template while starting a controller
247 * on this host. Operation queue sizes specific to a host are also
248 * read from this configuration handle
249 * @return handle to the host, NULL on error
251 struct GNUNET_TESTBED_Host *
252 GNUNET_TESTBED_host_create_by_id_ (uint32_t id,
253 const struct GNUNET_CONFIGURATION_Handle
256 return GNUNET_TESTBED_host_create_with_id (id, NULL, NULL, cfg, 0);
261 * Obtain the host's unique global ID.
263 * @param host handle to the host, NULL means 'localhost'
264 * @return id global host ID assigned to the host (0 is
265 * 'localhost', but then obviously not globally unique)
268 GNUNET_TESTBED_host_get_id_ (const struct GNUNET_TESTBED_Host * host)
275 * Obtain the host's hostname.
277 * @param host handle to the host, NULL means 'localhost'
278 * @return hostname of the host
281 GNUNET_TESTBED_host_get_hostname (const struct GNUNET_TESTBED_Host *host)
283 return host->hostname;
288 * Obtain the host's username
290 * @param host handle to the host, NULL means 'localhost'
291 * @return username to login to the host
294 GNUNET_TESTBED_host_get_username_ (const struct GNUNET_TESTBED_Host *host)
296 return host->username;
301 * Obtain the host's ssh port
303 * @param host handle to the host, NULL means 'localhost'
304 * @return username to login to the host
307 GNUNET_TESTBED_host_get_ssh_port_ (const struct GNUNET_TESTBED_Host * host)
314 * Check whether a controller is already started on the given host
316 * @param host the handle to the host
317 * @return GNUNET_YES if the controller is already started; GNUNET_NO if not
320 GNUNET_TESTBED_host_controller_started (const struct GNUNET_TESTBED_Host *host)
322 return host->controller_started;
327 * Obtain the host's configuration template
329 * @param host handle to the host
330 * @return the host's configuration template
332 const struct GNUNET_CONFIGURATION_Handle *
333 GNUNET_TESTBED_host_get_cfg_ (const struct GNUNET_TESTBED_Host *host)
340 * Create a host to run peers and controllers on.
342 * @param id global host ID assigned to the host; 0 is
343 * reserved to always mean 'localhost'
344 * @param hostname name of the host, use "NULL" for localhost
345 * @param username username to use for the login; may be NULL
346 * @param cfg the configuration to use as a template while starting a controller
347 * on this host. Operation queue sizes specific to a host are also
348 * read from this configuration handle
349 * @param port port number to use for ssh; use 0 to let ssh decide
350 * @return handle to the host, NULL on error
352 struct GNUNET_TESTBED_Host *
353 GNUNET_TESTBED_host_create_with_id (uint32_t id, const char *hostname,
354 const char *username,
355 const struct GNUNET_CONFIGURATION_Handle
359 struct GNUNET_TESTBED_Host *host;
360 unsigned int new_size;
362 if ((id < host_list_size) && (NULL != host_list[id]))
364 LOG (GNUNET_ERROR_TYPE_WARNING, "Host with id: %u already created\n", id);
367 host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
368 host->hostname = (NULL != hostname) ? GNUNET_strdup (hostname) : NULL;
369 host->username = (NULL != username) ? GNUNET_strdup (username) : NULL;
371 host->port = (0 == port) ? 22 : port;
372 host->cfg = GNUNET_CONFIGURATION_dup (cfg);
373 host->opq_parallel_overlay_connect_operations =
374 GNUNET_TESTBED_operation_queue_create_ (0);
375 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (host, 1);
376 host->poc_sd = GNUNET_TESTBED_SD_init_ (10);
377 new_size = host_list_size;
378 while (id >= new_size)
379 new_size += HOST_LIST_GROW_STEP;
380 if (new_size != host_list_size)
381 GNUNET_array_grow (host_list, host_list_size, new_size);
382 GNUNET_assert (id < host_list_size);
383 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding host with id: %u\n", host->id);
384 host_list[id] = host;
390 * Create a host to run peers and controllers on.
392 * @param hostname name of the host, use "NULL" for localhost
393 * @param username username to use for the login; may be NULL
394 * @param cfg the configuration to use as a template while starting a controller
395 * on this host. Operation queue sizes specific to a host are also
396 * read from this configuration handle
397 * @param port port number to use for ssh; use 0 to let ssh decide
398 * @return handle to the host, NULL on error
400 struct GNUNET_TESTBED_Host *
401 GNUNET_TESTBED_host_create (const char *hostname, const char *username,
402 const struct GNUNET_CONFIGURATION_Handle *cfg,
405 static uint32_t uid_generator;
407 if (NULL == hostname)
408 return GNUNET_TESTBED_host_create_with_id (0, hostname, username,
410 return GNUNET_TESTBED_host_create_with_id (++uid_generator, hostname,
411 username, cfg, port);
416 * Load a set of hosts from a configuration file.
418 * @param filename file with the host specification
419 * @param cfg the configuration to use as a template while starting a controller
420 * on any of the loaded hosts. Operation queue sizes specific to a host
421 * are also read from this configuration handle
422 * @param hosts set to the hosts found in the file; caller must free this if
423 * number of hosts returned is greater than 0
424 * @return number of hosts returned in 'hosts', 0 on error
427 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
428 const struct GNUNET_CONFIGURATION_Handle
430 struct GNUNET_TESTBED_Host ***hosts)
432 //struct GNUNET_TESTBED_Host **host_array;
433 struct GNUNET_TESTBED_Host *starting_host;
445 GNUNET_assert (NULL != filename);
446 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
448 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s not found\n"), filename);
452 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
456 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s has no data\n"), filename);
459 data = GNUNET_malloc (fs);
460 if (fs != GNUNET_DISK_fn_read (filename, data, fs))
463 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s cannot be read\n"),
469 starting_host = NULL;
471 while (offset < (fs - 1))
474 if (((data[offset] == '\n')) && (buf != &data[offset]))
478 SSCANF (buf, "%255[a-zA-Z0-9_]@%255[a-zA-Z0-9.]:%5hd", username,
482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
483 "Successfully read host %s, port %d and user %s from file\n",
484 hostname, port, username);
485 /* We store hosts in a static list; hence we only require the starting
486 * host pointer in that list to access the newly created list of hosts */
487 if (NULL == starting_host)
488 starting_host = GNUNET_TESTBED_host_create (hostname, username, cfg,
491 (void) GNUNET_TESTBED_host_create (hostname, username, cfg, port);
495 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
496 "Error reading line `%s' in hostfile\n", buf);
497 buf = &data[offset + 1];
499 else if ((data[offset] == '\n') || (data[offset] == '\0'))
500 buf = &data[offset + 1];
503 if (NULL == starting_host)
505 *hosts = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * count);
506 memcpy (*hosts, &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)],
507 sizeof (struct GNUNET_TESTBED_Host *) * count);
513 * Destroy a host handle. Must only be called once everything
514 * running on that host has been stopped.
516 * @param host handle to destroy
519 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
521 struct RegisteredController *rc;
524 GNUNET_assert (host->id < host_list_size);
525 GNUNET_assert (host_list[host->id] == host);
526 host_list[host->id] = NULL;
527 /* clear registered controllers list */
528 for (rc = host->rc_head; NULL != rc; rc = host->rc_head)
530 GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
533 GNUNET_free_non_null ((char *) host->username);
534 GNUNET_free_non_null ((char *) host->hostname);
535 GNUNET_TESTBED_operation_queue_destroy_
536 (host->opq_parallel_overlay_connect_operations);
537 GNUNET_TESTBED_SD_destroy_ (host->poc_sd);
538 GNUNET_free_non_null (host->tslots);
539 GNUNET_CONFIGURATION_destroy (host->cfg);
541 while (host_list_size >= HOST_LIST_GROW_STEP)
543 for (id = host_list_size - 1; id > host_list_size - HOST_LIST_GROW_STEP;
545 if (NULL != host_list[id])
547 if (id != host_list_size - HOST_LIST_GROW_STEP)
549 if (NULL != host_list[id])
551 host_list_size -= HOST_LIST_GROW_STEP;
554 GNUNET_realloc (host_list,
555 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size);
560 * Marks a host as registered with a controller
562 * @param host the host to mark
563 * @param controller the controller at which this host is registered
566 GNUNET_TESTBED_mark_host_registered_at_ (struct GNUNET_TESTBED_Host *host,
567 const struct GNUNET_TESTBED_Controller
570 struct RegisteredController *rc;
572 for (rc = host->rc_head; NULL != rc; rc = rc->next)
574 if (controller == rc->controller) /* already registered at controller */
580 rc = GNUNET_malloc (sizeof (struct RegisteredController));
581 rc->controller = controller;
582 GNUNET_CONTAINER_DLL_insert_tail (host->rc_head, host->rc_tail, rc);
587 * Checks whether a host has been registered
589 * @param host the host to check
590 * @param controller the controller at which host's registration is checked
591 * @return GNUNET_YES if registered; GNUNET_NO if not
594 GNUNET_TESTBED_is_host_registered_ (const struct GNUNET_TESTBED_Host *host,
595 const struct GNUNET_TESTBED_Controller
598 struct RegisteredController *rc;
600 for (rc = host->rc_head; NULL != rc; rc = rc->next)
602 if (controller == rc->controller) /* already registered at controller */
612 * Handle for controller process
614 struct GNUNET_TESTBED_ControllerProc
619 struct GNUNET_HELPER_Handle *helper;
622 * The arguments used to start the helper
627 * The host where the helper is run
629 struct GNUNET_TESTBED_Host *host;
632 * The controller error callback
634 GNUNET_TESTBED_ControllerStatusCallback cb;
637 * The closure for the above callback
642 * The send handle for the helper
644 struct GNUNET_HELPER_SendHandle *shandle;
647 * The message corresponding to send handle
649 struct GNUNET_MessageHeader *msg;
655 * Function to copy NULL terminated list of arguments
657 * @param argv the NULL terminated list of arguments. Cannot be NULL.
658 * @return the copied NULL terminated arguments
661 copy_argv (const char *const *argv)
666 GNUNET_assert (NULL != argv);
667 for (argp = 0; NULL != argv[argp]; argp++) ;
668 argv_dup = GNUNET_malloc (sizeof (char *) * (argp + 1));
669 for (argp = 0; NULL != argv[argp]; argp++)
670 argv_dup[argp] = strdup (argv[argp]);
676 * Function to join NULL terminated list of arguments
678 * @param argv1 the NULL terminated list of arguments. Cannot be NULL.
679 * @param argv2 the NULL terminated list of arguments. Cannot be NULL.
680 * @return the joined NULL terminated arguments
683 join_argv (const char *const *argv1, const char *const *argv2)
692 for (cnt = 0; NULL != argv1[cnt]; cnt++)
694 argv = GNUNET_strdup (argv1[cnt]);
695 GNUNET_array_append (argvj, carg, argv);
697 for (cnt = 0; NULL != argv2[cnt]; cnt++)
699 argv = GNUNET_strdup (argv2[cnt]);
700 GNUNET_array_append (argvj, carg, argv);
702 GNUNET_array_append (argvj, carg, NULL);
708 * Frees the given NULL terminated arguments
710 * @param argv the NULL terminated list of arguments
713 free_argv (char **argv)
717 for (argp = 0; NULL != argv[argp]; argp++)
718 GNUNET_free (argv[argp]);
724 * Generates arguments for opening a remote shell. Builds up the arguments
725 * from the environment variable GNUNET_TESTBED_RSH_CMD. The variable
726 * should not mention `-p' (port) option and destination address as these will
727 * be set locally in the function from its parameteres. If the environmental
728 * variable is not found then it defaults to `ssh -o BatchMode=yes -o
729 * NoHostAuthenticationForLocalhost=yes'
731 * @param port the destination port number
732 * @param dst the destination address
733 * @return NULL terminated list of arguments
736 gen_rsh_args (const char *port, const char *dst)
738 static const char *default_ssh_args[] = {
743 "NoHostAuthenticationForLocalhost=yes",
753 if (NULL != (ssh_cmd = getenv ("GNUNET_TESTBED_RSH_CMD")))
755 ssh_cmd = GNUNET_strdup (ssh_cmd);
756 ssh_cmd_cp = ssh_cmd;
757 for (cnt = 0; NULL != (arg = strtok (ssh_cmd, " ")); ssh_cmd = NULL)
758 GNUNET_array_append (ssh_args, cnt, GNUNET_strdup (arg));
759 GNUNET_free (ssh_cmd_cp);
763 ssh_args = copy_argv (default_ssh_args);
764 cnt = (sizeof (default_ssh_args)) / (sizeof (const char *));
765 GNUNET_array_grow (ssh_args, cnt, cnt - 1);
767 GNUNET_array_append (ssh_args, cnt, GNUNET_strdup ("-p"));
768 GNUNET_array_append (ssh_args, cnt, GNUNET_strdup (port));
769 GNUNET_array_append (ssh_args, cnt, GNUNET_strdup (dst));
770 GNUNET_array_append (ssh_args, cnt, NULL);
776 * Generates the arguments needed for executing the given binary in a remote
777 * shell. Builds the arguments from the environmental variable
778 * GNUNET_TETSBED_RSH_CMD_SUFFIX. If the environmental variable is not found,
779 * only the given binary name will be present in the returned arguments
781 * @param append_args the arguments to append after generating the suffix
782 * arguments. Can be NULL; if not must be NULL terminated 'char *' array
783 * @return NULL-terminated args
786 gen_rsh_suffix_args (const char * const *append_args)
793 unsigned int append_cnt;
797 if (NULL != (rshell_cmd = getenv ("GNUNET_TESTBED_RSH_CMD_SUFFIX")))
799 rshell_cmd = GNUNET_strdup (rshell_cmd);
800 rshell_cmd_cp = rshell_cmd;
801 for (; NULL != (arg = strtok (rshell_cmd, " ")); rshell_cmd = NULL)
802 GNUNET_array_append (rshell_args, cnt, GNUNET_strdup (arg));
803 GNUNET_free (rshell_cmd_cp);
805 if (NULL != append_args)
807 for (append_cnt = 0; NULL != append_args[append_cnt]; append_cnt++)
808 GNUNET_array_append (rshell_args, cnt, GNUNET_strdup (append_args[append_cnt]));
810 GNUNET_array_append (rshell_args, cnt, NULL);
816 * Functions with this signature are called whenever a
817 * complete message is received by the tokenizer.
819 * Do not call GNUNET_SERVER_mst_destroy in callback
822 * @param client identification of the client
823 * @param message the actual message
825 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
828 helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
830 struct GNUNET_TESTBED_ControllerProc *cp = cls;
831 const struct GNUNET_TESTBED_HelperReply *msg;
832 const char *hostname;
837 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
838 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) <
839 ntohs (msg->header.size));
840 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY ==
841 ntohs (msg->header.type));
842 config_size = (uLongf) ntohs (msg->config_size);
844 (uLongf) (ntohs (msg->header.size) -
845 sizeof (struct GNUNET_TESTBED_HelperReply));
846 config = GNUNET_malloc (config_size);
847 GNUNET_assert (Z_OK ==
848 uncompress ((Bytef *) config, &config_size,
849 (const Bytef *) &msg[1], xconfig_size));
850 /* Replace the configuration template present in the host with the
851 controller's running configuration */
852 GNUNET_CONFIGURATION_destroy (cp->host->cfg);
853 cp->host->cfg = GNUNET_CONFIGURATION_create ();
854 GNUNET_assert (GNUNET_CONFIGURATION_deserialize
855 (cp->host->cfg, config, config_size, GNUNET_NO));
856 GNUNET_free (config);
857 if ((NULL == cp->host) ||
858 (NULL == (hostname = GNUNET_TESTBED_host_get_hostname (cp->host))))
859 hostname = "localhost";
860 /* Change the hostname so that we can connect to it */
861 GNUNET_CONFIGURATION_set_value_string (cp->host->cfg, "testbed", "hostname",
863 cp->host->locked = GNUNET_NO;
864 cp->host->controller_started = GNUNET_YES;
865 cp->cb (cp->cls, cp->host->cfg, GNUNET_OK);
871 * Continuation function from GNUNET_HELPER_send()
874 * @param result GNUNET_OK on success,
875 * GNUNET_NO if helper process died
876 * GNUNET_SYSERR during GNUNET_HELPER_stop
879 clear_msg (void *cls, int result)
881 struct GNUNET_TESTBED_ControllerProc *cp = cls;
883 GNUNET_assert (NULL != cp->shandle);
885 GNUNET_free (cp->msg);
890 * Callback that will be called when the helper process dies. This is not called
891 * when the helper process is stoped using GNUNET_HELPER_stop()
893 * @param cls the closure from GNUNET_HELPER_start()
896 helper_exp_cb (void *cls)
898 struct GNUNET_TESTBED_ControllerProc *cp = cls;
899 GNUNET_TESTBED_ControllerStatusCallback cb;
905 GNUNET_TESTBED_controller_stop (cp);
907 cb (cb_cls, NULL, GNUNET_SYSERR);
912 * Starts a controller process at the given host
914 * @param trusted_ip the ip address of the controller which will be set as TRUSTED
915 * HOST(all connections form this ip are permitted by the testbed) when
916 * starting testbed controller at host. This can either be a single ip
917 * address or a network address in CIDR notation.
918 * @param host the host where the controller has to be started; NULL for
920 * @param cfg template configuration to use for the remote controller; the
921 * remote controller will be started with a slightly modified
922 * configuration (port numbers, unix domain sockets and service home
923 * values are changed as per TESTING library on the remote host)
924 * @param cb function called when the controller is successfully started or
925 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
926 * called if cb is called with GNUNET_SYSERR as status. Will never be
927 * called in the same task as 'GNUNET_TESTBED_controller_start'
928 * (synchronous errors will be signalled by returning NULL). This
929 * parameter cannot be NULL.
930 * @param cls closure for above callbacks
931 * @return the controller process handle, NULL on errors
933 struct GNUNET_TESTBED_ControllerProc *
934 GNUNET_TESTBED_controller_start (const char *trusted_ip,
935 struct GNUNET_TESTBED_Host *host,
936 const struct GNUNET_CONFIGURATION_Handle *cfg,
937 GNUNET_TESTBED_ControllerStatusCallback cb,
940 struct GNUNET_TESTBED_ControllerProc *cp;
941 struct GNUNET_TESTBED_HelperInit *msg;
942 const char *hostname;
944 static char *const binary_argv[] = {
945 HELPER_TESTBED_BINARY, NULL
949 API_VIOLATION (GNUNET_NO == host->locked,
950 "Host is already locked by a previous call to GNUNET_TESTBED_controller_start()");
951 host->locked = GNUNET_YES;
952 API_VIOLATION (GNUNET_NO == host->controller_started,
953 "Attempting to start a controller on a host which is already started a controller");
954 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
955 if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
958 GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv,
959 &helper_mst, &helper_exp_cb, cp);
963 char *helper_binary_path_args[2];
965 char **rsh_suffix_args;
966 const char *username;
970 username = GNUNET_TESTBED_host_get_username_ (host);
971 hostname = GNUNET_TESTBED_host_get_hostname (host);
972 GNUNET_asprintf (&port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
973 if (NULL == username)
974 GNUNET_asprintf (&dst, "%s", hostname);
976 GNUNET_asprintf (&dst, "%s@%s", username, hostname);
977 LOG_DEBUG ("Starting SSH to destination %s\n", dst);
980 GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
981 "HELPER_BINARY_PATH",
982 &helper_binary_path_args[0]))
983 helper_binary_path_args[0] =
984 GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
985 helper_binary_path_args[1] = NULL;
986 rsh_args = gen_rsh_args (port, dst);
987 rsh_suffix_args = gen_rsh_suffix_args ((const char **) helper_binary_path_args);
989 join_argv ((const char **) rsh_args, (const char **) rsh_suffix_args);
990 free_argv (rsh_args);
991 free_argv (rsh_suffix_args);
995 GNUNET_HELPER_start (GNUNET_NO, cp->helper_argv[0], cp->helper_argv, &helper_mst,
997 GNUNET_free (helper_binary_path_args[0]);
999 if (NULL == cp->helper)
1001 if (NULL != cp->helper_argv)
1002 free_argv (cp->helper_argv);
1009 msg = GNUNET_TESTBED_create_helper_init_msg_ (trusted_ip, hostname, cfg);
1010 cp->msg = &msg->header;
1012 GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO, &clear_msg, cp);
1013 if (NULL == cp->shandle)
1016 GNUNET_TESTBED_controller_stop (cp);
1024 * Stop the controller process (also will terminate all peers and controllers
1025 * dependent on this controller). This function blocks until the testbed has
1026 * been fully terminated (!). The controller status cb from
1027 * GNUNET_TESTBED_controller_start() will not be called.
1029 * @param cproc the controller process handle
1032 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
1034 if (NULL != cproc->shandle)
1035 GNUNET_HELPER_send_cancel (cproc->shandle);
1036 if (NULL != cproc->helper)
1037 GNUNET_HELPER_soft_stop (cproc->helper);
1038 if (NULL != cproc->helper_argv)
1039 free_argv (cproc->helper_argv);
1040 cproc->host->controller_started = GNUNET_NO;
1041 cproc->host->locked = GNUNET_NO;
1042 GNUNET_free (cproc);
1047 * The handle for whether a host is habitable or not
1049 struct GNUNET_TESTBED_HostHabitableCheckHandle
1054 const struct GNUNET_TESTBED_Host *host;
1057 * The callback to call once we have the status
1059 GNUNET_TESTBED_HostHabitableCallback cb;
1062 * The callback closure
1067 * The process handle for the SSH process
1069 struct GNUNET_OS_Process *auxp;
1072 * The arguments used to start the helper
1077 * Task id for the habitability check task
1079 GNUNET_SCHEDULER_TaskIdentifier habitability_check_task;
1082 * How long we wait before checking the process status. Should grow
1085 struct GNUNET_TIME_Relative wait_time;
1091 * Task for checking whether a host is habitable or not
1093 * @param cls GNUNET_TESTBED_HostHabitableCheckHandle
1094 * @param tc the scheduler task context
1097 habitability_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1099 struct GNUNET_TESTBED_HostHabitableCheckHandle *h = cls;
1101 GNUNET_TESTBED_HostHabitableCallback cb;
1102 const struct GNUNET_TESTBED_Host *host;
1104 enum GNUNET_OS_ProcessStatusType type;
1107 h->habitability_check_task = GNUNET_SCHEDULER_NO_TASK;
1108 ret = GNUNET_OS_process_status (h->auxp, &type, &code);
1109 if (GNUNET_SYSERR == ret)
1115 if (GNUNET_NO == ret)
1117 h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
1118 h->habitability_check_task =
1119 GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
1122 GNUNET_OS_process_destroy (h->auxp);
1124 ret = (0 != code) ? GNUNET_NO : GNUNET_YES;
1127 if (NULL != h->auxp)
1128 GNUNET_OS_process_destroy (h->auxp);
1134 cb (cb_cls, host, ret);
1139 * Checks whether a host can be used to start testbed service
1141 * @param host the host to check
1142 * @param config the configuration handle to lookup the path of the testbed
1144 * @param cb the callback to call to inform about habitability of the given host
1145 * @param cb_cls the closure for the callback
1146 * @return NULL upon any error or a handle which can be passed to
1147 * GNUNET_TESTBED_is_host_habitable_cancel()
1149 struct GNUNET_TESTBED_HostHabitableCheckHandle *
1150 GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
1151 const struct GNUNET_CONFIGURATION_Handle
1153 GNUNET_TESTBED_HostHabitableCallback cb,
1156 struct GNUNET_TESTBED_HostHabitableCheckHandle *h;
1158 char **rsh_suffix_args;
1160 const char *hostname;
1164 h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostHabitableCheckHandle));
1168 hostname = (NULL == host->hostname) ? "127.0.0.1" : host->hostname;
1169 if (NULL == host->username)
1170 dst = GNUNET_strdup (hostname);
1172 GNUNET_asprintf (&dst, "%s@%s", host->username, hostname);
1174 GNUNET_CONFIGURATION_get_value_string (config, "testbed",
1175 "HELPER_BINARY_PATH",
1178 GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
1179 GNUNET_asprintf (&port, "%u", host->port);
1180 rsh_args = gen_rsh_args (port, dst);
1185 stat_args[0] = "stat";
1186 stat_args[2] = NULL;
1187 rsh_suffix_args = gen_rsh_suffix_args ((const char **) stat_args);
1188 h->helper_argv = join_argv ((const char **) rsh_args,
1189 (const char **) rsh_suffix_args);
1190 GNUNET_free (rsh_suffix_args);
1191 GNUNET_free (rsh_args);
1193 GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, NULL,
1194 NULL, h->helper_argv[0], h->helper_argv);
1195 if (NULL == h->auxp)
1197 GNUNET_break (0); /* Cannot exec SSH? */
1201 h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
1202 h->habitability_check_task =
1203 GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
1209 * Function to cancel a request started using GNUNET_TESTBED_is_host_habitable()
1211 * @param handle the habitability check handle
1214 GNUNET_TESTBED_is_host_habitable_cancel (struct
1215 GNUNET_TESTBED_HostHabitableCheckHandle
1218 GNUNET_SCHEDULER_cancel (handle->habitability_check_task);
1219 (void) GNUNET_OS_process_kill (handle->auxp, SIGTERM);
1220 (void) GNUNET_OS_process_wait (handle->auxp);
1221 GNUNET_OS_process_destroy (handle->auxp);
1222 free_argv (handle->helper_argv);
1223 GNUNET_free (handle);
1228 * handle for host registration
1230 struct GNUNET_TESTBED_HostRegistrationHandle
1233 * The host being registered
1235 struct GNUNET_TESTBED_Host *host;
1238 * The controller at which this host is being registered
1240 struct GNUNET_TESTBED_Controller *c;
1243 * The Registartion completion callback
1245 GNUNET_TESTBED_HostRegistrationCompletion cc;
1248 * The closure for above callback
1255 * Register a host with the controller
1257 * @param controller the controller handle
1258 * @param host the host to register
1259 * @param cc the completion callback to call to inform the status of
1260 * registration. After calling this callback the registration handle
1261 * will be invalid. Cannot be NULL.
1262 * @param cc_cls the closure for the cc
1263 * @return handle to the host registration which can be used to cancel the
1266 struct GNUNET_TESTBED_HostRegistrationHandle *
1267 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1268 struct GNUNET_TESTBED_Host *host,
1269 GNUNET_TESTBED_HostRegistrationCompletion cc,
1272 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1273 struct GNUNET_TESTBED_AddHostMessage *msg;
1274 const char *username;
1275 const char *hostname;
1282 uint16_t username_length;
1283 uint16_t hostname_length;
1285 if (NULL != controller->rh)
1287 hostname = GNUNET_TESTBED_host_get_hostname (host);
1288 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1290 LOG (GNUNET_ERROR_TYPE_WARNING, "Host hostname: %s already registered\n",
1291 (NULL == hostname) ? "localhost" : hostname);
1294 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1297 GNUNET_assert (NULL != cc);
1299 rh->cc_cls = cc_cls;
1300 controller->rh = rh;
1301 username = GNUNET_TESTBED_host_get_username_ (host);
1302 username_length = 0;
1303 if (NULL != username)
1304 username_length = strlen (username);
1305 GNUNET_assert (NULL != hostname); /* Hostname must be present */
1306 hostname_length = strlen (hostname);
1307 GNUNET_assert (NULL != host->cfg);
1308 config = GNUNET_CONFIGURATION_serialize (host->cfg, &config_size);
1309 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1310 GNUNET_free (config);
1311 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1312 msg_size += username_length;
1313 msg_size += hostname_length;
1314 msg_size += cc_size;
1315 msg = GNUNET_malloc (msg_size);
1316 msg->header.size = htons (msg_size);
1317 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST);
1318 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1319 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1321 if (NULL != username)
1323 msg->username_length = htons (username_length);
1324 ptr = memcpy (ptr, username, username_length);
1325 ptr += username_length;
1327 msg->hostname_length = htons (hostname_length);
1328 ptr = memcpy (ptr, hostname, hostname_length);
1329 ptr += hostname_length;
1330 msg->config_size = htons (config_size);
1331 ptr = memcpy (ptr, cconfig, cc_size);
1333 GNUNET_assert ((ptr - (void *) msg) == msg_size);
1334 GNUNET_free (cconfig);
1335 GNUNET_TESTBED_queue_message_ (controller,
1336 (struct GNUNET_MessageHeader *) msg);
1342 * Cancel the pending registration. Note that if the registration message is
1343 * already sent to the service the cancellation has only the effect that the
1344 * registration completion callback for the registration is never called.
1346 * @param handle the registration handle to cancel
1349 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1352 if (handle != handle->c->rh)
1357 handle->c->rh = NULL;
1358 GNUNET_free (handle);
1363 * Initializes the operation queue for parallel overlay connects
1365 * @param h the host handle
1366 * @param npoc the number of parallel overlay connects - the queue size
1369 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (struct
1370 GNUNET_TESTBED_Host *h,
1373 //fprintf (stderr, "%d", npoc);
1374 GNUNET_free_non_null (h->tslots);
1375 h->tslots_filled = 0;
1376 h->num_parallel_connects = npoc;
1377 h->tslots = GNUNET_malloc (npoc * sizeof (struct TimeSlot));
1378 GNUNET_TESTBED_operation_queue_reset_max_active_
1379 (h->opq_parallel_overlay_connect_operations, npoc);
1384 * Returns a timing slot which will be exclusively locked
1386 * @param h the host handle
1387 * @param key a pointer which is associated to the returned slot; should not be
1388 * NULL. It serves as a key to determine the correct owner of the slot
1389 * @return the time slot index in the array of time slots in the controller
1393 GNUNET_TESTBED_get_tslot_ (struct GNUNET_TESTBED_Host *h, void *key)
1397 GNUNET_assert (NULL != h->tslots);
1398 GNUNET_assert (NULL != key);
1399 for (slot = 0; slot < h->num_parallel_connects; slot++)
1400 if (NULL == h->tslots[slot].key)
1402 h->tslots[slot].key = key;
1405 GNUNET_assert (0); /* We should always find a free tslot */
1410 * Decides whether any change in the number of parallel overlay connects is
1411 * necessary to adapt to the load on the system
1413 * @param h the host handle
1416 decide_npoc (struct GNUNET_TESTBED_Host *h)
1418 struct GNUNET_TIME_Relative avg;
1423 if (h->tslots_filled != h->num_parallel_connects)
1425 avg = GNUNET_TIME_UNIT_ZERO;
1427 for (slot = 0; slot < h->num_parallel_connects; slot++)
1429 avg = GNUNET_TIME_relative_add (avg, h->tslots[slot].time);
1430 nvals += h->tslots[slot].nvals;
1432 GNUNET_assert (nvals >= h->num_parallel_connects);
1433 avg = GNUNET_TIME_relative_divide (avg, nvals);
1434 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != avg.rel_value);
1435 sd = GNUNET_TESTBED_SD_deviation_factor_ (h->poc_sd, (unsigned int) avg.rel_value);
1437 (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1438 h->num_parallel_connects)) )
1439 GNUNET_TESTBED_SD_add_data_ (h->poc_sd, (unsigned int) avg.rel_value);
1440 if (GNUNET_SYSERR == sd)
1442 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1443 h->num_parallel_connects);
1446 GNUNET_assert (0 <= sd);
1449 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1450 h->num_parallel_connects
1456 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1457 h->num_parallel_connects
1461 if (1 == h->num_parallel_connects)
1463 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h, 1);
1468 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1469 h->num_parallel_connects
1473 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1474 h->num_parallel_connects /
1480 * Releases a time slot thus making it available for be used again
1482 * @param h the host handle
1483 * @param index the index of the the time slot
1484 * @param key the key to prove ownership of the timeslot
1485 * @return GNUNET_YES if the time slot is successfully removed; GNUNET_NO if the
1486 * time slot cannot be removed - this could be because of the index
1487 * greater than existing number of time slots or `key' being different
1490 GNUNET_TESTBED_release_time_slot_ (struct GNUNET_TESTBED_Host *h,
1491 unsigned int index, void *key)
1493 struct TimeSlot *slot;
1495 GNUNET_assert (NULL != key);
1496 if (index >= h->num_parallel_connects)
1498 slot = &h->tslots[index];
1499 if (key != slot->key)
1507 * Function to update a time slot
1509 * @param h the host handle
1510 * @param index the index of the time slot to update
1511 * @param key the key to identify ownership of the slot
1512 * @param time the new time
1513 * @param failed should this reading be treated as coming from a fail event
1516 GNUNET_TESTBED_update_time_slot_ (struct GNUNET_TESTBED_Host *h,
1517 unsigned int index, void *key,
1518 struct GNUNET_TIME_Relative time, int failed)
1520 struct TimeSlot *slot;
1522 if (GNUNET_YES == failed)
1524 if (1 == h->num_parallel_connects)
1526 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h, 1);
1529 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
1530 h->num_parallel_connects
1533 if (GNUNET_NO == GNUNET_TESTBED_release_time_slot_ (h, index, key))
1535 slot = &h->tslots[index];
1537 if (GNUNET_TIME_UNIT_ZERO.rel_value == slot->time.rel_value)
1544 slot->time = GNUNET_TIME_relative_add (slot->time, time);
1549 * Queues the given operation in the queue for parallel overlay connects of the
1552 * @param h the host handle
1553 * @param op the operation to queue in the given host's parally overlay connect
1557 GNUNET_TESTBED_host_queue_oc_ (struct GNUNET_TESTBED_Host *h,
1558 struct GNUNET_TESTBED_Operation *op)
1560 GNUNET_TESTBED_operation_queue_insert_
1561 (h->opq_parallel_overlay_connect_operations, op);
1566 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
1567 * controller (testbed service)
1569 * @param c the controller handler
1570 * @param msg message received
1571 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
1575 GNUNET_TESTBED_host_handle_addhostconfirm_ (struct GNUNET_TESTBED_Controller *c,
1577 GNUNET_TESTBED_HostConfirmedMessage
1580 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1589 if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
1591 LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
1592 GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
1596 msg_size = ntohs (msg->header.size);
1597 if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
1599 LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
1600 GNUNET_TESTBED_mark_host_registered_at_ (rh->host, c);
1601 rh->cc (rh->cc_cls, NULL);
1605 /* We have an error message */
1606 emsg = (char *) &msg[1];
1608 emsg[msg_size - sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
1614 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
1615 ntohl (msg->host_id), emsg);
1616 rh->cc (rh->cc_cls, emsg);
1621 /* end of testbed_api_hosts.c */