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)
228 printf ("%s failed at %s:%d: `%s'\n",
229 "curl_multi_perform",
231 __LINE__, curl_easy_strerror (msg->data.result));
235 curl_multi_remove_handle (multi, curl);
236 curl_multi_cleanup (multi);
237 curl_easy_cleanup (curl);
240 if (cbc.pos != strlen ("/hello_world"))
242 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
245 fprintf (stderr, "Download complete, shutting down!\n");
250 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
251 if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
253 delay = GNUNET_TIME_UNIT_SECONDS;
255 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
256 GNUNET_NETWORK_fdset_copy_native (&nrs,
259 GNUNET_NETWORK_fdset_copy_native (&nws,
262 curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
272 * Callback invoked from the VPN service once a redirection is
273 * available. Provides the IP address that can now be used to
274 * reach the requested destination (in our case, the MHD server)
277 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
278 * will match 'result_af' from the request
279 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
280 * that the VPN allocated for the redirection;
281 * traffic to this IP will now be redirected to the
282 * specified target peer; NULL on error
285 allocation_cb (void *cls,
289 char ips[INET6_ADDRSTRLEN];
295 "VPN failed to allocate appropriate address\n");
296 GNUNET_SCHEDULER_shutdown ();
299 GNUNET_asprintf (&url,
300 "http://%s:%u/hello_world",
301 inet_ntop (af, address, ips, sizeof (ips)),
302 (unsigned int) PORT);
303 curl = curl_easy_init ();
304 curl_easy_setopt (curl, CURLOPT_URL, url);
305 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer);
306 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
307 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
308 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
309 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
310 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
312 multi = curl_multi_init ();
313 GNUNET_assert (multi != NULL);
314 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
316 fprintf (stderr, "Beginning HTTP download from `%s'\n", url);
323 * Function to keep the HTTP server running.
331 const struct GNUNET_SCHEDULER_TaskContext *tc)
333 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
340 ctrl_c_shutdown (void *cls,
341 const struct GNUNET_SCHEDULER_TaskContext *tc)
343 ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
352 struct GNUNET_NETWORK_FDSet nrs;
353 struct GNUNET_NETWORK_FDSet nws;
358 unsigned MHD_LONG_LONG timeout;
359 struct GNUNET_TIME_Relative delay;
361 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == mhd_task_id);
366 GNUNET_assert (MHD_YES ==
367 MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
368 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
369 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
370 (unsigned int) timeout);
372 delay = GNUNET_TIME_UNIT_FOREVER_REL;
373 GNUNET_NETWORK_fdset_copy_native (&nrs,
376 GNUNET_NETWORK_fdset_copy_native (&nws,
379 mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
389 run (void *cls, char *const *args, const char *cfgfile,
390 const struct GNUNET_CONFIGURATION_Handle *cfg)
397 vpn = GNUNET_VPN_connect (cfg);
398 GNUNET_assert (NULL != vpn);
399 flags = MHD_USE_DEBUG;
400 if (AF_INET6 == dest_af)
401 flags |= MHD_USE_IPv6;
402 mhd = MHD_start_daemon (flags,
407 GNUNET_assert (NULL != mhd);
413 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v4));
417 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v6));
423 rr = GNUNET_VPN_redirect_to_ip (vpn,
428 GNUNET_TIME_UNIT_FOREVER_ABS,
429 &allocation_cb, NULL);
430 ctrl_c_task_id = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
437 setup_peer (struct PeerContext *p, const char *cfgname)
439 p->cfg = GNUNET_CONFIGURATION_create ();
442 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
443 "gnunet-service-arm",
447 "-c", cfgname, NULL);
449 GNUNET_assert (NULL != p->arm_proc);
450 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
455 stop_peer (struct PeerContext *p)
458 if (NULL != p->arm_proc)
460 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
461 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
462 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
463 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
465 GNUNET_OS_process_get_pid (p->arm_proc));
466 GNUNET_OS_process_close (p->arm_proc);
470 GNUNET_CONFIGURATION_destroy (p->cfg);
475 * Test if the given AF is supported by this system.
478 * @return GNUNET_OK if the AF is supported
485 s = socket (af, SOCK_STREAM, 0);
488 if (EAFNOSUPPORT == errno)
490 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
491 return GNUNET_SYSERR;
500 main (int argc, char *const *argv)
504 char *const argvx[] = {
507 "test_gnunet_vpn.conf",
513 struct GNUNET_GETOPT_CommandLineOption options[] = {
514 GNUNET_GETOPT_OPTION_END
517 if (0 != ACCESS ("/dev/net/tun", R_OK))
519 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
523 "WARNING: System unable to run test, skipping.\n");
527 GNUNET_OS_check_helper_binary ("gnunet-helper-vpn")) ||
529 GNUNET_OS_check_helper_binary ("gnunet-helper-exit")) )
532 "WARNING: gnunet-helper-{exit,vpn} binaries in $PATH are not SUID, refusing to run test (as it would have to fail).\n");
534 "Change $PATH ('.' in $PATH before $GNUNET_PREFIX/bin is problematic) or permissions (run 'make install' as root) to fix this!\n");
537 GNUNET_CRYPTO_setup_hostkey ("test_gnunet_vpn.conf");
539 if (NULL != strstr (bin, "lt-"))
540 bin = strstr (bin, "lt-") + 4;
541 type = strstr (bin, "-");
544 fprintf (stderr, "invalid binary name\n");
548 if (0 == strcmp (type, "4_to_6"))
550 dest_ip = "FC5A:04E1:C2BA::1";
554 else if (0 == strcmp (type, "6_to_4"))
556 dest_ip = "169.254.86.1";
560 else if (0 == strcmp (type, "4_over"))
562 dest_ip = "169.254.86.1";
566 else if (0 == strcmp (type, "6_over"))
568 dest_ip = "FC5A:04E1:C2BA::1";
574 fprintf (stderr, "invalid binary suffix `%s'\n", type);
577 if ( (GNUNET_OK != test_af (src_af)) ||
578 (GNUNET_OK != test_af (dest_af)) )
581 "Required address families not supported by this system, skipping test.\n");
586 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
588 setup_peer (&p1, "test_gnunet_vpn.conf");
589 GNUNET_log_setup ("test_gnunet_vpn",
596 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
597 "test_gnunet_vpn", "nohelp", options, &run, NULL);
599 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
603 /* end of test_gnunet_vpn.c */