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