fd129283e4d47192d0b4502d0d9d128234a28b87
[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_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"
35
36 #include "testbed_api.h"
37 #include "testbed_api_hosts.h"
38
39 /**
40  * Generic logging shorthand
41  */
42 #define LOG(kind, ...)                          \
43   GNUNET_log_from (kind, "testbed-api-hosts", __VA_ARGS__);
44
45 /**
46  * Number of extra elements we create space for when we grow host list
47  */
48 #define HOST_LIST_GROW_STEP 10
49
50
51 /**
52  * A list entry for registered controllers list
53  */
54 struct RegisteredController
55 {
56   /**
57    * The controller at which this host is registered
58    */
59   const struct GNUNET_TESTBED_Controller *controller;
60   
61   /**
62    * The next ptr for DLL
63    */
64   struct RegisteredController *next;
65
66   /**
67    * The prev ptr for DLL
68    */
69   struct RegisteredController *prev;
70 };
71
72
73 /**
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
76  * ssh-agent).
77  */
78 struct GNUNET_TESTBED_Host
79 {
80
81   /**
82    * The next pointer for DLL
83    */
84   struct GNUNET_TESTBED_Host *next;
85
86   /**
87    * The prev pointer for DLL
88    */
89   struct GNUNET_TESTBED_Host *prev;
90
91   /**
92    * The hostname of the host; NULL for localhost
93    */
94   const char *hostname;
95
96   /**
97    * The username to be used for SSH login
98    */
99   const char *username;
100
101   /**
102    * The head for the list of controllers where this host is registered
103    */
104   struct RegisteredController *rc_head;
105
106   /**
107    * The tail for the list of controllers where this host is registered
108    */
109   struct RegisteredController *rc_tail;
110
111   /**
112    * Global ID we use to refer to a host on the network
113    */
114   uint32_t id;
115
116   /**
117    * The port which is to be used for SSH
118    */
119   uint16_t port;
120
121 };
122
123
124 /**
125  * Array of available hosts
126  */
127 static struct GNUNET_TESTBED_Host **host_list;
128
129 /**
130  * The size of the available hosts list
131  */
132 static uint32_t host_list_size;
133
134
135 /**
136  * Lookup a host by ID.
137  * 
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
141  */
142 struct GNUNET_TESTBED_Host *
143 GNUNET_TESTBED_host_lookup_by_id_ (uint32_t id)
144 {
145   if (host_list_size <= id)
146     return NULL;
147   return host_list[id];
148 }
149
150
151 /**
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
154  * internally.
155  * 
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
159  */
160 struct GNUNET_TESTBED_Host *
161 GNUNET_TESTBED_host_create_by_id_ (uint32_t id)
162 {
163   return GNUNET_TESTBED_host_create_with_id (id, NULL, NULL, 0);
164 }
165
166
167 /**
168  * Obtain the host's unique global ID.
169  * 
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)
173  */
174 uint32_t
175 GNUNET_TESTBED_host_get_id_ (const struct GNUNET_TESTBED_Host *host)
176 {
177   return host->id;
178 }
179
180
181 /**
182  * Obtain the host's hostname.
183  * 
184  * @param host handle to the host, NULL means 'localhost'
185  * @return hostname of the host
186  */
187 const char *
188 GNUNET_TESTBED_host_get_hostname_ (const struct GNUNET_TESTBED_Host *host)
189 {
190   return host->hostname;
191 }
192
193
194 /**
195  * Obtain the host's username
196  * 
197  * @param host handle to the host, NULL means 'localhost'
198  * @return username to login to the host
199  */
200 const char *
201 GNUNET_TESTBED_host_get_username_ (const struct GNUNET_TESTBED_Host *host)
202 {
203   return host->username;
204 }
205
206
207 /**
208  * Obtain the host's ssh port
209  * 
210  * @param host handle to the host, NULL means 'localhost'
211  * @return username to login to the host
212  */
213 uint16_t
214 GNUNET_TESTBED_host_get_ssh_port_ (const struct GNUNET_TESTBED_Host *host)
215 {
216   return host->port;
217 }
218
219
220 /**
221  * Create a host to run peers and controllers on.
222  * 
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
229  */
230 struct GNUNET_TESTBED_Host *
231 GNUNET_TESTBED_host_create_with_id (uint32_t id,
232                                     const char *hostname,
233                                     const char *username,
234                                     uint16_t port)
235 {
236   struct GNUNET_TESTBED_Host *host;
237
238   if ((id < host_list_size) && (NULL != host_list[id]))
239   {
240     LOG (GNUNET_ERROR_TYPE_WARNING, "Host with id: %u already created\n");
241     return NULL;
242   }
243   host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
244   host->hostname = hostname;
245   host->username = username;
246   host->id = id;
247   host->port = (0 == port) ? 22 : port;
248   if (id >= host_list_size)
249   {
250     host_list_size += HOST_LIST_GROW_STEP;
251     host_list = GNUNET_realloc (host_list, sizeof (struct GNUNET_TESTBED_Host)
252                                 * host_list_size);
253     (void) memset(&host_list[host_list_size - HOST_LIST_GROW_STEP],
254                   0, sizeof (struct GNUNET_TESTBED_Host) * host_list_size);
255   }
256   LOG (GNUNET_ERROR_TYPE_DEBUG,
257        "Adding host with id: %u\n", host->id);
258   host_list[id] = host;
259   return host;
260 }
261
262
263 /**
264  * Create a host to run peers and controllers on.
265  * 
266  * @param hostname name of the host, use "NULL" for localhost
267  * @param username username to use for the login; may be NULL
268  * @param port port number to use for ssh; use 0 to let ssh decide
269  * @return handle to the host, NULL on error
270  */
271 struct GNUNET_TESTBED_Host *
272 GNUNET_TESTBED_host_create (const char *hostname,
273                             const char *username,
274                             uint16_t port)
275 {
276   static uint32_t uid_generator;
277
278   if (NULL == hostname)
279     return GNUNET_TESTBED_host_create_with_id (0, hostname, username, port);
280   return GNUNET_TESTBED_host_create_with_id (++uid_generator, 
281                                              hostname, username,
282                                              port);
283 }
284
285
286 /**
287  * Load a set of hosts from a configuration file.
288  *
289  * @param filename file with the host specification
290  * @param hosts set to the hosts found in the file
291  * @return number of hosts returned in 'hosts', 0 on error
292  */
293 unsigned int
294 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
295                                      struct GNUNET_TESTBED_Host **hosts)
296 {
297   // see testing_group.c, GNUNET_TESTING_hosts_load
298   GNUNET_break (0);
299   return 0;
300 }
301
302
303 /**
304  * Destroy a host handle.  Must only be called once everything
305  * running on that host has been stopped.
306  *
307  * @param host handle to destroy
308  */
309 void
310 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
311 {  
312   struct RegisteredController *rc;
313   uint32_t id;
314
315   GNUNET_assert (host->id < host_list_size);
316   GNUNET_assert (host_list[host->id] == host);
317   host_list[host->id] = NULL;
318   /* clear registered controllers list */
319   for (rc=host->rc_head; NULL != rc; rc=host->rc_head)
320   {
321     GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
322     GNUNET_free (rc);
323   }
324   for (id = 0; id < HOST_LIST_GROW_STEP; id++)
325   {
326     if (((host->id + id) >= host_list_size) || 
327         (NULL != host_list[host->id + id]))
328       break;
329   }
330   if (HOST_LIST_GROW_STEP == id)
331   {
332     host_list_size -= HOST_LIST_GROW_STEP;
333     host_list = GNUNET_realloc (host_list, host_list_size);
334   }
335   GNUNET_free (host);
336 }
337
338
339 /**
340  * Run a given helper process at the given host.  Communication
341  * with the helper will be via GNUnet messages on stdin/stdout.
342  * Runs the process via 'ssh' at the specified host, or locally.
343  * Essentially an SSH-wrapper around the 'gnunet_helper_lib.h' API.
344  * 
345  * @param host host to use, use "NULL" for localhost
346  * @param binary_argv binary name and command-line arguments to give to the binary
347  * @return handle to terminate the command, NULL on error
348  */
349 struct GNUNET_TESTBED_HelperHandle *
350 GNUNET_TESTBED_host_run_ (const struct GNUNET_TESTBED_Host *host,
351                           char *const binary_argv[])
352 {
353   struct GNUNET_TESTBED_HelperHandle *h;
354   unsigned int argc;
355
356   argc = 0;
357   while (NULL != binary_argv[argc]) 
358     argc++;
359   h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HelperHandle));
360   h->cpipe_in = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
361   h->cpipe_out = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_YES);
362   if ((NULL == h->cpipe_in) || (NULL == h->cpipe_out))
363   {
364     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
365                          "pipe");
366     GNUNET_free (h);
367     return NULL;
368   }
369   if ((NULL == host) || (0 == host->id))
370   {
371     h->process = GNUNET_OS_start_process_vap (GNUNET_YES,
372                                               GNUNET_OS_INHERIT_STD_ALL,
373                                               h->cpipe_in, h->cpipe_out,
374                                               "gnunet-service-testbed", 
375                                               binary_argv);
376   }
377   else
378   {
379     char *remote_args[argc + 6 + 1];
380     unsigned int argp;
381
382     GNUNET_asprintf (&h->port, "%d", host->port);
383     if (NULL == host->username)
384       GNUNET_asprintf (&h->dst, "%s", host->hostname);
385     else 
386       GNUNET_asprintf (&h->dst, "%s@%s", host->hostname, host->username);
387     argp = 0;
388     remote_args[argp++] = "ssh";
389     remote_args[argp++] = "-p";
390     remote_args[argp++] = h->port;
391     remote_args[argp++] = "-q";
392     remote_args[argp++] = h->dst;
393     remote_args[argp++] = "gnunet-service-testbed";
394     while (NULL != binary_argv[argp-6])
395     {
396       remote_args[argp] = binary_argv[argp - 6];
397       argp++;
398     } 
399     remote_args[argp++] = NULL;
400     GNUNET_assert (argp == argc + 6 + 1);
401     h->process = GNUNET_OS_start_process_vap (GNUNET_YES,
402                                               GNUNET_OS_INHERIT_STD_ALL,
403                                               h->cpipe_in, NULL,
404                                               "ssh", 
405                                               remote_args);
406   }
407   if (NULL == h->process)
408   {
409     GNUNET_break (GNUNET_OK == GNUNET_DISK_pipe_close (h->cpipe_in));
410     GNUNET_free_non_null (h->port);
411     GNUNET_free_non_null (h->dst);
412     GNUNET_free (h);
413     return NULL;
414   } 
415   GNUNET_break (GNUNET_OK == GNUNET_DISK_pipe_close_end (h->cpipe_in, GNUNET_DISK_PIPE_END_READ));
416   return h;
417 }
418
419
420 /**
421  * Stops a helper in the HelperHandle using GNUNET_HELPER_stop
422  *
423  * @param handle the handle returned from GNUNET_TESTBED_host_start_
424  */
425 void
426 GNUNET_TESTBED_host_stop_ (struct GNUNET_TESTBED_HelperHandle *handle)
427 {
428   GNUNET_break (GNUNET_OK == GNUNET_DISK_pipe_close (handle->cpipe_in));
429   GNUNET_break (GNUNET_OK == GNUNET_DISK_pipe_close (handle->cpipe_out));
430   GNUNET_break (0 == GNUNET_OS_process_kill (handle->process, SIGTERM));
431   GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (handle->process));
432   GNUNET_OS_process_destroy (handle->process);
433   GNUNET_free_non_null (handle->port);
434   GNUNET_free_non_null (handle->dst);
435   GNUNET_free (handle);
436 }
437
438
439 /**
440  * Marks a host as registered with a controller
441  *
442  * @param host the host to mark
443  * @param controller the controller at which this host is registered
444  */
445 void
446 GNUNET_TESTBED_mark_host_registered_at_ (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       GNUNET_break (0);
457       return;
458     }
459   }
460   rc = GNUNET_malloc (sizeof (struct RegisteredController));
461   rc->controller = controller;
462   //host->controller = controller;
463   GNUNET_CONTAINER_DLL_insert_tail (host->rc_head, host->rc_tail, rc);
464 }
465
466
467 /**
468  * Checks whether a host has been registered
469  *
470  * @param host the host to check
471  * @param controller the controller at which host's registration is checked
472  * @return GNUNET_YES if registered; GNUNET_NO if not
473  */
474 int
475 GNUNET_TESTBED_is_host_registered_ (const struct GNUNET_TESTBED_Host *host,
476                                     const struct GNUNET_TESTBED_Controller
477                                     *const controller)
478 {
479   struct RegisteredController *rc;
480   
481   for (rc=host->rc_head; NULL != rc; rc=rc->next)
482   {
483     if (controller == rc->controller) /* already registered at controller */
484     {
485       return GNUNET_YES;
486     }
487   }
488   return GNUNET_NO;
489 }
490
491
492 /**
493  * Creates a helper initialization message. Only for testing.
494  *
495  * @param cname the ip address of the controlling host
496  * @param cfg the configuration that has to used to start the testbed service
497  *          thru helper
498  * @return the initialization message
499  */
500 struct GNUNET_TESTBED_HelperInit *
501 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
502                                          const struct GNUNET_CONFIGURATION_Handle *cfg)
503 {
504   struct GNUNET_TESTBED_HelperInit *msg;
505   char *config;
506   char *xconfig;
507   size_t config_size;
508   size_t xconfig_size;
509   uint16_t cname_len;
510   uint16_t msg_size;
511
512   config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
513   GNUNET_assert (NULL != config);
514   xconfig_size =
515     GNUNET_TESTBED_compress_config (config, config_size, &xconfig);
516   GNUNET_free (config);
517   cname_len = strlen (cname);
518   msg_size = xconfig_size + cname_len + 1 + 
519     sizeof (struct GNUNET_TESTBED_HelperInit);
520   msg = GNUNET_realloc (xconfig, msg_size);
521   (void) memmove ( ((void *) &msg[1]) + cname_len + 1, msg, xconfig_size);
522   msg->header.size = htons (msg_size);
523   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
524   msg->cname_size = htons (cname_len);
525   msg->config_size = htons (config_size);
526   (void) strcpy ((char *) &msg[1], cname);
527   return msg;
528 }
529
530
531 /* end of testbed_api_hosts.c */