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 * Global ID we use to refer to a host on the network
72 * The port which is to be used for SSH
79 * Head element in the list of available hosts
81 static struct GNUNET_TESTBED_Host *host_list_head;
84 * Tail element in the list of available hosts
86 static struct GNUNET_TESTBED_Host *host_list_tail;
90 * Lookup a host by ID.
92 * @param id global host ID assigned to the host; 0 is
93 * reserved to always mean 'localhost'
94 * @return handle to the host, NULL if host not found
96 struct GNUNET_TESTBED_Host *
97 GNUNET_TESTBED_host_lookup_by_id_ (uint32_t id)
99 struct GNUNET_TESTBED_Host *host;
101 for (host = host_list_head; NULL != host; host=host->next)
102 if (id == host->unique_id)
109 * Create a host by ID; given this host handle, we could not
110 * run peers at the host, but we can talk about the host
113 * @param id global host ID assigned to the host; 0 is
114 * reserved to always mean 'localhost'
115 * @return handle to the host, NULL on error
117 struct GNUNET_TESTBED_Host *
118 GNUNET_TESTBED_host_create_by_id_ (uint32_t id)
120 struct GNUNET_TESTBED_Host *host;
122 host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
123 host->unique_id = id;
129 * Obtain a host's unique global ID.
131 * @param host handle to the host, NULL means 'localhost'
132 * @return id global host ID assigned to the host (0 is
133 * 'localhost', but then obviously not globally unique)
136 GNUNET_TESTBED_host_get_id_ (const struct GNUNET_TESTBED_Host *host)
138 return host->unique_id;
143 * Create a host to run peers and controllers on.
145 * @param id global host ID assigned to the host; 0 is
146 * reserved to always mean 'localhost'
147 * @param hostname name of the host, use "NULL" for localhost
148 * @param username username to use for the login; may be NULL
149 * @param port port number to use for ssh; use 0 to let ssh decide
150 * @return handle to the host, NULL on error
152 struct GNUNET_TESTBED_Host *
153 GNUNET_TESTBED_host_create_with_id (uint32_t id,
154 const char *hostname,
155 const char *username,
158 struct GNUNET_TESTBED_Host *host;
160 host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
161 host->hostname = hostname;
162 host->username = username;
163 host->unique_id = id;
164 host->port = (0 == port) ? 22 : port;
165 GNUNET_CONTAINER_DLL_insert_tail (host_list_head, host_list_tail, host);
171 * Create a host to run peers and controllers on.
173 * @param hostname name of the host, use "NULL" for localhost
174 * @param username username to use for the login; may be NULL
175 * @param port port number to use for ssh; use 0 to let ssh decide
176 * @return handle to the host, NULL on error
178 struct GNUNET_TESTBED_Host *
179 GNUNET_TESTBED_host_create (const char *hostname,
180 const char *username,
183 static uint32_t uid_generator;
185 if (NULL == hostname)
186 return GNUNET_TESTBED_host_create_with_id (0, hostname, username, port);
187 return GNUNET_TESTBED_host_create_with_id (++uid_generator,
194 * Load a set of hosts from a configuration file.
196 * @param filename file with the host specification
197 * @param hosts set to the hosts found in the file
198 * @return number of hosts returned in 'hosts', 0 on error
201 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
202 struct GNUNET_TESTBED_Host **hosts)
204 // see testing_group.c, GNUNET_TESTING_hosts_load
211 * Destroy a host handle. Must only be called once everything
212 * running on that host has been stopped.
214 * @param host handle to destroy
217 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
219 GNUNET_CONTAINER_DLL_remove (host_list_head, host_list_tail, host);
225 * Wrapper around GNUNET_HELPER_Handle
227 struct GNUNET_TESTBED_HelperHandle
232 struct GNUNET_OS_Process *process;
235 * Pipe connecting to stdin of the process.
237 struct GNUNET_DISK_PipeHandle *cpipe;
240 * The port number for ssh; used for helpers starting ssh
245 * The ssh destination string; used for helpers starting ssh
252 * Run a given helper process at the given host. Communication
253 * with the helper will be via GNUnet messages on stdin/stdout.
254 * Runs the process via 'ssh' at the specified host, or locally.
255 * Essentially an SSH-wrapper around the 'gnunet_helper_lib.h' API.
257 * @param host host to use, use "NULL" for localhost
258 * @param binary_argv binary name and command-line arguments to give to the binary
259 * @return handle to terminate the command, NULL on error
261 struct GNUNET_TESTBED_HelperHandle *
262 GNUNET_TESTBED_host_run_ (const struct GNUNET_TESTBED_Host *host,
263 char *const binary_argv[])
265 struct GNUNET_TESTBED_HelperHandle *h;
269 while (NULL != binary_argv[argc])
271 h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HelperHandle));
272 h->cpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
273 if (0 == host->unique_id)
275 h->process = GNUNET_OS_start_process_vap (GNUNET_YES,
277 "gnunet-service-testbed",
282 char *remote_args[argc + 6 + 1];
285 GNUNET_asprintf (&h->port, "%d", host->port);
286 if (NULL == host->username)
287 GNUNET_asprintf (&h->dst, "%s", host->hostname);
289 GNUNET_asprintf (&h->dst, "%s@%s", host->hostname, host->username);
291 remote_args[argp++] = "ssh";
292 remote_args[argp++] = "-p";
293 remote_args[argp++] = h->port;
294 remote_args[argp++] = "-q";
295 remote_args[argp++] = h->dst;
296 remote_args[argp++] = "gnunet-service-testbed";
297 while (NULL != binary_argv[argp-6])
299 remote_args[argp] = binary_argv[argp - 6];
302 remote_args[argp++] = NULL;
303 GNUNET_assert (argp == argc + 6 + 1);
304 h->process = GNUNET_OS_start_process_vap (GNUNET_YES,
309 if (NULL == h->process)
311 GNUNET_break (GNUNET_OK == GNUNET_DISK_pipe_close (h->cpipe));
312 GNUNET_free_non_null (h->port);
313 GNUNET_free_non_null (h->dst);
317 GNUNET_break (GNUNET_OK == GNUNET_DISK_pipe_close_end (h->cpipe, GNUNET_DISK_PIPE_END_READ));
323 * Stops a helper in the HelperHandle using GNUNET_HELPER_stop
325 * @param handle the handle returned from GNUNET_TESTBED_host_start_
328 GNUNET_TESTBED_host_stop_ (struct GNUNET_TESTBED_HelperHandle *handle)
330 GNUNET_break (GNUNET_OK == GNUNET_DISK_pipe_close (handle->cpipe));
331 GNUNET_break (0 == GNUNET_OS_process_kill (handle->process, SIGTERM));
332 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (handle->process));
333 GNUNET_OS_process_destroy (handle->process);
334 GNUNET_free_non_null (handle->port);
335 GNUNET_free_non_null (handle->dst);
336 GNUNET_free (handle);
339 /* end of testbed_api_hosts.c */