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"
40 * Generic logging shorthand
42 #define LOG(kind, ...) \
43 GNUNET_log_from (kind, "testbed-api-hosts", __VA_ARGS__);
46 * Number of extra elements we create space for when we grow host list
48 #define HOST_LIST_GROW_STEP 10
52 * A list entry for registered controllers list
54 struct RegisteredController
57 * The controller at which this host is registered
59 const struct GNUNET_TESTBED_Controller *controller;
62 * The next ptr for DLL
64 struct RegisteredController *next;
67 * The prev ptr for DLL
69 struct RegisteredController *prev;
74 * Opaque handle to a host running experiments managed by the testing framework.
75 * The master process must be able to SSH to this host without password (via
78 struct GNUNET_TESTBED_Host
82 * The next pointer for DLL
84 struct GNUNET_TESTBED_Host *next;
87 * The prev pointer for DLL
89 struct GNUNET_TESTBED_Host *prev;
92 * The hostname of the host; NULL for localhost
97 * The username to be used for SSH login
102 * The head for the list of controllers where this host is registered
104 struct RegisteredController *rc_head;
107 * The tail for the list of controllers where this host is registered
109 struct RegisteredController *rc_tail;
112 * Global ID we use to refer to a host on the network
117 * The port which is to be used for SSH
125 * Array of available hosts
127 static struct GNUNET_TESTBED_Host **host_list;
130 * The size of the available hosts list
132 static uint32_t host_list_size;
136 * Lookup a host by ID.
138 * @param id global host ID assigned to the host; 0 is
139 * reserved to always mean 'localhost'
140 * @return handle to the host, NULL if host not found
142 struct GNUNET_TESTBED_Host *
143 GNUNET_TESTBED_host_lookup_by_id_ (uint32_t id)
145 if (host_list_size <= id)
147 return host_list[id];
152 * Create a host by ID; given this host handle, we could not
153 * run peers at the host, but we can talk about the host
156 * @param id global host ID assigned to the host; 0 is
157 * reserved to always mean 'localhost'
158 * @return handle to the host, NULL on error
160 struct GNUNET_TESTBED_Host *
161 GNUNET_TESTBED_host_create_by_id_ (uint32_t id)
163 return GNUNET_TESTBED_host_create_with_id (id, NULL, NULL, 0);
168 * Obtain the host's unique global ID.
170 * @param host handle to the host, NULL means 'localhost'
171 * @return id global host ID assigned to the host (0 is
172 * 'localhost', but then obviously not globally unique)
175 GNUNET_TESTBED_host_get_id_ (const struct GNUNET_TESTBED_Host * host)
182 * Obtain the host's hostname.
184 * @param host handle to the host, NULL means 'localhost'
185 * @return hostname of the host
188 GNUNET_TESTBED_host_get_hostname_ (const struct GNUNET_TESTBED_Host *host)
190 return host->hostname;
195 * Obtain the host's username
197 * @param host handle to the host, NULL means 'localhost'
198 * @return username to login to the host
201 GNUNET_TESTBED_host_get_username_ (const struct GNUNET_TESTBED_Host *host)
203 return host->username;
208 * Obtain the host's ssh port
210 * @param host handle to the host, NULL means 'localhost'
211 * @return username to login to the host
214 GNUNET_TESTBED_host_get_ssh_port_ (const struct GNUNET_TESTBED_Host * host)
221 * Create a host to run peers and controllers on.
223 * @param id global host ID assigned to the host; 0 is
224 * reserved to always mean 'localhost'
225 * @param hostname name of the host, use "NULL" for localhost
226 * @param username username to use for the login; may be NULL
227 * @param port port number to use for ssh; use 0 to let ssh decide
228 * @return handle to the host, NULL on error
230 struct GNUNET_TESTBED_Host *
231 GNUNET_TESTBED_host_create_with_id (uint32_t id, const char *hostname,
232 const char *username, uint16_t port)
234 struct GNUNET_TESTBED_Host *host;
237 if ((id < host_list_size) && (NULL != host_list[id]))
239 LOG (GNUNET_ERROR_TYPE_WARNING, "Host with id: %u already created\n", id);
242 host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
243 host->hostname = (NULL != hostname) ? GNUNET_strdup (hostname) : NULL;
244 host->username = (NULL != username) ? GNUNET_strdup (username) : NULL;
246 host->port = (0 == port) ? 22 : port;
247 new_size = host_list_size;
248 while (id >= new_size)
249 new_size += HOST_LIST_GROW_STEP;
250 if (new_size != host_list_size)
253 GNUNET_realloc (host_list,
254 sizeof (struct GNUNET_TESTBED_Host *) * new_size);
255 (void) memset (&host_list[host_list_size], 0,
256 sizeof (struct GNUNET_TESTBED_Host *) * (new_size -
258 host_list_size = new_size;
260 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding host with id: %u\n", host->id);
261 host_list[id] = host;
267 * Create a host to run peers and controllers on.
269 * @param hostname name of the host, use "NULL" for localhost
270 * @param username username to use for the login; may be NULL
271 * @param port port number to use for ssh; use 0 to let ssh decide
272 * @return handle to the host, NULL on error
274 struct GNUNET_TESTBED_Host *
275 GNUNET_TESTBED_host_create (const char *hostname, const char *username,
278 static uint32_t uid_generator;
280 if (NULL == hostname)
281 return GNUNET_TESTBED_host_create_with_id (0, hostname, username, port);
282 return GNUNET_TESTBED_host_create_with_id (++uid_generator, hostname,
288 * Load a set of hosts from a configuration file.
290 * @param filename file with the host specification
291 * @param hosts set to the hosts found in the file; caller must free this if
292 * number of hosts returned is greater than 0
293 * @return number of hosts returned in 'hosts', 0 on error
296 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
297 struct GNUNET_TESTBED_Host ***hosts)
299 //struct GNUNET_TESTBED_Host **host_array;
300 struct GNUNET_TESTBED_Host *starting_host;
312 GNUNET_assert (NULL != filename);
313 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
315 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s not found\n"), filename);
319 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
323 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s has no data\n"), filename);
326 data = GNUNET_malloc (fs);
327 if (fs != GNUNET_DISK_fn_read (filename, data, fs))
330 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s cannot be read\n"),
336 starting_host = NULL;
338 while (offset < (fs - 1))
341 if (((data[offset] == '\n')) && (buf != &data[offset]))
346 ret = SSCANF (buf, "%a[a-zA-Z0-9_]@%a[a-zA-Z0-9.]:%hd",
347 &username, &hostname, &port);
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
351 "Successfully read host %s, port %d and user %s from file\n",
352 hostname, port, username);
353 /* We store hosts in a static list; hence we only require the starting
354 host pointer in that list to access the newly created list of hosts */
355 if (NULL == starting_host)
356 starting_host = GNUNET_TESTBED_host_create (hostname, username,
359 (void) GNUNET_TESTBED_host_create (hostname, username, port);
363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
364 "Error reading line `%s' in hostfile\n", buf);
365 GNUNET_free_non_null (hostname);
366 GNUNET_free_non_null (username);
367 buf = &data[offset + 1];
369 else if ((data[offset] == '\n') || (data[offset] == '\0'))
370 buf = &data[offset + 1];
373 if (NULL == starting_host)
375 *hosts = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * count);
377 &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)],
378 sizeof (struct GNUNET_TESTBED_Host *) * count);
384 * Destroy a host handle. Must only be called once everything
385 * running on that host has been stopped.
387 * @param host handle to destroy
390 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
392 struct RegisteredController *rc;
395 GNUNET_assert (host->id < host_list_size);
396 GNUNET_assert (host_list[host->id] == host);
397 host_list[host->id] = NULL;
398 /* clear registered controllers list */
399 for (rc = host->rc_head; NULL != rc; rc = host->rc_head)
401 GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
404 GNUNET_free_non_null ((char *) host->username);
405 GNUNET_free_non_null ((char *) host->hostname);
407 while (host_list_size >= HOST_LIST_GROW_STEP)
409 for (id = host_list_size - 1; id > host_list_size - HOST_LIST_GROW_STEP;
411 if (NULL != host_list[id])
413 if (id != host_list_size - HOST_LIST_GROW_STEP)
415 if (NULL != host_list[id])
417 host_list_size -= HOST_LIST_GROW_STEP;
420 GNUNET_realloc (host_list,
421 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size);
426 * Marks a host as registered with a controller
428 * @param host the host to mark
429 * @param controller the controller at which this host is registered
432 GNUNET_TESTBED_mark_host_registered_at_ (struct GNUNET_TESTBED_Host *host,
433 const struct GNUNET_TESTBED_Controller
436 struct RegisteredController *rc;
438 for (rc = host->rc_head; NULL != rc; rc = rc->next)
440 if (controller == rc->controller) /* already registered at controller */
446 rc = GNUNET_malloc (sizeof (struct RegisteredController));
447 rc->controller = controller;
448 //host->controller = controller;
449 GNUNET_CONTAINER_DLL_insert_tail (host->rc_head, host->rc_tail, rc);
454 * Checks whether a host has been registered
456 * @param host the host to check
457 * @param controller the controller at which host's registration is checked
458 * @return GNUNET_YES if registered; GNUNET_NO if not
461 GNUNET_TESTBED_is_host_registered_ (const struct GNUNET_TESTBED_Host *host,
462 const struct GNUNET_TESTBED_Controller
465 struct RegisteredController *rc;
467 for (rc = host->rc_head; NULL != rc; rc = rc->next)
469 if (controller == rc->controller) /* already registered at controller */
478 /* end of testbed_api_hosts.c */