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_testbed_service.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_transport_service.h"
33 #include "gnunet_hello_lib.h"
34 #include "gnunet_container_lib.h"
39 * Opaque handle to a host running experiments managed by the testing framework.
40 * The master process must be able to SSH to this host without password (via
43 struct GNUNET_TESTBED_Host
47 * The next pointer for DLL
49 struct GNUNET_TESTBED_Host *next;
52 * The prev pointer for DLL
54 struct GNUNET_TESTBED_Host *prev;
57 * The hostname of the host; NULL for localhost
62 * The username to be used for SSH login
67 * The controller at which this host is registered
69 const struct GNUNET_TESTBED_Controller *controller;
72 * Global ID we use to refer to a host on the network
77 * The port which is to be used for SSH
85 * Head element in the list of available hosts
87 static struct GNUNET_TESTBED_Host *host_list_head;
90 * Tail element in the list of available hosts
92 static struct GNUNET_TESTBED_Host *host_list_tail;
96 * Lookup a host by ID.
98 * @param id global host ID assigned to the host; 0 is
99 * reserved to always mean 'localhost'
100 * @return handle to the host, NULL if host not found
102 struct GNUNET_TESTBED_Host *
103 GNUNET_TESTBED_host_lookup_by_id_ (uint32_t id)
105 struct GNUNET_TESTBED_Host *host;
107 for (host = host_list_head; NULL != host; host=host->next)
108 if (id == host->unique_id)
115 * Create a host by ID; given this host handle, we could not
116 * run peers at the host, but we can talk about the host
119 * @param id global host ID assigned to the host; 0 is
120 * reserved to always mean 'localhost'
121 * @return handle to the host, NULL on error
123 struct GNUNET_TESTBED_Host *
124 GNUNET_TESTBED_host_create_by_id_ (uint32_t id)
126 struct GNUNET_TESTBED_Host *host;
128 host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
129 host->unique_id = id;
135 * Obtain the host's unique global ID.
137 * @param host handle to the host, NULL means 'localhost'
138 * @return id global host ID assigned to the host (0 is
139 * 'localhost', but then obviously not globally unique)
142 GNUNET_TESTBED_host_get_id_ (const struct GNUNET_TESTBED_Host *host)
144 return host->unique_id;
149 * Obtain the host's hostname.
151 * @param host handle to the host, NULL means 'localhost'
152 * @return hostname of the host
155 GNUNET_TESTBED_host_get_hostname_ (const struct GNUNET_TESTBED_Host *host)
157 return host->hostname;
162 * Obtain the host's username
164 * @param host handle to the host, NULL means 'localhost'
165 * @return username to login to the host
168 GNUNET_TESTBED_host_get_username_ (const struct GNUNET_TESTBED_Host *host)
170 return host->username;
175 * Obtain the host's ssh port
177 * @param host handle to the host, NULL means 'localhost'
178 * @return username to login to the host
181 GNUNET_TESTBED_host_get_ssh_port_ (const struct GNUNET_TESTBED_Host *host)
188 * Create a host to run peers and controllers on.
190 * @param id global host ID assigned to the host; 0 is
191 * reserved to always mean 'localhost'
192 * @param hostname name of the host, use "NULL" for localhost
193 * @param username username to use for the login; may be NULL
194 * @param port port number to use for ssh; use 0 to let ssh decide
195 * @return handle to the host, NULL on error
197 struct GNUNET_TESTBED_Host *
198 GNUNET_TESTBED_host_create_with_id (uint32_t id,
199 const char *hostname,
200 const char *username,
203 struct GNUNET_TESTBED_Host *host;
205 host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
206 host->hostname = hostname;
207 host->username = username;
208 host->unique_id = id;
209 host->port = (0 == port) ? 22 : port;
210 GNUNET_CONTAINER_DLL_insert_tail (host_list_head, host_list_tail, host);
216 * Create a host to run peers and controllers on.
218 * @param hostname name of the host, use "NULL" for localhost
219 * @param username username to use for the login; may be NULL
220 * @param port port number to use for ssh; use 0 to let ssh decide
221 * @return handle to the host, NULL on error
223 struct GNUNET_TESTBED_Host *
224 GNUNET_TESTBED_host_create (const char *hostname,
225 const char *username,
228 static uint32_t uid_generator;
230 if (NULL == hostname)
231 return GNUNET_TESTBED_host_create_with_id (0, hostname, username, port);
232 return GNUNET_TESTBED_host_create_with_id (++uid_generator,
239 * Load a set of hosts from a configuration file.
241 * @param filename file with the host specification
242 * @param hosts set to the hosts found in the file
243 * @return number of hosts returned in 'hosts', 0 on error
246 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
247 struct GNUNET_TESTBED_Host **hosts)
249 // see testing_group.c, GNUNET_TESTING_hosts_load
256 * Destroy a host handle. Must only be called once everything
257 * running on that host has been stopped.
259 * @param host handle to destroy
262 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
264 GNUNET_CONTAINER_DLL_remove (host_list_head, host_list_tail, host);
270 * Wrapper around GNUNET_HELPER_Handle
272 struct GNUNET_TESTBED_HelperHandle
277 struct GNUNET_OS_Process *process;
280 * Pipe connecting to stdin of the process.
282 struct GNUNET_DISK_PipeHandle *cpipe;
285 * The port number for ssh; used for helpers starting ssh
290 * The ssh destination string; used for helpers starting ssh
297 * Run a given helper process at the given host. Communication
298 * with the helper will be via GNUnet messages on stdin/stdout.
299 * Runs the process via 'ssh' at the specified host, or locally.
300 * Essentially an SSH-wrapper around the 'gnunet_helper_lib.h' API.
302 * @param host host to use, use "NULL" for localhost
303 * @param binary_argv binary name and command-line arguments to give to the binary
304 * @return handle to terminate the command, NULL on error
306 struct GNUNET_TESTBED_HelperHandle *
307 GNUNET_TESTBED_host_run_ (const struct GNUNET_TESTBED_Host *host,
308 char *const binary_argv[])
310 struct GNUNET_TESTBED_HelperHandle *h;
314 while (NULL != binary_argv[argc])
316 h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HelperHandle));
317 h->cpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
318 if (0 == host->unique_id)
320 h->process = GNUNET_OS_start_process_vap (GNUNET_YES,
322 "gnunet-service-testbed",
327 char *remote_args[argc + 6 + 1];
330 GNUNET_asprintf (&h->port, "%d", host->port);
331 if (NULL == host->username)
332 GNUNET_asprintf (&h->dst, "%s", host->hostname);
334 GNUNET_asprintf (&h->dst, "%s@%s", host->hostname, host->username);
336 remote_args[argp++] = "ssh";
337 remote_args[argp++] = "-p";
338 remote_args[argp++] = h->port;
339 remote_args[argp++] = "-q";
340 remote_args[argp++] = h->dst;
341 remote_args[argp++] = "gnunet-service-testbed";
342 while (NULL != binary_argv[argp-6])
344 remote_args[argp] = binary_argv[argp - 6];
347 remote_args[argp++] = NULL;
348 GNUNET_assert (argp == argc + 6 + 1);
349 h->process = GNUNET_OS_start_process_vap (GNUNET_YES,
354 if (NULL == h->process)
356 GNUNET_break (GNUNET_OK == GNUNET_DISK_pipe_close (h->cpipe));
357 GNUNET_free_non_null (h->port);
358 GNUNET_free_non_null (h->dst);
362 GNUNET_break (GNUNET_OK == GNUNET_DISK_pipe_close_end (h->cpipe, GNUNET_DISK_PIPE_END_READ));
368 * Stops a helper in the HelperHandle using GNUNET_HELPER_stop
370 * @param handle the handle returned from GNUNET_TESTBED_host_start_
373 GNUNET_TESTBED_host_stop_ (struct GNUNET_TESTBED_HelperHandle *handle)
375 GNUNET_break (GNUNET_OK == GNUNET_DISK_pipe_close (handle->cpipe));
376 GNUNET_break (0 == GNUNET_OS_process_kill (handle->process, SIGTERM));
377 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (handle->process));
378 GNUNET_OS_process_destroy (handle->process);
379 GNUNET_free_non_null (handle->port);
380 GNUNET_free_non_null (handle->dst);
381 GNUNET_free (handle);
386 * Marks a host as registered with a controller
388 * @param host the host to mark
389 * @param controller the controller at which this host is registered
392 GNUNET_TESTBED_mark_host_as_registered_ (struct GNUNET_TESTBED_Host *host,
393 const struct GNUNET_TESTBED_Controller
396 host->controller = controller;
401 * Checks whether a host has been registered
403 * @param host the host to check
404 * @param controller the controller at which host's registration is checked
405 * @return GNUNET_YES if registered; GNUNET_NO if not
408 GNUNET_TESTBED_is_host_registered_ (const struct GNUNET_TESTBED_Host *host,
409 const struct GNUNET_TESTBED_Controller
412 return (controller == host->controller) ? GNUNET_YES : GNUNET_NO;
416 /* end of testbed_api_hosts.c */