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