c4dfcf3c9e586ce27a139575ff08ffcc73d39a96
[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    * Global ID we use to refer to a host on the network
68    */
69   uint32_t unique_id;
70
71   /**
72    * The port which is to be used for SSH
73    */
74   uint16_t port;
75 };
76
77
78 /**
79  * Head element in the list of available hosts
80  */
81 static struct GNUNET_TESTBED_Host *host_list_head;
82
83 /**
84  * Tail element in the list of available hosts
85  */
86 static struct GNUNET_TESTBED_Host *host_list_tail;
87
88
89 /**
90  * Lookup a host by ID.
91  * 
92  * @param id global host ID assigned to the host; 0 is
93  *        reserved to always mean 'localhost'
94  * @return handle to the host, NULL if host not found
95  */
96 struct GNUNET_TESTBED_Host *
97 GNUNET_TESTBED_host_lookup_by_id_ (uint32_t id)
98 {
99   struct GNUNET_TESTBED_Host *host;
100
101   for (host = host_list_head; NULL != host; host=host->next)
102     if (id == host->unique_id)
103       return host;
104   return NULL;
105 }
106
107
108 /**
109  * Create a host by ID; given this host handle, we could not
110  * run peers at the host, but we can talk about the host
111  * internally.
112  * 
113  * @param id global host ID assigned to the host; 0 is
114  *        reserved to always mean 'localhost'
115  * @return handle to the host, NULL on error
116  */
117 struct GNUNET_TESTBED_Host *
118 GNUNET_TESTBED_host_create_by_id_ (uint32_t id)
119 {
120   GNUNET_break (0);
121   return NULL;
122 }
123
124
125 /**
126  * Obtain a host's unique global ID.
127  * 
128  * @param host handle to the host, NULL means 'localhost'
129  * @return id global host ID assigned to the host (0 is
130  *         'localhost', but then obviously not globally unique)
131  */
132 uint32_t
133 GNUNET_TESTBED_host_get_id_ (const struct GNUNET_TESTBED_Host *host)
134 {
135   return host->unique_id;
136 }
137
138
139 /**
140  * Create a host to run peers and controllers on.
141  * 
142  * @param id global host ID assigned to the host; 0 is
143  *        reserved to always mean 'localhost'
144  * @param hostname name of the host, use "NULL" for localhost
145  * @param username username to use for the login; may be NULL
146  * @param port port number to use for ssh; use 0 to let ssh decide
147  * @return handle to the host, NULL on error
148  */
149 struct GNUNET_TESTBED_Host *
150 GNUNET_TESTBED_host_create_with_id (uint32_t id,
151                                     const char *hostname,
152                                     const char *username,
153                                     uint16_t port)
154 {
155   struct GNUNET_TESTBED_Host *host;
156
157   host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
158   host->hostname = hostname;
159   host->username = username;
160   host->unique_id = id;
161   host->port = (0 == port) ? 22 : port;
162   GNUNET_CONTAINER_DLL_insert_tail (host_list_head, host_list_tail, host);
163   return host;
164 }
165
166
167 /**
168  * Create a host to run peers and controllers on.
169  * 
170  * @param hostname name of the host, use "NULL" for localhost
171  * @param username username to use for the login; may be NULL
172  * @param port port number to use for ssh; use 0 to let ssh decide
173  * @return handle to the host, NULL on error
174  */
175 struct GNUNET_TESTBED_Host *
176 GNUNET_TESTBED_host_create (const char *hostname,
177                             const char *username,
178                             uint16_t port)
179 {
180   static uint32_t uid_generator;
181
182   if (NULL == hostname)
183     return GNUNET_TESTBED_host_create_with_id (0, hostname, username, port);
184   return GNUNET_TESTBED_host_create_with_id (++uid_generator, 
185                                              hostname, username,
186                                              port);
187 }
188
189
190 /**
191  * Load a set of hosts from a configuration file.
192  *
193  * @param filename file with the host specification
194  * @param hosts set to the hosts found in the file
195  * @return number of hosts returned in 'hosts', 0 on error
196  */
197 unsigned int
198 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
199                                      struct GNUNET_TESTBED_Host **hosts)
200 {
201   // see testing_group.c, GNUNET_TESTING_hosts_load
202   GNUNET_break (0);
203   return 0;
204 }
205
206
207 /**
208  * Destroy a host handle.  Must only be called once everything
209  * running on that host has been stopped.
210  *
211  * @param host handle to destroy
212  */
213 void
214 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
215 {  
216   GNUNET_CONTAINER_DLL_remove (host_list_head, host_list_tail, host);
217   GNUNET_free (host);
218 }
219
220
221 /**
222  * Wrapper around GNUNET_HELPER_Handle
223  */
224 struct GNUNET_TESTBED_HelperHandle
225 {
226   /**
227    * The process handle
228    */
229   struct GNUNET_OS_Process *process;
230
231   /**
232    * Pipe connecting to stdin of the process.
233    */
234   struct GNUNET_DISK_PipeHandle *cpipe;
235
236   /**
237    * The port number for ssh; used for helpers starting ssh
238    */
239   char *port;
240
241   /**
242    * The ssh destination string; used for helpers starting ssh
243    */
244   char *dst; 
245 };
246
247
248 /**
249  * Run a given helper process at the given host.  Communication
250  * with the helper will be via GNUnet messages on stdin/stdout.
251  * Runs the process via 'ssh' at the specified host, or locally.
252  * Essentially an SSH-wrapper around the 'gnunet_helper_lib.h' API.
253  * 
254  * @param host host to use, use "NULL" for localhost
255  * @param binary_argv binary name and command-line arguments to give to the binary
256  * @return handle to terminate the command, NULL on error
257  */
258 struct GNUNET_TESTBED_HelperHandle *
259 GNUNET_TESTBED_host_run_ (const struct GNUNET_TESTBED_Host *host,
260                           char *const binary_argv[])
261 {
262   struct GNUNET_TESTBED_HelperHandle *h;
263   unsigned int argc;
264
265   argc = 0;
266   while (NULL != binary_argv[argc]) 
267     argc++;
268   h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HelperHandle));
269   h->cpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
270   if (0 == host->unique_id)
271   {
272     h->process = GNUNET_OS_start_process_vap (GNUNET_YES,
273                                               h->cpipe, NULL,
274                                               "gnunet-service-testbed", 
275                                               binary_argv);
276   }
277   else
278   {    
279     char *remote_args[argc + 6 + 1];
280     unsigned int argp;
281
282     GNUNET_asprintf (&h->port, "%d", host->port);
283     GNUNET_asprintf (&h->dst, "%s@%s", host->hostname, host->username);
284     argp = 0;
285     remote_args[argp++] = "ssh";
286     remote_args[argp++] = "-p";
287     remote_args[argp++] = h->port;
288     remote_args[argp++] = "-q";
289     remote_args[argp++] = h->dst;
290     remote_args[argp++] = "gnunet-service-testbed";
291     while (NULL != binary_argv[argp-6])
292     {
293       remote_args[argp] = binary_argv[argp - 6];
294       argp++;
295     } 
296     remote_args[argp++] = NULL;
297     GNUNET_assert (argp == argc + 6 + 1);
298     h->process = GNUNET_OS_start_process_vap (GNUNET_YES,
299                                               h->cpipe, NULL,
300                                               "ssh", 
301                                               remote_args);
302   }
303   if (NULL == h->process)
304   {
305     GNUNET_break (GNUNET_OK == GNUNET_DISK_pipe_close (h->cpipe));
306     GNUNET_free_non_null (h->port);
307     GNUNET_free_non_null (h->dst);
308     GNUNET_free (h);
309     return NULL;
310   } 
311   GNUNET_break (GNUNET_OK == GNUNET_DISK_pipe_close_end (h->cpipe, GNUNET_DISK_PIPE_END_READ));
312   return h;
313 }
314
315
316 /**
317  * Stops a helper in the HelperHandle using GNUNET_HELPER_stop
318  *
319  * @param handle the handle returned from GNUNET_TESTBED_host_start_
320  */
321 void
322 GNUNET_TESTBED_host_stop_ (struct GNUNET_TESTBED_HelperHandle *handle)
323 {
324   GNUNET_break (GNUNET_OK == GNUNET_DISK_pipe_close (handle->cpipe));
325   GNUNET_break (0 == GNUNET_OS_process_kill (handle->process, SIGTERM));
326   GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (handle->process));
327   GNUNET_OS_process_destroy (handle->process);
328   GNUNET_free_non_null (handle->port);
329   GNUNET_free_non_null (handle->dst);
330   GNUNET_free (handle);
331 }
332
333 /* end of testbed_api_hosts.c */