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