-test now passes
[oweals/gnunet.git] / src / vpn / test_gnunet_vpn.c
1 /*
2      This file is part of GNUnet
3      (C) 2007, 2009, 2011, 2012 Christian Grothoff
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 2, 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 test_gnunet_vpn.c
23  * @brief testcase for tunneling HTTP over the GNUnet VPN
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include <curl/curl.h>
28 #include <microhttpd.h>
29 #include "gnunet_vpn_service.h"
30 #include "gnunet_arm_service.h"
31
32 #define PORT 48080
33
34 #define START_ARM GNUNET_YES
35
36 #define VERBOSE GNUNET_NO
37
38 struct PeerContext
39 {
40   struct GNUNET_CONFIGURATION_Handle *cfg;
41   struct GNUNET_PeerIdentity id;
42 #if START_ARM
43   struct GNUNET_OS_Process *arm_proc;
44 #endif
45 };
46
47 static struct PeerContext p1;
48
49 /**
50  * Return value for 'main'.
51  */
52 static int global_ret;
53
54 static struct GNUNET_VPN_Handle *vpn;
55
56 static struct MHD_Daemon *mhd;
57
58 static GNUNET_SCHEDULER_TaskIdentifier mhd_task_id;
59
60 static GNUNET_SCHEDULER_TaskIdentifier curl_task_id;
61
62 static GNUNET_SCHEDULER_TaskIdentifier ctrl_c_task_id;
63
64 static struct GNUNET_VPN_RedirectionRequest *rr;
65
66 static CURL *curl;
67
68 static CURLM *multi;
69
70 static char *url;
71
72 struct CBC
73 {
74   char buf[1024];
75   size_t pos;
76 };
77
78 static struct CBC cbc;
79
80
81
82 static size_t
83 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
84 {
85   struct CBC *cbc = ctx;
86
87   if (cbc->pos + size * nmemb > sizeof(cbc->buf))
88     return 0;                   /* overflow */
89   memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
90   cbc->pos += size * nmemb;
91   return size * nmemb;
92 }
93
94
95 static int
96 mhd_ahc (void *cls,
97           struct MHD_Connection *connection,
98           const char *url,
99           const char *method,
100           const char *version,
101           const char *upload_data, size_t *upload_data_size,
102           void **unused)
103 {
104   static int ptr;
105   struct MHD_Response *response;
106   int ret;
107
108   if (0 != strcmp ("GET", method))
109     return MHD_NO;              /* unexpected method */
110   if (&ptr != *unused)
111     {
112       *unused = &ptr;
113       return MHD_YES;
114     }
115   *unused = NULL;
116   fprintf (stderr, "MHD sends respose for request to URL `%s'\n", url);
117   response = MHD_create_response_from_buffer (strlen (url),
118                                               (void *) url,
119                                               MHD_RESPMEM_MUST_COPY);
120   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
121   MHD_destroy_response (response);
122   if (ret == MHD_NO)
123     abort ();
124   return ret;
125 }
126
127
128 static void
129 do_shutdown ()
130 {
131   if (mhd_task_id != GNUNET_SCHEDULER_NO_TASK)
132   {
133     GNUNET_SCHEDULER_cancel (mhd_task_id);
134     mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
135   }
136   if (curl_task_id != GNUNET_SCHEDULER_NO_TASK)
137   {
138     GNUNET_SCHEDULER_cancel (curl_task_id);
139     curl_task_id = GNUNET_SCHEDULER_NO_TASK;
140   }
141   if (ctrl_c_task_id != GNUNET_SCHEDULER_NO_TASK)
142   {
143     GNUNET_SCHEDULER_cancel (ctrl_c_task_id);
144     ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
145   }
146   if (NULL != mhd)
147   {
148     MHD_stop_daemon (mhd);
149     mhd = NULL;
150   }
151   if (NULL != rr)
152   {
153     GNUNET_VPN_cancel_request (rr);
154     rr = NULL;
155   }
156   if (NULL != vpn)
157   {
158     GNUNET_VPN_disconnect (vpn);
159     vpn = NULL;
160   }
161   GNUNET_free_non_null (url);
162   url = NULL;
163 }
164
165
166 /**
167  * Function to run the HTTP client.
168  */
169 static void
170 curl_main (void);
171
172
173 static void
174 curl_task (void *cls,
175           const struct GNUNET_SCHEDULER_TaskContext *tc)
176 {
177   curl_task_id = GNUNET_SCHEDULER_NO_TASK;
178   curl_main ();
179 }
180
181
182 static void
183 curl_main ()
184 {
185   fd_set rs;
186   fd_set ws;
187   fd_set es;
188   int max;
189   struct GNUNET_NETWORK_FDSet nrs;
190   struct GNUNET_NETWORK_FDSet nws;
191   struct GNUNET_TIME_Relative delay;
192   long timeout;
193   int running;
194   struct CURLMsg *msg;
195
196   max = 0;
197   FD_ZERO (&rs);
198   FD_ZERO (&ws);
199   FD_ZERO (&es);
200   curl_multi_perform (multi, &running);
201   if (running == 0)
202   {
203     GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
204     if (msg->msg == CURLMSG_DONE)
205     {
206       if (msg->data.result != CURLE_OK)
207       {
208         printf ("%s failed at %s:%d: `%s'\n",
209                 "curl_multi_perform",
210                 __FILE__,
211                 __LINE__, curl_easy_strerror (msg->data.result));
212         global_ret = 1;
213       }
214     }
215     curl_multi_remove_handle (multi, curl);
216     curl_multi_cleanup (multi);
217     curl_easy_cleanup (curl);
218     curl = NULL;
219     multi = NULL;
220     if (cbc.pos != strlen ("/hello_world"))
221       global_ret = 2;
222     if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
223       global_ret = 3;
224     fprintf (stderr, "Download complete, shutting down!\n");
225     do_shutdown ();
226     return;    
227   }
228   GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max)); 
229   if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
230        (-1 == timeout) )
231     delay = GNUNET_TIME_UNIT_SECONDS;
232   else
233     delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
234   GNUNET_NETWORK_fdset_copy_native (&nrs,
235                                     &rs,
236                                     max + 1);
237   GNUNET_NETWORK_fdset_copy_native (&nws,
238                                     &ws,
239                                     max + 1);
240   curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
241                                               GNUNET_SCHEDULER_NO_TASK,
242                                               delay,
243                                               &nrs,
244                                               &nws,
245                                               &curl_task,
246                                               NULL);  
247 }
248
249
250 /**
251  * Callback invoked from the VPN service once a redirection is
252  * available.  Provides the IP address that can now be used to
253  * reach the requested destination (in our case, the MHD server)
254  *
255  * @param cls closure
256  * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
257  *                will match 'result_af' from the request
258  * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
259  *                that the VPN allocated for the redirection;
260  *                traffic to this IP will now be redirected to the 
261  *                specified target peer; NULL on error
262  */
263 static void
264 allocation_cb (void *cls,
265                int af,
266                const void *address)
267 {
268   char ips[INET_ADDRSTRLEN];
269
270   rr = NULL;
271   if (AF_INET != af)
272   {
273     fprintf (stderr, 
274              "VPN failed to allocate appropriate address\n");
275     GNUNET_SCHEDULER_shutdown ();
276     return;
277   }
278   GNUNET_asprintf (&url, 
279                    "http://%s:%u/hello_world",  
280                    inet_ntop (af, address, ips, sizeof (ips)),
281                    (unsigned int) PORT);
282   curl = curl_easy_init ();
283   curl_easy_setopt (curl, CURLOPT_URL, url);
284   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, &copyBuffer);
285   curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
286   curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
287   curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
288   curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
289   curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
290
291   multi = curl_multi_init ();
292   GNUNET_assert (multi != NULL);
293   GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
294
295   fprintf (stderr, "Beginning HTTP download from `%s'\n", url);
296   curl_main ();
297 }
298
299
300 /**
301  * Function to keep the HTTP server running.
302  */
303 static void 
304 mhd_main (void);
305
306
307 static void
308 mhd_task (void *cls,
309           const struct GNUNET_SCHEDULER_TaskContext *tc)
310 {
311   mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
312   MHD_run (mhd);
313   mhd_main ();
314 }
315
316
317 static void
318 ctrl_c_shutdown (void *cls,
319                  const struct GNUNET_SCHEDULER_TaskContext *tc)
320 {
321   ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
322   do_shutdown ();
323   global_ret = 1;
324 }
325
326
327 static void 
328 mhd_main ()
329 {
330   struct GNUNET_NETWORK_FDSet nrs;
331   struct GNUNET_NETWORK_FDSet nws;
332   fd_set rs;
333   fd_set ws;
334   fd_set es;
335   int max_fd;
336   unsigned MHD_LONG_LONG timeout;
337   struct GNUNET_TIME_Relative delay;
338
339   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == mhd_task_id);
340   FD_ZERO (&rs);
341   FD_ZERO (&ws);
342   FD_ZERO (&es);
343   max_fd = -1;
344   GNUNET_assert (MHD_YES ==
345                  MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
346   if (MHD_YES == MHD_get_timeout (mhd, &timeout))
347     delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
348                                            (unsigned int) timeout);
349   else
350     delay = GNUNET_TIME_UNIT_FOREVER_REL;
351   GNUNET_NETWORK_fdset_copy_native (&nrs,
352                                     &rs,
353                                     max_fd + 1);
354   GNUNET_NETWORK_fdset_copy_native (&nws,
355                                     &ws,
356                                     max_fd + 1);
357   mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
358                                              GNUNET_SCHEDULER_NO_TASK,
359                                              delay,
360                                              &nrs,
361                                              &nws,
362                                              &mhd_task,
363                                              NULL);  
364 }
365
366
367 static void
368 run (void *cls, char *const *args, const char *cfgfile,
369      const struct GNUNET_CONFIGURATION_Handle *cfg)
370 {
371   struct in_addr v4;
372
373   vpn = GNUNET_VPN_connect (cfg);
374   GNUNET_assert (NULL != vpn); 
375   mhd = MHD_start_daemon (MHD_USE_DEBUG,
376                           PORT,
377                           NULL, NULL,
378                           &mhd_ahc, NULL,
379                           MHD_OPTION_END);
380   GNUNET_assert (NULL != mhd);
381   mhd_main ();
382   GNUNET_assert (1 == inet_pton (AF_INET, "10.10.1.1", &v4));
383   rr = GNUNET_VPN_redirect_to_ip (vpn,
384                                   AF_INET,
385                                   AF_INET,
386                                   &v4,
387                                   GNUNET_YES,
388                                   GNUNET_TIME_UNIT_FOREVER_ABS,
389                                   &allocation_cb, NULL);
390   ctrl_c_task_id = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
391                                                  &ctrl_c_shutdown,
392                                                  NULL);
393 }
394
395
396 static void
397 setup_peer (struct PeerContext *p, const char *cfgname)
398 {
399   p->cfg = GNUNET_CONFIGURATION_create ();
400 #if START_ARM
401   p->arm_proc =
402       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
403                                "gnunet-service-arm",
404 #if VERBOSE
405                                "-L", "DEBUG",
406 #endif
407                                "-c", cfgname, NULL);
408 #endif
409   GNUNET_assert (NULL != p->arm_proc);
410   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
411 }
412
413
414 static void
415 stop_peer (struct PeerContext *p)
416 {
417 #if START_ARM
418   if (NULL != p->arm_proc)
419   {
420     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
421       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
422     if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
423       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
424     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
425                 GNUNET_OS_process_get_pid (p->arm_proc));
426     GNUNET_OS_process_close (p->arm_proc);
427     p->arm_proc = NULL;
428   }
429 #endif
430   GNUNET_CONFIGURATION_destroy (p->cfg);
431 }
432
433
434 int
435 main (int argc, char *const *argv)
436 {
437   char *const argvx[] = {
438     "test_gnunet_vpn",
439     "-c",
440     "test_gnunet_vpn.conf",
441 #if VERBOSE
442     "-L", "DEBUG",
443 #endif
444     NULL
445   };
446   struct GNUNET_GETOPT_CommandLineOption options[] = {
447     GNUNET_GETOPT_OPTION_END
448   };
449
450   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
451     return 2;
452   setup_peer (&p1, "test_gnunet_vpn.conf");
453   GNUNET_log_setup ("test_gnunet_vpn",
454 #if VERBOSE
455                     "DEBUG",
456 #else
457                     "WARNING",
458 #endif
459                     NULL);
460   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
461                       "test_gnunet_vpn", "nohelp", options, &run, NULL);
462   stop_peer (&p1);
463   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
464   return global_ret;
465 }