2 This file is part of GNUnet
3 (C) 2008--2013 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"
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
208 return GNUNET_TESTBED_host_create_with_id (id, NULL, NULL, cfg, 0);
213 * Obtain the host's unique global ID.
215 * @param host handle to the host, NULL means 'localhost'
216 * @return id global host ID assigned to the host (0 is
217 * 'localhost', but then obviously not globally unique)
220 GNUNET_TESTBED_host_get_id_ (const struct GNUNET_TESTBED_Host * host)
227 * Obtain the host's hostname.
229 * @param host handle to the host, NULL means 'localhost'
230 * @return hostname of the host
233 GNUNET_TESTBED_host_get_hostname (const struct GNUNET_TESTBED_Host *host)
235 return host->hostname;
240 * Obtain the host's username
242 * @param host handle to the host, NULL means 'localhost'
243 * @return username to login to the host
246 GNUNET_TESTBED_host_get_username_ (const struct GNUNET_TESTBED_Host *host)
248 return host->username;
253 * Obtain the host's ssh port
255 * @param host handle to the host, NULL means 'localhost'
256 * @return username to login to the host
259 GNUNET_TESTBED_host_get_ssh_port_ (const struct GNUNET_TESTBED_Host * host)
266 * Check whether a controller is already started on the given host
268 * @param host the handle to the host
269 * @return GNUNET_YES if the controller is already started; GNUNET_NO if not
272 GNUNET_TESTBED_host_controller_started (const struct GNUNET_TESTBED_Host *host)
274 return host->controller_started;
279 * Obtain the host's configuration template
281 * @param host handle to the host
282 * @return the host's configuration template
284 const struct GNUNET_CONFIGURATION_Handle *
285 GNUNET_TESTBED_host_get_cfg_ (const struct GNUNET_TESTBED_Host *host)
292 * Function to replace host's configuration
294 * @param host the host handle
295 * @param new_cfg the new configuration to replace the old one
298 GNUNET_TESTBED_host_replace_cfg_ (struct GNUNET_TESTBED_Host *host,
299 const struct GNUNET_CONFIGURATION_Handle *new_cfg)
301 GNUNET_CONFIGURATION_destroy (host->cfg);
302 host->cfg = GNUNET_CONFIGURATION_dup (new_cfg);
307 * Create a host to run peers and controllers on.
309 * @param id global host ID assigned to the host; 0 is
310 * reserved to always mean 'localhost'
311 * @param hostname name of the host, use "NULL" for localhost
312 * @param username username to use for the login; may be NULL
313 * @param cfg the configuration to use as a template while starting a controller
314 * on this host. Operation queue sizes specific to a host are also
315 * read from this configuration handle
316 * @param port port number to use for ssh; use 0 to let ssh decide
317 * @return handle to the host, NULL on error
319 struct GNUNET_TESTBED_Host *
320 GNUNET_TESTBED_host_create_with_id (uint32_t id, const char *hostname,
321 const char *username,
322 const struct GNUNET_CONFIGURATION_Handle
326 struct GNUNET_TESTBED_Host *host;
327 unsigned int new_size;
329 if ((id < host_list_size) && (NULL != host_list[id]))
331 LOG (GNUNET_ERROR_TYPE_WARNING, "Host with id: %u already created\n", id);
334 host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
335 host->hostname = (NULL != hostname) ? GNUNET_strdup (hostname) : NULL;
336 host->username = (NULL != username) ? GNUNET_strdup (username) : NULL;
338 host->port = (0 == port) ? 22 : port;
339 host->cfg = GNUNET_CONFIGURATION_dup (cfg);
340 host->opq_parallel_overlay_connect_operations =
341 GNUNET_TESTBED_operation_queue_create_ (OPERATION_QUEUE_TYPE_ADAPTIVE,
343 new_size = host_list_size;
344 while (id >= new_size)
345 new_size += HOST_LIST_GROW_STEP;
346 if (new_size != host_list_size)
347 GNUNET_array_grow (host_list, host_list_size, new_size);
348 GNUNET_assert (id < host_list_size);
349 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding host with id: %u\n", host->id);
350 host_list[id] = host;
356 * Create a host to run peers and controllers on.
358 * @param hostname name of the host, use "NULL" for localhost
359 * @param username username to use for the login; may be NULL
360 * @param cfg the configuration to use as a template while starting a controller
361 * on this host. Operation queue sizes specific to a host are also
362 * read from this configuration handle
363 * @param port port number to use for ssh; use 0 to let ssh decide
364 * @return handle to the host, NULL on error
366 struct GNUNET_TESTBED_Host *
367 GNUNET_TESTBED_host_create (const char *hostname, const char *username,
368 const struct GNUNET_CONFIGURATION_Handle *cfg,
371 static uint32_t uid_generator;
373 if (NULL == hostname)
374 return GNUNET_TESTBED_host_create_with_id (0, hostname, username,
376 return GNUNET_TESTBED_host_create_with_id (++uid_generator, hostname,
377 username, cfg, port);
382 * Load a set of hosts from a configuration file.
384 * @param filename file with the host specification
385 * @param cfg the configuration to use as a template while starting a controller
386 * on any of the loaded hosts. Operation queue sizes specific to a host
387 * are also read from this configuration handle
388 * @param hosts set to the hosts found in the file; caller must free this if
389 * number of hosts returned is greater than 0
390 * @return number of hosts returned in 'hosts', 0 on error
393 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
394 const struct GNUNET_CONFIGURATION_Handle
396 struct GNUNET_TESTBED_Host ***hosts)
398 //struct GNUNET_TESTBED_Host **host_array;
399 struct GNUNET_TESTBED_Host *starting_host;
405 regmatch_t pmatch[6];
412 GNUNET_assert (NULL != filename);
413 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
415 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s not found\n"), filename);
419 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
423 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s has no data\n"), filename);
426 data = GNUNET_malloc (fs);
427 if (fs != GNUNET_DISK_fn_read (filename, data, fs))
430 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s cannot be read\n"),
436 starting_host = NULL;
438 /* refer RFC 952 and RFC 1123 for valid hostnames */
439 GNUNET_assert (0 == regcomp (&rex,
440 "^(([[:alnum:]]+)@)?" /* username */
441 "([[:alnum:]]+[-[:alnum:]_\\.]+)" /* hostname */
442 "(:([[:digit:]]{1,5}))?", /* port */
443 REG_EXTENDED | REG_ICASE));
444 while (offset < (fs - 1))
447 if (((data[offset] == '\n')) && (buf != &data[offset]))
455 if ((REG_NOMATCH == regexec (&rex, buf, 6, pmatch, 0))
456 || (-1 == pmatch[3].rm_so))
458 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
459 "Error reading line `%s' in hostfile\n", buf);
460 buf = &data[offset + 1];
463 if (-1 != pmatch[2].rm_so)
465 size = pmatch[2].rm_eo - pmatch[2].rm_so;
466 username = GNUNET_malloc (size + 1);
467 username[size] = '\0';
468 GNUNET_assert (NULL != strncpy (username, buf + pmatch[2].rm_so, size));
470 if (-1 != pmatch[5].rm_so)
472 (void) SSCANF (buf + pmatch[5].rm_so, "%5hd", &port);
474 size = pmatch[3].rm_eo - pmatch[3].rm_so;
475 hostname = GNUNET_malloc (size + 1);
476 hostname[size] = '\0';
477 GNUNET_assert (NULL != strncpy (hostname, buf + pmatch[3].rm_so, size));
478 LOG (GNUNET_ERROR_TYPE_DEBUG,
479 "Successfully read host %s, port %d and user %s from file\n",
480 (NULL == hostname) ? "NULL" : hostname,
482 (NULL == username) ? "NULL" : username);
483 /* We store hosts in a static list; hence we only require the starting
484 * host pointer in that list to access the newly created list of hosts */
485 if (NULL == starting_host)
486 starting_host = GNUNET_TESTBED_host_create (hostname, username, cfg,
489 (void) GNUNET_TESTBED_host_create (hostname, username, cfg, port);
491 GNUNET_free_non_null (username);
492 GNUNET_free (hostname);
493 buf = &data[offset + 1];
495 else if ((data[offset] == '\n') || (data[offset] == '\0'))
496 buf = &data[offset + 1];
500 if (NULL == starting_host)
502 *hosts = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * count);
503 memcpy (*hosts, &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)],
504 sizeof (struct GNUNET_TESTBED_Host *) * count);
510 * Resolves a hostname using getaddrinfo
512 * @param host the hostname
513 * @return the string representing the IPv4 address of the given host; NULL upon error
516 simple_resolve (const char *host)
518 struct addrinfo *res;
519 const struct sockaddr_in *in_addr;
521 struct addrinfo hint;
524 hint.ai_family = AF_INET; /* IPv4 */
525 hint.ai_socktype = 0;
526 hint.ai_protocol = 0;
529 hint.ai_canonname = NULL;
531 hint.ai_flags = AI_NUMERICSERV;
533 LOG_DEBUG ("Resolving [%s]\n", host);
534 if (0 != (rc = getaddrinfo (host, "22", &hint, &res)))
536 LOG_GAI (GNUNET_ERROR_TYPE_ERROR, "getaddrinfo", rc);
539 GNUNET_assert (NULL != res);
540 GNUNET_assert (NULL != res->ai_addr);
541 GNUNET_assert (sizeof (struct sockaddr_in) == res->ai_addrlen);
542 in_addr = (const struct sockaddr_in *) res->ai_addr;
543 hostip = inet_ntoa (in_addr->sin_addr);
544 GNUNET_assert (NULL != hostip);
546 LOG_DEBUG ("Resolved [%s] to [%s]\n", host, hostip);
552 cmpstringp(const void *p1, const void *p2)
554 /* The actual arguments to this function are "pointers to
555 pointers to char", but strcmp(3) arguments are "pointers
556 to char", hence the following cast plus dereference */
558 return strcmp(* (char * const *) p1, * (char * const *) p2);
563 * Loads the set of host allocated by the LoadLeveler Job Scheduler. This
564 * function is only available when compiled with support for LoadLeveler and is
565 * used for running on the SuperMUC
567 * @param cfg the configuration to use as a template while starting a controller
568 * on any of the loaded hosts. Operation queue sizes specific to a host
569 * are also read from this configuration handle
570 * @param hosts set to the hosts found in the file; caller must free this if
571 * number of hosts returned is greater than 0
572 * @return number of hosts returned in 'hosts', 0 on error
575 GNUNET_TESTBED_hosts_load_from_loadleveler (const struct
576 GNUNET_CONFIGURATION_Handle *cfg,
577 struct GNUNET_TESTBED_Host ***hosts)
580 LOG (GNUNET_ERROR_TYPE_ERROR,
581 _("The function %s is only available when compiled with (--with-ll)\n"),
585 const char *hostfile;
589 struct GNUNET_TESTBED_Host **host_list;
602 if (NULL == (hostfile = getenv ("MP_SAVEHOSTFILE")))
607 if (GNUNET_SYSERR == GNUNET_DISK_file_size (hostfile, &size, GNUNET_YES,
618 buf = GNUNET_malloc (size + 1);
619 rsize = GNUNET_DISK_fn_read (hostfile, buf, (size_t) size);
620 if ( (GNUNET_SYSERR == rsize) || ((ssize_t) size != rsize) )
632 while (offset < size)
637 if ('!' == buf[offset])
643 if ('\n' == buf[offset])
647 if ('!' == buf[offset])
652 if ( (' ' == buf[offset])
653 || ('\t' == buf[offset])
654 || ('\r' == buf[offset]) )
659 hostname = &buf[offset];
663 if (isspace (buf[offset]))
666 for (host = 0; host < nhosts; host++)
667 if (0 == strcmp (hostnames[host], hostname))
671 LOG_DEBUG ("Adding host [%s]\n", hostname);
672 hostname = GNUNET_strdup (hostname);
673 GNUNET_array_append (hostnames, nhosts, hostname);
676 LOG_DEBUG ("Not adding host [%s] as it is already included\n", hostname);
684 GNUNET_free_non_null (buf);
685 if (NULL == hostnames)
689 qsort (hostnames, nhosts, sizeof (hostnames[0]), cmpstringp);
690 host_list = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * nhosts);
691 for (host = 0; host < nhosts; host++)
692 host_list[host] = GNUNET_TESTBED_host_create (hostnames[host], NULL, cfg, 0);
696 for (host = 0; host < nhosts; host++)
697 GNUNET_free (hostnames[host]);
698 GNUNET_free(hostnames);
705 * Destroy a host handle. Must only be called once everything
706 * running on that host has been stopped.
708 * @param host handle to destroy
711 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
713 struct RegisteredController *rc;
716 GNUNET_assert (host->id < host_list_size);
717 GNUNET_assert (host_list[host->id] == host);
718 host_list[host->id] = NULL;
719 /* clear registered controllers list */
720 for (rc = host->rc_head; NULL != rc; rc = host->rc_head)
722 GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
725 GNUNET_free_non_null ((char *) host->username);
726 GNUNET_free_non_null ((char *) host->hostname);
727 GNUNET_TESTBED_operation_queue_destroy_
728 (host->opq_parallel_overlay_connect_operations);
729 GNUNET_CONFIGURATION_destroy (host->cfg);
731 while (host_list_size >= HOST_LIST_GROW_STEP)
733 for (id = host_list_size - 1; id > host_list_size - HOST_LIST_GROW_STEP;
735 if (NULL != host_list[id])
737 if (id != host_list_size - HOST_LIST_GROW_STEP)
739 if (NULL != host_list[id])
741 host_list_size -= HOST_LIST_GROW_STEP;
744 GNUNET_realloc (host_list,
745 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size);
750 * Marks a host as registered with a controller
752 * @param host the host to mark
753 * @param controller the controller at which this host is registered
756 GNUNET_TESTBED_mark_host_registered_at_ (struct GNUNET_TESTBED_Host *host,
757 const struct GNUNET_TESTBED_Controller
760 struct RegisteredController *rc;
762 for (rc = host->rc_head; NULL != rc; rc = rc->next)
764 if (controller == rc->controller) /* already registered at controller */
770 rc = GNUNET_malloc (sizeof (struct RegisteredController));
771 rc->controller = controller;
772 GNUNET_CONTAINER_DLL_insert_tail (host->rc_head, host->rc_tail, rc);
777 * Unmarks a host registered at a controller
779 * @param host the host to unmark
780 * @param controller the controller at which this host has to be unmarked
783 GNUNET_TESTBED_deregister_host_at_ (struct GNUNET_TESTBED_Host *host,
784 const struct GNUNET_TESTBED_Controller
787 struct RegisteredController *rc;
789 for (rc = host->rc_head; NULL != rc; rc=rc->next)
790 if (controller == rc->controller)
797 GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
803 * Checks whether a host has been registered
805 * @param host the host to check
806 * @param controller the controller at which host's registration is checked
807 * @return GNUNET_YES if registered; GNUNET_NO if not
810 GNUNET_TESTBED_is_host_registered_ (const struct GNUNET_TESTBED_Host *host,
811 const struct GNUNET_TESTBED_Controller
814 struct RegisteredController *rc;
816 for (rc = host->rc_head; NULL != rc; rc = rc->next)
818 if (controller == rc->controller) /* already registered at controller */
828 * Handle for controller process
830 struct GNUNET_TESTBED_ControllerProc
835 struct GNUNET_HELPER_Handle *helper;
838 * The arguments used to start the helper
843 * The host where the helper is run
845 struct GNUNET_TESTBED_Host *host;
848 * The controller error callback
850 GNUNET_TESTBED_ControllerStatusCallback cb;
853 * The closure for the above callback
858 * The send handle for the helper
860 struct GNUNET_HELPER_SendHandle *shandle;
863 * The message corresponding to send handle
865 struct GNUNET_MessageHeader *msg;
871 * Function to copy NULL terminated list of arguments
873 * @param argv the NULL terminated list of arguments. Cannot be NULL.
874 * @return the copied NULL terminated arguments
877 copy_argv (const char *const *argv)
882 GNUNET_assert (NULL != argv);
883 for (argp = 0; NULL != argv[argp]; argp++) ;
884 argv_dup = GNUNET_malloc (sizeof (char *) * (argp + 1));
885 for (argp = 0; NULL != argv[argp]; argp++)
886 argv_dup[argp] = strdup (argv[argp]);
892 * Function to join NULL terminated list of arguments
894 * @param argv1 the NULL terminated list of arguments. Cannot be NULL.
895 * @param argv2 the NULL terminated list of arguments. Cannot be NULL.
896 * @return the joined NULL terminated arguments
899 join_argv (const char *const *argv1, const char *const *argv2)
908 for (cnt = 0; NULL != argv1[cnt]; cnt++)
910 argv = GNUNET_strdup (argv1[cnt]);
911 GNUNET_array_append (argvj, carg, argv);
913 for (cnt = 0; NULL != argv2[cnt]; cnt++)
915 argv = GNUNET_strdup (argv2[cnt]);
916 GNUNET_array_append (argvj, carg, argv);
918 GNUNET_array_append (argvj, carg, NULL);
924 * Frees the given NULL terminated arguments
926 * @param argv the NULL terminated list of arguments
929 free_argv (char **argv)
933 for (argp = 0; NULL != argv[argp]; argp++)
934 GNUNET_free (argv[argp]);
940 * Generates arguments for opening a remote shell. Builds up the arguments
941 * from the environment variable GNUNET_TESTBED_RSH_CMD. The variable
942 * should not mention `-p' (port) option and destination address as these will
943 * be set locally in the function from its parameteres. If the environmental
944 * variable is not found then it defaults to `ssh -o BatchMode=yes -o
945 * NoHostAuthenticationForLocalhost=yes'
947 * @param port the destination port number
948 * @param hostname the hostname of the target host
949 * @param username the username to use while connecting to target host
950 * @return NULL terminated list of arguments
953 gen_rsh_args (const char *port, const char *hostname, const char *username)
955 static const char *default_ssh_args[] = {
960 "NoHostAuthenticationForLocalhost=yes",
973 if (NULL != (ssh_cmd = getenv ("GNUNET_TESTBED_RSH_CMD")))
975 ssh_cmd = GNUNET_strdup (ssh_cmd);
976 ssh_cmd_cp = ssh_cmd;
977 for (size = 0; NULL != (arg = strtok (ssh_cmd, " ")); ssh_cmd = NULL)
978 GNUNET_array_append (ssh_args, size, GNUNET_strdup (arg));
979 GNUNET_free (ssh_cmd_cp);
983 ssh_args = copy_argv (default_ssh_args);
984 size = (sizeof (default_ssh_args)) / (sizeof (const char *));
985 GNUNET_array_grow (ssh_args, size, size - 1);
987 for (cnt = 0; cnt < size; cnt++)
1009 if (NULL == new_arg)
1012 ssh_args[cnt] = GNUNET_strdup (new_arg);
1014 GNUNET_array_append (ssh_args, size, NULL);
1020 * Generates the arguments needed for executing the given binary in a remote
1021 * shell. Builds the arguments from the environmental variable
1022 * GNUNET_TETSBED_RSH_CMD_SUFFIX. If the environmental variable is not found,
1023 * only the given binary name will be present in the returned arguments
1025 * @param append_args the arguments to append after generating the suffix
1026 * arguments. Can be NULL; if not must be NULL terminated 'char *' array
1027 * @return NULL-terminated args
1030 gen_rsh_suffix_args (const char * const *append_args)
1034 char *rshell_cmd_cp;
1037 unsigned int append_cnt;
1041 if (NULL != (rshell_cmd = getenv ("GNUNET_TESTBED_RSH_CMD_SUFFIX")))
1043 rshell_cmd = GNUNET_strdup (rshell_cmd);
1044 rshell_cmd_cp = rshell_cmd;
1045 for (; NULL != (arg = strtok (rshell_cmd, " ")); rshell_cmd = NULL)
1046 GNUNET_array_append (rshell_args, cnt, GNUNET_strdup (arg));
1047 GNUNET_free (rshell_cmd_cp);
1049 if (NULL != append_args)
1051 for (append_cnt = 0; NULL != append_args[append_cnt]; append_cnt++)
1052 GNUNET_array_append (rshell_args, cnt, GNUNET_strdup (append_args[append_cnt]));
1054 GNUNET_array_append (rshell_args, cnt, NULL);
1060 * Functions with this signature are called whenever a
1061 * complete message is received by the tokenizer.
1063 * Do not call GNUNET_SERVER_mst_destroy in callback
1065 * @param cls closure
1066 * @param client identification of the client
1067 * @param message the actual message
1069 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
1072 helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
1074 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1075 const struct GNUNET_TESTBED_HelperReply *msg;
1076 const char *hostname;
1079 uLongf xconfig_size;
1081 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
1082 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) <
1083 ntohs (msg->header.size));
1084 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY ==
1085 ntohs (msg->header.type));
1086 config_size = (uLongf) ntohs (msg->config_size);
1088 (uLongf) (ntohs (msg->header.size) -
1089 sizeof (struct GNUNET_TESTBED_HelperReply));
1090 config = GNUNET_malloc (config_size);
1091 GNUNET_assert (Z_OK ==
1092 uncompress ((Bytef *) config, &config_size,
1093 (const Bytef *) &msg[1], xconfig_size));
1094 /* Replace the configuration template present in the host with the
1095 controller's running configuration */
1096 GNUNET_CONFIGURATION_destroy (cp->host->cfg);
1097 cp->host->cfg = GNUNET_CONFIGURATION_create ();
1098 GNUNET_assert (GNUNET_CONFIGURATION_deserialize
1099 (cp->host->cfg, config, config_size, GNUNET_NO));
1100 GNUNET_free (config);
1101 if (NULL == (hostname = GNUNET_TESTBED_host_get_hostname (cp->host)))
1102 hostname = "localhost";
1103 /* Change the hostname so that we can connect to it */
1104 GNUNET_CONFIGURATION_set_value_string (cp->host->cfg, "testbed", "hostname",
1106 cp->host->locked = GNUNET_NO;
1107 cp->host->controller_started = GNUNET_YES;
1108 cp->cb (cp->cls, cp->host->cfg, GNUNET_OK);
1114 * Continuation function from GNUNET_HELPER_send()
1116 * @param cls closure
1117 * @param result GNUNET_OK on success,
1118 * GNUNET_NO if helper process died
1119 * GNUNET_SYSERR during GNUNET_HELPER_stop
1122 clear_msg (void *cls, int result)
1124 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1126 GNUNET_assert (NULL != cp->shandle);
1128 GNUNET_free (cp->msg);
1133 * Callback that will be called when the helper process dies. This is not called
1134 * when the helper process is stoped using GNUNET_HELPER_stop()
1136 * @param cls the closure from GNUNET_HELPER_start()
1139 helper_exp_cb (void *cls)
1141 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1142 GNUNET_TESTBED_ControllerStatusCallback cb;
1148 GNUNET_TESTBED_controller_stop (cp);
1150 cb (cb_cls, NULL, GNUNET_SYSERR);
1155 * Starts a controller process at the given host. The given host's configration
1156 * is used as a Template configuration to use for the remote controller; the
1157 * remote controller will be started with a slightly modified configuration
1158 * (port numbers, unix domain sockets and service home values are changed as per
1159 * TESTING library on the remote host). The modified configuration replaces the
1160 * host's existing configuration before signalling success through the
1161 * GNUNET_TESTBED_ControllerStatusCallback()
1163 * @param trusted_ip the ip address of the controller which will be set as TRUSTED
1164 * HOST(all connections form this ip are permitted by the testbed) when
1165 * starting testbed controller at host. This can either be a single ip
1166 * address or a network address in CIDR notation.
1167 * @param host the host where the controller has to be started. CANNOT be NULL.
1168 * @param cb function called when the controller is successfully started or
1169 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
1170 * called if cb is called with GNUNET_SYSERR as status. Will never be
1171 * called in the same task as 'GNUNET_TESTBED_controller_start'
1172 * (synchronous errors will be signalled by returning NULL). This
1173 * parameter cannot be NULL.
1174 * @param cls closure for above callbacks
1175 * @return the controller process handle, NULL on errors
1177 struct GNUNET_TESTBED_ControllerProc *
1178 GNUNET_TESTBED_controller_start (const char *trusted_ip,
1179 struct GNUNET_TESTBED_Host *host,
1180 GNUNET_TESTBED_ControllerStatusCallback cb,
1183 struct GNUNET_TESTBED_ControllerProc *cp;
1184 struct GNUNET_TESTBED_HelperInit *msg;
1185 const struct GNUNET_CONFIGURATION_Handle *cfg;
1186 const char *hostname;
1187 static char *const binary_argv[] = {
1188 HELPER_TESTBED_BINARY, NULL
1191 GNUNET_assert (NULL != host);
1192 GNUNET_assert (NULL != (cfg = GNUNET_TESTBED_host_get_cfg_ (host)));
1194 API_VIOLATION (GNUNET_NO == host->locked,
1195 "Host is already locked by a previous call to GNUNET_TESTBED_controller_start()");
1196 host->locked = GNUNET_YES;
1197 API_VIOLATION (GNUNET_NO == host->controller_started,
1198 "Attempting to start a controller on a host which is already started a controller");
1199 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
1200 if (0 == GNUNET_TESTBED_host_get_id_ (host))
1203 GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv,
1204 &helper_mst, &helper_exp_cb, cp);
1208 char *helper_binary_path_args[2];
1210 char **rsh_suffix_args;
1211 const char *username;
1214 username = host->username;
1215 hostname = host->hostname;
1216 GNUNET_asprintf (&port, "%u", host->port);
1217 LOG_DEBUG ("Starting remote connection to destination %s\n", hostname);
1219 GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
1220 "HELPER_BINARY_PATH",
1221 &helper_binary_path_args[0]))
1222 helper_binary_path_args[0] =
1223 GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
1224 helper_binary_path_args[1] = NULL;
1225 rsh_args = gen_rsh_args (port, hostname, username);
1226 rsh_suffix_args = gen_rsh_suffix_args ((const char **) helper_binary_path_args);
1228 join_argv ((const char **) rsh_args, (const char **) rsh_suffix_args);
1229 free_argv (rsh_args);
1230 free_argv (rsh_suffix_args);
1233 GNUNET_HELPER_start (GNUNET_NO, cp->helper_argv[0], cp->helper_argv, &helper_mst,
1234 &helper_exp_cb, cp);
1235 GNUNET_free (helper_binary_path_args[0]);
1237 if (NULL == cp->helper)
1239 if (NULL != cp->helper_argv)
1240 free_argv (cp->helper_argv);
1247 msg = GNUNET_TESTBED_create_helper_init_msg_ (trusted_ip, hostname, cfg);
1248 cp->msg = &msg->header;
1250 GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO, &clear_msg, cp);
1251 if (NULL == cp->shandle)
1254 GNUNET_TESTBED_controller_stop (cp);
1262 * Sends termination signal to the controller's helper process
1264 * @param cproc the handle to the controller's helper process
1267 GNUNET_TESTBED_controller_kill_ (struct GNUNET_TESTBED_ControllerProc *cproc)
1269 if (NULL != cproc->shandle)
1270 GNUNET_HELPER_send_cancel (cproc->shandle);
1271 if (NULL != cproc->helper)
1272 GNUNET_HELPER_kill (cproc->helper, GNUNET_YES);
1277 * Cleans-up the controller's helper process handle
1279 * @param cproc the handle to the controller's helper process
1282 GNUNET_TESTBED_controller_destroy_ (struct GNUNET_TESTBED_ControllerProc *cproc)
1284 if (NULL != cproc->helper)
1286 GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (cproc->helper));
1287 GNUNET_HELPER_destroy (cproc->helper);
1289 if (NULL != cproc->helper_argv)
1290 free_argv (cproc->helper_argv);
1291 cproc->host->controller_started = GNUNET_NO;
1292 cproc->host->locked = GNUNET_NO;
1293 GNUNET_free (cproc);
1298 * Stop the controller process (also will terminate all peers and controllers
1299 * dependent on this controller). This function blocks until the testbed has
1300 * been fully terminated (!). The controller status cb from
1301 * GNUNET_TESTBED_controller_start() will not be called.
1303 * @param cproc the controller process handle
1306 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
1308 GNUNET_TESTBED_controller_kill_ (cproc);
1309 GNUNET_TESTBED_controller_destroy_ (cproc);
1314 * The handle for whether a host is habitable or not
1316 struct GNUNET_TESTBED_HostHabitableCheckHandle
1321 const struct GNUNET_TESTBED_Host *host;
1324 * The callback to call once we have the status
1326 GNUNET_TESTBED_HostHabitableCallback cb;
1329 * The callback closure
1334 * The process handle for the SSH process
1336 struct GNUNET_OS_Process *auxp;
1339 * The arguments used to start the helper
1344 * Task id for the habitability check task
1346 GNUNET_SCHEDULER_TaskIdentifier habitability_check_task;
1349 * How long we wait before checking the process status. Should grow
1352 struct GNUNET_TIME_Relative wait_time;
1358 * Task for checking whether a host is habitable or not
1360 * @param cls GNUNET_TESTBED_HostHabitableCheckHandle
1361 * @param tc the scheduler task context
1364 habitability_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1366 struct GNUNET_TESTBED_HostHabitableCheckHandle *h = cls;
1368 GNUNET_TESTBED_HostHabitableCallback cb;
1369 const struct GNUNET_TESTBED_Host *host;
1371 enum GNUNET_OS_ProcessStatusType type;
1374 h->habitability_check_task = GNUNET_SCHEDULER_NO_TASK;
1375 ret = GNUNET_OS_process_status (h->auxp, &type, &code);
1376 if (GNUNET_SYSERR == ret)
1382 if (GNUNET_NO == ret)
1384 h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
1385 h->habitability_check_task =
1386 GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
1389 GNUNET_OS_process_destroy (h->auxp);
1391 ret = (0 != code) ? GNUNET_NO : GNUNET_YES;
1394 if (NULL != h->auxp)
1395 GNUNET_OS_process_destroy (h->auxp);
1399 free_argv (h->helper_argv);
1402 cb (cb_cls, host, ret);
1407 * Checks whether a host can be used to start testbed service
1409 * @param host the host to check
1410 * @param config the configuration handle to lookup the path of the testbed
1412 * @param cb the callback to call to inform about habitability of the given host
1413 * @param cb_cls the closure for the callback
1414 * @return NULL upon any error or a handle which can be passed to
1415 * GNUNET_TESTBED_is_host_habitable_cancel()
1417 struct GNUNET_TESTBED_HostHabitableCheckHandle *
1418 GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
1419 const struct GNUNET_CONFIGURATION_Handle
1421 GNUNET_TESTBED_HostHabitableCallback cb,
1424 struct GNUNET_TESTBED_HostHabitableCheckHandle *h;
1426 char **rsh_suffix_args;
1428 const char *hostname;
1431 h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostHabitableCheckHandle));
1435 hostname = (NULL == host->hostname) ? "127.0.0.1" : host->hostname;
1437 GNUNET_CONFIGURATION_get_value_string (config, "testbed",
1438 "HELPER_BINARY_PATH",
1441 GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
1442 GNUNET_asprintf (&port, "%u", host->port);
1443 rsh_args = gen_rsh_args (port, hostname, host->username);
1446 stat_args[0] = "stat";
1447 stat_args[2] = NULL;
1448 rsh_suffix_args = gen_rsh_suffix_args ((const char **) stat_args);
1449 GNUNET_free (stat_args[1]);
1450 h->helper_argv = join_argv ((const char **) rsh_args,
1451 (const char **) rsh_suffix_args);
1452 free_argv (rsh_suffix_args);
1453 free_argv (rsh_args);
1455 GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, NULL,
1456 NULL, h->helper_argv[0], h->helper_argv);
1457 if (NULL == h->auxp)
1459 GNUNET_break (0); /* Cannot exec SSH? */
1463 h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
1464 h->habitability_check_task =
1465 GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
1471 * Function to cancel a request started using GNUNET_TESTBED_is_host_habitable()
1473 * @param handle the habitability check handle
1476 GNUNET_TESTBED_is_host_habitable_cancel (struct
1477 GNUNET_TESTBED_HostHabitableCheckHandle
1480 GNUNET_SCHEDULER_cancel (handle->habitability_check_task);
1481 (void) GNUNET_OS_process_kill (handle->auxp, SIGTERM);
1482 (void) GNUNET_OS_process_wait (handle->auxp);
1483 GNUNET_OS_process_destroy (handle->auxp);
1484 free_argv (handle->helper_argv);
1485 GNUNET_free (handle);
1490 * handle for host registration
1492 struct GNUNET_TESTBED_HostRegistrationHandle
1495 * The host being registered
1497 struct GNUNET_TESTBED_Host *host;
1500 * The controller at which this host is being registered
1502 struct GNUNET_TESTBED_Controller *c;
1505 * The Registartion completion callback
1507 GNUNET_TESTBED_HostRegistrationCompletion cc;
1510 * The closure for above callback
1517 * Register a host with the controller
1519 * @param controller the controller handle
1520 * @param host the host to register
1521 * @param cc the completion callback to call to inform the status of
1522 * registration. After calling this callback the registration handle
1523 * will be invalid. Cannot be NULL.
1524 * @param cc_cls the closure for the cc
1525 * @return handle to the host registration which can be used to cancel the
1528 struct GNUNET_TESTBED_HostRegistrationHandle *
1529 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1530 struct GNUNET_TESTBED_Host *host,
1531 GNUNET_TESTBED_HostRegistrationCompletion cc,
1534 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1535 struct GNUNET_TESTBED_AddHostMessage *msg;
1536 const char *username;
1537 const char *hostname;
1544 uint16_t username_length;
1545 uint16_t hostname_length;
1547 if (NULL != controller->rh)
1549 hostname = GNUNET_TESTBED_host_get_hostname (host);
1550 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1552 LOG (GNUNET_ERROR_TYPE_WARNING, "Host hostname: %s already registered\n",
1553 (NULL == hostname) ? "localhost" : hostname);
1556 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1559 GNUNET_assert (NULL != cc);
1561 rh->cc_cls = cc_cls;
1562 controller->rh = rh;
1563 username = GNUNET_TESTBED_host_get_username_ (host);
1564 username_length = 0;
1565 if (NULL != username)
1566 username_length = strlen (username);
1567 GNUNET_assert (NULL != hostname); /* Hostname must be present */
1568 hostname_length = strlen (hostname);
1569 GNUNET_assert (NULL != host->cfg);
1570 config = GNUNET_CONFIGURATION_serialize (host->cfg, &config_size);
1571 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1572 GNUNET_free (config);
1573 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1574 msg_size += username_length;
1575 msg_size += hostname_length;
1576 msg_size += cc_size;
1577 msg = GNUNET_malloc (msg_size);
1578 msg->header.size = htons (msg_size);
1579 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST);
1580 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1581 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1583 if (NULL != username)
1585 msg->username_length = htons (username_length);
1586 ptr = memcpy (ptr, username, username_length);
1587 ptr += username_length;
1589 msg->hostname_length = htons (hostname_length);
1590 ptr = memcpy (ptr, hostname, hostname_length);
1591 ptr += hostname_length;
1592 msg->config_size = htons (config_size);
1593 ptr = memcpy (ptr, cconfig, cc_size);
1595 GNUNET_assert ((ptr - (void *) msg) == msg_size);
1596 GNUNET_free (cconfig);
1597 GNUNET_TESTBED_queue_message_ (controller,
1598 (struct GNUNET_MessageHeader *) msg);
1604 * Cancel the pending registration. Note that if the registration message is
1605 * already sent to the service the cancellation has only the effect that the
1606 * registration completion callback for the registration is never called.
1608 * @param handle the registration handle to cancel
1611 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1614 if (handle != handle->c->rh)
1619 handle->c->rh = NULL;
1620 GNUNET_free (handle);
1625 * Queues the given operation in the queue for parallel overlay connects of the
1628 * @param h the host handle
1629 * @param op the operation to queue in the given host's parally overlay connect
1633 GNUNET_TESTBED_host_queue_oc_ (struct GNUNET_TESTBED_Host *h,
1634 struct GNUNET_TESTBED_Operation *op)
1636 GNUNET_TESTBED_operation_queue_insert_
1637 (h->opq_parallel_overlay_connect_operations, op);
1642 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
1643 * controller (testbed service)
1645 * @param c the controller handler
1646 * @param msg message received
1647 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
1651 GNUNET_TESTBED_host_handle_addhostconfirm_ (struct GNUNET_TESTBED_Controller *c,
1653 GNUNET_TESTBED_HostConfirmedMessage
1656 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1665 if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
1667 LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
1668 GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
1672 msg_size = ntohs (msg->header.size);
1673 if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
1675 LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
1676 GNUNET_TESTBED_mark_host_registered_at_ (rh->host, c);
1677 rh->cc (rh->cc_cls, NULL);
1681 /* We have an error message */
1682 emsg = (char *) &msg[1];
1684 emsg[msg_size - sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
1690 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
1691 ntohl (msg->host_id), emsg);
1692 rh->cc (rh->cc_cls, emsg);
1699 * Resolves the hostname of the host to an ip address
1701 * @param host the host whose hostname is to be resolved
1704 GNUNET_TESTBED_host_resolve_ (struct GNUNET_TESTBED_Host *host)
1708 hostname = (char *) host->hostname;
1709 host->hostname = simple_resolve (hostname);
1710 if (NULL == host->hostname)
1713 host->hostname = hostname;
1716 GNUNET_free (hostname);
1717 host->hostname = GNUNET_strdup (host->hostname);
1720 /* end of testbed_api_hosts.c */