2 This file is part of GNUnet
3 Copyright (C) 2008--2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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"
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 * Log an error message at log-level 'level' that indicates a failure of the
67 * command 'cmd' with the message given by gai_strerror(rc).
69 #define LOG_GAI(level, cmd, rc) do { LOG(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gai_strerror(rc)); } while(0)
72 * Number of extra elements we create space for when we grow host list
74 #define HOST_LIST_GROW_STEP 10
78 * A list entry for registered controllers list
80 struct RegisteredController
83 * The controller at which this host is registered
85 const struct GNUNET_TESTBED_Controller *controller;
88 * The next ptr for DLL
90 struct RegisteredController *next;
93 * The prev ptr for DLL
95 struct RegisteredController *prev;
100 * Opaque handle to a host running experiments managed by the testing framework.
101 * The master process must be able to SSH to this host without password (via
104 struct GNUNET_TESTBED_Host
108 * The hostname of the host; NULL for localhost
110 const char *hostname;
113 * The username to be used for SSH login
115 const char *username;
118 * the configuration to use as a template while starting a controller on this
119 * host. Operation queue size specific to a host are also read from this
120 * configuration handle. After starting the controller, it points to the actual
121 * configuration with which the controller is running
123 struct GNUNET_CONFIGURATION_Handle *cfg;
126 * The head for the list of controllers where this host is registered
128 struct RegisteredController *rc_head;
131 * The tail for the list of controllers where this host is registered
133 struct RegisteredController *rc_tail;
136 * Operation queue for simultaneous overlay connect operations target at this
139 struct OperationQueue *opq_parallel_overlay_connect_operations;
142 * Is a controller started on this host? FIXME: Is this needed?
144 int controller_started;
147 * Is this host locked by GNUNET_TESTBED_controller_start()?
152 * Global ID we use to refer to a host on the network
157 * The port which is to be used for SSH
165 * Array of available hosts
167 static struct GNUNET_TESTBED_Host **host_list;
170 * The size of the available hosts list
172 static unsigned int host_list_size;
176 * Lookup a host by ID.
178 * @param id global host ID assigned to the host; 0 is
179 * reserved to always mean 'localhost'
180 * @return handle to the host, NULL if host not found
182 struct GNUNET_TESTBED_Host *
183 GNUNET_TESTBED_host_lookup_by_id_ (uint32_t id)
185 if (host_list_size <= id)
187 return host_list[id];
192 * Create a host by ID; given this host handle, we could not
193 * run peers at the host, but we can talk about the host
196 * @param id global host ID assigned to the host; 0 is
197 * reserved to always mean 'localhost'
198 * @param cfg the configuration to use as a template while starting a controller
199 * on this host. Operation queue sizes specific to a host are also
200 * read from this configuration handle
201 * @return handle to the host, NULL on error
203 struct GNUNET_TESTBED_Host *
204 GNUNET_TESTBED_host_create_by_id_ (uint32_t id,
205 const struct GNUNET_CONFIGURATION_Handle *cfg)
207 return GNUNET_TESTBED_host_create_with_id (id, NULL, NULL, cfg, 0);
212 * Obtain the host's unique global ID.
214 * @param host handle to the host, NULL means 'localhost'
215 * @return id global host ID assigned to the host (0 is
216 * 'localhost', but then obviously not globally unique)
219 GNUNET_TESTBED_host_get_id_ (const struct GNUNET_TESTBED_Host * host)
226 * Obtain the host's hostname.
228 * @param host handle to the host, NULL means 'localhost'
229 * @return hostname of the host
232 GNUNET_TESTBED_host_get_hostname (const struct GNUNET_TESTBED_Host *host)
234 return host->hostname;
239 * Obtain the host's username
241 * @param host handle to the host, NULL means 'localhost'
242 * @return username to login to the host
245 GNUNET_TESTBED_host_get_username_ (const struct GNUNET_TESTBED_Host *host)
247 return host->username;
252 * Obtain the host's ssh port
254 * @param host handle to the host, NULL means 'localhost'
255 * @return username to login to the host
258 GNUNET_TESTBED_host_get_ssh_port_ (const struct GNUNET_TESTBED_Host * host)
265 * Check whether a controller is already started on the given host
267 * @param host the handle to the host
268 * @return GNUNET_YES if the controller is already started; GNUNET_NO if not
271 GNUNET_TESTBED_host_controller_started (const struct GNUNET_TESTBED_Host *host)
273 return host->controller_started;
278 * Obtain the host's configuration template
280 * @param host handle to the host
281 * @return the host's configuration template
283 const struct GNUNET_CONFIGURATION_Handle *
284 GNUNET_TESTBED_host_get_cfg_ (const struct GNUNET_TESTBED_Host *host)
291 * Function to replace host's configuration
293 * @param host the host handle
294 * @param new_cfg the new configuration to replace the old one
297 GNUNET_TESTBED_host_replace_cfg_ (struct GNUNET_TESTBED_Host *host,
298 const struct GNUNET_CONFIGURATION_Handle *new_cfg)
300 GNUNET_CONFIGURATION_destroy (host->cfg);
301 host->cfg = GNUNET_CONFIGURATION_dup (new_cfg);
306 * Create a host to run peers and controllers on.
308 * @param id global host ID assigned to the host; 0 is
309 * reserved to always mean 'localhost'
310 * @param hostname name of the host, use "NULL" for localhost
311 * @param username username to use for the login; may be NULL
312 * @param cfg the configuration to use as a template while starting a controller
313 * on this host. Operation queue sizes specific to a host are also
314 * read from this configuration handle
315 * @param port port number to use for ssh; use 0 to let ssh decide
316 * @return handle to the host, NULL on error
318 struct GNUNET_TESTBED_Host *
319 GNUNET_TESTBED_host_create_with_id (uint32_t id, const char *hostname,
320 const char *username,
321 const struct GNUNET_CONFIGURATION_Handle
325 struct GNUNET_TESTBED_Host *host;
326 unsigned int new_size;
328 if ((id < host_list_size) && (NULL != host_list[id]))
330 LOG (GNUNET_ERROR_TYPE_WARNING, "Host with id: %u already created\n", id);
333 host = GNUNET_new (struct GNUNET_TESTBED_Host);
334 host->hostname = (NULL != hostname) ? GNUNET_strdup (hostname) : NULL;
335 host->username = (NULL != username) ? GNUNET_strdup (username) : NULL;
337 host->port = (0 == port) ? 22 : port;
338 host->cfg = GNUNET_CONFIGURATION_dup (cfg);
339 host->opq_parallel_overlay_connect_operations =
340 GNUNET_TESTBED_operation_queue_create_ (OPERATION_QUEUE_TYPE_ADAPTIVE,
342 new_size = host_list_size;
343 while (id >= new_size)
344 new_size += HOST_LIST_GROW_STEP;
345 if (new_size != host_list_size)
346 GNUNET_array_grow (host_list, host_list_size, new_size);
347 GNUNET_assert (id < host_list_size);
348 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding host with id: %u\n", host->id);
349 host_list[id] = host;
355 * Create a host to run peers and controllers on.
357 * @param hostname name of the host, use "NULL" for localhost
358 * @param username username to use for the login; may be NULL
359 * @param cfg the configuration to use as a template while starting a controller
360 * on this host. Operation queue sizes specific to a host are also
361 * read from this configuration handle
362 * @param port port number to use for ssh; use 0 to let ssh decide
363 * @return handle to the host, NULL on error
365 struct GNUNET_TESTBED_Host *
366 GNUNET_TESTBED_host_create (const char *hostname, const char *username,
367 const struct GNUNET_CONFIGURATION_Handle *cfg,
370 static uint32_t uid_generator;
372 if (NULL == hostname)
373 return GNUNET_TESTBED_host_create_with_id (0, hostname, username,
375 return GNUNET_TESTBED_host_create_with_id (++uid_generator, hostname,
376 username, cfg, port);
381 * Load a set of hosts from a configuration file.
383 * @param filename file with the host specification
384 * @param cfg the configuration to use as a template while starting a controller
385 * on any of the loaded hosts. Operation queue sizes specific to a host
386 * are also read from this configuration handle
387 * @param hosts set to the hosts found in the file; caller must free this if
388 * number of hosts returned is greater than 0
389 * @return number of hosts returned in 'hosts', 0 on error
392 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
393 const struct GNUNET_CONFIGURATION_Handle
395 struct GNUNET_TESTBED_Host ***hosts)
397 struct GNUNET_TESTBED_Host *starting_host;
403 regmatch_t pmatch[6];
410 GNUNET_assert (NULL != filename);
411 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
413 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s not found\n"), filename);
417 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
421 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s has no data\n"), filename);
424 data = GNUNET_malloc (fs);
425 if (fs != GNUNET_DISK_fn_read (filename, data, fs))
428 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s cannot be read\n"),
434 starting_host = NULL;
436 /* refer RFC 952 and RFC 1123 for valid hostnames */
437 GNUNET_assert (0 == regcomp (&rex,
438 "^(([[:alnum:]]+)@)?" /* username */
439 "([[:alnum:]]+[-[:alnum:]_\\.]+)" /* hostname */
440 "(:([[:digit:]]{1,5}))?", /* port */
441 REG_EXTENDED | REG_ICASE));
442 while (offset < (fs - 1))
445 if (((data[offset] == '\n')) && (buf != &data[offset]))
453 if ((REG_NOMATCH == regexec (&rex, buf, 6, pmatch, 0))
454 || (-1 == pmatch[3].rm_so))
456 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
457 "Error reading line `%s' in hostfile\n", buf);
458 buf = &data[offset + 1];
461 if (-1 != pmatch[2].rm_so)
463 size = pmatch[2].rm_eo - pmatch[2].rm_so;
464 username = GNUNET_malloc (size + 1);
465 username[size] = '\0';
466 GNUNET_assert (NULL != strncpy (username, buf + pmatch[2].rm_so, size));
468 if (-1 != pmatch[5].rm_so)
470 (void) SSCANF (buf + pmatch[5].rm_so, "%5hd", &port);
472 size = pmatch[3].rm_eo - pmatch[3].rm_so;
473 hostname = GNUNET_malloc (size + 1);
474 hostname[size] = '\0';
475 GNUNET_assert (NULL != strncpy (hostname, buf + pmatch[3].rm_so, size));
476 LOG (GNUNET_ERROR_TYPE_DEBUG,
477 "Successfully read host %s, port %d and user %s from file\n",
478 (NULL == hostname) ? "NULL" : hostname,
480 (NULL == username) ? "NULL" : username);
481 /* We store hosts in a static list; hence we only require the starting
482 * host pointer in that list to access the newly created list of hosts */
483 if (NULL == starting_host)
484 starting_host = GNUNET_TESTBED_host_create (hostname, username, cfg,
487 (void) GNUNET_TESTBED_host_create (hostname, username, cfg, port);
489 GNUNET_free_non_null (username);
490 GNUNET_free (hostname);
491 buf = &data[offset + 1];
493 else if ((data[offset] == '\n') || (data[offset] == '\0'))
494 buf = &data[offset + 1];
498 if (NULL == starting_host)
500 *hosts = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * count);
501 GNUNET_memcpy (*hosts,
502 &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)],
503 sizeof (struct GNUNET_TESTBED_Host *) * count);
509 * Resolves a hostname using getaddrinfo
511 * @param host the hostname
512 * @return the string representing the IPv4 address of the given host; NULL upon error
515 simple_resolve (const char *host)
517 struct addrinfo *res;
518 const struct sockaddr_in *in_addr;
520 struct addrinfo hint;
523 hint.ai_family = AF_INET; /* IPv4 */
524 hint.ai_socktype = 0;
525 hint.ai_protocol = 0;
528 hint.ai_canonname = NULL;
530 hint.ai_flags = AI_NUMERICSERV;
532 LOG_DEBUG ("Resolving [%s]\n", host);
533 if (0 != (rc = getaddrinfo (host, "22", &hint, &res)))
535 LOG_GAI (GNUNET_ERROR_TYPE_ERROR, "getaddrinfo", rc);
538 GNUNET_assert (NULL != res);
539 GNUNET_assert (NULL != res->ai_addr);
540 GNUNET_assert (sizeof (struct sockaddr_in) == res->ai_addrlen);
541 in_addr = (const struct sockaddr_in *) res->ai_addr;
542 hostip = inet_ntoa (in_addr->sin_addr);
543 GNUNET_assert (NULL != hostip);
545 LOG_DEBUG ("Resolved [%s] to [%s]\n", host, hostip);
551 * Loads the set of host allocated by the LoadLeveler Job Scheduler. This
552 * function is only available when compiled with support for LoadLeveler and is
553 * used for running on the SuperMUC
555 * @param cfg the configuration to use as a template while starting a controller
556 * on any of the loaded hosts. Operation queue sizes specific to a host
557 * are also read from this configuration handle
558 * @param hosts set to the hosts found in the file; caller must free this if
559 * number of hosts returned is greater than 0
560 * @return number of hosts returned in 'hosts', 0 on error
563 GNUNET_TESTBED_hosts_load_from_loadleveler (const struct
564 GNUNET_CONFIGURATION_Handle *cfg,
565 struct GNUNET_TESTBED_Host ***hosts)
568 LOG (GNUNET_ERROR_TYPE_ERROR,
569 _("The function %s is only available when compiled with (--with-ll)\n"),
573 const char *hostfile;
575 if (NULL == (hostfile = getenv ("MP_SAVEHOSTFILE")))
580 return GNUNET_TESTBED_hosts_load_from_file (hostfile, cfg, hosts);
586 * Destroy a host handle. Must only be called once everything
587 * running on that host has been stopped.
589 * @param host handle to destroy
592 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
594 struct RegisteredController *rc;
597 GNUNET_assert (host->id < host_list_size);
598 GNUNET_assert (host_list[host->id] == host);
599 host_list[host->id] = NULL;
600 /* clear registered controllers list */
601 for (rc = host->rc_head; NULL != rc; rc = host->rc_head)
603 GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
606 GNUNET_free_non_null ((char *) host->username);
607 GNUNET_free_non_null ((char *) host->hostname);
608 GNUNET_TESTBED_operation_queue_destroy_
609 (host->opq_parallel_overlay_connect_operations);
610 GNUNET_CONFIGURATION_destroy (host->cfg);
612 while (host_list_size >= HOST_LIST_GROW_STEP)
614 for (id = host_list_size - 1; id > host_list_size - HOST_LIST_GROW_STEP;
616 if (NULL != host_list[id])
618 if (id != host_list_size - HOST_LIST_GROW_STEP)
620 if (NULL != host_list[id])
622 host_list_size -= HOST_LIST_GROW_STEP;
625 GNUNET_realloc (host_list,
626 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size);
631 * Marks a host as registered with a controller
633 * @param host the host to mark
634 * @param controller the controller at which this host is registered
637 GNUNET_TESTBED_mark_host_registered_at_ (struct GNUNET_TESTBED_Host *host,
638 const struct GNUNET_TESTBED_Controller
641 struct RegisteredController *rc;
643 for (rc = host->rc_head; NULL != rc; rc = rc->next)
645 if (controller == rc->controller) /* already registered at controller */
651 rc = GNUNET_new (struct RegisteredController);
652 rc->controller = controller;
653 GNUNET_CONTAINER_DLL_insert_tail (host->rc_head, host->rc_tail, rc);
658 * Unmarks a host registered at a controller
660 * @param host the host to unmark
661 * @param controller the controller at which this host has to be unmarked
664 GNUNET_TESTBED_deregister_host_at_ (struct GNUNET_TESTBED_Host *host,
665 const struct GNUNET_TESTBED_Controller
668 struct RegisteredController *rc;
670 for (rc = host->rc_head; NULL != rc; rc=rc->next)
671 if (controller == rc->controller)
678 GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
684 * Checks whether a host has been registered
686 * @param host the host to check
687 * @param controller the controller at which host's registration is checked
688 * @return GNUNET_YES if registered; GNUNET_NO if not
691 GNUNET_TESTBED_is_host_registered_ (const struct GNUNET_TESTBED_Host *host,
692 const struct GNUNET_TESTBED_Controller
695 struct RegisteredController *rc;
697 for (rc = host->rc_head; NULL != rc; rc = rc->next)
699 if (controller == rc->controller) /* already registered at controller */
709 * Handle for controller process
711 struct GNUNET_TESTBED_ControllerProc
716 struct GNUNET_HELPER_Handle *helper;
719 * The arguments used to start the helper
724 * The host where the helper is run
726 struct GNUNET_TESTBED_Host *host;
729 * The controller error callback
731 GNUNET_TESTBED_ControllerStatusCallback cb;
734 * The closure for the above callback
739 * The send handle for the helper
741 struct GNUNET_HELPER_SendHandle *shandle;
744 * The message corresponding to send handle
746 struct GNUNET_MessageHeader *msg;
752 * Function to copy NULL terminated list of arguments
754 * @param argv the NULL terminated list of arguments. Cannot be NULL.
755 * @return the copied NULL terminated arguments
758 copy_argv (const char *const *argv)
763 GNUNET_assert (NULL != argv);
764 for (argp = 0; NULL != argv[argp]; argp++) ;
765 argv_dup = GNUNET_malloc (sizeof (char *) * (argp + 1));
766 for (argp = 0; NULL != argv[argp]; argp++)
767 argv_dup[argp] = GNUNET_strdup (argv[argp]);
773 * Function to join NULL terminated list of arguments
775 * @param argv1 the NULL terminated list of arguments. Cannot be NULL.
776 * @param argv2 the NULL terminated list of arguments. Cannot be NULL.
777 * @return the joined NULL terminated arguments
780 join_argv (const char *const *argv1, const char *const *argv2)
789 for (cnt = 0; NULL != argv1[cnt]; cnt++)
791 argv = GNUNET_strdup (argv1[cnt]);
792 GNUNET_array_append (argvj, carg, argv);
794 for (cnt = 0; NULL != argv2[cnt]; cnt++)
796 argv = GNUNET_strdup (argv2[cnt]);
797 GNUNET_array_append (argvj, carg, argv);
799 GNUNET_array_append (argvj, carg, NULL);
805 * Frees the given NULL terminated arguments
807 * @param argv the NULL terminated list of arguments
810 free_argv (char **argv)
814 for (argp = 0; NULL != argv[argp]; argp++)
815 GNUNET_free (argv[argp]);
821 * Generates arguments for opening a remote shell. Builds up the arguments
822 * from the environment variable GNUNET_TESTBED_RSH_CMD. The variable
823 * should not mention `-p' (port) option and destination address as these will
824 * be set locally in the function from its parameteres. If the environmental
825 * variable is not found then it defaults to `ssh -o BatchMode=yes -o
826 * NoHostAuthenticationForLocalhost=yes -o StrictHostkeyChecking=no -o
827 * PasswordAuthentication=noc'
829 * @param port the destination port number
830 * @param hostname the hostname of the target host
831 * @param username the username to use while connecting to target host
832 * @return NULL terminated list of arguments
835 gen_rsh_args (const char *port, const char *hostname, const char *username)
837 static const char *default_ssh_args[] = {
842 "NoHostAuthenticationForLocalhost=yes",
844 "StrictHostKeyChecking=no",
846 "PasswordAuthentication=no",
859 if (NULL != (ssh_cmd = getenv ("GNUNET_TESTBED_RSH_CMD")))
861 ssh_cmd = GNUNET_strdup (ssh_cmd);
862 ssh_cmd_cp = ssh_cmd;
863 for (size = 0; NULL != (arg = strtok (ssh_cmd, " ")); ssh_cmd = NULL)
864 GNUNET_array_append (ssh_args, size, GNUNET_strdup (arg));
865 GNUNET_free (ssh_cmd_cp);
869 ssh_args = copy_argv (default_ssh_args);
870 size = (sizeof (default_ssh_args)) / (sizeof (const char *));
871 GNUNET_array_grow (ssh_args, size, size - 1);
873 for (cnt = 0; cnt < size; cnt++)
898 ssh_args[cnt] = GNUNET_strdup (new_arg);
900 GNUNET_array_append (ssh_args, size, NULL);
906 * Generates the arguments needed for executing the given binary in a remote
907 * shell. Builds the arguments from the environmental variable
908 * GNUNET_TETSBED_RSH_CMD_SUFFIX. If the environmental variable is not found,
909 * only the given binary name will be present in the returned arguments
911 * @param append_args the arguments to append after generating the suffix
912 * arguments. Can be NULL; if not must be NULL terminated 'char *' array
913 * @return NULL-terminated args
916 gen_rsh_suffix_args (const char * const *append_args)
923 unsigned int append_cnt;
927 if (NULL != (rshell_cmd = getenv ("GNUNET_TESTBED_RSH_CMD_SUFFIX")))
929 rshell_cmd = GNUNET_strdup (rshell_cmd);
930 rshell_cmd_cp = rshell_cmd;
931 for (; NULL != (arg = strtok (rshell_cmd, " ")); rshell_cmd = NULL)
932 GNUNET_array_append (rshell_args, cnt, GNUNET_strdup (arg));
933 GNUNET_free (rshell_cmd_cp);
935 if (NULL != append_args)
937 for (append_cnt = 0; NULL != append_args[append_cnt]; append_cnt++)
938 GNUNET_array_append (rshell_args, cnt, GNUNET_strdup (append_args[append_cnt]));
940 GNUNET_array_append (rshell_args, cnt, NULL);
946 * Functions with this signature are called whenever a
947 * complete message is received by the tokenizer.
949 * Do not call GNUNET_SERVER_mst_destroy in callback
952 * @param client identification of the client
953 * @param message the actual message
955 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
958 helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
960 struct GNUNET_TESTBED_ControllerProc *cp = cls;
961 const struct GNUNET_TESTBED_HelperReply *msg;
962 const char *hostname;
967 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
968 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) <
969 ntohs (msg->header.size));
970 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY ==
971 ntohs (msg->header.type));
972 config_size = (uLongf) ntohs (msg->config_size);
974 (uLongf) (ntohs (msg->header.size) -
975 sizeof (struct GNUNET_TESTBED_HelperReply));
976 config = GNUNET_malloc (config_size);
977 GNUNET_assert (Z_OK ==
978 uncompress ((Bytef *) config, &config_size,
979 (const Bytef *) &msg[1], xconfig_size));
980 /* Replace the configuration template present in the host with the
981 controller's running configuration */
982 GNUNET_CONFIGURATION_destroy (cp->host->cfg);
983 cp->host->cfg = GNUNET_CONFIGURATION_create ();
984 GNUNET_assert (GNUNET_CONFIGURATION_deserialize
985 (cp->host->cfg, config, config_size, GNUNET_NO));
986 GNUNET_free (config);
987 if (NULL == (hostname = GNUNET_TESTBED_host_get_hostname (cp->host)))
988 hostname = "localhost";
989 /* Change the hostname so that we can connect to it */
990 GNUNET_CONFIGURATION_set_value_string (cp->host->cfg, "testbed", "hostname",
992 cp->host->locked = GNUNET_NO;
993 cp->host->controller_started = GNUNET_YES;
994 cp->cb (cp->cls, cp->host->cfg, GNUNET_OK);
1000 * Continuation function from GNUNET_HELPER_send()
1002 * @param cls closure
1003 * @param result GNUNET_OK on success,
1004 * GNUNET_NO if helper process died
1005 * GNUNET_SYSERR during GNUNET_HELPER_stop
1008 clear_msg (void *cls, int result)
1010 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1012 GNUNET_assert (NULL != cp->shandle);
1014 GNUNET_free (cp->msg);
1020 * Callback that will be called when the helper process dies. This is not called
1021 * when the helper process is stoped using GNUNET_HELPER_stop()
1023 * @param cls the closure from GNUNET_HELPER_start()
1026 helper_exp_cb (void *cls)
1028 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1029 GNUNET_TESTBED_ControllerStatusCallback cb;
1035 GNUNET_TESTBED_controller_stop (cp);
1037 cb (cb_cls, NULL, GNUNET_SYSERR);
1042 * Starts a controller process at the given host. The given host's configration
1043 * is used as a Template configuration to use for the remote controller; the
1044 * remote controller will be started with a slightly modified configuration
1045 * (port numbers, unix domain sockets and service home values are changed as per
1046 * TESTING library on the remote host). The modified configuration replaces the
1047 * host's existing configuration before signalling success through the
1048 * GNUNET_TESTBED_ControllerStatusCallback()
1050 * @param trusted_ip the ip address of the controller which will be set as TRUSTED
1051 * HOST(all connections form this ip are permitted by the testbed) when
1052 * starting testbed controller at host. This can either be a single ip
1053 * address or a network address in CIDR notation.
1054 * @param host the host where the controller has to be started. CANNOT be NULL.
1055 * @param cb function called when the controller is successfully started or
1056 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
1057 * called if cb is called with GNUNET_SYSERR as status. Will never be
1058 * called in the same task as 'GNUNET_TESTBED_controller_start'
1059 * (synchronous errors will be signalled by returning NULL). This
1060 * parameter cannot be NULL.
1061 * @param cls closure for above callbacks
1062 * @return the controller process handle, NULL on errors
1064 struct GNUNET_TESTBED_ControllerProc *
1065 GNUNET_TESTBED_controller_start (const char *trusted_ip,
1066 struct GNUNET_TESTBED_Host *host,
1067 GNUNET_TESTBED_ControllerStatusCallback cb,
1070 struct GNUNET_TESTBED_ControllerProc *cp;
1071 struct GNUNET_TESTBED_HelperInit *msg;
1072 const struct GNUNET_CONFIGURATION_Handle *cfg;
1073 const char *hostname;
1074 static char *const binary_argv[] = {
1075 HELPER_TESTBED_BINARY, NULL
1078 GNUNET_assert (NULL != host);
1079 GNUNET_assert (NULL != (cfg = GNUNET_TESTBED_host_get_cfg_ (host)));
1081 API_VIOLATION (GNUNET_NO == host->locked,
1082 "Host is already locked by a previous call to GNUNET_TESTBED_controller_start()");
1083 host->locked = GNUNET_YES;
1084 API_VIOLATION (GNUNET_NO == host->controller_started,
1085 "Attempting to start a controller on a host which is already started a controller");
1086 cp = GNUNET_new (struct GNUNET_TESTBED_ControllerProc);
1087 if (0 == GNUNET_TESTBED_host_get_id_ (host))
1090 GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv,
1091 &helper_mst, &helper_exp_cb, cp);
1095 char *helper_binary_path_args[2];
1097 char **rsh_suffix_args;
1098 const char *username;
1104 username = host->username;
1105 hostname = host->hostname;
1106 GNUNET_asprintf (&port, "%u", host->port);
1107 LOG_DEBUG ("Starting remote connection to destination %s\n", hostname);
1109 GNUNET_CONFIGURATION_get_value_filename (cfg, "testbed",
1110 "HELPER_BINARY_PATH",
1111 &helper_binary_path_args[0]))
1112 helper_binary_path_args[0] =
1113 GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
1114 helper_binary_path_args[1] = NULL;
1115 rsh_args = gen_rsh_args (port, hostname, username);
1116 rsh_suffix_args = gen_rsh_suffix_args ((const char **) helper_binary_path_args);
1118 join_argv ((const char **) rsh_args, (const char **) rsh_suffix_args);
1119 free_argv (rsh_args);
1120 free_argv (rsh_suffix_args);
1122 argstr = GNUNET_strdup ("");
1123 for (cnt = 0; NULL != cp->helper_argv[cnt]; cnt++)
1126 GNUNET_assert (0 < GNUNET_asprintf (&argstr, "%s %s", aux, cp->helper_argv[cnt]));
1129 LOG_DEBUG ("Helper cmd str: %s\n", argstr);
1130 GNUNET_free (argstr);
1132 GNUNET_HELPER_start (GNUNET_NO, cp->helper_argv[0], cp->helper_argv, &helper_mst,
1133 &helper_exp_cb, cp);
1134 GNUNET_free (helper_binary_path_args[0]);
1136 if (NULL == cp->helper)
1138 if (NULL != cp->helper_argv)
1139 free_argv (cp->helper_argv);
1146 msg = GNUNET_TESTBED_create_helper_init_msg_ (trusted_ip, hostname, cfg);
1147 cp->msg = &msg->header;
1149 GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO, &clear_msg, cp);
1150 if (NULL == cp->shandle)
1153 GNUNET_TESTBED_controller_stop (cp);
1161 * Sends termination signal to the controller's helper process
1163 * @param cproc the handle to the controller's helper process
1166 GNUNET_TESTBED_controller_kill_ (struct GNUNET_TESTBED_ControllerProc *cproc)
1168 if (NULL != cproc->shandle)
1169 GNUNET_HELPER_send_cancel (cproc->shandle);
1170 if (NULL != cproc->helper)
1171 GNUNET_HELPER_kill (cproc->helper, GNUNET_YES);
1176 * Cleans-up the controller's helper process handle
1178 * @param cproc the handle to the controller's helper process
1181 GNUNET_TESTBED_controller_destroy_ (struct GNUNET_TESTBED_ControllerProc *cproc)
1183 if (NULL != cproc->helper)
1185 GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (cproc->helper));
1186 GNUNET_HELPER_destroy (cproc->helper);
1188 if (NULL != cproc->helper_argv)
1189 free_argv (cproc->helper_argv);
1190 cproc->host->controller_started = GNUNET_NO;
1191 cproc->host->locked = GNUNET_NO;
1192 GNUNET_free_non_null (cproc->msg);
1193 GNUNET_free (cproc);
1198 * Stop the controller process (also will terminate all peers and controllers
1199 * dependent on this controller). This function blocks until the testbed has
1200 * been fully terminated (!). The controller status cb from
1201 * GNUNET_TESTBED_controller_start() will not be called.
1203 * @param cproc the controller process handle
1206 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
1208 GNUNET_TESTBED_controller_kill_ (cproc);
1209 GNUNET_TESTBED_controller_destroy_ (cproc);
1214 * The handle for whether a host is habitable or not
1216 struct GNUNET_TESTBED_HostHabitableCheckHandle
1221 const struct GNUNET_TESTBED_Host *host;
1224 * The callback to call once we have the status
1226 GNUNET_TESTBED_HostHabitableCallback cb;
1229 * The callback closure
1234 * The process handle for the SSH process
1236 struct GNUNET_OS_Process *auxp;
1239 * The arguments used to start the helper
1244 * Task id for the habitability check task
1246 struct GNUNET_SCHEDULER_Task * habitability_check_task;
1249 * How long we wait before checking the process status. Should grow
1252 struct GNUNET_TIME_Relative wait_time;
1258 * Task for checking whether a host is habitable or not
1260 * @param cls GNUNET_TESTBED_HostHabitableCheckHandle
1263 habitability_check (void *cls)
1265 struct GNUNET_TESTBED_HostHabitableCheckHandle *h = cls;
1267 GNUNET_TESTBED_HostHabitableCallback cb;
1268 const struct GNUNET_TESTBED_Host *host;
1270 enum GNUNET_OS_ProcessStatusType type;
1273 h->habitability_check_task = NULL;
1274 ret = GNUNET_OS_process_status (h->auxp, &type, &code);
1275 if (GNUNET_SYSERR == ret)
1281 if (GNUNET_NO == ret)
1283 h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
1284 h->habitability_check_task =
1285 GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
1288 GNUNET_OS_process_destroy (h->auxp);
1290 ret = (0 != code) ? GNUNET_NO : GNUNET_YES;
1293 if (NULL != h->auxp)
1294 GNUNET_OS_process_destroy (h->auxp);
1298 free_argv (h->helper_argv);
1301 cb (cb_cls, host, ret);
1306 * Checks whether a host can be used to start testbed service
1308 * @param host the host to check
1309 * @param config the configuration handle to lookup the path of the testbed
1311 * @param cb the callback to call to inform about habitability of the given host
1312 * @param cb_cls the closure for the callback
1313 * @return NULL upon any error or a handle which can be passed to
1314 * GNUNET_TESTBED_is_host_habitable_cancel()
1316 struct GNUNET_TESTBED_HostHabitableCheckHandle *
1317 GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
1318 const struct GNUNET_CONFIGURATION_Handle
1320 GNUNET_TESTBED_HostHabitableCallback cb,
1323 struct GNUNET_TESTBED_HostHabitableCheckHandle *h;
1325 char **rsh_suffix_args;
1327 const char *hostname;
1330 h = GNUNET_new (struct GNUNET_TESTBED_HostHabitableCheckHandle);
1334 hostname = (NULL == host->hostname) ? "127.0.0.1" : host->hostname;
1336 GNUNET_CONFIGURATION_get_value_filename (config, "testbed",
1337 "HELPER_BINARY_PATH",
1340 GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
1341 GNUNET_asprintf (&port, "%u", host->port);
1342 rsh_args = gen_rsh_args (port, hostname, host->username);
1345 stat_args[0] = "stat";
1346 stat_args[2] = NULL;
1347 rsh_suffix_args = gen_rsh_suffix_args ((const char **) stat_args);
1348 GNUNET_free (stat_args[1]);
1349 h->helper_argv = join_argv ((const char **) rsh_args,
1350 (const char **) rsh_suffix_args);
1351 free_argv (rsh_suffix_args);
1352 free_argv (rsh_args);
1354 GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, NULL,
1355 NULL, NULL, h->helper_argv[0], h->helper_argv);
1356 if (NULL == h->auxp)
1358 GNUNET_break (0); /* Cannot exec SSH? */
1362 h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
1363 h->habitability_check_task =
1364 GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
1370 * Function to cancel a request started using GNUNET_TESTBED_is_host_habitable()
1372 * @param handle the habitability check handle
1375 GNUNET_TESTBED_is_host_habitable_cancel (struct
1376 GNUNET_TESTBED_HostHabitableCheckHandle
1379 GNUNET_SCHEDULER_cancel (handle->habitability_check_task);
1380 (void) GNUNET_OS_process_kill (handle->auxp, GNUNET_TERM_SIG);
1381 (void) GNUNET_OS_process_wait (handle->auxp);
1382 GNUNET_OS_process_destroy (handle->auxp);
1383 free_argv (handle->helper_argv);
1384 GNUNET_free (handle);
1389 * Register a host with the controller
1391 * @param controller the controller handle
1392 * @param host the host to register
1393 * @param cc the completion callback to call to inform the status of
1394 * registration. After calling this callback the registration handle
1395 * will be invalid. Cannot be NULL.
1396 * @param cc_cls the closure for the cc
1397 * @return handle to the host registration which can be used to cancel the
1400 struct GNUNET_TESTBED_HostRegistrationHandle *
1401 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1402 struct GNUNET_TESTBED_Host *host,
1403 GNUNET_TESTBED_HostRegistrationCompletion cc,
1406 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1407 struct GNUNET_TESTBED_AddHostMessage *msg;
1408 const char *username;
1409 const char *hostname;
1416 uint16_t username_length;
1417 uint16_t hostname_length;
1419 if (NULL != controller->rh)
1421 hostname = GNUNET_TESTBED_host_get_hostname (host);
1422 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1424 LOG (GNUNET_ERROR_TYPE_WARNING, "Host hostname: %s already registered\n",
1425 (NULL == hostname) ? "localhost" : hostname);
1428 rh = GNUNET_new (struct GNUNET_TESTBED_HostRegistrationHandle);
1431 GNUNET_assert (NULL != cc);
1433 rh->cc_cls = cc_cls;
1434 controller->rh = rh;
1435 username = GNUNET_TESTBED_host_get_username_ (host);
1436 username_length = 0;
1437 if (NULL != username)
1438 username_length = strlen (username);
1439 GNUNET_assert (NULL != hostname); /* Hostname must be present */
1440 hostname_length = strlen (hostname);
1441 GNUNET_assert (NULL != host->cfg);
1442 config = GNUNET_CONFIGURATION_serialize (host->cfg, &config_size);
1443 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1444 GNUNET_free (config);
1445 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1446 msg_size += username_length;
1447 msg_size += hostname_length;
1448 msg_size += cc_size;
1449 msg = GNUNET_malloc (msg_size);
1450 msg->header.size = htons (msg_size);
1451 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST);
1452 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1453 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1455 if (NULL != username)
1457 msg->username_length = htons (username_length);
1458 GNUNET_memcpy (ptr, username, username_length);
1459 ptr += username_length;
1461 msg->hostname_length = htons (hostname_length);
1462 GNUNET_memcpy (ptr, hostname, hostname_length);
1463 ptr += hostname_length;
1464 msg->config_size = htons (config_size);
1465 GNUNET_memcpy (ptr, cconfig, cc_size);
1467 GNUNET_assert ((ptr - (void *) msg) == msg_size);
1468 GNUNET_free (cconfig);
1469 GNUNET_TESTBED_queue_message_ (controller,
1470 (struct GNUNET_MessageHeader *) msg);
1476 * Cancel the pending registration. Note that if the registration message is
1477 * already sent to the service the cancellation has only the effect that the
1478 * registration completion callback for the registration is never called.
1480 * @param handle the registration handle to cancel
1483 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1486 if (handle != handle->c->rh)
1491 handle->c->rh = NULL;
1492 GNUNET_free (handle);
1497 * Queues the given operation in the queue for parallel overlay connects of the
1500 * @param h the host handle
1501 * @param op the operation to queue in the given host's parally overlay connect
1505 GNUNET_TESTBED_host_queue_oc_ (struct GNUNET_TESTBED_Host *h,
1506 struct GNUNET_TESTBED_Operation *op)
1508 GNUNET_TESTBED_operation_queue_insert_
1509 (h->opq_parallel_overlay_connect_operations, op);
1514 * Resolves the hostname of the host to an ip address
1516 * @param host the host whose hostname is to be resolved
1519 GNUNET_TESTBED_host_resolve_ (struct GNUNET_TESTBED_Host *host)
1523 hostname = (char *) host->hostname;
1524 host->hostname = simple_resolve (hostname);
1525 if (NULL == host->hostname)
1528 host->hostname = hostname;
1531 GNUNET_free (hostname);
1532 host->hostname = GNUNET_strdup (host->hostname);
1535 /* end of testbed_api_hosts.c */