293f3493b771cc04bf0d4347eca2472967c684de
[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 unsigned int 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 username
194  *
195  * @param host handle to the host, NULL means 'localhost'
196  * @return username to login to the host
197  */
198 const char *
199 GNUNET_TESTBED_host_get_username_ (const struct GNUNET_TESTBED_Host *host)
200 {
201   return host->username;
202 }
203
204
205 /**
206  * Obtain the host's ssh port
207  *
208  * @param host handle to the host, NULL means 'localhost'
209  * @return username to login to the host
210  */
211 uint16_t
212 GNUNET_TESTBED_host_get_ssh_port_ (const struct GNUNET_TESTBED_Host * host)
213 {
214   return host->port;
215 }
216
217
218 /**
219  * Create a host to run peers and controllers on.
220  *
221  * @param id global host ID assigned to the host; 0 is
222  *        reserved to always mean 'localhost'
223  * @param hostname name of the host, use "NULL" for localhost
224  * @param username username to use for the login; may be NULL
225  * @param port port number to use for ssh; use 0 to let ssh decide
226  * @return handle to the host, NULL on error
227  */
228 struct GNUNET_TESTBED_Host *
229 GNUNET_TESTBED_host_create_with_id (uint32_t id, const char *hostname,
230                                     const char *username, uint16_t port)
231 {
232   struct GNUNET_TESTBED_Host *host;
233   unsigned int new_size;
234
235   if ((id < host_list_size) && (NULL != host_list[id]))
236   {
237     LOG (GNUNET_ERROR_TYPE_WARNING, "Host with id: %u already created\n", id);
238     return NULL;
239   }
240   host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
241   host->hostname = (NULL != hostname) ? GNUNET_strdup (hostname) : NULL;
242   host->username = (NULL != username) ? GNUNET_strdup (username) : NULL;
243   host->id = id;
244   host->port = (0 == port) ? 22 : port;
245   new_size = host_list_size;
246   while (id >= new_size)
247     new_size += HOST_LIST_GROW_STEP;
248   if (new_size != host_list_size)
249     GNUNET_array_grow (host_list, host_list_size, new_size);
250   GNUNET_assert (id < host_list_size);
251   LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding host with id: %u\n", host->id);
252   host_list[id] = host;
253   return host;
254 }
255
256
257 /**
258  * Create a host to run peers and controllers on.
259  *
260  * @param hostname name of the host, use "NULL" for localhost
261  * @param username username to use for the login; may be NULL
262  * @param port port number to use for ssh; use 0 to let ssh decide
263  * @return handle to the host, NULL on error
264  */
265 struct GNUNET_TESTBED_Host *
266 GNUNET_TESTBED_host_create (const char *hostname, const char *username,
267                             uint16_t port)
268 {
269   static uint32_t uid_generator;
270
271   if (NULL == hostname)
272     return GNUNET_TESTBED_host_create_with_id (0, hostname, username, port);
273   return GNUNET_TESTBED_host_create_with_id (++uid_generator, hostname,
274                                              username, port);
275 }
276
277
278 /**
279  * Load a set of hosts from a configuration file.
280  *
281  * @param filename file with the host specification
282  * @param hosts set to the hosts found in the file; caller must free this if
283  *          number of hosts returned is greater than 0
284  * @return number of hosts returned in 'hosts', 0 on error
285  */
286 unsigned int
287 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
288                                      struct GNUNET_TESTBED_Host ***hosts)
289 {
290   //struct GNUNET_TESTBED_Host **host_array;
291   struct GNUNET_TESTBED_Host *starting_host;
292   char *data;
293   char *buf;
294   char username[256];
295   char hostname[256];
296   uint64_t fs;
297   short int port;
298   int ret;
299   unsigned int offset;
300   unsigned int count;
301
302
303   GNUNET_assert (NULL != filename);
304   if (GNUNET_YES != GNUNET_DISK_file_test (filename))
305   {
306     LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s not found\n"), filename);
307     return 0;
308   }
309   if (GNUNET_OK !=
310       GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
311     fs = 0;
312   if (0 == fs)
313   {
314     LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s has no data\n"), filename);
315     return 0;
316   }
317   data = GNUNET_malloc (fs);
318   if (fs != GNUNET_DISK_fn_read (filename, data, fs))
319   {
320     GNUNET_free (data);
321     LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s cannot be read\n"),
322          filename);
323     return 0;
324   }
325   buf = data;
326   offset = 0;
327   starting_host = NULL;
328   count = 0;
329   while (offset < (fs - 1))
330   {
331     offset++;
332     if (((data[offset] == '\n')) && (buf != &data[offset]))
333     {
334       data[offset] = '\0';
335       ret =
336           SSCANF (buf, "%255[a-zA-Z0-9_]@%255[a-zA-Z0-9.]:%5hd", username,
337                   hostname, &port);
338       if (3 == ret)
339       {
340         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341                     "Successfully read host %s, port %d and user %s from file\n",
342                     hostname, port, username);
343         /* We store hosts in a static list; hence we only require the starting
344          * host pointer in that list to access the newly created list of hosts */
345         if (NULL == starting_host)
346           starting_host = GNUNET_TESTBED_host_create (hostname, username, port);
347         else
348           (void) GNUNET_TESTBED_host_create (hostname, username, port);
349         count++;
350       }
351       else
352         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
353                     "Error reading line `%s' in hostfile\n", buf);
354       buf = &data[offset + 1];
355     }
356     else if ((data[offset] == '\n') || (data[offset] == '\0'))
357       buf = &data[offset + 1];
358   }
359   GNUNET_free (data);
360   if (NULL == starting_host)
361     return 0;
362   *hosts = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * count);
363   memcpy (*hosts, &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)],
364           sizeof (struct GNUNET_TESTBED_Host *) * count);
365   return count;
366 }
367
368
369 /**
370  * Destroy a host handle.  Must only be called once everything
371  * running on that host has been stopped.
372  *
373  * @param host handle to destroy
374  */
375 void
376 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
377 {
378   struct RegisteredController *rc;
379   uint32_t id;
380
381   GNUNET_assert (host->id < host_list_size);
382   GNUNET_assert (host_list[host->id] == host);
383   host_list[host->id] = NULL;
384   /* clear registered controllers list */
385   for (rc = host->rc_head; NULL != rc; rc = host->rc_head)
386   {
387     GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
388     GNUNET_free (rc);
389   }
390   GNUNET_free_non_null ((char *) host->username);
391   GNUNET_free_non_null ((char *) host->hostname);
392   GNUNET_free (host);
393   while (host_list_size >= HOST_LIST_GROW_STEP)
394   {
395     for (id = host_list_size - 1; id > host_list_size - HOST_LIST_GROW_STEP;
396          id--)
397       if (NULL != host_list[id])
398         break;
399     if (id != host_list_size - HOST_LIST_GROW_STEP)
400       break;
401     if (NULL != host_list[id])
402       break;
403     host_list_size -= HOST_LIST_GROW_STEP;
404   }
405   host_list =
406       GNUNET_realloc (host_list,
407                       sizeof (struct GNUNET_TESTBED_Host *) * host_list_size);
408 }
409
410
411 /**
412  * Marks a host as registered with a controller
413  *
414  * @param host the host to mark
415  * @param controller the controller at which this host is registered
416  */
417 void
418 GNUNET_TESTBED_mark_host_registered_at_ (struct GNUNET_TESTBED_Host *host,
419                                          const struct GNUNET_TESTBED_Controller
420                                          *const controller)
421 {
422   struct RegisteredController *rc;
423
424   for (rc = host->rc_head; NULL != rc; rc = rc->next)
425   {
426     if (controller == rc->controller)   /* already registered at controller */
427     {
428       GNUNET_break (0);
429       return;
430     }
431   }
432   rc = GNUNET_malloc (sizeof (struct RegisteredController));
433   rc->controller = controller;
434   GNUNET_CONTAINER_DLL_insert_tail (host->rc_head, host->rc_tail, rc);
435 }
436
437
438 /**
439  * Checks whether a host has been registered
440  *
441  * @param host the host to check
442  * @param controller the controller at which host's registration is checked
443  * @return GNUNET_YES if registered; GNUNET_NO if not
444  */
445 int
446 GNUNET_TESTBED_is_host_registered_ (const struct GNUNET_TESTBED_Host *host,
447                                     const struct GNUNET_TESTBED_Controller
448                                     *const controller)
449 {
450   struct RegisteredController *rc;
451
452   for (rc = host->rc_head; NULL != rc; rc = rc->next)
453   {
454     if (controller == rc->controller)   /* already registered at controller */
455     {
456       return GNUNET_YES;
457     }
458   }
459   return GNUNET_NO;
460 }
461
462
463 /**
464  * The handle for whether a host is habitable or not
465  */
466 struct GNUNET_TESTBED_HostHabitableCheckHandle
467 {
468   /**
469    * The host to check
470    */
471   const struct GNUNET_TESTBED_Host *host;
472
473   /* /\** */
474   /*  * the configuration handle to lookup the path of the testbed helper */
475   /*  *\/ */
476   /* const struct GNUNET_CONFIGURATION_Handle *cfg; */
477
478   /**
479    * The callback to call once we have the status
480    */
481   GNUNET_TESTBED_HostHabitableCallback cb;
482
483   /**
484    * The callback closure
485    */
486   void *cb_cls;
487
488   /**
489    * The process handle for the SSH process
490    */
491   struct GNUNET_OS_Process *auxp;
492
493   /**
494    * The SSH destination address string
495    */
496   char *ssh_addr;
497
498   /**
499    * The destination port string
500    */
501   char *portstr;
502
503   /**
504    * The path for hte testbed helper binary
505    */
506   char *helper_binary_path;
507
508   /**
509    * Task id for the habitability check task
510    */
511   GNUNET_SCHEDULER_TaskIdentifier habitability_check_task;
512
513   /**
514    * How long we wait before checking the process status. Should grow
515    * exponentially
516    */
517   struct GNUNET_TIME_Relative wait_time;
518
519 };
520
521
522 /**
523  * Task for checking whether a host is habitable or not
524  *
525  * @param cls GNUNET_TESTBED_HostHabitableCheckHandle
526  * @param tc the scheduler task context
527  */
528 static void
529 habitability_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
530 {
531   struct GNUNET_TESTBED_HostHabitableCheckHandle *h = cls;
532   void *cb_cls;
533   GNUNET_TESTBED_HostHabitableCallback cb;
534   const struct GNUNET_TESTBED_Host *host;
535   unsigned long code;
536   enum GNUNET_OS_ProcessStatusType type;
537   int ret;
538
539   h->habitability_check_task = GNUNET_SCHEDULER_NO_TASK;
540   ret = GNUNET_OS_process_status (h->auxp, &type, &code);
541   if (GNUNET_SYSERR == ret)
542   {
543     GNUNET_break (0);
544     ret = GNUNET_NO;
545     goto call_cb;
546   }
547   if (GNUNET_NO == ret)
548   {
549     h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
550     h->habitability_check_task =
551         GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
552     return;
553   }
554   GNUNET_OS_process_destroy (h->auxp);
555   h->auxp = NULL;
556   ret = (0 != code) ? GNUNET_NO : GNUNET_YES;
557
558 call_cb:
559   GNUNET_free (h->ssh_addr);
560   GNUNET_free (h->portstr);
561   GNUNET_free (h->helper_binary_path);
562   if (NULL != h->auxp)
563     GNUNET_OS_process_destroy (h->auxp);
564   cb = h->cb;
565   cb_cls = h->cb_cls;
566   host = h->host;
567   GNUNET_free (h);
568   if (NULL != cb)
569     cb (cb_cls, host, ret);
570 }
571
572
573 /**
574  * Checks whether a host can be used to start testbed service
575  *
576  * @param host the host to check
577  * @param config the configuration handle to lookup the path of the testbed
578  *          helper
579  * @param cb the callback to call to inform about habitability of the given host
580  * @param cb_cls the closure for the callback
581  * @return NULL upon any error or a handle which can be passed to
582  *           GNUNET_TESTBED_is_host_habitable_cancel()
583  */
584 struct GNUNET_TESTBED_HostHabitableCheckHandle *
585 GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
586                                   const struct GNUNET_CONFIGURATION_Handle
587                                   *config,
588                                   GNUNET_TESTBED_HostHabitableCallback cb,
589                                   void *cb_cls)
590 {
591   struct GNUNET_TESTBED_HostHabitableCheckHandle *h;
592   char *remote_args[11];
593   const char *hostname;
594   unsigned int argp;
595
596   h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostHabitableCheckHandle));
597   h->cb = cb;
598   h->cb_cls = cb_cls;
599   h->host = host;
600   hostname = (NULL == host->hostname) ? "127.0.0.1" : host->hostname;
601   if (NULL == host->username)
602     h->ssh_addr = GNUNET_strdup (hostname);
603   else
604     GNUNET_asprintf (&h->ssh_addr, "%s@%s", host->username, hostname);
605   if (GNUNET_OK !=
606       GNUNET_CONFIGURATION_get_value_string (config, "testbed",
607                                              "HELPER_BINARY_PATH",
608                                              &h->helper_binary_path))
609     h->helper_binary_path =
610         GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
611   argp = 0;
612   remote_args[argp++] = "ssh";
613   GNUNET_asprintf (&h->portstr, "%u", host->port);
614   remote_args[argp++] = "-p";
615   remote_args[argp++] = h->portstr;
616   remote_args[argp++] = "-o";
617   remote_args[argp++] = "BatchMode=yes";
618   remote_args[argp++] = "-o";
619   remote_args[argp++] = "NoHostAuthenticationForLocalhost=yes";
620   remote_args[argp++] = h->ssh_addr;
621   remote_args[argp++] = "stat";
622   remote_args[argp++] = h->helper_binary_path;
623   remote_args[argp++] = NULL;
624   GNUNET_assert (argp == 11);
625   h->auxp =
626       GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, NULL,
627                                    NULL, "ssh", remote_args);
628   if (NULL == h->auxp)
629   {
630     GNUNET_break (0);           /* Cannot exec SSH? */
631     GNUNET_free (h->ssh_addr);
632     GNUNET_free (h->portstr);
633     GNUNET_free (h->helper_binary_path);
634     GNUNET_free (h);
635     return NULL;
636   }
637   h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
638   h->habitability_check_task =
639       GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
640   return h;
641 }
642
643
644 /**
645  * Function to cancel a request started using GNUNET_TESTBED_is_host_habitable()
646  *
647  * @param handle the habitability check handle
648  */
649 void
650 GNUNET_TESTBED_is_host_habitable_cancel (struct
651                                          GNUNET_TESTBED_HostHabitableCheckHandle
652                                          *handle)
653 {
654   GNUNET_SCHEDULER_cancel (handle->habitability_check_task);
655   (void) GNUNET_OS_process_kill (handle->auxp, SIGTERM);
656   (void) GNUNET_OS_process_wait (handle->auxp);
657   GNUNET_OS_process_destroy (handle->auxp);
658   GNUNET_free (handle->ssh_addr);
659   GNUNET_free (handle->portstr);
660   GNUNET_free (handle->helper_binary_path);
661   GNUNET_free (handle);
662 }
663
664 /* end of testbed_api_hosts.c */