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