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"
36 #include "testbed_api.h"
37 #include "testbed_api_hosts.h"
38 #include "testbed_helper.h"
41 * Generic logging shorthand
43 #define LOG(kind, ...) \
44 GNUNET_log_from (kind, "testbed-api-hosts", __VA_ARGS__);
47 * Number of extra elements we create space for when we grow host list
49 #define HOST_LIST_GROW_STEP 10
53 * A list entry for registered controllers list
55 struct RegisteredController
58 * The controller at which this host is registered
60 const struct GNUNET_TESTBED_Controller *controller;
63 * The next ptr for DLL
65 struct RegisteredController *next;
68 * The prev ptr for DLL
70 struct RegisteredController *prev;
75 * Opaque handle to a host running experiments managed by the testing framework.
76 * The master process must be able to SSH to this host without password (via
79 struct GNUNET_TESTBED_Host
83 * The next pointer for DLL
85 struct GNUNET_TESTBED_Host *next;
88 * The prev pointer for DLL
90 struct GNUNET_TESTBED_Host *prev;
93 * The hostname of the host; NULL for localhost
98 * The username to be used for SSH login
100 const char *username;
103 * The head for the list of controllers where this host is registered
105 struct RegisteredController *rc_head;
108 * The tail for the list of controllers where this host is registered
110 struct RegisteredController *rc_tail;
113 * Global ID we use to refer to a host on the network
118 * The port which is to be used for SSH
126 * Array of available hosts
128 static struct GNUNET_TESTBED_Host **host_list;
131 * The size of the available hosts list
133 static uint32_t host_list_size;
137 * Lookup a host by ID.
139 * @param id global host ID assigned to the host; 0 is
140 * reserved to always mean 'localhost'
141 * @return handle to the host, NULL if host not found
143 struct GNUNET_TESTBED_Host *
144 GNUNET_TESTBED_host_lookup_by_id_ (uint32_t id)
146 if (host_list_size <= id)
148 return host_list[id];
153 * Create a host by ID; given this host handle, we could not
154 * run peers at the host, but we can talk about the host
157 * @param id global host ID assigned to the host; 0 is
158 * reserved to always mean 'localhost'
159 * @return handle to the host, NULL on error
161 struct GNUNET_TESTBED_Host *
162 GNUNET_TESTBED_host_create_by_id_ (uint32_t id)
164 return GNUNET_TESTBED_host_create_with_id (id, NULL, NULL, 0);
169 * Obtain the host's unique global ID.
171 * @param host handle to the host, NULL means 'localhost'
172 * @return id global host ID assigned to the host (0 is
173 * 'localhost', but then obviously not globally unique)
176 GNUNET_TESTBED_host_get_id_ (const struct GNUNET_TESTBED_Host *host)
183 * Obtain the host's hostname.
185 * @param host handle to the host, NULL means 'localhost'
186 * @return hostname of the host
189 GNUNET_TESTBED_host_get_hostname_ (const struct GNUNET_TESTBED_Host *host)
191 return host->hostname;
196 * Obtain the host's username
198 * @param host handle to the host, NULL means 'localhost'
199 * @return username to login to the host
202 GNUNET_TESTBED_host_get_username_ (const struct GNUNET_TESTBED_Host *host)
204 return host->username;
209 * Obtain the host's ssh port
211 * @param host handle to the host, NULL means 'localhost'
212 * @return username to login to the host
215 GNUNET_TESTBED_host_get_ssh_port_ (const struct GNUNET_TESTBED_Host *host)
222 * Create a host to run peers and controllers on.
224 * @param id global host ID assigned to the host; 0 is
225 * reserved to always mean 'localhost'
226 * @param hostname name of the host, use "NULL" for localhost
227 * @param username username to use for the login; may be NULL
228 * @param port port number to use for ssh; use 0 to let ssh decide
229 * @return handle to the host, NULL on error
231 struct GNUNET_TESTBED_Host *
232 GNUNET_TESTBED_host_create_with_id (uint32_t id,
233 const char *hostname,
234 const char *username,
237 struct GNUNET_TESTBED_Host *host;
240 if ((id < host_list_size) && (NULL != host_list[id]))
242 LOG (GNUNET_ERROR_TYPE_WARNING, "Host with id: %u already created\n");
245 host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
246 host->hostname = hostname;
247 host->username = username;
249 host->port = (0 == port) ? 22 : port;
250 new_size = host_list_size;
251 while (id >= new_size)
252 new_size += HOST_LIST_GROW_STEP;
253 if (new_size != host_list_size)
255 host_list = GNUNET_realloc (host_list, sizeof (struct GNUNET_TESTBED_Host *)
257 (void) memset(&host_list[host_list_size], 0,
258 sizeof (struct GNUNET_TESTBED_Host *) *
259 (new_size - host_list_size));
260 host_list_size = new_size;
262 LOG (GNUNET_ERROR_TYPE_DEBUG,
263 "Adding host with id: %u\n", host->id);
264 host_list[id] = host;
270 * Create a host to run peers and controllers on.
272 * @param hostname name of the host, use "NULL" for localhost
273 * @param username username to use for the login; may be NULL
274 * @param port port number to use for ssh; use 0 to let ssh decide
275 * @return handle to the host, NULL on error
277 struct GNUNET_TESTBED_Host *
278 GNUNET_TESTBED_host_create (const char *hostname,
279 const char *username,
282 static uint32_t uid_generator;
284 if (NULL == hostname)
285 return GNUNET_TESTBED_host_create_with_id (0, hostname, username, port);
286 return GNUNET_TESTBED_host_create_with_id (++uid_generator,
293 * Load a set of hosts from a configuration file.
295 * @param filename file with the host specification
296 * @param hosts set to the hosts found in the file
297 * @return number of hosts returned in 'hosts', 0 on error
300 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
301 struct GNUNET_TESTBED_Host **hosts)
303 // see testing_group.c, GNUNET_TESTING_hosts_load
310 * Destroy a host handle. Must only be called once everything
311 * running on that host has been stopped.
313 * @param host handle to destroy
316 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
318 struct RegisteredController *rc;
321 GNUNET_assert (host->id < host_list_size);
322 GNUNET_assert (host_list[host->id] == host);
323 host_list[host->id] = NULL;
324 /* clear registered controllers list */
325 for (rc=host->rc_head; NULL != rc; rc=host->rc_head)
327 GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
331 while (host_list_size >= HOST_LIST_GROW_STEP)
333 for (id = host_list_size - 1;
334 id > host_list_size - HOST_LIST_GROW_STEP; id--)
335 if (NULL != host_list[id])
337 if (id != host_list_size - HOST_LIST_GROW_STEP)
339 if (NULL != host_list[id])
341 host_list_size -= HOST_LIST_GROW_STEP;
343 host_list = GNUNET_realloc (host_list, sizeof (struct GNUNET_TESTBED_Host) *
349 * Marks a host as registered with a controller
351 * @param host the host to mark
352 * @param controller the controller at which this host is registered
355 GNUNET_TESTBED_mark_host_registered_at_ (struct GNUNET_TESTBED_Host *host,
356 const struct GNUNET_TESTBED_Controller
359 struct RegisteredController *rc;
361 for (rc=host->rc_head; NULL != rc; rc=rc->next)
363 if (controller == rc->controller) /* already registered at controller */
369 rc = GNUNET_malloc (sizeof (struct RegisteredController));
370 rc->controller = controller;
371 //host->controller = controller;
372 GNUNET_CONTAINER_DLL_insert_tail (host->rc_head, host->rc_tail, rc);
377 * Checks whether a host has been registered
379 * @param host the host to check
380 * @param controller the controller at which host's registration is checked
381 * @return GNUNET_YES if registered; GNUNET_NO if not
384 GNUNET_TESTBED_is_host_registered_ (const struct GNUNET_TESTBED_Host *host,
385 const struct GNUNET_TESTBED_Controller
388 struct RegisteredController *rc;
390 for (rc=host->rc_head; NULL != rc; rc=rc->next)
392 if (controller == rc->controller) /* already registered at controller */
401 /* end of testbed_api_hosts.c */