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