clique topology
[oweals/gnunet.git] / src / testbed / testbed_api_hosts.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
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
27  */
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31 #include "gnunet_core_service.h"
32 #include "gnunet_transport_service.h"
33
34 #include "testbed_api.h"
35 #include "testbed_api_hosts.h"
36
37 /**
38  * Generic logging shorthand
39  */
40 #define LOG(kind, ...)                          \
41   GNUNET_log_from (kind, "testbed-api-hosts", __VA_ARGS__);
42
43 /**
44  * Number of extra elements we create space for when we grow host list
45  */
46 #define HOST_LIST_GROW_STEP 10
47
48
49 /**
50  * A list entry for registered controllers list
51  */
52 struct RegisteredController
53 {
54   /**
55    * The controller at which this host is registered
56    */
57   const struct GNUNET_TESTBED_Controller *controller;
58
59   /**
60    * The next ptr for DLL
61    */
62   struct RegisteredController *next;
63
64   /**
65    * The prev ptr for DLL
66    */
67   struct RegisteredController *prev;
68 };
69
70
71 /**
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
74  * ssh-agent).
75  */
76 struct GNUNET_TESTBED_Host
77 {
78
79   /**
80    * The next pointer for DLL
81    */
82   struct GNUNET_TESTBED_Host *next;
83
84   /**
85    * The prev pointer for DLL
86    */
87   struct GNUNET_TESTBED_Host *prev;
88
89   /**
90    * The hostname of the host; NULL for localhost
91    */
92   const char *hostname;
93
94   /**
95    * The username to be used for SSH login
96    */
97   const char *username;
98
99   /**
100    * The head for the list of controllers where this host is registered
101    */
102   struct RegisteredController *rc_head;
103
104   /**
105    * The tail for the list of controllers where this host is registered
106    */
107   struct RegisteredController *rc_tail;
108
109   /**
110    * Global ID we use to refer to a host on the network
111    */
112   uint32_t id;
113
114   /**
115    * The port which is to be used for SSH
116    */
117   uint16_t port;
118
119 };
120
121
122 /**
123  * Array of available hosts
124  */
125 static struct GNUNET_TESTBED_Host **host_list;
126
127 /**
128  * The size of the available hosts list
129  */
130 static uint32_t host_list_size;
131
132
133 /**
134  * Lookup a host by ID.
135  *
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
139  */
140 struct GNUNET_TESTBED_Host *
141 GNUNET_TESTBED_host_lookup_by_id_ (uint32_t id)
142 {
143   if (host_list_size <= id)
144     return NULL;
145   return host_list[id];
146 }
147
148
149 /**
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
152  * internally.
153  *
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
157  */
158 struct GNUNET_TESTBED_Host *
159 GNUNET_TESTBED_host_create_by_id_ (uint32_t id)
160 {
161   return GNUNET_TESTBED_host_create_with_id (id, NULL, NULL, 0);
162 }
163
164
165 /**
166  * Obtain the host's unique global ID.
167  *
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)
171  */
172 uint32_t
173 GNUNET_TESTBED_host_get_id_ (const struct GNUNET_TESTBED_Host * host)
174 {
175   return host->id;
176 }
177
178
179 /**
180  * Obtain the host's hostname.
181  *
182  * @param host handle to the host, NULL means 'localhost'
183  * @return hostname of the host
184  */
185 const char *
186 GNUNET_TESTBED_host_get_hostname_ (const struct GNUNET_TESTBED_Host *host)
187 {
188   return host->hostname;
189 }
190
191
192 /**
193  * Obtain the host's hostname.
194  *
195  * @param host handle to the host, NULL means 'localhost'
196  * @return hostname of the host
197  */
198 const char *
199 GNUNET_TESTBED_host_get_hostname (const struct GNUNET_TESTBED_Host *host)
200 {
201   return GNUNET_TESTBED_host_get_hostname_ (host);
202 }
203
204
205 /**
206  * Obtain the host's username
207  *
208  * @param host handle to the host, NULL means 'localhost'
209  * @return username to login to the host
210  */
211 const char *
212 GNUNET_TESTBED_host_get_username_ (const struct GNUNET_TESTBED_Host *host)
213 {
214   return host->username;
215 }
216
217
218 /**
219  * Obtain the host's ssh port
220  *
221  * @param host handle to the host, NULL means 'localhost'
222  * @return username to login to the host
223  */
224 uint16_t
225 GNUNET_TESTBED_host_get_ssh_port_ (const struct GNUNET_TESTBED_Host * host)
226 {
227   return host->port;
228 }
229
230
231 /**
232  * Create a host to run peers and controllers on.
233  *
234  * @param id global host ID assigned to the host; 0 is
235  *        reserved to always mean 'localhost'
236  * @param hostname name of the host, use "NULL" for localhost
237  * @param username username to use for the login; may be NULL
238  * @param port port number to use for ssh; use 0 to let ssh decide
239  * @return handle to the host, NULL on error
240  */
241 struct GNUNET_TESTBED_Host *
242 GNUNET_TESTBED_host_create_with_id (uint32_t id, const char *hostname,
243                                     const char *username, uint16_t port)
244 {
245   struct GNUNET_TESTBED_Host *host;
246   uint32_t new_size;
247
248   if ((id < host_list_size) && (NULL != host_list[id]))
249   {
250     LOG (GNUNET_ERROR_TYPE_WARNING, "Host with id: %u already created\n", id);
251     return NULL;
252   }
253   host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
254   host->hostname = (NULL != hostname) ? GNUNET_strdup (hostname) : NULL;
255   host->username = (NULL != username) ? GNUNET_strdup (username) : NULL;
256   host->id = id;
257   host->port = (0 == port) ? 22 : port;
258   new_size = host_list_size;
259   while (id >= new_size)
260     new_size += HOST_LIST_GROW_STEP;
261   if (new_size != host_list_size)
262   {
263     host_list =
264         GNUNET_realloc (host_list,
265                         sizeof (struct GNUNET_TESTBED_Host *) * new_size);
266     (void) memset (&host_list[host_list_size], 0,
267                    sizeof (struct GNUNET_TESTBED_Host *) * (new_size -
268                                                             host_list_size));
269     host_list_size = new_size;
270   }
271   LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding host with id: %u\n", host->id);
272   host_list[id] = host;
273   return host;
274 }
275
276
277 /**
278  * Create a host to run peers and controllers on.
279  *
280  * @param hostname name of the host, use "NULL" for localhost
281  * @param username username to use for the login; may be NULL
282  * @param port port number to use for ssh; use 0 to let ssh decide
283  * @return handle to the host, NULL on error
284  */
285 struct GNUNET_TESTBED_Host *
286 GNUNET_TESTBED_host_create (const char *hostname, const char *username,
287                             uint16_t port)
288 {
289   static uint32_t uid_generator;
290
291   if (NULL == hostname)
292     return GNUNET_TESTBED_host_create_with_id (0, hostname, username, port);
293   return GNUNET_TESTBED_host_create_with_id (++uid_generator, hostname,
294                                              username, port);
295 }
296
297
298 /**
299  * Load a set of hosts from a configuration file.
300  *
301  * @param filename file with the host specification
302  * @param hosts set to the hosts found in the file; caller must free this if
303  *          number of hosts returned is greater than 0
304  * @return number of hosts returned in 'hosts', 0 on error
305  */
306 unsigned int
307 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
308                                      struct GNUNET_TESTBED_Host ***hosts)
309 {
310   //struct GNUNET_TESTBED_Host **host_array;
311   struct GNUNET_TESTBED_Host *starting_host;
312   char *data;
313   char *buf;
314   char username[256];
315   char hostname[256];
316   uint64_t fs;
317   short int port;
318   int ret;
319   unsigned int offset;
320   unsigned int count;
321
322
323   GNUNET_assert (NULL != filename);
324   if (GNUNET_YES != GNUNET_DISK_file_test (filename))
325   {
326     LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s not found\n"), filename);
327     return 0;
328   }
329   if (GNUNET_OK !=
330       GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
331     fs = 0;
332   if (0 == fs)
333   {
334     LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s has no data\n"), filename);
335     return 0;
336   }
337   data = GNUNET_malloc (fs);
338   if (fs != GNUNET_DISK_fn_read (filename, data, fs))
339   {
340     GNUNET_free (data);
341     LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s cannot be read\n"),
342          filename);
343     return 0;
344   }
345   buf = data;
346   offset = 0;
347   starting_host = NULL;
348   count = 0;
349   while (offset < (fs - 1))
350   {
351     offset++;
352     if (((data[offset] == '\n')) && (buf != &data[offset]))
353     {
354       data[offset] = '\0';
355       ret = SSCANF (buf, "%255[a-zA-Z0-9_]@%255[a-zA-Z0-9.]:%5hd",
356                     username, hostname, &port);
357       if  (3 == ret)
358       {
359         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360                     "Successfully read host %s, port %d and user %s from file\n",
361                     hostname, port, username);
362         /* We store hosts in a static list; hence we only require the starting
363            host pointer in that list to access the newly created list of hosts */
364         if (NULL == starting_host)
365           starting_host = GNUNET_TESTBED_host_create (hostname, username,
366                                                       port);
367         else
368           (void) GNUNET_TESTBED_host_create (hostname, username, port);
369         count++;
370       }
371       else
372         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
373                     "Error reading line `%s' in hostfile\n", buf);
374       buf = &data[offset + 1];
375     }
376     else if ((data[offset] == '\n') || (data[offset] == '\0'))
377       buf = &data[offset + 1];
378   }
379   GNUNET_free (data);
380   if (NULL == starting_host)
381     return 0;
382   *hosts = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * count);
383   memcpy (*hosts,
384           &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)],
385           sizeof (struct GNUNET_TESTBED_Host *) * count);
386   return count;
387 }
388
389
390 /**
391  * Destroy a host handle.  Must only be called once everything
392  * running on that host has been stopped.
393  *
394  * @param host handle to destroy
395  */
396 void
397 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
398 {
399   struct RegisteredController *rc;
400   uint32_t id;
401
402   GNUNET_assert (host->id < host_list_size);
403   GNUNET_assert (host_list[host->id] == host);
404   host_list[host->id] = NULL;
405   /* clear registered controllers list */
406   for (rc = host->rc_head; NULL != rc; rc = host->rc_head)
407   {
408     GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
409     GNUNET_free (rc);
410   }
411   GNUNET_free_non_null ((char *) host->username);
412   GNUNET_free_non_null ((char *) host->hostname);
413   GNUNET_free (host);
414   while (host_list_size >= HOST_LIST_GROW_STEP)
415   {
416     for (id = host_list_size - 1; id > host_list_size - HOST_LIST_GROW_STEP;
417          id--)
418       if (NULL != host_list[id])
419         break;
420     if (id != host_list_size - HOST_LIST_GROW_STEP)
421       break;
422     if (NULL != host_list[id])
423       break;
424     host_list_size -= HOST_LIST_GROW_STEP;
425   }
426   host_list =
427       GNUNET_realloc (host_list,
428                       sizeof (struct GNUNET_TESTBED_Host *) * host_list_size);
429 }
430
431
432 /**
433  * Marks a host as registered with a controller
434  *
435  * @param host the host to mark
436  * @param controller the controller at which this host is registered
437  */
438 void
439 GNUNET_TESTBED_mark_host_registered_at_ (struct GNUNET_TESTBED_Host *host,
440                                          const struct GNUNET_TESTBED_Controller
441                                          *const controller)
442 {
443   struct RegisteredController *rc;
444
445   for (rc = host->rc_head; NULL != rc; rc = rc->next)
446   {
447     if (controller == rc->controller)   /* already registered at controller */
448     {
449       GNUNET_break (0);
450       return;
451     }
452   }
453   rc = GNUNET_malloc (sizeof (struct RegisteredController));
454   rc->controller = controller;
455   GNUNET_CONTAINER_DLL_insert_tail (host->rc_head, host->rc_tail, rc);
456 }
457
458
459 /**
460  * Checks whether a host has been registered
461  *
462  * @param host the host to check
463  * @param controller the controller at which host's registration is checked
464  * @return GNUNET_YES if registered; GNUNET_NO if not
465  */
466 int
467 GNUNET_TESTBED_is_host_registered_ (const struct GNUNET_TESTBED_Host *host,
468                                     const struct GNUNET_TESTBED_Controller
469                                     *const controller)
470 {
471   struct RegisteredController *rc;
472
473   for (rc = host->rc_head; NULL != rc; rc = rc->next)
474   {
475     if (controller == rc->controller)   /* already registered at controller */
476     {
477       return GNUNET_YES;
478     }
479   }
480   return GNUNET_NO;
481 }
482
483
484 /**
485  * Checks whether a host can be used to start testbed service
486  *
487  * @param host the host to check
488  * @return GNUNET_YES if testbed service can be started on the given host
489  *           remotely; GNUNET_NO if not
490  */
491 int
492 GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host)
493 {
494   char *remote_args[11];
495   char *portstr;
496   char *ssh_addr;
497   const char *hostname;
498   struct GNUNET_OS_Process *auxp;
499   unsigned long code;
500   enum GNUNET_OS_ProcessStatusType type;
501   int ret;
502   unsigned int argp;
503
504   portstr = NULL;
505   ssh_addr = NULL;
506   hostname = (NULL == host->hostname) ? "127.0.0.1" : host->hostname;
507   if (NULL == host->username)
508     ssh_addr = GNUNET_strdup (hostname);
509   else
510     GNUNET_asprintf (&ssh_addr, "%s@%s", host->username, hostname);
511   argp = 0;
512   remote_args[argp++] = "ssh";
513   GNUNET_asprintf (&portstr, "%u", host->port);
514   remote_args[argp++] = "-p";
515   remote_args[argp++] = portstr;
516   remote_args[argp++] = "-o";
517   remote_args[argp++] = "BatchMode=yes";
518   remote_args[argp++] = "-o";
519   remote_args[argp++] = "NoHostAuthenticationForLocalhost=yes";
520   remote_args[argp++] = ssh_addr;
521   remote_args[argp++] = "which";
522   remote_args[argp++] = "gnunet-helper-testbed";
523   remote_args[argp++] = NULL;
524   GNUNET_assert (argp == 11);
525   auxp =
526       GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
527                                    NULL, "ssh", remote_args);
528   if (NULL == auxp)
529   {
530     GNUNET_free (ssh_addr);
531     GNUNET_free (portstr);
532     return GNUNET_NO;
533   }
534   do
535   {
536     ret = GNUNET_OS_process_status (auxp, &type, &code);
537     GNUNET_assert (GNUNET_SYSERR != ret);
538     (void) usleep (300);
539   }
540   while (GNUNET_NO == ret);
541   //(void) GNUNET_OS_process_wait (auxp);
542   GNUNET_OS_process_destroy (auxp);
543   GNUNET_free (ssh_addr);
544   GNUNET_free (portstr);
545   return (0 != code) ? GNUNET_NO : GNUNET_YES;
546 }
547
548 /* end of testbed_api_hosts.c */