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