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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file testbed/testbed_api_hosts.c
21 * @brief API for manipulating 'hosts' controlled by the GNUnet testing service;
22 * allows parsing hosts files, starting, stopping and communicating (via
23 * SSH/stdin/stdout) with the remote (or local) processes
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_core_service.h"
30 #include "gnunet_transport_service.h"
32 #include "testbed_api.h"
33 #include "testbed_api_hosts.h"
34 #include "testbed_helper.h"
35 #include "testbed_api_operations.h"
41 * Generic logging shorthand
43 #define LOG(kind, ...) \
44 GNUNET_log_from (kind, "testbed-api-hosts", __VA_ARGS__);
47 * Debug logging shorthand
49 #define LOG_DEBUG(...) \
50 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
53 * Prints API violation message
55 #define API_VIOLATION(cond,errstr) \
59 LOG (GNUNET_ERROR_TYPE_ERROR, "API violation detected: %s\n", errstr); \
64 * Log an error message at log-level 'level' that indicates a failure of the
65 * command 'cmd' with the message given by gai_strerror(rc).
67 #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)
70 * Number of extra elements we create space for when we grow host list
72 #define HOST_LIST_GROW_STEP 10
76 * A list entry for registered controllers list
78 struct RegisteredController
81 * The controller at which this host is registered
83 const struct GNUNET_TESTBED_Controller *controller;
86 * The next ptr for DLL
88 struct RegisteredController *next;
91 * The prev ptr for DLL
93 struct RegisteredController *prev;
98 * Opaque handle to a host running experiments managed by the testing framework.
99 * The master process must be able to SSH to this host without password (via
102 struct GNUNET_TESTBED_Host
106 * The hostname of the host; NULL for localhost
108 const char *hostname;
111 * The username to be used for SSH login
113 const char *username;
116 * the configuration to use as a template while starting a controller on this
117 * host. Operation queue size specific to a host are also read from this
118 * configuration handle. After starting the controller, it points to the actual
119 * configuration with which the controller is running
121 struct GNUNET_CONFIGURATION_Handle *cfg;
124 * The head for the list of controllers where this host is registered
126 struct RegisteredController *rc_head;
129 * The tail for the list of controllers where this host is registered
131 struct RegisteredController *rc_tail;
134 * Operation queue for simultaneous overlay connect operations target at this
137 struct OperationQueue *opq_parallel_overlay_connect_operations;
140 * Is a controller started on this host? FIXME: Is this needed?
142 int controller_started;
145 * Is this host locked by GNUNET_TESTBED_controller_start()?
150 * Global ID we use to refer to a host on the network
155 * The port which is to be used for SSH
163 * Array of available hosts
165 static struct GNUNET_TESTBED_Host **host_list;
168 * The size of the available hosts list
170 static unsigned int host_list_size;
174 * Lookup a host by ID.
176 * @param id global host ID assigned to the host; 0 is
177 * reserved to always mean 'localhost'
178 * @return handle to the host, NULL if host not found
180 struct GNUNET_TESTBED_Host *
181 GNUNET_TESTBED_host_lookup_by_id_ (uint32_t id)
183 if (host_list_size <= id)
185 return host_list[id];
190 * Create a host by ID; given this host handle, we could not
191 * run peers at the host, but we can talk about the host
194 * @param id global host ID assigned to the host; 0 is
195 * reserved to always mean 'localhost'
196 * @param cfg the configuration to use as a template while starting a controller
197 * on this host. Operation queue sizes specific to a host are also
198 * read from this configuration handle
199 * @return handle to the host, NULL on error
201 struct GNUNET_TESTBED_Host *
202 GNUNET_TESTBED_host_create_by_id_ (uint32_t id,
203 const struct GNUNET_CONFIGURATION_Handle *cfg)
205 return GNUNET_TESTBED_host_create_with_id (id, NULL, NULL, cfg, 0);
210 * Obtain the host's unique global ID.
212 * @param host handle to the host, NULL means 'localhost'
213 * @return id global host ID assigned to the host (0 is
214 * 'localhost', but then obviously not globally unique)
217 GNUNET_TESTBED_host_get_id_ (const struct GNUNET_TESTBED_Host * host)
224 * Obtain the host's hostname.
226 * @param host handle to the host, NULL means 'localhost'
227 * @return hostname of the host
230 GNUNET_TESTBED_host_get_hostname (const struct GNUNET_TESTBED_Host *host)
232 return host->hostname;
237 * Obtain the host's username
239 * @param host handle to the host, NULL means 'localhost'
240 * @return username to login to the host
243 GNUNET_TESTBED_host_get_username_ (const struct GNUNET_TESTBED_Host *host)
245 return host->username;
250 * Obtain the host's ssh port
252 * @param host handle to the host, NULL means 'localhost'
253 * @return username to login to the host
256 GNUNET_TESTBED_host_get_ssh_port_ (const struct GNUNET_TESTBED_Host * host)
263 * Check whether a controller is already started on the given host
265 * @param host the handle to the host
266 * @return GNUNET_YES if the controller is already started; GNUNET_NO if not
269 GNUNET_TESTBED_host_controller_started (const struct GNUNET_TESTBED_Host *host)
271 return host->controller_started;
276 * Obtain the host's configuration template
278 * @param host handle to the host
279 * @return the host's configuration template
281 const struct GNUNET_CONFIGURATION_Handle *
282 GNUNET_TESTBED_host_get_cfg_ (const struct GNUNET_TESTBED_Host *host)
289 * Function to replace host's configuration
291 * @param host the host handle
292 * @param new_cfg the new configuration to replace the old one
295 GNUNET_TESTBED_host_replace_cfg_ (struct GNUNET_TESTBED_Host *host,
296 const struct GNUNET_CONFIGURATION_Handle *new_cfg)
298 GNUNET_CONFIGURATION_destroy (host->cfg);
299 host->cfg = GNUNET_CONFIGURATION_dup (new_cfg);
304 * Create a host to run peers and controllers on.
306 * @param id global host ID assigned to the host; 0 is
307 * reserved to always mean 'localhost'
308 * @param hostname name of the host, use "NULL" for localhost
309 * @param username username to use for the login; may be NULL
310 * @param cfg the configuration to use as a template while starting a controller
311 * on this host. Operation queue sizes specific to a host are also
312 * read from this configuration handle
313 * @param port port number to use for ssh; use 0 to let ssh decide
314 * @return handle to the host, NULL on error
316 struct GNUNET_TESTBED_Host *
317 GNUNET_TESTBED_host_create_with_id (uint32_t id, const char *hostname,
318 const char *username,
319 const struct GNUNET_CONFIGURATION_Handle
323 struct GNUNET_TESTBED_Host *host;
324 unsigned int new_size;
326 if ((id < host_list_size) && (NULL != host_list[id]))
328 LOG (GNUNET_ERROR_TYPE_WARNING, "Host with id: %u already created\n", id);
331 host = GNUNET_new (struct GNUNET_TESTBED_Host);
332 host->hostname = (NULL != hostname) ? GNUNET_strdup (hostname) : NULL;
333 host->username = (NULL != username) ? GNUNET_strdup (username) : NULL;
335 host->port = (0 == port) ? 22 : port;
336 host->cfg = GNUNET_CONFIGURATION_dup (cfg);
337 host->opq_parallel_overlay_connect_operations =
338 GNUNET_TESTBED_operation_queue_create_ (OPERATION_QUEUE_TYPE_ADAPTIVE,
340 new_size = host_list_size;
341 while (id >= new_size)
342 new_size += HOST_LIST_GROW_STEP;
343 if (new_size != host_list_size)
344 GNUNET_array_grow (host_list, host_list_size, new_size);
345 GNUNET_assert (id < host_list_size);
346 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding host with id: %u\n", host->id);
347 host_list[id] = host;
353 * Create a host to run peers and controllers on.
355 * @param hostname name of the host, use "NULL" for localhost
356 * @param username username to use for the login; may be NULL
357 * @param cfg the configuration to use as a template while starting a controller
358 * on this host. Operation queue sizes specific to a host are also
359 * read from this configuration handle
360 * @param port port number to use for ssh; use 0 to let ssh decide
361 * @return handle to the host, NULL on error
363 struct GNUNET_TESTBED_Host *
364 GNUNET_TESTBED_host_create (const char *hostname, const char *username,
365 const struct GNUNET_CONFIGURATION_Handle *cfg,
368 static uint32_t uid_generator;
370 if (NULL == hostname)
371 return GNUNET_TESTBED_host_create_with_id (0, hostname, username,
373 return GNUNET_TESTBED_host_create_with_id (++uid_generator, hostname,
374 username, cfg, port);
379 * Load a set of hosts from a configuration file.
381 * @param filename file with the host specification
382 * @param cfg the configuration to use as a template while starting a controller
383 * on any of the loaded hosts. Operation queue sizes specific to a host
384 * are also read from this configuration handle
385 * @param hosts set to the hosts found in the file; caller must free this if
386 * number of hosts returned is greater than 0
387 * @return number of hosts returned in 'hosts', 0 on error
390 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
391 const struct GNUNET_CONFIGURATION_Handle
393 struct GNUNET_TESTBED_Host ***hosts)
395 struct GNUNET_TESTBED_Host *starting_host;
401 regmatch_t pmatch[6];
408 GNUNET_assert (NULL != filename);
409 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
411 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s not found\n"), filename);
415 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
419 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s has no data\n"), filename);
422 data = GNUNET_malloc (fs);
423 if (fs != GNUNET_DISK_fn_read (filename, data, fs))
426 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s cannot be read\n"),
432 starting_host = NULL;
434 /* refer RFC 952 and RFC 1123 for valid hostnames */
435 GNUNET_assert (0 == regcomp (&rex,
436 "^(([[:alnum:]]+)@)?" /* username */
437 "([[:alnum:]]+[-[:alnum:]_\\.]+)" /* hostname */
438 "(:([[:digit:]]{1,5}))?", /* port */
439 REG_EXTENDED | REG_ICASE));
440 while (offset < (fs - 1))
443 if (((data[offset] == '\n')) && (buf != &data[offset]))
451 if ((REG_NOMATCH == regexec (&rex, buf, 6, pmatch, 0))
452 || (-1 == pmatch[3].rm_so))
454 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
455 "Error reading line `%s' in hostfile\n", buf);
456 buf = &data[offset + 1];
459 if (-1 != pmatch[2].rm_so)
461 size = pmatch[2].rm_eo - pmatch[2].rm_so;
462 username = GNUNET_malloc (size + 1);
463 username[size] = '\0';
464 GNUNET_assert (NULL != strncpy (username, buf + pmatch[2].rm_so, size));
466 if (-1 != pmatch[5].rm_so)
468 (void) SSCANF (buf + pmatch[5].rm_so, "%5hd", &port);
470 size = pmatch[3].rm_eo - pmatch[3].rm_so;
471 hostname = GNUNET_malloc (size + 1);
472 hostname[size] = '\0';
473 GNUNET_assert (NULL != strncpy (hostname, buf + pmatch[3].rm_so, size));
474 LOG (GNUNET_ERROR_TYPE_DEBUG,
475 "Successfully read host %s, port %d and user %s from file\n",
476 (NULL == hostname) ? "NULL" : hostname,
478 (NULL == username) ? "NULL" : username);
479 /* We store hosts in a static list; hence we only require the starting
480 * host pointer in that list to access the newly created list of hosts */
481 if (NULL == starting_host)
482 starting_host = GNUNET_TESTBED_host_create (hostname, username, cfg,
485 (void) GNUNET_TESTBED_host_create (hostname, username, cfg, port);
487 GNUNET_free_non_null (username);
488 GNUNET_free (hostname);
489 buf = &data[offset + 1];
491 else if ((data[offset] == '\n') || (data[offset] == '\0'))
492 buf = &data[offset + 1];
496 if (NULL == starting_host)
498 *hosts = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * count);
499 GNUNET_memcpy (*hosts,
500 &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)],
501 sizeof (struct GNUNET_TESTBED_Host *) * count);
507 * Resolves a hostname using getaddrinfo
509 * @param host the hostname
510 * @return the string representing the IPv4 address of the given host; NULL upon error
513 simple_resolve (const char *host)
515 struct addrinfo *res;
516 const struct sockaddr_in *in_addr;
518 struct addrinfo hint;
521 hint.ai_family = AF_INET; /* IPv4 */
522 hint.ai_socktype = 0;
523 hint.ai_protocol = 0;
526 hint.ai_canonname = NULL;
528 hint.ai_flags = AI_NUMERICSERV;
530 LOG_DEBUG ("Resolving [%s]\n", host);
531 if (0 != (rc = getaddrinfo (host, "22", &hint, &res)))
533 LOG_GAI (GNUNET_ERROR_TYPE_ERROR, "getaddrinfo", rc);
536 GNUNET_assert (NULL != res);
537 GNUNET_assert (NULL != res->ai_addr);
538 GNUNET_assert (sizeof (struct sockaddr_in) == res->ai_addrlen);
539 in_addr = (const struct sockaddr_in *) res->ai_addr;
540 hostip = inet_ntoa (in_addr->sin_addr);
541 GNUNET_assert (NULL != hostip);
543 LOG_DEBUG ("Resolved [%s] to [%s]\n", host, hostip);
549 * Loads the set of host allocated by the LoadLeveler Job Scheduler. This
550 * function is only available when compiled with support for LoadLeveler and is
551 * used for running on the SuperMUC
553 * @param cfg the configuration to use as a template while starting a controller
554 * on any of the loaded hosts. Operation queue sizes specific to a host
555 * are also read from this configuration handle
556 * @param hosts set to the hosts found in the file; caller must free this if
557 * number of hosts returned is greater than 0
558 * @return number of hosts returned in 'hosts', 0 on error
561 GNUNET_TESTBED_hosts_load_from_loadleveler (const struct
562 GNUNET_CONFIGURATION_Handle *cfg,
563 struct GNUNET_TESTBED_Host ***hosts)
566 LOG (GNUNET_ERROR_TYPE_ERROR,
567 _("The function %s is only available when compiled with (--with-ll)\n"),
571 const char *hostfile;
573 if (NULL == (hostfile = getenv ("MP_SAVEHOSTFILE")))
578 return GNUNET_TESTBED_hosts_load_from_file (hostfile, cfg, hosts);
584 * Destroy a host handle. Must only be called once everything
585 * running on that host has been stopped.
587 * @param host handle to destroy
590 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
592 struct RegisteredController *rc;
595 GNUNET_assert (host->id < host_list_size);
596 GNUNET_assert (host_list[host->id] == host);
597 host_list[host->id] = NULL;
598 /* clear registered controllers list */
599 for (rc = host->rc_head; NULL != rc; rc = host->rc_head)
601 GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
604 GNUNET_free_non_null ((char *) host->username);
605 GNUNET_free_non_null ((char *) host->hostname);
606 GNUNET_TESTBED_operation_queue_destroy_
607 (host->opq_parallel_overlay_connect_operations);
608 GNUNET_CONFIGURATION_destroy (host->cfg);
610 while (host_list_size >= HOST_LIST_GROW_STEP)
612 for (id = host_list_size - 1; id > host_list_size - HOST_LIST_GROW_STEP;
614 if (NULL != host_list[id])
616 if (id != host_list_size - HOST_LIST_GROW_STEP)
618 if (NULL != host_list[id])
620 host_list_size -= HOST_LIST_GROW_STEP;
623 GNUNET_realloc (host_list,
624 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size);
629 * Marks a host as registered with a controller
631 * @param host the host to mark
632 * @param controller the controller at which this host is registered
635 GNUNET_TESTBED_mark_host_registered_at_ (struct GNUNET_TESTBED_Host *host,
636 const struct GNUNET_TESTBED_Controller
639 struct RegisteredController *rc;
641 for (rc = host->rc_head; NULL != rc; rc = rc->next)
643 if (controller == rc->controller) /* already registered at controller */
649 rc = GNUNET_new (struct RegisteredController);
650 rc->controller = controller;
651 GNUNET_CONTAINER_DLL_insert_tail (host->rc_head, host->rc_tail, rc);
656 * Unmarks a host registered at a controller
658 * @param host the host to unmark
659 * @param controller the controller at which this host has to be unmarked
662 GNUNET_TESTBED_deregister_host_at_ (struct GNUNET_TESTBED_Host *host,
663 const struct GNUNET_TESTBED_Controller
666 struct RegisteredController *rc;
668 for (rc = host->rc_head; NULL != rc; rc=rc->next)
669 if (controller == rc->controller)
676 GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
682 * Checks whether a host has been registered
684 * @param host the host to check
685 * @param controller the controller at which host's registration is checked
686 * @return GNUNET_YES if registered; GNUNET_NO if not
689 GNUNET_TESTBED_is_host_registered_ (const struct GNUNET_TESTBED_Host *host,
690 const struct GNUNET_TESTBED_Controller
693 struct RegisteredController *rc;
695 for (rc = host->rc_head; NULL != rc; rc = rc->next)
697 if (controller == rc->controller) /* already registered at controller */
707 * Handle for controller process
709 struct GNUNET_TESTBED_ControllerProc
714 struct GNUNET_HELPER_Handle *helper;
717 * The arguments used to start the helper
722 * The host where the helper is run
724 struct GNUNET_TESTBED_Host *host;
727 * The controller error callback
729 GNUNET_TESTBED_ControllerStatusCallback cb;
732 * The closure for the above callback
737 * The send handle for the helper
739 struct GNUNET_HELPER_SendHandle *shandle;
742 * The message corresponding to send handle
744 struct GNUNET_MessageHeader *msg;
750 * Function to copy NULL terminated list of arguments
752 * @param argv the NULL terminated list of arguments. Cannot be NULL.
753 * @return the copied NULL terminated arguments
756 copy_argv (const char *const *argv)
761 GNUNET_assert (NULL != argv);
762 for (argp = 0; NULL != argv[argp]; argp++) ;
763 argv_dup = GNUNET_malloc (sizeof (char *) * (argp + 1));
764 for (argp = 0; NULL != argv[argp]; argp++)
765 argv_dup[argp] = GNUNET_strdup (argv[argp]);
771 * Function to join NULL terminated list of arguments
773 * @param argv1 the NULL terminated list of arguments. Cannot be NULL.
774 * @param argv2 the NULL terminated list of arguments. Cannot be NULL.
775 * @return the joined NULL terminated arguments
778 join_argv (const char *const *argv1, const char *const *argv2)
787 for (cnt = 0; NULL != argv1[cnt]; cnt++)
789 argv = GNUNET_strdup (argv1[cnt]);
790 GNUNET_array_append (argvj, carg, argv);
792 for (cnt = 0; NULL != argv2[cnt]; cnt++)
794 argv = GNUNET_strdup (argv2[cnt]);
795 GNUNET_array_append (argvj, carg, argv);
797 GNUNET_array_append (argvj, carg, NULL);
803 * Frees the given NULL terminated arguments
805 * @param argv the NULL terminated list of arguments
808 free_argv (char **argv)
812 for (argp = 0; NULL != argv[argp]; argp++)
813 GNUNET_free (argv[argp]);
819 * Generates arguments for opening a remote shell. Builds up the arguments
820 * from the environment variable GNUNET_TESTBED_RSH_CMD. The variable
821 * should not mention `-p' (port) option and destination address as these will
822 * be set locally in the function from its parameteres. If the environmental
823 * variable is not found then it defaults to `ssh -o BatchMode=yes -o
824 * NoHostAuthenticationForLocalhost=yes -o StrictHostkeyChecking=no -o
825 * PasswordAuthentication=noc'
827 * @param port the destination port number
828 * @param hostname the hostname of the target host
829 * @param username the username to use while connecting to target host
830 * @return NULL terminated list of arguments
833 gen_rsh_args (const char *port, const char *hostname, const char *username)
835 static const char *default_ssh_args[] = {
840 "NoHostAuthenticationForLocalhost=yes",
842 "StrictHostKeyChecking=no",
844 "PasswordAuthentication=no",
857 if (NULL != (ssh_cmd = getenv ("GNUNET_TESTBED_RSH_CMD")))
859 ssh_cmd = GNUNET_strdup (ssh_cmd);
860 ssh_cmd_cp = ssh_cmd;
861 for (size = 0; NULL != (arg = strtok (ssh_cmd, " ")); ssh_cmd = NULL)
862 GNUNET_array_append (ssh_args, size, GNUNET_strdup (arg));
863 GNUNET_free (ssh_cmd_cp);
867 ssh_args = copy_argv (default_ssh_args);
868 size = (sizeof (default_ssh_args)) / (sizeof (const char *));
869 GNUNET_array_grow (ssh_args, size, size - 1);
871 for (cnt = 0; cnt < size; cnt++)
896 ssh_args[cnt] = GNUNET_strdup (new_arg);
898 GNUNET_array_append (ssh_args, size, NULL);
904 * Generates the arguments needed for executing the given binary in a remote
905 * shell. Builds the arguments from the environmental variable
906 * GNUNET_TETSBED_RSH_CMD_SUFFIX. If the environmental variable is not found,
907 * only the given binary name will be present in the returned arguments
909 * @param append_args the arguments to append after generating the suffix
910 * arguments. Can be NULL; if not must be NULL terminated 'char *' array
911 * @return NULL-terminated args
914 gen_rsh_suffix_args (const char * const *append_args)
921 unsigned int append_cnt;
925 if (NULL != (rshell_cmd = getenv ("GNUNET_TESTBED_RSH_CMD_SUFFIX")))
927 rshell_cmd = GNUNET_strdup (rshell_cmd);
928 rshell_cmd_cp = rshell_cmd;
929 for (; NULL != (arg = strtok (rshell_cmd, " ")); rshell_cmd = NULL)
930 GNUNET_array_append (rshell_args, cnt, GNUNET_strdup (arg));
931 GNUNET_free (rshell_cmd_cp);
933 if (NULL != append_args)
935 for (append_cnt = 0; NULL != append_args[append_cnt]; append_cnt++)
936 GNUNET_array_append (rshell_args, cnt, GNUNET_strdup (append_args[append_cnt]));
938 GNUNET_array_append (rshell_args, cnt, NULL);
944 * Functions with this signature are called whenever a
945 * complete message is received by the tokenizer.
947 * Do not call GNUNET_SERVER_mst_destroy in callback
950 * @param client identification of the client
951 * @param message the actual message
953 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
956 helper_mst (void *cls,
957 const struct GNUNET_MessageHeader *message)
959 struct GNUNET_TESTBED_ControllerProc *cp = cls;
960 const struct GNUNET_TESTBED_HelperReply *msg;
961 const char *hostname;
966 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
967 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) <
968 ntohs (msg->header.size));
969 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY ==
970 ntohs (msg->header.type));
971 config_size = (uLongf) ntohs (msg->config_size);
973 (uLongf) (ntohs (msg->header.size) -
974 sizeof (struct GNUNET_TESTBED_HelperReply));
975 config = GNUNET_malloc (config_size);
976 GNUNET_assert (Z_OK ==
977 uncompress ((Bytef *) config, &config_size,
978 (const Bytef *) &msg[1], xconfig_size));
979 /* Replace the configuration template present in the host with the
980 controller's running configuration */
981 GNUNET_CONFIGURATION_destroy (cp->host->cfg);
982 cp->host->cfg = GNUNET_CONFIGURATION_create ();
983 GNUNET_assert (GNUNET_CONFIGURATION_deserialize
988 GNUNET_free (config);
989 if (NULL == (hostname = GNUNET_TESTBED_host_get_hostname (cp->host)))
990 hostname = "localhost";
991 /* Change the hostname so that we can connect to it */
992 GNUNET_CONFIGURATION_set_value_string (cp->host->cfg, "testbed", "hostname",
994 cp->host->locked = GNUNET_NO;
995 cp->host->controller_started = GNUNET_YES;
996 cp->cb (cp->cls, cp->host->cfg, GNUNET_OK);
1002 * Continuation function from GNUNET_HELPER_send()
1004 * @param cls closure
1005 * @param result GNUNET_OK on success,
1006 * GNUNET_NO if helper process died
1007 * GNUNET_SYSERR during GNUNET_HELPER_stop
1010 clear_msg (void *cls, int result)
1012 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1014 GNUNET_assert (NULL != cp->shandle);
1016 GNUNET_free (cp->msg);
1022 * Callback that will be called when the helper process dies. This is not called
1023 * when the helper process is stoped using GNUNET_HELPER_stop()
1025 * @param cls the closure from GNUNET_HELPER_start()
1028 helper_exp_cb (void *cls)
1030 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1031 GNUNET_TESTBED_ControllerStatusCallback cb;
1037 GNUNET_TESTBED_controller_stop (cp);
1039 cb (cb_cls, NULL, GNUNET_SYSERR);
1044 * Starts a controller process at the given host. The given host's configuration
1045 * is used as a Template configuration to use for the remote controller; the
1046 * remote controller will be started with a slightly modified configuration
1047 * (port numbers, unix domain sockets and service home values are changed as per
1048 * TESTING library on the remote host). The modified configuration replaces the
1049 * host's existing configuration before signalling success through the
1050 * GNUNET_TESTBED_ControllerStatusCallback()
1052 * @param trusted_ip the ip address of the controller which will be set as TRUSTED
1053 * HOST(all connections form this ip are permitted by the testbed) when
1054 * starting testbed controller at host. This can either be a single ip
1055 * address or a network address in CIDR notation.
1056 * @param host the host where the controller has to be started. CANNOT be NULL.
1057 * @param cb function called when the controller is successfully started or
1058 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
1059 * called if cb is called with GNUNET_SYSERR as status. Will never be
1060 * called in the same task as 'GNUNET_TESTBED_controller_start'
1061 * (synchronous errors will be signalled by returning NULL). This
1062 * parameter cannot be NULL.
1063 * @param cls closure for above callbacks
1064 * @return the controller process handle, NULL on errors
1066 struct GNUNET_TESTBED_ControllerProc *
1067 GNUNET_TESTBED_controller_start (const char *trusted_ip,
1068 struct GNUNET_TESTBED_Host *host,
1069 GNUNET_TESTBED_ControllerStatusCallback cb,
1072 struct GNUNET_TESTBED_ControllerProc *cp;
1073 struct GNUNET_TESTBED_HelperInit *msg;
1074 const struct GNUNET_CONFIGURATION_Handle *cfg;
1075 const char *hostname;
1076 static char *const binary_argv[] = {
1077 HELPER_TESTBED_BINARY, NULL
1080 GNUNET_assert (NULL != host);
1081 GNUNET_assert (NULL != (cfg = GNUNET_TESTBED_host_get_cfg_ (host)));
1083 API_VIOLATION (GNUNET_NO == host->locked,
1084 "Host is already locked by a previous call to GNUNET_TESTBED_controller_start()");
1085 host->locked = GNUNET_YES;
1086 API_VIOLATION (GNUNET_NO == host->controller_started,
1087 "Attempting to start a controller on a host which is already started a controller");
1088 cp = GNUNET_new (struct GNUNET_TESTBED_ControllerProc);
1089 if (0 == GNUNET_TESTBED_host_get_id_ (host))
1092 GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv,
1093 &helper_mst, &helper_exp_cb, cp);
1097 char *helper_binary_path_args[2];
1099 char **rsh_suffix_args;
1100 const char *username;
1106 username = host->username;
1107 hostname = host->hostname;
1108 GNUNET_asprintf (&port, "%u", host->port);
1109 LOG_DEBUG ("Starting remote connection to destination %s\n", hostname);
1111 GNUNET_CONFIGURATION_get_value_filename (cfg, "testbed",
1112 "HELPER_BINARY_PATH",
1113 &helper_binary_path_args[0]))
1114 helper_binary_path_args[0] =
1115 GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
1116 helper_binary_path_args[1] = NULL;
1117 rsh_args = gen_rsh_args (port, hostname, username);
1118 rsh_suffix_args = gen_rsh_suffix_args ((const char **) helper_binary_path_args);
1120 join_argv ((const char **) rsh_args, (const char **) rsh_suffix_args);
1121 free_argv (rsh_args);
1122 free_argv (rsh_suffix_args);
1124 argstr = GNUNET_strdup ("");
1125 for (cnt = 0; NULL != cp->helper_argv[cnt]; cnt++)
1128 GNUNET_assert (0 < GNUNET_asprintf (&argstr, "%s %s", aux, cp->helper_argv[cnt]));
1131 LOG_DEBUG ("Helper cmd str: %s\n", argstr);
1132 GNUNET_free (argstr);
1134 GNUNET_HELPER_start (GNUNET_NO, cp->helper_argv[0], cp->helper_argv, &helper_mst,
1135 &helper_exp_cb, cp);
1136 GNUNET_free (helper_binary_path_args[0]);
1138 if (NULL == cp->helper)
1140 if (NULL != cp->helper_argv)
1141 free_argv (cp->helper_argv);
1148 msg = GNUNET_TESTBED_create_helper_init_msg_ (trusted_ip, hostname, cfg);
1149 cp->msg = &msg->header;
1151 GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO, &clear_msg, cp);
1152 if (NULL == cp->shandle)
1155 GNUNET_TESTBED_controller_stop (cp);
1163 * Sends termination signal to the controller's helper process
1165 * @param cproc the handle to the controller's helper process
1168 GNUNET_TESTBED_controller_kill_ (struct GNUNET_TESTBED_ControllerProc *cproc)
1170 if (NULL != cproc->shandle)
1171 GNUNET_HELPER_send_cancel (cproc->shandle);
1172 if (NULL != cproc->helper)
1173 GNUNET_HELPER_kill (cproc->helper, GNUNET_YES);
1178 * Cleans-up the controller's helper process handle
1180 * @param cproc the handle to the controller's helper process
1183 GNUNET_TESTBED_controller_destroy_ (struct GNUNET_TESTBED_ControllerProc *cproc)
1185 if (NULL != cproc->helper)
1187 GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (cproc->helper));
1188 GNUNET_HELPER_destroy (cproc->helper);
1190 if (NULL != cproc->helper_argv)
1191 free_argv (cproc->helper_argv);
1192 cproc->host->controller_started = GNUNET_NO;
1193 cproc->host->locked = GNUNET_NO;
1194 GNUNET_free_non_null (cproc->msg);
1195 GNUNET_free (cproc);
1200 * Stop the controller process (also will terminate all peers and controllers
1201 * dependent on this controller). This function blocks until the testbed has
1202 * been fully terminated (!). The controller status cb from
1203 * GNUNET_TESTBED_controller_start() will not be called.
1205 * @param cproc the controller process handle
1208 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
1210 GNUNET_TESTBED_controller_kill_ (cproc);
1211 GNUNET_TESTBED_controller_destroy_ (cproc);
1216 * The handle for whether a host is habitable or not
1218 struct GNUNET_TESTBED_HostHabitableCheckHandle
1223 const struct GNUNET_TESTBED_Host *host;
1226 * The callback to call once we have the status
1228 GNUNET_TESTBED_HostHabitableCallback cb;
1231 * The callback closure
1236 * The process handle for the SSH process
1238 struct GNUNET_OS_Process *auxp;
1241 * The arguments used to start the helper
1246 * Task id for the habitability check task
1248 struct GNUNET_SCHEDULER_Task * habitability_check_task;
1251 * How long we wait before checking the process status. Should grow
1254 struct GNUNET_TIME_Relative wait_time;
1260 * Task for checking whether a host is habitable or not
1262 * @param cls GNUNET_TESTBED_HostHabitableCheckHandle
1265 habitability_check (void *cls)
1267 struct GNUNET_TESTBED_HostHabitableCheckHandle *h = cls;
1269 GNUNET_TESTBED_HostHabitableCallback cb;
1270 const struct GNUNET_TESTBED_Host *host;
1272 enum GNUNET_OS_ProcessStatusType type;
1275 h->habitability_check_task = NULL;
1276 ret = GNUNET_OS_process_status (h->auxp, &type, &code);
1277 if (GNUNET_SYSERR == ret)
1283 if (GNUNET_NO == ret)
1285 h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
1286 h->habitability_check_task =
1287 GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
1290 GNUNET_OS_process_destroy (h->auxp);
1292 ret = (0 != code) ? GNUNET_NO : GNUNET_YES;
1295 if (NULL != h->auxp)
1296 GNUNET_OS_process_destroy (h->auxp);
1300 free_argv (h->helper_argv);
1303 cb (cb_cls, host, ret);
1308 * Checks whether a host can be used to start testbed service
1310 * @param host the host to check
1311 * @param config the configuration handle to lookup the path of the testbed
1313 * @param cb the callback to call to inform about habitability of the given host
1314 * @param cb_cls the closure for the callback
1315 * @return NULL upon any error or a handle which can be passed to
1316 * GNUNET_TESTBED_is_host_habitable_cancel()
1318 struct GNUNET_TESTBED_HostHabitableCheckHandle *
1319 GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
1320 const struct GNUNET_CONFIGURATION_Handle
1322 GNUNET_TESTBED_HostHabitableCallback cb,
1325 struct GNUNET_TESTBED_HostHabitableCheckHandle *h;
1327 char **rsh_suffix_args;
1329 const char *hostname;
1332 h = GNUNET_new (struct GNUNET_TESTBED_HostHabitableCheckHandle);
1336 hostname = (NULL == host->hostname) ? "127.0.0.1" : host->hostname;
1338 GNUNET_CONFIGURATION_get_value_filename (config, "testbed",
1339 "HELPER_BINARY_PATH",
1342 GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
1343 GNUNET_asprintf (&port, "%u", host->port);
1344 rsh_args = gen_rsh_args (port, hostname, host->username);
1347 stat_args[0] = "stat";
1348 stat_args[2] = NULL;
1349 rsh_suffix_args = gen_rsh_suffix_args ((const char **) stat_args);
1350 GNUNET_free (stat_args[1]);
1351 h->helper_argv = join_argv ((const char **) rsh_args,
1352 (const char **) rsh_suffix_args);
1353 free_argv (rsh_suffix_args);
1354 free_argv (rsh_args);
1356 GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, NULL,
1357 NULL, NULL, h->helper_argv[0], h->helper_argv);
1358 if (NULL == h->auxp)
1360 GNUNET_break (0); /* Cannot exec SSH? */
1364 h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
1365 h->habitability_check_task =
1366 GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
1372 * Function to cancel a request started using GNUNET_TESTBED_is_host_habitable()
1374 * @param handle the habitability check handle
1377 GNUNET_TESTBED_is_host_habitable_cancel (struct
1378 GNUNET_TESTBED_HostHabitableCheckHandle
1381 GNUNET_SCHEDULER_cancel (handle->habitability_check_task);
1382 (void) GNUNET_OS_process_kill (handle->auxp, GNUNET_TERM_SIG);
1383 (void) GNUNET_OS_process_wait (handle->auxp);
1384 GNUNET_OS_process_destroy (handle->auxp);
1385 free_argv (handle->helper_argv);
1386 GNUNET_free (handle);
1391 * Register a host with the controller
1393 * @param controller the controller handle
1394 * @param host the host to register
1395 * @param cc the completion callback to call to inform the status of
1396 * registration. After calling this callback the registration handle
1397 * will be invalid. Cannot be NULL.
1398 * @param cc_cls the closure for the cc
1399 * @return handle to the host registration which can be used to cancel the
1402 struct GNUNET_TESTBED_HostRegistrationHandle *
1403 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1404 struct GNUNET_TESTBED_Host *host,
1405 GNUNET_TESTBED_HostRegistrationCompletion cc,
1408 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1409 struct GNUNET_TESTBED_AddHostMessage *msg;
1410 const char *username;
1411 const char *hostname;
1418 uint16_t username_length;
1419 uint16_t hostname_length;
1421 if (NULL != controller->rh)
1423 hostname = GNUNET_TESTBED_host_get_hostname (host);
1424 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1426 LOG (GNUNET_ERROR_TYPE_WARNING, "Host hostname: %s already registered\n",
1427 (NULL == hostname) ? "localhost" : hostname);
1430 rh = GNUNET_new (struct GNUNET_TESTBED_HostRegistrationHandle);
1433 GNUNET_assert (NULL != cc);
1435 rh->cc_cls = cc_cls;
1436 controller->rh = rh;
1437 username = GNUNET_TESTBED_host_get_username_ (host);
1438 username_length = 0;
1439 if (NULL != username)
1440 username_length = strlen (username);
1441 GNUNET_assert (NULL != hostname); /* Hostname must be present */
1442 hostname_length = strlen (hostname);
1443 GNUNET_assert (NULL != host->cfg);
1444 config = GNUNET_CONFIGURATION_serialize (host->cfg, &config_size);
1445 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1446 GNUNET_free (config);
1447 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1448 msg_size += username_length;
1449 msg_size += hostname_length;
1450 msg_size += cc_size;
1451 msg = GNUNET_malloc (msg_size);
1452 msg->header.size = htons (msg_size);
1453 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST);
1454 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1455 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1457 if (NULL != username)
1459 msg->username_length = htons (username_length);
1460 GNUNET_memcpy (ptr, username, username_length);
1461 ptr += username_length;
1463 msg->hostname_length = htons (hostname_length);
1464 GNUNET_memcpy (ptr, hostname, hostname_length);
1465 ptr += hostname_length;
1466 msg->config_size = htons (config_size);
1467 GNUNET_memcpy (ptr, cconfig, cc_size);
1469 GNUNET_assert ((ptr - (void *) msg) == msg_size);
1470 GNUNET_free (cconfig);
1471 GNUNET_TESTBED_queue_message_ (controller,
1472 (struct GNUNET_MessageHeader *) msg);
1478 * Cancel the pending registration. Note that if the registration message is
1479 * already sent to the service the cancellation has only the effect that the
1480 * registration completion callback for the registration is never called.
1482 * @param handle the registration handle to cancel
1485 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1488 if (handle != handle->c->rh)
1493 handle->c->rh = NULL;
1494 GNUNET_free (handle);
1499 * Queues the given operation in the queue for parallel overlay connects of the
1502 * @param h the host handle
1503 * @param op the operation to queue in the given host's parally overlay connect
1507 GNUNET_TESTBED_host_queue_oc_ (struct GNUNET_TESTBED_Host *h,
1508 struct GNUNET_TESTBED_Operation *op)
1510 GNUNET_TESTBED_operation_queue_insert_
1511 (h->opq_parallel_overlay_connect_operations, op);
1516 * Resolves the hostname of the host to an ip address
1518 * @param host the host whose hostname is to be resolved
1521 GNUNET_TESTBED_host_resolve_ (struct GNUNET_TESTBED_Host *host)
1525 hostname = (char *) host->hostname;
1526 host->hostname = simple_resolve (hostname);
1527 if (NULL == host->hostname)
1530 host->hostname = hostname;
1533 GNUNET_free (hostname);
1534 host->hostname = GNUNET_strdup (host->hostname);
1537 /* end of testbed_api_hosts.c */