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