2 This file is part of GNUnet
3 (C) 2008--2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/testbed_api_hosts.c
23 * @brief API for manipulating 'hosts' controlled by the GNUnet testing service;
24 * allows parsing hosts files, starting, stopping and communicating (via
25 * SSH/stdin/stdout) with the remote (or local) processes
26 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31 #include "gnunet_core_service.h"
32 #include "gnunet_transport_service.h"
34 #include "testbed_api.h"
35 #include "testbed_api_hosts.h"
38 * Generic logging shorthand
40 #define LOG(kind, ...) \
41 GNUNET_log_from (kind, "testbed-api-hosts", __VA_ARGS__);
44 * Number of extra elements we create space for when we grow host list
46 #define HOST_LIST_GROW_STEP 10
50 * A list entry for registered controllers list
52 struct RegisteredController
55 * The controller at which this host is registered
57 const struct GNUNET_TESTBED_Controller *controller;
60 * The next ptr for DLL
62 struct RegisteredController *next;
65 * The prev ptr for DLL
67 struct RegisteredController *prev;
72 * Opaque handle to a host running experiments managed by the testing framework.
73 * The master process must be able to SSH to this host without password (via
76 struct GNUNET_TESTBED_Host
80 * The next pointer for DLL
82 struct GNUNET_TESTBED_Host *next;
85 * The prev pointer for DLL
87 struct GNUNET_TESTBED_Host *prev;
90 * The hostname of the host; NULL for localhost
95 * The username to be used for SSH login
100 * The head for the list of controllers where this host is registered
102 struct RegisteredController *rc_head;
105 * The tail for the list of controllers where this host is registered
107 struct RegisteredController *rc_tail;
110 * Global ID we use to refer to a host on the network
115 * The port which is to be used for SSH
123 * Array of available hosts
125 static struct GNUNET_TESTBED_Host **host_list;
128 * The size of the available hosts list
130 static uint32_t host_list_size;
134 * Lookup a host by ID.
136 * @param id global host ID assigned to the host; 0 is
137 * reserved to always mean 'localhost'
138 * @return handle to the host, NULL if host not found
140 struct GNUNET_TESTBED_Host *
141 GNUNET_TESTBED_host_lookup_by_id_ (uint32_t id)
143 if (host_list_size <= id)
145 return host_list[id];
150 * Create a host by ID; given this host handle, we could not
151 * run peers at the host, but we can talk about the host
154 * @param id global host ID assigned to the host; 0 is
155 * reserved to always mean 'localhost'
156 * @return handle to the host, NULL on error
158 struct GNUNET_TESTBED_Host *
159 GNUNET_TESTBED_host_create_by_id_ (uint32_t id)
161 return GNUNET_TESTBED_host_create_with_id (id, NULL, NULL, 0);
166 * Obtain the host's unique global ID.
168 * @param host handle to the host, NULL means 'localhost'
169 * @return id global host ID assigned to the host (0 is
170 * 'localhost', but then obviously not globally unique)
173 GNUNET_TESTBED_host_get_id_ (const struct GNUNET_TESTBED_Host * host)
180 * Obtain the host's hostname.
182 * @param host handle to the host, NULL means 'localhost'
183 * @return hostname of the host
186 GNUNET_TESTBED_host_get_hostname (const struct GNUNET_TESTBED_Host *host)
188 return host->hostname;
193 * Obtain the host's username
195 * @param host handle to the host, NULL means 'localhost'
196 * @return username to login to the host
199 GNUNET_TESTBED_host_get_username_ (const struct GNUNET_TESTBED_Host *host)
201 return host->username;
206 * Obtain the host's ssh port
208 * @param host handle to the host, NULL means 'localhost'
209 * @return username to login to the host
212 GNUNET_TESTBED_host_get_ssh_port_ (const struct GNUNET_TESTBED_Host * host)
219 * Create a host to run peers and controllers on.
221 * @param id global host ID assigned to the host; 0 is
222 * reserved to always mean 'localhost'
223 * @param hostname name of the host, use "NULL" for localhost
224 * @param username username to use for the login; may be NULL
225 * @param port port number to use for ssh; use 0 to let ssh decide
226 * @return handle to the host, NULL on error
228 struct GNUNET_TESTBED_Host *
229 GNUNET_TESTBED_host_create_with_id (uint32_t id, const char *hostname,
230 const char *username, uint16_t port)
232 struct GNUNET_TESTBED_Host *host;
235 if ((id < host_list_size) && (NULL != host_list[id]))
237 LOG (GNUNET_ERROR_TYPE_WARNING, "Host with id: %u already created\n", id);
240 host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
241 host->hostname = (NULL != hostname) ? GNUNET_strdup (hostname) : NULL;
242 host->username = (NULL != username) ? GNUNET_strdup (username) : NULL;
244 host->port = (0 == port) ? 22 : port;
245 new_size = host_list_size;
246 while (id >= new_size)
247 new_size += HOST_LIST_GROW_STEP;
248 if (new_size != host_list_size)
251 GNUNET_realloc (host_list,
252 sizeof (struct GNUNET_TESTBED_Host *) * new_size);
253 (void) memset (&host_list[host_list_size], 0,
254 sizeof (struct GNUNET_TESTBED_Host *) * (new_size -
256 host_list_size = new_size;
258 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding host with id: %u\n", host->id);
259 host_list[id] = host;
265 * Create a host to run peers and controllers on.
267 * @param hostname name of the host, use "NULL" for localhost
268 * @param username username to use for the login; may be NULL
269 * @param port port number to use for ssh; use 0 to let ssh decide
270 * @return handle to the host, NULL on error
272 struct GNUNET_TESTBED_Host *
273 GNUNET_TESTBED_host_create (const char *hostname, const char *username,
276 static uint32_t uid_generator;
278 if (NULL == hostname)
279 return GNUNET_TESTBED_host_create_with_id (0, hostname, username, port);
280 return GNUNET_TESTBED_host_create_with_id (++uid_generator, hostname,
286 * Load a set of hosts from a configuration file.
288 * @param filename file with the host specification
289 * @param hosts set to the hosts found in the file; caller must free this if
290 * number of hosts returned is greater than 0
291 * @return number of hosts returned in 'hosts', 0 on error
294 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
295 struct GNUNET_TESTBED_Host ***hosts)
297 //struct GNUNET_TESTBED_Host **host_array;
298 struct GNUNET_TESTBED_Host *starting_host;
310 GNUNET_assert (NULL != filename);
311 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
313 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s not found\n"), filename);
317 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
321 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s has no data\n"), filename);
324 data = GNUNET_malloc (fs);
325 if (fs != GNUNET_DISK_fn_read (filename, data, fs))
328 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s cannot be read\n"),
334 starting_host = NULL;
336 while (offset < (fs - 1))
339 if (((data[offset] == '\n')) && (buf != &data[offset]))
342 ret = SSCANF (buf, "%255[a-zA-Z0-9_]@%255[a-zA-Z0-9.]:%5hd",
343 username, hostname, &port);
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347 "Successfully read host %s, port %d and user %s from file\n",
348 hostname, port, username);
349 /* We store hosts in a static list; hence we only require the starting
350 host pointer in that list to access the newly created list of hosts */
351 if (NULL == starting_host)
352 starting_host = GNUNET_TESTBED_host_create (hostname, username,
355 (void) GNUNET_TESTBED_host_create (hostname, username, port);
359 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
360 "Error reading line `%s' in hostfile\n", buf);
361 buf = &data[offset + 1];
363 else if ((data[offset] == '\n') || (data[offset] == '\0'))
364 buf = &data[offset + 1];
367 if (NULL == starting_host)
369 *hosts = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * count);
371 &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)],
372 sizeof (struct GNUNET_TESTBED_Host *) * count);
378 * Destroy a host handle. Must only be called once everything
379 * running on that host has been stopped.
381 * @param host handle to destroy
384 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
386 struct RegisteredController *rc;
389 GNUNET_assert (host->id < host_list_size);
390 GNUNET_assert (host_list[host->id] == host);
391 host_list[host->id] = NULL;
392 /* clear registered controllers list */
393 for (rc = host->rc_head; NULL != rc; rc = host->rc_head)
395 GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
398 GNUNET_free_non_null ((char *) host->username);
399 GNUNET_free_non_null ((char *) host->hostname);
401 while (host_list_size >= HOST_LIST_GROW_STEP)
403 for (id = host_list_size - 1; id > host_list_size - HOST_LIST_GROW_STEP;
405 if (NULL != host_list[id])
407 if (id != host_list_size - HOST_LIST_GROW_STEP)
409 if (NULL != host_list[id])
411 host_list_size -= HOST_LIST_GROW_STEP;
414 GNUNET_realloc (host_list,
415 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size);
420 * Marks a host as registered with a controller
422 * @param host the host to mark
423 * @param controller the controller at which this host is registered
426 GNUNET_TESTBED_mark_host_registered_at_ (struct GNUNET_TESTBED_Host *host,
427 const struct GNUNET_TESTBED_Controller
430 struct RegisteredController *rc;
432 for (rc = host->rc_head; NULL != rc; rc = rc->next)
434 if (controller == rc->controller) /* already registered at controller */
440 rc = GNUNET_malloc (sizeof (struct RegisteredController));
441 rc->controller = controller;
442 GNUNET_CONTAINER_DLL_insert_tail (host->rc_head, host->rc_tail, rc);
447 * Checks whether a host has been registered
449 * @param host the host to check
450 * @param controller the controller at which host's registration is checked
451 * @return GNUNET_YES if registered; GNUNET_NO if not
454 GNUNET_TESTBED_is_host_registered_ (const struct GNUNET_TESTBED_Host *host,
455 const struct GNUNET_TESTBED_Controller
458 struct RegisteredController *rc;
460 for (rc = host->rc_head; NULL != rc; rc = rc->next)
462 if (controller == rc->controller) /* already registered at controller */
472 * The handle for whether a host is habitable or not
474 struct GNUNET_TESTBED_HostHabitableCheckHandle
479 const struct GNUNET_TESTBED_Host *host;
482 /* * the configuration handle to lookup the path of the testbed helper */
484 /* const struct GNUNET_CONFIGURATION_Handle *cfg; */
487 * The callback to call once we have the status
489 GNUNET_TESTBED_HostHabitableCallback cb;
492 * The callback closure
497 * The process handle for the SSH process
499 struct GNUNET_OS_Process *auxp;
502 * The SSH destination address string
507 * The destination port string
512 * The path for hte testbed helper binary
514 char *helper_binary_path;
517 * Task id for the habitability check task
519 GNUNET_SCHEDULER_TaskIdentifier habitability_check_task;
522 * How long we wait before checking the process status. Should grow
525 struct GNUNET_TIME_Relative wait_time;
531 * Task for checking whether a host is habitable or not
533 * @param cls GNUNET_TESTBED_HostHabitableCheckHandle
534 * @param tc the scheduler task context
537 habitability_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
539 struct GNUNET_TESTBED_HostHabitableCheckHandle *h = cls;
541 GNUNET_TESTBED_HostHabitableCallback cb;
542 const struct GNUNET_TESTBED_Host *host;
544 enum GNUNET_OS_ProcessStatusType type;
547 h->habitability_check_task = GNUNET_SCHEDULER_NO_TASK;
548 ret = GNUNET_OS_process_status (h->auxp, &type, &code);
549 if (GNUNET_SYSERR == ret)
555 if (GNUNET_NO == ret)
557 h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
558 h->habitability_check_task =
559 GNUNET_SCHEDULER_add_delayed (h->wait_time,
560 &habitability_check, h);
563 GNUNET_OS_process_destroy (h->auxp);
565 ret = (0 != code) ? GNUNET_NO : GNUNET_YES;
568 GNUNET_free (h->ssh_addr);
569 GNUNET_free (h->portstr);
570 GNUNET_free (h->helper_binary_path);
572 GNUNET_OS_process_destroy (h->auxp);
578 cb (cb_cls, host, ret);
583 * Checks whether a host can be used to start testbed service
585 * @param host the host to check
586 * @param config the configuration handle to lookup the path of the testbed
588 * @param cb the callback to call to inform about habitability of the given host
589 * @param cb_cls the closure for the callback
590 * @return NULL upon any error or a handle which can be passed to
591 * GNUNET_TESTBED_is_host_habitable_cancel()
593 struct GNUNET_TESTBED_HostHabitableCheckHandle *
594 GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
595 const struct GNUNET_CONFIGURATION_Handle
597 GNUNET_TESTBED_HostHabitableCallback cb,
600 struct GNUNET_TESTBED_HostHabitableCheckHandle *h;
601 char *remote_args[11];
602 const char *hostname;
605 h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostHabitableCheckHandle));
609 hostname = (NULL == host->hostname) ? "127.0.0.1" : host->hostname;
610 if (NULL == host->username)
611 h->ssh_addr = GNUNET_strdup (hostname);
613 GNUNET_asprintf (&h->ssh_addr, "%s@%s", host->username, hostname);
614 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (config, "testbed",
615 "HELPER_BINARY_PATH",
616 &h->helper_binary_path))
617 h->helper_binary_path =
618 GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
620 remote_args[argp++] = "ssh";
621 GNUNET_asprintf (&h->portstr, "%u", host->port);
622 remote_args[argp++] = "-p";
623 remote_args[argp++] = h->portstr;
624 remote_args[argp++] = "-o";
625 remote_args[argp++] = "BatchMode=yes";
626 remote_args[argp++] = "-o";
627 remote_args[argp++] = "NoHostAuthenticationForLocalhost=yes";
628 remote_args[argp++] = h->ssh_addr;
629 remote_args[argp++] = "stat";
630 remote_args[argp++] = h->helper_binary_path;
631 remote_args[argp++] = NULL;
632 GNUNET_assert (argp == 11);
634 GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, NULL,
635 NULL, "ssh", remote_args);
638 GNUNET_break (0); /* Cannot exec SSH? */
639 GNUNET_free (h->ssh_addr);
640 GNUNET_free (h->portstr);
641 GNUNET_free (h->helper_binary_path);
645 h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
646 h->habitability_check_task =
647 GNUNET_SCHEDULER_add_delayed (h->wait_time,
648 &habitability_check, h);
654 * Function to cancel a request started using GNUNET_TESTBED_is_host_habitable()
656 * @param struct handle the habitability check handle
659 GNUNET_TESTBED_is_host_habitable_cancel (struct
660 GNUNET_TESTBED_HostHabitableCheckHandle
663 GNUNET_SCHEDULER_cancel (handle->habitability_check_task);
664 (void) GNUNET_OS_process_kill (handle->auxp, SIGTERM);
665 (void) GNUNET_OS_process_wait (handle->auxp);
666 GNUNET_OS_process_destroy (handle->auxp);
667 GNUNET_free (handle->ssh_addr);
668 GNUNET_free (handle->portstr);
669 GNUNET_free (handle->helper_binary_path);
670 GNUNET_free (handle);
672 /* end of testbed_api_hosts.c */