update velocity always at the end of iteration
[oweals/gnunet.git] / src / pt / test_gnunet_vpn.c
1 /*
2      This file is part of GNUnet
3      Copyright (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 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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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 #if HAVE_CURL_CURL_H
28 #include <curl/curl.h>
29 #elif HAVE_GNURL_CURL_H
30 #include <gnurl/curl.h>
31 #endif
32 #include <microhttpd.h>
33 #include "gnunet_vpn_service.h"
34 #include "gnunet_testing_lib.h"
35
36 #define PORT 48080
37
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
39
40
41 /**
42  * Return value for 'main'.
43  */
44 static int global_ret;
45
46 static struct GNUNET_VPN_Handle *vpn;
47
48 static struct MHD_Daemon *mhd;
49
50 static struct GNUNET_SCHEDULER_Task *mhd_task_id;
51
52 static struct GNUNET_SCHEDULER_Task *curl_task_id;
53
54 static struct GNUNET_SCHEDULER_Task *timeout_task_id;
55
56 static struct GNUNET_VPN_RedirectionRequest *rr;
57
58 static CURL *curl;
59
60 static CURLM *multi;
61
62 static char *url;
63
64 /**
65  * IP address of the ultimate destination.
66  */
67 static const char *dest_ip;
68
69 /**
70  * Address family of the dest_ip.
71  */
72 static int dest_af;
73
74 /**
75  * Address family to use by the curl client.
76  */
77 static int src_af;
78
79
80 struct CBC
81 {
82   char buf[1024];
83   size_t pos;
84 };
85
86 static struct CBC cbc;
87
88
89 static size_t
90 copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
91 {
92   struct CBC *cbc = ctx;
93
94   if (cbc->pos + size * nmemb > sizeof (cbc->buf))
95     return 0;                   /* overflow */
96   GNUNET_memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
97   cbc->pos += size * nmemb;
98   return size * nmemb;
99 }
100
101
102 static int
103 mhd_ahc (void *cls,
104          struct MHD_Connection *connection,
105          const char *url,
106          const char *method,
107          const char *version,
108          const char *upload_data,
109          size_t * upload_data_size,
110          void **unused)
111 {
112   static int ptr;
113   struct MHD_Response *response;
114   int ret;
115
116   if (0 != strcmp ("GET", method))
117     return MHD_NO;              /* unexpected method */
118   if (&ptr != *unused)
119   {
120     *unused = &ptr;
121     return MHD_YES;
122   }
123   *unused = NULL;
124   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125               "MHD sends respose for request to URL `%s'\n", url);
126   response =
127       MHD_create_response_from_buffer (strlen (url), (void *) url,
128                                        MHD_RESPMEM_MUST_COPY);
129   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
130   MHD_destroy_response (response);
131   if (ret == MHD_NO)
132     abort ();
133   return ret;
134 }
135
136
137 static void
138 do_shutdown (void *cls)
139 {
140   if (NULL != mhd_task_id)
141   {
142     GNUNET_SCHEDULER_cancel (mhd_task_id);
143     mhd_task_id = NULL;
144   }
145   if (NULL != curl_task_id)
146   {
147     GNUNET_SCHEDULER_cancel (curl_task_id);
148     curl_task_id = NULL;
149   }
150   if (NULL != timeout_task_id)
151   {
152     GNUNET_SCHEDULER_cancel (timeout_task_id);
153     timeout_task_id = NULL;
154   }
155   if (NULL != mhd)
156   {
157     MHD_stop_daemon (mhd);
158     mhd = NULL;
159   }
160   if (NULL != rr)
161   {
162     GNUNET_VPN_cancel_request (rr);
163     rr = NULL;
164   }
165   if (NULL != vpn)
166   {
167     GNUNET_VPN_disconnect (vpn);
168     vpn = NULL;
169   }
170   GNUNET_free_non_null (url);
171   url = NULL;
172 }
173
174
175 /**
176  * Function to run the HTTP client.
177  */
178 static void
179 curl_main (void *cls)
180 {
181   fd_set rs;
182   fd_set ws;
183   fd_set es;
184   int max;
185   struct GNUNET_NETWORK_FDSet nrs;
186   struct GNUNET_NETWORK_FDSet nws;
187   struct GNUNET_TIME_Relative delay;
188   long timeout;
189   int running;
190   struct CURLMsg *msg;
191
192   curl_task_id = NULL;
193   max = 0;
194   FD_ZERO (&rs);
195   FD_ZERO (&ws);
196   FD_ZERO (&es);
197   curl_multi_perform (multi, &running);
198   if (running == 0)
199   {
200     GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
201     if (msg->msg == CURLMSG_DONE)
202     {
203       if (msg->data.result != CURLE_OK)
204       {
205         fprintf (stderr, "%s failed at %s:%d: `%s'\n", "curl_multi_perform",
206                  __FILE__, __LINE__, curl_easy_strerror (msg->data.result));
207         global_ret = 1;
208       }
209     }
210     curl_multi_remove_handle (multi, curl);
211     curl_multi_cleanup (multi);
212     curl_easy_cleanup (curl);
213     curl = NULL;
214     multi = NULL;
215     if (cbc.pos != strlen ("/hello_world"))
216     {
217       GNUNET_break (0);
218       global_ret = 2;
219     }
220     if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
221     {
222       GNUNET_break (0);
223       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
224                   "You might want to check if your host-based firewall is blocking the connections.\n");
225       global_ret = 3;
226     }
227     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
228     GNUNET_SCHEDULER_shutdown ();
229     return;
230   }
231   GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
232   if ((CURLM_OK != curl_multi_timeout (multi, &timeout)) || (-1 == timeout))
233     delay = GNUNET_TIME_UNIT_SECONDS;
234   else
235     delay =
236         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
237                                        (unsigned int) timeout);
238   GNUNET_NETWORK_fdset_copy_native (&nrs, &rs, max + 1);
239   GNUNET_NETWORK_fdset_copy_native (&nws, &ws, max + 1);
240   curl_task_id =
241       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, delay,
242                                    &nrs, &nws, &curl_main, NULL);
243 }
244
245
246 /**
247  * Callback invoked from the VPN service once a redirection is
248  * available.  Provides the IP address that can now be used to
249  * reach the requested destination (in our case, the MHD server)
250  *
251  * @param cls closure
252  * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
253  *                will match 'result_af' from the request
254  * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
255  *                that the VPN allocated for the redirection;
256  *                traffic to this IP will now be redirected to the
257  *                specified target peer; NULL on error
258  */
259 static void
260 allocation_cb (void *cls, int af, const void *address)
261 {
262   char ips[INET6_ADDRSTRLEN];
263
264   rr = NULL;
265   if (src_af != af)
266   {
267     fprintf (stderr,
268              "VPN failed to allocate appropriate address\n");
269     GNUNET_SCHEDULER_shutdown ();
270     return;
271   }
272   if (AF_INET6 == af)
273     GNUNET_asprintf (&url,
274                      "http://[%s]:%u/hello_world",
275                      inet_ntop (af,
276                                 address,
277                                 ips,
278                                 sizeof (ips)),
279                      (unsigned int) PORT);
280   else
281     GNUNET_asprintf (&url,
282                      "http://%s:%u/hello_world",
283                      inet_ntop (af,
284                                 address,
285                                 ips,
286                                 sizeof (ips)),
287                      (unsigned int) PORT);
288   curl = curl_easy_init ();
289   curl_easy_setopt (curl, CURLOPT_URL, url);
290   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, &copy_buffer);
291   curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
292   curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
293   curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
294   curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
295   curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
296   curl_easy_setopt (curl, CURLOPT_VERBOSE, 0);
297
298   multi = curl_multi_init ();
299   GNUNET_assert (multi != NULL);
300   GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
301   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302               "Beginning HTTP download from `%s'\n",
303               url);
304   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
305                                 &curl_main,
306                                 NULL);
307 }
308
309
310 /**
311  * Function to keep the HTTP server running.
312  */
313 static void
314 mhd_main (void);
315
316
317 static void
318 mhd_task (void *cls)
319 {
320   mhd_task_id = NULL;
321   MHD_run (mhd);
322   mhd_main ();
323 }
324
325
326 static void
327 do_timeout (void *cls)
328 {
329   timeout_task_id = NULL;
330   GNUNET_SCHEDULER_shutdown ();
331   GNUNET_break (0);
332   global_ret = 1;
333 }
334
335
336 static void
337 mhd_main ()
338 {
339   struct GNUNET_NETWORK_FDSet nrs;
340   struct GNUNET_NETWORK_FDSet nws;
341   fd_set rs;
342   fd_set ws;
343   fd_set es;
344   int max_fd;
345   unsigned MHD_LONG_LONG timeout;
346   struct GNUNET_TIME_Relative delay;
347
348   GNUNET_assert (NULL == mhd_task_id);
349   FD_ZERO (&rs);
350   FD_ZERO (&ws);
351   FD_ZERO (&es);
352   max_fd = -1;
353   GNUNET_assert (MHD_YES == MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
354   if (MHD_YES == MHD_get_timeout (mhd, &timeout))
355     delay =
356         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
357                                        (unsigned int) timeout);
358   else
359     delay = GNUNET_TIME_UNIT_FOREVER_REL;
360   GNUNET_NETWORK_fdset_copy_native (&nrs, &rs, max_fd + 1);
361   GNUNET_NETWORK_fdset_copy_native (&nws, &ws, max_fd + 1);
362   mhd_task_id =
363       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, delay,
364                                    &nrs, &nws, &mhd_task, NULL);
365 }
366
367
368 static void
369 run (void *cls,
370      const struct GNUNET_CONFIGURATION_Handle *cfg,
371      struct GNUNET_TESTING_Peer *peer)
372 {
373   struct in_addr v4;
374   struct in6_addr v6;
375   void *addr;
376   enum MHD_FLAG flags;
377
378   vpn = GNUNET_VPN_connect (cfg);
379   GNUNET_assert (NULL != vpn);
380   flags = MHD_USE_DEBUG;
381   if (AF_INET6 == dest_af)
382     flags |= MHD_USE_IPv6;
383   mhd =
384       MHD_start_daemon (flags, PORT, NULL, NULL, &mhd_ahc, NULL,
385                         MHD_OPTION_END);
386
387
388   GNUNET_assert (NULL != mhd);
389   mhd_main ();
390   addr = NULL;
391   switch (dest_af)
392   {
393   case AF_INET:
394     GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v4));
395     addr = &v4;
396     break;
397   case AF_INET6:
398     GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v6));
399     addr = &v6;
400     break;
401   default:
402     GNUNET_assert (0);
403   }
404   rr = GNUNET_VPN_redirect_to_ip (vpn, src_af, dest_af, addr,
405                                   GNUNET_TIME_UNIT_FOREVER_ABS, &allocation_cb,
406                                   NULL);
407   timeout_task_id =
408       GNUNET_SCHEDULER_add_delayed (TIMEOUT,
409                                     &do_timeout,
410                                     NULL);
411   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
412                                  NULL);
413 }
414
415
416 int
417 main (int argc, char *const *argv)
418 {
419   const char *type;
420   const char *bin;
421   char *vpn_binary;
422   char *exit_binary;
423   int ret = 0;
424
425 #ifndef MINGW
426   if (0 != ACCESS ("/dev/net/tun", R_OK))
427   {
428     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
429                               "access",
430                               "/dev/net/tun");
431     fprintf (stderr,
432              "WARNING: System unable to run test, skipping.\n");
433     return 77;
434   }
435 #endif
436   vpn_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-vpn");
437   exit_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-exit");
438   if ((GNUNET_YES != (ret = GNUNET_OS_check_helper_binary (vpn_binary, GNUNET_YES, "-d gnunet-vpn - - 169.1.3.3.7 255.255.255.0"))) || //ipv4 only please!
439       (GNUNET_YES != (ret = GNUNET_OS_check_helper_binary (exit_binary, GNUNET_YES, "-d gnunet-vpn - - - 169.1.3.3.7 255.255.255.0")))) //no nat, ipv4 only
440   {
441     GNUNET_free (vpn_binary);
442     GNUNET_free (exit_binary);
443     fprintf (stderr,
444              "WARNING: gnunet-helper-{exit,vpn} binaries are not SUID, refusing to run test (as it would have to fail). %d\n", ret);
445     return 77;
446   }
447
448   GNUNET_free (vpn_binary);
449   GNUNET_free (exit_binary);
450   bin = argv[0];
451   if (NULL != strstr (bin, "lt-"))
452     bin = strstr (bin, "lt-") + 4;
453   type = strstr (bin, "-");
454   if (NULL == type)
455   {
456     fprintf (stderr,
457              "invalid binary name\n");
458     return 1;
459   }
460   type++;
461   /* on Windows, .exe is suffixed to these binaries,
462    * thus cease comparison after the 6th char.
463    */
464   if (0 == strncmp (type, "4_to_6",6))
465   {
466     dest_ip = "FC5A:04E1:C2BA::1";
467     dest_af = AF_INET6;
468     src_af = AF_INET;
469   }
470   else if (0 == strncmp (type, "6_to_4",6))
471   {
472     dest_ip = "169.254.86.1";
473     dest_af = AF_INET;
474     src_af = AF_INET6;
475   }
476   else if (0 == strncmp (type, "4_over",6))
477   {
478     dest_ip = "169.254.86.1";
479     dest_af = AF_INET;
480     src_af = AF_INET;
481   }
482   else if (0 == strncmp (type, "6_over",6))
483   {
484     dest_ip = "FC5A:04E1:C2BA::1";
485     dest_af = AF_INET6;
486     src_af = AF_INET6;
487   }
488   else
489   {
490     fprintf (stderr, "invalid binary suffix `%s'\n", type);
491     return 1;
492   }
493   if ((GNUNET_OK != GNUNET_NETWORK_test_pf (src_af)) ||
494       (GNUNET_OK != GNUNET_NETWORK_test_pf (dest_af)))
495   {
496     fprintf (stderr,
497              "Required address families not supported by this system, skipping test.\n");
498     return 0;
499   }
500   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
501   {
502     fprintf (stderr, "failed to initialize curl\n");
503     return 2;
504   }
505   if (0 !=
506       GNUNET_TESTING_peer_run ("test-gnunet-vpn", "test_gnunet_vpn.conf", &run,
507                                NULL))
508     return 1;
509   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
510   return global_ret;
511 }
512
513 /* end of test_gnunet_vpn.c */