init message handling
[oweals/gnunet.git] / src / testbed / gnunet-testbed-helper.c
1 /*
2       This file is part of GNUnet
3       (C) 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/gnunet-testbed-helper.c
23  * @brief Helper binary that is started from a remote controller to start
24  *          gnunet-service-testbed. This binary also receives configuration
25  *          from the remove controller which is put in a temporary location
26  *          with ports and paths fixed so that gnunet-service-testbed runs
27  *          without any hurdels. This binary also kills the testbed service
28  *          should the connection from the remote controller is dropped
29  * @author Sree Harsha Totakura <sreeharsha@totakura.in> 
30  */
31 #include "platform.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_testing_lib-new.h"
34 #include "testbed_helper.h"
35 #include <zlib.h>
36
37 /**
38  * Generic logging shortcut
39  */
40 #define LOG(kind, ...)                                   \
41   GNUNET_log (kind, __VA_ARGS__)
42
43 /**
44  * Debug logging shorthand
45  */
46 #define LOG_DEBUG(...)                          \
47   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
48
49 /**
50  * Handle to the testing system
51  */
52 static struct GNUNET_TESTING_System *test_system;
53
54 /**
55  * Our message stream tokenizer
56  */
57 struct GNUNET_SERVER_MessageStreamTokenizer *tokenizer;
58
59 /**
60  * Disk handle from stdin
61  */
62 static struct GNUNET_DISK_FileHandle *stdin_fd;
63
64 /**
65  * The process handle to the testbed service
66  */
67 static struct GNUNET_OS_Process *testbed;
68
69 /**
70  * Pipe handle to child's stdin
71  */
72 static struct GNUNET_DISK_PipeHandle *pipe_in;
73
74 /**
75  * Pipe handle to child's stdout
76  */
77 static struct GNUNET_DISK_PipeHandle *pipe_out;
78
79 /**
80  * Task identifier for the read task
81  */
82 static GNUNET_SCHEDULER_TaskIdentifier read_task_id;
83
84 /**
85  * Are we done reading messages from stdin?
86  */
87 static int done_reading;
88
89 /**
90  * Result to return in case we fail
91  */
92 static int ret;
93
94
95 /**
96  * Task to shutting down nicely
97  *
98  * @param cls NULL
99  * @return tc the task context
100  */
101 static void
102 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 {
104   if (GNUNET_SCHEDULER_NO_TASK != read_task_id)
105   {
106     GNUNET_SCHEDULER_cancel (read_task_id);
107     read_task_id = GNUNET_SCHEDULER_NO_TASK;
108   }
109   (void) GNUNET_DISK_file_close (stdin_fd);
110   GNUNET_SERVER_mst_destroy (tokenizer);  
111   tokenizer = NULL;
112   if (NULL != test_system)
113   {
114     GNUNET_TESTING_system_destroy (test_system, GNUNET_YES);
115     test_system = NULL;
116   }
117 }
118
119
120 /**
121  * Functions with this signature are called whenever a
122  * complete message is received by the tokenizer.
123  *
124  * Do not call GNUNET_SERVER_mst_destroy in callback
125  *
126  * @param cls closure
127  * @param client identification of the client
128  * @param message the actual message
129  *
130  * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
131  */
132 static int 
133 tokenizer_cb (void *cls, void *client,
134               const struct GNUNET_MessageHeader *message)
135 {
136   const struct GNUNET_TESTBED_HelperInit *msg;
137   struct GNUNET_CONFIGURATION_Handle *cfg;
138   char *controller;
139   char *config;
140   uLongf config_size;
141   uint16_t xconfig_size;
142   uint16_t cname_size;
143
144   if ((sizeof (struct GNUNET_TESTBED_HelperInit) >= ntohs (message->size)) ||
145       (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT != ntohs (message->type)))
146   {
147     LOG (GNUNET_ERROR_TYPE_WARNING,
148          "Received unexpected message -- exiting\n");
149     goto error;
150   }
151   msg = (const struct GNUNET_TESTBED_HelperInit *) message;
152   cname_size = ntohs (msg->cname_size);
153   controller = (char *) &msg[1];
154   if ('\0' != controller[cname_size])
155   {
156     LOG (GNUNET_ERROR_TYPE_WARNING, 
157          "Controller name cannot be empty -- exiting\n");
158     goto error;
159   }
160   config_size = (uLongf) ntohs (msg->config_size);
161   config = GNUNET_malloc (config_size);
162   xconfig_size = ntohs (message->size) - (cname_size + 1);
163   if (Z_OK != uncompress ((Bytef *) config, &config_size,
164                           (const Bytef *) (controller + cname_size + 1),
165                           (uLongf) xconfig_size))
166   {
167     LOG (GNUNET_ERROR_TYPE_WARNING, 
168          "Error while uncompressing config -- exiting\n");
169     GNUNET_free (config);
170     goto error;
171   }
172   cfg = GNUNET_CONFIGURATION_create ();  
173   if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
174                                                      GNUNET_NO))
175   {
176     LOG (GNUNET_ERROR_TYPE_WARNING, 
177          "Unable to deserialize config -- exiting\n");
178     GNUNET_free (config);
179     goto error;
180   }
181   GNUNET_free (config);
182   test_system = GNUNET_TESTING_system_create ("testbed-helper", controller);
183   GNUNET_assert (NULL != test_system);
184   GNUNET_assert (GNUNET_OK ==  GNUNET_TESTING_configuration_create
185                  (test_system, cfg));
186   pipe_in = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
187   pipe_out = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_YES);
188   if ((NULL == pipe_in) || (NULL == pipe_out))
189   {
190     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe");
191     GNUNET_free (config);
192     goto error;
193   }  
194   testbed = GNUNET_OS_start_process 
195     (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR /*verbose? */, pipe_in, pipe_out,
196      "gnunet-service-testbed", "gnunet-service-testbed", "-c", config, NULL);
197   if (NULL == testbed)
198   {
199     LOG (GNUNET_ERROR_TYPE_WARNING, 
200          "Unable to deserialize config -- exiting\n");
201     goto error;
202   }
203   GNUNET_DISK_pipe_close_end (pipe_out, GNUNET_DISK_PIPE_END_WRITE);
204   GNUNET_DISK_pipe_close_end (pipe_in, GNUNET_DISK_PIPE_END_READ);
205   done_reading = GNUNET_YES;
206   return GNUNET_OK;
207   
208  error:
209   ret = GNUNET_SYSERR;
210   GNUNET_SCHEDULER_shutdown ();
211   return GNUNET_SYSERR;
212 }
213
214
215 /**
216  * Task to read from stdin
217  *
218  * @param cls NULL
219  * @return tc the task context
220  */
221 static void
222 read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
223 {
224   char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
225   ssize_t sread;
226
227   read_task_id = GNUNET_SCHEDULER_NO_TASK;
228   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
229     return;  
230   sread = GNUNET_DISK_file_read (stdin_fd, buf, sizeof (buf));
231   if (GNUNET_SYSERR == sread)
232   {
233     GNUNET_break (0);           /* FIXME: stdin closed - kill child */
234     GNUNET_SCHEDULER_shutdown ();
235     return;
236   }
237   if (GNUNET_YES == done_reading)
238   {
239     /* didn't expect any more data! */
240     GNUNET_break (0);
241     GNUNET_SCHEDULER_shutdown ();
242     return;
243   }
244   LOG_DEBUG ("Read %u bytes\n", sread);
245   if (GNUNET_OK !=
246       GNUNET_SERVER_mst_receive (tokenizer, NULL, buf, sread,
247                                  GNUNET_NO, GNUNET_NO))
248   {
249     GNUNET_break (0);
250     GNUNET_SCHEDULER_shutdown ();
251     return;
252   }
253   read_task_id =                /* No timeout while reading */
254     GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
255                                     stdin_fd, &read_task, NULL);
256 }
257
258
259 /**
260  * Main function that will be run.
261  *
262  * @param cls closure
263  * @param args remaining command-line arguments
264  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
265  * @param cfg configuration
266  */
267 static void 
268 run (void *cls, char *const *args, const char *cfgfile,
269      const struct GNUNET_CONFIGURATION_Handle * cfg)
270 {
271   LOG_DEBUG ("Starting testbed helper...\n");
272   tokenizer = GNUNET_SERVER_mst_create (&tokenizer_cb, NULL);
273   stdin_fd = GNUNET_DISK_get_handle_from_native (stdin);
274   read_task_id =
275     GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
276                                     stdin_fd, &read_task, NULL);
277   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
278                                 &shutdown_task, NULL);
279 }
280
281
282 /**
283  * Main function
284  *
285  * @param argc the number of command line arguments
286  * @param argv command line arg array
287  * @return return code
288  */
289 int main (int argc, char **argv)
290 {
291   struct GNUNET_GETOPT_CommandLineOption options[] = {
292     GNUNET_GETOPT_OPTION_END
293   };
294
295   ret = GNUNET_OK;
296   if (GNUNET_OK != 
297       GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-helper",
298                           "Helper for starting gnunet-service-testbed",
299                           options, &run, NULL))
300     return 1;
301   return (GNUNET_OK == ret) ? 0 : 1;
302 }
303
304 /* end of gnunet-testbed-helper.c */