2 This file is part of GNUnet
3 (C) 2007, 2009, 2011, 2012 Christian Grothoff
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.
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.
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.
22 * @file test_gnunet_vpn.c
23 * @brief testcase for tunneling HTTP over the GNUnet VPN
24 * @author Christian Grothoff
27 #include <curl/curl.h>
28 #include <microhttpd.h>
29 #include "gnunet_vpn_service.h"
30 #include "gnunet_arm_service.h"
34 #define START_ARM GNUNET_YES
36 #define VERBOSE GNUNET_NO
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45)
42 struct GNUNET_CONFIGURATION_Handle *cfg;
43 struct GNUNET_PeerIdentity id;
45 struct GNUNET_OS_Process *arm_proc;
49 static struct PeerContext p1;
52 * Return value for 'main'.
54 static int global_ret;
56 static struct GNUNET_VPN_Handle *vpn;
58 static struct MHD_Daemon *mhd;
60 static GNUNET_SCHEDULER_TaskIdentifier mhd_task_id;
62 static GNUNET_SCHEDULER_TaskIdentifier curl_task_id;
64 static GNUNET_SCHEDULER_TaskIdentifier ctrl_c_task_id;
66 static struct GNUNET_VPN_RedirectionRequest *rr;
75 * IP address of the ultimate destination.
77 static const char *dest_ip;
80 * Address family of the dest_ip.
85 * Address family to use by the curl client.
96 static struct CBC cbc;
101 copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
103 struct CBC *cbc = ctx;
105 if (cbc->pos + size * nmemb > sizeof(cbc->buf))
106 return 0; /* overflow */
107 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
108 cbc->pos += size * nmemb;
115 struct MHD_Connection *connection,
119 const char *upload_data, size_t *upload_data_size,
123 struct MHD_Response *response;
126 if (0 != strcmp ("GET", method))
127 return MHD_NO; /* unexpected method */
135 fprintf (stderr, "MHD sends respose for request to URL `%s'\n", url);
137 response = MHD_create_response_from_buffer (strlen (url),
139 MHD_RESPMEM_MUST_COPY);
140 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
141 MHD_destroy_response (response);
151 if (mhd_task_id != GNUNET_SCHEDULER_NO_TASK)
153 GNUNET_SCHEDULER_cancel (mhd_task_id);
154 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
156 if (curl_task_id != GNUNET_SCHEDULER_NO_TASK)
158 GNUNET_SCHEDULER_cancel (curl_task_id);
159 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
161 if (ctrl_c_task_id != GNUNET_SCHEDULER_NO_TASK)
163 GNUNET_SCHEDULER_cancel (ctrl_c_task_id);
164 ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
168 MHD_stop_daemon (mhd);
173 GNUNET_VPN_cancel_request (rr);
178 GNUNET_VPN_disconnect (vpn);
181 GNUNET_free_non_null (url);
187 * Function to run the HTTP client.
194 curl_task (void *cls,
195 const struct GNUNET_SCHEDULER_TaskContext *tc)
197 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
209 struct GNUNET_NETWORK_FDSet nrs;
210 struct GNUNET_NETWORK_FDSet nws;
211 struct GNUNET_TIME_Relative delay;
220 curl_multi_perform (multi, &running);
223 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
224 if (msg->msg == CURLMSG_DONE)
226 if (msg->data.result != CURLE_OK)
229 "%s failed at %s:%d: `%s'\n",
230 "curl_multi_perform",
232 __LINE__, curl_easy_strerror (msg->data.result));
236 curl_multi_remove_handle (multi, curl);
237 curl_multi_cleanup (multi);
238 curl_easy_cleanup (curl);
241 if (cbc.pos != strlen ("/hello_world"))
246 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
252 fprintf (stderr, "Download complete, shutting down!\n");
257 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
258 if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
260 delay = GNUNET_TIME_UNIT_SECONDS;
262 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
263 GNUNET_NETWORK_fdset_copy_native (&nrs,
266 GNUNET_NETWORK_fdset_copy_native (&nws,
269 curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
279 * Callback invoked from the VPN service once a redirection is
280 * available. Provides the IP address that can now be used to
281 * reach the requested destination (in our case, the MHD server)
284 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
285 * will match 'result_af' from the request
286 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
287 * that the VPN allocated for the redirection;
288 * traffic to this IP will now be redirected to the
289 * specified target peer; NULL on error
292 allocation_cb (void *cls,
296 char ips[INET6_ADDRSTRLEN];
302 "VPN failed to allocate appropriate address\n");
303 GNUNET_SCHEDULER_shutdown ();
306 GNUNET_asprintf (&url,
307 "http://%s:%u/hello_world",
308 inet_ntop (af, address, ips, sizeof (ips)),
309 (unsigned int) PORT);
310 curl = curl_easy_init ();
311 curl_easy_setopt (curl, CURLOPT_URL, url);
312 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer);
313 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
314 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
315 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
316 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
317 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
319 multi = curl_multi_init ();
320 GNUNET_assert (multi != NULL);
321 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
323 fprintf (stderr, "Beginning HTTP download from `%s'\n", url);
330 * Function to keep the HTTP server running.
338 const struct GNUNET_SCHEDULER_TaskContext *tc)
340 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
347 ctrl_c_shutdown (void *cls,
348 const struct GNUNET_SCHEDULER_TaskContext *tc)
350 ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
360 struct GNUNET_NETWORK_FDSet nrs;
361 struct GNUNET_NETWORK_FDSet nws;
366 unsigned MHD_LONG_LONG timeout;
367 struct GNUNET_TIME_Relative delay;
369 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == mhd_task_id);
374 GNUNET_assert (MHD_YES ==
375 MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
376 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
377 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
378 (unsigned int) timeout);
380 delay = GNUNET_TIME_UNIT_FOREVER_REL;
381 GNUNET_NETWORK_fdset_copy_native (&nrs,
384 GNUNET_NETWORK_fdset_copy_native (&nws,
387 mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
397 run (void *cls, char *const *args, const char *cfgfile,
398 const struct GNUNET_CONFIGURATION_Handle *cfg)
405 vpn = GNUNET_VPN_connect (cfg);
406 GNUNET_assert (NULL != vpn);
407 flags = MHD_USE_DEBUG;
408 if (AF_INET6 == dest_af)
409 flags |= MHD_USE_IPv6;
410 mhd = MHD_start_daemon (flags,
415 GNUNET_assert (NULL != mhd);
421 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v4));
425 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v6));
431 rr = GNUNET_VPN_redirect_to_ip (vpn,
436 GNUNET_TIME_UNIT_FOREVER_ABS,
437 &allocation_cb, NULL);
438 ctrl_c_task_id = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
445 setup_peer (struct PeerContext *p, const char *cfgname)
447 p->cfg = GNUNET_CONFIGURATION_create ();
450 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
451 "gnunet-service-arm",
455 "-c", cfgname, NULL);
457 GNUNET_assert (NULL != p->arm_proc);
458 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
463 stop_peer (struct PeerContext *p)
466 if (NULL != p->arm_proc)
468 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
469 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
470 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
471 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
473 GNUNET_OS_process_get_pid (p->arm_proc));
474 GNUNET_OS_process_close (p->arm_proc);
478 GNUNET_CONFIGURATION_destroy (p->cfg);
483 * Test if the given AF is supported by this system.
486 * @return GNUNET_OK if the AF is supported
493 s = socket (af, SOCK_STREAM, 0);
496 if (EAFNOSUPPORT == errno)
498 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
499 return GNUNET_SYSERR;
508 main (int argc, char *const *argv)
512 char *const argvx[] = {
515 "test_gnunet_vpn.conf",
521 struct GNUNET_GETOPT_CommandLineOption options[] = {
522 GNUNET_GETOPT_OPTION_END
525 if (0 != ACCESS ("/dev/net/tun", R_OK))
527 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
531 "WARNING: System unable to run test, skipping.\n");
535 GNUNET_OS_check_helper_binary ("gnunet-helper-vpn")) ||
537 GNUNET_OS_check_helper_binary ("gnunet-helper-exit")) )
540 "WARNING: gnunet-helper-{exit,vpn} binaries in $PATH are not SUID, refusing to run test (as it would have to fail).\n");
542 "Change $PATH ('.' in $PATH before $GNUNET_PREFIX/bin is problematic) or permissions (run 'make install' as root) to fix this!\n");
545 GNUNET_CRYPTO_setup_hostkey ("test_gnunet_vpn.conf");
547 if (NULL != strstr (bin, "lt-"))
548 bin = strstr (bin, "lt-") + 4;
549 type = strstr (bin, "-");
552 fprintf (stderr, "invalid binary name\n");
556 if (0 == strcmp (type, "4_to_6"))
558 dest_ip = "FC5A:04E1:C2BA::1";
562 else if (0 == strcmp (type, "6_to_4"))
564 dest_ip = "169.254.86.1";
568 else if (0 == strcmp (type, "4_over"))
570 dest_ip = "169.254.86.1";
574 else if (0 == strcmp (type, "6_over"))
576 dest_ip = "FC5A:04E1:C2BA::1";
582 fprintf (stderr, "invalid binary suffix `%s'\n", type);
585 if ( (GNUNET_OK != test_af (src_af)) ||
586 (GNUNET_OK != test_af (dest_af)) )
589 "Required address families not supported by this system, skipping test.\n");
594 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
596 fprintf (stderr, "failed to initialize curl\n");
599 setup_peer (&p1, "test_gnunet_vpn.conf");
600 GNUNET_log_setup ("test_gnunet_vpn",
607 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
608 "test_gnunet_vpn", "nohelp", options, &run, NULL);
610 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
614 /* end of test_gnunet_vpn.c */